]>
Commit | Line | Data |
---|---|---|
a723baf1 | 1 | /* C++ Parser. |
4514aa8c | 2 | Copyright (C) 2000, 2001, 2002, 2003, 2004, |
634b5df5 | 3 | 2005, 2007, 2008, 2009 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 | 9 | under the terms of the GNU General Public License as published by |
e77f031d | 10 | the Free Software Foundation; either version 3, or (at your option) |
a723baf1 MM |
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 | ||
e77f031d NC |
18 | You should have received a copy of the GNU General Public License |
19 | along with GCC; see the file COPYING3. If not see | |
20 | <http://www.gnu.org/licenses/>. */ | |
a723baf1 MM |
21 | |
22 | #include "config.h" | |
23 | #include "system.h" | |
24 | #include "coretypes.h" | |
25 | #include "tm.h" | |
26 | #include "dyn-string.h" | |
27 | #include "varray.h" | |
28 | #include "cpplib.h" | |
29 | #include "tree.h" | |
30 | #include "cp-tree.h" | |
31 | #include "c-pragma.h" | |
32 | #include "decl.h" | |
33 | #include "flags.h" | |
34 | #include "diagnostic.h" | |
a723baf1 MM |
35 | #include "toplev.h" |
36 | #include "output.h" | |
62d1db17 | 37 | #include "target.h" |
474eccc6 | 38 | #include "cgraph.h" |
e58a9aa1 | 39 | #include "c-common.h" |
68a607d8 | 40 | #include "plugin.h" |
a723baf1 MM |
41 | |
42 | \f | |
43 | /* The lexer. */ | |
44 | ||
c162c75e MA |
45 | /* The cp_lexer_* routines mediate between the lexer proper (in libcpp |
46 | and c-lex.c) and the C++ parser. */ | |
a723baf1 | 47 | |
d6b418fa SM |
48 | /* A token's value and its associated deferred access checks and |
49 | qualifying scope. */ | |
50 | ||
d1b38208 | 51 | struct GTY(()) tree_check { |
d6b418fa SM |
52 | /* The value associated with the token. */ |
53 | tree value; | |
54 | /* The checks that have been associated with value. */ | |
55 | VEC (deferred_access_check, gc)* checks; | |
56 | /* The token's qualifying scope (used when it is a | |
57 | CPP_NESTED_NAME_SPECIFIER). */ | |
58 | tree qualifying_scope; | |
59 | }; | |
60 | ||
a723baf1 MM |
61 | /* A C++ token. */ |
62 | ||
d1b38208 | 63 | typedef struct GTY (()) cp_token { |
a723baf1 | 64 | /* The kind of token. */ |
df2b750f | 65 | ENUM_BITFIELD (cpp_ttype) type : 8; |
a723baf1 MM |
66 | /* If this token is a keyword, this value indicates which keyword. |
67 | Otherwise, this value is RID_MAX. */ | |
df2b750f | 68 | ENUM_BITFIELD (rid) keyword : 8; |
f4abade9 GB |
69 | /* Token flags. */ |
70 | unsigned char flags; | |
bc4071dd RH |
71 | /* Identifier for the pragma. */ |
72 | ENUM_BITFIELD (pragma_kind) pragma_kind : 6; | |
7d381002 MA |
73 | /* True if this token is from a context where it is implicitly extern "C" */ |
74 | BOOL_BITFIELD implicit_extern_c : 1; | |
91b1ca65 MM |
75 | /* True for a CPP_NAME token that is not a keyword (i.e., for which |
76 | KEYWORD is RID_MAX) iff this name was looked up and found to be | |
77 | ambiguous. An error has already been reported. */ | |
78 | BOOL_BITFIELD ambiguous_p : 1; | |
8f5929e1 JJ |
79 | /* The location at which this token was found. */ |
80 | location_t location; | |
522df488 | 81 | /* The value associated with this token, if any. */ |
d6b418fa SM |
82 | union cp_token_value { |
83 | /* Used for CPP_NESTED_NAME_SPECIFIER and CPP_TEMPLATE_ID. */ | |
84 | struct tree_check* GTY((tag ("1"))) tree_check_value; | |
85 | /* Use for all other tokens. */ | |
86 | tree GTY((tag ("0"))) value; | |
87 | } GTY((desc ("(%1.type == CPP_TEMPLATE_ID) || (%1.type == CPP_NESTED_NAME_SPECIFIER)"))) u; | |
a723baf1 MM |
88 | } cp_token; |
89 | ||
0c5e4866 NS |
90 | /* We use a stack of token pointer for saving token sets. */ |
91 | typedef struct cp_token *cp_token_position; | |
d4e6fecb NS |
92 | DEF_VEC_P (cp_token_position); |
93 | DEF_VEC_ALLOC_P (cp_token_position,heap); | |
0c5e4866 | 94 | |
fa233e34 | 95 | static cp_token eof_token = |
76aebc9f | 96 | { |
8f5929e1 | 97 | CPP_EOF, RID_MAX, 0, PRAGMA_NONE, false, 0, 0, { NULL } |
76aebc9f | 98 | }; |
0c5e4866 | 99 | |
a723baf1 MM |
100 | /* The cp_lexer structure represents the C++ lexer. It is responsible |
101 | for managing the token stream from the preprocessor and supplying | |
c162c75e | 102 | it to the parser. Tokens are never added to the cp_lexer after |
03fd3f84 | 103 | it is created. */ |
a723baf1 | 104 | |
d1b38208 | 105 | typedef struct GTY (()) cp_lexer { |
0c5e4866 NS |
106 | /* The memory allocated for the buffer. NULL if this lexer does not |
107 | own the token buffer. */ | |
76aebc9f NS |
108 | cp_token * GTY ((length ("%h.buffer_length"))) buffer; |
109 | /* If the lexer owns the buffer, this is the number of tokens in the | |
110 | buffer. */ | |
111 | size_t buffer_length; | |
c8094d83 | 112 | |
c162c75e | 113 | /* A pointer just past the last available token. The tokens |
03fd3f84 | 114 | in this lexer are [buffer, last_token). */ |
0c5e4866 | 115 | cp_token_position GTY ((skip)) last_token; |
c162c75e | 116 | |
0c5e4866 | 117 | /* The next available token. If NEXT_TOKEN is &eof_token, then there are |
a723baf1 | 118 | no more available tokens. */ |
0c5e4866 | 119 | cp_token_position GTY ((skip)) next_token; |
a723baf1 MM |
120 | |
121 | /* A stack indicating positions at which cp_lexer_save_tokens was | |
122 | called. The top entry is the most recent position at which we | |
0c5e4866 NS |
123 | began saving tokens. If the stack is non-empty, we are saving |
124 | tokens. */ | |
d4e6fecb | 125 | VEC(cp_token_position,heap) *GTY ((skip)) saved_tokens; |
a723baf1 | 126 | |
bc4071dd RH |
127 | /* The next lexer in a linked list of lexers. */ |
128 | struct cp_lexer *next; | |
129 | ||
a723baf1 MM |
130 | /* True if we should output debugging information. */ |
131 | bool debugging_p; | |
132 | ||
bc4071dd RH |
133 | /* True if we're in the context of parsing a pragma, and should not |
134 | increment past the end-of-line marker. */ | |
135 | bool in_pragma; | |
a723baf1 MM |
136 | } cp_lexer; |
137 | ||
c162c75e MA |
138 | /* cp_token_cache is a range of tokens. There is no need to represent |
139 | allocate heap memory for it, since tokens are never removed from the | |
140 | lexer's array. There is also no need for the GC to walk through | |
141 | a cp_token_cache, since everything in here is referenced through | |
03fd3f84 | 142 | a lexer. */ |
c162c75e | 143 | |
d1b38208 | 144 | typedef struct GTY(()) cp_token_cache { |
03fd3f84 | 145 | /* The beginning of the token range. */ |
c162c75e MA |
146 | cp_token * GTY((skip)) first; |
147 | ||
03fd3f84 | 148 | /* Points immediately after the last token in the range. */ |
c162c75e MA |
149 | cp_token * GTY ((skip)) last; |
150 | } cp_token_cache; | |
151 | ||
a723baf1 MM |
152 | /* Prototypes. */ |
153 | ||
17211ab5 | 154 | static cp_lexer *cp_lexer_new_main |
94edc4ab | 155 | (void); |
a723baf1 | 156 | static cp_lexer *cp_lexer_new_from_tokens |
c162c75e MA |
157 | (cp_token_cache *tokens); |
158 | static void cp_lexer_destroy | |
159 | (cp_lexer *); | |
a723baf1 | 160 | static int cp_lexer_saving_tokens |
94edc4ab | 161 | (const cp_lexer *); |
0c5e4866 NS |
162 | static cp_token_position cp_lexer_token_position |
163 | (cp_lexer *, bool); | |
164 | static cp_token *cp_lexer_token_at | |
165 | (cp_lexer *, cp_token_position); | |
a723baf1 | 166 | static void cp_lexer_get_preprocessor_token |
94edc4ab | 167 | (cp_lexer *, cp_token *); |
c162c75e MA |
168 | static inline cp_token *cp_lexer_peek_token |
169 | (cp_lexer *); | |
a723baf1 | 170 | static cp_token *cp_lexer_peek_nth_token |
94edc4ab | 171 | (cp_lexer *, size_t); |
f7b5ecd9 | 172 | static inline bool cp_lexer_next_token_is |
94edc4ab | 173 | (cp_lexer *, enum cpp_ttype); |
a723baf1 | 174 | static bool cp_lexer_next_token_is_not |
94edc4ab | 175 | (cp_lexer *, enum cpp_ttype); |
a723baf1 | 176 | static bool cp_lexer_next_token_is_keyword |
94edc4ab | 177 | (cp_lexer *, enum rid); |
21526606 | 178 | static cp_token *cp_lexer_consume_token |
94edc4ab | 179 | (cp_lexer *); |
a723baf1 MM |
180 | static void cp_lexer_purge_token |
181 | (cp_lexer *); | |
182 | static void cp_lexer_purge_tokens_after | |
0c5e4866 | 183 | (cp_lexer *, cp_token_position); |
a723baf1 | 184 | static void cp_lexer_save_tokens |
94edc4ab | 185 | (cp_lexer *); |
a723baf1 | 186 | static void cp_lexer_commit_tokens |
94edc4ab | 187 | (cp_lexer *); |
a723baf1 | 188 | static void cp_lexer_rollback_tokens |
94edc4ab | 189 | (cp_lexer *); |
6983ea08 | 190 | #ifdef ENABLE_CHECKING |
a723baf1 | 191 | static void cp_lexer_print_token |
94edc4ab | 192 | (FILE *, cp_token *); |
21526606 | 193 | static inline bool cp_lexer_debugging_p |
94edc4ab | 194 | (cp_lexer *); |
a723baf1 | 195 | static void cp_lexer_start_debugging |
94edc4ab | 196 | (cp_lexer *) ATTRIBUTE_UNUSED; |
a723baf1 | 197 | static void cp_lexer_stop_debugging |
94edc4ab | 198 | (cp_lexer *) ATTRIBUTE_UNUSED; |
6983ea08 | 199 | #else |
2cfe82fe ZW |
200 | /* If we define cp_lexer_debug_stream to NULL it will provoke warnings |
201 | about passing NULL to functions that require non-NULL arguments | |
202 | (fputs, fprintf). It will never be used, so all we need is a value | |
203 | of the right type that's guaranteed not to be NULL. */ | |
204 | #define cp_lexer_debug_stream stdout | |
205 | #define cp_lexer_print_token(str, tok) (void) 0 | |
6983ea08 MA |
206 | #define cp_lexer_debugging_p(lexer) 0 |
207 | #endif /* ENABLE_CHECKING */ | |
a723baf1 | 208 | |
c162c75e MA |
209 | static cp_token_cache *cp_token_cache_new |
210 | (cp_token *, cp_token *); | |
211 | ||
bc4071dd RH |
212 | static void cp_parser_initial_pragma |
213 | (cp_token *); | |
214 | ||
a723baf1 | 215 | /* Manifest constants. */ |
5ff5e6c8 | 216 | #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token)) |
0c5e4866 | 217 | #define CP_SAVED_TOKEN_STACK 5 |
a723baf1 MM |
218 | |
219 | /* A token type for keywords, as opposed to ordinary identifiers. */ | |
220 | #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1)) | |
221 | ||
222 | /* A token type for template-ids. If a template-id is processed while | |
223 | parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token; | |
224 | the value of the CPP_TEMPLATE_ID is whatever was returned by | |
225 | cp_parser_template_id. */ | |
226 | #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1)) | |
227 | ||
228 | /* A token type for nested-name-specifiers. If a | |
229 | nested-name-specifier is processed while parsing tentatively, it is | |
230 | replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the | |
231 | CPP_NESTED_NAME_SPECIFIER is whatever was returned by | |
232 | cp_parser_nested_name_specifier_opt. */ | |
233 | #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1)) | |
234 | ||
235 | /* A token type for tokens that are not tokens at all; these are used | |
c162c75e | 236 | to represent slots in the array where there used to be a token |
03fd3f84 | 237 | that has now been deleted. */ |
b8b94c5b PB |
238 | #define CPP_PURGED ((enum cpp_ttype) (CPP_NESTED_NAME_SPECIFIER + 1)) |
239 | ||
240 | /* The number of token types, including C++-specific ones. */ | |
241 | #define N_CP_TTYPES ((int) (CPP_PURGED + 1)) | |
a723baf1 MM |
242 | |
243 | /* Variables. */ | |
244 | ||
6983ea08 | 245 | #ifdef ENABLE_CHECKING |
a723baf1 MM |
246 | /* The stream to which debugging output should be written. */ |
247 | static FILE *cp_lexer_debug_stream; | |
6983ea08 | 248 | #endif /* ENABLE_CHECKING */ |
a723baf1 | 249 | |
7d882b83 ILT |
250 | /* Nonzero if we are parsing an unevaluated operand: an operand to |
251 | sizeof, typeof, or alignof. */ | |
252 | int cp_unevaluated_operand; | |
253 | ||
17211ab5 GK |
254 | /* Create a new main C++ lexer, the lexer that gets tokens from the |
255 | preprocessor. */ | |
a723baf1 MM |
256 | |
257 | static cp_lexer * | |
17211ab5 | 258 | cp_lexer_new_main (void) |
a723baf1 | 259 | { |
17211ab5 | 260 | cp_token first_token; |
76aebc9f NS |
261 | cp_lexer *lexer; |
262 | cp_token *pos; | |
263 | size_t alloc; | |
264 | size_t space; | |
265 | cp_token *buffer; | |
17211ab5 | 266 | |
bc4071dd RH |
267 | /* It's possible that parsing the first pragma will load a PCH file, |
268 | which is a GC collection point. So we have to do that before | |
269 | allocating any memory. */ | |
270 | cp_parser_initial_pragma (&first_token); | |
c162c75e | 271 | |
18c81520 | 272 | c_common_no_more_pch (); |
a723baf1 MM |
273 | |
274 | /* Allocate the memory. */ | |
99dd239f | 275 | lexer = GGC_CNEW (cp_lexer); |
a723baf1 | 276 | |
c8094d83 | 277 | #ifdef ENABLE_CHECKING |
c162c75e | 278 | /* Initially we are not debugging. */ |
a723baf1 | 279 | lexer->debugging_p = false; |
6983ea08 | 280 | #endif /* ENABLE_CHECKING */ |
d4e6fecb NS |
281 | lexer->saved_tokens = VEC_alloc (cp_token_position, heap, |
282 | CP_SAVED_TOKEN_STACK); | |
c8094d83 | 283 | |
76aebc9f NS |
284 | /* Create the buffer. */ |
285 | alloc = CP_LEXER_BUFFER_SIZE; | |
0ac1b889 | 286 | buffer = GGC_NEWVEC (cp_token, alloc); |
a723baf1 | 287 | |
76aebc9f NS |
288 | /* Put the first token in the buffer. */ |
289 | space = alloc; | |
290 | pos = buffer; | |
291 | *pos = first_token; | |
c8094d83 | 292 | |
03fd3f84 | 293 | /* Get the remaining tokens from the preprocessor. */ |
76aebc9f | 294 | while (pos->type != CPP_EOF) |
c162c75e | 295 | { |
76aebc9f NS |
296 | pos++; |
297 | if (!--space) | |
298 | { | |
299 | space = alloc; | |
300 | alloc *= 2; | |
7767580e | 301 | buffer = GGC_RESIZEVEC (cp_token, buffer, alloc); |
76aebc9f NS |
302 | pos = buffer + space; |
303 | } | |
304 | cp_lexer_get_preprocessor_token (lexer, pos); | |
c162c75e | 305 | } |
76aebc9f NS |
306 | lexer->buffer = buffer; |
307 | lexer->buffer_length = alloc - space; | |
308 | lexer->last_token = pos; | |
fa233e34 | 309 | lexer->next_token = lexer->buffer_length ? buffer : &eof_token; |
c162c75e | 310 | |
178b58b5 JM |
311 | /* Subsequent preprocessor diagnostics should use compiler |
312 | diagnostic functions to get the compiler source location. */ | |
148e4216 | 313 | done_lexing = true; |
178b58b5 | 314 | |
2cfe82fe | 315 | gcc_assert (lexer->next_token->type != CPP_PURGED); |
a723baf1 MM |
316 | return lexer; |
317 | } | |
318 | ||
c162c75e | 319 | /* Create a new lexer whose token stream is primed with the tokens in |
2cfe82fe | 320 | CACHE. When these tokens are exhausted, no new tokens will be read. */ |
a723baf1 MM |
321 | |
322 | static cp_lexer * | |
2cfe82fe | 323 | cp_lexer_new_from_tokens (cp_token_cache *cache) |
a723baf1 | 324 | { |
2cfe82fe ZW |
325 | cp_token *first = cache->first; |
326 | cp_token *last = cache->last; | |
c162c75e | 327 | cp_lexer *lexer = GGC_CNEW (cp_lexer); |
17211ab5 | 328 | |
0c5e4866 | 329 | /* We do not own the buffer. */ |
76aebc9f NS |
330 | lexer->buffer = NULL; |
331 | lexer->buffer_length = 0; | |
fa233e34 | 332 | lexer->next_token = first == last ? &eof_token : first; |
0c5e4866 | 333 | lexer->last_token = last; |
c8094d83 | 334 | |
d4e6fecb NS |
335 | lexer->saved_tokens = VEC_alloc (cp_token_position, heap, |
336 | CP_SAVED_TOKEN_STACK); | |
21526606 | 337 | |
6983ea08 | 338 | #ifdef ENABLE_CHECKING |
c162c75e | 339 | /* Initially we are not debugging. */ |
17211ab5 | 340 | lexer->debugging_p = false; |
c162c75e | 341 | #endif |
a723baf1 | 342 | |
2cfe82fe ZW |
343 | gcc_assert (lexer->next_token->type != CPP_PURGED); |
344 | return lexer; | |
c162c75e MA |
345 | } |
346 | ||
03fd3f84 | 347 | /* Frees all resources associated with LEXER. */ |
c162c75e MA |
348 | |
349 | static void | |
350 | cp_lexer_destroy (cp_lexer *lexer) | |
351 | { | |
0c5e4866 NS |
352 | if (lexer->buffer) |
353 | ggc_free (lexer->buffer); | |
d4e6fecb | 354 | VEC_free (cp_token_position, heap, lexer->saved_tokens); |
c162c75e MA |
355 | ggc_free (lexer); |
356 | } | |
357 | ||
4de8668e | 358 | /* Returns nonzero if debugging information should be output. */ |
a723baf1 | 359 | |
6983ea08 MA |
360 | #ifdef ENABLE_CHECKING |
361 | ||
f7b5ecd9 MM |
362 | static inline bool |
363 | cp_lexer_debugging_p (cp_lexer *lexer) | |
a723baf1 | 364 | { |
f7b5ecd9 MM |
365 | return lexer->debugging_p; |
366 | } | |
367 | ||
6983ea08 MA |
368 | #endif /* ENABLE_CHECKING */ |
369 | ||
0c5e4866 NS |
370 | static inline cp_token_position |
371 | cp_lexer_token_position (cp_lexer *lexer, bool previous_p) | |
a723baf1 | 372 | { |
0c5e4866 | 373 | gcc_assert (!previous_p || lexer->next_token != &eof_token); |
c8094d83 | 374 | |
0c5e4866 | 375 | return lexer->next_token - previous_p; |
a723baf1 MM |
376 | } |
377 | ||
a668c6ad | 378 | static inline cp_token * |
0c5e4866 | 379 | cp_lexer_token_at (cp_lexer *lexer ATTRIBUTE_UNUSED, cp_token_position pos) |
a668c6ad | 380 | { |
0c5e4866 | 381 | return pos; |
a668c6ad MM |
382 | } |
383 | ||
4de8668e | 384 | /* nonzero if we are presently saving tokens. */ |
f7b5ecd9 | 385 | |
0c5e4866 | 386 | static inline int |
94edc4ab | 387 | cp_lexer_saving_tokens (const cp_lexer* lexer) |
f7b5ecd9 | 388 | { |
0c5e4866 | 389 | return VEC_length (cp_token_position, lexer->saved_tokens) != 0; |
a723baf1 MM |
390 | } |
391 | ||
76aebc9f | 392 | /* Store the next token from the preprocessor in *TOKEN. Return true |
46c2514e TT |
393 | if we reach EOF. If LEXER is NULL, assume we are handling an |
394 | initial #pragma pch_preprocess, and thus want the lexer to return | |
395 | processed strings. */ | |
a723baf1 | 396 | |
21526606 | 397 | static void |
46c2514e | 398 | cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token) |
a723baf1 | 399 | { |
7d381002 | 400 | static int is_extern_c = 0; |
a723baf1 | 401 | |
51e63e60 | 402 | /* Get a new token from the preprocessor. */ |
b68b6828 | 403 | token->type |
46c2514e TT |
404 | = c_lex_with_flags (&token->u.value, &token->location, &token->flags, |
405 | lexer == NULL ? 0 : C_LEX_RAW_STRINGS); | |
bc4071dd RH |
406 | token->keyword = RID_MAX; |
407 | token->pragma_kind = PRAGMA_NONE; | |
a723baf1 | 408 | |
c8094d83 | 409 | /* On some systems, some header files are surrounded by an |
7d381002 | 410 | implicit extern "C" block. Set a flag in the token if it |
03fd3f84 | 411 | comes from such a header. */ |
7d381002 MA |
412 | is_extern_c += pending_lang_change; |
413 | pending_lang_change = 0; | |
414 | token->implicit_extern_c = is_extern_c > 0; | |
415 | ||
a723baf1 | 416 | /* Check to see if this token is a keyword. */ |
91b1ca65 | 417 | if (token->type == CPP_NAME) |
a723baf1 | 418 | { |
d6b418fa | 419 | if (C_IS_RESERVED_WORD (token->u.value)) |
91b1ca65 MM |
420 | { |
421 | /* Mark this token as a keyword. */ | |
422 | token->type = CPP_KEYWORD; | |
423 | /* Record which keyword. */ | |
d6b418fa | 424 | token->keyword = C_RID_CODE (token->u.value); |
91b1ca65 MM |
425 | } |
426 | else | |
37edf0a6 | 427 | { |
54654d29 DG |
428 | if (warn_cxx0x_compat |
429 | && C_RID_CODE (token->u.value) >= RID_FIRST_CXX0X | |
430 | && C_RID_CODE (token->u.value) <= RID_LAST_CXX0X) | |
431 | { | |
432 | /* Warn about the C++0x keyword (but still treat it as | |
433 | an identifier). */ | |
f1283f8f | 434 | warning (OPT_Wc__0x_compat, |
f41c4af3 JM |
435 | "identifier %qE will become a keyword in C++0x", |
436 | token->u.value); | |
54654d29 DG |
437 | |
438 | /* Clear out the C_RID_CODE so we don't warn about this | |
439 | particular identifier-turned-keyword again. */ | |
d75d71e0 | 440 | C_SET_RID_CODE (token->u.value, RID_MAX); |
54654d29 DG |
441 | } |
442 | ||
37edf0a6 MM |
443 | token->ambiguous_p = false; |
444 | token->keyword = RID_MAX; | |
445 | } | |
a723baf1 | 446 | } |
e58a9aa1 ZL |
447 | /* Handle Objective-C++ keywords. */ |
448 | else if (token->type == CPP_AT_NAME) | |
449 | { | |
450 | token->type = CPP_KEYWORD; | |
d6b418fa | 451 | switch (C_RID_CODE (token->u.value)) |
e58a9aa1 ZL |
452 | { |
453 | /* Map 'class' to '@class', 'private' to '@private', etc. */ | |
454 | case RID_CLASS: token->keyword = RID_AT_CLASS; break; | |
455 | case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break; | |
456 | case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break; | |
457 | case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break; | |
458 | case RID_THROW: token->keyword = RID_AT_THROW; break; | |
459 | case RID_TRY: token->keyword = RID_AT_TRY; break; | |
460 | case RID_CATCH: token->keyword = RID_AT_CATCH; break; | |
d6b418fa | 461 | default: token->keyword = C_RID_CODE (token->u.value); |
e58a9aa1 ZL |
462 | } |
463 | } | |
bc4071dd RH |
464 | else if (token->type == CPP_PRAGMA) |
465 | { | |
466 | /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */ | |
32e8bb8e ILT |
467 | token->pragma_kind = ((enum pragma_kind) |
468 | TREE_INT_CST_LOW (token->u.value)); | |
d6b418fa | 469 | token->u.value = NULL_TREE; |
bc4071dd | 470 | } |
a723baf1 MM |
471 | } |
472 | ||
aa14403d | 473 | /* Update the globals input_location and the input file stack from TOKEN. */ |
2cfe82fe ZW |
474 | static inline void |
475 | cp_lexer_set_source_position_from_token (cp_token *token) | |
476 | { | |
477 | if (token->type != CPP_EOF) | |
478 | { | |
479 | input_location = token->location; | |
2cfe82fe ZW |
480 | } |
481 | } | |
482 | ||
a723baf1 MM |
483 | /* Return a pointer to the next token in the token stream, but do not |
484 | consume it. */ | |
485 | ||
c162c75e MA |
486 | static inline cp_token * |
487 | cp_lexer_peek_token (cp_lexer *lexer) | |
a723baf1 | 488 | { |
a723baf1 | 489 | if (cp_lexer_debugging_p (lexer)) |
0c5e4866 NS |
490 | { |
491 | fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream); | |
492 | cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token); | |
493 | putc ('\n', cp_lexer_debug_stream); | |
494 | } | |
2cfe82fe | 495 | return lexer->next_token; |
a723baf1 MM |
496 | } |
497 | ||
498 | /* Return true if the next token has the indicated TYPE. */ | |
499 | ||
2cfe82fe | 500 | static inline bool |
94edc4ab | 501 | cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type) |
a723baf1 | 502 | { |
2cfe82fe | 503 | return cp_lexer_peek_token (lexer)->type == type; |
a723baf1 MM |
504 | } |
505 | ||
506 | /* Return true if the next token does not have the indicated TYPE. */ | |
507 | ||
2cfe82fe | 508 | static inline bool |
94edc4ab | 509 | cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type) |
a723baf1 MM |
510 | { |
511 | return !cp_lexer_next_token_is (lexer, type); | |
512 | } | |
513 | ||
514 | /* Return true if the next token is the indicated KEYWORD. */ | |
515 | ||
2cfe82fe | 516 | static inline bool |
94edc4ab | 517 | cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword) |
a723baf1 | 518 | { |
cba43cb6 | 519 | return cp_lexer_peek_token (lexer)->keyword == keyword; |
a723baf1 MM |
520 | } |
521 | ||
09357846 JM |
522 | /* Return true if the next token is not the indicated KEYWORD. */ |
523 | ||
524 | static inline bool | |
525 | cp_lexer_next_token_is_not_keyword (cp_lexer* lexer, enum rid keyword) | |
526 | { | |
527 | return cp_lexer_peek_token (lexer)->keyword != keyword; | |
528 | } | |
529 | ||
4ac6f4c5 MM |
530 | /* Return true if the next token is a keyword for a decl-specifier. */ |
531 | ||
d19b84e9 MM |
532 | static bool |
533 | cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer) | |
534 | { | |
535 | cp_token *token; | |
536 | ||
537 | token = cp_lexer_peek_token (lexer); | |
538 | switch (token->keyword) | |
539 | { | |
4dbc8575 DG |
540 | /* auto specifier: storage-class-specifier in C++, |
541 | simple-type-specifier in C++0x. */ | |
d19b84e9 | 542 | case RID_AUTO: |
4dbc8575 | 543 | /* Storage classes. */ |
d19b84e9 MM |
544 | case RID_REGISTER: |
545 | case RID_STATIC: | |
546 | case RID_EXTERN: | |
547 | case RID_MUTABLE: | |
548 | case RID_THREAD: | |
549 | /* Elaborated type specifiers. */ | |
550 | case RID_ENUM: | |
551 | case RID_CLASS: | |
552 | case RID_STRUCT: | |
553 | case RID_UNION: | |
554 | case RID_TYPENAME: | |
555 | /* Simple type specifiers. */ | |
556 | case RID_CHAR: | |
b6baa67d KVH |
557 | case RID_CHAR16: |
558 | case RID_CHAR32: | |
d19b84e9 MM |
559 | case RID_WCHAR: |
560 | case RID_BOOL: | |
561 | case RID_SHORT: | |
562 | case RID_INT: | |
563 | case RID_LONG: | |
564 | case RID_SIGNED: | |
565 | case RID_UNSIGNED: | |
566 | case RID_FLOAT: | |
567 | case RID_DOUBLE: | |
568 | case RID_VOID: | |
569 | /* GNU extensions. */ | |
570 | case RID_ATTRIBUTE: | |
571 | case RID_TYPEOF: | |
3ad6a8e1 DG |
572 | /* C++0x extensions. */ |
573 | case RID_DECLTYPE: | |
d19b84e9 MM |
574 | return true; |
575 | ||
576 | default: | |
577 | return false; | |
578 | } | |
579 | } | |
580 | ||
a723baf1 | 581 | /* Return a pointer to the Nth token in the token stream. If N is 1, |
2cfe82fe ZW |
582 | then this is precisely equivalent to cp_lexer_peek_token (except |
583 | that it is not inline). One would like to disallow that case, but | |
584 | there is one case (cp_parser_nth_token_starts_template_id) where | |
585 | the caller passes a variable for N and it might be 1. */ | |
a723baf1 MM |
586 | |
587 | static cp_token * | |
94edc4ab | 588 | cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n) |
a723baf1 MM |
589 | { |
590 | cp_token *token; | |
591 | ||
592 | /* N is 1-based, not zero-based. */ | |
863a3314 | 593 | gcc_assert (n > 0); |
3db45ab5 | 594 | |
2cfe82fe ZW |
595 | if (cp_lexer_debugging_p (lexer)) |
596 | fprintf (cp_lexer_debug_stream, | |
597 | "cp_lexer: peeking ahead %ld at token: ", (long)n); | |
598 | ||
c162c75e | 599 | --n; |
a723baf1 | 600 | token = lexer->next_token; |
863a3314 | 601 | gcc_assert (!n || token != &eof_token); |
c162c75e | 602 | while (n != 0) |
a723baf1 | 603 | { |
c162c75e | 604 | ++token; |
0c5e4866 NS |
605 | if (token == lexer->last_token) |
606 | { | |
fa233e34 | 607 | token = &eof_token; |
0c5e4866 NS |
608 | break; |
609 | } | |
c8094d83 | 610 | |
c162c75e MA |
611 | if (token->type != CPP_PURGED) |
612 | --n; | |
a723baf1 MM |
613 | } |
614 | ||
2cfe82fe ZW |
615 | if (cp_lexer_debugging_p (lexer)) |
616 | { | |
617 | cp_lexer_print_token (cp_lexer_debug_stream, token); | |
618 | putc ('\n', cp_lexer_debug_stream); | |
619 | } | |
620 | ||
a723baf1 MM |
621 | return token; |
622 | } | |
623 | ||
2cfe82fe ZW |
624 | /* Return the next token, and advance the lexer's next_token pointer |
625 | to point to the next non-purged token. */ | |
a723baf1 MM |
626 | |
627 | static cp_token * | |
94edc4ab | 628 | cp_lexer_consume_token (cp_lexer* lexer) |
a723baf1 | 629 | { |
2cfe82fe | 630 | cp_token *token = lexer->next_token; |
a723baf1 | 631 | |
0c5e4866 | 632 | gcc_assert (token != &eof_token); |
bc4071dd | 633 | gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL); |
c8094d83 | 634 | |
2cfe82fe | 635 | do |
0c5e4866 NS |
636 | { |
637 | lexer->next_token++; | |
638 | if (lexer->next_token == lexer->last_token) | |
639 | { | |
fa233e34 | 640 | lexer->next_token = &eof_token; |
0c5e4866 NS |
641 | break; |
642 | } | |
c8094d83 | 643 | |
0c5e4866 | 644 | } |
2cfe82fe | 645 | while (lexer->next_token->type == CPP_PURGED); |
c8094d83 | 646 | |
2cfe82fe | 647 | cp_lexer_set_source_position_from_token (token); |
c8094d83 | 648 | |
a723baf1 MM |
649 | /* Provide debugging output. */ |
650 | if (cp_lexer_debugging_p (lexer)) | |
651 | { | |
2cfe82fe | 652 | fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream); |
a723baf1 | 653 | cp_lexer_print_token (cp_lexer_debug_stream, token); |
2cfe82fe | 654 | putc ('\n', cp_lexer_debug_stream); |
a723baf1 | 655 | } |
c8094d83 | 656 | |
a723baf1 MM |
657 | return token; |
658 | } | |
659 | ||
2cfe82fe ZW |
660 | /* Permanently remove the next token from the token stream, and |
661 | advance the next_token pointer to refer to the next non-purged | |
662 | token. */ | |
a723baf1 MM |
663 | |
664 | static void | |
665 | cp_lexer_purge_token (cp_lexer *lexer) | |
666 | { | |
c162c75e | 667 | cp_token *tok = lexer->next_token; |
c8094d83 | 668 | |
0c5e4866 | 669 | gcc_assert (tok != &eof_token); |
c162c75e MA |
670 | tok->type = CPP_PURGED; |
671 | tok->location = UNKNOWN_LOCATION; | |
d6b418fa | 672 | tok->u.value = NULL_TREE; |
c162c75e | 673 | tok->keyword = RID_MAX; |
2cfe82fe ZW |
674 | |
675 | do | |
0c5e4866 NS |
676 | { |
677 | tok++; | |
678 | if (tok == lexer->last_token) | |
679 | { | |
fa233e34 | 680 | tok = &eof_token; |
0c5e4866 NS |
681 | break; |
682 | } | |
683 | } | |
684 | while (tok->type == CPP_PURGED); | |
685 | lexer->next_token = tok; | |
a723baf1 MM |
686 | } |
687 | ||
c162c75e | 688 | /* Permanently remove all tokens after TOK, up to, but not |
a723baf1 MM |
689 | including, the token that will be returned next by |
690 | cp_lexer_peek_token. */ | |
691 | ||
692 | static void | |
c162c75e | 693 | cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok) |
a723baf1 | 694 | { |
0c5e4866 | 695 | cp_token *peek = lexer->next_token; |
a723baf1 | 696 | |
0c5e4866 NS |
697 | if (peek == &eof_token) |
698 | peek = lexer->last_token; | |
c8094d83 | 699 | |
c162c75e MA |
700 | gcc_assert (tok < peek); |
701 | ||
702 | for ( tok += 1; tok != peek; tok += 1) | |
a723baf1 | 703 | { |
c162c75e MA |
704 | tok->type = CPP_PURGED; |
705 | tok->location = UNKNOWN_LOCATION; | |
d6b418fa | 706 | tok->u.value = NULL_TREE; |
c162c75e | 707 | tok->keyword = RID_MAX; |
a723baf1 | 708 | } |
c162c75e MA |
709 | } |
710 | ||
a723baf1 MM |
711 | /* Begin saving tokens. All tokens consumed after this point will be |
712 | preserved. */ | |
713 | ||
714 | static void | |
94edc4ab | 715 | cp_lexer_save_tokens (cp_lexer* lexer) |
a723baf1 MM |
716 | { |
717 | /* Provide debugging output. */ | |
718 | if (cp_lexer_debugging_p (lexer)) | |
719 | fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n"); | |
720 | ||
d4e6fecb NS |
721 | VEC_safe_push (cp_token_position, heap, |
722 | lexer->saved_tokens, lexer->next_token); | |
a723baf1 MM |
723 | } |
724 | ||
725 | /* Commit to the portion of the token stream most recently saved. */ | |
726 | ||
727 | static void | |
94edc4ab | 728 | cp_lexer_commit_tokens (cp_lexer* lexer) |
a723baf1 MM |
729 | { |
730 | /* Provide debugging output. */ | |
731 | if (cp_lexer_debugging_p (lexer)) | |
732 | fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n"); | |
733 | ||
0c5e4866 | 734 | VEC_pop (cp_token_position, lexer->saved_tokens); |
a723baf1 MM |
735 | } |
736 | ||
737 | /* Return all tokens saved since the last call to cp_lexer_save_tokens | |
738 | to the token stream. Stop saving tokens. */ | |
739 | ||
740 | static void | |
94edc4ab | 741 | cp_lexer_rollback_tokens (cp_lexer* lexer) |
a723baf1 | 742 | { |
a723baf1 MM |
743 | /* Provide debugging output. */ |
744 | if (cp_lexer_debugging_p (lexer)) | |
745 | fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n"); | |
746 | ||
0c5e4866 | 747 | lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens); |
a723baf1 MM |
748 | } |
749 | ||
a723baf1 MM |
750 | /* Print a representation of the TOKEN on the STREAM. */ |
751 | ||
6983ea08 MA |
752 | #ifdef ENABLE_CHECKING |
753 | ||
a723baf1 | 754 | static void |
c162c75e MA |
755 | cp_lexer_print_token (FILE * stream, cp_token *token) |
756 | { | |
757 | /* We don't use cpp_type2name here because the parser defines | |
758 | a few tokens of its own. */ | |
759 | static const char *const token_names[] = { | |
760 | /* cpplib-defined token types */ | |
761 | #define OP(e, s) #e, | |
762 | #define TK(e, s) #e, | |
763 | TTYPE_TABLE | |
764 | #undef OP | |
765 | #undef TK | |
766 | /* C++ parser token types - see "Manifest constants", above. */ | |
767 | "KEYWORD", | |
768 | "TEMPLATE_ID", | |
769 | "NESTED_NAME_SPECIFIER", | |
770 | "PURGED" | |
771 | }; | |
c8094d83 | 772 | |
c162c75e MA |
773 | /* If we have a name for the token, print it out. Otherwise, we |
774 | simply give the numeric code. */ | |
775 | gcc_assert (token->type < ARRAY_SIZE(token_names)); | |
776 | fputs (token_names[token->type], stream); | |
a723baf1 | 777 | |
c162c75e | 778 | /* For some tokens, print the associated data. */ |
a723baf1 MM |
779 | switch (token->type) |
780 | { | |
c162c75e MA |
781 | case CPP_KEYWORD: |
782 | /* Some keywords have a value that is not an IDENTIFIER_NODE. | |
783 | For example, `struct' is mapped to an INTEGER_CST. */ | |
d6b418fa | 784 | if (TREE_CODE (token->u.value) != IDENTIFIER_NODE) |
c162c75e MA |
785 | break; |
786 | /* else fall through */ | |
a723baf1 | 787 | case CPP_NAME: |
d6b418fa | 788 | fputs (IDENTIFIER_POINTER (token->u.value), stream); |
a723baf1 MM |
789 | break; |
790 | ||
c162c75e | 791 | case CPP_STRING: |
b6baa67d KVH |
792 | case CPP_STRING16: |
793 | case CPP_STRING32: | |
c162c75e | 794 | case CPP_WSTRING: |
d6b418fa | 795 | fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value)); |
a723baf1 MM |
796 | break; |
797 | ||
a723baf1 MM |
798 | default: |
799 | break; | |
800 | } | |
a723baf1 MM |
801 | } |
802 | ||
a723baf1 MM |
803 | /* Start emitting debugging information. */ |
804 | ||
805 | static void | |
94edc4ab | 806 | cp_lexer_start_debugging (cp_lexer* lexer) |
a723baf1 | 807 | { |
81122c44 | 808 | lexer->debugging_p = true; |
a723baf1 | 809 | } |
21526606 | 810 | |
a723baf1 MM |
811 | /* Stop emitting debugging information. */ |
812 | ||
813 | static void | |
94edc4ab | 814 | cp_lexer_stop_debugging (cp_lexer* lexer) |
a723baf1 | 815 | { |
81122c44 | 816 | lexer->debugging_p = false; |
a723baf1 MM |
817 | } |
818 | ||
6983ea08 MA |
819 | #endif /* ENABLE_CHECKING */ |
820 | ||
03fd3f84 | 821 | /* Create a new cp_token_cache, representing a range of tokens. */ |
c162c75e MA |
822 | |
823 | static cp_token_cache * | |
824 | cp_token_cache_new (cp_token *first, cp_token *last) | |
825 | { | |
826 | cp_token_cache *cache = GGC_NEW (cp_token_cache); | |
827 | cache->first = first; | |
828 | cache->last = last; | |
829 | return cache; | |
830 | } | |
831 | ||
a723baf1 | 832 | \f |
62d1db17 MM |
833 | /* Decl-specifiers. */ |
834 | ||
62d1db17 MM |
835 | /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */ |
836 | ||
837 | static void | |
838 | clear_decl_specs (cp_decl_specifier_seq *decl_specs) | |
839 | { | |
840 | memset (decl_specs, 0, sizeof (cp_decl_specifier_seq)); | |
841 | } | |
842 | ||
058b15c1 MM |
843 | /* Declarators. */ |
844 | ||
845 | /* Nothing other than the parser should be creating declarators; | |
846 | declarators are a semi-syntactic representation of C++ entities. | |
847 | Other parts of the front end that need to create entities (like | |
848 | VAR_DECLs or FUNCTION_DECLs) should do that directly. */ | |
849 | ||
98ca843c | 850 | static cp_declarator *make_call_declarator |
b344d949 | 851 | (cp_declarator *, tree, cp_cv_quals, tree, tree); |
98ca843c | 852 | static cp_declarator *make_array_declarator |
058b15c1 | 853 | (cp_declarator *, tree); |
98ca843c | 854 | static cp_declarator *make_pointer_declarator |
3c01e5df | 855 | (cp_cv_quals, cp_declarator *); |
98ca843c | 856 | static cp_declarator *make_reference_declarator |
8af2fec4 | 857 | (cp_cv_quals, cp_declarator *, bool); |
98ca843c | 858 | static cp_parameter_declarator *make_parameter_declarator |
62d1db17 | 859 | (cp_decl_specifier_seq *, cp_declarator *, tree); |
98ca843c | 860 | static cp_declarator *make_ptrmem_declarator |
3c01e5df | 861 | (cp_cv_quals, tree, cp_declarator *); |
058b15c1 | 862 | |
fa6098f8 MM |
863 | /* An erroneous declarator. */ |
864 | static cp_declarator *cp_error_declarator; | |
058b15c1 MM |
865 | |
866 | /* The obstack on which declarators and related data structures are | |
867 | allocated. */ | |
868 | static struct obstack declarator_obstack; | |
869 | ||
870 | /* Alloc BYTES from the declarator memory pool. */ | |
871 | ||
872 | static inline void * | |
873 | alloc_declarator (size_t bytes) | |
874 | { | |
875 | return obstack_alloc (&declarator_obstack, bytes); | |
876 | } | |
877 | ||
878 | /* Allocate a declarator of the indicated KIND. Clear fields that are | |
879 | common to all declarators. */ | |
880 | ||
881 | static cp_declarator * | |
882 | make_declarator (cp_declarator_kind kind) | |
883 | { | |
884 | cp_declarator *declarator; | |
885 | ||
886 | declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator)); | |
887 | declarator->kind = kind; | |
888 | declarator->attributes = NULL_TREE; | |
889 | declarator->declarator = NULL; | |
5d80a306 | 890 | declarator->parameter_pack_p = false; |
058b15c1 MM |
891 | |
892 | return declarator; | |
893 | } | |
894 | ||
d85d3d57 MM |
895 | /* Make a declarator for a generalized identifier. If |
896 | QUALIFYING_SCOPE is non-NULL, the identifier is | |
897 | QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just | |
898 | UNQUALIFIED_NAME. SFK indicates the kind of special function this | |
899 | is, if any. */ | |
058b15c1 | 900 | |
1d786913 | 901 | static cp_declarator * |
d85d3d57 MM |
902 | make_id_declarator (tree qualifying_scope, tree unqualified_name, |
903 | special_function_kind sfk) | |
058b15c1 MM |
904 | { |
905 | cp_declarator *declarator; | |
98ca843c | 906 | |
1d786913 MM |
907 | /* It is valid to write: |
908 | ||
909 | class C { void f(); }; | |
910 | typedef C D; | |
911 | void D::f(); | |
912 | ||
913 | The standard is not clear about whether `typedef const C D' is | |
914 | legal; as of 2002-09-15 the committee is considering that | |
915 | question. EDG 3.0 allows that syntax. Therefore, we do as | |
916 | well. */ | |
917 | if (qualifying_scope && TYPE_P (qualifying_scope)) | |
918 | qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope); | |
919 | ||
d85d3d57 MM |
920 | gcc_assert (TREE_CODE (unqualified_name) == IDENTIFIER_NODE |
921 | || TREE_CODE (unqualified_name) == BIT_NOT_EXPR | |
922 | || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR); | |
923 | ||
058b15c1 | 924 | declarator = make_declarator (cdk_id); |
1d786913 MM |
925 | declarator->u.id.qualifying_scope = qualifying_scope; |
926 | declarator->u.id.unqualified_name = unqualified_name; | |
d85d3d57 | 927 | declarator->u.id.sfk = sfk; |
5d80a306 | 928 | |
058b15c1 MM |
929 | return declarator; |
930 | } | |
931 | ||
932 | /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list | |
933 | of modifiers such as const or volatile to apply to the pointer | |
934 | type, represented as identifiers. */ | |
935 | ||
936 | cp_declarator * | |
3c01e5df | 937 | make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target) |
058b15c1 MM |
938 | { |
939 | cp_declarator *declarator; | |
940 | ||
941 | declarator = make_declarator (cdk_pointer); | |
942 | declarator->declarator = target; | |
943 | declarator->u.pointer.qualifiers = cv_qualifiers; | |
944 | declarator->u.pointer.class_type = NULL_TREE; | |
5d80a306 DG |
945 | if (target) |
946 | { | |
947 | declarator->parameter_pack_p = target->parameter_pack_p; | |
948 | target->parameter_pack_p = false; | |
949 | } | |
950 | else | |
951 | declarator->parameter_pack_p = false; | |
058b15c1 MM |
952 | |
953 | return declarator; | |
954 | } | |
955 | ||
956 | /* Like make_pointer_declarator -- but for references. */ | |
957 | ||
958 | cp_declarator * | |
8af2fec4 RY |
959 | make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target, |
960 | bool rvalue_ref) | |
058b15c1 MM |
961 | { |
962 | cp_declarator *declarator; | |
963 | ||
964 | declarator = make_declarator (cdk_reference); | |
965 | declarator->declarator = target; | |
8af2fec4 RY |
966 | declarator->u.reference.qualifiers = cv_qualifiers; |
967 | declarator->u.reference.rvalue_ref = rvalue_ref; | |
5d80a306 DG |
968 | if (target) |
969 | { | |
970 | declarator->parameter_pack_p = target->parameter_pack_p; | |
971 | target->parameter_pack_p = false; | |
972 | } | |
973 | else | |
974 | declarator->parameter_pack_p = false; | |
058b15c1 MM |
975 | |
976 | return declarator; | |
977 | } | |
978 | ||
979 | /* Like make_pointer_declarator -- but for a pointer to a non-static | |
980 | member of CLASS_TYPE. */ | |
981 | ||
982 | cp_declarator * | |
3c01e5df | 983 | make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type, |
058b15c1 MM |
984 | cp_declarator *pointee) |
985 | { | |
986 | cp_declarator *declarator; | |
987 | ||
988 | declarator = make_declarator (cdk_ptrmem); | |
989 | declarator->declarator = pointee; | |
990 | declarator->u.pointer.qualifiers = cv_qualifiers; | |
991 | declarator->u.pointer.class_type = class_type; | |
992 | ||
5d80a306 DG |
993 | if (pointee) |
994 | { | |
995 | declarator->parameter_pack_p = pointee->parameter_pack_p; | |
996 | pointee->parameter_pack_p = false; | |
997 | } | |
998 | else | |
999 | declarator->parameter_pack_p = false; | |
1000 | ||
058b15c1 MM |
1001 | return declarator; |
1002 | } | |
1003 | ||
1004 | /* Make a declarator for the function given by TARGET, with the | |
1005 | indicated PARMS. The CV_QUALIFIERS aply to the function, as in | |
1006 | "const"-qualified member function. The EXCEPTION_SPECIFICATION | |
1007 | indicates what exceptions can be thrown. */ | |
1008 | ||
1009 | cp_declarator * | |
98ca843c | 1010 | make_call_declarator (cp_declarator *target, |
b344d949 | 1011 | tree parms, |
3c01e5df | 1012 | cp_cv_quals cv_qualifiers, |
f57ca1ea JM |
1013 | tree exception_specification, |
1014 | tree late_return_type) | |
058b15c1 MM |
1015 | { |
1016 | cp_declarator *declarator; | |
1017 | ||
1018 | declarator = make_declarator (cdk_function); | |
1019 | declarator->declarator = target; | |
1020 | declarator->u.function.parameters = parms; | |
1021 | declarator->u.function.qualifiers = cv_qualifiers; | |
1022 | declarator->u.function.exception_specification = exception_specification; | |
f57ca1ea | 1023 | declarator->u.function.late_return_type = late_return_type; |
5d80a306 DG |
1024 | if (target) |
1025 | { | |
1026 | declarator->parameter_pack_p = target->parameter_pack_p; | |
1027 | target->parameter_pack_p = false; | |
1028 | } | |
1029 | else | |
1030 | declarator->parameter_pack_p = false; | |
058b15c1 MM |
1031 | |
1032 | return declarator; | |
1033 | } | |
1034 | ||
1035 | /* Make a declarator for an array of BOUNDS elements, each of which is | |
1036 | defined by ELEMENT. */ | |
1037 | ||
1038 | cp_declarator * | |
1039 | make_array_declarator (cp_declarator *element, tree bounds) | |
1040 | { | |
1041 | cp_declarator *declarator; | |
1042 | ||
1043 | declarator = make_declarator (cdk_array); | |
1044 | declarator->declarator = element; | |
1045 | declarator->u.array.bounds = bounds; | |
5d80a306 DG |
1046 | if (element) |
1047 | { | |
1048 | declarator->parameter_pack_p = element->parameter_pack_p; | |
1049 | element->parameter_pack_p = false; | |
1050 | } | |
1051 | else | |
1052 | declarator->parameter_pack_p = false; | |
058b15c1 MM |
1053 | |
1054 | return declarator; | |
1055 | } | |
1056 | ||
d393153e DG |
1057 | /* Determine whether the declarator we've seen so far can be a |
1058 | parameter pack, when followed by an ellipsis. */ | |
1059 | static bool | |
1060 | declarator_can_be_parameter_pack (cp_declarator *declarator) | |
1061 | { | |
1062 | /* Search for a declarator name, or any other declarator that goes | |
1063 | after the point where the ellipsis could appear in a parameter | |
1064 | pack. If we find any of these, then this declarator can not be | |
1065 | made into a parameter pack. */ | |
1066 | bool found = false; | |
1067 | while (declarator && !found) | |
1068 | { | |
1069 | switch ((int)declarator->kind) | |
1070 | { | |
1071 | case cdk_id: | |
d393153e | 1072 | case cdk_array: |
d393153e DG |
1073 | found = true; |
1074 | break; | |
cdcae745 DG |
1075 | |
1076 | case cdk_error: | |
1077 | return true; | |
1078 | ||
d393153e DG |
1079 | default: |
1080 | declarator = declarator->declarator; | |
1081 | break; | |
1082 | } | |
1083 | } | |
1084 | ||
1085 | return !found; | |
1086 | } | |
1087 | ||
058b15c1 MM |
1088 | cp_parameter_declarator *no_parameters; |
1089 | ||
1090 | /* Create a parameter declarator with the indicated DECL_SPECIFIERS, | |
1091 | DECLARATOR and DEFAULT_ARGUMENT. */ | |
1092 | ||
1093 | cp_parameter_declarator * | |
98ca843c | 1094 | make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers, |
058b15c1 MM |
1095 | cp_declarator *declarator, |
1096 | tree default_argument) | |
1097 | { | |
1098 | cp_parameter_declarator *parameter; | |
1099 | ||
98ca843c | 1100 | parameter = ((cp_parameter_declarator *) |
058b15c1 MM |
1101 | alloc_declarator (sizeof (cp_parameter_declarator))); |
1102 | parameter->next = NULL; | |
62d1db17 MM |
1103 | if (decl_specifiers) |
1104 | parameter->decl_specifiers = *decl_specifiers; | |
1105 | else | |
1106 | clear_decl_specs (¶meter->decl_specifiers); | |
058b15c1 MM |
1107 | parameter->declarator = declarator; |
1108 | parameter->default_argument = default_argument; | |
1109 | parameter->ellipsis_p = false; | |
1110 | ||
1111 | return parameter; | |
1112 | } | |
1113 | ||
0b45aedd MM |
1114 | /* Returns true iff DECLARATOR is a declaration for a function. */ |
1115 | ||
1116 | static bool | |
1117 | function_declarator_p (const cp_declarator *declarator) | |
1118 | { | |
1119 | while (declarator) | |
1120 | { | |
1121 | if (declarator->kind == cdk_function | |
1122 | && declarator->declarator->kind == cdk_id) | |
1123 | return true; | |
1124 | if (declarator->kind == cdk_id | |
1125 | || declarator->kind == cdk_error) | |
1126 | return false; | |
1127 | declarator = declarator->declarator; | |
1128 | } | |
1129 | return false; | |
1130 | } | |
1131 | ||
a723baf1 MM |
1132 | /* The parser. */ |
1133 | ||
1134 | /* Overview | |
1135 | -------- | |
1136 | ||
1137 | A cp_parser parses the token stream as specified by the C++ | |
1138 | grammar. Its job is purely parsing, not semantic analysis. For | |
1139 | example, the parser breaks the token stream into declarators, | |
1140 | expressions, statements, and other similar syntactic constructs. | |
1141 | It does not check that the types of the expressions on either side | |
1142 | of an assignment-statement are compatible, or that a function is | |
1143 | not declared with a parameter of type `void'. | |
1144 | ||
1145 | The parser invokes routines elsewhere in the compiler to perform | |
1146 | semantic analysis and to build up the abstract syntax tree for the | |
21526606 | 1147 | code processed. |
a723baf1 MM |
1148 | |
1149 | The parser (and the template instantiation code, which is, in a | |
1150 | way, a close relative of parsing) are the only parts of the | |
1151 | compiler that should be calling push_scope and pop_scope, or | |
1152 | related functions. The parser (and template instantiation code) | |
1153 | keeps track of what scope is presently active; everything else | |
1154 | should simply honor that. (The code that generates static | |
1155 | initializers may also need to set the scope, in order to check | |
1156 | access control correctly when emitting the initializers.) | |
1157 | ||
1158 | Methodology | |
1159 | ----------- | |
21526606 | 1160 | |
a723baf1 MM |
1161 | The parser is of the standard recursive-descent variety. Upcoming |
1162 | tokens in the token stream are examined in order to determine which | |
1163 | production to use when parsing a non-terminal. Some C++ constructs | |
1164 | require arbitrary look ahead to disambiguate. For example, it is | |
1165 | impossible, in the general case, to tell whether a statement is an | |
1166 | expression or declaration without scanning the entire statement. | |
1167 | Therefore, the parser is capable of "parsing tentatively." When the | |
1168 | parser is not sure what construct comes next, it enters this mode. | |
1169 | Then, while we attempt to parse the construct, the parser queues up | |
1170 | error messages, rather than issuing them immediately, and saves the | |
1171 | tokens it consumes. If the construct is parsed successfully, the | |
1172 | parser "commits", i.e., it issues any queued error messages and | |
1173 | the tokens that were being preserved are permanently discarded. | |
1174 | If, however, the construct is not parsed successfully, the parser | |
1175 | rolls back its state completely so that it can resume parsing using | |
1176 | a different alternative. | |
1177 | ||
1178 | Future Improvements | |
1179 | ------------------- | |
21526606 | 1180 | |
b8b94c5b PB |
1181 | The performance of the parser could probably be improved substantially. |
1182 | We could often eliminate the need to parse tentatively by looking ahead | |
1183 | a little bit. In some places, this approach might not entirely eliminate | |
1184 | the need to parse tentatively, but it might still speed up the average | |
1185 | case. */ | |
a723baf1 MM |
1186 | |
1187 | /* Flags that are passed to some parsing functions. These values can | |
1188 | be bitwise-ored together. */ | |
1189 | ||
7e5487a2 | 1190 | enum |
a723baf1 MM |
1191 | { |
1192 | /* No flags. */ | |
1193 | CP_PARSER_FLAGS_NONE = 0x0, | |
1194 | /* The construct is optional. If it is not present, then no error | |
1195 | should be issued. */ | |
1196 | CP_PARSER_FLAGS_OPTIONAL = 0x1, | |
1197 | /* When parsing a type-specifier, do not allow user-defined types. */ | |
1198 | CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2 | |
32e8bb8e ILT |
1199 | }; |
1200 | ||
1201 | /* This type is used for parameters and variables which hold | |
7e5487a2 | 1202 | combinations of the above flags. */ |
32e8bb8e | 1203 | typedef int cp_parser_flags; |
a723baf1 | 1204 | |
62b8a44e NS |
1205 | /* The different kinds of declarators we want to parse. */ |
1206 | ||
1207 | typedef enum cp_parser_declarator_kind | |
1208 | { | |
852dcbdd | 1209 | /* We want an abstract declarator. */ |
62b8a44e NS |
1210 | CP_PARSER_DECLARATOR_ABSTRACT, |
1211 | /* We want a named declarator. */ | |
1212 | CP_PARSER_DECLARATOR_NAMED, | |
04c06002 | 1213 | /* We don't mind, but the name must be an unqualified-id. */ |
62b8a44e NS |
1214 | CP_PARSER_DECLARATOR_EITHER |
1215 | } cp_parser_declarator_kind; | |
1216 | ||
b8b94c5b PB |
1217 | /* The precedence values used to parse binary expressions. The minimum value |
1218 | of PREC must be 1, because zero is reserved to quickly discriminate | |
1219 | binary operators from other tokens. */ | |
a723baf1 | 1220 | |
b8b94c5b | 1221 | enum cp_parser_prec |
a723baf1 | 1222 | { |
b8b94c5b PB |
1223 | PREC_NOT_OPERATOR, |
1224 | PREC_LOGICAL_OR_EXPRESSION, | |
1225 | PREC_LOGICAL_AND_EXPRESSION, | |
1226 | PREC_INCLUSIVE_OR_EXPRESSION, | |
1227 | PREC_EXCLUSIVE_OR_EXPRESSION, | |
1228 | PREC_AND_EXPRESSION, | |
b8b94c5b | 1229 | PREC_EQUALITY_EXPRESSION, |
69475123 | 1230 | PREC_RELATIONAL_EXPRESSION, |
b8b94c5b PB |
1231 | PREC_SHIFT_EXPRESSION, |
1232 | PREC_ADDITIVE_EXPRESSION, | |
1233 | PREC_MULTIPLICATIVE_EXPRESSION, | |
1234 | PREC_PM_EXPRESSION, | |
1235 | NUM_PREC_VALUES = PREC_PM_EXPRESSION | |
1236 | }; | |
a723baf1 | 1237 | |
b8b94c5b PB |
1238 | /* A mapping from a token type to a corresponding tree node type, with a |
1239 | precedence value. */ | |
a723baf1 | 1240 | |
b8b94c5b PB |
1241 | typedef struct cp_parser_binary_operations_map_node |
1242 | { | |
1243 | /* The token type. */ | |
1244 | enum cpp_ttype token_type; | |
1245 | /* The corresponding tree code. */ | |
1246 | enum tree_code tree_type; | |
1247 | /* The precedence of this operator. */ | |
1248 | enum cp_parser_prec prec; | |
1249 | } cp_parser_binary_operations_map_node; | |
a723baf1 MM |
1250 | |
1251 | /* The status of a tentative parse. */ | |
1252 | ||
1253 | typedef enum cp_parser_status_kind | |
1254 | { | |
1255 | /* No errors have occurred. */ | |
1256 | CP_PARSER_STATUS_KIND_NO_ERROR, | |
1257 | /* An error has occurred. */ | |
1258 | CP_PARSER_STATUS_KIND_ERROR, | |
1259 | /* We are committed to this tentative parse, whether or not an error | |
1260 | has occurred. */ | |
1261 | CP_PARSER_STATUS_KIND_COMMITTED | |
1262 | } cp_parser_status_kind; | |
1263 | ||
b8b94c5b PB |
1264 | typedef struct cp_parser_expression_stack_entry |
1265 | { | |
2a67bec2 ILT |
1266 | /* Left hand side of the binary operation we are currently |
1267 | parsing. */ | |
b8b94c5b | 1268 | tree lhs; |
2a67bec2 ILT |
1269 | /* Original tree code for left hand side, if it was a binary |
1270 | expression itself (used for -Wparentheses). */ | |
1271 | enum tree_code lhs_type; | |
1272 | /* Tree code for the binary operation we are parsing. */ | |
b8b94c5b | 1273 | enum tree_code tree_type; |
2a67bec2 | 1274 | /* Precedence of the binary operation we are parsing. */ |
32e8bb8e | 1275 | enum cp_parser_prec prec; |
b8b94c5b PB |
1276 | } cp_parser_expression_stack_entry; |
1277 | ||
43c2a69a PB |
1278 | /* The stack for storing partial expressions. We only need NUM_PREC_VALUES |
1279 | entries because precedence levels on the stack are monotonically | |
1280 | increasing. */ | |
b8b94c5b PB |
1281 | typedef struct cp_parser_expression_stack_entry |
1282 | cp_parser_expression_stack[NUM_PREC_VALUES]; | |
a723baf1 | 1283 | |
b8b94c5b | 1284 | /* Context that is saved and restored when parsing tentatively. */ |
d1b38208 | 1285 | typedef struct GTY (()) cp_parser_context { |
a723baf1 MM |
1286 | /* If this is a tentative parsing context, the status of the |
1287 | tentative parse. */ | |
1288 | enum cp_parser_status_kind status; | |
1289 | /* If non-NULL, we have just seen a `x->' or `x.' expression. Names | |
1290 | that are looked up in this context must be looked up both in the | |
1291 | scope given by OBJECT_TYPE (the type of `x' or `*x') and also in | |
1292 | the context of the containing expression. */ | |
1293 | tree object_type; | |
b8b94c5b | 1294 | |
a723baf1 MM |
1295 | /* The next parsing context in the stack. */ |
1296 | struct cp_parser_context *next; | |
1297 | } cp_parser_context; | |
1298 | ||
1299 | /* Prototypes. */ | |
1300 | ||
1301 | /* Constructors and destructors. */ | |
1302 | ||
1303 | static cp_parser_context *cp_parser_context_new | |
94edc4ab | 1304 | (cp_parser_context *); |
a723baf1 | 1305 | |
e5976695 MM |
1306 | /* Class variables. */ |
1307 | ||
1431042e | 1308 | static GTY((deletable)) cp_parser_context* cp_parser_context_free_list; |
e5976695 | 1309 | |
b8b94c5b PB |
1310 | /* The operator-precedence table used by cp_parser_binary_expression. |
1311 | Transformed into an associative array (binops_by_token) by | |
1312 | cp_parser_new. */ | |
1313 | ||
1314 | static const cp_parser_binary_operations_map_node binops[] = { | |
1315 | { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION }, | |
1316 | { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION }, | |
1317 | ||
1318 | { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION }, | |
1319 | { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION }, | |
1320 | { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION }, | |
1321 | ||
1322 | { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION }, | |
1323 | { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION }, | |
1324 | ||
1325 | { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION }, | |
1326 | { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION }, | |
1327 | ||
1328 | { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION }, | |
1329 | { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION }, | |
1330 | { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION }, | |
1331 | { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION }, | |
b8b94c5b PB |
1332 | |
1333 | { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION }, | |
1334 | { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION }, | |
1335 | ||
1336 | { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION }, | |
1337 | ||
1338 | { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION }, | |
1339 | ||
1340 | { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION }, | |
1341 | ||
1342 | { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION }, | |
1343 | ||
1344 | { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION } | |
1345 | }; | |
1346 | ||
1347 | /* The same as binops, but initialized by cp_parser_new so that | |
1348 | binops_by_token[N].token_type == N. Used in cp_parser_binary_expression | |
1349 | for speed. */ | |
1350 | static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES]; | |
1351 | ||
a723baf1 MM |
1352 | /* Constructors and destructors. */ |
1353 | ||
1354 | /* Construct a new context. The context below this one on the stack | |
1355 | is given by NEXT. */ | |
1356 | ||
1357 | static cp_parser_context * | |
94edc4ab | 1358 | cp_parser_context_new (cp_parser_context* next) |
a723baf1 MM |
1359 | { |
1360 | cp_parser_context *context; | |
1361 | ||
1362 | /* Allocate the storage. */ | |
e5976695 MM |
1363 | if (cp_parser_context_free_list != NULL) |
1364 | { | |
1365 | /* Pull the first entry from the free list. */ | |
1366 | context = cp_parser_context_free_list; | |
1367 | cp_parser_context_free_list = context->next; | |
c68b0a84 | 1368 | memset (context, 0, sizeof (*context)); |
e5976695 MM |
1369 | } |
1370 | else | |
99dd239f | 1371 | context = GGC_CNEW (cp_parser_context); |
b8b94c5b | 1372 | |
a723baf1 MM |
1373 | /* No errors have occurred yet in this context. */ |
1374 | context->status = CP_PARSER_STATUS_KIND_NO_ERROR; | |
39a13be5 | 1375 | /* If this is not the bottommost context, copy information that we |
a723baf1 MM |
1376 | need from the previous context. */ |
1377 | if (next) | |
1378 | { | |
1379 | /* If, in the NEXT context, we are parsing an `x->' or `x.' | |
1380 | expression, then we are parsing one in this context, too. */ | |
1381 | context->object_type = next->object_type; | |
a723baf1 MM |
1382 | /* Thread the stack. */ |
1383 | context->next = next; | |
1384 | } | |
1385 | ||
1386 | return context; | |
1387 | } | |
1388 | ||
1389 | /* The cp_parser structure represents the C++ parser. */ | |
1390 | ||
d1b38208 | 1391 | typedef struct GTY(()) cp_parser { |
a723baf1 MM |
1392 | /* The lexer from which we are obtaining tokens. */ |
1393 | cp_lexer *lexer; | |
1394 | ||
1395 | /* The scope in which names should be looked up. If NULL_TREE, then | |
1396 | we look up names in the scope that is currently open in the | |
1397 | source program. If non-NULL, this is either a TYPE or | |
8fe4d24b NS |
1398 | NAMESPACE_DECL for the scope in which we should look. It can |
1399 | also be ERROR_MARK, when we've parsed a bogus scope. | |
a723baf1 MM |
1400 | |
1401 | This value is not cleared automatically after a name is looked | |
1402 | up, so we must be careful to clear it before starting a new look | |
1403 | up sequence. (If it is not cleared, then `X::Y' followed by `Z' | |
1404 | will look up `Z' in the scope of `X', rather than the current | |
1405 | scope.) Unfortunately, it is difficult to tell when name lookup | |
1406 | is complete, because we sometimes peek at a token, look it up, | |
8fe4d24b | 1407 | and then decide not to consume it. */ |
a723baf1 MM |
1408 | tree scope; |
1409 | ||
1410 | /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the | |
1411 | last lookup took place. OBJECT_SCOPE is used if an expression | |
1412 | like "x->y" or "x.y" was used; it gives the type of "*x" or "x", | |
21526606 | 1413 | respectively. QUALIFYING_SCOPE is used for an expression of the |
a723baf1 MM |
1414 | form "X::Y"; it refers to X. */ |
1415 | tree object_scope; | |
1416 | tree qualifying_scope; | |
1417 | ||
1418 | /* A stack of parsing contexts. All but the bottom entry on the | |
1419 | stack will be tentative contexts. | |
1420 | ||
1421 | We parse tentatively in order to determine which construct is in | |
1422 | use in some situations. For example, in order to determine | |
1423 | whether a statement is an expression-statement or a | |
1424 | declaration-statement we parse it tentatively as a | |
1425 | declaration-statement. If that fails, we then reparse the same | |
1426 | token stream as an expression-statement. */ | |
1427 | cp_parser_context *context; | |
1428 | ||
1429 | /* True if we are parsing GNU C++. If this flag is not set, then | |
1430 | GNU extensions are not recognized. */ | |
1431 | bool allow_gnu_extensions_p; | |
1432 | ||
1433 | /* TRUE if the `>' token should be interpreted as the greater-than | |
1434 | operator. FALSE if it is the end of a template-id or | |
a2ca1ab2 DG |
1435 | template-parameter-list. In C++0x mode, this flag also applies to |
1436 | `>>' tokens, which are viewed as two consecutive `>' tokens when | |
1437 | this flag is FALSE. */ | |
a723baf1 MM |
1438 | bool greater_than_is_operator_p; |
1439 | ||
1440 | /* TRUE if default arguments are allowed within a parameter list | |
1441 | that starts at this point. FALSE if only a gnu extension makes | |
cd0be382 | 1442 | them permissible. */ |
a723baf1 | 1443 | bool default_arg_ok_p; |
21526606 | 1444 | |
a723baf1 MM |
1445 | /* TRUE if we are parsing an integral constant-expression. See |
1446 | [expr.const] for a precise definition. */ | |
67c03833 | 1447 | bool integral_constant_expression_p; |
a723baf1 | 1448 | |
14d22dd6 MM |
1449 | /* TRUE if we are parsing an integral constant-expression -- but a |
1450 | non-constant expression should be permitted as well. This flag | |
1451 | is used when parsing an array bound so that GNU variable-length | |
1452 | arrays are tolerated. */ | |
67c03833 | 1453 | bool allow_non_integral_constant_expression_p; |
14d22dd6 MM |
1454 | |
1455 | /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has | |
1456 | been seen that makes the expression non-constant. */ | |
67c03833 | 1457 | bool non_integral_constant_expression_p; |
14d22dd6 | 1458 | |
a723baf1 MM |
1459 | /* TRUE if local variable names and `this' are forbidden in the |
1460 | current context. */ | |
1461 | bool local_variables_forbidden_p; | |
1462 | ||
1463 | /* TRUE if the declaration we are parsing is part of a | |
1464 | linkage-specification of the form `extern string-literal | |
1465 | declaration'. */ | |
1466 | bool in_unbraced_linkage_specification_p; | |
1467 | ||
1468 | /* TRUE if we are presently parsing a declarator, after the | |
1469 | direct-declarator. */ | |
1470 | bool in_declarator_p; | |
1471 | ||
4bb8ca28 MM |
1472 | /* TRUE if we are presently parsing a template-argument-list. */ |
1473 | bool in_template_argument_list_p; | |
1474 | ||
1799e5d5 RH |
1475 | /* Set to IN_ITERATION_STMT if parsing an iteration-statement, |
1476 | to IN_OMP_BLOCK if parsing OpenMP structured block and | |
1477 | IN_OMP_FOR if parsing OpenMP loop. If parsing a switch statement, | |
1478 | this is bitwise ORed with IN_SWITCH_STMT, unless parsing an | |
1479 | iteration-statement, OpenMP block or loop within that switch. */ | |
1480 | #define IN_SWITCH_STMT 1 | |
1481 | #define IN_ITERATION_STMT 2 | |
1482 | #define IN_OMP_BLOCK 4 | |
1483 | #define IN_OMP_FOR 8 | |
4606b05c | 1484 | #define IN_IF_STMT 16 |
1799e5d5 RH |
1485 | unsigned char in_statement; |
1486 | ||
1487 | /* TRUE if we are presently parsing the body of a switch statement. | |
1488 | Note that this doesn't quite overlap with in_statement above. | |
1489 | The difference relates to giving the right sets of error messages: | |
1490 | "case not in switch" vs "break statement used with OpenMP...". */ | |
0e59b3fb MM |
1491 | bool in_switch_statement_p; |
1492 | ||
4f8163b1 MM |
1493 | /* TRUE if we are parsing a type-id in an expression context. In |
1494 | such a situation, both "type (expr)" and "type (type)" are valid | |
1495 | alternatives. */ | |
1496 | bool in_type_id_in_expr_p; | |
1497 | ||
7d381002 | 1498 | /* TRUE if we are currently in a header file where declarations are |
03fd3f84 | 1499 | implicitly extern "C". */ |
7d381002 MA |
1500 | bool implicit_extern_c; |
1501 | ||
c162c75e MA |
1502 | /* TRUE if strings in expressions should be translated to the execution |
1503 | character set. */ | |
1504 | bool translate_strings_p; | |
1505 | ||
56c7f9c1 MM |
1506 | /* TRUE if we are presently parsing the body of a function, but not |
1507 | a local class. */ | |
1508 | bool in_function_body; | |
1509 | ||
a723baf1 MM |
1510 | /* If non-NULL, then we are parsing a construct where new type |
1511 | definitions are not permitted. The string stored here will be | |
1512 | issued as an error message if a type is defined. */ | |
1513 | const char *type_definition_forbidden_message; | |
1514 | ||
8db1028e NS |
1515 | /* A list of lists. The outer list is a stack, used for member |
1516 | functions of local classes. At each level there are two sub-list, | |
1517 | one on TREE_VALUE and one on TREE_PURPOSE. Each of those | |
1518 | sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their | |
1519 | TREE_VALUE's. The functions are chained in reverse declaration | |
1520 | order. | |
1521 | ||
1522 | The TREE_PURPOSE sublist contains those functions with default | |
1523 | arguments that need post processing, and the TREE_VALUE sublist | |
1524 | contains those functions with definitions that need post | |
1525 | processing. | |
1526 | ||
1527 | These lists can only be processed once the outermost class being | |
9bcb9aae | 1528 | defined is complete. */ |
a723baf1 MM |
1529 | tree unparsed_functions_queues; |
1530 | ||
1531 | /* The number of classes whose definitions are currently in | |
1532 | progress. */ | |
1533 | unsigned num_classes_being_defined; | |
1534 | ||
1535 | /* The number of template parameter lists that apply directly to the | |
1536 | current declaration. */ | |
1537 | unsigned num_template_parameter_lists; | |
1538 | } cp_parser; | |
1539 | ||
a723baf1 MM |
1540 | /* Prototypes. */ |
1541 | ||
1542 | /* Constructors and destructors. */ | |
1543 | ||
1544 | static cp_parser *cp_parser_new | |
94edc4ab | 1545 | (void); |
a723baf1 | 1546 | |
21526606 | 1547 | /* Routines to parse various constructs. |
a723baf1 MM |
1548 | |
1549 | Those that return `tree' will return the error_mark_node (rather | |
1550 | than NULL_TREE) if a parse error occurs, unless otherwise noted. | |
1551 | Sometimes, they will return an ordinary node if error-recovery was | |
34cd5ae7 | 1552 | attempted, even though a parse error occurred. So, to check |
a723baf1 MM |
1553 | whether or not a parse error occurred, you should always use |
1554 | cp_parser_error_occurred. If the construct is optional (indicated | |
1555 | either by an `_opt' in the name of the function that does the | |
1556 | parsing or via a FLAGS parameter), then NULL_TREE is returned if | |
1557 | the construct is not present. */ | |
1558 | ||
1559 | /* Lexical conventions [gram.lex] */ | |
1560 | ||
1561 | static tree cp_parser_identifier | |
94edc4ab | 1562 | (cp_parser *); |
c162c75e MA |
1563 | static tree cp_parser_string_literal |
1564 | (cp_parser *, bool, bool); | |
a723baf1 MM |
1565 | |
1566 | /* Basic concepts [gram.basic] */ | |
1567 | ||
1568 | static bool cp_parser_translation_unit | |
94edc4ab | 1569 | (cp_parser *); |
a723baf1 MM |
1570 | |
1571 | /* Expressions [gram.expr] */ | |
1572 | ||
1573 | static tree cp_parser_primary_expression | |
02ed62dd | 1574 | (cp_parser *, bool, bool, bool, cp_id_kind *); |
a723baf1 | 1575 | static tree cp_parser_id_expression |
88525e1b | 1576 | (cp_parser *, bool, bool, bool *, bool, bool); |
a723baf1 | 1577 | static tree cp_parser_unqualified_id |
88525e1b | 1578 | (cp_parser *, bool, bool, bool, bool); |
a723baf1 | 1579 | static tree cp_parser_nested_name_specifier_opt |
a668c6ad | 1580 | (cp_parser *, bool, bool, bool, bool); |
a723baf1 | 1581 | static tree cp_parser_nested_name_specifier |
a723baf1 | 1582 | (cp_parser *, bool, bool, bool, bool); |
adf2edec | 1583 | static tree cp_parser_qualifying_entity |
a668c6ad | 1584 | (cp_parser *, bool, bool, bool, bool, bool); |
a723baf1 | 1585 | static tree cp_parser_postfix_expression |
cee9035d | 1586 | (cp_parser *, bool, bool, bool, cp_id_kind *); |
7a3ea201 RH |
1587 | static tree cp_parser_postfix_open_square_expression |
1588 | (cp_parser *, tree, bool); | |
1589 | static tree cp_parser_postfix_dot_deref_expression | |
2b7a3abf | 1590 | (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *, location_t); |
c166b898 | 1591 | static VEC(tree,gc) *cp_parser_parenthesized_expression_list |
5d80a306 | 1592 | (cp_parser *, bool, bool, bool, bool *); |
a723baf1 | 1593 | static void cp_parser_pseudo_destructor_name |
94edc4ab | 1594 | (cp_parser *, tree *, tree *); |
a723baf1 | 1595 | static tree cp_parser_unary_expression |
cee9035d | 1596 | (cp_parser *, bool, bool, cp_id_kind *); |
a723baf1 | 1597 | static enum tree_code cp_parser_unary_operator |
94edc4ab | 1598 | (cp_token *); |
a723baf1 | 1599 | static tree cp_parser_new_expression |
94edc4ab | 1600 | (cp_parser *); |
c166b898 | 1601 | static VEC(tree,gc) *cp_parser_new_placement |
94edc4ab | 1602 | (cp_parser *); |
a723baf1 | 1603 | static tree cp_parser_new_type_id |
058b15c1 MM |
1604 | (cp_parser *, tree *); |
1605 | static cp_declarator *cp_parser_new_declarator_opt | |
94edc4ab | 1606 | (cp_parser *); |
058b15c1 | 1607 | static cp_declarator *cp_parser_direct_new_declarator |
94edc4ab | 1608 | (cp_parser *); |
c166b898 | 1609 | static VEC(tree,gc) *cp_parser_new_initializer |
94edc4ab | 1610 | (cp_parser *); |
a723baf1 | 1611 | static tree cp_parser_delete_expression |
94edc4ab | 1612 | (cp_parser *); |
21526606 | 1613 | static tree cp_parser_cast_expression |
cee9035d | 1614 | (cp_parser *, bool, bool, cp_id_kind *); |
b8b94c5b | 1615 | static tree cp_parser_binary_expression |
c5cdb03f | 1616 | (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *); |
a723baf1 | 1617 | static tree cp_parser_question_colon_clause |
94edc4ab | 1618 | (cp_parser *, tree); |
a723baf1 | 1619 | static tree cp_parser_assignment_expression |
cee9035d | 1620 | (cp_parser *, bool, cp_id_kind *); |
a723baf1 | 1621 | static enum tree_code cp_parser_assignment_operator_opt |
94edc4ab | 1622 | (cp_parser *); |
a723baf1 | 1623 | static tree cp_parser_expression |
cee9035d | 1624 | (cp_parser *, bool, cp_id_kind *); |
a723baf1 | 1625 | static tree cp_parser_constant_expression |
14d22dd6 | 1626 | (cp_parser *, bool, bool *); |
7a3ea201 RH |
1627 | static tree cp_parser_builtin_offsetof |
1628 | (cp_parser *); | |
a723baf1 MM |
1629 | |
1630 | /* Statements [gram.stmt.stmt] */ | |
1631 | ||
1632 | static void cp_parser_statement | |
2a67bec2 | 1633 | (cp_parser *, tree, bool, bool *); |
ddd039d5 SB |
1634 | static void cp_parser_label_for_labeled_statement |
1635 | (cp_parser *); | |
a723baf1 | 1636 | static tree cp_parser_expression_statement |
325c3691 | 1637 | (cp_parser *, tree); |
a723baf1 | 1638 | static tree cp_parser_compound_statement |
325c3691 | 1639 | (cp_parser *, tree, bool); |
a723baf1 | 1640 | static void cp_parser_statement_seq_opt |
325c3691 | 1641 | (cp_parser *, tree); |
a723baf1 | 1642 | static tree cp_parser_selection_statement |
2a67bec2 | 1643 | (cp_parser *, bool *); |
a723baf1 | 1644 | static tree cp_parser_condition |
94edc4ab | 1645 | (cp_parser *); |
a723baf1 | 1646 | static tree cp_parser_iteration_statement |
94edc4ab | 1647 | (cp_parser *); |
a723baf1 | 1648 | static void cp_parser_for_init_statement |
94edc4ab | 1649 | (cp_parser *); |
a723baf1 | 1650 | static tree cp_parser_jump_statement |
94edc4ab | 1651 | (cp_parser *); |
a723baf1 | 1652 | static void cp_parser_declaration_statement |
94edc4ab | 1653 | (cp_parser *); |
a723baf1 MM |
1654 | |
1655 | static tree cp_parser_implicitly_scoped_statement | |
2a67bec2 | 1656 | (cp_parser *, bool *); |
a723baf1 | 1657 | static void cp_parser_already_scoped_statement |
94edc4ab | 1658 | (cp_parser *); |
a723baf1 MM |
1659 | |
1660 | /* Declarations [gram.dcl.dcl] */ | |
1661 | ||
1662 | static void cp_parser_declaration_seq_opt | |
94edc4ab | 1663 | (cp_parser *); |
a723baf1 | 1664 | static void cp_parser_declaration |
94edc4ab | 1665 | (cp_parser *); |
a723baf1 | 1666 | static void cp_parser_block_declaration |
94edc4ab | 1667 | (cp_parser *, bool); |
a723baf1 | 1668 | static void cp_parser_simple_declaration |
94edc4ab | 1669 | (cp_parser *, bool); |
62d1db17 MM |
1670 | static void cp_parser_decl_specifier_seq |
1671 | (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *); | |
a723baf1 | 1672 | static tree cp_parser_storage_class_specifier_opt |
94edc4ab | 1673 | (cp_parser *); |
a723baf1 | 1674 | static tree cp_parser_function_specifier_opt |
62d1db17 | 1675 | (cp_parser *, cp_decl_specifier_seq *); |
a723baf1 | 1676 | static tree cp_parser_type_specifier |
98ca843c | 1677 | (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool, |
62d1db17 | 1678 | int *, bool *); |
a723baf1 | 1679 | static tree cp_parser_simple_type_specifier |
62d1db17 | 1680 | (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags); |
a723baf1 | 1681 | static tree cp_parser_type_name |
94edc4ab | 1682 | (cp_parser *); |
359be76e MLI |
1683 | static tree cp_parser_nonclass_name |
1684 | (cp_parser* parser); | |
a723baf1 | 1685 | static tree cp_parser_elaborated_type_specifier |
94edc4ab | 1686 | (cp_parser *, bool, bool); |
a723baf1 | 1687 | static tree cp_parser_enum_specifier |
94edc4ab | 1688 | (cp_parser *); |
a723baf1 | 1689 | static void cp_parser_enumerator_list |
94edc4ab | 1690 | (cp_parser *, tree); |
21526606 | 1691 | static void cp_parser_enumerator_definition |
94edc4ab | 1692 | (cp_parser *, tree); |
a723baf1 | 1693 | static tree cp_parser_namespace_name |
94edc4ab | 1694 | (cp_parser *); |
a723baf1 | 1695 | static void cp_parser_namespace_definition |
94edc4ab | 1696 | (cp_parser *); |
a723baf1 | 1697 | static void cp_parser_namespace_body |
94edc4ab | 1698 | (cp_parser *); |
a723baf1 | 1699 | static tree cp_parser_qualified_namespace_specifier |
94edc4ab | 1700 | (cp_parser *); |
a723baf1 | 1701 | static void cp_parser_namespace_alias_definition |
94edc4ab | 1702 | (cp_parser *); |
981114e1 MM |
1703 | static bool cp_parser_using_declaration |
1704 | (cp_parser *, bool); | |
a723baf1 | 1705 | static void cp_parser_using_directive |
94edc4ab | 1706 | (cp_parser *); |
a723baf1 | 1707 | static void cp_parser_asm_definition |
94edc4ab | 1708 | (cp_parser *); |
a723baf1 | 1709 | static void cp_parser_linkage_specification |
94edc4ab | 1710 | (cp_parser *); |
55a3debe DG |
1711 | static void cp_parser_static_assert |
1712 | (cp_parser *, bool); | |
3ad6a8e1 DG |
1713 | static tree cp_parser_decltype |
1714 | (cp_parser *); | |
a723baf1 MM |
1715 | |
1716 | /* Declarators [gram.dcl.decl] */ | |
1717 | ||
1718 | static tree cp_parser_init_declarator | |
d6b418fa | 1719 | (cp_parser *, cp_decl_specifier_seq *, VEC (deferred_access_check,gc)*, bool, bool, int, bool *); |
058b15c1 | 1720 | static cp_declarator *cp_parser_declarator |
db86dd14 | 1721 | (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool); |
058b15c1 | 1722 | static cp_declarator *cp_parser_direct_declarator |
db86dd14 | 1723 | (cp_parser *, cp_parser_declarator_kind, int *, bool); |
a723baf1 | 1724 | static enum tree_code cp_parser_ptr_operator |
3c01e5df MM |
1725 | (cp_parser *, tree *, cp_cv_quals *); |
1726 | static cp_cv_quals cp_parser_cv_qualifier_seq_opt | |
94edc4ab | 1727 | (cp_parser *); |
f57ca1ea JM |
1728 | static tree cp_parser_late_return_type_opt |
1729 | (cp_parser *); | |
a723baf1 | 1730 | static tree cp_parser_declarator_id |
fa6098f8 | 1731 | (cp_parser *, bool); |
a723baf1 | 1732 | static tree cp_parser_type_id |
94edc4ab | 1733 | (cp_parser *); |
0d9c0892 JM |
1734 | static tree cp_parser_template_type_arg |
1735 | (cp_parser *); | |
1736 | static tree cp_parser_type_id_1 | |
1737 | (cp_parser *, bool); | |
62d1db17 | 1738 | static void cp_parser_type_specifier_seq |
d4113656 | 1739 | (cp_parser *, bool, cp_decl_specifier_seq *); |
b344d949 | 1740 | static tree cp_parser_parameter_declaration_clause |
94edc4ab | 1741 | (cp_parser *); |
b344d949 | 1742 | static tree cp_parser_parameter_declaration_list |
058b15c1 MM |
1743 | (cp_parser *, bool *); |
1744 | static cp_parameter_declarator *cp_parser_parameter_declaration | |
4bb8ca28 | 1745 | (cp_parser *, bool, bool *); |
cdcae745 DG |
1746 | static tree cp_parser_default_argument |
1747 | (cp_parser *, bool); | |
a723baf1 MM |
1748 | static void cp_parser_function_body |
1749 | (cp_parser *); | |
1750 | static tree cp_parser_initializer | |
39703eb9 | 1751 | (cp_parser *, bool *, bool *); |
a723baf1 | 1752 | static tree cp_parser_initializer_clause |
39703eb9 | 1753 | (cp_parser *, bool *); |
09357846 JM |
1754 | static tree cp_parser_braced_list |
1755 | (cp_parser*, bool*); | |
4038c495 | 1756 | static VEC(constructor_elt,gc) *cp_parser_initializer_list |
39703eb9 | 1757 | (cp_parser *, bool *); |
a723baf1 MM |
1758 | |
1759 | static bool cp_parser_ctor_initializer_opt_and_function_body | |
1760 | (cp_parser *); | |
1761 | ||
1762 | /* Classes [gram.class] */ | |
1763 | ||
1764 | static tree cp_parser_class_name | |
fc6a28d7 | 1765 | (cp_parser *, bool, bool, enum tag_types, bool, bool, bool); |
a723baf1 | 1766 | static tree cp_parser_class_specifier |
94edc4ab | 1767 | (cp_parser *); |
a723baf1 | 1768 | static tree cp_parser_class_head |
7f9faf5e | 1769 | (cp_parser *, bool *, tree *, tree *); |
a723baf1 | 1770 | static enum tag_types cp_parser_class_key |
94edc4ab | 1771 | (cp_parser *); |
a723baf1 | 1772 | static void cp_parser_member_specification_opt |
94edc4ab | 1773 | (cp_parser *); |
a723baf1 | 1774 | static void cp_parser_member_declaration |
94edc4ab | 1775 | (cp_parser *); |
a723baf1 | 1776 | static tree cp_parser_pure_specifier |
94edc4ab | 1777 | (cp_parser *); |
a723baf1 | 1778 | static tree cp_parser_constant_initializer |
94edc4ab | 1779 | (cp_parser *); |
a723baf1 MM |
1780 | |
1781 | /* Derived classes [gram.class.derived] */ | |
1782 | ||
1783 | static tree cp_parser_base_clause | |
94edc4ab | 1784 | (cp_parser *); |
a723baf1 | 1785 | static tree cp_parser_base_specifier |
94edc4ab | 1786 | (cp_parser *); |
a723baf1 MM |
1787 | |
1788 | /* Special member functions [gram.special] */ | |
1789 | ||
1790 | static tree cp_parser_conversion_function_id | |
94edc4ab | 1791 | (cp_parser *); |
a723baf1 | 1792 | static tree cp_parser_conversion_type_id |
94edc4ab | 1793 | (cp_parser *); |
058b15c1 | 1794 | static cp_declarator *cp_parser_conversion_declarator_opt |
94edc4ab | 1795 | (cp_parser *); |
a723baf1 | 1796 | static bool cp_parser_ctor_initializer_opt |
94edc4ab | 1797 | (cp_parser *); |
a723baf1 | 1798 | static void cp_parser_mem_initializer_list |
94edc4ab | 1799 | (cp_parser *); |
a723baf1 | 1800 | static tree cp_parser_mem_initializer |
94edc4ab | 1801 | (cp_parser *); |
a723baf1 | 1802 | static tree cp_parser_mem_initializer_id |
94edc4ab | 1803 | (cp_parser *); |
a723baf1 MM |
1804 | |
1805 | /* Overloading [gram.over] */ | |
1806 | ||
1807 | static tree cp_parser_operator_function_id | |
94edc4ab | 1808 | (cp_parser *); |
a723baf1 | 1809 | static tree cp_parser_operator |
94edc4ab | 1810 | (cp_parser *); |
a723baf1 MM |
1811 | |
1812 | /* Templates [gram.temp] */ | |
1813 | ||
1814 | static void cp_parser_template_declaration | |
94edc4ab | 1815 | (cp_parser *, bool); |
a723baf1 | 1816 | static tree cp_parser_template_parameter_list |
94edc4ab | 1817 | (cp_parser *); |
a723baf1 | 1818 | static tree cp_parser_template_parameter |
5d80a306 | 1819 | (cp_parser *, bool *, bool *); |
a723baf1 | 1820 | static tree cp_parser_type_parameter |
5d80a306 | 1821 | (cp_parser *, bool *); |
a723baf1 | 1822 | static tree cp_parser_template_id |
a668c6ad | 1823 | (cp_parser *, bool, bool, bool); |
a723baf1 | 1824 | static tree cp_parser_template_name |
a668c6ad | 1825 | (cp_parser *, bool, bool, bool, bool *); |
a723baf1 | 1826 | static tree cp_parser_template_argument_list |
94edc4ab | 1827 | (cp_parser *); |
a723baf1 | 1828 | static tree cp_parser_template_argument |
94edc4ab | 1829 | (cp_parser *); |
a723baf1 | 1830 | static void cp_parser_explicit_instantiation |
94edc4ab | 1831 | (cp_parser *); |
a723baf1 | 1832 | static void cp_parser_explicit_specialization |
94edc4ab | 1833 | (cp_parser *); |
a723baf1 MM |
1834 | |
1835 | /* Exception handling [gram.exception] */ | |
1836 | ||
21526606 | 1837 | static tree cp_parser_try_block |
94edc4ab | 1838 | (cp_parser *); |
a723baf1 | 1839 | static bool cp_parser_function_try_block |
94edc4ab | 1840 | (cp_parser *); |
a723baf1 | 1841 | static void cp_parser_handler_seq |
94edc4ab | 1842 | (cp_parser *); |
a723baf1 | 1843 | static void cp_parser_handler |
94edc4ab | 1844 | (cp_parser *); |
a723baf1 | 1845 | static tree cp_parser_exception_declaration |
94edc4ab | 1846 | (cp_parser *); |
a723baf1 | 1847 | static tree cp_parser_throw_expression |
94edc4ab | 1848 | (cp_parser *); |
a723baf1 | 1849 | static tree cp_parser_exception_specification_opt |
94edc4ab | 1850 | (cp_parser *); |
a723baf1 | 1851 | static tree cp_parser_type_id_list |
94edc4ab | 1852 | (cp_parser *); |
a723baf1 MM |
1853 | |
1854 | /* GNU Extensions */ | |
1855 | ||
1856 | static tree cp_parser_asm_specification_opt | |
94edc4ab | 1857 | (cp_parser *); |
a723baf1 | 1858 | static tree cp_parser_asm_operand_list |
94edc4ab | 1859 | (cp_parser *); |
a723baf1 | 1860 | static tree cp_parser_asm_clobber_list |
94edc4ab | 1861 | (cp_parser *); |
a723baf1 | 1862 | static tree cp_parser_attributes_opt |
94edc4ab | 1863 | (cp_parser *); |
a723baf1 | 1864 | static tree cp_parser_attribute_list |
94edc4ab | 1865 | (cp_parser *); |
a723baf1 | 1866 | static bool cp_parser_extension_opt |
94edc4ab | 1867 | (cp_parser *, int *); |
a723baf1 | 1868 | static void cp_parser_label_declaration |
94edc4ab | 1869 | (cp_parser *); |
a723baf1 | 1870 | |
bc4071dd RH |
1871 | enum pragma_context { pragma_external, pragma_stmt, pragma_compound }; |
1872 | static bool cp_parser_pragma | |
1873 | (cp_parser *, enum pragma_context); | |
1874 | ||
e58a9aa1 ZL |
1875 | /* Objective-C++ Productions */ |
1876 | ||
1877 | static tree cp_parser_objc_message_receiver | |
1878 | (cp_parser *); | |
1879 | static tree cp_parser_objc_message_args | |
1880 | (cp_parser *); | |
1881 | static tree cp_parser_objc_message_expression | |
1882 | (cp_parser *); | |
1883 | static tree cp_parser_objc_encode_expression | |
1884 | (cp_parser *); | |
c8094d83 | 1885 | static tree cp_parser_objc_defs_expression |
e58a9aa1 ZL |
1886 | (cp_parser *); |
1887 | static tree cp_parser_objc_protocol_expression | |
1888 | (cp_parser *); | |
1889 | static tree cp_parser_objc_selector_expression | |
1890 | (cp_parser *); | |
1891 | static tree cp_parser_objc_expression | |
1892 | (cp_parser *); | |
1893 | static bool cp_parser_objc_selector_p | |
1894 | (enum cpp_ttype); | |
1895 | static tree cp_parser_objc_selector | |
1896 | (cp_parser *); | |
1897 | static tree cp_parser_objc_protocol_refs_opt | |
1898 | (cp_parser *); | |
1899 | static void cp_parser_objc_declaration | |
1900 | (cp_parser *); | |
1901 | static tree cp_parser_objc_statement | |
1902 | (cp_parser *); | |
1903 | ||
a723baf1 MM |
1904 | /* Utility Routines */ |
1905 | ||
1906 | static tree cp_parser_lookup_name | |
2b7a3abf | 1907 | (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t); |
a723baf1 | 1908 | static tree cp_parser_lookup_name_simple |
2b7a3abf | 1909 | (cp_parser *, tree, location_t); |
a723baf1 MM |
1910 | static tree cp_parser_maybe_treat_template_as_class |
1911 | (tree, bool); | |
1912 | static bool cp_parser_check_declarator_template_parameters | |
2b7a3abf | 1913 | (cp_parser *, cp_declarator *, location_t); |
a723baf1 | 1914 | static bool cp_parser_check_template_parameters |
0d246707 | 1915 | (cp_parser *, unsigned, location_t, cp_declarator *); |
d6b4ea85 MM |
1916 | static tree cp_parser_simple_cast_expression |
1917 | (cp_parser *); | |
a723baf1 | 1918 | static tree cp_parser_global_scope_opt |
88525e1b | 1919 | (cp_parser *, bool); |
a723baf1 MM |
1920 | static bool cp_parser_constructor_declarator_p |
1921 | (cp_parser *, bool); | |
1922 | static tree cp_parser_function_definition_from_specifiers_and_declarator | |
62d1db17 | 1923 | (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *); |
a723baf1 | 1924 | static tree cp_parser_function_definition_after_declarator |
94edc4ab | 1925 | (cp_parser *, bool); |
a723baf1 | 1926 | static void cp_parser_template_declaration_after_export |
94edc4ab | 1927 | (cp_parser *, bool); |
6b648482 | 1928 | static void cp_parser_perform_template_parameter_access_checks |
d6b418fa | 1929 | (VEC (deferred_access_check,gc)*); |
a723baf1 | 1930 | static tree cp_parser_single_declaration |
736b8100 | 1931 | (cp_parser *, VEC (deferred_access_check,gc)*, bool, bool, bool *); |
a723baf1 | 1932 | static tree cp_parser_functional_cast |
94edc4ab | 1933 | (cp_parser *, tree); |
4bb8ca28 | 1934 | static tree cp_parser_save_member_function_body |
62d1db17 | 1935 | (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree); |
ec75414f MM |
1936 | static tree cp_parser_enclosed_template_argument_list |
1937 | (cp_parser *); | |
8db1028e NS |
1938 | static void cp_parser_save_default_args |
1939 | (cp_parser *, tree); | |
a723baf1 | 1940 | static void cp_parser_late_parsing_for_member |
94edc4ab | 1941 | (cp_parser *, tree); |
a723baf1 | 1942 | static void cp_parser_late_parsing_default_args |
8218bd34 | 1943 | (cp_parser *, tree); |
a723baf1 | 1944 | static tree cp_parser_sizeof_operand |
94edc4ab | 1945 | (cp_parser *, enum rid); |
cb68ec50 PC |
1946 | static tree cp_parser_trait_expr |
1947 | (cp_parser *, enum rid); | |
a723baf1 | 1948 | static bool cp_parser_declares_only_class_p |
94edc4ab | 1949 | (cp_parser *); |
62d1db17 | 1950 | static void cp_parser_set_storage_class |
2b7a3abf | 1951 | (cp_parser *, cp_decl_specifier_seq *, enum rid, location_t); |
98ca843c | 1952 | static void cp_parser_set_decl_spec_type |
760b18ad | 1953 | (cp_decl_specifier_seq *, tree, location_t, bool); |
a723baf1 | 1954 | static bool cp_parser_friend_p |
62d1db17 | 1955 | (const cp_decl_specifier_seq *); |
a723baf1 | 1956 | static cp_token *cp_parser_require |
94edc4ab | 1957 | (cp_parser *, enum cpp_ttype, const char *); |
a723baf1 | 1958 | static cp_token *cp_parser_require_keyword |
94edc4ab | 1959 | (cp_parser *, enum rid, const char *); |
21526606 | 1960 | static bool cp_parser_token_starts_function_definition_p |
94edc4ab | 1961 | (cp_token *); |
a723baf1 MM |
1962 | static bool cp_parser_next_token_starts_class_definition_p |
1963 | (cp_parser *); | |
d17811fd MM |
1964 | static bool cp_parser_next_token_ends_template_argument_p |
1965 | (cp_parser *); | |
f4abade9 GB |
1966 | static bool cp_parser_nth_token_starts_template_argument_list_p |
1967 | (cp_parser *, size_t); | |
a723baf1 | 1968 | static enum tag_types cp_parser_token_is_class_key |
94edc4ab | 1969 | (cp_token *); |
a723baf1 MM |
1970 | static void cp_parser_check_class_key |
1971 | (enum tag_types, tree type); | |
37d407a1 | 1972 | static void cp_parser_check_access_in_redeclaration |
2b7a3abf | 1973 | (tree type, location_t location); |
a723baf1 MM |
1974 | static bool cp_parser_optional_template_keyword |
1975 | (cp_parser *); | |
21526606 | 1976 | static void cp_parser_pre_parsed_nested_name_specifier |
2050a1bb | 1977 | (cp_parser *); |
09357846 | 1978 | static bool cp_parser_cache_group |
c162c75e | 1979 | (cp_parser *, enum cpp_ttype, unsigned); |
21526606 | 1980 | static void cp_parser_parse_tentatively |
94edc4ab | 1981 | (cp_parser *); |
a723baf1 | 1982 | static void cp_parser_commit_to_tentative_parse |
94edc4ab | 1983 | (cp_parser *); |
a723baf1 | 1984 | static void cp_parser_abort_tentative_parse |
94edc4ab | 1985 | (cp_parser *); |
a723baf1 | 1986 | static bool cp_parser_parse_definitely |
94edc4ab | 1987 | (cp_parser *); |
f7b5ecd9 | 1988 | static inline bool cp_parser_parsing_tentatively |
94edc4ab | 1989 | (cp_parser *); |
0b16f8f4 | 1990 | static bool cp_parser_uncommitted_to_tentative_parse_p |
94edc4ab | 1991 | (cp_parser *); |
a723baf1 | 1992 | static void cp_parser_error |
94edc4ab | 1993 | (cp_parser *, const char *); |
4bb8ca28 | 1994 | static void cp_parser_name_lookup_error |
2b7a3abf | 1995 | (cp_parser *, tree, tree, const char *, location_t); |
e5976695 | 1996 | static bool cp_parser_simulate_error |
94edc4ab | 1997 | (cp_parser *); |
d19b84e9 | 1998 | static bool cp_parser_check_type_definition |
94edc4ab | 1999 | (cp_parser *); |
560ad596 | 2000 | static void cp_parser_check_for_definition_in_return_type |
760b18ad | 2001 | (cp_declarator *, tree, location_t type_location); |
ee43dab5 | 2002 | static void cp_parser_check_for_invalid_template_id |
760b18ad | 2003 | (cp_parser *, tree, location_t location); |
625cbf93 MM |
2004 | static bool cp_parser_non_integral_constant_expression |
2005 | (cp_parser *, const char *); | |
2097b5f2 | 2006 | static void cp_parser_diagnose_invalid_type_name |
2b7a3abf | 2007 | (cp_parser *, tree, tree, location_t); |
2097b5f2 | 2008 | static bool cp_parser_parse_and_diagnose_invalid_type_name |
8fbc5ae7 | 2009 | (cp_parser *); |
7efa3e22 | 2010 | static int cp_parser_skip_to_closing_parenthesis |
a668c6ad | 2011 | (cp_parser *, bool, bool, bool); |
a723baf1 | 2012 | static void cp_parser_skip_to_end_of_statement |
94edc4ab | 2013 | (cp_parser *); |
e0860732 MM |
2014 | static void cp_parser_consume_semicolon_at_end_of_statement |
2015 | (cp_parser *); | |
a723baf1 | 2016 | static void cp_parser_skip_to_end_of_block_or_statement |
94edc4ab | 2017 | (cp_parser *); |
1094da91 | 2018 | static bool cp_parser_skip_to_closing_brace |
a723baf1 | 2019 | (cp_parser *); |
a7b9d08c VR |
2020 | static void cp_parser_skip_to_end_of_template_parameter_list |
2021 | (cp_parser *); | |
bc4071dd RH |
2022 | static void cp_parser_skip_to_pragma_eol |
2023 | (cp_parser*, cp_token *); | |
a723baf1 | 2024 | static bool cp_parser_error_occurred |
94edc4ab | 2025 | (cp_parser *); |
a723baf1 | 2026 | static bool cp_parser_allow_gnu_extensions_p |
94edc4ab | 2027 | (cp_parser *); |
a723baf1 | 2028 | static bool cp_parser_is_string_literal |
94edc4ab | 2029 | (cp_token *); |
21526606 | 2030 | static bool cp_parser_is_keyword |
94edc4ab | 2031 | (cp_token *, enum rid); |
2097b5f2 | 2032 | static tree cp_parser_make_typename_type |
2b7a3abf | 2033 | (cp_parser *, tree, tree, location_t location); |
8af2fec4 RY |
2034 | static cp_declarator * cp_parser_make_indirect_declarator |
2035 | (enum tree_code, tree, cp_cv_quals, cp_declarator *); | |
a723baf1 | 2036 | |
4de8668e | 2037 | /* Returns nonzero if we are parsing tentatively. */ |
f7b5ecd9 MM |
2038 | |
2039 | static inline bool | |
94edc4ab | 2040 | cp_parser_parsing_tentatively (cp_parser* parser) |
f7b5ecd9 MM |
2041 | { |
2042 | return parser->context->next != NULL; | |
2043 | } | |
2044 | ||
4de8668e | 2045 | /* Returns nonzero if TOKEN is a string literal. */ |
a723baf1 MM |
2046 | |
2047 | static bool | |
94edc4ab | 2048 | cp_parser_is_string_literal (cp_token* token) |
a723baf1 | 2049 | { |
b6baa67d KVH |
2050 | return (token->type == CPP_STRING || |
2051 | token->type == CPP_STRING16 || | |
2052 | token->type == CPP_STRING32 || | |
2053 | token->type == CPP_WSTRING); | |
a723baf1 MM |
2054 | } |
2055 | ||
4de8668e | 2056 | /* Returns nonzero if TOKEN is the indicated KEYWORD. */ |
a723baf1 MM |
2057 | |
2058 | static bool | |
94edc4ab | 2059 | cp_parser_is_keyword (cp_token* token, enum rid keyword) |
a723baf1 MM |
2060 | { |
2061 | return token->keyword == keyword; | |
2062 | } | |
2063 | ||
2cfe82fe ZW |
2064 | /* If not parsing tentatively, issue a diagnostic of the form |
2065 | FILE:LINE: MESSAGE before TOKEN | |
2066 | where TOKEN is the next token in the input stream. MESSAGE | |
2067 | (specified by the caller) is usually of the form "expected | |
2068 | OTHER-TOKEN". */ | |
a723baf1 MM |
2069 | |
2070 | static void | |
94edc4ab | 2071 | cp_parser_error (cp_parser* parser, const char* message) |
a723baf1 | 2072 | { |
e5976695 | 2073 | if (!cp_parser_simulate_error (parser)) |
4bb8ca28 | 2074 | { |
2cfe82fe ZW |
2075 | cp_token *token = cp_lexer_peek_token (parser->lexer); |
2076 | /* This diagnostic makes more sense if it is tagged to the line | |
2077 | of the token we just peeked at. */ | |
2078 | cp_lexer_set_source_position_from_token (token); | |
bc4071dd | 2079 | |
0d63048c MM |
2080 | if (token->type == CPP_PRAGMA) |
2081 | { | |
69bc6bff MLI |
2082 | error_at (token->location, |
2083 | "%<#pragma%> is not allowed here"); | |
bc4071dd | 2084 | cp_parser_skip_to_pragma_eol (parser, token); |
0d63048c MM |
2085 | return; |
2086 | } | |
bc4071dd | 2087 | |
21526606 | 2088 | c_parse_error (message, |
5c832178 MM |
2089 | /* Because c_parser_error does not understand |
2090 | CPP_KEYWORD, keywords are treated like | |
2091 | identifiers. */ | |
21526606 | 2092 | (token->type == CPP_KEYWORD ? CPP_NAME : token->type), |
cfc93532 | 2093 | token->u.value, token->flags); |
4bb8ca28 MM |
2094 | } |
2095 | } | |
2096 | ||
2097 | /* Issue an error about name-lookup failing. NAME is the | |
2098 | IDENTIFIER_NODE DECL is the result of | |
2099 | the lookup (as returned from cp_parser_lookup_name). DESIRED is | |
2100 | the thing that we hoped to find. */ | |
2101 | ||
2102 | static void | |
2103 | cp_parser_name_lookup_error (cp_parser* parser, | |
2104 | tree name, | |
2105 | tree decl, | |
2b7a3abf DS |
2106 | const char* desired, |
2107 | location_t location) | |
4bb8ca28 MM |
2108 | { |
2109 | /* If name lookup completely failed, tell the user that NAME was not | |
2110 | declared. */ | |
2111 | if (decl == error_mark_node) | |
2112 | { | |
2113 | if (parser->scope && parser->scope != global_namespace) | |
69bc6bff MLI |
2114 | error_at (location, "%<%E::%E%> has not been declared", |
2115 | parser->scope, name); | |
4bb8ca28 | 2116 | else if (parser->scope == global_namespace) |
69bc6bff | 2117 | error_at (location, "%<::%E%> has not been declared", name); |
c8094d83 | 2118 | else if (parser->object_scope |
b14454ba | 2119 | && !CLASS_TYPE_P (parser->object_scope)) |
69bc6bff MLI |
2120 | error_at (location, "request for member %qE in non-class type %qT", |
2121 | name, parser->object_scope); | |
b14454ba | 2122 | else if (parser->object_scope) |
69bc6bff MLI |
2123 | error_at (location, "%<%T::%E%> has not been declared", |
2124 | parser->object_scope, name); | |
4bb8ca28 | 2125 | else |
69bc6bff | 2126 | error_at (location, "%qE has not been declared", name); |
4bb8ca28 MM |
2127 | } |
2128 | else if (parser->scope && parser->scope != global_namespace) | |
69bc6bff | 2129 | error_at (location, "%<%E::%E%> %s", parser->scope, name, desired); |
4bb8ca28 | 2130 | else if (parser->scope == global_namespace) |
69bc6bff | 2131 | error_at (location, "%<::%E%> %s", name, desired); |
4bb8ca28 | 2132 | else |
69bc6bff | 2133 | error_at (location, "%qE %s", name, desired); |
a723baf1 MM |
2134 | } |
2135 | ||
2136 | /* If we are parsing tentatively, remember that an error has occurred | |
e5976695 | 2137 | during this tentative parse. Returns true if the error was |
77077b39 | 2138 | simulated; false if a message should be issued by the caller. */ |
a723baf1 | 2139 | |
e5976695 | 2140 | static bool |
94edc4ab | 2141 | cp_parser_simulate_error (cp_parser* parser) |
a723baf1 | 2142 | { |
0b16f8f4 | 2143 | if (cp_parser_uncommitted_to_tentative_parse_p (parser)) |
e5976695 MM |
2144 | { |
2145 | parser->context->status = CP_PARSER_STATUS_KIND_ERROR; | |
2146 | return true; | |
2147 | } | |
2148 | return false; | |
a723baf1 MM |
2149 | } |
2150 | ||
856367df VR |
2151 | /* Check for repeated decl-specifiers. */ |
2152 | ||
2153 | static void | |
2b7a3abf DS |
2154 | cp_parser_check_decl_spec (cp_decl_specifier_seq *decl_specs, |
2155 | location_t location) | |
856367df | 2156 | { |
09639a83 | 2157 | int ds; |
856367df VR |
2158 | |
2159 | for (ds = ds_first; ds != ds_last; ++ds) | |
2160 | { | |
09639a83 | 2161 | unsigned count = decl_specs->specs[ds]; |
856367df VR |
2162 | if (count < 2) |
2163 | continue; | |
2164 | /* The "long" specifier is a special case because of "long long". */ | |
2165 | if (ds == ds_long) | |
2166 | { | |
2167 | if (count > 2) | |
69bc6bff | 2168 | error_at (location, "%<long long long%> is too long for GCC"); |
9c650d90 MLI |
2169 | else |
2170 | pedwarn_cxx98 (location, OPT_Wlong_long, | |
2171 | "ISO C++ 1998 does not support %<long long%>"); | |
856367df VR |
2172 | } |
2173 | else if (count > 1) | |
2174 | { | |
2175 | static const char *const decl_spec_names[] = { | |
2176 | "signed", | |
2177 | "unsigned", | |
2178 | "short", | |
2179 | "long", | |
2180 | "const", | |
2181 | "volatile", | |
2182 | "restrict", | |
2183 | "inline", | |
2184 | "virtual", | |
2185 | "explicit", | |
2186 | "friend", | |
2187 | "typedef", | |
2188 | "__complex", | |
2189 | "__thread" | |
2190 | }; | |
69bc6bff | 2191 | error_at (location, "duplicate %qs", decl_spec_names[ds]); |
856367df VR |
2192 | } |
2193 | } | |
2194 | } | |
2195 | ||
a723baf1 MM |
2196 | /* This function is called when a type is defined. If type |
2197 | definitions are forbidden at this point, an error message is | |
2198 | issued. */ | |
2199 | ||
d19b84e9 | 2200 | static bool |
94edc4ab | 2201 | cp_parser_check_type_definition (cp_parser* parser) |
a723baf1 MM |
2202 | { |
2203 | /* If types are forbidden here, issue a message. */ | |
2204 | if (parser->type_definition_forbidden_message) | |
d19b84e9 | 2205 | { |
8b4efb4e VR |
2206 | /* Don't use `%s' to print the string, because quotations (`%<', `%>') |
2207 | in the message need to be interpreted. */ | |
2208 | error (parser->type_definition_forbidden_message); | |
d19b84e9 MM |
2209 | return false; |
2210 | } | |
2211 | return true; | |
a723baf1 MM |
2212 | } |
2213 | ||
fc6a28d7 | 2214 | /* This function is called when the DECLARATOR is processed. The TYPE |
472c29c3 | 2215 | was a type defined in the decl-specifiers. If it is invalid to |
fc6a28d7 | 2216 | define a type in the decl-specifiers for DECLARATOR, an error is |
760b18ad DS |
2217 | issued. TYPE_LOCATION is the location of TYPE and is used |
2218 | for error reporting. */ | |
560ad596 MM |
2219 | |
2220 | static void | |
058b15c1 | 2221 | cp_parser_check_for_definition_in_return_type (cp_declarator *declarator, |
760b18ad | 2222 | tree type, location_t type_location) |
560ad596 MM |
2223 | { |
2224 | /* [dcl.fct] forbids type definitions in return types. | |
2225 | Unfortunately, it's not easy to know whether or not we are | |
2226 | processing a return type until after the fact. */ | |
2227 | while (declarator | |
058b15c1 MM |
2228 | && (declarator->kind == cdk_pointer |
2229 | || declarator->kind == cdk_reference | |
2230 | || declarator->kind == cdk_ptrmem)) | |
2231 | declarator = declarator->declarator; | |
560ad596 | 2232 | if (declarator |
fc6a28d7 MM |
2233 | && declarator->kind == cdk_function) |
2234 | { | |
69bc6bff MLI |
2235 | error_at (type_location, |
2236 | "new types may not be defined in a return type"); | |
1f5b3869 MLI |
2237 | inform (type_location, |
2238 | "(perhaps a semicolon is missing after the definition of %qT)", | |
2239 | type); | |
fc6a28d7 | 2240 | } |
560ad596 MM |
2241 | } |
2242 | ||
ee43dab5 MM |
2243 | /* A type-specifier (TYPE) has been parsed which cannot be followed by |
2244 | "<" in any valid C++ program. If the next token is indeed "<", | |
2245 | issue a message warning the user about what appears to be an | |
760b18ad DS |
2246 | invalid attempt to form a template-id. LOCATION is the location |
2247 | of the type-specifier (TYPE) */ | |
ee43dab5 MM |
2248 | |
2249 | static void | |
21526606 | 2250 | cp_parser_check_for_invalid_template_id (cp_parser* parser, |
760b18ad | 2251 | tree type, location_t location) |
ee43dab5 | 2252 | { |
0c5e4866 | 2253 | cp_token_position start = 0; |
ee43dab5 MM |
2254 | |
2255 | if (cp_lexer_next_token_is (parser->lexer, CPP_LESS)) | |
2256 | { | |
2257 | if (TYPE_P (type)) | |
69bc6bff | 2258 | error_at (location, "%qT is not a template", type); |
ee43dab5 | 2259 | else if (TREE_CODE (type) == IDENTIFIER_NODE) |
69bc6bff | 2260 | error_at (location, "%qE is not a template", type); |
ee43dab5 | 2261 | else |
69bc6bff | 2262 | error_at (location, "invalid template-id"); |
ee43dab5 | 2263 | /* Remember the location of the invalid "<". */ |
0b16f8f4 | 2264 | if (cp_parser_uncommitted_to_tentative_parse_p (parser)) |
0c5e4866 | 2265 | start = cp_lexer_token_position (parser->lexer, true); |
ee43dab5 MM |
2266 | /* Consume the "<". */ |
2267 | cp_lexer_consume_token (parser->lexer); | |
2268 | /* Parse the template arguments. */ | |
2269 | cp_parser_enclosed_template_argument_list (parser); | |
da1d7781 | 2270 | /* Permanently remove the invalid template arguments so that |
ee43dab5 | 2271 | this error message is not issued again. */ |
0c5e4866 NS |
2272 | if (start) |
2273 | cp_lexer_purge_tokens_after (parser->lexer, start); | |
ee43dab5 MM |
2274 | } |
2275 | } | |
2276 | ||
625cbf93 MM |
2277 | /* If parsing an integral constant-expression, issue an error message |
2278 | about the fact that THING appeared and return true. Otherwise, | |
93678513 | 2279 | return false. In either case, set |
c8094d83 | 2280 | PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */ |
14d22dd6 | 2281 | |
625cbf93 MM |
2282 | static bool |
2283 | cp_parser_non_integral_constant_expression (cp_parser *parser, | |
2284 | const char *thing) | |
14d22dd6 | 2285 | { |
93678513 | 2286 | parser->non_integral_constant_expression_p = true; |
625cbf93 MM |
2287 | if (parser->integral_constant_expression_p) |
2288 | { | |
2289 | if (!parser->allow_non_integral_constant_expression_p) | |
2290 | { | |
2a97bf74 VR |
2291 | /* Don't use `%s' to print THING, because quotations (`%<', `%>') |
2292 | in the message need to be interpreted. */ | |
2293 | char *message = concat (thing, | |
2294 | " cannot appear in a constant-expression", | |
2295 | NULL); | |
2296 | error (message); | |
2297 | free (message); | |
625cbf93 MM |
2298 | return true; |
2299 | } | |
625cbf93 MM |
2300 | } |
2301 | return false; | |
14d22dd6 MM |
2302 | } |
2303 | ||
0c88d886 | 2304 | /* Emit a diagnostic for an invalid type name. SCOPE is the |
6ca2d67f MM |
2305 | qualifying scope (or NULL, if none) for ID. This function commits |
2306 | to the current active tentative parse, if any. (Otherwise, the | |
2307 | problematic construct might be encountered again later, resulting | |
760b18ad | 2308 | in duplicate error messages.) LOCATION is the location of ID. */ |
8fbc5ae7 | 2309 | |
2097b5f2 | 2310 | static void |
2b7a3abf DS |
2311 | cp_parser_diagnose_invalid_type_name (cp_parser *parser, |
2312 | tree scope, tree id, | |
760b18ad | 2313 | location_t location) |
6c0cc713 GB |
2314 | { |
2315 | tree decl, old_scope; | |
2097b5f2 GB |
2316 | /* Try to lookup the identifier. */ |
2317 | old_scope = parser->scope; | |
2318 | parser->scope = scope; | |
760b18ad | 2319 | decl = cp_parser_lookup_name_simple (parser, id, location); |
2097b5f2 GB |
2320 | parser->scope = old_scope; |
2321 | /* If the lookup found a template-name, it means that the user forgot | |
c72a1a86 | 2322 | to specify an argument list. Emit a useful error message. */ |
2097b5f2 | 2323 | if (TREE_CODE (decl) == TEMPLATE_DECL) |
69bc6bff MLI |
2324 | error_at (location, |
2325 | "invalid use of template-name %qE without an argument list", | |
2326 | decl); | |
04499540 | 2327 | else if (TREE_CODE (id) == BIT_NOT_EXPR) |
69bc6bff | 2328 | error_at (location, "invalid use of destructor %qD as a type", id); |
5df1740d JM |
2329 | else if (TREE_CODE (decl) == TYPE_DECL) |
2330 | /* Something like 'unsigned A a;' */ | |
69bc6bff | 2331 | error_at (location, "invalid combination of multiple type-specifiers"); |
91b1ca65 | 2332 | else if (!parser->scope) |
8fbc5ae7 | 2333 | { |
8fbc5ae7 | 2334 | /* Issue an error message. */ |
69bc6bff | 2335 | error_at (location, "%qE does not name a type", id); |
8fbc5ae7 MM |
2336 | /* If we're in a template class, it's possible that the user was |
2337 | referring to a type from a base class. For example: | |
2338 | ||
2339 | template <typename T> struct A { typedef T X; }; | |
2340 | template <typename T> struct B : public A<T> { X x; }; | |
2341 | ||
2342 | The user should have said "typename A<T>::X". */ | |
32db39c0 PC |
2343 | if (processing_template_decl && current_class_type |
2344 | && TYPE_BINFO (current_class_type)) | |
8fbc5ae7 MM |
2345 | { |
2346 | tree b; | |
2347 | ||
2348 | for (b = TREE_CHAIN (TYPE_BINFO (current_class_type)); | |
2349 | b; | |
2350 | b = TREE_CHAIN (b)) | |
2351 | { | |
2352 | tree base_type = BINFO_TYPE (b); | |
21526606 | 2353 | if (CLASS_TYPE_P (base_type) |
1fb3244a | 2354 | && dependent_type_p (base_type)) |
8fbc5ae7 MM |
2355 | { |
2356 | tree field; | |
2357 | /* Go from a particular instantiation of the | |
2358 | template (which will have an empty TYPE_FIELDs), | |
2359 | to the main version. */ | |
353b4fc0 | 2360 | base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type); |
8fbc5ae7 MM |
2361 | for (field = TYPE_FIELDS (base_type); |
2362 | field; | |
2363 | field = TREE_CHAIN (field)) | |
2364 | if (TREE_CODE (field) == TYPE_DECL | |
2097b5f2 | 2365 | && DECL_NAME (field) == id) |
8fbc5ae7 | 2366 | { |
1f5b3869 MLI |
2367 | inform (location, |
2368 | "(perhaps %<typename %T::%E%> was intended)", | |
2369 | BINFO_TYPE (b), id); | |
8fbc5ae7 MM |
2370 | break; |
2371 | } | |
2372 | if (field) | |
2373 | break; | |
2374 | } | |
2375 | } | |
2376 | } | |
8fbc5ae7 | 2377 | } |
2097b5f2 GB |
2378 | /* Here we diagnose qualified-ids where the scope is actually correct, |
2379 | but the identifier does not resolve to a valid type name. */ | |
91b1ca65 | 2380 | else if (parser->scope != error_mark_node) |
2097b5f2 GB |
2381 | { |
2382 | if (TREE_CODE (parser->scope) == NAMESPACE_DECL) | |
69bc6bff MLI |
2383 | error_at (location, "%qE in namespace %qE does not name a type", |
2384 | id, parser->scope); | |
2097b5f2 | 2385 | else if (TYPE_P (parser->scope)) |
69bc6bff MLI |
2386 | error_at (location, "%qE in class %qT does not name a type", |
2387 | id, parser->scope); | |
2097b5f2 | 2388 | else |
315fb5db | 2389 | gcc_unreachable (); |
2097b5f2 | 2390 | } |
6ca2d67f | 2391 | cp_parser_commit_to_tentative_parse (parser); |
2097b5f2 | 2392 | } |
8fbc5ae7 | 2393 | |
2097b5f2 GB |
2394 | /* Check for a common situation where a type-name should be present, |
2395 | but is not, and issue a sensible error message. Returns true if an | |
2396 | invalid type-name was detected. | |
21526606 | 2397 | |
2097b5f2 | 2398 | The situation handled by this function are variable declarations of the |
21526606 EC |
2399 | form `ID a', where `ID' is an id-expression and `a' is a plain identifier. |
2400 | Usually, `ID' should name a type, but if we got here it means that it | |
2097b5f2 | 2401 | does not. We try to emit the best possible error message depending on |
3db45ab5 | 2402 | how exactly the id-expression looks like. */ |
2097b5f2 GB |
2403 | |
2404 | static bool | |
2405 | cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser) | |
2406 | { | |
2407 | tree id; | |
2b7a3abf | 2408 | cp_token *token = cp_lexer_peek_token (parser->lexer); |
2097b5f2 GB |
2409 | |
2410 | cp_parser_parse_tentatively (parser); | |
21526606 | 2411 | id = cp_parser_id_expression (parser, |
2097b5f2 GB |
2412 | /*template_keyword_p=*/false, |
2413 | /*check_dependency_p=*/true, | |
2414 | /*template_p=*/NULL, | |
fa6098f8 | 2415 | /*declarator_p=*/true, |
88525e1b | 2416 | /*optional_p=*/false); |
2097b5f2 GB |
2417 | /* After the id-expression, there should be a plain identifier, |
2418 | otherwise this is not a simple variable declaration. Also, if | |
2419 | the scope is dependent, we cannot do much. */ | |
2420 | if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME) | |
21526606 | 2421 | || (parser->scope && TYPE_P (parser->scope) |
8b84995a MM |
2422 | && dependent_type_p (parser->scope)) |
2423 | || TREE_CODE (id) == TYPE_DECL) | |
2097b5f2 GB |
2424 | { |
2425 | cp_parser_abort_tentative_parse (parser); | |
2426 | return false; | |
2427 | } | |
8b84995a | 2428 | if (!cp_parser_parse_definitely (parser)) |
2097b5f2 GB |
2429 | return false; |
2430 | ||
2097b5f2 | 2431 | /* Emit a diagnostic for the invalid type. */ |
2b7a3abf DS |
2432 | cp_parser_diagnose_invalid_type_name (parser, parser->scope, |
2433 | id, token->location); | |
2097b5f2 GB |
2434 | /* Skip to the end of the declaration; there's no point in |
2435 | trying to process it. */ | |
2436 | cp_parser_skip_to_end_of_block_or_statement (parser); | |
2437 | return true; | |
8fbc5ae7 MM |
2438 | } |
2439 | ||
21526606 | 2440 | /* Consume tokens up to, and including, the next non-nested closing `)'. |
7efa3e22 NS |
2441 | Returns 1 iff we found a closing `)'. RECOVERING is true, if we |
2442 | are doing error recovery. Returns -1 if OR_COMMA is true and we | |
2443 | found an unnested comma. */ | |
a723baf1 | 2444 | |
7efa3e22 NS |
2445 | static int |
2446 | cp_parser_skip_to_closing_parenthesis (cp_parser *parser, | |
21526606 | 2447 | bool recovering, |
a668c6ad MM |
2448 | bool or_comma, |
2449 | bool consume_paren) | |
a723baf1 | 2450 | { |
7efa3e22 NS |
2451 | unsigned paren_depth = 0; |
2452 | unsigned brace_depth = 0; | |
a723baf1 | 2453 | |
0b16f8f4 VR |
2454 | if (recovering && !or_comma |
2455 | && cp_parser_uncommitted_to_tentative_parse_p (parser)) | |
7efa3e22 | 2456 | return 0; |
21526606 | 2457 | |
a723baf1 MM |
2458 | while (true) |
2459 | { | |
bc4071dd | 2460 | cp_token * token = cp_lexer_peek_token (parser->lexer); |
21526606 | 2461 | |
bc4071dd | 2462 | switch (token->type) |
0173bb6f | 2463 | { |
bc4071dd RH |
2464 | case CPP_EOF: |
2465 | case CPP_PRAGMA_EOL: | |
2466 | /* If we've run out of tokens, then there is no closing `)'. */ | |
2467 | return 0; | |
a723baf1 | 2468 | |
bc4071dd RH |
2469 | case CPP_SEMICOLON: |
2470 | /* This matches the processing in skip_to_end_of_statement. */ | |
2471 | if (!brace_depth) | |
2472 | return 0; | |
2473 | break; | |
21526606 | 2474 | |
bc4071dd RH |
2475 | case CPP_OPEN_BRACE: |
2476 | ++brace_depth; | |
0173bb6f | 2477 | break; |
bc4071dd | 2478 | case CPP_CLOSE_BRACE: |
a668c6ad | 2479 | if (!brace_depth--) |
bc4071dd | 2480 | return 0; |
0173bb6f | 2481 | break; |
21526606 | 2482 | |
bc4071dd RH |
2483 | case CPP_COMMA: |
2484 | if (recovering && or_comma && !brace_depth && !paren_depth) | |
2485 | return -1; | |
2486 | break; | |
2487 | ||
2488 | case CPP_OPEN_PAREN: | |
2489 | if (!brace_depth) | |
7efa3e22 | 2490 | ++paren_depth; |
bc4071dd RH |
2491 | break; |
2492 | ||
2493 | case CPP_CLOSE_PAREN: | |
2494 | if (!brace_depth && !paren_depth--) | |
a668c6ad MM |
2495 | { |
2496 | if (consume_paren) | |
2497 | cp_lexer_consume_token (parser->lexer); | |
bc4071dd | 2498 | return 1; |
a668c6ad | 2499 | } |
bc4071dd RH |
2500 | break; |
2501 | ||
2502 | default: | |
2503 | break; | |
7efa3e22 | 2504 | } |
21526606 | 2505 | |
a668c6ad MM |
2506 | /* Consume the token. */ |
2507 | cp_lexer_consume_token (parser->lexer); | |
a723baf1 MM |
2508 | } |
2509 | } | |
2510 | ||
2511 | /* Consume tokens until we reach the end of the current statement. | |
2512 | Normally, that will be just before consuming a `;'. However, if a | |
2513 | non-nested `}' comes first, then we stop before consuming that. */ | |
2514 | ||
2515 | static void | |
94edc4ab | 2516 | cp_parser_skip_to_end_of_statement (cp_parser* parser) |
a723baf1 MM |
2517 | { |
2518 | unsigned nesting_depth = 0; | |
2519 | ||
2520 | while (true) | |
2521 | { | |
bc4071dd | 2522 | cp_token *token = cp_lexer_peek_token (parser->lexer); |
a723baf1 | 2523 | |
bc4071dd | 2524 | switch (token->type) |
a723baf1 | 2525 | { |
bc4071dd RH |
2526 | case CPP_EOF: |
2527 | case CPP_PRAGMA_EOL: | |
2528 | /* If we've run out of tokens, stop. */ | |
2529 | return; | |
2530 | ||
2531 | case CPP_SEMICOLON: | |
2532 | /* If the next token is a `;', we have reached the end of the | |
2533 | statement. */ | |
2534 | if (!nesting_depth) | |
2535 | return; | |
2536 | break; | |
2537 | ||
2538 | case CPP_CLOSE_BRACE: | |
2539 | /* If this is a non-nested '}', stop before consuming it. | |
a723baf1 MM |
2540 | That way, when confronted with something like: |
2541 | ||
21526606 | 2542 | { 3 + } |
a723baf1 | 2543 | |
bc4071dd | 2544 | we stop before consuming the closing '}', even though we |
a723baf1 MM |
2545 | have not yet reached a `;'. */ |
2546 | if (nesting_depth == 0) | |
bc4071dd RH |
2547 | return; |
2548 | ||
2549 | /* If it is the closing '}' for a block that we have | |
a723baf1 MM |
2550 | scanned, stop -- but only after consuming the token. |
2551 | That way given: | |
2552 | ||
0cbd7506 | 2553 | void f g () { ... } |
a723baf1 MM |
2554 | typedef int I; |
2555 | ||
2556 | we will stop after the body of the erroneously declared | |
2557 | function, but before consuming the following `typedef' | |
2558 | declaration. */ | |
2559 | if (--nesting_depth == 0) | |
2560 | { | |
2561 | cp_lexer_consume_token (parser->lexer); | |
bc4071dd | 2562 | return; |
a723baf1 | 2563 | } |
bc4071dd RH |
2564 | |
2565 | case CPP_OPEN_BRACE: | |
2566 | ++nesting_depth; | |
2567 | break; | |
2568 | ||
2569 | default: | |
2570 | break; | |
a723baf1 | 2571 | } |
bc4071dd | 2572 | |
a723baf1 MM |
2573 | /* Consume the token. */ |
2574 | cp_lexer_consume_token (parser->lexer); | |
2575 | } | |
2576 | } | |
2577 | ||
e0860732 MM |
2578 | /* This function is called at the end of a statement or declaration. |
2579 | If the next token is a semicolon, it is consumed; otherwise, error | |
2580 | recovery is attempted. */ | |
2581 | ||
2582 | static void | |
2583 | cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser) | |
2584 | { | |
2585 | /* Look for the trailing `;'. */ | |
3a483371 | 2586 | if (!cp_parser_require (parser, CPP_SEMICOLON, "%<;%>")) |
e0860732 MM |
2587 | { |
2588 | /* If there is additional (erroneous) input, skip to the end of | |
2589 | the statement. */ | |
2590 | cp_parser_skip_to_end_of_statement (parser); | |
2591 | /* If the next token is now a `;', consume it. */ | |
2592 | if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)) | |
2593 | cp_lexer_consume_token (parser->lexer); | |
2594 | } | |
2595 | } | |
2596 | ||
a723baf1 MM |
2597 | /* Skip tokens until we have consumed an entire block, or until we |
2598 | have consumed a non-nested `;'. */ | |
2599 | ||
2600 | static void | |
94edc4ab | 2601 | cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser) |
a723baf1 | 2602 | { |
8fe4d24b | 2603 | int nesting_depth = 0; |
a723baf1 | 2604 | |
8fe4d24b | 2605 | while (nesting_depth >= 0) |
a723baf1 | 2606 | { |
8fe4d24b | 2607 | cp_token *token = cp_lexer_peek_token (parser->lexer); |
c8094d83 | 2608 | |
8fe4d24b | 2609 | switch (token->type) |
a723baf1 | 2610 | { |
8fe4d24b | 2611 | case CPP_EOF: |
bc4071dd | 2612 | case CPP_PRAGMA_EOL: |
8fe4d24b | 2613 | /* If we've run out of tokens, stop. */ |
bc4071dd | 2614 | return; |
8fe4d24b NS |
2615 | |
2616 | case CPP_SEMICOLON: | |
2617 | /* Stop if this is an unnested ';'. */ | |
2618 | if (!nesting_depth) | |
2619 | nesting_depth = -1; | |
2620 | break; | |
2621 | ||
2622 | case CPP_CLOSE_BRACE: | |
2623 | /* Stop if this is an unnested '}', or closes the outermost | |
2624 | nesting level. */ | |
2625 | nesting_depth--; | |
72b75d03 L |
2626 | if (nesting_depth < 0) |
2627 | return; | |
8fe4d24b NS |
2628 | if (!nesting_depth) |
2629 | nesting_depth = -1; | |
2630 | break; | |
c8094d83 | 2631 | |
8fe4d24b NS |
2632 | case CPP_OPEN_BRACE: |
2633 | /* Nest. */ | |
2634 | nesting_depth++; | |
2635 | break; | |
2636 | ||
2637 | default: | |
a723baf1 MM |
2638 | break; |
2639 | } | |
c8094d83 | 2640 | |
a723baf1 | 2641 | /* Consume the token. */ |
8fe4d24b | 2642 | cp_lexer_consume_token (parser->lexer); |
a723baf1 MM |
2643 | } |
2644 | } | |
2645 | ||
2646 | /* Skip tokens until a non-nested closing curly brace is the next | |
1094da91 SM |
2647 | token, or there are no more tokens. Return true in the first case, |
2648 | false otherwise. */ | |
a723baf1 | 2649 | |
1094da91 | 2650 | static bool |
a723baf1 MM |
2651 | cp_parser_skip_to_closing_brace (cp_parser *parser) |
2652 | { | |
2653 | unsigned nesting_depth = 0; | |
2654 | ||
2655 | while (true) | |
2656 | { | |
bc4071dd RH |
2657 | cp_token *token = cp_lexer_peek_token (parser->lexer); |
2658 | ||
2659 | switch (token->type) | |
2660 | { | |
2661 | case CPP_EOF: | |
2662 | case CPP_PRAGMA_EOL: | |
2663 | /* If we've run out of tokens, stop. */ | |
1094da91 | 2664 | return false; |
bc4071dd RH |
2665 | |
2666 | case CPP_CLOSE_BRACE: | |
2667 | /* If the next token is a non-nested `}', then we have reached | |
2668 | the end of the current block. */ | |
2669 | if (nesting_depth-- == 0) | |
1094da91 | 2670 | return true; |
bc4071dd RH |
2671 | break; |
2672 | ||
2673 | case CPP_OPEN_BRACE: | |
2674 | /* If it the next token is a `{', then we are entering a new | |
2675 | block. Consume the entire block. */ | |
2676 | ++nesting_depth; | |
2677 | break; | |
2678 | ||
2679 | default: | |
2680 | break; | |
2681 | } | |
a723baf1 | 2682 | |
a723baf1 MM |
2683 | /* Consume the token. */ |
2684 | cp_lexer_consume_token (parser->lexer); | |
2685 | } | |
2686 | } | |
2687 | ||
bc4071dd RH |
2688 | /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK |
2689 | parameter is the PRAGMA token, allowing us to purge the entire pragma | |
2690 | sequence. */ | |
2691 | ||
2692 | static void | |
2693 | cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok) | |
2694 | { | |
2695 | cp_token *token; | |
2696 | ||
2697 | parser->lexer->in_pragma = false; | |
2698 | ||
2699 | do | |
2700 | token = cp_lexer_consume_token (parser->lexer); | |
2701 | while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF); | |
2702 | ||
2703 | /* Ensure that the pragma is not parsed again. */ | |
2704 | cp_lexer_purge_tokens_after (parser->lexer, pragma_tok); | |
2705 | } | |
2706 | ||
1799e5d5 RH |
2707 | /* Require pragma end of line, resyncing with it as necessary. The |
2708 | arguments are as for cp_parser_skip_to_pragma_eol. */ | |
2709 | ||
2710 | static void | |
2711 | cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok) | |
2712 | { | |
2713 | parser->lexer->in_pragma = false; | |
2714 | if (!cp_parser_require (parser, CPP_PRAGMA_EOL, "end of line")) | |
2715 | cp_parser_skip_to_pragma_eol (parser, pragma_tok); | |
2716 | } | |
2717 | ||
2097b5f2 GB |
2718 | /* This is a simple wrapper around make_typename_type. When the id is |
2719 | an unresolved identifier node, we can provide a superior diagnostic | |
2720 | using cp_parser_diagnose_invalid_type_name. */ | |
2721 | ||
2722 | static tree | |
2b7a3abf DS |
2723 | cp_parser_make_typename_type (cp_parser *parser, tree scope, |
2724 | tree id, location_t id_location) | |
6c0cc713 GB |
2725 | { |
2726 | tree result; | |
2727 | if (TREE_CODE (id) == IDENTIFIER_NODE) | |
2728 | { | |
fc6a28d7 | 2729 | result = make_typename_type (scope, id, typename_type, |
3db45ab5 | 2730 | /*complain=*/tf_none); |
6c0cc713 | 2731 | if (result == error_mark_node) |
2b7a3abf | 2732 | cp_parser_diagnose_invalid_type_name (parser, scope, id, id_location); |
6c0cc713 GB |
2733 | return result; |
2734 | } | |
fc6a28d7 | 2735 | return make_typename_type (scope, id, typename_type, tf_error); |
2097b5f2 GB |
2736 | } |
2737 | ||
8af2fec4 RY |
2738 | /* This is a wrapper around the |
2739 | make_{pointer,ptrmem,reference}_declarator functions that decides | |
2740 | which one to call based on the CODE and CLASS_TYPE arguments. The | |
2741 | CODE argument should be one of the values returned by | |
2742 | cp_parser_ptr_operator. */ | |
2743 | static cp_declarator * | |
2744 | cp_parser_make_indirect_declarator (enum tree_code code, tree class_type, | |
2745 | cp_cv_quals cv_qualifiers, | |
2746 | cp_declarator *target) | |
2747 | { | |
b8ade407 PC |
2748 | if (code == ERROR_MARK) |
2749 | return cp_error_declarator; | |
2750 | ||
8af2fec4 RY |
2751 | if (code == INDIRECT_REF) |
2752 | if (class_type == NULL_TREE) | |
2753 | return make_pointer_declarator (cv_qualifiers, target); | |
2754 | else | |
2755 | return make_ptrmem_declarator (cv_qualifiers, class_type, target); | |
2756 | else if (code == ADDR_EXPR && class_type == NULL_TREE) | |
2757 | return make_reference_declarator (cv_qualifiers, target, false); | |
2758 | else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE) | |
2759 | return make_reference_declarator (cv_qualifiers, target, true); | |
2760 | gcc_unreachable (); | |
2761 | } | |
2097b5f2 | 2762 | |
a723baf1 MM |
2763 | /* Create a new C++ parser. */ |
2764 | ||
2765 | static cp_parser * | |
94edc4ab | 2766 | cp_parser_new (void) |
a723baf1 MM |
2767 | { |
2768 | cp_parser *parser; | |
17211ab5 | 2769 | cp_lexer *lexer; |
b8b94c5b | 2770 | unsigned i; |
17211ab5 GK |
2771 | |
2772 | /* cp_lexer_new_main is called before calling ggc_alloc because | |
2773 | cp_lexer_new_main might load a PCH file. */ | |
2774 | lexer = cp_lexer_new_main (); | |
a723baf1 | 2775 | |
b8b94c5b PB |
2776 | /* Initialize the binops_by_token so that we can get the tree |
2777 | directly from the token. */ | |
2778 | for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++) | |
2779 | binops_by_token[binops[i].token_type] = binops[i]; | |
2780 | ||
99dd239f | 2781 | parser = GGC_CNEW (cp_parser); |
17211ab5 | 2782 | parser->lexer = lexer; |
a723baf1 MM |
2783 | parser->context = cp_parser_context_new (NULL); |
2784 | ||
2785 | /* For now, we always accept GNU extensions. */ | |
2786 | parser->allow_gnu_extensions_p = 1; | |
2787 | ||
2788 | /* The `>' token is a greater-than operator, not the end of a | |
2789 | template-id. */ | |
2790 | parser->greater_than_is_operator_p = true; | |
2791 | ||
2792 | parser->default_arg_ok_p = true; | |
21526606 | 2793 | |
a723baf1 | 2794 | /* We are not parsing a constant-expression. */ |
67c03833 JM |
2795 | parser->integral_constant_expression_p = false; |
2796 | parser->allow_non_integral_constant_expression_p = false; | |
2797 | parser->non_integral_constant_expression_p = false; | |
a723baf1 MM |
2798 | |
2799 | /* Local variable names are not forbidden. */ | |
2800 | parser->local_variables_forbidden_p = false; | |
2801 | ||
34cd5ae7 | 2802 | /* We are not processing an `extern "C"' declaration. */ |
a723baf1 MM |
2803 | parser->in_unbraced_linkage_specification_p = false; |
2804 | ||
2805 | /* We are not processing a declarator. */ | |
2806 | parser->in_declarator_p = false; | |
2807 | ||
4bb8ca28 MM |
2808 | /* We are not processing a template-argument-list. */ |
2809 | parser->in_template_argument_list_p = false; | |
2810 | ||
0e59b3fb | 2811 | /* We are not in an iteration statement. */ |
1799e5d5 | 2812 | parser->in_statement = 0; |
0e59b3fb MM |
2813 | |
2814 | /* We are not in a switch statement. */ | |
2815 | parser->in_switch_statement_p = false; | |
2816 | ||
4f8163b1 MM |
2817 | /* We are not parsing a type-id inside an expression. */ |
2818 | parser->in_type_id_in_expr_p = false; | |
2819 | ||
03fd3f84 | 2820 | /* Declarations aren't implicitly extern "C". */ |
7d381002 MA |
2821 | parser->implicit_extern_c = false; |
2822 | ||
c162c75e MA |
2823 | /* String literals should be translated to the execution character set. */ |
2824 | parser->translate_strings_p = true; | |
2825 | ||
56c7f9c1 MM |
2826 | /* We are not parsing a function body. */ |
2827 | parser->in_function_body = false; | |
2828 | ||
a723baf1 MM |
2829 | /* The unparsed function queue is empty. */ |
2830 | parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE); | |
2831 | ||
2832 | /* There are no classes being defined. */ | |
2833 | parser->num_classes_being_defined = 0; | |
2834 | ||
2835 | /* No template parameters apply. */ | |
2836 | parser->num_template_parameter_lists = 0; | |
2837 | ||
2838 | return parser; | |
2839 | } | |
2840 | ||
2cfe82fe ZW |
2841 | /* Create a cp_lexer structure which will emit the tokens in CACHE |
2842 | and push it onto the parser's lexer stack. This is used for delayed | |
2843 | parsing of in-class method bodies and default arguments, and should | |
2844 | not be confused with tentative parsing. */ | |
2845 | static void | |
2846 | cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache) | |
2847 | { | |
2848 | cp_lexer *lexer = cp_lexer_new_from_tokens (cache); | |
2849 | lexer->next = parser->lexer; | |
2850 | parser->lexer = lexer; | |
2851 | ||
2852 | /* Move the current source position to that of the first token in the | |
2853 | new lexer. */ | |
2854 | cp_lexer_set_source_position_from_token (lexer->next_token); | |
2855 | } | |
2856 | ||
2857 | /* Pop the top lexer off the parser stack. This is never used for the | |
2858 | "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */ | |
2859 | static void | |
2860 | cp_parser_pop_lexer (cp_parser *parser) | |
2861 | { | |
2862 | cp_lexer *lexer = parser->lexer; | |
2863 | parser->lexer = lexer->next; | |
2864 | cp_lexer_destroy (lexer); | |
2865 | ||
2866 | /* Put the current source position back where it was before this | |
2867 | lexer was pushed. */ | |
2868 | cp_lexer_set_source_position_from_token (parser->lexer->next_token); | |
2869 | } | |
2870 | ||
a723baf1 MM |
2871 | /* Lexical conventions [gram.lex] */ |
2872 | ||
2873 | /* Parse an identifier. Returns an IDENTIFIER_NODE representing the | |
2874 | identifier. */ | |
2875 | ||
21526606 | 2876 | static tree |
94edc4ab | 2877 | cp_parser_identifier (cp_parser* parser) |
a723baf1 MM |
2878 | { |
2879 | cp_token *token; | |
2880 | ||
2881 | /* Look for the identifier. */ | |
2882 | token = cp_parser_require (parser, CPP_NAME, "identifier"); | |
2883 | /* Return the value. */ | |
d6b418fa | 2884 | return token ? token->u.value : error_mark_node; |
a723baf1 MM |
2885 | } |
2886 | ||
c162c75e MA |
2887 | /* Parse a sequence of adjacent string constants. Returns a |
2888 | TREE_STRING representing the combined, nul-terminated string | |
2889 | constant. If TRANSLATE is true, translate the string to the | |
2890 | execution character set. If WIDE_OK is true, a wide string is | |
2891 | invalid here. | |
2892 | ||
2893 | C++98 [lex.string] says that if a narrow string literal token is | |
2894 | adjacent to a wide string literal token, the behavior is undefined. | |
2895 | However, C99 6.4.5p4 says that this results in a wide string literal. | |
2896 | We follow C99 here, for consistency with the C front end. | |
2897 | ||
2898 | This code is largely lifted from lex_string() in c-lex.c. | |
2899 | ||
2900 | FUTURE: ObjC++ will need to handle @-strings here. */ | |
2901 | static tree | |
2902 | cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok) | |
2903 | { | |
2904 | tree value; | |
c162c75e MA |
2905 | size_t count; |
2906 | struct obstack str_ob; | |
2907 | cpp_string str, istr, *strs; | |
2908 | cp_token *tok; | |
b6baa67d | 2909 | enum cpp_ttype type; |
c162c75e MA |
2910 | |
2911 | tok = cp_lexer_peek_token (parser->lexer); | |
2912 | if (!cp_parser_is_string_literal (tok)) | |
2913 | { | |
2914 | cp_parser_error (parser, "expected string-literal"); | |
2915 | return error_mark_node; | |
2916 | } | |
2917 | ||
b6baa67d KVH |
2918 | type = tok->type; |
2919 | ||
9688c3b8 | 2920 | /* Try to avoid the overhead of creating and destroying an obstack |
c162c75e | 2921 | for the common case of just one string. */ |
2cfe82fe ZW |
2922 | if (!cp_parser_is_string_literal |
2923 | (cp_lexer_peek_nth_token (parser->lexer, 2))) | |
c162c75e | 2924 | { |
2cfe82fe ZW |
2925 | cp_lexer_consume_token (parser->lexer); |
2926 | ||
d6b418fa SM |
2927 | str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value); |
2928 | str.len = TREE_STRING_LENGTH (tok->u.value); | |
c162c75e | 2929 | count = 1; |
c162c75e MA |
2930 | |
2931 | strs = &str; | |
2932 | } | |
2933 | else | |
2934 | { | |
2935 | gcc_obstack_init (&str_ob); | |
2936 | count = 0; | |
2937 | ||
2938 | do | |
2939 | { | |
2cfe82fe | 2940 | cp_lexer_consume_token (parser->lexer); |
c162c75e | 2941 | count++; |
e5cfc29f | 2942 | str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value); |
d6b418fa | 2943 | str.len = TREE_STRING_LENGTH (tok->u.value); |
b6baa67d KVH |
2944 | |
2945 | if (type != tok->type) | |
2946 | { | |
2947 | if (type == CPP_STRING) | |
2948 | type = tok->type; | |
2949 | else if (tok->type != CPP_STRING) | |
69bc6bff MLI |
2950 | error_at (tok->location, |
2951 | "unsupported non-standard concatenation " | |
2952 | "of string literals"); | |
b6baa67d | 2953 | } |
c162c75e MA |
2954 | |
2955 | obstack_grow (&str_ob, &str, sizeof (cpp_string)); | |
2956 | ||
2cfe82fe | 2957 | tok = cp_lexer_peek_token (parser->lexer); |
c162c75e MA |
2958 | } |
2959 | while (cp_parser_is_string_literal (tok)); | |
2960 | ||
2961 | strs = (cpp_string *) obstack_finish (&str_ob); | |
2962 | } | |
2963 | ||
b6baa67d | 2964 | if (type != CPP_STRING && !wide_ok) |
c162c75e MA |
2965 | { |
2966 | cp_parser_error (parser, "a wide string is invalid in this context"); | |
b6baa67d | 2967 | type = CPP_STRING; |
c162c75e MA |
2968 | } |
2969 | ||
2970 | if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate) | |
b6baa67d | 2971 | (parse_in, strs, count, &istr, type)) |
c162c75e | 2972 | { |
68a57691 | 2973 | value = build_string (istr.len, (const char *)istr.text); |
b1d5455a | 2974 | free (CONST_CAST (unsigned char *, istr.text)); |
c162c75e | 2975 | |
b6baa67d KVH |
2976 | switch (type) |
2977 | { | |
2978 | default: | |
2979 | case CPP_STRING: | |
2980 | TREE_TYPE (value) = char_array_type_node; | |
2981 | break; | |
2982 | case CPP_STRING16: | |
2983 | TREE_TYPE (value) = char16_array_type_node; | |
2984 | break; | |
2985 | case CPP_STRING32: | |
2986 | TREE_TYPE (value) = char32_array_type_node; | |
2987 | break; | |
2988 | case CPP_WSTRING: | |
2989 | TREE_TYPE (value) = wchar_array_type_node; | |
2990 | break; | |
2991 | } | |
2992 | ||
c162c75e MA |
2993 | value = fix_string_type (value); |
2994 | } | |
2995 | else | |
2996 | /* cpp_interpret_string has issued an error. */ | |
2997 | value = error_mark_node; | |
2998 | ||
2999 | if (count > 1) | |
3000 | obstack_free (&str_ob, 0); | |
3001 | ||
3002 | return value; | |
3003 | } | |
3004 | ||
3005 | ||
a723baf1 MM |
3006 | /* Basic concepts [gram.basic] */ |
3007 | ||
3008 | /* Parse a translation-unit. | |
3009 | ||
3010 | translation-unit: | |
21526606 | 3011 | declaration-seq [opt] |
a723baf1 MM |
3012 | |
3013 | Returns TRUE if all went well. */ | |
3014 | ||
3015 | static bool | |
94edc4ab | 3016 | cp_parser_translation_unit (cp_parser* parser) |
a723baf1 | 3017 | { |
058b15c1 MM |
3018 | /* The address of the first non-permanent object on the declarator |
3019 | obstack. */ | |
3020 | static void *declarator_obstack_base; | |
3021 | ||
3022 | bool success; | |
98ca843c | 3023 | |
058b15c1 MM |
3024 | /* Create the declarator obstack, if necessary. */ |
3025 | if (!cp_error_declarator) | |
3026 | { | |
3027 | gcc_obstack_init (&declarator_obstack); | |
3028 | /* Create the error declarator. */ | |
3029 | cp_error_declarator = make_declarator (cdk_error); | |
3030 | /* Create the empty parameter list. */ | |
62d1db17 | 3031 | no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE); |
058b15c1 MM |
3032 | /* Remember where the base of the declarator obstack lies. */ |
3033 | declarator_obstack_base = obstack_next_free (&declarator_obstack); | |
3034 | } | |
3035 | ||
a30efae8 | 3036 | cp_parser_declaration_seq_opt (parser); |
3db45ab5 | 3037 | |
a30efae8 GDR |
3038 | /* If there are no tokens left then all went well. */ |
3039 | if (cp_lexer_next_token_is (parser->lexer, CPP_EOF)) | |
a723baf1 | 3040 | { |
a30efae8 GDR |
3041 | /* Get rid of the token array; we don't need it any more. */ |
3042 | cp_lexer_destroy (parser->lexer); | |
3043 | parser->lexer = NULL; | |
3db45ab5 | 3044 | |
a30efae8 | 3045 | /* This file might have been a context that's implicitly extern |
3db45ab5 | 3046 | "C". If so, pop the lang context. (Only relevant for PCH.) */ |
a30efae8 | 3047 | if (parser->implicit_extern_c) |
3db45ab5 MS |
3048 | { |
3049 | pop_lang_context (); | |
3050 | parser->implicit_extern_c = false; | |
3051 | } | |
3052 | ||
a30efae8 GDR |
3053 | /* Finish up. */ |
3054 | finish_translation_unit (); | |
3db45ab5 | 3055 | |
a30efae8 | 3056 | success = true; |
a723baf1 | 3057 | } |
a30efae8 GDR |
3058 | else |
3059 | { | |
3060 | cp_parser_error (parser, "expected declaration"); | |
3061 | success = false; | |
3062 | } | |
3db45ab5 | 3063 | |
058b15c1 | 3064 | /* Make sure the declarator obstack was fully cleaned up. */ |
50bc768d NS |
3065 | gcc_assert (obstack_next_free (&declarator_obstack) |
3066 | == declarator_obstack_base); | |
a723baf1 MM |
3067 | |
3068 | /* All went well. */ | |
058b15c1 | 3069 | return success; |
a723baf1 MM |
3070 | } |
3071 | ||
3072 | /* Expressions [gram.expr] */ | |
3073 | ||
3074 | /* Parse a primary-expression. | |
3075 | ||
3076 | primary-expression: | |
3077 | literal | |
3078 | this | |
3079 | ( expression ) | |
3080 | id-expression | |
3081 | ||
3082 | GNU Extensions: | |
3083 | ||
3084 | primary-expression: | |
3085 | ( compound-statement ) | |
3086 | __builtin_va_arg ( assignment-expression , type-id ) | |
93846d56 | 3087 | __builtin_offsetof ( type-id , offsetof-expression ) |
a723baf1 | 3088 | |
cb68ec50 PC |
3089 | C++ Extensions: |
3090 | __has_nothrow_assign ( type-id ) | |
3091 | __has_nothrow_constructor ( type-id ) | |
3092 | __has_nothrow_copy ( type-id ) | |
3093 | __has_trivial_assign ( type-id ) | |
3094 | __has_trivial_constructor ( type-id ) | |
3095 | __has_trivial_copy ( type-id ) | |
3096 | __has_trivial_destructor ( type-id ) | |
3097 | __has_virtual_destructor ( type-id ) | |
3098 | __is_abstract ( type-id ) | |
3099 | __is_base_of ( type-id , type-id ) | |
3100 | __is_class ( type-id ) | |
3101 | __is_convertible_to ( type-id , type-id ) | |
3102 | __is_empty ( type-id ) | |
3103 | __is_enum ( type-id ) | |
3104 | __is_pod ( type-id ) | |
3105 | __is_polymorphic ( type-id ) | |
3106 | __is_union ( type-id ) | |
3107 | ||
e58a9aa1 ZL |
3108 | Objective-C++ Extension: |
3109 | ||
3110 | primary-expression: | |
3111 | objc-expression | |
3112 | ||
a723baf1 MM |
3113 | literal: |
3114 | __null | |
3115 | ||
02ed62dd MM |
3116 | ADDRESS_P is true iff this expression was immediately preceded by |
3117 | "&" and therefore might denote a pointer-to-member. CAST_P is true | |
3118 | iff this expression is the target of a cast. TEMPLATE_ARG_P is | |
bcf51da2 | 3119 | true iff this expression is a template argument. |
93678513 | 3120 | |
02ed62dd MM |
3121 | Returns a representation of the expression. Upon return, *IDK |
3122 | indicates what kind of id-expression (if any) was present. */ | |
a723baf1 MM |
3123 | |
3124 | static tree | |
21526606 | 3125 | cp_parser_primary_expression (cp_parser *parser, |
02ed62dd | 3126 | bool address_p, |
93678513 | 3127 | bool cast_p, |
02ed62dd MM |
3128 | bool template_arg_p, |
3129 | cp_id_kind *idk) | |
a723baf1 | 3130 | { |
2b7a3abf | 3131 | cp_token *token = NULL; |
a723baf1 MM |
3132 | |
3133 | /* Assume the primary expression is not an id-expression. */ | |
b3445994 | 3134 | *idk = CP_ID_KIND_NONE; |
a723baf1 MM |
3135 | |
3136 | /* Peek at the next token. */ | |
3137 | token = cp_lexer_peek_token (parser->lexer); | |
3138 | switch (token->type) | |
3139 | { | |
3140 | /* literal: | |
3141 | integer-literal | |
3142 | character-literal | |
3143 | floating-literal | |
3144 | string-literal | |
3145 | boolean-literal */ | |
3146 | case CPP_CHAR: | |
b6baa67d KVH |
3147 | case CPP_CHAR16: |
3148 | case CPP_CHAR32: | |
a723baf1 | 3149 | case CPP_WCHAR: |
a723baf1 MM |
3150 | case CPP_NUMBER: |
3151 | token = cp_lexer_consume_token (parser->lexer); | |
754ccf7c JJ |
3152 | if (TREE_CODE (token->u.value) == FIXED_CST) |
3153 | { | |
69bc6bff MLI |
3154 | error_at (token->location, |
3155 | "fixed-point types not supported in C++"); | |
754ccf7c JJ |
3156 | return error_mark_node; |
3157 | } | |
93678513 MM |
3158 | /* Floating-point literals are only allowed in an integral |
3159 | constant expression if they are cast to an integral or | |
3160 | enumeration type. */ | |
d6b418fa | 3161 | if (TREE_CODE (token->u.value) == REAL_CST |
8c94c75a MM |
3162 | && parser->integral_constant_expression_p |
3163 | && pedantic) | |
93678513 MM |
3164 | { |
3165 | /* CAST_P will be set even in invalid code like "int(2.7 + | |
3166 | ...)". Therefore, we have to check that the next token | |
3167 | is sure to end the cast. */ | |
3168 | if (cast_p) | |
3169 | { | |
3170 | cp_token *next_token; | |
3171 | ||
3172 | next_token = cp_lexer_peek_token (parser->lexer); | |
3173 | if (/* The comma at the end of an | |
3174 | enumerator-definition. */ | |
3175 | next_token->type != CPP_COMMA | |
3176 | /* The curly brace at the end of an enum-specifier. */ | |
3177 | && next_token->type != CPP_CLOSE_BRACE | |
3178 | /* The end of a statement. */ | |
3179 | && next_token->type != CPP_SEMICOLON | |
3180 | /* The end of the cast-expression. */ | |
3181 | && next_token->type != CPP_CLOSE_PAREN | |
3182 | /* The end of an array bound. */ | |
060e7327 MM |
3183 | && next_token->type != CPP_CLOSE_SQUARE |
3184 | /* The closing ">" in a template-argument-list. */ | |
3185 | && (next_token->type != CPP_GREATER | |
a2ca1ab2 DG |
3186 | || parser->greater_than_is_operator_p) |
3187 | /* C++0x only: A ">>" treated like two ">" tokens, | |
3188 | in a template-argument-list. */ | |
3189 | && (next_token->type != CPP_RSHIFT | |
c1ae8be5 | 3190 | || (cxx_dialect == cxx98) |
060e7327 | 3191 | || parser->greater_than_is_operator_p)) |
93678513 MM |
3192 | cast_p = false; |
3193 | } | |
3194 | ||
3195 | /* If we are within a cast, then the constraint that the | |
3196 | cast is to an integral or enumeration type will be | |
3197 | checked at that point. If we are not within a cast, then | |
3198 | this code is invalid. */ | |
3199 | if (!cast_p) | |
c8094d83 | 3200 | cp_parser_non_integral_constant_expression |
93678513 MM |
3201 | (parser, "floating-point literal"); |
3202 | } | |
d6b418fa | 3203 | return token->u.value; |
a723baf1 | 3204 | |
0173bb6f | 3205 | case CPP_STRING: |
b6baa67d KVH |
3206 | case CPP_STRING16: |
3207 | case CPP_STRING32: | |
0173bb6f | 3208 | case CPP_WSTRING: |
c162c75e | 3209 | /* ??? Should wide strings be allowed when parser->translate_strings_p |
0cbd7506 | 3210 | is false (i.e. in attributes)? If not, we can kill the third |
c162c75e MA |
3211 | argument to cp_parser_string_literal. */ |
3212 | return cp_parser_string_literal (parser, | |
3213 | parser->translate_strings_p, | |
3214 | true); | |
0173bb6f | 3215 | |
a723baf1 MM |
3216 | case CPP_OPEN_PAREN: |
3217 | { | |
3218 | tree expr; | |
3219 | bool saved_greater_than_is_operator_p; | |
3220 | ||
3221 | /* Consume the `('. */ | |
3222 | cp_lexer_consume_token (parser->lexer); | |
3223 | /* Within a parenthesized expression, a `>' token is always | |
3224 | the greater-than operator. */ | |
21526606 | 3225 | saved_greater_than_is_operator_p |
a723baf1 MM |
3226 | = parser->greater_than_is_operator_p; |
3227 | parser->greater_than_is_operator_p = true; | |
3228 | /* If we see `( { ' then we are looking at the beginning of | |
3229 | a GNU statement-expression. */ | |
3230 | if (cp_parser_allow_gnu_extensions_p (parser) | |
3231 | && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)) | |
3232 | { | |
3233 | /* Statement-expressions are not allowed by the standard. */ | |
509c9d60 MLI |
3234 | pedwarn (token->location, OPT_pedantic, |
3235 | "ISO C++ forbids braced-groups within expressions"); | |
21526606 | 3236 | |
a723baf1 MM |
3237 | /* And they're not allowed outside of a function-body; you |
3238 | cannot, for example, write: | |
21526606 | 3239 | |
0cbd7506 | 3240 | int i = ({ int j = 3; j + 1; }); |
21526606 | 3241 | |
a723baf1 | 3242 | at class or namespace scope. */ |
03ba2e11 PC |
3243 | if (!parser->in_function_body |
3244 | || parser->in_template_argument_list_p) | |
7c2da05e | 3245 | { |
69bc6bff MLI |
3246 | error_at (token->location, |
3247 | "statement-expressions are not allowed outside " | |
3248 | "functions nor in template-argument lists"); | |
7c2da05e SM |
3249 | cp_parser_skip_to_end_of_block_or_statement (parser); |
3250 | expr = error_mark_node; | |
3251 | } | |
3252 | else | |
3253 | { | |
3254 | /* Start the statement-expression. */ | |
3255 | expr = begin_stmt_expr (); | |
3256 | /* Parse the compound-statement. */ | |
3257 | cp_parser_compound_statement (parser, expr, false); | |
3258 | /* Finish up. */ | |
3259 | expr = finish_stmt_expr (expr, false); | |
3260 | } | |
a723baf1 MM |
3261 | } |
3262 | else | |
3263 | { | |
3264 | /* Parse the parenthesized expression. */ | |
cee9035d | 3265 | expr = cp_parser_expression (parser, cast_p, idk); |
a723baf1 MM |
3266 | /* Let the front end know that this expression was |
3267 | enclosed in parentheses. This matters in case, for | |
3268 | example, the expression is of the form `A::B', since | |
3269 | `&A::B' might be a pointer-to-member, but `&(A::B)' is | |
3270 | not. */ | |
3271 | finish_parenthesized_expr (expr); | |
3272 | } | |
3273 | /* The `>' token might be the end of a template-id or | |
3274 | template-parameter-list now. */ | |
21526606 | 3275 | parser->greater_than_is_operator_p |
a723baf1 MM |
3276 | = saved_greater_than_is_operator_p; |
3277 | /* Consume the `)'. */ | |
3a483371 | 3278 | if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>")) |
a723baf1 MM |
3279 | cp_parser_skip_to_end_of_statement (parser); |
3280 | ||
3281 | return expr; | |
3282 | } | |
3283 | ||
3284 | case CPP_KEYWORD: | |
3285 | switch (token->keyword) | |
3286 | { | |
3287 | /* These two are the boolean literals. */ | |
3288 | case RID_TRUE: | |
3289 | cp_lexer_consume_token (parser->lexer); | |
3290 | return boolean_true_node; | |
3291 | case RID_FALSE: | |
3292 | cp_lexer_consume_token (parser->lexer); | |
3293 | return boolean_false_node; | |
21526606 | 3294 | |
a723baf1 MM |
3295 | /* The `__null' literal. */ |
3296 | case RID_NULL: | |
3297 | cp_lexer_consume_token (parser->lexer); | |
3298 | return null_node; | |
3299 | ||
3300 | /* Recognize the `this' keyword. */ | |
3301 | case RID_THIS: | |
3302 | cp_lexer_consume_token (parser->lexer); | |
3303 | if (parser->local_variables_forbidden_p) | |
3304 | { | |
69bc6bff MLI |
3305 | error_at (token->location, |
3306 | "%<this%> may not be used in this context"); | |
a723baf1 MM |
3307 | return error_mark_node; |
3308 | } | |
14d22dd6 | 3309 | /* Pointers cannot appear in constant-expressions. */ |
2a97bf74 | 3310 | if (cp_parser_non_integral_constant_expression (parser, "%<this%>")) |
625cbf93 | 3311 | return error_mark_node; |
a723baf1 MM |
3312 | return finish_this_expr (); |
3313 | ||
3314 | /* The `operator' keyword can be the beginning of an | |
3315 | id-expression. */ | |
3316 | case RID_OPERATOR: | |
3317 | goto id_expression; | |
3318 | ||
3319 | case RID_FUNCTION_NAME: | |
3320 | case RID_PRETTY_FUNCTION_NAME: | |
3321 | case RID_C99_FUNCTION_NAME: | |
cc81fd18 JJ |
3322 | { |
3323 | const char *name; | |
3324 | ||
3325 | /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and | |
3326 | __func__ are the names of variables -- but they are | |
3327 | treated specially. Therefore, they are handled here, | |
3328 | rather than relying on the generic id-expression logic | |
3329 | below. Grammatically, these names are id-expressions. | |
3330 | ||
3331 | Consume the token. */ | |
3332 | token = cp_lexer_consume_token (parser->lexer); | |
3333 | ||
3334 | switch (token->keyword) | |
3335 | { | |
3336 | case RID_FUNCTION_NAME: | |
3337 | name = "%<__FUNCTION__%>"; | |
3338 | break; | |
3339 | case RID_PRETTY_FUNCTION_NAME: | |
3340 | name = "%<__PRETTY_FUNCTION__%>"; | |
3341 | break; | |
3342 | case RID_C99_FUNCTION_NAME: | |
3343 | name = "%<__func__%>"; | |
3344 | break; | |
3345 | default: | |
3346 | gcc_unreachable (); | |
3347 | } | |
3348 | ||
3349 | if (cp_parser_non_integral_constant_expression (parser, name)) | |
3350 | return error_mark_node; | |
3351 | ||
3352 | /* Look up the name. */ | |
3353 | return finish_fname (token->u.value); | |
3354 | } | |
a723baf1 MM |
3355 | |
3356 | case RID_VA_ARG: | |
3357 | { | |
3358 | tree expression; | |
3359 | tree type; | |
3360 | ||
3361 | /* The `__builtin_va_arg' construct is used to handle | |
3362 | `va_arg'. Consume the `__builtin_va_arg' token. */ | |
3363 | cp_lexer_consume_token (parser->lexer); | |
3364 | /* Look for the opening `('. */ | |
3a483371 | 3365 | cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"); |
a723baf1 | 3366 | /* Now, parse the assignment-expression. */ |
93678513 | 3367 | expression = cp_parser_assignment_expression (parser, |
cee9035d | 3368 | /*cast_p=*/false, NULL); |
a723baf1 | 3369 | /* Look for the `,'. */ |
3a483371 | 3370 | cp_parser_require (parser, CPP_COMMA, "%<,%>"); |
a723baf1 MM |
3371 | /* Parse the type-id. */ |
3372 | type = cp_parser_type_id (parser); | |
3373 | /* Look for the closing `)'. */ | |
3a483371 | 3374 | cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"); |
14d22dd6 MM |
3375 | /* Using `va_arg' in a constant-expression is not |
3376 | allowed. */ | |
625cbf93 | 3377 | if (cp_parser_non_integral_constant_expression (parser, |
2a97bf74 | 3378 | "%<va_arg%>")) |
625cbf93 | 3379 | return error_mark_node; |
a723baf1 MM |
3380 | return build_x_va_arg (expression, type); |
3381 | } | |
3382 | ||
263ee052 | 3383 | case RID_OFFSETOF: |
7a3ea201 | 3384 | return cp_parser_builtin_offsetof (parser); |
263ee052 | 3385 | |
cb68ec50 PC |
3386 | case RID_HAS_NOTHROW_ASSIGN: |
3387 | case RID_HAS_NOTHROW_CONSTRUCTOR: | |
3388 | case RID_HAS_NOTHROW_COPY: | |
3389 | case RID_HAS_TRIVIAL_ASSIGN: | |
3390 | case RID_HAS_TRIVIAL_CONSTRUCTOR: | |
3391 | case RID_HAS_TRIVIAL_COPY: | |
3392 | case RID_HAS_TRIVIAL_DESTRUCTOR: | |
3393 | case RID_HAS_VIRTUAL_DESTRUCTOR: | |
3394 | case RID_IS_ABSTRACT: | |
3395 | case RID_IS_BASE_OF: | |
3396 | case RID_IS_CLASS: | |
3397 | case RID_IS_CONVERTIBLE_TO: | |
3398 | case RID_IS_EMPTY: | |
3399 | case RID_IS_ENUM: | |
3400 | case RID_IS_POD: | |
3401 | case RID_IS_POLYMORPHIC: | |
c32097d8 JM |
3402 | case RID_IS_STD_LAYOUT: |
3403 | case RID_IS_TRIVIAL: | |
cb68ec50 PC |
3404 | case RID_IS_UNION: |
3405 | return cp_parser_trait_expr (parser, token->keyword); | |
3406 | ||
3407 | /* Objective-C++ expressions. */ | |
e58a9aa1 ZL |
3408 | case RID_AT_ENCODE: |
3409 | case RID_AT_PROTOCOL: | |
3410 | case RID_AT_SELECTOR: | |
3411 | return cp_parser_objc_expression (parser); | |
3412 | ||
a723baf1 MM |
3413 | default: |
3414 | cp_parser_error (parser, "expected primary-expression"); | |
3415 | return error_mark_node; | |
3416 | } | |
a723baf1 MM |
3417 | |
3418 | /* An id-expression can start with either an identifier, a | |
3419 | `::' as the beginning of a qualified-id, or the "operator" | |
3420 | keyword. */ | |
3421 | case CPP_NAME: | |
3422 | case CPP_SCOPE: | |
3423 | case CPP_TEMPLATE_ID: | |
3424 | case CPP_NESTED_NAME_SPECIFIER: | |
3425 | { | |
3426 | tree id_expression; | |
3427 | tree decl; | |
b3445994 | 3428 | const char *error_msg; |
02ed62dd MM |
3429 | bool template_p; |
3430 | bool done; | |
2b7a3abf | 3431 | cp_token *id_expr_token; |
a723baf1 MM |
3432 | |
3433 | id_expression: | |
3434 | /* Parse the id-expression. */ | |
21526606 EC |
3435 | id_expression |
3436 | = cp_parser_id_expression (parser, | |
a723baf1 MM |
3437 | /*template_keyword_p=*/false, |
3438 | /*check_dependency_p=*/true, | |
02ed62dd | 3439 | &template_p, |
fa6098f8 | 3440 | /*declarator_p=*/false, |
88525e1b | 3441 | /*optional_p=*/false); |
a723baf1 MM |
3442 | if (id_expression == error_mark_node) |
3443 | return error_mark_node; | |
2b7a3abf | 3444 | id_expr_token = token; |
02ed62dd MM |
3445 | token = cp_lexer_peek_token (parser->lexer); |
3446 | done = (token->type != CPP_OPEN_SQUARE | |
3447 | && token->type != CPP_OPEN_PAREN | |
3448 | && token->type != CPP_DOT | |
3449 | && token->type != CPP_DEREF | |
3450 | && token->type != CPP_PLUS_PLUS | |
3451 | && token->type != CPP_MINUS_MINUS); | |
a723baf1 MM |
3452 | /* If we have a template-id, then no further lookup is |
3453 | required. If the template-id was for a template-class, we | |
3454 | will sometimes have a TYPE_DECL at this point. */ | |
02ed62dd MM |
3455 | if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR |
3456 | || TREE_CODE (id_expression) == TYPE_DECL) | |
a723baf1 MM |
3457 | decl = id_expression; |
3458 | /* Look up the name. */ | |
21526606 | 3459 | else |
a723baf1 | 3460 | { |
91b1ca65 | 3461 | tree ambiguous_decls; |
8f78f01f MM |
3462 | |
3463 | decl = cp_parser_lookup_name (parser, id_expression, | |
fc6a28d7 | 3464 | none_type, |
02ed62dd | 3465 | template_p, |
8f78f01f MM |
3466 | /*is_namespace=*/false, |
3467 | /*check_dependency=*/true, | |
2b7a3abf DS |
3468 | &ambiguous_decls, |
3469 | id_expr_token->location); | |
8f78f01f MM |
3470 | /* If the lookup was ambiguous, an error will already have |
3471 | been issued. */ | |
91b1ca65 | 3472 | if (ambiguous_decls) |
8f78f01f | 3473 | return error_mark_node; |
e58a9aa1 ZL |
3474 | |
3475 | /* In Objective-C++, an instance variable (ivar) may be preferred | |
3476 | to whatever cp_parser_lookup_name() found. */ | |
3477 | decl = objc_lookup_ivar (decl, id_expression); | |
3478 | ||
a723baf1 | 3479 | /* If name lookup gives us a SCOPE_REF, then the |
02ed62dd | 3480 | qualifying scope was dependent. */ |
a723baf1 | 3481 | if (TREE_CODE (decl) == SCOPE_REF) |
32a11c08 MM |
3482 | { |
3483 | /* At this point, we do not know if DECL is a valid | |
3484 | integral constant expression. We assume that it is | |
3485 | in fact such an expression, so that code like: | |
3486 | ||
3487 | template <int N> struct A { | |
3488 | int a[B<N>::i]; | |
3489 | }; | |
3490 | ||
3491 | is accepted. At template-instantiation time, we | |
3492 | will check that B<N>::i is actually a constant. */ | |
3493 | return decl; | |
3494 | } | |
a723baf1 MM |
3495 | /* Check to see if DECL is a local variable in a context |
3496 | where that is forbidden. */ | |
3497 | if (parser->local_variables_forbidden_p | |
3498 | && local_variable_p (decl)) | |
3499 | { | |
3500 | /* It might be that we only found DECL because we are | |
3501 | trying to be generous with pre-ISO scoping rules. | |
3502 | For example, consider: | |
3503 | ||
3504 | int i; | |
3505 | void g() { | |
3506 | for (int i = 0; i < 10; ++i) {} | |
3507 | extern void f(int j = i); | |
3508 | } | |
3509 | ||
21526606 | 3510 | Here, name look up will originally find the out |
a723baf1 MM |
3511 | of scope `i'. We need to issue a warning message, |
3512 | but then use the global `i'. */ | |
3513 | decl = check_for_out_of_scope_variable (decl); | |
3514 | if (local_variable_p (decl)) | |
3515 | { | |
69bc6bff MLI |
3516 | error_at (id_expr_token->location, |
3517 | "local variable %qD may not appear in this context", | |
3518 | decl); | |
a723baf1 MM |
3519 | return error_mark_node; |
3520 | } | |
3521 | } | |
c006d942 | 3522 | } |
21526606 | 3523 | |
3db45ab5 | 3524 | decl = (finish_id_expression |
02ed62dd MM |
3525 | (id_expression, decl, parser->scope, |
3526 | idk, | |
3527 | parser->integral_constant_expression_p, | |
3528 | parser->allow_non_integral_constant_expression_p, | |
3529 | &parser->non_integral_constant_expression_p, | |
3530 | template_p, done, address_p, | |
3531 | template_arg_p, | |
2b7a3abf DS |
3532 | &error_msg, |
3533 | id_expr_token->location)); | |
b3445994 MM |
3534 | if (error_msg) |
3535 | cp_parser_error (parser, error_msg); | |
a723baf1 MM |
3536 | return decl; |
3537 | } | |
3538 | ||
3539 | /* Anything else is an error. */ | |
3540 | default: | |
097552fa GDR |
3541 | /* ...unless we have an Objective-C++ message or string literal, |
3542 | that is. */ | |
c8094d83 | 3543 | if (c_dialect_objc () |
097552fa GDR |
3544 | && (token->type == CPP_OPEN_SQUARE |
3545 | || token->type == CPP_OBJC_STRING)) | |
e58a9aa1 ZL |
3546 | return cp_parser_objc_expression (parser); |
3547 | ||
a723baf1 MM |
3548 | cp_parser_error (parser, "expected primary-expression"); |
3549 | return error_mark_node; | |
3550 | } | |
3551 | } | |
3552 | ||
3553 | /* Parse an id-expression. | |
3554 | ||
3555 | id-expression: | |
3556 | unqualified-id | |
3557 | qualified-id | |
3558 | ||
3559 | qualified-id: | |
3560 | :: [opt] nested-name-specifier template [opt] unqualified-id | |
3561 | :: identifier | |
3562 | :: operator-function-id | |
3563 | :: template-id | |
3564 | ||
3565 | Return a representation of the unqualified portion of the | |
3566 | identifier. Sets PARSER->SCOPE to the qualifying scope if there is | |
3567 | a `::' or nested-name-specifier. | |
3568 | ||
3569 | Often, if the id-expression was a qualified-id, the caller will | |
3570 | want to make a SCOPE_REF to represent the qualified-id. This | |
3571 | function does not do this in order to avoid wastefully creating | |
3572 | SCOPE_REFs when they are not required. | |
3573 | ||
a723baf1 MM |
3574 | If TEMPLATE_KEYWORD_P is true, then we have just seen the |
3575 | `template' keyword. | |
3576 | ||
3577 | If CHECK_DEPENDENCY_P is false, then names are looked up inside | |
21526606 | 3578 | uninstantiated templates. |
a723baf1 | 3579 | |
15d2cb19 | 3580 | If *TEMPLATE_P is non-NULL, it is set to true iff the |
a723baf1 | 3581 | `template' keyword is used to explicitly indicate that the entity |
21526606 | 3582 | named is a template. |
f3c2dfc6 MM |
3583 | |
3584 | If DECLARATOR_P is true, the id-expression is appearing as part of | |
cd0be382 | 3585 | a declarator, rather than as part of an expression. */ |
a723baf1 MM |
3586 | |
3587 | static tree | |
3588 | cp_parser_id_expression (cp_parser *parser, | |
3589 | bool template_keyword_p, | |
3590 | bool check_dependency_p, | |
f3c2dfc6 | 3591 | bool *template_p, |
fa6098f8 | 3592 | bool declarator_p, |
88525e1b | 3593 | bool optional_p) |
a723baf1 MM |
3594 | { |
3595 | bool global_scope_p; | |
3596 | bool nested_name_specifier_p; | |
3597 | ||
3598 | /* Assume the `template' keyword was not used. */ | |
3599 | if (template_p) | |
02ed62dd | 3600 | *template_p = template_keyword_p; |
a723baf1 MM |
3601 | |
3602 | /* Look for the optional `::' operator. */ | |
21526606 | 3603 | global_scope_p |
88525e1b | 3604 | = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false) |
a723baf1 MM |
3605 | != NULL_TREE); |
3606 | /* Look for the optional nested-name-specifier. */ | |
21526606 | 3607 | nested_name_specifier_p |
a723baf1 MM |
3608 | = (cp_parser_nested_name_specifier_opt (parser, |
3609 | /*typename_keyword_p=*/false, | |
3610 | check_dependency_p, | |
a668c6ad | 3611 | /*type_p=*/false, |
a52eb3bc | 3612 | declarator_p) |
a723baf1 MM |
3613 | != NULL_TREE); |
3614 | /* If there is a nested-name-specifier, then we are looking at | |
3615 | the first qualified-id production. */ | |
3616 | if (nested_name_specifier_p) | |
3617 | { | |
3618 | tree saved_scope; | |
3619 | tree saved_object_scope; | |
3620 | tree saved_qualifying_scope; | |
3621 | tree unqualified_id; | |
3622 | bool is_template; | |
3623 | ||
3624 | /* See if the next token is the `template' keyword. */ | |
3625 | if (!template_p) | |
3626 | template_p = &is_template; | |
3627 | *template_p = cp_parser_optional_template_keyword (parser); | |
3628 | /* Name lookup we do during the processing of the | |
3629 | unqualified-id might obliterate SCOPE. */ | |
3630 | saved_scope = parser->scope; | |
3631 | saved_object_scope = parser->object_scope; | |
3632 | saved_qualifying_scope = parser->qualifying_scope; | |
3633 | /* Process the final unqualified-id. */ | |
3634 | unqualified_id = cp_parser_unqualified_id (parser, *template_p, | |
f3c2dfc6 | 3635 | check_dependency_p, |
fa6098f8 | 3636 | declarator_p, |
88525e1b | 3637 | /*optional_p=*/false); |
a723baf1 MM |
3638 | /* Restore the SAVED_SCOPE for our caller. */ |
3639 | parser->scope = saved_scope; | |
3640 | parser->object_scope = saved_object_scope; | |
3641 | parser->qualifying_scope = saved_qualifying_scope; | |
3642 | ||
3643 | return unqualified_id; | |
3644 | } | |
3645 | /* Otherwise, if we are in global scope, then we are looking at one | |
3646 | of the other qualified-id productions. */ | |
3647 | else if (global_scope_p) | |
3648 | { | |
3649 | cp_token *token; | |
3650 | tree id; | |
3651 | ||
e5976695 MM |
3652 | /* Peek at the next token. */ |
3653 | token = cp_lexer_peek_token (parser->lexer); | |
3654 | ||
3655 | /* If it's an identifier, and the next token is not a "<", then | |
3656 | we can avoid the template-id case. This is an optimization | |
3657 | for this common case. */ | |
21526606 EC |
3658 | if (token->type == CPP_NAME |
3659 | && !cp_parser_nth_token_starts_template_argument_list_p | |
f4abade9 | 3660 | (parser, 2)) |
e5976695 MM |
3661 | return cp_parser_identifier (parser); |
3662 | ||
a723baf1 MM |
3663 | cp_parser_parse_tentatively (parser); |
3664 | /* Try a template-id. */ | |
21526606 | 3665 | id = cp_parser_template_id (parser, |
a723baf1 | 3666 | /*template_keyword_p=*/false, |
a668c6ad MM |
3667 | /*check_dependency_p=*/true, |
3668 | declarator_p); | |
a723baf1 MM |
3669 | /* If that worked, we're done. */ |
3670 | if (cp_parser_parse_definitely (parser)) | |
3671 | return id; | |
3672 | ||
e5976695 MM |
3673 | /* Peek at the next token. (Changes in the token buffer may |
3674 | have invalidated the pointer obtained above.) */ | |
a723baf1 MM |
3675 | token = cp_lexer_peek_token (parser->lexer); |
3676 | ||
3677 | switch (token->type) | |
3678 | { | |
3679 | case CPP_NAME: | |
3680 | return cp_parser_identifier (parser); | |
3681 | ||
3682 | case CPP_KEYWORD: | |
3683 | if (token->keyword == RID_OPERATOR) | |
3684 | return cp_parser_operator_function_id (parser); | |
3685 | /* Fall through. */ | |
21526606 | 3686 | |
a723baf1 MM |
3687 | default: |
3688 | cp_parser_error (parser, "expected id-expression"); | |
3689 | return error_mark_node; | |
3690 | } | |
3691 | } | |
3692 | else | |
3693 | return cp_parser_unqualified_id (parser, template_keyword_p, | |
f3c2dfc6 | 3694 | /*check_dependency_p=*/true, |
88525e1b NS |
3695 | declarator_p, |
3696 | optional_p); | |
a723baf1 MM |
3697 | } |
3698 | ||
3699 | /* Parse an unqualified-id. | |
3700 | ||
3701 | unqualified-id: | |
3702 | identifier | |
3703 | operator-function-id | |
3704 | conversion-function-id | |
3705 | ~ class-name | |
3706 | template-id | |
3707 | ||
3708 | If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template' | |
3709 | keyword, in a construct like `A::template ...'. | |
3710 | ||
3711 | Returns a representation of unqualified-id. For the `identifier' | |
3712 | production, an IDENTIFIER_NODE is returned. For the `~ class-name' | |
3713 | production a BIT_NOT_EXPR is returned; the operand of the | |
3714 | BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the | |
3715 | other productions, see the documentation accompanying the | |
3716 | corresponding parsing functions. If CHECK_DEPENDENCY_P is false, | |
f3c2dfc6 MM |
3717 | names are looked up in uninstantiated templates. If DECLARATOR_P |
3718 | is true, the unqualified-id is appearing as part of a declarator, | |
3719 | rather than as part of an expression. */ | |
a723baf1 MM |
3720 | |
3721 | static tree | |
21526606 | 3722 | cp_parser_unqualified_id (cp_parser* parser, |
0cbd7506 | 3723 | bool template_keyword_p, |
f3c2dfc6 | 3724 | bool check_dependency_p, |
3db45ab5 | 3725 | bool declarator_p, |
88525e1b | 3726 | bool optional_p) |
a723baf1 MM |
3727 | { |
3728 | cp_token *token; | |
3729 | ||
3730 | /* Peek at the next token. */ | |
3731 | token = cp_lexer_peek_token (parser->lexer); | |
21526606 | 3732 | |
a723baf1 MM |
3733 | switch (token->type) |
3734 | { | |
3735 | case CPP_NAME: | |
3736 | { | |
3737 | tree id; | |
3738 | ||
3739 | /* We don't know yet whether or not this will be a | |
3740 | template-id. */ | |
3741 | cp_parser_parse_tentatively (parser); | |
3742 | /* Try a template-id. */ | |
3743 | id = cp_parser_template_id (parser, template_keyword_p, | |
a668c6ad MM |
3744 | check_dependency_p, |
3745 | declarator_p); | |
a723baf1 MM |
3746 | /* If it worked, we're done. */ |
3747 | if (cp_parser_parse_definitely (parser)) | |
3748 | return id; | |
3749 | /* Otherwise, it's an ordinary identifier. */ | |
3750 | return cp_parser_identifier (parser); | |
3751 | } | |
3752 | ||
3753 | case CPP_TEMPLATE_ID: | |
3754 | return cp_parser_template_id (parser, template_keyword_p, | |
a668c6ad MM |
3755 | check_dependency_p, |
3756 | declarator_p); | |
a723baf1 MM |
3757 | |
3758 | case CPP_COMPL: | |
3759 | { | |
3760 | tree type_decl; | |
3761 | tree qualifying_scope; | |
3762 | tree object_scope; | |
3763 | tree scope; | |
88e95ee3 | 3764 | bool done; |
a723baf1 MM |
3765 | |
3766 | /* Consume the `~' token. */ | |
3767 | cp_lexer_consume_token (parser->lexer); | |
3768 | /* Parse the class-name. The standard, as written, seems to | |
3769 | say that: | |
3770 | ||
3771 | template <typename T> struct S { ~S (); }; | |
3772 | template <typename T> S<T>::~S() {} | |
3773 | ||
0cbd7506 | 3774 | is invalid, since `~' must be followed by a class-name, but |
a723baf1 MM |
3775 | `S<T>' is dependent, and so not known to be a class. |
3776 | That's not right; we need to look in uninstantiated | |
3777 | templates. A further complication arises from: | |
3778 | ||
3779 | template <typename T> void f(T t) { | |
3780 | t.T::~T(); | |
21526606 | 3781 | } |
a723baf1 MM |
3782 | |
3783 | Here, it is not possible to look up `T' in the scope of `T' | |
3784 | itself. We must look in both the current scope, and the | |
21526606 | 3785 | scope of the containing complete expression. |
a723baf1 MM |
3786 | |
3787 | Yet another issue is: | |
3788 | ||
0cbd7506 MS |
3789 | struct S { |
3790 | int S; | |
3791 | ~S(); | |
3792 | }; | |
a723baf1 | 3793 | |
0cbd7506 | 3794 | S::~S() {} |
a723baf1 | 3795 | |
0cbd7506 | 3796 | The standard does not seem to say that the `S' in `~S' |
a723baf1 MM |
3797 | should refer to the type `S' and not the data member |
3798 | `S::S'. */ | |
3799 | ||
3800 | /* DR 244 says that we look up the name after the "~" in the | |
3801 | same scope as we looked up the qualifying name. That idea | |
3802 | isn't fully worked out; it's more complicated than that. */ | |
3803 | scope = parser->scope; | |
3804 | object_scope = parser->object_scope; | |
3805 | qualifying_scope = parser->qualifying_scope; | |
3806 | ||
c0dc47a8 VR |
3807 | /* Check for invalid scopes. */ |
3808 | if (scope == error_mark_node) | |
3809 | { | |
04499540 VR |
3810 | if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)) |
3811 | cp_lexer_consume_token (parser->lexer); | |
c0dc47a8 VR |
3812 | return error_mark_node; |
3813 | } | |
3814 | if (scope && TREE_CODE (scope) == NAMESPACE_DECL) | |
3815 | { | |
3816 | if (!cp_parser_uncommitted_to_tentative_parse_p (parser)) | |
69bc6bff MLI |
3817 | error_at (token->location, |
3818 | "scope %qT before %<~%> is not a class-name", | |
3819 | scope); | |
04499540 VR |
3820 | cp_parser_simulate_error (parser); |
3821 | if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)) | |
3822 | cp_lexer_consume_token (parser->lexer); | |
c0dc47a8 VR |
3823 | return error_mark_node; |
3824 | } | |
3825 | gcc_assert (!scope || TYPE_P (scope)); | |
3826 | ||
a723baf1 | 3827 | /* If the name is of the form "X::~X" it's OK. */ |
84019f23 | 3828 | token = cp_lexer_peek_token (parser->lexer); |
c0dc47a8 | 3829 | if (scope |
84019f23 | 3830 | && token->type == CPP_NAME |
21526606 | 3831 | && (cp_lexer_peek_nth_token (parser->lexer, 2)->type |
a723baf1 | 3832 | == CPP_OPEN_PAREN) |
d6b418fa | 3833 | && constructor_name_p (token->u.value, scope)) |
a723baf1 MM |
3834 | { |
3835 | cp_lexer_consume_token (parser->lexer); | |
3836 | return build_nt (BIT_NOT_EXPR, scope); | |
3837 | } | |
3838 | ||
3839 | /* If there was an explicit qualification (S::~T), first look | |
3840 | in the scope given by the qualification (i.e., S). */ | |
88e95ee3 | 3841 | done = false; |
e3754f9c | 3842 | type_decl = NULL_TREE; |
a723baf1 MM |
3843 | if (scope) |
3844 | { | |
3845 | cp_parser_parse_tentatively (parser); | |
21526606 | 3846 | type_decl = cp_parser_class_name (parser, |
a723baf1 MM |
3847 | /*typename_keyword_p=*/false, |
3848 | /*template_keyword_p=*/false, | |
fc6a28d7 | 3849 | none_type, |
a723baf1 | 3850 | /*check_dependency=*/false, |
a668c6ad MM |
3851 | /*class_head_p=*/false, |
3852 | declarator_p); | |
a723baf1 | 3853 | if (cp_parser_parse_definitely (parser)) |
88e95ee3 | 3854 | done = true; |
a723baf1 MM |
3855 | } |
3856 | /* In "N::S::~S", look in "N" as well. */ | |
88e95ee3 | 3857 | if (!done && scope && qualifying_scope) |
a723baf1 MM |
3858 | { |
3859 | cp_parser_parse_tentatively (parser); | |
3860 | parser->scope = qualifying_scope; | |
3861 | parser->object_scope = NULL_TREE; | |
3862 | parser->qualifying_scope = NULL_TREE; | |
21526606 EC |
3863 | type_decl |
3864 | = cp_parser_class_name (parser, | |
a723baf1 MM |
3865 | /*typename_keyword_p=*/false, |
3866 | /*template_keyword_p=*/false, | |
fc6a28d7 | 3867 | none_type, |
a723baf1 | 3868 | /*check_dependency=*/false, |
a668c6ad MM |
3869 | /*class_head_p=*/false, |
3870 | declarator_p); | |
a723baf1 | 3871 | if (cp_parser_parse_definitely (parser)) |
88e95ee3 | 3872 | done = true; |
a723baf1 | 3873 | } |
88525e1b NS |
3874 | /* In "p->S::~T", look in the scope given by "*p" as well. */ |
3875 | else if (!done && object_scope) | |
a723baf1 MM |
3876 | { |
3877 | cp_parser_parse_tentatively (parser); | |
3878 | parser->scope = object_scope; | |
3879 | parser->object_scope = NULL_TREE; | |
3880 | parser->qualifying_scope = NULL_TREE; | |
21526606 EC |
3881 | type_decl |
3882 | = cp_parser_class_name (parser, | |
88525e1b | 3883 | /*typename_keyword_p=*/false, |
a723baf1 | 3884 | /*template_keyword_p=*/false, |
fc6a28d7 | 3885 | none_type, |
a723baf1 | 3886 | /*check_dependency=*/false, |
a668c6ad MM |
3887 | /*class_head_p=*/false, |
3888 | declarator_p); | |
a723baf1 | 3889 | if (cp_parser_parse_definitely (parser)) |
88e95ee3 | 3890 | done = true; |
a723baf1 MM |
3891 | } |
3892 | /* Look in the surrounding context. */ | |
88e95ee3 MM |
3893 | if (!done) |
3894 | { | |
3895 | parser->scope = NULL_TREE; | |
3896 | parser->object_scope = NULL_TREE; | |
3897 | parser->qualifying_scope = NULL_TREE; | |
05134211 JM |
3898 | if (processing_template_decl) |
3899 | cp_parser_parse_tentatively (parser); | |
88e95ee3 MM |
3900 | type_decl |
3901 | = cp_parser_class_name (parser, | |
3902 | /*typename_keyword_p=*/false, | |
3903 | /*template_keyword_p=*/false, | |
3904 | none_type, | |
3905 | /*check_dependency=*/false, | |
3906 | /*class_head_p=*/false, | |
3907 | declarator_p); | |
05134211 JM |
3908 | if (processing_template_decl |
3909 | && ! cp_parser_parse_definitely (parser)) | |
3910 | { | |
3911 | /* We couldn't find a type with this name, so just accept | |
3912 | it and check for a match at instantiation time. */ | |
3913 | type_decl = cp_parser_identifier (parser); | |
ce6b9ebb JM |
3914 | if (type_decl != error_mark_node) |
3915 | type_decl = build_nt (BIT_NOT_EXPR, type_decl); | |
3916 | return type_decl; | |
05134211 | 3917 | } |
88e95ee3 | 3918 | } |
a723baf1 MM |
3919 | /* If an error occurred, assume that the name of the |
3920 | destructor is the same as the name of the qualifying | |
3921 | class. That allows us to keep parsing after running | |
3922 | into ill-formed destructor names. */ | |
c0dc47a8 | 3923 | if (type_decl == error_mark_node && scope) |
a723baf1 MM |
3924 | return build_nt (BIT_NOT_EXPR, scope); |
3925 | else if (type_decl == error_mark_node) | |
3926 | return error_mark_node; | |
3927 | ||
1b3d28a8 VR |
3928 | /* Check that destructor name and scope match. */ |
3929 | if (declarator_p && scope && !check_dtor_name (scope, type_decl)) | |
3930 | { | |
3931 | if (!cp_parser_uncommitted_to_tentative_parse_p (parser)) | |
69bc6bff MLI |
3932 | error_at (token->location, |
3933 | "declaration of %<~%T%> as member of %qT", | |
3934 | type_decl, scope); | |
04499540 | 3935 | cp_parser_simulate_error (parser); |
1b3d28a8 VR |
3936 | return error_mark_node; |
3937 | } | |
3938 | ||
f3c2dfc6 MM |
3939 | /* [class.dtor] |
3940 | ||
3941 | A typedef-name that names a class shall not be used as the | |
3942 | identifier in the declarator for a destructor declaration. */ | |
21526606 | 3943 | if (declarator_p |
f3c2dfc6 | 3944 | && !DECL_IMPLICIT_TYPEDEF_P (type_decl) |
33a69702 VR |
3945 | && !DECL_SELF_REFERENCE_P (type_decl) |
3946 | && !cp_parser_uncommitted_to_tentative_parse_p (parser)) | |
69bc6bff MLI |
3947 | error_at (token->location, |
3948 | "typedef-name %qD used as destructor declarator", | |
3949 | type_decl); | |
f3c2dfc6 | 3950 | |
a723baf1 MM |
3951 | return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl)); |
3952 | } | |
3953 | ||
3954 | case CPP_KEYWORD: | |
3955 | if (token->keyword == RID_OPERATOR) | |
3956 | { | |
3957 | tree id; | |
3958 | ||
3959 | /* This could be a template-id, so we try that first. */ | |
3960 | cp_parser_parse_tentatively (parser); | |
3961 | /* Try a template-id. */ | |
3962 | id = cp_parser_template_id (parser, template_keyword_p, | |
a668c6ad MM |
3963 | /*check_dependency_p=*/true, |
3964 | declarator_p); | |
a723baf1 MM |
3965 | /* If that worked, we're done. */ |
3966 | if (cp_parser_parse_definitely (parser)) | |
3967 | return id; | |
3968 | /* We still don't know whether we're looking at an | |
3969 | operator-function-id or a conversion-function-id. */ | |
3970 | cp_parser_parse_tentatively (parser); | |
3971 | /* Try an operator-function-id. */ | |
3972 | id = cp_parser_operator_function_id (parser); | |
3973 | /* If that didn't work, try a conversion-function-id. */ | |
3974 | if (!cp_parser_parse_definitely (parser)) | |
3975 | id = cp_parser_conversion_function_id (parser); | |
3976 | ||
3977 | return id; | |
3978 | } | |
3979 | /* Fall through. */ | |
3980 | ||
3981 | default: | |
fa6098f8 MM |
3982 | if (optional_p) |
3983 | return NULL_TREE; | |
a723baf1 MM |
3984 | cp_parser_error (parser, "expected unqualified-id"); |
3985 | return error_mark_node; | |
3986 | } | |
3987 | } | |
3988 | ||
3989 | /* Parse an (optional) nested-name-specifier. | |
3990 | ||
adf2edec | 3991 | nested-name-specifier: [C++98] |
a723baf1 MM |
3992 | class-or-namespace-name :: nested-name-specifier [opt] |
3993 | class-or-namespace-name :: template nested-name-specifier [opt] | |
3994 | ||
adf2edec DG |
3995 | nested-name-specifier: [C++0x] |
3996 | type-name :: | |
3997 | namespace-name :: | |
3998 | nested-name-specifier identifier :: | |
3999 | nested-name-specifier template [opt] simple-template-id :: | |
4000 | ||
a723baf1 MM |
4001 | PARSER->SCOPE should be set appropriately before this function is |
4002 | called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in | |
4003 | effect. TYPE_P is TRUE if we non-type bindings should be ignored | |
4004 | in name lookups. | |
4005 | ||
4006 | Sets PARSER->SCOPE to the class (TYPE) or namespace | |
4007 | (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves | |
4008 | it unchanged if there is no nested-name-specifier. Returns the new | |
21526606 | 4009 | scope iff there is a nested-name-specifier, or NULL_TREE otherwise. |
a668c6ad MM |
4010 | |
4011 | If IS_DECLARATION is TRUE, the nested-name-specifier is known to be | |
4012 | part of a declaration and/or decl-specifier. */ | |
a723baf1 MM |
4013 | |
4014 | static tree | |
21526606 EC |
4015 | cp_parser_nested_name_specifier_opt (cp_parser *parser, |
4016 | bool typename_keyword_p, | |
a723baf1 | 4017 | bool check_dependency_p, |
a668c6ad MM |
4018 | bool type_p, |
4019 | bool is_declaration) | |
a723baf1 MM |
4020 | { |
4021 | bool success = false; | |
0c5e4866 NS |
4022 | cp_token_position start = 0; |
4023 | cp_token *token; | |
a723baf1 | 4024 | |
a723baf1 | 4025 | /* Remember where the nested-name-specifier starts. */ |
0b16f8f4 | 4026 | if (cp_parser_uncommitted_to_tentative_parse_p (parser)) |
19e65f30 MM |
4027 | { |
4028 | start = cp_lexer_token_position (parser->lexer, false); | |
4029 | push_deferring_access_checks (dk_deferred); | |
4030 | } | |
cf22909c | 4031 | |
a723baf1 MM |
4032 | while (true) |
4033 | { | |
4034 | tree new_scope; | |
4035 | tree old_scope; | |
4036 | tree saved_qualifying_scope; | |
a723baf1 MM |
4037 | bool template_keyword_p; |
4038 | ||
2050a1bb MM |
4039 | /* Spot cases that cannot be the beginning of a |
4040 | nested-name-specifier. */ | |
4041 | token = cp_lexer_peek_token (parser->lexer); | |
4042 | ||
4043 | /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process | |
4044 | the already parsed nested-name-specifier. */ | |
4045 | if (token->type == CPP_NESTED_NAME_SPECIFIER) | |
4046 | { | |
4047 | /* Grab the nested-name-specifier and continue the loop. */ | |
4048 | cp_parser_pre_parsed_nested_name_specifier (parser); | |
1cd7d7bf MM |
4049 | /* If we originally encountered this nested-name-specifier |
4050 | with IS_DECLARATION set to false, we will not have | |
4051 | resolved TYPENAME_TYPEs, so we must do so here. */ | |
547e3b2a MM |
4052 | if (is_declaration |
4053 | && TREE_CODE (parser->scope) == TYPENAME_TYPE) | |
1cd7d7bf MM |
4054 | { |
4055 | new_scope = resolve_typename_type (parser->scope, | |
4056 | /*only_current_p=*/false); | |
4195a767 | 4057 | if (TREE_CODE (new_scope) != TYPENAME_TYPE) |
1cd7d7bf MM |
4058 | parser->scope = new_scope; |
4059 | } | |
2050a1bb MM |
4060 | success = true; |
4061 | continue; | |
4062 | } | |
4063 | ||
a723baf1 MM |
4064 | /* Spot cases that cannot be the beginning of a |
4065 | nested-name-specifier. On the second and subsequent times | |
4066 | through the loop, we look for the `template' keyword. */ | |
f7b5ecd9 | 4067 | if (success && token->keyword == RID_TEMPLATE) |
a723baf1 MM |
4068 | ; |
4069 | /* A template-id can start a nested-name-specifier. */ | |
f7b5ecd9 | 4070 | else if (token->type == CPP_TEMPLATE_ID) |
a723baf1 MM |
4071 | ; |
4072 | else | |
4073 | { | |
4074 | /* If the next token is not an identifier, then it is | |
adf2edec | 4075 | definitely not a type-name or namespace-name. */ |
f7b5ecd9 | 4076 | if (token->type != CPP_NAME) |
a723baf1 MM |
4077 | break; |
4078 | /* If the following token is neither a `<' (to begin a | |
4079 | template-id), nor a `::', then we are not looking at a | |
4080 | nested-name-specifier. */ | |
4081 | token = cp_lexer_peek_nth_token (parser->lexer, 2); | |
f4abade9 GB |
4082 | if (token->type != CPP_SCOPE |
4083 | && !cp_parser_nth_token_starts_template_argument_list_p | |
4084 | (parser, 2)) | |
a723baf1 MM |
4085 | break; |
4086 | } | |
4087 | ||
4088 | /* The nested-name-specifier is optional, so we parse | |
4089 | tentatively. */ | |
4090 | cp_parser_parse_tentatively (parser); | |
4091 | ||
4092 | /* Look for the optional `template' keyword, if this isn't the | |
4093 | first time through the loop. */ | |
4094 | if (success) | |
4095 | template_keyword_p = cp_parser_optional_template_keyword (parser); | |
4096 | else | |
4097 | template_keyword_p = false; | |
4098 | ||
4099 | /* Save the old scope since the name lookup we are about to do | |
4100 | might destroy it. */ | |
4101 | old_scope = parser->scope; | |
4102 | saved_qualifying_scope = parser->qualifying_scope; | |
a52eb3bc MM |
4103 | /* In a declarator-id like "X<T>::I::Y<T>" we must be able to |
4104 | look up names in "X<T>::I" in order to determine that "Y" is | |
4105 | a template. So, if we have a typename at this point, we make | |
4106 | an effort to look through it. */ | |
c8094d83 | 4107 | if (is_declaration |
67bcc252 | 4108 | && !typename_keyword_p |
c8094d83 | 4109 | && parser->scope |
a52eb3bc | 4110 | && TREE_CODE (parser->scope) == TYPENAME_TYPE) |
c8094d83 | 4111 | parser->scope = resolve_typename_type (parser->scope, |
a52eb3bc | 4112 | /*only_current_p=*/false); |
a723baf1 | 4113 | /* Parse the qualifying entity. */ |
21526606 | 4114 | new_scope |
adf2edec DG |
4115 | = cp_parser_qualifying_entity (parser, |
4116 | typename_keyword_p, | |
4117 | template_keyword_p, | |
4118 | check_dependency_p, | |
4119 | type_p, | |
4120 | is_declaration); | |
a723baf1 | 4121 | /* Look for the `::' token. */ |
3a483371 | 4122 | cp_parser_require (parser, CPP_SCOPE, "%<::%>"); |
a723baf1 MM |
4123 | |
4124 | /* If we found what we wanted, we keep going; otherwise, we're | |
4125 | done. */ | |
4126 | if (!cp_parser_parse_definitely (parser)) | |
4127 | { | |
4128 | bool error_p = false; | |
4129 | ||
4130 | /* Restore the OLD_SCOPE since it was valid before the | |
4131 | failed attempt at finding the last | |
4132 | class-or-namespace-name. */ | |
4133 | parser->scope = old_scope; | |
4134 | parser->qualifying_scope = saved_qualifying_scope; | |
84019f23 MM |
4135 | if (cp_parser_uncommitted_to_tentative_parse_p (parser)) |
4136 | break; | |
a723baf1 MM |
4137 | /* If the next token is an identifier, and the one after |
4138 | that is a `::', then any valid interpretation would have | |
4139 | found a class-or-namespace-name. */ | |
4140 | while (cp_lexer_next_token_is (parser->lexer, CPP_NAME) | |
21526606 | 4141 | && (cp_lexer_peek_nth_token (parser->lexer, 2)->type |
a723baf1 | 4142 | == CPP_SCOPE) |
21526606 | 4143 | && (cp_lexer_peek_nth_token (parser->lexer, 3)->type |
a723baf1 MM |
4144 | != CPP_COMPL)) |
4145 | { | |
4146 | token = cp_lexer_consume_token (parser->lexer); | |
21526606 | 4147 | if (!error_p) |
a723baf1 | 4148 | { |
91b1ca65 MM |
4149 | if (!token->ambiguous_p) |
4150 | { | |
4151 | tree decl; | |
4152 | tree ambiguous_decls; | |
4153 | ||
d6b418fa | 4154 | decl = cp_parser_lookup_name (parser, token->u.value, |
91b1ca65 MM |
4155 | none_type, |
4156 | /*is_template=*/false, | |
4157 | /*is_namespace=*/false, | |
4158 | /*check_dependency=*/true, | |
2b7a3abf DS |
4159 | &ambiguous_decls, |
4160 | token->location); | |
91b1ca65 | 4161 | if (TREE_CODE (decl) == TEMPLATE_DECL) |
69bc6bff MLI |
4162 | error_at (token->location, |
4163 | "%qD used without template parameters", | |
4164 | decl); | |
91b1ca65 MM |
4165 | else if (ambiguous_decls) |
4166 | { | |
69bc6bff MLI |
4167 | error_at (token->location, |
4168 | "reference to %qD is ambiguous", | |
4169 | token->u.value); | |
91b1ca65 MM |
4170 | print_candidates (ambiguous_decls); |
4171 | decl = error_mark_node; | |
4172 | } | |
4173 | else | |
adf2edec DG |
4174 | { |
4175 | const char* msg = "is not a class or namespace"; | |
4176 | if (cxx_dialect != cxx98) | |
4177 | msg = "is not a class, namespace, or enumeration"; | |
4178 | cp_parser_name_lookup_error | |
4179 | (parser, token->u.value, decl, msg, | |
4180 | token->location); | |
4181 | } | |
91b1ca65 MM |
4182 | } |
4183 | parser->scope = error_mark_node; | |
a723baf1 | 4184 | error_p = true; |
eea9800f MM |
4185 | /* Treat this as a successful nested-name-specifier |
4186 | due to: | |
4187 | ||
4188 | [basic.lookup.qual] | |
4189 | ||
4190 | If the name found is not a class-name (clause | |
4191 | _class_) or namespace-name (_namespace.def_), the | |
4192 | program is ill-formed. */ | |
4193 | success = true; | |
a723baf1 MM |
4194 | } |
4195 | cp_lexer_consume_token (parser->lexer); | |
4196 | } | |
4197 | break; | |
4198 | } | |
a723baf1 MM |
4199 | /* We've found one valid nested-name-specifier. */ |
4200 | success = true; | |
02ed62dd MM |
4201 | /* Name lookup always gives us a DECL. */ |
4202 | if (TREE_CODE (new_scope) == TYPE_DECL) | |
4203 | new_scope = TREE_TYPE (new_scope); | |
4204 | /* Uses of "template" must be followed by actual templates. */ | |
4205 | if (template_keyword_p | |
4206 | && !(CLASS_TYPE_P (new_scope) | |
4207 | && ((CLASSTYPE_USE_TEMPLATE (new_scope) | |
4208 | && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope))) | |
4209 | || CLASSTYPE_IS_TEMPLATE (new_scope))) | |
4210 | && !(TREE_CODE (new_scope) == TYPENAME_TYPE | |
4211 | && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope)) | |
4212 | == TEMPLATE_ID_EXPR))) | |
cbe5f3b3 | 4213 | permerror (input_location, TYPE_P (new_scope) |
37ec60ed JW |
4214 | ? "%qT is not a template" |
4215 | : "%qD is not a template", | |
4216 | new_scope); | |
a723baf1 MM |
4217 | /* If it is a class scope, try to complete it; we are about to |
4218 | be looking up names inside the class. */ | |
02ed62dd | 4219 | if (TYPE_P (new_scope) |
8fbc5ae7 MM |
4220 | /* Since checking types for dependency can be expensive, |
4221 | avoid doing it if the type is already complete. */ | |
02ed62dd | 4222 | && !COMPLETE_TYPE_P (new_scope) |
8fbc5ae7 | 4223 | /* Do not try to complete dependent types. */ |
02ed62dd | 4224 | && !dependent_type_p (new_scope)) |
eba5fc70 JJ |
4225 | { |
4226 | new_scope = complete_type (new_scope); | |
4227 | /* If it is a typedef to current class, use the current | |
4228 | class instead, as the typedef won't have any names inside | |
4229 | it yet. */ | |
4230 | if (!COMPLETE_TYPE_P (new_scope) | |
4231 | && currently_open_class (new_scope)) | |
4232 | new_scope = TYPE_MAIN_VARIANT (new_scope); | |
4233 | } | |
02ed62dd MM |
4234 | /* Make sure we look in the right scope the next time through |
4235 | the loop. */ | |
4236 | parser->scope = new_scope; | |
a723baf1 MM |
4237 | } |
4238 | ||
4239 | /* If parsing tentatively, replace the sequence of tokens that makes | |
4240 | up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER | |
4241 | token. That way, should we re-parse the token stream, we will | |
4242 | not have to repeat the effort required to do the parse, nor will | |
4243 | we issue duplicate error messages. */ | |
0c5e4866 | 4244 | if (success && start) |
a723baf1 | 4245 | { |
19e65f30 | 4246 | cp_token *token; |
c8094d83 | 4247 | |
19e65f30 | 4248 | token = cp_lexer_token_at (parser->lexer, start); |
a723baf1 MM |
4249 | /* Reset the contents of the START token. */ |
4250 | token->type = CPP_NESTED_NAME_SPECIFIER; | |
19e65f30 MM |
4251 | /* Retrieve any deferred checks. Do not pop this access checks yet |
4252 | so the memory will not be reclaimed during token replacing below. */ | |
d6b418fa SM |
4253 | token->u.tree_check_value = GGC_CNEW (struct tree_check); |
4254 | token->u.tree_check_value->value = parser->scope; | |
4255 | token->u.tree_check_value->checks = get_deferred_access_checks (); | |
4256 | token->u.tree_check_value->qualifying_scope = | |
4257 | parser->qualifying_scope; | |
a723baf1 | 4258 | token->keyword = RID_MAX; |
c8094d83 | 4259 | |
a723baf1 | 4260 | /* Purge all subsequent tokens. */ |
0c5e4866 | 4261 | cp_lexer_purge_tokens_after (parser->lexer, start); |
a723baf1 | 4262 | } |
3db45ab5 | 4263 | |
19e65f30 MM |
4264 | if (start) |
4265 | pop_to_parent_deferring_access_checks (); | |
a723baf1 MM |
4266 | |
4267 | return success ? parser->scope : NULL_TREE; | |
4268 | } | |
4269 | ||
4270 | /* Parse a nested-name-specifier. See | |
4271 | cp_parser_nested_name_specifier_opt for details. This function | |
4272 | behaves identically, except that it will an issue an error if no | |
8fe4d24b | 4273 | nested-name-specifier is present. */ |
a723baf1 MM |
4274 | |
4275 | static tree | |
21526606 EC |
4276 | cp_parser_nested_name_specifier (cp_parser *parser, |
4277 | bool typename_keyword_p, | |
a723baf1 | 4278 | bool check_dependency_p, |
a668c6ad MM |
4279 | bool type_p, |
4280 | bool is_declaration) | |
a723baf1 MM |
4281 | { |
4282 | tree scope; | |
4283 | ||
4284 | /* Look for the nested-name-specifier. */ | |
4285 | scope = cp_parser_nested_name_specifier_opt (parser, | |
4286 | typename_keyword_p, | |
4287 | check_dependency_p, | |
a668c6ad MM |
4288 | type_p, |
4289 | is_declaration); | |
a723baf1 MM |
4290 | /* If it was not present, issue an error message. */ |
4291 | if (!scope) | |
4292 | { | |
4293 | cp_parser_error (parser, "expected nested-name-specifier"); | |
eb5abb39 | 4294 | parser->scope = NULL_TREE; |
a723baf1 MM |
4295 | } |
4296 | ||
4297 | return scope; | |
4298 | } | |
4299 | ||
adf2edec DG |
4300 | /* Parse the qualifying entity in a nested-name-specifier. For C++98, |
4301 | this is either a class-name or a namespace-name (which corresponds | |
4302 | to the class-or-namespace-name production in the grammar). For | |
4303 | C++0x, it can also be a type-name that refers to an enumeration | |
4304 | type. | |
a723baf1 MM |
4305 | |
4306 | TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect. | |
4307 | TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect. | |
4308 | CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up. | |
4309 | TYPE_P is TRUE iff the next name should be taken as a class-name, | |
4310 | even the same name is declared to be another entity in the same | |
4311 | scope. | |
4312 | ||
4313 | Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL) | |
eea9800f MM |
4314 | specified by the class-or-namespace-name. If neither is found the |
4315 | ERROR_MARK_NODE is returned. */ | |
a723baf1 MM |
4316 | |
4317 | static tree | |
adf2edec DG |
4318 | cp_parser_qualifying_entity (cp_parser *parser, |
4319 | bool typename_keyword_p, | |
4320 | bool template_keyword_p, | |
4321 | bool check_dependency_p, | |
4322 | bool type_p, | |
4323 | bool is_declaration) | |
a723baf1 MM |
4324 | { |
4325 | tree saved_scope; | |
4326 | tree saved_qualifying_scope; | |
4327 | tree saved_object_scope; | |
4328 | tree scope; | |
eea9800f | 4329 | bool only_class_p; |
adf2edec | 4330 | bool successful_parse_p; |
a723baf1 | 4331 | |
a723baf1 MM |
4332 | /* Before we try to parse the class-name, we must save away the |
4333 | current PARSER->SCOPE since cp_parser_class_name will destroy | |
4334 | it. */ | |
4335 | saved_scope = parser->scope; | |
4336 | saved_qualifying_scope = parser->qualifying_scope; | |
4337 | saved_object_scope = parser->object_scope; | |
eea9800f MM |
4338 | /* Try for a class-name first. If the SAVED_SCOPE is a type, then |
4339 | there is no need to look for a namespace-name. */ | |
adf2edec DG |
4340 | only_class_p = template_keyword_p |
4341 | || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98); | |
eea9800f MM |
4342 | if (!only_class_p) |
4343 | cp_parser_parse_tentatively (parser); | |
21526606 | 4344 | scope = cp_parser_class_name (parser, |
a723baf1 MM |
4345 | typename_keyword_p, |
4346 | template_keyword_p, | |
fc6a28d7 | 4347 | type_p ? class_type : none_type, |
a723baf1 | 4348 | check_dependency_p, |
a668c6ad MM |
4349 | /*class_head_p=*/false, |
4350 | is_declaration); | |
adf2edec DG |
4351 | successful_parse_p = only_class_p || cp_parser_parse_definitely (parser); |
4352 | /* If that didn't work and we're in C++0x mode, try for a type-name. */ | |
4353 | if (!only_class_p | |
4354 | && cxx_dialect != cxx98 | |
4355 | && !successful_parse_p) | |
4356 | { | |
4357 | /* Restore the saved scope. */ | |
4358 | parser->scope = saved_scope; | |
4359 | parser->qualifying_scope = saved_qualifying_scope; | |
4360 | parser->object_scope = saved_object_scope; | |
4361 | ||
4362 | /* Parse tentatively. */ | |
4363 | cp_parser_parse_tentatively (parser); | |
4364 | ||
4365 | /* Parse a typedef-name or enum-name. */ | |
4366 | scope = cp_parser_nonclass_name (parser); | |
4367 | successful_parse_p = cp_parser_parse_definitely (parser); | |
4368 | } | |
a723baf1 | 4369 | /* If that didn't work, try for a namespace-name. */ |
adf2edec | 4370 | if (!only_class_p && !successful_parse_p) |
a723baf1 MM |
4371 | { |
4372 | /* Restore the saved scope. */ | |
4373 | parser->scope = saved_scope; | |
4374 | parser->qualifying_scope = saved_qualifying_scope; | |
4375 | parser->object_scope = saved_object_scope; | |
eea9800f MM |
4376 | /* If we are not looking at an identifier followed by the scope |
4377 | resolution operator, then this is not part of a | |
4378 | nested-name-specifier. (Note that this function is only used | |
4379 | to parse the components of a nested-name-specifier.) */ | |
4380 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME) | |
4381 | || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE) | |
4382 | return error_mark_node; | |
a723baf1 MM |
4383 | scope = cp_parser_namespace_name (parser); |
4384 | } | |
4385 | ||
4386 | return scope; | |
4387 | } | |
4388 | ||
4389 | /* Parse a postfix-expression. | |
4390 | ||
4391 | postfix-expression: | |
4392 | primary-expression | |
4393 | postfix-expression [ expression ] | |
4394 | postfix-expression ( expression-list [opt] ) | |
4395 | simple-type-specifier ( expression-list [opt] ) | |
21526606 | 4396 | typename :: [opt] nested-name-specifier identifier |
a723baf1 MM |
4397 | ( expression-list [opt] ) |
4398 | typename :: [opt] nested-name-specifier template [opt] template-id | |
4399 | ( expression-list [opt] ) | |
4400 | postfix-expression . template [opt] id-expression | |
4401 | postfix-expression -> template [opt] id-expression | |
4402 | postfix-expression . pseudo-destructor-name | |
4403 | postfix-expression -> pseudo-destructor-name | |
4404 | postfix-expression ++ | |
4405 | postfix-expression -- | |
4406 | dynamic_cast < type-id > ( expression ) | |
4407 | static_cast < type-id > ( expression ) | |
4408 | reinterpret_cast < type-id > ( expression ) | |
4409 | const_cast < type-id > ( expression ) | |
4410 | typeid ( expression ) | |
4411 | typeid ( type-id ) | |
4412 | ||
4413 | GNU Extension: | |
21526606 | 4414 | |
a723baf1 MM |
4415 | postfix-expression: |
4416 | ( type-id ) { initializer-list , [opt] } | |
4417 | ||
4418 | This extension is a GNU version of the C99 compound-literal | |
4419 | construct. (The C99 grammar uses `type-name' instead of `type-id', | |
4420 | but they are essentially the same concept.) | |
4421 | ||
4422 | If ADDRESS_P is true, the postfix expression is the operand of the | |
93678513 | 4423 | `&' operator. CAST_P is true if this expression is the target of a |
c8094d83 | 4424 | cast. |
a723baf1 | 4425 | |
3ad6a8e1 DG |
4426 | If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are |
4427 | class member access expressions [expr.ref]. | |
4428 | ||
a723baf1 MM |
4429 | Returns a representation of the expression. */ |
4430 | ||
4431 | static tree | |
3ad6a8e1 | 4432 | cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p, |
cee9035d NC |
4433 | bool member_access_only_p, |
4434 | cp_id_kind * pidk_return) | |
a723baf1 MM |
4435 | { |
4436 | cp_token *token; | |
4437 | enum rid keyword; | |
b3445994 | 4438 | cp_id_kind idk = CP_ID_KIND_NONE; |
a723baf1 | 4439 | tree postfix_expression = NULL_TREE; |
3ad6a8e1 | 4440 | bool is_member_access = false; |
a723baf1 MM |
4441 | |
4442 | /* Peek at the next token. */ | |
4443 | token = cp_lexer_peek_token (parser->lexer); | |
4444 | /* Some of the productions are determined by keywords. */ | |
4445 | keyword = token->keyword; | |
4446 | switch (keyword) | |
4447 | { | |
4448 | case RID_DYNCAST: | |
4449 | case RID_STATCAST: | |
4450 | case RID_REINTCAST: | |
4451 | case RID_CONSTCAST: | |
4452 | { | |
4453 | tree type; | |
4454 | tree expression; | |
4455 | const char *saved_message; | |
4456 | ||
4457 | /* All of these can be handled in the same way from the point | |
4458 | of view of parsing. Begin by consuming the token | |
4459 | identifying the cast. */ | |
4460 | cp_lexer_consume_token (parser->lexer); | |
21526606 | 4461 | |
a723baf1 MM |
4462 | /* New types cannot be defined in the cast. */ |
4463 | saved_message = parser->type_definition_forbidden_message; | |
4464 | parser->type_definition_forbidden_message | |
4465 | = "types may not be defined in casts"; | |
4466 | ||
4467 | /* Look for the opening `<'. */ | |
3a483371 | 4468 | cp_parser_require (parser, CPP_LESS, "%<<%>"); |
a723baf1 MM |
4469 | /* Parse the type to which we are casting. */ |
4470 | type = cp_parser_type_id (parser); | |
4471 | /* Look for the closing `>'. */ | |
3a483371 | 4472 | cp_parser_require (parser, CPP_GREATER, "%<>%>"); |
a723baf1 MM |
4473 | /* Restore the old message. */ |
4474 | parser->type_definition_forbidden_message = saved_message; | |
4475 | ||
4476 | /* And the expression which is being cast. */ | |
3a483371 | 4477 | cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"); |
cee9035d | 4478 | expression = cp_parser_expression (parser, /*cast_p=*/true, & idk); |
3a483371 | 4479 | cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"); |
a723baf1 | 4480 | |
14d22dd6 MM |
4481 | /* Only type conversions to integral or enumeration types |
4482 | can be used in constant-expressions. */ | |
015c2c66 | 4483 | if (!cast_valid_in_integral_constant_expression_p (type) |
98ca843c | 4484 | && (cp_parser_non_integral_constant_expression |
625cbf93 MM |
4485 | (parser, |
4486 | "a cast to a type other than an integral or " | |
4487 | "enumeration type"))) | |
4488 | return error_mark_node; | |
14d22dd6 | 4489 | |
a723baf1 MM |
4490 | switch (keyword) |
4491 | { | |
4492 | case RID_DYNCAST: | |
4493 | postfix_expression | |
5ade1ed2 | 4494 | = build_dynamic_cast (type, expression, tf_warning_or_error); |
a723baf1 MM |
4495 | break; |
4496 | case RID_STATCAST: | |
4497 | postfix_expression | |
5ade1ed2 | 4498 | = build_static_cast (type, expression, tf_warning_or_error); |
a723baf1 MM |
4499 | break; |
4500 | case RID_REINTCAST: | |
4501 | postfix_expression | |
5ade1ed2 DG |
4502 | = build_reinterpret_cast (type, expression, |
4503 | tf_warning_or_error); | |
a723baf1 MM |
4504 | break; |
4505 | case RID_CONSTCAST: | |
4506 | postfix_expression | |
5ade1ed2 | 4507 | = build_const_cast (type, expression, tf_warning_or_error); |
a723baf1 MM |
4508 | break; |
4509 | default: | |
315fb5db | 4510 | gcc_unreachable (); |
a723baf1 MM |
4511 | } |
4512 | } | |
4513 | break; | |
4514 | ||
4515 | case RID_TYPEID: | |
4516 | { | |
4517 | tree type; | |
4518 | const char *saved_message; | |
4f8163b1 | 4519 | bool saved_in_type_id_in_expr_p; |
a723baf1 MM |
4520 | |
4521 | /* Consume the `typeid' token. */ | |
4522 | cp_lexer_consume_token (parser->lexer); | |
4523 | /* Look for the `(' token. */ | |
3a483371 | 4524 | cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"); |
a723baf1 MM |
4525 | /* Types cannot be defined in a `typeid' expression. */ |
4526 | saved_message = parser->type_definition_forbidden_message; | |
4527 | parser->type_definition_forbidden_message | |
8b4efb4e | 4528 | = "types may not be defined in a %<typeid%> expression"; |
a723baf1 MM |
4529 | /* We can't be sure yet whether we're looking at a type-id or an |
4530 | expression. */ | |
4531 | cp_parser_parse_tentatively (parser); | |
4532 | /* Try a type-id first. */ | |
4f8163b1 MM |
4533 | saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p; |
4534 | parser->in_type_id_in_expr_p = true; | |
a723baf1 | 4535 | type = cp_parser_type_id (parser); |
4f8163b1 | 4536 | parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p; |
a723baf1 MM |
4537 | /* Look for the `)' token. Otherwise, we can't be sure that |
4538 | we're not looking at an expression: consider `typeid (int | |
4539 | (3))', for example. */ | |
3a483371 | 4540 | cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"); |
a723baf1 MM |
4541 | /* If all went well, simply lookup the type-id. */ |
4542 | if (cp_parser_parse_definitely (parser)) | |
4543 | postfix_expression = get_typeid (type); | |
4544 | /* Otherwise, fall back to the expression variant. */ | |
4545 | else | |
4546 | { | |
4547 | tree expression; | |
4548 | ||
4549 | /* Look for an expression. */ | |
cee9035d | 4550 | expression = cp_parser_expression (parser, /*cast_p=*/false, & idk); |
a723baf1 MM |
4551 | /* Compute its typeid. */ |
4552 | postfix_expression = build_typeid (expression); | |
4553 | /* Look for the `)' token. */ | |
3a483371 | 4554 | cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"); |
a723baf1 | 4555 | } |
5e8c38c2 SM |
4556 | /* Restore the saved message. */ |
4557 | parser->type_definition_forbidden_message = saved_message; | |
4424e0da | 4558 | /* `typeid' may not appear in an integral constant expression. */ |
98ca843c | 4559 | if (cp_parser_non_integral_constant_expression(parser, |
2a97bf74 | 4560 | "%<typeid%> operator")) |
4424e0da | 4561 | return error_mark_node; |
a723baf1 MM |
4562 | } |
4563 | break; | |
21526606 | 4564 | |
a723baf1 MM |
4565 | case RID_TYPENAME: |
4566 | { | |
a723baf1 | 4567 | tree type; |
88a33c34 MM |
4568 | /* The syntax permitted here is the same permitted for an |
4569 | elaborated-type-specifier. */ | |
4570 | type = cp_parser_elaborated_type_specifier (parser, | |
4571 | /*is_friend=*/false, | |
4572 | /*is_declaration=*/false); | |
a723baf1 MM |
4573 | postfix_expression = cp_parser_functional_cast (parser, type); |
4574 | } | |
4575 | break; | |
4576 | ||
4577 | default: | |
4578 | { | |
4579 | tree type; | |
4580 | ||
4581 | /* If the next thing is a simple-type-specifier, we may be | |
4582 | looking at a functional cast. We could also be looking at | |
4583 | an id-expression. So, we try the functional cast, and if | |
4584 | that doesn't work we fall back to the primary-expression. */ | |
4585 | cp_parser_parse_tentatively (parser); | |
4586 | /* Look for the simple-type-specifier. */ | |
21526606 | 4587 | type = cp_parser_simple_type_specifier (parser, |
62d1db17 MM |
4588 | /*decl_specs=*/NULL, |
4589 | CP_PARSER_FLAGS_NONE); | |
a723baf1 MM |
4590 | /* Parse the cast itself. */ |
4591 | if (!cp_parser_error_occurred (parser)) | |
21526606 | 4592 | postfix_expression |
a723baf1 MM |
4593 | = cp_parser_functional_cast (parser, type); |
4594 | /* If that worked, we're done. */ | |
4595 | if (cp_parser_parse_definitely (parser)) | |
4596 | break; | |
4597 | ||
4598 | /* If the functional-cast didn't work out, try a | |
4599 | compound-literal. */ | |
14d22dd6 MM |
4600 | if (cp_parser_allow_gnu_extensions_p (parser) |
4601 | && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)) | |
a723baf1 | 4602 | { |
4038c495 | 4603 | VEC(constructor_elt,gc) *initializer_list = NULL; |
4f8163b1 | 4604 | bool saved_in_type_id_in_expr_p; |
a723baf1 MM |
4605 | |
4606 | cp_parser_parse_tentatively (parser); | |
14d22dd6 MM |
4607 | /* Consume the `('. */ |
4608 | cp_lexer_consume_token (parser->lexer); | |
4609 | /* Parse the type. */ | |
4f8163b1 MM |
4610 | saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p; |
4611 | parser->in_type_id_in_expr_p = true; | |
14d22dd6 | 4612 | type = cp_parser_type_id (parser); |
4f8163b1 | 4613 | parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p; |
14d22dd6 | 4614 | /* Look for the `)'. */ |
3a483371 | 4615 | cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"); |
14d22dd6 | 4616 | /* Look for the `{'. */ |
3a483371 | 4617 | cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"); |
14d22dd6 MM |
4618 | /* If things aren't going well, there's no need to |
4619 | keep going. */ | |
4620 | if (!cp_parser_error_occurred (parser)) | |
a723baf1 | 4621 | { |
39703eb9 | 4622 | bool non_constant_p; |
14d22dd6 | 4623 | /* Parse the initializer-list. */ |
21526606 | 4624 | initializer_list |
39703eb9 | 4625 | = cp_parser_initializer_list (parser, &non_constant_p); |
14d22dd6 MM |
4626 | /* Allow a trailing `,'. */ |
4627 | if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)) | |
4628 | cp_lexer_consume_token (parser->lexer); | |
4629 | /* Look for the final `}'. */ | |
3a483371 | 4630 | cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>"); |
a723baf1 MM |
4631 | } |
4632 | /* If that worked, we're definitely looking at a | |
4633 | compound-literal expression. */ | |
4634 | if (cp_parser_parse_definitely (parser)) | |
4635 | { | |
4636 | /* Warn the user that a compound literal is not | |
4637 | allowed in standard C++. */ | |
509c9d60 | 4638 | pedwarn (input_location, OPT_pedantic, "ISO C++ forbids compound-literals"); |
a757b5c5 KH |
4639 | /* For simplicity, we disallow compound literals in |
4640 | constant-expressions. We could | |
3c38f0ff MM |
4641 | allow compound literals of integer type, whose |
4642 | initializer was a constant, in constant | |
4643 | expressions. Permitting that usage, as a further | |
4644 | extension, would not change the meaning of any | |
4645 | currently accepted programs. (Of course, as | |
4646 | compound literals are not part of ISO C++, the | |
4647 | standard has nothing to say.) */ | |
4648 | if (cp_parser_non_integral_constant_expression | |
4649 | (parser, "non-constant compound literals")) | |
4650 | { | |
4651 | postfix_expression = error_mark_node; | |
4652 | break; | |
4653 | } | |
a723baf1 | 4654 | /* Form the representation of the compound-literal. */ |
21526606 | 4655 | postfix_expression |
09357846 JM |
4656 | = (finish_compound_literal |
4657 | (type, build_constructor (init_list_type_node, | |
4658 | initializer_list))); | |
a723baf1 MM |
4659 | break; |
4660 | } | |
4661 | } | |
4662 | ||
4663 | /* It must be a primary-expression. */ | |
3db45ab5 MS |
4664 | postfix_expression |
4665 | = cp_parser_primary_expression (parser, address_p, cast_p, | |
02ed62dd MM |
4666 | /*template_arg_p=*/false, |
4667 | &idk); | |
a723baf1 MM |
4668 | } |
4669 | break; | |
4670 | } | |
4671 | ||
a723baf1 MM |
4672 | /* Keep looping until the postfix-expression is complete. */ |
4673 | while (true) | |
4674 | { | |
10b1d5e7 MM |
4675 | if (idk == CP_ID_KIND_UNQUALIFIED |
4676 | && TREE_CODE (postfix_expression) == IDENTIFIER_NODE | |
a723baf1 | 4677 | && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)) |
b3445994 | 4678 | /* It is not a Koenig lookup function call. */ |
21526606 | 4679 | postfix_expression |
b3445994 | 4680 | = unqualified_name_lookup_error (postfix_expression); |
21526606 | 4681 | |
a723baf1 MM |
4682 | /* Peek at the next token. */ |
4683 | token = cp_lexer_peek_token (parser->lexer); | |
4684 | ||
4685 | switch (token->type) | |
4686 | { | |
4687 | case CPP_OPEN_SQUARE: | |
7a3ea201 RH |
4688 | postfix_expression |
4689 | = cp_parser_postfix_open_square_expression (parser, | |
4690 | postfix_expression, | |
4691 | false); | |
4692 | idk = CP_ID_KIND_NONE; | |
3ad6a8e1 | 4693 | is_member_access = false; |
a723baf1 MM |
4694 | break; |
4695 | ||
4696 | case CPP_OPEN_PAREN: | |
4697 | /* postfix-expression ( expression-list [opt] ) */ | |
4698 | { | |
6d80c4b9 | 4699 | bool koenig_p; |
88a7beb7 MM |
4700 | bool is_builtin_constant_p; |
4701 | bool saved_integral_constant_expression_p = false; | |
4702 | bool saved_non_integral_constant_expression_p = false; | |
c166b898 | 4703 | VEC(tree,gc) *args; |
88a7beb7 | 4704 | |
3ad6a8e1 DG |
4705 | is_member_access = false; |
4706 | ||
c8094d83 | 4707 | is_builtin_constant_p |
88a7beb7 MM |
4708 | = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression); |
4709 | if (is_builtin_constant_p) | |
4710 | { | |
4711 | /* The whole point of __builtin_constant_p is to allow | |
4712 | non-constant expressions to appear as arguments. */ | |
4713 | saved_integral_constant_expression_p | |
4714 | = parser->integral_constant_expression_p; | |
4715 | saved_non_integral_constant_expression_p | |
4716 | = parser->non_integral_constant_expression_p; | |
4717 | parser->integral_constant_expression_p = false; | |
4718 | } | |
4719 | args = (cp_parser_parenthesized_expression_list | |
c8094d83 | 4720 | (parser, /*is_attribute_list=*/false, |
5d80a306 | 4721 | /*cast_p=*/false, /*allow_expansion_p=*/true, |
88a7beb7 MM |
4722 | /*non_constant_p=*/NULL)); |
4723 | if (is_builtin_constant_p) | |
4724 | { | |
4725 | parser->integral_constant_expression_p | |
4726 | = saved_integral_constant_expression_p; | |
4727 | parser->non_integral_constant_expression_p | |
4728 | = saved_non_integral_constant_expression_p; | |
4729 | } | |
a723baf1 | 4730 | |
c166b898 | 4731 | if (args == NULL) |
7efa3e22 NS |
4732 | { |
4733 | postfix_expression = error_mark_node; | |
4734 | break; | |
4735 | } | |
21526606 | 4736 | |
14d22dd6 MM |
4737 | /* Function calls are not permitted in |
4738 | constant-expressions. */ | |
100d337a MA |
4739 | if (! builtin_valid_in_constant_expr_p (postfix_expression) |
4740 | && cp_parser_non_integral_constant_expression (parser, | |
4741 | "a function call")) | |
14d22dd6 | 4742 | { |
625cbf93 | 4743 | postfix_expression = error_mark_node; |
c166b898 | 4744 | release_tree_vector (args); |
625cbf93 | 4745 | break; |
14d22dd6 | 4746 | } |
a723baf1 | 4747 | |
6d80c4b9 | 4748 | koenig_p = false; |
d095e03c JM |
4749 | if (idk == CP_ID_KIND_UNQUALIFIED |
4750 | || idk == CP_ID_KIND_TEMPLATE_ID) | |
399dedb9 | 4751 | { |
89d594a2 NS |
4752 | if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE) |
4753 | { | |
c166b898 | 4754 | if (!VEC_empty (tree, args)) |
89d594a2 NS |
4755 | { |
4756 | koenig_p = true; | |
5094a795 JM |
4757 | if (!any_type_dependent_arguments_p (args)) |
4758 | postfix_expression | |
4759 | = perform_koenig_lookup (postfix_expression, args); | |
89d594a2 NS |
4760 | } |
4761 | else | |
4762 | postfix_expression | |
4763 | = unqualified_fn_lookup_error (postfix_expression); | |
4764 | } | |
676e33ca MM |
4765 | /* We do not perform argument-dependent lookup if |
4766 | normal lookup finds a non-function, in accordance | |
4767 | with the expected resolution of DR 218. */ | |
c166b898 ILT |
4768 | else if (!VEC_empty (tree, args) |
4769 | && is_overloaded_fn (postfix_expression)) | |
6d80c4b9 | 4770 | { |
89d594a2 NS |
4771 | tree fn = get_first_fn (postfix_expression); |
4772 | ||
4773 | if (TREE_CODE (fn) == TEMPLATE_ID_EXPR) | |
4774 | fn = OVL_CURRENT (TREE_OPERAND (fn, 0)); | |
4775 | ||
4776 | /* Only do argument dependent lookup if regular | |
4777 | lookup does not find a set of member functions. | |
4778 | [basic.lookup.koenig]/2a */ | |
4779 | if (!DECL_FUNCTION_MEMBER_P (fn)) | |
4780 | { | |
4781 | koenig_p = true; | |
5094a795 JM |
4782 | if (!any_type_dependent_arguments_p (args)) |
4783 | postfix_expression | |
4784 | = perform_koenig_lookup (postfix_expression, args); | |
89d594a2 | 4785 | } |
6d80c4b9 | 4786 | } |
399dedb9 | 4787 | } |
21526606 | 4788 | |
d17811fd | 4789 | if (TREE_CODE (postfix_expression) == COMPONENT_REF) |
a723baf1 | 4790 | { |
d17811fd MM |
4791 | tree instance = TREE_OPERAND (postfix_expression, 0); |
4792 | tree fn = TREE_OPERAND (postfix_expression, 1); | |
4793 | ||
4794 | if (processing_template_decl | |
4795 | && (type_dependent_expression_p (instance) | |
4796 | || (!BASELINK_P (fn) | |
4797 | && TREE_CODE (fn) != FIELD_DECL) | |
584672ee | 4798 | || type_dependent_expression_p (fn) |
d17811fd MM |
4799 | || any_type_dependent_arguments_p (args))) |
4800 | { | |
4801 | postfix_expression | |
c166b898 ILT |
4802 | = build_nt_call_vec (postfix_expression, args); |
4803 | release_tree_vector (args); | |
d17811fd MM |
4804 | break; |
4805 | } | |
9f880ef9 MM |
4806 | |
4807 | if (BASELINK_P (fn)) | |
cee9035d | 4808 | { |
9f880ef9 | 4809 | postfix_expression |
21526606 | 4810 | = (build_new_method_call |
c166b898 | 4811 | (instance, fn, &args, NULL_TREE, |
21526606 | 4812 | (idk == CP_ID_KIND_QUALIFIED |
63c9a190 | 4813 | ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL), |
5ade1ed2 DG |
4814 | /*fn_p=*/NULL, |
4815 | tf_warning_or_error)); | |
cee9035d | 4816 | } |
9f880ef9 MM |
4817 | else |
4818 | postfix_expression | |
c166b898 | 4819 | = finish_call_expr (postfix_expression, &args, |
9f880ef9 | 4820 | /*disallow_virtual=*/false, |
5ade1ed2 DG |
4821 | /*koenig_p=*/false, |
4822 | tf_warning_or_error); | |
a723baf1 | 4823 | } |
d17811fd MM |
4824 | else if (TREE_CODE (postfix_expression) == OFFSET_REF |
4825 | || TREE_CODE (postfix_expression) == MEMBER_REF | |
4826 | || TREE_CODE (postfix_expression) == DOTSTAR_EXPR) | |
a723baf1 | 4827 | postfix_expression = (build_offset_ref_call_from_tree |
c166b898 | 4828 | (postfix_expression, &args)); |
b3445994 | 4829 | else if (idk == CP_ID_KIND_QUALIFIED) |
2050a1bb MM |
4830 | /* A call to a static class member, or a namespace-scope |
4831 | function. */ | |
4832 | postfix_expression | |
c166b898 | 4833 | = finish_call_expr (postfix_expression, &args, |
6d80c4b9 | 4834 | /*disallow_virtual=*/true, |
5ade1ed2 DG |
4835 | koenig_p, |
4836 | tf_warning_or_error); | |
a723baf1 | 4837 | else |
2050a1bb | 4838 | /* All other function calls. */ |
21526606 | 4839 | postfix_expression |
c166b898 | 4840 | = finish_call_expr (postfix_expression, &args, |
6d80c4b9 | 4841 | /*disallow_virtual=*/false, |
5ade1ed2 DG |
4842 | koenig_p, |
4843 | tf_warning_or_error); | |
a723baf1 MM |
4844 | |
4845 | /* The POSTFIX_EXPRESSION is certainly no longer an id. */ | |
b3445994 | 4846 | idk = CP_ID_KIND_NONE; |
c166b898 ILT |
4847 | |
4848 | release_tree_vector (args); | |
a723baf1 MM |
4849 | } |
4850 | break; | |
21526606 | 4851 | |
a723baf1 MM |
4852 | case CPP_DOT: |
4853 | case CPP_DEREF: | |
21526606 EC |
4854 | /* postfix-expression . template [opt] id-expression |
4855 | postfix-expression . pseudo-destructor-name | |
a723baf1 MM |
4856 | postfix-expression -> template [opt] id-expression |
4857 | postfix-expression -> pseudo-destructor-name */ | |
98ca843c | 4858 | |
7a3ea201 RH |
4859 | /* Consume the `.' or `->' operator. */ |
4860 | cp_lexer_consume_token (parser->lexer); | |
a723baf1 | 4861 | |
7a3ea201 RH |
4862 | postfix_expression |
4863 | = cp_parser_postfix_dot_deref_expression (parser, token->type, | |
4864 | postfix_expression, | |
2b7a3abf DS |
4865 | false, &idk, |
4866 | token->location); | |
3ad6a8e1 DG |
4867 | |
4868 | is_member_access = true; | |
a723baf1 MM |
4869 | break; |
4870 | ||
4871 | case CPP_PLUS_PLUS: | |
4872 | /* postfix-expression ++ */ | |
4873 | /* Consume the `++' token. */ | |
4874 | cp_lexer_consume_token (parser->lexer); | |
a5ac3982 | 4875 | /* Generate a representation for the complete expression. */ |
21526606 EC |
4876 | postfix_expression |
4877 | = finish_increment_expr (postfix_expression, | |
a5ac3982 | 4878 | POSTINCREMENT_EXPR); |
14d22dd6 | 4879 | /* Increments may not appear in constant-expressions. */ |
625cbf93 MM |
4880 | if (cp_parser_non_integral_constant_expression (parser, |
4881 | "an increment")) | |
4882 | postfix_expression = error_mark_node; | |
b3445994 | 4883 | idk = CP_ID_KIND_NONE; |
3ad6a8e1 | 4884 | is_member_access = false; |
a723baf1 MM |
4885 | break; |
4886 | ||
4887 | case CPP_MINUS_MINUS: | |
4888 | /* postfix-expression -- */ | |
4889 | /* Consume the `--' token. */ | |
4890 | cp_lexer_consume_token (parser->lexer); | |
a5ac3982 | 4891 | /* Generate a representation for the complete expression. */ |
21526606 EC |
4892 | postfix_expression |
4893 | = finish_increment_expr (postfix_expression, | |
a5ac3982 | 4894 | POSTDECREMENT_EXPR); |
14d22dd6 | 4895 | /* Decrements may not appear in constant-expressions. */ |
625cbf93 MM |
4896 | if (cp_parser_non_integral_constant_expression (parser, |
4897 | "a decrement")) | |
4898 | postfix_expression = error_mark_node; | |
b3445994 | 4899 | idk = CP_ID_KIND_NONE; |
3ad6a8e1 | 4900 | is_member_access = false; |
a723baf1 MM |
4901 | break; |
4902 | ||
4903 | default: | |
cee9035d NC |
4904 | if (pidk_return != NULL) |
4905 | * pidk_return = idk; | |
3ad6a8e1 DG |
4906 | if (member_access_only_p) |
4907 | return is_member_access? postfix_expression : error_mark_node; | |
4908 | else | |
4909 | return postfix_expression; | |
a723baf1 MM |
4910 | } |
4911 | } | |
4912 | ||
4913 | /* We should never get here. */ | |
315fb5db | 4914 | gcc_unreachable (); |
a723baf1 MM |
4915 | return error_mark_node; |
4916 | } | |
4917 | ||
7a3ea201 RH |
4918 | /* A subroutine of cp_parser_postfix_expression that also gets hijacked |
4919 | by cp_parser_builtin_offsetof. We're looking for | |
4920 | ||
4921 | postfix-expression [ expression ] | |
4922 | ||
4923 | FOR_OFFSETOF is set if we're being called in that context, which | |
4924 | changes how we deal with integer constant expressions. */ | |
4925 | ||
4926 | static tree | |
4927 | cp_parser_postfix_open_square_expression (cp_parser *parser, | |
4928 | tree postfix_expression, | |
4929 | bool for_offsetof) | |
4930 | { | |
4931 | tree index; | |
4932 | ||
4933 | /* Consume the `[' token. */ | |
4934 | cp_lexer_consume_token (parser->lexer); | |
4935 | ||
4936 | /* Parse the index expression. */ | |
4937 | /* ??? For offsetof, there is a question of what to allow here. If | |
4938 | offsetof is not being used in an integral constant expression context, | |
4939 | then we *could* get the right answer by computing the value at runtime. | |
4940 | If we are in an integral constant expression context, then we might | |
4941 | could accept any constant expression; hard to say without analysis. | |
4942 | Rather than open the barn door too wide right away, allow only integer | |
77880ae4 | 4943 | constant expressions here. */ |
7a3ea201 RH |
4944 | if (for_offsetof) |
4945 | index = cp_parser_constant_expression (parser, false, NULL); | |
4946 | else | |
cee9035d | 4947 | index = cp_parser_expression (parser, /*cast_p=*/false, NULL); |
7a3ea201 RH |
4948 | |
4949 | /* Look for the closing `]'. */ | |
3a483371 | 4950 | cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>"); |
7a3ea201 RH |
4951 | |
4952 | /* Build the ARRAY_REF. */ | |
4953 | postfix_expression = grok_array_decl (postfix_expression, index); | |
4954 | ||
4955 | /* When not doing offsetof, array references are not permitted in | |
4956 | constant-expressions. */ | |
4957 | if (!for_offsetof | |
4958 | && (cp_parser_non_integral_constant_expression | |
4959 | (parser, "an array reference"))) | |
4960 | postfix_expression = error_mark_node; | |
4961 | ||
4962 | return postfix_expression; | |
4963 | } | |
4964 | ||
4965 | /* A subroutine of cp_parser_postfix_expression that also gets hijacked | |
4966 | by cp_parser_builtin_offsetof. We're looking for | |
4967 | ||
4968 | postfix-expression . template [opt] id-expression | |
4969 | postfix-expression . pseudo-destructor-name | |
4970 | postfix-expression -> template [opt] id-expression | |
4971 | postfix-expression -> pseudo-destructor-name | |
4972 | ||
4973 | FOR_OFFSETOF is set if we're being called in that context. That sorta | |
4974 | limits what of the above we'll actually accept, but nevermind. | |
4975 | TOKEN_TYPE is the "." or "->" token, which will already have been | |
4976 | removed from the stream. */ | |
4977 | ||
4978 | static tree | |
4979 | cp_parser_postfix_dot_deref_expression (cp_parser *parser, | |
4980 | enum cpp_ttype token_type, | |
4981 | tree postfix_expression, | |
2b7a3abf DS |
4982 | bool for_offsetof, cp_id_kind *idk, |
4983 | location_t location) | |
7a3ea201 RH |
4984 | { |
4985 | tree name; | |
4986 | bool dependent_p; | |
17a27b4f | 4987 | bool pseudo_destructor_p; |
7a3ea201 RH |
4988 | tree scope = NULL_TREE; |
4989 | ||
4990 | /* If this is a `->' operator, dereference the pointer. */ | |
4991 | if (token_type == CPP_DEREF) | |
4992 | postfix_expression = build_x_arrow (postfix_expression); | |
4993 | /* Check to see whether or not the expression is type-dependent. */ | |
4994 | dependent_p = type_dependent_expression_p (postfix_expression); | |
4995 | /* The identifier following the `->' or `.' is not qualified. */ | |
4996 | parser->scope = NULL_TREE; | |
4997 | parser->qualifying_scope = NULL_TREE; | |
4998 | parser->object_scope = NULL_TREE; | |
4999 | *idk = CP_ID_KIND_NONE; | |
cee9035d | 5000 | |
7a3ea201 RH |
5001 | /* Enter the scope corresponding to the type of the object |
5002 | given by the POSTFIX_EXPRESSION. */ | |
88525e1b | 5003 | if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE) |
7a3ea201 | 5004 | { |
88525e1b | 5005 | scope = TREE_TYPE (postfix_expression); |
7a3ea201 RH |
5006 | /* According to the standard, no expression should ever have |
5007 | reference type. Unfortunately, we do not currently match | |
5008 | the standard in this respect in that our internal representation | |
5009 | of an expression may have reference type even when the standard | |
5010 | says it does not. Therefore, we have to manually obtain the | |
5011 | underlying type here. */ | |
5012 | scope = non_reference (scope); | |
5013 | /* The type of the POSTFIX_EXPRESSION must be complete. */ | |
e4ba2534 MM |
5014 | if (scope == unknown_type_node) |
5015 | { | |
69bc6bff MLI |
5016 | error_at (location, "%qE does not have class type", |
5017 | postfix_expression); | |
e4ba2534 MM |
5018 | scope = NULL_TREE; |
5019 | } | |
88525e1b | 5020 | else |
e4ba2534 | 5021 | scope = complete_type_or_else (scope, NULL_TREE); |
88525e1b NS |
5022 | /* Let the name lookup machinery know that we are processing a |
5023 | class member access expression. */ | |
5024 | parser->context->object_type = scope; | |
7a3ea201 RH |
5025 | /* If something went wrong, we want to be able to discern that case, |
5026 | as opposed to the case where there was no SCOPE due to the type | |
5027 | of expression being dependent. */ | |
5028 | if (!scope) | |
5029 | scope = error_mark_node; | |
5030 | /* If the SCOPE was erroneous, make the various semantic analysis | |
5031 | functions exit quickly -- and without issuing additional error | |
5032 | messages. */ | |
5033 | if (scope == error_mark_node) | |
5034 | postfix_expression = error_mark_node; | |
5035 | } | |
5036 | ||
17a27b4f MM |
5037 | /* Assume this expression is not a pseudo-destructor access. */ |
5038 | pseudo_destructor_p = false; | |
5039 | ||
5040 | /* If the SCOPE is a scalar type, then, if this is a valid program, | |
e426bd2c JJ |
5041 | we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION |
5042 | is type dependent, it can be pseudo-destructor-name or something else. | |
5043 | Try to parse it as pseudo-destructor-name first. */ | |
5044 | if ((scope && SCALAR_TYPE_P (scope)) || dependent_p) | |
7a3ea201 | 5045 | { |
17a27b4f MM |
5046 | tree s; |
5047 | tree type; | |
5048 | ||
5049 | cp_parser_parse_tentatively (parser); | |
5050 | /* Parse the pseudo-destructor-name. */ | |
5051 | s = NULL_TREE; | |
5052 | cp_parser_pseudo_destructor_name (parser, &s, &type); | |
e426bd2c JJ |
5053 | if (dependent_p |
5054 | && (cp_parser_error_occurred (parser) | |
5055 | || TREE_CODE (type) != TYPE_DECL | |
5056 | || !SCALAR_TYPE_P (TREE_TYPE (type)))) | |
5057 | cp_parser_abort_tentative_parse (parser); | |
5058 | else if (cp_parser_parse_definitely (parser)) | |
17a27b4f MM |
5059 | { |
5060 | pseudo_destructor_p = true; | |
5061 | postfix_expression | |
5062 | = finish_pseudo_destructor_expr (postfix_expression, | |
5063 | s, TREE_TYPE (type)); | |
5064 | } | |
5065 | } | |
5066 | ||
5067 | if (!pseudo_destructor_p) | |
5068 | { | |
5069 | /* If the SCOPE is not a scalar type, we are looking at an | |
5070 | ordinary class member access expression, rather than a | |
5071 | pseudo-destructor-name. */ | |
02ed62dd | 5072 | bool template_p; |
2b7a3abf | 5073 | cp_token *token = cp_lexer_peek_token (parser->lexer); |
7a3ea201 | 5074 | /* Parse the id-expression. */ |
3db45ab5 MS |
5075 | name = (cp_parser_id_expression |
5076 | (parser, | |
02ed62dd MM |
5077 | cp_parser_optional_template_keyword (parser), |
5078 | /*check_dependency_p=*/true, | |
5079 | &template_p, | |
fa6098f8 | 5080 | /*declarator_p=*/false, |
88525e1b | 5081 | /*optional_p=*/false)); |
7a3ea201 RH |
5082 | /* In general, build a SCOPE_REF if the member name is qualified. |
5083 | However, if the name was not dependent and has already been | |
5084 | resolved; there is no need to build the SCOPE_REF. For example; | |
5085 | ||
0cbd7506 MS |
5086 | struct X { void f(); }; |
5087 | template <typename T> void f(T* t) { t->X::f(); } | |
7a3ea201 RH |
5088 | |
5089 | Even though "t" is dependent, "X::f" is not and has been resolved | |
5090 | to a BASELINK; there is no need to include scope information. */ | |
5091 | ||
5092 | /* But we do need to remember that there was an explicit scope for | |
5093 | virtual function calls. */ | |
5094 | if (parser->scope) | |
5095 | *idk = CP_ID_KIND_QUALIFIED; | |
5096 | ||
fc6a28d7 MM |
5097 | /* If the name is a template-id that names a type, we will get a |
5098 | TYPE_DECL here. That is invalid code. */ | |
5099 | if (TREE_CODE (name) == TYPE_DECL) | |
7a3ea201 | 5100 | { |
69bc6bff | 5101 | error_at (token->location, "invalid use of %qD", name); |
fc6a28d7 MM |
5102 | postfix_expression = error_mark_node; |
5103 | } | |
5104 | else | |
5105 | { | |
5106 | if (name != error_mark_node && !BASELINK_P (name) && parser->scope) | |
5107 | { | |
02ed62dd MM |
5108 | name = build_qualified_name (/*type=*/NULL_TREE, |
5109 | parser->scope, | |
5110 | name, | |
5111 | template_p); | |
fc6a28d7 MM |
5112 | parser->scope = NULL_TREE; |
5113 | parser->qualifying_scope = NULL_TREE; | |
5114 | parser->object_scope = NULL_TREE; | |
5115 | } | |
5116 | if (scope && name && BASELINK_P (name)) | |
5117 | adjust_result_of_qualified_name_lookup | |
297d4105 | 5118 | (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope); |
fc6a28d7 | 5119 | postfix_expression |
02ed62dd | 5120 | = finish_class_member_access_expr (postfix_expression, name, |
5ade1ed2 DG |
5121 | template_p, |
5122 | tf_warning_or_error); | |
7a3ea201 | 5123 | } |
7a3ea201 | 5124 | } |
7a3ea201 RH |
5125 | |
5126 | /* We no longer need to look up names in the scope of the object on | |
5127 | the left-hand side of the `.' or `->' operator. */ | |
5128 | parser->context->object_type = NULL_TREE; | |
5129 | ||
5130 | /* Outside of offsetof, these operators may not appear in | |
5131 | constant-expressions. */ | |
5132 | if (!for_offsetof | |
98ca843c | 5133 | && (cp_parser_non_integral_constant_expression |
2a97bf74 | 5134 | (parser, token_type == CPP_DEREF ? "%<->%>" : "%<.%>"))) |
7a3ea201 RH |
5135 | postfix_expression = error_mark_node; |
5136 | ||
5137 | return postfix_expression; | |
5138 | } | |
5139 | ||
7efa3e22 | 5140 | /* Parse a parenthesized expression-list. |
a723baf1 MM |
5141 | |
5142 | expression-list: | |
5143 | assignment-expression | |
5144 | expression-list, assignment-expression | |
5145 | ||
7efa3e22 NS |
5146 | attribute-list: |
5147 | expression-list | |
5148 | identifier | |
5149 | identifier, expression-list | |
5150 | ||
93678513 MM |
5151 | CAST_P is true if this expression is the target of a cast. |
5152 | ||
5d80a306 DG |
5153 | ALLOW_EXPANSION_P is true if this expression allows expansion of an |
5154 | argument pack. | |
5155 | ||
c166b898 ILT |
5156 | Returns a vector of trees. Each element is a representation of an |
5157 | assignment-expression. NULL is returned if the ( and or ) are | |
5158 | missing. An empty, but allocated, vector is returned on no | |
5159 | expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is true | |
5160 | if this is really an attribute list being parsed. If | |
5161 | NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or | |
5162 | not all of the expressions in the list were constant. */ | |
a723baf1 | 5163 | |
c166b898 | 5164 | static VEC(tree,gc) * |
21526606 | 5165 | cp_parser_parenthesized_expression_list (cp_parser* parser, |
39703eb9 | 5166 | bool is_attribute_list, |
93678513 | 5167 | bool cast_p, |
5d80a306 | 5168 | bool allow_expansion_p, |
39703eb9 | 5169 | bool *non_constant_p) |
a723baf1 | 5170 | { |
c166b898 | 5171 | VEC(tree,gc) *expression_list; |
1ed3dfd5 | 5172 | bool fold_expr_p = is_attribute_list; |
7efa3e22 | 5173 | tree identifier = NULL_TREE; |
0f1e3321 | 5174 | bool saved_greater_than_is_operator_p; |
39703eb9 MM |
5175 | |
5176 | /* Assume all the expressions will be constant. */ | |
5177 | if (non_constant_p) | |
5178 | *non_constant_p = false; | |
5179 | ||
3a483371 | 5180 | if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>")) |
c166b898 ILT |
5181 | return NULL; |
5182 | ||
5183 | expression_list = make_tree_vector (); | |
21526606 | 5184 | |
0f1e3321 JJ |
5185 | /* Within a parenthesized expression, a `>' token is always |
5186 | the greater-than operator. */ | |
5187 | saved_greater_than_is_operator_p | |
5188 | = parser->greater_than_is_operator_p; | |
5189 | parser->greater_than_is_operator_p = true; | |
5190 | ||
a723baf1 | 5191 | /* Consume expressions until there are no more. */ |
7efa3e22 NS |
5192 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)) |
5193 | while (true) | |
5194 | { | |
5195 | tree expr; | |
21526606 | 5196 | |
7efa3e22 NS |
5197 | /* At the beginning of attribute lists, check to see if the |
5198 | next token is an identifier. */ | |
5199 | if (is_attribute_list | |
5200 | && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME) | |
5201 | { | |
5202 | cp_token *token; | |
21526606 | 5203 | |
7efa3e22 NS |
5204 | /* Consume the identifier. */ |
5205 | token = cp_lexer_consume_token (parser->lexer); | |
5206 | /* Save the identifier. */ | |
d6b418fa | 5207 | identifier = token->u.value; |
7efa3e22 NS |
5208 | } |
5209 | else | |
5210 | { | |
09357846 JM |
5211 | bool expr_non_constant_p; |
5212 | ||
7efa3e22 | 5213 | /* Parse the next assignment-expression. */ |
09357846 JM |
5214 | if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)) |
5215 | { | |
5216 | /* A braced-init-list. */ | |
5217 | maybe_warn_cpp0x ("extended initializer lists"); | |
5218 | expr = cp_parser_braced_list (parser, &expr_non_constant_p); | |
5219 | if (non_constant_p && expr_non_constant_p) | |
5220 | *non_constant_p = true; | |
5221 | } | |
5222 | else if (non_constant_p) | |
39703eb9 | 5223 | { |
21526606 | 5224 | expr = (cp_parser_constant_expression |
39703eb9 MM |
5225 | (parser, /*allow_non_constant_p=*/true, |
5226 | &expr_non_constant_p)); | |
5227 | if (expr_non_constant_p) | |
5228 | *non_constant_p = true; | |
5229 | } | |
5230 | else | |
cee9035d | 5231 | expr = cp_parser_assignment_expression (parser, cast_p, NULL); |
a723baf1 | 5232 | |
1ed3dfd5 GB |
5233 | if (fold_expr_p) |
5234 | expr = fold_non_dependent_expr (expr); | |
5235 | ||
5d80a306 DG |
5236 | /* If we have an ellipsis, then this is an expression |
5237 | expansion. */ | |
5238 | if (allow_expansion_p | |
5239 | && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)) | |
5240 | { | |
5241 | /* Consume the `...'. */ | |
5242 | cp_lexer_consume_token (parser->lexer); | |
5243 | ||
5244 | /* Build the argument pack. */ | |
5245 | expr = make_pack_expansion (expr); | |
5246 | } | |
5247 | ||
7efa3e22 NS |
5248 | /* Add it to the list. We add error_mark_node |
5249 | expressions to the list, so that we can still tell if | |
5250 | the correct form for a parenthesized expression-list | |
5251 | is found. That gives better errors. */ | |
c166b898 | 5252 | VEC_safe_push (tree, gc, expression_list, expr); |
a723baf1 | 5253 | |
7efa3e22 NS |
5254 | if (expr == error_mark_node) |
5255 | goto skip_comma; | |
5256 | } | |
a723baf1 | 5257 | |
7efa3e22 NS |
5258 | /* After the first item, attribute lists look the same as |
5259 | expression lists. */ | |
5260 | is_attribute_list = false; | |
21526606 | 5261 | |
7efa3e22 NS |
5262 | get_comma:; |
5263 | /* If the next token isn't a `,', then we are done. */ | |
5264 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)) | |
5265 | break; | |
5266 | ||
5267 | /* Otherwise, consume the `,' and keep going. */ | |
5268 | cp_lexer_consume_token (parser->lexer); | |
5269 | } | |
21526606 | 5270 | |
3a483371 | 5271 | if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>")) |
7efa3e22 NS |
5272 | { |
5273 | int ending; | |
21526606 | 5274 | |
7efa3e22 NS |
5275 | skip_comma:; |
5276 | /* We try and resync to an unnested comma, as that will give the | |
5277 | user better diagnostics. */ | |
21526606 EC |
5278 | ending = cp_parser_skip_to_closing_parenthesis (parser, |
5279 | /*recovering=*/true, | |
4bb8ca28 | 5280 | /*or_comma=*/true, |
a668c6ad | 5281 | /*consume_paren=*/true); |
7efa3e22 NS |
5282 | if (ending < 0) |
5283 | goto get_comma; | |
5284 | if (!ending) | |
0f1e3321 JJ |
5285 | { |
5286 | parser->greater_than_is_operator_p | |
5287 | = saved_greater_than_is_operator_p; | |
c166b898 | 5288 | return NULL; |
0f1e3321 | 5289 | } |
a723baf1 MM |
5290 | } |
5291 | ||
0f1e3321 JJ |
5292 | parser->greater_than_is_operator_p |
5293 | = saved_greater_than_is_operator_p; | |
5294 | ||
7efa3e22 | 5295 | if (identifier) |
c166b898 | 5296 | VEC_safe_insert (tree, gc, expression_list, 0, identifier); |
21526606 | 5297 | |
7efa3e22 | 5298 | return expression_list; |
a723baf1 MM |
5299 | } |
5300 | ||
5301 | /* Parse a pseudo-destructor-name. | |
5302 | ||
5303 | pseudo-destructor-name: | |
5304 | :: [opt] nested-name-specifier [opt] type-name :: ~ type-name | |
5305 | :: [opt] nested-name-specifier template template-id :: ~ type-name | |
5306 | :: [opt] nested-name-specifier [opt] ~ type-name | |
5307 | ||
5308 | If either of the first two productions is used, sets *SCOPE to the | |
5309 | TYPE specified before the final `::'. Otherwise, *SCOPE is set to | |
5310 | NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name, | |
d6e57462 | 5311 | or ERROR_MARK_NODE if the parse fails. */ |
a723baf1 MM |
5312 | |
5313 | static void | |
21526606 | 5314 | cp_parser_pseudo_destructor_name (cp_parser* parser, |
0cbd7506 MS |
5315 | tree* scope, |
5316 | tree* type) | |
a723baf1 MM |
5317 | { |
5318 | bool nested_name_specifier_p; | |
5319 | ||
b14454ba MM |
5320 | /* Assume that things will not work out. */ |
5321 | *type = error_mark_node; | |
5322 | ||
a723baf1 | 5323 | /* Look for the optional `::' operator. */ |
88525e1b | 5324 | cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true); |
a723baf1 | 5325 | /* Look for the optional nested-name-specifier. */ |
21526606 | 5326 | nested_name_specifier_p |
a723baf1 MM |
5327 | = (cp_parser_nested_name_specifier_opt (parser, |
5328 | /*typename_keyword_p=*/false, | |
5329 | /*check_dependency_p=*/true, | |
a668c6ad | 5330 | /*type_p=*/false, |
2de6c675 | 5331 | /*is_declaration=*/false) |
a723baf1 MM |
5332 | != NULL_TREE); |
5333 | /* Now, if we saw a nested-name-specifier, we might be doing the | |
5334 | second production. */ | |
21526606 | 5335 | if (nested_name_specifier_p |
a723baf1 MM |
5336 | && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE)) |
5337 | { | |
5338 | /* Consume the `template' keyword. */ | |
5339 | cp_lexer_consume_token (parser->lexer); | |
5340 | /* Parse the template-id. */ | |
21526606 | 5341 | cp_parser_template_id (parser, |
a723baf1 | 5342 | /*template_keyword_p=*/true, |
a668c6ad MM |
5343 | /*check_dependency_p=*/false, |
5344 | /*is_declaration=*/true); | |
a723baf1 | 5345 | /* Look for the `::' token. */ |
3a483371 | 5346 | cp_parser_require (parser, CPP_SCOPE, "%<::%>"); |
a723baf1 MM |
5347 | } |
5348 | /* If the next token is not a `~', then there might be some | |
9bcb9aae | 5349 | additional qualification. */ |
a723baf1 MM |
5350 | else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL)) |
5351 | { | |
5586014a JJ |
5352 | /* At this point, we're looking for "type-name :: ~". The type-name |
5353 | must not be a class-name, since this is a pseudo-destructor. So, | |
5354 | it must be either an enum-name, or a typedef-name -- both of which | |
5355 | are just identifiers. So, we peek ahead to check that the "::" | |
5356 | and "~" tokens are present; if they are not, then we can avoid | |
5357 | calling type_name. */ | |
5358 | if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME | |
5359 | || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE | |
5360 | || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL) | |
5361 | { | |
5362 | cp_parser_error (parser, "non-scalar type"); | |
5363 | return; | |
5364 | } | |
5365 | ||
a723baf1 | 5366 | /* Look for the type-name. */ |
359be76e | 5367 | *scope = TREE_TYPE (cp_parser_nonclass_name (parser)); |
b14454ba MM |
5368 | if (*scope == error_mark_node) |
5369 | return; | |
5370 | ||
a723baf1 | 5371 | /* Look for the `::' token. */ |
3a483371 | 5372 | cp_parser_require (parser, CPP_SCOPE, "%<::%>"); |
a723baf1 MM |
5373 | } |
5374 | else | |
5375 | *scope = NULL_TREE; | |
5376 | ||
5377 | /* Look for the `~'. */ | |
3a483371 | 5378 | cp_parser_require (parser, CPP_COMPL, "%<~%>"); |
a723baf1 MM |
5379 | /* Look for the type-name again. We are not responsible for |
5380 | checking that it matches the first type-name. */ | |
359be76e | 5381 | *type = cp_parser_nonclass_name (parser); |
a723baf1 MM |
5382 | } |
5383 | ||
5384 | /* Parse a unary-expression. | |
5385 | ||
5386 | unary-expression: | |
5387 | postfix-expression | |
5388 | ++ cast-expression | |
5389 | -- cast-expression | |
5390 | unary-operator cast-expression | |
5391 | sizeof unary-expression | |
5392 | sizeof ( type-id ) | |
5393 | new-expression | |
5394 | delete-expression | |
5395 | ||
5396 | GNU Extensions: | |
5397 | ||
5398 | unary-expression: | |
5399 | __extension__ cast-expression | |
5400 | __alignof__ unary-expression | |
5401 | __alignof__ ( type-id ) | |
5402 | __real__ cast-expression | |
5403 | __imag__ cast-expression | |
5404 | && identifier | |
5405 | ||
5406 | ADDRESS_P is true iff the unary-expression is appearing as the | |
93678513 MM |
5407 | operand of the `&' operator. CAST_P is true if this expression is |
5408 | the target of a cast. | |
a723baf1 | 5409 | |
34cd5ae7 | 5410 | Returns a representation of the expression. */ |
a723baf1 MM |
5411 | |
5412 | static tree | |
cee9035d NC |
5413 | cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p, |
5414 | cp_id_kind * pidk) | |
a723baf1 MM |
5415 | { |
5416 | cp_token *token; | |
5417 | enum tree_code unary_operator; | |
5418 | ||
5419 | /* Peek at the next token. */ | |
5420 | token = cp_lexer_peek_token (parser->lexer); | |
5421 | /* Some keywords give away the kind of expression. */ | |
5422 | if (token->type == CPP_KEYWORD) | |
5423 | { | |
5424 | enum rid keyword = token->keyword; | |
5425 | ||
5426 | switch (keyword) | |
5427 | { | |
5428 | case RID_ALIGNOF: | |
a723baf1 MM |
5429 | case RID_SIZEOF: |
5430 | { | |
5431 | tree operand; | |
7a18b933 | 5432 | enum tree_code op; |
21526606 | 5433 | |
7a18b933 NS |
5434 | op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR; |
5435 | /* Consume the token. */ | |
a723baf1 MM |
5436 | cp_lexer_consume_token (parser->lexer); |
5437 | /* Parse the operand. */ | |
5438 | operand = cp_parser_sizeof_operand (parser, keyword); | |
5439 | ||
7a18b933 NS |
5440 | if (TYPE_P (operand)) |
5441 | return cxx_sizeof_or_alignof_type (operand, op, true); | |
a723baf1 | 5442 | else |
5ade1ed2 | 5443 | return cxx_sizeof_or_alignof_expr (operand, op, true); |
a723baf1 MM |
5444 | } |
5445 | ||
5446 | case RID_NEW: | |
5447 | return cp_parser_new_expression (parser); | |
5448 | ||
5449 | case RID_DELETE: | |
5450 | return cp_parser_delete_expression (parser); | |
21526606 | 5451 | |
a723baf1 MM |
5452 | case RID_EXTENSION: |
5453 | { | |
5454 | /* The saved value of the PEDANTIC flag. */ | |
5455 | int saved_pedantic; | |
5456 | tree expr; | |
5457 | ||
5458 | /* Save away the PEDANTIC flag. */ | |
5459 | cp_parser_extension_opt (parser, &saved_pedantic); | |
5460 | /* Parse the cast-expression. */ | |
d6b4ea85 | 5461 | expr = cp_parser_simple_cast_expression (parser); |
a723baf1 MM |
5462 | /* Restore the PEDANTIC flag. */ |
5463 | pedantic = saved_pedantic; | |
5464 | ||
5465 | return expr; | |
5466 | } | |
5467 | ||
5468 | case RID_REALPART: | |
5469 | case RID_IMAGPART: | |
5470 | { | |
5471 | tree expression; | |
5472 | ||
5473 | /* Consume the `__real__' or `__imag__' token. */ | |
5474 | cp_lexer_consume_token (parser->lexer); | |
5475 | /* Parse the cast-expression. */ | |
d6b4ea85 | 5476 | expression = cp_parser_simple_cast_expression (parser); |
a723baf1 MM |
5477 | /* Create the complete representation. */ |
5478 | return build_x_unary_op ((keyword == RID_REALPART | |
5479 | ? REALPART_EXPR : IMAGPART_EXPR), | |
5ade1ed2 DG |
5480 | expression, |
5481 | tf_warning_or_error); | |
a723baf1 MM |
5482 | } |
5483 | break; | |
5484 | ||
5485 | default: | |
5486 | break; | |
5487 | } | |
5488 | } | |
5489 | ||
5490 | /* Look for the `:: new' and `:: delete', which also signal the | |
5491 | beginning of a new-expression, or delete-expression, | |
5492 | respectively. If the next token is `::', then it might be one of | |
5493 | these. */ | |
5494 | if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)) | |
5495 | { | |
5496 | enum rid keyword; | |
5497 | ||
5498 | /* See if the token after the `::' is one of the keywords in | |
5499 | which we're interested. */ | |
5500 | keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword; | |
5501 | /* If it's `new', we have a new-expression. */ | |
5502 | if (keyword == RID_NEW) | |
5503 | return cp_parser_new_expression (parser); | |
5504 | /* Similarly, for `delete'. */ | |
5505 | else if (keyword == RID_DELETE) | |
5506 | return cp_parser_delete_expression (parser); | |
5507 | } | |
5508 | ||
5509 | /* Look for a unary operator. */ | |
5510 | unary_operator = cp_parser_unary_operator (token); | |
5511 | /* The `++' and `--' operators can be handled similarly, even though | |
5512 | they are not technically unary-operators in the grammar. */ | |
5513 | if (unary_operator == ERROR_MARK) | |
5514 | { | |
5515 | if (token->type == CPP_PLUS_PLUS) | |
5516 | unary_operator = PREINCREMENT_EXPR; | |
5517 | else if (token->type == CPP_MINUS_MINUS) | |
5518 | unary_operator = PREDECREMENT_EXPR; | |
5519 | /* Handle the GNU address-of-label extension. */ | |
5520 | else if (cp_parser_allow_gnu_extensions_p (parser) | |
5521 | && token->type == CPP_AND_AND) | |
5522 | { | |
5523 | tree identifier; | |
f370e66b | 5524 | tree expression; |
6a3799eb | 5525 | location_t loc = cp_lexer_peek_token (parser->lexer)->location; |
a723baf1 MM |
5526 | |
5527 | /* Consume the '&&' token. */ | |
5528 | cp_lexer_consume_token (parser->lexer); | |
5529 | /* Look for the identifier. */ | |
5530 | identifier = cp_parser_identifier (parser); | |
5531 | /* Create an expression representing the address. */ | |
6a3799eb | 5532 | expression = finish_label_address_expr (identifier, loc); |
f370e66b JJ |
5533 | if (cp_parser_non_integral_constant_expression (parser, |
5534 | "the address of a label")) | |
5535 | expression = error_mark_node; | |
5536 | return expression; | |
a723baf1 MM |
5537 | } |
5538 | } | |
5539 | if (unary_operator != ERROR_MARK) | |
5540 | { | |
5541 | tree cast_expression; | |
a5ac3982 MM |
5542 | tree expression = error_mark_node; |
5543 | const char *non_constant_p = NULL; | |
a723baf1 MM |
5544 | |
5545 | /* Consume the operator token. */ | |
5546 | token = cp_lexer_consume_token (parser->lexer); | |
5547 | /* Parse the cast-expression. */ | |
21526606 | 5548 | cast_expression |
c8094d83 | 5549 | = cp_parser_cast_expression (parser, |
93678513 | 5550 | unary_operator == ADDR_EXPR, |
cee9035d | 5551 | /*cast_p=*/false, pidk); |
a723baf1 MM |
5552 | /* Now, build an appropriate representation. */ |
5553 | switch (unary_operator) | |
5554 | { | |
5555 | case INDIRECT_REF: | |
2a97bf74 | 5556 | non_constant_p = "%<*%>"; |
5ade1ed2 DG |
5557 | expression = build_x_indirect_ref (cast_expression, "unary *", |
5558 | tf_warning_or_error); | |
a5ac3982 MM |
5559 | break; |
5560 | ||
a723baf1 | 5561 | case ADDR_EXPR: |
2a97bf74 | 5562 | non_constant_p = "%<&%>"; |
a5ac3982 | 5563 | /* Fall through. */ |
d17811fd | 5564 | case BIT_NOT_EXPR: |
5ade1ed2 DG |
5565 | expression = build_x_unary_op (unary_operator, cast_expression, |
5566 | tf_warning_or_error); | |
a5ac3982 MM |
5567 | break; |
5568 | ||
14d22dd6 MM |
5569 | case PREINCREMENT_EXPR: |
5570 | case PREDECREMENT_EXPR: | |
a5ac3982 | 5571 | non_constant_p = (unary_operator == PREINCREMENT_EXPR |
2a97bf74 | 5572 | ? "%<++%>" : "%<--%>"); |
14d22dd6 | 5573 | /* Fall through. */ |
392e3d51 | 5574 | case UNARY_PLUS_EXPR: |
a723baf1 MM |
5575 | case NEGATE_EXPR: |
5576 | case TRUTH_NOT_EXPR: | |
a5ac3982 MM |
5577 | expression = finish_unary_op_expr (unary_operator, cast_expression); |
5578 | break; | |
a723baf1 | 5579 | |
a723baf1 | 5580 | default: |
315fb5db | 5581 | gcc_unreachable (); |
a723baf1 | 5582 | } |
a5ac3982 | 5583 | |
98ca843c | 5584 | if (non_constant_p |
625cbf93 MM |
5585 | && cp_parser_non_integral_constant_expression (parser, |
5586 | non_constant_p)) | |
5587 | expression = error_mark_node; | |
a5ac3982 MM |
5588 | |
5589 | return expression; | |
a723baf1 MM |
5590 | } |
5591 | ||
3ad6a8e1 | 5592 | return cp_parser_postfix_expression (parser, address_p, cast_p, |
cee9035d NC |
5593 | /*member_access_only_p=*/false, |
5594 | pidk); | |
a723baf1 MM |
5595 | } |
5596 | ||
5597 | /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a | |
5598 | unary-operator, the corresponding tree code is returned. */ | |
5599 | ||
5600 | static enum tree_code | |
94edc4ab | 5601 | cp_parser_unary_operator (cp_token* token) |
a723baf1 MM |
5602 | { |
5603 | switch (token->type) | |
5604 | { | |
5605 | case CPP_MULT: | |
5606 | return INDIRECT_REF; | |
5607 | ||
5608 | case CPP_AND: | |
5609 | return ADDR_EXPR; | |
5610 | ||
5611 | case CPP_PLUS: | |
392e3d51 | 5612 | return UNARY_PLUS_EXPR; |
a723baf1 MM |
5613 | |
5614 | case CPP_MINUS: | |
5615 | return NEGATE_EXPR; | |
5616 | ||
5617 | case CPP_NOT: | |
5618 | return TRUTH_NOT_EXPR; | |
21526606 | 5619 | |
a723baf1 MM |
5620 | case CPP_COMPL: |
5621 | return BIT_NOT_EXPR; | |
5622 | ||
5623 | default: | |
5624 | return ERROR_MARK; | |
5625 | } | |
5626 | } | |
5627 | ||
5628 | /* Parse a new-expression. | |
5629 | ||
ca099ac8 | 5630 | new-expression: |
a723baf1 MM |
5631 | :: [opt] new new-placement [opt] new-type-id new-initializer [opt] |
5632 | :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt] | |
5633 | ||
5634 | Returns a representation of the expression. */ | |
5635 | ||
5636 | static tree | |
94edc4ab | 5637 | cp_parser_new_expression (cp_parser* parser) |
a723baf1 MM |
5638 | { |
5639 | bool global_scope_p; | |
c166b898 | 5640 | VEC(tree,gc) *placement; |
a723baf1 | 5641 | tree type; |
c166b898 | 5642 | VEC(tree,gc) *initializer; |
058b15c1 | 5643 | tree nelts; |
c166b898 | 5644 | tree ret; |
a723baf1 MM |
5645 | |
5646 | /* Look for the optional `::' operator. */ | |
21526606 | 5647 | global_scope_p |
a723baf1 | 5648 | = (cp_parser_global_scope_opt (parser, |
88525e1b | 5649 | /*current_scope_valid_p=*/false) |
a723baf1 MM |
5650 | != NULL_TREE); |
5651 | /* Look for the `new' operator. */ | |
3a483371 | 5652 | cp_parser_require_keyword (parser, RID_NEW, "%<new%>"); |
a723baf1 MM |
5653 | /* There's no easy way to tell a new-placement from the |
5654 | `( type-id )' construct. */ | |
5655 | cp_parser_parse_tentatively (parser); | |
5656 | /* Look for a new-placement. */ | |
5657 | placement = cp_parser_new_placement (parser); | |
5658 | /* If that didn't work out, there's no new-placement. */ | |
5659 | if (!cp_parser_parse_definitely (parser)) | |
c166b898 ILT |
5660 | { |
5661 | if (placement != NULL) | |
5662 | release_tree_vector (placement); | |
5663 | placement = NULL; | |
5664 | } | |
a723baf1 MM |
5665 | |
5666 | /* If the next token is a `(', then we have a parenthesized | |
5667 | type-id. */ | |
5668 | if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)) | |
5669 | { | |
2b7a3abf | 5670 | cp_token *token; |
a723baf1 MM |
5671 | /* Consume the `('. */ |
5672 | cp_lexer_consume_token (parser->lexer); | |
5673 | /* Parse the type-id. */ | |
5674 | type = cp_parser_type_id (parser); | |
5675 | /* Look for the closing `)'. */ | |
3a483371 | 5676 | cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"); |
2b7a3abf | 5677 | token = cp_lexer_peek_token (parser->lexer); |
98ca843c | 5678 | /* There should not be a direct-new-declarator in this production, |
0cbd7506 | 5679 | but GCC used to allowed this, so we check and emit a sensible error |
063e900f GB |
5680 | message for this case. */ |
5681 | if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE)) | |
0da99d4e | 5682 | { |
69bc6bff MLI |
5683 | error_at (token->location, |
5684 | "array bound forbidden after parenthesized type-id"); | |
1f5b3869 MLI |
5685 | inform (token->location, |
5686 | "try removing the parentheses around the type-id"); | |
063e900f GB |
5687 | cp_parser_direct_new_declarator (parser); |
5688 | } | |
17a27b4f | 5689 | nelts = NULL_TREE; |
a723baf1 MM |
5690 | } |
5691 | /* Otherwise, there must be a new-type-id. */ | |
5692 | else | |
058b15c1 | 5693 | type = cp_parser_new_type_id (parser, &nelts); |
a723baf1 | 5694 | |
09357846 JM |
5695 | /* If the next token is a `(' or '{', then we have a new-initializer. */ |
5696 | if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN) | |
5697 | || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)) | |
a723baf1 MM |
5698 | initializer = cp_parser_new_initializer (parser); |
5699 | else | |
c166b898 | 5700 | initializer = NULL; |
a723baf1 | 5701 | |
625cbf93 MM |
5702 | /* A new-expression may not appear in an integral constant |
5703 | expression. */ | |
2a97bf74 | 5704 | if (cp_parser_non_integral_constant_expression (parser, "%<new%>")) |
c166b898 ILT |
5705 | ret = error_mark_node; |
5706 | else | |
5707 | { | |
5708 | /* Create a representation of the new-expression. */ | |
5709 | ret = build_new (&placement, type, nelts, &initializer, global_scope_p, | |
5710 | tf_warning_or_error); | |
5711 | } | |
625cbf93 | 5712 | |
c166b898 ILT |
5713 | if (placement != NULL) |
5714 | release_tree_vector (placement); | |
5715 | if (initializer != NULL) | |
5716 | release_tree_vector (initializer); | |
5717 | ||
5718 | return ret; | |
a723baf1 MM |
5719 | } |
5720 | ||
5721 | /* Parse a new-placement. | |
5722 | ||
5723 | new-placement: | |
5724 | ( expression-list ) | |
5725 | ||
5726 | Returns the same representation as for an expression-list. */ | |
5727 | ||
c166b898 | 5728 | static VEC(tree,gc) * |
94edc4ab | 5729 | cp_parser_new_placement (cp_parser* parser) |
a723baf1 | 5730 | { |
c166b898 | 5731 | VEC(tree,gc) *expression_list; |
a723baf1 | 5732 | |
a723baf1 | 5733 | /* Parse the expression-list. */ |
21526606 | 5734 | expression_list = (cp_parser_parenthesized_expression_list |
5d80a306 | 5735 | (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true, |
93678513 | 5736 | /*non_constant_p=*/NULL)); |
a723baf1 MM |
5737 | |
5738 | return expression_list; | |
5739 | } | |
5740 | ||
5741 | /* Parse a new-type-id. | |
5742 | ||
5743 | new-type-id: | |
5744 | type-specifier-seq new-declarator [opt] | |
5745 | ||
058b15c1 MM |
5746 | Returns the TYPE allocated. If the new-type-id indicates an array |
5747 | type, *NELTS is set to the number of elements in the last array | |
5748 | bound; the TYPE will not include the last array bound. */ | |
a723baf1 MM |
5749 | |
5750 | static tree | |
058b15c1 | 5751 | cp_parser_new_type_id (cp_parser* parser, tree *nelts) |
a723baf1 | 5752 | { |
62d1db17 | 5753 | cp_decl_specifier_seq type_specifier_seq; |
058b15c1 MM |
5754 | cp_declarator *new_declarator; |
5755 | cp_declarator *declarator; | |
5756 | cp_declarator *outer_declarator; | |
a723baf1 | 5757 | const char *saved_message; |
058b15c1 | 5758 | tree type; |
a723baf1 MM |
5759 | |
5760 | /* The type-specifier sequence must not contain type definitions. | |
5761 | (It cannot contain declarations of new types either, but if they | |
5762 | are not definitions we will catch that because they are not | |
5763 | complete.) */ | |
5764 | saved_message = parser->type_definition_forbidden_message; | |
5765 | parser->type_definition_forbidden_message | |
5766 | = "types may not be defined in a new-type-id"; | |
5767 | /* Parse the type-specifier-seq. */ | |
d4113656 MM |
5768 | cp_parser_type_specifier_seq (parser, /*is_condition=*/false, |
5769 | &type_specifier_seq); | |
a723baf1 MM |
5770 | /* Restore the old message. */ |
5771 | parser->type_definition_forbidden_message = saved_message; | |
5772 | /* Parse the new-declarator. */ | |
058b15c1 MM |
5773 | new_declarator = cp_parser_new_declarator_opt (parser); |
5774 | ||
5775 | /* Determine the number of elements in the last array dimension, if | |
5776 | any. */ | |
5777 | *nelts = NULL_TREE; | |
5778 | /* Skip down to the last array dimension. */ | |
5779 | declarator = new_declarator; | |
5780 | outer_declarator = NULL; | |
5781 | while (declarator && (declarator->kind == cdk_pointer | |
5782 | || declarator->kind == cdk_ptrmem)) | |
5783 | { | |
5784 | outer_declarator = declarator; | |
5785 | declarator = declarator->declarator; | |
5786 | } | |
98ca843c | 5787 | while (declarator |
058b15c1 MM |
5788 | && declarator->kind == cdk_array |
5789 | && declarator->declarator | |
5790 | && declarator->declarator->kind == cdk_array) | |
5791 | { | |
5792 | outer_declarator = declarator; | |
5793 | declarator = declarator->declarator; | |
5794 | } | |
98ca843c | 5795 | |
058b15c1 MM |
5796 | if (declarator && declarator->kind == cdk_array) |
5797 | { | |
5798 | *nelts = declarator->u.array.bounds; | |
5799 | if (*nelts == error_mark_node) | |
5800 | *nelts = integer_one_node; | |
c8094d83 | 5801 | |
058b15c1 MM |
5802 | if (outer_declarator) |
5803 | outer_declarator->declarator = declarator->declarator; | |
5804 | else | |
5805 | new_declarator = NULL; | |
5806 | } | |
a723baf1 | 5807 | |
0d9c0892 | 5808 | type = groktypename (&type_specifier_seq, new_declarator, false); |
058b15c1 | 5809 | return type; |
a723baf1 MM |
5810 | } |
5811 | ||
5812 | /* Parse an (optional) new-declarator. | |
5813 | ||
5814 | new-declarator: | |
5815 | ptr-operator new-declarator [opt] | |
5816 | direct-new-declarator | |
5817 | ||
058b15c1 | 5818 | Returns the declarator. */ |
a723baf1 | 5819 | |
058b15c1 | 5820 | static cp_declarator * |
94edc4ab | 5821 | cp_parser_new_declarator_opt (cp_parser* parser) |
a723baf1 MM |
5822 | { |
5823 | enum tree_code code; | |
5824 | tree type; | |
3c01e5df | 5825 | cp_cv_quals cv_quals; |
a723baf1 MM |
5826 | |
5827 | /* We don't know if there's a ptr-operator next, or not. */ | |
5828 | cp_parser_parse_tentatively (parser); | |
5829 | /* Look for a ptr-operator. */ | |
3c01e5df | 5830 | code = cp_parser_ptr_operator (parser, &type, &cv_quals); |
a723baf1 MM |
5831 | /* If that worked, look for more new-declarators. */ |
5832 | if (cp_parser_parse_definitely (parser)) | |
5833 | { | |
058b15c1 | 5834 | cp_declarator *declarator; |
a723baf1 MM |
5835 | |
5836 | /* Parse another optional declarator. */ | |
5837 | declarator = cp_parser_new_declarator_opt (parser); | |
5838 | ||
8af2fec4 RY |
5839 | return cp_parser_make_indirect_declarator |
5840 | (code, type, cv_quals, declarator); | |
a723baf1 MM |
5841 | } |
5842 | ||
5843 | /* If the next token is a `[', there is a direct-new-declarator. */ | |
5844 | if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE)) | |
5845 | return cp_parser_direct_new_declarator (parser); | |
5846 | ||
058b15c1 | 5847 | return NULL; |
a723baf1 MM |
5848 | } |
5849 | ||
5850 | /* Parse a direct-new-declarator. | |
5851 | ||
5852 | direct-new-declarator: | |
5853 | [ expression ] | |
21526606 | 5854 | direct-new-declarator [constant-expression] |
a723baf1 | 5855 | |
058b15c1 | 5856 | */ |
a723baf1 | 5857 | |
058b15c1 | 5858 | static cp_declarator * |
94edc4ab | 5859 | cp_parser_direct_new_declarator (cp_parser* parser) |
a723baf1 | 5860 | { |
058b15c1 | 5861 | cp_declarator *declarator = NULL; |
a723baf1 MM |
5862 | |
5863 | while (true) | |
5864 | { | |
5865 | tree expression; | |
5866 | ||
5867 | /* Look for the opening `['. */ | |
3a483371 | 5868 | cp_parser_require (parser, CPP_OPEN_SQUARE, "%<[%>"); |
a723baf1 MM |
5869 | /* The first expression is not required to be constant. */ |
5870 | if (!declarator) | |
5871 | { | |
2b7a3abf | 5872 | cp_token *token = cp_lexer_peek_token (parser->lexer); |
cee9035d | 5873 | expression = cp_parser_expression (parser, /*cast_p=*/false, NULL); |
a723baf1 MM |
5874 | /* The standard requires that the expression have integral |
5875 | type. DR 74 adds enumeration types. We believe that the | |
5876 | real intent is that these expressions be handled like the | |
5877 | expression in a `switch' condition, which also allows | |
5878 | classes with a single conversion to integral or | |
5879 | enumeration type. */ | |
5880 | if (!processing_template_decl) | |
5881 | { | |
21526606 | 5882 | expression |
a723baf1 MM |
5883 | = build_expr_type_conversion (WANT_INT | WANT_ENUM, |
5884 | expression, | |
b746c5dc | 5885 | /*complain=*/true); |
a723baf1 MM |
5886 | if (!expression) |
5887 | { | |
69bc6bff MLI |
5888 | error_at (token->location, |
5889 | "expression in new-declarator must have integral " | |
5890 | "or enumeration type"); | |
a723baf1 MM |
5891 | expression = error_mark_node; |
5892 | } | |
5893 | } | |
5894 | } | |
5895 | /* But all the other expressions must be. */ | |
5896 | else | |
21526606 EC |
5897 | expression |
5898 | = cp_parser_constant_expression (parser, | |
14d22dd6 MM |
5899 | /*allow_non_constant=*/false, |
5900 | NULL); | |
a723baf1 | 5901 | /* Look for the closing `]'. */ |
3a483371 | 5902 | cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>"); |
a723baf1 MM |
5903 | |
5904 | /* Add this bound to the declarator. */ | |
058b15c1 | 5905 | declarator = make_array_declarator (declarator, expression); |
a723baf1 MM |
5906 | |
5907 | /* If the next token is not a `[', then there are no more | |
5908 | bounds. */ | |
5909 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE)) | |
5910 | break; | |
5911 | } | |
5912 | ||
5913 | return declarator; | |
5914 | } | |
5915 | ||
5916 | /* Parse a new-initializer. | |
5917 | ||
5918 | new-initializer: | |
5919 | ( expression-list [opt] ) | |
09357846 | 5920 | braced-init-list |
a723baf1 | 5921 | |
c166b898 | 5922 | Returns a representation of the expression-list. */ |
a723baf1 | 5923 | |
c166b898 | 5924 | static VEC(tree,gc) * |
94edc4ab | 5925 | cp_parser_new_initializer (cp_parser* parser) |
a723baf1 | 5926 | { |
c166b898 | 5927 | VEC(tree,gc) *expression_list; |
a723baf1 | 5928 | |
09357846 JM |
5929 | if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)) |
5930 | { | |
c166b898 | 5931 | tree t; |
09357846 JM |
5932 | bool expr_non_constant_p; |
5933 | maybe_warn_cpp0x ("extended initializer lists"); | |
c166b898 ILT |
5934 | t = cp_parser_braced_list (parser, &expr_non_constant_p); |
5935 | CONSTRUCTOR_IS_DIRECT_INIT (t) = 1; | |
5936 | expression_list = make_tree_vector_single (t); | |
09357846 JM |
5937 | } |
5938 | else | |
5939 | expression_list = (cp_parser_parenthesized_expression_list | |
5940 | (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true, | |
5941 | /*non_constant_p=*/NULL)); | |
a723baf1 MM |
5942 | |
5943 | return expression_list; | |
5944 | } | |
5945 | ||
5946 | /* Parse a delete-expression. | |
5947 | ||
5948 | delete-expression: | |
5949 | :: [opt] delete cast-expression | |
5950 | :: [opt] delete [ ] cast-expression | |
5951 | ||
5952 | Returns a representation of the expression. */ | |
5953 | ||
5954 | static tree | |
94edc4ab | 5955 | cp_parser_delete_expression (cp_parser* parser) |
a723baf1 MM |
5956 | { |
5957 | bool global_scope_p; | |
5958 | bool array_p; | |
5959 | tree expression; | |
5960 | ||
5961 | /* Look for the optional `::' operator. */ | |
5962 | global_scope_p | |
5963 | = (cp_parser_global_scope_opt (parser, | |
88525e1b | 5964 | /*current_scope_valid_p=*/false) |
a723baf1 MM |
5965 | != NULL_TREE); |
5966 | /* Look for the `delete' keyword. */ | |
3a483371 | 5967 | cp_parser_require_keyword (parser, RID_DELETE, "%<delete%>"); |
a723baf1 MM |
5968 | /* See if the array syntax is in use. */ |
5969 | if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE)) | |
5970 | { | |
5971 | /* Consume the `[' token. */ | |
5972 | cp_lexer_consume_token (parser->lexer); | |
5973 | /* Look for the `]' token. */ | |
3a483371 | 5974 | cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>"); |
a723baf1 MM |
5975 | /* Remember that this is the `[]' construct. */ |
5976 | array_p = true; | |
5977 | } | |
5978 | else | |
5979 | array_p = false; | |
5980 | ||
5981 | /* Parse the cast-expression. */ | |
d6b4ea85 | 5982 | expression = cp_parser_simple_cast_expression (parser); |
a723baf1 | 5983 | |
625cbf93 MM |
5984 | /* A delete-expression may not appear in an integral constant |
5985 | expression. */ | |
2a97bf74 | 5986 | if (cp_parser_non_integral_constant_expression (parser, "%<delete%>")) |
625cbf93 MM |
5987 | return error_mark_node; |
5988 | ||
a723baf1 MM |
5989 | return delete_sanity (expression, NULL_TREE, array_p, global_scope_p); |
5990 | } | |
5991 | ||
d3f7b2c6 MLI |
5992 | /* Returns true if TOKEN may start a cast-expression and false |
5993 | otherwise. */ | |
5994 | ||
5995 | static bool | |
5996 | cp_parser_token_starts_cast_expression (cp_token *token) | |
5997 | { | |
5998 | switch (token->type) | |
5999 | { | |
6000 | case CPP_COMMA: | |
6001 | case CPP_SEMICOLON: | |
6002 | case CPP_QUERY: | |
6003 | case CPP_COLON: | |
6004 | case CPP_CLOSE_SQUARE: | |
6005 | case CPP_CLOSE_PAREN: | |
6006 | case CPP_CLOSE_BRACE: | |
6007 | case CPP_DOT: | |
6008 | case CPP_DOT_STAR: | |
6009 | case CPP_DEREF: | |
6010 | case CPP_DEREF_STAR: | |
6011 | case CPP_DIV: | |
6012 | case CPP_MOD: | |
6013 | case CPP_LSHIFT: | |
6014 | case CPP_RSHIFT: | |
6015 | case CPP_LESS: | |
6016 | case CPP_GREATER: | |
6017 | case CPP_LESS_EQ: | |
6018 | case CPP_GREATER_EQ: | |
6019 | case CPP_EQ_EQ: | |
6020 | case CPP_NOT_EQ: | |
6021 | case CPP_EQ: | |
6022 | case CPP_MULT_EQ: | |
6023 | case CPP_DIV_EQ: | |
6024 | case CPP_MOD_EQ: | |
6025 | case CPP_PLUS_EQ: | |
6026 | case CPP_MINUS_EQ: | |
6027 | case CPP_RSHIFT_EQ: | |
6028 | case CPP_LSHIFT_EQ: | |
6029 | case CPP_AND_EQ: | |
6030 | case CPP_XOR_EQ: | |
6031 | case CPP_OR_EQ: | |
6032 | case CPP_XOR: | |
6033 | case CPP_OR: | |
6034 | case CPP_OR_OR: | |
a1166926 | 6035 | case CPP_EOF: |
d3f7b2c6 MLI |
6036 | return false; |
6037 | ||
6038 | /* '[' may start a primary-expression in obj-c++. */ | |
6039 | case CPP_OPEN_SQUARE: | |
6040 | return c_dialect_objc (); | |
6041 | ||
6042 | default: | |
6043 | return true; | |
6044 | } | |
6045 | } | |
6046 | ||
a723baf1 MM |
6047 | /* Parse a cast-expression. |
6048 | ||
6049 | cast-expression: | |
6050 | unary-expression | |
6051 | ( type-id ) cast-expression | |
6052 | ||
93678513 MM |
6053 | ADDRESS_P is true iff the unary-expression is appearing as the |
6054 | operand of the `&' operator. CAST_P is true if this expression is | |
6055 | the target of a cast. | |
6056 | ||
a723baf1 MM |
6057 | Returns a representation of the expression. */ |
6058 | ||
6059 | static tree | |
cee9035d NC |
6060 | cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p, |
6061 | cp_id_kind * pidk) | |
a723baf1 MM |
6062 | { |
6063 | /* If it's a `(', then we might be looking at a cast. */ | |
6064 | if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)) | |
6065 | { | |
6066 | tree type = NULL_TREE; | |
6067 | tree expr = NULL_TREE; | |
6068 | bool compound_literal_p; | |
6069 | const char *saved_message; | |
6070 | ||
6071 | /* There's no way to know yet whether or not this is a cast. | |
6072 | For example, `(int (3))' is a unary-expression, while `(int) | |
6073 | 3' is a cast. So, we resort to parsing tentatively. */ | |
6074 | cp_parser_parse_tentatively (parser); | |
6075 | /* Types may not be defined in a cast. */ | |
6076 | saved_message = parser->type_definition_forbidden_message; | |
6077 | parser->type_definition_forbidden_message | |
6078 | = "types may not be defined in casts"; | |
6079 | /* Consume the `('. */ | |
6080 | cp_lexer_consume_token (parser->lexer); | |
6081 | /* A very tricky bit is that `(struct S) { 3 }' is a | |
6082 | compound-literal (which we permit in C++ as an extension). | |
6083 | But, that construct is not a cast-expression -- it is a | |
6084 | postfix-expression. (The reason is that `(struct S) { 3 }.i' | |
6085 | is legal; if the compound-literal were a cast-expression, | |
6086 | you'd need an extra set of parentheses.) But, if we parse | |
6087 | the type-id, and it happens to be a class-specifier, then we | |
6088 | will commit to the parse at that point, because we cannot | |
6089 | undo the action that is done when creating a new class. So, | |
21526606 | 6090 | then we cannot back up and do a postfix-expression. |
a723baf1 MM |
6091 | |
6092 | Therefore, we scan ahead to the closing `)', and check to see | |
6093 | if the token after the `)' is a `{'. If so, we are not | |
21526606 | 6094 | looking at a cast-expression. |
a723baf1 MM |
6095 | |
6096 | Save tokens so that we can put them back. */ | |
6097 | cp_lexer_save_tokens (parser->lexer); | |
6098 | /* Skip tokens until the next token is a closing parenthesis. | |
6099 | If we find the closing `)', and the next token is a `{', then | |
6100 | we are looking at a compound-literal. */ | |
21526606 | 6101 | compound_literal_p |
a668c6ad MM |
6102 | = (cp_parser_skip_to_closing_parenthesis (parser, false, false, |
6103 | /*consume_paren=*/true) | |
a723baf1 MM |
6104 | && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)); |
6105 | /* Roll back the tokens we skipped. */ | |
6106 | cp_lexer_rollback_tokens (parser->lexer); | |
6107 | /* If we were looking at a compound-literal, simulate an error | |
6108 | so that the call to cp_parser_parse_definitely below will | |
6109 | fail. */ | |
6110 | if (compound_literal_p) | |
6111 | cp_parser_simulate_error (parser); | |
6112 | else | |
6113 | { | |
4f8163b1 MM |
6114 | bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p; |
6115 | parser->in_type_id_in_expr_p = true; | |
a723baf1 MM |
6116 | /* Look for the type-id. */ |
6117 | type = cp_parser_type_id (parser); | |
6118 | /* Look for the closing `)'. */ | |
3a483371 | 6119 | cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"); |
4f8163b1 | 6120 | parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p; |
a723baf1 MM |
6121 | } |
6122 | ||
6123 | /* Restore the saved message. */ | |
6124 | parser->type_definition_forbidden_message = saved_message; | |
6125 | ||
d3f7b2c6 MLI |
6126 | /* At this point this can only be either a cast or a |
6127 | parenthesized ctor such as `(T ())' that looks like a cast to | |
6128 | function returning T. */ | |
6129 | if (!cp_parser_error_occurred (parser) | |
6130 | && cp_parser_token_starts_cast_expression (cp_lexer_peek_token | |
6131 | (parser->lexer))) | |
a723baf1 | 6132 | { |
d3f7b2c6 MLI |
6133 | cp_parser_parse_definitely (parser); |
6134 | expr = cp_parser_cast_expression (parser, | |
6135 | /*address_p=*/false, | |
cee9035d | 6136 | /*cast_p=*/true, pidk); |
d3f7b2c6 | 6137 | |
a723baf1 | 6138 | /* Warn about old-style casts, if so requested. */ |
21526606 EC |
6139 | if (warn_old_style_cast |
6140 | && !in_system_header | |
6141 | && !VOID_TYPE_P (type) | |
a723baf1 | 6142 | && current_lang_name != lang_name_c) |
b323323f | 6143 | warning (OPT_Wold_style_cast, "use of old-style cast"); |
14d22dd6 MM |
6144 | |
6145 | /* Only type conversions to integral or enumeration types | |
6146 | can be used in constant-expressions. */ | |
015c2c66 | 6147 | if (!cast_valid_in_integral_constant_expression_p (type) |
98ca843c | 6148 | && (cp_parser_non_integral_constant_expression |
625cbf93 MM |
6149 | (parser, |
6150 | "a cast to a type other than an integral or " | |
6151 | "enumeration type"))) | |
6152 | return error_mark_node; | |
6153 | ||
a723baf1 | 6154 | /* Perform the cast. */ |
c2255bc4 | 6155 | expr = build_c_cast (input_location, type, expr); |
bbaab916 | 6156 | return expr; |
a723baf1 | 6157 | } |
d3f7b2c6 MLI |
6158 | else |
6159 | cp_parser_abort_tentative_parse (parser); | |
a723baf1 MM |
6160 | } |
6161 | ||
6162 | /* If we get here, then it's not a cast, so it must be a | |
6163 | unary-expression. */ | |
cee9035d | 6164 | return cp_parser_unary_expression (parser, address_p, cast_p, pidk); |
a723baf1 MM |
6165 | } |
6166 | ||
b8b94c5b | 6167 | /* Parse a binary expression of the general form: |
a723baf1 MM |
6168 | |
6169 | pm-expression: | |
6170 | cast-expression | |
6171 | pm-expression .* cast-expression | |
6172 | pm-expression ->* cast-expression | |
6173 | ||
77077b39 | 6174 | multiplicative-expression: |
a723baf1 MM |
6175 | pm-expression |
6176 | multiplicative-expression * pm-expression | |
6177 | multiplicative-expression / pm-expression | |
6178 | multiplicative-expression % pm-expression | |
6179 | ||
a723baf1 MM |
6180 | additive-expression: |
6181 | multiplicative-expression | |
6182 | additive-expression + multiplicative-expression | |
6183 | additive-expression - multiplicative-expression | |
6184 | ||
a723baf1 MM |
6185 | shift-expression: |
6186 | additive-expression | |
6187 | shift-expression << additive-expression | |
6188 | shift-expression >> additive-expression | |
6189 | ||
a723baf1 MM |
6190 | relational-expression: |
6191 | shift-expression | |
6192 | relational-expression < shift-expression | |
6193 | relational-expression > shift-expression | |
6194 | relational-expression <= shift-expression | |
6195 | relational-expression >= shift-expression | |
6196 | ||
b8b94c5b | 6197 | GNU Extension: |
c8094d83 | 6198 | |
a723baf1 MM |
6199 | relational-expression: |
6200 | relational-expression <? shift-expression | |
6201 | relational-expression >? shift-expression | |
6202 | ||
a723baf1 MM |
6203 | equality-expression: |
6204 | relational-expression | |
6205 | equality-expression == relational-expression | |
6206 | equality-expression != relational-expression | |
6207 | ||
a723baf1 MM |
6208 | and-expression: |
6209 | equality-expression | |
6210 | and-expression & equality-expression | |
6211 | ||
a723baf1 MM |
6212 | exclusive-or-expression: |
6213 | and-expression | |
6214 | exclusive-or-expression ^ and-expression | |
6215 | ||
b8b94c5b PB |
6216 | inclusive-or-expression: |
6217 | exclusive-or-expression | |
6218 | inclusive-or-expression | exclusive-or-expression | |
a723baf1 | 6219 | |
b8b94c5b PB |
6220 | logical-and-expression: |
6221 | inclusive-or-expression | |
6222 | logical-and-expression && inclusive-or-expression | |
a723baf1 | 6223 | |
b8b94c5b PB |
6224 | logical-or-expression: |
6225 | logical-and-expression | |
6226 | logical-or-expression || logical-and-expression | |
a723baf1 | 6227 | |
b8b94c5b | 6228 | All these are implemented with a single function like: |
a723baf1 | 6229 | |
b8b94c5b PB |
6230 | binary-expression: |
6231 | simple-cast-expression | |
6232 | binary-expression <token> binary-expression | |
a723baf1 | 6233 | |
93678513 MM |
6234 | CAST_P is true if this expression is the target of a cast. |
6235 | ||
b8b94c5b PB |
6236 | The binops_by_token map is used to get the tree codes for each <token> type. |
6237 | binary-expressions are associated according to a precedence table. */ | |
a723baf1 | 6238 | |
c1ae8be5 SM |
6239 | #define TOKEN_PRECEDENCE(token) \ |
6240 | (((token->type == CPP_GREATER \ | |
6241 | || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \ | |
6242 | && !parser->greater_than_is_operator_p) \ | |
6243 | ? PREC_NOT_OPERATOR \ | |
a2ca1ab2 | 6244 | : binops_by_token[token->type].prec) |
a723baf1 MM |
6245 | |
6246 | static tree | |
a68ab351 | 6247 | cp_parser_binary_expression (cp_parser* parser, bool cast_p, |
c5cdb03f | 6248 | bool no_toplevel_fold_p, |
cee9035d NC |
6249 | enum cp_parser_prec prec, |
6250 | cp_id_kind * pidk) | |
a723baf1 | 6251 | { |
b8b94c5b PB |
6252 | cp_parser_expression_stack stack; |
6253 | cp_parser_expression_stack_entry *sp = &stack[0]; | |
6254 | tree lhs, rhs; | |
6255 | cp_token *token; | |
2a67bec2 | 6256 | enum tree_code tree_type, lhs_type, rhs_type; |
a68ab351 | 6257 | enum cp_parser_prec new_prec, lookahead_prec; |
b8b94c5b | 6258 | bool overloaded_p; |
a723baf1 | 6259 | |
b8b94c5b | 6260 | /* Parse the first expression. */ |
cee9035d | 6261 | lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p, pidk); |
2a67bec2 | 6262 | lhs_type = ERROR_MARK; |
a723baf1 | 6263 | |
b8b94c5b PB |
6264 | for (;;) |
6265 | { | |
6266 | /* Get an operator token. */ | |
6267 | token = cp_lexer_peek_token (parser->lexer); | |
8ff24a79 | 6268 | |
a2ca1ab2 DG |
6269 | if (warn_cxx0x_compat |
6270 | && token->type == CPP_RSHIFT | |
6271 | && !parser->greater_than_is_operator_p) | |
6272 | { | |
69bc6bff MLI |
6273 | if (warning_at (token->location, OPT_Wc__0x_compat, |
6274 | "%<>>%> operator will be treated as" | |
6275 | " two right angle brackets in C++0x")) | |
6276 | inform (token->location, | |
6277 | "suggest parentheses around %<>>%> expression"); | |
a2ca1ab2 DG |
6278 | } |
6279 | ||
b8b94c5b PB |
6280 | new_prec = TOKEN_PRECEDENCE (token); |
6281 | ||
6282 | /* Popping an entry off the stack means we completed a subexpression: | |
0cbd7506 MS |
6283 | - either we found a token which is not an operator (`>' where it is not |
6284 | an operator, or prec == PREC_NOT_OPERATOR), in which case popping | |
6285 | will happen repeatedly; | |
6286 | - or, we found an operator which has lower priority. This is the case | |
6287 | where the recursive descent *ascends*, as in `3 * 4 + 5' after | |
6288 | parsing `3 * 4'. */ | |
b8b94c5b | 6289 | if (new_prec <= prec) |
0cbd7506 MS |
6290 | { |
6291 | if (sp == stack) | |
b8b94c5b | 6292 | break; |
0cbd7506 | 6293 | else |
b8b94c5b | 6294 | goto pop; |
0cbd7506 | 6295 | } |
a723baf1 | 6296 | |
b8b94c5b PB |
6297 | get_rhs: |
6298 | tree_type = binops_by_token[token->type].tree_type; | |
a723baf1 | 6299 | |
03fd3f84 | 6300 | /* We used the operator token. */ |
b8b94c5b | 6301 | cp_lexer_consume_token (parser->lexer); |
a723baf1 | 6302 | |
98f80e91 ILT |
6303 | /* For "false && x" or "true || x", x will never be executed; |
6304 | disable warnings while evaluating it. */ | |
6305 | if (tree_type == TRUTH_ANDIF_EXPR) | |
6306 | c_inhibit_evaluation_warnings += lhs == truthvalue_false_node; | |
6307 | else if (tree_type == TRUTH_ORIF_EXPR) | |
6308 | c_inhibit_evaluation_warnings += lhs == truthvalue_true_node; | |
6309 | ||
b8b94c5b | 6310 | /* Extract another operand. It may be the RHS of this expression |
0cbd7506 | 6311 | or the LHS of a new, higher priority expression. */ |
b8b94c5b | 6312 | rhs = cp_parser_simple_cast_expression (parser); |
2a67bec2 | 6313 | rhs_type = ERROR_MARK; |
a723baf1 | 6314 | |
b8b94c5b | 6315 | /* Get another operator token. Look up its precedence to avoid |
0cbd7506 MS |
6316 | building a useless (immediately popped) stack entry for common |
6317 | cases such as 3 + 4 + 5 or 3 * 4 + 5. */ | |
b8b94c5b PB |
6318 | token = cp_lexer_peek_token (parser->lexer); |
6319 | lookahead_prec = TOKEN_PRECEDENCE (token); | |
6320 | if (lookahead_prec > new_prec) | |
0cbd7506 MS |
6321 | { |
6322 | /* ... and prepare to parse the RHS of the new, higher priority | |
6323 | expression. Since precedence levels on the stack are | |
43c2a69a PB |
6324 | monotonically increasing, we do not have to care about |
6325 | stack overflows. */ | |
0cbd7506 MS |
6326 | sp->prec = prec; |
6327 | sp->tree_type = tree_type; | |
6328 | sp->lhs = lhs; | |
2a67bec2 | 6329 | sp->lhs_type = lhs_type; |
0cbd7506 MS |
6330 | sp++; |
6331 | lhs = rhs; | |
2a67bec2 | 6332 | lhs_type = rhs_type; |
0cbd7506 MS |
6333 | prec = new_prec; |
6334 | new_prec = lookahead_prec; | |
6335 | goto get_rhs; | |
6336 | ||
6337 | pop: | |
c5cdb03f | 6338 | lookahead_prec = new_prec; |
0cbd7506 | 6339 | /* If the stack is not empty, we have parsed into LHS the right side |
b8b94c5b | 6340 | (`4' in the example above) of an expression we had suspended. |
c8094d83 | 6341 | We can use the information on the stack to recover the LHS (`3') |
b8b94c5b PB |
6342 | from the stack together with the tree code (`MULT_EXPR'), and |
6343 | the precedence of the higher level subexpression | |
6344 | (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token, | |
6345 | which will be used to actually build the additive expression. */ | |
0cbd7506 | 6346 | --sp; |
b8b94c5b | 6347 | prec = sp->prec; |
0cbd7506 MS |
6348 | tree_type = sp->tree_type; |
6349 | rhs = lhs; | |
2a67bec2 | 6350 | rhs_type = lhs_type; |
0cbd7506 | 6351 | lhs = sp->lhs; |
2a67bec2 | 6352 | lhs_type = sp->lhs_type; |
0cbd7506 | 6353 | } |
a723baf1 | 6354 | |
98f80e91 ILT |
6355 | /* Undo the disabling of warnings done above. */ |
6356 | if (tree_type == TRUTH_ANDIF_EXPR) | |
6357 | c_inhibit_evaluation_warnings -= lhs == truthvalue_false_node; | |
6358 | else if (tree_type == TRUTH_ORIF_EXPR) | |
6359 | c_inhibit_evaluation_warnings -= lhs == truthvalue_true_node; | |
6360 | ||
b8b94c5b | 6361 | overloaded_p = false; |
100d537d MLI |
6362 | /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type == |
6363 | ERROR_MARK for everything that is not a binary expression. | |
6364 | This makes warn_about_parentheses miss some warnings that | |
6365 | involve unary operators. For unary expressions we should | |
6366 | pass the correct tree_code unless the unary expression was | |
6367 | surrounded by parentheses. | |
6368 | */ | |
c5cdb03f JJ |
6369 | if (no_toplevel_fold_p |
6370 | && lookahead_prec <= prec | |
6371 | && sp == stack | |
6372 | && TREE_CODE_CLASS (tree_type) == tcc_comparison) | |
6373 | lhs = build2 (tree_type, boolean_type_node, lhs, rhs); | |
6374 | else | |
6375 | lhs = build_x_binary_op (tree_type, lhs, lhs_type, rhs, rhs_type, | |
6376 | &overloaded_p, tf_warning_or_error); | |
2a67bec2 | 6377 | lhs_type = tree_type; |
a723baf1 | 6378 | |
b8b94c5b | 6379 | /* If the binary operator required the use of an overloaded operator, |
0cbd7506 MS |
6380 | then this expression cannot be an integral constant-expression. |
6381 | An overloaded operator can be used even if both operands are | |
6382 | otherwise permissible in an integral constant-expression if at | |
6383 | least one of the operands is of enumeration type. */ | |
a723baf1 | 6384 | |
b8b94c5b | 6385 | if (overloaded_p |
0cbd7506 MS |
6386 | && (cp_parser_non_integral_constant_expression |
6387 | (parser, "calls to overloaded operators"))) | |
6388 | return error_mark_node; | |
b8b94c5b | 6389 | } |
a723baf1 | 6390 | |
b8b94c5b | 6391 | return lhs; |
a723baf1 MM |
6392 | } |
6393 | ||
b8b94c5b | 6394 | |
a723baf1 MM |
6395 | /* Parse the `? expression : assignment-expression' part of a |
6396 | conditional-expression. The LOGICAL_OR_EXPR is the | |
6397 | logical-or-expression that started the conditional-expression. | |
6398 | Returns a representation of the entire conditional-expression. | |
6399 | ||
39703eb9 | 6400 | This routine is used by cp_parser_assignment_expression. |
a723baf1 MM |
6401 | |
6402 | ? expression : assignment-expression | |
21526606 | 6403 | |
a723baf1 | 6404 | GNU Extensions: |
21526606 | 6405 | |
a723baf1 MM |
6406 | ? : assignment-expression */ |
6407 | ||
6408 | static tree | |
94edc4ab | 6409 | cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr) |
a723baf1 MM |
6410 | { |
6411 | tree expr; | |
6412 | tree assignment_expr; | |
6413 | ||
6414 | /* Consume the `?' token. */ | |
6415 | cp_lexer_consume_token (parser->lexer); | |
6416 | if (cp_parser_allow_gnu_extensions_p (parser) | |
6417 | && cp_lexer_next_token_is (parser->lexer, CPP_COLON)) | |
7d882b83 ILT |
6418 | { |
6419 | /* Implicit true clause. */ | |
6420 | expr = NULL_TREE; | |
6421 | c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_true_node; | |
6422 | } | |
a723baf1 | 6423 | else |
7d882b83 ILT |
6424 | { |
6425 | /* Parse the expression. */ | |
6426 | c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_false_node; | |
6427 | expr = cp_parser_expression (parser, /*cast_p=*/false, NULL); | |
6428 | c_inhibit_evaluation_warnings += | |
6429 | ((logical_or_expr == truthvalue_true_node) | |
6430 | - (logical_or_expr == truthvalue_false_node)); | |
6431 | } | |
21526606 | 6432 | |
a723baf1 | 6433 | /* The next token should be a `:'. */ |
3a483371 | 6434 | cp_parser_require (parser, CPP_COLON, "%<:%>"); |
a723baf1 | 6435 | /* Parse the assignment-expression. */ |
cee9035d | 6436 | assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL); |
7d882b83 | 6437 | c_inhibit_evaluation_warnings -= logical_or_expr == truthvalue_true_node; |
a723baf1 MM |
6438 | |
6439 | /* Build the conditional-expression. */ | |
6440 | return build_x_conditional_expr (logical_or_expr, | |
6441 | expr, | |
5ade1ed2 DG |
6442 | assignment_expr, |
6443 | tf_warning_or_error); | |
a723baf1 MM |
6444 | } |
6445 | ||
6446 | /* Parse an assignment-expression. | |
6447 | ||
6448 | assignment-expression: | |
6449 | conditional-expression | |
6450 | logical-or-expression assignment-operator assignment_expression | |
6451 | throw-expression | |
6452 | ||
93678513 MM |
6453 | CAST_P is true if this expression is the target of a cast. |
6454 | ||
a723baf1 MM |
6455 | Returns a representation for the expression. */ |
6456 | ||
6457 | static tree | |
cee9035d NC |
6458 | cp_parser_assignment_expression (cp_parser* parser, bool cast_p, |
6459 | cp_id_kind * pidk) | |
a723baf1 MM |
6460 | { |
6461 | tree expr; | |
6462 | ||
6463 | /* If the next token is the `throw' keyword, then we're looking at | |
6464 | a throw-expression. */ | |
6465 | if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW)) | |
6466 | expr = cp_parser_throw_expression (parser); | |
6467 | /* Otherwise, it must be that we are looking at a | |
6468 | logical-or-expression. */ | |
6469 | else | |
6470 | { | |
b8b94c5b | 6471 | /* Parse the binary expressions (logical-or-expression). */ |
c5cdb03f JJ |
6472 | expr = cp_parser_binary_expression (parser, cast_p, false, |
6473 | PREC_NOT_OPERATOR, pidk); | |
a723baf1 MM |
6474 | /* If the next token is a `?' then we're actually looking at a |
6475 | conditional-expression. */ | |
6476 | if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY)) | |
6477 | return cp_parser_question_colon_clause (parser, expr); | |
21526606 | 6478 | else |
a723baf1 MM |
6479 | { |
6480 | enum tree_code assignment_operator; | |
6481 | ||
6482 | /* If it's an assignment-operator, we're using the second | |
6483 | production. */ | |
21526606 | 6484 | assignment_operator |
a723baf1 MM |
6485 | = cp_parser_assignment_operator_opt (parser); |
6486 | if (assignment_operator != ERROR_MARK) | |
6487 | { | |
09357846 | 6488 | bool non_constant_p; |
a723baf1 MM |
6489 | |
6490 | /* Parse the right-hand side of the assignment. */ | |
09357846 JM |
6491 | tree rhs = cp_parser_initializer_clause (parser, &non_constant_p); |
6492 | ||
6493 | if (BRACE_ENCLOSED_INITIALIZER_P (rhs)) | |
6494 | maybe_warn_cpp0x ("extended initializer lists"); | |
6495 | ||
14d22dd6 MM |
6496 | /* An assignment may not appear in a |
6497 | constant-expression. */ | |
625cbf93 MM |
6498 | if (cp_parser_non_integral_constant_expression (parser, |
6499 | "an assignment")) | |
6500 | return error_mark_node; | |
34cd5ae7 | 6501 | /* Build the assignment expression. */ |
21526606 EC |
6502 | expr = build_x_modify_expr (expr, |
6503 | assignment_operator, | |
5ade1ed2 DG |
6504 | rhs, |
6505 | tf_warning_or_error); | |
a723baf1 MM |
6506 | } |
6507 | } | |
6508 | } | |
6509 | ||
6510 | return expr; | |
6511 | } | |
6512 | ||
6513 | /* Parse an (optional) assignment-operator. | |
6514 | ||
21526606 EC |
6515 | assignment-operator: one of |
6516 | = *= /= %= += -= >>= <<= &= ^= |= | |
a723baf1 MM |
6517 | |
6518 | GNU Extension: | |
21526606 | 6519 | |
a723baf1 MM |
6520 | assignment-operator: one of |
6521 | <?= >?= | |
6522 | ||
6523 | If the next token is an assignment operator, the corresponding tree | |
6524 | code is returned, and the token is consumed. For example, for | |
6525 | `+=', PLUS_EXPR is returned. For `=' itself, the code returned is | |
6526 | NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%', | |
6527 | TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment | |
6528 | operator, ERROR_MARK is returned. */ | |
6529 | ||
6530 | static enum tree_code | |
94edc4ab | 6531 | cp_parser_assignment_operator_opt (cp_parser* parser) |
a723baf1 MM |
6532 | { |
6533 | enum tree_code op; | |
6534 | cp_token *token; | |
6535 | ||
39a13be5 | 6536 | /* Peek at the next token. */ |
a723baf1 MM |
6537 | token = cp_lexer_peek_token (parser->lexer); |
6538 | ||
6539 | switch (token->type) | |
6540 | { | |
6541 | case CPP_EQ: | |
6542 | op = NOP_EXPR; | |
6543 | break; | |
6544 | ||
6545 | case CPP_MULT_EQ: | |
6546 | op = MULT_EXPR; | |
6547 | break; | |
6548 | ||
6549 | case CPP_DIV_EQ: | |
6550 | op = TRUNC_DIV_EXPR; | |
6551 | break; | |
6552 | ||
6553 | case CPP_MOD_EQ: | |
6554 | op = TRUNC_MOD_EXPR; | |
6555 | break; | |
6556 | ||
6557 | case CPP_PLUS_EQ: | |
6558 | op = PLUS_EXPR; | |
6559 | break; | |
6560 | ||
6561 | case CPP_MINUS_EQ: | |
6562 | op = MINUS_EXPR; | |
6563 | break; | |
6564 | ||
6565 | case CPP_RSHIFT_EQ: | |
6566 | op = RSHIFT_EXPR; | |
6567 | break; | |
6568 | ||
6569 | case CPP_LSHIFT_EQ: | |
6570 | op = LSHIFT_EXPR; | |
6571 | break; | |
6572 | ||
6573 | case CPP_AND_EQ: | |
6574 | op = BIT_AND_EXPR; | |
6575 | break; | |
6576 | ||
6577 | case CPP_XOR_EQ: | |
6578 | op = BIT_XOR_EXPR; | |
6579 | break; | |
6580 | ||
6581 | case CPP_OR_EQ: | |
6582 | op = BIT_IOR_EXPR; | |
6583 | break; | |
6584 | ||
21526606 | 6585 | default: |
a723baf1 MM |
6586 | /* Nothing else is an assignment operator. */ |
6587 | op = ERROR_MARK; | |
6588 | } | |
6589 | ||
6590 | /* If it was an assignment operator, consume it. */ | |
6591 | if (op != ERROR_MARK) | |
6592 | cp_lexer_consume_token (parser->lexer); | |
6593 | ||
6594 | return op; | |
6595 | } | |
6596 | ||
6597 | /* Parse an expression. | |
6598 | ||
6599 | expression: | |
6600 | assignment-expression | |
6601 | expression , assignment-expression | |
6602 | ||
93678513 MM |
6603 | CAST_P is true if this expression is the target of a cast. |
6604 | ||
a723baf1 MM |
6605 | Returns a representation of the expression. */ |
6606 | ||
6607 | static tree | |
cee9035d | 6608 | cp_parser_expression (cp_parser* parser, bool cast_p, cp_id_kind * pidk) |
a723baf1 MM |
6609 | { |
6610 | tree expression = NULL_TREE; | |
a723baf1 MM |
6611 | |
6612 | while (true) | |
6613 | { | |
6614 | tree assignment_expression; | |
6615 | ||
6616 | /* Parse the next assignment-expression. */ | |
21526606 | 6617 | assignment_expression |
cee9035d | 6618 | = cp_parser_assignment_expression (parser, cast_p, pidk); |
a723baf1 MM |
6619 | /* If this is the first assignment-expression, we can just |
6620 | save it away. */ | |
6621 | if (!expression) | |
6622 | expression = assignment_expression; | |
a723baf1 | 6623 | else |
d17811fd | 6624 | expression = build_x_compound_expr (expression, |
5ade1ed2 DG |
6625 | assignment_expression, |
6626 | tf_warning_or_error); | |
a723baf1 MM |
6627 | /* If the next token is not a comma, then we are done with the |
6628 | expression. */ | |
6629 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)) | |
6630 | break; | |
6631 | /* Consume the `,'. */ | |
6632 | cp_lexer_consume_token (parser->lexer); | |
14d22dd6 | 6633 | /* A comma operator cannot appear in a constant-expression. */ |
625cbf93 MM |
6634 | if (cp_parser_non_integral_constant_expression (parser, |
6635 | "a comma operator")) | |
6636 | expression = error_mark_node; | |
14d22dd6 | 6637 | } |
a723baf1 MM |
6638 | |
6639 | return expression; | |
6640 | } | |
6641 | ||
21526606 | 6642 | /* Parse a constant-expression. |
a723baf1 MM |
6643 | |
6644 | constant-expression: | |
21526606 | 6645 | conditional-expression |
14d22dd6 MM |
6646 | |
6647 | If ALLOW_NON_CONSTANT_P a non-constant expression is silently | |
d17811fd MM |
6648 | accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not |
6649 | constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P | |
6650 | is false, NON_CONSTANT_P should be NULL. */ | |
a723baf1 MM |
6651 | |
6652 | static tree | |
21526606 | 6653 | cp_parser_constant_expression (cp_parser* parser, |
14d22dd6 MM |
6654 | bool allow_non_constant_p, |
6655 | bool *non_constant_p) | |
a723baf1 | 6656 | { |
67c03833 JM |
6657 | bool saved_integral_constant_expression_p; |
6658 | bool saved_allow_non_integral_constant_expression_p; | |
6659 | bool saved_non_integral_constant_expression_p; | |
a723baf1 MM |
6660 | tree expression; |
6661 | ||
6662 | /* It might seem that we could simply parse the | |
6663 | conditional-expression, and then check to see if it were | |
6664 | TREE_CONSTANT. However, an expression that is TREE_CONSTANT is | |
6665 | one that the compiler can figure out is constant, possibly after | |
6666 | doing some simplifications or optimizations. The standard has a | |
6667 | precise definition of constant-expression, and we must honor | |
6668 | that, even though it is somewhat more restrictive. | |
6669 | ||
6670 | For example: | |
6671 | ||
6672 | int i[(2, 3)]; | |
6673 | ||
6674 | is not a legal declaration, because `(2, 3)' is not a | |
6675 | constant-expression. The `,' operator is forbidden in a | |
6676 | constant-expression. However, GCC's constant-folding machinery | |
6677 | will fold this operation to an INTEGER_CST for `3'. */ | |
6678 | ||
14d22dd6 | 6679 | /* Save the old settings. */ |
67c03833 | 6680 | saved_integral_constant_expression_p = parser->integral_constant_expression_p; |
21526606 | 6681 | saved_allow_non_integral_constant_expression_p |
67c03833 JM |
6682 | = parser->allow_non_integral_constant_expression_p; |
6683 | saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p; | |
a723baf1 | 6684 | /* We are now parsing a constant-expression. */ |
67c03833 JM |
6685 | parser->integral_constant_expression_p = true; |
6686 | parser->allow_non_integral_constant_expression_p = allow_non_constant_p; | |
6687 | parser->non_integral_constant_expression_p = false; | |
39703eb9 MM |
6688 | /* Although the grammar says "conditional-expression", we parse an |
6689 | "assignment-expression", which also permits "throw-expression" | |
6690 | and the use of assignment operators. In the case that | |
6691 | ALLOW_NON_CONSTANT_P is false, we get better errors than we would | |
6692 | otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is | |
6693 | actually essential that we look for an assignment-expression. | |
6694 | For example, cp_parser_initializer_clauses uses this function to | |
6695 | determine whether a particular assignment-expression is in fact | |
6696 | constant. */ | |
cee9035d | 6697 | expression = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL); |
14d22dd6 | 6698 | /* Restore the old settings. */ |
c8094d83 | 6699 | parser->integral_constant_expression_p |
93678513 | 6700 | = saved_integral_constant_expression_p; |
21526606 | 6701 | parser->allow_non_integral_constant_expression_p |
67c03833 | 6702 | = saved_allow_non_integral_constant_expression_p; |
14d22dd6 | 6703 | if (allow_non_constant_p) |
67c03833 | 6704 | *non_constant_p = parser->non_integral_constant_expression_p; |
93678513 MM |
6705 | else if (parser->non_integral_constant_expression_p) |
6706 | expression = error_mark_node; | |
c8094d83 | 6707 | parser->non_integral_constant_expression_p |
93678513 | 6708 | = saved_non_integral_constant_expression_p; |
a723baf1 MM |
6709 | |
6710 | return expression; | |
6711 | } | |
6712 | ||
7a3ea201 RH |
6713 | /* Parse __builtin_offsetof. |
6714 | ||
6715 | offsetof-expression: | |
6716 | "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")" | |
6717 | ||
6718 | offsetof-member-designator: | |
6719 | id-expression | |
6720 | | offsetof-member-designator "." id-expression | |
634b5df5 JJ |
6721 | | offsetof-member-designator "[" expression "]" |
6722 | | offsetof-member-designator "->" id-expression */ | |
7a3ea201 RH |
6723 | |
6724 | static tree | |
6725 | cp_parser_builtin_offsetof (cp_parser *parser) | |
6726 | { | |
6727 | int save_ice_p, save_non_ice_p; | |
6728 | tree type, expr; | |
6729 | cp_id_kind dummy; | |
2b7a3abf | 6730 | cp_token *token; |
7a3ea201 RH |
6731 | |
6732 | /* We're about to accept non-integral-constant things, but will | |
6733 | definitely yield an integral constant expression. Save and | |
6734 | restore these values around our local parsing. */ | |
6735 | save_ice_p = parser->integral_constant_expression_p; | |
6736 | save_non_ice_p = parser->non_integral_constant_expression_p; | |
6737 | ||
6738 | /* Consume the "__builtin_offsetof" token. */ | |
6739 | cp_lexer_consume_token (parser->lexer); | |
6740 | /* Consume the opening `('. */ | |
3a483371 | 6741 | cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"); |
7a3ea201 RH |
6742 | /* Parse the type-id. */ |
6743 | type = cp_parser_type_id (parser); | |
6744 | /* Look for the `,'. */ | |
3a483371 | 6745 | cp_parser_require (parser, CPP_COMMA, "%<,%>"); |
2b7a3abf | 6746 | token = cp_lexer_peek_token (parser->lexer); |
7a3ea201 RH |
6747 | |
6748 | /* Build the (type *)null that begins the traditional offsetof macro. */ | |
5ade1ed2 DG |
6749 | expr = build_static_cast (build_pointer_type (type), null_pointer_node, |
6750 | tf_warning_or_error); | |
7a3ea201 RH |
6751 | |
6752 | /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */ | |
6753 | expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr, | |
2b7a3abf | 6754 | true, &dummy, token->location); |
7a3ea201 RH |
6755 | while (true) |
6756 | { | |
2b7a3abf | 6757 | token = cp_lexer_peek_token (parser->lexer); |
7a3ea201 RH |
6758 | switch (token->type) |
6759 | { | |
6760 | case CPP_OPEN_SQUARE: | |
6761 | /* offsetof-member-designator "[" expression "]" */ | |
6762 | expr = cp_parser_postfix_open_square_expression (parser, expr, true); | |
6763 | break; | |
6764 | ||
634b5df5 JJ |
6765 | case CPP_DEREF: |
6766 | /* offsetof-member-designator "->" identifier */ | |
6767 | expr = grok_array_decl (expr, integer_zero_node); | |
6768 | /* FALLTHRU */ | |
6769 | ||
7a3ea201 RH |
6770 | case CPP_DOT: |
6771 | /* offsetof-member-designator "." identifier */ | |
6772 | cp_lexer_consume_token (parser->lexer); | |
634b5df5 JJ |
6773 | expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT, |
6774 | expr, true, &dummy, | |
2b7a3abf | 6775 | token->location); |
7a3ea201 RH |
6776 | break; |
6777 | ||
6778 | case CPP_CLOSE_PAREN: | |
6779 | /* Consume the ")" token. */ | |
6780 | cp_lexer_consume_token (parser->lexer); | |
6781 | goto success; | |
6782 | ||
6783 | default: | |
6784 | /* Error. We know the following require will fail, but | |
6785 | that gives the proper error message. */ | |
3a483371 | 6786 | cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"); |
7a3ea201 RH |
6787 | cp_parser_skip_to_closing_parenthesis (parser, true, false, true); |
6788 | expr = error_mark_node; | |
6789 | goto failure; | |
6790 | } | |
6791 | } | |
6792 | ||
6793 | success: | |
42c244d8 RH |
6794 | /* If we're processing a template, we can't finish the semantics yet. |
6795 | Otherwise we can fold the entire expression now. */ | |
6796 | if (processing_template_decl) | |
6797 | expr = build1 (OFFSETOF_EXPR, size_type_node, expr); | |
6798 | else | |
c291f8b1 | 6799 | expr = finish_offsetof (expr); |
7a3ea201 RH |
6800 | |
6801 | failure: | |
6802 | parser->integral_constant_expression_p = save_ice_p; | |
6803 | parser->non_integral_constant_expression_p = save_non_ice_p; | |
6804 | ||
6805 | return expr; | |
6806 | } | |
6807 | ||
cb68ec50 PC |
6808 | /* Parse a trait expression. */ |
6809 | ||
6810 | static tree | |
6811 | cp_parser_trait_expr (cp_parser* parser, enum rid keyword) | |
6812 | { | |
6813 | cp_trait_kind kind; | |
6814 | tree type1, type2 = NULL_TREE; | |
6815 | bool binary = false; | |
6816 | cp_decl_specifier_seq decl_specs; | |
6817 | ||
6818 | switch (keyword) | |
6819 | { | |
6820 | case RID_HAS_NOTHROW_ASSIGN: | |
6821 | kind = CPTK_HAS_NOTHROW_ASSIGN; | |
6822 | break; | |
6823 | case RID_HAS_NOTHROW_CONSTRUCTOR: | |
6824 | kind = CPTK_HAS_NOTHROW_CONSTRUCTOR; | |
6825 | break; | |
6826 | case RID_HAS_NOTHROW_COPY: | |
6827 | kind = CPTK_HAS_NOTHROW_COPY; | |
6828 | break; | |
6829 | case RID_HAS_TRIVIAL_ASSIGN: | |
6830 | kind = CPTK_HAS_TRIVIAL_ASSIGN; | |
6831 | break; | |
6832 | case RID_HAS_TRIVIAL_CONSTRUCTOR: | |
6833 | kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR; | |
6834 | break; | |
6835 | case RID_HAS_TRIVIAL_COPY: | |
6836 | kind = CPTK_HAS_TRIVIAL_COPY; | |
6837 | break; | |
6838 | case RID_HAS_TRIVIAL_DESTRUCTOR: | |
6839 | kind = CPTK_HAS_TRIVIAL_DESTRUCTOR; | |
6840 | break; | |
6841 | case RID_HAS_VIRTUAL_DESTRUCTOR: | |
6842 | kind = CPTK_HAS_VIRTUAL_DESTRUCTOR; | |
6843 | break; | |
6844 | case RID_IS_ABSTRACT: | |
6845 | kind = CPTK_IS_ABSTRACT; | |
6846 | break; | |
6847 | case RID_IS_BASE_OF: | |
6848 | kind = CPTK_IS_BASE_OF; | |
6849 | binary = true; | |
6850 | break; | |
6851 | case RID_IS_CLASS: | |
6852 | kind = CPTK_IS_CLASS; | |
6853 | break; | |
6854 | case RID_IS_CONVERTIBLE_TO: | |
6855 | kind = CPTK_IS_CONVERTIBLE_TO; | |
6856 | binary = true; | |
6857 | break; | |
6858 | case RID_IS_EMPTY: | |
6859 | kind = CPTK_IS_EMPTY; | |
6860 | break; | |
6861 | case RID_IS_ENUM: | |
6862 | kind = CPTK_IS_ENUM; | |
6863 | break; | |
6864 | case RID_IS_POD: | |
6865 | kind = CPTK_IS_POD; | |
6866 | break; | |
6867 | case RID_IS_POLYMORPHIC: | |
6868 | kind = CPTK_IS_POLYMORPHIC; | |
6869 | break; | |
c32097d8 JM |
6870 | case RID_IS_STD_LAYOUT: |
6871 | kind = CPTK_IS_STD_LAYOUT; | |
6872 | break; | |
6873 | case RID_IS_TRIVIAL: | |
6874 | kind = CPTK_IS_TRIVIAL; | |
6875 | break; | |
cb68ec50 PC |
6876 | case RID_IS_UNION: |
6877 | kind = CPTK_IS_UNION; | |
6878 | break; | |
6879 | default: | |
6880 | gcc_unreachable (); | |
6881 | } | |
6882 | ||
6883 | /* Consume the token. */ | |
6884 | cp_lexer_consume_token (parser->lexer); | |
6885 | ||
3a483371 | 6886 | cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"); |
cb68ec50 PC |
6887 | |
6888 | type1 = cp_parser_type_id (parser); | |
6889 | ||
a53bf42c PC |
6890 | if (type1 == error_mark_node) |
6891 | return error_mark_node; | |
6892 | ||
cb68ec50 PC |
6893 | /* Build a trivial decl-specifier-seq. */ |
6894 | clear_decl_specs (&decl_specs); | |
6895 | decl_specs.type = type1; | |
6896 | ||
6897 | /* Call grokdeclarator to figure out what type this is. */ | |
6898 | type1 = grokdeclarator (NULL, &decl_specs, TYPENAME, | |
6899 | /*initialized=*/0, /*attrlist=*/NULL); | |
6900 | ||
6901 | if (binary) | |
6902 | { | |
3a483371 | 6903 | cp_parser_require (parser, CPP_COMMA, "%<,%>"); |
cb68ec50 PC |
6904 | |
6905 | type2 = cp_parser_type_id (parser); | |
6906 | ||
a53bf42c PC |
6907 | if (type2 == error_mark_node) |
6908 | return error_mark_node; | |
6909 | ||
cb68ec50 PC |
6910 | /* Build a trivial decl-specifier-seq. */ |
6911 | clear_decl_specs (&decl_specs); | |
6912 | decl_specs.type = type2; | |
6913 | ||
6914 | /* Call grokdeclarator to figure out what type this is. */ | |
6915 | type2 = grokdeclarator (NULL, &decl_specs, TYPENAME, | |
6916 | /*initialized=*/0, /*attrlist=*/NULL); | |
6917 | } | |
6918 | ||
3a483371 | 6919 | cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"); |
cb68ec50 | 6920 | |
a53bf42c PC |
6921 | /* Complete the trait expression, which may mean either processing |
6922 | the trait expr now or saving it for template instantiation. */ | |
cb68ec50 PC |
6923 | return finish_trait_expr (kind, type1, type2); |
6924 | } | |
6925 | ||
a723baf1 MM |
6926 | /* Statements [gram.stmt.stmt] */ |
6927 | ||
21526606 | 6928 | /* Parse a statement. |
a723baf1 MM |
6929 | |
6930 | statement: | |
6931 | labeled-statement | |
6932 | expression-statement | |
6933 | compound-statement | |
6934 | selection-statement | |
6935 | iteration-statement | |
6936 | jump-statement | |
6937 | declaration-statement | |
bc4071dd RH |
6938 | try-block |
6939 | ||
3db45ab5 | 6940 | IN_COMPOUND is true when the statement is nested inside a |
2a67bec2 ILT |
6941 | cp_parser_compound_statement; this matters for certain pragmas. |
6942 | ||
6943 | If IF_P is not NULL, *IF_P is set to indicate whether the statement | |
6944 | is a (possibly labeled) if statement which is not enclosed in braces | |
6945 | and has an else clause. This is used to implement -Wparentheses. */ | |
a723baf1 MM |
6946 | |
6947 | static void | |
bc4071dd | 6948 | cp_parser_statement (cp_parser* parser, tree in_statement_expr, |
2a67bec2 | 6949 | bool in_compound, bool *if_p) |
a723baf1 MM |
6950 | { |
6951 | tree statement; | |
6952 | cp_token *token; | |
93409b8c | 6953 | location_t statement_location; |
a723baf1 | 6954 | |
bc4071dd | 6955 | restart: |
2a67bec2 ILT |
6956 | if (if_p != NULL) |
6957 | *if_p = false; | |
a723baf1 MM |
6958 | /* There is no statement yet. */ |
6959 | statement = NULL_TREE; | |
6960 | /* Peek at the next token. */ | |
6961 | token = cp_lexer_peek_token (parser->lexer); | |
6de9cd9a | 6962 | /* Remember the location of the first token in the statement. */ |
93409b8c | 6963 | statement_location = token->location; |
a723baf1 MM |
6964 | /* If this is a keyword, then that will often determine what kind of |
6965 | statement we have. */ | |
6966 | if (token->type == CPP_KEYWORD) | |
6967 | { | |
6968 | enum rid keyword = token->keyword; | |
6969 | ||
6970 | switch (keyword) | |
6971 | { | |
6972 | case RID_CASE: | |
6973 | case RID_DEFAULT: | |
ddd039d5 SB |
6974 | /* Looks like a labeled-statement with a case label. |
6975 | Parse the label, and then use tail recursion to parse | |
6976 | the statement. */ | |
6977 | cp_parser_label_for_labeled_statement (parser); | |
6978 | goto restart; | |
a723baf1 MM |
6979 | |
6980 | case RID_IF: | |
6981 | case RID_SWITCH: | |
2a67bec2 | 6982 | statement = cp_parser_selection_statement (parser, if_p); |
a723baf1 MM |
6983 | break; |
6984 | ||
6985 | case RID_WHILE: | |
6986 | case RID_DO: | |
6987 | case RID_FOR: | |
6988 | statement = cp_parser_iteration_statement (parser); | |
6989 | break; | |
6990 | ||
6991 | case RID_BREAK: | |
6992 | case RID_CONTINUE: | |
6993 | case RID_RETURN: | |
6994 | case RID_GOTO: | |
6995 | statement = cp_parser_jump_statement (parser); | |
6996 | break; | |
6997 | ||
e58a9aa1 ZL |
6998 | /* Objective-C++ exception-handling constructs. */ |
6999 | case RID_AT_TRY: | |
7000 | case RID_AT_CATCH: | |
7001 | case RID_AT_FINALLY: | |
7002 | case RID_AT_SYNCHRONIZED: | |
7003 | case RID_AT_THROW: | |
7004 | statement = cp_parser_objc_statement (parser); | |
7005 | break; | |
7006 | ||
a723baf1 MM |
7007 | case RID_TRY: |
7008 | statement = cp_parser_try_block (parser); | |
7009 | break; | |
7010 | ||
2ce88bea MLI |
7011 | case RID_NAMESPACE: |
7012 | /* This must be a namespace alias definition. */ | |
7013 | cp_parser_declaration_statement (parser); | |
7014 | return; | |
7015 | ||
a723baf1 MM |
7016 | default: |
7017 | /* It might be a keyword like `int' that can start a | |
7018 | declaration-statement. */ | |
7019 | break; | |
7020 | } | |
7021 | } | |
7022 | else if (token->type == CPP_NAME) | |
7023 | { | |
7024 | /* If the next token is a `:', then we are looking at a | |
7025 | labeled-statement. */ | |
7026 | token = cp_lexer_peek_nth_token (parser->lexer, 2); | |
7027 | if (token->type == CPP_COLON) | |
ddd039d5 SB |
7028 | { |
7029 | /* Looks like a labeled-statement with an ordinary label. | |
7030 | Parse the label, and then use tail recursion to parse | |
7031 | the statement. */ | |
7032 | cp_parser_label_for_labeled_statement (parser); | |
7033 | goto restart; | |
7034 | } | |
a723baf1 MM |
7035 | } |
7036 | /* Anything that starts with a `{' must be a compound-statement. */ | |
7037 | else if (token->type == CPP_OPEN_BRACE) | |
325c3691 | 7038 | statement = cp_parser_compound_statement (parser, NULL, false); |
36952dea ZW |
7039 | /* CPP_PRAGMA is a #pragma inside a function body, which constitutes |
7040 | a statement all its own. */ | |
7041 | else if (token->type == CPP_PRAGMA) | |
7042 | { | |
bc4071dd RH |
7043 | /* Only certain OpenMP pragmas are attached to statements, and thus |
7044 | are considered statements themselves. All others are not. In | |
7045 | the context of a compound, accept the pragma as a "statement" and | |
3db45ab5 | 7046 | return so that we can check for a close brace. Otherwise we |
bc4071dd RH |
7047 | require a real statement and must go back and read one. */ |
7048 | if (in_compound) | |
7049 | cp_parser_pragma (parser, pragma_compound); | |
7050 | else if (!cp_parser_pragma (parser, pragma_stmt)) | |
7051 | goto restart; | |
36952dea ZW |
7052 | return; |
7053 | } | |
0ef8776d VR |
7054 | else if (token->type == CPP_EOF) |
7055 | { | |
7056 | cp_parser_error (parser, "expected statement"); | |
7057 | return; | |
7058 | } | |
a723baf1 MM |
7059 | |
7060 | /* Everything else must be a declaration-statement or an | |
21526606 | 7061 | expression-statement. Try for the declaration-statement |
a723baf1 MM |
7062 | first, unless we are looking at a `;', in which case we know that |
7063 | we have an expression-statement. */ | |
7064 | if (!statement) | |
7065 | { | |
7066 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)) | |
7067 | { | |
7068 | cp_parser_parse_tentatively (parser); | |
7069 | /* Try to parse the declaration-statement. */ | |
7070 | cp_parser_declaration_statement (parser); | |
7071 | /* If that worked, we're done. */ | |
7072 | if (cp_parser_parse_definitely (parser)) | |
7073 | return; | |
7074 | } | |
7075 | /* Look for an expression-statement instead. */ | |
325c3691 | 7076 | statement = cp_parser_expression_statement (parser, in_statement_expr); |
a723baf1 MM |
7077 | } |
7078 | ||
7079 | /* Set the line number for the statement. */ | |
009ed910 | 7080 | if (statement && STATEMENT_CODE_P (TREE_CODE (statement))) |
93409b8c | 7081 | SET_EXPR_LOCATION (statement, statement_location); |
a723baf1 MM |
7082 | } |
7083 | ||
ddd039d5 | 7084 | /* Parse the label for a labeled-statement, i.e. |
a723baf1 | 7085 | |
ddd039d5 SB |
7086 | identifier : |
7087 | case constant-expression : | |
7088 | default : | |
98ce043b MM |
7089 | |
7090 | GNU Extension: | |
ddd039d5 | 7091 | case constant-expression ... constant-expression : statement |
21526606 | 7092 | |
ddd039d5 SB |
7093 | When a label is parsed without errors, the label is added to the |
7094 | parse tree by the finish_* functions, so this function doesn't | |
7095 | have to return the label. */ | |
bc4071dd | 7096 | |
ddd039d5 SB |
7097 | static void |
7098 | cp_parser_label_for_labeled_statement (cp_parser* parser) | |
a723baf1 MM |
7099 | { |
7100 | cp_token *token; | |
5bca4e80 | 7101 | tree label = NULL_TREE; |
a723baf1 MM |
7102 | |
7103 | /* The next token should be an identifier. */ | |
7104 | token = cp_lexer_peek_token (parser->lexer); | |
7105 | if (token->type != CPP_NAME | |
7106 | && token->type != CPP_KEYWORD) | |
7107 | { | |
7108 | cp_parser_error (parser, "expected labeled-statement"); | |
ddd039d5 | 7109 | return; |
a723baf1 MM |
7110 | } |
7111 | ||
7112 | switch (token->keyword) | |
7113 | { | |
7114 | case RID_CASE: | |
7115 | { | |
98ce043b MM |
7116 | tree expr, expr_hi; |
7117 | cp_token *ellipsis; | |
a723baf1 MM |
7118 | |
7119 | /* Consume the `case' token. */ | |
7120 | cp_lexer_consume_token (parser->lexer); | |
7121 | /* Parse the constant-expression. */ | |
21526606 | 7122 | expr = cp_parser_constant_expression (parser, |
d17811fd | 7123 | /*allow_non_constant_p=*/false, |
14d22dd6 | 7124 | NULL); |
98ce043b MM |
7125 | |
7126 | ellipsis = cp_lexer_peek_token (parser->lexer); | |
7127 | if (ellipsis->type == CPP_ELLIPSIS) | |
7128 | { | |
0cbd7506 | 7129 | /* Consume the `...' token. */ |
98ce043b MM |
7130 | cp_lexer_consume_token (parser->lexer); |
7131 | expr_hi = | |
7132 | cp_parser_constant_expression (parser, | |
0cbd7506 | 7133 | /*allow_non_constant_p=*/false, |
98ce043b MM |
7134 | NULL); |
7135 | /* We don't need to emit warnings here, as the common code | |
7136 | will do this for us. */ | |
7137 | } | |
7138 | else | |
7139 | expr_hi = NULL_TREE; | |
7140 | ||
bc4071dd | 7141 | if (parser->in_switch_statement_p) |
c2255bc4 | 7142 | finish_case_label (token->location, expr, expr_hi); |
bc4071dd | 7143 | else |
69bc6bff MLI |
7144 | error_at (token->location, |
7145 | "case label %qE not within a switch statement", | |
7146 | expr); | |
a723baf1 MM |
7147 | } |
7148 | break; | |
7149 | ||
7150 | case RID_DEFAULT: | |
7151 | /* Consume the `default' token. */ | |
7152 | cp_lexer_consume_token (parser->lexer); | |
bc4071dd RH |
7153 | |
7154 | if (parser->in_switch_statement_p) | |
c2255bc4 | 7155 | finish_case_label (token->location, NULL_TREE, NULL_TREE); |
bc4071dd | 7156 | else |
69bc6bff | 7157 | error_at (token->location, "case label not within a switch statement"); |
a723baf1 MM |
7158 | break; |
7159 | ||
7160 | default: | |
7161 | /* Anything else must be an ordinary label. */ | |
5bca4e80 | 7162 | label = finish_label_stmt (cp_parser_identifier (parser)); |
a723baf1 MM |
7163 | break; |
7164 | } | |
7165 | ||
7166 | /* Require the `:' token. */ | |
3a483371 | 7167 | cp_parser_require (parser, CPP_COLON, "%<:%>"); |
5bca4e80 ILT |
7168 | |
7169 | /* An ordinary label may optionally be followed by attributes. | |
7170 | However, this is only permitted if the attributes are then | |
7171 | followed by a semicolon. This is because, for backward | |
7172 | compatibility, when parsing | |
7173 | lab: __attribute__ ((unused)) int i; | |
7174 | we want the attribute to attach to "i", not "lab". */ | |
7175 | if (label != NULL_TREE | |
7176 | && cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE)) | |
7177 | { | |
7178 | tree attrs; | |
7179 | ||
7180 | cp_parser_parse_tentatively (parser); | |
7181 | attrs = cp_parser_attributes_opt (parser); | |
7182 | if (attrs == NULL_TREE | |
7183 | || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)) | |
7184 | cp_parser_abort_tentative_parse (parser); | |
7185 | else if (!cp_parser_parse_definitely (parser)) | |
7186 | ; | |
7187 | else | |
7188 | cplus_decl_attributes (&label, attrs, 0); | |
7189 | } | |
a723baf1 MM |
7190 | } |
7191 | ||
7192 | /* Parse an expression-statement. | |
7193 | ||
7194 | expression-statement: | |
7195 | expression [opt] ; | |
7196 | ||
7197 | Returns the new EXPR_STMT -- or NULL_TREE if the expression | |
a5bcc582 NS |
7198 | statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P |
7199 | indicates whether this expression-statement is part of an | |
7200 | expression statement. */ | |
a723baf1 MM |
7201 | |
7202 | static tree | |
325c3691 | 7203 | cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr) |
a723baf1 | 7204 | { |
a5bcc582 | 7205 | tree statement = NULL_TREE; |
a723baf1 | 7206 | |
a5bcc582 | 7207 | /* If the next token is a ';', then there is no expression |
04c06002 | 7208 | statement. */ |
a723baf1 | 7209 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)) |
cee9035d | 7210 | statement = cp_parser_expression (parser, /*cast_p=*/false, NULL); |
21526606 | 7211 | |
a723baf1 | 7212 | /* Consume the final `;'. */ |
e0860732 | 7213 | cp_parser_consume_semicolon_at_end_of_statement (parser); |
a723baf1 | 7214 | |
325c3691 | 7215 | if (in_statement_expr |
a5bcc582 | 7216 | && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE)) |
93678513 MM |
7217 | /* This is the final expression statement of a statement |
7218 | expression. */ | |
7219 | statement = finish_stmt_expr_expr (statement, in_statement_expr); | |
a5bcc582 NS |
7220 | else if (statement) |
7221 | statement = finish_expr_stmt (statement); | |
7222 | else | |
7223 | finish_stmt (); | |
21526606 | 7224 | |
a723baf1 MM |
7225 | return statement; |
7226 | } | |
7227 | ||
7228 | /* Parse a compound-statement. | |
7229 | ||
7230 | compound-statement: | |
7231 | { statement-seq [opt] } | |
21526606 | 7232 | |
3d3585eb JJ |
7233 | GNU extension: |
7234 | ||
7235 | compound-statement: | |
7236 | { label-declaration-seq [opt] statement-seq [opt] } | |
7237 | ||
7238 | label-declaration-seq: | |
7239 | label-declaration | |
7240 | label-declaration-seq label-declaration | |
7241 | ||
5882f0f3 | 7242 | Returns a tree representing the statement. */ |
a723baf1 MM |
7243 | |
7244 | static tree | |
325c3691 RH |
7245 | cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr, |
7246 | bool in_try) | |
a723baf1 MM |
7247 | { |
7248 | tree compound_stmt; | |
7249 | ||
7250 | /* Consume the `{'. */ | |
3a483371 | 7251 | if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>")) |
a723baf1 MM |
7252 | return error_mark_node; |
7253 | /* Begin the compound-statement. */ | |
325c3691 | 7254 | compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0); |
3d3585eb JJ |
7255 | /* If the next keyword is `__label__' we have a label declaration. */ |
7256 | while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL)) | |
7257 | cp_parser_label_declaration (parser); | |
a723baf1 | 7258 | /* Parse an (optional) statement-seq. */ |
325c3691 | 7259 | cp_parser_statement_seq_opt (parser, in_statement_expr); |
a723baf1 | 7260 | /* Finish the compound-statement. */ |
7a3397c7 | 7261 | finish_compound_stmt (compound_stmt); |
a723baf1 | 7262 | /* Consume the `}'. */ |
3a483371 | 7263 | cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>"); |
a723baf1 MM |
7264 | |
7265 | return compound_stmt; | |
7266 | } | |
7267 | ||
7268 | /* Parse an (optional) statement-seq. | |
7269 | ||
7270 | statement-seq: | |
7271 | statement | |
7272 | statement-seq [opt] statement */ | |
7273 | ||
7274 | static void | |
325c3691 | 7275 | cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr) |
a723baf1 MM |
7276 | { |
7277 | /* Scan statements until there aren't any more. */ | |
7278 | while (true) | |
7279 | { | |
bc4071dd RH |
7280 | cp_token *token = cp_lexer_peek_token (parser->lexer); |
7281 | ||
a723baf1 | 7282 | /* If we're looking at a `}', then we've run out of statements. */ |
bc4071dd RH |
7283 | if (token->type == CPP_CLOSE_BRACE |
7284 | || token->type == CPP_EOF | |
7285 | || token->type == CPP_PRAGMA_EOL) | |
a723baf1 | 7286 | break; |
4606b05c MLI |
7287 | |
7288 | /* If we are in a compound statement and find 'else' then | |
7289 | something went wrong. */ | |
7290 | else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE) | |
7291 | { | |
7292 | if (parser->in_statement & IN_IF_STMT) | |
7293 | break; | |
7294 | else | |
7295 | { | |
7296 | token = cp_lexer_consume_token (parser->lexer); | |
69bc6bff | 7297 | error_at (token->location, "%<else%> without a previous %<if%>"); |
4606b05c MLI |
7298 | } |
7299 | } | |
a723baf1 MM |
7300 | |
7301 | /* Parse the statement. */ | |
2a67bec2 | 7302 | cp_parser_statement (parser, in_statement_expr, true, NULL); |
a723baf1 MM |
7303 | } |
7304 | } | |
7305 | ||
7306 | /* Parse a selection-statement. | |
7307 | ||
7308 | selection-statement: | |
7309 | if ( condition ) statement | |
7310 | if ( condition ) statement else statement | |
21526606 | 7311 | switch ( condition ) statement |
a723baf1 | 7312 | |
2a67bec2 ILT |
7313 | Returns the new IF_STMT or SWITCH_STMT. |
7314 | ||
7315 | If IF_P is not NULL, *IF_P is set to indicate whether the statement | |
7316 | is a (possibly labeled) if statement which is not enclosed in | |
7317 | braces and has an else clause. This is used to implement | |
7318 | -Wparentheses. */ | |
a723baf1 MM |
7319 | |
7320 | static tree | |
2a67bec2 | 7321 | cp_parser_selection_statement (cp_parser* parser, bool *if_p) |
a723baf1 MM |
7322 | { |
7323 | cp_token *token; | |
7324 | enum rid keyword; | |
7325 | ||
2a67bec2 ILT |
7326 | if (if_p != NULL) |
7327 | *if_p = false; | |
7328 | ||
a723baf1 MM |
7329 | /* Peek at the next token. */ |
7330 | token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement"); | |
7331 | ||
7332 | /* See what kind of keyword it is. */ | |
7333 | keyword = token->keyword; | |
7334 | switch (keyword) | |
7335 | { | |
7336 | case RID_IF: | |
7337 | case RID_SWITCH: | |
7338 | { | |
7339 | tree statement; | |
7340 | tree condition; | |
7341 | ||
7342 | /* Look for the `('. */ | |
3a483371 | 7343 | if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>")) |
a723baf1 MM |
7344 | { |
7345 | cp_parser_skip_to_end_of_statement (parser); | |
7346 | return error_mark_node; | |
7347 | } | |
7348 | ||
7349 | /* Begin the selection-statement. */ | |
7350 | if (keyword == RID_IF) | |
7351 | statement = begin_if_stmt (); | |
7352 | else | |
7353 | statement = begin_switch_stmt (); | |
7354 | ||
7355 | /* Parse the condition. */ | |
7356 | condition = cp_parser_condition (parser); | |
7357 | /* Look for the `)'. */ | |
3a483371 | 7358 | if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>")) |
a668c6ad MM |
7359 | cp_parser_skip_to_closing_parenthesis (parser, true, false, |
7360 | /*consume_paren=*/true); | |
a723baf1 MM |
7361 | |
7362 | if (keyword == RID_IF) | |
7363 | { | |
2a67bec2 | 7364 | bool nested_if; |
4606b05c | 7365 | unsigned char in_statement; |
2a67bec2 | 7366 | |
a723baf1 MM |
7367 | /* Add the condition. */ |
7368 | finish_if_stmt_cond (condition, statement); | |
7369 | ||
7370 | /* Parse the then-clause. */ | |
4606b05c MLI |
7371 | in_statement = parser->in_statement; |
7372 | parser->in_statement |= IN_IF_STMT; | |
626c34b5 PB |
7373 | if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)) |
7374 | { | |
7375 | location_t loc = cp_lexer_peek_token (parser->lexer)->location; | |
c2255bc4 | 7376 | add_stmt (build_empty_stmt (loc)); |
626c34b5 PB |
7377 | cp_lexer_consume_token (parser->lexer); |
7378 | if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE)) | |
7379 | warning_at (loc, OPT_Wempty_body, "suggest braces around " | |
7380 | "empty body in an %<if%> statement"); | |
2d66eeeb | 7381 | nested_if = false; |
626c34b5 PB |
7382 | } |
7383 | else | |
7384 | cp_parser_implicitly_scoped_statement (parser, &nested_if); | |
4606b05c MLI |
7385 | parser->in_statement = in_statement; |
7386 | ||
a723baf1 MM |
7387 | finish_then_clause (statement); |
7388 | ||
7389 | /* If the next token is `else', parse the else-clause. */ | |
7390 | if (cp_lexer_next_token_is_keyword (parser->lexer, | |
7391 | RID_ELSE)) | |
7392 | { | |
a723baf1 MM |
7393 | /* Consume the `else' keyword. */ |
7394 | cp_lexer_consume_token (parser->lexer); | |
325c3691 | 7395 | begin_else_clause (statement); |
a723baf1 | 7396 | /* Parse the else-clause. */ |
626c34b5 PB |
7397 | if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)) |
7398 | { | |
c2255bc4 AH |
7399 | location_t loc; |
7400 | loc = cp_lexer_peek_token (parser->lexer)->location; | |
7401 | warning_at (loc, | |
626c34b5 PB |
7402 | OPT_Wempty_body, "suggest braces around " |
7403 | "empty body in an %<else%> statement"); | |
c2255bc4 | 7404 | add_stmt (build_empty_stmt (loc)); |
626c34b5 PB |
7405 | cp_lexer_consume_token (parser->lexer); |
7406 | } | |
7407 | else | |
7408 | cp_parser_implicitly_scoped_statement (parser, NULL); | |
7409 | ||
a723baf1 | 7410 | finish_else_clause (statement); |
2a67bec2 ILT |
7411 | |
7412 | /* If we are currently parsing a then-clause, then | |
7413 | IF_P will not be NULL. We set it to true to | |
7414 | indicate that this if statement has an else clause. | |
7415 | This may trigger the Wparentheses warning below | |
7416 | when we get back up to the parent if statement. */ | |
7417 | if (if_p != NULL) | |
7418 | *if_p = true; | |
7419 | } | |
7420 | else | |
7421 | { | |
7422 | /* This if statement does not have an else clause. If | |
7423 | NESTED_IF is true, then the then-clause is an if | |
7424 | statement which does have an else clause. We warn | |
7425 | about the potential ambiguity. */ | |
7426 | if (nested_if) | |
69bc6bff MLI |
7427 | warning_at (EXPR_LOCATION (statement), OPT_Wparentheses, |
7428 | "suggest explicit braces to avoid ambiguous" | |
7429 | " %<else%>"); | |
a723baf1 MM |
7430 | } |
7431 | ||
7432 | /* Now we're all done with the if-statement. */ | |
325c3691 | 7433 | finish_if_stmt (statement); |
a723baf1 MM |
7434 | } |
7435 | else | |
7436 | { | |
0e59b3fb | 7437 | bool in_switch_statement_p; |
1799e5d5 | 7438 | unsigned char in_statement; |
a723baf1 MM |
7439 | |
7440 | /* Add the condition. */ | |
7441 | finish_switch_cond (condition, statement); | |
7442 | ||
7443 | /* Parse the body of the switch-statement. */ | |
0e59b3fb | 7444 | in_switch_statement_p = parser->in_switch_statement_p; |
1799e5d5 | 7445 | in_statement = parser->in_statement; |
0e59b3fb | 7446 | parser->in_switch_statement_p = true; |
1799e5d5 | 7447 | parser->in_statement |= IN_SWITCH_STMT; |
2a67bec2 | 7448 | cp_parser_implicitly_scoped_statement (parser, NULL); |
0e59b3fb | 7449 | parser->in_switch_statement_p = in_switch_statement_p; |
1799e5d5 | 7450 | parser->in_statement = in_statement; |
a723baf1 MM |
7451 | |
7452 | /* Now we're all done with the switch-statement. */ | |
7453 | finish_switch_stmt (statement); | |
7454 | } | |
7455 | ||
7456 | return statement; | |
7457 | } | |
7458 | break; | |
7459 | ||
7460 | default: | |
7461 | cp_parser_error (parser, "expected selection-statement"); | |
7462 | return error_mark_node; | |
7463 | } | |
7464 | } | |
7465 | ||
21526606 | 7466 | /* Parse a condition. |
a723baf1 MM |
7467 | |
7468 | condition: | |
7469 | expression | |
09357846 JM |
7470 | type-specifier-seq declarator = initializer-clause |
7471 | type-specifier-seq declarator braced-init-list | |
a723baf1 MM |
7472 | |
7473 | GNU Extension: | |
21526606 | 7474 | |
a723baf1 | 7475 | condition: |
21526606 | 7476 | type-specifier-seq declarator asm-specification [opt] |
a723baf1 | 7477 | attributes [opt] = assignment-expression |
21526606 | 7478 | |
a723baf1 MM |
7479 | Returns the expression that should be tested. */ |
7480 | ||
7481 | static tree | |
94edc4ab | 7482 | cp_parser_condition (cp_parser* parser) |
a723baf1 | 7483 | { |
62d1db17 | 7484 | cp_decl_specifier_seq type_specifiers; |
a723baf1 MM |
7485 | const char *saved_message; |
7486 | ||
7487 | /* Try the declaration first. */ | |
7488 | cp_parser_parse_tentatively (parser); | |
7489 | /* New types are not allowed in the type-specifier-seq for a | |
7490 | condition. */ | |
7491 | saved_message = parser->type_definition_forbidden_message; | |
7492 | parser->type_definition_forbidden_message | |
7493 | = "types may not be defined in conditions"; | |
7494 | /* Parse the type-specifier-seq. */ | |
d4113656 MM |
7495 | cp_parser_type_specifier_seq (parser, /*is_condition==*/true, |
7496 | &type_specifiers); | |
a723baf1 MM |
7497 | /* Restore the saved message. */ |
7498 | parser->type_definition_forbidden_message = saved_message; | |
7499 | /* If all is well, we might be looking at a declaration. */ | |
7500 | if (!cp_parser_error_occurred (parser)) | |
7501 | { | |
7502 | tree decl; | |
7503 | tree asm_specification; | |
7504 | tree attributes; | |
058b15c1 | 7505 | cp_declarator *declarator; |
a723baf1 | 7506 | tree initializer = NULL_TREE; |
21526606 | 7507 | |
a723baf1 | 7508 | /* Parse the declarator. */ |
62b8a44e | 7509 | declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED, |
4bb8ca28 | 7510 | /*ctor_dtor_or_conv_p=*/NULL, |
db86dd14 MM |
7511 | /*parenthesized_p=*/NULL, |
7512 | /*member_p=*/false); | |
a723baf1 MM |
7513 | /* Parse the attributes. */ |
7514 | attributes = cp_parser_attributes_opt (parser); | |
7515 | /* Parse the asm-specification. */ | |
7516 | asm_specification = cp_parser_asm_specification_opt (parser); | |
09357846 | 7517 | /* If the next token is not an `=' or '{', then we might still be |
a723baf1 | 7518 | looking at an expression. For example: |
21526606 | 7519 | |
a723baf1 | 7520 | if (A(a).x) |
21526606 | 7521 | |
a723baf1 MM |
7522 | looks like a decl-specifier-seq and a declarator -- but then |
7523 | there is no `=', so this is an expression. */ | |
09357846 JM |
7524 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ) |
7525 | && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)) | |
7526 | cp_parser_simulate_error (parser); | |
7527 | ||
7528 | /* If we did see an `=' or '{', then we are looking at a declaration | |
a723baf1 MM |
7529 | for sure. */ |
7530 | if (cp_parser_parse_definitely (parser)) | |
7531 | { | |
c8094d83 | 7532 | tree pushed_scope; |
d174af6c | 7533 | bool non_constant_p; |
09357846 | 7534 | bool flags = LOOKUP_ONLYCONVERTING; |
73a8adb6 | 7535 | |
a723baf1 | 7536 | /* Create the declaration. */ |
62d1db17 | 7537 | decl = start_decl (declarator, &type_specifiers, |
a723baf1 | 7538 | /*initialized_p=*/true, |
73a8adb6 | 7539 | attributes, /*prefix_attributes=*/NULL_TREE, |
4514aa8c | 7540 | &pushed_scope); |
09357846 JM |
7541 | |
7542 | /* Parse the initializer. */ | |
7543 | if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)) | |
7544 | { | |
7545 | initializer = cp_parser_braced_list (parser, &non_constant_p); | |
7546 | CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1; | |
7547 | flags = 0; | |
7548 | } | |
7549 | else | |
7550 | { | |
7551 | /* Consume the `='. */ | |
049dba02 | 7552 | cp_parser_require (parser, CPP_EQ, "%<=%>"); |
09357846 JM |
7553 | initializer = cp_parser_initializer_clause (parser, &non_constant_p); |
7554 | } | |
7555 | if (BRACE_ENCLOSED_INITIALIZER_P (initializer)) | |
7556 | maybe_warn_cpp0x ("extended initializer lists"); | |
7557 | ||
d174af6c MM |
7558 | if (!non_constant_p) |
7559 | initializer = fold_non_dependent_expr (initializer); | |
21526606 | 7560 | |
a723baf1 | 7561 | /* Process the initializer. */ |
21526606 | 7562 | cp_finish_decl (decl, |
3db45ab5 | 7563 | initializer, !non_constant_p, |
21526606 | 7564 | asm_specification, |
09357846 | 7565 | flags); |
c162c75e | 7566 | |
4514aa8c NS |
7567 | if (pushed_scope) |
7568 | pop_scope (pushed_scope); | |
21526606 | 7569 | |
a723baf1 MM |
7570 | return convert_from_reference (decl); |
7571 | } | |
7572 | } | |
7573 | /* If we didn't even get past the declarator successfully, we are | |
7574 | definitely not looking at a declaration. */ | |
7575 | else | |
7576 | cp_parser_abort_tentative_parse (parser); | |
7577 | ||
7578 | /* Otherwise, we are looking at an expression. */ | |
cee9035d | 7579 | return cp_parser_expression (parser, /*cast_p=*/false, NULL); |
a723baf1 MM |
7580 | } |
7581 | ||
7582 | /* Parse an iteration-statement. | |
7583 | ||
7584 | iteration-statement: | |
7585 | while ( condition ) statement | |
7586 | do statement while ( expression ) ; | |
7587 | for ( for-init-statement condition [opt] ; expression [opt] ) | |
7588 | statement | |
7589 | ||
7590 | Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */ | |
7591 | ||
7592 | static tree | |
94edc4ab | 7593 | cp_parser_iteration_statement (cp_parser* parser) |
a723baf1 MM |
7594 | { |
7595 | cp_token *token; | |
7596 | enum rid keyword; | |
7597 | tree statement; | |
1799e5d5 | 7598 | unsigned char in_statement; |
a723baf1 MM |
7599 | |
7600 | /* Peek at the next token. */ | |
7601 | token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement"); | |
7602 | if (!token) | |
7603 | return error_mark_node; | |
7604 | ||
0e59b3fb | 7605 | /* Remember whether or not we are already within an iteration |
21526606 | 7606 | statement. */ |
1799e5d5 | 7607 | in_statement = parser->in_statement; |
0e59b3fb | 7608 | |
a723baf1 MM |
7609 | /* See what kind of keyword it is. */ |
7610 | keyword = token->keyword; | |
7611 | switch (keyword) | |
7612 | { | |
7613 | case RID_WHILE: | |
7614 | { | |
7615 | tree condition; | |
7616 | ||
7617 | /* Begin the while-statement. */ | |
7618 | statement = begin_while_stmt (); | |
7619 | /* Look for the `('. */ | |
3a483371 | 7620 | cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"); |
a723baf1 MM |
7621 | /* Parse the condition. */ |
7622 | condition = cp_parser_condition (parser); | |
7623 | finish_while_stmt_cond (condition, statement); | |
7624 | /* Look for the `)'. */ | |
3a483371 | 7625 | cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"); |
a723baf1 | 7626 | /* Parse the dependent statement. */ |
1799e5d5 | 7627 | parser->in_statement = IN_ITERATION_STMT; |
a723baf1 | 7628 | cp_parser_already_scoped_statement (parser); |
1799e5d5 | 7629 | parser->in_statement = in_statement; |
a723baf1 MM |
7630 | /* We're done with the while-statement. */ |
7631 | finish_while_stmt (statement); | |
7632 | } | |
7633 | break; | |
7634 | ||
7635 | case RID_DO: | |
7636 | { | |
7637 | tree expression; | |
7638 | ||
7639 | /* Begin the do-statement. */ | |
7640 | statement = begin_do_stmt (); | |
7641 | /* Parse the body of the do-statement. */ | |
1799e5d5 | 7642 | parser->in_statement = IN_ITERATION_STMT; |
2a67bec2 | 7643 | cp_parser_implicitly_scoped_statement (parser, NULL); |
1799e5d5 | 7644 | parser->in_statement = in_statement; |
a723baf1 MM |
7645 | finish_do_body (statement); |
7646 | /* Look for the `while' keyword. */ | |
3a483371 | 7647 | cp_parser_require_keyword (parser, RID_WHILE, "%<while%>"); |
a723baf1 | 7648 | /* Look for the `('. */ |
3a483371 | 7649 | cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"); |
a723baf1 | 7650 | /* Parse the expression. */ |
cee9035d | 7651 | expression = cp_parser_expression (parser, /*cast_p=*/false, NULL); |
a723baf1 MM |
7652 | /* We're done with the do-statement. */ |
7653 | finish_do_stmt (expression, statement); | |
7654 | /* Look for the `)'. */ | |
3a483371 | 7655 | cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"); |
a723baf1 | 7656 | /* Look for the `;'. */ |
3a483371 | 7657 | cp_parser_require (parser, CPP_SEMICOLON, "%<;%>"); |
a723baf1 MM |
7658 | } |
7659 | break; | |
7660 | ||
7661 | case RID_FOR: | |
7662 | { | |
7663 | tree condition = NULL_TREE; | |
7664 | tree expression = NULL_TREE; | |
7665 | ||
7666 | /* Begin the for-statement. */ | |
7667 | statement = begin_for_stmt (); | |
7668 | /* Look for the `('. */ | |
3a483371 | 7669 | cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"); |
a723baf1 MM |
7670 | /* Parse the initialization. */ |
7671 | cp_parser_for_init_statement (parser); | |
7672 | finish_for_init_stmt (statement); | |
7673 | ||
7674 | /* If there's a condition, process it. */ | |
7675 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)) | |
7676 | condition = cp_parser_condition (parser); | |
7677 | finish_for_cond (condition, statement); | |
7678 | /* Look for the `;'. */ | |
3a483371 | 7679 | cp_parser_require (parser, CPP_SEMICOLON, "%<;%>"); |
a723baf1 MM |
7680 | |
7681 | /* If there's an expression, process it. */ | |
7682 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)) | |
cee9035d | 7683 | expression = cp_parser_expression (parser, /*cast_p=*/false, NULL); |
a723baf1 MM |
7684 | finish_for_expr (expression, statement); |
7685 | /* Look for the `)'. */ | |
3a483371 | 7686 | cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"); |
98ca843c | 7687 | |
a723baf1 | 7688 | /* Parse the body of the for-statement. */ |
1799e5d5 | 7689 | parser->in_statement = IN_ITERATION_STMT; |
a723baf1 | 7690 | cp_parser_already_scoped_statement (parser); |
1799e5d5 | 7691 | parser->in_statement = in_statement; |
a723baf1 MM |
7692 | |
7693 | /* We're done with the for-statement. */ | |
7694 | finish_for_stmt (statement); | |
7695 | } | |
7696 | break; | |
7697 | ||
7698 | default: | |
7699 | cp_parser_error (parser, "expected iteration-statement"); | |
7700 | statement = error_mark_node; | |
7701 | break; | |
7702 | } | |
7703 | ||
7704 | return statement; | |
7705 | } | |
7706 | ||
7707 | /* Parse a for-init-statement. | |
7708 | ||
7709 | for-init-statement: | |
7710 | expression-statement | |
7711 | simple-declaration */ | |
7712 | ||
7713 | static void | |
94edc4ab | 7714 | cp_parser_for_init_statement (cp_parser* parser) |
a723baf1 MM |
7715 | { |
7716 | /* If the next token is a `;', then we have an empty | |
34cd5ae7 | 7717 | expression-statement. Grammatically, this is also a |
a723baf1 MM |
7718 | simple-declaration, but an invalid one, because it does not |
7719 | declare anything. Therefore, if we did not handle this case | |
7720 | specially, we would issue an error message about an invalid | |
7721 | declaration. */ | |
7722 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)) | |
7723 | { | |
7724 | /* We're going to speculatively look for a declaration, falling back | |
7725 | to an expression, if necessary. */ | |
7726 | cp_parser_parse_tentatively (parser); | |
7727 | /* Parse the declaration. */ | |
7728 | cp_parser_simple_declaration (parser, | |
7729 | /*function_definition_allowed_p=*/false); | |
7730 | /* If the tentative parse failed, then we shall need to look for an | |
7731 | expression-statement. */ | |
7732 | if (cp_parser_parse_definitely (parser)) | |
7733 | return; | |
7734 | } | |
7735 | ||
a5bcc582 | 7736 | cp_parser_expression_statement (parser, false); |
a723baf1 MM |
7737 | } |
7738 | ||
7739 | /* Parse a jump-statement. | |
7740 | ||
7741 | jump-statement: | |
7742 | break ; | |
7743 | continue ; | |
7744 | return expression [opt] ; | |
09357846 | 7745 | return braced-init-list ; |
21526606 | 7746 | goto identifier ; |
a723baf1 MM |
7747 | |
7748 | GNU extension: | |
7749 | ||
7750 | jump-statement: | |
7751 | goto * expression ; | |
7752 | ||
5088b058 | 7753 | Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */ |
a723baf1 MM |
7754 | |
7755 | static tree | |
94edc4ab | 7756 | cp_parser_jump_statement (cp_parser* parser) |
a723baf1 MM |
7757 | { |
7758 | tree statement = error_mark_node; | |
7759 | cp_token *token; | |
7760 | enum rid keyword; | |
4606b05c | 7761 | unsigned char in_statement; |
a723baf1 MM |
7762 | |
7763 | /* Peek at the next token. */ | |
7764 | token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement"); | |
7765 | if (!token) | |
7766 | return error_mark_node; | |
7767 | ||
7768 | /* See what kind of keyword it is. */ | |
7769 | keyword = token->keyword; | |
7770 | switch (keyword) | |
7771 | { | |
7772 | case RID_BREAK: | |
4606b05c MLI |
7773 | in_statement = parser->in_statement & ~IN_IF_STMT; |
7774 | switch (in_statement) | |
0e59b3fb | 7775 | { |
1799e5d5 | 7776 | case 0: |
69bc6bff | 7777 | error_at (token->location, "break statement not within loop or switch"); |
1799e5d5 RH |
7778 | break; |
7779 | default: | |
4606b05c MLI |
7780 | gcc_assert ((in_statement & IN_SWITCH_STMT) |
7781 | || in_statement == IN_ITERATION_STMT); | |
1799e5d5 RH |
7782 | statement = finish_break_stmt (); |
7783 | break; | |
7784 | case IN_OMP_BLOCK: | |
69bc6bff | 7785 | error_at (token->location, "invalid exit from OpenMP structured block"); |
1799e5d5 RH |
7786 | break; |
7787 | case IN_OMP_FOR: | |
69bc6bff | 7788 | error_at (token->location, "break statement used with OpenMP for loop"); |
1799e5d5 | 7789 | break; |
0e59b3fb | 7790 | } |
2a13a625 | 7791 | cp_parser_require (parser, CPP_SEMICOLON, "%<;%>"); |
a723baf1 MM |
7792 | break; |
7793 | ||
7794 | case RID_CONTINUE: | |
4606b05c | 7795 | switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT)) |
0e59b3fb | 7796 | { |
1799e5d5 | 7797 | case 0: |
69bc6bff | 7798 | error_at (token->location, "continue statement not within a loop"); |
1799e5d5 RH |
7799 | break; |
7800 | case IN_ITERATION_STMT: | |
7801 | case IN_OMP_FOR: | |
7802 | statement = finish_continue_stmt (); | |
7803 | break; | |
7804 | case IN_OMP_BLOCK: | |
69bc6bff | 7805 | error_at (token->location, "invalid exit from OpenMP structured block"); |
1799e5d5 RH |
7806 | break; |
7807 | default: | |
7808 | gcc_unreachable (); | |
0e59b3fb | 7809 | } |
2a13a625 | 7810 | cp_parser_require (parser, CPP_SEMICOLON, "%<;%>"); |
a723baf1 MM |
7811 | break; |
7812 | ||
7813 | case RID_RETURN: | |
7814 | { | |
7815 | tree expr; | |
09357846 | 7816 | bool expr_non_constant_p; |
a723baf1 | 7817 | |
09357846 JM |
7818 | if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)) |
7819 | { | |
7820 | maybe_warn_cpp0x ("extended initializer lists"); | |
7821 | expr = cp_parser_braced_list (parser, &expr_non_constant_p); | |
7822 | } | |
7823 | else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)) | |
cee9035d | 7824 | expr = cp_parser_expression (parser, /*cast_p=*/false, NULL); |
a723baf1 | 7825 | else |
09357846 JM |
7826 | /* If the next token is a `;', then there is no |
7827 | expression. */ | |
a723baf1 MM |
7828 | expr = NULL_TREE; |
7829 | /* Build the return-statement. */ | |
7830 | statement = finish_return_stmt (expr); | |
7831 | /* Look for the final `;'. */ | |
2a13a625 | 7832 | cp_parser_require (parser, CPP_SEMICOLON, "%<;%>"); |
a723baf1 MM |
7833 | } |
7834 | break; | |
7835 | ||
7836 | case RID_GOTO: | |
7837 | /* Create the goto-statement. */ | |
7838 | if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)) | |
7839 | { | |
7840 | /* Issue a warning about this use of a GNU extension. */ | |
509c9d60 | 7841 | pedwarn (token->location, OPT_pedantic, "ISO C++ forbids computed gotos"); |
a723baf1 MM |
7842 | /* Consume the '*' token. */ |
7843 | cp_lexer_consume_token (parser->lexer); | |
7844 | /* Parse the dependent expression. */ | |
cee9035d | 7845 | finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false, NULL)); |
a723baf1 MM |
7846 | } |
7847 | else | |
7848 | finish_goto_stmt (cp_parser_identifier (parser)); | |
7849 | /* Look for the final `;'. */ | |
2a13a625 | 7850 | cp_parser_require (parser, CPP_SEMICOLON, "%<;%>"); |
a723baf1 MM |
7851 | break; |
7852 | ||
7853 | default: | |
7854 | cp_parser_error (parser, "expected jump-statement"); | |
7855 | break; | |
7856 | } | |
7857 | ||
7858 | return statement; | |
7859 | } | |
7860 | ||
7861 | /* Parse a declaration-statement. | |
7862 | ||
7863 | declaration-statement: | |
7864 | block-declaration */ | |
7865 | ||
7866 | static void | |
94edc4ab | 7867 | cp_parser_declaration_statement (cp_parser* parser) |
a723baf1 | 7868 | { |
058b15c1 MM |
7869 | void *p; |
7870 | ||
7871 | /* Get the high-water mark for the DECLARATOR_OBSTACK. */ | |
7872 | p = obstack_alloc (&declarator_obstack, 0); | |
7873 | ||
7874 | /* Parse the block-declaration. */ | |
a723baf1 MM |
7875 | cp_parser_block_declaration (parser, /*statement_p=*/true); |
7876 | ||
058b15c1 MM |
7877 | /* Free any declarators allocated. */ |
7878 | obstack_free (&declarator_obstack, p); | |
7879 | ||
a723baf1 MM |
7880 | /* Finish off the statement. */ |
7881 | finish_stmt (); | |
7882 | } | |
7883 | ||
7884 | /* Some dependent statements (like `if (cond) statement'), are | |
7885 | implicitly in their own scope. In other words, if the statement is | |
7886 | a single statement (as opposed to a compound-statement), it is | |
7887 | none-the-less treated as if it were enclosed in braces. Any | |
7888 | declarations appearing in the dependent statement are out of scope | |
7889 | after control passes that point. This function parses a statement, | |
7890 | but ensures that is in its own scope, even if it is not a | |
21526606 | 7891 | compound-statement. |
a723baf1 | 7892 | |
2a67bec2 ILT |
7893 | If IF_P is not NULL, *IF_P is set to indicate whether the statement |
7894 | is a (possibly labeled) if statement which is not enclosed in | |
7895 | braces and has an else clause. This is used to implement | |
7896 | -Wparentheses. | |
7897 | ||
a723baf1 MM |
7898 | Returns the new statement. */ |
7899 | ||
7900 | static tree | |
2a67bec2 | 7901 | cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p) |
a723baf1 MM |
7902 | { |
7903 | tree statement; | |
7904 | ||
2a67bec2 ILT |
7905 | if (if_p != NULL) |
7906 | *if_p = false; | |
7907 | ||
74ac79fa DM |
7908 | /* Mark if () ; with a special NOP_EXPR. */ |
7909 | if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)) | |
7910 | { | |
c2255bc4 | 7911 | location_t loc = cp_lexer_peek_token (parser->lexer)->location; |
74ac79fa | 7912 | cp_lexer_consume_token (parser->lexer); |
c2255bc4 | 7913 | statement = add_stmt (build_empty_stmt (loc)); |
74ac79fa DM |
7914 | } |
7915 | /* if a compound is opened, we simply parse the statement directly. */ | |
7916 | else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)) | |
7917 | statement = cp_parser_compound_statement (parser, NULL, false); | |
a723baf1 | 7918 | /* If the token is not a `{', then we must take special action. */ |
74ac79fa | 7919 | else |
a723baf1 MM |
7920 | { |
7921 | /* Create a compound-statement. */ | |
325c3691 | 7922 | statement = begin_compound_stmt (0); |
a723baf1 | 7923 | /* Parse the dependent-statement. */ |
2a67bec2 | 7924 | cp_parser_statement (parser, NULL_TREE, false, if_p); |
a723baf1 | 7925 | /* Finish the dummy compound-statement. */ |
7a3397c7 | 7926 | finish_compound_stmt (statement); |
a723baf1 | 7927 | } |
a723baf1 MM |
7928 | |
7929 | /* Return the statement. */ | |
7930 | return statement; | |
7931 | } | |
7932 | ||
7933 | /* For some dependent statements (like `while (cond) statement'), we | |
7934 | have already created a scope. Therefore, even if the dependent | |
7935 | statement is a compound-statement, we do not want to create another | |
7936 | scope. */ | |
7937 | ||
7938 | static void | |
94edc4ab | 7939 | cp_parser_already_scoped_statement (cp_parser* parser) |
a723baf1 | 7940 | { |
325c3691 RH |
7941 | /* If the token is a `{', then we must take special action. */ |
7942 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)) | |
2a67bec2 | 7943 | cp_parser_statement (parser, NULL_TREE, false, NULL); |
325c3691 | 7944 | else |
a723baf1 | 7945 | { |
325c3691 RH |
7946 | /* Avoid calling cp_parser_compound_statement, so that we |
7947 | don't create a new scope. Do everything else by hand. */ | |
3a483371 | 7948 | cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"); |
ac9bc18b JJ |
7949 | /* If the next keyword is `__label__' we have a label declaration. */ |
7950 | while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL)) | |
7951 | cp_parser_label_declaration (parser); | |
7952 | /* Parse an (optional) statement-seq. */ | |
bc4071dd | 7953 | cp_parser_statement_seq_opt (parser, NULL_TREE); |
3a483371 | 7954 | cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>"); |
a723baf1 | 7955 | } |
a723baf1 MM |
7956 | } |
7957 | ||
7958 | /* Declarations [gram.dcl.dcl] */ | |
7959 | ||
7960 | /* Parse an optional declaration-sequence. | |
7961 | ||
7962 | declaration-seq: | |
7963 | declaration | |
7964 | declaration-seq declaration */ | |
7965 | ||
7966 | static void | |
94edc4ab | 7967 | cp_parser_declaration_seq_opt (cp_parser* parser) |
a723baf1 MM |
7968 | { |
7969 | while (true) | |
7970 | { | |
7971 | cp_token *token; | |
7972 | ||
7973 | token = cp_lexer_peek_token (parser->lexer); | |
7974 | ||
7975 | if (token->type == CPP_CLOSE_BRACE | |
bc4071dd RH |
7976 | || token->type == CPP_EOF |
7977 | || token->type == CPP_PRAGMA_EOL) | |
a723baf1 MM |
7978 | break; |
7979 | ||
21526606 | 7980 | if (token->type == CPP_SEMICOLON) |
a723baf1 MM |
7981 | { |
7982 | /* A declaration consisting of a single semicolon is | |
7983 | invalid. Allow it unless we're being pedantic. */ | |
a723baf1 | 7984 | cp_lexer_consume_token (parser->lexer); |
fcf73884 | 7985 | if (!in_system_header) |
509c9d60 | 7986 | pedwarn (input_location, OPT_pedantic, "extra %<;%>"); |
a723baf1 MM |
7987 | continue; |
7988 | } | |
7989 | ||
7d381002 | 7990 | /* If we're entering or exiting a region that's implicitly |
03fd3f84 | 7991 | extern "C", modify the lang context appropriately. */ |
7d381002 MA |
7992 | if (!parser->implicit_extern_c && token->implicit_extern_c) |
7993 | { | |
7994 | push_lang_context (lang_name_c); | |
7995 | parser->implicit_extern_c = true; | |
7996 | } | |
7997 | else if (parser->implicit_extern_c && !token->implicit_extern_c) | |
7998 | { | |
7999 | pop_lang_context (); | |
8000 | parser->implicit_extern_c = false; | |
8001 | } | |
8002 | ||
36952dea ZW |
8003 | if (token->type == CPP_PRAGMA) |
8004 | { | |
8005 | /* A top-level declaration can consist solely of a #pragma. | |
8006 | A nested declaration cannot, so this is done here and not | |
8007 | in cp_parser_declaration. (A #pragma at block scope is | |
8008 | handled in cp_parser_statement.) */ | |
bc4071dd | 8009 | cp_parser_pragma (parser, pragma_external); |
36952dea ZW |
8010 | continue; |
8011 | } | |
8012 | ||
c838d82f | 8013 | /* Parse the declaration itself. */ |
a723baf1 MM |
8014 | cp_parser_declaration (parser); |
8015 | } | |
8016 | } | |
8017 | ||
8018 | /* Parse a declaration. | |
8019 | ||
8020 | declaration: | |
8021 | block-declaration | |
8022 | function-definition | |
8023 | template-declaration | |
8024 | explicit-instantiation | |
8025 | explicit-specialization | |
8026 | linkage-specification | |
21526606 | 8027 | namespace-definition |
1092805d MM |
8028 | |
8029 | GNU extension: | |
8030 | ||
8031 | declaration: | |
8032 | __extension__ declaration */ | |
a723baf1 MM |
8033 | |
8034 | static void | |
94edc4ab | 8035 | cp_parser_declaration (cp_parser* parser) |
a723baf1 MM |
8036 | { |
8037 | cp_token token1; | |
8038 | cp_token token2; | |
1092805d | 8039 | int saved_pedantic; |
058b15c1 | 8040 | void *p; |
1092805d MM |
8041 | |
8042 | /* Check for the `__extension__' keyword. */ | |
8043 | if (cp_parser_extension_opt (parser, &saved_pedantic)) | |
8044 | { | |
8045 | /* Parse the qualified declaration. */ | |
8046 | cp_parser_declaration (parser); | |
8047 | /* Restore the PEDANTIC flag. */ | |
8048 | pedantic = saved_pedantic; | |
8049 | ||
8050 | return; | |
8051 | } | |
a723baf1 MM |
8052 | |
8053 | /* Try to figure out what kind of declaration is present. */ | |
8054 | token1 = *cp_lexer_peek_token (parser->lexer); | |
21526606 | 8055 | |
a723baf1 MM |
8056 | if (token1.type != CPP_EOF) |
8057 | token2 = *cp_lexer_peek_nth_token (parser->lexer, 2); | |
32cafd73 | 8058 | else |
728cdd08 GDR |
8059 | { |
8060 | token2.type = CPP_EOF; | |
8061 | token2.keyword = RID_MAX; | |
8062 | } | |
a723baf1 | 8063 | |
058b15c1 MM |
8064 | /* Get the high-water mark for the DECLARATOR_OBSTACK. */ |
8065 | p = obstack_alloc (&declarator_obstack, 0); | |
8066 | ||
a723baf1 MM |
8067 | /* If the next token is `extern' and the following token is a string |
8068 | literal, then we have a linkage specification. */ | |
8069 | if (token1.keyword == RID_EXTERN | |
8070 | && cp_parser_is_string_literal (&token2)) | |
8071 | cp_parser_linkage_specification (parser); | |
8072 | /* If the next token is `template', then we have either a template | |
8073 | declaration, an explicit instantiation, or an explicit | |
8074 | specialization. */ | |
8075 | else if (token1.keyword == RID_TEMPLATE) | |
8076 | { | |
8077 | /* `template <>' indicates a template specialization. */ | |
8078 | if (token2.type == CPP_LESS | |
8079 | && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER) | |
8080 | cp_parser_explicit_specialization (parser); | |
8081 | /* `template <' indicates a template declaration. */ | |
8082 | else if (token2.type == CPP_LESS) | |
8083 | cp_parser_template_declaration (parser, /*member_p=*/false); | |
8084 | /* Anything else must be an explicit instantiation. */ | |
8085 | else | |
8086 | cp_parser_explicit_instantiation (parser); | |
8087 | } | |
8088 | /* If the next token is `export', then we have a template | |
8089 | declaration. */ | |
8090 | else if (token1.keyword == RID_EXPORT) | |
8091 | cp_parser_template_declaration (parser, /*member_p=*/false); | |
8092 | /* If the next token is `extern', 'static' or 'inline' and the one | |
8093 | after that is `template', we have a GNU extended explicit | |
8094 | instantiation directive. */ | |
8095 | else if (cp_parser_allow_gnu_extensions_p (parser) | |
8096 | && (token1.keyword == RID_EXTERN | |
8097 | || token1.keyword == RID_STATIC | |
8098 | || token1.keyword == RID_INLINE) | |
8099 | && token2.keyword == RID_TEMPLATE) | |
8100 | cp_parser_explicit_instantiation (parser); | |
8101 | /* If the next token is `namespace', check for a named or unnamed | |
8102 | namespace definition. */ | |
8103 | else if (token1.keyword == RID_NAMESPACE | |
8104 | && (/* A named namespace definition. */ | |
8105 | (token2.type == CPP_NAME | |
21526606 | 8106 | && (cp_lexer_peek_nth_token (parser->lexer, 3)->type |
0ed5edac | 8107 | != CPP_EQ)) |
a723baf1 | 8108 | /* An unnamed namespace definition. */ |
aa09f986 JM |
8109 | || token2.type == CPP_OPEN_BRACE |
8110 | || token2.keyword == RID_ATTRIBUTE)) | |
a723baf1 | 8111 | cp_parser_namespace_definition (parser); |
4cfaec1c JM |
8112 | /* An inline (associated) namespace definition. */ |
8113 | else if (token1.keyword == RID_INLINE | |
8114 | && token2.keyword == RID_NAMESPACE) | |
8115 | cp_parser_namespace_definition (parser); | |
e58a9aa1 ZL |
8116 | /* Objective-C++ declaration/definition. */ |
8117 | else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword)) | |
8118 | cp_parser_objc_declaration (parser); | |
a723baf1 MM |
8119 | /* We must have either a block declaration or a function |
8120 | definition. */ | |
8121 | else | |
8122 | /* Try to parse a block-declaration, or a function-definition. */ | |
8123 | cp_parser_block_declaration (parser, /*statement_p=*/false); | |
058b15c1 MM |
8124 | |
8125 | /* Free any declarators allocated. */ | |
8126 | obstack_free (&declarator_obstack, p); | |
a723baf1 MM |
8127 | } |
8128 | ||
21526606 | 8129 | /* Parse a block-declaration. |
a723baf1 MM |
8130 | |
8131 | block-declaration: | |
8132 | simple-declaration | |
8133 | asm-definition | |
8134 | namespace-alias-definition | |
8135 | using-declaration | |
21526606 | 8136 | using-directive |
a723baf1 MM |
8137 | |
8138 | GNU Extension: | |
8139 | ||
8140 | block-declaration: | |
21526606 | 8141 | __extension__ block-declaration |
a723baf1 | 8142 | |
55a3debe DG |
8143 | C++0x Extension: |
8144 | ||
8145 | block-declaration: | |
8146 | static_assert-declaration | |
8147 | ||
34cd5ae7 | 8148 | If STATEMENT_P is TRUE, then this block-declaration is occurring as |
a723baf1 MM |
8149 | part of a declaration-statement. */ |
8150 | ||
8151 | static void | |
21526606 | 8152 | cp_parser_block_declaration (cp_parser *parser, |
a723baf1 MM |
8153 | bool statement_p) |
8154 | { | |
8155 | cp_token *token1; | |
8156 | int saved_pedantic; | |
8157 | ||
8158 | /* Check for the `__extension__' keyword. */ | |
8159 | if (cp_parser_extension_opt (parser, &saved_pedantic)) | |
8160 | { | |
8161 | /* Parse the qualified declaration. */ | |
8162 | cp_parser_block_declaration (parser, statement_p); | |
8163 | /* Restore the PEDANTIC flag. */ | |
8164 | pedantic = saved_pedantic; | |
8165 | ||
8166 | return; | |
8167 | } | |
8168 | ||
8169 | /* Peek at the next token to figure out which kind of declaration is | |
8170 | present. */ | |
8171 | token1 = cp_lexer_peek_token (parser->lexer); | |
8172 | ||
8173 | /* If the next keyword is `asm', we have an asm-definition. */ | |
8174 | if (token1->keyword == RID_ASM) | |
8175 | { | |
8176 | if (statement_p) | |
8177 | cp_parser_commit_to_tentative_parse (parser); | |
8178 | cp_parser_asm_definition (parser); | |
8179 | } | |
8180 | /* If the next keyword is `namespace', we have a | |
8181 | namespace-alias-definition. */ | |
8182 | else if (token1->keyword == RID_NAMESPACE) | |
8183 | cp_parser_namespace_alias_definition (parser); | |
8184 | /* If the next keyword is `using', we have either a | |
8185 | using-declaration or a using-directive. */ | |
8186 | else if (token1->keyword == RID_USING) | |
8187 | { | |
8188 | cp_token *token2; | |
8189 | ||
8190 | if (statement_p) | |
8191 | cp_parser_commit_to_tentative_parse (parser); | |
8192 | /* If the token after `using' is `namespace', then we have a | |
8193 | using-directive. */ | |
8194 | token2 = cp_lexer_peek_nth_token (parser->lexer, 2); | |
8195 | if (token2->keyword == RID_NAMESPACE) | |
8196 | cp_parser_using_directive (parser); | |
8197 | /* Otherwise, it's a using-declaration. */ | |
8198 | else | |
981114e1 MM |
8199 | cp_parser_using_declaration (parser, |
8200 | /*access_declaration_p=*/false); | |
a723baf1 | 8201 | } |
3d3585eb JJ |
8202 | /* If the next keyword is `__label__' we have a misplaced label |
8203 | declaration. */ | |
a723baf1 MM |
8204 | else if (token1->keyword == RID_LABEL) |
8205 | { | |
3d3585eb | 8206 | cp_lexer_consume_token (parser->lexer); |
69bc6bff | 8207 | error_at (token1->location, "%<__label__%> not at the beginning of a block"); |
3d3585eb JJ |
8208 | cp_parser_skip_to_end_of_statement (parser); |
8209 | /* If the next token is now a `;', consume it. */ | |
8210 | if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)) | |
8211 | cp_lexer_consume_token (parser->lexer); | |
a723baf1 | 8212 | } |
55a3debe DG |
8213 | /* If the next token is `static_assert' we have a static assertion. */ |
8214 | else if (token1->keyword == RID_STATIC_ASSERT) | |
8215 | cp_parser_static_assert (parser, /*member_p=*/false); | |
a723baf1 MM |
8216 | /* Anything else must be a simple-declaration. */ |
8217 | else | |
8218 | cp_parser_simple_declaration (parser, !statement_p); | |
8219 | } | |
8220 | ||
8221 | /* Parse a simple-declaration. | |
8222 | ||
8223 | simple-declaration: | |
21526606 | 8224 | decl-specifier-seq [opt] init-declarator-list [opt] ; |
a723baf1 MM |
8225 | |
8226 | init-declarator-list: | |
8227 | init-declarator | |
21526606 | 8228 | init-declarator-list , init-declarator |
a723baf1 | 8229 | |
34cd5ae7 | 8230 | If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a |
9bcb9aae | 8231 | function-definition as a simple-declaration. */ |
a723baf1 MM |
8232 | |
8233 | static void | |
21526606 | 8234 | cp_parser_simple_declaration (cp_parser* parser, |
0cbd7506 | 8235 | bool function_definition_allowed_p) |
a723baf1 | 8236 | { |
62d1db17 | 8237 | cp_decl_specifier_seq decl_specifiers; |
560ad596 | 8238 | int declares_class_or_enum; |
a723baf1 MM |
8239 | bool saw_declarator; |
8240 | ||
8241 | /* Defer access checks until we know what is being declared; the | |
8242 | checks for names appearing in the decl-specifier-seq should be | |
8243 | done as if we were in the scope of the thing being declared. */ | |
8d241e0b | 8244 | push_deferring_access_checks (dk_deferred); |
cf22909c | 8245 | |
a723baf1 MM |
8246 | /* Parse the decl-specifier-seq. We have to keep track of whether |
8247 | or not the decl-specifier-seq declares a named class or | |
8248 | enumeration type, since that is the only case in which the | |
21526606 | 8249 | init-declarator-list is allowed to be empty. |
a723baf1 MM |
8250 | |
8251 | [dcl.dcl] | |
8252 | ||
8253 | In a simple-declaration, the optional init-declarator-list can be | |
8254 | omitted only when declaring a class or enumeration, that is when | |
8255 | the decl-specifier-seq contains either a class-specifier, an | |
8256 | elaborated-type-specifier, or an enum-specifier. */ | |
62d1db17 MM |
8257 | cp_parser_decl_specifier_seq (parser, |
8258 | CP_PARSER_FLAGS_OPTIONAL, | |
8259 | &decl_specifiers, | |
8260 | &declares_class_or_enum); | |
a723baf1 | 8261 | /* We no longer need to defer access checks. */ |
cf22909c | 8262 | stop_deferring_access_checks (); |
24c0ef37 | 8263 | |
39703eb9 MM |
8264 | /* In a block scope, a valid declaration must always have a |
8265 | decl-specifier-seq. By not trying to parse declarators, we can | |
8266 | resolve the declaration/expression ambiguity more quickly. */ | |
98ca843c | 8267 | if (!function_definition_allowed_p |
62d1db17 | 8268 | && !decl_specifiers.any_specifiers_p) |
39703eb9 MM |
8269 | { |
8270 | cp_parser_error (parser, "expected declaration"); | |
8271 | goto done; | |
8272 | } | |
8273 | ||
8fbc5ae7 MM |
8274 | /* If the next two tokens are both identifiers, the code is |
8275 | erroneous. The usual cause of this situation is code like: | |
8276 | ||
8277 | T t; | |
8278 | ||
8279 | where "T" should name a type -- but does not. */ | |
de3fe73c MM |
8280 | if (!decl_specifiers.type |
8281 | && cp_parser_parse_and_diagnose_invalid_type_name (parser)) | |
8fbc5ae7 | 8282 | { |
8d241e0b | 8283 | /* If parsing tentatively, we should commit; we really are |
8fbc5ae7 MM |
8284 | looking at a declaration. */ |
8285 | cp_parser_commit_to_tentative_parse (parser); | |
8286 | /* Give up. */ | |
39703eb9 | 8287 | goto done; |
8fbc5ae7 | 8288 | } |
c8094d83 | 8289 | |
996c2b52 MM |
8290 | /* If we have seen at least one decl-specifier, and the next token |
8291 | is not a parenthesis, then we must be looking at a declaration. | |
8292 | (After "int (" we might be looking at a functional cast.) */ | |
c8094d83 | 8293 | if (decl_specifiers.any_specifiers_p |
09357846 | 8294 | && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN) |
192657ec JJ |
8295 | && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE) |
8296 | && !cp_parser_error_occurred (parser)) | |
996c2b52 | 8297 | cp_parser_commit_to_tentative_parse (parser); |
8fbc5ae7 | 8298 | |
a723baf1 MM |
8299 | /* Keep going until we hit the `;' at the end of the simple |
8300 | declaration. */ | |
8301 | saw_declarator = false; | |
21526606 | 8302 | while (cp_lexer_next_token_is_not (parser->lexer, |
a723baf1 MM |
8303 | CPP_SEMICOLON)) |
8304 | { | |
8305 | cp_token *token; | |
8306 | bool function_definition_p; | |
560ad596 | 8307 | tree decl; |
a723baf1 | 8308 | |
6d328225 PM |
8309 | if (saw_declarator) |
8310 | { | |
8311 | /* If we are processing next declarator, coma is expected */ | |
8312 | token = cp_lexer_peek_token (parser->lexer); | |
8313 | gcc_assert (token->type == CPP_COMMA); | |
8314 | cp_lexer_consume_token (parser->lexer); | |
8315 | } | |
8316 | else | |
8317 | saw_declarator = true; | |
8318 | ||
a723baf1 | 8319 | /* Parse the init-declarator. */ |
62d1db17 | 8320 | decl = cp_parser_init_declarator (parser, &decl_specifiers, |
d6b418fa | 8321 | /*checks=*/NULL, |
560ad596 MM |
8322 | function_definition_allowed_p, |
8323 | /*member_p=*/false, | |
8324 | declares_class_or_enum, | |
8325 | &function_definition_p); | |
1fb3244a MM |
8326 | /* If an error occurred while parsing tentatively, exit quickly. |
8327 | (That usually happens when in the body of a function; each | |
8328 | statement is treated as a declaration-statement until proven | |
8329 | otherwise.) */ | |
8330 | if (cp_parser_error_occurred (parser)) | |
39703eb9 | 8331 | goto done; |
a723baf1 MM |
8332 | /* Handle function definitions specially. */ |
8333 | if (function_definition_p) | |
8334 | { | |
8335 | /* If the next token is a `,', then we are probably | |
8336 | processing something like: | |
8337 | ||
8338 | void f() {}, *p; | |
8339 | ||
8340 | which is erroneous. */ | |
8341 | if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)) | |
2b7a3abf DS |
8342 | { |
8343 | cp_token *token = cp_lexer_peek_token (parser->lexer); | |
69bc6bff MLI |
8344 | error_at (token->location, |
8345 | "mixing" | |
8346 | " declarations and function-definitions is forbidden"); | |
2b7a3abf | 8347 | } |
a723baf1 MM |
8348 | /* Otherwise, we're done with the list of declarators. */ |
8349 | else | |
24c0ef37 | 8350 | { |
cf22909c | 8351 | pop_deferring_access_checks (); |
24c0ef37 GS |
8352 | return; |
8353 | } | |
a723baf1 MM |
8354 | } |
8355 | /* The next token should be either a `,' or a `;'. */ | |
8356 | token = cp_lexer_peek_token (parser->lexer); | |
8357 | /* If it's a `,', there are more declarators to come. */ | |
8358 | if (token->type == CPP_COMMA) | |
6d328225 | 8359 | /* will be consumed next time around */; |
a723baf1 MM |
8360 | /* If it's a `;', we are done. */ |
8361 | else if (token->type == CPP_SEMICOLON) | |
8362 | break; | |
8363 | /* Anything else is an error. */ | |
8364 | else | |
8365 | { | |
996c2b52 MM |
8366 | /* If we have already issued an error message we don't need |
8367 | to issue another one. */ | |
8368 | if (decl != error_mark_node | |
0b16f8f4 | 8369 | || cp_parser_uncommitted_to_tentative_parse_p (parser)) |
2a13a625 | 8370 | cp_parser_error (parser, "expected %<,%> or %<;%>"); |
a723baf1 MM |
8371 | /* Skip tokens until we reach the end of the statement. */ |
8372 | cp_parser_skip_to_end_of_statement (parser); | |
5a98fa7b MM |
8373 | /* If the next token is now a `;', consume it. */ |
8374 | if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)) | |
8375 | cp_lexer_consume_token (parser->lexer); | |
39703eb9 | 8376 | goto done; |
a723baf1 MM |
8377 | } |
8378 | /* After the first time around, a function-definition is not | |
8379 | allowed -- even if it was OK at first. For example: | |
8380 | ||
0cbd7506 | 8381 | int i, f() {} |
a723baf1 | 8382 | |
0cbd7506 | 8383 | is not valid. */ |
a723baf1 MM |
8384 | function_definition_allowed_p = false; |
8385 | } | |
8386 | ||
8387 | /* Issue an error message if no declarators are present, and the | |
8388 | decl-specifier-seq does not itself declare a class or | |
8389 | enumeration. */ | |
8390 | if (!saw_declarator) | |
8391 | { | |
8392 | if (cp_parser_declares_only_class_p (parser)) | |
62d1db17 | 8393 | shadow_tag (&decl_specifiers); |
a723baf1 | 8394 | /* Perform any deferred access checks. */ |
cf22909c | 8395 | perform_deferred_access_checks (); |
a723baf1 MM |
8396 | } |
8397 | ||
8398 | /* Consume the `;'. */ | |
3a483371 | 8399 | cp_parser_require (parser, CPP_SEMICOLON, "%<;%>"); |
a723baf1 | 8400 | |
39703eb9 MM |
8401 | done: |
8402 | pop_deferring_access_checks (); | |
a723baf1 MM |
8403 | } |
8404 | ||
8405 | /* Parse a decl-specifier-seq. | |
8406 | ||
8407 | decl-specifier-seq: | |
8408 | decl-specifier-seq [opt] decl-specifier | |
8409 | ||
8410 | decl-specifier: | |
8411 | storage-class-specifier | |
8412 | type-specifier | |
8413 | function-specifier | |
8414 | friend | |
21526606 | 8415 | typedef |
a723baf1 MM |
8416 | |
8417 | GNU Extension: | |
8418 | ||
15077df5 MM |
8419 | decl-specifier: |
8420 | attributes | |
a723baf1 | 8421 | |
62d1db17 | 8422 | Set *DECL_SPECS to a representation of the decl-specifier-seq. |
a723baf1 | 8423 | |
eb1aef53 | 8424 | The parser flags FLAGS is used to control type-specifier parsing. |
560ad596 MM |
8425 | |
8426 | *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following | |
543ca912 | 8427 | flags: |
560ad596 MM |
8428 | |
8429 | 1: one of the decl-specifiers is an elaborated-type-specifier | |
0cbd7506 | 8430 | (i.e., a type declaration) |
560ad596 | 8431 | 2: one of the decl-specifiers is an enum-specifier or a |
0cbd7506 | 8432 | class-specifier (i.e., a type definition) |
98ca843c | 8433 | |
560ad596 | 8434 | */ |
a723baf1 | 8435 | |
62d1db17 | 8436 | static void |
21526606 | 8437 | cp_parser_decl_specifier_seq (cp_parser* parser, |
62d1db17 MM |
8438 | cp_parser_flags flags, |
8439 | cp_decl_specifier_seq *decl_specs, | |
560ad596 | 8440 | int* declares_class_or_enum) |
a723baf1 | 8441 | { |
f2ce60b8 | 8442 | bool constructor_possible_p = !parser->in_declarator_p; |
2b7a3abf | 8443 | cp_token *start_token = NULL; |
21526606 | 8444 | |
62d1db17 MM |
8445 | /* Clear DECL_SPECS. */ |
8446 | clear_decl_specs (decl_specs); | |
8447 | ||
a723baf1 | 8448 | /* Assume no class or enumeration type is declared. */ |
560ad596 | 8449 | *declares_class_or_enum = 0; |
a723baf1 | 8450 | |
a723baf1 MM |
8451 | /* Keep reading specifiers until there are no more to read. */ |
8452 | while (true) | |
8453 | { | |
a723baf1 | 8454 | bool constructor_p; |
62d1db17 | 8455 | bool found_decl_spec; |
a723baf1 MM |
8456 | cp_token *token; |
8457 | ||
8458 | /* Peek at the next token. */ | |
8459 | token = cp_lexer_peek_token (parser->lexer); | |
2b7a3abf DS |
8460 | |
8461 | /* Save the first token of the decl spec list for error | |
8462 | reporting. */ | |
8463 | if (!start_token) | |
8464 | start_token = token; | |
a723baf1 MM |
8465 | /* Handle attributes. */ |
8466 | if (token->keyword == RID_ATTRIBUTE) | |
8467 | { | |
8468 | /* Parse the attributes. */ | |
98ca843c | 8469 | decl_specs->attributes |
62d1db17 MM |
8470 | = chainon (decl_specs->attributes, |
8471 | cp_parser_attributes_opt (parser)); | |
a723baf1 MM |
8472 | continue; |
8473 | } | |
62d1db17 MM |
8474 | /* Assume we will find a decl-specifier keyword. */ |
8475 | found_decl_spec = true; | |
a723baf1 MM |
8476 | /* If the next token is an appropriate keyword, we can simply |
8477 | add it to the list. */ | |
8478 | switch (token->keyword) | |
8479 | { | |
a723baf1 MM |
8480 | /* decl-specifier: |
8481 | friend */ | |
62d1db17 | 8482 | case RID_FRIEND: |
c7baf9e9 MM |
8483 | if (!at_class_scope_p ()) |
8484 | { | |
69bc6bff | 8485 | error_at (token->location, "%<friend%> used outside of class"); |
c7baf9e9 MM |
8486 | cp_lexer_purge_token (parser->lexer); |
8487 | } | |
8488 | else | |
8489 | { | |
8490 | ++decl_specs->specs[(int) ds_friend]; | |
8491 | /* Consume the token. */ | |
8492 | cp_lexer_consume_token (parser->lexer); | |
8493 | } | |
a723baf1 MM |
8494 | break; |
8495 | ||
8496 | /* function-specifier: | |
8497 | inline | |
8498 | virtual | |
8499 | explicit */ | |
8500 | case RID_INLINE: | |
8501 | case RID_VIRTUAL: | |
8502 | case RID_EXPLICIT: | |
62d1db17 | 8503 | cp_parser_function_specifier_opt (parser, decl_specs); |
a723baf1 | 8504 | break; |
21526606 | 8505 | |
a723baf1 MM |
8506 | /* decl-specifier: |
8507 | typedef */ | |
8508 | case RID_TYPEDEF: | |
62d1db17 | 8509 | ++decl_specs->specs[(int) ds_typedef]; |
a723baf1 MM |
8510 | /* Consume the token. */ |
8511 | cp_lexer_consume_token (parser->lexer); | |
2050a1bb MM |
8512 | /* A constructor declarator cannot appear in a typedef. */ |
8513 | constructor_possible_p = false; | |
c006d942 MM |
8514 | /* The "typedef" keyword can only occur in a declaration; we |
8515 | may as well commit at this point. */ | |
8516 | cp_parser_commit_to_tentative_parse (parser); | |
26106542 LM |
8517 | |
8518 | if (decl_specs->storage_class != sc_none) | |
8519 | decl_specs->conflicting_specifiers_p = true; | |
a723baf1 MM |
8520 | break; |
8521 | ||
8522 | /* storage-class-specifier: | |
8523 | auto | |
8524 | register | |
8525 | static | |
8526 | extern | |
21526606 | 8527 | mutable |
a723baf1 | 8528 | |
0cbd7506 | 8529 | GNU Extension: |
a723baf1 MM |
8530 | thread */ |
8531 | case RID_AUTO: | |
4dbc8575 DG |
8532 | if (cxx_dialect == cxx98) |
8533 | { | |
86a09a9e JM |
8534 | /* Consume the token. */ |
8535 | cp_lexer_consume_token (parser->lexer); | |
8536 | ||
4dbc8575 DG |
8537 | /* Complain about `auto' as a storage specifier, if |
8538 | we're complaining about C++0x compatibility. */ | |
69bc6bff MLI |
8539 | warning_at (token->location, OPT_Wc__0x_compat, "%<auto%>" |
8540 | " will change meaning in C++0x; please remove it"); | |
4dbc8575 DG |
8541 | |
8542 | /* Set the storage class anyway. */ | |
2b7a3abf DS |
8543 | cp_parser_set_storage_class (parser, decl_specs, RID_AUTO, |
8544 | token->location); | |
4dbc8575 | 8545 | } |
86a09a9e JM |
8546 | else |
8547 | /* C++0x auto type-specifier. */ | |
8548 | found_decl_spec = false; | |
4dbc8575 DG |
8549 | break; |
8550 | ||
a723baf1 MM |
8551 | case RID_REGISTER: |
8552 | case RID_STATIC: | |
8553 | case RID_EXTERN: | |
8554 | case RID_MUTABLE: | |
62d1db17 MM |
8555 | /* Consume the token. */ |
8556 | cp_lexer_consume_token (parser->lexer); | |
2b7a3abf DS |
8557 | cp_parser_set_storage_class (parser, decl_specs, token->keyword, |
8558 | token->location); | |
62d1db17 | 8559 | break; |
a723baf1 | 8560 | case RID_THREAD: |
62d1db17 MM |
8561 | /* Consume the token. */ |
8562 | cp_lexer_consume_token (parser->lexer); | |
8563 | ++decl_specs->specs[(int) ds_thread]; | |
a723baf1 | 8564 | break; |
21526606 | 8565 | |
a723baf1 | 8566 | default: |
62d1db17 MM |
8567 | /* We did not yet find a decl-specifier yet. */ |
8568 | found_decl_spec = false; | |
a723baf1 MM |
8569 | break; |
8570 | } | |
8571 | ||
8572 | /* Constructors are a special case. The `S' in `S()' is not a | |
8573 | decl-specifier; it is the beginning of the declarator. */ | |
98ca843c | 8574 | constructor_p |
62d1db17 MM |
8575 | = (!found_decl_spec |
8576 | && constructor_possible_p | |
98ca843c | 8577 | && (cp_parser_constructor_declarator_p |
62d1db17 | 8578 | (parser, decl_specs->specs[(int) ds_friend] != 0))); |
a723baf1 MM |
8579 | |
8580 | /* If we don't have a DECL_SPEC yet, then we must be looking at | |
8581 | a type-specifier. */ | |
62d1db17 | 8582 | if (!found_decl_spec && !constructor_p) |
a723baf1 | 8583 | { |
560ad596 | 8584 | int decl_spec_declares_class_or_enum; |
a723baf1 | 8585 | bool is_cv_qualifier; |
62d1db17 | 8586 | tree type_spec; |
a723baf1 | 8587 | |
62d1db17 | 8588 | type_spec |
a723baf1 | 8589 | = cp_parser_type_specifier (parser, flags, |
62d1db17 | 8590 | decl_specs, |
a723baf1 MM |
8591 | /*is_declaration=*/true, |
8592 | &decl_spec_declares_class_or_enum, | |
8593 | &is_cv_qualifier); | |
a723baf1 MM |
8594 | *declares_class_or_enum |= decl_spec_declares_class_or_enum; |
8595 | ||
8596 | /* If this type-specifier referenced a user-defined type | |
8597 | (a typedef, class-name, etc.), then we can't allow any | |
8598 | more such type-specifiers henceforth. | |
8599 | ||
8600 | [dcl.spec] | |
8601 | ||
8602 | The longest sequence of decl-specifiers that could | |
8603 | possibly be a type name is taken as the | |
8604 | decl-specifier-seq of a declaration. The sequence shall | |
8605 | be self-consistent as described below. | |
8606 | ||
8607 | [dcl.type] | |
8608 | ||
8609 | As a general rule, at most one type-specifier is allowed | |
8610 | in the complete decl-specifier-seq of a declaration. The | |
8611 | only exceptions are the following: | |
8612 | ||
8613 | -- const or volatile can be combined with any other | |
21526606 | 8614 | type-specifier. |
a723baf1 MM |
8615 | |
8616 | -- signed or unsigned can be combined with char, long, | |
8617 | short, or int. | |
8618 | ||
8619 | -- .. | |
8620 | ||
8621 | Example: | |
8622 | ||
8623 | typedef char* Pc; | |
8624 | void g (const int Pc); | |
8625 | ||
8626 | Here, Pc is *not* part of the decl-specifier seq; it's | |
8627 | the declarator. Therefore, once we see a type-specifier | |
8628 | (other than a cv-qualifier), we forbid any additional | |
8629 | user-defined types. We *do* still allow things like `int | |
8630 | int' to be considered a decl-specifier-seq, and issue the | |
8631 | error message later. */ | |
62d1db17 | 8632 | if (type_spec && !is_cv_qualifier) |
a723baf1 | 8633 | flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES; |
2050a1bb | 8634 | /* A constructor declarator cannot follow a type-specifier. */ |
62d1db17 | 8635 | if (type_spec) |
a723baf1 | 8636 | { |
62d1db17 MM |
8637 | constructor_possible_p = false; |
8638 | found_decl_spec = true; | |
a723baf1 | 8639 | } |
a723baf1 MM |
8640 | } |
8641 | ||
62d1db17 MM |
8642 | /* If we still do not have a DECL_SPEC, then there are no more |
8643 | decl-specifiers. */ | |
8644 | if (!found_decl_spec) | |
8645 | break; | |
a723baf1 | 8646 | |
62d1db17 | 8647 | decl_specs->any_specifiers_p = true; |
a723baf1 MM |
8648 | /* After we see one decl-specifier, further decl-specifiers are |
8649 | always optional. */ | |
8650 | flags |= CP_PARSER_FLAGS_OPTIONAL; | |
8651 | } | |
8652 | ||
2b7a3abf | 8653 | cp_parser_check_decl_spec (decl_specs, start_token->location); |
28c84d63 | 8654 | |
0426c4ca | 8655 | /* Don't allow a friend specifier with a class definition. */ |
62d1db17 MM |
8656 | if (decl_specs->specs[(int) ds_friend] != 0 |
8657 | && (*declares_class_or_enum & 2)) | |
69bc6bff MLI |
8658 | error_at (start_token->location, |
8659 | "class definition may not be declared a friend"); | |
a723baf1 MM |
8660 | } |
8661 | ||
21526606 | 8662 | /* Parse an (optional) storage-class-specifier. |
a723baf1 MM |
8663 | |
8664 | storage-class-specifier: | |
8665 | auto | |
8666 | register | |
8667 | static | |
8668 | extern | |
21526606 | 8669 | mutable |
a723baf1 MM |
8670 | |
8671 | GNU Extension: | |
8672 | ||
8673 | storage-class-specifier: | |
8674 | thread | |
8675 | ||
8676 | Returns an IDENTIFIER_NODE corresponding to the keyword used. */ | |
21526606 | 8677 | |
a723baf1 | 8678 | static tree |
94edc4ab | 8679 | cp_parser_storage_class_specifier_opt (cp_parser* parser) |
a723baf1 MM |
8680 | { |
8681 | switch (cp_lexer_peek_token (parser->lexer)->keyword) | |
8682 | { | |
8683 | case RID_AUTO: | |
4dbc8575 DG |
8684 | if (cxx_dialect != cxx98) |
8685 | return NULL_TREE; | |
8686 | /* Fall through for C++98. */ | |
8687 | ||
a723baf1 MM |
8688 | case RID_REGISTER: |
8689 | case RID_STATIC: | |
8690 | case RID_EXTERN: | |
8691 | case RID_MUTABLE: | |
8692 | case RID_THREAD: | |
8693 | /* Consume the token. */ | |
d6b418fa | 8694 | return cp_lexer_consume_token (parser->lexer)->u.value; |
a723baf1 MM |
8695 | |
8696 | default: | |
8697 | return NULL_TREE; | |
8698 | } | |
8699 | } | |
8700 | ||
21526606 | 8701 | /* Parse an (optional) function-specifier. |
a723baf1 MM |
8702 | |
8703 | function-specifier: | |
8704 | inline | |
8705 | virtual | |
8706 | explicit | |
8707 | ||
62d1db17 MM |
8708 | Returns an IDENTIFIER_NODE corresponding to the keyword used. |
8709 | Updates DECL_SPECS, if it is non-NULL. */ | |
21526606 | 8710 | |
a723baf1 | 8711 | static tree |
62d1db17 MM |
8712 | cp_parser_function_specifier_opt (cp_parser* parser, |
8713 | cp_decl_specifier_seq *decl_specs) | |
a723baf1 | 8714 | { |
2b7a3abf DS |
8715 | cp_token *token = cp_lexer_peek_token (parser->lexer); |
8716 | switch (token->keyword) | |
a723baf1 MM |
8717 | { |
8718 | case RID_INLINE: | |
62d1db17 MM |
8719 | if (decl_specs) |
8720 | ++decl_specs->specs[(int) ds_inline]; | |
8721 | break; | |
8722 | ||
a723baf1 | 8723 | case RID_VIRTUAL: |
ceacde63 | 8724 | /* 14.5.2.3 [temp.mem] |
3db45ab5 MS |
8725 | |
8726 | A member function template shall not be virtual. */ | |
ceacde63 | 8727 | if (PROCESSING_REAL_TEMPLATE_DECL_P ()) |
69bc6bff | 8728 | error_at (token->location, "templates may not be %<virtual%>"); |
ceacde63 | 8729 | else if (decl_specs) |
62d1db17 MM |
8730 | ++decl_specs->specs[(int) ds_virtual]; |
8731 | break; | |
8732 | ||
a723baf1 | 8733 | case RID_EXPLICIT: |
62d1db17 MM |
8734 | if (decl_specs) |
8735 | ++decl_specs->specs[(int) ds_explicit]; | |
8736 | break; | |
a723baf1 MM |
8737 | |
8738 | default: | |
8739 | return NULL_TREE; | |
8740 | } | |
62d1db17 MM |
8741 | |
8742 | /* Consume the token. */ | |
d6b418fa | 8743 | return cp_lexer_consume_token (parser->lexer)->u.value; |
a723baf1 MM |
8744 | } |
8745 | ||
8746 | /* Parse a linkage-specification. | |
8747 | ||
8748 | linkage-specification: | |
8749 | extern string-literal { declaration-seq [opt] } | |
8750 | extern string-literal declaration */ | |
8751 | ||
8752 | static void | |
94edc4ab | 8753 | cp_parser_linkage_specification (cp_parser* parser) |
a723baf1 | 8754 | { |
a723baf1 MM |
8755 | tree linkage; |
8756 | ||
8757 | /* Look for the `extern' keyword. */ | |
3a483371 | 8758 | cp_parser_require_keyword (parser, RID_EXTERN, "%<extern%>"); |
a723baf1 | 8759 | |
c162c75e MA |
8760 | /* Look for the string-literal. */ |
8761 | linkage = cp_parser_string_literal (parser, false, false); | |
a723baf1 MM |
8762 | |
8763 | /* Transform the literal into an identifier. If the literal is a | |
8764 | wide-character string, or contains embedded NULs, then we can't | |
8765 | handle it as the user wants. */ | |
c162c75e MA |
8766 | if (strlen (TREE_STRING_POINTER (linkage)) |
8767 | != (size_t) (TREE_STRING_LENGTH (linkage) - 1)) | |
a723baf1 MM |
8768 | { |
8769 | cp_parser_error (parser, "invalid linkage-specification"); | |
8770 | /* Assume C++ linkage. */ | |
c162c75e | 8771 | linkage = lang_name_cplusplus; |
a723baf1 | 8772 | } |
a723baf1 | 8773 | else |
c162c75e | 8774 | linkage = get_identifier (TREE_STRING_POINTER (linkage)); |
a723baf1 MM |
8775 | |
8776 | /* We're now using the new linkage. */ | |
8777 | push_lang_context (linkage); | |
8778 | ||
8779 | /* If the next token is a `{', then we're using the first | |
8780 | production. */ | |
8781 | if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)) | |
8782 | { | |
8783 | /* Consume the `{' token. */ | |
8784 | cp_lexer_consume_token (parser->lexer); | |
8785 | /* Parse the declarations. */ | |
8786 | cp_parser_declaration_seq_opt (parser); | |
8787 | /* Look for the closing `}'. */ | |
3a483371 | 8788 | cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>"); |
a723baf1 MM |
8789 | } |
8790 | /* Otherwise, there's just one declaration. */ | |
8791 | else | |
8792 | { | |
8793 | bool saved_in_unbraced_linkage_specification_p; | |
8794 | ||
21526606 | 8795 | saved_in_unbraced_linkage_specification_p |
a723baf1 MM |
8796 | = parser->in_unbraced_linkage_specification_p; |
8797 | parser->in_unbraced_linkage_specification_p = true; | |
a723baf1 | 8798 | cp_parser_declaration (parser); |
21526606 | 8799 | parser->in_unbraced_linkage_specification_p |
a723baf1 MM |
8800 | = saved_in_unbraced_linkage_specification_p; |
8801 | } | |
8802 | ||
8803 | /* We're done with the linkage-specification. */ | |
8804 | pop_lang_context (); | |
8805 | } | |
8806 | ||
55a3debe DG |
8807 | /* Parse a static_assert-declaration. |
8808 | ||
8809 | static_assert-declaration: | |
8810 | static_assert ( constant-expression , string-literal ) ; | |
8811 | ||
8812 | If MEMBER_P, this static_assert is a class member. */ | |
8813 | ||
8814 | static void | |
8815 | cp_parser_static_assert(cp_parser *parser, bool member_p) | |
8816 | { | |
8817 | tree condition; | |
8818 | tree message; | |
8819 | cp_token *token; | |
8820 | location_t saved_loc; | |
8821 | ||
8822 | /* Peek at the `static_assert' token so we can keep track of exactly | |
8823 | where the static assertion started. */ | |
8824 | token = cp_lexer_peek_token (parser->lexer); | |
8825 | saved_loc = token->location; | |
8826 | ||
8827 | /* Look for the `static_assert' keyword. */ | |
8828 | if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT, | |
3a483371 | 8829 | "%<static_assert%>")) |
55a3debe DG |
8830 | return; |
8831 | ||
8832 | /* We know we are in a static assertion; commit to any tentative | |
8833 | parse. */ | |
8834 | if (cp_parser_parsing_tentatively (parser)) | |
8835 | cp_parser_commit_to_tentative_parse (parser); | |
8836 | ||
8837 | /* Parse the `(' starting the static assertion condition. */ | |
3a483371 | 8838 | cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"); |
55a3debe DG |
8839 | |
8840 | /* Parse the constant-expression. */ | |
8841 | condition = | |
8842 | cp_parser_constant_expression (parser, | |
8843 | /*allow_non_constant_p=*/false, | |
8844 | /*non_constant_p=*/NULL); | |
8845 | ||
8846 | /* Parse the separating `,'. */ | |
3a483371 | 8847 | cp_parser_require (parser, CPP_COMMA, "%<,%>"); |
55a3debe DG |
8848 | |
8849 | /* Parse the string-literal message. */ | |
8850 | message = cp_parser_string_literal (parser, | |
8851 | /*translate=*/false, | |
8852 | /*wide_ok=*/true); | |
8853 | ||
8854 | /* A `)' completes the static assertion. */ | |
3a483371 | 8855 | if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>")) |
55a3debe DG |
8856 | cp_parser_skip_to_closing_parenthesis (parser, |
8857 | /*recovering=*/true, | |
8858 | /*or_comma=*/false, | |
8859 | /*consume_paren=*/true); | |
8860 | ||
8861 | /* A semicolon terminates the declaration. */ | |
3a483371 | 8862 | cp_parser_require (parser, CPP_SEMICOLON, "%<;%>"); |
55a3debe DG |
8863 | |
8864 | /* Complete the static assertion, which may mean either processing | |
8865 | the static assert now or saving it for template instantiation. */ | |
8866 | finish_static_assert (condition, message, saved_loc, member_p); | |
8867 | } | |
8868 | ||
3ad6a8e1 DG |
8869 | /* Parse a `decltype' type. Returns the type. |
8870 | ||
8871 | simple-type-specifier: | |
8872 | decltype ( expression ) */ | |
8873 | ||
8874 | static tree | |
8875 | cp_parser_decltype (cp_parser *parser) | |
8876 | { | |
8877 | tree expr; | |
8878 | bool id_expression_or_member_access_p = false; | |
8879 | const char *saved_message; | |
8880 | bool saved_integral_constant_expression_p; | |
8881 | bool saved_non_integral_constant_expression_p; | |
2b7a3abf | 8882 | cp_token *id_expr_start_token; |
3ad6a8e1 DG |
8883 | |
8884 | /* Look for the `decltype' token. */ | |
3a483371 | 8885 | if (!cp_parser_require_keyword (parser, RID_DECLTYPE, "%<decltype%>")) |
3ad6a8e1 DG |
8886 | return error_mark_node; |
8887 | ||
8888 | /* Types cannot be defined in a `decltype' expression. Save away the | |
8889 | old message. */ | |
8890 | saved_message = parser->type_definition_forbidden_message; | |
8891 | ||
8892 | /* And create the new one. */ | |
8893 | parser->type_definition_forbidden_message | |
8b4efb4e | 8894 | = "types may not be defined in %<decltype%> expressions"; |
3ad6a8e1 DG |
8895 | |
8896 | /* The restrictions on constant-expressions do not apply inside | |
8897 | decltype expressions. */ | |
8898 | saved_integral_constant_expression_p | |
8899 | = parser->integral_constant_expression_p; | |
8900 | saved_non_integral_constant_expression_p | |
8901 | = parser->non_integral_constant_expression_p; | |
8902 | parser->integral_constant_expression_p = false; | |
8903 | ||
8904 | /* Do not actually evaluate the expression. */ | |
7d882b83 ILT |
8905 | ++cp_unevaluated_operand; |
8906 | ||
8907 | /* Do not warn about problems with the expression. */ | |
8908 | ++c_inhibit_evaluation_warnings; | |
3ad6a8e1 DG |
8909 | |
8910 | /* Parse the opening `('. */ | |
3a483371 | 8911 | if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>")) |
2b536806 | 8912 | return error_mark_node; |
3ad6a8e1 DG |
8913 | |
8914 | /* First, try parsing an id-expression. */ | |
2b7a3abf | 8915 | id_expr_start_token = cp_lexer_peek_token (parser->lexer); |
3ad6a8e1 DG |
8916 | cp_parser_parse_tentatively (parser); |
8917 | expr = cp_parser_id_expression (parser, | |
8918 | /*template_keyword_p=*/false, | |
8919 | /*check_dependency_p=*/true, | |
8920 | /*template_p=*/NULL, | |
8921 | /*declarator_p=*/false, | |
8922 | /*optional_p=*/false); | |
8923 | ||
8924 | if (!cp_parser_error_occurred (parser) && expr != error_mark_node) | |
8925 | { | |
8926 | bool non_integral_constant_expression_p = false; | |
8927 | tree id_expression = expr; | |
8928 | cp_id_kind idk; | |
8929 | const char *error_msg; | |
8930 | ||
2b536806 DG |
8931 | if (TREE_CODE (expr) == IDENTIFIER_NODE) |
8932 | /* Lookup the name we got back from the id-expression. */ | |
8933 | expr = cp_parser_lookup_name (parser, expr, | |
8934 | none_type, | |
8935 | /*is_template=*/false, | |
8936 | /*is_namespace=*/false, | |
8937 | /*check_dependency=*/true, | |
2b7a3abf DS |
8938 | /*ambiguous_decls=*/NULL, |
8939 | id_expr_start_token->location); | |
2b536806 | 8940 | |
7a547b93 | 8941 | if (expr |
3ad6a8e1 DG |
8942 | && expr != error_mark_node |
8943 | && TREE_CODE (expr) != TEMPLATE_ID_EXPR | |
8944 | && TREE_CODE (expr) != TYPE_DECL | |
7a547b93 JJ |
8945 | && (TREE_CODE (expr) != BIT_NOT_EXPR |
8946 | || !TYPE_P (TREE_OPERAND (expr, 0))) | |
3ad6a8e1 DG |
8947 | && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN) |
8948 | { | |
8949 | /* Complete lookup of the id-expression. */ | |
8950 | expr = (finish_id_expression | |
8951 | (id_expression, expr, parser->scope, &idk, | |
8952 | /*integral_constant_expression_p=*/false, | |
8953 | /*allow_non_integral_constant_expression_p=*/true, | |
8954 | &non_integral_constant_expression_p, | |
8955 | /*template_p=*/false, | |
8956 | /*done=*/true, | |
8957 | /*address_p=*/false, | |
8958 | /*template_arg_p=*/false, | |
2b7a3abf DS |
8959 | &error_msg, |
8960 | id_expr_start_token->location)); | |
3ad6a8e1 DG |
8961 | |
8962 | if (expr == error_mark_node) | |
8963 | /* We found an id-expression, but it was something that we | |
8964 | should not have found. This is an error, not something | |
8965 | we can recover from, so note that we found an | |
8966 | id-expression and we'll recover as gracefully as | |
8967 | possible. */ | |
8968 | id_expression_or_member_access_p = true; | |
8969 | } | |
8970 | ||
8971 | if (expr | |
8972 | && expr != error_mark_node | |
8973 | && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN) | |
8974 | /* We have an id-expression. */ | |
8975 | id_expression_or_member_access_p = true; | |
8976 | } | |
8977 | ||
8978 | if (!id_expression_or_member_access_p) | |
8979 | { | |
8980 | /* Abort the id-expression parse. */ | |
8981 | cp_parser_abort_tentative_parse (parser); | |
8982 | ||
8983 | /* Parsing tentatively, again. */ | |
8984 | cp_parser_parse_tentatively (parser); | |
8985 | ||
8986 | /* Parse a class member access. */ | |
8987 | expr = cp_parser_postfix_expression (parser, /*address_p=*/false, | |
8988 | /*cast_p=*/false, | |
cee9035d | 8989 | /*member_access_only_p=*/true, NULL); |
3ad6a8e1 DG |
8990 | |
8991 | if (expr | |
8992 | && expr != error_mark_node | |
8993 | && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN) | |
8994 | /* We have an id-expression. */ | |
8995 | id_expression_or_member_access_p = true; | |
8996 | } | |
8997 | ||
8998 | if (id_expression_or_member_access_p) | |
8999 | /* We have parsed the complete id-expression or member access. */ | |
9000 | cp_parser_parse_definitely (parser); | |
9001 | else | |
9002 | { | |
9003 | /* Abort our attempt to parse an id-expression or member access | |
9004 | expression. */ | |
9005 | cp_parser_abort_tentative_parse (parser); | |
9006 | ||
9007 | /* Parse a full expression. */ | |
cee9035d | 9008 | expr = cp_parser_expression (parser, /*cast_p=*/false, NULL); |
3ad6a8e1 DG |
9009 | } |
9010 | ||
9011 | /* Go back to evaluating expressions. */ | |
7d882b83 ILT |
9012 | --cp_unevaluated_operand; |
9013 | --c_inhibit_evaluation_warnings; | |
3ad6a8e1 DG |
9014 | |
9015 | /* Restore the old message and the integral constant expression | |
9016 | flags. */ | |
9017 | parser->type_definition_forbidden_message = saved_message; | |
9018 | parser->integral_constant_expression_p | |
9019 | = saved_integral_constant_expression_p; | |
9020 | parser->non_integral_constant_expression_p | |
9021 | = saved_non_integral_constant_expression_p; | |
9022 | ||
9023 | if (expr == error_mark_node) | |
9024 | { | |
9025 | /* Skip everything up to the closing `)'. */ | |
9026 | cp_parser_skip_to_closing_parenthesis (parser, true, false, | |
9027 | /*consume_paren=*/true); | |
9028 | return error_mark_node; | |
9029 | } | |
9030 | ||
9031 | /* Parse to the closing `)'. */ | |
3a483371 | 9032 | if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>")) |
91929b4d JJ |
9033 | { |
9034 | cp_parser_skip_to_closing_parenthesis (parser, true, false, | |
9035 | /*consume_paren=*/true); | |
9036 | return error_mark_node; | |
9037 | } | |
3ad6a8e1 DG |
9038 | |
9039 | return finish_decltype_type (expr, id_expression_or_member_access_p); | |
9040 | } | |
9041 | ||
a723baf1 MM |
9042 | /* Special member functions [gram.special] */ |
9043 | ||
9044 | /* Parse a conversion-function-id. | |
9045 | ||
9046 | conversion-function-id: | |
21526606 | 9047 | operator conversion-type-id |
a723baf1 MM |
9048 | |
9049 | Returns an IDENTIFIER_NODE representing the operator. */ | |
9050 | ||
21526606 | 9051 | static tree |
94edc4ab | 9052 | cp_parser_conversion_function_id (cp_parser* parser) |
a723baf1 MM |
9053 | { |
9054 | tree type; | |
9055 | tree saved_scope; | |
9056 | tree saved_qualifying_scope; | |
9057 | tree saved_object_scope; | |
4514aa8c | 9058 | tree pushed_scope = NULL_TREE; |
a723baf1 MM |
9059 | |
9060 | /* Look for the `operator' token. */ | |
3a483371 | 9061 | if (!cp_parser_require_keyword (parser, RID_OPERATOR, "%<operator%>")) |
a723baf1 MM |
9062 | return error_mark_node; |
9063 | /* When we parse the conversion-type-id, the current scope will be | |
9064 | reset. However, we need that information in able to look up the | |
9065 | conversion function later, so we save it here. */ | |
9066 | saved_scope = parser->scope; | |
9067 | saved_qualifying_scope = parser->qualifying_scope; | |
9068 | saved_object_scope = parser->object_scope; | |
9069 | /* We must enter the scope of the class so that the names of | |
9070 | entities declared within the class are available in the | |
9071 | conversion-type-id. For example, consider: | |
9072 | ||
21526606 | 9073 | struct S { |
0cbd7506 | 9074 | typedef int I; |
a723baf1 MM |
9075 | operator I(); |
9076 | }; | |
9077 | ||
9078 | S::operator I() { ... } | |
9079 | ||
9080 | In order to see that `I' is a type-name in the definition, we | |
9081 | must be in the scope of `S'. */ | |
9082 | if (saved_scope) | |
4514aa8c | 9083 | pushed_scope = push_scope (saved_scope); |
a723baf1 MM |
9084 | /* Parse the conversion-type-id. */ |
9085 | type = cp_parser_conversion_type_id (parser); | |
9086 | /* Leave the scope of the class, if any. */ | |
4514aa8c NS |
9087 | if (pushed_scope) |
9088 | pop_scope (pushed_scope); | |
a723baf1 MM |
9089 | /* Restore the saved scope. */ |
9090 | parser->scope = saved_scope; | |
9091 | parser->qualifying_scope = saved_qualifying_scope; | |
9092 | parser->object_scope = saved_object_scope; | |
9093 | /* If the TYPE is invalid, indicate failure. */ | |
9094 | if (type == error_mark_node) | |
9095 | return error_mark_node; | |
9096 | return mangle_conv_op_name_for_type (type); | |
9097 | } | |
9098 | ||
9099 | /* Parse a conversion-type-id: | |
9100 | ||
9101 | conversion-type-id: | |
9102 | type-specifier-seq conversion-declarator [opt] | |
9103 | ||
9104 | Returns the TYPE specified. */ | |
9105 | ||
9106 | static tree | |
94edc4ab | 9107 | cp_parser_conversion_type_id (cp_parser* parser) |
a723baf1 MM |
9108 | { |
9109 | tree attributes; | |
62d1db17 | 9110 | cp_decl_specifier_seq type_specifiers; |
058b15c1 | 9111 | cp_declarator *declarator; |
037cc9c5 | 9112 | tree type_specified; |
a723baf1 MM |
9113 | |
9114 | /* Parse the attributes. */ | |
9115 | attributes = cp_parser_attributes_opt (parser); | |
9116 | /* Parse the type-specifiers. */ | |
d4113656 MM |
9117 | cp_parser_type_specifier_seq (parser, /*is_condition=*/false, |
9118 | &type_specifiers); | |
a723baf1 | 9119 | /* If that didn't work, stop. */ |
62d1db17 | 9120 | if (type_specifiers.type == error_mark_node) |
a723baf1 MM |
9121 | return error_mark_node; |
9122 | /* Parse the conversion-declarator. */ | |
9123 | declarator = cp_parser_conversion_declarator_opt (parser); | |
9124 | ||
037cc9c5 | 9125 | type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME, |
0cbd7506 | 9126 | /*initialized=*/0, &attributes); |
037cc9c5 FJ |
9127 | if (attributes) |
9128 | cplus_decl_attributes (&type_specified, attributes, /*flags=*/0); | |
64e12c68 JM |
9129 | |
9130 | /* Don't give this error when parsing tentatively. This happens to | |
9131 | work because we always parse this definitively once. */ | |
9132 | if (! cp_parser_uncommitted_to_tentative_parse_p (parser) | |
9133 | && type_uses_auto (type_specified)) | |
9134 | { | |
9135 | error ("invalid use of %<auto%> in conversion operator"); | |
9136 | return error_mark_node; | |
9137 | } | |
9138 | ||
037cc9c5 | 9139 | return type_specified; |
a723baf1 MM |
9140 | } |
9141 | ||
9142 | /* Parse an (optional) conversion-declarator. | |
9143 | ||
9144 | conversion-declarator: | |
21526606 | 9145 | ptr-operator conversion-declarator [opt] |
a723baf1 | 9146 | |
058b15c1 | 9147 | */ |
a723baf1 | 9148 | |
058b15c1 | 9149 | static cp_declarator * |
94edc4ab | 9150 | cp_parser_conversion_declarator_opt (cp_parser* parser) |
a723baf1 MM |
9151 | { |
9152 | enum tree_code code; | |
9153 | tree class_type; | |
3c01e5df | 9154 | cp_cv_quals cv_quals; |
a723baf1 MM |
9155 | |
9156 | /* We don't know if there's a ptr-operator next, or not. */ | |
9157 | cp_parser_parse_tentatively (parser); | |
9158 | /* Try the ptr-operator. */ | |
3c01e5df | 9159 | code = cp_parser_ptr_operator (parser, &class_type, &cv_quals); |
a723baf1 MM |
9160 | /* If it worked, look for more conversion-declarators. */ |
9161 | if (cp_parser_parse_definitely (parser)) | |
9162 | { | |
058b15c1 | 9163 | cp_declarator *declarator; |
98ca843c | 9164 | |
058b15c1 MM |
9165 | /* Parse another optional declarator. */ |
9166 | declarator = cp_parser_conversion_declarator_opt (parser); | |
98ca843c | 9167 | |
8af2fec4 RY |
9168 | return cp_parser_make_indirect_declarator |
9169 | (code, class_type, cv_quals, declarator); | |
a723baf1 MM |
9170 | } |
9171 | ||
058b15c1 | 9172 | return NULL; |
a723baf1 MM |
9173 | } |
9174 | ||
9175 | /* Parse an (optional) ctor-initializer. | |
9176 | ||
9177 | ctor-initializer: | |
21526606 | 9178 | : mem-initializer-list |
a723baf1 MM |
9179 | |
9180 | Returns TRUE iff the ctor-initializer was actually present. */ | |
9181 | ||
9182 | static bool | |
94edc4ab | 9183 | cp_parser_ctor_initializer_opt (cp_parser* parser) |
a723baf1 MM |
9184 | { |
9185 | /* If the next token is not a `:', then there is no | |
9186 | ctor-initializer. */ | |
9187 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)) | |
9188 | { | |
9189 | /* Do default initialization of any bases and members. */ | |
9190 | if (DECL_CONSTRUCTOR_P (current_function_decl)) | |
9191 | finish_mem_initializers (NULL_TREE); | |
9192 | ||
9193 | return false; | |
9194 | } | |
9195 | ||
9196 | /* Consume the `:' token. */ | |
9197 | cp_lexer_consume_token (parser->lexer); | |
9198 | /* And the mem-initializer-list. */ | |
9199 | cp_parser_mem_initializer_list (parser); | |
9200 | ||
9201 | return true; | |
9202 | } | |
9203 | ||
9204 | /* Parse a mem-initializer-list. | |
9205 | ||
9206 | mem-initializer-list: | |
5d80a306 DG |
9207 | mem-initializer ... [opt] |
9208 | mem-initializer ... [opt] , mem-initializer-list */ | |
a723baf1 MM |
9209 | |
9210 | static void | |
94edc4ab | 9211 | cp_parser_mem_initializer_list (cp_parser* parser) |
a723baf1 MM |
9212 | { |
9213 | tree mem_initializer_list = NULL_TREE; | |
2b7a3abf | 9214 | cp_token *token = cp_lexer_peek_token (parser->lexer); |
a723baf1 MM |
9215 | |
9216 | /* Let the semantic analysis code know that we are starting the | |
9217 | mem-initializer-list. */ | |
0e136342 | 9218 | if (!DECL_CONSTRUCTOR_P (current_function_decl)) |
69bc6bff MLI |
9219 | error_at (token->location, |
9220 | "only constructors take base initializers"); | |
a723baf1 MM |
9221 | |
9222 | /* Loop through the list. */ | |
9223 | while (true) | |
9224 | { | |
9225 | tree mem_initializer; | |
9226 | ||
2b7a3abf | 9227 | token = cp_lexer_peek_token (parser->lexer); |
a723baf1 MM |
9228 | /* Parse the mem-initializer. */ |
9229 | mem_initializer = cp_parser_mem_initializer (parser); | |
5d80a306 DG |
9230 | /* If the next token is a `...', we're expanding member initializers. */ |
9231 | if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)) | |
9232 | { | |
9233 | /* Consume the `...'. */ | |
9234 | cp_lexer_consume_token (parser->lexer); | |
9235 | ||
9236 | /* The TREE_PURPOSE must be a _TYPE, because base-specifiers | |
9237 | can be expanded but members cannot. */ | |
9238 | if (mem_initializer != error_mark_node | |
9239 | && !TYPE_P (TREE_PURPOSE (mem_initializer))) | |
9240 | { | |
69bc6bff MLI |
9241 | error_at (token->location, |
9242 | "cannot expand initializer for member %<%D%>", | |
9243 | TREE_PURPOSE (mem_initializer)); | |
5d80a306 DG |
9244 | mem_initializer = error_mark_node; |
9245 | } | |
9246 | ||
9247 | /* Construct the pack expansion type. */ | |
9248 | if (mem_initializer != error_mark_node) | |
9249 | mem_initializer = make_pack_expansion (mem_initializer); | |
9250 | } | |
a723baf1 | 9251 | /* Add it to the list, unless it was erroneous. */ |
357d956e | 9252 | if (mem_initializer != error_mark_node) |
a723baf1 MM |
9253 | { |
9254 | TREE_CHAIN (mem_initializer) = mem_initializer_list; | |
9255 | mem_initializer_list = mem_initializer; | |
9256 | } | |
9257 | /* If the next token is not a `,', we're done. */ | |
9258 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)) | |
9259 | break; | |
9260 | /* Consume the `,' token. */ | |
9261 | cp_lexer_consume_token (parser->lexer); | |
9262 | } | |
9263 | ||
9264 | /* Perform semantic analysis. */ | |
0e136342 MM |
9265 | if (DECL_CONSTRUCTOR_P (current_function_decl)) |
9266 | finish_mem_initializers (mem_initializer_list); | |
a723baf1 MM |
9267 | } |
9268 | ||
9269 | /* Parse a mem-initializer. | |
9270 | ||
9271 | mem-initializer: | |
21526606 | 9272 | mem-initializer-id ( expression-list [opt] ) |
09357846 | 9273 | mem-initializer-id braced-init-list |
a723baf1 MM |
9274 | |
9275 | GNU extension: | |
21526606 | 9276 | |
a723baf1 | 9277 | mem-initializer: |
34cd5ae7 | 9278 | ( expression-list [opt] ) |
a723baf1 MM |
9279 | |
9280 | Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base | |
9281 | class) or FIELD_DECL (for a non-static data member) to initialize; | |
357d956e MM |
9282 | the TREE_VALUE is the expression-list. An empty initialization |
9283 | list is represented by void_list_node. */ | |
a723baf1 MM |
9284 | |
9285 | static tree | |
94edc4ab | 9286 | cp_parser_mem_initializer (cp_parser* parser) |
a723baf1 MM |
9287 | { |
9288 | tree mem_initializer_id; | |
9289 | tree expression_list; | |
1f5a253a | 9290 | tree member; |
2b7a3abf | 9291 | cp_token *token = cp_lexer_peek_token (parser->lexer); |
21526606 | 9292 | |
a723baf1 MM |
9293 | /* Find out what is being initialized. */ |
9294 | if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)) | |
9295 | { | |
cbe5f3b3 MLI |
9296 | permerror (token->location, |
9297 | "anachronistic old-style base class initializer"); | |
a723baf1 MM |
9298 | mem_initializer_id = NULL_TREE; |
9299 | } | |
9300 | else | |
aad5badd L |
9301 | { |
9302 | mem_initializer_id = cp_parser_mem_initializer_id (parser); | |
9303 | if (mem_initializer_id == error_mark_node) | |
9304 | return mem_initializer_id; | |
9305 | } | |
1f5a253a NS |
9306 | member = expand_member_init (mem_initializer_id); |
9307 | if (member && !DECL_P (member)) | |
9308 | in_base_initializer = 1; | |
7efa3e22 | 9309 | |
09357846 JM |
9310 | if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)) |
9311 | { | |
9312 | bool expr_non_constant_p; | |
9313 | maybe_warn_cpp0x ("extended initializer lists"); | |
9314 | expression_list = cp_parser_braced_list (parser, &expr_non_constant_p); | |
9315 | CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1; | |
9316 | expression_list = build_tree_list (NULL_TREE, expression_list); | |
9317 | } | |
9318 | else | |
c166b898 ILT |
9319 | { |
9320 | VEC(tree,gc)* vec; | |
9321 | vec = cp_parser_parenthesized_expression_list (parser, false, | |
9322 | /*cast_p=*/false, | |
9323 | /*allow_expansion_p=*/true, | |
9324 | /*non_constant_p=*/NULL); | |
9325 | if (vec == NULL) | |
9326 | return error_mark_node; | |
9327 | expression_list = build_tree_list_vec (vec); | |
9328 | release_tree_vector (vec); | |
9329 | } | |
9330 | ||
357d956e MM |
9331 | if (expression_list == error_mark_node) |
9332 | return error_mark_node; | |
7efa3e22 | 9333 | if (!expression_list) |
a723baf1 | 9334 | expression_list = void_type_node; |
a723baf1 | 9335 | |
1f5a253a | 9336 | in_base_initializer = 0; |
21526606 | 9337 | |
357d956e | 9338 | return member ? build_tree_list (member, expression_list) : error_mark_node; |
a723baf1 MM |
9339 | } |
9340 | ||
9341 | /* Parse a mem-initializer-id. | |
9342 | ||
9343 | mem-initializer-id: | |
9344 | :: [opt] nested-name-specifier [opt] class-name | |
21526606 | 9345 | identifier |
a723baf1 MM |
9346 | |
9347 | Returns a TYPE indicating the class to be initializer for the first | |
9348 | production. Returns an IDENTIFIER_NODE indicating the data member | |
9349 | to be initialized for the second production. */ | |
9350 | ||
9351 | static tree | |
94edc4ab | 9352 | cp_parser_mem_initializer_id (cp_parser* parser) |
a723baf1 MM |
9353 | { |
9354 | bool global_scope_p; | |
9355 | bool nested_name_specifier_p; | |
8a83a693 | 9356 | bool template_p = false; |
a723baf1 MM |
9357 | tree id; |
9358 | ||
2b7a3abf DS |
9359 | cp_token *token = cp_lexer_peek_token (parser->lexer); |
9360 | ||
8a83a693 GB |
9361 | /* `typename' is not allowed in this context ([temp.res]). */ |
9362 | if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME)) | |
9363 | { | |
69bc6bff MLI |
9364 | error_at (token->location, |
9365 | "keyword %<typename%> not allowed in this context (a qualified " | |
9366 | "member initializer is implicitly a type)"); | |
8a83a693 GB |
9367 | cp_lexer_consume_token (parser->lexer); |
9368 | } | |
a723baf1 | 9369 | /* Look for the optional `::' operator. */ |
21526606 EC |
9370 | global_scope_p |
9371 | = (cp_parser_global_scope_opt (parser, | |
88525e1b | 9372 | /*current_scope_valid_p=*/false) |
a723baf1 MM |
9373 | != NULL_TREE); |
9374 | /* Look for the optional nested-name-specifier. The simplest way to | |
9375 | implement: | |
9376 | ||
9377 | [temp.res] | |
9378 | ||
9379 | The keyword `typename' is not permitted in a base-specifier or | |
9380 | mem-initializer; in these contexts a qualified name that | |
9381 | depends on a template-parameter is implicitly assumed to be a | |
9382 | type name. | |
9383 | ||
9384 | is to assume that we have seen the `typename' keyword at this | |
9385 | point. */ | |
21526606 | 9386 | nested_name_specifier_p |
a723baf1 MM |
9387 | = (cp_parser_nested_name_specifier_opt (parser, |
9388 | /*typename_keyword_p=*/true, | |
9389 | /*check_dependency_p=*/true, | |
a668c6ad MM |
9390 | /*type_p=*/true, |
9391 | /*is_declaration=*/true) | |
a723baf1 | 9392 | != NULL_TREE); |
8a83a693 GB |
9393 | if (nested_name_specifier_p) |
9394 | template_p = cp_parser_optional_template_keyword (parser); | |
a723baf1 MM |
9395 | /* If there is a `::' operator or a nested-name-specifier, then we |
9396 | are definitely looking for a class-name. */ | |
9397 | if (global_scope_p || nested_name_specifier_p) | |
9398 | return cp_parser_class_name (parser, | |
9399 | /*typename_keyword_p=*/true, | |
8a83a693 | 9400 | /*template_keyword_p=*/template_p, |
fc6a28d7 | 9401 | none_type, |
a723baf1 | 9402 | /*check_dependency_p=*/true, |
a668c6ad MM |
9403 | /*class_head_p=*/false, |
9404 | /*is_declaration=*/true); | |
a723baf1 MM |
9405 | /* Otherwise, we could also be looking for an ordinary identifier. */ |
9406 | cp_parser_parse_tentatively (parser); | |
9407 | /* Try a class-name. */ | |
21526606 | 9408 | id = cp_parser_class_name (parser, |
a723baf1 MM |
9409 | /*typename_keyword_p=*/true, |
9410 | /*template_keyword_p=*/false, | |
fc6a28d7 | 9411 | none_type, |
a723baf1 | 9412 | /*check_dependency_p=*/true, |
a668c6ad MM |
9413 | /*class_head_p=*/false, |
9414 | /*is_declaration=*/true); | |
a723baf1 MM |
9415 | /* If we found one, we're done. */ |
9416 | if (cp_parser_parse_definitely (parser)) | |
9417 | return id; | |
9418 | /* Otherwise, look for an ordinary identifier. */ | |
9419 | return cp_parser_identifier (parser); | |
9420 | } | |
9421 | ||
9422 | /* Overloading [gram.over] */ | |
9423 | ||
9424 | /* Parse an operator-function-id. | |
9425 | ||
9426 | operator-function-id: | |
21526606 | 9427 | operator operator |
a723baf1 MM |
9428 | |
9429 | Returns an IDENTIFIER_NODE for the operator which is a | |
9430 | human-readable spelling of the identifier, e.g., `operator +'. */ | |
9431 | ||
21526606 | 9432 | static tree |
94edc4ab | 9433 | cp_parser_operator_function_id (cp_parser* parser) |
a723baf1 MM |
9434 | { |
9435 | /* Look for the `operator' keyword. */ | |
3a483371 | 9436 | if (!cp_parser_require_keyword (parser, RID_OPERATOR, "%<operator%>")) |
a723baf1 MM |
9437 | return error_mark_node; |
9438 | /* And then the name of the operator itself. */ | |
9439 | return cp_parser_operator (parser); | |
9440 | } | |
9441 | ||
9442 | /* Parse an operator. | |
9443 | ||
9444 | operator: | |
9445 | new delete new[] delete[] + - * / % ^ & | ~ ! = < > | |
9446 | += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= && | |
9447 | || ++ -- , ->* -> () [] | |
9448 | ||
9449 | GNU Extensions: | |
21526606 | 9450 | |
a723baf1 MM |
9451 | operator: |
9452 | <? >? <?= >?= | |
9453 | ||
9454 | Returns an IDENTIFIER_NODE for the operator which is a | |
9455 | human-readable spelling of the identifier, e.g., `operator +'. */ | |
21526606 | 9456 | |
a723baf1 | 9457 | static tree |
94edc4ab | 9458 | cp_parser_operator (cp_parser* parser) |
a723baf1 MM |
9459 | { |
9460 | tree id = NULL_TREE; | |
9461 | cp_token *token; | |
9462 | ||
9463 | /* Peek at the next token. */ | |
9464 | token = cp_lexer_peek_token (parser->lexer); | |
9465 | /* Figure out which operator we have. */ | |
9466 | switch (token->type) | |
9467 | { | |
9468 | case CPP_KEYWORD: | |
9469 | { | |
9470 | enum tree_code op; | |
9471 | ||
9472 | /* The keyword should be either `new' or `delete'. */ | |
9473 | if (token->keyword == RID_NEW) | |
9474 | op = NEW_EXPR; | |
9475 | else if (token->keyword == RID_DELETE) | |
9476 | op = DELETE_EXPR; | |
9477 | else | |
9478 | break; | |
9479 | ||
9480 | /* Consume the `new' or `delete' token. */ | |
9481 | cp_lexer_consume_token (parser->lexer); | |
9482 | ||
9483 | /* Peek at the next token. */ | |
9484 | token = cp_lexer_peek_token (parser->lexer); | |
9485 | /* If it's a `[' token then this is the array variant of the | |
9486 | operator. */ | |
9487 | if (token->type == CPP_OPEN_SQUARE) | |
9488 | { | |
9489 | /* Consume the `[' token. */ | |
9490 | cp_lexer_consume_token (parser->lexer); | |
9491 | /* Look for the `]' token. */ | |
3a483371 | 9492 | cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>"); |
21526606 | 9493 | id = ansi_opname (op == NEW_EXPR |
a723baf1 MM |
9494 | ? VEC_NEW_EXPR : VEC_DELETE_EXPR); |
9495 | } | |
9496 | /* Otherwise, we have the non-array variant. */ | |
9497 | else | |
9498 | id = ansi_opname (op); | |
9499 | ||
9500 | return id; | |
9501 | } | |
9502 | ||
9503 | case CPP_PLUS: | |
9504 | id = ansi_opname (PLUS_EXPR); | |
9505 | break; | |
9506 | ||
9507 | case CPP_MINUS: | |
9508 | id = ansi_opname (MINUS_EXPR); | |
9509 | break; | |
9510 | ||
9511 | case CPP_MULT: | |
9512 | id = ansi_opname (MULT_EXPR); | |
9513 | break; | |
9514 | ||
9515 | case CPP_DIV: | |
9516 | id = ansi_opname (TRUNC_DIV_EXPR); | |
9517 | break; | |
9518 | ||
9519 | case CPP_MOD: | |
9520 | id = ansi_opname (TRUNC_MOD_EXPR); | |
9521 | break; | |
9522 | ||
9523 | case CPP_XOR: | |
9524 | id = ansi_opname (BIT_XOR_EXPR); | |
9525 | break; | |
9526 | ||
9527 | case CPP_AND: | |
9528 | id = ansi_opname (BIT_AND_EXPR); | |
9529 | break; | |
9530 | ||
9531 | case CPP_OR: | |
9532 | id = ansi_opname (BIT_IOR_EXPR); | |
9533 | break; | |
9534 | ||
9535 | case CPP_COMPL: | |
9536 | id = ansi_opname (BIT_NOT_EXPR); | |
9537 | break; | |
21526606 | 9538 | |
a723baf1 MM |
9539 | case CPP_NOT: |
9540 | id = ansi_opname (TRUTH_NOT_EXPR); | |
9541 | break; | |
9542 | ||
9543 | case CPP_EQ: | |
9544 | id = ansi_assopname (NOP_EXPR); | |
9545 | break; | |
9546 | ||
9547 | case CPP_LESS: | |
9548 | id = ansi_opname (LT_EXPR); | |
9549 | break; | |
9550 | ||
9551 | case CPP_GREATER: | |
9552 | id = ansi_opname (GT_EXPR); | |
9553 | break; | |
9554 | ||
9555 | case CPP_PLUS_EQ: | |
9556 | id = ansi_assopname (PLUS_EXPR); | |
9557 | break; | |
9558 | ||
9559 | case CPP_MINUS_EQ: | |
9560 | id = ansi_assopname (MINUS_EXPR); | |
9561 | break; | |
9562 | ||
9563 | case CPP_MULT_EQ: | |
9564 | id = ansi_assopname (MULT_EXPR); | |
9565 | break; | |
9566 | ||
9567 | case CPP_DIV_EQ: | |
9568 | id = ansi_assopname (TRUNC_DIV_EXPR); | |
9569 | break; | |
9570 | ||
9571 | case CPP_MOD_EQ: | |
9572 | id = ansi_assopname (TRUNC_MOD_EXPR); | |
9573 | break; | |
9574 | ||
9575 | case CPP_XOR_EQ: | |
9576 | id = ansi_assopname (BIT_XOR_EXPR); | |
9577 | break; | |
9578 | ||
9579 | case CPP_AND_EQ: | |
9580 | id = ansi_assopname (BIT_AND_EXPR); | |
9581 | break; | |
9582 | ||
9583 | case CPP_OR_EQ: | |
9584 | id = ansi_assopname (BIT_IOR_EXPR); | |
9585 | break; | |
9586 | ||
9587 | case CPP_LSHIFT: | |
9588 | id = ansi_opname (LSHIFT_EXPR); | |
9589 | break; | |
9590 | ||
9591 | case CPP_RSHIFT: | |
9592 | id = ansi_opname (RSHIFT_EXPR); | |
9593 | break; | |
9594 | ||
9595 | case CPP_LSHIFT_EQ: | |
9596 | id = ansi_assopname (LSHIFT_EXPR); | |
9597 | break; | |
9598 | ||
9599 | case CPP_RSHIFT_EQ: | |
9600 | id = ansi_assopname (RSHIFT_EXPR); | |
9601 | break; | |
9602 | ||
9603 | case CPP_EQ_EQ: | |
9604 | id = ansi_opname (EQ_EXPR); | |
9605 | break; | |
9606 | ||
9607 | case CPP_NOT_EQ: | |
9608 | id = ansi_opname (NE_EXPR); | |
9609 | break; | |
9610 | ||
9611 | case CPP_LESS_EQ: | |
9612 | id = ansi_opname (LE_EXPR); | |
9613 | break; | |
9614 | ||
9615 | case CPP_GREATER_EQ: | |
9616 | id = ansi_opname (GE_EXPR); | |
9617 | break; | |
9618 | ||
9619 | case CPP_AND_AND: | |
9620 | id = ansi_opname (TRUTH_ANDIF_EXPR); | |
9621 | break; | |
9622 | ||
9623 | case CPP_OR_OR: | |
9624 | id = ansi_opname (TRUTH_ORIF_EXPR); | |
9625 | break; | |
21526606 | 9626 | |
a723baf1 MM |
9627 | case CPP_PLUS_PLUS: |
9628 | id = ansi_opname (POSTINCREMENT_EXPR); | |
9629 | break; | |
9630 | ||
9631 | case CPP_MINUS_MINUS: | |
9632 | id = ansi_opname (PREDECREMENT_EXPR); | |
9633 | break; | |
9634 | ||
9635 | case CPP_COMMA: | |
9636 | id = ansi_opname (COMPOUND_EXPR); | |
9637 | break; | |
9638 | ||
9639 | case CPP_DEREF_STAR: | |
9640 | id = ansi_opname (MEMBER_REF); | |
9641 | break; | |
9642 | ||
9643 | case CPP_DEREF: | |
9644 | id = ansi_opname (COMPONENT_REF); | |
9645 | break; | |
9646 | ||
9647 | case CPP_OPEN_PAREN: | |
9648 | /* Consume the `('. */ | |
9649 | cp_lexer_consume_token (parser->lexer); | |
9650 | /* Look for the matching `)'. */ | |
3a483371 | 9651 | cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"); |
a723baf1 MM |
9652 | return ansi_opname (CALL_EXPR); |
9653 | ||
9654 | case CPP_OPEN_SQUARE: | |
9655 | /* Consume the `['. */ | |
9656 | cp_lexer_consume_token (parser->lexer); | |
9657 | /* Look for the matching `]'. */ | |
3a483371 | 9658 | cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>"); |
a723baf1 MM |
9659 | return ansi_opname (ARRAY_REF); |
9660 | ||
a723baf1 MM |
9661 | default: |
9662 | /* Anything else is an error. */ | |
9663 | break; | |
9664 | } | |
9665 | ||
9666 | /* If we have selected an identifier, we need to consume the | |
9667 | operator token. */ | |
9668 | if (id) | |
9669 | cp_lexer_consume_token (parser->lexer); | |
9670 | /* Otherwise, no valid operator name was present. */ | |
9671 | else | |
9672 | { | |
9673 | cp_parser_error (parser, "expected operator"); | |
9674 | id = error_mark_node; | |
9675 | } | |
9676 | ||
9677 | return id; | |
9678 | } | |
9679 | ||
9680 | /* Parse a template-declaration. | |
9681 | ||
9682 | template-declaration: | |
21526606 | 9683 | export [opt] template < template-parameter-list > declaration |
a723baf1 MM |
9684 | |
9685 | If MEMBER_P is TRUE, this template-declaration occurs within a | |
21526606 | 9686 | class-specifier. |
a723baf1 MM |
9687 | |
9688 | The grammar rule given by the standard isn't correct. What | |
9689 | is really meant is: | |
9690 | ||
9691 | template-declaration: | |
21526606 | 9692 | export [opt] template-parameter-list-seq |
a723baf1 | 9693 | decl-specifier-seq [opt] init-declarator [opt] ; |
21526606 | 9694 | export [opt] template-parameter-list-seq |
a723baf1 MM |
9695 | function-definition |
9696 | ||
9697 | template-parameter-list-seq: | |
9698 | template-parameter-list-seq [opt] | |
9699 | template < template-parameter-list > */ | |
9700 | ||
9701 | static void | |
94edc4ab | 9702 | cp_parser_template_declaration (cp_parser* parser, bool member_p) |
a723baf1 MM |
9703 | { |
9704 | /* Check for `export'. */ | |
9705 | if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT)) | |
9706 | { | |
9707 | /* Consume the `export' token. */ | |
9708 | cp_lexer_consume_token (parser->lexer); | |
9709 | /* Warn that we do not support `export'. */ | |
d4ee4d25 | 9710 | warning (0, "keyword %<export%> not implemented, and will be ignored"); |
a723baf1 MM |
9711 | } |
9712 | ||
9713 | cp_parser_template_declaration_after_export (parser, member_p); | |
9714 | } | |
9715 | ||
9716 | /* Parse a template-parameter-list. | |
9717 | ||
9718 | template-parameter-list: | |
9719 | template-parameter | |
9720 | template-parameter-list , template-parameter | |
9721 | ||
9722 | Returns a TREE_LIST. Each node represents a template parameter. | |
9723 | The nodes are connected via their TREE_CHAINs. */ | |
9724 | ||
9725 | static tree | |
94edc4ab | 9726 | cp_parser_template_parameter_list (cp_parser* parser) |
a723baf1 MM |
9727 | { |
9728 | tree parameter_list = NULL_TREE; | |
9729 | ||
357d956e | 9730 | begin_template_parm_list (); |
a723baf1 MM |
9731 | while (true) |
9732 | { | |
9733 | tree parameter; | |
058b15c1 | 9734 | bool is_non_type; |
5d80a306 | 9735 | bool is_parameter_pack; |
c2255bc4 | 9736 | location_t parm_loc; |
a723baf1 MM |
9737 | |
9738 | /* Parse the template-parameter. */ | |
c2255bc4 | 9739 | parm_loc = cp_lexer_peek_token (parser->lexer)->location; |
5d80a306 DG |
9740 | parameter = cp_parser_template_parameter (parser, |
9741 | &is_non_type, | |
9742 | &is_parameter_pack); | |
a723baf1 | 9743 | /* Add it to the list. */ |
943e3ede MM |
9744 | if (parameter != error_mark_node) |
9745 | parameter_list = process_template_parm (parameter_list, | |
c2255bc4 | 9746 | parm_loc, |
943e3ede | 9747 | parameter, |
5d80a306 DG |
9748 | is_non_type, |
9749 | is_parameter_pack); | |
b89c561c LM |
9750 | else |
9751 | { | |
9752 | tree err_parm = build_tree_list (parameter, parameter); | |
9753 | TREE_VALUE (err_parm) = error_mark_node; | |
9754 | parameter_list = chainon (parameter_list, err_parm); | |
9755 | } | |
9756 | ||
de21c145 VR |
9757 | /* If the next token is not a `,', we're done. */ |
9758 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)) | |
a723baf1 MM |
9759 | break; |
9760 | /* Otherwise, consume the `,' token. */ | |
9761 | cp_lexer_consume_token (parser->lexer); | |
9762 | } | |
9763 | ||
357d956e | 9764 | return end_template_parm_list (parameter_list); |
a723baf1 MM |
9765 | } |
9766 | ||
9767 | /* Parse a template-parameter. | |
9768 | ||
9769 | template-parameter: | |
9770 | type-parameter | |
9771 | parameter-declaration | |
9772 | ||
943e3ede MM |
9773 | If all goes well, returns a TREE_LIST. The TREE_VALUE represents |
9774 | the parameter. The TREE_PURPOSE is the default value, if any. | |
9775 | Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true | |
5d80a306 DG |
9776 | iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is |
9777 | set to true iff this parameter is a parameter pack. */ | |
a723baf1 MM |
9778 | |
9779 | static tree | |
5d80a306 DG |
9780 | cp_parser_template_parameter (cp_parser* parser, bool *is_non_type, |
9781 | bool *is_parameter_pack) | |
a723baf1 MM |
9782 | { |
9783 | cp_token *token; | |
62d1db17 | 9784 | cp_parameter_declarator *parameter_declarator; |
cde7bfee | 9785 | cp_declarator *id_declarator; |
943e3ede | 9786 | tree parm; |
a723baf1 | 9787 | |
058b15c1 MM |
9788 | /* Assume it is a type parameter or a template parameter. */ |
9789 | *is_non_type = false; | |
5d80a306 DG |
9790 | /* Assume it not a parameter pack. */ |
9791 | *is_parameter_pack = false; | |
a723baf1 MM |
9792 | /* Peek at the next token. */ |
9793 | token = cp_lexer_peek_token (parser->lexer); | |
9794 | /* If it is `class' or `template', we have a type-parameter. */ | |
9795 | if (token->keyword == RID_TEMPLATE) | |
5d80a306 | 9796 | return cp_parser_type_parameter (parser, is_parameter_pack); |
a723baf1 MM |
9797 | /* If it is `class' or `typename' we do not know yet whether it is a |
9798 | type parameter or a non-type parameter. Consider: | |
9799 | ||
9800 | template <typename T, typename T::X X> ... | |
9801 | ||
9802 | or: | |
21526606 | 9803 | |
a723baf1 MM |
9804 | template <class C, class D*> ... |
9805 | ||
9806 | Here, the first parameter is a type parameter, and the second is | |
9807 | a non-type parameter. We can tell by looking at the token after | |
9808 | the identifier -- if it is a `,', `=', or `>' then we have a type | |
9809 | parameter. */ | |
9810 | if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS) | |
9811 | { | |
9812 | /* Peek at the token after `class' or `typename'. */ | |
9813 | token = cp_lexer_peek_nth_token (parser->lexer, 2); | |
5d80a306 DG |
9814 | /* If it's an ellipsis, we have a template type parameter |
9815 | pack. */ | |
9816 | if (token->type == CPP_ELLIPSIS) | |
9817 | return cp_parser_type_parameter (parser, is_parameter_pack); | |
a723baf1 MM |
9818 | /* If it's an identifier, skip it. */ |
9819 | if (token->type == CPP_NAME) | |
9820 | token = cp_lexer_peek_nth_token (parser->lexer, 3); | |
9821 | /* Now, see if the token looks like the end of a template | |
9822 | parameter. */ | |
21526606 | 9823 | if (token->type == CPP_COMMA |
a723baf1 MM |
9824 | || token->type == CPP_EQ |
9825 | || token->type == CPP_GREATER) | |
5d80a306 | 9826 | return cp_parser_type_parameter (parser, is_parameter_pack); |
a723baf1 MM |
9827 | } |
9828 | ||
21526606 | 9829 | /* Otherwise, it is a non-type parameter. |
a723baf1 MM |
9830 | |
9831 | [temp.param] | |
9832 | ||
9833 | When parsing a default template-argument for a non-type | |
9834 | template-parameter, the first non-nested `>' is taken as the end | |
9835 | of the template parameter-list rather than a greater-than | |
9836 | operator. */ | |
058b15c1 MM |
9837 | *is_non_type = true; |
9838 | parameter_declarator | |
9839 | = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true, | |
9840 | /*parenthesized_p=*/NULL); | |
5d80a306 DG |
9841 | |
9842 | /* If the parameter declaration is marked as a parameter pack, set | |
9843 | *IS_PARAMETER_PACK to notify the caller. Also, unmark the | |
9844 | declarator's PACK_EXPANSION_P, otherwise we'll get errors from | |
9845 | grokdeclarator. */ | |
9846 | if (parameter_declarator | |
9847 | && parameter_declarator->declarator | |
9848 | && parameter_declarator->declarator->parameter_pack_p) | |
9849 | { | |
9850 | *is_parameter_pack = true; | |
9851 | parameter_declarator->declarator->parameter_pack_p = false; | |
9852 | } | |
9853 | ||
9854 | /* If the next token is an ellipsis, and we don't already have it | |
9855 | marked as a parameter pack, then we have a parameter pack (that | |
cdcae745 | 9856 | has no declarator). */ |
5d80a306 | 9857 | if (!*is_parameter_pack |
d393153e DG |
9858 | && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS) |
9859 | && declarator_can_be_parameter_pack (parameter_declarator->declarator)) | |
5d80a306 | 9860 | { |
cdcae745 | 9861 | /* Consume the `...'. */ |
5d80a306 DG |
9862 | cp_lexer_consume_token (parser->lexer); |
9863 | maybe_warn_variadic_templates (); | |
9864 | ||
9865 | *is_parameter_pack = true; | |
063cc99c DG |
9866 | } |
9867 | /* We might end up with a pack expansion as the type of the non-type | |
9868 | template parameter, in which case this is a non-type template | |
9869 | parameter pack. */ | |
9870 | else if (parameter_declarator | |
9871 | && parameter_declarator->decl_specifiers.type | |
9872 | && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type)) | |
9873 | { | |
9874 | *is_parameter_pack = true; | |
9875 | parameter_declarator->decl_specifiers.type = | |
9876 | PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type); | |
9877 | } | |
cdcae745 | 9878 | |
063cc99c DG |
9879 | if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ)) |
9880 | { | |
cdcae745 DG |
9881 | /* Parameter packs cannot have default arguments. However, a |
9882 | user may try to do so, so we'll parse them and give an | |
9883 | appropriate diagnostic here. */ | |
cdcae745 | 9884 | |
063cc99c | 9885 | /* Consume the `='. */ |
2b7a3abf | 9886 | cp_token *start_token = cp_lexer_peek_token (parser->lexer); |
063cc99c DG |
9887 | cp_lexer_consume_token (parser->lexer); |
9888 | ||
9889 | /* Find the name of the parameter pack. */ | |
9890 | id_declarator = parameter_declarator->declarator; | |
9891 | while (id_declarator && id_declarator->kind != cdk_id) | |
9892 | id_declarator = id_declarator->declarator; | |
9893 | ||
9894 | if (id_declarator && id_declarator->kind == cdk_id) | |
69bc6bff MLI |
9895 | error_at (start_token->location, |
9896 | "template parameter pack %qD cannot have a default argument", | |
9897 | id_declarator->u.id.unqualified_name); | |
063cc99c | 9898 | else |
69bc6bff MLI |
9899 | error_at (start_token->location, |
9900 | "template parameter pack cannot have a default argument"); | |
063cc99c DG |
9901 | |
9902 | /* Parse the default argument, but throw away the result. */ | |
9903 | cp_parser_default_argument (parser, /*template_parm_p=*/true); | |
5d80a306 DG |
9904 | } |
9905 | ||
943e3ede MM |
9906 | parm = grokdeclarator (parameter_declarator->declarator, |
9907 | ¶meter_declarator->decl_specifiers, | |
9908 | PARM, /*initialized=*/0, | |
9909 | /*attrlist=*/NULL); | |
9910 | if (parm == error_mark_node) | |
9911 | return error_mark_node; | |
5d80a306 | 9912 | |
943e3ede | 9913 | return build_tree_list (parameter_declarator->default_argument, parm); |
a723baf1 MM |
9914 | } |
9915 | ||
9916 | /* Parse a type-parameter. | |
9917 | ||
9918 | type-parameter: | |
9919 | class identifier [opt] | |
9920 | class identifier [opt] = type-id | |
9921 | typename identifier [opt] | |
9922 | typename identifier [opt] = type-id | |
9923 | template < template-parameter-list > class identifier [opt] | |
21526606 EC |
9924 | template < template-parameter-list > class identifier [opt] |
9925 | = id-expression | |
a723baf1 | 9926 | |
5d80a306 DG |
9927 | GNU Extension (variadic templates): |
9928 | ||
9929 | type-parameter: | |
9930 | class ... identifier [opt] | |
9931 | typename ... identifier [opt] | |
9932 | ||
a723baf1 MM |
9933 | Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The |
9934 | TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is | |
5d80a306 DG |
9935 | the declaration of the parameter. |
9936 | ||
9937 | Sets *IS_PARAMETER_PACK if this is a template parameter pack. */ | |
a723baf1 MM |
9938 | |
9939 | static tree | |
5d80a306 | 9940 | cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack) |
a723baf1 MM |
9941 | { |
9942 | cp_token *token; | |
9943 | tree parameter; | |
9944 | ||
9945 | /* Look for a keyword to tell us what kind of parameter this is. */ | |
21526606 | 9946 | token = cp_parser_require (parser, CPP_KEYWORD, |
3a483371 | 9947 | "%<class%>, %<typename%>, or %<template%>"); |
a723baf1 MM |
9948 | if (!token) |
9949 | return error_mark_node; | |
9950 | ||
9951 | switch (token->keyword) | |
9952 | { | |
9953 | case RID_CLASS: | |
9954 | case RID_TYPENAME: | |
9955 | { | |
9956 | tree identifier; | |
9957 | tree default_argument; | |
9958 | ||
5d80a306 DG |
9959 | /* If the next token is an ellipsis, we have a template |
9960 | argument pack. */ | |
9961 | if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)) | |
9962 | { | |
9963 | /* Consume the `...' token. */ | |
9964 | cp_lexer_consume_token (parser->lexer); | |
9965 | maybe_warn_variadic_templates (); | |
9966 | ||
9967 | *is_parameter_pack = true; | |
9968 | } | |
9969 | ||
a723baf1 | 9970 | /* If the next token is an identifier, then it names the |
0cbd7506 | 9971 | parameter. */ |
a723baf1 MM |
9972 | if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)) |
9973 | identifier = cp_parser_identifier (parser); | |
9974 | else | |
9975 | identifier = NULL_TREE; | |
9976 | ||
9977 | /* Create the parameter. */ | |
9978 | parameter = finish_template_type_parm (class_type_node, identifier); | |
9979 | ||
9980 | /* If the next token is an `=', we have a default argument. */ | |
9981 | if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)) | |
9982 | { | |
9983 | /* Consume the `=' token. */ | |
9984 | cp_lexer_consume_token (parser->lexer); | |
34cd5ae7 | 9985 | /* Parse the default-argument. */ |
6b648482 | 9986 | push_deferring_access_checks (dk_no_deferred); |
a723baf1 | 9987 | default_argument = cp_parser_type_id (parser); |
5d80a306 DG |
9988 | |
9989 | /* Template parameter packs cannot have default | |
9990 | arguments. */ | |
9991 | if (*is_parameter_pack) | |
9992 | { | |
9993 | if (identifier) | |
69bc6bff MLI |
9994 | error_at (token->location, |
9995 | "template parameter pack %qD cannot have a " | |
9996 | "default argument", identifier); | |
5d80a306 | 9997 | else |
69bc6bff MLI |
9998 | error_at (token->location, |
9999 | "template parameter packs cannot have " | |
10000 | "default arguments"); | |
5d80a306 DG |
10001 | default_argument = NULL_TREE; |
10002 | } | |
6b648482 | 10003 | pop_deferring_access_checks (); |
a723baf1 MM |
10004 | } |
10005 | else | |
10006 | default_argument = NULL_TREE; | |
10007 | ||
10008 | /* Create the combined representation of the parameter and the | |
10009 | default argument. */ | |
c67d36d0 | 10010 | parameter = build_tree_list (default_argument, parameter); |
a723baf1 MM |
10011 | } |
10012 | break; | |
10013 | ||
10014 | case RID_TEMPLATE: | |
10015 | { | |
10016 | tree parameter_list; | |
10017 | tree identifier; | |
10018 | tree default_argument; | |
10019 | ||
10020 | /* Look for the `<'. */ | |
3a483371 | 10021 | cp_parser_require (parser, CPP_LESS, "%<<%>"); |
a723baf1 | 10022 | /* Parse the template-parameter-list. */ |
357d956e | 10023 | parameter_list = cp_parser_template_parameter_list (parser); |
a723baf1 | 10024 | /* Look for the `>'. */ |
3a483371 | 10025 | cp_parser_require (parser, CPP_GREATER, "%<>%>"); |
a723baf1 | 10026 | /* Look for the `class' keyword. */ |
3a483371 | 10027 | cp_parser_require_keyword (parser, RID_CLASS, "%<class%>"); |
5d80a306 DG |
10028 | /* If the next token is an ellipsis, we have a template |
10029 | argument pack. */ | |
10030 | if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)) | |
10031 | { | |
10032 | /* Consume the `...' token. */ | |
10033 | cp_lexer_consume_token (parser->lexer); | |
10034 | maybe_warn_variadic_templates (); | |
10035 | ||
10036 | *is_parameter_pack = true; | |
10037 | } | |
a723baf1 MM |
10038 | /* If the next token is an `=', then there is a |
10039 | default-argument. If the next token is a `>', we are at | |
10040 | the end of the parameter-list. If the next token is a `,', | |
10041 | then we are at the end of this parameter. */ | |
10042 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ) | |
10043 | && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER) | |
10044 | && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)) | |
71bd7186 MM |
10045 | { |
10046 | identifier = cp_parser_identifier (parser); | |
03fd3f84 | 10047 | /* Treat invalid names as if the parameter were nameless. */ |
71bd7186 MM |
10048 | if (identifier == error_mark_node) |
10049 | identifier = NULL_TREE; | |
10050 | } | |
a723baf1 MM |
10051 | else |
10052 | identifier = NULL_TREE; | |
71bd7186 | 10053 | |
a723baf1 MM |
10054 | /* Create the template parameter. */ |
10055 | parameter = finish_template_template_parm (class_type_node, | |
10056 | identifier); | |
21526606 | 10057 | |
a723baf1 MM |
10058 | /* If the next token is an `=', then there is a |
10059 | default-argument. */ | |
10060 | if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)) | |
10061 | { | |
b0bc6e8e KL |
10062 | bool is_template; |
10063 | ||
a723baf1 MM |
10064 | /* Consume the `='. */ |
10065 | cp_lexer_consume_token (parser->lexer); | |
10066 | /* Parse the id-expression. */ | |
6b648482 | 10067 | push_deferring_access_checks (dk_no_deferred); |
2b7a3abf DS |
10068 | /* save token before parsing the id-expression, for error |
10069 | reporting */ | |
10070 | token = cp_lexer_peek_token (parser->lexer); | |
21526606 | 10071 | default_argument |
a723baf1 MM |
10072 | = cp_parser_id_expression (parser, |
10073 | /*template_keyword_p=*/false, | |
10074 | /*check_dependency_p=*/true, | |
b0bc6e8e | 10075 | /*template_p=*/&is_template, |
fa6098f8 | 10076 | /*declarator_p=*/false, |
88525e1b | 10077 | /*optional_p=*/false); |
a3a503a5 GB |
10078 | if (TREE_CODE (default_argument) == TYPE_DECL) |
10079 | /* If the id-expression was a template-id that refers to | |
10080 | a template-class, we already have the declaration here, | |
10081 | so no further lookup is needed. */ | |
10082 | ; | |
10083 | else | |
10084 | /* Look up the name. */ | |
21526606 | 10085 | default_argument |
a3a503a5 | 10086 | = cp_parser_lookup_name (parser, default_argument, |
fc6a28d7 MM |
10087 | none_type, |
10088 | /*is_template=*/is_template, | |
10089 | /*is_namespace=*/false, | |
10090 | /*check_dependency=*/true, | |
2b7a3abf DS |
10091 | /*ambiguous_decls=*/NULL, |
10092 | token->location); | |
a723baf1 MM |
10093 | /* See if the default argument is valid. */ |
10094 | default_argument | |
10095 | = check_template_template_default_arg (default_argument); | |
5d80a306 DG |
10096 | |
10097 | /* Template parameter packs cannot have default | |
10098 | arguments. */ | |
10099 | if (*is_parameter_pack) | |
10100 | { | |
10101 | if (identifier) | |
69bc6bff MLI |
10102 | error_at (token->location, |
10103 | "template parameter pack %qD cannot " | |
10104 | "have a default argument", | |
10105 | identifier); | |
5d80a306 | 10106 | else |
69bc6bff MLI |
10107 | error_at (token->location, "template parameter packs cannot " |
10108 | "have default arguments"); | |
5d80a306 DG |
10109 | default_argument = NULL_TREE; |
10110 | } | |
6b648482 | 10111 | pop_deferring_access_checks (); |
a723baf1 MM |
10112 | } |
10113 | else | |
10114 | default_argument = NULL_TREE; | |
10115 | ||
10116 | /* Create the combined representation of the parameter and the | |
10117 | default argument. */ | |
71bd7186 | 10118 | parameter = build_tree_list (default_argument, parameter); |
a723baf1 MM |
10119 | } |
10120 | break; | |
10121 | ||
10122 | default: | |
71bd7186 MM |
10123 | gcc_unreachable (); |
10124 | break; | |
a723baf1 | 10125 | } |
21526606 | 10126 | |
a723baf1 MM |
10127 | return parameter; |
10128 | } | |
10129 | ||
10130 | /* Parse a template-id. | |
10131 | ||
10132 | template-id: | |
10133 | template-name < template-argument-list [opt] > | |
10134 | ||
10135 | If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the | |
10136 | `template' keyword. In this case, a TEMPLATE_ID_EXPR will be | |
10137 | returned. Otherwise, if the template-name names a function, or set | |
10138 | of functions, returns a TEMPLATE_ID_EXPR. If the template-name | |
21526606 | 10139 | names a class, returns a TYPE_DECL for the specialization. |
a723baf1 MM |
10140 | |
10141 | If CHECK_DEPENDENCY_P is FALSE, names are looked up in | |
10142 | uninstantiated templates. */ | |
10143 | ||
10144 | static tree | |
21526606 EC |
10145 | cp_parser_template_id (cp_parser *parser, |
10146 | bool template_keyword_p, | |
a668c6ad MM |
10147 | bool check_dependency_p, |
10148 | bool is_declaration) | |
a723baf1 | 10149 | { |
d6b418fa | 10150 | int i; |
be93747e | 10151 | tree templ; |
a723baf1 | 10152 | tree arguments; |
a723baf1 | 10153 | tree template_id; |
0c5e4866 | 10154 | cp_token_position start_of_id = 0; |
d6b418fa SM |
10155 | deferred_access_check *chk; |
10156 | VEC (deferred_access_check,gc) *access_check; | |
2b7a3abf | 10157 | cp_token *next_token = NULL, *next_token_2 = NULL, *token = NULL; |
a668c6ad | 10158 | bool is_identifier; |
a723baf1 MM |
10159 | |
10160 | /* If the next token corresponds to a template-id, there is no need | |
10161 | to reparse it. */ | |
2050a1bb MM |
10162 | next_token = cp_lexer_peek_token (parser->lexer); |
10163 | if (next_token->type == CPP_TEMPLATE_ID) | |
a723baf1 | 10164 | { |
d6b418fa | 10165 | struct tree_check *check_value; |
a723baf1 MM |
10166 | |
10167 | /* Get the stored value. */ | |
d6b418fa | 10168 | check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value; |
a723baf1 | 10169 | /* Perform any access checks that were deferred. */ |
d6b418fa SM |
10170 | access_check = check_value->checks; |
10171 | if (access_check) | |
10172 | { | |
10173 | for (i = 0 ; | |
10174 | VEC_iterate (deferred_access_check, access_check, i, chk) ; | |
10175 | ++i) | |
10176 | { | |
10177 | perform_or_defer_access_check (chk->binfo, | |
10178 | chk->decl, | |
10179 | chk->diag_decl); | |
10180 | } | |
10181 | } | |
a723baf1 | 10182 | /* Return the stored value. */ |
d6b418fa | 10183 | return check_value->value; |
a723baf1 MM |
10184 | } |
10185 | ||
2050a1bb MM |
10186 | /* Avoid performing name lookup if there is no possibility of |
10187 | finding a template-id. */ | |
10188 | if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR) | |
10189 | || (next_token->type == CPP_NAME | |
21526606 | 10190 | && !cp_parser_nth_token_starts_template_argument_list_p |
f4abade9 | 10191 | (parser, 2))) |
2050a1bb MM |
10192 | { |
10193 | cp_parser_error (parser, "expected template-id"); | |
10194 | return error_mark_node; | |
10195 | } | |
10196 | ||
a723baf1 | 10197 | /* Remember where the template-id starts. */ |
0b16f8f4 | 10198 | if (cp_parser_uncommitted_to_tentative_parse_p (parser)) |
0c5e4866 | 10199 | start_of_id = cp_lexer_token_position (parser->lexer, false); |
a723baf1 | 10200 | |
8d241e0b | 10201 | push_deferring_access_checks (dk_deferred); |
cf22909c | 10202 | |
a723baf1 | 10203 | /* Parse the template-name. */ |
a668c6ad | 10204 | is_identifier = false; |
2b7a3abf | 10205 | token = cp_lexer_peek_token (parser->lexer); |
be93747e KG |
10206 | templ = cp_parser_template_name (parser, template_keyword_p, |
10207 | check_dependency_p, | |
10208 | is_declaration, | |
10209 | &is_identifier); | |
10210 | if (templ == error_mark_node || is_identifier) | |
cf22909c KL |
10211 | { |
10212 | pop_deferring_access_checks (); | |
be93747e | 10213 | return templ; |
cf22909c | 10214 | } |
a723baf1 | 10215 | |
21526606 | 10216 | /* If we find the sequence `[:' after a template-name, it's probably |
f4abade9 GB |
10217 | a digraph-typo for `< ::'. Substitute the tokens and check if we can |
10218 | parse correctly the argument list. */ | |
2cfe82fe | 10219 | next_token = cp_lexer_peek_token (parser->lexer); |
f4abade9 | 10220 | next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2); |
21526606 | 10221 | if (next_token->type == CPP_OPEN_SQUARE |
f4abade9 | 10222 | && next_token->flags & DIGRAPH |
21526606 | 10223 | && next_token_2->type == CPP_COLON |
f4abade9 | 10224 | && !(next_token_2->flags & PREV_WHITE)) |
cf22909c | 10225 | { |
f4abade9 GB |
10226 | cp_parser_parse_tentatively (parser); |
10227 | /* Change `:' into `::'. */ | |
10228 | next_token_2->type = CPP_SCOPE; | |
10229 | /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is | |
0cbd7506 | 10230 | CPP_LESS. */ |
f4abade9 | 10231 | cp_lexer_consume_token (parser->lexer); |
2b7a3abf | 10232 | |
f4abade9 GB |
10233 | /* Parse the arguments. */ |
10234 | arguments = cp_parser_enclosed_template_argument_list (parser); | |
10235 | if (!cp_parser_parse_definitely (parser)) | |
10236 | { | |
10237 | /* If we couldn't parse an argument list, then we revert our changes | |
10238 | and return simply an error. Maybe this is not a template-id | |
10239 | after all. */ | |
10240 | next_token_2->type = CPP_COLON; | |
2a13a625 | 10241 | cp_parser_error (parser, "expected %<<%>"); |
f4abade9 GB |
10242 | pop_deferring_access_checks (); |
10243 | return error_mark_node; | |
10244 | } | |
10245 | /* Otherwise, emit an error about the invalid digraph, but continue | |
0cbd7506 | 10246 | parsing because we got our argument list. */ |
cbe5f3b3 MLI |
10247 | if (permerror (next_token->location, |
10248 | "%<<::%> cannot begin a template-argument list")) | |
71205d17 MLI |
10249 | { |
10250 | static bool hint = false; | |
1f5b3869 MLI |
10251 | inform (next_token->location, |
10252 | "%<<:%> is an alternate spelling for %<[%>." | |
10253 | " Insert whitespace between %<<%> and %<::%>"); | |
71205d17 | 10254 | if (!hint && !flag_permissive) |
f4abade9 | 10255 | { |
1f5b3869 MLI |
10256 | inform (next_token->location, "(if you use %<-fpermissive%>" |
10257 | " G++ will accept your code)"); | |
f4abade9 GB |
10258 | hint = true; |
10259 | } | |
10260 | } | |
10261 | } | |
10262 | else | |
10263 | { | |
10264 | /* Look for the `<' that starts the template-argument-list. */ | |
3a483371 | 10265 | if (!cp_parser_require (parser, CPP_LESS, "%<<%>")) |
f4abade9 GB |
10266 | { |
10267 | pop_deferring_access_checks (); | |
10268 | return error_mark_node; | |
10269 | } | |
10270 | /* Parse the arguments. */ | |
10271 | arguments = cp_parser_enclosed_template_argument_list (parser); | |
cf22909c | 10272 | } |
a723baf1 MM |
10273 | |
10274 | /* Build a representation of the specialization. */ | |
be93747e KG |
10275 | if (TREE_CODE (templ) == IDENTIFIER_NODE) |
10276 | template_id = build_min_nt (TEMPLATE_ID_EXPR, templ, arguments); | |
10277 | else if (DECL_CLASS_TEMPLATE_P (templ) | |
10278 | || DECL_TEMPLATE_TEMPLATE_PARM_P (templ)) | |
84019f23 MM |
10279 | { |
10280 | bool entering_scope; | |
10281 | /* In "template <typename T> ... A<T>::", A<T> is the abstract A | |
10282 | template (rather than some instantiation thereof) only if | |
10283 | is not nested within some other construct. For example, in | |
10284 | "template <typename T> void f(T) { A<T>::", A<T> is just an | |
10285 | instantiation of A. */ | |
10286 | entering_scope = (template_parm_scope_p () | |
10287 | && cp_lexer_next_token_is (parser->lexer, | |
10288 | CPP_SCOPE)); | |
10289 | template_id | |
be93747e | 10290 | = finish_template_type (templ, arguments, entering_scope); |
84019f23 | 10291 | } |
a723baf1 MM |
10292 | else |
10293 | { | |
10294 | /* If it's not a class-template or a template-template, it should be | |
10295 | a function-template. */ | |
be93747e KG |
10296 | gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ) |
10297 | || TREE_CODE (templ) == OVERLOAD | |
10298 | || BASELINK_P (templ))); | |
21526606 | 10299 | |
be93747e | 10300 | template_id = lookup_template_function (templ, arguments); |
a723baf1 | 10301 | } |
21526606 | 10302 | |
a723baf1 MM |
10303 | /* If parsing tentatively, replace the sequence of tokens that makes |
10304 | up the template-id with a CPP_TEMPLATE_ID token. That way, | |
10305 | should we re-parse the token stream, we will not have to repeat | |
10306 | the effort required to do the parse, nor will we issue duplicate | |
10307 | error messages about problems during instantiation of the | |
e894ab29 | 10308 | template. */ |
c8a7ed43 | 10309 | if (start_of_id) |
a723baf1 | 10310 | { |
0c5e4866 | 10311 | cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id); |
c8094d83 | 10312 | |
a723baf1 MM |
10313 | /* Reset the contents of the START_OF_ID token. */ |
10314 | token->type = CPP_TEMPLATE_ID; | |
d6b418fa SM |
10315 | /* Retrieve any deferred checks. Do not pop this access checks yet |
10316 | so the memory will not be reclaimed during token replacing below. */ | |
10317 | token->u.tree_check_value = GGC_CNEW (struct tree_check); | |
10318 | token->u.tree_check_value->value = template_id; | |
10319 | token->u.tree_check_value->checks = get_deferred_access_checks (); | |
a723baf1 | 10320 | token->keyword = RID_MAX; |
c8094d83 | 10321 | |
a723baf1 | 10322 | /* Purge all subsequent tokens. */ |
0c5e4866 | 10323 | cp_lexer_purge_tokens_after (parser->lexer, start_of_id); |
c8a7ed43 AO |
10324 | |
10325 | /* ??? Can we actually assume that, if template_id == | |
10326 | error_mark_node, we will have issued a diagnostic to the | |
10327 | user, as opposed to simply marking the tentative parse as | |
10328 | failed? */ | |
10329 | if (cp_parser_error_occurred (parser) && template_id != error_mark_node) | |
69bc6bff | 10330 | error_at (token->location, "parse error in template argument list"); |
a723baf1 MM |
10331 | } |
10332 | ||
cf22909c | 10333 | pop_deferring_access_checks (); |
a723baf1 MM |
10334 | return template_id; |
10335 | } | |
10336 | ||
10337 | /* Parse a template-name. | |
10338 | ||
10339 | template-name: | |
10340 | identifier | |
21526606 | 10341 | |
a723baf1 MM |
10342 | The standard should actually say: |
10343 | ||
10344 | template-name: | |
10345 | identifier | |
10346 | operator-function-id | |
a723baf1 MM |
10347 | |
10348 | A defect report has been filed about this issue. | |
10349 | ||
0d956474 GB |
10350 | A conversion-function-id cannot be a template name because they cannot |
10351 | be part of a template-id. In fact, looking at this code: | |
10352 | ||
10353 | a.operator K<int>() | |
10354 | ||
10355 | the conversion-function-id is "operator K<int>", and K<int> is a type-id. | |
21526606 | 10356 | It is impossible to call a templated conversion-function-id with an |
0d956474 GB |
10357 | explicit argument list, since the only allowed template parameter is |
10358 | the type to which it is converting. | |
10359 | ||
a723baf1 MM |
10360 | If TEMPLATE_KEYWORD_P is true, then we have just seen the |
10361 | `template' keyword, in a construction like: | |
10362 | ||
10363 | T::template f<3>() | |
10364 | ||
10365 | In that case `f' is taken to be a template-name, even though there | |
10366 | is no way of knowing for sure. | |
10367 | ||
10368 | Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the | |
10369 | name refers to a set of overloaded functions, at least one of which | |
10370 | is a template, or an IDENTIFIER_NODE with the name of the template, | |
10371 | if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE, | |
10372 | names are looked up inside uninstantiated templates. */ | |
10373 | ||
10374 | static tree | |
21526606 | 10375 | cp_parser_template_name (cp_parser* parser, |
0cbd7506 MS |
10376 | bool template_keyword_p, |
10377 | bool check_dependency_p, | |
a668c6ad MM |
10378 | bool is_declaration, |
10379 | bool *is_identifier) | |
a723baf1 MM |
10380 | { |
10381 | tree identifier; | |
10382 | tree decl; | |
10383 | tree fns; | |
2b7a3abf | 10384 | cp_token *token = cp_lexer_peek_token (parser->lexer); |
a723baf1 MM |
10385 | |
10386 | /* If the next token is `operator', then we have either an | |
10387 | operator-function-id or a conversion-function-id. */ | |
10388 | if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR)) | |
10389 | { | |
10390 | /* We don't know whether we're looking at an | |
10391 | operator-function-id or a conversion-function-id. */ | |
10392 | cp_parser_parse_tentatively (parser); | |
10393 | /* Try an operator-function-id. */ | |
10394 | identifier = cp_parser_operator_function_id (parser); | |
10395 | /* If that didn't work, try a conversion-function-id. */ | |
10396 | if (!cp_parser_parse_definitely (parser)) | |
0cbd7506 | 10397 | { |
0d956474 GB |
10398 | cp_parser_error (parser, "expected template-name"); |
10399 | return error_mark_node; | |
0cbd7506 | 10400 | } |
a723baf1 MM |
10401 | } |
10402 | /* Look for the identifier. */ | |
10403 | else | |
10404 | identifier = cp_parser_identifier (parser); | |
21526606 | 10405 | |
a723baf1 MM |
10406 | /* If we didn't find an identifier, we don't have a template-id. */ |
10407 | if (identifier == error_mark_node) | |
10408 | return error_mark_node; | |
10409 | ||
10410 | /* If the name immediately followed the `template' keyword, then it | |
10411 | is a template-name. However, if the next token is not `<', then | |
10412 | we do not treat it as a template-name, since it is not being used | |
10413 | as part of a template-id. This enables us to handle constructs | |
10414 | like: | |
10415 | ||
10416 | template <typename T> struct S { S(); }; | |
10417 | template <typename T> S<T>::S(); | |
10418 | ||
10419 | correctly. We would treat `S' as a template -- if it were `S<T>' | |
10420 | -- but we do not if there is no `<'. */ | |
a668c6ad MM |
10421 | |
10422 | if (processing_template_decl | |
f4abade9 | 10423 | && cp_parser_nth_token_starts_template_argument_list_p (parser, 1)) |
a668c6ad MM |
10424 | { |
10425 | /* In a declaration, in a dependent context, we pretend that the | |
10426 | "template" keyword was present in order to improve error | |
10427 | recovery. For example, given: | |
21526606 | 10428 | |
a668c6ad | 10429 | template <typename T> void f(T::X<int>); |
21526606 | 10430 | |
a668c6ad | 10431 | we want to treat "X<int>" as a template-id. */ |
21526606 EC |
10432 | if (is_declaration |
10433 | && !template_keyword_p | |
a668c6ad | 10434 | && parser->scope && TYPE_P (parser->scope) |
a52eb3bc | 10435 | && check_dependency_p |
1cb801bc | 10436 | && dependent_scope_p (parser->scope) |
4e0f4df5 GB |
10437 | /* Do not do this for dtors (or ctors), since they never |
10438 | need the template keyword before their name. */ | |
10439 | && !constructor_name_p (identifier, parser->scope)) | |
a668c6ad | 10440 | { |
0c5e4866 | 10441 | cp_token_position start = 0; |
c8094d83 | 10442 | |
a668c6ad | 10443 | /* Explain what went wrong. */ |
69bc6bff MLI |
10444 | error_at (token->location, "non-template %qD used as template", |
10445 | identifier); | |
10446 | inform (token->location, "use %<%T::template %D%> to indicate that it is a template", | |
4e0f4df5 | 10447 | parser->scope, identifier); |
0b16f8f4 VR |
10448 | /* If parsing tentatively, find the location of the "<" token. */ |
10449 | if (cp_parser_simulate_error (parser)) | |
10450 | start = cp_lexer_token_position (parser->lexer, true); | |
a668c6ad MM |
10451 | /* Parse the template arguments so that we can issue error |
10452 | messages about them. */ | |
10453 | cp_lexer_consume_token (parser->lexer); | |
10454 | cp_parser_enclosed_template_argument_list (parser); | |
10455 | /* Skip tokens until we find a good place from which to | |
10456 | continue parsing. */ | |
10457 | cp_parser_skip_to_closing_parenthesis (parser, | |
10458 | /*recovering=*/true, | |
10459 | /*or_comma=*/true, | |
10460 | /*consume_paren=*/false); | |
10461 | /* If parsing tentatively, permanently remove the | |
10462 | template argument list. That will prevent duplicate | |
10463 | error messages from being issued about the missing | |
10464 | "template" keyword. */ | |
0c5e4866 NS |
10465 | if (start) |
10466 | cp_lexer_purge_tokens_after (parser->lexer, start); | |
a668c6ad MM |
10467 | if (is_identifier) |
10468 | *is_identifier = true; | |
10469 | return identifier; | |
10470 | } | |
9d363a56 MM |
10471 | |
10472 | /* If the "template" keyword is present, then there is generally | |
10473 | no point in doing name-lookup, so we just return IDENTIFIER. | |
10474 | But, if the qualifying scope is non-dependent then we can | |
10475 | (and must) do name-lookup normally. */ | |
10476 | if (template_keyword_p | |
10477 | && (!parser->scope | |
98ca843c | 10478 | || (TYPE_P (parser->scope) |
9d363a56 | 10479 | && dependent_type_p (parser->scope)))) |
a668c6ad MM |
10480 | return identifier; |
10481 | } | |
a723baf1 MM |
10482 | |
10483 | /* Look up the name. */ | |
10484 | decl = cp_parser_lookup_name (parser, identifier, | |
fc6a28d7 | 10485 | none_type, |
b0bc6e8e | 10486 | /*is_template=*/false, |
eea9800f | 10487 | /*is_namespace=*/false, |
8f78f01f | 10488 | check_dependency_p, |
2b7a3abf DS |
10489 | /*ambiguous_decls=*/NULL, |
10490 | token->location); | |
a723baf1 MM |
10491 | decl = maybe_get_template_decl_from_type_decl (decl); |
10492 | ||
10493 | /* If DECL is a template, then the name was a template-name. */ | |
10494 | if (TREE_CODE (decl) == TEMPLATE_DECL) | |
10495 | ; | |
21526606 | 10496 | else |
a723baf1 | 10497 | { |
d58a2b83 MM |
10498 | tree fn = NULL_TREE; |
10499 | ||
a723baf1 MM |
10500 | /* The standard does not explicitly indicate whether a name that |
10501 | names a set of overloaded declarations, some of which are | |
10502 | templates, is a template-name. However, such a name should | |
10503 | be a template-name; otherwise, there is no way to form a | |
10504 | template-id for the overloaded templates. */ | |
10505 | fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl; | |
10506 | if (TREE_CODE (fns) == OVERLOAD) | |
d58a2b83 MM |
10507 | for (fn = fns; fn; fn = OVL_NEXT (fn)) |
10508 | if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL) | |
10509 | break; | |
21526606 | 10510 | |
d58a2b83 | 10511 | if (!fn) |
a723baf1 | 10512 | { |
d58a2b83 | 10513 | /* The name does not name a template. */ |
a723baf1 MM |
10514 | cp_parser_error (parser, "expected template-name"); |
10515 | return error_mark_node; | |
10516 | } | |
10517 | } | |
10518 | ||
10519 | /* If DECL is dependent, and refers to a function, then just return | |
10520 | its name; we will look it up again during template instantiation. */ | |
10521 | if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl)) | |
10522 | { | |
10523 | tree scope = CP_DECL_CONTEXT (get_first_fn (decl)); | |
1fb3244a | 10524 | if (TYPE_P (scope) && dependent_type_p (scope)) |
a723baf1 MM |
10525 | return identifier; |
10526 | } | |
10527 | ||
10528 | return decl; | |
10529 | } | |
10530 | ||
10531 | /* Parse a template-argument-list. | |
10532 | ||
10533 | template-argument-list: | |
5d80a306 DG |
10534 | template-argument ... [opt] |
10535 | template-argument-list , template-argument ... [opt] | |
a723baf1 | 10536 | |
04c06002 | 10537 | Returns a TREE_VEC containing the arguments. */ |
a723baf1 MM |
10538 | |
10539 | static tree | |
94edc4ab | 10540 | cp_parser_template_argument_list (cp_parser* parser) |
a723baf1 | 10541 | { |
bf12d54d NS |
10542 | tree fixed_args[10]; |
10543 | unsigned n_args = 0; | |
10544 | unsigned alloced = 10; | |
10545 | tree *arg_ary = fixed_args; | |
10546 | tree vec; | |
4bb8ca28 | 10547 | bool saved_in_template_argument_list_p; |
5e9edb0f MM |
10548 | bool saved_ice_p; |
10549 | bool saved_non_ice_p; | |
a723baf1 | 10550 | |
4bb8ca28 MM |
10551 | saved_in_template_argument_list_p = parser->in_template_argument_list_p; |
10552 | parser->in_template_argument_list_p = true; | |
5e9edb0f | 10553 | /* Even if the template-id appears in an integral |
3db45ab5 MS |
10554 | constant-expression, the contents of the argument list do |
10555 | not. */ | |
5e9edb0f MM |
10556 | saved_ice_p = parser->integral_constant_expression_p; |
10557 | parser->integral_constant_expression_p = false; | |
10558 | saved_non_ice_p = parser->non_integral_constant_expression_p; | |
10559 | parser->non_integral_constant_expression_p = false; | |
02ed62dd | 10560 | /* Parse the arguments. */ |
bf12d54d | 10561 | do |
a723baf1 MM |
10562 | { |
10563 | tree argument; | |
10564 | ||
bf12d54d | 10565 | if (n_args) |
04c06002 | 10566 | /* Consume the comma. */ |
bf12d54d | 10567 | cp_lexer_consume_token (parser->lexer); |
21526606 | 10568 | |
a723baf1 MM |
10569 | /* Parse the template-argument. */ |
10570 | argument = cp_parser_template_argument (parser); | |
5d80a306 DG |
10571 | |
10572 | /* If the next token is an ellipsis, we're expanding a template | |
10573 | argument pack. */ | |
10574 | if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)) | |
10575 | { | |
a58f7da1 DS |
10576 | if (argument == error_mark_node) |
10577 | { | |
10578 | cp_token *token = cp_lexer_peek_token (parser->lexer); | |
69bc6bff MLI |
10579 | error_at (token->location, |
10580 | "expected parameter pack before %<...%>"); | |
a58f7da1 | 10581 | } |
5d80a306 DG |
10582 | /* Consume the `...' token. */ |
10583 | cp_lexer_consume_token (parser->lexer); | |
10584 | ||
10585 | /* Make the argument into a TYPE_PACK_EXPANSION or | |
10586 | EXPR_PACK_EXPANSION. */ | |
10587 | argument = make_pack_expansion (argument); | |
10588 | } | |
10589 | ||
bf12d54d NS |
10590 | if (n_args == alloced) |
10591 | { | |
10592 | alloced *= 2; | |
21526606 | 10593 | |
bf12d54d NS |
10594 | if (arg_ary == fixed_args) |
10595 | { | |
0ac1b889 | 10596 | arg_ary = XNEWVEC (tree, alloced); |
bf12d54d NS |
10597 | memcpy (arg_ary, fixed_args, sizeof (tree) * n_args); |
10598 | } | |
10599 | else | |
7767580e | 10600 | arg_ary = XRESIZEVEC (tree, arg_ary, alloced); |
bf12d54d NS |
10601 | } |
10602 | arg_ary[n_args++] = argument; | |
a723baf1 | 10603 | } |
bf12d54d NS |
10604 | while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)); |
10605 | ||
10606 | vec = make_tree_vec (n_args); | |
a723baf1 | 10607 | |
bf12d54d NS |
10608 | while (n_args--) |
10609 | TREE_VEC_ELT (vec, n_args) = arg_ary[n_args]; | |
21526606 | 10610 | |
bf12d54d NS |
10611 | if (arg_ary != fixed_args) |
10612 | free (arg_ary); | |
5e9edb0f MM |
10613 | parser->non_integral_constant_expression_p = saved_non_ice_p; |
10614 | parser->integral_constant_expression_p = saved_ice_p; | |
4bb8ca28 | 10615 | parser->in_template_argument_list_p = saved_in_template_argument_list_p; |
bf12d54d | 10616 | return vec; |
a723baf1 MM |
10617 | } |
10618 | ||
10619 | /* Parse a template-argument. | |
10620 | ||
10621 | template-argument: | |
10622 | assignment-expression | |
10623 | type-id | |
10624 | id-expression | |
10625 | ||
10626 | The representation is that of an assignment-expression, type-id, or | |
10627 | id-expression -- except that the qualified id-expression is | |
10628 | evaluated, so that the value returned is either a DECL or an | |
21526606 | 10629 | OVERLOAD. |
d17811fd MM |
10630 | |
10631 | Although the standard says "assignment-expression", it forbids | |
10632 | throw-expressions or assignments in the template argument. | |
10633 | Therefore, we use "conditional-expression" instead. */ | |
a723baf1 MM |
10634 | |
10635 | static tree | |
94edc4ab | 10636 | cp_parser_template_argument (cp_parser* parser) |
a723baf1 MM |
10637 | { |
10638 | tree argument; | |
10639 | bool template_p; | |
d17811fd | 10640 | bool address_p; |
4d5297fa | 10641 | bool maybe_type_id = false; |
2b7a3abf | 10642 | cp_token *token = NULL, *argument_start_token = NULL; |
b3445994 | 10643 | cp_id_kind idk; |
a723baf1 MM |
10644 | |
10645 | /* There's really no way to know what we're looking at, so we just | |
21526606 | 10646 | try each alternative in order. |
a723baf1 MM |
10647 | |
10648 | [temp.arg] | |
10649 | ||
10650 | In a template-argument, an ambiguity between a type-id and an | |
10651 | expression is resolved to a type-id, regardless of the form of | |
21526606 | 10652 | the corresponding template-parameter. |
a723baf1 MM |
10653 | |
10654 | Therefore, we try a type-id first. */ | |
10655 | cp_parser_parse_tentatively (parser); | |
0d9c0892 | 10656 | argument = cp_parser_template_type_arg (parser); |
865a3a42 DG |
10657 | /* If there was no error parsing the type-id but the next token is a |
10658 | '>>', our behavior depends on which dialect of C++ we're | |
10659 | parsing. In C++98, we probably found a typo for '> >'. But there | |
10660 | are type-id which are also valid expressions. For instance: | |
4d5297fa GB |
10661 | |
10662 | struct X { int operator >> (int); }; | |
10663 | template <int V> struct Foo {}; | |
10664 | Foo<X () >> 5> r; | |
10665 | ||
10666 | Here 'X()' is a valid type-id of a function type, but the user just | |
10667 | wanted to write the expression "X() >> 5". Thus, we remember that we | |
10668 | found a valid type-id, but we still try to parse the argument as an | |
865a3a42 DG |
10669 | expression to see what happens. |
10670 | ||
10671 | In C++0x, the '>>' will be considered two separate '>' | |
10672 | tokens. */ | |
4d5297fa | 10673 | if (!cp_parser_error_occurred (parser) |
865a3a42 | 10674 | && cxx_dialect == cxx98 |
4d5297fa GB |
10675 | && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT)) |
10676 | { | |
10677 | maybe_type_id = true; | |
10678 | cp_parser_abort_tentative_parse (parser); | |
10679 | } | |
10680 | else | |
10681 | { | |
10682 | /* If the next token isn't a `,' or a `>', then this argument wasn't | |
10683 | really finished. This means that the argument is not a valid | |
10684 | type-id. */ | |
10685 | if (!cp_parser_next_token_ends_template_argument_p (parser)) | |
10686 | cp_parser_error (parser, "expected template-argument"); | |
10687 | /* If that worked, we're done. */ | |
10688 | if (cp_parser_parse_definitely (parser)) | |
10689 | return argument; | |
10690 | } | |
a723baf1 MM |
10691 | /* We're still not sure what the argument will be. */ |
10692 | cp_parser_parse_tentatively (parser); | |
10693 | /* Try a template. */ | |
2b7a3abf | 10694 | argument_start_token = cp_lexer_peek_token (parser->lexer); |
21526606 | 10695 | argument = cp_parser_id_expression (parser, |
a723baf1 MM |
10696 | /*template_keyword_p=*/false, |
10697 | /*check_dependency_p=*/true, | |
f3c2dfc6 | 10698 | &template_p, |
fa6098f8 | 10699 | /*declarator_p=*/false, |
88525e1b | 10700 | /*optional_p=*/false); |
a723baf1 MM |
10701 | /* If the next token isn't a `,' or a `>', then this argument wasn't |
10702 | really finished. */ | |
d17811fd | 10703 | if (!cp_parser_next_token_ends_template_argument_p (parser)) |
a723baf1 MM |
10704 | cp_parser_error (parser, "expected template-argument"); |
10705 | if (!cp_parser_error_occurred (parser)) | |
10706 | { | |
f746161e MM |
10707 | /* Figure out what is being referred to. If the id-expression |
10708 | was for a class template specialization, then we will have a | |
10709 | TYPE_DECL at this point. There is no need to do name lookup | |
10710 | at this point in that case. */ | |
10711 | if (TREE_CODE (argument) != TYPE_DECL) | |
10712 | argument = cp_parser_lookup_name (parser, argument, | |
fc6a28d7 | 10713 | none_type, |
f746161e MM |
10714 | /*is_template=*/template_p, |
10715 | /*is_namespace=*/false, | |
8f78f01f | 10716 | /*check_dependency=*/true, |
2b7a3abf DS |
10717 | /*ambiguous_decls=*/NULL, |
10718 | argument_start_token->location); | |
5b4acce1 KL |
10719 | if (TREE_CODE (argument) != TEMPLATE_DECL |
10720 | && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE) | |
a723baf1 MM |
10721 | cp_parser_error (parser, "expected template-name"); |
10722 | } | |
10723 | if (cp_parser_parse_definitely (parser)) | |
10724 | return argument; | |
d17811fd MM |
10725 | /* It must be a non-type argument. There permitted cases are given |
10726 | in [temp.arg.nontype]: | |
10727 | ||
10728 | -- an integral constant-expression of integral or enumeration | |
0cbd7506 | 10729 | type; or |
d17811fd MM |
10730 | |
10731 | -- the name of a non-type template-parameter; or | |
10732 | ||
10733 | -- the name of an object or function with external linkage... | |
10734 | ||
10735 | -- the address of an object or function with external linkage... | |
10736 | ||
04c06002 | 10737 | -- a pointer to member... */ |
d17811fd MM |
10738 | /* Look for a non-type template parameter. */ |
10739 | if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)) | |
10740 | { | |
10741 | cp_parser_parse_tentatively (parser); | |
10742 | argument = cp_parser_primary_expression (parser, | |
39a13be5 | 10743 | /*address_p=*/false, |
93678513 | 10744 | /*cast_p=*/false, |
02ed62dd MM |
10745 | /*template_arg_p=*/true, |
10746 | &idk); | |
d17811fd MM |
10747 | if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX |
10748 | || !cp_parser_next_token_ends_template_argument_p (parser)) | |
10749 | cp_parser_simulate_error (parser); | |
10750 | if (cp_parser_parse_definitely (parser)) | |
10751 | return argument; | |
10752 | } | |
db24eb1f | 10753 | |
d17811fd MM |
10754 | /* If the next token is "&", the argument must be the address of an |
10755 | object or function with external linkage. */ | |
10756 | address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND); | |
10757 | if (address_p) | |
10758 | cp_lexer_consume_token (parser->lexer); | |
10759 | /* See if we might have an id-expression. */ | |
10760 | token = cp_lexer_peek_token (parser->lexer); | |
10761 | if (token->type == CPP_NAME | |
10762 | || token->keyword == RID_OPERATOR | |
10763 | || token->type == CPP_SCOPE | |
10764 | || token->type == CPP_TEMPLATE_ID | |
10765 | || token->type == CPP_NESTED_NAME_SPECIFIER) | |
10766 | { | |
10767 | cp_parser_parse_tentatively (parser); | |
10768 | argument = cp_parser_primary_expression (parser, | |
02ed62dd | 10769 | address_p, |
93678513 | 10770 | /*cast_p=*/false, |
02ed62dd MM |
10771 | /*template_arg_p=*/true, |
10772 | &idk); | |
d17811fd MM |
10773 | if (cp_parser_error_occurred (parser) |
10774 | || !cp_parser_next_token_ends_template_argument_p (parser)) | |
10775 | cp_parser_abort_tentative_parse (parser); | |
10776 | else | |
10777 | { | |
db24eb1f NS |
10778 | if (TREE_CODE (argument) == INDIRECT_REF) |
10779 | { | |
10780 | gcc_assert (REFERENCE_REF_P (argument)); | |
10781 | argument = TREE_OPERAND (argument, 0); | |
10782 | } | |
c8094d83 | 10783 | |
d17811fd MM |
10784 | if (TREE_CODE (argument) == VAR_DECL) |
10785 | { | |
10786 | /* A variable without external linkage might still be a | |
10787 | valid constant-expression, so no error is issued here | |
10788 | if the external-linkage check fails. */ | |
1082fd10 | 10789 | if (!address_p && !DECL_EXTERNAL_LINKAGE_P (argument)) |
d17811fd MM |
10790 | cp_parser_simulate_error (parser); |
10791 | } | |
10792 | else if (is_overloaded_fn (argument)) | |
10793 | /* All overloaded functions are allowed; if the external | |
10794 | linkage test does not pass, an error will be issued | |
10795 | later. */ | |
10796 | ; | |
10797 | else if (address_p | |
21526606 | 10798 | && (TREE_CODE (argument) == OFFSET_REF |
d17811fd MM |
10799 | || TREE_CODE (argument) == SCOPE_REF)) |
10800 | /* A pointer-to-member. */ | |
10801 | ; | |
db24eb1f NS |
10802 | else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX) |
10803 | ; | |
d17811fd MM |
10804 | else |
10805 | cp_parser_simulate_error (parser); | |
10806 | ||
10807 | if (cp_parser_parse_definitely (parser)) | |
10808 | { | |
10809 | if (address_p) | |
5ade1ed2 DG |
10810 | argument = build_x_unary_op (ADDR_EXPR, argument, |
10811 | tf_warning_or_error); | |
d17811fd MM |
10812 | return argument; |
10813 | } | |
10814 | } | |
10815 | } | |
10816 | /* If the argument started with "&", there are no other valid | |
10817 | alternatives at this point. */ | |
10818 | if (address_p) | |
10819 | { | |
10820 | cp_parser_error (parser, "invalid non-type template argument"); | |
10821 | return error_mark_node; | |
10822 | } | |
db24eb1f | 10823 | |
4d5297fa | 10824 | /* If the argument wasn't successfully parsed as a type-id followed |
21526606 | 10825 | by '>>', the argument can only be a constant expression now. |
4d5297fa GB |
10826 | Otherwise, we try parsing the constant-expression tentatively, |
10827 | because the argument could really be a type-id. */ | |
10828 | if (maybe_type_id) | |
10829 | cp_parser_parse_tentatively (parser); | |
21526606 | 10830 | argument = cp_parser_constant_expression (parser, |
d17811fd MM |
10831 | /*allow_non_constant_p=*/false, |
10832 | /*non_constant_p=*/NULL); | |
9baa27a9 | 10833 | argument = fold_non_dependent_expr (argument); |
4d5297fa GB |
10834 | if (!maybe_type_id) |
10835 | return argument; | |
10836 | if (!cp_parser_next_token_ends_template_argument_p (parser)) | |
10837 | cp_parser_error (parser, "expected template-argument"); | |
10838 | if (cp_parser_parse_definitely (parser)) | |
10839 | return argument; | |
10840 | /* We did our best to parse the argument as a non type-id, but that | |
10841 | was the only alternative that matched (albeit with a '>' after | |
21526606 | 10842 | it). We can assume it's just a typo from the user, and a |
4d5297fa | 10843 | diagnostic will then be issued. */ |
0d9c0892 | 10844 | return cp_parser_template_type_arg (parser); |
a723baf1 MM |
10845 | } |
10846 | ||
10847 | /* Parse an explicit-instantiation. | |
10848 | ||
10849 | explicit-instantiation: | |
21526606 | 10850 | template declaration |
a723baf1 MM |
10851 | |
10852 | Although the standard says `declaration', what it really means is: | |
10853 | ||
10854 | explicit-instantiation: | |
21526606 | 10855 | template decl-specifier-seq [opt] declarator [opt] ; |
a723baf1 MM |
10856 | |
10857 | Things like `template int S<int>::i = 5, int S<double>::j;' are not | |
10858 | supposed to be allowed. A defect report has been filed about this | |
21526606 | 10859 | issue. |
a723baf1 MM |
10860 | |
10861 | GNU Extension: | |
21526606 | 10862 | |
a723baf1 | 10863 | explicit-instantiation: |
21526606 | 10864 | storage-class-specifier template |
a723baf1 | 10865 | decl-specifier-seq [opt] declarator [opt] ; |
21526606 | 10866 | function-specifier template |
a723baf1 MM |
10867 | decl-specifier-seq [opt] declarator [opt] ; */ |
10868 | ||
10869 | static void | |
94edc4ab | 10870 | cp_parser_explicit_instantiation (cp_parser* parser) |
a723baf1 | 10871 | { |
560ad596 | 10872 | int declares_class_or_enum; |
62d1db17 | 10873 | cp_decl_specifier_seq decl_specifiers; |
a723baf1 | 10874 | tree extension_specifier = NULL_TREE; |
760b18ad | 10875 | cp_token *token; |
a723baf1 MM |
10876 | |
10877 | /* Look for an (optional) storage-class-specifier or | |
10878 | function-specifier. */ | |
10879 | if (cp_parser_allow_gnu_extensions_p (parser)) | |
10880 | { | |
21526606 | 10881 | extension_specifier |
a723baf1 MM |
10882 | = cp_parser_storage_class_specifier_opt (parser); |
10883 | if (!extension_specifier) | |
98ca843c | 10884 | extension_specifier |
62d1db17 MM |
10885 | = cp_parser_function_specifier_opt (parser, |
10886 | /*decl_specs=*/NULL); | |
a723baf1 MM |
10887 | } |
10888 | ||
10889 | /* Look for the `template' keyword. */ | |
3a483371 | 10890 | cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>"); |
a723baf1 MM |
10891 | /* Let the front end know that we are processing an explicit |
10892 | instantiation. */ | |
10893 | begin_explicit_instantiation (); | |
10894 | /* [temp.explicit] says that we are supposed to ignore access | |
10895 | control while processing explicit instantiation directives. */ | |
78757caa | 10896 | push_deferring_access_checks (dk_no_check); |
a723baf1 | 10897 | /* Parse a decl-specifier-seq. */ |
760b18ad | 10898 | token = cp_lexer_peek_token (parser->lexer); |
62d1db17 MM |
10899 | cp_parser_decl_specifier_seq (parser, |
10900 | CP_PARSER_FLAGS_OPTIONAL, | |
10901 | &decl_specifiers, | |
10902 | &declares_class_or_enum); | |
a723baf1 MM |
10903 | /* If there was exactly one decl-specifier, and it declared a class, |
10904 | and there's no declarator, then we have an explicit type | |
10905 | instantiation. */ | |
10906 | if (declares_class_or_enum && cp_parser_declares_only_class_p (parser)) | |
10907 | { | |
10908 | tree type; | |
10909 | ||
62d1db17 | 10910 | type = check_tag_decl (&decl_specifiers); |
b7fc8b57 KL |
10911 | /* Turn access control back on for names used during |
10912 | template instantiation. */ | |
10913 | pop_deferring_access_checks (); | |
a723baf1 | 10914 | if (type) |
8da15291 | 10915 | do_type_instantiation (type, extension_specifier, |
3db45ab5 | 10916 | /*complain=*/tf_error); |
a723baf1 MM |
10917 | } |
10918 | else | |
10919 | { | |
058b15c1 | 10920 | cp_declarator *declarator; |
a723baf1 MM |
10921 | tree decl; |
10922 | ||
10923 | /* Parse the declarator. */ | |
21526606 | 10924 | declarator |
62b8a44e | 10925 | = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED, |
4bb8ca28 | 10926 | /*ctor_dtor_or_conv_p=*/NULL, |
db86dd14 MM |
10927 | /*parenthesized_p=*/NULL, |
10928 | /*member_p=*/false); | |
fc6a28d7 MM |
10929 | if (declares_class_or_enum & 2) |
10930 | cp_parser_check_for_definition_in_return_type (declarator, | |
760b18ad DS |
10931 | decl_specifiers.type, |
10932 | decl_specifiers.type_location); | |
058b15c1 | 10933 | if (declarator != cp_error_declarator) |
216bb6e1 | 10934 | { |
62d1db17 | 10935 | decl = grokdeclarator (declarator, &decl_specifiers, |
b9e75696 | 10936 | NORMAL, 0, &decl_specifiers.attributes); |
216bb6e1 MM |
10937 | /* Turn access control back on for names used during |
10938 | template instantiation. */ | |
10939 | pop_deferring_access_checks (); | |
10940 | /* Do the explicit instantiation. */ | |
10941 | do_decl_instantiation (decl, extension_specifier); | |
10942 | } | |
10943 | else | |
10944 | { | |
10945 | pop_deferring_access_checks (); | |
10946 | /* Skip the body of the explicit instantiation. */ | |
10947 | cp_parser_skip_to_end_of_statement (parser); | |
10948 | } | |
a723baf1 MM |
10949 | } |
10950 | /* We're done with the instantiation. */ | |
10951 | end_explicit_instantiation (); | |
a723baf1 | 10952 | |
e0860732 | 10953 | cp_parser_consume_semicolon_at_end_of_statement (parser); |
a723baf1 MM |
10954 | } |
10955 | ||
10956 | /* Parse an explicit-specialization. | |
10957 | ||
10958 | explicit-specialization: | |
21526606 | 10959 | template < > declaration |
a723baf1 MM |
10960 | |
10961 | Although the standard says `declaration', what it really means is: | |
10962 | ||
10963 | explicit-specialization: | |
10964 | template <> decl-specifier [opt] init-declarator [opt] ; | |
21526606 | 10965 | template <> function-definition |
a723baf1 MM |
10966 | template <> explicit-specialization |
10967 | template <> template-declaration */ | |
10968 | ||
10969 | static void | |
94edc4ab | 10970 | cp_parser_explicit_specialization (cp_parser* parser) |
a723baf1 | 10971 | { |
2f1b1731 | 10972 | bool need_lang_pop; |
2b7a3abf DS |
10973 | cp_token *token = cp_lexer_peek_token (parser->lexer); |
10974 | ||
a723baf1 | 10975 | /* Look for the `template' keyword. */ |
3a483371 | 10976 | cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>"); |
a723baf1 | 10977 | /* Look for the `<'. */ |
3a483371 | 10978 | cp_parser_require (parser, CPP_LESS, "%<<%>"); |
a723baf1 | 10979 | /* Look for the `>'. */ |
3a483371 | 10980 | cp_parser_require (parser, CPP_GREATER, "%<>%>"); |
a723baf1 MM |
10981 | /* We have processed another parameter list. */ |
10982 | ++parser->num_template_parameter_lists; | |
2f1b1731 | 10983 | /* [temp] |
3db45ab5 | 10984 | |
2f1b1731 | 10985 | A template ... explicit specialization ... shall not have C |
3db45ab5 | 10986 | linkage. */ |
2f1b1731 MM |
10987 | if (current_lang_name == lang_name_c) |
10988 | { | |
69bc6bff | 10989 | error_at (token->location, "template specialization with C linkage"); |
2f1b1731 MM |
10990 | /* Give it C++ linkage to avoid confusing other parts of the |
10991 | front end. */ | |
10992 | push_lang_context (lang_name_cplusplus); | |
10993 | need_lang_pop = true; | |
10994 | } | |
10995 | else | |
10996 | need_lang_pop = false; | |
a723baf1 | 10997 | /* Let the front end know that we are beginning a specialization. */ |
9b910171 LM |
10998 | if (!begin_specialization ()) |
10999 | { | |
11000 | end_specialization (); | |
9b910171 LM |
11001 | return; |
11002 | } | |
11003 | ||
a723baf1 MM |
11004 | /* If the next keyword is `template', we need to figure out whether |
11005 | or not we're looking a template-declaration. */ | |
11006 | if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE)) | |
11007 | { | |
11008 | if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS | |
11009 | && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER) | |
11010 | cp_parser_template_declaration_after_export (parser, | |
11011 | /*member_p=*/false); | |
11012 | else | |
11013 | cp_parser_explicit_specialization (parser); | |
11014 | } | |
11015 | else | |
11016 | /* Parse the dependent declaration. */ | |
21526606 | 11017 | cp_parser_single_declaration (parser, |
d6b418fa | 11018 | /*checks=*/NULL, |
a723baf1 | 11019 | /*member_p=*/false, |
736b8100 | 11020 | /*explicit_specialization_p=*/true, |
a723baf1 | 11021 | /*friend_p=*/NULL); |
a723baf1 MM |
11022 | /* We're done with the specialization. */ |
11023 | end_specialization (); | |
2f1b1731 MM |
11024 | /* For the erroneous case of a template with C linkage, we pushed an |
11025 | implicit C++ linkage scope; exit that scope now. */ | |
11026 | if (need_lang_pop) | |
11027 | pop_lang_context (); | |
a723baf1 MM |
11028 | /* We're done with this parameter list. */ |
11029 | --parser->num_template_parameter_lists; | |
11030 | } | |
11031 | ||
11032 | /* Parse a type-specifier. | |
11033 | ||
11034 | type-specifier: | |
11035 | simple-type-specifier | |
11036 | class-specifier | |
11037 | enum-specifier | |
11038 | elaborated-type-specifier | |
11039 | cv-qualifier | |
11040 | ||
11041 | GNU Extension: | |
11042 | ||
11043 | type-specifier: | |
11044 | __complex__ | |
11045 | ||
62d1db17 MM |
11046 | Returns a representation of the type-specifier. For a |
11047 | class-specifier, enum-specifier, or elaborated-type-specifier, a | |
11048 | TREE_TYPE is returned; otherwise, a TYPE_DECL is returned. | |
a723baf1 | 11049 | |
eb1aef53 KL |
11050 | The parser flags FLAGS is used to control type-specifier parsing. |
11051 | ||
11052 | If IS_DECLARATION is TRUE, then this type-specifier is appearing | |
11053 | in a decl-specifier-seq. | |
a723baf1 MM |
11054 | |
11055 | If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a | |
11056 | class-specifier, enum-specifier, or elaborated-type-specifier, then | |
83a00410 | 11057 | *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1 |
560ad596 MM |
11058 | if a type is declared; 2 if it is defined. Otherwise, it is set to |
11059 | zero. | |
a723baf1 MM |
11060 | |
11061 | If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a | |
11062 | cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it | |
11063 | is set to FALSE. */ | |
11064 | ||
11065 | static tree | |
21526606 EC |
11066 | cp_parser_type_specifier (cp_parser* parser, |
11067 | cp_parser_flags flags, | |
62d1db17 | 11068 | cp_decl_specifier_seq *decl_specs, |
94edc4ab | 11069 | bool is_declaration, |
560ad596 | 11070 | int* declares_class_or_enum, |
94edc4ab | 11071 | bool* is_cv_qualifier) |
a723baf1 MM |
11072 | { |
11073 | tree type_spec = NULL_TREE; | |
11074 | cp_token *token; | |
11075 | enum rid keyword; | |
62d1db17 | 11076 | cp_decl_spec ds = ds_last; |
a723baf1 MM |
11077 | |
11078 | /* Assume this type-specifier does not declare a new type. */ | |
11079 | if (declares_class_or_enum) | |
543ca912 | 11080 | *declares_class_or_enum = 0; |
a723baf1 MM |
11081 | /* And that it does not specify a cv-qualifier. */ |
11082 | if (is_cv_qualifier) | |
11083 | *is_cv_qualifier = false; | |
11084 | /* Peek at the next token. */ | |
11085 | token = cp_lexer_peek_token (parser->lexer); | |
11086 | ||
11087 | /* If we're looking at a keyword, we can use that to guide the | |
11088 | production we choose. */ | |
11089 | keyword = token->keyword; | |
11090 | switch (keyword) | |
11091 | { | |
ff4eb0b5 | 11092 | case RID_ENUM: |
b9e75696 JM |
11093 | /* Look for the enum-specifier. */ |
11094 | type_spec = cp_parser_enum_specifier (parser); | |
11095 | /* If that worked, we're done. */ | |
11096 | if (type_spec) | |
ff4eb0b5 | 11097 | { |
ff4eb0b5 ZW |
11098 | if (declares_class_or_enum) |
11099 | *declares_class_or_enum = 2; | |
11100 | if (decl_specs) | |
11101 | cp_parser_set_decl_spec_type (decl_specs, | |
11102 | type_spec, | |
760b18ad | 11103 | token->location, |
ff4eb0b5 ZW |
11104 | /*user_defined_p=*/true); |
11105 | return type_spec; | |
11106 | } | |
11107 | else | |
11108 | goto elaborated_type_specifier; | |
11109 | ||
a723baf1 MM |
11110 | /* Any of these indicate either a class-specifier, or an |
11111 | elaborated-type-specifier. */ | |
11112 | case RID_CLASS: | |
11113 | case RID_STRUCT: | |
11114 | case RID_UNION: | |
a723baf1 | 11115 | /* Parse tentatively so that we can back up if we don't find a |
ff4eb0b5 | 11116 | class-specifier. */ |
a723baf1 | 11117 | cp_parser_parse_tentatively (parser); |
ff4eb0b5 ZW |
11118 | /* Look for the class-specifier. */ |
11119 | type_spec = cp_parser_class_specifier (parser); | |
dab71827 | 11120 | invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec); |
a723baf1 MM |
11121 | /* If that worked, we're done. */ |
11122 | if (cp_parser_parse_definitely (parser)) | |
11123 | { | |
11124 | if (declares_class_or_enum) | |
560ad596 | 11125 | *declares_class_or_enum = 2; |
62d1db17 MM |
11126 | if (decl_specs) |
11127 | cp_parser_set_decl_spec_type (decl_specs, | |
11128 | type_spec, | |
760b18ad | 11129 | token->location, |
62d1db17 | 11130 | /*user_defined_p=*/true); |
a723baf1 MM |
11131 | return type_spec; |
11132 | } | |
11133 | ||
11134 | /* Fall through. */ | |
ff4eb0b5 ZW |
11135 | elaborated_type_specifier: |
11136 | /* We're declaring (not defining) a class or enum. */ | |
11137 | if (declares_class_or_enum) | |
11138 | *declares_class_or_enum = 1; | |
a723baf1 | 11139 | |
ff4eb0b5 | 11140 | /* Fall through. */ |
a723baf1 MM |
11141 | case RID_TYPENAME: |
11142 | /* Look for an elaborated-type-specifier. */ | |
98ca843c EC |
11143 | type_spec |
11144 | = (cp_parser_elaborated_type_specifier | |
62d1db17 MM |
11145 | (parser, |
11146 | decl_specs && decl_specs->specs[(int) ds_friend], | |
11147 | is_declaration)); | |
62d1db17 MM |
11148 | if (decl_specs) |
11149 | cp_parser_set_decl_spec_type (decl_specs, | |
11150 | type_spec, | |
760b18ad | 11151 | token->location, |
62d1db17 | 11152 | /*user_defined_p=*/true); |
a723baf1 MM |
11153 | return type_spec; |
11154 | ||
11155 | case RID_CONST: | |
62d1db17 MM |
11156 | ds = ds_const; |
11157 | if (is_cv_qualifier) | |
11158 | *is_cv_qualifier = true; | |
11159 | break; | |
98ca843c | 11160 | |
a723baf1 | 11161 | case RID_VOLATILE: |
62d1db17 | 11162 | ds = ds_volatile; |
a723baf1 MM |
11163 | if (is_cv_qualifier) |
11164 | *is_cv_qualifier = true; | |
62d1db17 | 11165 | break; |
a723baf1 | 11166 | |
62d1db17 MM |
11167 | case RID_RESTRICT: |
11168 | ds = ds_restrict; | |
11169 | if (is_cv_qualifier) | |
11170 | *is_cv_qualifier = true; | |
11171 | break; | |
a723baf1 MM |
11172 | |
11173 | case RID_COMPLEX: | |
11174 | /* The `__complex__' keyword is a GNU extension. */ | |
62d1db17 MM |
11175 | ds = ds_complex; |
11176 | break; | |
a723baf1 MM |
11177 | |
11178 | default: | |
11179 | break; | |
11180 | } | |
11181 | ||
62d1db17 MM |
11182 | /* Handle simple keywords. */ |
11183 | if (ds != ds_last) | |
11184 | { | |
11185 | if (decl_specs) | |
11186 | { | |
11187 | ++decl_specs->specs[(int)ds]; | |
11188 | decl_specs->any_specifiers_p = true; | |
11189 | } | |
d6b418fa | 11190 | return cp_lexer_consume_token (parser->lexer)->u.value; |
62d1db17 MM |
11191 | } |
11192 | ||
a723baf1 MM |
11193 | /* If we do not already have a type-specifier, assume we are looking |
11194 | at a simple-type-specifier. */ | |
98ca843c | 11195 | type_spec = cp_parser_simple_type_specifier (parser, |
62d1db17 MM |
11196 | decl_specs, |
11197 | flags); | |
a723baf1 MM |
11198 | |
11199 | /* If we didn't find a type-specifier, and a type-specifier was not | |
11200 | optional in this context, issue an error message. */ | |
11201 | if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL)) | |
11202 | { | |
11203 | cp_parser_error (parser, "expected type specifier"); | |
11204 | return error_mark_node; | |
11205 | } | |
11206 | ||
11207 | return type_spec; | |
11208 | } | |
11209 | ||
11210 | /* Parse a simple-type-specifier. | |
11211 | ||
11212 | simple-type-specifier: | |
11213 | :: [opt] nested-name-specifier [opt] type-name | |
11214 | :: [opt] nested-name-specifier template template-id | |
11215 | char | |
11216 | wchar_t | |
11217 | bool | |
11218 | short | |
11219 | int | |
11220 | long | |
11221 | signed | |
11222 | unsigned | |
11223 | float | |
11224 | double | |
21526606 | 11225 | void |
a723baf1 | 11226 | |
3ad6a8e1 DG |
11227 | C++0x Extension: |
11228 | ||
11229 | simple-type-specifier: | |
4dbc8575 | 11230 | auto |
3ad6a8e1 | 11231 | decltype ( expression ) |
b6baa67d KVH |
11232 | char16_t |
11233 | char32_t | |
3ad6a8e1 | 11234 | |
a723baf1 MM |
11235 | GNU Extension: |
11236 | ||
11237 | simple-type-specifier: | |
11238 | __typeof__ unary-expression | |
11239 | __typeof__ ( type-id ) | |
11240 | ||
62d1db17 MM |
11241 | Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is |
11242 | appropriately updated. */ | |
a723baf1 MM |
11243 | |
11244 | static tree | |
98ca843c | 11245 | cp_parser_simple_type_specifier (cp_parser* parser, |
62d1db17 MM |
11246 | cp_decl_specifier_seq *decl_specs, |
11247 | cp_parser_flags flags) | |
a723baf1 MM |
11248 | { |
11249 | tree type = NULL_TREE; | |
11250 | cp_token *token; | |
11251 | ||
11252 | /* Peek at the next token. */ | |
11253 | token = cp_lexer_peek_token (parser->lexer); | |
11254 | ||
11255 | /* If we're looking at a keyword, things are easy. */ | |
11256 | switch (token->keyword) | |
11257 | { | |
11258 | case RID_CHAR: | |
62d1db17 MM |
11259 | if (decl_specs) |
11260 | decl_specs->explicit_char_p = true; | |
4b0d3cbe MM |
11261 | type = char_type_node; |
11262 | break; | |
b6baa67d KVH |
11263 | case RID_CHAR16: |
11264 | type = char16_type_node; | |
11265 | break; | |
11266 | case RID_CHAR32: | |
11267 | type = char32_type_node; | |
11268 | break; | |
a723baf1 | 11269 | case RID_WCHAR: |
4b0d3cbe MM |
11270 | type = wchar_type_node; |
11271 | break; | |
a723baf1 | 11272 | case RID_BOOL: |
4b0d3cbe MM |
11273 | type = boolean_type_node; |
11274 | break; | |
a723baf1 | 11275 | case RID_SHORT: |
62d1db17 MM |
11276 | if (decl_specs) |
11277 | ++decl_specs->specs[(int) ds_short]; | |
4b0d3cbe MM |
11278 | type = short_integer_type_node; |
11279 | break; | |
a723baf1 | 11280 | case RID_INT: |
62d1db17 MM |
11281 | if (decl_specs) |
11282 | decl_specs->explicit_int_p = true; | |
4b0d3cbe MM |
11283 | type = integer_type_node; |
11284 | break; | |
a723baf1 | 11285 | case RID_LONG: |
62d1db17 MM |
11286 | if (decl_specs) |
11287 | ++decl_specs->specs[(int) ds_long]; | |
4b0d3cbe MM |
11288 | type = long_integer_type_node; |
11289 | break; | |
a723baf1 | 11290 | case RID_SIGNED: |
62d1db17 MM |
11291 | if (decl_specs) |
11292 | ++decl_specs->specs[(int) ds_signed]; | |
4b0d3cbe MM |
11293 | type = integer_type_node; |
11294 | break; | |
a723baf1 | 11295 | case RID_UNSIGNED: |
62d1db17 MM |
11296 | if (decl_specs) |
11297 | ++decl_specs->specs[(int) ds_unsigned]; | |
4b0d3cbe MM |
11298 | type = unsigned_type_node; |
11299 | break; | |
a723baf1 | 11300 | case RID_FLOAT: |
4b0d3cbe MM |
11301 | type = float_type_node; |
11302 | break; | |
a723baf1 | 11303 | case RID_DOUBLE: |
4b0d3cbe MM |
11304 | type = double_type_node; |
11305 | break; | |
a723baf1 | 11306 | case RID_VOID: |
4b0d3cbe MM |
11307 | type = void_type_node; |
11308 | break; | |
4dbc8575 DG |
11309 | |
11310 | case RID_AUTO: | |
86a09a9e JM |
11311 | maybe_warn_cpp0x ("C++0x auto"); |
11312 | type = make_auto (); | |
4dbc8575 | 11313 | break; |
a723baf1 | 11314 | |
3ad6a8e1 DG |
11315 | case RID_DECLTYPE: |
11316 | /* Parse the `decltype' type. */ | |
11317 | type = cp_parser_decltype (parser); | |
11318 | ||
11319 | if (decl_specs) | |
11320 | cp_parser_set_decl_spec_type (decl_specs, type, | |
760b18ad | 11321 | token->location, |
3ad6a8e1 DG |
11322 | /*user_defined_p=*/true); |
11323 | ||
11324 | return type; | |
11325 | ||
a723baf1 | 11326 | case RID_TYPEOF: |
62d1db17 MM |
11327 | /* Consume the `typeof' token. */ |
11328 | cp_lexer_consume_token (parser->lexer); | |
11329 | /* Parse the operand to `typeof'. */ | |
11330 | type = cp_parser_sizeof_operand (parser, RID_TYPEOF); | |
11331 | /* If it is not already a TYPE, take its type. */ | |
11332 | if (!TYPE_P (type)) | |
11333 | type = finish_typeof (type); | |
11334 | ||
11335 | if (decl_specs) | |
11336 | cp_parser_set_decl_spec_type (decl_specs, type, | |
760b18ad | 11337 | token->location, |
62d1db17 | 11338 | /*user_defined_p=*/true); |
98ca843c | 11339 | |
62d1db17 | 11340 | return type; |
a723baf1 MM |
11341 | |
11342 | default: | |
11343 | break; | |
11344 | } | |
11345 | ||
4b0d3cbe MM |
11346 | /* If the type-specifier was for a built-in type, we're done. */ |
11347 | if (type) | |
11348 | { | |
11349 | tree id; | |
11350 | ||
62d1db17 MM |
11351 | /* Record the type. */ |
11352 | if (decl_specs | |
11353 | && (token->keyword != RID_SIGNED | |
11354 | && token->keyword != RID_UNSIGNED | |
11355 | && token->keyword != RID_SHORT | |
11356 | && token->keyword != RID_LONG)) | |
98ca843c | 11357 | cp_parser_set_decl_spec_type (decl_specs, |
62d1db17 | 11358 | type, |
760b18ad | 11359 | token->location, |
62d1db17 MM |
11360 | /*user_defined=*/false); |
11361 | if (decl_specs) | |
11362 | decl_specs->any_specifiers_p = true; | |
11363 | ||
4b0d3cbe | 11364 | /* Consume the token. */ |
d6b418fa | 11365 | id = cp_lexer_consume_token (parser->lexer)->u.value; |
0d956474 GB |
11366 | |
11367 | /* There is no valid C++ program where a non-template type is | |
11368 | followed by a "<". That usually indicates that the user thought | |
11369 | that the type was a template. */ | |
760b18ad | 11370 | cp_parser_check_for_invalid_template_id (parser, type, token->location); |
0d956474 | 11371 | |
62d1db17 | 11372 | return TYPE_NAME (type); |
4b0d3cbe MM |
11373 | } |
11374 | ||
a723baf1 | 11375 | /* The type-specifier must be a user-defined type. */ |
21526606 | 11376 | if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES)) |
a723baf1 | 11377 | { |
0c1a1ecd | 11378 | bool qualified_p; |
f68e4dc8 | 11379 | bool global_p; |
0c1a1ecd | 11380 | |
a723baf1 MM |
11381 | /* Don't gobble tokens or issue error messages if this is an |
11382 | optional type-specifier. */ | |
11383 | if (flags & CP_PARSER_FLAGS_OPTIONAL) | |
11384 | cp_parser_parse_tentatively (parser); | |
11385 | ||
11386 | /* Look for the optional `::' operator. */ | |
f68e4dc8 | 11387 | global_p |
da740453 | 11388 | = (cp_parser_global_scope_opt (parser, |
88525e1b | 11389 | /*current_scope_valid_p=*/false) |
da740453 | 11390 | != NULL_TREE); |
a723baf1 | 11391 | /* Look for the nested-name specifier. */ |
0c1a1ecd MM |
11392 | qualified_p |
11393 | = (cp_parser_nested_name_specifier_opt (parser, | |
11394 | /*typename_keyword_p=*/false, | |
11395 | /*check_dependency_p=*/true, | |
11396 | /*type_p=*/false, | |
6661a85f EB |
11397 | /*is_declaration=*/false) |
11398 | != NULL_TREE); | |
760b18ad | 11399 | token = cp_lexer_peek_token (parser->lexer); |
a723baf1 MM |
11400 | /* If we have seen a nested-name-specifier, and the next token |
11401 | is `template', then we are using the template-id production. */ | |
21526606 | 11402 | if (parser->scope |
a723baf1 MM |
11403 | && cp_parser_optional_template_keyword (parser)) |
11404 | { | |
11405 | /* Look for the template-id. */ | |
21526606 | 11406 | type = cp_parser_template_id (parser, |
a723baf1 | 11407 | /*template_keyword_p=*/true, |
a668c6ad MM |
11408 | /*check_dependency_p=*/true, |
11409 | /*is_declaration=*/false); | |
a723baf1 MM |
11410 | /* If the template-id did not name a type, we are out of |
11411 | luck. */ | |
11412 | if (TREE_CODE (type) != TYPE_DECL) | |
11413 | { | |
11414 | cp_parser_error (parser, "expected template-id for type"); | |
11415 | type = NULL_TREE; | |
11416 | } | |
11417 | } | |
11418 | /* Otherwise, look for a type-name. */ | |
11419 | else | |
4bb8ca28 | 11420 | type = cp_parser_type_name (parser); |
0c1a1ecd | 11421 | /* Keep track of all name-lookups performed in class scopes. */ |
98ca843c | 11422 | if (type |
f68e4dc8 | 11423 | && !global_p |
0c1a1ecd | 11424 | && !qualified_p |
98ca843c | 11425 | && TREE_CODE (type) == TYPE_DECL |
0c1a1ecd MM |
11426 | && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE) |
11427 | maybe_note_name_used_in_class (DECL_NAME (type), type); | |
a723baf1 | 11428 | /* If it didn't work out, we don't have a TYPE. */ |
21526606 | 11429 | if ((flags & CP_PARSER_FLAGS_OPTIONAL) |
a723baf1 MM |
11430 | && !cp_parser_parse_definitely (parser)) |
11431 | type = NULL_TREE; | |
62d1db17 MM |
11432 | if (type && decl_specs) |
11433 | cp_parser_set_decl_spec_type (decl_specs, type, | |
760b18ad | 11434 | token->location, |
62d1db17 | 11435 | /*user_defined=*/true); |
a723baf1 MM |
11436 | } |
11437 | ||
11438 | /* If we didn't get a type-name, issue an error message. */ | |
11439 | if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL)) | |
11440 | { | |
11441 | cp_parser_error (parser, "expected type-name"); | |
11442 | return error_mark_node; | |
11443 | } | |
11444 | ||
a668c6ad MM |
11445 | /* There is no valid C++ program where a non-template type is |
11446 | followed by a "<". That usually indicates that the user thought | |
11447 | that the type was a template. */ | |
4bb8ca28 | 11448 | if (type && type != error_mark_node) |
e58a9aa1 ZL |
11449 | { |
11450 | /* As a last-ditch effort, see if TYPE is an Objective-C type. | |
11451 | If it is, then the '<'...'>' enclose protocol names rather than | |
11452 | template arguments, and so everything is fine. */ | |
11453 | if (c_dialect_objc () | |
11454 | && (objc_is_id (type) || objc_is_class_name (type))) | |
11455 | { | |
11456 | tree protos = cp_parser_objc_protocol_refs_opt (parser); | |
11457 | tree qual_type = objc_get_protocol_qualified_type (type, protos); | |
11458 | ||
11459 | /* Clobber the "unqualified" type previously entered into | |
128a79fb | 11460 | DECL_SPECS with the new, improved protocol-qualified version. */ |
e58a9aa1 ZL |
11461 | if (decl_specs) |
11462 | decl_specs->type = qual_type; | |
11463 | ||
11464 | return qual_type; | |
11465 | } | |
11466 | ||
760b18ad DS |
11467 | cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type), |
11468 | token->location); | |
c8094d83 | 11469 | } |
ec75414f | 11470 | |
a723baf1 MM |
11471 | return type; |
11472 | } | |
11473 | ||
11474 | /* Parse a type-name. | |
11475 | ||
11476 | type-name: | |
11477 | class-name | |
11478 | enum-name | |
21526606 | 11479 | typedef-name |
a723baf1 MM |
11480 | |
11481 | enum-name: | |
11482 | identifier | |
11483 | ||
11484 | typedef-name: | |
21526606 | 11485 | identifier |
a723baf1 | 11486 | |
78dcd41a | 11487 | Returns a TYPE_DECL for the type. */ |
a723baf1 MM |
11488 | |
11489 | static tree | |
94edc4ab | 11490 | cp_parser_type_name (cp_parser* parser) |
a723baf1 MM |
11491 | { |
11492 | tree type_decl; | |
a723baf1 MM |
11493 | |
11494 | /* We can't know yet whether it is a class-name or not. */ | |
11495 | cp_parser_parse_tentatively (parser); | |
11496 | /* Try a class-name. */ | |
21526606 | 11497 | type_decl = cp_parser_class_name (parser, |
a723baf1 MM |
11498 | /*typename_keyword_p=*/false, |
11499 | /*template_keyword_p=*/false, | |
fc6a28d7 | 11500 | none_type, |
a723baf1 | 11501 | /*check_dependency_p=*/true, |
a668c6ad MM |
11502 | /*class_head_p=*/false, |
11503 | /*is_declaration=*/false); | |
a723baf1 MM |
11504 | /* If it's not a class-name, keep looking. */ |
11505 | if (!cp_parser_parse_definitely (parser)) | |
11506 | { | |
11507 | /* It must be a typedef-name or an enum-name. */ | |
359be76e MLI |
11508 | return cp_parser_nonclass_name (parser); |
11509 | } | |
21526606 | 11510 | |
359be76e MLI |
11511 | return type_decl; |
11512 | } | |
e58a9aa1 | 11513 | |
359be76e | 11514 | /* Parse a non-class type-name, that is, either an enum-name or a typedef-name. |
e58a9aa1 | 11515 | |
359be76e MLI |
11516 | enum-name: |
11517 | identifier | |
11518 | ||
11519 | typedef-name: | |
11520 | identifier | |
11521 | ||
11522 | Returns a TYPE_DECL for the type. */ | |
21526606 | 11523 | |
359be76e MLI |
11524 | static tree |
11525 | cp_parser_nonclass_name (cp_parser* parser) | |
11526 | { | |
11527 | tree type_decl; | |
11528 | tree identifier; | |
11529 | ||
2b7a3abf | 11530 | cp_token *token = cp_lexer_peek_token (parser->lexer); |
359be76e MLI |
11531 | identifier = cp_parser_identifier (parser); |
11532 | if (identifier == error_mark_node) | |
11533 | return error_mark_node; | |
11534 | ||
11535 | /* Look up the type-name. */ | |
2b7a3abf | 11536 | type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location); |
359be76e MLI |
11537 | |
11538 | if (TREE_CODE (type_decl) != TYPE_DECL | |
11539 | && (objc_is_id (identifier) || objc_is_class_name (identifier))) | |
11540 | { | |
11541 | /* See if this is an Objective-C type. */ | |
11542 | tree protos = cp_parser_objc_protocol_refs_opt (parser); | |
11543 | tree type = objc_get_protocol_qualified_type (identifier, protos); | |
11544 | if (type) | |
11545 | type_decl = TYPE_NAME (type); | |
11546 | } | |
11547 | ||
11548 | /* Issue an error if we did not find a type-name. */ | |
11549 | if (TREE_CODE (type_decl) != TYPE_DECL) | |
11550 | { | |
11551 | if (!cp_parser_simulate_error (parser)) | |
11552 | cp_parser_name_lookup_error (parser, identifier, type_decl, | |
2b7a3abf | 11553 | "is not a type", token->location); |
359be76e MLI |
11554 | return error_mark_node; |
11555 | } | |
11556 | /* Remember that the name was used in the definition of the | |
11557 | current class so that we can check later to see if the | |
11558 | meaning would have been different after the class was | |
11559 | entirely defined. */ | |
11560 | else if (type_decl != error_mark_node | |
11561 | && !parser->scope) | |
11562 | maybe_note_name_used_in_class (identifier, type_decl); | |
11563 | ||
a723baf1 MM |
11564 | return type_decl; |
11565 | } | |
11566 | ||
a723baf1 MM |
11567 | /* Parse an elaborated-type-specifier. Note that the grammar given |
11568 | here incorporates the resolution to DR68. | |
11569 | ||
11570 | elaborated-type-specifier: | |
11571 | class-key :: [opt] nested-name-specifier [opt] identifier | |
11572 | class-key :: [opt] nested-name-specifier [opt] template [opt] template-id | |
adf2edec | 11573 | enum-key :: [opt] nested-name-specifier [opt] identifier |
a723baf1 | 11574 | typename :: [opt] nested-name-specifier identifier |
21526606 EC |
11575 | typename :: [opt] nested-name-specifier template [opt] |
11576 | template-id | |
a723baf1 | 11577 | |
360d1b99 MM |
11578 | GNU extension: |
11579 | ||
11580 | elaborated-type-specifier: | |
11581 | class-key attributes :: [opt] nested-name-specifier [opt] identifier | |
21526606 | 11582 | class-key attributes :: [opt] nested-name-specifier [opt] |
0cbd7506 | 11583 | template [opt] template-id |
360d1b99 MM |
11584 | enum attributes :: [opt] nested-name-specifier [opt] identifier |
11585 | ||
a723baf1 MM |
11586 | If IS_FRIEND is TRUE, then this elaborated-type-specifier is being |
11587 | declared `friend'. If IS_DECLARATION is TRUE, then this | |
11588 | elaborated-type-specifier appears in a decl-specifiers-seq, i.e., | |
11589 | something is being declared. | |
11590 | ||
11591 | Returns the TYPE specified. */ | |
11592 | ||
11593 | static tree | |
21526606 | 11594 | cp_parser_elaborated_type_specifier (cp_parser* parser, |
0cbd7506 MS |
11595 | bool is_friend, |
11596 | bool is_declaration) | |
a723baf1 MM |
11597 | { |
11598 | enum tag_types tag_type; | |
11599 | tree identifier; | |
11600 | tree type = NULL_TREE; | |
360d1b99 | 11601 | tree attributes = NULL_TREE; |
84a900c9 | 11602 | tree globalscope; |
2b7a3abf | 11603 | cp_token *token = NULL; |
a723baf1 MM |
11604 | |
11605 | /* See if we're looking at the `enum' keyword. */ | |
11606 | if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM)) | |
11607 | { | |
11608 | /* Consume the `enum' token. */ | |
11609 | cp_lexer_consume_token (parser->lexer); | |
11610 | /* Remember that it's an enumeration type. */ | |
11611 | tag_type = enum_type; | |
adf2edec DG |
11612 | /* Parse the optional `struct' or `class' key (for C++0x scoped |
11613 | enums). */ | |
11614 | if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS) | |
11615 | || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT)) | |
11616 | { | |
11617 | if (cxx_dialect == cxx98) | |
11618 | maybe_warn_cpp0x ("scoped enums"); | |
11619 | ||
11620 | /* Consume the `struct' or `class'. */ | |
11621 | cp_lexer_consume_token (parser->lexer); | |
11622 | } | |
360d1b99 MM |
11623 | /* Parse the attributes. */ |
11624 | attributes = cp_parser_attributes_opt (parser); | |
a723baf1 MM |
11625 | } |
11626 | /* Or, it might be `typename'. */ | |
11627 | else if (cp_lexer_next_token_is_keyword (parser->lexer, | |
11628 | RID_TYPENAME)) | |
11629 | { | |
11630 | /* Consume the `typename' token. */ | |
11631 | cp_lexer_consume_token (parser->lexer); | |
11632 | /* Remember that it's a `typename' type. */ | |
11633 | tag_type = typename_type; | |
a723baf1 MM |
11634 | } |
11635 | /* Otherwise it must be a class-key. */ | |
11636 | else | |
11637 | { | |
11638 | tag_type = cp_parser_class_key (parser); | |
11639 | if (tag_type == none_type) | |
11640 | return error_mark_node; | |
360d1b99 MM |
11641 | /* Parse the attributes. */ |
11642 | attributes = cp_parser_attributes_opt (parser); | |
a723baf1 MM |
11643 | } |
11644 | ||
11645 | /* Look for the `::' operator. */ | |
84a900c9 AP |
11646 | globalscope = cp_parser_global_scope_opt (parser, |
11647 | /*current_scope_valid_p=*/false); | |
a723baf1 | 11648 | /* Look for the nested-name-specifier. */ |
84a900c9 | 11649 | if (tag_type == typename_type && !globalscope) |
8fa1ad0e | 11650 | { |
8fe4d24b | 11651 | if (!cp_parser_nested_name_specifier (parser, |
8fa1ad0e MM |
11652 | /*typename_keyword_p=*/true, |
11653 | /*check_dependency_p=*/true, | |
a668c6ad | 11654 | /*type_p=*/true, |
8fe4d24b | 11655 | is_declaration)) |
8fa1ad0e MM |
11656 | return error_mark_node; |
11657 | } | |
a723baf1 MM |
11658 | else |
11659 | /* Even though `typename' is not present, the proposed resolution | |
11660 | to Core Issue 180 says that in `class A<T>::B', `B' should be | |
11661 | considered a type-name, even if `A<T>' is dependent. */ | |
11662 | cp_parser_nested_name_specifier_opt (parser, | |
11663 | /*typename_keyword_p=*/true, | |
11664 | /*check_dependency_p=*/true, | |
a668c6ad MM |
11665 | /*type_p=*/true, |
11666 | is_declaration); | |
6311165e MM |
11667 | /* For everything but enumeration types, consider a template-id. |
11668 | For an enumeration type, consider only a plain identifier. */ | |
a723baf1 MM |
11669 | if (tag_type != enum_type) |
11670 | { | |
11671 | bool template_p = false; | |
11672 | tree decl; | |
11673 | ||
11674 | /* Allow the `template' keyword. */ | |
11675 | template_p = cp_parser_optional_template_keyword (parser); | |
11676 | /* If we didn't see `template', we don't know if there's a | |
0cbd7506 | 11677 | template-id or not. */ |
a723baf1 MM |
11678 | if (!template_p) |
11679 | cp_parser_parse_tentatively (parser); | |
11680 | /* Parse the template-id. */ | |
2b7a3abf | 11681 | token = cp_lexer_peek_token (parser->lexer); |
a723baf1 | 11682 | decl = cp_parser_template_id (parser, template_p, |
a668c6ad MM |
11683 | /*check_dependency_p=*/true, |
11684 | is_declaration); | |
a723baf1 | 11685 | /* If we didn't find a template-id, look for an ordinary |
0cbd7506 | 11686 | identifier. */ |
a723baf1 MM |
11687 | if (!template_p && !cp_parser_parse_definitely (parser)) |
11688 | ; | |
11689 | /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is | |
11690 | in effect, then we must assume that, upon instantiation, the | |
11691 | template will correspond to a class. */ | |
11692 | else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR | |
11693 | && tag_type == typename_type) | |
11694 | type = make_typename_type (parser->scope, decl, | |
fc6a28d7 | 11695 | typename_type, |
8da15291 | 11696 | /*complain=*/tf_error); |
8ec0d73b AP |
11697 | /* If the `typename' keyword is in effect and DECL is not a type |
11698 | decl. Then type is non existant. */ | |
11699 | else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL) | |
11700 | type = NULL_TREE; | |
11701 | else | |
a723baf1 MM |
11702 | type = TREE_TYPE (decl); |
11703 | } | |
11704 | ||
a723baf1 MM |
11705 | if (!type) |
11706 | { | |
2b7a3abf | 11707 | token = cp_lexer_peek_token (parser->lexer); |
a723baf1 MM |
11708 | identifier = cp_parser_identifier (parser); |
11709 | ||
11710 | if (identifier == error_mark_node) | |
eb5abb39 NS |
11711 | { |
11712 | parser->scope = NULL_TREE; | |
11713 | return error_mark_node; | |
11714 | } | |
a723baf1 MM |
11715 | |
11716 | /* For a `typename', we needn't call xref_tag. */ | |
c8094d83 | 11717 | if (tag_type == typename_type |
0c88d886 | 11718 | && TREE_CODE (parser->scope) != NAMESPACE_DECL) |
21526606 | 11719 | return cp_parser_make_typename_type (parser, parser->scope, |
2b7a3abf DS |
11720 | identifier, |
11721 | token->location); | |
a723baf1 MM |
11722 | /* Look up a qualified name in the usual way. */ |
11723 | if (parser->scope) | |
11724 | { | |
11725 | tree decl; | |
af92ab36 | 11726 | tree ambiguous_decls; |
a723baf1 | 11727 | |
21526606 | 11728 | decl = cp_parser_lookup_name (parser, identifier, |
fc6a28d7 | 11729 | tag_type, |
b0bc6e8e | 11730 | /*is_template=*/false, |
eea9800f | 11731 | /*is_namespace=*/false, |
8f78f01f | 11732 | /*check_dependency=*/true, |
2b7a3abf DS |
11733 | &ambiguous_decls, |
11734 | token->location); | |
af92ab36 OW |
11735 | |
11736 | /* If the lookup was ambiguous, an error will already have been | |
11737 | issued. */ | |
11738 | if (ambiguous_decls) | |
11739 | return error_mark_node; | |
710b73e6 KL |
11740 | |
11741 | /* If we are parsing friend declaration, DECL may be a | |
11742 | TEMPLATE_DECL tree node here. However, we need to check | |
11743 | whether this TEMPLATE_DECL results in valid code. Consider | |
11744 | the following example: | |
11745 | ||
11746 | namespace N { | |
11747 | template <class T> class C {}; | |
11748 | } | |
11749 | class X { | |
11750 | template <class T> friend class N::C; // #1, valid code | |
11751 | }; | |
11752 | template <class T> class Y { | |
11753 | friend class N::C; // #2, invalid code | |
11754 | }; | |
11755 | ||
11756 | For both case #1 and #2, we arrive at a TEMPLATE_DECL after | |
11757 | name lookup of `N::C'. We see that friend declaration must | |
11758 | be template for the code to be valid. Note that | |
11759 | processing_template_decl does not work here since it is | |
11760 | always 1 for the above two cases. */ | |
11761 | ||
21526606 | 11762 | decl = (cp_parser_maybe_treat_template_as_class |
710b73e6 KL |
11763 | (decl, /*tag_name_p=*/is_friend |
11764 | && parser->num_template_parameter_lists)); | |
a723baf1 MM |
11765 | |
11766 | if (TREE_CODE (decl) != TYPE_DECL) | |
11767 | { | |
c8094d83 | 11768 | cp_parser_diagnose_invalid_type_name (parser, |
0c88d886 | 11769 | parser->scope, |
2b7a3abf DS |
11770 | identifier, |
11771 | token->location); | |
a723baf1 MM |
11772 | return error_mark_node; |
11773 | } | |
560ad596 MM |
11774 | |
11775 | if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE) | |
baa5bf11 LM |
11776 | { |
11777 | bool allow_template = (parser->num_template_parameter_lists | |
11778 | || DECL_SELF_REFERENCE_P (decl)); | |
11779 | type = check_elaborated_type_specifier (tag_type, decl, | |
11780 | allow_template); | |
11781 | ||
11782 | if (type == error_mark_node) | |
11783 | return error_mark_node; | |
11784 | } | |
a723baf1 | 11785 | |
2a7b8343 SB |
11786 | /* Forward declarations of nested types, such as |
11787 | ||
11788 | class C1::C2; | |
11789 | class C1::C2::C3; | |
11790 | ||
11791 | are invalid unless all components preceding the final '::' | |
11792 | are complete. If all enclosing types are complete, these | |
11793 | declarations become merely pointless. | |
11794 | ||
11795 | Invalid forward declarations of nested types are errors | |
11796 | caught elsewhere in parsing. Those that are pointless arrive | |
11797 | here. */ | |
11798 | ||
771ce78e | 11799 | if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON) |
2a7b8343 SB |
11800 | && !is_friend && !processing_explicit_instantiation) |
11801 | warning (0, "declaration %qD does not declare anything", decl); | |
11802 | ||
a723baf1 MM |
11803 | type = TREE_TYPE (decl); |
11804 | } | |
21526606 | 11805 | else |
a723baf1 MM |
11806 | { |
11807 | /* An elaborated-type-specifier sometimes introduces a new type and | |
11808 | sometimes names an existing type. Normally, the rule is that it | |
11809 | introduces a new type only if there is not an existing type of | |
11810 | the same name already in scope. For example, given: | |
11811 | ||
11812 | struct S {}; | |
11813 | void f() { struct S s; } | |
11814 | ||
11815 | the `struct S' in the body of `f' is the same `struct S' as in | |
11816 | the global scope; the existing definition is used. However, if | |
21526606 | 11817 | there were no global declaration, this would introduce a new |
a723baf1 MM |
11818 | local class named `S'. |
11819 | ||
11820 | An exception to this rule applies to the following code: | |
11821 | ||
11822 | namespace N { struct S; } | |
11823 | ||
11824 | Here, the elaborated-type-specifier names a new type | |
11825 | unconditionally; even if there is already an `S' in the | |
11826 | containing scope this declaration names a new type. | |
11827 | This exception only applies if the elaborated-type-specifier | |
11828 | forms the complete declaration: | |
11829 | ||
21526606 | 11830 | [class.name] |
a723baf1 MM |
11831 | |
11832 | A declaration consisting solely of `class-key identifier ;' is | |
11833 | either a redeclaration of the name in the current scope or a | |
11834 | forward declaration of the identifier as a class name. It | |
11835 | introduces the name into the current scope. | |
11836 | ||
11837 | We are in this situation precisely when the next token is a `;'. | |
11838 | ||
11839 | An exception to the exception is that a `friend' declaration does | |
11840 | *not* name a new type; i.e., given: | |
11841 | ||
11842 | struct S { friend struct T; }; | |
11843 | ||
21526606 | 11844 | `T' is not a new type in the scope of `S'. |
a723baf1 MM |
11845 | |
11846 | Also, `new struct S' or `sizeof (struct S)' never results in the | |
11847 | definition of a new type; a new type can only be declared in a | |
9bcb9aae | 11848 | declaration context. */ |
a723baf1 | 11849 | |
29ef83de | 11850 | tag_scope ts; |
ca85f659 MM |
11851 | bool template_p; |
11852 | ||
29ef83de KL |
11853 | if (is_friend) |
11854 | /* Friends have special name lookup rules. */ | |
11855 | ts = ts_within_enclosing_non_class; | |
11856 | else if (is_declaration | |
11857 | && cp_lexer_next_token_is (parser->lexer, | |
11858 | CPP_SEMICOLON)) | |
11859 | /* This is a `class-key identifier ;' */ | |
11860 | ts = ts_current; | |
11861 | else | |
11862 | ts = ts_global; | |
11863 | ||
3db45ab5 | 11864 | template_p = |
ca85f659 MM |
11865 | (parser->num_template_parameter_lists |
11866 | && (cp_parser_next_token_starts_class_definition_p (parser) | |
11867 | || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))); | |
357d956e MM |
11868 | /* An unqualified name was used to reference this type, so |
11869 | there were no qualifying templates. */ | |
3db45ab5 | 11870 | if (!cp_parser_check_template_parameters (parser, |
2b7a3abf | 11871 | /*num_templates=*/0, |
0d246707 MLI |
11872 | token->location, |
11873 | /*declarator=*/NULL)) | |
357d956e | 11874 | return error_mark_node; |
ca85f659 | 11875 | type = xref_tag (tag_type, identifier, ts, template_p); |
a723baf1 MM |
11876 | } |
11877 | } | |
b9e75696 | 11878 | |
31b29c62 LM |
11879 | if (type == error_mark_node) |
11880 | return error_mark_node; | |
11881 | ||
b9e75696 JM |
11882 | /* Allow attributes on forward declarations of classes. */ |
11883 | if (attributes) | |
11884 | { | |
abab4604 JM |
11885 | if (TREE_CODE (type) == TYPENAME_TYPE) |
11886 | warning (OPT_Wattributes, | |
11887 | "attributes ignored on uninstantiated type"); | |
11888 | else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type) | |
11889 | && ! processing_explicit_instantiation) | |
b9e75696 JM |
11890 | warning (OPT_Wattributes, |
11891 | "attributes ignored on template instantiation"); | |
11892 | else if (is_declaration && cp_parser_declares_only_class_p (parser)) | |
11893 | cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE); | |
11894 | else | |
11895 | warning (OPT_Wattributes, | |
11896 | "attributes ignored on elaborated-type-specifier that is not a forward declaration"); | |
11897 | } | |
11898 | ||
a723baf1 MM |
11899 | if (tag_type != enum_type) |
11900 | cp_parser_check_class_key (tag_type, type); | |
ee43dab5 MM |
11901 | |
11902 | /* A "<" cannot follow an elaborated type specifier. If that | |
11903 | happens, the user was probably trying to form a template-id. */ | |
760b18ad | 11904 | cp_parser_check_for_invalid_template_id (parser, type, token->location); |
ee43dab5 | 11905 | |
a723baf1 MM |
11906 | return type; |
11907 | } | |
11908 | ||
11909 | /* Parse an enum-specifier. | |
11910 | ||
11911 | enum-specifier: | |
adf2edec DG |
11912 | enum-key identifier [opt] enum-base [opt] { enumerator-list [opt] } |
11913 | ||
11914 | enum-key: | |
11915 | enum | |
11916 | enum class [C++0x] | |
11917 | enum struct [C++0x] | |
11918 | ||
11919 | enum-base: [C++0x] | |
11920 | : type-specifier-seq | |
a723baf1 | 11921 | |
f6af9a15 | 11922 | GNU Extensions: |
adf2edec DG |
11923 | enum-key attributes[opt] identifier [opt] enum-base [opt] |
11924 | { enumerator-list [opt] }attributes[opt] | |
f6af9a15 | 11925 | |
b9e75696 JM |
11926 | Returns an ENUM_TYPE representing the enumeration, or NULL_TREE |
11927 | if the token stream isn't an enum-specifier after all. */ | |
a723baf1 MM |
11928 | |
11929 | static tree | |
94edc4ab | 11930 | cp_parser_enum_specifier (cp_parser* parser) |
a723baf1 | 11931 | { |
ff4eb0b5 | 11932 | tree identifier; |
a723baf1 | 11933 | tree type; |
b9e75696 | 11934 | tree attributes; |
adf2edec | 11935 | bool scoped_enum_p = false; |
5072c6bd | 11936 | bool has_underlying_type = false; |
adf2edec | 11937 | tree underlying_type = NULL_TREE; |
b9e75696 JM |
11938 | |
11939 | /* Parse tentatively so that we can back up if we don't find a | |
11940 | enum-specifier. */ | |
11941 | cp_parser_parse_tentatively (parser); | |
a723baf1 | 11942 | |
ff4eb0b5 ZW |
11943 | /* Caller guarantees that the current token is 'enum', an identifier |
11944 | possibly follows, and the token after that is an opening brace. | |
11945 | If we don't have an identifier, fabricate an anonymous name for | |
11946 | the enumeration being defined. */ | |
11947 | cp_lexer_consume_token (parser->lexer); | |
a723baf1 | 11948 | |
adf2edec DG |
11949 | /* Parse the "class" or "struct", which indicates a scoped |
11950 | enumeration type in C++0x. */ | |
11951 | if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS) | |
11952 | || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT)) | |
11953 | { | |
11954 | if (cxx_dialect == cxx98) | |
11955 | maybe_warn_cpp0x ("scoped enums"); | |
11956 | ||
11957 | /* Consume the `struct' or `class' token. */ | |
11958 | cp_lexer_consume_token (parser->lexer); | |
11959 | ||
11960 | scoped_enum_p = true; | |
11961 | } | |
5072c6bd | 11962 | |
b9e75696 JM |
11963 | attributes = cp_parser_attributes_opt (parser); |
11964 | ||
ff4eb0b5 | 11965 | if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)) |
a723baf1 | 11966 | identifier = cp_parser_identifier (parser); |
ff4eb0b5 ZW |
11967 | else |
11968 | identifier = make_anon_name (); | |
a723baf1 | 11969 | |
adf2edec DG |
11970 | /* Check for the `:' that denotes a specified underlying type in C++0x. */ |
11971 | if (cp_lexer_next_token_is (parser->lexer, CPP_COLON)) | |
11972 | { | |
11973 | cp_decl_specifier_seq type_specifiers; | |
11974 | ||
5072c6bd JJ |
11975 | /* At this point this is surely not elaborated type specifier. */ |
11976 | if (!cp_parser_parse_definitely (parser)) | |
11977 | return NULL_TREE; | |
11978 | ||
adf2edec DG |
11979 | if (cxx_dialect == cxx98) |
11980 | maybe_warn_cpp0x ("scoped enums"); | |
11981 | ||
11982 | /* Consume the `:'. */ | |
11983 | cp_lexer_consume_token (parser->lexer); | |
11984 | ||
5072c6bd JJ |
11985 | has_underlying_type = true; |
11986 | ||
adf2edec DG |
11987 | /* Parse the type-specifier-seq. */ |
11988 | cp_parser_type_specifier_seq (parser, /*is_condition=*/false, | |
11989 | &type_specifiers); | |
5072c6bd | 11990 | |
adf2edec DG |
11991 | /* If that didn't work, stop. */ |
11992 | if (type_specifiers.type != error_mark_node) | |
11993 | { | |
11994 | underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME, | |
11995 | /*initialized=*/0, NULL); | |
11996 | if (underlying_type == error_mark_node) | |
11997 | underlying_type = NULL_TREE; | |
11998 | } | |
adf2edec DG |
11999 | } |
12000 | ||
b9e75696 JM |
12001 | /* Look for the `{' but don't consume it yet. */ |
12002 | if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)) | |
5072c6bd JJ |
12003 | { |
12004 | cp_parser_error (parser, "expected %<{%>"); | |
12005 | if (has_underlying_type) | |
12006 | return NULL_TREE; | |
12007 | } | |
b9e75696 | 12008 | |
5072c6bd | 12009 | if (!has_underlying_type && !cp_parser_parse_definitely (parser)) |
b9e75696 JM |
12010 | return NULL_TREE; |
12011 | ||
a723baf1 | 12012 | /* Issue an error message if type-definitions are forbidden here. */ |
d19b84e9 MM |
12013 | if (!cp_parser_check_type_definition (parser)) |
12014 | type = error_mark_node; | |
12015 | else | |
12016 | /* Create the new type. We do this before consuming the opening | |
12017 | brace so the enum will be recorded as being on the line of its | |
12018 | tag (or the 'enum' keyword, if there is no tag). */ | |
adf2edec | 12019 | type = start_enum (identifier, underlying_type, scoped_enum_p); |
d19b84e9 | 12020 | |
2cfe82fe ZW |
12021 | /* Consume the opening brace. */ |
12022 | cp_lexer_consume_token (parser->lexer); | |
12023 | ||
b9e75696 JM |
12024 | if (type == error_mark_node) |
12025 | { | |
12026 | cp_parser_skip_to_end_of_block_or_statement (parser); | |
12027 | return error_mark_node; | |
12028 | } | |
12029 | ||
ff4eb0b5 ZW |
12030 | /* If the next token is not '}', then there are some enumerators. */ |
12031 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE)) | |
a723baf1 | 12032 | cp_parser_enumerator_list (parser, type); |
ff4eb0b5 ZW |
12033 | |
12034 | /* Consume the final '}'. */ | |
3a483371 | 12035 | cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>"); |
a723baf1 | 12036 | |
f6af9a15 | 12037 | /* Look for trailing attributes to apply to this enumeration, and |
03fd3f84 | 12038 | apply them if appropriate. */ |
f6af9a15 MA |
12039 | if (cp_parser_allow_gnu_extensions_p (parser)) |
12040 | { | |
12041 | tree trailing_attr = cp_parser_attributes_opt (parser); | |
57c2231b | 12042 | trailing_attr = chainon (trailing_attr, attributes); |
f6af9a15 MA |
12043 | cplus_decl_attributes (&type, |
12044 | trailing_attr, | |
12045 | (int) ATTR_FLAG_TYPE_IN_PLACE); | |
12046 | } | |
12047 | ||
a723baf1 MM |
12048 | /* Finish up the enumeration. */ |
12049 | finish_enum (type); | |
12050 | ||
12051 | return type; | |
12052 | } | |
12053 | ||
12054 | /* Parse an enumerator-list. The enumerators all have the indicated | |
21526606 | 12055 | TYPE. |
a723baf1 MM |
12056 | |
12057 | enumerator-list: | |
12058 | enumerator-definition | |
12059 | enumerator-list , enumerator-definition */ | |
12060 | ||
12061 | static void | |
94edc4ab | 12062 | cp_parser_enumerator_list (cp_parser* parser, tree type) |
a723baf1 MM |
12063 | { |
12064 | while (true) | |
12065 | { | |
a723baf1 MM |
12066 | /* Parse an enumerator-definition. */ |
12067 | cp_parser_enumerator_definition (parser, type); | |
ff4eb0b5 ZW |
12068 | |
12069 | /* If the next token is not a ',', we've reached the end of | |
12070 | the list. */ | |
12071 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)) | |
a723baf1 MM |
12072 | break; |
12073 | /* Otherwise, consume the `,' and keep going. */ | |
12074 | cp_lexer_consume_token (parser->lexer); | |
12075 | /* If the next token is a `}', there is a trailing comma. */ | |
12076 | if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE)) | |
12077 | { | |
fcf73884 | 12078 | if (!in_system_header) |
509c9d60 | 12079 | pedwarn (input_location, OPT_pedantic, "comma at end of enumerator list"); |
a723baf1 MM |
12080 | break; |
12081 | } | |
12082 | } | |
12083 | } | |
12084 | ||
12085 | /* Parse an enumerator-definition. The enumerator has the indicated | |
12086 | TYPE. | |
12087 | ||
12088 | enumerator-definition: | |
12089 | enumerator | |
12090 | enumerator = constant-expression | |
21526606 | 12091 | |
a723baf1 MM |
12092 | enumerator: |
12093 | identifier */ | |
12094 | ||
12095 | static void | |
94edc4ab | 12096 | cp_parser_enumerator_definition (cp_parser* parser, tree type) |
a723baf1 | 12097 | { |
a723baf1 MM |
12098 | tree identifier; |
12099 | tree value; | |
12100 | ||
12101 | /* Look for the identifier. */ | |
12102 | identifier = cp_parser_identifier (parser); | |
12103 | if (identifier == error_mark_node) | |
12104 | return; | |
21526606 | 12105 | |
ff4eb0b5 ZW |
12106 | /* If the next token is an '=', then there is an explicit value. */ |
12107 | if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)) | |
a723baf1 MM |
12108 | { |
12109 | /* Consume the `=' token. */ | |
12110 | cp_lexer_consume_token (parser->lexer); | |
12111 | /* Parse the value. */ | |
21526606 | 12112 | value = cp_parser_constant_expression (parser, |
d17811fd | 12113 | /*allow_non_constant_p=*/false, |
14d22dd6 | 12114 | NULL); |
a723baf1 MM |
12115 | } |
12116 | else | |
12117 | value = NULL_TREE; | |
12118 | ||
02060927 DS |
12119 | /* If we are processing a template, make sure the initializer of the |
12120 | enumerator doesn't contain any bare template parameter pack. */ | |
12121 | if (check_for_bare_parameter_packs (value)) | |
12122 | value = error_mark_node; | |
12123 | ||
a723baf1 MM |
12124 | /* Create the enumerator. */ |
12125 | build_enumerator (identifier, value, type); | |
12126 | } | |
12127 | ||
12128 | /* Parse a namespace-name. | |
12129 | ||
12130 | namespace-name: | |
12131 | original-namespace-name | |
12132 | namespace-alias | |
12133 | ||
12134 | Returns the NAMESPACE_DECL for the namespace. */ | |
12135 | ||
12136 | static tree | |
94edc4ab | 12137 | cp_parser_namespace_name (cp_parser* parser) |
a723baf1 MM |
12138 | { |
12139 | tree identifier; | |
12140 | tree namespace_decl; | |
12141 | ||
2b7a3abf DS |
12142 | cp_token *token = cp_lexer_peek_token (parser->lexer); |
12143 | ||
a723baf1 MM |
12144 | /* Get the name of the namespace. */ |
12145 | identifier = cp_parser_identifier (parser); | |
12146 | if (identifier == error_mark_node) | |
12147 | return error_mark_node; | |
12148 | ||
eea9800f MM |
12149 | /* Look up the identifier in the currently active scope. Look only |
12150 | for namespaces, due to: | |
12151 | ||
12152 | [basic.lookup.udir] | |
12153 | ||
12154 | When looking up a namespace-name in a using-directive or alias | |
21526606 | 12155 | definition, only namespace names are considered. |
eea9800f MM |
12156 | |
12157 | And: | |
12158 | ||
12159 | [basic.lookup.qual] | |
12160 | ||
12161 | During the lookup of a name preceding the :: scope resolution | |
21526606 | 12162 | operator, object, function, and enumerator names are ignored. |
eea9800f | 12163 | |
adf2edec | 12164 | (Note that cp_parser_qualifying_entity only calls this |
eea9800f MM |
12165 | function if the token after the name is the scope resolution |
12166 | operator.) */ | |
12167 | namespace_decl = cp_parser_lookup_name (parser, identifier, | |
fc6a28d7 | 12168 | none_type, |
b0bc6e8e | 12169 | /*is_template=*/false, |
eea9800f | 12170 | /*is_namespace=*/true, |
8f78f01f | 12171 | /*check_dependency=*/true, |
2b7a3abf DS |
12172 | /*ambiguous_decls=*/NULL, |
12173 | token->location); | |
a723baf1 MM |
12174 | /* If it's not a namespace, issue an error. */ |
12175 | if (namespace_decl == error_mark_node | |
12176 | || TREE_CODE (namespace_decl) != NAMESPACE_DECL) | |
12177 | { | |
166206ce | 12178 | if (!cp_parser_uncommitted_to_tentative_parse_p (parser)) |
69bc6bff | 12179 | error_at (token->location, "%qD is not a namespace-name", identifier); |
a723baf1 MM |
12180 | cp_parser_error (parser, "expected namespace-name"); |
12181 | namespace_decl = error_mark_node; | |
12182 | } | |
21526606 | 12183 | |
a723baf1 MM |
12184 | return namespace_decl; |
12185 | } | |
12186 | ||
12187 | /* Parse a namespace-definition. | |
12188 | ||
12189 | namespace-definition: | |
12190 | named-namespace-definition | |
21526606 | 12191 | unnamed-namespace-definition |
a723baf1 MM |
12192 | |
12193 | named-namespace-definition: | |
12194 | original-namespace-definition | |
12195 | extension-namespace-definition | |
12196 | ||
12197 | original-namespace-definition: | |
12198 | namespace identifier { namespace-body } | |
21526606 | 12199 | |
a723baf1 MM |
12200 | extension-namespace-definition: |
12201 | namespace original-namespace-name { namespace-body } | |
21526606 | 12202 | |
a723baf1 MM |
12203 | unnamed-namespace-definition: |
12204 | namespace { namespace-body } */ | |
12205 | ||
12206 | static void | |
94edc4ab | 12207 | cp_parser_namespace_definition (cp_parser* parser) |
a723baf1 | 12208 | { |
0ed5edac | 12209 | tree identifier, attribs; |
65567efa | 12210 | bool has_visibility; |
4cfaec1c JM |
12211 | bool is_inline; |
12212 | ||
12213 | if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE)) | |
12214 | { | |
12215 | is_inline = true; | |
12216 | cp_lexer_consume_token (parser->lexer); | |
12217 | } | |
12218 | else | |
12219 | is_inline = false; | |
a723baf1 MM |
12220 | |
12221 | /* Look for the `namespace' keyword. */ | |
3a483371 | 12222 | cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>"); |
a723baf1 MM |
12223 | |
12224 | /* Get the name of the namespace. We do not attempt to distinguish | |
12225 | between an original-namespace-definition and an | |
12226 | extension-namespace-definition at this point. The semantic | |
12227 | analysis routines are responsible for that. */ | |
12228 | if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)) | |
12229 | identifier = cp_parser_identifier (parser); | |
12230 | else | |
12231 | identifier = NULL_TREE; | |
12232 | ||
0ed5edac JM |
12233 | /* Parse any specified attributes. */ |
12234 | attribs = cp_parser_attributes_opt (parser); | |
12235 | ||
a723baf1 | 12236 | /* Look for the `{' to start the namespace. */ |
3a483371 | 12237 | cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"); |
a723baf1 | 12238 | /* Start the namespace. */ |
65567efa JM |
12239 | push_namespace (identifier); |
12240 | ||
4cfaec1c JM |
12241 | /* "inline namespace" is equivalent to a stub namespace definition |
12242 | followed by a strong using directive. */ | |
12243 | if (is_inline) | |
12244 | { | |
be93747e | 12245 | tree name_space = current_namespace; |
4cfaec1c | 12246 | /* Set up namespace association. */ |
be93747e KG |
12247 | DECL_NAMESPACE_ASSOCIATIONS (name_space) |
12248 | = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE, | |
12249 | DECL_NAMESPACE_ASSOCIATIONS (name_space)); | |
4cfaec1c JM |
12250 | /* Import the contents of the inline namespace. */ |
12251 | pop_namespace (); | |
be93747e | 12252 | do_using_directive (name_space); |
4cfaec1c JM |
12253 | push_namespace (identifier); |
12254 | } | |
12255 | ||
65567efa JM |
12256 | has_visibility = handle_namespace_attrs (current_namespace, attribs); |
12257 | ||
a723baf1 MM |
12258 | /* Parse the body of the namespace. */ |
12259 | cp_parser_namespace_body (parser); | |
65567efa JM |
12260 | |
12261 | #ifdef HANDLE_PRAGMA_VISIBILITY | |
12262 | if (has_visibility) | |
12263 | pop_visibility (); | |
12264 | #endif | |
12265 | ||
a723baf1 MM |
12266 | /* Finish the namespace. */ |
12267 | pop_namespace (); | |
12268 | /* Look for the final `}'. */ | |
3a483371 | 12269 | cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>"); |
a723baf1 MM |
12270 | } |
12271 | ||
12272 | /* Parse a namespace-body. | |
12273 | ||
12274 | namespace-body: | |
12275 | declaration-seq [opt] */ | |
12276 | ||
12277 | static void | |
94edc4ab | 12278 | cp_parser_namespace_body (cp_parser* parser) |
a723baf1 MM |
12279 | { |
12280 | cp_parser_declaration_seq_opt (parser); | |
12281 | } | |
12282 | ||
12283 | /* Parse a namespace-alias-definition. | |
12284 | ||
12285 | namespace-alias-definition: | |
12286 | namespace identifier = qualified-namespace-specifier ; */ | |
12287 | ||
12288 | static void | |
94edc4ab | 12289 | cp_parser_namespace_alias_definition (cp_parser* parser) |
a723baf1 MM |
12290 | { |
12291 | tree identifier; | |
12292 | tree namespace_specifier; | |
12293 | ||
2b7a3abf DS |
12294 | cp_token *token = cp_lexer_peek_token (parser->lexer); |
12295 | ||
a723baf1 | 12296 | /* Look for the `namespace' keyword. */ |
3a483371 | 12297 | cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>"); |
a723baf1 MM |
12298 | /* Look for the identifier. */ |
12299 | identifier = cp_parser_identifier (parser); | |
12300 | if (identifier == error_mark_node) | |
12301 | return; | |
12302 | /* Look for the `=' token. */ | |
2ce88bea MLI |
12303 | if (!cp_parser_uncommitted_to_tentative_parse_p (parser) |
12304 | && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)) | |
12305 | { | |
69bc6bff | 12306 | error_at (token->location, "%<namespace%> definition is not allowed here"); |
2ce88bea MLI |
12307 | /* Skip the definition. */ |
12308 | cp_lexer_consume_token (parser->lexer); | |
1094da91 SM |
12309 | if (cp_parser_skip_to_closing_brace (parser)) |
12310 | cp_lexer_consume_token (parser->lexer); | |
2ce88bea MLI |
12311 | return; |
12312 | } | |
3a483371 | 12313 | cp_parser_require (parser, CPP_EQ, "%<=%>"); |
a723baf1 | 12314 | /* Look for the qualified-namespace-specifier. */ |
21526606 | 12315 | namespace_specifier |
a723baf1 MM |
12316 | = cp_parser_qualified_namespace_specifier (parser); |
12317 | /* Look for the `;' token. */ | |
3a483371 | 12318 | cp_parser_require (parser, CPP_SEMICOLON, "%<;%>"); |
a723baf1 MM |
12319 | |
12320 | /* Register the alias in the symbol table. */ | |
12321 | do_namespace_alias (identifier, namespace_specifier); | |
12322 | } | |
12323 | ||
12324 | /* Parse a qualified-namespace-specifier. | |
12325 | ||
12326 | qualified-namespace-specifier: | |
12327 | :: [opt] nested-name-specifier [opt] namespace-name | |
12328 | ||
12329 | Returns a NAMESPACE_DECL corresponding to the specified | |
12330 | namespace. */ | |
12331 | ||
12332 | static tree | |
94edc4ab | 12333 | cp_parser_qualified_namespace_specifier (cp_parser* parser) |
a723baf1 MM |
12334 | { |
12335 | /* Look for the optional `::'. */ | |
21526606 | 12336 | cp_parser_global_scope_opt (parser, |
88525e1b | 12337 | /*current_scope_valid_p=*/false); |
a723baf1 MM |
12338 | |
12339 | /* Look for the optional nested-name-specifier. */ | |
12340 | cp_parser_nested_name_specifier_opt (parser, | |
12341 | /*typename_keyword_p=*/false, | |
12342 | /*check_dependency_p=*/true, | |
a668c6ad MM |
12343 | /*type_p=*/false, |
12344 | /*is_declaration=*/true); | |
a723baf1 MM |
12345 | |
12346 | return cp_parser_namespace_name (parser); | |
12347 | } | |
12348 | ||
981114e1 MM |
12349 | /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an |
12350 | access declaration. | |
a723baf1 MM |
12351 | |
12352 | using-declaration: | |
12353 | using typename [opt] :: [opt] nested-name-specifier unqualified-id ; | |
981114e1 | 12354 | using :: unqualified-id ; |
a723baf1 | 12355 | |
981114e1 MM |
12356 | access-declaration: |
12357 | qualified-id ; | |
12358 | ||
12359 | */ | |
12360 | ||
12361 | static bool | |
12362 | cp_parser_using_declaration (cp_parser* parser, | |
12363 | bool access_declaration_p) | |
a723baf1 MM |
12364 | { |
12365 | cp_token *token; | |
12366 | bool typename_p = false; | |
12367 | bool global_scope_p; | |
12368 | tree decl; | |
12369 | tree identifier; | |
ed5f054f | 12370 | tree qscope; |
a723baf1 | 12371 | |
981114e1 MM |
12372 | if (access_declaration_p) |
12373 | cp_parser_parse_tentatively (parser); | |
12374 | else | |
a723baf1 | 12375 | { |
981114e1 | 12376 | /* Look for the `using' keyword. */ |
3a483371 | 12377 | cp_parser_require_keyword (parser, RID_USING, "%<using%>"); |
981114e1 MM |
12378 | |
12379 | /* Peek at the next token. */ | |
12380 | token = cp_lexer_peek_token (parser->lexer); | |
12381 | /* See if it's `typename'. */ | |
12382 | if (token->keyword == RID_TYPENAME) | |
12383 | { | |
12384 | /* Remember that we've seen it. */ | |
12385 | typename_p = true; | |
12386 | /* Consume the `typename' token. */ | |
12387 | cp_lexer_consume_token (parser->lexer); | |
12388 | } | |
a723baf1 MM |
12389 | } |
12390 | ||
12391 | /* Look for the optional global scope qualification. */ | |
21526606 | 12392 | global_scope_p |
a723baf1 | 12393 | = (cp_parser_global_scope_opt (parser, |
88525e1b | 12394 | /*current_scope_valid_p=*/false) |
a723baf1 MM |
12395 | != NULL_TREE); |
12396 | ||
12397 | /* If we saw `typename', or didn't see `::', then there must be a | |
12398 | nested-name-specifier present. */ | |
12399 | if (typename_p || !global_scope_p) | |
21526606 | 12400 | qscope = cp_parser_nested_name_specifier (parser, typename_p, |
ed5f054f AO |
12401 | /*check_dependency_p=*/true, |
12402 | /*type_p=*/false, | |
12403 | /*is_declaration=*/true); | |
a723baf1 MM |
12404 | /* Otherwise, we could be in either of the two productions. In that |
12405 | case, treat the nested-name-specifier as optional. */ | |
12406 | else | |
ed5f054f AO |
12407 | qscope = cp_parser_nested_name_specifier_opt (parser, |
12408 | /*typename_keyword_p=*/false, | |
12409 | /*check_dependency_p=*/true, | |
12410 | /*type_p=*/false, | |
12411 | /*is_declaration=*/true); | |
12412 | if (!qscope) | |
12413 | qscope = global_namespace; | |
a723baf1 | 12414 | |
21864445 MM |
12415 | if (access_declaration_p && cp_parser_error_occurred (parser)) |
12416 | /* Something has already gone wrong; there's no need to parse | |
12417 | further. Since an error has occurred, the return value of | |
12418 | cp_parser_parse_definitely will be false, as required. */ | |
12419 | return cp_parser_parse_definitely (parser); | |
12420 | ||
2b7a3abf | 12421 | token = cp_lexer_peek_token (parser->lexer); |
a723baf1 | 12422 | /* Parse the unqualified-id. */ |
21526606 | 12423 | identifier = cp_parser_unqualified_id (parser, |
a723baf1 | 12424 | /*template_keyword_p=*/false, |
f3c2dfc6 | 12425 | /*check_dependency_p=*/true, |
fa6098f8 | 12426 | /*declarator_p=*/true, |
88525e1b | 12427 | /*optional_p=*/false); |
a723baf1 | 12428 | |
981114e1 MM |
12429 | if (access_declaration_p) |
12430 | { | |
12431 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)) | |
12432 | cp_parser_simulate_error (parser); | |
12433 | if (!cp_parser_parse_definitely (parser)) | |
12434 | return false; | |
12435 | } | |
12436 | ||
a723baf1 MM |
12437 | /* The function we call to handle a using-declaration is different |
12438 | depending on what scope we are in. */ | |
56bbd9d6 | 12439 | if (qscope == error_mark_node || identifier == error_mark_node) |
f3c2dfc6 MM |
12440 | ; |
12441 | else if (TREE_CODE (identifier) != IDENTIFIER_NODE | |
12442 | && TREE_CODE (identifier) != BIT_NOT_EXPR) | |
12443 | /* [namespace.udecl] | |
12444 | ||
12445 | A using declaration shall not name a template-id. */ | |
69bc6bff MLI |
12446 | error_at (token->location, |
12447 | "a template-id may not appear in a using-declaration"); | |
a723baf1 MM |
12448 | else |
12449 | { | |
a5201a91 | 12450 | if (at_class_scope_p ()) |
4eb6d609 | 12451 | { |
f3c2dfc6 | 12452 | /* Create the USING_DECL. */ |
1d786913 | 12453 | decl = do_class_using_decl (parser->scope, identifier); |
4439d02f | 12454 | |
7b3e2d46 | 12455 | if (check_for_bare_parameter_packs (decl)) |
4439d02f DG |
12456 | return false; |
12457 | else | |
12458 | /* Add it to the list of members in this class. */ | |
12459 | finish_member_declaration (decl); | |
4eb6d609 | 12460 | } |
a723baf1 | 12461 | else |
f3c2dfc6 | 12462 | { |
2b7a3abf DS |
12463 | decl = cp_parser_lookup_name_simple (parser, |
12464 | identifier, | |
12465 | token->location); | |
f3c2dfc6 | 12466 | if (decl == error_mark_node) |
2b7a3abf DS |
12467 | cp_parser_name_lookup_error (parser, identifier, |
12468 | decl, NULL, | |
12469 | token->location); | |
7b3e2d46 | 12470 | else if (check_for_bare_parameter_packs (decl)) |
4439d02f | 12471 | return false; |
a5201a91 | 12472 | else if (!at_namespace_scope_p ()) |
ed5f054f | 12473 | do_local_using_decl (decl, qscope, identifier); |
f3c2dfc6 | 12474 | else |
ed5f054f | 12475 | do_toplevel_using_decl (decl, qscope, identifier); |
f3c2dfc6 | 12476 | } |
a723baf1 MM |
12477 | } |
12478 | ||
12479 | /* Look for the final `;'. */ | |
3a483371 | 12480 | cp_parser_require (parser, CPP_SEMICOLON, "%<;%>"); |
981114e1 MM |
12481 | |
12482 | return true; | |
a723baf1 MM |
12483 | } |
12484 | ||
21526606 EC |
12485 | /* Parse a using-directive. |
12486 | ||
a723baf1 MM |
12487 | using-directive: |
12488 | using namespace :: [opt] nested-name-specifier [opt] | |
12489 | namespace-name ; */ | |
12490 | ||
12491 | static void | |
94edc4ab | 12492 | cp_parser_using_directive (cp_parser* parser) |
a723baf1 MM |
12493 | { |
12494 | tree namespace_decl; | |
86098eb8 | 12495 | tree attribs; |
a723baf1 MM |
12496 | |
12497 | /* Look for the `using' keyword. */ | |
3a483371 | 12498 | cp_parser_require_keyword (parser, RID_USING, "%<using%>"); |
a723baf1 | 12499 | /* And the `namespace' keyword. */ |
3a483371 | 12500 | cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>"); |
a723baf1 | 12501 | /* Look for the optional `::' operator. */ |
88525e1b | 12502 | cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false); |
34cd5ae7 | 12503 | /* And the optional nested-name-specifier. */ |
a723baf1 MM |
12504 | cp_parser_nested_name_specifier_opt (parser, |
12505 | /*typename_keyword_p=*/false, | |
12506 | /*check_dependency_p=*/true, | |
a668c6ad MM |
12507 | /*type_p=*/false, |
12508 | /*is_declaration=*/true); | |
a723baf1 MM |
12509 | /* Get the namespace being used. */ |
12510 | namespace_decl = cp_parser_namespace_name (parser); | |
86098eb8 JM |
12511 | /* And any specified attributes. */ |
12512 | attribs = cp_parser_attributes_opt (parser); | |
a723baf1 | 12513 | /* Update the symbol table. */ |
86098eb8 | 12514 | parse_using_directive (namespace_decl, attribs); |
a723baf1 | 12515 | /* Look for the final `;'. */ |
3a483371 | 12516 | cp_parser_require (parser, CPP_SEMICOLON, "%<;%>"); |
a723baf1 MM |
12517 | } |
12518 | ||
12519 | /* Parse an asm-definition. | |
12520 | ||
12521 | asm-definition: | |
21526606 | 12522 | asm ( string-literal ) ; |
a723baf1 MM |
12523 | |
12524 | GNU Extension: | |
12525 | ||
12526 | asm-definition: | |
12527 | asm volatile [opt] ( string-literal ) ; | |
12528 | asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ; | |
12529 | asm volatile [opt] ( string-literal : asm-operand-list [opt] | |
0cbd7506 | 12530 | : asm-operand-list [opt] ) ; |
21526606 | 12531 | asm volatile [opt] ( string-literal : asm-operand-list [opt] |
0cbd7506 MS |
12532 | : asm-operand-list [opt] |
12533 | : asm-operand-list [opt] ) ; */ | |
a723baf1 MM |
12534 | |
12535 | static void | |
94edc4ab | 12536 | cp_parser_asm_definition (cp_parser* parser) |
a723baf1 | 12537 | { |
a723baf1 MM |
12538 | tree string; |
12539 | tree outputs = NULL_TREE; | |
12540 | tree inputs = NULL_TREE; | |
12541 | tree clobbers = NULL_TREE; | |
12542 | tree asm_stmt; | |
12543 | bool volatile_p = false; | |
12544 | bool extended_p = false; | |
f8ff69ea LM |
12545 | bool invalid_inputs_p = false; |
12546 | bool invalid_outputs_p = false; | |
a723baf1 MM |
12547 | |
12548 | /* Look for the `asm' keyword. */ | |
3a483371 | 12549 | cp_parser_require_keyword (parser, RID_ASM, "%<asm%>"); |
a723baf1 MM |
12550 | /* See if the next token is `volatile'. */ |
12551 | if (cp_parser_allow_gnu_extensions_p (parser) | |
12552 | && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE)) | |
12553 | { | |
12554 | /* Remember that we saw the `volatile' keyword. */ | |
12555 | volatile_p = true; | |
12556 | /* Consume the token. */ | |
12557 | cp_lexer_consume_token (parser->lexer); | |
12558 | } | |
12559 | /* Look for the opening `('. */ | |
3a483371 | 12560 | if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>")) |
c162c75e | 12561 | return; |
a723baf1 | 12562 | /* Look for the string. */ |
c162c75e MA |
12563 | string = cp_parser_string_literal (parser, false, false); |
12564 | if (string == error_mark_node) | |
12565 | { | |
12566 | cp_parser_skip_to_closing_parenthesis (parser, true, false, | |
12567 | /*consume_paren=*/true); | |
12568 | return; | |
12569 | } | |
12570 | ||
a723baf1 | 12571 | /* If we're allowing GNU extensions, check for the extended assembly |
21526606 | 12572 | syntax. Unfortunately, the `:' tokens need not be separated by |
a723baf1 MM |
12573 | a space in C, and so, for compatibility, we tolerate that here |
12574 | too. Doing that means that we have to treat the `::' operator as | |
12575 | two `:' tokens. */ | |
12576 | if (cp_parser_allow_gnu_extensions_p (parser) | |
56c7f9c1 | 12577 | && parser->in_function_body |
a723baf1 MM |
12578 | && (cp_lexer_next_token_is (parser->lexer, CPP_COLON) |
12579 | || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))) | |
12580 | { | |
12581 | bool inputs_p = false; | |
12582 | bool clobbers_p = false; | |
12583 | ||
12584 | /* The extended syntax was used. */ | |
12585 | extended_p = true; | |
12586 | ||
12587 | /* Look for outputs. */ | |
12588 | if (cp_lexer_next_token_is (parser->lexer, CPP_COLON)) | |
12589 | { | |
12590 | /* Consume the `:'. */ | |
12591 | cp_lexer_consume_token (parser->lexer); | |
12592 | /* Parse the output-operands. */ | |
21526606 | 12593 | if (cp_lexer_next_token_is_not (parser->lexer, |
a723baf1 MM |
12594 | CPP_COLON) |
12595 | && cp_lexer_next_token_is_not (parser->lexer, | |
8caf4c38 MM |
12596 | CPP_SCOPE) |
12597 | && cp_lexer_next_token_is_not (parser->lexer, | |
12598 | CPP_CLOSE_PAREN)) | |
a723baf1 | 12599 | outputs = cp_parser_asm_operand_list (parser); |
f8ff69ea LM |
12600 | |
12601 | if (outputs == error_mark_node) | |
12602 | invalid_outputs_p = true; | |
a723baf1 MM |
12603 | } |
12604 | /* If the next token is `::', there are no outputs, and the | |
12605 | next token is the beginning of the inputs. */ | |
12606 | else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)) | |
bf5930d4 JB |
12607 | /* The inputs are coming next. */ |
12608 | inputs_p = true; | |
a723baf1 MM |
12609 | |
12610 | /* Look for inputs. */ | |
12611 | if (inputs_p | |
12612 | || cp_lexer_next_token_is (parser->lexer, CPP_COLON)) | |
12613 | { | |
bf5930d4 JB |
12614 | /* Consume the `:' or `::'. */ |
12615 | cp_lexer_consume_token (parser->lexer); | |
a723baf1 | 12616 | /* Parse the output-operands. */ |
21526606 | 12617 | if (cp_lexer_next_token_is_not (parser->lexer, |
a723baf1 | 12618 | CPP_COLON) |
8caf4c38 MM |
12619 | && cp_lexer_next_token_is_not (parser->lexer, |
12620 | CPP_CLOSE_PAREN)) | |
a723baf1 | 12621 | inputs = cp_parser_asm_operand_list (parser); |
f8ff69ea LM |
12622 | |
12623 | if (inputs == error_mark_node) | |
12624 | invalid_inputs_p = true; | |
a723baf1 MM |
12625 | } |
12626 | else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)) | |
12627 | /* The clobbers are coming next. */ | |
12628 | clobbers_p = true; | |
12629 | ||
12630 | /* Look for clobbers. */ | |
21526606 | 12631 | if (clobbers_p |
a723baf1 MM |
12632 | || cp_lexer_next_token_is (parser->lexer, CPP_COLON)) |
12633 | { | |
bf5930d4 JB |
12634 | /* Consume the `:' or `::'. */ |
12635 | cp_lexer_consume_token (parser->lexer); | |
a723baf1 | 12636 | /* Parse the clobbers. */ |
8caf4c38 MM |
12637 | if (cp_lexer_next_token_is_not (parser->lexer, |
12638 | CPP_CLOSE_PAREN)) | |
12639 | clobbers = cp_parser_asm_clobber_list (parser); | |
a723baf1 MM |
12640 | } |
12641 | } | |
12642 | /* Look for the closing `)'. */ | |
3a483371 | 12643 | if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>")) |
a668c6ad MM |
12644 | cp_parser_skip_to_closing_parenthesis (parser, true, false, |
12645 | /*consume_paren=*/true); | |
3a483371 | 12646 | cp_parser_require (parser, CPP_SEMICOLON, "%<;%>"); |
a723baf1 | 12647 | |
f8ff69ea | 12648 | if (!invalid_inputs_p && !invalid_outputs_p) |
a723baf1 | 12649 | { |
f8ff69ea LM |
12650 | /* Create the ASM_EXPR. */ |
12651 | if (parser->in_function_body) | |
ca059043 | 12652 | { |
f8ff69ea LM |
12653 | asm_stmt = finish_asm_stmt (volatile_p, string, outputs, |
12654 | inputs, clobbers); | |
12655 | /* If the extended syntax was not used, mark the ASM_EXPR. */ | |
12656 | if (!extended_p) | |
12657 | { | |
12658 | tree temp = asm_stmt; | |
12659 | if (TREE_CODE (temp) == CLEANUP_POINT_EXPR) | |
12660 | temp = TREE_OPERAND (temp, 0); | |
c8094d83 | 12661 | |
f8ff69ea LM |
12662 | ASM_INPUT_P (temp) = 1; |
12663 | } | |
ca059043 | 12664 | } |
f8ff69ea LM |
12665 | else |
12666 | cgraph_add_asm_node (string); | |
a723baf1 | 12667 | } |
a723baf1 MM |
12668 | } |
12669 | ||
12670 | /* Declarators [gram.dcl.decl] */ | |
12671 | ||
12672 | /* Parse an init-declarator. | |
12673 | ||
12674 | init-declarator: | |
12675 | declarator initializer [opt] | |
12676 | ||
12677 | GNU Extension: | |
12678 | ||
12679 | init-declarator: | |
12680 | declarator asm-specification [opt] attributes [opt] initializer [opt] | |
12681 | ||
4bb8ca28 MM |
12682 | function-definition: |
12683 | decl-specifier-seq [opt] declarator ctor-initializer [opt] | |
21526606 EC |
12684 | function-body |
12685 | decl-specifier-seq [opt] declarator function-try-block | |
4bb8ca28 MM |
12686 | |
12687 | GNU Extension: | |
12688 | ||
12689 | function-definition: | |
21526606 | 12690 | __extension__ function-definition |
4bb8ca28 | 12691 | |
6b648482 MM |
12692 | The DECL_SPECIFIERS apply to this declarator. Returns a |
12693 | representation of the entity declared. If MEMBER_P is TRUE, then | |
12694 | this declarator appears in a class scope. The new DECL created by | |
12695 | this declarator is returned. | |
12696 | ||
12697 | The CHECKS are access checks that should be performed once we know | |
12698 | what entity is being declared (and, therefore, what classes have | |
12699 | befriended it). | |
a723baf1 MM |
12700 | |
12701 | If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and | |
12702 | for a function-definition here as well. If the declarator is a | |
12703 | declarator for a function-definition, *FUNCTION_DEFINITION_P will | |
12704 | be TRUE upon return. By that point, the function-definition will | |
12705 | have been completely parsed. | |
12706 | ||
12707 | FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P | |
12708 | is FALSE. */ | |
12709 | ||
12710 | static tree | |
21526606 | 12711 | cp_parser_init_declarator (cp_parser* parser, |
62d1db17 | 12712 | cp_decl_specifier_seq *decl_specifiers, |
d6b418fa | 12713 | VEC (deferred_access_check,gc)* checks, |
94edc4ab NN |
12714 | bool function_definition_allowed_p, |
12715 | bool member_p, | |
560ad596 | 12716 | int declares_class_or_enum, |
94edc4ab | 12717 | bool* function_definition_p) |
a723baf1 | 12718 | { |
2b7a3abf DS |
12719 | cp_token *token = NULL, *asm_spec_start_token = NULL, |
12720 | *attributes_start_token = NULL; | |
058b15c1 | 12721 | cp_declarator *declarator; |
62d1db17 | 12722 | tree prefix_attributes; |
a723baf1 MM |
12723 | tree attributes; |
12724 | tree asm_specification; | |
12725 | tree initializer; | |
12726 | tree decl = NULL_TREE; | |
12727 | tree scope; | |
b87d79e6 | 12728 | int is_initialized; |
63c9a190 MM |
12729 | /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if |
12730 | initialized with "= ..", CPP_OPEN_PAREN if initialized with | |
12731 | "(...)". */ | |
12732 | enum cpp_ttype initialization_kind; | |
09357846 | 12733 | bool is_direct_init = false; |
39703eb9 | 12734 | bool is_non_constant_init; |
7efa3e22 | 12735 | int ctor_dtor_or_conv_p; |
a723baf1 | 12736 | bool friend_p; |
4514aa8c | 12737 | tree pushed_scope = NULL; |
a723baf1 | 12738 | |
62d1db17 MM |
12739 | /* Gather the attributes that were provided with the |
12740 | decl-specifiers. */ | |
12741 | prefix_attributes = decl_specifiers->attributes; | |
62d1db17 | 12742 | |
a723baf1 MM |
12743 | /* Assume that this is not the declarator for a function |
12744 | definition. */ | |
12745 | if (function_definition_p) | |
12746 | *function_definition_p = false; | |
12747 | ||
12748 | /* Defer access checks while parsing the declarator; we cannot know | |
21526606 | 12749 | what names are accessible until we know what is being |
a723baf1 | 12750 | declared. */ |
cf22909c KL |
12751 | resume_deferring_access_checks (); |
12752 | ||
a723baf1 | 12753 | /* Parse the declarator. */ |
2b7a3abf | 12754 | token = cp_lexer_peek_token (parser->lexer); |
21526606 | 12755 | declarator |
62b8a44e | 12756 | = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED, |
4bb8ca28 | 12757 | &ctor_dtor_or_conv_p, |
db86dd14 MM |
12758 | /*parenthesized_p=*/NULL, |
12759 | /*member_p=*/false); | |
a723baf1 | 12760 | /* Gather up the deferred checks. */ |
cf22909c | 12761 | stop_deferring_access_checks (); |
24c0ef37 | 12762 | |
a723baf1 MM |
12763 | /* If the DECLARATOR was erroneous, there's no need to go |
12764 | further. */ | |
058b15c1 | 12765 | if (declarator == cp_error_declarator) |
cf22909c | 12766 | return error_mark_node; |
a723baf1 | 12767 | |
9ba7a2f2 | 12768 | /* Check that the number of template-parameter-lists is OK. */ |
2b7a3abf DS |
12769 | if (!cp_parser_check_declarator_template_parameters (parser, declarator, |
12770 | token->location)) | |
9ba7a2f2 MM |
12771 | return error_mark_node; |
12772 | ||
fc6a28d7 MM |
12773 | if (declares_class_or_enum & 2) |
12774 | cp_parser_check_for_definition_in_return_type (declarator, | |
760b18ad DS |
12775 | decl_specifiers->type, |
12776 | decl_specifiers->type_location); | |
560ad596 | 12777 | |
a723baf1 MM |
12778 | /* Figure out what scope the entity declared by the DECLARATOR is |
12779 | located in. `grokdeclarator' sometimes changes the scope, so | |
12780 | we compute it now. */ | |
12781 | scope = get_scope_of_declarator (declarator); | |
12782 | ||
12783 | /* If we're allowing GNU extensions, look for an asm-specification | |
12784 | and attributes. */ | |
12785 | if (cp_parser_allow_gnu_extensions_p (parser)) | |
12786 | { | |
12787 | /* Look for an asm-specification. */ | |
2b7a3abf | 12788 | asm_spec_start_token = cp_lexer_peek_token (parser->lexer); |
a723baf1 MM |
12789 | asm_specification = cp_parser_asm_specification_opt (parser); |
12790 | /* And attributes. */ | |
2b7a3abf | 12791 | attributes_start_token = cp_lexer_peek_token (parser->lexer); |
a723baf1 MM |
12792 | attributes = cp_parser_attributes_opt (parser); |
12793 | } | |
12794 | else | |
12795 | { | |
12796 | asm_specification = NULL_TREE; | |
12797 | attributes = NULL_TREE; | |
12798 | } | |
12799 | ||
12800 | /* Peek at the next token. */ | |
12801 | token = cp_lexer_peek_token (parser->lexer); | |
12802 | /* Check to see if the token indicates the start of a | |
12803 | function-definition. */ | |
09357846 JM |
12804 | if (function_declarator_p (declarator) |
12805 | && cp_parser_token_starts_function_definition_p (token)) | |
a723baf1 MM |
12806 | { |
12807 | if (!function_definition_allowed_p) | |
12808 | { | |
12809 | /* If a function-definition should not appear here, issue an | |
12810 | error message. */ | |
12811 | cp_parser_error (parser, | |
12812 | "a function-definition is not allowed here"); | |
12813 | return error_mark_node; | |
12814 | } | |
12815 | else | |
12816 | { | |
402b8cf6 AH |
12817 | location_t func_brace_location |
12818 | = cp_lexer_peek_token (parser->lexer)->location; | |
12819 | ||
a723baf1 MM |
12820 | /* Neither attributes nor an asm-specification are allowed |
12821 | on a function-definition. */ | |
12822 | if (asm_specification) | |
69bc6bff MLI |
12823 | error_at (asm_spec_start_token->location, |
12824 | "an asm-specification is not allowed " | |
12825 | "on a function-definition"); | |
a723baf1 | 12826 | if (attributes) |
69bc6bff MLI |
12827 | error_at (attributes_start_token->location, |
12828 | "attributes are not allowed on a function-definition"); | |
a723baf1 MM |
12829 | /* This is a function-definition. */ |
12830 | *function_definition_p = true; | |
12831 | ||
a723baf1 | 12832 | /* Parse the function definition. */ |
4bb8ca28 MM |
12833 | if (member_p) |
12834 | decl = cp_parser_save_member_function_body (parser, | |
12835 | decl_specifiers, | |
12836 | declarator, | |
12837 | prefix_attributes); | |
12838 | else | |
21526606 | 12839 | decl |
4bb8ca28 MM |
12840 | = (cp_parser_function_definition_from_specifiers_and_declarator |
12841 | (parser, decl_specifiers, prefix_attributes, declarator)); | |
24c0ef37 | 12842 | |
402b8cf6 AH |
12843 | if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl)) |
12844 | { | |
12845 | /* This is where the prologue starts... */ | |
12846 | DECL_STRUCT_FUNCTION (decl)->function_start_locus | |
12847 | = func_brace_location; | |
12848 | } | |
12849 | ||
a723baf1 MM |
12850 | return decl; |
12851 | } | |
12852 | } | |
12853 | ||
12854 | /* [dcl.dcl] | |
12855 | ||
12856 | Only in function declarations for constructors, destructors, and | |
21526606 | 12857 | type conversions can the decl-specifier-seq be omitted. |
a723baf1 MM |
12858 | |
12859 | We explicitly postpone this check past the point where we handle | |
12860 | function-definitions because we tolerate function-definitions | |
12861 | that are missing their return types in some modes. */ | |
62d1db17 | 12862 | if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0) |
a723baf1 | 12863 | { |
21526606 | 12864 | cp_parser_error (parser, |
a723baf1 MM |
12865 | "expected constructor, destructor, or type conversion"); |
12866 | return error_mark_node; | |
12867 | } | |
12868 | ||
09357846 | 12869 | /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */ |
63c9a190 | 12870 | if (token->type == CPP_EQ |
09357846 JM |
12871 | || token->type == CPP_OPEN_PAREN |
12872 | || token->type == CPP_OPEN_BRACE) | |
a723baf1 | 12873 | { |
4ad610c9 | 12874 | is_initialized = SD_INITIALIZED; |
63c9a190 | 12875 | initialization_kind = token->type; |
b87d79e6 JM |
12876 | |
12877 | if (token->type == CPP_EQ | |
12878 | && function_declarator_p (declarator)) | |
12879 | { | |
12880 | cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2); | |
12881 | if (t2->keyword == RID_DEFAULT) | |
4ad610c9 | 12882 | is_initialized = SD_DEFAULTED; |
b87d79e6 | 12883 | else if (t2->keyword == RID_DELETE) |
4ad610c9 | 12884 | is_initialized = SD_DELETED; |
b87d79e6 | 12885 | } |
63c9a190 MM |
12886 | } |
12887 | else | |
12888 | { | |
12889 | /* If the init-declarator isn't initialized and isn't followed by a | |
12890 | `,' or `;', it's not a valid init-declarator. */ | |
12891 | if (token->type != CPP_COMMA | |
12892 | && token->type != CPP_SEMICOLON) | |
12893 | { | |
12894 | cp_parser_error (parser, "expected initializer"); | |
12895 | return error_mark_node; | |
12896 | } | |
4ad610c9 | 12897 | is_initialized = SD_UNINITIALIZED; |
63c9a190 | 12898 | initialization_kind = CPP_EOF; |
a723baf1 MM |
12899 | } |
12900 | ||
12901 | /* Because start_decl has side-effects, we should only call it if we | |
12902 | know we're going ahead. By this point, we know that we cannot | |
12903 | possibly be looking at any other construct. */ | |
12904 | cp_parser_commit_to_tentative_parse (parser); | |
12905 | ||
e90c7b84 ILT |
12906 | /* If the decl specifiers were bad, issue an error now that we're |
12907 | sure this was intended to be a declarator. Then continue | |
12908 | declaring the variable(s), as int, to try to cut down on further | |
12909 | errors. */ | |
62d1db17 MM |
12910 | if (decl_specifiers->any_specifiers_p |
12911 | && decl_specifiers->type == error_mark_node) | |
e90c7b84 ILT |
12912 | { |
12913 | cp_parser_error (parser, "invalid type in declaration"); | |
62d1db17 | 12914 | decl_specifiers->type = integer_type_node; |
e90c7b84 ILT |
12915 | } |
12916 | ||
a723baf1 MM |
12917 | /* Check to see whether or not this declaration is a friend. */ |
12918 | friend_p = cp_parser_friend_p (decl_specifiers); | |
12919 | ||
a723baf1 MM |
12920 | /* Enter the newly declared entry in the symbol table. If we're |
12921 | processing a declaration in a class-specifier, we wait until | |
12922 | after processing the initializer. */ | |
12923 | if (!member_p) | |
12924 | { | |
12925 | if (parser->in_unbraced_linkage_specification_p) | |
75a82978 | 12926 | decl_specifiers->storage_class = sc_extern; |
ee3071ef | 12927 | decl = start_decl (declarator, decl_specifiers, |
73a8adb6 | 12928 | is_initialized, attributes, prefix_attributes, |
4514aa8c | 12929 | &pushed_scope); |
a723baf1 | 12930 | } |
73a8adb6 MM |
12931 | else if (scope) |
12932 | /* Enter the SCOPE. That way unqualified names appearing in the | |
12933 | initializer will be looked up in SCOPE. */ | |
4514aa8c | 12934 | pushed_scope = push_scope (scope); |
a723baf1 MM |
12935 | |
12936 | /* Perform deferred access control checks, now that we know in which | |
12937 | SCOPE the declared entity resides. */ | |
21526606 | 12938 | if (!member_p && decl) |
a723baf1 MM |
12939 | { |
12940 | tree saved_current_function_decl = NULL_TREE; | |
12941 | ||
12942 | /* If the entity being declared is a function, pretend that we | |
12943 | are in its scope. If it is a `friend', it may have access to | |
9bcb9aae | 12944 | things that would not otherwise be accessible. */ |
a723baf1 MM |
12945 | if (TREE_CODE (decl) == FUNCTION_DECL) |
12946 | { | |
12947 | saved_current_function_decl = current_function_decl; | |
12948 | current_function_decl = decl; | |
12949 | } | |
21526606 | 12950 | |
6b648482 MM |
12951 | /* Perform access checks for template parameters. */ |
12952 | cp_parser_perform_template_parameter_access_checks (checks); | |
12953 | ||
cf22909c | 12954 | /* Perform the access control checks for the declarator and the |
39a13be5 | 12955 | decl-specifiers. */ |
cf22909c | 12956 | perform_deferred_access_checks (); |
a723baf1 MM |
12957 | |
12958 | /* Restore the saved value. */ | |
12959 | if (TREE_CODE (decl) == FUNCTION_DECL) | |
12960 | current_function_decl = saved_current_function_decl; | |
12961 | } | |
12962 | ||
12963 | /* Parse the initializer. */ | |
aa9d194e | 12964 | initializer = NULL_TREE; |
09357846 | 12965 | is_direct_init = false; |
aa9d194e | 12966 | is_non_constant_init = true; |
a723baf1 | 12967 | if (is_initialized) |
63c9a190 | 12968 | { |
277264f8 MM |
12969 | if (function_declarator_p (declarator)) |
12970 | { | |
2b7a3abf | 12971 | cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer); |
277264f8 MM |
12972 | if (initialization_kind == CPP_EQ) |
12973 | initializer = cp_parser_pure_specifier (parser); | |
12974 | else | |
12975 | { | |
12976 | /* If the declaration was erroneous, we don't really | |
12977 | know what the user intended, so just silently | |
12978 | consume the initializer. */ | |
12979 | if (decl != error_mark_node) | |
69bc6bff MLI |
12980 | error_at (initializer_start_token->location, |
12981 | "initializer provided for function"); | |
277264f8 MM |
12982 | cp_parser_skip_to_closing_parenthesis (parser, |
12983 | /*recovering=*/true, | |
12984 | /*or_comma=*/false, | |
12985 | /*consume_paren=*/true); | |
12986 | } | |
12987 | } | |
63c9a190 MM |
12988 | else |
12989 | initializer = cp_parser_initializer (parser, | |
09357846 | 12990 | &is_direct_init, |
63c9a190 MM |
12991 | &is_non_constant_init); |
12992 | } | |
a723baf1 MM |
12993 | |
12994 | /* The old parser allows attributes to appear after a parenthesized | |
12995 | initializer. Mark Mitchell proposed removing this functionality | |
12996 | on the GCC mailing lists on 2002-08-13. This parser accepts the | |
12997 | attributes -- but ignores them. */ | |
09357846 JM |
12998 | if (cp_parser_allow_gnu_extensions_p (parser) |
12999 | && initialization_kind == CPP_OPEN_PAREN) | |
a723baf1 | 13000 | if (cp_parser_attributes_opt (parser)) |
5c498b10 DD |
13001 | warning (OPT_Wattributes, |
13002 | "attributes after parenthesized initializer ignored"); | |
a723baf1 | 13003 | |
a723baf1 MM |
13004 | /* For an in-class declaration, use `grokfield' to create the |
13005 | declaration. */ | |
13006 | if (member_p) | |
8db1028e | 13007 | { |
4514aa8c | 13008 | if (pushed_scope) |
62a4d942 | 13009 | { |
4514aa8c NS |
13010 | pop_scope (pushed_scope); |
13011 | pushed_scope = false; | |
62a4d942 | 13012 | } |
8db1028e | 13013 | decl = grokfield (declarator, decl_specifiers, |
d174af6c MM |
13014 | initializer, !is_non_constant_init, |
13015 | /*asmspec=*/NULL_TREE, | |
d08fd9d6 | 13016 | prefix_attributes); |
8db1028e NS |
13017 | if (decl && TREE_CODE (decl) == FUNCTION_DECL) |
13018 | cp_parser_save_default_args (parser, decl); | |
13019 | } | |
21526606 | 13020 | |
a723baf1 MM |
13021 | /* Finish processing the declaration. But, skip friend |
13022 | declarations. */ | |
550205c3 | 13023 | if (!friend_p && decl && decl != error_mark_node) |
73a8adb6 MM |
13024 | { |
13025 | cp_finish_decl (decl, | |
d174af6c | 13026 | initializer, !is_non_constant_init, |
73a8adb6 MM |
13027 | asm_specification, |
13028 | /* If the initializer is in parentheses, then this is | |
13029 | a direct-initialization, which means that an | |
13030 | `explicit' constructor is OK. Otherwise, an | |
13031 | `explicit' constructor cannot be used. */ | |
09357846 JM |
13032 | ((is_direct_init || !is_initialized) |
13033 | ? 0 : LOOKUP_ONLYCONVERTING)); | |
73a8adb6 | 13034 | } |
c1ae8be5 SM |
13035 | else if ((cxx_dialect != cxx98) && friend_p |
13036 | && decl && TREE_CODE (decl) == FUNCTION_DECL) | |
9b7dd5e8 DG |
13037 | /* Core issue #226 (C++0x only): A default template-argument |
13038 | shall not be specified in a friend class template | |
13039 | declaration. */ | |
13040 | check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/1, | |
13041 | /*is_partial=*/0, /*is_friend_decl=*/1); | |
13042 | ||
4514aa8c NS |
13043 | if (!friend_p && pushed_scope) |
13044 | pop_scope (pushed_scope); | |
a723baf1 MM |
13045 | |
13046 | return decl; | |
13047 | } | |
13048 | ||
13049 | /* Parse a declarator. | |
21526606 | 13050 | |
a723baf1 MM |
13051 | declarator: |
13052 | direct-declarator | |
21526606 | 13053 | ptr-operator declarator |
a723baf1 MM |
13054 | |
13055 | abstract-declarator: | |
13056 | ptr-operator abstract-declarator [opt] | |
13057 | direct-abstract-declarator | |
13058 | ||
13059 | GNU Extensions: | |
13060 | ||
13061 | declarator: | |
13062 | attributes [opt] direct-declarator | |
21526606 | 13063 | attributes [opt] ptr-operator declarator |
a723baf1 MM |
13064 | |
13065 | abstract-declarator: | |
13066 | attributes [opt] ptr-operator abstract-declarator [opt] | |
13067 | attributes [opt] direct-abstract-declarator | |
21526606 | 13068 | |
7efa3e22 NS |
13069 | If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to |
13070 | detect constructor, destructor or conversion operators. It is set | |
13071 | to -1 if the declarator is a name, and +1 if it is a | |
13072 | function. Otherwise it is set to zero. Usually you just want to | |
13073 | test for >0, but internally the negative value is used. | |
21526606 | 13074 | |
a723baf1 MM |
13075 | (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have |
13076 | a decl-specifier-seq unless it declares a constructor, destructor, | |
13077 | or conversion. It might seem that we could check this condition in | |
13078 | semantic analysis, rather than parsing, but that makes it difficult | |
13079 | to handle something like `f()'. We want to notice that there are | |
13080 | no decl-specifiers, and therefore realize that this is an | |
21526606 EC |
13081 | expression, not a declaration.) |
13082 | ||
4bb8ca28 | 13083 | If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff |
c8094d83 | 13084 | the declarator is a direct-declarator of the form "(...)". |
db86dd14 MM |
13085 | |
13086 | MEMBER_P is true iff this declarator is a member-declarator. */ | |
a723baf1 | 13087 | |
058b15c1 | 13088 | static cp_declarator * |
21526606 | 13089 | cp_parser_declarator (cp_parser* parser, |
0cbd7506 MS |
13090 | cp_parser_declarator_kind dcl_kind, |
13091 | int* ctor_dtor_or_conv_p, | |
db86dd14 MM |
13092 | bool* parenthesized_p, |
13093 | bool member_p) | |
a723baf1 MM |
13094 | { |
13095 | cp_token *token; | |
058b15c1 | 13096 | cp_declarator *declarator; |
a723baf1 | 13097 | enum tree_code code; |
3c01e5df | 13098 | cp_cv_quals cv_quals; |
a723baf1 MM |
13099 | tree class_type; |
13100 | tree attributes = NULL_TREE; | |
13101 | ||
13102 | /* Assume this is not a constructor, destructor, or type-conversion | |
13103 | operator. */ | |
13104 | if (ctor_dtor_or_conv_p) | |
7efa3e22 | 13105 | *ctor_dtor_or_conv_p = 0; |
a723baf1 MM |
13106 | |
13107 | if (cp_parser_allow_gnu_extensions_p (parser)) | |
13108 | attributes = cp_parser_attributes_opt (parser); | |
21526606 | 13109 | |
a723baf1 MM |
13110 | /* Peek at the next token. */ |
13111 | token = cp_lexer_peek_token (parser->lexer); | |
21526606 | 13112 | |
a723baf1 MM |
13113 | /* Check for the ptr-operator production. */ |
13114 | cp_parser_parse_tentatively (parser); | |
13115 | /* Parse the ptr-operator. */ | |
21526606 EC |
13116 | code = cp_parser_ptr_operator (parser, |
13117 | &class_type, | |
3c01e5df | 13118 | &cv_quals); |
a723baf1 MM |
13119 | /* If that worked, then we have a ptr-operator. */ |
13120 | if (cp_parser_parse_definitely (parser)) | |
13121 | { | |
4bb8ca28 MM |
13122 | /* If a ptr-operator was found, then this declarator was not |
13123 | parenthesized. */ | |
13124 | if (parenthesized_p) | |
13125 | *parenthesized_p = true; | |
a723baf1 MM |
13126 | /* The dependent declarator is optional if we are parsing an |
13127 | abstract-declarator. */ | |
62b8a44e | 13128 | if (dcl_kind != CP_PARSER_DECLARATOR_NAMED) |
a723baf1 MM |
13129 | cp_parser_parse_tentatively (parser); |
13130 | ||
13131 | /* Parse the dependent declarator. */ | |
62b8a44e | 13132 | declarator = cp_parser_declarator (parser, dcl_kind, |
4bb8ca28 | 13133 | /*ctor_dtor_or_conv_p=*/NULL, |
db86dd14 MM |
13134 | /*parenthesized_p=*/NULL, |
13135 | /*member_p=*/false); | |
a723baf1 MM |
13136 | |
13137 | /* If we are parsing an abstract-declarator, we must handle the | |
13138 | case where the dependent declarator is absent. */ | |
62b8a44e NS |
13139 | if (dcl_kind != CP_PARSER_DECLARATOR_NAMED |
13140 | && !cp_parser_parse_definitely (parser)) | |
058b15c1 | 13141 | declarator = NULL; |
21526606 | 13142 | |
8af2fec4 RY |
13143 | declarator = cp_parser_make_indirect_declarator |
13144 | (code, class_type, cv_quals, declarator); | |
a723baf1 MM |
13145 | } |
13146 | /* Everything else is a direct-declarator. */ | |
13147 | else | |
4bb8ca28 MM |
13148 | { |
13149 | if (parenthesized_p) | |
13150 | *parenthesized_p = cp_lexer_next_token_is (parser->lexer, | |
13151 | CPP_OPEN_PAREN); | |
13152 | declarator = cp_parser_direct_declarator (parser, dcl_kind, | |
db86dd14 MM |
13153 | ctor_dtor_or_conv_p, |
13154 | member_p); | |
4bb8ca28 | 13155 | } |
a723baf1 | 13156 | |
84019f23 | 13157 | if (attributes && declarator && declarator != cp_error_declarator) |
058b15c1 | 13158 | declarator->attributes = attributes; |
21526606 | 13159 | |
a723baf1 MM |
13160 | return declarator; |
13161 | } | |
13162 | ||
13163 | /* Parse a direct-declarator or direct-abstract-declarator. | |
13164 | ||
13165 | direct-declarator: | |
13166 | declarator-id | |
13167 | direct-declarator ( parameter-declaration-clause ) | |
21526606 | 13168 | cv-qualifier-seq [opt] |
a723baf1 MM |
13169 | exception-specification [opt] |
13170 | direct-declarator [ constant-expression [opt] ] | |
21526606 | 13171 | ( declarator ) |
a723baf1 MM |
13172 | |
13173 | direct-abstract-declarator: | |
13174 | direct-abstract-declarator [opt] | |
21526606 | 13175 | ( parameter-declaration-clause ) |
a723baf1 MM |
13176 | cv-qualifier-seq [opt] |
13177 | exception-specification [opt] | |
13178 | direct-abstract-declarator [opt] [ constant-expression [opt] ] | |
13179 | ( abstract-declarator ) | |
13180 | ||
62b8a44e NS |
13181 | Returns a representation of the declarator. DCL_KIND is |
13182 | CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a | |
13183 | direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if | |
13184 | we are parsing a direct-declarator. It is | |
13185 | CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case | |
13186 | of ambiguity we prefer an abstract declarator, as per | |
db86dd14 | 13187 | [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for |
058b15c1 | 13188 | cp_parser_declarator. */ |
a723baf1 | 13189 | |
058b15c1 | 13190 | static cp_declarator * |
94edc4ab | 13191 | cp_parser_direct_declarator (cp_parser* parser, |
0cbd7506 MS |
13192 | cp_parser_declarator_kind dcl_kind, |
13193 | int* ctor_dtor_or_conv_p, | |
db86dd14 | 13194 | bool member_p) |
a723baf1 MM |
13195 | { |
13196 | cp_token *token; | |
058b15c1 | 13197 | cp_declarator *declarator = NULL; |
a723baf1 MM |
13198 | tree scope = NULL_TREE; |
13199 | bool saved_default_arg_ok_p = parser->default_arg_ok_p; | |
13200 | bool saved_in_declarator_p = parser->in_declarator_p; | |
62b8a44e | 13201 | bool first = true; |
4514aa8c | 13202 | tree pushed_scope = NULL_TREE; |
21526606 | 13203 | |
62b8a44e | 13204 | while (true) |
a723baf1 | 13205 | { |
62b8a44e NS |
13206 | /* Peek at the next token. */ |
13207 | token = cp_lexer_peek_token (parser->lexer); | |
13208 | if (token->type == CPP_OPEN_PAREN) | |
a723baf1 | 13209 | { |
62b8a44e | 13210 | /* This is either a parameter-declaration-clause, or a |
0cbd7506 MS |
13211 | parenthesized declarator. When we know we are parsing a |
13212 | named declarator, it must be a parenthesized declarator | |
13213 | if FIRST is true. For instance, `(int)' is a | |
13214 | parameter-declaration-clause, with an omitted | |
13215 | direct-abstract-declarator. But `((*))', is a | |
13216 | parenthesized abstract declarator. Finally, when T is a | |
13217 | template parameter `(T)' is a | |
13218 | parameter-declaration-clause, and not a parenthesized | |
13219 | named declarator. | |
21526606 | 13220 | |
62b8a44e NS |
13221 | We first try and parse a parameter-declaration-clause, |
13222 | and then try a nested declarator (if FIRST is true). | |
a723baf1 | 13223 | |
62b8a44e NS |
13224 | It is not an error for it not to be a |
13225 | parameter-declaration-clause, even when FIRST is | |
13226 | false. Consider, | |
13227 | ||
13228 | int i (int); | |
13229 | int i (3); | |
13230 | ||
13231 | The first is the declaration of a function while the | |
39a13be5 | 13232 | second is the definition of a variable, including its |
62b8a44e NS |
13233 | initializer. |
13234 | ||
13235 | Having seen only the parenthesis, we cannot know which of | |
13236 | these two alternatives should be selected. Even more | |
13237 | complex are examples like: | |
13238 | ||
0cbd7506 | 13239 | int i (int (a)); |
62b8a44e NS |
13240 | int i (int (3)); |
13241 | ||
13242 | The former is a function-declaration; the latter is a | |
21526606 | 13243 | variable initialization. |
62b8a44e | 13244 | |
34cd5ae7 | 13245 | Thus again, we try a parameter-declaration-clause, and if |
62b8a44e NS |
13246 | that fails, we back out and return. */ |
13247 | ||
13248 | if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED) | |
a723baf1 | 13249 | { |
b344d949 | 13250 | tree params; |
4047b164 | 13251 | unsigned saved_num_template_parameter_lists; |
b344d949 JM |
13252 | bool is_declarator = false; |
13253 | tree t; | |
21526606 | 13254 | |
db86dd14 MM |
13255 | /* In a member-declarator, the only valid interpretation |
13256 | of a parenthesis is the start of a | |
13257 | parameter-declaration-clause. (It is invalid to | |
13258 | initialize a static data member with a parenthesized | |
13259 | initializer; only the "=" form of initialization is | |
13260 | permitted.) */ | |
13261 | if (!member_p) | |
13262 | cp_parser_parse_tentatively (parser); | |
a723baf1 | 13263 | |
62b8a44e NS |
13264 | /* Consume the `('. */ |
13265 | cp_lexer_consume_token (parser->lexer); | |
13266 | if (first) | |
13267 | { | |
13268 | /* If this is going to be an abstract declarator, we're | |
13269 | in a declarator and we can't have default args. */ | |
13270 | parser->default_arg_ok_p = false; | |
13271 | parser->in_declarator_p = true; | |
13272 | } | |
21526606 | 13273 | |
4047b164 KL |
13274 | /* Inside the function parameter list, surrounding |
13275 | template-parameter-lists do not apply. */ | |
13276 | saved_num_template_parameter_lists | |
13277 | = parser->num_template_parameter_lists; | |
13278 | parser->num_template_parameter_lists = 0; | |
13279 | ||
b344d949 JM |
13280 | begin_scope (sk_function_parms, NULL_TREE); |
13281 | ||
62b8a44e NS |
13282 | /* Parse the parameter-declaration-clause. */ |
13283 | params = cp_parser_parameter_declaration_clause (parser); | |
13284 | ||
4047b164 KL |
13285 | parser->num_template_parameter_lists |
13286 | = saved_num_template_parameter_lists; | |
13287 | ||
62b8a44e | 13288 | /* If all went well, parse the cv-qualifier-seq and the |
0cbd7506 | 13289 | exception-specification. */ |
db86dd14 | 13290 | if (member_p || cp_parser_parse_definitely (parser)) |
62b8a44e | 13291 | { |
3c01e5df | 13292 | cp_cv_quals cv_quals; |
62b8a44e | 13293 | tree exception_specification; |
f57ca1ea | 13294 | tree late_return; |
7efa3e22 | 13295 | |
b344d949 JM |
13296 | is_declarator = true; |
13297 | ||
7efa3e22 NS |
13298 | if (ctor_dtor_or_conv_p) |
13299 | *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0; | |
62b8a44e NS |
13300 | first = false; |
13301 | /* Consume the `)'. */ | |
3a483371 | 13302 | cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"); |
62b8a44e NS |
13303 | |
13304 | /* Parse the cv-qualifier-seq. */ | |
3c01e5df | 13305 | cv_quals = cp_parser_cv_qualifier_seq_opt (parser); |
62b8a44e | 13306 | /* And the exception-specification. */ |
21526606 | 13307 | exception_specification |
62b8a44e NS |
13308 | = cp_parser_exception_specification_opt (parser); |
13309 | ||
f57ca1ea JM |
13310 | late_return |
13311 | = cp_parser_late_return_type_opt (parser); | |
13312 | ||
62b8a44e NS |
13313 | /* Create the function-declarator. */ |
13314 | declarator = make_call_declarator (declarator, | |
13315 | params, | |
3c01e5df | 13316 | cv_quals, |
f57ca1ea JM |
13317 | exception_specification, |
13318 | late_return); | |
62b8a44e | 13319 | /* Any subsequent parameter lists are to do with |
0cbd7506 MS |
13320 | return type, so are not those of the declared |
13321 | function. */ | |
62b8a44e | 13322 | parser->default_arg_ok_p = false; |
62b8a44e | 13323 | } |
b344d949 JM |
13324 | |
13325 | /* Remove the function parms from scope. */ | |
13326 | for (t = current_binding_level->names; t; t = TREE_CHAIN (t)) | |
13327 | pop_binding (DECL_NAME (t), t); | |
13328 | leave_scope(); | |
13329 | ||
13330 | if (is_declarator) | |
13331 | /* Repeat the main loop. */ | |
13332 | continue; | |
62b8a44e | 13333 | } |
21526606 | 13334 | |
62b8a44e NS |
13335 | /* If this is the first, we can try a parenthesized |
13336 | declarator. */ | |
13337 | if (first) | |
a723baf1 | 13338 | { |
a7324e75 MM |
13339 | bool saved_in_type_id_in_expr_p; |
13340 | ||
a723baf1 | 13341 | parser->default_arg_ok_p = saved_default_arg_ok_p; |
62b8a44e | 13342 | parser->in_declarator_p = saved_in_declarator_p; |
21526606 | 13343 | |
62b8a44e NS |
13344 | /* Consume the `('. */ |
13345 | cp_lexer_consume_token (parser->lexer); | |
13346 | /* Parse the nested declarator. */ | |
a7324e75 MM |
13347 | saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p; |
13348 | parser->in_type_id_in_expr_p = true; | |
21526606 | 13349 | declarator |
4bb8ca28 | 13350 | = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p, |
db86dd14 MM |
13351 | /*parenthesized_p=*/NULL, |
13352 | member_p); | |
a7324e75 | 13353 | parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p; |
62b8a44e NS |
13354 | first = false; |
13355 | /* Expect a `)'. */ | |
3a483371 | 13356 | if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>")) |
058b15c1 MM |
13357 | declarator = cp_error_declarator; |
13358 | if (declarator == cp_error_declarator) | |
62b8a44e | 13359 | break; |
21526606 | 13360 | |
62b8a44e | 13361 | goto handle_declarator; |
a723baf1 | 13362 | } |
9bcb9aae | 13363 | /* Otherwise, we must be done. */ |
62b8a44e NS |
13364 | else |
13365 | break; | |
a723baf1 | 13366 | } |
62b8a44e NS |
13367 | else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED) |
13368 | && token->type == CPP_OPEN_SQUARE) | |
a723baf1 | 13369 | { |
62b8a44e | 13370 | /* Parse an array-declarator. */ |
a723baf1 MM |
13371 | tree bounds; |
13372 | ||
7efa3e22 NS |
13373 | if (ctor_dtor_or_conv_p) |
13374 | *ctor_dtor_or_conv_p = 0; | |
21526606 | 13375 | |
62b8a44e NS |
13376 | first = false; |
13377 | parser->default_arg_ok_p = false; | |
13378 | parser->in_declarator_p = true; | |
a723baf1 MM |
13379 | /* Consume the `['. */ |
13380 | cp_lexer_consume_token (parser->lexer); | |
13381 | /* Peek at the next token. */ | |
13382 | token = cp_lexer_peek_token (parser->lexer); | |
13383 | /* If the next token is `]', then there is no | |
13384 | constant-expression. */ | |
13385 | if (token->type != CPP_CLOSE_SQUARE) | |
14d22dd6 MM |
13386 | { |
13387 | bool non_constant_p; | |
13388 | ||
21526606 | 13389 | bounds |
14d22dd6 MM |
13390 | = cp_parser_constant_expression (parser, |
13391 | /*allow_non_constant=*/true, | |
13392 | &non_constant_p); | |
d17811fd | 13393 | if (!non_constant_p) |
9baa27a9 | 13394 | bounds = fold_non_dependent_expr (bounds); |
88e95ee3 MM |
13395 | /* Normally, the array bound must be an integral constant |
13396 | expression. However, as an extension, we allow VLAs | |
c8094d83 | 13397 | in function scopes. */ |
56c7f9c1 | 13398 | else if (!parser->in_function_body) |
44370687 | 13399 | { |
69bc6bff MLI |
13400 | error_at (token->location, |
13401 | "array bound is not an integer constant"); | |
44370687 MM |
13402 | bounds = error_mark_node; |
13403 | } | |
85a988d1 JJ |
13404 | else if (processing_template_decl && !error_operand_p (bounds)) |
13405 | { | |
13406 | /* Remember this wasn't a constant-expression. */ | |
13407 | bounds = build_nop (TREE_TYPE (bounds), bounds); | |
13408 | TREE_SIDE_EFFECTS (bounds) = 1; | |
13409 | } | |
14d22dd6 | 13410 | } |
a723baf1 MM |
13411 | else |
13412 | bounds = NULL_TREE; | |
13413 | /* Look for the closing `]'. */ | |
3a483371 | 13414 | if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>")) |
62b8a44e | 13415 | { |
058b15c1 | 13416 | declarator = cp_error_declarator; |
62b8a44e NS |
13417 | break; |
13418 | } | |
a723baf1 | 13419 | |
058b15c1 | 13420 | declarator = make_array_declarator (declarator, bounds); |
a723baf1 | 13421 | } |
62b8a44e | 13422 | else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT) |
a723baf1 | 13423 | { |
e1b7793c ILT |
13424 | { |
13425 | tree qualifying_scope; | |
13426 | tree unqualified_name; | |
13427 | special_function_kind sfk; | |
13428 | bool abstract_ok; | |
13429 | bool pack_expansion_p = false; | |
13430 | cp_token *declarator_id_start_token; | |
13431 | ||
13432 | /* Parse a declarator-id */ | |
13433 | abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER); | |
13434 | if (abstract_ok) | |
13435 | { | |
13436 | cp_parser_parse_tentatively (parser); | |
5d80a306 | 13437 | |
e1b7793c ILT |
13438 | /* If we see an ellipsis, we should be looking at a |
13439 | parameter pack. */ | |
13440 | if (token->type == CPP_ELLIPSIS) | |
13441 | { | |
13442 | /* Consume the `...' */ | |
13443 | cp_lexer_consume_token (parser->lexer); | |
5d80a306 | 13444 | |
e1b7793c ILT |
13445 | pack_expansion_p = true; |
13446 | } | |
13447 | } | |
5d80a306 | 13448 | |
e1b7793c ILT |
13449 | declarator_id_start_token = cp_lexer_peek_token (parser->lexer); |
13450 | unqualified_name | |
13451 | = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok); | |
13452 | qualifying_scope = parser->scope; | |
13453 | if (abstract_ok) | |
13454 | { | |
13455 | bool okay = false; | |
21526606 | 13456 | |
e1b7793c ILT |
13457 | if (!unqualified_name && pack_expansion_p) |
13458 | { | |
13459 | /* Check whether an error occurred. */ | |
13460 | okay = !cp_parser_error_occurred (parser); | |
13461 | ||
13462 | /* We already consumed the ellipsis to mark a | |
13463 | parameter pack, but we have no way to report it, | |
13464 | so abort the tentative parse. We will be exiting | |
13465 | immediately anyway. */ | |
13466 | cp_parser_abort_tentative_parse (parser); | |
13467 | } | |
13468 | else | |
13469 | okay = cp_parser_parse_definitely (parser); | |
21526606 | 13470 | |
e1b7793c ILT |
13471 | if (!okay) |
13472 | unqualified_name = error_mark_node; | |
13473 | else if (unqualified_name | |
13474 | && (qualifying_scope | |
13475 | || (TREE_CODE (unqualified_name) | |
13476 | != IDENTIFIER_NODE))) | |
13477 | { | |
13478 | cp_parser_error (parser, "expected unqualified-id"); | |
13479 | unqualified_name = error_mark_node; | |
13480 | } | |
13481 | } | |
21526606 | 13482 | |
e1b7793c ILT |
13483 | if (!unqualified_name) |
13484 | return NULL; | |
13485 | if (unqualified_name == error_mark_node) | |
13486 | { | |
13487 | declarator = cp_error_declarator; | |
13488 | pack_expansion_p = false; | |
13489 | declarator->parameter_pack_p = false; | |
13490 | break; | |
13491 | } | |
5d80a306 | 13492 | |
e1b7793c ILT |
13493 | if (qualifying_scope && at_namespace_scope_p () |
13494 | && TREE_CODE (qualifying_scope) == TYPENAME_TYPE) | |
13495 | { | |
13496 | /* In the declaration of a member of a template class | |
13497 | outside of the class itself, the SCOPE will sometimes | |
13498 | be a TYPENAME_TYPE. For example, given: | |
13499 | ||
13500 | template <typename T> | |
13501 | int S<T>::R::i = 3; | |
13502 | ||
13503 | the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In | |
13504 | this context, we must resolve S<T>::R to an ordinary | |
13505 | type, rather than a typename type. | |
13506 | ||
13507 | The reason we normally avoid resolving TYPENAME_TYPEs | |
13508 | is that a specialization of `S' might render | |
13509 | `S<T>::R' not a type. However, if `S' is | |
13510 | specialized, then this `i' will not be used, so there | |
13511 | is no harm in resolving the types here. */ | |
13512 | tree type; | |
13513 | ||
13514 | /* Resolve the TYPENAME_TYPE. */ | |
13515 | type = resolve_typename_type (qualifying_scope, | |
13516 | /*only_current_p=*/false); | |
13517 | /* If that failed, the declarator is invalid. */ | |
13518 | if (TREE_CODE (type) == TYPENAME_TYPE) | |
69bc6bff MLI |
13519 | error_at (declarator_id_start_token->location, |
13520 | "%<%T::%E%> is not a type", | |
13521 | TYPE_CONTEXT (qualifying_scope), | |
13522 | TYPE_IDENTIFIER (qualifying_scope)); | |
e1b7793c ILT |
13523 | qualifying_scope = type; |
13524 | } | |
62b8a44e | 13525 | |
e1b7793c | 13526 | sfk = sfk_none; |
62b8a44e | 13527 | |
e1b7793c ILT |
13528 | if (unqualified_name) |
13529 | { | |
13530 | tree class_type; | |
d85d3d57 | 13531 | |
e1b7793c ILT |
13532 | if (qualifying_scope |
13533 | && CLASS_TYPE_P (qualifying_scope)) | |
13534 | class_type = qualifying_scope; | |
13535 | else | |
13536 | class_type = current_class_type; | |
d85d3d57 | 13537 | |
e1b7793c ILT |
13538 | if (TREE_CODE (unqualified_name) == TYPE_DECL) |
13539 | { | |
13540 | tree name_type = TREE_TYPE (unqualified_name); | |
13541 | if (class_type && same_type_p (name_type, class_type)) | |
13542 | { | |
13543 | if (qualifying_scope | |
13544 | && CLASSTYPE_USE_TEMPLATE (name_type)) | |
13545 | { | |
69bc6bff MLI |
13546 | error_at (declarator_id_start_token->location, |
13547 | "invalid use of constructor as a template"); | |
13548 | inform (declarator_id_start_token->location, | |
13549 | "use %<%T::%D%> instead of %<%T::%D%> to " | |
e1b7793c ILT |
13550 | "name the constructor in a qualified name", |
13551 | class_type, | |
13552 | DECL_NAME (TYPE_TI_TEMPLATE (class_type)), | |
13553 | class_type, name_type); | |
13554 | declarator = cp_error_declarator; | |
13555 | break; | |
13556 | } | |
13557 | else | |
13558 | unqualified_name = constructor_name (class_type); | |
13559 | } | |
13560 | else | |
13561 | { | |
13562 | /* We do not attempt to print the declarator | |
13563 | here because we do not have enough | |
13564 | information about its original syntactic | |
13565 | form. */ | |
13566 | cp_parser_error (parser, "invalid declarator"); | |
13567 | declarator = cp_error_declarator; | |
13568 | break; | |
13569 | } | |
13570 | } | |
5d80a306 | 13571 | |
e1b7793c ILT |
13572 | if (class_type) |
13573 | { | |
13574 | if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR) | |
13575 | sfk = sfk_destructor; | |
13576 | else if (IDENTIFIER_TYPENAME_P (unqualified_name)) | |
13577 | sfk = sfk_conversion; | |
13578 | else if (/* There's no way to declare a constructor | |
13579 | for an anonymous type, even if the type | |
13580 | got a name for linkage purposes. */ | |
13581 | !TYPE_WAS_ANONYMOUS (class_type) | |
13582 | && constructor_name_p (unqualified_name, | |
13583 | class_type)) | |
13584 | { | |
13585 | unqualified_name = constructor_name (class_type); | |
13586 | sfk = sfk_constructor; | |
13587 | } | |
13588 | ||
13589 | if (ctor_dtor_or_conv_p && sfk != sfk_none) | |
13590 | *ctor_dtor_or_conv_p = -1; | |
13591 | } | |
13592 | } | |
13593 | declarator = make_id_declarator (qualifying_scope, | |
13594 | unqualified_name, | |
13595 | sfk); | |
13596 | declarator->id_loc = token->location; | |
13597 | declarator->parameter_pack_p = pack_expansion_p; | |
13598 | ||
13599 | if (pack_expansion_p) | |
13600 | maybe_warn_variadic_templates (); | |
13601 | } | |
62b8a44e NS |
13602 | |
13603 | handle_declarator:; | |
13604 | scope = get_scope_of_declarator (declarator); | |
13605 | if (scope) | |
91b004e5 MM |
13606 | /* Any names that appear after the declarator-id for a |
13607 | member are looked up in the containing scope. */ | |
4514aa8c | 13608 | pushed_scope = push_scope (scope); |
62b8a44e NS |
13609 | parser->in_declarator_p = true; |
13610 | if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p) | |
058b15c1 | 13611 | || (declarator && declarator->kind == cdk_id)) |
62b8a44e NS |
13612 | /* Default args are only allowed on function |
13613 | declarations. */ | |
13614 | parser->default_arg_ok_p = saved_default_arg_ok_p; | |
a723baf1 | 13615 | else |
62b8a44e NS |
13616 | parser->default_arg_ok_p = false; |
13617 | ||
13618 | first = false; | |
a723baf1 | 13619 | } |
62b8a44e | 13620 | /* We're done. */ |
a723baf1 MM |
13621 | else |
13622 | break; | |
a723baf1 MM |
13623 | } |
13624 | ||
13625 | /* For an abstract declarator, we might wind up with nothing at this | |
13626 | point. That's an error; the declarator is not optional. */ | |
13627 | if (!declarator) | |
13628 | cp_parser_error (parser, "expected declarator"); | |
13629 | ||
13630 | /* If we entered a scope, we must exit it now. */ | |
4514aa8c NS |
13631 | if (pushed_scope) |
13632 | pop_scope (pushed_scope); | |
a723baf1 MM |
13633 | |
13634 | parser->default_arg_ok_p = saved_default_arg_ok_p; | |
13635 | parser->in_declarator_p = saved_in_declarator_p; | |
21526606 | 13636 | |
a723baf1 MM |
13637 | return declarator; |
13638 | } | |
13639 | ||
21526606 | 13640 | /* Parse a ptr-operator. |
a723baf1 MM |
13641 | |
13642 | ptr-operator: | |
13643 | * cv-qualifier-seq [opt] | |
13644 | & | |
13645 | :: [opt] nested-name-specifier * cv-qualifier-seq [opt] | |
13646 | ||
13647 | GNU Extension: | |
13648 | ||
13649 | ptr-operator: | |
13650 | & cv-qualifier-seq [opt] | |
13651 | ||
3c01e5df | 13652 | Returns INDIRECT_REF if a pointer, or pointer-to-member, was used. |
8af2fec4 RY |
13653 | Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for |
13654 | an rvalue reference. In the case of a pointer-to-member, *TYPE is | |
13655 | filled in with the TYPE containing the member. *CV_QUALS is | |
13656 | filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there | |
13657 | are no cv-qualifiers. Returns ERROR_MARK if an error occurred. | |
13658 | Note that the tree codes returned by this function have nothing | |
13659 | to do with the types of trees that will be eventually be created | |
13660 | to represent the pointer or reference type being parsed. They are | |
13661 | just constants with suggestive names. */ | |
a723baf1 | 13662 | static enum tree_code |
21526606 | 13663 | cp_parser_ptr_operator (cp_parser* parser, |
0cbd7506 | 13664 | tree* type, |
3c01e5df | 13665 | cp_cv_quals *cv_quals) |
a723baf1 MM |
13666 | { |
13667 | enum tree_code code = ERROR_MARK; | |
13668 | cp_token *token; | |
13669 | ||
13670 | /* Assume that it's not a pointer-to-member. */ | |
13671 | *type = NULL_TREE; | |
13672 | /* And that there are no cv-qualifiers. */ | |
3c01e5df | 13673 | *cv_quals = TYPE_UNQUALIFIED; |
a723baf1 MM |
13674 | |
13675 | /* Peek at the next token. */ | |
13676 | token = cp_lexer_peek_token (parser->lexer); | |
a723baf1 | 13677 | |
8af2fec4 RY |
13678 | /* If it's a `*', `&' or `&&' we have a pointer or reference. */ |
13679 | if (token->type == CPP_MULT) | |
13680 | code = INDIRECT_REF; | |
13681 | else if (token->type == CPP_AND) | |
13682 | code = ADDR_EXPR; | |
c1ae8be5 SM |
13683 | else if ((cxx_dialect != cxx98) && |
13684 | token->type == CPP_AND_AND) /* C++0x only */ | |
8af2fec4 RY |
13685 | code = NON_LVALUE_EXPR; |
13686 | ||
13687 | if (code != ERROR_MARK) | |
13688 | { | |
13689 | /* Consume the `*', `&' or `&&'. */ | |
a723baf1 MM |
13690 | cp_lexer_consume_token (parser->lexer); |
13691 | ||
13692 | /* A `*' can be followed by a cv-qualifier-seq, and so can a | |
13693 | `&', if we are allowing GNU extensions. (The only qualifier | |
13694 | that can legally appear after `&' is `restrict', but that is | |
13695 | enforced during semantic analysis. */ | |
21526606 | 13696 | if (code == INDIRECT_REF |
a723baf1 | 13697 | || cp_parser_allow_gnu_extensions_p (parser)) |
3c01e5df | 13698 | *cv_quals = cp_parser_cv_qualifier_seq_opt (parser); |
a723baf1 MM |
13699 | } |
13700 | else | |
13701 | { | |
13702 | /* Try the pointer-to-member case. */ | |
13703 | cp_parser_parse_tentatively (parser); | |
13704 | /* Look for the optional `::' operator. */ | |
13705 | cp_parser_global_scope_opt (parser, | |
88525e1b | 13706 | /*current_scope_valid_p=*/false); |
a723baf1 | 13707 | /* Look for the nested-name specifier. */ |
2b7a3abf | 13708 | token = cp_lexer_peek_token (parser->lexer); |
a723baf1 MM |
13709 | cp_parser_nested_name_specifier (parser, |
13710 | /*typename_keyword_p=*/false, | |
13711 | /*check_dependency_p=*/true, | |
a668c6ad MM |
13712 | /*type_p=*/false, |
13713 | /*is_declaration=*/false); | |
a723baf1 MM |
13714 | /* If we found it, and the next token is a `*', then we are |
13715 | indeed looking at a pointer-to-member operator. */ | |
13716 | if (!cp_parser_error_occurred (parser) | |
3a483371 | 13717 | && cp_parser_require (parser, CPP_MULT, "%<*%>")) |
a723baf1 | 13718 | { |
a723baf1 MM |
13719 | /* Indicate that the `*' operator was used. */ |
13720 | code = INDIRECT_REF; | |
63c9a190 MM |
13721 | |
13722 | if (TREE_CODE (parser->scope) == NAMESPACE_DECL) | |
69bc6bff | 13723 | error_at (token->location, "%qD is a namespace", parser->scope); |
63c9a190 MM |
13724 | else |
13725 | { | |
13726 | /* The type of which the member is a member is given by the | |
13727 | current SCOPE. */ | |
13728 | *type = parser->scope; | |
13729 | /* The next name will not be qualified. */ | |
13730 | parser->scope = NULL_TREE; | |
13731 | parser->qualifying_scope = NULL_TREE; | |
13732 | parser->object_scope = NULL_TREE; | |
13733 | /* Look for the optional cv-qualifier-seq. */ | |
13734 | *cv_quals = cp_parser_cv_qualifier_seq_opt (parser); | |
13735 | } | |
a723baf1 MM |
13736 | } |
13737 | /* If that didn't work we don't have a ptr-operator. */ | |
13738 | if (!cp_parser_parse_definitely (parser)) | |
13739 | cp_parser_error (parser, "expected ptr-operator"); | |
13740 | } | |
13741 | ||
13742 | return code; | |
13743 | } | |
13744 | ||
13745 | /* Parse an (optional) cv-qualifier-seq. | |
13746 | ||
13747 | cv-qualifier-seq: | |
21526606 | 13748 | cv-qualifier cv-qualifier-seq [opt] |
a723baf1 | 13749 | |
a723baf1 MM |
13750 | cv-qualifier: |
13751 | const | |
21526606 | 13752 | volatile |
a723baf1 MM |
13753 | |
13754 | GNU Extension: | |
13755 | ||
13756 | cv-qualifier: | |
98ca843c | 13757 | __restrict__ |
a723baf1 | 13758 | |
3c01e5df MM |
13759 | Returns a bitmask representing the cv-qualifiers. */ |
13760 | ||
13761 | static cp_cv_quals | |
13762 | cp_parser_cv_qualifier_seq_opt (cp_parser* parser) | |
a723baf1 | 13763 | { |
3c01e5df | 13764 | cp_cv_quals cv_quals = TYPE_UNQUALIFIED; |
a723baf1 | 13765 | |
3c01e5df | 13766 | while (true) |
a723baf1 | 13767 | { |
3c01e5df MM |
13768 | cp_token *token; |
13769 | cp_cv_quals cv_qualifier; | |
98ca843c | 13770 | |
3c01e5df MM |
13771 | /* Peek at the next token. */ |
13772 | token = cp_lexer_peek_token (parser->lexer); | |
13773 | /* See if it's a cv-qualifier. */ | |
13774 | switch (token->keyword) | |
13775 | { | |
13776 | case RID_CONST: | |
13777 | cv_qualifier = TYPE_QUAL_CONST; | |
13778 | break; | |
98ca843c | 13779 | |
3c01e5df MM |
13780 | case RID_VOLATILE: |
13781 | cv_qualifier = TYPE_QUAL_VOLATILE; | |
13782 | break; | |
98ca843c | 13783 | |
3c01e5df MM |
13784 | case RID_RESTRICT: |
13785 | cv_qualifier = TYPE_QUAL_RESTRICT; | |
13786 | break; | |
98ca843c | 13787 | |
3c01e5df MM |
13788 | default: |
13789 | cv_qualifier = TYPE_UNQUALIFIED; | |
13790 | break; | |
13791 | } | |
98ca843c | 13792 | |
3c01e5df MM |
13793 | if (!cv_qualifier) |
13794 | break; | |
a723baf1 | 13795 | |
3c01e5df MM |
13796 | if (cv_quals & cv_qualifier) |
13797 | { | |
69bc6bff | 13798 | error_at (token->location, "duplicate cv-qualifier"); |
3c01e5df MM |
13799 | cp_lexer_purge_token (parser->lexer); |
13800 | } | |
13801 | else | |
13802 | { | |
13803 | cp_lexer_consume_token (parser->lexer); | |
13804 | cv_quals |= cv_qualifier; | |
13805 | } | |
a723baf1 MM |
13806 | } |
13807 | ||
3c01e5df | 13808 | return cv_quals; |
a723baf1 MM |
13809 | } |
13810 | ||
f57ca1ea JM |
13811 | /* Parse a late-specified return type, if any. This is not a separate |
13812 | non-terminal, but part of a function declarator, which looks like | |
13813 | ||
13814 | -> type-id | |
13815 | ||
13816 | Returns the type indicated by the type-id. */ | |
13817 | ||
13818 | static tree | |
13819 | cp_parser_late_return_type_opt (cp_parser* parser) | |
13820 | { | |
13821 | cp_token *token; | |
13822 | ||
13823 | /* Peek at the next token. */ | |
13824 | token = cp_lexer_peek_token (parser->lexer); | |
13825 | /* A late-specified return type is indicated by an initial '->'. */ | |
13826 | if (token->type != CPP_DEREF) | |
13827 | return NULL_TREE; | |
13828 | ||
13829 | /* Consume the ->. */ | |
13830 | cp_lexer_consume_token (parser->lexer); | |
13831 | ||
13832 | return cp_parser_type_id (parser); | |
13833 | } | |
13834 | ||
a723baf1 MM |
13835 | /* Parse a declarator-id. |
13836 | ||
13837 | declarator-id: | |
13838 | id-expression | |
21526606 | 13839 | :: [opt] nested-name-specifier [opt] type-name |
a723baf1 MM |
13840 | |
13841 | In the `id-expression' case, the value returned is as for | |
13842 | cp_parser_id_expression if the id-expression was an unqualified-id. | |
13843 | If the id-expression was a qualified-id, then a SCOPE_REF is | |
13844 | returned. The first operand is the scope (either a NAMESPACE_DECL | |
13845 | or TREE_TYPE), but the second is still just a representation of an | |
13846 | unqualified-id. */ | |
13847 | ||
13848 | static tree | |
fa6098f8 | 13849 | cp_parser_declarator_id (cp_parser* parser, bool optional_p) |
a723baf1 | 13850 | { |
d85d3d57 | 13851 | tree id; |
a723baf1 MM |
13852 | /* The expression must be an id-expression. Assume that qualified |
13853 | names are the names of types so that: | |
13854 | ||
13855 | template <class T> | |
13856 | int S<T>::R::i = 3; | |
13857 | ||
13858 | will work; we must treat `S<T>::R' as the name of a type. | |
13859 | Similarly, assume that qualified names are templates, where | |
13860 | required, so that: | |
13861 | ||
13862 | template <class T> | |
13863 | int S<T>::R<T>::i = 3; | |
13864 | ||
13865 | will work, too. */ | |
d85d3d57 MM |
13866 | id = cp_parser_id_expression (parser, |
13867 | /*template_keyword_p=*/false, | |
13868 | /*check_dependency_p=*/false, | |
13869 | /*template_p=*/NULL, | |
fa6098f8 | 13870 | /*declarator_p=*/true, |
88525e1b | 13871 | optional_p); |
fa6098f8 | 13872 | if (id && BASELINK_P (id)) |
d85d3d57 MM |
13873 | id = BASELINK_FUNCTIONS (id); |
13874 | return id; | |
a723baf1 MM |
13875 | } |
13876 | ||
13877 | /* Parse a type-id. | |
13878 | ||
13879 | type-id: | |
13880 | type-specifier-seq abstract-declarator [opt] | |
13881 | ||
13882 | Returns the TYPE specified. */ | |
13883 | ||
13884 | static tree | |
0d9c0892 | 13885 | cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg) |
a723baf1 | 13886 | { |
62d1db17 | 13887 | cp_decl_specifier_seq type_specifier_seq; |
058b15c1 | 13888 | cp_declarator *abstract_declarator; |
a723baf1 MM |
13889 | |
13890 | /* Parse the type-specifier-seq. */ | |
d4113656 MM |
13891 | cp_parser_type_specifier_seq (parser, /*is_condition=*/false, |
13892 | &type_specifier_seq); | |
62d1db17 | 13893 | if (type_specifier_seq.type == error_mark_node) |
a723baf1 MM |
13894 | return error_mark_node; |
13895 | ||
13896 | /* There might or might not be an abstract declarator. */ | |
13897 | cp_parser_parse_tentatively (parser); | |
13898 | /* Look for the declarator. */ | |
21526606 | 13899 | abstract_declarator |
4bb8ca28 | 13900 | = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL, |
db86dd14 MM |
13901 | /*parenthesized_p=*/NULL, |
13902 | /*member_p=*/false); | |
a723baf1 MM |
13903 | /* Check to see if there really was a declarator. */ |
13904 | if (!cp_parser_parse_definitely (parser)) | |
058b15c1 | 13905 | abstract_declarator = NULL; |
a723baf1 | 13906 | |
3f50c846 JM |
13907 | if (type_specifier_seq.type |
13908 | && type_uses_auto (type_specifier_seq.type)) | |
13909 | { | |
3dbbb7af JM |
13910 | /* A type-id with type 'auto' is only ok if the abstract declarator |
13911 | is a function declarator with a late-specified return type. */ | |
13912 | if (abstract_declarator | |
13913 | && abstract_declarator->kind == cdk_function | |
13914 | && abstract_declarator->u.function.late_return_type) | |
13915 | /* OK */; | |
13916 | else | |
13917 | { | |
13918 | error ("invalid use of %<auto%>"); | |
13919 | return error_mark_node; | |
13920 | } | |
3f50c846 JM |
13921 | } |
13922 | ||
0d9c0892 JM |
13923 | return groktypename (&type_specifier_seq, abstract_declarator, |
13924 | is_template_arg); | |
13925 | } | |
13926 | ||
13927 | static tree cp_parser_type_id (cp_parser *parser) | |
13928 | { | |
13929 | return cp_parser_type_id_1 (parser, false); | |
13930 | } | |
13931 | ||
13932 | static tree cp_parser_template_type_arg (cp_parser *parser) | |
13933 | { | |
13934 | return cp_parser_type_id_1 (parser, true); | |
a723baf1 MM |
13935 | } |
13936 | ||
13937 | /* Parse a type-specifier-seq. | |
13938 | ||
13939 | type-specifier-seq: | |
13940 | type-specifier type-specifier-seq [opt] | |
13941 | ||
13942 | GNU extension: | |
13943 | ||
13944 | type-specifier-seq: | |
13945 | attributes type-specifier-seq [opt] | |
13946 | ||
d4113656 MM |
13947 | If IS_CONDITION is true, we are at the start of a "condition", |
13948 | e.g., we've just seen "if (". | |
13949 | ||
62d1db17 | 13950 | Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */ |
a723baf1 | 13951 | |
62d1db17 MM |
13952 | static void |
13953 | cp_parser_type_specifier_seq (cp_parser* parser, | |
d4113656 | 13954 | bool is_condition, |
62d1db17 | 13955 | cp_decl_specifier_seq *type_specifier_seq) |
a723baf1 MM |
13956 | { |
13957 | bool seen_type_specifier = false; | |
d4113656 | 13958 | cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL; |
2b7a3abf | 13959 | cp_token *start_token = NULL; |
62d1db17 MM |
13960 | |
13961 | /* Clear the TYPE_SPECIFIER_SEQ. */ | |
13962 | clear_decl_specs (type_specifier_seq); | |
a723baf1 MM |
13963 | |
13964 | /* Parse the type-specifiers and attributes. */ | |
13965 | while (true) | |
13966 | { | |
13967 | tree type_specifier; | |
d4113656 | 13968 | bool is_cv_qualifier; |
a723baf1 MM |
13969 | |
13970 | /* Check for attributes first. */ | |
13971 | if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE)) | |
13972 | { | |
98ca843c | 13973 | type_specifier_seq->attributes = |
62d1db17 MM |
13974 | chainon (type_specifier_seq->attributes, |
13975 | cp_parser_attributes_opt (parser)); | |
a723baf1 MM |
13976 | continue; |
13977 | } | |
13978 | ||
2b7a3abf DS |
13979 | /* record the token of the beginning of the type specifier seq, |
13980 | for error reporting purposes*/ | |
13981 | if (!start_token) | |
13982 | start_token = cp_lexer_peek_token (parser->lexer); | |
13983 | ||
a723baf1 | 13984 | /* Look for the type-specifier. */ |
21526606 | 13985 | type_specifier = cp_parser_type_specifier (parser, |
d4113656 | 13986 | flags, |
62d1db17 | 13987 | type_specifier_seq, |
a723baf1 MM |
13988 | /*is_declaration=*/false, |
13989 | NULL, | |
d4113656 MM |
13990 | &is_cv_qualifier); |
13991 | if (!type_specifier) | |
62d1db17 | 13992 | { |
d4113656 MM |
13993 | /* If the first type-specifier could not be found, this is not a |
13994 | type-specifier-seq at all. */ | |
13995 | if (!seen_type_specifier) | |
13996 | { | |
13997 | cp_parser_error (parser, "expected type-specifier"); | |
13998 | type_specifier_seq->type = error_mark_node; | |
13999 | return; | |
14000 | } | |
14001 | /* If subsequent type-specifiers could not be found, the | |
14002 | type-specifier-seq is complete. */ | |
14003 | break; | |
62d1db17 | 14004 | } |
a723baf1 | 14005 | |
a723baf1 | 14006 | seen_type_specifier = true; |
d4113656 MM |
14007 | /* The standard says that a condition can be: |
14008 | ||
0cbd7506 | 14009 | type-specifier-seq declarator = assignment-expression |
c8094d83 | 14010 | |
d4113656 MM |
14011 | However, given: |
14012 | ||
14013 | struct S {}; | |
14014 | if (int S = ...) | |
14015 | ||
0cbd7506 MS |
14016 | we should treat the "S" as a declarator, not as a |
14017 | type-specifier. The standard doesn't say that explicitly for | |
14018 | type-specifier-seq, but it does say that for | |
14019 | decl-specifier-seq in an ordinary declaration. Perhaps it | |
14020 | would be clearer just to allow a decl-specifier-seq here, and | |
14021 | then add a semantic restriction that if any decl-specifiers | |
14022 | that are not type-specifiers appear, the program is invalid. */ | |
d4113656 | 14023 | if (is_condition && !is_cv_qualifier) |
c8094d83 | 14024 | flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES; |
a723baf1 | 14025 | } |
856367df | 14026 | |
2b7a3abf | 14027 | cp_parser_check_decl_spec (type_specifier_seq, start_token->location); |
a723baf1 MM |
14028 | } |
14029 | ||
14030 | /* Parse a parameter-declaration-clause. | |
14031 | ||
14032 | parameter-declaration-clause: | |
14033 | parameter-declaration-list [opt] ... [opt] | |
14034 | parameter-declaration-list , ... | |
14035 | ||
058b15c1 MM |
14036 | Returns a representation for the parameter declarations. A return |
14037 | value of NULL indicates a parameter-declaration-clause consisting | |
14038 | only of an ellipsis. */ | |
a723baf1 | 14039 | |
b344d949 | 14040 | static tree |
94edc4ab | 14041 | cp_parser_parameter_declaration_clause (cp_parser* parser) |
a723baf1 | 14042 | { |
b344d949 | 14043 | tree parameters; |
a723baf1 MM |
14044 | cp_token *token; |
14045 | bool ellipsis_p; | |
058b15c1 | 14046 | bool is_error; |
a723baf1 MM |
14047 | |
14048 | /* Peek at the next token. */ | |
14049 | token = cp_lexer_peek_token (parser->lexer); | |
14050 | /* Check for trivial parameter-declaration-clauses. */ | |
14051 | if (token->type == CPP_ELLIPSIS) | |
14052 | { | |
14053 | /* Consume the `...' token. */ | |
14054 | cp_lexer_consume_token (parser->lexer); | |
b344d949 | 14055 | return NULL_TREE; |
a723baf1 MM |
14056 | } |
14057 | else if (token->type == CPP_CLOSE_PAREN) | |
14058 | /* There are no parameters. */ | |
c73aecdf DE |
14059 | { |
14060 | #ifndef NO_IMPLICIT_EXTERN_C | |
14061 | if (in_system_header && current_class_type == NULL | |
14062 | && current_lang_name == lang_name_c) | |
b344d949 | 14063 | return NULL_TREE; |
c73aecdf DE |
14064 | else |
14065 | #endif | |
b344d949 | 14066 | return void_list_node; |
c73aecdf | 14067 | } |
a723baf1 MM |
14068 | /* Check for `(void)', too, which is a special case. */ |
14069 | else if (token->keyword == RID_VOID | |
21526606 | 14070 | && (cp_lexer_peek_nth_token (parser->lexer, 2)->type |
a723baf1 MM |
14071 | == CPP_CLOSE_PAREN)) |
14072 | { | |
14073 | /* Consume the `void' token. */ | |
14074 | cp_lexer_consume_token (parser->lexer); | |
14075 | /* There are no parameters. */ | |
b344d949 | 14076 | return void_list_node; |
a723baf1 | 14077 | } |
21526606 | 14078 | |
a723baf1 | 14079 | /* Parse the parameter-declaration-list. */ |
058b15c1 | 14080 | parameters = cp_parser_parameter_declaration_list (parser, &is_error); |
a723baf1 MM |
14081 | /* If a parse error occurred while parsing the |
14082 | parameter-declaration-list, then the entire | |
14083 | parameter-declaration-clause is erroneous. */ | |
058b15c1 MM |
14084 | if (is_error) |
14085 | return NULL; | |
a723baf1 MM |
14086 | |
14087 | /* Peek at the next token. */ | |
14088 | token = cp_lexer_peek_token (parser->lexer); | |
14089 | /* If it's a `,', the clause should terminate with an ellipsis. */ | |
14090 | if (token->type == CPP_COMMA) | |
14091 | { | |
14092 | /* Consume the `,'. */ | |
14093 | cp_lexer_consume_token (parser->lexer); | |
14094 | /* Expect an ellipsis. */ | |
21526606 | 14095 | ellipsis_p |
3a483371 | 14096 | = (cp_parser_require (parser, CPP_ELLIPSIS, "%<...%>") != NULL); |
a723baf1 | 14097 | } |
21526606 | 14098 | /* It might also be `...' if the optional trailing `,' was |
a723baf1 MM |
14099 | omitted. */ |
14100 | else if (token->type == CPP_ELLIPSIS) | |
14101 | { | |
14102 | /* Consume the `...' token. */ | |
14103 | cp_lexer_consume_token (parser->lexer); | |
14104 | /* And remember that we saw it. */ | |
14105 | ellipsis_p = true; | |
14106 | } | |
14107 | else | |
14108 | ellipsis_p = false; | |
14109 | ||
14110 | /* Finish the parameter list. */ | |
b344d949 JM |
14111 | if (!ellipsis_p) |
14112 | parameters = chainon (parameters, void_list_node); | |
98ca843c | 14113 | |
058b15c1 | 14114 | return parameters; |
a723baf1 MM |
14115 | } |
14116 | ||
14117 | /* Parse a parameter-declaration-list. | |
14118 | ||
14119 | parameter-declaration-list: | |
14120 | parameter-declaration | |
14121 | parameter-declaration-list , parameter-declaration | |
14122 | ||
14123 | Returns a representation of the parameter-declaration-list, as for | |
14124 | cp_parser_parameter_declaration_clause. However, the | |
058b15c1 MM |
14125 | `void_list_node' is never appended to the list. Upon return, |
14126 | *IS_ERROR will be true iff an error occurred. */ | |
a723baf1 | 14127 | |
b344d949 | 14128 | static tree |
058b15c1 | 14129 | cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error) |
a723baf1 | 14130 | { |
b344d949 JM |
14131 | tree parameters = NULL_TREE; |
14132 | tree *tail = ¶meters; | |
75a82978 | 14133 | bool saved_in_unbraced_linkage_specification_p; |
ad909c97 | 14134 | int index = 0; |
058b15c1 MM |
14135 | |
14136 | /* Assume all will go well. */ | |
14137 | *is_error = false; | |
75a82978 MM |
14138 | /* The special considerations that apply to a function within an |
14139 | unbraced linkage specifications do not apply to the parameters | |
14140 | to the function. */ | |
14141 | saved_in_unbraced_linkage_specification_p | |
14142 | = parser->in_unbraced_linkage_specification_p; | |
14143 | parser->in_unbraced_linkage_specification_p = false; | |
a723baf1 MM |
14144 | |
14145 | /* Look for more parameters. */ | |
14146 | while (true) | |
14147 | { | |
058b15c1 | 14148 | cp_parameter_declarator *parameter; |
b344d949 | 14149 | tree decl = error_mark_node; |
4bb8ca28 | 14150 | bool parenthesized_p; |
a723baf1 | 14151 | /* Parse the parameter. */ |
21526606 EC |
14152 | parameter |
14153 | = cp_parser_parameter_declaration (parser, | |
4bb8ca28 MM |
14154 | /*template_parm_p=*/false, |
14155 | &parenthesized_p); | |
ec194454 | 14156 | |
b344d949 JM |
14157 | /* We don't know yet if the enclosing context is deprecated, so wait |
14158 | and warn in grokparms if appropriate. */ | |
14159 | deprecated_state = DEPRECATED_SUPPRESS; | |
14160 | ||
14161 | if (parameter) | |
14162 | decl = grokdeclarator (parameter->declarator, | |
14163 | ¶meter->decl_specifiers, | |
14164 | PARM, | |
14165 | parameter->default_argument != NULL_TREE, | |
14166 | ¶meter->decl_specifiers.attributes); | |
14167 | ||
14168 | deprecated_state = DEPRECATED_NORMAL; | |
14169 | ||
34cd5ae7 | 14170 | /* If a parse error occurred parsing the parameter declaration, |
a723baf1 | 14171 | then the entire parameter-declaration-list is erroneous. */ |
b344d949 | 14172 | if (decl == error_mark_node) |
a723baf1 | 14173 | { |
058b15c1 | 14174 | *is_error = true; |
b344d949 | 14175 | parameters = error_mark_node; |
a723baf1 MM |
14176 | break; |
14177 | } | |
b344d949 JM |
14178 | |
14179 | if (parameter->decl_specifiers.attributes) | |
14180 | cplus_decl_attributes (&decl, | |
14181 | parameter->decl_specifiers.attributes, | |
14182 | 0); | |
14183 | if (DECL_NAME (decl)) | |
14184 | decl = pushdecl (decl); | |
14185 | ||
ad909c97 JM |
14186 | if (decl != error_mark_node) |
14187 | { | |
14188 | retrofit_lang_decl (decl); | |
14189 | DECL_PARM_INDEX (decl) = ++index; | |
14190 | } | |
14191 | ||
a723baf1 | 14192 | /* Add the new parameter to the list. */ |
b344d949 JM |
14193 | *tail = build_tree_list (parameter->default_argument, decl); |
14194 | tail = &TREE_CHAIN (*tail); | |
a723baf1 MM |
14195 | |
14196 | /* Peek at the next token. */ | |
14197 | if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN) | |
e58a9aa1 ZL |
14198 | || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS) |
14199 | /* These are for Objective-C++ */ | |
14200 | || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON) | |
14201 | || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)) | |
a723baf1 MM |
14202 | /* The parameter-declaration-list is complete. */ |
14203 | break; | |
14204 | else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)) | |
14205 | { | |
14206 | cp_token *token; | |
14207 | ||
14208 | /* Peek at the next token. */ | |
14209 | token = cp_lexer_peek_nth_token (parser->lexer, 2); | |
14210 | /* If it's an ellipsis, then the list is complete. */ | |
14211 | if (token->type == CPP_ELLIPSIS) | |
14212 | break; | |
14213 | /* Otherwise, there must be more parameters. Consume the | |
14214 | `,'. */ | |
14215 | cp_lexer_consume_token (parser->lexer); | |
4bb8ca28 MM |
14216 | /* When parsing something like: |
14217 | ||
0cbd7506 | 14218 | int i(float f, double d) |
21526606 | 14219 | |
0cbd7506 | 14220 | we can tell after seeing the declaration for "f" that we |
4bb8ca28 | 14221 | are not looking at an initialization of a variable "i", |
21526606 | 14222 | but rather at the declaration of a function "i". |
4bb8ca28 MM |
14223 | |
14224 | Due to the fact that the parsing of template arguments | |
14225 | (as specified to a template-id) requires backtracking we | |
14226 | cannot use this technique when inside a template argument | |
14227 | list. */ | |
14228 | if (!parser->in_template_argument_list_p | |
4d5fe289 | 14229 | && !parser->in_type_id_in_expr_p |
0b16f8f4 | 14230 | && cp_parser_uncommitted_to_tentative_parse_p (parser) |
4bb8ca28 MM |
14231 | /* However, a parameter-declaration of the form |
14232 | "foat(f)" (which is a valid declaration of a | |
14233 | parameter "f") can also be interpreted as an | |
14234 | expression (the conversion of "f" to "float"). */ | |
14235 | && !parenthesized_p) | |
14236 | cp_parser_commit_to_tentative_parse (parser); | |
a723baf1 MM |
14237 | } |
14238 | else | |
14239 | { | |
2a13a625 | 14240 | cp_parser_error (parser, "expected %<,%> or %<...%>"); |
0b16f8f4 | 14241 | if (!cp_parser_uncommitted_to_tentative_parse_p (parser)) |
21526606 | 14242 | cp_parser_skip_to_closing_parenthesis (parser, |
4bb8ca28 | 14243 | /*recovering=*/true, |
5c832178 | 14244 | /*or_comma=*/false, |
4bb8ca28 | 14245 | /*consume_paren=*/false); |
a723baf1 MM |
14246 | break; |
14247 | } | |
14248 | } | |
14249 | ||
75a82978 MM |
14250 | parser->in_unbraced_linkage_specification_p |
14251 | = saved_in_unbraced_linkage_specification_p; | |
14252 | ||
058b15c1 | 14253 | return parameters; |
a723baf1 MM |
14254 | } |
14255 | ||
14256 | /* Parse a parameter declaration. | |
14257 | ||
14258 | parameter-declaration: | |
5d80a306 | 14259 | decl-specifier-seq ... [opt] declarator |
a723baf1 | 14260 | decl-specifier-seq declarator = assignment-expression |
5d80a306 | 14261 | decl-specifier-seq ... [opt] abstract-declarator [opt] |
a723baf1 MM |
14262 | decl-specifier-seq abstract-declarator [opt] = assignment-expression |
14263 | ||
ec194454 MM |
14264 | If TEMPLATE_PARM_P is TRUE, then this parameter-declaration |
14265 | declares a template parameter. (In that case, a non-nested `>' | |
14266 | token encountered during the parsing of the assignment-expression | |
14267 | is not interpreted as a greater-than operator.) | |
a723baf1 | 14268 | |
058b15c1 MM |
14269 | Returns a representation of the parameter, or NULL if an error |
14270 | occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to | |
14271 | true iff the declarator is of the form "(p)". */ | |
a723baf1 | 14272 | |
058b15c1 | 14273 | static cp_parameter_declarator * |
21526606 | 14274 | cp_parser_parameter_declaration (cp_parser *parser, |
4bb8ca28 MM |
14275 | bool template_parm_p, |
14276 | bool *parenthesized_p) | |
a723baf1 | 14277 | { |
560ad596 | 14278 | int declares_class_or_enum; |
ec194454 | 14279 | bool greater_than_is_operator_p; |
62d1db17 | 14280 | cp_decl_specifier_seq decl_specifiers; |
058b15c1 | 14281 | cp_declarator *declarator; |
a723baf1 | 14282 | tree default_argument; |
2b7a3abf | 14283 | cp_token *token = NULL, *declarator_token_start = NULL; |
a723baf1 MM |
14284 | const char *saved_message; |
14285 | ||
ec194454 MM |
14286 | /* In a template parameter, `>' is not an operator. |
14287 | ||
14288 | [temp.param] | |
14289 | ||
14290 | When parsing a default template-argument for a non-type | |
14291 | template-parameter, the first non-nested `>' is taken as the end | |
14292 | of the template parameter-list rather than a greater-than | |
14293 | operator. */ | |
14294 | greater_than_is_operator_p = !template_parm_p; | |
14295 | ||
a723baf1 MM |
14296 | /* Type definitions may not appear in parameter types. */ |
14297 | saved_message = parser->type_definition_forbidden_message; | |
21526606 | 14298 | parser->type_definition_forbidden_message |
a723baf1 MM |
14299 | = "types may not be defined in parameter types"; |
14300 | ||
14301 | /* Parse the declaration-specifiers. */ | |
62d1db17 MM |
14302 | cp_parser_decl_specifier_seq (parser, |
14303 | CP_PARSER_FLAGS_NONE, | |
14304 | &decl_specifiers, | |
14305 | &declares_class_or_enum); | |
a723baf1 MM |
14306 | /* If an error occurred, there's no reason to attempt to parse the |
14307 | rest of the declaration. */ | |
14308 | if (cp_parser_error_occurred (parser)) | |
14309 | { | |
14310 | parser->type_definition_forbidden_message = saved_message; | |
058b15c1 | 14311 | return NULL; |
a723baf1 MM |
14312 | } |
14313 | ||
14314 | /* Peek at the next token. */ | |
14315 | token = cp_lexer_peek_token (parser->lexer); | |
5d80a306 | 14316 | |
a723baf1 | 14317 | /* If the next token is a `)', `,', `=', `>', or `...', then there |
5d80a306 DG |
14318 | is no declarator. However, when variadic templates are enabled, |
14319 | there may be a declarator following `...'. */ | |
21526606 | 14320 | if (token->type == CPP_CLOSE_PAREN |
a723baf1 MM |
14321 | || token->type == CPP_COMMA |
14322 | || token->type == CPP_EQ | |
a723baf1 | 14323 | || token->type == CPP_GREATER) |
4bb8ca28 | 14324 | { |
058b15c1 | 14325 | declarator = NULL; |
4bb8ca28 MM |
14326 | if (parenthesized_p) |
14327 | *parenthesized_p = false; | |
14328 | } | |
a723baf1 MM |
14329 | /* Otherwise, there should be a declarator. */ |
14330 | else | |
14331 | { | |
14332 | bool saved_default_arg_ok_p = parser->default_arg_ok_p; | |
14333 | parser->default_arg_ok_p = false; | |
21526606 | 14334 | |
5c832178 MM |
14335 | /* After seeing a decl-specifier-seq, if the next token is not a |
14336 | "(", there is no possibility that the code is a valid | |
4f8163b1 MM |
14337 | expression. Therefore, if parsing tentatively, we commit at |
14338 | this point. */ | |
5c832178 | 14339 | if (!parser->in_template_argument_list_p |
643aee72 | 14340 | /* In an expression context, having seen: |
4f8163b1 | 14341 | |
a7324e75 | 14342 | (int((char ... |
4f8163b1 MM |
14343 | |
14344 | we cannot be sure whether we are looking at a | |
a7324e75 MM |
14345 | function-type (taking a "char" as a parameter) or a cast |
14346 | of some object of type "char" to "int". */ | |
4f8163b1 | 14347 | && !parser->in_type_id_in_expr_p |
0b16f8f4 | 14348 | && cp_parser_uncommitted_to_tentative_parse_p (parser) |
5c832178 MM |
14349 | && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)) |
14350 | cp_parser_commit_to_tentative_parse (parser); | |
14351 | /* Parse the declarator. */ | |
2b7a3abf | 14352 | declarator_token_start = token; |
a723baf1 | 14353 | declarator = cp_parser_declarator (parser, |
62b8a44e | 14354 | CP_PARSER_DECLARATOR_EITHER, |
4bb8ca28 | 14355 | /*ctor_dtor_or_conv_p=*/NULL, |
db86dd14 MM |
14356 | parenthesized_p, |
14357 | /*member_p=*/false); | |
a723baf1 | 14358 | parser->default_arg_ok_p = saved_default_arg_ok_p; |
4971227d | 14359 | /* After the declarator, allow more attributes. */ |
62d1db17 | 14360 | decl_specifiers.attributes |
98ca843c | 14361 | = chainon (decl_specifiers.attributes, |
62d1db17 | 14362 | cp_parser_attributes_opt (parser)); |
a723baf1 MM |
14363 | } |
14364 | ||
d393153e DG |
14365 | /* If the next token is an ellipsis, and we have not seen a |
14366 | declarator name, and the type of the declarator contains parameter | |
14367 | packs but it is not a TYPE_PACK_EXPANSION, then we actually have | |
14368 | a parameter pack expansion expression. Otherwise, leave the | |
14369 | ellipsis for a C-style variadic function. */ | |
5d80a306 DG |
14370 | token = cp_lexer_peek_token (parser->lexer); |
14371 | if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)) | |
14372 | { | |
14373 | tree type = decl_specifiers.type; | |
14374 | ||
d393153e | 14375 | if (type && DECL_P (type)) |
5d80a306 DG |
14376 | type = TREE_TYPE (type); |
14377 | ||
d393153e DG |
14378 | if (type |
14379 | && TREE_CODE (type) != TYPE_PACK_EXPANSION | |
14380 | && declarator_can_be_parameter_pack (declarator) | |
5d80a306 DG |
14381 | && (!declarator || !declarator->parameter_pack_p) |
14382 | && uses_parameter_packs (type)) | |
14383 | { | |
d393153e DG |
14384 | /* Consume the `...'. */ |
14385 | cp_lexer_consume_token (parser->lexer); | |
14386 | maybe_warn_variadic_templates (); | |
14387 | ||
14388 | /* Build a pack expansion type */ | |
14389 | if (declarator) | |
14390 | declarator->parameter_pack_p = true; | |
14391 | else | |
14392 | decl_specifiers.type = make_pack_expansion (type); | |
14393 | } | |
5d80a306 DG |
14394 | } |
14395 | ||
62b8a44e | 14396 | /* The restriction on defining new types applies only to the type |
a723baf1 MM |
14397 | of the parameter, not to the default argument. */ |
14398 | parser->type_definition_forbidden_message = saved_message; | |
14399 | ||
14400 | /* If the next token is `=', then process a default argument. */ | |
14401 | if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)) | |
14402 | { | |
a723baf1 MM |
14403 | /* Consume the `='. */ |
14404 | cp_lexer_consume_token (parser->lexer); | |
14405 | ||
14406 | /* If we are defining a class, then the tokens that make up the | |
14407 | default argument must be saved and processed later. */ | |
21526606 | 14408 | if (!template_parm_p && at_class_scope_p () |
ec194454 | 14409 | && TYPE_BEING_DEFINED (current_class_type)) |
a723baf1 MM |
14410 | { |
14411 | unsigned depth = 0; | |
bda37952 | 14412 | int maybe_template_id = 0; |
c162c75e MA |
14413 | cp_token *first_token; |
14414 | cp_token *token; | |
a723baf1 MM |
14415 | |
14416 | /* Add tokens until we have processed the entire default | |
03fd3f84 | 14417 | argument. We add the range [first_token, token). */ |
c162c75e | 14418 | first_token = cp_lexer_peek_token (parser->lexer); |
a723baf1 MM |
14419 | while (true) |
14420 | { | |
14421 | bool done = false; | |
a723baf1 MM |
14422 | |
14423 | /* Peek at the next token. */ | |
14424 | token = cp_lexer_peek_token (parser->lexer); | |
14425 | /* What we do depends on what token we have. */ | |
14426 | switch (token->type) | |
14427 | { | |
14428 | /* In valid code, a default argument must be | |
14429 | immediately followed by a `,' `)', or `...'. */ | |
14430 | case CPP_COMMA: | |
bda37952 JM |
14431 | if (depth == 0 && maybe_template_id) |
14432 | { | |
14433 | /* If we've seen a '<', we might be in a | |
14434 | template-argument-list. Until Core issue 325 is | |
14435 | resolved, we don't know how this situation ought | |
14436 | to be handled, so try to DTRT. We check whether | |
14437 | what comes after the comma is a valid parameter | |
14438 | declaration list. If it is, then the comma ends | |
14439 | the default argument; otherwise the default | |
14440 | argument continues. */ | |
14441 | bool error = false; | |
14442 | ||
14443 | /* Set ITALP so cp_parser_parameter_declaration_list | |
14444 | doesn't decide to commit to this parse. */ | |
14445 | bool saved_italp = parser->in_template_argument_list_p; | |
14446 | parser->in_template_argument_list_p = true; | |
14447 | ||
14448 | cp_parser_parse_tentatively (parser); | |
14449 | cp_lexer_consume_token (parser->lexer); | |
14450 | cp_parser_parameter_declaration_list (parser, &error); | |
14451 | if (!cp_parser_error_occurred (parser) && !error) | |
14452 | done = true; | |
14453 | cp_parser_abort_tentative_parse (parser); | |
14454 | ||
14455 | parser->in_template_argument_list_p = saved_italp; | |
14456 | break; | |
14457 | } | |
a723baf1 MM |
14458 | case CPP_CLOSE_PAREN: |
14459 | case CPP_ELLIPSIS: | |
14460 | /* If we run into a non-nested `;', `}', or `]', | |
14461 | then the code is invalid -- but the default | |
14462 | argument is certainly over. */ | |
14463 | case CPP_SEMICOLON: | |
14464 | case CPP_CLOSE_BRACE: | |
14465 | case CPP_CLOSE_SQUARE: | |
14466 | if (depth == 0) | |
14467 | done = true; | |
14468 | /* Update DEPTH, if necessary. */ | |
14469 | else if (token->type == CPP_CLOSE_PAREN | |
14470 | || token->type == CPP_CLOSE_BRACE | |
14471 | || token->type == CPP_CLOSE_SQUARE) | |
14472 | --depth; | |
14473 | break; | |
14474 | ||
14475 | case CPP_OPEN_PAREN: | |
14476 | case CPP_OPEN_SQUARE: | |
14477 | case CPP_OPEN_BRACE: | |
14478 | ++depth; | |
14479 | break; | |
14480 | ||
bda37952 JM |
14481 | case CPP_LESS: |
14482 | if (depth == 0) | |
14483 | /* This might be the comparison operator, or it might | |
14484 | start a template argument list. */ | |
14485 | ++maybe_template_id; | |
14486 | break; | |
14487 | ||
a2ca1ab2 | 14488 | case CPP_RSHIFT: |
c1ae8be5 | 14489 | if (cxx_dialect == cxx98) |
a2ca1ab2 DG |
14490 | break; |
14491 | /* Fall through for C++0x, which treats the `>>' | |
14492 | operator like two `>' tokens in certain | |
14493 | cases. */ | |
14494 | ||
a723baf1 | 14495 | case CPP_GREATER: |
bda37952 JM |
14496 | if (depth == 0) |
14497 | { | |
14498 | /* This might be an operator, or it might close a | |
14499 | template argument list. But if a previous '<' | |
14500 | started a template argument list, this will have | |
14501 | closed it, so we can't be in one anymore. */ | |
14502 | maybe_template_id -= 1 + (token->type == CPP_RSHIFT); | |
14503 | if (maybe_template_id < 0) | |
14504 | maybe_template_id = 0; | |
14505 | } | |
a723baf1 MM |
14506 | break; |
14507 | ||
14508 | /* If we run out of tokens, issue an error message. */ | |
14509 | case CPP_EOF: | |
bc4071dd | 14510 | case CPP_PRAGMA_EOL: |
69bc6bff | 14511 | error_at (token->location, "file ends in default argument"); |
a723baf1 MM |
14512 | done = true; |
14513 | break; | |
14514 | ||
14515 | case CPP_NAME: | |
14516 | case CPP_SCOPE: | |
14517 | /* In these cases, we should look for template-ids. | |
21526606 | 14518 | For example, if the default argument is |
a723baf1 MM |
14519 | `X<int, double>()', we need to do name lookup to |
14520 | figure out whether or not `X' is a template; if | |
34cd5ae7 | 14521 | so, the `,' does not end the default argument. |
a723baf1 MM |
14522 | |
14523 | That is not yet done. */ | |
14524 | break; | |
14525 | ||
14526 | default: | |
14527 | break; | |
14528 | } | |
14529 | ||
14530 | /* If we've reached the end, stop. */ | |
14531 | if (done) | |
14532 | break; | |
21526606 | 14533 | |
a723baf1 MM |
14534 | /* Add the token to the token block. */ |
14535 | token = cp_lexer_consume_token (parser->lexer); | |
a723baf1 | 14536 | } |
c162c75e | 14537 | |
7179604f | 14538 | /* Create a DEFAULT_ARG to represent the unparsed default |
0cbd7506 | 14539 | argument. */ |
c162c75e MA |
14540 | default_argument = make_node (DEFAULT_ARG); |
14541 | DEFARG_TOKENS (default_argument) | |
01ea1ea8 NS |
14542 | = cp_token_cache_new (first_token, token); |
14543 | DEFARG_INSTANTIATIONS (default_argument) = NULL; | |
a723baf1 MM |
14544 | } |
14545 | /* Outside of a class definition, we can just parse the | |
0cbd7506 | 14546 | assignment-expression. */ |
a723baf1 | 14547 | else |
2b7a3abf DS |
14548 | { |
14549 | token = cp_lexer_peek_token (parser->lexer); | |
14550 | default_argument | |
14551 | = cp_parser_default_argument (parser, template_parm_p); | |
14552 | } | |
cdcae745 | 14553 | |
a723baf1 MM |
14554 | if (!parser->default_arg_ok_p) |
14555 | { | |
37ec60ed | 14556 | if (flag_permissive) |
d4ee4d25 | 14557 | warning (0, "deprecated use of default argument for parameter of non-function"); |
c67d36d0 NS |
14558 | else |
14559 | { | |
69bc6bff MLI |
14560 | error_at (token->location, |
14561 | "default arguments are only " | |
14562 | "permitted for function parameters"); | |
c67d36d0 NS |
14563 | default_argument = NULL_TREE; |
14564 | } | |
a723baf1 | 14565 | } |
cdcae745 DG |
14566 | else if ((declarator && declarator->parameter_pack_p) |
14567 | || (decl_specifiers.type | |
14568 | && PACK_EXPANSION_P (decl_specifiers.type))) | |
14569 | { | |
cdcae745 DG |
14570 | /* Find the name of the parameter pack. */ |
14571 | cp_declarator *id_declarator = declarator; | |
14572 | while (id_declarator && id_declarator->kind != cdk_id) | |
14573 | id_declarator = id_declarator->declarator; | |
14574 | ||
14575 | if (id_declarator && id_declarator->kind == cdk_id) | |
69bc6bff MLI |
14576 | error_at (declarator_token_start->location, |
14577 | template_parm_p | |
14578 | ? "template parameter pack %qD" | |
14579 | " cannot have a default argument" | |
14580 | : "parameter pack %qD cannot have a default argument", | |
14581 | id_declarator->u.id.unqualified_name); | |
cdcae745 | 14582 | else |
69bc6bff MLI |
14583 | error_at (declarator_token_start->location, |
14584 | template_parm_p | |
14585 | ? "template parameter pack cannot have a default argument" | |
14586 | : "parameter pack cannot have a default argument"); | |
cdcae745 DG |
14587 | |
14588 | default_argument = NULL_TREE; | |
14589 | } | |
a723baf1 MM |
14590 | } |
14591 | else | |
14592 | default_argument = NULL_TREE; | |
21526606 | 14593 | |
62d1db17 | 14594 | return make_parameter_declarator (&decl_specifiers, |
058b15c1 MM |
14595 | declarator, |
14596 | default_argument); | |
a723baf1 MM |
14597 | } |
14598 | ||
cdcae745 DG |
14599 | /* Parse a default argument and return it. |
14600 | ||
14601 | TEMPLATE_PARM_P is true if this is a default argument for a | |
14602 | non-type template parameter. */ | |
14603 | static tree | |
14604 | cp_parser_default_argument (cp_parser *parser, bool template_parm_p) | |
14605 | { | |
14606 | tree default_argument = NULL_TREE; | |
14607 | bool saved_greater_than_is_operator_p; | |
14608 | bool saved_local_variables_forbidden_p; | |
14609 | ||
14610 | /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is | |
14611 | set correctly. */ | |
14612 | saved_greater_than_is_operator_p = parser->greater_than_is_operator_p; | |
14613 | parser->greater_than_is_operator_p = !template_parm_p; | |
14614 | /* Local variable names (and the `this' keyword) may not | |
14615 | appear in a default argument. */ | |
14616 | saved_local_variables_forbidden_p = parser->local_variables_forbidden_p; | |
14617 | parser->local_variables_forbidden_p = true; | |
14618 | /* The default argument expression may cause implicitly | |
14619 | defined member functions to be synthesized, which will | |
14620 | result in garbage collection. We must treat this | |
14621 | situation as if we were within the body of function so as | |
14622 | to avoid collecting live data on the stack. */ | |
14623 | ++function_depth; | |
14624 | /* Parse the assignment-expression. */ | |
14625 | if (template_parm_p) | |
14626 | push_deferring_access_checks (dk_no_deferred); | |
14627 | default_argument | |
cee9035d | 14628 | = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL); |
cdcae745 DG |
14629 | if (template_parm_p) |
14630 | pop_deferring_access_checks (); | |
14631 | /* Restore saved state. */ | |
14632 | --function_depth; | |
14633 | parser->greater_than_is_operator_p = saved_greater_than_is_operator_p; | |
14634 | parser->local_variables_forbidden_p = saved_local_variables_forbidden_p; | |
14635 | ||
14636 | return default_argument; | |
14637 | } | |
14638 | ||
a723baf1 MM |
14639 | /* Parse a function-body. |
14640 | ||
14641 | function-body: | |
14642 | compound_statement */ | |
14643 | ||
14644 | static void | |
14645 | cp_parser_function_body (cp_parser *parser) | |
14646 | { | |
325c3691 | 14647 | cp_parser_compound_statement (parser, NULL, false); |
a723baf1 MM |
14648 | } |
14649 | ||
14650 | /* Parse a ctor-initializer-opt followed by a function-body. Return | |
14651 | true if a ctor-initializer was present. */ | |
14652 | ||
14653 | static bool | |
14654 | cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser) | |
14655 | { | |
14656 | tree body; | |
14657 | bool ctor_initializer_p; | |
14658 | ||
14659 | /* Begin the function body. */ | |
14660 | body = begin_function_body (); | |
14661 | /* Parse the optional ctor-initializer. */ | |
14662 | ctor_initializer_p = cp_parser_ctor_initializer_opt (parser); | |
14663 | /* Parse the function-body. */ | |
14664 | cp_parser_function_body (parser); | |
14665 | /* Finish the function body. */ | |
14666 | finish_function_body (body); | |
14667 | ||
14668 | return ctor_initializer_p; | |
14669 | } | |
14670 | ||
14671 | /* Parse an initializer. | |
14672 | ||
14673 | initializer: | |
14674 | = initializer-clause | |
21526606 | 14675 | ( expression-list ) |
a723baf1 | 14676 | |
c72a1a86 | 14677 | Returns an expression representing the initializer. If no |
21526606 | 14678 | initializer is present, NULL_TREE is returned. |
a723baf1 | 14679 | |
09357846 JM |
14680 | *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause' |
14681 | production is used, and TRUE otherwise. *IS_DIRECT_INIT is | |
14682 | set to TRUE if there is no initializer present. If there is an | |
39703eb9 MM |
14683 | initializer, and it is not a constant-expression, *NON_CONSTANT_P |
14684 | is set to true; otherwise it is set to false. */ | |
a723baf1 MM |
14685 | |
14686 | static tree | |
09357846 | 14687 | cp_parser_initializer (cp_parser* parser, bool* is_direct_init, |
39703eb9 | 14688 | bool* non_constant_p) |
a723baf1 MM |
14689 | { |
14690 | cp_token *token; | |
14691 | tree init; | |
14692 | ||
14693 | /* Peek at the next token. */ | |
14694 | token = cp_lexer_peek_token (parser->lexer); | |
14695 | ||
14696 | /* Let our caller know whether or not this initializer was | |
14697 | parenthesized. */ | |
09357846 | 14698 | *is_direct_init = (token->type != CPP_EQ); |
39703eb9 MM |
14699 | /* Assume that the initializer is constant. */ |
14700 | *non_constant_p = false; | |
a723baf1 MM |
14701 | |
14702 | if (token->type == CPP_EQ) | |
14703 | { | |
14704 | /* Consume the `='. */ | |
14705 | cp_lexer_consume_token (parser->lexer); | |
14706 | /* Parse the initializer-clause. */ | |
39703eb9 | 14707 | init = cp_parser_initializer_clause (parser, non_constant_p); |
a723baf1 MM |
14708 | } |
14709 | else if (token->type == CPP_OPEN_PAREN) | |
c166b898 ILT |
14710 | { |
14711 | VEC(tree,gc) *vec; | |
14712 | vec = cp_parser_parenthesized_expression_list (parser, false, | |
14713 | /*cast_p=*/false, | |
14714 | /*allow_expansion_p=*/true, | |
14715 | non_constant_p); | |
14716 | if (vec == NULL) | |
14717 | return error_mark_node; | |
14718 | init = build_tree_list_vec (vec); | |
14719 | release_tree_vector (vec); | |
14720 | } | |
09357846 JM |
14721 | else if (token->type == CPP_OPEN_BRACE) |
14722 | { | |
14723 | maybe_warn_cpp0x ("extended initializer lists"); | |
14724 | init = cp_parser_braced_list (parser, non_constant_p); | |
14725 | CONSTRUCTOR_IS_DIRECT_INIT (init) = 1; | |
14726 | } | |
a723baf1 MM |
14727 | else |
14728 | { | |
14729 | /* Anything else is an error. */ | |
14730 | cp_parser_error (parser, "expected initializer"); | |
14731 | init = error_mark_node; | |
14732 | } | |
14733 | ||
14734 | return init; | |
14735 | } | |
14736 | ||
21526606 | 14737 | /* Parse an initializer-clause. |
a723baf1 MM |
14738 | |
14739 | initializer-clause: | |
14740 | assignment-expression | |
09357846 | 14741 | braced-init-list |
a723baf1 | 14742 | |
21526606 | 14743 | Returns an expression representing the initializer. |
a723baf1 MM |
14744 | |
14745 | If the `assignment-expression' production is used the value | |
21526606 | 14746 | returned is simply a representation for the expression. |
a723baf1 | 14747 | |
09357846 | 14748 | Otherwise, calls cp_parser_braced_list. */ |
a723baf1 MM |
14749 | |
14750 | static tree | |
39703eb9 | 14751 | cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p) |
a723baf1 MM |
14752 | { |
14753 | tree initializer; | |
14754 | ||
b2802a4b R |
14755 | /* Assume the expression is constant. */ |
14756 | *non_constant_p = false; | |
14757 | ||
a723baf1 MM |
14758 | /* If it is not a `{', then we are looking at an |
14759 | assignment-expression. */ | |
14760 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)) | |
0da99d4e | 14761 | { |
98ca843c | 14762 | initializer |
0da99d4e GB |
14763 | = cp_parser_constant_expression (parser, |
14764 | /*allow_non_constant_p=*/true, | |
14765 | non_constant_p); | |
14766 | if (!*non_constant_p) | |
14767 | initializer = fold_non_dependent_expr (initializer); | |
14768 | } | |
a723baf1 | 14769 | else |
09357846 JM |
14770 | initializer = cp_parser_braced_list (parser, non_constant_p); |
14771 | ||
14772 | return initializer; | |
14773 | } | |
14774 | ||
14775 | /* Parse a brace-enclosed initializer list. | |
14776 | ||
14777 | braced-init-list: | |
14778 | { initializer-list , [opt] } | |
14779 | { } | |
14780 | ||
14781 | Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be | |
14782 | the elements of the initializer-list (or NULL, if the last | |
14783 | production is used). The TREE_TYPE for the CONSTRUCTOR will be | |
14784 | NULL_TREE. There is no way to detect whether or not the optional | |
14785 | trailing `,' was provided. NON_CONSTANT_P is as for | |
14786 | cp_parser_initializer. */ | |
14787 | ||
14788 | static tree | |
14789 | cp_parser_braced_list (cp_parser* parser, bool* non_constant_p) | |
14790 | { | |
14791 | tree initializer; | |
14792 | ||
14793 | /* Consume the `{' token. */ | |
14794 | cp_lexer_consume_token (parser->lexer); | |
14795 | /* Create a CONSTRUCTOR to represent the braced-initializer. */ | |
14796 | initializer = make_node (CONSTRUCTOR); | |
14797 | /* If it's not a `}', then there is a non-trivial initializer. */ | |
14798 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE)) | |
a723baf1 | 14799 | { |
09357846 JM |
14800 | /* Parse the initializer list. */ |
14801 | CONSTRUCTOR_ELTS (initializer) | |
14802 | = cp_parser_initializer_list (parser, non_constant_p); | |
14803 | /* A trailing `,' token is allowed. */ | |
14804 | if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)) | |
14805 | cp_lexer_consume_token (parser->lexer); | |
a723baf1 | 14806 | } |
09357846 JM |
14807 | /* Now, there should be a trailing `}'. */ |
14808 | cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>"); | |
14809 | TREE_TYPE (initializer) = init_list_type_node; | |
a723baf1 MM |
14810 | return initializer; |
14811 | } | |
14812 | ||
14813 | /* Parse an initializer-list. | |
14814 | ||
14815 | initializer-list: | |
5d80a306 DG |
14816 | initializer-clause ... [opt] |
14817 | initializer-list , initializer-clause ... [opt] | |
a723baf1 MM |
14818 | |
14819 | GNU Extension: | |
21526606 | 14820 | |
a723baf1 MM |
14821 | initializer-list: |
14822 | identifier : initializer-clause | |
14823 | initializer-list, identifier : initializer-clause | |
14824 | ||
4038c495 GB |
14825 | Returns a VEC of constructor_elt. The VALUE of each elt is an expression |
14826 | for the initializer. If the INDEX of the elt is non-NULL, it is the | |
39703eb9 MM |
14827 | IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is |
14828 | as for cp_parser_initializer. */ | |
a723baf1 | 14829 | |
4038c495 | 14830 | static VEC(constructor_elt,gc) * |
39703eb9 | 14831 | cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p) |
a723baf1 | 14832 | { |
4038c495 | 14833 | VEC(constructor_elt,gc) *v = NULL; |
a723baf1 | 14834 | |
39703eb9 MM |
14835 | /* Assume all of the expressions are constant. */ |
14836 | *non_constant_p = false; | |
14837 | ||
a723baf1 MM |
14838 | /* Parse the rest of the list. */ |
14839 | while (true) | |
14840 | { | |
14841 | cp_token *token; | |
14842 | tree identifier; | |
14843 | tree initializer; | |
39703eb9 MM |
14844 | bool clause_non_constant_p; |
14845 | ||
a723baf1 MM |
14846 | /* If the next token is an identifier and the following one is a |
14847 | colon, we are looking at the GNU designated-initializer | |
14848 | syntax. */ | |
14849 | if (cp_parser_allow_gnu_extensions_p (parser) | |
14850 | && cp_lexer_next_token_is (parser->lexer, CPP_NAME) | |
14851 | && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON) | |
14852 | { | |
05b22df9 | 14853 | /* Warn the user that they are using an extension. */ |
509c9d60 | 14854 | pedwarn (input_location, OPT_pedantic, |
fcf73884 | 14855 | "ISO C++ does not allow designated initializers"); |
a723baf1 | 14856 | /* Consume the identifier. */ |
d6b418fa | 14857 | identifier = cp_lexer_consume_token (parser->lexer)->u.value; |
a723baf1 MM |
14858 | /* Consume the `:'. */ |
14859 | cp_lexer_consume_token (parser->lexer); | |
14860 | } | |
14861 | else | |
14862 | identifier = NULL_TREE; | |
14863 | ||
14864 | /* Parse the initializer. */ | |
21526606 | 14865 | initializer = cp_parser_initializer_clause (parser, |
39703eb9 MM |
14866 | &clause_non_constant_p); |
14867 | /* If any clause is non-constant, so is the entire initializer. */ | |
14868 | if (clause_non_constant_p) | |
14869 | *non_constant_p = true; | |
4038c495 | 14870 | |
5d80a306 DG |
14871 | /* If we have an ellipsis, this is an initializer pack |
14872 | expansion. */ | |
14873 | if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)) | |
14874 | { | |
14875 | /* Consume the `...'. */ | |
14876 | cp_lexer_consume_token (parser->lexer); | |
14877 | ||
14878 | /* Turn the initializer into an initializer expansion. */ | |
14879 | initializer = make_pack_expansion (initializer); | |
14880 | } | |
14881 | ||
4038c495 GB |
14882 | /* Add it to the vector. */ |
14883 | CONSTRUCTOR_APPEND_ELT(v, identifier, initializer); | |
a723baf1 MM |
14884 | |
14885 | /* If the next token is not a comma, we have reached the end of | |
14886 | the list. */ | |
14887 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)) | |
14888 | break; | |
14889 | ||
14890 | /* Peek at the next token. */ | |
14891 | token = cp_lexer_peek_nth_token (parser->lexer, 2); | |
14892 | /* If the next token is a `}', then we're still done. An | |
14893 | initializer-clause can have a trailing `,' after the | |
14894 | initializer-list and before the closing `}'. */ | |
14895 | if (token->type == CPP_CLOSE_BRACE) | |
14896 | break; | |
14897 | ||
14898 | /* Consume the `,' token. */ | |
14899 | cp_lexer_consume_token (parser->lexer); | |
14900 | } | |
14901 | ||
4038c495 | 14902 | return v; |
a723baf1 MM |
14903 | } |
14904 | ||
14905 | /* Classes [gram.class] */ | |
14906 | ||
14907 | /* Parse a class-name. | |
14908 | ||
14909 | class-name: | |
14910 | identifier | |
14911 | template-id | |
14912 | ||
14913 | TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used | |
14914 | to indicate that names looked up in dependent types should be | |
14915 | assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template' | |
14916 | keyword has been used to indicate that the name that appears next | |
fc6a28d7 MM |
14917 | is a template. TAG_TYPE indicates the explicit tag given before |
14918 | the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are | |
14919 | looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class | |
14920 | is the class being defined in a class-head. | |
a723baf1 MM |
14921 | |
14922 | Returns the TYPE_DECL representing the class. */ | |
14923 | ||
14924 | static tree | |
21526606 EC |
14925 | cp_parser_class_name (cp_parser *parser, |
14926 | bool typename_keyword_p, | |
14927 | bool template_keyword_p, | |
fc6a28d7 | 14928 | enum tag_types tag_type, |
a723baf1 | 14929 | bool check_dependency_p, |
a668c6ad MM |
14930 | bool class_head_p, |
14931 | bool is_declaration) | |
a723baf1 MM |
14932 | { |
14933 | tree decl; | |
14934 | tree scope; | |
14935 | bool typename_p; | |
e5976695 | 14936 | cp_token *token; |
b5a029b1 | 14937 | tree identifier = NULL_TREE; |
e5976695 MM |
14938 | |
14939 | /* All class-names start with an identifier. */ | |
14940 | token = cp_lexer_peek_token (parser->lexer); | |
14941 | if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID) | |
14942 | { | |
14943 | cp_parser_error (parser, "expected class-name"); | |
14944 | return error_mark_node; | |
14945 | } | |
21526606 | 14946 | |
a723baf1 MM |
14947 | /* PARSER->SCOPE can be cleared when parsing the template-arguments |
14948 | to a template-id, so we save it here. */ | |
14949 | scope = parser->scope; | |
3adee96c KL |
14950 | if (scope == error_mark_node) |
14951 | return error_mark_node; | |
21526606 | 14952 | |
a723baf1 MM |
14953 | /* Any name names a type if we're following the `typename' keyword |
14954 | in a qualified name where the enclosing scope is type-dependent. */ | |
14955 | typename_p = (typename_keyword_p && scope && TYPE_P (scope) | |
1fb3244a | 14956 | && dependent_type_p (scope)); |
e5976695 MM |
14957 | /* Handle the common case (an identifier, but not a template-id) |
14958 | efficiently. */ | |
21526606 | 14959 | if (token->type == CPP_NAME |
f4abade9 | 14960 | && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2)) |
a723baf1 | 14961 | { |
91b1ca65 | 14962 | cp_token *identifier_token; |
91b1ca65 | 14963 | bool ambiguous_p; |
a723baf1 MM |
14964 | |
14965 | /* Look for the identifier. */ | |
91b1ca65 MM |
14966 | identifier_token = cp_lexer_peek_token (parser->lexer); |
14967 | ambiguous_p = identifier_token->ambiguous_p; | |
a723baf1 MM |
14968 | identifier = cp_parser_identifier (parser); |
14969 | /* If the next token isn't an identifier, we are certainly not | |
14970 | looking at a class-name. */ | |
14971 | if (identifier == error_mark_node) | |
14972 | decl = error_mark_node; | |
14973 | /* If we know this is a type-name, there's no need to look it | |
14974 | up. */ | |
14975 | else if (typename_p) | |
14976 | decl = identifier; | |
14977 | else | |
14978 | { | |
91b1ca65 MM |
14979 | tree ambiguous_decls; |
14980 | /* If we already know that this lookup is ambiguous, then | |
14981 | we've already issued an error message; there's no reason | |
14982 | to check again. */ | |
14983 | if (ambiguous_p) | |
14984 | { | |
14985 | cp_parser_simulate_error (parser); | |
14986 | return error_mark_node; | |
14987 | } | |
a723baf1 MM |
14988 | /* If the next token is a `::', then the name must be a type |
14989 | name. | |
14990 | ||
14991 | [basic.lookup.qual] | |
14992 | ||
14993 | During the lookup for a name preceding the :: scope | |
14994 | resolution operator, object, function, and enumerator | |
14995 | names are ignored. */ | |
14996 | if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)) | |
fc6a28d7 | 14997 | tag_type = typename_type; |
a723baf1 | 14998 | /* Look up the name. */ |
21526606 | 14999 | decl = cp_parser_lookup_name (parser, identifier, |
fc6a28d7 | 15000 | tag_type, |
b0bc6e8e | 15001 | /*is_template=*/false, |
eea9800f | 15002 | /*is_namespace=*/false, |
8f78f01f | 15003 | check_dependency_p, |
2b7a3abf DS |
15004 | &ambiguous_decls, |
15005 | identifier_token->location); | |
91b1ca65 MM |
15006 | if (ambiguous_decls) |
15007 | { | |
69bc6bff MLI |
15008 | error_at (identifier_token->location, |
15009 | "reference to %qD is ambiguous", identifier); | |
91b1ca65 MM |
15010 | print_candidates (ambiguous_decls); |
15011 | if (cp_parser_parsing_tentatively (parser)) | |
15012 | { | |
15013 | identifier_token->ambiguous_p = true; | |
15014 | cp_parser_simulate_error (parser); | |
15015 | } | |
15016 | return error_mark_node; | |
15017 | } | |
a723baf1 MM |
15018 | } |
15019 | } | |
e5976695 MM |
15020 | else |
15021 | { | |
15022 | /* Try a template-id. */ | |
15023 | decl = cp_parser_template_id (parser, template_keyword_p, | |
a668c6ad MM |
15024 | check_dependency_p, |
15025 | is_declaration); | |
e5976695 MM |
15026 | if (decl == error_mark_node) |
15027 | return error_mark_node; | |
15028 | } | |
a723baf1 MM |
15029 | |
15030 | decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p); | |
15031 | ||
15032 | /* If this is a typename, create a TYPENAME_TYPE. */ | |
15033 | if (typename_p && decl != error_mark_node) | |
4bfb8bba | 15034 | { |
8da15291 | 15035 | decl = make_typename_type (scope, decl, typename_type, |
3db45ab5 | 15036 | /*complain=*/tf_error); |
4bfb8bba MM |
15037 | if (decl != error_mark_node) |
15038 | decl = TYPE_NAME (decl); | |
15039 | } | |
a723baf1 MM |
15040 | |
15041 | /* Check to see that it is really the name of a class. */ | |
21526606 | 15042 | if (TREE_CODE (decl) == TEMPLATE_ID_EXPR |
a723baf1 MM |
15043 | && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE |
15044 | && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)) | |
15045 | /* Situations like this: | |
15046 | ||
15047 | template <typename T> struct A { | |
21526606 | 15048 | typename T::template X<int>::I i; |
a723baf1 MM |
15049 | }; |
15050 | ||
15051 | are problematic. Is `T::template X<int>' a class-name? The | |
15052 | standard does not seem to be definitive, but there is no other | |
15053 | valid interpretation of the following `::'. Therefore, those | |
15054 | names are considered class-names. */ | |
a723baf1 | 15055 | { |
94d285a5 VR |
15056 | decl = make_typename_type (scope, decl, tag_type, tf_error); |
15057 | if (decl != error_mark_node) | |
15058 | decl = TYPE_NAME (decl); | |
a723baf1 | 15059 | } |
94d285a5 VR |
15060 | else if (TREE_CODE (decl) != TYPE_DECL |
15061 | || TREE_TYPE (decl) == error_mark_node | |
9e1e64ec | 15062 | || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))) |
94d285a5 VR |
15063 | decl = error_mark_node; |
15064 | ||
15065 | if (decl == error_mark_node) | |
15066 | cp_parser_error (parser, "expected class-name"); | |
b5a029b1 JM |
15067 | else if (identifier && !parser->scope) |
15068 | maybe_note_name_used_in_class (identifier, decl); | |
a723baf1 MM |
15069 | |
15070 | return decl; | |
15071 | } | |
15072 | ||
15073 | /* Parse a class-specifier. | |
15074 | ||
15075 | class-specifier: | |
15076 | class-head { member-specification [opt] } | |
15077 | ||
15078 | Returns the TREE_TYPE representing the class. */ | |
15079 | ||
15080 | static tree | |
94edc4ab | 15081 | cp_parser_class_specifier (cp_parser* parser) |
a723baf1 | 15082 | { |
a723baf1 | 15083 | tree type; |
6de9cd9a | 15084 | tree attributes = NULL_TREE; |
a723baf1 | 15085 | bool nested_name_specifier_p; |
a723baf1 | 15086 | unsigned saved_num_template_parameter_lists; |
56c7f9c1 | 15087 | bool saved_in_function_body; |
73f971b7 | 15088 | bool saved_in_unbraced_linkage_specification_p; |
87c465f5 | 15089 | tree old_scope = NULL_TREE; |
2436b51f | 15090 | tree scope = NULL_TREE; |
7f9faf5e | 15091 | tree bases; |
a723baf1 | 15092 | |
8d241e0b | 15093 | push_deferring_access_checks (dk_no_deferred); |
cf22909c | 15094 | |
a723baf1 MM |
15095 | /* Parse the class-head. */ |
15096 | type = cp_parser_class_head (parser, | |
38b305d0 | 15097 | &nested_name_specifier_p, |
7f9faf5e LM |
15098 | &attributes, |
15099 | &bases); | |
a723baf1 MM |
15100 | /* If the class-head was a semantic disaster, skip the entire body |
15101 | of the class. */ | |
15102 | if (!type) | |
15103 | { | |
15104 | cp_parser_skip_to_end_of_block_or_statement (parser); | |
cf22909c | 15105 | pop_deferring_access_checks (); |
a723baf1 MM |
15106 | return error_mark_node; |
15107 | } | |
cf22909c | 15108 | |
a723baf1 | 15109 | /* Look for the `{'. */ |
3a483371 | 15110 | if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>")) |
cf22909c KL |
15111 | { |
15112 | pop_deferring_access_checks (); | |
15113 | return error_mark_node; | |
15114 | } | |
15115 | ||
7f9faf5e LM |
15116 | /* Process the base classes. If they're invalid, skip the |
15117 | entire class body. */ | |
15118 | if (!xref_basetypes (type, bases)) | |
15119 | { | |
7f9faf5e LM |
15120 | /* Consuming the closing brace yields better error messages |
15121 | later on. */ | |
1094da91 SM |
15122 | if (cp_parser_skip_to_closing_brace (parser)) |
15123 | cp_lexer_consume_token (parser->lexer); | |
7f9faf5e LM |
15124 | pop_deferring_access_checks (); |
15125 | return error_mark_node; | |
15126 | } | |
15127 | ||
a723baf1 MM |
15128 | /* Issue an error message if type-definitions are forbidden here. */ |
15129 | cp_parser_check_type_definition (parser); | |
15130 | /* Remember that we are defining one more class. */ | |
15131 | ++parser->num_classes_being_defined; | |
15132 | /* Inside the class, surrounding template-parameter-lists do not | |
15133 | apply. */ | |
21526606 EC |
15134 | saved_num_template_parameter_lists |
15135 | = parser->num_template_parameter_lists; | |
a723baf1 | 15136 | parser->num_template_parameter_lists = 0; |
56c7f9c1 MM |
15137 | /* We are not in a function body. */ |
15138 | saved_in_function_body = parser->in_function_body; | |
15139 | parser->in_function_body = false; | |
73f971b7 JC |
15140 | /* We are not immediately inside an extern "lang" block. */ |
15141 | saved_in_unbraced_linkage_specification_p | |
15142 | = parser->in_unbraced_linkage_specification_p; | |
15143 | parser->in_unbraced_linkage_specification_p = false; | |
78757caa | 15144 | |
a723baf1 | 15145 | /* Start the class. */ |
eeb23c11 | 15146 | if (nested_name_specifier_p) |
2436b51f MM |
15147 | { |
15148 | scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type)); | |
87c465f5 | 15149 | old_scope = push_inner_scope (scope); |
2436b51f | 15150 | } |
b9e75696 | 15151 | type = begin_class_definition (type, attributes); |
98ca843c | 15152 | |
a723baf1 | 15153 | if (type == error_mark_node) |
9bcb9aae | 15154 | /* If the type is erroneous, skip the entire body of the class. */ |
a723baf1 MM |
15155 | cp_parser_skip_to_closing_brace (parser); |
15156 | else | |
15157 | /* Parse the member-specification. */ | |
15158 | cp_parser_member_specification_opt (parser); | |
98ca843c | 15159 | |
a723baf1 | 15160 | /* Look for the trailing `}'. */ |
3a483371 | 15161 | cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>"); |
38b305d0 | 15162 | /* Look for trailing attributes to apply to this class. */ |
a723baf1 | 15163 | if (cp_parser_allow_gnu_extensions_p (parser)) |
b9e75696 | 15164 | attributes = cp_parser_attributes_opt (parser); |
38b305d0 JM |
15165 | if (type != error_mark_node) |
15166 | type = finish_struct (type, attributes); | |
87c465f5 KL |
15167 | if (nested_name_specifier_p) |
15168 | pop_inner_scope (old_scope, scope); | |
a723baf1 MM |
15169 | /* If this class is not itself within the scope of another class, |
15170 | then we need to parse the bodies of all of the queued function | |
15171 | definitions. Note that the queued functions defined in a class | |
15172 | are not always processed immediately following the | |
15173 | class-specifier for that class. Consider: | |
15174 | ||
15175 | struct A { | |
0cbd7506 | 15176 | struct B { void f() { sizeof (A); } }; |
a723baf1 MM |
15177 | }; |
15178 | ||
15179 | If `f' were processed before the processing of `A' were | |
15180 | completed, there would be no way to compute the size of `A'. | |
15181 | Note that the nesting we are interested in here is lexical -- | |
15182 | not the semantic nesting given by TYPE_CONTEXT. In particular, | |
15183 | for: | |
15184 | ||
15185 | struct A { struct B; }; | |
15186 | struct A::B { void f() { } }; | |
15187 | ||
15188 | there is no need to delay the parsing of `A::B::f'. */ | |
21526606 | 15189 | if (--parser->num_classes_being_defined == 0) |
a723baf1 | 15190 | { |
8218bd34 MM |
15191 | tree queue_entry; |
15192 | tree fn; | |
4514aa8c NS |
15193 | tree class_type = NULL_TREE; |
15194 | tree pushed_scope = NULL_TREE; | |
3db45ab5 | 15195 | |
8218bd34 MM |
15196 | /* In a first pass, parse default arguments to the functions. |
15197 | Then, in a second pass, parse the bodies of the functions. | |
15198 | This two-phased approach handles cases like: | |
21526606 EC |
15199 | |
15200 | struct S { | |
0cbd7506 MS |
15201 | void f() { g(); } |
15202 | void g(int i = 3); | |
15203 | }; | |
8218bd34 | 15204 | |
0cbd7506 | 15205 | */ |
8db1028e NS |
15206 | for (TREE_PURPOSE (parser->unparsed_functions_queues) |
15207 | = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues)); | |
15208 | (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues)); | |
15209 | TREE_PURPOSE (parser->unparsed_functions_queues) | |
15210 | = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues))) | |
8218bd34 MM |
15211 | { |
15212 | fn = TREE_VALUE (queue_entry); | |
8218bd34 MM |
15213 | /* If there are default arguments that have not yet been processed, |
15214 | take care of them now. */ | |
f44b0c8e MM |
15215 | if (class_type != TREE_PURPOSE (queue_entry)) |
15216 | { | |
4514aa8c NS |
15217 | if (pushed_scope) |
15218 | pop_scope (pushed_scope); | |
f44b0c8e | 15219 | class_type = TREE_PURPOSE (queue_entry); |
4514aa8c | 15220 | pushed_scope = push_scope (class_type); |
f44b0c8e MM |
15221 | } |
15222 | /* Make sure that any template parameters are in scope. */ | |
15223 | maybe_begin_member_template_processing (fn); | |
15224 | /* Parse the default argument expressions. */ | |
8218bd34 MM |
15225 | cp_parser_late_parsing_default_args (parser, fn); |
15226 | /* Remove any template parameters from the symbol table. */ | |
15227 | maybe_end_member_template_processing (); | |
15228 | } | |
4514aa8c NS |
15229 | if (pushed_scope) |
15230 | pop_scope (pushed_scope); | |
8218bd34 | 15231 | /* Now parse the body of the functions. */ |
8db1028e NS |
15232 | for (TREE_VALUE (parser->unparsed_functions_queues) |
15233 | = nreverse (TREE_VALUE (parser->unparsed_functions_queues)); | |
15234 | (queue_entry = TREE_VALUE (parser->unparsed_functions_queues)); | |
15235 | TREE_VALUE (parser->unparsed_functions_queues) | |
15236 | = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues))) | |
a723baf1 | 15237 | { |
a723baf1 | 15238 | /* Figure out which function we need to process. */ |
a723baf1 | 15239 | fn = TREE_VALUE (queue_entry); |
a723baf1 MM |
15240 | /* Parse the function. */ |
15241 | cp_parser_late_parsing_for_member (parser, fn); | |
a723baf1 | 15242 | } |
a723baf1 MM |
15243 | } |
15244 | ||
15245 | /* Put back any saved access checks. */ | |
cf22909c | 15246 | pop_deferring_access_checks (); |
a723baf1 | 15247 | |
56c7f9c1 MM |
15248 | /* Restore saved state. */ |
15249 | parser->in_function_body = saved_in_function_body; | |
a723baf1 MM |
15250 | parser->num_template_parameter_lists |
15251 | = saved_num_template_parameter_lists; | |
73f971b7 JC |
15252 | parser->in_unbraced_linkage_specification_p |
15253 | = saved_in_unbraced_linkage_specification_p; | |
a723baf1 MM |
15254 | |
15255 | return type; | |
15256 | } | |
15257 | ||
15258 | /* Parse a class-head. | |
15259 | ||
15260 | class-head: | |
15261 | class-key identifier [opt] base-clause [opt] | |
15262 | class-key nested-name-specifier identifier base-clause [opt] | |
21526606 EC |
15263 | class-key nested-name-specifier [opt] template-id |
15264 | base-clause [opt] | |
a723baf1 MM |
15265 | |
15266 | GNU Extensions: | |
15267 | class-key attributes identifier [opt] base-clause [opt] | |
15268 | class-key attributes nested-name-specifier identifier base-clause [opt] | |
21526606 EC |
15269 | class-key attributes nested-name-specifier [opt] template-id |
15270 | base-clause [opt] | |
a723baf1 | 15271 | |
583c0b05 DM |
15272 | Upon return BASES is initialized to the list of base classes (or |
15273 | NULL, if there are none) in the same form returned by | |
15274 | cp_parser_base_clause. | |
15275 | ||
a723baf1 MM |
15276 | Returns the TYPE of the indicated class. Sets |
15277 | *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions | |
15278 | involving a nested-name-specifier was used, and FALSE otherwise. | |
a723baf1 | 15279 | |
55dcbc12 | 15280 | Returns error_mark_node if this is not a class-head. |
c8094d83 | 15281 | |
a723baf1 MM |
15282 | Returns NULL_TREE if the class-head is syntactically valid, but |
15283 | semantically invalid in a way that means we should skip the entire | |
15284 | body of the class. */ | |
15285 | ||
15286 | static tree | |
21526606 | 15287 | cp_parser_class_head (cp_parser* parser, |
38b305d0 | 15288 | bool* nested_name_specifier_p, |
7f9faf5e LM |
15289 | tree *attributes_p, |
15290 | tree *bases) | |
a723baf1 | 15291 | { |
a723baf1 MM |
15292 | tree nested_name_specifier; |
15293 | enum tag_types class_key; | |
15294 | tree id = NULL_TREE; | |
15295 | tree type = NULL_TREE; | |
15296 | tree attributes; | |
15297 | bool template_id_p = false; | |
15298 | bool qualified_p = false; | |
15299 | bool invalid_nested_name_p = false; | |
afb0918a | 15300 | bool invalid_explicit_specialization_p = false; |
4514aa8c | 15301 | tree pushed_scope = NULL_TREE; |
a723baf1 | 15302 | unsigned num_templates; |
2b7a3abf | 15303 | cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL; |
a723baf1 MM |
15304 | /* Assume no nested-name-specifier will be present. */ |
15305 | *nested_name_specifier_p = false; | |
15306 | /* Assume no template parameter lists will be used in defining the | |
15307 | type. */ | |
15308 | num_templates = 0; | |
15309 | ||
583c0b05 DM |
15310 | *bases = NULL_TREE; |
15311 | ||
a723baf1 MM |
15312 | /* Look for the class-key. */ |
15313 | class_key = cp_parser_class_key (parser); | |
15314 | if (class_key == none_type) | |
15315 | return error_mark_node; | |
15316 | ||
15317 | /* Parse the attributes. */ | |
15318 | attributes = cp_parser_attributes_opt (parser); | |
15319 | ||
15320 | /* If the next token is `::', that is invalid -- but sometimes | |
15321 | people do try to write: | |
15322 | ||
21526606 | 15323 | struct ::S {}; |
a723baf1 MM |
15324 | |
15325 | Handle this gracefully by accepting the extra qualifier, and then | |
15326 | issuing an error about it later if this really is a | |
2050a1bb | 15327 | class-head. If it turns out just to be an elaborated type |
a723baf1 | 15328 | specifier, remain silent. */ |
88525e1b | 15329 | if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)) |
a723baf1 MM |
15330 | qualified_p = true; |
15331 | ||
8d241e0b KL |
15332 | push_deferring_access_checks (dk_no_check); |
15333 | ||
a723baf1 MM |
15334 | /* Determine the name of the class. Begin by looking for an |
15335 | optional nested-name-specifier. */ | |
2b7a3abf | 15336 | nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer); |
21526606 | 15337 | nested_name_specifier |
a723baf1 MM |
15338 | = cp_parser_nested_name_specifier_opt (parser, |
15339 | /*typename_keyword_p=*/false, | |
66d418e6 | 15340 | /*check_dependency_p=*/false, |
a668c6ad MM |
15341 | /*type_p=*/false, |
15342 | /*is_declaration=*/false); | |
a723baf1 MM |
15343 | /* If there was a nested-name-specifier, then there *must* be an |
15344 | identifier. */ | |
15345 | if (nested_name_specifier) | |
15346 | { | |
2b7a3abf | 15347 | type_start_token = cp_lexer_peek_token (parser->lexer); |
a723baf1 MM |
15348 | /* Although the grammar says `identifier', it really means |
15349 | `class-name' or `template-name'. You are only allowed to | |
15350 | define a class that has already been declared with this | |
21526606 | 15351 | syntax. |
a723baf1 | 15352 | |
ddd0b831 | 15353 | The proposed resolution for Core Issue 180 says that wherever |
a723baf1 | 15354 | you see `class T::X' you should treat `X' as a type-name. |
21526606 | 15355 | |
a723baf1 | 15356 | It is OK to define an inaccessible class; for example: |
21526606 | 15357 | |
0cbd7506 MS |
15358 | class A { class B; }; |
15359 | class A::B {}; | |
21526606 | 15360 | |
0cbd7506 | 15361 | We do not know if we will see a class-name, or a |
a723baf1 MM |
15362 | template-name. We look for a class-name first, in case the |
15363 | class-name is a template-id; if we looked for the | |
15364 | template-name first we would stop after the template-name. */ | |
15365 | cp_parser_parse_tentatively (parser); | |
15366 | type = cp_parser_class_name (parser, | |
15367 | /*typename_keyword_p=*/false, | |
15368 | /*template_keyword_p=*/false, | |
fc6a28d7 | 15369 | class_type, |
a723baf1 | 15370 | /*check_dependency_p=*/false, |
a668c6ad MM |
15371 | /*class_head_p=*/true, |
15372 | /*is_declaration=*/false); | |
a723baf1 MM |
15373 | /* If that didn't work, ignore the nested-name-specifier. */ |
15374 | if (!cp_parser_parse_definitely (parser)) | |
15375 | { | |
15376 | invalid_nested_name_p = true; | |
760b18ad | 15377 | type_start_token = cp_lexer_peek_token (parser->lexer); |
a723baf1 MM |
15378 | id = cp_parser_identifier (parser); |
15379 | if (id == error_mark_node) | |
15380 | id = NULL_TREE; | |
15381 | } | |
15382 | /* If we could not find a corresponding TYPE, treat this | |
15383 | declaration like an unqualified declaration. */ | |
15384 | if (type == error_mark_node) | |
15385 | nested_name_specifier = NULL_TREE; | |
15386 | /* Otherwise, count the number of templates used in TYPE and its | |
15387 | containing scopes. */ | |
21526606 | 15388 | else |
a723baf1 MM |
15389 | { |
15390 | tree scope; | |
15391 | ||
21526606 | 15392 | for (scope = TREE_TYPE (type); |
a723baf1 | 15393 | scope && TREE_CODE (scope) != NAMESPACE_DECL; |
21526606 | 15394 | scope = (TYPE_P (scope) |
a723baf1 | 15395 | ? TYPE_CONTEXT (scope) |
21526606 EC |
15396 | : DECL_CONTEXT (scope))) |
15397 | if (TYPE_P (scope) | |
a723baf1 MM |
15398 | && CLASS_TYPE_P (scope) |
15399 | && CLASSTYPE_TEMPLATE_INFO (scope) | |
2050a1bb MM |
15400 | && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)) |
15401 | && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope)) | |
a723baf1 MM |
15402 | ++num_templates; |
15403 | } | |
15404 | } | |
15405 | /* Otherwise, the identifier is optional. */ | |
15406 | else | |
15407 | { | |
15408 | /* We don't know whether what comes next is a template-id, | |
15409 | an identifier, or nothing at all. */ | |
15410 | cp_parser_parse_tentatively (parser); | |
15411 | /* Check for a template-id. */ | |
2b7a3abf | 15412 | type_start_token = cp_lexer_peek_token (parser->lexer); |
21526606 | 15413 | id = cp_parser_template_id (parser, |
a723baf1 | 15414 | /*template_keyword_p=*/false, |
a668c6ad MM |
15415 | /*check_dependency_p=*/true, |
15416 | /*is_declaration=*/true); | |
a723baf1 MM |
15417 | /* If that didn't work, it could still be an identifier. */ |
15418 | if (!cp_parser_parse_definitely (parser)) | |
15419 | { | |
15420 | if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)) | |
760b18ad DS |
15421 | { |
15422 | type_start_token = cp_lexer_peek_token (parser->lexer); | |
15423 | id = cp_parser_identifier (parser); | |
15424 | } | |
a723baf1 MM |
15425 | else |
15426 | id = NULL_TREE; | |
15427 | } | |
15428 | else | |
15429 | { | |
15430 | template_id_p = true; | |
15431 | ++num_templates; | |
15432 | } | |
15433 | } | |
15434 | ||
8d241e0b KL |
15435 | pop_deferring_access_checks (); |
15436 | ||
15077df5 | 15437 | if (id) |
760b18ad DS |
15438 | cp_parser_check_for_invalid_template_id (parser, id, |
15439 | type_start_token->location); | |
ee43dab5 | 15440 | |
a723baf1 MM |
15441 | /* If it's not a `:' or a `{' then we can't really be looking at a |
15442 | class-head, since a class-head only appears as part of a | |
15443 | class-specifier. We have to detect this situation before calling | |
15444 | xref_tag, since that has irreversible side-effects. */ | |
15445 | if (!cp_parser_next_token_starts_class_definition_p (parser)) | |
15446 | { | |
2a13a625 | 15447 | cp_parser_error (parser, "expected %<{%> or %<:%>"); |
a723baf1 MM |
15448 | return error_mark_node; |
15449 | } | |
15450 | ||
15451 | /* At this point, we're going ahead with the class-specifier, even | |
15452 | if some other problem occurs. */ | |
15453 | cp_parser_commit_to_tentative_parse (parser); | |
15454 | /* Issue the error about the overly-qualified name now. */ | |
15455 | if (qualified_p) | |
905319d9 PC |
15456 | { |
15457 | cp_parser_error (parser, | |
15458 | "global qualification of class name is invalid"); | |
15459 | return error_mark_node; | |
15460 | } | |
a723baf1 | 15461 | else if (invalid_nested_name_p) |
905319d9 PC |
15462 | { |
15463 | cp_parser_error (parser, | |
15464 | "qualified name does not name a class"); | |
15465 | return error_mark_node; | |
15466 | } | |
88081599 MM |
15467 | else if (nested_name_specifier) |
15468 | { | |
15469 | tree scope; | |
9bf0e588 VR |
15470 | |
15471 | /* Reject typedef-names in class heads. */ | |
15472 | if (!DECL_IMPLICIT_TYPEDEF_P (type)) | |
15473 | { | |
69bc6bff MLI |
15474 | error_at (type_start_token->location, |
15475 | "invalid class name in declaration of %qD", | |
15476 | type); | |
9bf0e588 VR |
15477 | type = NULL_TREE; |
15478 | goto done; | |
15479 | } | |
15480 | ||
88081599 MM |
15481 | /* Figure out in what scope the declaration is being placed. */ |
15482 | scope = current_scope (); | |
88081599 MM |
15483 | /* If that scope does not contain the scope in which the |
15484 | class was originally declared, the program is invalid. */ | |
15485 | if (scope && !is_ancestor (scope, nested_name_specifier)) | |
15486 | { | |
968fc32d | 15487 | if (at_namespace_scope_p ()) |
69bc6bff MLI |
15488 | error_at (type_start_token->location, |
15489 | "declaration of %qD in namespace %qD which does not " | |
15490 | "enclose %qD", | |
15491 | type, scope, nested_name_specifier); | |
968fc32d | 15492 | else |
69bc6bff MLI |
15493 | error_at (type_start_token->location, |
15494 | "declaration of %qD in %qD which does not enclose %qD", | |
15495 | type, scope, nested_name_specifier); | |
88081599 MM |
15496 | type = NULL_TREE; |
15497 | goto done; | |
15498 | } | |
15499 | /* [dcl.meaning] | |
15500 | ||
39a13be5 | 15501 | A declarator-id shall not be qualified except for the |
88081599 | 15502 | definition of a ... nested class outside of its class |
39a13be5 | 15503 | ... [or] the definition or explicit instantiation of a |
88081599 MM |
15504 | class member of a namespace outside of its namespace. */ |
15505 | if (scope == nested_name_specifier) | |
15506 | { | |
69bc6bff MLI |
15507 | permerror (nested_name_specifier_token_start->location, |
15508 | "extra qualification not allowed"); | |
88081599 MM |
15509 | nested_name_specifier = NULL_TREE; |
15510 | num_templates = 0; | |
15511 | } | |
15512 | } | |
afb0918a MM |
15513 | /* An explicit-specialization must be preceded by "template <>". If |
15514 | it is not, try to recover gracefully. */ | |
21526606 | 15515 | if (at_namespace_scope_p () |
afb0918a | 15516 | && parser->num_template_parameter_lists == 0 |
eeb23c11 | 15517 | && template_id_p) |
afb0918a | 15518 | { |
69bc6bff MLI |
15519 | error_at (type_start_token->location, |
15520 | "an explicit specialization must be preceded by %<template <>%>"); | |
afb0918a MM |
15521 | invalid_explicit_specialization_p = true; |
15522 | /* Take the same action that would have been taken by | |
15523 | cp_parser_explicit_specialization. */ | |
15524 | ++parser->num_template_parameter_lists; | |
15525 | begin_specialization (); | |
15526 | } | |
15527 | /* There must be no "return" statements between this point and the | |
15528 | end of this function; set "type "to the correct return value and | |
15529 | use "goto done;" to return. */ | |
a723baf1 MM |
15530 | /* Make sure that the right number of template parameters were |
15531 | present. */ | |
2b7a3abf | 15532 | if (!cp_parser_check_template_parameters (parser, num_templates, |
0d246707 MLI |
15533 | type_start_token->location, |
15534 | /*declarator=*/NULL)) | |
afb0918a MM |
15535 | { |
15536 | /* If something went wrong, there is no point in even trying to | |
15537 | process the class-definition. */ | |
15538 | type = NULL_TREE; | |
15539 | goto done; | |
15540 | } | |
a723baf1 | 15541 | |
a723baf1 MM |
15542 | /* Look up the type. */ |
15543 | if (template_id_p) | |
15544 | { | |
542f8eb9 JJ |
15545 | if (TREE_CODE (id) == TEMPLATE_ID_EXPR |
15546 | && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0)) | |
15547 | || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD)) | |
15548 | { | |
69bc6bff MLI |
15549 | error_at (type_start_token->location, |
15550 | "function template %qD redeclared as a class template", id); | |
542f8eb9 JJ |
15551 | type = error_mark_node; |
15552 | } | |
15553 | else | |
15554 | { | |
15555 | type = TREE_TYPE (id); | |
15556 | type = maybe_process_partial_specialization (type); | |
15557 | } | |
4514aa8c NS |
15558 | if (nested_name_specifier) |
15559 | pushed_scope = push_scope (nested_name_specifier); | |
a723baf1 | 15560 | } |
4514aa8c | 15561 | else if (nested_name_specifier) |
a723baf1 | 15562 | { |
a723baf1 MM |
15563 | tree class_type; |
15564 | ||
15565 | /* Given: | |
15566 | ||
15567 | template <typename T> struct S { struct T }; | |
14d22dd6 | 15568 | template <typename T> struct S<T>::T { }; |
a723baf1 MM |
15569 | |
15570 | we will get a TYPENAME_TYPE when processing the definition of | |
15571 | `S::T'. We need to resolve it to the actual type before we | |
15572 | try to define it. */ | |
15573 | if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE) | |
15574 | { | |
14d22dd6 MM |
15575 | class_type = resolve_typename_type (TREE_TYPE (type), |
15576 | /*only_current_p=*/false); | |
4195a767 | 15577 | if (TREE_CODE (class_type) != TYPENAME_TYPE) |
14d22dd6 MM |
15578 | type = TYPE_NAME (class_type); |
15579 | else | |
15580 | { | |
15581 | cp_parser_error (parser, "could not resolve typename type"); | |
15582 | type = error_mark_node; | |
15583 | } | |
a723baf1 MM |
15584 | } |
15585 | ||
c5285718 PC |
15586 | if (maybe_process_partial_specialization (TREE_TYPE (type)) |
15587 | == error_mark_node) | |
15588 | { | |
15589 | type = NULL_TREE; | |
15590 | goto done; | |
15591 | } | |
15592 | ||
560ad596 MM |
15593 | class_type = current_class_type; |
15594 | /* Enter the scope indicated by the nested-name-specifier. */ | |
4514aa8c | 15595 | pushed_scope = push_scope (nested_name_specifier); |
560ad596 MM |
15596 | /* Get the canonical version of this type. */ |
15597 | type = TYPE_MAIN_DECL (TREE_TYPE (type)); | |
15598 | if (PROCESSING_REAL_TEMPLATE_DECL_P () | |
15599 | && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type))) | |
55dcbc12 NS |
15600 | { |
15601 | type = push_template_decl (type); | |
15602 | if (type == error_mark_node) | |
15603 | { | |
15604 | type = NULL_TREE; | |
15605 | goto done; | |
15606 | } | |
15607 | } | |
c8094d83 | 15608 | |
560ad596 | 15609 | type = TREE_TYPE (type); |
4514aa8c | 15610 | *nested_name_specifier_p = true; |
a723baf1 | 15611 | } |
4514aa8c NS |
15612 | else /* The name is not a nested name. */ |
15613 | { | |
15614 | /* If the class was unnamed, create a dummy name. */ | |
15615 | if (!id) | |
15616 | id = make_anon_name (); | |
15617 | type = xref_tag (class_key, id, /*tag_scope=*/ts_current, | |
15618 | parser->num_template_parameter_lists); | |
15619 | } | |
15620 | ||
a723baf1 MM |
15621 | /* Indicate whether this class was declared as a `class' or as a |
15622 | `struct'. */ | |
15623 | if (TREE_CODE (type) == RECORD_TYPE) | |
15624 | CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type); | |
15625 | cp_parser_check_class_key (class_key, type); | |
15626 | ||
744b12b6 MM |
15627 | /* If this type was already complete, and we see another definition, |
15628 | that's an error. */ | |
15629 | if (type != error_mark_node && COMPLETE_TYPE_P (type)) | |
15630 | { | |
69bc6bff MLI |
15631 | error_at (type_start_token->location, "redefinition of %q#T", |
15632 | type); | |
15633 | error_at (type_start_token->location, "previous definition of %q+#T", | |
15634 | type); | |
0f3744f8 VR |
15635 | type = NULL_TREE; |
15636 | goto done; | |
744b12b6 | 15637 | } |
7f9faf5e LM |
15638 | else if (type == error_mark_node) |
15639 | type = NULL_TREE; | |
744b12b6 | 15640 | |
4514aa8c | 15641 | /* We will have entered the scope containing the class; the names of |
744b12b6 | 15642 | base classes should be looked up in that context. For example: |
a723baf1 MM |
15643 | |
15644 | struct A { struct B {}; struct C; }; | |
15645 | struct A::C : B {}; | |
15646 | ||
15647 | is valid. */ | |
98ca843c | 15648 | |
cad7e87b NS |
15649 | /* Get the list of base-classes, if there is one. */ |
15650 | if (cp_lexer_next_token_is (parser->lexer, CPP_COLON)) | |
7f9faf5e | 15651 | *bases = cp_parser_base_clause (parser); |
a723baf1 | 15652 | |
4514aa8c | 15653 | done: |
a723baf1 MM |
15654 | /* Leave the scope given by the nested-name-specifier. We will |
15655 | enter the class scope itself while processing the members. */ | |
4514aa8c NS |
15656 | if (pushed_scope) |
15657 | pop_scope (pushed_scope); | |
a723baf1 | 15658 | |
afb0918a MM |
15659 | if (invalid_explicit_specialization_p) |
15660 | { | |
15661 | end_specialization (); | |
15662 | --parser->num_template_parameter_lists; | |
15663 | } | |
38b305d0 | 15664 | *attributes_p = attributes; |
a723baf1 MM |
15665 | return type; |
15666 | } | |
15667 | ||
15668 | /* Parse a class-key. | |
15669 | ||
15670 | class-key: | |
15671 | class | |
15672 | struct | |
15673 | union | |
15674 | ||
15675 | Returns the kind of class-key specified, or none_type to indicate | |
15676 | error. */ | |
15677 | ||
15678 | static enum tag_types | |
94edc4ab | 15679 | cp_parser_class_key (cp_parser* parser) |
a723baf1 MM |
15680 | { |
15681 | cp_token *token; | |
15682 | enum tag_types tag_type; | |
15683 | ||
15684 | /* Look for the class-key. */ | |
15685 | token = cp_parser_require (parser, CPP_KEYWORD, "class-key"); | |
15686 | if (!token) | |
15687 | return none_type; | |
15688 | ||
15689 | /* Check to see if the TOKEN is a class-key. */ | |
15690 | tag_type = cp_parser_token_is_class_key (token); | |
15691 | if (!tag_type) | |
15692 | cp_parser_error (parser, "expected class-key"); | |
15693 | return tag_type; | |
15694 | } | |
15695 | ||
15696 | /* Parse an (optional) member-specification. | |
15697 | ||
15698 | member-specification: | |
15699 | member-declaration member-specification [opt] | |
15700 | access-specifier : member-specification [opt] */ | |
15701 | ||
15702 | static void | |
94edc4ab | 15703 | cp_parser_member_specification_opt (cp_parser* parser) |
a723baf1 MM |
15704 | { |
15705 | while (true) | |
15706 | { | |
15707 | cp_token *token; | |
15708 | enum rid keyword; | |
15709 | ||
15710 | /* Peek at the next token. */ | |
15711 | token = cp_lexer_peek_token (parser->lexer); | |
15712 | /* If it's a `}', or EOF then we've seen all the members. */ | |
bc4071dd RH |
15713 | if (token->type == CPP_CLOSE_BRACE |
15714 | || token->type == CPP_EOF | |
15715 | || token->type == CPP_PRAGMA_EOL) | |
a723baf1 MM |
15716 | break; |
15717 | ||
15718 | /* See if this token is a keyword. */ | |
15719 | keyword = token->keyword; | |
15720 | switch (keyword) | |
15721 | { | |
15722 | case RID_PUBLIC: | |
15723 | case RID_PROTECTED: | |
15724 | case RID_PRIVATE: | |
15725 | /* Consume the access-specifier. */ | |
15726 | cp_lexer_consume_token (parser->lexer); | |
15727 | /* Remember which access-specifier is active. */ | |
d6b418fa | 15728 | current_access_specifier = token->u.value; |
a723baf1 | 15729 | /* Look for the `:'. */ |
3a483371 | 15730 | cp_parser_require (parser, CPP_COLON, "%<:%>"); |
a723baf1 MM |
15731 | break; |
15732 | ||
15733 | default: | |
de3fe73c MM |
15734 | /* Accept #pragmas at class scope. */ |
15735 | if (token->type == CPP_PRAGMA) | |
15736 | { | |
bc4071dd | 15737 | cp_parser_pragma (parser, pragma_external); |
de3fe73c MM |
15738 | break; |
15739 | } | |
15740 | ||
a723baf1 MM |
15741 | /* Otherwise, the next construction must be a |
15742 | member-declaration. */ | |
15743 | cp_parser_member_declaration (parser); | |
a723baf1 MM |
15744 | } |
15745 | } | |
15746 | } | |
15747 | ||
21526606 | 15748 | /* Parse a member-declaration. |
a723baf1 MM |
15749 | |
15750 | member-declaration: | |
15751 | decl-specifier-seq [opt] member-declarator-list [opt] ; | |
15752 | function-definition ; [opt] | |
15753 | :: [opt] nested-name-specifier template [opt] unqualified-id ; | |
15754 | using-declaration | |
21526606 | 15755 | template-declaration |
a723baf1 MM |
15756 | |
15757 | member-declarator-list: | |
15758 | member-declarator | |
15759 | member-declarator-list , member-declarator | |
15760 | ||
15761 | member-declarator: | |
21526606 | 15762 | declarator pure-specifier [opt] |
a723baf1 | 15763 | declarator constant-initializer [opt] |
21526606 | 15764 | identifier [opt] : constant-expression |
a723baf1 MM |
15765 | |
15766 | GNU Extensions: | |
15767 | ||
15768 | member-declaration: | |
15769 | __extension__ member-declaration | |
15770 | ||
15771 | member-declarator: | |
15772 | declarator attributes [opt] pure-specifier [opt] | |
15773 | declarator attributes [opt] constant-initializer [opt] | |
55a3debe DG |
15774 | identifier [opt] attributes [opt] : constant-expression |
15775 | ||
15776 | C++0x Extensions: | |
15777 | ||
15778 | member-declaration: | |
15779 | static_assert-declaration */ | |
a723baf1 MM |
15780 | |
15781 | static void | |
94edc4ab | 15782 | cp_parser_member_declaration (cp_parser* parser) |
a723baf1 | 15783 | { |
62d1db17 | 15784 | cp_decl_specifier_seq decl_specifiers; |
a723baf1 MM |
15785 | tree prefix_attributes; |
15786 | tree decl; | |
560ad596 | 15787 | int declares_class_or_enum; |
a723baf1 | 15788 | bool friend_p; |
2b7a3abf DS |
15789 | cp_token *token = NULL; |
15790 | cp_token *decl_spec_token_start = NULL; | |
15791 | cp_token *initializer_token_start = NULL; | |
a723baf1 MM |
15792 | int saved_pedantic; |
15793 | ||
15794 | /* Check for the `__extension__' keyword. */ | |
15795 | if (cp_parser_extension_opt (parser, &saved_pedantic)) | |
15796 | { | |
15797 | /* Recurse. */ | |
15798 | cp_parser_member_declaration (parser); | |
15799 | /* Restore the old value of the PEDANTIC flag. */ | |
15800 | pedantic = saved_pedantic; | |
15801 | ||
15802 | return; | |
15803 | } | |
15804 | ||
15805 | /* Check for a template-declaration. */ | |
15806 | if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE)) | |
15807 | { | |
7e2a12d3 JC |
15808 | /* An explicit specialization here is an error condition, and we |
15809 | expect the specialization handler to detect and report this. */ | |
15810 | if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS | |
15811 | && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER) | |
15812 | cp_parser_explicit_specialization (parser); | |
15813 | else | |
15814 | cp_parser_template_declaration (parser, /*member_p=*/true); | |
a723baf1 MM |
15815 | |
15816 | return; | |
15817 | } | |
15818 | ||
15819 | /* Check for a using-declaration. */ | |
15820 | if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING)) | |
15821 | { | |
15822 | /* Parse the using-declaration. */ | |
981114e1 MM |
15823 | cp_parser_using_declaration (parser, |
15824 | /*access_declaration_p=*/false); | |
a723baf1 MM |
15825 | return; |
15826 | } | |
21526606 | 15827 | |
e58a9aa1 ZL |
15828 | /* Check for @defs. */ |
15829 | if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS)) | |
15830 | { | |
15831 | tree ivar, member; | |
15832 | tree ivar_chains = cp_parser_objc_defs_expression (parser); | |
15833 | ivar = ivar_chains; | |
15834 | while (ivar) | |
15835 | { | |
15836 | member = ivar; | |
15837 | ivar = TREE_CHAIN (member); | |
15838 | TREE_CHAIN (member) = NULL_TREE; | |
15839 | finish_member_declaration (member); | |
15840 | } | |
15841 | return; | |
15842 | } | |
15843 | ||
55a3debe DG |
15844 | /* If the next token is `static_assert' we have a static assertion. */ |
15845 | if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT)) | |
15846 | { | |
15847 | cp_parser_static_assert (parser, /*member_p=*/true); | |
15848 | return; | |
15849 | } | |
15850 | ||
981114e1 MM |
15851 | if (cp_parser_using_declaration (parser, /*access_declaration=*/true)) |
15852 | return; | |
15853 | ||
a723baf1 | 15854 | /* Parse the decl-specifier-seq. */ |
2b7a3abf | 15855 | decl_spec_token_start = cp_lexer_peek_token (parser->lexer); |
62d1db17 MM |
15856 | cp_parser_decl_specifier_seq (parser, |
15857 | CP_PARSER_FLAGS_OPTIONAL, | |
15858 | &decl_specifiers, | |
15859 | &declares_class_or_enum); | |
15860 | prefix_attributes = decl_specifiers.attributes; | |
15861 | decl_specifiers.attributes = NULL_TREE; | |
8fbc5ae7 | 15862 | /* Check for an invalid type-name. */ |
de3fe73c MM |
15863 | if (!decl_specifiers.type |
15864 | && cp_parser_parse_and_diagnose_invalid_type_name (parser)) | |
8fbc5ae7 | 15865 | return; |
a723baf1 MM |
15866 | /* If there is no declarator, then the decl-specifier-seq should |
15867 | specify a type. */ | |
15868 | if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)) | |
15869 | { | |
15870 | /* If there was no decl-specifier-seq, and the next token is a | |
15871 | `;', then we have something like: | |
15872 | ||
15873 | struct S { ; }; | |
15874 | ||
15875 | [class.mem] | |
15876 | ||
15877 | Each member-declaration shall declare at least one member | |
15878 | name of the class. */ | |
62d1db17 | 15879 | if (!decl_specifiers.any_specifiers_p) |
a723baf1 | 15880 | { |
2cfe82fe | 15881 | cp_token *token = cp_lexer_peek_token (parser->lexer); |
fcf73884 | 15882 | if (!in_system_header_at (token->location)) |
509c9d60 | 15883 | pedwarn (token->location, OPT_pedantic, "extra %<;%>"); |
a723baf1 | 15884 | } |
21526606 | 15885 | else |
a723baf1 MM |
15886 | { |
15887 | tree type; | |
21526606 | 15888 | |
a723baf1 | 15889 | /* See if this declaration is a friend. */ |
62d1db17 | 15890 | friend_p = cp_parser_friend_p (&decl_specifiers); |
a723baf1 MM |
15891 | /* If there were decl-specifiers, check to see if there was |
15892 | a class-declaration. */ | |
62d1db17 | 15893 | type = check_tag_decl (&decl_specifiers); |
a723baf1 MM |
15894 | /* Nested classes have already been added to the class, but |
15895 | a `friend' needs to be explicitly registered. */ | |
15896 | if (friend_p) | |
15897 | { | |
15898 | /* If the `friend' keyword was present, the friend must | |
15899 | be introduced with a class-key. */ | |
15900 | if (!declares_class_or_enum) | |
69bc6bff MLI |
15901 | error_at (decl_spec_token_start->location, |
15902 | "a class-key must be used when declaring a friend"); | |
a723baf1 MM |
15903 | /* In this case: |
15904 | ||
21526606 | 15905 | template <typename T> struct A { |
0cbd7506 MS |
15906 | friend struct A<T>::B; |
15907 | }; | |
21526606 | 15908 | |
a723baf1 MM |
15909 | A<T>::B will be represented by a TYPENAME_TYPE, and |
15910 | therefore not recognized by check_tag_decl. */ | |
98ca843c | 15911 | if (!type |
62d1db17 MM |
15912 | && decl_specifiers.type |
15913 | && TYPE_P (decl_specifiers.type)) | |
15914 | type = decl_specifiers.type; | |
fdd09134 | 15915 | if (!type || !TYPE_P (type)) |
69bc6bff MLI |
15916 | error_at (decl_spec_token_start->location, |
15917 | "friend declaration does not name a class or " | |
15918 | "function"); | |
a723baf1 | 15919 | else |
19db77ce KL |
15920 | make_friend_class (current_class_type, type, |
15921 | /*complain=*/true); | |
a723baf1 MM |
15922 | } |
15923 | /* If there is no TYPE, an error message will already have | |
15924 | been issued. */ | |
62d1db17 | 15925 | else if (!type || type == error_mark_node) |
a723baf1 MM |
15926 | ; |
15927 | /* An anonymous aggregate has to be handled specially; such | |
15928 | a declaration really declares a data member (with a | |
15929 | particular type), as opposed to a nested class. */ | |
15930 | else if (ANON_AGGR_TYPE_P (type)) | |
15931 | { | |
15932 | /* Remove constructors and such from TYPE, now that we | |
34cd5ae7 | 15933 | know it is an anonymous aggregate. */ |
a723baf1 MM |
15934 | fixup_anonymous_aggr (type); |
15935 | /* And make the corresponding data member. */ | |
c2255bc4 AH |
15936 | decl = build_decl (decl_spec_token_start->location, |
15937 | FIELD_DECL, NULL_TREE, type); | |
a723baf1 MM |
15938 | /* Add it to the class. */ |
15939 | finish_member_declaration (decl); | |
15940 | } | |
37d407a1 | 15941 | else |
2b7a3abf DS |
15942 | cp_parser_check_access_in_redeclaration |
15943 | (TYPE_NAME (type), | |
15944 | decl_spec_token_start->location); | |
a723baf1 MM |
15945 | } |
15946 | } | |
15947 | else | |
15948 | { | |
15949 | /* See if these declarations will be friends. */ | |
62d1db17 | 15950 | friend_p = cp_parser_friend_p (&decl_specifiers); |
a723baf1 | 15951 | |
21526606 | 15952 | /* Keep going until we hit the `;' at the end of the |
a723baf1 MM |
15953 | declaration. */ |
15954 | while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)) | |
15955 | { | |
15956 | tree attributes = NULL_TREE; | |
15957 | tree first_attribute; | |
15958 | ||
15959 | /* Peek at the next token. */ | |
15960 | token = cp_lexer_peek_token (parser->lexer); | |
15961 | ||
15962 | /* Check for a bitfield declaration. */ | |
15963 | if (token->type == CPP_COLON | |
15964 | || (token->type == CPP_NAME | |
21526606 | 15965 | && cp_lexer_peek_nth_token (parser->lexer, 2)->type |
a723baf1 MM |
15966 | == CPP_COLON)) |
15967 | { | |
15968 | tree identifier; | |
15969 | tree width; | |
15970 | ||
15971 | /* Get the name of the bitfield. Note that we cannot just | |
15972 | check TOKEN here because it may have been invalidated by | |
15973 | the call to cp_lexer_peek_nth_token above. */ | |
15974 | if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON) | |
15975 | identifier = cp_parser_identifier (parser); | |
15976 | else | |
15977 | identifier = NULL_TREE; | |
15978 | ||
15979 | /* Consume the `:' token. */ | |
15980 | cp_lexer_consume_token (parser->lexer); | |
15981 | /* Get the width of the bitfield. */ | |
21526606 | 15982 | width |
14d22dd6 MM |
15983 | = cp_parser_constant_expression (parser, |
15984 | /*allow_non_constant=*/false, | |
15985 | NULL); | |
a723baf1 MM |
15986 | |
15987 | /* Look for attributes that apply to the bitfield. */ | |
15988 | attributes = cp_parser_attributes_opt (parser); | |
15989 | /* Remember which attributes are prefix attributes and | |
15990 | which are not. */ | |
15991 | first_attribute = attributes; | |
15992 | /* Combine the attributes. */ | |
15993 | attributes = chainon (prefix_attributes, attributes); | |
15994 | ||
15995 | /* Create the bitfield declaration. */ | |
98ca843c | 15996 | decl = grokbitfield (identifier |
1d786913 | 15997 | ? make_id_declarator (NULL_TREE, |
d85d3d57 MM |
15998 | identifier, |
15999 | sfk_none) | |
058b15c1 | 16000 | : NULL, |
62d1db17 | 16001 | &decl_specifiers, |
a6d9bc9d SM |
16002 | width, |
16003 | attributes); | |
a723baf1 MM |
16004 | } |
16005 | else | |
16006 | { | |
058b15c1 | 16007 | cp_declarator *declarator; |
a723baf1 MM |
16008 | tree initializer; |
16009 | tree asm_specification; | |
7efa3e22 | 16010 | int ctor_dtor_or_conv_p; |
a723baf1 MM |
16011 | |
16012 | /* Parse the declarator. */ | |
21526606 | 16013 | declarator |
62b8a44e | 16014 | = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED, |
4bb8ca28 | 16015 | &ctor_dtor_or_conv_p, |
db86dd14 MM |
16016 | /*parenthesized_p=*/NULL, |
16017 | /*member_p=*/true); | |
a723baf1 MM |
16018 | |
16019 | /* If something went wrong parsing the declarator, make sure | |
16020 | that we at least consume some tokens. */ | |
058b15c1 | 16021 | if (declarator == cp_error_declarator) |
a723baf1 MM |
16022 | { |
16023 | /* Skip to the end of the statement. */ | |
16024 | cp_parser_skip_to_end_of_statement (parser); | |
4bb8ca28 MM |
16025 | /* If the next token is not a semicolon, that is |
16026 | probably because we just skipped over the body of | |
16027 | a function. So, we consume a semicolon if | |
16028 | present, but do not issue an error message if it | |
16029 | is not present. */ | |
16030 | if (cp_lexer_next_token_is (parser->lexer, | |
16031 | CPP_SEMICOLON)) | |
16032 | cp_lexer_consume_token (parser->lexer); | |
16033 | return; | |
a723baf1 MM |
16034 | } |
16035 | ||
fc6a28d7 MM |
16036 | if (declares_class_or_enum & 2) |
16037 | cp_parser_check_for_definition_in_return_type | |
760b18ad DS |
16038 | (declarator, decl_specifiers.type, |
16039 | decl_specifiers.type_location); | |
560ad596 | 16040 | |
a723baf1 MM |
16041 | /* Look for an asm-specification. */ |
16042 | asm_specification = cp_parser_asm_specification_opt (parser); | |
16043 | /* Look for attributes that apply to the declaration. */ | |
16044 | attributes = cp_parser_attributes_opt (parser); | |
16045 | /* Remember which attributes are prefix attributes and | |
16046 | which are not. */ | |
16047 | first_attribute = attributes; | |
16048 | /* Combine the attributes. */ | |
16049 | attributes = chainon (prefix_attributes, attributes); | |
16050 | ||
16051 | /* If it's an `=', then we have a constant-initializer or a | |
16052 | pure-specifier. It is not correct to parse the | |
16053 | initializer before registering the member declaration | |
16054 | since the member declaration should be in scope while | |
16055 | its initializer is processed. However, the rest of the | |
16056 | front end does not yet provide an interface that allows | |
16057 | us to handle this correctly. */ | |
16058 | if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)) | |
16059 | { | |
16060 | /* In [class.mem]: | |
16061 | ||
16062 | A pure-specifier shall be used only in the declaration of | |
21526606 | 16063 | a virtual function. |
a723baf1 MM |
16064 | |
16065 | A member-declarator can contain a constant-initializer | |
16066 | only if it declares a static member of integral or | |
21526606 | 16067 | enumeration type. |
a723baf1 MM |
16068 | |
16069 | Therefore, if the DECLARATOR is for a function, we look | |
16070 | for a pure-specifier; otherwise, we look for a | |
16071 | constant-initializer. When we call `grokfield', it will | |
16072 | perform more stringent semantics checks. */ | |
2b7a3abf | 16073 | initializer_token_start = cp_lexer_peek_token (parser->lexer); |
0b45aedd | 16074 | if (function_declarator_p (declarator)) |
a723baf1 MM |
16075 | initializer = cp_parser_pure_specifier (parser); |
16076 | else | |
4bb8ca28 MM |
16077 | /* Parse the initializer. */ |
16078 | initializer = cp_parser_constant_initializer (parser); | |
a723baf1 MM |
16079 | } |
16080 | /* Otherwise, there is no initializer. */ | |
16081 | else | |
16082 | initializer = NULL_TREE; | |
16083 | ||
16084 | /* See if we are probably looking at a function | |
5a19910e | 16085 | definition. We are certainly not looking at a |
a723baf1 MM |
16086 | member-declarator. Calling `grokfield' has |
16087 | side-effects, so we must not do it unless we are sure | |
16088 | that we are looking at a member-declarator. */ | |
21526606 | 16089 | if (cp_parser_token_starts_function_definition_p |
a723baf1 | 16090 | (cp_lexer_peek_token (parser->lexer))) |
4bb8ca28 MM |
16091 | { |
16092 | /* The grammar does not allow a pure-specifier to be | |
16093 | used when a member function is defined. (It is | |
16094 | possible that this fact is an oversight in the | |
16095 | standard, since a pure function may be defined | |
16096 | outside of the class-specifier. */ | |
16097 | if (initializer) | |
69bc6bff MLI |
16098 | error_at (initializer_token_start->location, |
16099 | "pure-specifier on function-definition"); | |
4bb8ca28 | 16100 | decl = cp_parser_save_member_function_body (parser, |
62d1db17 | 16101 | &decl_specifiers, |
4bb8ca28 MM |
16102 | declarator, |
16103 | attributes); | |
16104 | /* If the member was not a friend, declare it here. */ | |
16105 | if (!friend_p) | |
16106 | finish_member_declaration (decl); | |
16107 | /* Peek at the next token. */ | |
16108 | token = cp_lexer_peek_token (parser->lexer); | |
16109 | /* If the next token is a semicolon, consume it. */ | |
16110 | if (token->type == CPP_SEMICOLON) | |
9dfbac5b | 16111 | cp_lexer_consume_token (parser->lexer); |
4bb8ca28 MM |
16112 | return; |
16113 | } | |
a723baf1 | 16114 | else |
402b8cf6 AH |
16115 | if (declarator->kind == cdk_function) |
16116 | declarator->id_loc = token->location; | |
d174af6c MM |
16117 | /* Create the declaration. */ |
16118 | decl = grokfield (declarator, &decl_specifiers, | |
16119 | initializer, /*init_const_expr_p=*/true, | |
16120 | asm_specification, | |
16121 | attributes); | |
a723baf1 MM |
16122 | } |
16123 | ||
16124 | /* Reset PREFIX_ATTRIBUTES. */ | |
16125 | while (attributes && TREE_CHAIN (attributes) != first_attribute) | |
16126 | attributes = TREE_CHAIN (attributes); | |
16127 | if (attributes) | |
16128 | TREE_CHAIN (attributes) = NULL_TREE; | |
16129 | ||
16130 | /* If there is any qualification still in effect, clear it | |
16131 | now; we will be starting fresh with the next declarator. */ | |
16132 | parser->scope = NULL_TREE; | |
16133 | parser->qualifying_scope = NULL_TREE; | |
16134 | parser->object_scope = NULL_TREE; | |
16135 | /* If it's a `,', then there are more declarators. */ | |
16136 | if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)) | |
16137 | cp_lexer_consume_token (parser->lexer); | |
16138 | /* If the next token isn't a `;', then we have a parse error. */ | |
16139 | else if (cp_lexer_next_token_is_not (parser->lexer, | |
16140 | CPP_SEMICOLON)) | |
16141 | { | |
2a13a625 | 16142 | cp_parser_error (parser, "expected %<;%>"); |
04c06002 | 16143 | /* Skip tokens until we find a `;'. */ |
a723baf1 MM |
16144 | cp_parser_skip_to_end_of_statement (parser); |
16145 | ||
16146 | break; | |
16147 | } | |
16148 | ||
16149 | if (decl) | |
16150 | { | |
16151 | /* Add DECL to the list of members. */ | |
16152 | if (!friend_p) | |
16153 | finish_member_declaration (decl); | |
16154 | ||
a723baf1 | 16155 | if (TREE_CODE (decl) == FUNCTION_DECL) |
8db1028e | 16156 | cp_parser_save_default_args (parser, decl); |
a723baf1 MM |
16157 | } |
16158 | } | |
16159 | } | |
16160 | ||
3a483371 | 16161 | cp_parser_require (parser, CPP_SEMICOLON, "%<;%>"); |
a723baf1 MM |
16162 | } |
16163 | ||
16164 | /* Parse a pure-specifier. | |
16165 | ||
16166 | pure-specifier: | |
16167 | = 0 | |
16168 | ||
16169 | Returns INTEGER_ZERO_NODE if a pure specifier is found. | |
cd0be382 | 16170 | Otherwise, ERROR_MARK_NODE is returned. */ |
a723baf1 MM |
16171 | |
16172 | static tree | |
94edc4ab | 16173 | cp_parser_pure_specifier (cp_parser* parser) |
a723baf1 MM |
16174 | { |
16175 | cp_token *token; | |
16176 | ||
16177 | /* Look for the `=' token. */ | |
3a483371 | 16178 | if (!cp_parser_require (parser, CPP_EQ, "%<=%>")) |
a723baf1 MM |
16179 | return error_mark_node; |
16180 | /* Look for the `0' token. */ | |
def88a88 PC |
16181 | token = cp_lexer_peek_token (parser->lexer); |
16182 | ||
16183 | if (token->type == CPP_EOF | |
16184 | || token->type == CPP_PRAGMA_EOL) | |
16185 | return error_mark_node; | |
16186 | ||
16187 | cp_lexer_consume_token (parser->lexer); | |
b87d79e6 JM |
16188 | |
16189 | /* Accept = default or = delete in c++0x mode. */ | |
16190 | if (token->keyword == RID_DEFAULT | |
16191 | || token->keyword == RID_DELETE) | |
16192 | { | |
16193 | maybe_warn_cpp0x ("defaulted and deleted functions"); | |
16194 | return token->u.value; | |
16195 | } | |
16196 | ||
ab84748a | 16197 | /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */ |
ceacde63 MM |
16198 | if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO)) |
16199 | { | |
3db45ab5 | 16200 | cp_parser_error (parser, |
3a483371 | 16201 | "invalid pure specifier (only %<= 0%> is allowed)"); |
ceacde63 MM |
16202 | cp_parser_skip_to_end_of_statement (parser); |
16203 | return error_mark_node; | |
16204 | } | |
16205 | if (PROCESSING_REAL_TEMPLATE_DECL_P ()) | |
16206 | { | |
69bc6bff | 16207 | error_at (token->location, "templates may not be %<virtual%>"); |
ceacde63 MM |
16208 | return error_mark_node; |
16209 | } | |
a723baf1 | 16210 | |
ceacde63 | 16211 | return integer_zero_node; |
a723baf1 MM |
16212 | } |
16213 | ||
16214 | /* Parse a constant-initializer. | |
16215 | ||
16216 | constant-initializer: | |
16217 | = constant-expression | |
16218 | ||
16219 | Returns a representation of the constant-expression. */ | |
16220 | ||
16221 | static tree | |
94edc4ab | 16222 | cp_parser_constant_initializer (cp_parser* parser) |
a723baf1 MM |
16223 | { |
16224 | /* Look for the `=' token. */ | |
3a483371 | 16225 | if (!cp_parser_require (parser, CPP_EQ, "%<=%>")) |
a723baf1 MM |
16226 | return error_mark_node; |
16227 | ||
16228 | /* It is invalid to write: | |
16229 | ||
16230 | struct S { static const int i = { 7 }; }; | |
16231 | ||
16232 | */ | |
16233 | if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)) | |
16234 | { | |
16235 | cp_parser_error (parser, | |
16236 | "a brace-enclosed initializer is not allowed here"); | |
16237 | /* Consume the opening brace. */ | |
16238 | cp_lexer_consume_token (parser->lexer); | |
16239 | /* Skip the initializer. */ | |
16240 | cp_parser_skip_to_closing_brace (parser); | |
16241 | /* Look for the trailing `}'. */ | |
3a483371 | 16242 | cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>"); |
21526606 | 16243 | |
a723baf1 MM |
16244 | return error_mark_node; |
16245 | } | |
16246 | ||
21526606 | 16247 | return cp_parser_constant_expression (parser, |
14d22dd6 MM |
16248 | /*allow_non_constant=*/false, |
16249 | NULL); | |
a723baf1 MM |
16250 | } |
16251 | ||
16252 | /* Derived classes [gram.class.derived] */ | |
16253 | ||
16254 | /* Parse a base-clause. | |
16255 | ||
16256 | base-clause: | |
21526606 | 16257 | : base-specifier-list |
a723baf1 MM |
16258 | |
16259 | base-specifier-list: | |
5d80a306 DG |
16260 | base-specifier ... [opt] |
16261 | base-specifier-list , base-specifier ... [opt] | |
a723baf1 MM |
16262 | |
16263 | Returns a TREE_LIST representing the base-classes, in the order in | |
16264 | which they were declared. The representation of each node is as | |
21526606 | 16265 | described by cp_parser_base_specifier. |
a723baf1 MM |
16266 | |
16267 | In the case that no bases are specified, this function will return | |
16268 | NULL_TREE, not ERROR_MARK_NODE. */ | |
16269 | ||
16270 | static tree | |
94edc4ab | 16271 | cp_parser_base_clause (cp_parser* parser) |
a723baf1 MM |
16272 | { |
16273 | tree bases = NULL_TREE; | |
16274 | ||
16275 | /* Look for the `:' that begins the list. */ | |
3a483371 | 16276 | cp_parser_require (parser, CPP_COLON, "%<:%>"); |
a723baf1 MM |
16277 | |
16278 | /* Scan the base-specifier-list. */ | |
16279 | while (true) | |
16280 | { | |
16281 | cp_token *token; | |
16282 | tree base; | |
5d80a306 | 16283 | bool pack_expansion_p = false; |
a723baf1 MM |
16284 | |
16285 | /* Look for the base-specifier. */ | |
16286 | base = cp_parser_base_specifier (parser); | |
5d80a306 DG |
16287 | /* Look for the (optional) ellipsis. */ |
16288 | if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)) | |
16289 | { | |
16290 | /* Consume the `...'. */ | |
16291 | cp_lexer_consume_token (parser->lexer); | |
16292 | ||
16293 | pack_expansion_p = true; | |
16294 | } | |
16295 | ||
a723baf1 MM |
16296 | /* Add BASE to the front of the list. */ |
16297 | if (base != error_mark_node) | |
16298 | { | |
5d80a306 DG |
16299 | if (pack_expansion_p) |
16300 | /* Make this a pack expansion type. */ | |
16301 | TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base)); | |
4439d02f | 16302 | |
5d80a306 | 16303 | |
7b3e2d46 | 16304 | if (!check_for_bare_parameter_packs (TREE_VALUE (base))) |
4439d02f DG |
16305 | { |
16306 | TREE_CHAIN (base) = bases; | |
16307 | bases = base; | |
16308 | } | |
a723baf1 MM |
16309 | } |
16310 | /* Peek at the next token. */ | |
16311 | token = cp_lexer_peek_token (parser->lexer); | |
16312 | /* If it's not a comma, then the list is complete. */ | |
16313 | if (token->type != CPP_COMMA) | |
16314 | break; | |
16315 | /* Consume the `,'. */ | |
16316 | cp_lexer_consume_token (parser->lexer); | |
16317 | } | |
16318 | ||
16319 | /* PARSER->SCOPE may still be non-NULL at this point, if the last | |
16320 | base class had a qualified name. However, the next name that | |
16321 | appears is certainly not qualified. */ | |
16322 | parser->scope = NULL_TREE; | |
16323 | parser->qualifying_scope = NULL_TREE; | |
16324 | parser->object_scope = NULL_TREE; | |
16325 | ||
16326 | return nreverse (bases); | |
16327 | } | |
16328 | ||
16329 | /* Parse a base-specifier. | |
16330 | ||
16331 | base-specifier: | |
16332 | :: [opt] nested-name-specifier [opt] class-name | |
16333 | virtual access-specifier [opt] :: [opt] nested-name-specifier | |
16334 | [opt] class-name | |
16335 | access-specifier virtual [opt] :: [opt] nested-name-specifier | |
16336 | [opt] class-name | |
16337 | ||
16338 | Returns a TREE_LIST. The TREE_PURPOSE will be one of | |
16339 | ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to | |
16340 | indicate the specifiers provided. The TREE_VALUE will be a TYPE | |
16341 | (or the ERROR_MARK_NODE) indicating the type that was specified. */ | |
21526606 | 16342 | |
a723baf1 | 16343 | static tree |
94edc4ab | 16344 | cp_parser_base_specifier (cp_parser* parser) |
a723baf1 MM |
16345 | { |
16346 | cp_token *token; | |
16347 | bool done = false; | |
16348 | bool virtual_p = false; | |
16349 | bool duplicate_virtual_error_issued_p = false; | |
16350 | bool duplicate_access_error_issued_p = false; | |
bbaab916 | 16351 | bool class_scope_p, template_p; |
dbbf88d1 | 16352 | tree access = access_default_node; |
a723baf1 MM |
16353 | tree type; |
16354 | ||
16355 | /* Process the optional `virtual' and `access-specifier'. */ | |
16356 | while (!done) | |
16357 | { | |
16358 | /* Peek at the next token. */ | |
16359 | token = cp_lexer_peek_token (parser->lexer); | |
16360 | /* Process `virtual'. */ | |
16361 | switch (token->keyword) | |
16362 | { | |
16363 | case RID_VIRTUAL: | |
16364 | /* If `virtual' appears more than once, issue an error. */ | |
16365 | if (virtual_p && !duplicate_virtual_error_issued_p) | |
16366 | { | |
16367 | cp_parser_error (parser, | |
2a13a625 | 16368 | "%<virtual%> specified more than once in base-specified"); |
a723baf1 MM |
16369 | duplicate_virtual_error_issued_p = true; |
16370 | } | |
16371 | ||
16372 | virtual_p = true; | |
16373 | ||
16374 | /* Consume the `virtual' token. */ | |
16375 | cp_lexer_consume_token (parser->lexer); | |
16376 | ||
16377 | break; | |
16378 | ||
16379 | case RID_PUBLIC: | |
16380 | case RID_PROTECTED: | |
16381 | case RID_PRIVATE: | |
16382 | /* If more than one access specifier appears, issue an | |
16383 | error. */ | |
dbbf88d1 NS |
16384 | if (access != access_default_node |
16385 | && !duplicate_access_error_issued_p) | |
a723baf1 MM |
16386 | { |
16387 | cp_parser_error (parser, | |
16388 | "more than one access specifier in base-specified"); | |
16389 | duplicate_access_error_issued_p = true; | |
16390 | } | |
16391 | ||
dbbf88d1 | 16392 | access = ridpointers[(int) token->keyword]; |
a723baf1 MM |
16393 | |
16394 | /* Consume the access-specifier. */ | |
16395 | cp_lexer_consume_token (parser->lexer); | |
16396 | ||
16397 | break; | |
16398 | ||
16399 | default: | |
16400 | done = true; | |
16401 | break; | |
16402 | } | |
16403 | } | |
852dcbdd | 16404 | /* It is not uncommon to see programs mechanically, erroneously, use |
a3a503a5 | 16405 | the 'typename' keyword to denote (dependent) qualified types |
1ed53ef3 GB |
16406 | as base classes. */ |
16407 | if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME)) | |
16408 | { | |
2b7a3abf | 16409 | token = cp_lexer_peek_token (parser->lexer); |
1ed53ef3 | 16410 | if (!processing_template_decl) |
69bc6bff MLI |
16411 | error_at (token->location, |
16412 | "keyword %<typename%> not allowed outside of templates"); | |
1ed53ef3 | 16413 | else |
69bc6bff MLI |
16414 | error_at (token->location, |
16415 | "keyword %<typename%> not allowed in this context " | |
16416 | "(the base class is implicitly a type)"); | |
1ed53ef3 GB |
16417 | cp_lexer_consume_token (parser->lexer); |
16418 | } | |
a723baf1 | 16419 | |
a723baf1 | 16420 | /* Look for the optional `::' operator. */ |
88525e1b | 16421 | cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false); |
a723baf1 MM |
16422 | /* Look for the nested-name-specifier. The simplest way to |
16423 | implement: | |
16424 | ||
16425 | [temp.res] | |
16426 | ||
16427 | The keyword `typename' is not permitted in a base-specifier or | |
16428 | mem-initializer; in these contexts a qualified name that | |
16429 | depends on a template-parameter is implicitly assumed to be a | |
16430 | type name. | |
16431 | ||
16432 | is to pretend that we have seen the `typename' keyword at this | |
21526606 | 16433 | point. */ |
a723baf1 MM |
16434 | cp_parser_nested_name_specifier_opt (parser, |
16435 | /*typename_keyword_p=*/true, | |
16436 | /*check_dependency_p=*/true, | |
fc6a28d7 | 16437 | typename_type, |
a668c6ad | 16438 | /*is_declaration=*/true); |
a723baf1 MM |
16439 | /* If the base class is given by a qualified name, assume that names |
16440 | we see are type names or templates, as appropriate. */ | |
16441 | class_scope_p = (parser->scope && TYPE_P (parser->scope)); | |
bbaab916 | 16442 | template_p = class_scope_p && cp_parser_optional_template_keyword (parser); |
21526606 | 16443 | |
a723baf1 | 16444 | /* Finally, look for the class-name. */ |
21526606 | 16445 | type = cp_parser_class_name (parser, |
a723baf1 | 16446 | class_scope_p, |
bbaab916 | 16447 | template_p, |
fc6a28d7 | 16448 | typename_type, |
a723baf1 | 16449 | /*check_dependency_p=*/true, |
a668c6ad MM |
16450 | /*class_head_p=*/false, |
16451 | /*is_declaration=*/true); | |
a723baf1 MM |
16452 | |
16453 | if (type == error_mark_node) | |
16454 | return error_mark_node; | |
16455 | ||
dbbf88d1 | 16456 | return finish_base_specifier (TREE_TYPE (type), access, virtual_p); |
a723baf1 MM |
16457 | } |
16458 | ||
16459 | /* Exception handling [gram.exception] */ | |
16460 | ||
16461 | /* Parse an (optional) exception-specification. | |
16462 | ||
16463 | exception-specification: | |
16464 | throw ( type-id-list [opt] ) | |
16465 | ||
16466 | Returns a TREE_LIST representing the exception-specification. The | |
16467 | TREE_VALUE of each node is a type. */ | |
16468 | ||
16469 | static tree | |
94edc4ab | 16470 | cp_parser_exception_specification_opt (cp_parser* parser) |
a723baf1 MM |
16471 | { |
16472 | cp_token *token; | |
16473 | tree type_id_list; | |
16474 | ||
16475 | /* Peek at the next token. */ | |
16476 | token = cp_lexer_peek_token (parser->lexer); | |
16477 | /* If it's not `throw', then there's no exception-specification. */ | |
16478 | if (!cp_parser_is_keyword (token, RID_THROW)) | |
16479 | return NULL_TREE; | |
16480 | ||
16481 | /* Consume the `throw'. */ | |
16482 | cp_lexer_consume_token (parser->lexer); | |
16483 | ||
16484 | /* Look for the `('. */ | |
3a483371 | 16485 | cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"); |
a723baf1 MM |
16486 | |
16487 | /* Peek at the next token. */ | |
16488 | token = cp_lexer_peek_token (parser->lexer); | |
16489 | /* If it's not a `)', then there is a type-id-list. */ | |
16490 | if (token->type != CPP_CLOSE_PAREN) | |
16491 | { | |
16492 | const char *saved_message; | |
16493 | ||
16494 | /* Types may not be defined in an exception-specification. */ | |
16495 | saved_message = parser->type_definition_forbidden_message; | |
16496 | parser->type_definition_forbidden_message | |
16497 | = "types may not be defined in an exception-specification"; | |
16498 | /* Parse the type-id-list. */ | |
16499 | type_id_list = cp_parser_type_id_list (parser); | |
16500 | /* Restore the saved message. */ | |
16501 | parser->type_definition_forbidden_message = saved_message; | |
16502 | } | |
16503 | else | |
16504 | type_id_list = empty_except_spec; | |
16505 | ||
16506 | /* Look for the `)'. */ | |
3a483371 | 16507 | cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"); |
a723baf1 MM |
16508 | |
16509 | return type_id_list; | |
16510 | } | |
16511 | ||
16512 | /* Parse an (optional) type-id-list. | |
16513 | ||
16514 | type-id-list: | |
5d80a306 DG |
16515 | type-id ... [opt] |
16516 | type-id-list , type-id ... [opt] | |
a723baf1 MM |
16517 | |
16518 | Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE, | |
16519 | in the order that the types were presented. */ | |
16520 | ||
16521 | static tree | |
94edc4ab | 16522 | cp_parser_type_id_list (cp_parser* parser) |
a723baf1 MM |
16523 | { |
16524 | tree types = NULL_TREE; | |
16525 | ||
16526 | while (true) | |
16527 | { | |
16528 | cp_token *token; | |
16529 | tree type; | |
16530 | ||
16531 | /* Get the next type-id. */ | |
16532 | type = cp_parser_type_id (parser); | |
5d80a306 DG |
16533 | /* Parse the optional ellipsis. */ |
16534 | if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)) | |
16535 | { | |
16536 | /* Consume the `...'. */ | |
16537 | cp_lexer_consume_token (parser->lexer); | |
16538 | ||
16539 | /* Turn the type into a pack expansion expression. */ | |
16540 | type = make_pack_expansion (type); | |
16541 | } | |
a723baf1 MM |
16542 | /* Add it to the list. */ |
16543 | types = add_exception_specifier (types, type, /*complain=*/1); | |
16544 | /* Peek at the next token. */ | |
16545 | token = cp_lexer_peek_token (parser->lexer); | |
16546 | /* If it is not a `,', we are done. */ | |
16547 | if (token->type != CPP_COMMA) | |
16548 | break; | |
16549 | /* Consume the `,'. */ | |
16550 | cp_lexer_consume_token (parser->lexer); | |
16551 | } | |
16552 | ||
16553 | return nreverse (types); | |
16554 | } | |
16555 | ||
16556 | /* Parse a try-block. | |
16557 | ||
16558 | try-block: | |
16559 | try compound-statement handler-seq */ | |
16560 | ||
16561 | static tree | |
94edc4ab | 16562 | cp_parser_try_block (cp_parser* parser) |
a723baf1 MM |
16563 | { |
16564 | tree try_block; | |
16565 | ||
3a483371 | 16566 | cp_parser_require_keyword (parser, RID_TRY, "%<try%>"); |
a723baf1 | 16567 | try_block = begin_try_block (); |
325c3691 | 16568 | cp_parser_compound_statement (parser, NULL, true); |
a723baf1 MM |
16569 | finish_try_block (try_block); |
16570 | cp_parser_handler_seq (parser); | |
16571 | finish_handler_sequence (try_block); | |
16572 | ||
16573 | return try_block; | |
16574 | } | |
16575 | ||
16576 | /* Parse a function-try-block. | |
16577 | ||
16578 | function-try-block: | |
16579 | try ctor-initializer [opt] function-body handler-seq */ | |
16580 | ||
16581 | static bool | |
94edc4ab | 16582 | cp_parser_function_try_block (cp_parser* parser) |
a723baf1 | 16583 | { |
eaf6fb90 | 16584 | tree compound_stmt; |
a723baf1 MM |
16585 | tree try_block; |
16586 | bool ctor_initializer_p; | |
16587 | ||
16588 | /* Look for the `try' keyword. */ | |
3a483371 | 16589 | if (!cp_parser_require_keyword (parser, RID_TRY, "%<try%>")) |
a723baf1 | 16590 | return false; |
3b426391 | 16591 | /* Let the rest of the front end know where we are. */ |
eaf6fb90 | 16592 | try_block = begin_function_try_block (&compound_stmt); |
a723baf1 | 16593 | /* Parse the function-body. */ |
21526606 | 16594 | ctor_initializer_p |
a723baf1 MM |
16595 | = cp_parser_ctor_initializer_opt_and_function_body (parser); |
16596 | /* We're done with the `try' part. */ | |
16597 | finish_function_try_block (try_block); | |
16598 | /* Parse the handlers. */ | |
16599 | cp_parser_handler_seq (parser); | |
16600 | /* We're done with the handlers. */ | |
eaf6fb90 | 16601 | finish_function_handler_sequence (try_block, compound_stmt); |
a723baf1 MM |
16602 | |
16603 | return ctor_initializer_p; | |
16604 | } | |
16605 | ||
16606 | /* Parse a handler-seq. | |
16607 | ||
16608 | handler-seq: | |
16609 | handler handler-seq [opt] */ | |
16610 | ||
16611 | static void | |
94edc4ab | 16612 | cp_parser_handler_seq (cp_parser* parser) |
a723baf1 MM |
16613 | { |
16614 | while (true) | |
16615 | { | |
16616 | cp_token *token; | |
16617 | ||
16618 | /* Parse the handler. */ | |
16619 | cp_parser_handler (parser); | |
16620 | /* Peek at the next token. */ | |
16621 | token = cp_lexer_peek_token (parser->lexer); | |
16622 | /* If it's not `catch' then there are no more handlers. */ | |
16623 | if (!cp_parser_is_keyword (token, RID_CATCH)) | |
16624 | break; | |
16625 | } | |
16626 | } | |
16627 | ||
16628 | /* Parse a handler. | |
16629 | ||
16630 | handler: | |
16631 | catch ( exception-declaration ) compound-statement */ | |
16632 | ||
16633 | static void | |
94edc4ab | 16634 | cp_parser_handler (cp_parser* parser) |
a723baf1 MM |
16635 | { |
16636 | tree handler; | |
16637 | tree declaration; | |
16638 | ||
3a483371 | 16639 | cp_parser_require_keyword (parser, RID_CATCH, "%<catch%>"); |
a723baf1 | 16640 | handler = begin_handler (); |
3a483371 | 16641 | cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"); |
a723baf1 MM |
16642 | declaration = cp_parser_exception_declaration (parser); |
16643 | finish_handler_parms (declaration, handler); | |
3a483371 | 16644 | cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"); |
325c3691 | 16645 | cp_parser_compound_statement (parser, NULL, false); |
a723baf1 MM |
16646 | finish_handler (handler); |
16647 | } | |
16648 | ||
16649 | /* Parse an exception-declaration. | |
16650 | ||
16651 | exception-declaration: | |
16652 | type-specifier-seq declarator | |
16653 | type-specifier-seq abstract-declarator | |
16654 | type-specifier-seq | |
21526606 | 16655 | ... |
a723baf1 MM |
16656 | |
16657 | Returns a VAR_DECL for the declaration, or NULL_TREE if the | |
16658 | ellipsis variant is used. */ | |
16659 | ||
16660 | static tree | |
94edc4ab | 16661 | cp_parser_exception_declaration (cp_parser* parser) |
a723baf1 | 16662 | { |
62d1db17 | 16663 | cp_decl_specifier_seq type_specifiers; |
058b15c1 | 16664 | cp_declarator *declarator; |
a723baf1 MM |
16665 | const char *saved_message; |
16666 | ||
16667 | /* If it's an ellipsis, it's easy to handle. */ | |
16668 | if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)) | |
16669 | { | |
16670 | /* Consume the `...' token. */ | |
16671 | cp_lexer_consume_token (parser->lexer); | |
16672 | return NULL_TREE; | |
16673 | } | |
16674 | ||
16675 | /* Types may not be defined in exception-declarations. */ | |
16676 | saved_message = parser->type_definition_forbidden_message; | |
16677 | parser->type_definition_forbidden_message | |
16678 | = "types may not be defined in exception-declarations"; | |
16679 | ||
16680 | /* Parse the type-specifier-seq. */ | |
d4113656 MM |
16681 | cp_parser_type_specifier_seq (parser, /*is_condition=*/false, |
16682 | &type_specifiers); | |
a723baf1 MM |
16683 | /* If it's a `)', then there is no declarator. */ |
16684 | if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)) | |
058b15c1 | 16685 | declarator = NULL; |
a723baf1 | 16686 | else |
62b8a44e | 16687 | declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER, |
4bb8ca28 | 16688 | /*ctor_dtor_or_conv_p=*/NULL, |
db86dd14 MM |
16689 | /*parenthesized_p=*/NULL, |
16690 | /*member_p=*/false); | |
a723baf1 MM |
16691 | |
16692 | /* Restore the saved message. */ | |
16693 | parser->type_definition_forbidden_message = saved_message; | |
16694 | ||
2a50edcd VR |
16695 | if (!type_specifiers.any_specifiers_p) |
16696 | return error_mark_node; | |
058b15c1 | 16697 | |
2a50edcd | 16698 | return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL); |
a723baf1 MM |
16699 | } |
16700 | ||
21526606 | 16701 | /* Parse a throw-expression. |
a723baf1 MM |
16702 | |
16703 | throw-expression: | |
34cd5ae7 | 16704 | throw assignment-expression [opt] |
a723baf1 MM |
16705 | |
16706 | Returns a THROW_EXPR representing the throw-expression. */ | |
16707 | ||
16708 | static tree | |
94edc4ab | 16709 | cp_parser_throw_expression (cp_parser* parser) |
a723baf1 MM |
16710 | { |
16711 | tree expression; | |
89f1a6ec | 16712 | cp_token* token; |
a723baf1 | 16713 | |
3a483371 | 16714 | cp_parser_require_keyword (parser, RID_THROW, "%<throw%>"); |
89f1a6ec MM |
16715 | token = cp_lexer_peek_token (parser->lexer); |
16716 | /* Figure out whether or not there is an assignment-expression | |
16717 | following the "throw" keyword. */ | |
16718 | if (token->type == CPP_COMMA | |
16719 | || token->type == CPP_SEMICOLON | |
16720 | || token->type == CPP_CLOSE_PAREN | |
16721 | || token->type == CPP_CLOSE_SQUARE | |
16722 | || token->type == CPP_CLOSE_BRACE | |
16723 | || token->type == CPP_COLON) | |
a723baf1 | 16724 | expression = NULL_TREE; |
89f1a6ec | 16725 | else |
93678513 | 16726 | expression = cp_parser_assignment_expression (parser, |
cee9035d | 16727 | /*cast_p=*/false, NULL); |
a723baf1 MM |
16728 | |
16729 | return build_throw (expression); | |
16730 | } | |
16731 | ||
16732 | /* GNU Extensions */ | |
16733 | ||
16734 | /* Parse an (optional) asm-specification. | |
16735 | ||
16736 | asm-specification: | |
16737 | asm ( string-literal ) | |
16738 | ||
16739 | If the asm-specification is present, returns a STRING_CST | |
16740 | corresponding to the string-literal. Otherwise, returns | |
16741 | NULL_TREE. */ | |
16742 | ||
16743 | static tree | |
94edc4ab | 16744 | cp_parser_asm_specification_opt (cp_parser* parser) |
a723baf1 MM |
16745 | { |
16746 | cp_token *token; | |
16747 | tree asm_specification; | |
16748 | ||
16749 | /* Peek at the next token. */ | |
16750 | token = cp_lexer_peek_token (parser->lexer); | |
21526606 | 16751 | /* If the next token isn't the `asm' keyword, then there's no |
a723baf1 MM |
16752 | asm-specification. */ |
16753 | if (!cp_parser_is_keyword (token, RID_ASM)) | |
16754 | return NULL_TREE; | |
16755 | ||
16756 | /* Consume the `asm' token. */ | |
16757 | cp_lexer_consume_token (parser->lexer); | |
16758 | /* Look for the `('. */ | |
3a483371 | 16759 | cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"); |
a723baf1 MM |
16760 | |
16761 | /* Look for the string-literal. */ | |
c162c75e | 16762 | asm_specification = cp_parser_string_literal (parser, false, false); |
a723baf1 MM |
16763 | |
16764 | /* Look for the `)'. */ | |
3a483371 | 16765 | cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"); |
a723baf1 MM |
16766 | |
16767 | return asm_specification; | |
16768 | } | |
16769 | ||
21526606 | 16770 | /* Parse an asm-operand-list. |
a723baf1 MM |
16771 | |
16772 | asm-operand-list: | |
16773 | asm-operand | |
16774 | asm-operand-list , asm-operand | |
21526606 | 16775 | |
a723baf1 | 16776 | asm-operand: |
21526606 | 16777 | string-literal ( expression ) |
a723baf1 MM |
16778 | [ string-literal ] string-literal ( expression ) |
16779 | ||
16780 | Returns a TREE_LIST representing the operands. The TREE_VALUE of | |
16781 | each node is the expression. The TREE_PURPOSE is itself a | |
16782 | TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed | |
16783 | string-literal (or NULL_TREE if not present) and whose TREE_VALUE | |
f8ff69ea LM |
16784 | is a STRING_CST for the string literal before the parenthesis. Returns |
16785 | ERROR_MARK_NODE if any of the operands are invalid. */ | |
a723baf1 MM |
16786 | |
16787 | static tree | |
94edc4ab | 16788 | cp_parser_asm_operand_list (cp_parser* parser) |
a723baf1 MM |
16789 | { |
16790 | tree asm_operands = NULL_TREE; | |
f8ff69ea | 16791 | bool invalid_operands = false; |
a723baf1 MM |
16792 | |
16793 | while (true) | |
16794 | { | |
16795 | tree string_literal; | |
16796 | tree expression; | |
16797 | tree name; | |
21526606 | 16798 | |
21526606 | 16799 | if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE)) |
a723baf1 MM |
16800 | { |
16801 | /* Consume the `[' token. */ | |
16802 | cp_lexer_consume_token (parser->lexer); | |
16803 | /* Read the operand name. */ | |
16804 | name = cp_parser_identifier (parser); | |
21526606 | 16805 | if (name != error_mark_node) |
a723baf1 MM |
16806 | name = build_string (IDENTIFIER_LENGTH (name), |
16807 | IDENTIFIER_POINTER (name)); | |
16808 | /* Look for the closing `]'. */ | |
3a483371 | 16809 | cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>"); |
a723baf1 MM |
16810 | } |
16811 | else | |
16812 | name = NULL_TREE; | |
16813 | /* Look for the string-literal. */ | |
c162c75e MA |
16814 | string_literal = cp_parser_string_literal (parser, false, false); |
16815 | ||
a723baf1 | 16816 | /* Look for the `('. */ |
3a483371 | 16817 | cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"); |
a723baf1 | 16818 | /* Parse the expression. */ |
cee9035d | 16819 | expression = cp_parser_expression (parser, /*cast_p=*/false, NULL); |
a723baf1 | 16820 | /* Look for the `)'. */ |
3a483371 | 16821 | cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"); |
c162c75e | 16822 | |
f8ff69ea LM |
16823 | if (name == error_mark_node |
16824 | || string_literal == error_mark_node | |
16825 | || expression == error_mark_node) | |
16826 | invalid_operands = true; | |
16827 | ||
a723baf1 MM |
16828 | /* Add this operand to the list. */ |
16829 | asm_operands = tree_cons (build_tree_list (name, string_literal), | |
21526606 | 16830 | expression, |
a723baf1 | 16831 | asm_operands); |
21526606 | 16832 | /* If the next token is not a `,', there are no more |
a723baf1 MM |
16833 | operands. */ |
16834 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)) | |
16835 | break; | |
16836 | /* Consume the `,'. */ | |
16837 | cp_lexer_consume_token (parser->lexer); | |
16838 | } | |
16839 | ||
f8ff69ea | 16840 | return invalid_operands ? error_mark_node : nreverse (asm_operands); |
a723baf1 MM |
16841 | } |
16842 | ||
21526606 | 16843 | /* Parse an asm-clobber-list. |
a723baf1 MM |
16844 | |
16845 | asm-clobber-list: | |
16846 | string-literal | |
21526606 | 16847 | asm-clobber-list , string-literal |
a723baf1 MM |
16848 | |
16849 | Returns a TREE_LIST, indicating the clobbers in the order that they | |
16850 | appeared. The TREE_VALUE of each node is a STRING_CST. */ | |
16851 | ||
16852 | static tree | |
94edc4ab | 16853 | cp_parser_asm_clobber_list (cp_parser* parser) |
a723baf1 MM |
16854 | { |
16855 | tree clobbers = NULL_TREE; | |
16856 | ||
16857 | while (true) | |
16858 | { | |
a723baf1 MM |
16859 | tree string_literal; |
16860 | ||
16861 | /* Look for the string literal. */ | |
c162c75e | 16862 | string_literal = cp_parser_string_literal (parser, false, false); |
a723baf1 MM |
16863 | /* Add it to the list. */ |
16864 | clobbers = tree_cons (NULL_TREE, string_literal, clobbers); | |
21526606 | 16865 | /* If the next token is not a `,', then the list is |
a723baf1 MM |
16866 | complete. */ |
16867 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)) | |
16868 | break; | |
16869 | /* Consume the `,' token. */ | |
16870 | cp_lexer_consume_token (parser->lexer); | |
16871 | } | |
16872 | ||
16873 | return clobbers; | |
16874 | } | |
16875 | ||
16876 | /* Parse an (optional) series of attributes. | |
16877 | ||
16878 | attributes: | |
16879 | attributes attribute | |
16880 | ||
16881 | attribute: | |
21526606 | 16882 | __attribute__ (( attribute-list [opt] )) |
a723baf1 MM |
16883 | |
16884 | The return value is as for cp_parser_attribute_list. */ | |
21526606 | 16885 | |
a723baf1 | 16886 | static tree |
94edc4ab | 16887 | cp_parser_attributes_opt (cp_parser* parser) |
a723baf1 MM |
16888 | { |
16889 | tree attributes = NULL_TREE; | |
16890 | ||
16891 | while (true) | |
16892 | { | |
16893 | cp_token *token; | |
16894 | tree attribute_list; | |
16895 | ||
16896 | /* Peek at the next token. */ | |
16897 | token = cp_lexer_peek_token (parser->lexer); | |
16898 | /* If it's not `__attribute__', then we're done. */ | |
16899 | if (token->keyword != RID_ATTRIBUTE) | |
16900 | break; | |
16901 | ||
16902 | /* Consume the `__attribute__' keyword. */ | |
16903 | cp_lexer_consume_token (parser->lexer); | |
16904 | /* Look for the two `(' tokens. */ | |
3a483371 VR |
16905 | cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"); |
16906 | cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"); | |
a723baf1 MM |
16907 | |
16908 | /* Peek at the next token. */ | |
16909 | token = cp_lexer_peek_token (parser->lexer); | |
16910 | if (token->type != CPP_CLOSE_PAREN) | |
16911 | /* Parse the attribute-list. */ | |
16912 | attribute_list = cp_parser_attribute_list (parser); | |
16913 | else | |
16914 | /* If the next token is a `)', then there is no attribute | |
16915 | list. */ | |
16916 | attribute_list = NULL; | |
16917 | ||
16918 | /* Look for the two `)' tokens. */ | |
3a483371 VR |
16919 | cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"); |
16920 | cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"); | |
a723baf1 MM |
16921 | |
16922 | /* Add these new attributes to the list. */ | |
16923 | attributes = chainon (attributes, attribute_list); | |
16924 | } | |
16925 | ||
16926 | return attributes; | |
16927 | } | |
16928 | ||
21526606 | 16929 | /* Parse an attribute-list. |
a723baf1 | 16930 | |
21526606 EC |
16931 | attribute-list: |
16932 | attribute | |
a723baf1 MM |
16933 | attribute-list , attribute |
16934 | ||
16935 | attribute: | |
21526606 | 16936 | identifier |
a723baf1 MM |
16937 | identifier ( identifier ) |
16938 | identifier ( identifier , expression-list ) | |
21526606 | 16939 | identifier ( expression-list ) |
a723baf1 | 16940 | |
88e95ee3 MM |
16941 | Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds |
16942 | to an attribute. The TREE_PURPOSE of each node is the identifier | |
16943 | indicating which attribute is in use. The TREE_VALUE represents | |
16944 | the arguments, if any. */ | |
a723baf1 MM |
16945 | |
16946 | static tree | |
94edc4ab | 16947 | cp_parser_attribute_list (cp_parser* parser) |
a723baf1 MM |
16948 | { |
16949 | tree attribute_list = NULL_TREE; | |
c162c75e | 16950 | bool save_translate_strings_p = parser->translate_strings_p; |
a723baf1 | 16951 | |
c162c75e | 16952 | parser->translate_strings_p = false; |
a723baf1 MM |
16953 | while (true) |
16954 | { | |
16955 | cp_token *token; | |
16956 | tree identifier; | |
16957 | tree attribute; | |
16958 | ||
16959 | /* Look for the identifier. We also allow keywords here; for | |
16960 | example `__attribute__ ((const))' is legal. */ | |
16961 | token = cp_lexer_peek_token (parser->lexer); | |
88e95ee3 MM |
16962 | if (token->type == CPP_NAME |
16963 | || token->type == CPP_KEYWORD) | |
16964 | { | |
0b9cb8c2 VR |
16965 | tree arguments = NULL_TREE; |
16966 | ||
88e95ee3 MM |
16967 | /* Consume the token. */ |
16968 | token = cp_lexer_consume_token (parser->lexer); | |
21526606 | 16969 | |
88e95ee3 | 16970 | /* Save away the identifier that indicates which attribute |
c8094d83 | 16971 | this is. */ |
67beaaa6 MLI |
16972 | identifier = (token->type == CPP_KEYWORD) |
16973 | /* For keywords, use the canonical spelling, not the | |
16974 | parsed identifier. */ | |
16975 | ? ridpointers[(int) token->keyword] | |
16976 | : token->u.value; | |
16977 | ||
88e95ee3 | 16978 | attribute = build_tree_list (identifier, NULL_TREE); |
a723baf1 | 16979 | |
88e95ee3 MM |
16980 | /* Peek at the next token. */ |
16981 | token = cp_lexer_peek_token (parser->lexer); | |
16982 | /* If it's an `(', then parse the attribute arguments. */ | |
16983 | if (token->type == CPP_OPEN_PAREN) | |
16984 | { | |
c166b898 ILT |
16985 | VEC(tree,gc) *vec; |
16986 | vec = cp_parser_parenthesized_expression_list | |
16987 | (parser, true, /*cast_p=*/false, | |
16988 | /*allow_expansion_p=*/false, | |
16989 | /*non_constant_p=*/NULL); | |
16990 | if (vec == NULL) | |
16991 | arguments = error_mark_node; | |
16992 | else | |
16993 | { | |
16994 | arguments = build_tree_list_vec (vec); | |
16995 | release_tree_vector (vec); | |
16996 | } | |
0b9cb8c2 | 16997 | /* Save the arguments away. */ |
88e95ee3 MM |
16998 | TREE_VALUE (attribute) = arguments; |
16999 | } | |
a723baf1 | 17000 | |
0b9cb8c2 VR |
17001 | if (arguments != error_mark_node) |
17002 | { | |
17003 | /* Add this attribute to the list. */ | |
17004 | TREE_CHAIN (attribute) = attribute_list; | |
17005 | attribute_list = attribute; | |
17006 | } | |
a723baf1 | 17007 | |
88e95ee3 MM |
17008 | token = cp_lexer_peek_token (parser->lexer); |
17009 | } | |
17010 | /* Now, look for more attributes. If the next token isn't a | |
17011 | `,', we're done. */ | |
a723baf1 MM |
17012 | if (token->type != CPP_COMMA) |
17013 | break; | |
17014 | ||
cd0be382 | 17015 | /* Consume the comma and keep going. */ |
a723baf1 MM |
17016 | cp_lexer_consume_token (parser->lexer); |
17017 | } | |
c162c75e | 17018 | parser->translate_strings_p = save_translate_strings_p; |
a723baf1 MM |
17019 | |
17020 | /* We built up the list in reverse order. */ | |
17021 | return nreverse (attribute_list); | |
17022 | } | |
17023 | ||
17024 | /* Parse an optional `__extension__' keyword. Returns TRUE if it is | |
17025 | present, and FALSE otherwise. *SAVED_PEDANTIC is set to the | |
17026 | current value of the PEDANTIC flag, regardless of whether or not | |
17027 | the `__extension__' keyword is present. The caller is responsible | |
17028 | for restoring the value of the PEDANTIC flag. */ | |
17029 | ||
17030 | static bool | |
94edc4ab | 17031 | cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic) |
a723baf1 MM |
17032 | { |
17033 | /* Save the old value of the PEDANTIC flag. */ | |
17034 | *saved_pedantic = pedantic; | |
17035 | ||
17036 | if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION)) | |
17037 | { | |
17038 | /* Consume the `__extension__' token. */ | |
17039 | cp_lexer_consume_token (parser->lexer); | |
17040 | /* We're not being pedantic while the `__extension__' keyword is | |
17041 | in effect. */ | |
17042 | pedantic = 0; | |
17043 | ||
17044 | return true; | |
17045 | } | |
17046 | ||
17047 | return false; | |
17048 | } | |
17049 | ||
17050 | /* Parse a label declaration. | |
17051 | ||
17052 | label-declaration: | |
17053 | __label__ label-declarator-seq ; | |
17054 | ||
17055 | label-declarator-seq: | |
17056 | identifier , label-declarator-seq | |
17057 | identifier */ | |
17058 | ||
17059 | static void | |
94edc4ab | 17060 | cp_parser_label_declaration (cp_parser* parser) |
a723baf1 MM |
17061 | { |
17062 | /* Look for the `__label__' keyword. */ | |
3a483371 | 17063 | cp_parser_require_keyword (parser, RID_LABEL, "%<__label__%>"); |
a723baf1 MM |
17064 | |
17065 | while (true) | |
17066 | { | |
17067 | tree identifier; | |
17068 | ||
17069 | /* Look for an identifier. */ | |
17070 | identifier = cp_parser_identifier (parser); | |
cb6d4a9f VR |
17071 | /* If we failed, stop. */ |
17072 | if (identifier == error_mark_node) | |
17073 | break; | |
17074 | /* Declare it as a label. */ | |
a723baf1 MM |
17075 | finish_label_decl (identifier); |
17076 | /* If the next token is a `;', stop. */ | |
17077 | if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)) | |
17078 | break; | |
17079 | /* Look for the `,' separating the label declarations. */ | |
3a483371 | 17080 | cp_parser_require (parser, CPP_COMMA, "%<,%>"); |
a723baf1 MM |
17081 | } |
17082 | ||
17083 | /* Look for the final `;'. */ | |
3a483371 | 17084 | cp_parser_require (parser, CPP_SEMICOLON, "%<;%>"); |
a723baf1 MM |
17085 | } |
17086 | ||
17087 | /* Support Functions */ | |
17088 | ||
17089 | /* Looks up NAME in the current scope, as given by PARSER->SCOPE. | |
17090 | NAME should have one of the representations used for an | |
17091 | id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE | |
17092 | is returned. If PARSER->SCOPE is a dependent type, then a | |
17093 | SCOPE_REF is returned. | |
17094 | ||
17095 | If NAME is a TEMPLATE_ID_EXPR, then it will be immediately | |
17096 | returned; the name was already resolved when the TEMPLATE_ID_EXPR | |
17097 | was formed. Abstractly, such entities should not be passed to this | |
17098 | function, because they do not need to be looked up, but it is | |
17099 | simpler to check for this special case here, rather than at the | |
17100 | call-sites. | |
17101 | ||
17102 | In cases not explicitly covered above, this function returns a | |
17103 | DECL, OVERLOAD, or baselink representing the result of the lookup. | |
17104 | If there was no entity with the indicated NAME, the ERROR_MARK_NODE | |
17105 | is returned. | |
17106 | ||
472c29c3 | 17107 | If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword |
fc6a28d7 MM |
17108 | (e.g., "struct") that was used. In that case bindings that do not |
17109 | refer to types are ignored. | |
a723baf1 | 17110 | |
b0bc6e8e KL |
17111 | If IS_TEMPLATE is TRUE, bindings that do not refer to templates are |
17112 | ignored. | |
17113 | ||
eea9800f MM |
17114 | If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces |
17115 | are ignored. | |
17116 | ||
a723baf1 | 17117 | If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent |
c8094d83 | 17118 | types. |
8f78f01f | 17119 | |
91b1ca65 | 17120 | If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a |
bcf51da2 | 17121 | TREE_LIST of candidates if name-lookup results in an ambiguity, and |
3db45ab5 | 17122 | NULL_TREE otherwise. */ |
a723baf1 MM |
17123 | |
17124 | static tree | |
21526606 | 17125 | cp_parser_lookup_name (cp_parser *parser, tree name, |
fc6a28d7 | 17126 | enum tag_types tag_type, |
3db45ab5 | 17127 | bool is_template, |
02ed62dd | 17128 | bool is_namespace, |
8f78f01f | 17129 | bool check_dependency, |
2b7a3abf DS |
17130 | tree *ambiguous_decls, |
17131 | location_t name_location) | |
a723baf1 | 17132 | { |
ef07d61b | 17133 | int flags = 0; |
a723baf1 MM |
17134 | tree decl; |
17135 | tree object_type = parser->context->object_type; | |
17136 | ||
ef07d61b VR |
17137 | if (!cp_parser_uncommitted_to_tentative_parse_p (parser)) |
17138 | flags |= LOOKUP_COMPLAIN; | |
17139 | ||
8f78f01f | 17140 | /* Assume that the lookup will be unambiguous. */ |
91b1ca65 MM |
17141 | if (ambiguous_decls) |
17142 | *ambiguous_decls = NULL_TREE; | |
8f78f01f | 17143 | |
a723baf1 MM |
17144 | /* Now that we have looked up the name, the OBJECT_TYPE (if any) is |
17145 | no longer valid. Note that if we are parsing tentatively, and | |
17146 | the parse fails, OBJECT_TYPE will be automatically restored. */ | |
17147 | parser->context->object_type = NULL_TREE; | |
17148 | ||
17149 | if (name == error_mark_node) | |
17150 | return error_mark_node; | |
17151 | ||
17152 | /* A template-id has already been resolved; there is no lookup to | |
17153 | do. */ | |
17154 | if (TREE_CODE (name) == TEMPLATE_ID_EXPR) | |
17155 | return name; | |
17156 | if (BASELINK_P (name)) | |
17157 | { | |
50bc768d NS |
17158 | gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name)) |
17159 | == TEMPLATE_ID_EXPR); | |
a723baf1 MM |
17160 | return name; |
17161 | } | |
17162 | ||
17163 | /* A BIT_NOT_EXPR is used to represent a destructor. By this point, | |
17164 | it should already have been checked to make sure that the name | |
17165 | used matches the type being destroyed. */ | |
17166 | if (TREE_CODE (name) == BIT_NOT_EXPR) | |
17167 | { | |
17168 | tree type; | |
17169 | ||
17170 | /* Figure out to which type this destructor applies. */ | |
17171 | if (parser->scope) | |
17172 | type = parser->scope; | |
17173 | else if (object_type) | |
17174 | type = object_type; | |
17175 | else | |
17176 | type = current_class_type; | |
17177 | /* If that's not a class type, there is no destructor. */ | |
17178 | if (!type || !CLASS_TYPE_P (type)) | |
17179 | return error_mark_node; | |
9f4faeae MM |
17180 | if (CLASSTYPE_LAZY_DESTRUCTOR (type)) |
17181 | lazily_declare_fn (sfk_destructor, type); | |
fd6e3cce GB |
17182 | if (!CLASSTYPE_DESTRUCTORS (type)) |
17183 | return error_mark_node; | |
a723baf1 MM |
17184 | /* If it was a class type, return the destructor. */ |
17185 | return CLASSTYPE_DESTRUCTORS (type); | |
17186 | } | |
17187 | ||
17188 | /* By this point, the NAME should be an ordinary identifier. If | |
17189 | the id-expression was a qualified name, the qualifying scope is | |
17190 | stored in PARSER->SCOPE at this point. */ | |
50bc768d | 17191 | gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE); |
21526606 | 17192 | |
a723baf1 MM |
17193 | /* Perform the lookup. */ |
17194 | if (parser->scope) | |
21526606 | 17195 | { |
1fb3244a | 17196 | bool dependent_p; |
a723baf1 MM |
17197 | |
17198 | if (parser->scope == error_mark_node) | |
17199 | return error_mark_node; | |
17200 | ||
17201 | /* If the SCOPE is dependent, the lookup must be deferred until | |
17202 | the template is instantiated -- unless we are explicitly | |
17203 | looking up names in uninstantiated templates. Even then, we | |
17204 | cannot look up the name if the scope is not a class type; it | |
17205 | might, for example, be a template type parameter. */ | |
1fb3244a | 17206 | dependent_p = (TYPE_P (parser->scope) |
1cb801bc | 17207 | && dependent_scope_p (parser->scope)); |
a723baf1 | 17208 | if ((check_dependency || !CLASS_TYPE_P (parser->scope)) |
1cb801bc JM |
17209 | && dependent_p) |
17210 | /* Defer lookup. */ | |
17211 | decl = error_mark_node; | |
a723baf1 MM |
17212 | else |
17213 | { | |
4514aa8c | 17214 | tree pushed_scope = NULL_TREE; |
91b004e5 | 17215 | |
a723baf1 MM |
17216 | /* If PARSER->SCOPE is a dependent type, then it must be a |
17217 | class type, and we must not be checking dependencies; | |
17218 | otherwise, we would have processed this lookup above. So | |
17219 | that PARSER->SCOPE is not considered a dependent base by | |
17220 | lookup_member, we must enter the scope here. */ | |
1fb3244a | 17221 | if (dependent_p) |
4514aa8c | 17222 | pushed_scope = push_scope (parser->scope); |
78dcd41a | 17223 | /* If the PARSER->SCOPE is a template specialization, it |
a723baf1 MM |
17224 | may be instantiated during name lookup. In that case, |
17225 | errors may be issued. Even if we rollback the current | |
17226 | tentative parse, those errors are valid. */ | |
c8094d83 MS |
17227 | decl = lookup_qualified_name (parser->scope, name, |
17228 | tag_type != none_type, | |
5e08432e | 17229 | /*complain=*/true); |
58627576 JJ |
17230 | |
17231 | /* If we have a single function from a using decl, pull it out. */ | |
1cb801bc | 17232 | if (TREE_CODE (decl) == OVERLOAD |
58627576 JJ |
17233 | && !really_overloaded_fn (decl)) |
17234 | decl = OVL_FUNCTION (decl); | |
17235 | ||
4514aa8c NS |
17236 | if (pushed_scope) |
17237 | pop_scope (pushed_scope); | |
a723baf1 | 17238 | } |
1cb801bc JM |
17239 | |
17240 | /* If the scope is a dependent type and either we deferred lookup or | |
17241 | we did lookup but didn't find the name, rememeber the name. */ | |
17242 | if (decl == error_mark_node && TYPE_P (parser->scope) | |
17243 | && dependent_type_p (parser->scope)) | |
17244 | { | |
17245 | if (tag_type) | |
17246 | { | |
17247 | tree type; | |
17248 | ||
17249 | /* The resolution to Core Issue 180 says that `struct | |
17250 | A::B' should be considered a type-name, even if `A' | |
17251 | is dependent. */ | |
17252 | type = make_typename_type (parser->scope, name, tag_type, | |
17253 | /*complain=*/tf_error); | |
17254 | decl = TYPE_NAME (type); | |
17255 | } | |
17256 | else if (is_template | |
17257 | && (cp_parser_next_token_ends_template_argument_p (parser) | |
17258 | || cp_lexer_next_token_is (parser->lexer, | |
17259 | CPP_CLOSE_PAREN))) | |
17260 | decl = make_unbound_class_template (parser->scope, | |
17261 | name, NULL_TREE, | |
17262 | /*complain=*/tf_error); | |
17263 | else | |
17264 | decl = build_qualified_name (/*type=*/NULL_TREE, | |
17265 | parser->scope, name, | |
17266 | is_template); | |
17267 | } | |
a723baf1 MM |
17268 | parser->qualifying_scope = parser->scope; |
17269 | parser->object_scope = NULL_TREE; | |
17270 | } | |
17271 | else if (object_type) | |
17272 | { | |
17273 | tree object_decl = NULL_TREE; | |
17274 | /* Look up the name in the scope of the OBJECT_TYPE, unless the | |
17275 | OBJECT_TYPE is not a class. */ | |
17276 | if (CLASS_TYPE_P (object_type)) | |
17277 | /* If the OBJECT_TYPE is a template specialization, it may | |
17278 | be instantiated during name lookup. In that case, errors | |
17279 | may be issued. Even if we rollback the current tentative | |
17280 | parse, those errors are valid. */ | |
17281 | object_decl = lookup_member (object_type, | |
17282 | name, | |
c8094d83 | 17283 | /*protect=*/0, |
fc6a28d7 | 17284 | tag_type != none_type); |
a723baf1 | 17285 | /* Look it up in the enclosing context, too. */ |
c8094d83 | 17286 | decl = lookup_name_real (name, tag_type != none_type, |
fc6a28d7 | 17287 | /*nonclass=*/0, |
ef07d61b | 17288 | /*block_p=*/true, is_namespace, flags); |
a723baf1 MM |
17289 | parser->object_scope = object_type; |
17290 | parser->qualifying_scope = NULL_TREE; | |
17291 | if (object_decl) | |
17292 | decl = object_decl; | |
17293 | } | |
17294 | else | |
17295 | { | |
c8094d83 | 17296 | decl = lookup_name_real (name, tag_type != none_type, |
fc6a28d7 | 17297 | /*nonclass=*/0, |
ef07d61b | 17298 | /*block_p=*/true, is_namespace, flags); |
a723baf1 MM |
17299 | parser->qualifying_scope = NULL_TREE; |
17300 | parser->object_scope = NULL_TREE; | |
17301 | } | |
17302 | ||
17303 | /* If the lookup failed, let our caller know. */ | |
bd3d082e | 17304 | if (!decl || decl == error_mark_node) |
a723baf1 MM |
17305 | return error_mark_node; |
17306 | ||
17307 | /* If it's a TREE_LIST, the result of the lookup was ambiguous. */ | |
17308 | if (TREE_CODE (decl) == TREE_LIST) | |
17309 | { | |
91b1ca65 MM |
17310 | if (ambiguous_decls) |
17311 | *ambiguous_decls = decl; | |
a723baf1 MM |
17312 | /* The error message we have to print is too complicated for |
17313 | cp_parser_error, so we incorporate its actions directly. */ | |
e5976695 | 17314 | if (!cp_parser_simulate_error (parser)) |
a723baf1 | 17315 | { |
69bc6bff MLI |
17316 | error_at (name_location, "reference to %qD is ambiguous", |
17317 | name); | |
a723baf1 MM |
17318 | print_candidates (decl); |
17319 | } | |
17320 | return error_mark_node; | |
17321 | } | |
17322 | ||
50bc768d NS |
17323 | gcc_assert (DECL_P (decl) |
17324 | || TREE_CODE (decl) == OVERLOAD | |
17325 | || TREE_CODE (decl) == SCOPE_REF | |
17326 | || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE | |
17327 | || BASELINK_P (decl)); | |
a723baf1 MM |
17328 | |
17329 | /* If we have resolved the name of a member declaration, check to | |
17330 | see if the declaration is accessible. When the name resolves to | |
34cd5ae7 | 17331 | set of overloaded functions, accessibility is checked when |
21526606 | 17332 | overload resolution is done. |
a723baf1 MM |
17333 | |
17334 | During an explicit instantiation, access is not checked at all, | |
17335 | as per [temp.explicit]. */ | |
8d241e0b | 17336 | if (DECL_P (decl)) |
ee76b931 | 17337 | check_accessibility_of_qualified_id (decl, object_type, parser->scope); |
a723baf1 MM |
17338 | |
17339 | return decl; | |
17340 | } | |
17341 | ||
17342 | /* Like cp_parser_lookup_name, but for use in the typical case where | |
b0bc6e8e KL |
17343 | CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE, |
17344 | IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */ | |
a723baf1 MM |
17345 | |
17346 | static tree | |
2b7a3abf | 17347 | cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location) |
a723baf1 | 17348 | { |
21526606 | 17349 | return cp_parser_lookup_name (parser, name, |
fc6a28d7 | 17350 | none_type, |
b0bc6e8e | 17351 | /*is_template=*/false, |
eea9800f | 17352 | /*is_namespace=*/false, |
8f78f01f | 17353 | /*check_dependency=*/true, |
2b7a3abf DS |
17354 | /*ambiguous_decls=*/NULL, |
17355 | location); | |
a723baf1 MM |
17356 | } |
17357 | ||
a723baf1 MM |
17358 | /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in |
17359 | the current context, return the TYPE_DECL. If TAG_NAME_P is | |
17360 | true, the DECL indicates the class being defined in a class-head, | |
17361 | or declared in an elaborated-type-specifier. | |
17362 | ||
17363 | Otherwise, return DECL. */ | |
17364 | ||
17365 | static tree | |
17366 | cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p) | |
17367 | { | |
710b73e6 KL |
17368 | /* If the TEMPLATE_DECL is being declared as part of a class-head, |
17369 | the translation from TEMPLATE_DECL to TYPE_DECL occurs: | |
a723baf1 | 17370 | |
21526606 | 17371 | struct A { |
0cbd7506 | 17372 | template <typename T> struct B; |
a723baf1 MM |
17373 | }; |
17374 | ||
21526606 EC |
17375 | template <typename T> struct A::B {}; |
17376 | ||
c72a1a86 | 17377 | Similarly, in an elaborated-type-specifier: |
a723baf1 MM |
17378 | |
17379 | namespace N { struct X{}; } | |
17380 | ||
17381 | struct A { | |
0cbd7506 | 17382 | template <typename T> friend struct N::X; |
a723baf1 MM |
17383 | }; |
17384 | ||
710b73e6 KL |
17385 | However, if the DECL refers to a class type, and we are in |
17386 | the scope of the class, then the name lookup automatically | |
17387 | finds the TYPE_DECL created by build_self_reference rather | |
17388 | than a TEMPLATE_DECL. For example, in: | |
17389 | ||
17390 | template <class T> struct S { | |
0cbd7506 | 17391 | S s; |
710b73e6 KL |
17392 | }; |
17393 | ||
17394 | there is no need to handle such case. */ | |
17395 | ||
17396 | if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p) | |
a723baf1 MM |
17397 | return DECL_TEMPLATE_RESULT (decl); |
17398 | ||
17399 | return decl; | |
17400 | } | |
17401 | ||
17402 | /* If too many, or too few, template-parameter lists apply to the | |
17403 | declarator, issue an error message. Returns TRUE if all went well, | |
17404 | and FALSE otherwise. */ | |
17405 | ||
17406 | static bool | |
21526606 | 17407 | cp_parser_check_declarator_template_parameters (cp_parser* parser, |
2b7a3abf DS |
17408 | cp_declarator *declarator, |
17409 | location_t declarator_location) | |
a723baf1 MM |
17410 | { |
17411 | unsigned num_templates; | |
17412 | ||
17413 | /* We haven't seen any classes that involve template parameters yet. */ | |
17414 | num_templates = 0; | |
17415 | ||
058b15c1 | 17416 | switch (declarator->kind) |
a723baf1 | 17417 | { |
058b15c1 | 17418 | case cdk_id: |
1d786913 | 17419 | if (declarator->u.id.qualifying_scope) |
058b15c1 MM |
17420 | { |
17421 | tree scope; | |
17422 | tree member; | |
a723baf1 | 17423 | |
1d786913 MM |
17424 | scope = declarator->u.id.qualifying_scope; |
17425 | member = declarator->u.id.unqualified_name; | |
a723baf1 | 17426 | |
058b15c1 MM |
17427 | while (scope && CLASS_TYPE_P (scope)) |
17428 | { | |
17429 | /* You're supposed to have one `template <...>' | |
17430 | for every template class, but you don't need one | |
17431 | for a full specialization. For example: | |
17432 | ||
17433 | template <class T> struct S{}; | |
17434 | template <> struct S<int> { void f(); }; | |
17435 | void S<int>::f () {} | |
17436 | ||
17437 | is correct; there shouldn't be a `template <>' for | |
17438 | the definition of `S<int>::f'. */ | |
9ba7a2f2 MM |
17439 | if (!CLASSTYPE_TEMPLATE_INFO (scope)) |
17440 | /* If SCOPE does not have template information of any | |
17441 | kind, then it is not a template, nor is it nested | |
17442 | within a template. */ | |
17443 | break; | |
17444 | if (explicit_class_specialization_p (scope)) | |
17445 | break; | |
17446 | if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))) | |
058b15c1 MM |
17447 | ++num_templates; |
17448 | ||
17449 | scope = TYPE_CONTEXT (scope); | |
17450 | } | |
17451 | } | |
c8094d83 | 17452 | else if (TREE_CODE (declarator->u.id.unqualified_name) |
1d786913 MM |
17453 | == TEMPLATE_ID_EXPR) |
17454 | /* If the DECLARATOR has the form `X<y>' then it uses one | |
17455 | additional level of template parameters. */ | |
a723baf1 MM |
17456 | ++num_templates; |
17457 | ||
0d246707 MLI |
17458 | return cp_parser_check_template_parameters |
17459 | (parser, num_templates, declarator_location, declarator); | |
17460 | ||
058b15c1 MM |
17461 | |
17462 | case cdk_function: | |
17463 | case cdk_array: | |
17464 | case cdk_pointer: | |
17465 | case cdk_reference: | |
17466 | case cdk_ptrmem: | |
98ca843c | 17467 | return (cp_parser_check_declarator_template_parameters |
2b7a3abf | 17468 | (parser, declarator->declarator, declarator_location)); |
058b15c1 MM |
17469 | |
17470 | case cdk_error: | |
17471 | return true; | |
17472 | ||
17473 | default: | |
315fb5db | 17474 | gcc_unreachable (); |
a723baf1 | 17475 | } |
315fb5db | 17476 | return false; |
a723baf1 MM |
17477 | } |
17478 | ||
17479 | /* NUM_TEMPLATES were used in the current declaration. If that is | |
17480 | invalid, return FALSE and issue an error messages. Otherwise, | |
0d246707 MLI |
17481 | return TRUE. If DECLARATOR is non-NULL, then we are checking a |
17482 | declarator and we can print more accurate diagnostics. */ | |
a723baf1 MM |
17483 | |
17484 | static bool | |
94edc4ab | 17485 | cp_parser_check_template_parameters (cp_parser* parser, |
2b7a3abf | 17486 | unsigned num_templates, |
0d246707 MLI |
17487 | location_t location, |
17488 | cp_declarator *declarator) | |
a723baf1 | 17489 | { |
0d246707 MLI |
17490 | /* If there are the same number of template classes and parameter |
17491 | lists, that's OK. */ | |
17492 | if (parser->num_template_parameter_lists == num_templates) | |
17493 | return true; | |
17494 | /* If there are more, but only one more, then we are referring to a | |
17495 | member template. That's OK too. */ | |
17496 | if (parser->num_template_parameter_lists == num_templates + 1) | |
17497 | return true; | |
a723baf1 MM |
17498 | /* If there are more template classes than parameter lists, we have |
17499 | something like: | |
21526606 | 17500 | |
a723baf1 MM |
17501 | template <class T> void S<T>::R<T>::f (); */ |
17502 | if (parser->num_template_parameter_lists < num_templates) | |
17503 | { | |
0d246707 MLI |
17504 | if (declarator) |
17505 | error_at (location, "specializing member %<%T::%E%> " | |
17506 | "requires %<template<>%> syntax", | |
17507 | declarator->u.id.qualifying_scope, | |
17508 | declarator->u.id.unqualified_name); | |
17509 | else | |
17510 | error_at (location, "too few template-parameter-lists"); | |
a723baf1 MM |
17511 | return false; |
17512 | } | |
a723baf1 MM |
17513 | /* Otherwise, there are too many template parameter lists. We have |
17514 | something like: | |
17515 | ||
17516 | template <class T> template <class U> void S::f(); */ | |
69bc6bff | 17517 | error_at (location, "too many template-parameter-lists"); |
a723baf1 MM |
17518 | return false; |
17519 | } | |
17520 | ||
a723baf1 MM |
17521 | /* Parse an optional `::' token indicating that the following name is |
17522 | from the global namespace. If so, PARSER->SCOPE is set to the | |
17523 | GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE, | |
17524 | unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone. | |
17525 | Returns the new value of PARSER->SCOPE, if the `::' token is | |
17526 | present, and NULL_TREE otherwise. */ | |
17527 | ||
17528 | static tree | |
88525e1b | 17529 | cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p) |
a723baf1 MM |
17530 | { |
17531 | cp_token *token; | |
17532 | ||
17533 | /* Peek at the next token. */ | |
17534 | token = cp_lexer_peek_token (parser->lexer); | |
17535 | /* If we're looking at a `::' token then we're starting from the | |
17536 | global namespace, not our current location. */ | |
17537 | if (token->type == CPP_SCOPE) | |
17538 | { | |
17539 | /* Consume the `::' token. */ | |
17540 | cp_lexer_consume_token (parser->lexer); | |
17541 | /* Set the SCOPE so that we know where to start the lookup. */ | |
17542 | parser->scope = global_namespace; | |
17543 | parser->qualifying_scope = global_namespace; | |
17544 | parser->object_scope = NULL_TREE; | |
17545 | ||
17546 | return parser->scope; | |
17547 | } | |
88525e1b | 17548 | else if (!current_scope_valid_p) |
a723baf1 MM |
17549 | { |
17550 | parser->scope = NULL_TREE; | |
17551 | parser->qualifying_scope = NULL_TREE; | |
88525e1b | 17552 | parser->object_scope = NULL_TREE; |
a723baf1 MM |
17553 | } |
17554 | ||
17555 | return NULL_TREE; | |
17556 | } | |
17557 | ||
17558 | /* Returns TRUE if the upcoming token sequence is the start of a | |
17559 | constructor declarator. If FRIEND_P is true, the declarator is | |
17560 | preceded by the `friend' specifier. */ | |
17561 | ||
17562 | static bool | |
17563 | cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p) | |
17564 | { | |
17565 | bool constructor_p; | |
17566 | tree type_decl = NULL_TREE; | |
17567 | bool nested_name_p; | |
2050a1bb MM |
17568 | cp_token *next_token; |
17569 | ||
17570 | /* The common case is that this is not a constructor declarator, so | |
8fbc5ae7 MM |
17571 | try to avoid doing lots of work if at all possible. It's not |
17572 | valid declare a constructor at function scope. */ | |
56c7f9c1 | 17573 | if (parser->in_function_body) |
8fbc5ae7 MM |
17574 | return false; |
17575 | /* And only certain tokens can begin a constructor declarator. */ | |
2050a1bb MM |
17576 | next_token = cp_lexer_peek_token (parser->lexer); |
17577 | if (next_token->type != CPP_NAME | |
17578 | && next_token->type != CPP_SCOPE | |
17579 | && next_token->type != CPP_NESTED_NAME_SPECIFIER | |
17580 | && next_token->type != CPP_TEMPLATE_ID) | |
17581 | return false; | |
a723baf1 MM |
17582 | |
17583 | /* Parse tentatively; we are going to roll back all of the tokens | |
17584 | consumed here. */ | |
17585 | cp_parser_parse_tentatively (parser); | |
17586 | /* Assume that we are looking at a constructor declarator. */ | |
17587 | constructor_p = true; | |
8d241e0b | 17588 | |
a723baf1 MM |
17589 | /* Look for the optional `::' operator. */ |
17590 | cp_parser_global_scope_opt (parser, | |
88525e1b | 17591 | /*current_scope_valid_p=*/false); |
a723baf1 | 17592 | /* Look for the nested-name-specifier. */ |
21526606 | 17593 | nested_name_p |
a723baf1 MM |
17594 | = (cp_parser_nested_name_specifier_opt (parser, |
17595 | /*typename_keyword_p=*/false, | |
17596 | /*check_dependency_p=*/false, | |
a668c6ad MM |
17597 | /*type_p=*/false, |
17598 | /*is_declaration=*/false) | |
a723baf1 MM |
17599 | != NULL_TREE); |
17600 | /* Outside of a class-specifier, there must be a | |
17601 | nested-name-specifier. */ | |
21526606 | 17602 | if (!nested_name_p && |
a723baf1 MM |
17603 | (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type) |
17604 | || friend_p)) | |
17605 | constructor_p = false; | |
17606 | /* If we still think that this might be a constructor-declarator, | |
17607 | look for a class-name. */ | |
17608 | if (constructor_p) | |
17609 | { | |
17610 | /* If we have: | |
17611 | ||
8fbc5ae7 | 17612 | template <typename T> struct S { S(); }; |
a723baf1 MM |
17613 | template <typename T> S<T>::S (); |
17614 | ||
17615 | we must recognize that the nested `S' names a class. | |
17616 | Similarly, for: | |
17617 | ||
17618 | template <typename T> S<T>::S<T> (); | |
17619 | ||
17620 | we must recognize that the nested `S' names a template. */ | |
17621 | type_decl = cp_parser_class_name (parser, | |
17622 | /*typename_keyword_p=*/false, | |
17623 | /*template_keyword_p=*/false, | |
fc6a28d7 | 17624 | none_type, |
a723baf1 | 17625 | /*check_dependency_p=*/false, |
a668c6ad MM |
17626 | /*class_head_p=*/false, |
17627 | /*is_declaration=*/false); | |
a723baf1 MM |
17628 | /* If there was no class-name, then this is not a constructor. */ |
17629 | constructor_p = !cp_parser_error_occurred (parser); | |
17630 | } | |
8d241e0b | 17631 | |
a723baf1 MM |
17632 | /* If we're still considering a constructor, we have to see a `(', |
17633 | to begin the parameter-declaration-clause, followed by either a | |
17634 | `)', an `...', or a decl-specifier. We need to check for a | |
17635 | type-specifier to avoid being fooled into thinking that: | |
17636 | ||
17637 | S::S (f) (int); | |
17638 | ||
17639 | is a constructor. (It is actually a function named `f' that | |
17640 | takes one parameter (of type `int') and returns a value of type | |
17641 | `S::S'. */ | |
21526606 | 17642 | if (constructor_p |
3a483371 | 17643 | && cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>")) |
a723baf1 MM |
17644 | { |
17645 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN) | |
17646 | && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS) | |
15077df5 MM |
17647 | /* A parameter declaration begins with a decl-specifier, |
17648 | which is either the "attribute" keyword, a storage class | |
17649 | specifier, or (usually) a type-specifier. */ | |
d19b84e9 | 17650 | && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer)) |
a723baf1 | 17651 | { |
5dae1114 | 17652 | tree type; |
4514aa8c | 17653 | tree pushed_scope = NULL_TREE; |
4047b164 | 17654 | unsigned saved_num_template_parameter_lists; |
5dae1114 MM |
17655 | |
17656 | /* Names appearing in the type-specifier should be looked up | |
17657 | in the scope of the class. */ | |
17658 | if (current_class_type) | |
17659 | type = NULL_TREE; | |
a723baf1 MM |
17660 | else |
17661 | { | |
5dae1114 MM |
17662 | type = TREE_TYPE (type_decl); |
17663 | if (TREE_CODE (type) == TYPENAME_TYPE) | |
14d22dd6 | 17664 | { |
21526606 | 17665 | type = resolve_typename_type (type, |
14d22dd6 | 17666 | /*only_current_p=*/false); |
4195a767 | 17667 | if (TREE_CODE (type) == TYPENAME_TYPE) |
14d22dd6 MM |
17668 | { |
17669 | cp_parser_abort_tentative_parse (parser); | |
17670 | return false; | |
17671 | } | |
17672 | } | |
4514aa8c | 17673 | pushed_scope = push_scope (type); |
a723baf1 | 17674 | } |
4047b164 KL |
17675 | |
17676 | /* Inside the constructor parameter list, surrounding | |
17677 | template-parameter-lists do not apply. */ | |
17678 | saved_num_template_parameter_lists | |
17679 | = parser->num_template_parameter_lists; | |
17680 | parser->num_template_parameter_lists = 0; | |
17681 | ||
5dae1114 MM |
17682 | /* Look for the type-specifier. */ |
17683 | cp_parser_type_specifier (parser, | |
17684 | CP_PARSER_FLAGS_NONE, | |
62d1db17 | 17685 | /*decl_specs=*/NULL, |
5dae1114 MM |
17686 | /*is_declarator=*/true, |
17687 | /*declares_class_or_enum=*/NULL, | |
17688 | /*is_cv_qualifier=*/NULL); | |
4047b164 KL |
17689 | |
17690 | parser->num_template_parameter_lists | |
17691 | = saved_num_template_parameter_lists; | |
17692 | ||
5dae1114 | 17693 | /* Leave the scope of the class. */ |
4514aa8c NS |
17694 | if (pushed_scope) |
17695 | pop_scope (pushed_scope); | |
5dae1114 MM |
17696 | |
17697 | constructor_p = !cp_parser_error_occurred (parser); | |
a723baf1 MM |
17698 | } |
17699 | } | |
17700 | else | |
17701 | constructor_p = false; | |
17702 | /* We did not really want to consume any tokens. */ | |
17703 | cp_parser_abort_tentative_parse (parser); | |
17704 | ||
17705 | return constructor_p; | |
17706 | } | |
17707 | ||
17708 | /* Parse the definition of the function given by the DECL_SPECIFIERS, | |
cf22909c | 17709 | ATTRIBUTES, and DECLARATOR. The access checks have been deferred; |
a723baf1 MM |
17710 | they must be performed once we are in the scope of the function. |
17711 | ||
17712 | Returns the function defined. */ | |
17713 | ||
17714 | static tree | |
17715 | cp_parser_function_definition_from_specifiers_and_declarator | |
94edc4ab | 17716 | (cp_parser* parser, |
62d1db17 | 17717 | cp_decl_specifier_seq *decl_specifiers, |
94edc4ab | 17718 | tree attributes, |
058b15c1 | 17719 | const cp_declarator *declarator) |
a723baf1 MM |
17720 | { |
17721 | tree fn; | |
17722 | bool success_p; | |
17723 | ||
17724 | /* Begin the function-definition. */ | |
058b15c1 MM |
17725 | success_p = start_function (decl_specifiers, declarator, attributes); |
17726 | ||
17727 | /* The things we're about to see are not directly qualified by any | |
17728 | template headers we've seen thus far. */ | |
17729 | reset_specialization (); | |
a723baf1 MM |
17730 | |
17731 | /* If there were names looked up in the decl-specifier-seq that we | |
17732 | did not check, check them now. We must wait until we are in the | |
17733 | scope of the function to perform the checks, since the function | |
17734 | might be a friend. */ | |
cf22909c | 17735 | perform_deferred_access_checks (); |
a723baf1 MM |
17736 | |
17737 | if (!success_p) | |
17738 | { | |
058b15c1 | 17739 | /* Skip the entire function. */ |
a723baf1 MM |
17740 | cp_parser_skip_to_end_of_block_or_statement (parser); |
17741 | fn = error_mark_node; | |
17742 | } | |
72c4a4ca GK |
17743 | else if (DECL_INITIAL (current_function_decl) != error_mark_node) |
17744 | { | |
17745 | /* Seen already, skip it. An error message has already been output. */ | |
17746 | cp_parser_skip_to_end_of_block_or_statement (parser); | |
17747 | fn = current_function_decl; | |
17748 | current_function_decl = NULL_TREE; | |
17749 | /* If this is a function from a class, pop the nested class. */ | |
17750 | if (current_class_name) | |
17751 | pop_nested_class (); | |
17752 | } | |
a723baf1 MM |
17753 | else |
17754 | fn = cp_parser_function_definition_after_declarator (parser, | |
17755 | /*inline_p=*/false); | |
17756 | ||
17757 | return fn; | |
17758 | } | |
17759 | ||
17760 | /* Parse the part of a function-definition that follows the | |
17761 | declarator. INLINE_P is TRUE iff this function is an inline | |
17762 | function defined with a class-specifier. | |
17763 | ||
17764 | Returns the function defined. */ | |
17765 | ||
21526606 EC |
17766 | static tree |
17767 | cp_parser_function_definition_after_declarator (cp_parser* parser, | |
94edc4ab | 17768 | bool inline_p) |
a723baf1 MM |
17769 | { |
17770 | tree fn; | |
17771 | bool ctor_initializer_p = false; | |
17772 | bool saved_in_unbraced_linkage_specification_p; | |
56c7f9c1 | 17773 | bool saved_in_function_body; |
a723baf1 | 17774 | unsigned saved_num_template_parameter_lists; |
2b7a3abf | 17775 | cp_token *token; |
a723baf1 | 17776 | |
56c7f9c1 MM |
17777 | saved_in_function_body = parser->in_function_body; |
17778 | parser->in_function_body = true; | |
a723baf1 MM |
17779 | /* If the next token is `return', then the code may be trying to |
17780 | make use of the "named return value" extension that G++ used to | |
17781 | support. */ | |
2b7a3abf | 17782 | token = cp_lexer_peek_token (parser->lexer); |
a723baf1 MM |
17783 | if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN)) |
17784 | { | |
17785 | /* Consume the `return' keyword. */ | |
17786 | cp_lexer_consume_token (parser->lexer); | |
17787 | /* Look for the identifier that indicates what value is to be | |
17788 | returned. */ | |
17789 | cp_parser_identifier (parser); | |
17790 | /* Issue an error message. */ | |
69bc6bff MLI |
17791 | error_at (token->location, |
17792 | "named return values are no longer supported"); | |
a723baf1 | 17793 | /* Skip tokens until we reach the start of the function body. */ |
bc4071dd RH |
17794 | while (true) |
17795 | { | |
17796 | cp_token *token = cp_lexer_peek_token (parser->lexer); | |
17797 | if (token->type == CPP_OPEN_BRACE | |
17798 | || token->type == CPP_EOF | |
17799 | || token->type == CPP_PRAGMA_EOL) | |
17800 | break; | |
17801 | cp_lexer_consume_token (parser->lexer); | |
17802 | } | |
a723baf1 MM |
17803 | } |
17804 | /* The `extern' in `extern "C" void f () { ... }' does not apply to | |
17805 | anything declared inside `f'. */ | |
21526606 | 17806 | saved_in_unbraced_linkage_specification_p |
a723baf1 MM |
17807 | = parser->in_unbraced_linkage_specification_p; |
17808 | parser->in_unbraced_linkage_specification_p = false; | |
17809 | /* Inside the function, surrounding template-parameter-lists do not | |
17810 | apply. */ | |
21526606 EC |
17811 | saved_num_template_parameter_lists |
17812 | = parser->num_template_parameter_lists; | |
a723baf1 MM |
17813 | parser->num_template_parameter_lists = 0; |
17814 | /* If the next token is `try', then we are looking at a | |
17815 | function-try-block. */ | |
17816 | if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY)) | |
17817 | ctor_initializer_p = cp_parser_function_try_block (parser); | |
17818 | /* A function-try-block includes the function-body, so we only do | |
17819 | this next part if we're not processing a function-try-block. */ | |
17820 | else | |
21526606 | 17821 | ctor_initializer_p |
a723baf1 MM |
17822 | = cp_parser_ctor_initializer_opt_and_function_body (parser); |
17823 | ||
17824 | /* Finish the function. */ | |
21526606 | 17825 | fn = finish_function ((ctor_initializer_p ? 1 : 0) | |
a723baf1 MM |
17826 | (inline_p ? 2 : 0)); |
17827 | /* Generate code for it, if necessary. */ | |
8cd2462c | 17828 | expand_or_defer_fn (fn); |
a723baf1 | 17829 | /* Restore the saved values. */ |
21526606 | 17830 | parser->in_unbraced_linkage_specification_p |
a723baf1 | 17831 | = saved_in_unbraced_linkage_specification_p; |
21526606 | 17832 | parser->num_template_parameter_lists |
a723baf1 | 17833 | = saved_num_template_parameter_lists; |
56c7f9c1 | 17834 | parser->in_function_body = saved_in_function_body; |
a723baf1 MM |
17835 | |
17836 | return fn; | |
17837 | } | |
17838 | ||
17839 | /* Parse a template-declaration, assuming that the `export' (and | |
17840 | `extern') keywords, if present, has already been scanned. MEMBER_P | |
17841 | is as for cp_parser_template_declaration. */ | |
17842 | ||
17843 | static void | |
94edc4ab | 17844 | cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p) |
a723baf1 MM |
17845 | { |
17846 | tree decl = NULL_TREE; | |
d6b418fa | 17847 | VEC (deferred_access_check,gc) *checks; |
a723baf1 MM |
17848 | tree parameter_list; |
17849 | bool friend_p = false; | |
2f1b1731 | 17850 | bool need_lang_pop; |
2b7a3abf | 17851 | cp_token *token; |
a723baf1 MM |
17852 | |
17853 | /* Look for the `template' keyword. */ | |
2b7a3abf | 17854 | token = cp_lexer_peek_token (parser->lexer); |
3a483371 | 17855 | if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>")) |
a723baf1 | 17856 | return; |
21526606 | 17857 | |
a723baf1 | 17858 | /* And the `<'. */ |
3a483371 | 17859 | if (!cp_parser_require (parser, CPP_LESS, "%<<%>")) |
a723baf1 | 17860 | return; |
7f1ba716 MM |
17861 | if (at_class_scope_p () && current_function_decl) |
17862 | { | |
17863 | /* 14.5.2.2 [temp.mem] | |
17864 | ||
17865 | A local class shall not have member templates. */ | |
69bc6bff MLI |
17866 | error_at (token->location, |
17867 | "invalid declaration of member template in local class"); | |
7f1ba716 MM |
17868 | cp_parser_skip_to_end_of_block_or_statement (parser); |
17869 | return; | |
17870 | } | |
2f1b1731 | 17871 | /* [temp] |
3db45ab5 | 17872 | |
2f1b1731 MM |
17873 | A template ... shall not have C linkage. */ |
17874 | if (current_lang_name == lang_name_c) | |
17875 | { | |
69bc6bff | 17876 | error_at (token->location, "template with C linkage"); |
2f1b1731 MM |
17877 | /* Give it C++ linkage to avoid confusing other parts of the |
17878 | front end. */ | |
17879 | push_lang_context (lang_name_cplusplus); | |
17880 | need_lang_pop = true; | |
17881 | } | |
17882 | else | |
17883 | need_lang_pop = false; | |
6b648482 MM |
17884 | |
17885 | /* We cannot perform access checks on the template parameter | |
17886 | declarations until we know what is being declared, just as we | |
17887 | cannot check the decl-specifier list. */ | |
17888 | push_deferring_access_checks (dk_deferred); | |
17889 | ||
a723baf1 MM |
17890 | /* If the next token is `>', then we have an invalid |
17891 | specialization. Rather than complain about an invalid template | |
17892 | parameter, issue an error message here. */ | |
17893 | if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)) | |
17894 | { | |
17895 | cp_parser_error (parser, "invalid explicit specialization"); | |
2f9afd51 | 17896 | begin_specialization (); |
a723baf1 MM |
17897 | parameter_list = NULL_TREE; |
17898 | } | |
17899 | else | |
357d956e MM |
17900 | /* Parse the template parameters. */ |
17901 | parameter_list = cp_parser_template_parameter_list (parser); | |
2f9afd51 | 17902 | |
6b648482 MM |
17903 | /* Get the deferred access checks from the parameter list. These |
17904 | will be checked once we know what is being declared, as for a | |
17905 | member template the checks must be performed in the scope of the | |
17906 | class containing the member. */ | |
17907 | checks = get_deferred_access_checks (); | |
17908 | ||
a723baf1 | 17909 | /* Look for the `>'. */ |
a7b9d08c | 17910 | cp_parser_skip_to_end_of_template_parameter_list (parser); |
a723baf1 MM |
17911 | /* We just processed one more parameter list. */ |
17912 | ++parser->num_template_parameter_lists; | |
17913 | /* If the next token is `template', there are more template | |
17914 | parameters. */ | |
21526606 | 17915 | if (cp_lexer_next_token_is_keyword (parser->lexer, |
a723baf1 MM |
17916 | RID_TEMPLATE)) |
17917 | cp_parser_template_declaration_after_export (parser, member_p); | |
17918 | else | |
17919 | { | |
fe88415f | 17920 | /* There are no access checks when parsing a template, as we do not |
0cbd7506 | 17921 | know if a specialization will be a friend. */ |
fe88415f | 17922 | push_deferring_access_checks (dk_no_check); |
2b7a3abf | 17923 | token = cp_lexer_peek_token (parser->lexer); |
a723baf1 | 17924 | decl = cp_parser_single_declaration (parser, |
6b648482 | 17925 | checks, |
a723baf1 | 17926 | member_p, |
736b8100 | 17927 | /*explicit_specialization_p=*/false, |
a723baf1 | 17928 | &friend_p); |
fe88415f | 17929 | pop_deferring_access_checks (); |
98ca843c | 17930 | |
a723baf1 MM |
17931 | /* If this is a member template declaration, let the front |
17932 | end know. */ | |
17933 | if (member_p && !friend_p && decl) | |
37d407a1 KL |
17934 | { |
17935 | if (TREE_CODE (decl) == TYPE_DECL) | |
2b7a3abf | 17936 | cp_parser_check_access_in_redeclaration (decl, token->location); |
37d407a1 KL |
17937 | |
17938 | decl = finish_member_template_decl (decl); | |
17939 | } | |
a723baf1 | 17940 | else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL) |
19db77ce KL |
17941 | make_friend_class (current_class_type, TREE_TYPE (decl), |
17942 | /*complain=*/true); | |
a723baf1 MM |
17943 | } |
17944 | /* We are done with the current parameter list. */ | |
17945 | --parser->num_template_parameter_lists; | |
17946 | ||
6b648482 MM |
17947 | pop_deferring_access_checks (); |
17948 | ||
a723baf1 MM |
17949 | /* Finish up. */ |
17950 | finish_template_decl (parameter_list); | |
17951 | ||
17952 | /* Register member declarations. */ | |
17953 | if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl)) | |
17954 | finish_member_declaration (decl); | |
2f1b1731 MM |
17955 | /* For the erroneous case of a template with C linkage, we pushed an |
17956 | implicit C++ linkage scope; exit that scope now. */ | |
17957 | if (need_lang_pop) | |
17958 | pop_lang_context (); | |
a723baf1 MM |
17959 | /* If DECL is a function template, we must return to parse it later. |
17960 | (Even though there is no definition, there might be default | |
17961 | arguments that need handling.) */ | |
21526606 | 17962 | if (member_p && decl |
a723baf1 MM |
17963 | && (TREE_CODE (decl) == FUNCTION_DECL |
17964 | || DECL_FUNCTION_TEMPLATE_P (decl))) | |
17965 | TREE_VALUE (parser->unparsed_functions_queues) | |
21526606 | 17966 | = tree_cons (NULL_TREE, decl, |
a723baf1 MM |
17967 | TREE_VALUE (parser->unparsed_functions_queues)); |
17968 | } | |
17969 | ||
6b648482 MM |
17970 | /* Perform the deferred access checks from a template-parameter-list. |
17971 | CHECKS is a TREE_LIST of access checks, as returned by | |
17972 | get_deferred_access_checks. */ | |
17973 | ||
17974 | static void | |
d6b418fa | 17975 | cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks) |
6b648482 MM |
17976 | { |
17977 | ++processing_template_parmlist; | |
17978 | perform_access_checks (checks); | |
17979 | --processing_template_parmlist; | |
17980 | } | |
17981 | ||
a723baf1 MM |
17982 | /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or |
17983 | `function-definition' sequence. MEMBER_P is true, this declaration | |
17984 | appears in a class scope. | |
17985 | ||
17986 | Returns the DECL for the declared entity. If FRIEND_P is non-NULL, | |
17987 | *FRIEND_P is set to TRUE iff the declaration is a friend. */ | |
17988 | ||
17989 | static tree | |
21526606 | 17990 | cp_parser_single_declaration (cp_parser* parser, |
d6b418fa | 17991 | VEC (deferred_access_check,gc)* checks, |
94edc4ab | 17992 | bool member_p, |
736b8100 | 17993 | bool explicit_specialization_p, |
94edc4ab | 17994 | bool* friend_p) |
a723baf1 | 17995 | { |
560ad596 | 17996 | int declares_class_or_enum; |
a723baf1 | 17997 | tree decl = NULL_TREE; |
62d1db17 | 17998 | cp_decl_specifier_seq decl_specifiers; |
4bb8ca28 | 17999 | bool function_definition_p = false; |
2b7a3abf | 18000 | cp_token *decl_spec_token_start; |
a723baf1 | 18001 | |
71bd7186 MM |
18002 | /* This function is only used when processing a template |
18003 | declaration. */ | |
18004 | gcc_assert (innermost_scope_kind () == sk_template_parms | |
18005 | || innermost_scope_kind () == sk_template_spec); | |
18006 | ||
a723baf1 | 18007 | /* Defer access checks until we know what is being declared. */ |
8d241e0b | 18008 | push_deferring_access_checks (dk_deferred); |
cf22909c | 18009 | |
a723baf1 MM |
18010 | /* Try the `decl-specifier-seq [opt] init-declarator [opt]' |
18011 | alternative. */ | |
2b7a3abf | 18012 | decl_spec_token_start = cp_lexer_peek_token (parser->lexer); |
62d1db17 MM |
18013 | cp_parser_decl_specifier_seq (parser, |
18014 | CP_PARSER_FLAGS_OPTIONAL, | |
18015 | &decl_specifiers, | |
18016 | &declares_class_or_enum); | |
4bb8ca28 | 18017 | if (friend_p) |
62d1db17 | 18018 | *friend_p = cp_parser_friend_p (&decl_specifiers); |
71bd7186 MM |
18019 | |
18020 | /* There are no template typedefs. */ | |
18021 | if (decl_specifiers.specs[(int) ds_typedef]) | |
18022 | { | |
69bc6bff MLI |
18023 | error_at (decl_spec_token_start->location, |
18024 | "template declaration of %<typedef%>"); | |
71bd7186 MM |
18025 | decl = error_mark_node; |
18026 | } | |
18027 | ||
a723baf1 MM |
18028 | /* Gather up the access checks that occurred the |
18029 | decl-specifier-seq. */ | |
cf22909c KL |
18030 | stop_deferring_access_checks (); |
18031 | ||
a723baf1 MM |
18032 | /* Check for the declaration of a template class. */ |
18033 | if (declares_class_or_enum) | |
18034 | { | |
18035 | if (cp_parser_declares_only_class_p (parser)) | |
18036 | { | |
62d1db17 | 18037 | decl = shadow_tag (&decl_specifiers); |
b939a023 KL |
18038 | |
18039 | /* In this case: | |
18040 | ||
18041 | struct C { | |
18042 | friend template <typename T> struct A<T>::B; | |
18043 | }; | |
18044 | ||
18045 | A<T>::B will be represented by a TYPENAME_TYPE, and | |
18046 | therefore not recognized by shadow_tag. */ | |
18047 | if (friend_p && *friend_p | |
18048 | && !decl | |
18049 | && decl_specifiers.type | |
18050 | && TYPE_P (decl_specifiers.type)) | |
18051 | decl = decl_specifiers.type; | |
18052 | ||
62d1db17 | 18053 | if (decl && decl != error_mark_node) |
a723baf1 MM |
18054 | decl = TYPE_NAME (decl); |
18055 | else | |
18056 | decl = error_mark_node; | |
6b648482 MM |
18057 | |
18058 | /* Perform access checks for template parameters. */ | |
18059 | cp_parser_perform_template_parameter_access_checks (checks); | |
a723baf1 MM |
18060 | } |
18061 | } | |
a723baf1 MM |
18062 | /* If it's not a template class, try for a template function. If |
18063 | the next token is a `;', then this declaration does not declare | |
18064 | anything. But, if there were errors in the decl-specifiers, then | |
18065 | the error might well have come from an attempted class-specifier. | |
18066 | In that case, there's no need to warn about a missing declarator. */ | |
18067 | if (!decl | |
18068 | && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON) | |
62d1db17 | 18069 | || decl_specifiers.type != error_mark_node)) |
736b8100 SB |
18070 | { |
18071 | decl = cp_parser_init_declarator (parser, | |
18072 | &decl_specifiers, | |
18073 | checks, | |
18074 | /*function_definition_allowed_p=*/true, | |
18075 | member_p, | |
18076 | declares_class_or_enum, | |
18077 | &function_definition_p); | |
18078 | ||
18079 | /* 7.1.1-1 [dcl.stc] | |
18080 | ||
18081 | A storage-class-specifier shall not be specified in an explicit | |
18082 | specialization... */ | |
18083 | if (decl | |
18084 | && explicit_specialization_p | |
18085 | && decl_specifiers.storage_class != sc_none) | |
18086 | { | |
69bc6bff MLI |
18087 | error_at (decl_spec_token_start->location, |
18088 | "explicit template specialization cannot have a storage class"); | |
736b8100 SB |
18089 | decl = error_mark_node; |
18090 | } | |
18091 | } | |
cf22909c KL |
18092 | |
18093 | pop_deferring_access_checks (); | |
18094 | ||
a723baf1 MM |
18095 | /* Clear any current qualification; whatever comes next is the start |
18096 | of something new. */ | |
18097 | parser->scope = NULL_TREE; | |
18098 | parser->qualifying_scope = NULL_TREE; | |
18099 | parser->object_scope = NULL_TREE; | |
18100 | /* Look for a trailing `;' after the declaration. */ | |
4bb8ca28 | 18101 | if (!function_definition_p |
71bd7186 | 18102 | && (decl == error_mark_node |
3a483371 | 18103 | || !cp_parser_require (parser, CPP_SEMICOLON, "%<;%>"))) |
a723baf1 | 18104 | cp_parser_skip_to_end_of_block_or_statement (parser); |
a723baf1 MM |
18105 | |
18106 | return decl; | |
18107 | } | |
18108 | ||
d6b4ea85 MM |
18109 | /* Parse a cast-expression that is not the operand of a unary "&". */ |
18110 | ||
18111 | static tree | |
18112 | cp_parser_simple_cast_expression (cp_parser *parser) | |
18113 | { | |
93678513 | 18114 | return cp_parser_cast_expression (parser, /*address_p=*/false, |
cee9035d | 18115 | /*cast_p=*/false, NULL); |
d6b4ea85 MM |
18116 | } |
18117 | ||
a723baf1 MM |
18118 | /* Parse a functional cast to TYPE. Returns an expression |
18119 | representing the cast. */ | |
18120 | ||
18121 | static tree | |
94edc4ab | 18122 | cp_parser_functional_cast (cp_parser* parser, tree type) |
a723baf1 | 18123 | { |
c166b898 | 18124 | VEC(tree,gc) *vec; |
a723baf1 | 18125 | tree expression_list; |
d36d5600 | 18126 | tree cast; |
09357846 JM |
18127 | bool nonconst_p; |
18128 | ||
18129 | if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)) | |
18130 | { | |
18131 | maybe_warn_cpp0x ("extended initializer lists"); | |
18132 | expression_list = cp_parser_braced_list (parser, &nonconst_p); | |
18133 | CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1; | |
18134 | if (TREE_CODE (type) == TYPE_DECL) | |
18135 | type = TREE_TYPE (type); | |
18136 | return finish_compound_literal (type, expression_list); | |
18137 | } | |
a723baf1 | 18138 | |
c166b898 ILT |
18139 | |
18140 | vec = cp_parser_parenthesized_expression_list (parser, false, | |
18141 | /*cast_p=*/true, | |
18142 | /*allow_expansion_p=*/true, | |
18143 | /*non_constant_p=*/NULL); | |
18144 | if (vec == NULL) | |
18145 | expression_list = error_mark_node; | |
18146 | else | |
18147 | { | |
18148 | expression_list = build_tree_list_vec (vec); | |
18149 | release_tree_vector (vec); | |
18150 | } | |
a723baf1 | 18151 | |
5ade1ed2 DG |
18152 | cast = build_functional_cast (type, expression_list, |
18153 | tf_warning_or_error); | |
d36d5600 GB |
18154 | /* [expr.const]/1: In an integral constant expression "only type |
18155 | conversions to integral or enumeration type can be used". */ | |
3ce5fa4f NS |
18156 | if (TREE_CODE (type) == TYPE_DECL) |
18157 | type = TREE_TYPE (type); | |
015c2c66 MM |
18158 | if (cast != error_mark_node |
18159 | && !cast_valid_in_integral_constant_expression_p (type) | |
18160 | && (cp_parser_non_integral_constant_expression | |
18161 | (parser, "a call to a constructor"))) | |
18162 | return error_mark_node; | |
d36d5600 | 18163 | return cast; |
a723baf1 MM |
18164 | } |
18165 | ||
4bb8ca28 MM |
18166 | /* Save the tokens that make up the body of a member function defined |
18167 | in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have | |
18168 | already been parsed. The ATTRIBUTES are any GNU "__attribute__" | |
18169 | specifiers applied to the declaration. Returns the FUNCTION_DECL | |
18170 | for the member function. */ | |
18171 | ||
7ce27103 | 18172 | static tree |
4bb8ca28 | 18173 | cp_parser_save_member_function_body (cp_parser* parser, |
62d1db17 | 18174 | cp_decl_specifier_seq *decl_specifiers, |
058b15c1 | 18175 | cp_declarator *declarator, |
4bb8ca28 MM |
18176 | tree attributes) |
18177 | { | |
c162c75e MA |
18178 | cp_token *first; |
18179 | cp_token *last; | |
4bb8ca28 MM |
18180 | tree fn; |
18181 | ||
18182 | /* Create the function-declaration. */ | |
18183 | fn = start_method (decl_specifiers, declarator, attributes); | |
18184 | /* If something went badly wrong, bail out now. */ | |
18185 | if (fn == error_mark_node) | |
18186 | { | |
18187 | /* If there's a function-body, skip it. */ | |
21526606 | 18188 | if (cp_parser_token_starts_function_definition_p |
4bb8ca28 MM |
18189 | (cp_lexer_peek_token (parser->lexer))) |
18190 | cp_parser_skip_to_end_of_block_or_statement (parser); | |
18191 | return error_mark_node; | |
18192 | } | |
18193 | ||
18194 | /* Remember it, if there default args to post process. */ | |
18195 | cp_parser_save_default_args (parser, fn); | |
18196 | ||
21526606 | 18197 | /* Save away the tokens that make up the body of the |
4bb8ca28 | 18198 | function. */ |
c162c75e | 18199 | first = parser->lexer->next_token; |
09357846 JM |
18200 | /* We can have braced-init-list mem-initializers before the fn body. */ |
18201 | if (cp_lexer_next_token_is (parser->lexer, CPP_COLON)) | |
18202 | { | |
18203 | cp_lexer_consume_token (parser->lexer); | |
18204 | while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE) | |
18205 | && cp_lexer_next_token_is_not_keyword (parser->lexer, RID_TRY)) | |
18206 | { | |
18207 | /* cache_group will stop after an un-nested { } pair, too. */ | |
18208 | if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0)) | |
18209 | break; | |
18210 | ||
18211 | /* variadic mem-inits have ... after the ')'. */ | |
18212 | if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)) | |
18213 | cp_lexer_consume_token (parser->lexer); | |
18214 | } | |
18215 | } | |
c162c75e | 18216 | cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0); |
4bb8ca28 MM |
18217 | /* Handle function try blocks. */ |
18218 | while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH)) | |
c162c75e MA |
18219 | cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0); |
18220 | last = parser->lexer->next_token; | |
4bb8ca28 MM |
18221 | |
18222 | /* Save away the inline definition; we will process it when the | |
18223 | class is complete. */ | |
c162c75e | 18224 | DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last); |
4bb8ca28 MM |
18225 | DECL_PENDING_INLINE_P (fn) = 1; |
18226 | ||
18227 | /* We need to know that this was defined in the class, so that | |
18228 | friend templates are handled correctly. */ | |
18229 | DECL_INITIALIZED_IN_CLASS_P (fn) = 1; | |
18230 | ||
18231 | /* We're done with the inline definition. */ | |
18232 | finish_method (fn); | |
18233 | ||
18234 | /* Add FN to the queue of functions to be parsed later. */ | |
18235 | TREE_VALUE (parser->unparsed_functions_queues) | |
21526606 | 18236 | = tree_cons (NULL_TREE, fn, |
4bb8ca28 MM |
18237 | TREE_VALUE (parser->unparsed_functions_queues)); |
18238 | ||
18239 | return fn; | |
18240 | } | |
18241 | ||
ec75414f MM |
18242 | /* Parse a template-argument-list, as well as the trailing ">" (but |
18243 | not the opening ">"). See cp_parser_template_argument_list for the | |
18244 | return value. */ | |
18245 | ||
18246 | static tree | |
18247 | cp_parser_enclosed_template_argument_list (cp_parser* parser) | |
18248 | { | |
18249 | tree arguments; | |
18250 | tree saved_scope; | |
18251 | tree saved_qualifying_scope; | |
18252 | tree saved_object_scope; | |
18253 | bool saved_greater_than_is_operator_p; | |
7d882b83 ILT |
18254 | int saved_unevaluated_operand; |
18255 | int saved_inhibit_evaluation_warnings; | |
ec75414f MM |
18256 | |
18257 | /* [temp.names] | |
18258 | ||
18259 | When parsing a template-id, the first non-nested `>' is taken as | |
18260 | the end of the template-argument-list rather than a greater-than | |
18261 | operator. */ | |
21526606 | 18262 | saved_greater_than_is_operator_p |
ec75414f MM |
18263 | = parser->greater_than_is_operator_p; |
18264 | parser->greater_than_is_operator_p = false; | |
18265 | /* Parsing the argument list may modify SCOPE, so we save it | |
18266 | here. */ | |
18267 | saved_scope = parser->scope; | |
18268 | saved_qualifying_scope = parser->qualifying_scope; | |
18269 | saved_object_scope = parser->object_scope; | |
49f210a2 MM |
18270 | /* We need to evaluate the template arguments, even though this |
18271 | template-id may be nested within a "sizeof". */ | |
7d882b83 ILT |
18272 | saved_unevaluated_operand = cp_unevaluated_operand; |
18273 | cp_unevaluated_operand = 0; | |
18274 | saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings; | |
18275 | c_inhibit_evaluation_warnings = 0; | |
ec75414f | 18276 | /* Parse the template-argument-list itself. */ |
a2ca1ab2 DG |
18277 | if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER) |
18278 | || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT)) | |
ec75414f MM |
18279 | arguments = NULL_TREE; |
18280 | else | |
18281 | arguments = cp_parser_template_argument_list (parser); | |
4d5297fa GB |
18282 | /* Look for the `>' that ends the template-argument-list. If we find |
18283 | a '>>' instead, it's probably just a typo. */ | |
18284 | if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT)) | |
18285 | { | |
c1ae8be5 | 18286 | if (cxx_dialect != cxx98) |
a2ca1ab2 DG |
18287 | { |
18288 | /* In C++0x, a `>>' in a template argument list or cast | |
18289 | expression is considered to be two separate `>' | |
18290 | tokens. So, change the current token to a `>', but don't | |
18291 | consume it: it will be consumed later when the outer | |
18292 | template argument list (or cast expression) is parsed. | |
18293 | Note that this replacement of `>' for `>>' is necessary | |
18294 | even if we are parsing tentatively: in the tentative | |
18295 | case, after calling | |
18296 | cp_parser_enclosed_template_argument_list we will always | |
18297 | throw away all of the template arguments and the first | |
18298 | closing `>', either because the template argument list | |
18299 | was erroneous or because we are replacing those tokens | |
18300 | with a CPP_TEMPLATE_ID token. The second `>' (which will | |
18301 | not have been thrown away) is needed either to close an | |
18302 | outer template argument list or to complete a new-style | |
18303 | cast. */ | |
18304 | cp_token *token = cp_lexer_peek_token (parser->lexer); | |
18305 | token->type = CPP_GREATER; | |
18306 | } | |
18307 | else if (!saved_greater_than_is_operator_p) | |
4d5297fa | 18308 | { |
2cfe82fe ZW |
18309 | /* If we're in a nested template argument list, the '>>' has |
18310 | to be a typo for '> >'. We emit the error message, but we | |
18311 | continue parsing and we push a '>' as next token, so that | |
18312 | the argument list will be parsed correctly. Note that the | |
18313 | global source location is still on the token before the | |
18314 | '>>', so we need to say explicitly where we want it. */ | |
18315 | cp_token *token = cp_lexer_peek_token (parser->lexer); | |
69bc6bff MLI |
18316 | error_at (token->location, "%<>>%> should be %<> >%> " |
18317 | "within a nested template argument list"); | |
2cfe82fe | 18318 | |
4d5297fa GB |
18319 | token->type = CPP_GREATER; |
18320 | } | |
18321 | else | |
18322 | { | |
2cfe82fe ZW |
18323 | /* If this is not a nested template argument list, the '>>' |
18324 | is a typo for '>'. Emit an error message and continue. | |
18325 | Same deal about the token location, but here we can get it | |
18326 | right by consuming the '>>' before issuing the diagnostic. */ | |
2b7a3abf | 18327 | cp_token *token = cp_lexer_consume_token (parser->lexer); |
69bc6bff MLI |
18328 | error_at (token->location, |
18329 | "spurious %<>>%>, use %<>%> to terminate " | |
18330 | "a template argument list"); | |
4d5297fa GB |
18331 | } |
18332 | } | |
2cfe82fe | 18333 | else |
a7b9d08c | 18334 | cp_parser_skip_to_end_of_template_parameter_list (parser); |
ec75414f | 18335 | /* The `>' token might be a greater-than operator again now. */ |
21526606 | 18336 | parser->greater_than_is_operator_p |
ec75414f MM |
18337 | = saved_greater_than_is_operator_p; |
18338 | /* Restore the SAVED_SCOPE. */ | |
18339 | parser->scope = saved_scope; | |
18340 | parser->qualifying_scope = saved_qualifying_scope; | |
18341 | parser->object_scope = saved_object_scope; | |
7d882b83 ILT |
18342 | cp_unevaluated_operand = saved_unevaluated_operand; |
18343 | c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings; | |
ec75414f MM |
18344 | |
18345 | return arguments; | |
18346 | } | |
18347 | ||
a723baf1 MM |
18348 | /* MEMBER_FUNCTION is a member function, or a friend. If default |
18349 | arguments, or the body of the function have not yet been parsed, | |
18350 | parse them now. */ | |
18351 | ||
18352 | static void | |
94edc4ab | 18353 | cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function) |
a723baf1 | 18354 | { |
a723baf1 MM |
18355 | /* If this member is a template, get the underlying |
18356 | FUNCTION_DECL. */ | |
18357 | if (DECL_FUNCTION_TEMPLATE_P (member_function)) | |
18358 | member_function = DECL_TEMPLATE_RESULT (member_function); | |
18359 | ||
18360 | /* There should not be any class definitions in progress at this | |
18361 | point; the bodies of members are only parsed outside of all class | |
18362 | definitions. */ | |
50bc768d | 18363 | gcc_assert (parser->num_classes_being_defined == 0); |
a723baf1 MM |
18364 | /* While we're parsing the member functions we might encounter more |
18365 | classes. We want to handle them right away, but we don't want | |
18366 | them getting mixed up with functions that are currently in the | |
18367 | queue. */ | |
18368 | parser->unparsed_functions_queues | |
18369 | = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues); | |
18370 | ||
18371 | /* Make sure that any template parameters are in scope. */ | |
18372 | maybe_begin_member_template_processing (member_function); | |
18373 | ||
a723baf1 MM |
18374 | /* If the body of the function has not yet been parsed, parse it |
18375 | now. */ | |
18376 | if (DECL_PENDING_INLINE_P (member_function)) | |
18377 | { | |
18378 | tree function_scope; | |
18379 | cp_token_cache *tokens; | |
18380 | ||
18381 | /* The function is no longer pending; we are processing it. */ | |
18382 | tokens = DECL_PENDING_INLINE_INFO (member_function); | |
18383 | DECL_PENDING_INLINE_INFO (member_function) = NULL; | |
18384 | DECL_PENDING_INLINE_P (member_function) = 0; | |
c8094d83 | 18385 | |
f769035f NS |
18386 | /* If this is a local class, enter the scope of the containing |
18387 | function. */ | |
18388 | function_scope = current_function_decl; | |
a723baf1 | 18389 | if (function_scope) |
d2784db4 | 18390 | push_function_context (); |
c8094d83 | 18391 | |
2cfe82fe ZW |
18392 | /* Push the body of the function onto the lexer stack. */ |
18393 | cp_parser_push_lexer_for_tokens (parser, tokens); | |
21526606 | 18394 | |
a723baf1 MM |
18395 | /* Let the front end know that we going to be defining this |
18396 | function. */ | |
058b15c1 MM |
18397 | start_preparsed_function (member_function, NULL_TREE, |
18398 | SF_PRE_PARSED | SF_INCLASS_INLINE); | |
21526606 | 18399 | |
2d637547 NS |
18400 | /* Don't do access checking if it is a templated function. */ |
18401 | if (processing_template_decl) | |
18402 | push_deferring_access_checks (dk_no_check); | |
c8094d83 | 18403 | |
a723baf1 MM |
18404 | /* Now, parse the body of the function. */ |
18405 | cp_parser_function_definition_after_declarator (parser, | |
18406 | /*inline_p=*/true); | |
21526606 | 18407 | |
2d637547 NS |
18408 | if (processing_template_decl) |
18409 | pop_deferring_access_checks (); | |
c8094d83 | 18410 | |
a723baf1 MM |
18411 | /* Leave the scope of the containing function. */ |
18412 | if (function_scope) | |
d2784db4 | 18413 | pop_function_context (); |
2cfe82fe | 18414 | cp_parser_pop_lexer (parser); |
a723baf1 MM |
18415 | } |
18416 | ||
18417 | /* Remove any template parameters from the symbol table. */ | |
18418 | maybe_end_member_template_processing (); | |
18419 | ||
18420 | /* Restore the queue. */ | |
21526606 | 18421 | parser->unparsed_functions_queues |
a723baf1 MM |
18422 | = TREE_CHAIN (parser->unparsed_functions_queues); |
18423 | } | |
18424 | ||
cd0be382 | 18425 | /* If DECL contains any default args, remember it on the unparsed |
8db1028e NS |
18426 | functions queue. */ |
18427 | ||
18428 | static void | |
18429 | cp_parser_save_default_args (cp_parser* parser, tree decl) | |
18430 | { | |
18431 | tree probe; | |
18432 | ||
18433 | for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl)); | |
18434 | probe; | |
18435 | probe = TREE_CHAIN (probe)) | |
18436 | if (TREE_PURPOSE (probe)) | |
18437 | { | |
18438 | TREE_PURPOSE (parser->unparsed_functions_queues) | |
f44b0c8e | 18439 | = tree_cons (current_class_type, decl, |
8db1028e NS |
18440 | TREE_PURPOSE (parser->unparsed_functions_queues)); |
18441 | break; | |
18442 | } | |
8db1028e NS |
18443 | } |
18444 | ||
8218bd34 | 18445 | /* FN is a FUNCTION_DECL which may contains a parameter with an |
f44b0c8e MM |
18446 | unparsed DEFAULT_ARG. Parse the default args now. This function |
18447 | assumes that the current scope is the scope in which the default | |
18448 | argument should be processed. */ | |
a723baf1 MM |
18449 | |
18450 | static void | |
8218bd34 | 18451 | cp_parser_late_parsing_default_args (cp_parser *parser, tree fn) |
a723baf1 | 18452 | { |
a723baf1 | 18453 | bool saved_local_variables_forbidden_p; |
2cfe82fe | 18454 | tree parm; |
8218bd34 | 18455 | |
b92bc2a0 NS |
18456 | /* While we're parsing the default args, we might (due to the |
18457 | statement expression extension) encounter more classes. We want | |
18458 | to handle them right away, but we don't want them getting mixed | |
18459 | up with default args that are currently in the queue. */ | |
18460 | parser->unparsed_functions_queues | |
18461 | = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues); | |
18462 | ||
2cfe82fe ZW |
18463 | /* Local variable names (and the `this' keyword) may not appear |
18464 | in a default argument. */ | |
18465 | saved_local_variables_forbidden_p = parser->local_variables_forbidden_p; | |
18466 | parser->local_variables_forbidden_p = true; | |
18467 | ||
18468 | for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)); | |
18469 | parm; | |
18470 | parm = TREE_CHAIN (parm)) | |
a723baf1 | 18471 | { |
2cfe82fe | 18472 | cp_token_cache *tokens; |
5e97d404 NS |
18473 | tree default_arg = TREE_PURPOSE (parm); |
18474 | tree parsed_arg; | |
01ea1ea8 NS |
18475 | VEC(tree,gc) *insts; |
18476 | tree copy; | |
18477 | unsigned ix; | |
c8094d83 | 18478 | |
5e97d404 | 18479 | if (!default_arg) |
2cfe82fe | 18480 | continue; |
a723baf1 | 18481 | |
efb169b0 NS |
18482 | if (TREE_CODE (default_arg) != DEFAULT_ARG) |
18483 | /* This can happen for a friend declaration for a function | |
18484 | already declared with default arguments. */ | |
18485 | continue; | |
5e97d404 | 18486 | |
2cfe82fe ZW |
18487 | /* Push the saved tokens for the default argument onto the parser's |
18488 | lexer stack. */ | |
5e97d404 | 18489 | tokens = DEFARG_TOKENS (default_arg); |
2cfe82fe | 18490 | cp_parser_push_lexer_for_tokens (parser, tokens); |
a723baf1 | 18491 | |
2cfe82fe | 18492 | /* Parse the assignment-expression. */ |
cee9035d | 18493 | parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL); |
ae9b2a48 L |
18494 | if (parsed_arg == error_mark_node) |
18495 | { | |
18496 | cp_parser_pop_lexer (parser); | |
18497 | continue; | |
18498 | } | |
5e97d404 | 18499 | |
c3ee4651 NS |
18500 | if (!processing_template_decl) |
18501 | parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg); | |
3db45ab5 | 18502 | |
5e97d404 NS |
18503 | TREE_PURPOSE (parm) = parsed_arg; |
18504 | ||
18505 | /* Update any instantiations we've already created. */ | |
01ea1ea8 NS |
18506 | for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0; |
18507 | VEC_iterate (tree, insts, ix, copy); ix++) | |
18508 | TREE_PURPOSE (copy) = parsed_arg; | |
a723baf1 | 18509 | |
676e33ca MM |
18510 | /* If the token stream has not been completely used up, then |
18511 | there was extra junk after the end of the default | |
18512 | argument. */ | |
18513 | if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF)) | |
2a13a625 | 18514 | cp_parser_error (parser, "expected %<,%>"); |
676e33ca | 18515 | |
2cfe82fe ZW |
18516 | /* Revert to the main lexer. */ |
18517 | cp_parser_pop_lexer (parser); | |
a723baf1 | 18518 | } |
b92bc2a0 | 18519 | |
607c855e VR |
18520 | /* Make sure no default arg is missing. */ |
18521 | check_default_args (fn); | |
18522 | ||
2cfe82fe ZW |
18523 | /* Restore the state of local_variables_forbidden_p. */ |
18524 | parser->local_variables_forbidden_p = saved_local_variables_forbidden_p; | |
18525 | ||
b92bc2a0 | 18526 | /* Restore the queue. */ |
21526606 | 18527 | parser->unparsed_functions_queues |
b92bc2a0 | 18528 | = TREE_CHAIN (parser->unparsed_functions_queues); |
a723baf1 MM |
18529 | } |
18530 | ||
18531 | /* Parse the operand of `sizeof' (or a similar operator). Returns | |
18532 | either a TYPE or an expression, depending on the form of the | |
18533 | input. The KEYWORD indicates which kind of expression we have | |
18534 | encountered. */ | |
18535 | ||
18536 | static tree | |
94edc4ab | 18537 | cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword) |
a723baf1 | 18538 | { |
a723baf1 MM |
18539 | tree expr = NULL_TREE; |
18540 | const char *saved_message; | |
e5cfc29f | 18541 | char *tmp; |
67c03833 | 18542 | bool saved_integral_constant_expression_p; |
93678513 | 18543 | bool saved_non_integral_constant_expression_p; |
5d80a306 | 18544 | bool pack_expansion_p = false; |
a723baf1 | 18545 | |
a723baf1 MM |
18546 | /* Types cannot be defined in a `sizeof' expression. Save away the |
18547 | old message. */ | |
18548 | saved_message = parser->type_definition_forbidden_message; | |
18549 | /* And create the new one. */ | |
8b4efb4e VR |
18550 | tmp = concat ("types may not be defined in %<", |
18551 | IDENTIFIER_POINTER (ridpointers[keyword]), | |
18552 | "%> expressions", NULL); | |
18553 | parser->type_definition_forbidden_message = tmp; | |
a723baf1 MM |
18554 | |
18555 | /* The restrictions on constant-expressions do not apply inside | |
18556 | sizeof expressions. */ | |
c8094d83 | 18557 | saved_integral_constant_expression_p |
93678513 MM |
18558 | = parser->integral_constant_expression_p; |
18559 | saved_non_integral_constant_expression_p | |
18560 | = parser->non_integral_constant_expression_p; | |
67c03833 | 18561 | parser->integral_constant_expression_p = false; |
a723baf1 | 18562 | |
5d80a306 DG |
18563 | /* If it's a `...', then we are computing the length of a parameter |
18564 | pack. */ | |
18565 | if (keyword == RID_SIZEOF | |
18566 | && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)) | |
18567 | { | |
18568 | /* Consume the `...'. */ | |
18569 | cp_lexer_consume_token (parser->lexer); | |
18570 | maybe_warn_variadic_templates (); | |
18571 | ||
18572 | /* Note that this is an expansion. */ | |
18573 | pack_expansion_p = true; | |
18574 | } | |
18575 | ||
3beb3abf | 18576 | /* Do not actually evaluate the expression. */ |
7d882b83 ILT |
18577 | ++cp_unevaluated_operand; |
18578 | ++c_inhibit_evaluation_warnings; | |
a723baf1 MM |
18579 | /* If it's a `(', then we might be looking at the type-id |
18580 | construction. */ | |
18581 | if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)) | |
18582 | { | |
18583 | tree type; | |
4f8163b1 | 18584 | bool saved_in_type_id_in_expr_p; |
a723baf1 MM |
18585 | |
18586 | /* We can't be sure yet whether we're looking at a type-id or an | |
18587 | expression. */ | |
18588 | cp_parser_parse_tentatively (parser); | |
18589 | /* Consume the `('. */ | |
18590 | cp_lexer_consume_token (parser->lexer); | |
18591 | /* Parse the type-id. */ | |
4f8163b1 MM |
18592 | saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p; |
18593 | parser->in_type_id_in_expr_p = true; | |
a723baf1 | 18594 | type = cp_parser_type_id (parser); |
4f8163b1 | 18595 | parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p; |
a723baf1 | 18596 | /* Now, look for the trailing `)'. */ |
9e637a26 | 18597 | cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"); |
a723baf1 MM |
18598 | /* If all went well, then we're done. */ |
18599 | if (cp_parser_parse_definitely (parser)) | |
18600 | { | |
62d1db17 MM |
18601 | cp_decl_specifier_seq decl_specs; |
18602 | ||
18603 | /* Build a trivial decl-specifier-seq. */ | |
18604 | clear_decl_specs (&decl_specs); | |
18605 | decl_specs.type = type; | |
a723baf1 MM |
18606 | |
18607 | /* Call grokdeclarator to figure out what type this is. */ | |
058b15c1 | 18608 | expr = grokdeclarator (NULL, |
62d1db17 | 18609 | &decl_specs, |
a723baf1 MM |
18610 | TYPENAME, |
18611 | /*initialized=*/0, | |
18612 | /*attrlist=*/NULL); | |
18613 | } | |
18614 | } | |
18615 | ||
18616 | /* If the type-id production did not work out, then we must be | |
18617 | looking at the unary-expression production. */ | |
18618 | if (!expr) | |
93678513 | 18619 | expr = cp_parser_unary_expression (parser, /*address_p=*/false, |
cee9035d | 18620 | /*cast_p=*/false, NULL); |
5d80a306 DG |
18621 | |
18622 | if (pack_expansion_p) | |
18623 | /* Build a pack expansion. */ | |
18624 | expr = make_pack_expansion (expr); | |
18625 | ||
3beb3abf | 18626 | /* Go back to evaluating expressions. */ |
7d882b83 ILT |
18627 | --cp_unevaluated_operand; |
18628 | --c_inhibit_evaluation_warnings; | |
a723baf1 MM |
18629 | |
18630 | /* Free the message we created. */ | |
e5cfc29f | 18631 | free (tmp); |
a723baf1 MM |
18632 | /* And restore the old one. */ |
18633 | parser->type_definition_forbidden_message = saved_message; | |
c8094d83 | 18634 | parser->integral_constant_expression_p |
93678513 MM |
18635 | = saved_integral_constant_expression_p; |
18636 | parser->non_integral_constant_expression_p | |
18637 | = saved_non_integral_constant_expression_p; | |
a723baf1 MM |
18638 | |
18639 | return expr; | |
18640 | } | |
18641 | ||
18642 | /* If the current declaration has no declarator, return true. */ | |
18643 | ||
18644 | static bool | |
18645 | cp_parser_declares_only_class_p (cp_parser *parser) | |
18646 | { | |
21526606 | 18647 | /* If the next token is a `;' or a `,' then there is no |
a723baf1 MM |
18648 | declarator. */ |
18649 | return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON) | |
18650 | || cp_lexer_next_token_is (parser->lexer, CPP_COMMA)); | |
18651 | } | |
18652 | ||
bfce0853 MM |
18653 | /* Update the DECL_SPECS to reflect the storage class indicated by |
18654 | KEYWORD. */ | |
a723baf1 | 18655 | |
62d1db17 | 18656 | static void |
bfce0853 MM |
18657 | cp_parser_set_storage_class (cp_parser *parser, |
18658 | cp_decl_specifier_seq *decl_specs, | |
2b7a3abf DS |
18659 | enum rid keyword, |
18660 | location_t location) | |
a723baf1 | 18661 | { |
bfce0853 MM |
18662 | cp_storage_class storage_class; |
18663 | ||
18664 | if (parser->in_unbraced_linkage_specification_p) | |
18665 | { | |
69bc6bff MLI |
18666 | error_at (location, "invalid use of %qD in linkage specification", |
18667 | ridpointers[keyword]); | |
bfce0853 MM |
18668 | return; |
18669 | } | |
18670 | else if (decl_specs->storage_class != sc_none) | |
18671 | { | |
26106542 | 18672 | decl_specs->conflicting_specifiers_p = true; |
bfce0853 MM |
18673 | return; |
18674 | } | |
18675 | ||
18676 | if ((keyword == RID_EXTERN || keyword == RID_STATIC) | |
18677 | && decl_specs->specs[(int) ds_thread]) | |
18678 | { | |
69bc6bff | 18679 | error_at (location, "%<__thread%> before %qD", ridpointers[keyword]); |
bfce0853 MM |
18680 | decl_specs->specs[(int) ds_thread] = 0; |
18681 | } | |
18682 | ||
3db45ab5 | 18683 | switch (keyword) |
bfce0853 MM |
18684 | { |
18685 | case RID_AUTO: | |
18686 | storage_class = sc_auto; | |
18687 | break; | |
18688 | case RID_REGISTER: | |
18689 | storage_class = sc_register; | |
18690 | break; | |
18691 | case RID_STATIC: | |
18692 | storage_class = sc_static; | |
18693 | break; | |
18694 | case RID_EXTERN: | |
18695 | storage_class = sc_extern; | |
18696 | break; | |
18697 | case RID_MUTABLE: | |
18698 | storage_class = sc_mutable; | |
18699 | break; | |
18700 | default: | |
18701 | gcc_unreachable (); | |
18702 | } | |
18703 | decl_specs->storage_class = storage_class; | |
26106542 LM |
18704 | |
18705 | /* A storage class specifier cannot be applied alongside a typedef | |
18706 | specifier. If there is a typedef specifier present then set | |
18707 | conflicting_specifiers_p which will trigger an error later | |
18708 | on in grokdeclarator. */ | |
18709 | if (decl_specs->specs[(int)ds_typedef]) | |
18710 | decl_specs->conflicting_specifiers_p = true; | |
62d1db17 MM |
18711 | } |
18712 | ||
18713 | /* Update the DECL_SPECS to reflect the TYPE_SPEC. If USER_DEFINED_P | |
18714 | is true, the type is a user-defined type; otherwise it is a | |
18715 | built-in type specified by a keyword. */ | |
a723baf1 | 18716 | |
62d1db17 MM |
18717 | static void |
18718 | cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs, | |
18719 | tree type_spec, | |
760b18ad | 18720 | location_t location, |
62d1db17 MM |
18721 | bool user_defined_p) |
18722 | { | |
18723 | decl_specs->any_specifiers_p = true; | |
98ca843c | 18724 | |
b6baa67d KVH |
18725 | /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t |
18726 | (with, for example, in "typedef int wchar_t;") we remember that | |
18727 | this is what happened. In system headers, we ignore these | |
18728 | declarations so that G++ can work with system headers that are not | |
18729 | C++-safe. */ | |
98ca843c | 18730 | if (decl_specs->specs[(int) ds_typedef] |
f84b6c96 | 18731 | && !user_defined_p |
9306cccb | 18732 | && (type_spec == boolean_type_node |
b6baa67d KVH |
18733 | || type_spec == char16_type_node |
18734 | || type_spec == char32_type_node | |
9306cccb | 18735 | || type_spec == wchar_type_node) |
f84b6c96 MM |
18736 | && (decl_specs->type |
18737 | || decl_specs->specs[(int) ds_long] | |
18738 | || decl_specs->specs[(int) ds_short] | |
18739 | || decl_specs->specs[(int) ds_unsigned] | |
18740 | || decl_specs->specs[(int) ds_signed])) | |
0a73e37f MM |
18741 | { |
18742 | decl_specs->redefined_builtin_type = type_spec; | |
18743 | if (!decl_specs->type) | |
18744 | { | |
18745 | decl_specs->type = type_spec; | |
18746 | decl_specs->user_defined_type_p = false; | |
760b18ad | 18747 | decl_specs->type_location = location; |
0a73e37f MM |
18748 | } |
18749 | } | |
f84b6c96 MM |
18750 | else if (decl_specs->type) |
18751 | decl_specs->multiple_types_p = true; | |
62d1db17 MM |
18752 | else |
18753 | { | |
18754 | decl_specs->type = type_spec; | |
18755 | decl_specs->user_defined_type_p = user_defined_p; | |
0a73e37f | 18756 | decl_specs->redefined_builtin_type = NULL_TREE; |
760b18ad | 18757 | decl_specs->type_location = location; |
a723baf1 | 18758 | } |
62d1db17 | 18759 | } |
a723baf1 | 18760 | |
62d1db17 MM |
18761 | /* DECL_SPECIFIERS is the representation of a decl-specifier-seq. |
18762 | Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */ | |
18763 | ||
18764 | static bool | |
18765 | cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers) | |
18766 | { | |
18767 | return decl_specifiers->specs[(int) ds_friend] != 0; | |
a723baf1 MM |
18768 | } |
18769 | ||
18770 | /* If the next token is of the indicated TYPE, consume it. Otherwise, | |
18771 | issue an error message indicating that TOKEN_DESC was expected. | |
21526606 | 18772 | |
a723baf1 MM |
18773 | Returns the token consumed, if the token had the appropriate type. |
18774 | Otherwise, returns NULL. */ | |
18775 | ||
18776 | static cp_token * | |
94edc4ab | 18777 | cp_parser_require (cp_parser* parser, |
0cbd7506 MS |
18778 | enum cpp_ttype type, |
18779 | const char* token_desc) | |
a723baf1 MM |
18780 | { |
18781 | if (cp_lexer_next_token_is (parser->lexer, type)) | |
18782 | return cp_lexer_consume_token (parser->lexer); | |
18783 | else | |
18784 | { | |
e5976695 MM |
18785 | /* Output the MESSAGE -- unless we're parsing tentatively. */ |
18786 | if (!cp_parser_simulate_error (parser)) | |
216bb6e1 MM |
18787 | { |
18788 | char *message = concat ("expected ", token_desc, NULL); | |
18789 | cp_parser_error (parser, message); | |
18790 | free (message); | |
18791 | } | |
a723baf1 MM |
18792 | return NULL; |
18793 | } | |
18794 | } | |
18795 | ||
a7b9d08c VR |
18796 | /* An error message is produced if the next token is not '>'. |
18797 | All further tokens are skipped until the desired token is | |
18798 | found or '{', '}', ';' or an unbalanced ')' or ']'. */ | |
a723baf1 MM |
18799 | |
18800 | static void | |
a7b9d08c | 18801 | cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser) |
a723baf1 | 18802 | { |
a7b9d08c VR |
18803 | /* Current level of '< ... >'. */ |
18804 | unsigned level = 0; | |
18805 | /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */ | |
a723baf1 MM |
18806 | unsigned nesting_depth = 0; |
18807 | ||
a7b9d08c VR |
18808 | /* Are we ready, yet? If not, issue error message. */ |
18809 | if (cp_parser_require (parser, CPP_GREATER, "%<>%>")) | |
a723baf1 MM |
18810 | return; |
18811 | ||
18812 | /* Skip tokens until the desired token is found. */ | |
18813 | while (true) | |
18814 | { | |
18815 | /* Peek at the next token. */ | |
a7b9d08c | 18816 | switch (cp_lexer_peek_token (parser->lexer)->type) |
a723baf1 | 18817 | { |
a7b9d08c VR |
18818 | case CPP_LESS: |
18819 | if (!nesting_depth) | |
18820 | ++level; | |
18821 | break; | |
bc4071dd | 18822 | |
a2ca1ab2 | 18823 | case CPP_RSHIFT: |
c1ae8be5 | 18824 | if (cxx_dialect == cxx98) |
a2ca1ab2 DG |
18825 | /* C++0x views the `>>' operator as two `>' tokens, but |
18826 | C++98 does not. */ | |
18827 | break; | |
18828 | else if (!nesting_depth && level-- == 0) | |
18829 | { | |
18830 | /* We've hit a `>>' where the first `>' closes the | |
18831 | template argument list, and the second `>' is | |
18832 | spurious. Just consume the `>>' and stop; we've | |
18833 | already produced at least one error. */ | |
18834 | cp_lexer_consume_token (parser->lexer); | |
18835 | return; | |
18836 | } | |
18837 | /* Fall through for C++0x, so we handle the second `>' in | |
18838 | the `>>'. */ | |
18839 | ||
a7b9d08c VR |
18840 | case CPP_GREATER: |
18841 | if (!nesting_depth && level-- == 0) | |
18842 | { | |
18843 | /* We've reached the token we want, consume it and stop. */ | |
18844 | cp_lexer_consume_token (parser->lexer); | |
18845 | return; | |
18846 | } | |
18847 | break; | |
bc4071dd | 18848 | |
bc4071dd RH |
18849 | case CPP_OPEN_PAREN: |
18850 | case CPP_OPEN_SQUARE: | |
18851 | ++nesting_depth; | |
18852 | break; | |
18853 | ||
bc4071dd RH |
18854 | case CPP_CLOSE_PAREN: |
18855 | case CPP_CLOSE_SQUARE: | |
a723baf1 MM |
18856 | if (nesting_depth-- == 0) |
18857 | return; | |
bc4071dd RH |
18858 | break; |
18859 | ||
a7b9d08c VR |
18860 | case CPP_EOF: |
18861 | case CPP_PRAGMA_EOL: | |
18862 | case CPP_SEMICOLON: | |
18863 | case CPP_OPEN_BRACE: | |
18864 | case CPP_CLOSE_BRACE: | |
18865 | /* The '>' was probably forgotten, don't look further. */ | |
18866 | return; | |
18867 | ||
bc4071dd RH |
18868 | default: |
18869 | break; | |
a723baf1 | 18870 | } |
bc4071dd | 18871 | |
a723baf1 MM |
18872 | /* Consume this token. */ |
18873 | cp_lexer_consume_token (parser->lexer); | |
18874 | } | |
18875 | } | |
18876 | ||
18877 | /* If the next token is the indicated keyword, consume it. Otherwise, | |
18878 | issue an error message indicating that TOKEN_DESC was expected. | |
21526606 | 18879 | |
a723baf1 MM |
18880 | Returns the token consumed, if the token had the appropriate type. |
18881 | Otherwise, returns NULL. */ | |
18882 | ||
18883 | static cp_token * | |
94edc4ab | 18884 | cp_parser_require_keyword (cp_parser* parser, |
0cbd7506 MS |
18885 | enum rid keyword, |
18886 | const char* token_desc) | |
a723baf1 MM |
18887 | { |
18888 | cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc); | |
18889 | ||
18890 | if (token && token->keyword != keyword) | |
18891 | { | |
18892 | dyn_string_t error_msg; | |
18893 | ||
18894 | /* Format the error message. */ | |
18895 | error_msg = dyn_string_new (0); | |
18896 | dyn_string_append_cstr (error_msg, "expected "); | |
18897 | dyn_string_append_cstr (error_msg, token_desc); | |
18898 | cp_parser_error (parser, error_msg->s); | |
18899 | dyn_string_delete (error_msg); | |
18900 | return NULL; | |
18901 | } | |
18902 | ||
18903 | return token; | |
18904 | } | |
18905 | ||
18906 | /* Returns TRUE iff TOKEN is a token that can begin the body of a | |
18907 | function-definition. */ | |
18908 | ||
21526606 | 18909 | static bool |
94edc4ab | 18910 | cp_parser_token_starts_function_definition_p (cp_token* token) |
a723baf1 MM |
18911 | { |
18912 | return (/* An ordinary function-body begins with an `{'. */ | |
18913 | token->type == CPP_OPEN_BRACE | |
18914 | /* A ctor-initializer begins with a `:'. */ | |
18915 | || token->type == CPP_COLON | |
18916 | /* A function-try-block begins with `try'. */ | |
18917 | || token->keyword == RID_TRY | |
18918 | /* The named return value extension begins with `return'. */ | |
18919 | || token->keyword == RID_RETURN); | |
18920 | } | |
18921 | ||
18922 | /* Returns TRUE iff the next token is the ":" or "{" beginning a class | |
18923 | definition. */ | |
18924 | ||
18925 | static bool | |
18926 | cp_parser_next_token_starts_class_definition_p (cp_parser *parser) | |
18927 | { | |
18928 | cp_token *token; | |
18929 | ||
18930 | token = cp_lexer_peek_token (parser->lexer); | |
18931 | return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON); | |
18932 | } | |
18933 | ||
a2ca1ab2 DG |
18934 | /* Returns TRUE iff the next token is the "," or ">" (or `>>', in |
18935 | C++0x) ending a template-argument. */ | |
d17811fd MM |
18936 | |
18937 | static bool | |
18938 | cp_parser_next_token_ends_template_argument_p (cp_parser *parser) | |
18939 | { | |
18940 | cp_token *token; | |
18941 | ||
18942 | token = cp_lexer_peek_token (parser->lexer); | |
5d80a306 DG |
18943 | return (token->type == CPP_COMMA |
18944 | || token->type == CPP_GREATER | |
a2ca1ab2 | 18945 | || token->type == CPP_ELLIPSIS |
c1ae8be5 | 18946 | || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)); |
d17811fd | 18947 | } |
f4abade9 | 18948 | |
48b5c5c1 | 18949 | /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the |
f4abade9 GB |
18950 | (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */ |
18951 | ||
18952 | static bool | |
21526606 | 18953 | cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser, |
f4abade9 GB |
18954 | size_t n) |
18955 | { | |
18956 | cp_token *token; | |
18957 | ||
18958 | token = cp_lexer_peek_nth_token (parser->lexer, n); | |
18959 | if (token->type == CPP_LESS) | |
18960 | return true; | |
18961 | /* Check for the sequence `<::' in the original code. It would be lexed as | |
18962 | `[:', where `[' is a digraph, and there is no whitespace before | |
18963 | `:'. */ | |
18964 | if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH) | |
18965 | { | |
18966 | cp_token *token2; | |
18967 | token2 = cp_lexer_peek_nth_token (parser->lexer, n+1); | |
18968 | if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE)) | |
18969 | return true; | |
18970 | } | |
18971 | return false; | |
18972 | } | |
21526606 | 18973 | |
a723baf1 MM |
18974 | /* Returns the kind of tag indicated by TOKEN, if it is a class-key, |
18975 | or none_type otherwise. */ | |
18976 | ||
18977 | static enum tag_types | |
94edc4ab | 18978 | cp_parser_token_is_class_key (cp_token* token) |
a723baf1 MM |
18979 | { |
18980 | switch (token->keyword) | |
18981 | { | |
18982 | case RID_CLASS: | |
18983 | return class_type; | |
18984 | case RID_STRUCT: | |
18985 | return record_type; | |
18986 | case RID_UNION: | |
18987 | return union_type; | |
21526606 | 18988 | |
a723baf1 MM |
18989 | default: |
18990 | return none_type; | |
18991 | } | |
18992 | } | |
18993 | ||
18994 | /* Issue an error message if the CLASS_KEY does not match the TYPE. */ | |
18995 | ||
18996 | static void | |
18997 | cp_parser_check_class_key (enum tag_types class_key, tree type) | |
18998 | { | |
18999 | if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type)) | |
cbe5f3b3 | 19000 | permerror (input_location, "%qs tag used in naming %q#T", |
a723baf1 | 19001 | class_key == union_type ? "union" |
21526606 | 19002 | : class_key == record_type ? "struct" : "class", |
a723baf1 MM |
19003 | type); |
19004 | } | |
21526606 | 19005 | |
cd0be382 | 19006 | /* Issue an error message if DECL is redeclared with different |
37d407a1 KL |
19007 | access than its original declaration [class.access.spec/3]. |
19008 | This applies to nested classes and nested class templates. | |
19009 | [class.mem/1]. */ | |
19010 | ||
2a13a625 | 19011 | static void |
2b7a3abf | 19012 | cp_parser_check_access_in_redeclaration (tree decl, location_t location) |
37d407a1 | 19013 | { |
e4fd5b87 | 19014 | if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl))) |
37d407a1 KL |
19015 | return; |
19016 | ||
19017 | if ((TREE_PRIVATE (decl) | |
19018 | != (current_access_specifier == access_private_node)) | |
19019 | || (TREE_PROTECTED (decl) | |
19020 | != (current_access_specifier == access_protected_node))) | |
69bc6bff | 19021 | error_at (location, "%qD redeclared with different access", decl); |
37d407a1 KL |
19022 | } |
19023 | ||
a723baf1 | 19024 | /* Look for the `template' keyword, as a syntactic disambiguator. |
21526606 | 19025 | Return TRUE iff it is present, in which case it will be |
a723baf1 MM |
19026 | consumed. */ |
19027 | ||
19028 | static bool | |
19029 | cp_parser_optional_template_keyword (cp_parser *parser) | |
19030 | { | |
19031 | if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE)) | |
19032 | { | |
19033 | /* The `template' keyword can only be used within templates; | |
19034 | outside templates the parser can always figure out what is a | |
19035 | template and what is not. */ | |
19036 | if (!processing_template_decl) | |
19037 | { | |
2b7a3abf | 19038 | cp_token *token = cp_lexer_peek_token (parser->lexer); |
69bc6bff MLI |
19039 | error_at (token->location, |
19040 | "%<template%> (as a disambiguator) is only allowed " | |
19041 | "within templates"); | |
a723baf1 MM |
19042 | /* If this part of the token stream is rescanned, the same |
19043 | error message would be generated. So, we purge the token | |
19044 | from the stream. */ | |
19045 | cp_lexer_purge_token (parser->lexer); | |
19046 | return false; | |
19047 | } | |
19048 | else | |
19049 | { | |
19050 | /* Consume the `template' keyword. */ | |
19051 | cp_lexer_consume_token (parser->lexer); | |
19052 | return true; | |
19053 | } | |
19054 | } | |
19055 | ||
19056 | return false; | |
19057 | } | |
19058 | ||
2050a1bb MM |
19059 | /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token, |
19060 | set PARSER->SCOPE, and perform other related actions. */ | |
19061 | ||
19062 | static void | |
19063 | cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser) | |
19064 | { | |
d6b418fa SM |
19065 | int i; |
19066 | struct tree_check *check_value; | |
19067 | deferred_access_check *chk; | |
19068 | VEC (deferred_access_check,gc) *checks; | |
2050a1bb MM |
19069 | |
19070 | /* Get the stored value. */ | |
d6b418fa | 19071 | check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value; |
2050a1bb | 19072 | /* Perform any access checks that were deferred. */ |
d6b418fa SM |
19073 | checks = check_value->checks; |
19074 | if (checks) | |
19075 | { | |
19076 | for (i = 0 ; | |
19077 | VEC_iterate (deferred_access_check, checks, i, chk) ; | |
19078 | ++i) | |
19079 | { | |
19080 | perform_or_defer_access_check (chk->binfo, | |
19081 | chk->decl, | |
19082 | chk->diag_decl); | |
19083 | } | |
19084 | } | |
2050a1bb | 19085 | /* Set the scope from the stored value. */ |
d6b418fa SM |
19086 | parser->scope = check_value->value; |
19087 | parser->qualifying_scope = check_value->qualifying_scope; | |
2050a1bb MM |
19088 | parser->object_scope = NULL_TREE; |
19089 | } | |
19090 | ||
09357846 JM |
19091 | /* Consume tokens up through a non-nested END token. Returns TRUE if we |
19092 | encounter the end of a block before what we were looking for. */ | |
a723baf1 | 19093 | |
09357846 | 19094 | static bool |
c162c75e MA |
19095 | cp_parser_cache_group (cp_parser *parser, |
19096 | enum cpp_ttype end, | |
19097 | unsigned depth) | |
a723baf1 MM |
19098 | { |
19099 | while (true) | |
19100 | { | |
09357846 | 19101 | cp_token *token = cp_lexer_peek_token (parser->lexer); |
a723baf1 | 19102 | |
09357846 | 19103 | /* Abort a parenthesized expression if we encounter a semicolon. */ |
a723baf1 | 19104 | if ((end == CPP_CLOSE_PAREN || depth == 0) |
09357846 JM |
19105 | && token->type == CPP_SEMICOLON) |
19106 | return true; | |
a723baf1 | 19107 | /* If we've reached the end of the file, stop. */ |
09357846 | 19108 | if (token->type == CPP_EOF |
bc4071dd | 19109 | || (end != CPP_PRAGMA_EOL |
09357846 JM |
19110 | && token->type == CPP_PRAGMA_EOL)) |
19111 | return true; | |
19112 | if (token->type == CPP_CLOSE_BRACE && depth == 0) | |
19113 | /* We've hit the end of an enclosing block, so there's been some | |
19114 | kind of syntax error. */ | |
19115 | return true; | |
19116 | ||
19117 | /* Consume the token. */ | |
19118 | cp_lexer_consume_token (parser->lexer); | |
a723baf1 MM |
19119 | /* See if it starts a new group. */ |
19120 | if (token->type == CPP_OPEN_BRACE) | |
19121 | { | |
c162c75e | 19122 | cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1); |
09357846 JM |
19123 | /* In theory this should probably check end == '}', but |
19124 | cp_parser_save_member_function_body needs it to exit | |
19125 | after either '}' or ')' when called with ')'. */ | |
a723baf1 | 19126 | if (depth == 0) |
09357846 | 19127 | return false; |
a723baf1 MM |
19128 | } |
19129 | else if (token->type == CPP_OPEN_PAREN) | |
09357846 JM |
19130 | { |
19131 | cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1); | |
19132 | if (depth == 0 && end == CPP_CLOSE_PAREN) | |
19133 | return false; | |
19134 | } | |
bc4071dd RH |
19135 | else if (token->type == CPP_PRAGMA) |
19136 | cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1); | |
a723baf1 | 19137 | else if (token->type == end) |
09357846 | 19138 | return false; |
a723baf1 MM |
19139 | } |
19140 | } | |
19141 | ||
19142 | /* Begin parsing tentatively. We always save tokens while parsing | |
19143 | tentatively so that if the tentative parsing fails we can restore the | |
19144 | tokens. */ | |
19145 | ||
19146 | static void | |
94edc4ab | 19147 | cp_parser_parse_tentatively (cp_parser* parser) |
a723baf1 MM |
19148 | { |
19149 | /* Enter a new parsing context. */ | |
19150 | parser->context = cp_parser_context_new (parser->context); | |
19151 | /* Begin saving tokens. */ | |
19152 | cp_lexer_save_tokens (parser->lexer); | |
19153 | /* In order to avoid repetitive access control error messages, | |
19154 | access checks are queued up until we are no longer parsing | |
19155 | tentatively. */ | |
8d241e0b | 19156 | push_deferring_access_checks (dk_deferred); |
a723baf1 MM |
19157 | } |
19158 | ||
19159 | /* Commit to the currently active tentative parse. */ | |
19160 | ||
19161 | static void | |
94edc4ab | 19162 | cp_parser_commit_to_tentative_parse (cp_parser* parser) |
a723baf1 MM |
19163 | { |
19164 | cp_parser_context *context; | |
19165 | cp_lexer *lexer; | |
19166 | ||
19167 | /* Mark all of the levels as committed. */ | |
19168 | lexer = parser->lexer; | |
19169 | for (context = parser->context; context->next; context = context->next) | |
19170 | { | |
19171 | if (context->status == CP_PARSER_STATUS_KIND_COMMITTED) | |
19172 | break; | |
19173 | context->status = CP_PARSER_STATUS_KIND_COMMITTED; | |
19174 | while (!cp_lexer_saving_tokens (lexer)) | |
19175 | lexer = lexer->next; | |
19176 | cp_lexer_commit_tokens (lexer); | |
19177 | } | |
19178 | } | |
19179 | ||
19180 | /* Abort the currently active tentative parse. All consumed tokens | |
19181 | will be rolled back, and no diagnostics will be issued. */ | |
19182 | ||
19183 | static void | |
94edc4ab | 19184 | cp_parser_abort_tentative_parse (cp_parser* parser) |
a723baf1 MM |
19185 | { |
19186 | cp_parser_simulate_error (parser); | |
19187 | /* Now, pretend that we want to see if the construct was | |
19188 | successfully parsed. */ | |
19189 | cp_parser_parse_definitely (parser); | |
19190 | } | |
19191 | ||
34cd5ae7 | 19192 | /* Stop parsing tentatively. If a parse error has occurred, restore the |
a723baf1 MM |
19193 | token stream. Otherwise, commit to the tokens we have consumed. |
19194 | Returns true if no error occurred; false otherwise. */ | |
19195 | ||
19196 | static bool | |
94edc4ab | 19197 | cp_parser_parse_definitely (cp_parser* parser) |
a723baf1 MM |
19198 | { |
19199 | bool error_occurred; | |
19200 | cp_parser_context *context; | |
19201 | ||
34cd5ae7 | 19202 | /* Remember whether or not an error occurred, since we are about to |
a723baf1 MM |
19203 | destroy that information. */ |
19204 | error_occurred = cp_parser_error_occurred (parser); | |
19205 | /* Remove the topmost context from the stack. */ | |
19206 | context = parser->context; | |
19207 | parser->context = context->next; | |
19208 | /* If no parse errors occurred, commit to the tentative parse. */ | |
19209 | if (!error_occurred) | |
19210 | { | |
19211 | /* Commit to the tokens read tentatively, unless that was | |
19212 | already done. */ | |
19213 | if (context->status != CP_PARSER_STATUS_KIND_COMMITTED) | |
19214 | cp_lexer_commit_tokens (parser->lexer); | |
cf22909c KL |
19215 | |
19216 | pop_to_parent_deferring_access_checks (); | |
a723baf1 MM |
19217 | } |
19218 | /* Otherwise, if errors occurred, roll back our state so that things | |
19219 | are just as they were before we began the tentative parse. */ | |
19220 | else | |
cf22909c KL |
19221 | { |
19222 | cp_lexer_rollback_tokens (parser->lexer); | |
19223 | pop_deferring_access_checks (); | |
19224 | } | |
e5976695 MM |
19225 | /* Add the context to the front of the free list. */ |
19226 | context->next = cp_parser_context_free_list; | |
19227 | cp_parser_context_free_list = context; | |
19228 | ||
19229 | return !error_occurred; | |
a723baf1 MM |
19230 | } |
19231 | ||
0b16f8f4 VR |
19232 | /* Returns true if we are parsing tentatively and are not committed to |
19233 | this tentative parse. */ | |
a723baf1 MM |
19234 | |
19235 | static bool | |
0b16f8f4 | 19236 | cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser) |
a723baf1 MM |
19237 | { |
19238 | return (cp_parser_parsing_tentatively (parser) | |
0b16f8f4 | 19239 | && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED); |
a723baf1 MM |
19240 | } |
19241 | ||
4de8668e | 19242 | /* Returns nonzero iff an error has occurred during the most recent |
a723baf1 | 19243 | tentative parse. */ |
21526606 | 19244 | |
a723baf1 | 19245 | static bool |
94edc4ab | 19246 | cp_parser_error_occurred (cp_parser* parser) |
a723baf1 MM |
19247 | { |
19248 | return (cp_parser_parsing_tentatively (parser) | |
19249 | && parser->context->status == CP_PARSER_STATUS_KIND_ERROR); | |
19250 | } | |
19251 | ||
4de8668e | 19252 | /* Returns nonzero if GNU extensions are allowed. */ |
a723baf1 MM |
19253 | |
19254 | static bool | |
94edc4ab | 19255 | cp_parser_allow_gnu_extensions_p (cp_parser* parser) |
a723baf1 MM |
19256 | { |
19257 | return parser->allow_gnu_extensions_p; | |
19258 | } | |
e58a9aa1 ZL |
19259 | \f |
19260 | /* Objective-C++ Productions */ | |
19261 | ||
19262 | ||
19263 | /* Parse an Objective-C expression, which feeds into a primary-expression | |
19264 | above. | |
19265 | ||
19266 | objc-expression: | |
19267 | objc-message-expression | |
19268 | objc-string-literal | |
19269 | objc-encode-expression | |
19270 | objc-protocol-expression | |
19271 | objc-selector-expression | |
19272 | ||
19273 | Returns a tree representation of the expression. */ | |
19274 | ||
19275 | static tree | |
19276 | cp_parser_objc_expression (cp_parser* parser) | |
19277 | { | |
19278 | /* Try to figure out what kind of declaration is present. */ | |
19279 | cp_token *kwd = cp_lexer_peek_token (parser->lexer); | |
19280 | ||
19281 | switch (kwd->type) | |
19282 | { | |
19283 | case CPP_OPEN_SQUARE: | |
19284 | return cp_parser_objc_message_expression (parser); | |
19285 | ||
19286 | case CPP_OBJC_STRING: | |
19287 | kwd = cp_lexer_consume_token (parser->lexer); | |
d6b418fa | 19288 | return objc_build_string_object (kwd->u.value); |
e58a9aa1 ZL |
19289 | |
19290 | case CPP_KEYWORD: | |
19291 | switch (kwd->keyword) | |
19292 | { | |
19293 | case RID_AT_ENCODE: | |
19294 | return cp_parser_objc_encode_expression (parser); | |
19295 | ||
19296 | case RID_AT_PROTOCOL: | |
19297 | return cp_parser_objc_protocol_expression (parser); | |
19298 | ||
19299 | case RID_AT_SELECTOR: | |
19300 | return cp_parser_objc_selector_expression (parser); | |
19301 | ||
19302 | default: | |
19303 | break; | |
19304 | } | |
19305 | default: | |
69bc6bff MLI |
19306 | error_at (kwd->location, |
19307 | "misplaced %<@%D%> Objective-C++ construct", | |
19308 | kwd->u.value); | |
e58a9aa1 ZL |
19309 | cp_parser_skip_to_end_of_block_or_statement (parser); |
19310 | } | |
19311 | ||
19312 | return error_mark_node; | |
19313 | } | |
19314 | ||
19315 | /* Parse an Objective-C message expression. | |
19316 | ||
19317 | objc-message-expression: | |
19318 | [ objc-message-receiver objc-message-args ] | |
19319 | ||
19320 | Returns a representation of an Objective-C message. */ | |
19321 | ||
19322 | static tree | |
19323 | cp_parser_objc_message_expression (cp_parser* parser) | |
19324 | { | |
19325 | tree receiver, messageargs; | |
19326 | ||
19327 | cp_lexer_consume_token (parser->lexer); /* Eat '['. */ | |
19328 | receiver = cp_parser_objc_message_receiver (parser); | |
19329 | messageargs = cp_parser_objc_message_args (parser); | |
3a483371 | 19330 | cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>"); |
e58a9aa1 ZL |
19331 | |
19332 | return objc_build_message_expr (build_tree_list (receiver, messageargs)); | |
19333 | } | |
19334 | ||
19335 | /* Parse an objc-message-receiver. | |
19336 | ||
19337 | objc-message-receiver: | |
e58a9aa1 | 19338 | expression |
660845bf | 19339 | simple-type-specifier |
e58a9aa1 ZL |
19340 | |
19341 | Returns a representation of the type or expression. */ | |
19342 | ||
19343 | static tree | |
19344 | cp_parser_objc_message_receiver (cp_parser* parser) | |
19345 | { | |
19346 | tree rcv; | |
e58a9aa1 ZL |
19347 | |
19348 | /* An Objective-C message receiver may be either (1) a type | |
19349 | or (2) an expression. */ | |
19350 | cp_parser_parse_tentatively (parser); | |
cee9035d | 19351 | rcv = cp_parser_expression (parser, false, NULL); |
e58a9aa1 ZL |
19352 | |
19353 | if (cp_parser_parse_definitely (parser)) | |
19354 | return rcv; | |
19355 | ||
660845bf ZL |
19356 | rcv = cp_parser_simple_type_specifier (parser, |
19357 | /*decl_specs=*/NULL, | |
19358 | CP_PARSER_FLAGS_NONE); | |
e58a9aa1 ZL |
19359 | |
19360 | return objc_get_class_reference (rcv); | |
19361 | } | |
19362 | ||
19363 | /* Parse the arguments and selectors comprising an Objective-C message. | |
19364 | ||
19365 | objc-message-args: | |
19366 | objc-selector | |
19367 | objc-selector-args | |
19368 | objc-selector-args , objc-comma-args | |
19369 | ||
19370 | objc-selector-args: | |
19371 | objc-selector [opt] : assignment-expression | |
19372 | objc-selector-args objc-selector [opt] : assignment-expression | |
19373 | ||
19374 | objc-comma-args: | |
19375 | assignment-expression | |
19376 | objc-comma-args , assignment-expression | |
19377 | ||
19378 | Returns a TREE_LIST, with TREE_PURPOSE containing a list of | |
19379 | selector arguments and TREE_VALUE containing a list of comma | |
19380 | arguments. */ | |
19381 | ||
19382 | static tree | |
19383 | cp_parser_objc_message_args (cp_parser* parser) | |
19384 | { | |
19385 | tree sel_args = NULL_TREE, addl_args = NULL_TREE; | |
19386 | bool maybe_unary_selector_p = true; | |
19387 | cp_token *token = cp_lexer_peek_token (parser->lexer); | |
19388 | ||
19389 | while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON) | |
19390 | { | |
19391 | tree selector = NULL_TREE, arg; | |
19392 | ||
19393 | if (token->type != CPP_COLON) | |
19394 | selector = cp_parser_objc_selector (parser); | |
19395 | ||
19396 | /* Detect if we have a unary selector. */ | |
19397 | if (maybe_unary_selector_p | |
19398 | && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)) | |
19399 | return build_tree_list (selector, NULL_TREE); | |
19400 | ||
19401 | maybe_unary_selector_p = false; | |
3a483371 | 19402 | cp_parser_require (parser, CPP_COLON, "%<:%>"); |
cee9035d | 19403 | arg = cp_parser_assignment_expression (parser, false, NULL); |
e58a9aa1 ZL |
19404 | |
19405 | sel_args | |
19406 | = chainon (sel_args, | |
19407 | build_tree_list (selector, arg)); | |
19408 | ||
19409 | token = cp_lexer_peek_token (parser->lexer); | |
19410 | } | |
19411 | ||
19412 | /* Handle non-selector arguments, if any. */ | |
19413 | while (token->type == CPP_COMMA) | |
19414 | { | |
19415 | tree arg; | |
19416 | ||
19417 | cp_lexer_consume_token (parser->lexer); | |
cee9035d | 19418 | arg = cp_parser_assignment_expression (parser, false, NULL); |
e58a9aa1 ZL |
19419 | |
19420 | addl_args | |
19421 | = chainon (addl_args, | |
19422 | build_tree_list (NULL_TREE, arg)); | |
19423 | ||
19424 | token = cp_lexer_peek_token (parser->lexer); | |
19425 | } | |
19426 | ||
19427 | return build_tree_list (sel_args, addl_args); | |
19428 | } | |
19429 | ||
19430 | /* Parse an Objective-C encode expression. | |
19431 | ||
19432 | objc-encode-expression: | |
19433 | @encode objc-typename | |
c8094d83 | 19434 | |
e58a9aa1 ZL |
19435 | Returns an encoded representation of the type argument. */ |
19436 | ||
19437 | static tree | |
19438 | cp_parser_objc_encode_expression (cp_parser* parser) | |
19439 | { | |
19440 | tree type; | |
2b7a3abf | 19441 | cp_token *token; |
e58a9aa1 ZL |
19442 | |
19443 | cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */ | |
3a483371 | 19444 | cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"); |
2b7a3abf | 19445 | token = cp_lexer_peek_token (parser->lexer); |
e58a9aa1 | 19446 | type = complete_type (cp_parser_type_id (parser)); |
3a483371 | 19447 | cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"); |
e58a9aa1 ZL |
19448 | |
19449 | if (!type) | |
19450 | { | |
69bc6bff MLI |
19451 | error_at (token->location, |
19452 | "%<@encode%> must specify a type as an argument"); | |
e58a9aa1 ZL |
19453 | return error_mark_node; |
19454 | } | |
19455 | ||
19456 | return objc_build_encode_expr (type); | |
19457 | } | |
19458 | ||
19459 | /* Parse an Objective-C @defs expression. */ | |
19460 | ||
19461 | static tree | |
19462 | cp_parser_objc_defs_expression (cp_parser *parser) | |
19463 | { | |
19464 | tree name; | |
19465 | ||
19466 | cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */ | |
3a483371 | 19467 | cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"); |
e58a9aa1 | 19468 | name = cp_parser_identifier (parser); |
3a483371 | 19469 | cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"); |
e58a9aa1 ZL |
19470 | |
19471 | return objc_get_class_ivars (name); | |
19472 | } | |
19473 | ||
19474 | /* Parse an Objective-C protocol expression. | |
19475 | ||
19476 | objc-protocol-expression: | |
19477 | @protocol ( identifier ) | |
19478 | ||
19479 | Returns a representation of the protocol expression. */ | |
19480 | ||
19481 | static tree | |
19482 | cp_parser_objc_protocol_expression (cp_parser* parser) | |
19483 | { | |
19484 | tree proto; | |
19485 | ||
19486 | cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */ | |
3a483371 | 19487 | cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"); |
e58a9aa1 | 19488 | proto = cp_parser_identifier (parser); |
3a483371 | 19489 | cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"); |
e58a9aa1 ZL |
19490 | |
19491 | return objc_build_protocol_expr (proto); | |
19492 | } | |
19493 | ||
19494 | /* Parse an Objective-C selector expression. | |
19495 | ||
19496 | objc-selector-expression: | |
19497 | @selector ( objc-method-signature ) | |
19498 | ||
19499 | objc-method-signature: | |
19500 | objc-selector | |
19501 | objc-selector-seq | |
19502 | ||
19503 | objc-selector-seq: | |
19504 | objc-selector : | |
19505 | objc-selector-seq objc-selector : | |
19506 | ||
19507 | Returns a representation of the method selector. */ | |
19508 | ||
19509 | static tree | |
19510 | cp_parser_objc_selector_expression (cp_parser* parser) | |
19511 | { | |
19512 | tree sel_seq = NULL_TREE; | |
19513 | bool maybe_unary_selector_p = true; | |
19514 | cp_token *token; | |
c2255bc4 | 19515 | location_t loc = cp_lexer_peek_token (parser->lexer)->location; |
e58a9aa1 ZL |
19516 | |
19517 | cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */ | |
3a483371 | 19518 | cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"); |
e58a9aa1 ZL |
19519 | token = cp_lexer_peek_token (parser->lexer); |
19520 | ||
d95036e3 | 19521 | while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON |
3db45ab5 | 19522 | || token->type == CPP_SCOPE) |
e58a9aa1 ZL |
19523 | { |
19524 | tree selector = NULL_TREE; | |
19525 | ||
d95036e3 AP |
19526 | if (token->type != CPP_COLON |
19527 | || token->type == CPP_SCOPE) | |
e58a9aa1 ZL |
19528 | selector = cp_parser_objc_selector (parser); |
19529 | ||
d95036e3 | 19530 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON) |
3db45ab5 | 19531 | && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE)) |
e58a9aa1 | 19532 | { |
d95036e3 AP |
19533 | /* Detect if we have a unary selector. */ |
19534 | if (maybe_unary_selector_p) | |
19535 | { | |
19536 | sel_seq = selector; | |
19537 | goto finish_selector; | |
19538 | } | |
19539 | else | |
19540 | { | |
19541 | cp_parser_error (parser, "expected %<:%>"); | |
19542 | } | |
e58a9aa1 | 19543 | } |
e58a9aa1 | 19544 | maybe_unary_selector_p = false; |
d95036e3 | 19545 | token = cp_lexer_consume_token (parser->lexer); |
3db45ab5 | 19546 | |
d95036e3 | 19547 | if (token->type == CPP_SCOPE) |
3db45ab5 | 19548 | { |
d95036e3 AP |
19549 | sel_seq |
19550 | = chainon (sel_seq, | |
19551 | build_tree_list (selector, NULL_TREE)); | |
19552 | sel_seq | |
19553 | = chainon (sel_seq, | |
19554 | build_tree_list (NULL_TREE, NULL_TREE)); | |
19555 | } | |
19556 | else | |
19557 | sel_seq | |
19558 | = chainon (sel_seq, | |
19559 | build_tree_list (selector, NULL_TREE)); | |
e58a9aa1 ZL |
19560 | |
19561 | token = cp_lexer_peek_token (parser->lexer); | |
19562 | } | |
19563 | ||
19564 | finish_selector: | |
3a483371 | 19565 | cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"); |
e58a9aa1 | 19566 | |
c2255bc4 | 19567 | return objc_build_selector_expr (loc, sel_seq); |
e58a9aa1 ZL |
19568 | } |
19569 | ||
19570 | /* Parse a list of identifiers. | |
19571 | ||
19572 | objc-identifier-list: | |
19573 | identifier | |
19574 | objc-identifier-list , identifier | |
19575 | ||
19576 | Returns a TREE_LIST of identifier nodes. */ | |
19577 | ||
19578 | static tree | |
19579 | cp_parser_objc_identifier_list (cp_parser* parser) | |
19580 | { | |
19581 | tree list = build_tree_list (NULL_TREE, cp_parser_identifier (parser)); | |
19582 | cp_token *sep = cp_lexer_peek_token (parser->lexer); | |
19583 | ||
19584 | while (sep->type == CPP_COMMA) | |
19585 | { | |
19586 | cp_lexer_consume_token (parser->lexer); /* Eat ','. */ | |
c8094d83 | 19587 | list = chainon (list, |
e58a9aa1 ZL |
19588 | build_tree_list (NULL_TREE, |
19589 | cp_parser_identifier (parser))); | |
19590 | sep = cp_lexer_peek_token (parser->lexer); | |
19591 | } | |
c8094d83 | 19592 | |
e58a9aa1 ZL |
19593 | return list; |
19594 | } | |
19595 | ||
19596 | /* Parse an Objective-C alias declaration. | |
19597 | ||
19598 | objc-alias-declaration: | |
19599 | @compatibility_alias identifier identifier ; | |
19600 | ||
3b426391 | 19601 | This function registers the alias mapping with the Objective-C front end. |
e58a9aa1 ZL |
19602 | It returns nothing. */ |
19603 | ||
19604 | static void | |
19605 | cp_parser_objc_alias_declaration (cp_parser* parser) | |
19606 | { | |
19607 | tree alias, orig; | |
19608 | ||
19609 | cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */ | |
19610 | alias = cp_parser_identifier (parser); | |
19611 | orig = cp_parser_identifier (parser); | |
19612 | objc_declare_alias (alias, orig); | |
19613 | cp_parser_consume_semicolon_at_end_of_statement (parser); | |
19614 | } | |
19615 | ||
19616 | /* Parse an Objective-C class forward-declaration. | |
19617 | ||
19618 | objc-class-declaration: | |
19619 | @class objc-identifier-list ; | |
19620 | ||
19621 | The function registers the forward declarations with the Objective-C | |
3b426391 | 19622 | front end. It returns nothing. */ |
e58a9aa1 ZL |
19623 | |
19624 | static void | |
19625 | cp_parser_objc_class_declaration (cp_parser* parser) | |
19626 | { | |
19627 | cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */ | |
19628 | objc_declare_class (cp_parser_objc_identifier_list (parser)); | |
19629 | cp_parser_consume_semicolon_at_end_of_statement (parser); | |
19630 | } | |
19631 | ||
19632 | /* Parse a list of Objective-C protocol references. | |
19633 | ||
19634 | objc-protocol-refs-opt: | |
19635 | objc-protocol-refs [opt] | |
19636 | ||
19637 | objc-protocol-refs: | |
19638 | < objc-identifier-list > | |
19639 | ||
19640 | Returns a TREE_LIST of identifiers, if any. */ | |
19641 | ||
19642 | static tree | |
19643 | cp_parser_objc_protocol_refs_opt (cp_parser* parser) | |
19644 | { | |
19645 | tree protorefs = NULL_TREE; | |
19646 | ||
19647 | if(cp_lexer_next_token_is (parser->lexer, CPP_LESS)) | |
19648 | { | |
19649 | cp_lexer_consume_token (parser->lexer); /* Eat '<'. */ | |
19650 | protorefs = cp_parser_objc_identifier_list (parser); | |
3a483371 | 19651 | cp_parser_require (parser, CPP_GREATER, "%<>%>"); |
e58a9aa1 ZL |
19652 | } |
19653 | ||
19654 | return protorefs; | |
19655 | } | |
19656 | ||
19657 | /* Parse a Objective-C visibility specification. */ | |
19658 | ||
19659 | static void | |
19660 | cp_parser_objc_visibility_spec (cp_parser* parser) | |
19661 | { | |
19662 | cp_token *vis = cp_lexer_peek_token (parser->lexer); | |
19663 | ||
19664 | switch (vis->keyword) | |
19665 | { | |
19666 | case RID_AT_PRIVATE: | |
19667 | objc_set_visibility (2); | |
19668 | break; | |
19669 | case RID_AT_PROTECTED: | |
19670 | objc_set_visibility (0); | |
19671 | break; | |
19672 | case RID_AT_PUBLIC: | |
19673 | objc_set_visibility (1); | |
19674 | break; | |
19675 | default: | |
19676 | return; | |
19677 | } | |
a723baf1 | 19678 | |
e58a9aa1 ZL |
19679 | /* Eat '@private'/'@protected'/'@public'. */ |
19680 | cp_lexer_consume_token (parser->lexer); | |
19681 | } | |
19682 | ||
19683 | /* Parse an Objective-C method type. */ | |
19684 | ||
19685 | static void | |
19686 | cp_parser_objc_method_type (cp_parser* parser) | |
19687 | { | |
19688 | objc_set_method_type | |
19689 | (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS | |
19690 | ? PLUS_EXPR | |
19691 | : MINUS_EXPR); | |
19692 | } | |
19693 | ||
19694 | /* Parse an Objective-C protocol qualifier. */ | |
19695 | ||
19696 | static tree | |
19697 | cp_parser_objc_protocol_qualifiers (cp_parser* parser) | |
19698 | { | |
19699 | tree quals = NULL_TREE, node; | |
19700 | cp_token *token = cp_lexer_peek_token (parser->lexer); | |
19701 | ||
d6b418fa | 19702 | node = token->u.value; |
e58a9aa1 ZL |
19703 | |
19704 | while (node && TREE_CODE (node) == IDENTIFIER_NODE | |
19705 | && (node == ridpointers [(int) RID_IN] | |
19706 | || node == ridpointers [(int) RID_OUT] | |
19707 | || node == ridpointers [(int) RID_INOUT] | |
19708 | || node == ridpointers [(int) RID_BYCOPY] | |
0cbd7506 | 19709 | || node == ridpointers [(int) RID_BYREF] |
e58a9aa1 ZL |
19710 | || node == ridpointers [(int) RID_ONEWAY])) |
19711 | { | |
19712 | quals = tree_cons (NULL_TREE, node, quals); | |
19713 | cp_lexer_consume_token (parser->lexer); | |
19714 | token = cp_lexer_peek_token (parser->lexer); | |
d6b418fa | 19715 | node = token->u.value; |
e58a9aa1 ZL |
19716 | } |
19717 | ||
19718 | return quals; | |
19719 | } | |
19720 | ||
19721 | /* Parse an Objective-C typename. */ | |
19722 | ||
19723 | static tree | |
19724 | cp_parser_objc_typename (cp_parser* parser) | |
19725 | { | |
be93747e | 19726 | tree type_name = NULL_TREE; |
e58a9aa1 ZL |
19727 | |
19728 | if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)) | |
19729 | { | |
19730 | tree proto_quals, cp_type = NULL_TREE; | |
19731 | ||
19732 | cp_lexer_consume_token (parser->lexer); /* Eat '('. */ | |
19733 | proto_quals = cp_parser_objc_protocol_qualifiers (parser); | |
19734 | ||
19735 | /* An ObjC type name may consist of just protocol qualifiers, in which | |
19736 | case the type shall default to 'id'. */ | |
19737 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)) | |
19738 | cp_type = cp_parser_type_id (parser); | |
19739 | ||
3a483371 | 19740 | cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"); |
be93747e | 19741 | type_name = build_tree_list (proto_quals, cp_type); |
e58a9aa1 ZL |
19742 | } |
19743 | ||
be93747e | 19744 | return type_name; |
e58a9aa1 ZL |
19745 | } |
19746 | ||
19747 | /* Check to see if TYPE refers to an Objective-C selector name. */ | |
19748 | ||
19749 | static bool | |
19750 | cp_parser_objc_selector_p (enum cpp_ttype type) | |
19751 | { | |
19752 | return (type == CPP_NAME || type == CPP_KEYWORD | |
19753 | || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND | |
19754 | || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT | |
19755 | || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ | |
19756 | || type == CPP_XOR || type == CPP_XOR_EQ); | |
19757 | } | |
19758 | ||
19759 | /* Parse an Objective-C selector. */ | |
19760 | ||
19761 | static tree | |
19762 | cp_parser_objc_selector (cp_parser* parser) | |
19763 | { | |
19764 | cp_token *token = cp_lexer_consume_token (parser->lexer); | |
c8094d83 | 19765 | |
e58a9aa1 ZL |
19766 | if (!cp_parser_objc_selector_p (token->type)) |
19767 | { | |
69bc6bff | 19768 | error_at (token->location, "invalid Objective-C++ selector name"); |
e58a9aa1 ZL |
19769 | return error_mark_node; |
19770 | } | |
19771 | ||
19772 | /* C++ operator names are allowed to appear in ObjC selectors. */ | |
19773 | switch (token->type) | |
19774 | { | |
19775 | case CPP_AND_AND: return get_identifier ("and"); | |
19776 | case CPP_AND_EQ: return get_identifier ("and_eq"); | |
19777 | case CPP_AND: return get_identifier ("bitand"); | |
19778 | case CPP_OR: return get_identifier ("bitor"); | |
19779 | case CPP_COMPL: return get_identifier ("compl"); | |
19780 | case CPP_NOT: return get_identifier ("not"); | |
19781 | case CPP_NOT_EQ: return get_identifier ("not_eq"); | |
19782 | case CPP_OR_OR: return get_identifier ("or"); | |
19783 | case CPP_OR_EQ: return get_identifier ("or_eq"); | |
19784 | case CPP_XOR: return get_identifier ("xor"); | |
19785 | case CPP_XOR_EQ: return get_identifier ("xor_eq"); | |
d6b418fa | 19786 | default: return token->u.value; |
e58a9aa1 ZL |
19787 | } |
19788 | } | |
19789 | ||
19790 | /* Parse an Objective-C params list. */ | |
19791 | ||
19792 | static tree | |
19793 | cp_parser_objc_method_keyword_params (cp_parser* parser) | |
19794 | { | |
19795 | tree params = NULL_TREE; | |
19796 | bool maybe_unary_selector_p = true; | |
19797 | cp_token *token = cp_lexer_peek_token (parser->lexer); | |
19798 | ||
19799 | while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON) | |
19800 | { | |
be93747e | 19801 | tree selector = NULL_TREE, type_name, identifier; |
e58a9aa1 ZL |
19802 | |
19803 | if (token->type != CPP_COLON) | |
19804 | selector = cp_parser_objc_selector (parser); | |
19805 | ||
19806 | /* Detect if we have a unary selector. */ | |
19807 | if (maybe_unary_selector_p | |
19808 | && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)) | |
19809 | return selector; | |
19810 | ||
19811 | maybe_unary_selector_p = false; | |
3a483371 | 19812 | cp_parser_require (parser, CPP_COLON, "%<:%>"); |
be93747e | 19813 | type_name = cp_parser_objc_typename (parser); |
e58a9aa1 ZL |
19814 | identifier = cp_parser_identifier (parser); |
19815 | ||
19816 | params | |
19817 | = chainon (params, | |
c8094d83 | 19818 | objc_build_keyword_decl (selector, |
be93747e | 19819 | type_name, |
e58a9aa1 ZL |
19820 | identifier)); |
19821 | ||
19822 | token = cp_lexer_peek_token (parser->lexer); | |
19823 | } | |
19824 | ||
19825 | return params; | |
19826 | } | |
19827 | ||
19828 | /* Parse the non-keyword Objective-C params. */ | |
19829 | ||
19830 | static tree | |
19831 | cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp) | |
19832 | { | |
19833 | tree params = make_node (TREE_LIST); | |
19834 | cp_token *token = cp_lexer_peek_token (parser->lexer); | |
19835 | *ellipsisp = false; /* Initially, assume no ellipsis. */ | |
19836 | ||
19837 | while (token->type == CPP_COMMA) | |
19838 | { | |
19839 | cp_parameter_declarator *parmdecl; | |
19840 | tree parm; | |
19841 | ||
19842 | cp_lexer_consume_token (parser->lexer); /* Eat ','. */ | |
19843 | token = cp_lexer_peek_token (parser->lexer); | |
19844 | ||
19845 | if (token->type == CPP_ELLIPSIS) | |
19846 | { | |
19847 | cp_lexer_consume_token (parser->lexer); /* Eat '...'. */ | |
19848 | *ellipsisp = true; | |
19849 | break; | |
19850 | } | |
19851 | ||
19852 | parmdecl = cp_parser_parameter_declaration (parser, false, NULL); | |
19853 | parm = grokdeclarator (parmdecl->declarator, | |
19854 | &parmdecl->decl_specifiers, | |
c8094d83 | 19855 | PARM, /*initialized=*/0, |
e58a9aa1 ZL |
19856 | /*attrlist=*/NULL); |
19857 | ||
19858 | chainon (params, build_tree_list (NULL_TREE, parm)); | |
19859 | token = cp_lexer_peek_token (parser->lexer); | |
19860 | } | |
19861 | ||
19862 | return params; | |
19863 | } | |
19864 | ||
19865 | /* Parse a linkage specification, a pragma, an extra semicolon or a block. */ | |
19866 | ||
19867 | static void | |
19868 | cp_parser_objc_interstitial_code (cp_parser* parser) | |
19869 | { | |
19870 | cp_token *token = cp_lexer_peek_token (parser->lexer); | |
19871 | ||
19872 | /* If the next token is `extern' and the following token is a string | |
19873 | literal, then we have a linkage specification. */ | |
19874 | if (token->keyword == RID_EXTERN | |
19875 | && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2))) | |
19876 | cp_parser_linkage_specification (parser); | |
19877 | /* Handle #pragma, if any. */ | |
19878 | else if (token->type == CPP_PRAGMA) | |
bc4071dd | 19879 | cp_parser_pragma (parser, pragma_external); |
e58a9aa1 ZL |
19880 | /* Allow stray semicolons. */ |
19881 | else if (token->type == CPP_SEMICOLON) | |
19882 | cp_lexer_consume_token (parser->lexer); | |
19883 | /* Finally, try to parse a block-declaration, or a function-definition. */ | |
19884 | else | |
19885 | cp_parser_block_declaration (parser, /*statement_p=*/false); | |
19886 | } | |
19887 | ||
19888 | /* Parse a method signature. */ | |
19889 | ||
19890 | static tree | |
19891 | cp_parser_objc_method_signature (cp_parser* parser) | |
19892 | { | |
19893 | tree rettype, kwdparms, optparms; | |
19894 | bool ellipsis = false; | |
19895 | ||
19896 | cp_parser_objc_method_type (parser); | |
19897 | rettype = cp_parser_objc_typename (parser); | |
19898 | kwdparms = cp_parser_objc_method_keyword_params (parser); | |
19899 | optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis); | |
19900 | ||
19901 | return objc_build_method_signature (rettype, kwdparms, optparms, ellipsis); | |
19902 | } | |
19903 | ||
19904 | /* Pars an Objective-C method prototype list. */ | |
19905 | ||
19906 | static void | |
19907 | cp_parser_objc_method_prototype_list (cp_parser* parser) | |
19908 | { | |
19909 | cp_token *token = cp_lexer_peek_token (parser->lexer); | |
19910 | ||
19911 | while (token->keyword != RID_AT_END) | |
19912 | { | |
19913 | if (token->type == CPP_PLUS || token->type == CPP_MINUS) | |
19914 | { | |
19915 | objc_add_method_declaration | |
19916 | (cp_parser_objc_method_signature (parser)); | |
19917 | cp_parser_consume_semicolon_at_end_of_statement (parser); | |
19918 | } | |
19919 | else | |
19920 | /* Allow for interspersed non-ObjC++ code. */ | |
19921 | cp_parser_objc_interstitial_code (parser); | |
19922 | ||
19923 | token = cp_lexer_peek_token (parser->lexer); | |
19924 | } | |
19925 | ||
19926 | cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */ | |
19927 | objc_finish_interface (); | |
19928 | } | |
19929 | ||
19930 | /* Parse an Objective-C method definition list. */ | |
19931 | ||
19932 | static void | |
19933 | cp_parser_objc_method_definition_list (cp_parser* parser) | |
19934 | { | |
19935 | cp_token *token = cp_lexer_peek_token (parser->lexer); | |
19936 | ||
19937 | while (token->keyword != RID_AT_END) | |
19938 | { | |
19939 | tree meth; | |
19940 | ||
19941 | if (token->type == CPP_PLUS || token->type == CPP_MINUS) | |
19942 | { | |
19943 | push_deferring_access_checks (dk_deferred); | |
19944 | objc_start_method_definition | |
19945 | (cp_parser_objc_method_signature (parser)); | |
19946 | ||
19947 | /* For historical reasons, we accept an optional semicolon. */ | |
19948 | if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)) | |
19949 | cp_lexer_consume_token (parser->lexer); | |
19950 | ||
19951 | perform_deferred_access_checks (); | |
19952 | stop_deferring_access_checks (); | |
19953 | meth = cp_parser_function_definition_after_declarator (parser, | |
19954 | false); | |
19955 | pop_deferring_access_checks (); | |
19956 | objc_finish_method_definition (meth); | |
19957 | } | |
19958 | else | |
19959 | /* Allow for interspersed non-ObjC++ code. */ | |
19960 | cp_parser_objc_interstitial_code (parser); | |
19961 | ||
19962 | token = cp_lexer_peek_token (parser->lexer); | |
19963 | } | |
19964 | ||
19965 | cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */ | |
19966 | objc_finish_implementation (); | |
19967 | } | |
19968 | ||
19969 | /* Parse Objective-C ivars. */ | |
19970 | ||
19971 | static void | |
19972 | cp_parser_objc_class_ivars (cp_parser* parser) | |
19973 | { | |
19974 | cp_token *token = cp_lexer_peek_token (parser->lexer); | |
19975 | ||
19976 | if (token->type != CPP_OPEN_BRACE) | |
19977 | return; /* No ivars specified. */ | |
19978 | ||
19979 | cp_lexer_consume_token (parser->lexer); /* Eat '{'. */ | |
19980 | token = cp_lexer_peek_token (parser->lexer); | |
19981 | ||
19982 | while (token->type != CPP_CLOSE_BRACE) | |
19983 | { | |
19984 | cp_decl_specifier_seq declspecs; | |
19985 | int decl_class_or_enum_p; | |
19986 | tree prefix_attributes; | |
19987 | ||
19988 | cp_parser_objc_visibility_spec (parser); | |
19989 | ||
19990 | if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE)) | |
19991 | break; | |
19992 | ||
19993 | cp_parser_decl_specifier_seq (parser, | |
19994 | CP_PARSER_FLAGS_OPTIONAL, | |
19995 | &declspecs, | |
19996 | &decl_class_or_enum_p); | |
19997 | prefix_attributes = declspecs.attributes; | |
19998 | declspecs.attributes = NULL_TREE; | |
19999 | ||
20000 | /* Keep going until we hit the `;' at the end of the | |
20001 | declaration. */ | |
20002 | while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)) | |
20003 | { | |
20004 | tree width = NULL_TREE, attributes, first_attribute, decl; | |
20005 | cp_declarator *declarator = NULL; | |
20006 | int ctor_dtor_or_conv_p; | |
20007 | ||
20008 | /* Check for a (possibly unnamed) bitfield declaration. */ | |
20009 | token = cp_lexer_peek_token (parser->lexer); | |
20010 | if (token->type == CPP_COLON) | |
20011 | goto eat_colon; | |
20012 | ||
20013 | if (token->type == CPP_NAME | |
20014 | && (cp_lexer_peek_nth_token (parser->lexer, 2)->type | |
20015 | == CPP_COLON)) | |
20016 | { | |
20017 | /* Get the name of the bitfield. */ | |
20018 | declarator = make_id_declarator (NULL_TREE, | |
d85d3d57 MM |
20019 | cp_parser_identifier (parser), |
20020 | sfk_none); | |
e58a9aa1 ZL |
20021 | |
20022 | eat_colon: | |
20023 | cp_lexer_consume_token (parser->lexer); /* Eat ':'. */ | |
20024 | /* Get the width of the bitfield. */ | |
20025 | width | |
20026 | = cp_parser_constant_expression (parser, | |
20027 | /*allow_non_constant=*/false, | |
20028 | NULL); | |
20029 | } | |
20030 | else | |
20031 | { | |
20032 | /* Parse the declarator. */ | |
c8094d83 | 20033 | declarator |
e58a9aa1 ZL |
20034 | = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED, |
20035 | &ctor_dtor_or_conv_p, | |
20036 | /*parenthesized_p=*/NULL, | |
20037 | /*member_p=*/false); | |
20038 | } | |
20039 | ||
20040 | /* Look for attributes that apply to the ivar. */ | |
20041 | attributes = cp_parser_attributes_opt (parser); | |
20042 | /* Remember which attributes are prefix attributes and | |
20043 | which are not. */ | |
20044 | first_attribute = attributes; | |
20045 | /* Combine the attributes. */ | |
20046 | attributes = chainon (prefix_attributes, attributes); | |
20047 | ||
20048 | if (width) | |
e58a9aa1 | 20049 | /* Create the bitfield declaration. */ |
a6d9bc9d SM |
20050 | decl = grokbitfield (declarator, &declspecs, |
20051 | width, | |
20052 | attributes); | |
e58a9aa1 | 20053 | else |
3db45ab5 | 20054 | decl = grokfield (declarator, &declspecs, |
d174af6c | 20055 | NULL_TREE, /*init_const_expr_p=*/false, |
e58a9aa1 | 20056 | NULL_TREE, attributes); |
c8094d83 | 20057 | |
e58a9aa1 ZL |
20058 | /* Add the instance variable. */ |
20059 | objc_add_instance_variable (decl); | |
20060 | ||
20061 | /* Reset PREFIX_ATTRIBUTES. */ | |
20062 | while (attributes && TREE_CHAIN (attributes) != first_attribute) | |
20063 | attributes = TREE_CHAIN (attributes); | |
20064 | if (attributes) | |
20065 | TREE_CHAIN (attributes) = NULL_TREE; | |
20066 | ||
20067 | token = cp_lexer_peek_token (parser->lexer); | |
20068 | ||
20069 | if (token->type == CPP_COMMA) | |
20070 | { | |
20071 | cp_lexer_consume_token (parser->lexer); /* Eat ','. */ | |
20072 | continue; | |
20073 | } | |
20074 | break; | |
20075 | } | |
20076 | ||
20077 | cp_parser_consume_semicolon_at_end_of_statement (parser); | |
20078 | token = cp_lexer_peek_token (parser->lexer); | |
20079 | } | |
20080 | ||
20081 | cp_lexer_consume_token (parser->lexer); /* Eat '}'. */ | |
20082 | /* For historical reasons, we accept an optional semicolon. */ | |
20083 | if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)) | |
20084 | cp_lexer_consume_token (parser->lexer); | |
20085 | } | |
20086 | ||
20087 | /* Parse an Objective-C protocol declaration. */ | |
20088 | ||
20089 | static void | |
20090 | cp_parser_objc_protocol_declaration (cp_parser* parser) | |
20091 | { | |
20092 | tree proto, protorefs; | |
20093 | cp_token *tok; | |
20094 | ||
20095 | cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */ | |
20096 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)) | |
20097 | { | |
2b7a3abf | 20098 | tok = cp_lexer_peek_token (parser->lexer); |
69bc6bff | 20099 | error_at (tok->location, "identifier expected after %<@protocol%>"); |
e58a9aa1 ZL |
20100 | goto finish; |
20101 | } | |
20102 | ||
128a79fb | 20103 | /* See if we have a forward declaration or a definition. */ |
e58a9aa1 | 20104 | tok = cp_lexer_peek_nth_token (parser->lexer, 2); |
c8094d83 | 20105 | |
e58a9aa1 ZL |
20106 | /* Try a forward declaration first. */ |
20107 | if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON) | |
20108 | { | |
20109 | objc_declare_protocols (cp_parser_objc_identifier_list (parser)); | |
c8094d83 | 20110 | finish: |
e58a9aa1 | 20111 | cp_parser_consume_semicolon_at_end_of_statement (parser); |
c8094d83 | 20112 | } |
e58a9aa1 ZL |
20113 | |
20114 | /* Ok, we got a full-fledged definition (or at least should). */ | |
20115 | else | |
20116 | { | |
20117 | proto = cp_parser_identifier (parser); | |
20118 | protorefs = cp_parser_objc_protocol_refs_opt (parser); | |
20119 | objc_start_protocol (proto, protorefs); | |
20120 | cp_parser_objc_method_prototype_list (parser); | |
20121 | } | |
20122 | } | |
20123 | ||
20124 | /* Parse an Objective-C superclass or category. */ | |
20125 | ||
20126 | static void | |
20127 | cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super, | |
20128 | tree *categ) | |
20129 | { | |
20130 | cp_token *next = cp_lexer_peek_token (parser->lexer); | |
20131 | ||
20132 | *super = *categ = NULL_TREE; | |
20133 | if (next->type == CPP_COLON) | |
20134 | { | |
20135 | cp_lexer_consume_token (parser->lexer); /* Eat ':'. */ | |
20136 | *super = cp_parser_identifier (parser); | |
20137 | } | |
20138 | else if (next->type == CPP_OPEN_PAREN) | |
20139 | { | |
20140 | cp_lexer_consume_token (parser->lexer); /* Eat '('. */ | |
20141 | *categ = cp_parser_identifier (parser); | |
3a483371 | 20142 | cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"); |
e58a9aa1 ZL |
20143 | } |
20144 | } | |
20145 | ||
20146 | /* Parse an Objective-C class interface. */ | |
20147 | ||
20148 | static void | |
20149 | cp_parser_objc_class_interface (cp_parser* parser) | |
20150 | { | |
20151 | tree name, super, categ, protos; | |
20152 | ||
20153 | cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */ | |
20154 | name = cp_parser_identifier (parser); | |
20155 | cp_parser_objc_superclass_or_category (parser, &super, &categ); | |
20156 | protos = cp_parser_objc_protocol_refs_opt (parser); | |
20157 | ||
20158 | /* We have either a class or a category on our hands. */ | |
20159 | if (categ) | |
20160 | objc_start_category_interface (name, categ, protos); | |
20161 | else | |
20162 | { | |
20163 | objc_start_class_interface (name, super, protos); | |
20164 | /* Handle instance variable declarations, if any. */ | |
20165 | cp_parser_objc_class_ivars (parser); | |
20166 | objc_continue_interface (); | |
20167 | } | |
20168 | ||
20169 | cp_parser_objc_method_prototype_list (parser); | |
20170 | } | |
20171 | ||
20172 | /* Parse an Objective-C class implementation. */ | |
20173 | ||
20174 | static void | |
20175 | cp_parser_objc_class_implementation (cp_parser* parser) | |
20176 | { | |
20177 | tree name, super, categ; | |
20178 | ||
20179 | cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */ | |
20180 | name = cp_parser_identifier (parser); | |
20181 | cp_parser_objc_superclass_or_category (parser, &super, &categ); | |
20182 | ||
20183 | /* We have either a class or a category on our hands. */ | |
20184 | if (categ) | |
20185 | objc_start_category_implementation (name, categ); | |
20186 | else | |
20187 | { | |
20188 | objc_start_class_implementation (name, super); | |
20189 | /* Handle instance variable declarations, if any. */ | |
20190 | cp_parser_objc_class_ivars (parser); | |
20191 | objc_continue_implementation (); | |
20192 | } | |
20193 | ||
20194 | cp_parser_objc_method_definition_list (parser); | |
20195 | } | |
20196 | ||
20197 | /* Consume the @end token and finish off the implementation. */ | |
20198 | ||
20199 | static void | |
20200 | cp_parser_objc_end_implementation (cp_parser* parser) | |
20201 | { | |
20202 | cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */ | |
20203 | objc_finish_implementation (); | |
20204 | } | |
20205 | ||
20206 | /* Parse an Objective-C declaration. */ | |
20207 | ||
20208 | static void | |
20209 | cp_parser_objc_declaration (cp_parser* parser) | |
20210 | { | |
20211 | /* Try to figure out what kind of declaration is present. */ | |
20212 | cp_token *kwd = cp_lexer_peek_token (parser->lexer); | |
20213 | ||
20214 | switch (kwd->keyword) | |
20215 | { | |
20216 | case RID_AT_ALIAS: | |
20217 | cp_parser_objc_alias_declaration (parser); | |
20218 | break; | |
20219 | case RID_AT_CLASS: | |
20220 | cp_parser_objc_class_declaration (parser); | |
20221 | break; | |
20222 | case RID_AT_PROTOCOL: | |
20223 | cp_parser_objc_protocol_declaration (parser); | |
20224 | break; | |
20225 | case RID_AT_INTERFACE: | |
20226 | cp_parser_objc_class_interface (parser); | |
20227 | break; | |
20228 | case RID_AT_IMPLEMENTATION: | |
20229 | cp_parser_objc_class_implementation (parser); | |
20230 | break; | |
20231 | case RID_AT_END: | |
20232 | cp_parser_objc_end_implementation (parser); | |
20233 | break; | |
20234 | default: | |
69bc6bff MLI |
20235 | error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct", |
20236 | kwd->u.value); | |
e58a9aa1 ZL |
20237 | cp_parser_skip_to_end_of_block_or_statement (parser); |
20238 | } | |
20239 | } | |
20240 | ||
20241 | /* Parse an Objective-C try-catch-finally statement. | |
20242 | ||
20243 | objc-try-catch-finally-stmt: | |
20244 | @try compound-statement objc-catch-clause-seq [opt] | |
20245 | objc-finally-clause [opt] | |
20246 | ||
20247 | objc-catch-clause-seq: | |
20248 | objc-catch-clause objc-catch-clause-seq [opt] | |
20249 | ||
20250 | objc-catch-clause: | |
20251 | @catch ( exception-declaration ) compound-statement | |
20252 | ||
20253 | objc-finally-clause | |
20254 | @finally compound-statement | |
20255 | ||
20256 | Returns NULL_TREE. */ | |
20257 | ||
20258 | static tree | |
20259 | cp_parser_objc_try_catch_finally_statement (cp_parser *parser) { | |
20260 | location_t location; | |
20261 | tree stmt; | |
20262 | ||
3a483371 | 20263 | cp_parser_require_keyword (parser, RID_AT_TRY, "%<@try%>"); |
e58a9aa1 ZL |
20264 | location = cp_lexer_peek_token (parser->lexer)->location; |
20265 | /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST | |
20266 | node, lest it get absorbed into the surrounding block. */ | |
20267 | stmt = push_stmt_list (); | |
20268 | cp_parser_compound_statement (parser, NULL, false); | |
20269 | objc_begin_try_stmt (location, pop_stmt_list (stmt)); | |
c8094d83 | 20270 | |
e58a9aa1 ZL |
20271 | while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH)) |
20272 | { | |
20273 | cp_parameter_declarator *parmdecl; | |
20274 | tree parm; | |
20275 | ||
20276 | cp_lexer_consume_token (parser->lexer); | |
3a483371 | 20277 | cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"); |
e58a9aa1 ZL |
20278 | parmdecl = cp_parser_parameter_declaration (parser, false, NULL); |
20279 | parm = grokdeclarator (parmdecl->declarator, | |
20280 | &parmdecl->decl_specifiers, | |
c8094d83 | 20281 | PARM, /*initialized=*/0, |
e58a9aa1 | 20282 | /*attrlist=*/NULL); |
3a483371 | 20283 | cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"); |
e58a9aa1 ZL |
20284 | objc_begin_catch_clause (parm); |
20285 | cp_parser_compound_statement (parser, NULL, false); | |
20286 | objc_finish_catch_clause (); | |
20287 | } | |
20288 | ||
20289 | if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY)) | |
20290 | { | |
20291 | cp_lexer_consume_token (parser->lexer); | |
20292 | location = cp_lexer_peek_token (parser->lexer)->location; | |
20293 | /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST | |
20294 | node, lest it get absorbed into the surrounding block. */ | |
20295 | stmt = push_stmt_list (); | |
20296 | cp_parser_compound_statement (parser, NULL, false); | |
20297 | objc_build_finally_clause (location, pop_stmt_list (stmt)); | |
20298 | } | |
20299 | ||
20300 | return objc_finish_try_stmt (); | |
20301 | } | |
20302 | ||
20303 | /* Parse an Objective-C synchronized statement. | |
20304 | ||
20305 | objc-synchronized-stmt: | |
20306 | @synchronized ( expression ) compound-statement | |
20307 | ||
20308 | Returns NULL_TREE. */ | |
20309 | ||
20310 | static tree | |
20311 | cp_parser_objc_synchronized_statement (cp_parser *parser) { | |
20312 | location_t location; | |
20313 | tree lock, stmt; | |
20314 | ||
3a483371 | 20315 | cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, "%<@synchronized%>"); |
e58a9aa1 ZL |
20316 | |
20317 | location = cp_lexer_peek_token (parser->lexer)->location; | |
3a483371 | 20318 | cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"); |
cee9035d | 20319 | lock = cp_parser_expression (parser, false, NULL); |
3a483371 | 20320 | cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"); |
e58a9aa1 ZL |
20321 | |
20322 | /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST | |
20323 | node, lest it get absorbed into the surrounding block. */ | |
20324 | stmt = push_stmt_list (); | |
20325 | cp_parser_compound_statement (parser, NULL, false); | |
20326 | ||
20327 | return objc_build_synchronized (location, lock, pop_stmt_list (stmt)); | |
20328 | } | |
20329 | ||
20330 | /* Parse an Objective-C throw statement. | |
20331 | ||
20332 | objc-throw-stmt: | |
20333 | @throw assignment-expression [opt] ; | |
20334 | ||
20335 | Returns a constructed '@throw' statement. */ | |
20336 | ||
20337 | static tree | |
20338 | cp_parser_objc_throw_statement (cp_parser *parser) { | |
20339 | tree expr = NULL_TREE; | |
c2255bc4 | 20340 | location_t loc = cp_lexer_peek_token (parser->lexer)->location; |
e58a9aa1 | 20341 | |
3a483371 | 20342 | cp_parser_require_keyword (parser, RID_AT_THROW, "%<@throw%>"); |
e58a9aa1 ZL |
20343 | |
20344 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)) | |
cee9035d | 20345 | expr = cp_parser_assignment_expression (parser, false, NULL); |
e58a9aa1 ZL |
20346 | |
20347 | cp_parser_consume_semicolon_at_end_of_statement (parser); | |
20348 | ||
c2255bc4 | 20349 | return objc_build_throw_stmt (loc, expr); |
e58a9aa1 ZL |
20350 | } |
20351 | ||
20352 | /* Parse an Objective-C statement. */ | |
20353 | ||
20354 | static tree | |
20355 | cp_parser_objc_statement (cp_parser * parser) { | |
20356 | /* Try to figure out what kind of declaration is present. */ | |
20357 | cp_token *kwd = cp_lexer_peek_token (parser->lexer); | |
20358 | ||
20359 | switch (kwd->keyword) | |
20360 | { | |
20361 | case RID_AT_TRY: | |
20362 | return cp_parser_objc_try_catch_finally_statement (parser); | |
20363 | case RID_AT_SYNCHRONIZED: | |
20364 | return cp_parser_objc_synchronized_statement (parser); | |
20365 | case RID_AT_THROW: | |
20366 | return cp_parser_objc_throw_statement (parser); | |
20367 | default: | |
69bc6bff MLI |
20368 | error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct", |
20369 | kwd->u.value); | |
e58a9aa1 ZL |
20370 | cp_parser_skip_to_end_of_block_or_statement (parser); |
20371 | } | |
20372 | ||
20373 | return error_mark_node; | |
20374 | } | |
1799e5d5 RH |
20375 | \f |
20376 | /* OpenMP 2.5 parsing routines. */ | |
20377 | ||
1799e5d5 RH |
20378 | /* Returns name of the next clause. |
20379 | If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and | |
20380 | the token is not consumed. Otherwise appropriate pragma_omp_clause is | |
20381 | returned and the token is consumed. */ | |
20382 | ||
20383 | static pragma_omp_clause | |
20384 | cp_parser_omp_clause_name (cp_parser *parser) | |
20385 | { | |
20386 | pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE; | |
20387 | ||
20388 | if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF)) | |
20389 | result = PRAGMA_OMP_CLAUSE_IF; | |
20390 | else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT)) | |
20391 | result = PRAGMA_OMP_CLAUSE_DEFAULT; | |
20392 | else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE)) | |
20393 | result = PRAGMA_OMP_CLAUSE_PRIVATE; | |
20394 | else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)) | |
20395 | { | |
d6b418fa | 20396 | tree id = cp_lexer_peek_token (parser->lexer)->u.value; |
1799e5d5 RH |
20397 | const char *p = IDENTIFIER_POINTER (id); |
20398 | ||
20399 | switch (p[0]) | |
20400 | { | |
20401 | case 'c': | |
a68ab351 JJ |
20402 | if (!strcmp ("collapse", p)) |
20403 | result = PRAGMA_OMP_CLAUSE_COLLAPSE; | |
20404 | else if (!strcmp ("copyin", p)) | |
1799e5d5 | 20405 | result = PRAGMA_OMP_CLAUSE_COPYIN; |
3db45ab5 | 20406 | else if (!strcmp ("copyprivate", p)) |
1799e5d5 RH |
20407 | result = PRAGMA_OMP_CLAUSE_COPYPRIVATE; |
20408 | break; | |
20409 | case 'f': | |
20410 | if (!strcmp ("firstprivate", p)) | |
20411 | result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE; | |
20412 | break; | |
20413 | case 'l': | |
20414 | if (!strcmp ("lastprivate", p)) | |
20415 | result = PRAGMA_OMP_CLAUSE_LASTPRIVATE; | |
20416 | break; | |
20417 | case 'n': | |
20418 | if (!strcmp ("nowait", p)) | |
20419 | result = PRAGMA_OMP_CLAUSE_NOWAIT; | |
20420 | else if (!strcmp ("num_threads", p)) | |
20421 | result = PRAGMA_OMP_CLAUSE_NUM_THREADS; | |
20422 | break; | |
20423 | case 'o': | |
20424 | if (!strcmp ("ordered", p)) | |
20425 | result = PRAGMA_OMP_CLAUSE_ORDERED; | |
20426 | break; | |
20427 | case 'r': | |
20428 | if (!strcmp ("reduction", p)) | |
20429 | result = PRAGMA_OMP_CLAUSE_REDUCTION; | |
20430 | break; | |
20431 | case 's': | |
20432 | if (!strcmp ("schedule", p)) | |
20433 | result = PRAGMA_OMP_CLAUSE_SCHEDULE; | |
20434 | else if (!strcmp ("shared", p)) | |
20435 | result = PRAGMA_OMP_CLAUSE_SHARED; | |
20436 | break; | |
a68ab351 JJ |
20437 | case 'u': |
20438 | if (!strcmp ("untied", p)) | |
20439 | result = PRAGMA_OMP_CLAUSE_UNTIED; | |
20440 | break; | |
1799e5d5 RH |
20441 | } |
20442 | } | |
a723baf1 | 20443 | |
1799e5d5 RH |
20444 | if (result != PRAGMA_OMP_CLAUSE_NONE) |
20445 | cp_lexer_consume_token (parser->lexer); | |
a723baf1 | 20446 | |
1799e5d5 RH |
20447 | return result; |
20448 | } | |
bc4071dd | 20449 | |
1799e5d5 | 20450 | /* Validate that a clause of the given type does not already exist. */ |
bc4071dd RH |
20451 | |
20452 | static void | |
e0c68ce9 | 20453 | check_no_duplicate_clause (tree clauses, enum omp_clause_code code, |
2b7a3abf | 20454 | const char *name, location_t location) |
bc4071dd | 20455 | { |
1799e5d5 | 20456 | tree c; |
bc4071dd | 20457 | |
1799e5d5 RH |
20458 | for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c)) |
20459 | if (OMP_CLAUSE_CODE (c) == code) | |
20460 | { | |
69bc6bff | 20461 | error_at (location, "too many %qs clauses", name); |
1799e5d5 RH |
20462 | break; |
20463 | } | |
20464 | } | |
bc4071dd | 20465 | |
1799e5d5 RH |
20466 | /* OpenMP 2.5: |
20467 | variable-list: | |
20468 | identifier | |
20469 | variable-list , identifier | |
20470 | ||
20471 | In addition, we match a closing parenthesis. An opening parenthesis | |
20472 | will have been consumed by the caller. | |
20473 | ||
20474 | If KIND is nonzero, create the appropriate node and install the decl | |
20475 | in OMP_CLAUSE_DECL and add the node to the head of the list. | |
20476 | ||
20477 | If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE; | |
20478 | return the list created. */ | |
20479 | ||
20480 | static tree | |
20481 | cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind, | |
20482 | tree list) | |
20483 | { | |
2b7a3abf | 20484 | cp_token *token; |
1799e5d5 | 20485 | while (1) |
bc4071dd | 20486 | { |
1799e5d5 | 20487 | tree name, decl; |
bc4071dd | 20488 | |
2b7a3abf | 20489 | token = cp_lexer_peek_token (parser->lexer); |
1799e5d5 RH |
20490 | name = cp_parser_id_expression (parser, /*template_p=*/false, |
20491 | /*check_dependency_p=*/true, | |
20492 | /*template_p=*/NULL, | |
fa6098f8 | 20493 | /*declarator_p=*/false, |
88525e1b | 20494 | /*optional_p=*/false); |
1799e5d5 RH |
20495 | if (name == error_mark_node) |
20496 | goto skip_comma; | |
20497 | ||
2b7a3abf | 20498 | decl = cp_parser_lookup_name_simple (parser, name, token->location); |
1799e5d5 | 20499 | if (decl == error_mark_node) |
2b7a3abf | 20500 | cp_parser_name_lookup_error (parser, name, decl, NULL, token->location); |
1799e5d5 RH |
20501 | else if (kind != 0) |
20502 | { | |
c2255bc4 | 20503 | tree u = build_omp_clause (token->location, kind); |
1799e5d5 RH |
20504 | OMP_CLAUSE_DECL (u) = decl; |
20505 | OMP_CLAUSE_CHAIN (u) = list; | |
20506 | list = u; | |
20507 | } | |
20508 | else | |
20509 | list = tree_cons (decl, NULL_TREE, list); | |
20510 | ||
20511 | get_comma: | |
20512 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)) | |
20513 | break; | |
20514 | cp_lexer_consume_token (parser->lexer); | |
20515 | } | |
20516 | ||
3a483371 | 20517 | if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>")) |
1799e5d5 RH |
20518 | { |
20519 | int ending; | |
20520 | ||
20521 | /* Try to resync to an unnested comma. Copied from | |
20522 | cp_parser_parenthesized_expression_list. */ | |
20523 | skip_comma: | |
20524 | ending = cp_parser_skip_to_closing_parenthesis (parser, | |
20525 | /*recovering=*/true, | |
20526 | /*or_comma=*/true, | |
20527 | /*consume_paren=*/true); | |
20528 | if (ending < 0) | |
20529 | goto get_comma; | |
20530 | } | |
20531 | ||
20532 | return list; | |
20533 | } | |
20534 | ||
20535 | /* Similarly, but expect leading and trailing parenthesis. This is a very | |
20536 | common case for omp clauses. */ | |
20537 | ||
20538 | static tree | |
20539 | cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list) | |
20540 | { | |
3a483371 | 20541 | if (cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>")) |
1799e5d5 RH |
20542 | return cp_parser_omp_var_list_no_open (parser, kind, list); |
20543 | return list; | |
20544 | } | |
20545 | ||
a68ab351 JJ |
20546 | /* OpenMP 3.0: |
20547 | collapse ( constant-expression ) */ | |
20548 | ||
20549 | static tree | |
2b7a3abf | 20550 | cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location) |
a68ab351 JJ |
20551 | { |
20552 | tree c, num; | |
20553 | location_t loc; | |
20554 | HOST_WIDE_INT n; | |
20555 | ||
20556 | loc = cp_lexer_peek_token (parser->lexer)->location; | |
20557 | if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>")) | |
20558 | return list; | |
20559 | ||
20560 | num = cp_parser_constant_expression (parser, false, NULL); | |
20561 | ||
20562 | if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>")) | |
20563 | cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true, | |
20564 | /*or_comma=*/false, | |
20565 | /*consume_paren=*/true); | |
20566 | ||
20567 | if (num == error_mark_node) | |
20568 | return list; | |
20569 | num = fold_non_dependent_expr (num); | |
20570 | if (!INTEGRAL_TYPE_P (TREE_TYPE (num)) | |
20571 | || !host_integerp (num, 0) | |
20572 | || (n = tree_low_cst (num, 0)) <= 0 | |
20573 | || (int) n != n) | |
20574 | { | |
69bc6bff | 20575 | error_at (loc, "collapse argument needs positive constant integer expression"); |
a68ab351 JJ |
20576 | return list; |
20577 | } | |
20578 | ||
2b7a3abf | 20579 | check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location); |
c2255bc4 | 20580 | c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE); |
a68ab351 JJ |
20581 | OMP_CLAUSE_CHAIN (c) = list; |
20582 | OMP_CLAUSE_COLLAPSE_EXPR (c) = num; | |
20583 | ||
20584 | return c; | |
20585 | } | |
20586 | ||
1799e5d5 RH |
20587 | /* OpenMP 2.5: |
20588 | default ( shared | none ) */ | |
20589 | ||
20590 | static tree | |
2b7a3abf | 20591 | cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location) |
1799e5d5 RH |
20592 | { |
20593 | enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED; | |
20594 | tree c; | |
20595 | ||
3a483371 | 20596 | if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>")) |
1799e5d5 RH |
20597 | return list; |
20598 | if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)) | |
20599 | { | |
d6b418fa | 20600 | tree id = cp_lexer_peek_token (parser->lexer)->u.value; |
1799e5d5 RH |
20601 | const char *p = IDENTIFIER_POINTER (id); |
20602 | ||
20603 | switch (p[0]) | |
20604 | { | |
20605 | case 'n': | |
20606 | if (strcmp ("none", p) != 0) | |
20607 | goto invalid_kind; | |
20608 | kind = OMP_CLAUSE_DEFAULT_NONE; | |
20609 | break; | |
20610 | ||
20611 | case 's': | |
20612 | if (strcmp ("shared", p) != 0) | |
20613 | goto invalid_kind; | |
20614 | kind = OMP_CLAUSE_DEFAULT_SHARED; | |
20615 | break; | |
20616 | ||
20617 | default: | |
20618 | goto invalid_kind; | |
20619 | } | |
20620 | ||
20621 | cp_lexer_consume_token (parser->lexer); | |
bc4071dd RH |
20622 | } |
20623 | else | |
1799e5d5 RH |
20624 | { |
20625 | invalid_kind: | |
20626 | cp_parser_error (parser, "expected %<none%> or %<shared%>"); | |
20627 | } | |
bc4071dd | 20628 | |
3a483371 | 20629 | if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>")) |
1799e5d5 RH |
20630 | cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true, |
20631 | /*or_comma=*/false, | |
20632 | /*consume_paren=*/true); | |
3db45ab5 | 20633 | |
1799e5d5 RH |
20634 | if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED) |
20635 | return list; | |
bc4071dd | 20636 | |
2b7a3abf | 20637 | check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location); |
c2255bc4 | 20638 | c = build_omp_clause (location, OMP_CLAUSE_DEFAULT); |
1799e5d5 RH |
20639 | OMP_CLAUSE_CHAIN (c) = list; |
20640 | OMP_CLAUSE_DEFAULT_KIND (c) = kind; | |
bc4071dd | 20641 | |
1799e5d5 | 20642 | return c; |
bc4071dd RH |
20643 | } |
20644 | ||
1799e5d5 RH |
20645 | /* OpenMP 2.5: |
20646 | if ( expression ) */ | |
bc4071dd | 20647 | |
1799e5d5 | 20648 | static tree |
2b7a3abf | 20649 | cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location) |
bc4071dd | 20650 | { |
1799e5d5 | 20651 | tree t, c; |
bc4071dd | 20652 | |
3a483371 | 20653 | if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>")) |
1799e5d5 | 20654 | return list; |
bc4071dd | 20655 | |
1799e5d5 RH |
20656 | t = cp_parser_condition (parser); |
20657 | ||
20658 | if (t == error_mark_node | |
3a483371 | 20659 | || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>")) |
1799e5d5 RH |
20660 | cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true, |
20661 | /*or_comma=*/false, | |
20662 | /*consume_paren=*/true); | |
20663 | ||
2b7a3abf | 20664 | check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location); |
1799e5d5 | 20665 | |
c2255bc4 | 20666 | c = build_omp_clause (location, OMP_CLAUSE_IF); |
1799e5d5 RH |
20667 | OMP_CLAUSE_IF_EXPR (c) = t; |
20668 | OMP_CLAUSE_CHAIN (c) = list; | |
20669 | ||
20670 | return c; | |
20671 | } | |
20672 | ||
20673 | /* OpenMP 2.5: | |
20674 | nowait */ | |
20675 | ||
20676 | static tree | |
2b7a3abf DS |
20677 | cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED, |
20678 | tree list, location_t location) | |
1799e5d5 RH |
20679 | { |
20680 | tree c; | |
20681 | ||
2b7a3abf | 20682 | check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location); |
1799e5d5 | 20683 | |
c2255bc4 | 20684 | c = build_omp_clause (location, OMP_CLAUSE_NOWAIT); |
1799e5d5 RH |
20685 | OMP_CLAUSE_CHAIN (c) = list; |
20686 | return c; | |
20687 | } | |
20688 | ||
20689 | /* OpenMP 2.5: | |
20690 | num_threads ( expression ) */ | |
20691 | ||
20692 | static tree | |
2b7a3abf DS |
20693 | cp_parser_omp_clause_num_threads (cp_parser *parser, tree list, |
20694 | location_t location) | |
1799e5d5 RH |
20695 | { |
20696 | tree t, c; | |
20697 | ||
3a483371 | 20698 | if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>")) |
1799e5d5 RH |
20699 | return list; |
20700 | ||
cee9035d | 20701 | t = cp_parser_expression (parser, false, NULL); |
1799e5d5 RH |
20702 | |
20703 | if (t == error_mark_node | |
3a483371 | 20704 | || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>")) |
1799e5d5 RH |
20705 | cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true, |
20706 | /*or_comma=*/false, | |
20707 | /*consume_paren=*/true); | |
20708 | ||
2b7a3abf DS |
20709 | check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS, |
20710 | "num_threads", location); | |
1799e5d5 | 20711 | |
c2255bc4 | 20712 | c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS); |
1799e5d5 RH |
20713 | OMP_CLAUSE_NUM_THREADS_EXPR (c) = t; |
20714 | OMP_CLAUSE_CHAIN (c) = list; | |
20715 | ||
20716 | return c; | |
20717 | } | |
20718 | ||
20719 | /* OpenMP 2.5: | |
20720 | ordered */ | |
20721 | ||
20722 | static tree | |
2b7a3abf DS |
20723 | cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED, |
20724 | tree list, location_t location) | |
1799e5d5 RH |
20725 | { |
20726 | tree c; | |
20727 | ||
2b7a3abf DS |
20728 | check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED, |
20729 | "ordered", location); | |
1799e5d5 | 20730 | |
c2255bc4 | 20731 | c = build_omp_clause (location, OMP_CLAUSE_ORDERED); |
1799e5d5 RH |
20732 | OMP_CLAUSE_CHAIN (c) = list; |
20733 | return c; | |
20734 | } | |
20735 | ||
20736 | /* OpenMP 2.5: | |
20737 | reduction ( reduction-operator : variable-list ) | |
20738 | ||
20739 | reduction-operator: | |
20740 | One of: + * - & ^ | && || */ | |
20741 | ||
20742 | static tree | |
20743 | cp_parser_omp_clause_reduction (cp_parser *parser, tree list) | |
20744 | { | |
20745 | enum tree_code code; | |
20746 | tree nlist, c; | |
20747 | ||
3a483371 | 20748 | if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>")) |
1799e5d5 RH |
20749 | return list; |
20750 | ||
20751 | switch (cp_lexer_peek_token (parser->lexer)->type) | |
bc4071dd | 20752 | { |
1799e5d5 RH |
20753 | case CPP_PLUS: |
20754 | code = PLUS_EXPR; | |
20755 | break; | |
20756 | case CPP_MULT: | |
20757 | code = MULT_EXPR; | |
20758 | break; | |
20759 | case CPP_MINUS: | |
20760 | code = MINUS_EXPR; | |
20761 | break; | |
20762 | case CPP_AND: | |
20763 | code = BIT_AND_EXPR; | |
20764 | break; | |
20765 | case CPP_XOR: | |
20766 | code = BIT_XOR_EXPR; | |
20767 | break; | |
20768 | case CPP_OR: | |
20769 | code = BIT_IOR_EXPR; | |
20770 | break; | |
20771 | case CPP_AND_AND: | |
20772 | code = TRUTH_ANDIF_EXPR; | |
20773 | break; | |
20774 | case CPP_OR_OR: | |
20775 | code = TRUTH_ORIF_EXPR; | |
bc4071dd | 20776 | break; |
bc4071dd | 20777 | default: |
8ffb4ce0 VR |
20778 | cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, " |
20779 | "%<|%>, %<&&%>, or %<||%>"); | |
1799e5d5 RH |
20780 | resync_fail: |
20781 | cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true, | |
20782 | /*or_comma=*/false, | |
20783 | /*consume_paren=*/true); | |
20784 | return list; | |
20785 | } | |
20786 | cp_lexer_consume_token (parser->lexer); | |
20787 | ||
3a483371 | 20788 | if (!cp_parser_require (parser, CPP_COLON, "%<:%>")) |
1799e5d5 RH |
20789 | goto resync_fail; |
20790 | ||
20791 | nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list); | |
20792 | for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c)) | |
20793 | OMP_CLAUSE_REDUCTION_CODE (c) = code; | |
20794 | ||
20795 | return nlist; | |
20796 | } | |
20797 | ||
20798 | /* OpenMP 2.5: | |
20799 | schedule ( schedule-kind ) | |
20800 | schedule ( schedule-kind , expression ) | |
20801 | ||
20802 | schedule-kind: | |
a68ab351 | 20803 | static | dynamic | guided | runtime | auto */ |
1799e5d5 RH |
20804 | |
20805 | static tree | |
2b7a3abf | 20806 | cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location) |
1799e5d5 RH |
20807 | { |
20808 | tree c, t; | |
20809 | ||
8ffb4ce0 | 20810 | if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>")) |
1799e5d5 RH |
20811 | return list; |
20812 | ||
c2255bc4 | 20813 | c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE); |
1799e5d5 RH |
20814 | |
20815 | if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)) | |
20816 | { | |
d6b418fa | 20817 | tree id = cp_lexer_peek_token (parser->lexer)->u.value; |
1799e5d5 RH |
20818 | const char *p = IDENTIFIER_POINTER (id); |
20819 | ||
20820 | switch (p[0]) | |
20821 | { | |
20822 | case 'd': | |
20823 | if (strcmp ("dynamic", p) != 0) | |
20824 | goto invalid_kind; | |
20825 | OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC; | |
20826 | break; | |
20827 | ||
3db45ab5 | 20828 | case 'g': |
1799e5d5 RH |
20829 | if (strcmp ("guided", p) != 0) |
20830 | goto invalid_kind; | |
20831 | OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED; | |
20832 | break; | |
20833 | ||
20834 | case 'r': | |
20835 | if (strcmp ("runtime", p) != 0) | |
20836 | goto invalid_kind; | |
20837 | OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME; | |
20838 | break; | |
20839 | ||
20840 | default: | |
20841 | goto invalid_kind; | |
20842 | } | |
20843 | } | |
20844 | else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC)) | |
20845 | OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC; | |
a68ab351 JJ |
20846 | else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO)) |
20847 | OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO; | |
1799e5d5 RH |
20848 | else |
20849 | goto invalid_kind; | |
20850 | cp_lexer_consume_token (parser->lexer); | |
20851 | ||
20852 | if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)) | |
20853 | { | |
2b7a3abf | 20854 | cp_token *token; |
1799e5d5 RH |
20855 | cp_lexer_consume_token (parser->lexer); |
20856 | ||
2b7a3abf | 20857 | token = cp_lexer_peek_token (parser->lexer); |
cee9035d | 20858 | t = cp_parser_assignment_expression (parser, false, NULL); |
1799e5d5 RH |
20859 | |
20860 | if (t == error_mark_node) | |
20861 | goto resync_fail; | |
20862 | else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME) | |
69bc6bff MLI |
20863 | error_at (token->location, "schedule %<runtime%> does not take " |
20864 | "a %<chunk_size%> parameter"); | |
a68ab351 | 20865 | else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO) |
69bc6bff MLI |
20866 | error_at (token->location, "schedule %<auto%> does not take " |
20867 | "a %<chunk_size%> parameter"); | |
1799e5d5 RH |
20868 | else |
20869 | OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t; | |
20870 | ||
3a483371 | 20871 | if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>")) |
1799e5d5 RH |
20872 | goto resync_fail; |
20873 | } | |
3a483371 | 20874 | else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<,%> or %<)%>")) |
1799e5d5 RH |
20875 | goto resync_fail; |
20876 | ||
2b7a3abf | 20877 | check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location); |
1799e5d5 RH |
20878 | OMP_CLAUSE_CHAIN (c) = list; |
20879 | return c; | |
20880 | ||
20881 | invalid_kind: | |
20882 | cp_parser_error (parser, "invalid schedule kind"); | |
20883 | resync_fail: | |
20884 | cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true, | |
20885 | /*or_comma=*/false, | |
20886 | /*consume_paren=*/true); | |
20887 | return list; | |
20888 | } | |
20889 | ||
a68ab351 JJ |
20890 | /* OpenMP 3.0: |
20891 | untied */ | |
20892 | ||
20893 | static tree | |
2b7a3abf DS |
20894 | cp_parser_omp_clause_untied (cp_parser *parser ATTRIBUTE_UNUSED, |
20895 | tree list, location_t location) | |
a68ab351 JJ |
20896 | { |
20897 | tree c; | |
20898 | ||
2b7a3abf | 20899 | check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location); |
a68ab351 | 20900 | |
c2255bc4 | 20901 | c = build_omp_clause (location, OMP_CLAUSE_UNTIED); |
a68ab351 JJ |
20902 | OMP_CLAUSE_CHAIN (c) = list; |
20903 | return c; | |
20904 | } | |
20905 | ||
1799e5d5 RH |
20906 | /* Parse all OpenMP clauses. The set clauses allowed by the directive |
20907 | is a bitmask in MASK. Return the list of clauses found; the result | |
20908 | of clause default goes in *pdefault. */ | |
20909 | ||
20910 | static tree | |
20911 | cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask, | |
20912 | const char *where, cp_token *pragma_tok) | |
20913 | { | |
20914 | tree clauses = NULL; | |
8085ca15 | 20915 | bool first = true; |
2b7a3abf | 20916 | cp_token *token = NULL; |
1799e5d5 RH |
20917 | |
20918 | while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)) | |
20919 | { | |
8085ca15 | 20920 | pragma_omp_clause c_kind; |
1799e5d5 RH |
20921 | const char *c_name; |
20922 | tree prev = clauses; | |
20923 | ||
8085ca15 JJ |
20924 | if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA)) |
20925 | cp_lexer_consume_token (parser->lexer); | |
20926 | ||
2b7a3abf | 20927 | token = cp_lexer_peek_token (parser->lexer); |
8085ca15 JJ |
20928 | c_kind = cp_parser_omp_clause_name (parser); |
20929 | first = false; | |
20930 | ||
1799e5d5 RH |
20931 | switch (c_kind) |
20932 | { | |
a68ab351 | 20933 | case PRAGMA_OMP_CLAUSE_COLLAPSE: |
2b7a3abf DS |
20934 | clauses = cp_parser_omp_clause_collapse (parser, clauses, |
20935 | token->location); | |
a68ab351 JJ |
20936 | c_name = "collapse"; |
20937 | break; | |
1799e5d5 RH |
20938 | case PRAGMA_OMP_CLAUSE_COPYIN: |
20939 | clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses); | |
20940 | c_name = "copyin"; | |
20941 | break; | |
20942 | case PRAGMA_OMP_CLAUSE_COPYPRIVATE: | |
20943 | clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE, | |
20944 | clauses); | |
20945 | c_name = "copyprivate"; | |
20946 | break; | |
20947 | case PRAGMA_OMP_CLAUSE_DEFAULT: | |
2b7a3abf DS |
20948 | clauses = cp_parser_omp_clause_default (parser, clauses, |
20949 | token->location); | |
1799e5d5 RH |
20950 | c_name = "default"; |
20951 | break; | |
20952 | case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE: | |
20953 | clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE, | |
20954 | clauses); | |
20955 | c_name = "firstprivate"; | |
20956 | break; | |
20957 | case PRAGMA_OMP_CLAUSE_IF: | |
2b7a3abf | 20958 | clauses = cp_parser_omp_clause_if (parser, clauses, token->location); |
1799e5d5 RH |
20959 | c_name = "if"; |
20960 | break; | |
20961 | case PRAGMA_OMP_CLAUSE_LASTPRIVATE: | |
20962 | clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE, | |
20963 | clauses); | |
20964 | c_name = "lastprivate"; | |
20965 | break; | |
20966 | case PRAGMA_OMP_CLAUSE_NOWAIT: | |
2b7a3abf | 20967 | clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location); |
1799e5d5 RH |
20968 | c_name = "nowait"; |
20969 | break; | |
20970 | case PRAGMA_OMP_CLAUSE_NUM_THREADS: | |
2b7a3abf DS |
20971 | clauses = cp_parser_omp_clause_num_threads (parser, clauses, |
20972 | token->location); | |
1799e5d5 RH |
20973 | c_name = "num_threads"; |
20974 | break; | |
20975 | case PRAGMA_OMP_CLAUSE_ORDERED: | |
2b7a3abf DS |
20976 | clauses = cp_parser_omp_clause_ordered (parser, clauses, |
20977 | token->location); | |
1799e5d5 RH |
20978 | c_name = "ordered"; |
20979 | break; | |
20980 | case PRAGMA_OMP_CLAUSE_PRIVATE: | |
20981 | clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE, | |
20982 | clauses); | |
20983 | c_name = "private"; | |
20984 | break; | |
20985 | case PRAGMA_OMP_CLAUSE_REDUCTION: | |
20986 | clauses = cp_parser_omp_clause_reduction (parser, clauses); | |
20987 | c_name = "reduction"; | |
20988 | break; | |
20989 | case PRAGMA_OMP_CLAUSE_SCHEDULE: | |
2b7a3abf DS |
20990 | clauses = cp_parser_omp_clause_schedule (parser, clauses, |
20991 | token->location); | |
1799e5d5 RH |
20992 | c_name = "schedule"; |
20993 | break; | |
20994 | case PRAGMA_OMP_CLAUSE_SHARED: | |
20995 | clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED, | |
20996 | clauses); | |
20997 | c_name = "shared"; | |
20998 | break; | |
a68ab351 | 20999 | case PRAGMA_OMP_CLAUSE_UNTIED: |
2b7a3abf DS |
21000 | clauses = cp_parser_omp_clause_untied (parser, clauses, |
21001 | token->location); | |
a68ab351 JJ |
21002 | c_name = "nowait"; |
21003 | break; | |
1799e5d5 RH |
21004 | default: |
21005 | cp_parser_error (parser, "expected %<#pragma omp%> clause"); | |
21006 | goto saw_error; | |
21007 | } | |
21008 | ||
21009 | if (((mask >> c_kind) & 1) == 0) | |
21010 | { | |
21011 | /* Remove the invalid clause(s) from the list to avoid | |
21012 | confusing the rest of the compiler. */ | |
21013 | clauses = prev; | |
69bc6bff | 21014 | error_at (token->location, "%qs is not valid for %qs", c_name, where); |
1799e5d5 RH |
21015 | } |
21016 | } | |
21017 | saw_error: | |
21018 | cp_parser_skip_to_pragma_eol (parser, pragma_tok); | |
21019 | return finish_omp_clauses (clauses); | |
21020 | } | |
21021 | ||
21022 | /* OpenMP 2.5: | |
21023 | structured-block: | |
21024 | statement | |
21025 | ||
21026 | In practice, we're also interested in adding the statement to an | |
21027 | outer node. So it is convenient if we work around the fact that | |
21028 | cp_parser_statement calls add_stmt. */ | |
21029 | ||
21030 | static unsigned | |
21031 | cp_parser_begin_omp_structured_block (cp_parser *parser) | |
21032 | { | |
21033 | unsigned save = parser->in_statement; | |
21034 | ||
21035 | /* Only move the values to IN_OMP_BLOCK if they weren't false. | |
21036 | This preserves the "not within loop or switch" style error messages | |
21037 | for nonsense cases like | |
21038 | void foo() { | |
21039 | #pragma omp single | |
21040 | break; | |
21041 | } | |
21042 | */ | |
21043 | if (parser->in_statement) | |
21044 | parser->in_statement = IN_OMP_BLOCK; | |
21045 | ||
21046 | return save; | |
21047 | } | |
21048 | ||
21049 | static void | |
21050 | cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save) | |
21051 | { | |
21052 | parser->in_statement = save; | |
21053 | } | |
21054 | ||
21055 | static tree | |
21056 | cp_parser_omp_structured_block (cp_parser *parser) | |
21057 | { | |
21058 | tree stmt = begin_omp_structured_block (); | |
21059 | unsigned int save = cp_parser_begin_omp_structured_block (parser); | |
21060 | ||
2a67bec2 | 21061 | cp_parser_statement (parser, NULL_TREE, false, NULL); |
1799e5d5 RH |
21062 | |
21063 | cp_parser_end_omp_structured_block (parser, save); | |
21064 | return finish_omp_structured_block (stmt); | |
21065 | } | |
21066 | ||
21067 | /* OpenMP 2.5: | |
21068 | # pragma omp atomic new-line | |
21069 | expression-stmt | |
21070 | ||
21071 | expression-stmt: | |
21072 | x binop= expr | x++ | ++x | x-- | --x | |
21073 | binop: | |
21074 | +, *, -, /, &, ^, |, <<, >> | |
21075 | ||
21076 | where x is an lvalue expression with scalar type. */ | |
21077 | ||
21078 | static void | |
21079 | cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok) | |
21080 | { | |
21081 | tree lhs, rhs; | |
21082 | enum tree_code code; | |
21083 | ||
21084 | cp_parser_require_pragma_eol (parser, pragma_tok); | |
21085 | ||
21086 | lhs = cp_parser_unary_expression (parser, /*address_p=*/false, | |
cee9035d | 21087 | /*cast_p=*/false, NULL); |
1799e5d5 RH |
21088 | switch (TREE_CODE (lhs)) |
21089 | { | |
21090 | case ERROR_MARK: | |
21091 | goto saw_error; | |
21092 | ||
21093 | case PREINCREMENT_EXPR: | |
21094 | case POSTINCREMENT_EXPR: | |
21095 | lhs = TREE_OPERAND (lhs, 0); | |
21096 | code = PLUS_EXPR; | |
21097 | rhs = integer_one_node; | |
21098 | break; | |
21099 | ||
21100 | case PREDECREMENT_EXPR: | |
21101 | case POSTDECREMENT_EXPR: | |
21102 | lhs = TREE_OPERAND (lhs, 0); | |
21103 | code = MINUS_EXPR; | |
21104 | rhs = integer_one_node; | |
21105 | break; | |
21106 | ||
21107 | default: | |
21108 | switch (cp_lexer_peek_token (parser->lexer)->type) | |
21109 | { | |
21110 | case CPP_MULT_EQ: | |
21111 | code = MULT_EXPR; | |
21112 | break; | |
21113 | case CPP_DIV_EQ: | |
21114 | code = TRUNC_DIV_EXPR; | |
21115 | break; | |
21116 | case CPP_PLUS_EQ: | |
21117 | code = PLUS_EXPR; | |
21118 | break; | |
21119 | case CPP_MINUS_EQ: | |
21120 | code = MINUS_EXPR; | |
21121 | break; | |
21122 | case CPP_LSHIFT_EQ: | |
21123 | code = LSHIFT_EXPR; | |
21124 | break; | |
21125 | case CPP_RSHIFT_EQ: | |
21126 | code = RSHIFT_EXPR; | |
21127 | break; | |
21128 | case CPP_AND_EQ: | |
21129 | code = BIT_AND_EXPR; | |
21130 | break; | |
21131 | case CPP_OR_EQ: | |
21132 | code = BIT_IOR_EXPR; | |
21133 | break; | |
21134 | case CPP_XOR_EQ: | |
21135 | code = BIT_XOR_EXPR; | |
21136 | break; | |
21137 | default: | |
21138 | cp_parser_error (parser, | |
21139 | "invalid operator for %<#pragma omp atomic%>"); | |
21140 | goto saw_error; | |
21141 | } | |
21142 | cp_lexer_consume_token (parser->lexer); | |
21143 | ||
cee9035d | 21144 | rhs = cp_parser_expression (parser, false, NULL); |
1799e5d5 RH |
21145 | if (rhs == error_mark_node) |
21146 | goto saw_error; | |
21147 | break; | |
21148 | } | |
21149 | finish_omp_atomic (code, lhs, rhs); | |
21150 | cp_parser_consume_semicolon_at_end_of_statement (parser); | |
21151 | return; | |
21152 | ||
21153 | saw_error: | |
21154 | cp_parser_skip_to_end_of_block_or_statement (parser); | |
21155 | } | |
21156 | ||
21157 | ||
21158 | /* OpenMP 2.5: | |
3db45ab5 | 21159 | # pragma omp barrier new-line */ |
1799e5d5 RH |
21160 | |
21161 | static void | |
21162 | cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok) | |
21163 | { | |
21164 | cp_parser_require_pragma_eol (parser, pragma_tok); | |
21165 | finish_omp_barrier (); | |
21166 | } | |
21167 | ||
21168 | /* OpenMP 2.5: | |
21169 | # pragma omp critical [(name)] new-line | |
3db45ab5 | 21170 | structured-block */ |
1799e5d5 RH |
21171 | |
21172 | static tree | |
21173 | cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok) | |
21174 | { | |
21175 | tree stmt, name = NULL; | |
21176 | ||
21177 | if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)) | |
21178 | { | |
21179 | cp_lexer_consume_token (parser->lexer); | |
21180 | ||
21181 | name = cp_parser_identifier (parser); | |
3db45ab5 | 21182 | |
1799e5d5 | 21183 | if (name == error_mark_node |
3a483371 | 21184 | || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>")) |
1799e5d5 RH |
21185 | cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true, |
21186 | /*or_comma=*/false, | |
21187 | /*consume_paren=*/true); | |
21188 | if (name == error_mark_node) | |
21189 | name = NULL; | |
21190 | } | |
21191 | cp_parser_require_pragma_eol (parser, pragma_tok); | |
21192 | ||
21193 | stmt = cp_parser_omp_structured_block (parser); | |
c2255bc4 | 21194 | return c_finish_omp_critical (input_location, stmt, name); |
1799e5d5 RH |
21195 | } |
21196 | ||
21197 | /* OpenMP 2.5: | |
21198 | # pragma omp flush flush-vars[opt] new-line | |
21199 | ||
21200 | flush-vars: | |
21201 | ( variable-list ) */ | |
21202 | ||
21203 | static void | |
21204 | cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok) | |
21205 | { | |
21206 | if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)) | |
a916d97f | 21207 | (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL); |
1799e5d5 RH |
21208 | cp_parser_require_pragma_eol (parser, pragma_tok); |
21209 | ||
21210 | finish_omp_flush (); | |
21211 | } | |
21212 | ||
a68ab351 | 21213 | /* Helper function, to parse omp for increment expression. */ |
1799e5d5 RH |
21214 | |
21215 | static tree | |
a68ab351 | 21216 | cp_parser_omp_for_cond (cp_parser *parser, tree decl) |
1799e5d5 | 21217 | { |
c5cdb03f JJ |
21218 | tree cond = cp_parser_binary_expression (parser, false, true, |
21219 | PREC_NOT_OPERATOR, NULL); | |
21220 | bool overloaded_p; | |
1799e5d5 | 21221 | |
c5cdb03f JJ |
21222 | if (cond == error_mark_node |
21223 | || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)) | |
1799e5d5 | 21224 | { |
a68ab351 JJ |
21225 | cp_parser_skip_to_end_of_statement (parser); |
21226 | return error_mark_node; | |
1799e5d5 | 21227 | } |
1799e5d5 | 21228 | |
c5cdb03f | 21229 | switch (TREE_CODE (cond)) |
a68ab351 | 21230 | { |
a68ab351 JJ |
21231 | case GT_EXPR: |
21232 | case GE_EXPR: | |
c5cdb03f JJ |
21233 | case LT_EXPR: |
21234 | case LE_EXPR: | |
a68ab351 JJ |
21235 | break; |
21236 | default: | |
a68ab351 JJ |
21237 | return error_mark_node; |
21238 | } | |
21239 | ||
c5cdb03f JJ |
21240 | /* If decl is an iterator, preserve LHS and RHS of the relational |
21241 | expr until finish_omp_for. */ | |
21242 | if (decl | |
21243 | && (type_dependent_expression_p (decl) | |
21244 | || CLASS_TYPE_P (TREE_TYPE (decl)))) | |
21245 | return cond; | |
1799e5d5 | 21246 | |
c5cdb03f JJ |
21247 | return build_x_binary_op (TREE_CODE (cond), |
21248 | TREE_OPERAND (cond, 0), ERROR_MARK, | |
21249 | TREE_OPERAND (cond, 1), ERROR_MARK, | |
21250 | &overloaded_p, tf_warning_or_error); | |
a68ab351 | 21251 | } |
1799e5d5 | 21252 | |
a68ab351 JJ |
21253 | /* Helper function, to parse omp for increment expression. */ |
21254 | ||
21255 | static tree | |
21256 | cp_parser_omp_for_incr (cp_parser *parser, tree decl) | |
21257 | { | |
21258 | cp_token *token = cp_lexer_peek_token (parser->lexer); | |
21259 | enum tree_code op; | |
21260 | tree lhs, rhs; | |
21261 | cp_id_kind idk; | |
21262 | bool decl_first; | |
21263 | ||
21264 | if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS) | |
21265 | { | |
21266 | op = (token->type == CPP_PLUS_PLUS | |
21267 | ? PREINCREMENT_EXPR : PREDECREMENT_EXPR); | |
21268 | cp_lexer_consume_token (parser->lexer); | |
cee9035d | 21269 | lhs = cp_parser_cast_expression (parser, false, false, NULL); |
a68ab351 JJ |
21270 | if (lhs != decl) |
21271 | return error_mark_node; | |
21272 | return build2 (op, TREE_TYPE (decl), decl, NULL_TREE); | |
21273 | } | |
21274 | ||
21275 | lhs = cp_parser_primary_expression (parser, false, false, false, &idk); | |
21276 | if (lhs != decl) | |
21277 | return error_mark_node; | |
21278 | ||
21279 | token = cp_lexer_peek_token (parser->lexer); | |
21280 | if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS) | |
21281 | { | |
21282 | op = (token->type == CPP_PLUS_PLUS | |
21283 | ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR); | |
21284 | cp_lexer_consume_token (parser->lexer); | |
21285 | return build2 (op, TREE_TYPE (decl), decl, NULL_TREE); | |
21286 | } | |
21287 | ||
21288 | op = cp_parser_assignment_operator_opt (parser); | |
21289 | if (op == ERROR_MARK) | |
21290 | return error_mark_node; | |
21291 | ||
21292 | if (op != NOP_EXPR) | |
21293 | { | |
cee9035d | 21294 | rhs = cp_parser_assignment_expression (parser, false, NULL); |
a68ab351 JJ |
21295 | rhs = build2 (op, TREE_TYPE (decl), decl, rhs); |
21296 | return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs); | |
21297 | } | |
21298 | ||
c5cdb03f | 21299 | lhs = cp_parser_binary_expression (parser, false, false, |
cee9035d | 21300 | PREC_ADDITIVE_EXPRESSION, NULL); |
a68ab351 JJ |
21301 | token = cp_lexer_peek_token (parser->lexer); |
21302 | decl_first = lhs == decl; | |
21303 | if (decl_first) | |
21304 | lhs = NULL_TREE; | |
21305 | if (token->type != CPP_PLUS | |
21306 | && token->type != CPP_MINUS) | |
21307 | return error_mark_node; | |
21308 | ||
21309 | do | |
21310 | { | |
21311 | op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR; | |
21312 | cp_lexer_consume_token (parser->lexer); | |
c5cdb03f | 21313 | rhs = cp_parser_binary_expression (parser, false, false, |
cee9035d | 21314 | PREC_ADDITIVE_EXPRESSION, NULL); |
a68ab351 JJ |
21315 | token = cp_lexer_peek_token (parser->lexer); |
21316 | if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first) | |
1799e5d5 | 21317 | { |
a68ab351 JJ |
21318 | if (lhs == NULL_TREE) |
21319 | { | |
21320 | if (op == PLUS_EXPR) | |
21321 | lhs = rhs; | |
21322 | else | |
21323 | lhs = build_x_unary_op (NEGATE_EXPR, rhs, tf_warning_or_error); | |
21324 | } | |
21325 | else | |
21326 | lhs = build_x_binary_op (op, lhs, ERROR_MARK, rhs, ERROR_MARK, | |
21327 | NULL, tf_warning_or_error); | |
21328 | } | |
21329 | } | |
21330 | while (token->type == CPP_PLUS || token->type == CPP_MINUS); | |
1799e5d5 | 21331 | |
a68ab351 JJ |
21332 | if (!decl_first) |
21333 | { | |
21334 | if (rhs != decl || op == MINUS_EXPR) | |
21335 | return error_mark_node; | |
21336 | rhs = build2 (op, TREE_TYPE (decl), lhs, decl); | |
21337 | } | |
21338 | else | |
21339 | rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs); | |
21340 | ||
21341 | return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs); | |
21342 | } | |
21343 | ||
1cbc62c0 | 21344 | /* Parse the restricted form of the for statement allowed by OpenMP. */ |
a68ab351 JJ |
21345 | |
21346 | static tree | |
21347 | cp_parser_omp_for_loop (cp_parser *parser, tree clauses, tree *par_clauses) | |
21348 | { | |
21349 | tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret; | |
21350 | tree for_block = NULL_TREE, real_decl, initv, condv, incrv, declv; | |
21351 | tree this_pre_body, cl; | |
21352 | location_t loc_first; | |
21353 | bool collapse_err = false; | |
21354 | int i, collapse = 1, nbraces = 0; | |
21355 | ||
21356 | for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl)) | |
21357 | if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE) | |
21358 | collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0); | |
21359 | ||
21360 | gcc_assert (collapse >= 1); | |
21361 | ||
21362 | declv = make_tree_vec (collapse); | |
21363 | initv = make_tree_vec (collapse); | |
21364 | condv = make_tree_vec (collapse); | |
21365 | incrv = make_tree_vec (collapse); | |
21366 | ||
21367 | loc_first = cp_lexer_peek_token (parser->lexer)->location; | |
21368 | ||
21369 | for (i = 0; i < collapse; i++) | |
21370 | { | |
21371 | int bracecount = 0; | |
21372 | bool add_private_clause = false; | |
21373 | location_t loc; | |
21374 | ||
21375 | if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR)) | |
21376 | { | |
21377 | cp_parser_error (parser, "for statement expected"); | |
21378 | return NULL; | |
21379 | } | |
21380 | loc = cp_lexer_consume_token (parser->lexer)->location; | |
21381 | ||
21382 | if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>")) | |
21383 | return NULL; | |
21384 | ||
21385 | init = decl = real_decl = NULL; | |
21386 | this_pre_body = push_stmt_list (); | |
21387 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)) | |
21388 | { | |
0e03ef47 MLI |
21389 | /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too): |
21390 | ||
21391 | init-expr: | |
21392 | var = lb | |
21393 | integer-type var = lb | |
21394 | random-access-iterator-type var = lb | |
21395 | pointer-type var = lb | |
21396 | */ | |
a68ab351 JJ |
21397 | cp_decl_specifier_seq type_specifiers; |
21398 | ||
21399 | /* First, try to parse as an initialized declaration. See | |
21400 | cp_parser_condition, from whence the bulk of this is copied. */ | |
21401 | ||
21402 | cp_parser_parse_tentatively (parser); | |
21403 | cp_parser_type_specifier_seq (parser, /*is_condition=*/false, | |
21404 | &type_specifiers); | |
0e03ef47 | 21405 | if (cp_parser_parse_definitely (parser)) |
1799e5d5 | 21406 | { |
0e03ef47 MLI |
21407 | /* If parsing a type specifier seq succeeded, then this |
21408 | MUST be a initialized declaration. */ | |
a68ab351 JJ |
21409 | tree asm_specification, attributes; |
21410 | cp_declarator *declarator; | |
21411 | ||
21412 | declarator = cp_parser_declarator (parser, | |
21413 | CP_PARSER_DECLARATOR_NAMED, | |
21414 | /*ctor_dtor_or_conv_p=*/NULL, | |
21415 | /*parenthesized_p=*/NULL, | |
21416 | /*member_p=*/false); | |
21417 | attributes = cp_parser_attributes_opt (parser); | |
21418 | asm_specification = cp_parser_asm_specification_opt (parser); | |
21419 | ||
0e03ef47 MLI |
21420 | if (declarator == cp_error_declarator) |
21421 | cp_parser_skip_to_end_of_statement (parser); | |
21422 | ||
21423 | else | |
a68ab351 | 21424 | { |
a2d4cdc9 | 21425 | tree pushed_scope, auto_node; |
a68ab351 JJ |
21426 | |
21427 | decl = start_decl (declarator, &type_specifiers, | |
a2d4cdc9 | 21428 | SD_INITIALIZED, attributes, |
a68ab351 JJ |
21429 | /*prefix_attributes=*/NULL_TREE, |
21430 | &pushed_scope); | |
21431 | ||
a2d4cdc9 | 21432 | auto_node = type_uses_auto (TREE_TYPE (decl)); |
0e03ef47 MLI |
21433 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)) |
21434 | { | |
21435 | if (cp_lexer_next_token_is (parser->lexer, | |
21436 | CPP_OPEN_PAREN)) | |
21437 | error ("parenthesized initialization is not allowed in " | |
21438 | "OpenMP %<for%> loop"); | |
21439 | else | |
21440 | /* Trigger an error. */ | |
21441 | cp_parser_require (parser, CPP_EQ, "%<=%>"); | |
21442 | ||
21443 | init = error_mark_node; | |
21444 | cp_parser_skip_to_end_of_statement (parser); | |
21445 | } | |
21446 | else if (CLASS_TYPE_P (TREE_TYPE (decl)) | |
a2d4cdc9 JJ |
21447 | || type_dependent_expression_p (decl) |
21448 | || auto_node) | |
a68ab351 | 21449 | { |
09357846 | 21450 | bool is_direct_init, is_non_constant_init; |
a68ab351 JJ |
21451 | |
21452 | init = cp_parser_initializer (parser, | |
09357846 | 21453 | &is_direct_init, |
a68ab351 JJ |
21454 | &is_non_constant_init); |
21455 | ||
a77f94e2 | 21456 | if (auto_node && describable_type (init)) |
a2d4cdc9 JJ |
21457 | { |
21458 | TREE_TYPE (decl) | |
21459 | = do_auto_deduction (TREE_TYPE (decl), init, | |
21460 | auto_node); | |
21461 | ||
21462 | if (!CLASS_TYPE_P (TREE_TYPE (decl)) | |
21463 | && !type_dependent_expression_p (decl)) | |
21464 | goto non_class; | |
21465 | } | |
21466 | ||
a68ab351 JJ |
21467 | cp_finish_decl (decl, init, !is_non_constant_init, |
21468 | asm_specification, | |
21469 | LOOKUP_ONLYCONVERTING); | |
21470 | if (CLASS_TYPE_P (TREE_TYPE (decl))) | |
21471 | { | |
21472 | for_block | |
21473 | = tree_cons (NULL, this_pre_body, for_block); | |
21474 | init = NULL_TREE; | |
21475 | } | |
21476 | else | |
21477 | init = pop_stmt_list (this_pre_body); | |
21478 | this_pre_body = NULL_TREE; | |
21479 | } | |
21480 | else | |
21481 | { | |
0e03ef47 MLI |
21482 | /* Consume '='. */ |
21483 | cp_lexer_consume_token (parser->lexer); | |
cee9035d | 21484 | init = cp_parser_assignment_expression (parser, false, NULL); |
1799e5d5 | 21485 | |
a2d4cdc9 | 21486 | non_class: |
a68ab351 JJ |
21487 | if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE) |
21488 | init = error_mark_node; | |
21489 | else | |
21490 | cp_finish_decl (decl, NULL_TREE, | |
21491 | /*init_const_expr_p=*/false, | |
21492 | asm_specification, | |
21493 | LOOKUP_ONLYCONVERTING); | |
21494 | } | |
1799e5d5 | 21495 | |
a68ab351 JJ |
21496 | if (pushed_scope) |
21497 | pop_scope (pushed_scope); | |
21498 | } | |
21499 | } | |
0e03ef47 | 21500 | else |
a68ab351 JJ |
21501 | { |
21502 | cp_id_kind idk; | |
0e03ef47 MLI |
21503 | /* If parsing a type specifier sequence failed, then |
21504 | this MUST be a simple expression. */ | |
a68ab351 JJ |
21505 | cp_parser_parse_tentatively (parser); |
21506 | decl = cp_parser_primary_expression (parser, false, false, | |
21507 | false, &idk); | |
21508 | if (!cp_parser_error_occurred (parser) | |
21509 | && decl | |
21510 | && DECL_P (decl) | |
21511 | && CLASS_TYPE_P (TREE_TYPE (decl))) | |
21512 | { | |
21513 | tree rhs; | |
21514 | ||
21515 | cp_parser_parse_definitely (parser); | |
21516 | cp_parser_require (parser, CPP_EQ, "%<=%>"); | |
cee9035d | 21517 | rhs = cp_parser_assignment_expression (parser, false, NULL); |
a68ab351 JJ |
21518 | finish_expr_stmt (build_x_modify_expr (decl, NOP_EXPR, |
21519 | rhs, | |
21520 | tf_warning_or_error)); | |
21521 | add_private_clause = true; | |
21522 | } | |
dadb19e0 | 21523 | else |
a68ab351 JJ |
21524 | { |
21525 | decl = NULL; | |
21526 | cp_parser_abort_tentative_parse (parser); | |
cee9035d | 21527 | init = cp_parser_expression (parser, false, NULL); |
a68ab351 JJ |
21528 | if (init) |
21529 | { | |
21530 | if (TREE_CODE (init) == MODIFY_EXPR | |
21531 | || TREE_CODE (init) == MODOP_EXPR) | |
21532 | real_decl = TREE_OPERAND (init, 0); | |
21533 | } | |
21534 | } | |
21535 | } | |
21536 | } | |
21537 | cp_parser_require (parser, CPP_SEMICOLON, "%<;%>"); | |
21538 | if (this_pre_body) | |
21539 | { | |
21540 | this_pre_body = pop_stmt_list (this_pre_body); | |
21541 | if (pre_body) | |
21542 | { | |
21543 | tree t = pre_body; | |
21544 | pre_body = push_stmt_list (); | |
21545 | add_stmt (t); | |
21546 | add_stmt (this_pre_body); | |
21547 | pre_body = pop_stmt_list (pre_body); | |
21548 | } | |
21549 | else | |
21550 | pre_body = this_pre_body; | |
21551 | } | |
1799e5d5 | 21552 | |
a68ab351 JJ |
21553 | if (decl) |
21554 | real_decl = decl; | |
21555 | if (par_clauses != NULL && real_decl != NULL_TREE) | |
21556 | { | |
21557 | tree *c; | |
21558 | for (c = par_clauses; *c ; ) | |
21559 | if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE | |
21560 | && OMP_CLAUSE_DECL (*c) == real_decl) | |
21561 | { | |
69bc6bff MLI |
21562 | error_at (loc, "iteration variable %qD" |
21563 | " should not be firstprivate", real_decl); | |
a68ab351 JJ |
21564 | *c = OMP_CLAUSE_CHAIN (*c); |
21565 | } | |
21566 | else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE | |
21567 | && OMP_CLAUSE_DECL (*c) == real_decl) | |
21568 | { | |
21569 | /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES, | |
21570 | change it to shared (decl) in OMP_PARALLEL_CLAUSES. */ | |
c2255bc4 | 21571 | tree l = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE); |
a68ab351 JJ |
21572 | OMP_CLAUSE_DECL (l) = real_decl; |
21573 | OMP_CLAUSE_CHAIN (l) = clauses; | |
21574 | CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c); | |
21575 | clauses = l; | |
21576 | OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED); | |
21577 | CP_OMP_CLAUSE_INFO (*c) = NULL; | |
21578 | add_private_clause = false; | |
21579 | } | |
21580 | else | |
21581 | { | |
21582 | if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE | |
21583 | && OMP_CLAUSE_DECL (*c) == real_decl) | |
21584 | add_private_clause = false; | |
21585 | c = &OMP_CLAUSE_CHAIN (*c); | |
21586 | } | |
21587 | } | |
21588 | ||
21589 | if (add_private_clause) | |
21590 | { | |
21591 | tree c; | |
21592 | for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c)) | |
21593 | { | |
21594 | if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE | |
21595 | || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE) | |
21596 | && OMP_CLAUSE_DECL (c) == decl) | |
21597 | break; | |
21598 | else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE | |
21599 | && OMP_CLAUSE_DECL (c) == decl) | |
69bc6bff MLI |
21600 | error_at (loc, "iteration variable %qD " |
21601 | "should not be firstprivate", | |
21602 | decl); | |
a68ab351 JJ |
21603 | else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION |
21604 | && OMP_CLAUSE_DECL (c) == decl) | |
69bc6bff MLI |
21605 | error_at (loc, "iteration variable %qD should not be reduction", |
21606 | decl); | |
a68ab351 JJ |
21607 | } |
21608 | if (c == NULL) | |
21609 | { | |
c2255bc4 | 21610 | c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE); |
a68ab351 JJ |
21611 | OMP_CLAUSE_DECL (c) = decl; |
21612 | c = finish_omp_clauses (c); | |
21613 | if (c) | |
21614 | { | |
21615 | OMP_CLAUSE_CHAIN (c) = clauses; | |
21616 | clauses = c; | |
21617 | } | |
1799e5d5 RH |
21618 | } |
21619 | } | |
21620 | ||
a68ab351 JJ |
21621 | cond = NULL; |
21622 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)) | |
ea1199ee | 21623 | cond = cp_parser_omp_for_cond (parser, decl); |
a68ab351 | 21624 | cp_parser_require (parser, CPP_SEMICOLON, "%<;%>"); |
1799e5d5 | 21625 | |
a68ab351 JJ |
21626 | incr = NULL; |
21627 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)) | |
21628 | { | |
21629 | /* If decl is an iterator, preserve the operator on decl | |
21630 | until finish_omp_for. */ | |
21631 | if (decl | |
21632 | && (type_dependent_expression_p (decl) | |
21633 | || CLASS_TYPE_P (TREE_TYPE (decl)))) | |
21634 | incr = cp_parser_omp_for_incr (parser, decl); | |
21635 | else | |
cee9035d | 21636 | incr = cp_parser_expression (parser, false, NULL); |
a68ab351 | 21637 | } |
1799e5d5 | 21638 | |
a68ab351 JJ |
21639 | if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>")) |
21640 | cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true, | |
21641 | /*or_comma=*/false, | |
21642 | /*consume_paren=*/true); | |
1799e5d5 | 21643 | |
a68ab351 JJ |
21644 | TREE_VEC_ELT (declv, i) = decl; |
21645 | TREE_VEC_ELT (initv, i) = init; | |
21646 | TREE_VEC_ELT (condv, i) = cond; | |
21647 | TREE_VEC_ELT (incrv, i) = incr; | |
21648 | ||
21649 | if (i == collapse - 1) | |
21650 | break; | |
21651 | ||
21652 | /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed | |
21653 | in between the collapsed for loops to be still considered perfectly | |
21654 | nested. Hopefully the final version clarifies this. | |
21655 | For now handle (multiple) {'s and empty statements. */ | |
21656 | cp_parser_parse_tentatively (parser); | |
21657 | do | |
21658 | { | |
21659 | if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR)) | |
21660 | break; | |
21661 | else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)) | |
21662 | { | |
21663 | cp_lexer_consume_token (parser->lexer); | |
21664 | bracecount++; | |
21665 | } | |
21666 | else if (bracecount | |
21667 | && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)) | |
21668 | cp_lexer_consume_token (parser->lexer); | |
21669 | else | |
21670 | { | |
21671 | loc = cp_lexer_peek_token (parser->lexer)->location; | |
69bc6bff | 21672 | error_at (loc, "not enough collapsed for loops"); |
a68ab351 JJ |
21673 | collapse_err = true; |
21674 | cp_parser_abort_tentative_parse (parser); | |
21675 | declv = NULL_TREE; | |
21676 | break; | |
21677 | } | |
21678 | } | |
21679 | while (1); | |
21680 | ||
21681 | if (declv) | |
21682 | { | |
21683 | cp_parser_parse_definitely (parser); | |
21684 | nbraces += bracecount; | |
21685 | } | |
21686 | } | |
1799e5d5 RH |
21687 | |
21688 | /* Note that we saved the original contents of this flag when we entered | |
21689 | the structured block, and so we don't need to re-save it here. */ | |
21690 | parser->in_statement = IN_OMP_FOR; | |
21691 | ||
21692 | /* Note that the grammar doesn't call for a structured block here, | |
21693 | though the loop as a whole is a structured block. */ | |
21694 | body = push_stmt_list (); | |
2a67bec2 | 21695 | cp_parser_statement (parser, NULL_TREE, false, NULL); |
1799e5d5 RH |
21696 | body = pop_stmt_list (body); |
21697 | ||
a68ab351 JJ |
21698 | if (declv == NULL_TREE) |
21699 | ret = NULL_TREE; | |
21700 | else | |
21701 | ret = finish_omp_for (loc_first, declv, initv, condv, incrv, body, | |
21702 | pre_body, clauses); | |
21703 | ||
21704 | while (nbraces) | |
21705 | { | |
21706 | if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE)) | |
21707 | { | |
21708 | cp_lexer_consume_token (parser->lexer); | |
21709 | nbraces--; | |
21710 | } | |
21711 | else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)) | |
21712 | cp_lexer_consume_token (parser->lexer); | |
21713 | else | |
21714 | { | |
21715 | if (!collapse_err) | |
760b18ad | 21716 | { |
69bc6bff MLI |
21717 | error_at (cp_lexer_peek_token (parser->lexer)->location, |
21718 | "collapsed loops not perfectly nested"); | |
760b18ad | 21719 | } |
a68ab351 JJ |
21720 | collapse_err = true; |
21721 | cp_parser_statement_seq_opt (parser, NULL); | |
21722 | cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>"); | |
21723 | } | |
21724 | } | |
21725 | ||
21726 | while (for_block) | |
21727 | { | |
21728 | add_stmt (pop_stmt_list (TREE_VALUE (for_block))); | |
21729 | for_block = TREE_CHAIN (for_block); | |
21730 | } | |
21731 | ||
21732 | return ret; | |
1799e5d5 RH |
21733 | } |
21734 | ||
21735 | /* OpenMP 2.5: | |
21736 | #pragma omp for for-clause[optseq] new-line | |
3db45ab5 | 21737 | for-loop */ |
1799e5d5 RH |
21738 | |
21739 | #define OMP_FOR_CLAUSE_MASK \ | |
21740 | ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \ | |
21741 | | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \ | |
21742 | | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \ | |
21743 | | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \ | |
21744 | | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \ | |
21745 | | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \ | |
a68ab351 JJ |
21746 | | (1u << PRAGMA_OMP_CLAUSE_NOWAIT) \ |
21747 | | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE)) | |
1799e5d5 RH |
21748 | |
21749 | static tree | |
21750 | cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok) | |
21751 | { | |
21752 | tree clauses, sb, ret; | |
21753 | unsigned int save; | |
21754 | ||
21755 | clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK, | |
21756 | "#pragma omp for", pragma_tok); | |
21757 | ||
21758 | sb = begin_omp_structured_block (); | |
21759 | save = cp_parser_begin_omp_structured_block (parser); | |
21760 | ||
a68ab351 | 21761 | ret = cp_parser_omp_for_loop (parser, clauses, NULL); |
1799e5d5 RH |
21762 | |
21763 | cp_parser_end_omp_structured_block (parser, save); | |
21764 | add_stmt (finish_omp_structured_block (sb)); | |
21765 | ||
21766 | return ret; | |
21767 | } | |
21768 | ||
21769 | /* OpenMP 2.5: | |
21770 | # pragma omp master new-line | |
3db45ab5 | 21771 | structured-block */ |
1799e5d5 RH |
21772 | |
21773 | static tree | |
21774 | cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok) | |
21775 | { | |
21776 | cp_parser_require_pragma_eol (parser, pragma_tok); | |
c2255bc4 AH |
21777 | return c_finish_omp_master (input_location, |
21778 | cp_parser_omp_structured_block (parser)); | |
1799e5d5 RH |
21779 | } |
21780 | ||
21781 | /* OpenMP 2.5: | |
21782 | # pragma omp ordered new-line | |
3db45ab5 | 21783 | structured-block */ |
1799e5d5 RH |
21784 | |
21785 | static tree | |
21786 | cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok) | |
21787 | { | |
c2255bc4 | 21788 | location_t loc = cp_lexer_peek_token (parser->lexer)->location; |
1799e5d5 | 21789 | cp_parser_require_pragma_eol (parser, pragma_tok); |
c2255bc4 | 21790 | return c_finish_omp_ordered (loc, cp_parser_omp_structured_block (parser)); |
1799e5d5 RH |
21791 | } |
21792 | ||
21793 | /* OpenMP 2.5: | |
21794 | ||
21795 | section-scope: | |
21796 | { section-sequence } | |
21797 | ||
21798 | section-sequence: | |
21799 | section-directive[opt] structured-block | |
21800 | section-sequence section-directive structured-block */ | |
21801 | ||
21802 | static tree | |
21803 | cp_parser_omp_sections_scope (cp_parser *parser) | |
21804 | { | |
21805 | tree stmt, substmt; | |
21806 | bool error_suppress = false; | |
21807 | cp_token *tok; | |
21808 | ||
3a483371 | 21809 | if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>")) |
1799e5d5 RH |
21810 | return NULL_TREE; |
21811 | ||
21812 | stmt = push_stmt_list (); | |
21813 | ||
21814 | if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION) | |
21815 | { | |
21816 | unsigned save; | |
21817 | ||
21818 | substmt = begin_omp_structured_block (); | |
21819 | save = cp_parser_begin_omp_structured_block (parser); | |
21820 | ||
21821 | while (1) | |
21822 | { | |
2a67bec2 | 21823 | cp_parser_statement (parser, NULL_TREE, false, NULL); |
1799e5d5 RH |
21824 | |
21825 | tok = cp_lexer_peek_token (parser->lexer); | |
21826 | if (tok->pragma_kind == PRAGMA_OMP_SECTION) | |
21827 | break; | |
21828 | if (tok->type == CPP_CLOSE_BRACE) | |
21829 | break; | |
21830 | if (tok->type == CPP_EOF) | |
21831 | break; | |
21832 | } | |
21833 | ||
21834 | cp_parser_end_omp_structured_block (parser, save); | |
21835 | substmt = finish_omp_structured_block (substmt); | |
21836 | substmt = build1 (OMP_SECTION, void_type_node, substmt); | |
21837 | add_stmt (substmt); | |
21838 | } | |
21839 | ||
21840 | while (1) | |
21841 | { | |
21842 | tok = cp_lexer_peek_token (parser->lexer); | |
21843 | if (tok->type == CPP_CLOSE_BRACE) | |
21844 | break; | |
21845 | if (tok->type == CPP_EOF) | |
21846 | break; | |
21847 | ||
21848 | if (tok->pragma_kind == PRAGMA_OMP_SECTION) | |
21849 | { | |
21850 | cp_lexer_consume_token (parser->lexer); | |
21851 | cp_parser_require_pragma_eol (parser, tok); | |
21852 | error_suppress = false; | |
21853 | } | |
21854 | else if (!error_suppress) | |
21855 | { | |
21856 | cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>"); | |
21857 | error_suppress = true; | |
21858 | } | |
21859 | ||
21860 | substmt = cp_parser_omp_structured_block (parser); | |
21861 | substmt = build1 (OMP_SECTION, void_type_node, substmt); | |
21862 | add_stmt (substmt); | |
21863 | } | |
3a483371 | 21864 | cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>"); |
1799e5d5 RH |
21865 | |
21866 | substmt = pop_stmt_list (stmt); | |
21867 | ||
21868 | stmt = make_node (OMP_SECTIONS); | |
21869 | TREE_TYPE (stmt) = void_type_node; | |
21870 | OMP_SECTIONS_BODY (stmt) = substmt; | |
21871 | ||
21872 | add_stmt (stmt); | |
21873 | return stmt; | |
21874 | } | |
21875 | ||
21876 | /* OpenMP 2.5: | |
21877 | # pragma omp sections sections-clause[optseq] newline | |
3db45ab5 | 21878 | sections-scope */ |
1799e5d5 RH |
21879 | |
21880 | #define OMP_SECTIONS_CLAUSE_MASK \ | |
21881 | ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \ | |
21882 | | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \ | |
21883 | | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \ | |
21884 | | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \ | |
21885 | | (1u << PRAGMA_OMP_CLAUSE_NOWAIT)) | |
21886 | ||
21887 | static tree | |
21888 | cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok) | |
21889 | { | |
21890 | tree clauses, ret; | |
21891 | ||
21892 | clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK, | |
21893 | "#pragma omp sections", pragma_tok); | |
21894 | ||
21895 | ret = cp_parser_omp_sections_scope (parser); | |
21896 | if (ret) | |
21897 | OMP_SECTIONS_CLAUSES (ret) = clauses; | |
21898 | ||
21899 | return ret; | |
21900 | } | |
21901 | ||
21902 | /* OpenMP 2.5: | |
21903 | # pragma parallel parallel-clause new-line | |
21904 | # pragma parallel for parallel-for-clause new-line | |
3db45ab5 | 21905 | # pragma parallel sections parallel-sections-clause new-line */ |
1799e5d5 RH |
21906 | |
21907 | #define OMP_PARALLEL_CLAUSE_MASK \ | |
21908 | ( (1u << PRAGMA_OMP_CLAUSE_IF) \ | |
21909 | | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \ | |
21910 | | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \ | |
21911 | | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \ | |
21912 | | (1u << PRAGMA_OMP_CLAUSE_SHARED) \ | |
21913 | | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \ | |
21914 | | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \ | |
21915 | | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS)) | |
21916 | ||
21917 | static tree | |
21918 | cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok) | |
21919 | { | |
21920 | enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL; | |
21921 | const char *p_name = "#pragma omp parallel"; | |
21922 | tree stmt, clauses, par_clause, ws_clause, block; | |
21923 | unsigned int mask = OMP_PARALLEL_CLAUSE_MASK; | |
21924 | unsigned int save; | |
c2255bc4 | 21925 | location_t loc = cp_lexer_peek_token (parser->lexer)->location; |
1799e5d5 RH |
21926 | |
21927 | if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR)) | |
21928 | { | |
21929 | cp_lexer_consume_token (parser->lexer); | |
21930 | p_kind = PRAGMA_OMP_PARALLEL_FOR; | |
21931 | p_name = "#pragma omp parallel for"; | |
21932 | mask |= OMP_FOR_CLAUSE_MASK; | |
21933 | mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT); | |
21934 | } | |
21935 | else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)) | |
21936 | { | |
d6b418fa | 21937 | tree id = cp_lexer_peek_token (parser->lexer)->u.value; |
1799e5d5 RH |
21938 | const char *p = IDENTIFIER_POINTER (id); |
21939 | if (strcmp (p, "sections") == 0) | |
21940 | { | |
21941 | cp_lexer_consume_token (parser->lexer); | |
21942 | p_kind = PRAGMA_OMP_PARALLEL_SECTIONS; | |
21943 | p_name = "#pragma omp parallel sections"; | |
21944 | mask |= OMP_SECTIONS_CLAUSE_MASK; | |
21945 | mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT); | |
21946 | } | |
21947 | } | |
21948 | ||
21949 | clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok); | |
21950 | block = begin_omp_parallel (); | |
21951 | save = cp_parser_begin_omp_structured_block (parser); | |
21952 | ||
21953 | switch (p_kind) | |
21954 | { | |
21955 | case PRAGMA_OMP_PARALLEL: | |
5259c813 | 21956 | cp_parser_statement (parser, NULL_TREE, false, NULL); |
1799e5d5 RH |
21957 | par_clause = clauses; |
21958 | break; | |
21959 | ||
21960 | case PRAGMA_OMP_PARALLEL_FOR: | |
c2255bc4 | 21961 | c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause); |
a68ab351 | 21962 | cp_parser_omp_for_loop (parser, ws_clause, &par_clause); |
1799e5d5 RH |
21963 | break; |
21964 | ||
21965 | case PRAGMA_OMP_PARALLEL_SECTIONS: | |
c2255bc4 | 21966 | c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause); |
1799e5d5 RH |
21967 | stmt = cp_parser_omp_sections_scope (parser); |
21968 | if (stmt) | |
21969 | OMP_SECTIONS_CLAUSES (stmt) = ws_clause; | |
21970 | break; | |
21971 | ||
21972 | default: | |
21973 | gcc_unreachable (); | |
21974 | } | |
21975 | ||
21976 | cp_parser_end_omp_structured_block (parser, save); | |
761041be JJ |
21977 | stmt = finish_omp_parallel (par_clause, block); |
21978 | if (p_kind != PRAGMA_OMP_PARALLEL) | |
21979 | OMP_PARALLEL_COMBINED (stmt) = 1; | |
21980 | return stmt; | |
1799e5d5 RH |
21981 | } |
21982 | ||
21983 | /* OpenMP 2.5: | |
21984 | # pragma omp single single-clause[optseq] new-line | |
3db45ab5 | 21985 | structured-block */ |
1799e5d5 RH |
21986 | |
21987 | #define OMP_SINGLE_CLAUSE_MASK \ | |
21988 | ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \ | |
21989 | | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \ | |
21990 | | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \ | |
21991 | | (1u << PRAGMA_OMP_CLAUSE_NOWAIT)) | |
21992 | ||
21993 | static tree | |
21994 | cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok) | |
21995 | { | |
21996 | tree stmt = make_node (OMP_SINGLE); | |
21997 | TREE_TYPE (stmt) = void_type_node; | |
21998 | ||
21999 | OMP_SINGLE_CLAUSES (stmt) | |
22000 | = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK, | |
22001 | "#pragma omp single", pragma_tok); | |
22002 | OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser); | |
22003 | ||
22004 | return add_stmt (stmt); | |
22005 | } | |
22006 | ||
a68ab351 JJ |
22007 | /* OpenMP 3.0: |
22008 | # pragma omp task task-clause[optseq] new-line | |
22009 | structured-block */ | |
22010 | ||
22011 | #define OMP_TASK_CLAUSE_MASK \ | |
22012 | ( (1u << PRAGMA_OMP_CLAUSE_IF) \ | |
22013 | | (1u << PRAGMA_OMP_CLAUSE_UNTIED) \ | |
22014 | | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \ | |
22015 | | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \ | |
22016 | | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \ | |
22017 | | (1u << PRAGMA_OMP_CLAUSE_SHARED)) | |
22018 | ||
22019 | static tree | |
22020 | cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok) | |
22021 | { | |
22022 | tree clauses, block; | |
22023 | unsigned int save; | |
22024 | ||
22025 | clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK, | |
22026 | "#pragma omp task", pragma_tok); | |
22027 | block = begin_omp_task (); | |
22028 | save = cp_parser_begin_omp_structured_block (parser); | |
22029 | cp_parser_statement (parser, NULL_TREE, false, NULL); | |
22030 | cp_parser_end_omp_structured_block (parser, save); | |
22031 | return finish_omp_task (clauses, block); | |
22032 | } | |
22033 | ||
22034 | /* OpenMP 3.0: | |
22035 | # pragma omp taskwait new-line */ | |
22036 | ||
22037 | static void | |
22038 | cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok) | |
22039 | { | |
22040 | cp_parser_require_pragma_eol (parser, pragma_tok); | |
22041 | finish_omp_taskwait (); | |
22042 | } | |
22043 | ||
1799e5d5 RH |
22044 | /* OpenMP 2.5: |
22045 | # pragma omp threadprivate (variable-list) */ | |
22046 | ||
22047 | static void | |
22048 | cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok) | |
22049 | { | |
22050 | tree vars; | |
22051 | ||
a916d97f | 22052 | vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL); |
1799e5d5 RH |
22053 | cp_parser_require_pragma_eol (parser, pragma_tok); |
22054 | ||
1799e5d5 RH |
22055 | finish_omp_threadprivate (vars); |
22056 | } | |
22057 | ||
22058 | /* Main entry point to OpenMP statement pragmas. */ | |
22059 | ||
22060 | static void | |
22061 | cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok) | |
22062 | { | |
22063 | tree stmt; | |
22064 | ||
22065 | switch (pragma_tok->pragma_kind) | |
22066 | { | |
22067 | case PRAGMA_OMP_ATOMIC: | |
22068 | cp_parser_omp_atomic (parser, pragma_tok); | |
22069 | return; | |
22070 | case PRAGMA_OMP_CRITICAL: | |
22071 | stmt = cp_parser_omp_critical (parser, pragma_tok); | |
22072 | break; | |
22073 | case PRAGMA_OMP_FOR: | |
22074 | stmt = cp_parser_omp_for (parser, pragma_tok); | |
22075 | break; | |
22076 | case PRAGMA_OMP_MASTER: | |
22077 | stmt = cp_parser_omp_master (parser, pragma_tok); | |
22078 | break; | |
22079 | case PRAGMA_OMP_ORDERED: | |
22080 | stmt = cp_parser_omp_ordered (parser, pragma_tok); | |
22081 | break; | |
22082 | case PRAGMA_OMP_PARALLEL: | |
22083 | stmt = cp_parser_omp_parallel (parser, pragma_tok); | |
22084 | break; | |
22085 | case PRAGMA_OMP_SECTIONS: | |
22086 | stmt = cp_parser_omp_sections (parser, pragma_tok); | |
22087 | break; | |
22088 | case PRAGMA_OMP_SINGLE: | |
22089 | stmt = cp_parser_omp_single (parser, pragma_tok); | |
22090 | break; | |
a68ab351 JJ |
22091 | case PRAGMA_OMP_TASK: |
22092 | stmt = cp_parser_omp_task (parser, pragma_tok); | |
22093 | break; | |
1799e5d5 RH |
22094 | default: |
22095 | gcc_unreachable (); | |
22096 | } | |
22097 | ||
22098 | if (stmt) | |
22099 | SET_EXPR_LOCATION (stmt, pragma_tok->location); | |
22100 | } | |
22101 | \f | |
22102 | /* The parser. */ | |
22103 | ||
22104 | static GTY (()) cp_parser *the_parser; | |
22105 | ||
22106 | \f | |
22107 | /* Special handling for the first token or line in the file. The first | |
22108 | thing in the file might be #pragma GCC pch_preprocess, which loads a | |
22109 | PCH file, which is a GC collection point. So we need to handle this | |
22110 | first pragma without benefit of an existing lexer structure. | |
22111 | ||
3db45ab5 | 22112 | Always returns one token to the caller in *FIRST_TOKEN. This is |
1799e5d5 RH |
22113 | either the true first token of the file, or the first token after |
22114 | the initial pragma. */ | |
22115 | ||
22116 | static void | |
22117 | cp_parser_initial_pragma (cp_token *first_token) | |
22118 | { | |
22119 | tree name = NULL; | |
22120 | ||
22121 | cp_lexer_get_preprocessor_token (NULL, first_token); | |
22122 | if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS) | |
22123 | return; | |
22124 | ||
22125 | cp_lexer_get_preprocessor_token (NULL, first_token); | |
22126 | if (first_token->type == CPP_STRING) | |
22127 | { | |
d6b418fa | 22128 | name = first_token->u.value; |
1799e5d5 RH |
22129 | |
22130 | cp_lexer_get_preprocessor_token (NULL, first_token); | |
22131 | if (first_token->type != CPP_PRAGMA_EOL) | |
69bc6bff MLI |
22132 | error_at (first_token->location, |
22133 | "junk at end of %<#pragma GCC pch_preprocess%>"); | |
1799e5d5 RH |
22134 | } |
22135 | else | |
69bc6bff | 22136 | error_at (first_token->location, "expected string literal"); |
1799e5d5 RH |
22137 | |
22138 | /* Skip to the end of the pragma. */ | |
22139 | while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF) | |
22140 | cp_lexer_get_preprocessor_token (NULL, first_token); | |
22141 | ||
1799e5d5 RH |
22142 | /* Now actually load the PCH file. */ |
22143 | if (name) | |
22144 | c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name)); | |
f3d5aeeb AO |
22145 | |
22146 | /* Read one more token to return to our caller. We have to do this | |
22147 | after reading the PCH file in, since its pointers have to be | |
22148 | live. */ | |
22149 | cp_lexer_get_preprocessor_token (NULL, first_token); | |
1799e5d5 RH |
22150 | } |
22151 | ||
22152 | /* Normal parsing of a pragma token. Here we can (and must) use the | |
22153 | regular lexer. */ | |
22154 | ||
22155 | static bool | |
22156 | cp_parser_pragma (cp_parser *parser, enum pragma_context context) | |
22157 | { | |
22158 | cp_token *pragma_tok; | |
22159 | unsigned int id; | |
22160 | ||
22161 | pragma_tok = cp_lexer_consume_token (parser->lexer); | |
22162 | gcc_assert (pragma_tok->type == CPP_PRAGMA); | |
22163 | parser->lexer->in_pragma = true; | |
22164 | ||
22165 | id = pragma_tok->pragma_kind; | |
22166 | switch (id) | |
22167 | { | |
22168 | case PRAGMA_GCC_PCH_PREPROCESS: | |
69bc6bff MLI |
22169 | error_at (pragma_tok->location, |
22170 | "%<#pragma GCC pch_preprocess%> must be first"); | |
1799e5d5 RH |
22171 | break; |
22172 | ||
22173 | case PRAGMA_OMP_BARRIER: | |
22174 | switch (context) | |
22175 | { | |
22176 | case pragma_compound: | |
22177 | cp_parser_omp_barrier (parser, pragma_tok); | |
22178 | return false; | |
22179 | case pragma_stmt: | |
69bc6bff MLI |
22180 | error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be " |
22181 | "used in compound statements"); | |
1799e5d5 RH |
22182 | break; |
22183 | default: | |
22184 | goto bad_stmt; | |
22185 | } | |
22186 | break; | |
22187 | ||
22188 | case PRAGMA_OMP_FLUSH: | |
22189 | switch (context) | |
22190 | { | |
22191 | case pragma_compound: | |
22192 | cp_parser_omp_flush (parser, pragma_tok); | |
22193 | return false; | |
22194 | case pragma_stmt: | |
69bc6bff MLI |
22195 | error_at (pragma_tok->location, "%<#pragma omp flush%> may only be " |
22196 | "used in compound statements"); | |
1799e5d5 RH |
22197 | break; |
22198 | default: | |
22199 | goto bad_stmt; | |
22200 | } | |
22201 | break; | |
22202 | ||
a68ab351 JJ |
22203 | case PRAGMA_OMP_TASKWAIT: |
22204 | switch (context) | |
22205 | { | |
22206 | case pragma_compound: | |
22207 | cp_parser_omp_taskwait (parser, pragma_tok); | |
22208 | return false; | |
22209 | case pragma_stmt: | |
69bc6bff MLI |
22210 | error_at (pragma_tok->location, |
22211 | "%<#pragma omp taskwait%> may only be " | |
22212 | "used in compound statements"); | |
a68ab351 JJ |
22213 | break; |
22214 | default: | |
22215 | goto bad_stmt; | |
22216 | } | |
22217 | break; | |
22218 | ||
1799e5d5 RH |
22219 | case PRAGMA_OMP_THREADPRIVATE: |
22220 | cp_parser_omp_threadprivate (parser, pragma_tok); | |
22221 | return false; | |
22222 | ||
22223 | case PRAGMA_OMP_ATOMIC: | |
22224 | case PRAGMA_OMP_CRITICAL: | |
22225 | case PRAGMA_OMP_FOR: | |
22226 | case PRAGMA_OMP_MASTER: | |
22227 | case PRAGMA_OMP_ORDERED: | |
22228 | case PRAGMA_OMP_PARALLEL: | |
22229 | case PRAGMA_OMP_SECTIONS: | |
22230 | case PRAGMA_OMP_SINGLE: | |
a68ab351 | 22231 | case PRAGMA_OMP_TASK: |
1799e5d5 RH |
22232 | if (context == pragma_external) |
22233 | goto bad_stmt; | |
22234 | cp_parser_omp_construct (parser, pragma_tok); | |
22235 | return true; | |
22236 | ||
22237 | case PRAGMA_OMP_SECTION: | |
69bc6bff MLI |
22238 | error_at (pragma_tok->location, |
22239 | "%<#pragma omp section%> may only be used in " | |
22240 | "%<#pragma omp sections%> construct"); | |
1799e5d5 RH |
22241 | break; |
22242 | ||
22243 | default: | |
22244 | gcc_assert (id >= PRAGMA_FIRST_EXTERNAL); | |
22245 | c_invoke_pragma_handler (id); | |
22246 | break; | |
22247 | ||
22248 | bad_stmt: | |
22249 | cp_parser_error (parser, "expected declaration specifiers"); | |
bc4071dd RH |
22250 | break; |
22251 | } | |
22252 | ||
22253 | cp_parser_skip_to_pragma_eol (parser, pragma_tok); | |
22254 | return false; | |
22255 | } | |
22256 | ||
22257 | /* The interface the pragma parsers have to the lexer. */ | |
22258 | ||
22259 | enum cpp_ttype | |
22260 | pragma_lex (tree *value) | |
22261 | { | |
22262 | cp_token *tok; | |
22263 | enum cpp_ttype ret; | |
22264 | ||
22265 | tok = cp_lexer_peek_token (the_parser->lexer); | |
22266 | ||
22267 | ret = tok->type; | |
d6b418fa | 22268 | *value = tok->u.value; |
bc4071dd RH |
22269 | |
22270 | if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF) | |
22271 | ret = CPP_EOF; | |
22272 | else if (ret == CPP_STRING) | |
22273 | *value = cp_parser_string_literal (the_parser, false, false); | |
22274 | else | |
22275 | { | |
22276 | cp_lexer_consume_token (the_parser->lexer); | |
22277 | if (ret == CPP_KEYWORD) | |
22278 | ret = CPP_NAME; | |
22279 | } | |
22280 | ||
22281 | return ret; | |
22282 | } | |
22283 | ||
22284 | \f | |
a723baf1 MM |
22285 | /* External interface. */ |
22286 | ||
d1bd0ded | 22287 | /* Parse one entire translation unit. */ |
a723baf1 | 22288 | |
d1bd0ded GK |
22289 | void |
22290 | c_parse_file (void) | |
a723baf1 MM |
22291 | { |
22292 | bool error_occurred; | |
f75fbaf7 ZW |
22293 | static bool already_called = false; |
22294 | ||
22295 | if (already_called) | |
22296 | { | |
22297 | sorry ("inter-module optimizations not implemented for C++"); | |
22298 | return; | |
22299 | } | |
22300 | already_called = true; | |
a723baf1 MM |
22301 | |
22302 | the_parser = cp_parser_new (); | |
78757caa KL |
22303 | push_deferring_access_checks (flag_access_control |
22304 | ? dk_no_deferred : dk_no_check); | |
a723baf1 MM |
22305 | error_occurred = cp_parser_translation_unit (the_parser); |
22306 | the_parser = NULL; | |
a723baf1 MM |
22307 | } |
22308 | ||
a723baf1 | 22309 | #include "gt-cp-parser.h" |