]> gcc.gnu.org Git - gcc.git/blame - gcc/c-parser.c
c-decl.c (grokdeclarator): If -Wc++-compat, warn about a global variable with an...
[gcc.git] / gcc / c-parser.c
CommitLineData
27bf414c
JM
1/* Parser for C and Objective-C.
2 Copyright (C) 1987, 1988, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
b295aee2 3 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2007, 2008, 2009
5df27e4a 4 Free Software Foundation, Inc.
27bf414c
JM
5
6 Parser actions based on the old Bison parser; structure somewhat
7 influenced by and fragments based on the C++ parser.
8
9This file is part of GCC.
10
11GCC is free software; you can redistribute it and/or modify it under
12the terms of the GNU General Public License as published by the Free
9dcd6f09 13Software Foundation; either version 3, or (at your option) any later
27bf414c
JM
14version.
15
16GCC is distributed in the hope that it will be useful, but WITHOUT ANY
17WARRANTY; without even the implied warranty of MERCHANTABILITY or
18FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19for more details.
20
21You should have received a copy of the GNU General Public License
9dcd6f09
NC
22along with GCC; see the file COPYING3. If not see
23<http://www.gnu.org/licenses/>. */
27bf414c
JM
24
25/* TODO:
26
27 Make sure all relevant comments, and all relevant code from all
28 actions, brought over from old parser. Verify exact correspondence
29 of syntax accepted.
30
31 Add testcases covering every input symbol in every state in old and
32 new parsers.
33
34 Include full syntax for GNU C, including erroneous cases accepted
35 with error messages, in syntax productions in comments.
36
37 Make more diagnostics in the front end generally take an explicit
38 location rather than implicitly using input_location. */
39
40#include "config.h"
41#include "system.h"
42#include "coretypes.h"
43#include "tm.h"
44#include "tree.h"
bc4071dd 45#include "rtl.h"
27bf414c
JM
46#include "langhooks.h"
47#include "input.h"
48#include "cpplib.h"
49#include "timevar.h"
50#include "c-pragma.h"
51#include "c-tree.h"
52#include "flags.h"
53#include "output.h"
54#include "toplev.h"
55#include "ggc.h"
56#include "c-common.h"
bc4071dd
RH
57#include "vec.h"
58#include "target.h"
474eccc6 59#include "cgraph.h"
68a607d8 60#include "plugin.h"
27bf414c
JM
61
62\f
27bf414c
JM
63/* Initialization routine for this file. */
64
65void
66c_parse_init (void)
67{
68 /* The only initialization required is of the reserved word
69 identifiers. */
70 unsigned int i;
71 tree id;
eea1139b 72 int mask = 0;
27bf414c 73
eea1139b
ILT
74 mask |= D_CXXONLY;
75 if (!flag_isoc99)
76 mask |= D_C99;
77 if (flag_no_asm)
78 {
79 mask |= D_ASM | D_EXT;
80 if (!flag_isoc99)
81 mask |= D_EXT89;
82 }
27bf414c 83 if (!c_dialect_objc ())
eea1139b 84 mask |= D_OBJC | D_CXX_OBJC;
27bf414c
JM
85
86 ridpointers = GGC_CNEWVEC (tree, (int) RID_MAX);
eea1139b 87 for (i = 0; i < num_c_common_reswords; i++)
27bf414c
JM
88 {
89 /* If a keyword is disabled, do not enter it into the table
90 and so create a canonical spelling that isn't a keyword. */
eea1139b
ILT
91 if (c_common_reswords[i].disable & mask)
92 {
93 if (warn_cxx_compat
94 && (c_common_reswords[i].disable & D_CXXWARN))
95 {
96 id = get_identifier (c_common_reswords[i].word);
97 C_SET_RID_CODE (id, RID_CXX_COMPAT_WARN);
98 C_IS_RESERVED_WORD (id) = 1;
99 }
100 continue;
101 }
27bf414c 102
eea1139b
ILT
103 id = get_identifier (c_common_reswords[i].word);
104 C_SET_RID_CODE (id, c_common_reswords[i].rid);
27bf414c 105 C_IS_RESERVED_WORD (id) = 1;
eea1139b 106 ridpointers [(int) c_common_reswords[i].rid] = id;
27bf414c
JM
107 }
108}
109\f
110/* The C lexer intermediates between the lexer in cpplib and c-lex.c
111 and the C parser. Unlike the C++ lexer, the parser structure
112 stores the lexer information instead of using a separate structure.
113 Identifiers are separated into ordinary identifiers, type names,
114 keywords and some other Objective-C types of identifiers, and some
115 look-ahead is maintained.
116
117 ??? It might be a good idea to lex the whole file up front (as for
118 C++). It would then be possible to share more of the C and C++
119 lexer code, if desired. */
120
121/* The following local token type is used. */
122
123/* A keyword. */
124#define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
125
27bf414c
JM
126/* More information about the type of a CPP_NAME token. */
127typedef enum c_id_kind {
128 /* An ordinary identifier. */
129 C_ID_ID,
130 /* An identifier declared as a typedef name. */
131 C_ID_TYPENAME,
132 /* An identifier declared as an Objective-C class name. */
133 C_ID_CLASSNAME,
134 /* Not an identifier. */
135 C_ID_NONE
136} c_id_kind;
137
138/* A single C token after string literal concatenation and conversion
139 of preprocessing tokens to tokens. */
d1b38208 140typedef struct GTY (()) c_token {
27bf414c
JM
141 /* The kind of token. */
142 ENUM_BITFIELD (cpp_ttype) type : 8;
143 /* If this token is a CPP_NAME, this value indicates whether also
144 declared as some kind of type. Otherwise, it is C_ID_NONE. */
145 ENUM_BITFIELD (c_id_kind) id_kind : 8;
146 /* If this token is a keyword, this value indicates which keyword.
147 Otherwise, this value is RID_MAX. */
148 ENUM_BITFIELD (rid) keyword : 8;
bc4071dd
RH
149 /* If this token is a CPP_PRAGMA, this indicates the pragma that
150 was seen. Otherwise it is PRAGMA_NONE. */
13fa1171 151 ENUM_BITFIELD (pragma_kind) pragma_kind : 8;
27bf414c
JM
152 /* The value associated with this token, if any. */
153 tree value;
154 /* The location at which this token was found. */
155 location_t location;
156} c_token;
157
158/* A parser structure recording information about the state and
159 context of parsing. Includes lexer information with up to two
160 tokens of look-ahead; more are not needed for C. */
d1b38208 161typedef struct GTY(()) c_parser {
27bf414c
JM
162 /* The look-ahead tokens. */
163 c_token tokens[2];
164 /* How many look-ahead tokens are available (0, 1 or 2). */
165 short tokens_avail;
166 /* True if a syntax error is being recovered from; false otherwise.
167 c_parser_error sets this flag. It should clear this flag when
168 enough tokens have been consumed to recover from the error. */
169 BOOL_BITFIELD error : 1;
bc4071dd
RH
170 /* True if we're processing a pragma, and shouldn't automatically
171 consume CPP_PRAGMA_EOL. */
172 BOOL_BITFIELD in_pragma : 1;
b4b56033
MLI
173 /* True if we're parsing the outermost block of an if statement. */
174 BOOL_BITFIELD in_if_block : 1;
46c2514e
TT
175 /* True if we want to lex an untranslated string. */
176 BOOL_BITFIELD lex_untranslated_string : 1;
0bacb8c7
TT
177 /* Objective-C specific parser/lexer information. */
178 BOOL_BITFIELD objc_pq_context : 1;
179 /* The following flag is needed to contextualize Objective-C lexical
180 analysis. In some cases (e.g., 'int NSObject;'), it is
181 undesirable to bind an identifier to an Objective-C class, even
182 if a class with that name exists. */
183 BOOL_BITFIELD objc_need_raw_identifier : 1;
27bf414c
JM
184} c_parser;
185
bc4071dd
RH
186
187/* The actual parser and external interface. ??? Does this need to be
188 garbage-collected? */
189
190static GTY (()) c_parser *the_parser;
191
192
27bf414c
JM
193/* Read in and lex a single token, storing it in *TOKEN. */
194
195static void
0bacb8c7 196c_lex_one_token (c_parser *parser, c_token *token)
27bf414c
JM
197{
198 timevar_push (TV_LEX);
bc4071dd 199
46c2514e
TT
200 token->type = c_lex_with_flags (&token->value, &token->location, NULL,
201 (parser->lex_untranslated_string
202 ? C_LEX_STRING_NO_TRANSLATE : 0));
bc4071dd
RH
203 token->id_kind = C_ID_NONE;
204 token->keyword = RID_MAX;
205 token->pragma_kind = PRAGMA_NONE;
bc4071dd 206
27bf414c
JM
207 switch (token->type)
208 {
209 case CPP_NAME:
27bf414c
JM
210 {
211 tree decl;
212
0bacb8c7
TT
213 bool objc_force_identifier = parser->objc_need_raw_identifier;
214 if (c_dialect_objc ())
215 parser->objc_need_raw_identifier = false;
27bf414c
JM
216
217 if (C_IS_RESERVED_WORD (token->value))
218 {
219 enum rid rid_code = C_RID_CODE (token->value);
220
eea1139b
ILT
221 if (rid_code == RID_CXX_COMPAT_WARN)
222 {
3ba09659
AH
223 warning_at (token->location,
224 OPT_Wc___compat,
88388a52
JM
225 "identifier %qE conflicts with C++ keyword",
226 token->value);
eea1139b
ILT
227 }
228 else if (c_dialect_objc ())
27bf414c 229 {
eea1139b 230 if (!objc_is_reserved_word (token->value)
0bacb8c7
TT
231 && (!OBJC_IS_PQ_KEYWORD (rid_code)
232 || parser->objc_pq_context))
27bf414c
JM
233 {
234 /* Return the canonical spelling for this keyword. */
235 token->value = ridpointers[(int) rid_code];
236 token->type = CPP_KEYWORD;
237 token->keyword = rid_code;
238 break;
239 }
240 }
241 else
242 {
27bf414c
JM
243 token->type = CPP_KEYWORD;
244 token->keyword = rid_code;
245 break;
246 }
247 }
248
249 decl = lookup_name (token->value);
250 if (decl)
251 {
252 if (TREE_CODE (decl) == TYPE_DECL)
253 {
254 token->id_kind = C_ID_TYPENAME;
255 break;
256 }
257 }
258 else if (c_dialect_objc ())
259 {
260 tree objc_interface_decl = objc_is_class_name (token->value);
261 /* Objective-C class names are in the same namespace as
262 variables and typedefs, and hence are shadowed by local
263 declarations. */
264 if (objc_interface_decl
265 && (global_bindings_p ()
266 || (!objc_force_identifier && !decl)))
267 {
268 token->value = objc_interface_decl;
269 token->id_kind = C_ID_CLASSNAME;
270 break;
271 }
272 }
bc4071dd 273 token->id_kind = C_ID_ID;
27bf414c 274 }
27bf414c
JM
275 break;
276 case CPP_AT_NAME:
277 /* This only happens in Objective-C; it must be a keyword. */
278 token->type = CPP_KEYWORD;
27bf414c
JM
279 token->keyword = C_RID_CODE (token->value);
280 break;
281 case CPP_COLON:
282 case CPP_COMMA:
283 case CPP_CLOSE_PAREN:
284 case CPP_SEMICOLON:
285 /* These tokens may affect the interpretation of any identifiers
286 following, if doing Objective-C. */
0bacb8c7
TT
287 if (c_dialect_objc ())
288 parser->objc_need_raw_identifier = false;
bc4071dd
RH
289 break;
290 case CPP_PRAGMA:
291 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
d75d71e0 292 token->pragma_kind = (enum pragma_kind) TREE_INT_CST_LOW (token->value);
bc4071dd 293 token->value = NULL;
27bf414c
JM
294 break;
295 default:
27bf414c
JM
296 break;
297 }
298 timevar_pop (TV_LEX);
299}
300
301/* Return a pointer to the next token from PARSER, reading it in if
302 necessary. */
303
304static inline c_token *
305c_parser_peek_token (c_parser *parser)
306{
307 if (parser->tokens_avail == 0)
308 {
0bacb8c7 309 c_lex_one_token (parser, &parser->tokens[0]);
27bf414c
JM
310 parser->tokens_avail = 1;
311 }
312 return &parser->tokens[0];
313}
314
315/* Return true if the next token from PARSER has the indicated
316 TYPE. */
317
318static inline bool
319c_parser_next_token_is (c_parser *parser, enum cpp_ttype type)
320{
321 return c_parser_peek_token (parser)->type == type;
322}
323
324/* Return true if the next token from PARSER does not have the
325 indicated TYPE. */
326
327static inline bool
328c_parser_next_token_is_not (c_parser *parser, enum cpp_ttype type)
329{
330 return !c_parser_next_token_is (parser, type);
331}
332
333/* Return true if the next token from PARSER is the indicated
334 KEYWORD. */
335
336static inline bool
337c_parser_next_token_is_keyword (c_parser *parser, enum rid keyword)
338{
dbc518f0 339 return c_parser_peek_token (parser)->keyword == keyword;
27bf414c
JM
340}
341
342/* Return true if TOKEN can start a type name,
343 false otherwise. */
344static bool
345c_token_starts_typename (c_token *token)
346{
347 switch (token->type)
348 {
349 case CPP_NAME:
350 switch (token->id_kind)
351 {
352 case C_ID_ID:
353 return false;
354 case C_ID_TYPENAME:
355 return true;
356 case C_ID_CLASSNAME:
357 gcc_assert (c_dialect_objc ());
358 return true;
359 default:
360 gcc_unreachable ();
361 }
362 case CPP_KEYWORD:
363 switch (token->keyword)
364 {
365 case RID_UNSIGNED:
366 case RID_LONG:
367 case RID_SHORT:
368 case RID_SIGNED:
369 case RID_COMPLEX:
370 case RID_INT:
371 case RID_CHAR:
372 case RID_FLOAT:
373 case RID_DOUBLE:
374 case RID_VOID:
9a8ce21f
JG
375 case RID_DFLOAT32:
376 case RID_DFLOAT64:
377 case RID_DFLOAT128:
27bf414c
JM
378 case RID_BOOL:
379 case RID_ENUM:
380 case RID_STRUCT:
381 case RID_UNION:
382 case RID_TYPEOF:
383 case RID_CONST:
384 case RID_VOLATILE:
385 case RID_RESTRICT:
386 case RID_ATTRIBUTE:
ab22c1fa
CF
387 case RID_FRACT:
388 case RID_ACCUM:
389 case RID_SAT:
27bf414c
JM
390 return true;
391 default:
392 return false;
393 }
394 case CPP_LESS:
395 if (c_dialect_objc ())
396 return true;
397 return false;
398 default:
399 return false;
400 }
401}
402
403/* Return true if the next token from PARSER can start a type name,
404 false otherwise. */
405static inline bool
406c_parser_next_token_starts_typename (c_parser *parser)
407{
408 c_token *token = c_parser_peek_token (parser);
409 return c_token_starts_typename (token);
410}
411
412/* Return true if TOKEN can start declaration specifiers, false
413 otherwise. */
414static bool
415c_token_starts_declspecs (c_token *token)
416{
417 switch (token->type)
418 {
419 case CPP_NAME:
420 switch (token->id_kind)
421 {
422 case C_ID_ID:
423 return false;
424 case C_ID_TYPENAME:
425 return true;
426 case C_ID_CLASSNAME:
427 gcc_assert (c_dialect_objc ());
428 return true;
429 default:
430 gcc_unreachable ();
431 }
432 case CPP_KEYWORD:
433 switch (token->keyword)
434 {
435 case RID_STATIC:
436 case RID_EXTERN:
437 case RID_REGISTER:
438 case RID_TYPEDEF:
439 case RID_INLINE:
440 case RID_AUTO:
441 case RID_THREAD:
442 case RID_UNSIGNED:
443 case RID_LONG:
444 case RID_SHORT:
445 case RID_SIGNED:
446 case RID_COMPLEX:
447 case RID_INT:
448 case RID_CHAR:
449 case RID_FLOAT:
450 case RID_DOUBLE:
451 case RID_VOID:
9a8ce21f
JG
452 case RID_DFLOAT32:
453 case RID_DFLOAT64:
454 case RID_DFLOAT128:
27bf414c
JM
455 case RID_BOOL:
456 case RID_ENUM:
457 case RID_STRUCT:
458 case RID_UNION:
459 case RID_TYPEOF:
460 case RID_CONST:
461 case RID_VOLATILE:
462 case RID_RESTRICT:
463 case RID_ATTRIBUTE:
ab22c1fa
CF
464 case RID_FRACT:
465 case RID_ACCUM:
466 case RID_SAT:
27bf414c
JM
467 return true;
468 default:
469 return false;
470 }
471 case CPP_LESS:
472 if (c_dialect_objc ())
473 return true;
474 return false;
475 default:
476 return false;
477 }
478}
479
480/* Return true if the next token from PARSER can start declaration
481 specifiers, false otherwise. */
482static inline bool
483c_parser_next_token_starts_declspecs (c_parser *parser)
484{
485 c_token *token = c_parser_peek_token (parser);
486 return c_token_starts_declspecs (token);
487}
488
489/* Return a pointer to the next-but-one token from PARSER, reading it
490 in if necessary. The next token is already read in. */
491
492static c_token *
493c_parser_peek_2nd_token (c_parser *parser)
494{
495 if (parser->tokens_avail >= 2)
496 return &parser->tokens[1];
497 gcc_assert (parser->tokens_avail == 1);
498 gcc_assert (parser->tokens[0].type != CPP_EOF);
bc4071dd 499 gcc_assert (parser->tokens[0].type != CPP_PRAGMA_EOL);
0bacb8c7 500 c_lex_one_token (parser, &parser->tokens[1]);
27bf414c
JM
501 parser->tokens_avail = 2;
502 return &parser->tokens[1];
503}
504
505/* Consume the next token from PARSER. */
506
507static void
508c_parser_consume_token (c_parser *parser)
509{
bc4071dd
RH
510 gcc_assert (parser->tokens_avail >= 1);
511 gcc_assert (parser->tokens[0].type != CPP_EOF);
512 gcc_assert (!parser->in_pragma || parser->tokens[0].type != CPP_PRAGMA_EOL);
513 gcc_assert (parser->error || parser->tokens[0].type != CPP_PRAGMA);
27bf414c
JM
514 if (parser->tokens_avail == 2)
515 parser->tokens[0] = parser->tokens[1];
27bf414c
JM
516 parser->tokens_avail--;
517}
518
bc4071dd
RH
519/* Expect the current token to be a #pragma. Consume it and remember
520 that we've begun parsing a pragma. */
521
522static void
523c_parser_consume_pragma (c_parser *parser)
524{
525 gcc_assert (!parser->in_pragma);
526 gcc_assert (parser->tokens_avail >= 1);
527 gcc_assert (parser->tokens[0].type == CPP_PRAGMA);
528 if (parser->tokens_avail == 2)
529 parser->tokens[0] = parser->tokens[1];
530 parser->tokens_avail--;
531 parser->in_pragma = true;
532}
533
27bf414c
JM
534/* Update the globals input_location and in_system_header from
535 TOKEN. */
536static inline void
537c_parser_set_source_position_from_token (c_token *token)
538{
539 if (token->type != CPP_EOF)
540 {
541 input_location = token->location;
27bf414c
JM
542 }
543}
544
27bf414c
JM
545/* Issue a diagnostic of the form
546 FILE:LINE: MESSAGE before TOKEN
547 where TOKEN is the next token in the input stream of PARSER.
548 MESSAGE (specified by the caller) is usually of the form "expected
549 OTHER-TOKEN".
550
551 Do not issue a diagnostic if still recovering from an error.
552
553 ??? This is taken from the C++ parser, but building up messages in
554 this way is not i18n-friendly and some other approach should be
555 used. */
556
557static void
4b794eaf 558c_parser_error (c_parser *parser, const char *gmsgid)
27bf414c
JM
559{
560 c_token *token = c_parser_peek_token (parser);
561 if (parser->error)
562 return;
563 parser->error = true;
4b794eaf 564 if (!gmsgid)
27bf414c
JM
565 return;
566 /* This diagnostic makes more sense if it is tagged to the line of
567 the token we just peeked at. */
568 c_parser_set_source_position_from_token (token);
4b794eaf 569 c_parse_error (gmsgid,
27bf414c
JM
570 /* Because c_parse_error does not understand
571 CPP_KEYWORD, keywords are treated like
572 identifiers. */
573 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
cfc93532
MLI
574 /* ??? The C parser does not save the cpp flags of a
575 token, we need to pass 0 here and we will not get
576 the source spelling of some tokens but rather the
577 canonical spelling. */
578 token->value, /*flags=*/0);
27bf414c
JM
579}
580
581/* If the next token is of the indicated TYPE, consume it. Otherwise,
582 issue the error MSGID. If MSGID is NULL then a message has already
583 been produced and no message will be produced this time. Returns
584 true if found, false otherwise. */
585
586static bool
587c_parser_require (c_parser *parser,
588 enum cpp_ttype type,
589 const char *msgid)
590{
591 if (c_parser_next_token_is (parser, type))
592 {
593 c_parser_consume_token (parser);
594 return true;
595 }
596 else
597 {
598 c_parser_error (parser, msgid);
599 return false;
600 }
601}
602
603/* If the next token is the indicated keyword, consume it. Otherwise,
604 issue the error MSGID. Returns true if found, false otherwise. */
605
606static bool
607c_parser_require_keyword (c_parser *parser,
608 enum rid keyword,
609 const char *msgid)
610{
611 if (c_parser_next_token_is_keyword (parser, keyword))
612 {
613 c_parser_consume_token (parser);
614 return true;
615 }
616 else
617 {
618 c_parser_error (parser, msgid);
619 return false;
620 }
621}
622
623/* Like c_parser_require, except that tokens will be skipped until the
624 desired token is found. An error message is still produced if the
625 next token is not as expected. If MSGID is NULL then a message has
626 already been produced and no message will be produced this
627 time. */
628
629static void
630c_parser_skip_until_found (c_parser *parser,
631 enum cpp_ttype type,
632 const char *msgid)
633{
634 unsigned nesting_depth = 0;
635
636 if (c_parser_require (parser, type, msgid))
637 return;
638
639 /* Skip tokens until the desired token is found. */
640 while (true)
641 {
642 /* Peek at the next token. */
643 c_token *token = c_parser_peek_token (parser);
644 /* If we've reached the token we want, consume it and stop. */
645 if (token->type == type && !nesting_depth)
646 {
647 c_parser_consume_token (parser);
648 break;
649 }
bc4071dd 650
27bf414c
JM
651 /* If we've run out of tokens, stop. */
652 if (token->type == CPP_EOF)
653 return;
bc4071dd
RH
654 if (token->type == CPP_PRAGMA_EOL && parser->in_pragma)
655 return;
27bf414c
JM
656 if (token->type == CPP_OPEN_BRACE
657 || token->type == CPP_OPEN_PAREN
658 || token->type == CPP_OPEN_SQUARE)
659 ++nesting_depth;
660 else if (token->type == CPP_CLOSE_BRACE
661 || token->type == CPP_CLOSE_PAREN
662 || token->type == CPP_CLOSE_SQUARE)
663 {
664 if (nesting_depth-- == 0)
665 break;
666 }
667 /* Consume this token. */
668 c_parser_consume_token (parser);
669 }
670 parser->error = false;
671}
672
673/* Skip tokens until the end of a parameter is found, but do not
674 consume the comma, semicolon or closing delimiter. */
675
676static void
677c_parser_skip_to_end_of_parameter (c_parser *parser)
678{
679 unsigned nesting_depth = 0;
680
681 while (true)
682 {
683 c_token *token = c_parser_peek_token (parser);
684 if ((token->type == CPP_COMMA || token->type == CPP_SEMICOLON)
685 && !nesting_depth)
686 break;
687 /* If we've run out of tokens, stop. */
688 if (token->type == CPP_EOF)
689 return;
bc4071dd
RH
690 if (token->type == CPP_PRAGMA_EOL && parser->in_pragma)
691 return;
27bf414c
JM
692 if (token->type == CPP_OPEN_BRACE
693 || token->type == CPP_OPEN_PAREN
694 || token->type == CPP_OPEN_SQUARE)
695 ++nesting_depth;
696 else if (token->type == CPP_CLOSE_BRACE
697 || token->type == CPP_CLOSE_PAREN
698 || token->type == CPP_CLOSE_SQUARE)
699 {
700 if (nesting_depth-- == 0)
701 break;
702 }
703 /* Consume this token. */
704 c_parser_consume_token (parser);
705 }
706 parser->error = false;
707}
708
bc4071dd
RH
709/* Expect to be at the end of the pragma directive and consume an
710 end of line marker. */
711
712static void
713c_parser_skip_to_pragma_eol (c_parser *parser)
714{
715 gcc_assert (parser->in_pragma);
716 parser->in_pragma = false;
717
718 if (!c_parser_require (parser, CPP_PRAGMA_EOL, "expected end of line"))
719 while (true)
720 {
721 c_token *token = c_parser_peek_token (parser);
722 if (token->type == CPP_EOF)
723 break;
724 if (token->type == CPP_PRAGMA_EOL)
725 {
726 c_parser_consume_token (parser);
727 break;
728 }
729 c_parser_consume_token (parser);
730 }
731
732 parser->error = false;
733}
27bf414c 734
2a83cc52
RH
735/* Skip tokens until we have consumed an entire block, or until we
736 have consumed a non-nested ';'. */
737
738static void
739c_parser_skip_to_end_of_block_or_statement (c_parser *parser)
740{
741 unsigned nesting_depth = 0;
742 bool save_error = parser->error;
743
744 while (true)
745 {
746 c_token *token;
747
748 /* Peek at the next token. */
749 token = c_parser_peek_token (parser);
750
751 switch (token->type)
752 {
753 case CPP_EOF:
754 return;
755
756 case CPP_PRAGMA_EOL:
757 if (parser->in_pragma)
758 return;
759 break;
760
761 case CPP_SEMICOLON:
762 /* If the next token is a ';', we have reached the
763 end of the statement. */
764 if (!nesting_depth)
765 {
766 /* Consume the ';'. */
767 c_parser_consume_token (parser);
768 goto finished;
769 }
770 break;
771
772 case CPP_CLOSE_BRACE:
773 /* If the next token is a non-nested '}', then we have
774 reached the end of the current block. */
775 if (nesting_depth == 0 || --nesting_depth == 0)
776 {
777 c_parser_consume_token (parser);
778 goto finished;
779 }
780 break;
781
782 case CPP_OPEN_BRACE:
783 /* If it the next token is a '{', then we are entering a new
784 block. Consume the entire block. */
785 ++nesting_depth;
786 break;
787
788 case CPP_PRAGMA:
789 /* If we see a pragma, consume the whole thing at once. We
790 have some safeguards against consuming pragmas willy-nilly.
791 Normally, we'd expect to be here with parser->error set,
792 which disables these safeguards. But it's possible to get
793 here for secondary error recovery, after parser->error has
794 been cleared. */
795 c_parser_consume_pragma (parser);
796 c_parser_skip_to_pragma_eol (parser);
797 parser->error = save_error;
798 continue;
9e33de05
RS
799
800 default:
801 break;
2a83cc52
RH
802 }
803
804 c_parser_consume_token (parser);
805 }
806
807 finished:
808 parser->error = false;
809}
810
d2e796ad
MLI
811/* CPP's options (initialized by c-opts.c). */
812extern cpp_options *cpp_opts;
813
27bf414c
JM
814/* Save the warning flags which are controlled by __extension__. */
815
816static inline int
817disable_extension_diagnostics (void)
818{
819 int ret = (pedantic
820 | (warn_pointer_arith << 1)
821 | (warn_traditional << 2)
d2e796ad 822 | (flag_iso << 3)
24b97832
ILT
823 | (warn_long_long << 4)
824 | (warn_cxx_compat << 5));
d2e796ad 825 cpp_opts->pedantic = pedantic = 0;
27bf414c 826 warn_pointer_arith = 0;
d2e796ad 827 cpp_opts->warn_traditional = warn_traditional = 0;
27bf414c 828 flag_iso = 0;
9c650d90 829 cpp_opts->warn_long_long = warn_long_long = 0;
24b97832 830 warn_cxx_compat = 0;
27bf414c
JM
831 return ret;
832}
833
834/* Restore the warning flags which are controlled by __extension__.
835 FLAGS is the return value from disable_extension_diagnostics. */
836
837static inline void
838restore_extension_diagnostics (int flags)
839{
d2e796ad 840 cpp_opts->pedantic = pedantic = flags & 1;
27bf414c 841 warn_pointer_arith = (flags >> 1) & 1;
d2e796ad 842 cpp_opts->warn_traditional = warn_traditional = (flags >> 2) & 1;
27bf414c 843 flag_iso = (flags >> 3) & 1;
9c650d90 844 cpp_opts->warn_long_long = warn_long_long = (flags >> 4) & 1;
24b97832 845 warn_cxx_compat = (flags >> 5) & 1;
27bf414c
JM
846}
847
848/* Possibly kinds of declarator to parse. */
849typedef enum c_dtr_syn {
850 /* A normal declarator with an identifier. */
851 C_DTR_NORMAL,
852 /* An abstract declarator (maybe empty). */
853 C_DTR_ABSTRACT,
854 /* A parameter declarator: may be either, but after a type name does
855 not redeclare a typedef name as an identifier if it can
856 alternatively be interpreted as a typedef name; see DR#009,
857 applied in C90 TC1, omitted from C99 and reapplied in C99 TC2
858 following DR#249. For example, given a typedef T, "int T" and
859 "int *T" are valid parameter declarations redeclaring T, while
860 "int (T)" and "int * (T)" and "int (T[])" and "int (T (int))" are
861 abstract declarators rather than involving redundant parentheses;
862 the same applies with attributes inside the parentheses before
863 "T". */
864 C_DTR_PARM
865} c_dtr_syn;
866
867static void c_parser_external_declaration (c_parser *);
868static void c_parser_asm_definition (c_parser *);
869static void c_parser_declaration_or_fndef (c_parser *, bool, bool, bool, bool);
870static void c_parser_declspecs (c_parser *, struct c_declspecs *, bool, bool,
871 bool);
872static struct c_typespec c_parser_enum_specifier (c_parser *);
873static struct c_typespec c_parser_struct_or_union_specifier (c_parser *);
874static tree c_parser_struct_declaration (c_parser *);
875static struct c_typespec c_parser_typeof_specifier (c_parser *);
876static struct c_declarator *c_parser_declarator (c_parser *, bool, c_dtr_syn,
877 bool *);
878static struct c_declarator *c_parser_direct_declarator (c_parser *, bool,
879 c_dtr_syn, bool *);
880static struct c_declarator *c_parser_direct_declarator_inner (c_parser *,
881 bool,
882 struct c_declarator *);
883static struct c_arg_info *c_parser_parms_declarator (c_parser *, bool, tree);
884static struct c_arg_info *c_parser_parms_list_declarator (c_parser *, tree);
885static struct c_parm *c_parser_parameter_declaration (c_parser *, tree);
886static tree c_parser_simple_asm_expr (c_parser *);
887static tree c_parser_attributes (c_parser *);
888static struct c_type_name *c_parser_type_name (c_parser *);
889static struct c_expr c_parser_initializer (c_parser *);
890static struct c_expr c_parser_braced_init (c_parser *, tree, bool);
891static void c_parser_initelt (c_parser *);
892static void c_parser_initval (c_parser *, struct c_expr *);
893static tree c_parser_compound_statement (c_parser *);
894static void c_parser_compound_statement_nostart (c_parser *);
895static void c_parser_label (c_parser *);
896static void c_parser_statement (c_parser *);
897static void c_parser_statement_after_labels (c_parser *);
898static void c_parser_if_statement (c_parser *);
899static void c_parser_switch_statement (c_parser *);
900static void c_parser_while_statement (c_parser *);
901static void c_parser_do_statement (c_parser *);
902static void c_parser_for_statement (c_parser *);
903static tree c_parser_asm_statement (c_parser *);
46bdb9cf 904static tree c_parser_asm_operands (c_parser *, bool);
27bf414c
JM
905static tree c_parser_asm_clobbers (c_parser *);
906static struct c_expr c_parser_expr_no_commas (c_parser *, struct c_expr *);
907static struct c_expr c_parser_conditional_expression (c_parser *,
908 struct c_expr *);
909static struct c_expr c_parser_binary_expression (c_parser *, struct c_expr *);
910static struct c_expr c_parser_cast_expression (c_parser *, struct c_expr *);
911static struct c_expr c_parser_unary_expression (c_parser *);
912static struct c_expr c_parser_sizeof_expression (c_parser *);
913static struct c_expr c_parser_alignof_expression (c_parser *);
914static struct c_expr c_parser_postfix_expression (c_parser *);
915static struct c_expr c_parser_postfix_expression_after_paren_type (c_parser *,
24b97832
ILT
916 struct c_type_name *,
917 location_t);
27bf414c 918static struct c_expr c_parser_postfix_expression_after_primary (c_parser *,
c2255bc4 919 location_t loc,
27bf414c
JM
920 struct c_expr);
921static struct c_expr c_parser_expression (c_parser *);
46bdb9cf 922static struct c_expr c_parser_expression_conv (c_parser *);
bbbbb16a
ILT
923static VEC(tree,gc) *c_parser_expr_list (c_parser *, bool, bool,
924 VEC(tree,gc) **);
953ff289
DN
925static void c_parser_omp_construct (c_parser *);
926static void c_parser_omp_threadprivate (c_parser *);
927static void c_parser_omp_barrier (c_parser *);
928static void c_parser_omp_flush (c_parser *);
a68ab351 929static void c_parser_omp_taskwait (c_parser *);
27bf414c 930
bc4071dd
RH
931enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
932static bool c_parser_pragma (c_parser *, enum pragma_context);
933
27bf414c
JM
934/* These Objective-C parser functions are only ever called when
935 compiling Objective-C. */
936static void c_parser_objc_class_definition (c_parser *);
937static void c_parser_objc_class_instance_variables (c_parser *);
938static void c_parser_objc_class_declaration (c_parser *);
939static void c_parser_objc_alias_declaration (c_parser *);
940static void c_parser_objc_protocol_definition (c_parser *);
941static enum tree_code c_parser_objc_method_type (c_parser *);
942static void c_parser_objc_method_definition (c_parser *);
943static void c_parser_objc_methodprotolist (c_parser *);
944static void c_parser_objc_methodproto (c_parser *);
945static tree c_parser_objc_method_decl (c_parser *);
946static tree c_parser_objc_type_name (c_parser *);
947static tree c_parser_objc_protocol_refs (c_parser *);
948static void c_parser_objc_try_catch_statement (c_parser *);
949static void c_parser_objc_synchronized_statement (c_parser *);
950static tree c_parser_objc_selector (c_parser *);
951static tree c_parser_objc_selector_arg (c_parser *);
952static tree c_parser_objc_receiver (c_parser *);
953static tree c_parser_objc_message_args (c_parser *);
954static tree c_parser_objc_keywordexpr (c_parser *);
955
956/* Parse a translation unit (C90 6.7, C99 6.9).
957
958 translation-unit:
959 external-declarations
960
961 external-declarations:
962 external-declaration
963 external-declarations external-declaration
964
965 GNU extensions:
966
967 translation-unit:
968 empty
969*/
970
971static void
972c_parser_translation_unit (c_parser *parser)
973{
974 if (c_parser_next_token_is (parser, CPP_EOF))
975 {
509c9d60
MLI
976 pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic,
977 "ISO C forbids an empty translation unit");
27bf414c
JM
978 }
979 else
980 {
981 void *obstack_position = obstack_alloc (&parser_obstack, 0);
6ec637a4 982 mark_valid_location_for_stdc_pragma (false);
27bf414c
JM
983 do
984 {
985 ggc_collect ();
986 c_parser_external_declaration (parser);
987 obstack_free (&parser_obstack, obstack_position);
988 }
989 while (c_parser_next_token_is_not (parser, CPP_EOF));
990 }
991}
992
993/* Parse an external declaration (C90 6.7, C99 6.9).
994
995 external-declaration:
996 function-definition
997 declaration
998
999 GNU extensions:
1000
1001 external-declaration:
1002 asm-definition
1003 ;
1004 __extension__ external-declaration
1005
1006 Objective-C:
1007
1008 external-declaration:
1009 objc-class-definition
1010 objc-class-declaration
1011 objc-alias-declaration
1012 objc-protocol-definition
1013 objc-method-definition
1014 @end
1015*/
1016
1017static void
1018c_parser_external_declaration (c_parser *parser)
1019{
1020 int ext;
1021 switch (c_parser_peek_token (parser)->type)
1022 {
1023 case CPP_KEYWORD:
1024 switch (c_parser_peek_token (parser)->keyword)
1025 {
1026 case RID_EXTENSION:
1027 ext = disable_extension_diagnostics ();
1028 c_parser_consume_token (parser);
1029 c_parser_external_declaration (parser);
1030 restore_extension_diagnostics (ext);
1031 break;
1032 case RID_ASM:
1033 c_parser_asm_definition (parser);
1034 break;
1035 case RID_AT_INTERFACE:
1036 case RID_AT_IMPLEMENTATION:
1037 gcc_assert (c_dialect_objc ());
1038 c_parser_objc_class_definition (parser);
1039 break;
eea1139b 1040 case RID_CLASS:
27bf414c
JM
1041 gcc_assert (c_dialect_objc ());
1042 c_parser_objc_class_declaration (parser);
1043 break;
1044 case RID_AT_ALIAS:
1045 gcc_assert (c_dialect_objc ());
1046 c_parser_objc_alias_declaration (parser);
1047 break;
1048 case RID_AT_PROTOCOL:
1049 gcc_assert (c_dialect_objc ());
1050 c_parser_objc_protocol_definition (parser);
1051 break;
1052 case RID_AT_END:
1053 gcc_assert (c_dialect_objc ());
1054 c_parser_consume_token (parser);
1055 objc_finish_implementation ();
1056 break;
1057 default:
1058 goto decl_or_fndef;
1059 }
1060 break;
1061 case CPP_SEMICOLON:
509c9d60
MLI
1062 pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic,
1063 "ISO C does not allow extra %<;%> outside of a function");
27bf414c
JM
1064 c_parser_consume_token (parser);
1065 break;
bc4071dd 1066 case CPP_PRAGMA:
6ec637a4 1067 mark_valid_location_for_stdc_pragma (true);
bc4071dd 1068 c_parser_pragma (parser, pragma_external);
6ec637a4 1069 mark_valid_location_for_stdc_pragma (false);
bc4071dd 1070 break;
27bf414c
JM
1071 case CPP_PLUS:
1072 case CPP_MINUS:
1073 if (c_dialect_objc ())
1074 {
1075 c_parser_objc_method_definition (parser);
1076 break;
1077 }
1078 /* Else fall through, and yield a syntax error trying to parse
1079 as a declaration or function definition. */
1080 default:
1081 decl_or_fndef:
1082 /* A declaration or a function definition. We can only tell
1083 which after parsing the declaration specifiers, if any, and
1084 the first declarator. */
1085 c_parser_declaration_or_fndef (parser, true, true, false, true);
1086 break;
1087 }
1088}
1089
bc4071dd 1090
27bf414c
JM
1091/* Parse a declaration or function definition (C90 6.5, 6.7.1, C99
1092 6.7, 6.9.1). If FNDEF_OK is true, a function definition is
1093 accepted; otherwise (old-style parameter declarations) only other
1094 declarations are accepted. If NESTED is true, we are inside a
1095 function or parsing old-style parameter declarations; any functions
1096 encountered are nested functions and declaration specifiers are
1097 required; otherwise we are at top level and functions are normal
1098 functions and declaration specifiers may be optional. If EMPTY_OK
1099 is true, empty declarations are OK (subject to all other
1100 constraints); otherwise (old-style parameter declarations) they are
1101 diagnosed. If START_ATTR_OK is true, the declaration specifiers
1102 may start with attributes; otherwise they may not.
1103
1104 declaration:
1105 declaration-specifiers init-declarator-list[opt] ;
1106
1107 function-definition:
1108 declaration-specifiers[opt] declarator declaration-list[opt]
1109 compound-statement
1110
1111 declaration-list:
1112 declaration
1113 declaration-list declaration
1114
1115 init-declarator-list:
1116 init-declarator
1117 init-declarator-list , init-declarator
1118
1119 init-declarator:
1120 declarator simple-asm-expr[opt] attributes[opt]
1121 declarator simple-asm-expr[opt] attributes[opt] = initializer
1122
1123 GNU extensions:
1124
1125 nested-function-definition:
1126 declaration-specifiers declarator declaration-list[opt]
1127 compound-statement
1128
1129 The simple-asm-expr and attributes are GNU extensions.
1130
1131 This function does not handle __extension__; that is handled in its
1132 callers. ??? Following the old parser, __extension__ may start
1133 external declarations, declarations in functions and declarations
1134 at the start of "for" loops, but not old-style parameter
1135 declarations.
1136
1137 C99 requires declaration specifiers in a function definition; the
1138 absence is diagnosed through the diagnosis of implicit int. In GNU
1139 C we also allow but diagnose declarations without declaration
1140 specifiers, but only at top level (elsewhere they conflict with
953ff289
DN
1141 other syntax).
1142
1143 OpenMP:
1144
1145 declaration:
1146 threadprivate-directive */
27bf414c
JM
1147
1148static void
1149c_parser_declaration_or_fndef (c_parser *parser, bool fndef_ok, bool empty_ok,
1150 bool nested, bool start_attr_ok)
1151{
1152 struct c_declspecs *specs;
1153 tree prefix_attrs;
1154 tree all_prefix_attrs;
1155 bool diagnosed_no_specs = false;
c7412148 1156 location_t here = c_parser_peek_token (parser)->location;
bc4071dd 1157
27bf414c
JM
1158 specs = build_null_declspecs ();
1159 c_parser_declspecs (parser, specs, true, true, start_attr_ok);
1160 if (parser->error)
1161 {
1162 c_parser_skip_to_end_of_block_or_statement (parser);
1163 return;
1164 }
1165 if (nested && !specs->declspecs_seen_p)
1166 {
1167 c_parser_error (parser, "expected declaration specifiers");
1168 c_parser_skip_to_end_of_block_or_statement (parser);
1169 return;
1170 }
1171 finish_declspecs (specs);
1172 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1173 {
1174 if (empty_ok)
1175 shadow_tag (specs);
1176 else
1177 {
1178 shadow_tag_warned (specs, 1);
509c9d60 1179 pedwarn (here, 0, "empty declaration");
27bf414c
JM
1180 }
1181 c_parser_consume_token (parser);
1182 return;
1183 }
1184 pending_xref_error ();
1185 prefix_attrs = specs->attrs;
1186 all_prefix_attrs = prefix_attrs;
1187 specs->attrs = NULL_TREE;
1188 while (true)
1189 {
1190 struct c_declarator *declarator;
1191 bool dummy = false;
1192 tree fnbody;
1193 /* Declaring either one or more declarators (in which case we
1194 should diagnose if there were no declaration specifiers) or a
1195 function definition (in which case the diagnostic for
1196 implicit int suffices). */
1197 declarator = c_parser_declarator (parser, specs->type_seen_p,
1198 C_DTR_NORMAL, &dummy);
1199 if (declarator == NULL)
1200 {
1201 c_parser_skip_to_end_of_block_or_statement (parser);
1202 return;
1203 }
1204 if (c_parser_next_token_is (parser, CPP_EQ)
1205 || c_parser_next_token_is (parser, CPP_COMMA)
1206 || c_parser_next_token_is (parser, CPP_SEMICOLON)
1207 || c_parser_next_token_is_keyword (parser, RID_ASM)
1208 || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1209 {
1210 tree asm_name = NULL_TREE;
1211 tree postfix_attrs = NULL_TREE;
1212 if (!diagnosed_no_specs && !specs->declspecs_seen_p)
1213 {
1214 diagnosed_no_specs = true;
509c9d60 1215 pedwarn (here, 0, "data definition has no type or storage class");
27bf414c
JM
1216 }
1217 /* Having seen a data definition, there cannot now be a
1218 function definition. */
1219 fndef_ok = false;
1220 if (c_parser_next_token_is_keyword (parser, RID_ASM))
1221 asm_name = c_parser_simple_asm_expr (parser);
1222 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1223 postfix_attrs = c_parser_attributes (parser);
1224 if (c_parser_next_token_is (parser, CPP_EQ))
1225 {
1226 tree d;
1227 struct c_expr init;
c2255bc4 1228 location_t init_loc;
27bf414c
JM
1229 c_parser_consume_token (parser);
1230 /* The declaration of the variable is in effect while
1231 its initializer is parsed. */
1232 d = start_decl (declarator, specs, true,
1233 chainon (postfix_attrs, all_prefix_attrs));
1234 if (!d)
1235 d = error_mark_node;
1236 start_init (d, asm_name, global_bindings_p ());
c2255bc4 1237 init_loc = c_parser_peek_token (parser)->location;
27bf414c
JM
1238 init = c_parser_initializer (parser);
1239 finish_init ();
1240 if (d != error_mark_node)
1241 {
1242 maybe_warn_string_init (TREE_TYPE (d), init);
c2255bc4
AH
1243 finish_decl (d, init_loc, init.value,
1244 init.original_type, asm_name);
27bf414c
JM
1245 }
1246 }
1247 else
1248 {
1249 tree d = start_decl (declarator, specs, false,
1250 chainon (postfix_attrs,
1251 all_prefix_attrs));
1252 if (d)
c2255bc4
AH
1253 finish_decl (d, UNKNOWN_LOCATION, NULL_TREE,
1254 NULL_TREE, asm_name);
27bf414c
JM
1255 }
1256 if (c_parser_next_token_is (parser, CPP_COMMA))
1257 {
1258 c_parser_consume_token (parser);
1259 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1260 all_prefix_attrs = chainon (c_parser_attributes (parser),
1261 prefix_attrs);
1262 else
1263 all_prefix_attrs = prefix_attrs;
1264 continue;
1265 }
1266 else if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1267 {
1268 c_parser_consume_token (parser);
1269 return;
1270 }
1271 else
1272 {
1273 c_parser_error (parser, "expected %<,%> or %<;%>");
1274 c_parser_skip_to_end_of_block_or_statement (parser);
1275 return;
1276 }
1277 }
1278 else if (!fndef_ok)
1279 {
1280 c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, "
1281 "%<asm%> or %<__attribute__%>");
1282 c_parser_skip_to_end_of_block_or_statement (parser);
1283 return;
1284 }
1285 /* Function definition (nested or otherwise). */
1286 if (nested)
1287 {
509c9d60 1288 pedwarn (here, OPT_pedantic, "ISO C forbids nested functions");
d2784db4 1289 c_push_function_context ();
27bf414c
JM
1290 }
1291 if (!start_function (specs, declarator, all_prefix_attrs))
1292 {
1293 /* This can appear in many cases looking nothing like a
1294 function definition, so we don't give a more specific
1295 error suggesting there was one. */
1296 c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, %<asm%> "
1297 "or %<__attribute__%>");
1298 if (nested)
d2784db4 1299 c_pop_function_context ();
27bf414c
JM
1300 break;
1301 }
1302 /* Parse old-style parameter declarations. ??? Attributes are
1303 not allowed to start declaration specifiers here because of a
1304 syntax conflict between a function declaration with attribute
1305 suffix and a function definition with an attribute prefix on
1306 first old-style parameter declaration. Following the old
1307 parser, they are not accepted on subsequent old-style
1308 parameter declarations either. However, there is no
1309 ambiguity after the first declaration, nor indeed on the
1310 first as long as we don't allow postfix attributes after a
1311 declarator with a nonempty identifier list in a definition;
1312 and postfix attributes have never been accepted here in
1313 function definitions either. */
1314 while (c_parser_next_token_is_not (parser, CPP_EOF)
1315 && c_parser_next_token_is_not (parser, CPP_OPEN_BRACE))
1316 c_parser_declaration_or_fndef (parser, false, false, true, false);
27bf414c 1317 store_parm_decls ();
1751ecd6
AH
1318 DECL_STRUCT_FUNCTION (current_function_decl)->function_start_locus
1319 = c_parser_peek_token (parser)->location;
27bf414c
JM
1320 fnbody = c_parser_compound_statement (parser);
1321 if (nested)
1322 {
1323 tree decl = current_function_decl;
1324 add_stmt (fnbody);
1325 finish_function ();
d2784db4 1326 c_pop_function_context ();
c2255bc4 1327 add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl));
27bf414c
JM
1328 }
1329 else
1330 {
1331 add_stmt (fnbody);
1332 finish_function ();
1333 }
1334 break;
1335 }
1336}
1337
1338/* Parse an asm-definition (asm() outside a function body). This is a
1339 GNU extension.
1340
1341 asm-definition:
1342 simple-asm-expr ;
1343*/
1344
1345static void
1346c_parser_asm_definition (c_parser *parser)
1347{
1348 tree asm_str = c_parser_simple_asm_expr (parser);
27bf414c 1349 if (asm_str)
474eccc6 1350 cgraph_add_asm_node (asm_str);
27bf414c
JM
1351 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
1352}
1353
1354/* Parse some declaration specifiers (possibly none) (C90 6.5, C99
1355 6.7), adding them to SPECS (which may already include some).
1356 Storage class specifiers are accepted iff SCSPEC_OK; type
1357 specifiers are accepted iff TYPESPEC_OK; attributes are accepted at
1358 the start iff START_ATTR_OK.
1359
1360 declaration-specifiers:
1361 storage-class-specifier declaration-specifiers[opt]
1362 type-specifier declaration-specifiers[opt]
1363 type-qualifier declaration-specifiers[opt]
1364 function-specifier declaration-specifiers[opt]
1365
1366 Function specifiers (inline) are from C99, and are currently
1367 handled as storage class specifiers, as is __thread.
1368
1369 C90 6.5.1, C99 6.7.1:
1370 storage-class-specifier:
1371 typedef
1372 extern
1373 static
1374 auto
1375 register
1376
1377 C99 6.7.4:
1378 function-specifier:
1379 inline
1380
1381 C90 6.5.2, C99 6.7.2:
1382 type-specifier:
1383 void
1384 char
1385 short
1386 int
1387 long
1388 float
1389 double
1390 signed
1391 unsigned
1392 _Bool
1393 _Complex
1394 [_Imaginary removed in C99 TC2]
1395 struct-or-union-specifier
1396 enum-specifier
1397 typedef-name
1398
1399 (_Bool and _Complex are new in C99.)
1400
1401 C90 6.5.3, C99 6.7.3:
1402
1403 type-qualifier:
1404 const
1405 restrict
1406 volatile
1407
1408 (restrict is new in C99.)
1409
1410 GNU extensions:
1411
1412 declaration-specifiers:
1413 attributes declaration-specifiers[opt]
1414
1415 storage-class-specifier:
1416 __thread
1417
1418 type-specifier:
1419 typeof-specifier
9a8ce21f
JG
1420 _Decimal32
1421 _Decimal64
1422 _Decimal128
ab22c1fa
CF
1423 _Fract
1424 _Accum
1425 _Sat
1426
1427 (_Fract, _Accum, and _Sat are new from ISO/IEC DTR 18037:
1428 http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1169.pdf)
27bf414c
JM
1429
1430 Objective-C:
1431
1432 type-specifier:
1433 class-name objc-protocol-refs[opt]
1434 typedef-name objc-protocol-refs
1435 objc-protocol-refs
1436*/
1437
1438static void
1439c_parser_declspecs (c_parser *parser, struct c_declspecs *specs,
1440 bool scspec_ok, bool typespec_ok, bool start_attr_ok)
1441{
1442 bool attrs_ok = start_attr_ok;
1443 bool seen_type = specs->type_seen_p;
1444 while (c_parser_next_token_is (parser, CPP_NAME)
1445 || c_parser_next_token_is (parser, CPP_KEYWORD)
1446 || (c_dialect_objc () && c_parser_next_token_is (parser, CPP_LESS)))
1447 {
1448 struct c_typespec t;
1449 tree attrs;
1450 if (c_parser_next_token_is (parser, CPP_NAME))
1451 {
1452 tree value = c_parser_peek_token (parser)->value;
1453 c_id_kind kind = c_parser_peek_token (parser)->id_kind;
1454 /* This finishes the specifiers unless a type name is OK, it
1455 is declared as a type name and a type name hasn't yet
1456 been seen. */
1457 if (!typespec_ok || seen_type
1458 || (kind != C_ID_TYPENAME && kind != C_ID_CLASSNAME))
1459 break;
1460 c_parser_consume_token (parser);
1461 seen_type = true;
1462 attrs_ok = true;
1463 if (kind == C_ID_TYPENAME
1464 && (!c_dialect_objc ()
1465 || c_parser_next_token_is_not (parser, CPP_LESS)))
1466 {
1467 t.kind = ctsk_typedef;
1468 /* For a typedef name, record the meaning, not the name.
1469 In case of 'foo foo, bar;'. */
1470 t.spec = lookup_name (value);
928c19bb
JM
1471 t.expr = NULL_TREE;
1472 t.expr_const_operands = true;
27bf414c
JM
1473 }
1474 else
1475 {
1476 tree proto = NULL_TREE;
1477 gcc_assert (c_dialect_objc ());
1478 t.kind = ctsk_objc;
1479 if (c_parser_next_token_is (parser, CPP_LESS))
1480 proto = c_parser_objc_protocol_refs (parser);
1481 t.spec = objc_get_protocol_qualified_type (value, proto);
928c19bb
JM
1482 t.expr = NULL_TREE;
1483 t.expr_const_operands = true;
27bf414c
JM
1484 }
1485 declspecs_add_type (specs, t);
1486 continue;
1487 }
1488 if (c_parser_next_token_is (parser, CPP_LESS))
1489 {
1490 /* Make "<SomeProtocol>" equivalent to "id <SomeProtocol>" -
1491 nisse@lysator.liu.se. */
1492 tree proto;
1493 gcc_assert (c_dialect_objc ());
1494 if (!typespec_ok || seen_type)
1495 break;
1496 proto = c_parser_objc_protocol_refs (parser);
1497 t.kind = ctsk_objc;
1498 t.spec = objc_get_protocol_qualified_type (NULL_TREE, proto);
928c19bb
JM
1499 t.expr = NULL_TREE;
1500 t.expr_const_operands = true;
27bf414c
JM
1501 declspecs_add_type (specs, t);
1502 continue;
1503 }
1504 gcc_assert (c_parser_next_token_is (parser, CPP_KEYWORD));
1505 switch (c_parser_peek_token (parser)->keyword)
1506 {
1507 case RID_STATIC:
1508 case RID_EXTERN:
1509 case RID_REGISTER:
1510 case RID_TYPEDEF:
1511 case RID_INLINE:
1512 case RID_AUTO:
1513 case RID_THREAD:
1514 if (!scspec_ok)
1515 goto out;
1516 attrs_ok = true;
1517 /* TODO: Distinguish between function specifiers (inline)
1518 and storage class specifiers, either here or in
1519 declspecs_add_scspec. */
1520 declspecs_add_scspec (specs, c_parser_peek_token (parser)->value);
1521 c_parser_consume_token (parser);
1522 break;
1523 case RID_UNSIGNED:
1524 case RID_LONG:
1525 case RID_SHORT:
1526 case RID_SIGNED:
1527 case RID_COMPLEX:
1528 case RID_INT:
1529 case RID_CHAR:
1530 case RID_FLOAT:
1531 case RID_DOUBLE:
1532 case RID_VOID:
9a8ce21f
JG
1533 case RID_DFLOAT32:
1534 case RID_DFLOAT64:
1535 case RID_DFLOAT128:
27bf414c 1536 case RID_BOOL:
ab22c1fa
CF
1537 case RID_FRACT:
1538 case RID_ACCUM:
1539 case RID_SAT:
27bf414c
JM
1540 if (!typespec_ok)
1541 goto out;
1542 attrs_ok = true;
1543 seen_type = true;
0bacb8c7
TT
1544 if (c_dialect_objc ())
1545 parser->objc_need_raw_identifier = true;
27bf414c
JM
1546 t.kind = ctsk_resword;
1547 t.spec = c_parser_peek_token (parser)->value;
928c19bb
JM
1548 t.expr = NULL_TREE;
1549 t.expr_const_operands = true;
27bf414c
JM
1550 declspecs_add_type (specs, t);
1551 c_parser_consume_token (parser);
1552 break;
1553 case RID_ENUM:
1554 if (!typespec_ok)
1555 goto out;
1556 attrs_ok = true;
1557 seen_type = true;
1558 t = c_parser_enum_specifier (parser);
1559 declspecs_add_type (specs, t);
1560 break;
1561 case RID_STRUCT:
1562 case RID_UNION:
1563 if (!typespec_ok)
1564 goto out;
1565 attrs_ok = true;
1566 seen_type = true;
1567 t = c_parser_struct_or_union_specifier (parser);
dab71827 1568 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, t.spec);
27bf414c
JM
1569 declspecs_add_type (specs, t);
1570 break;
1571 case RID_TYPEOF:
1572 /* ??? The old parser rejected typeof after other type
1573 specifiers, but is a syntax error the best way of
1574 handling this? */
1575 if (!typespec_ok || seen_type)
1576 goto out;
1577 attrs_ok = true;
1578 seen_type = true;
1579 t = c_parser_typeof_specifier (parser);
1580 declspecs_add_type (specs, t);
1581 break;
1582 case RID_CONST:
1583 case RID_VOLATILE:
1584 case RID_RESTRICT:
1585 attrs_ok = true;
1586 declspecs_add_qual (specs, c_parser_peek_token (parser)->value);
1587 c_parser_consume_token (parser);
1588 break;
1589 case RID_ATTRIBUTE:
1590 if (!attrs_ok)
1591 goto out;
1592 attrs = c_parser_attributes (parser);
1593 declspecs_add_attrs (specs, attrs);
1594 break;
1595 default:
1596 goto out;
1597 }
1598 }
1599 out: ;
1600}
1601
1602/* Parse an enum specifier (C90 6.5.2.2, C99 6.7.2.2).
1603
1604 enum-specifier:
1605 enum attributes[opt] identifier[opt] { enumerator-list } attributes[opt]
1606 enum attributes[opt] identifier[opt] { enumerator-list , } attributes[opt]
1607 enum attributes[opt] identifier
1608
1609 The form with trailing comma is new in C99. The forms with
1610 attributes are GNU extensions. In GNU C, we accept any expression
1611 without commas in the syntax (assignment expressions, not just
1612 conditional expressions); assignment expressions will be diagnosed
1613 as non-constant.
1614
1615 enumerator-list:
1616 enumerator
1617 enumerator-list , enumerator
1618
1619 enumerator:
1620 enumeration-constant
1621 enumeration-constant = constant-expression
1622*/
1623
1624static struct c_typespec
1625c_parser_enum_specifier (c_parser *parser)
1626{
1627 struct c_typespec ret;
1628 tree attrs;
1629 tree ident = NULL_TREE;
24b97832 1630 location_t enum_loc;
922f2908 1631 location_t ident_loc = UNKNOWN_LOCATION; /* Quiet warning. */
27bf414c 1632 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ENUM));
24b97832 1633 enum_loc = c_parser_peek_token (parser)->location;
27bf414c
JM
1634 c_parser_consume_token (parser);
1635 attrs = c_parser_attributes (parser);
c2255bc4 1636 enum_loc = c_parser_peek_token (parser)->location;
5af28c74
TT
1637 /* Set the location in case we create a decl now. */
1638 c_parser_set_source_position_from_token (c_parser_peek_token (parser));
27bf414c
JM
1639 if (c_parser_next_token_is (parser, CPP_NAME))
1640 {
1641 ident = c_parser_peek_token (parser)->value;
c7412148 1642 ident_loc = c_parser_peek_token (parser)->location;
24b97832 1643 enum_loc = ident_loc;
27bf414c
JM
1644 c_parser_consume_token (parser);
1645 }
1646 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
1647 {
1648 /* Parse an enum definition. */
7114359f 1649 struct c_enum_contents the_enum;
c2255bc4 1650 tree type = start_enum (enum_loc, &the_enum, ident);
27bf414c
JM
1651 tree postfix_attrs;
1652 /* We chain the enumerators in reverse order, then put them in
1653 forward order at the end. */
1654 tree values = NULL_TREE;
1655 c_parser_consume_token (parser);
1656 while (true)
1657 {
1658 tree enum_id;
1659 tree enum_value;
1660 tree enum_decl;
1661 bool seen_comma;
5af28c74 1662 c_token *token;
922f2908 1663 location_t comma_loc = UNKNOWN_LOCATION; /* Quiet warning. */
85790e66 1664 location_t value_loc;
27bf414c
JM
1665 if (c_parser_next_token_is_not (parser, CPP_NAME))
1666 {
1667 c_parser_error (parser, "expected identifier");
1668 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
1669 values = error_mark_node;
1670 break;
1671 }
5af28c74
TT
1672 token = c_parser_peek_token (parser);
1673 enum_id = token->value;
1674 /* Set the location in case we create a decl now. */
1675 c_parser_set_source_position_from_token (token);
85790e66 1676 value_loc = token->location;
27bf414c
JM
1677 c_parser_consume_token (parser);
1678 if (c_parser_next_token_is (parser, CPP_EQ))
1679 {
1680 c_parser_consume_token (parser);
85790e66 1681 value_loc = c_parser_peek_token (parser)->location;
27bf414c
JM
1682 enum_value = c_parser_expr_no_commas (parser, NULL).value;
1683 }
1684 else
1685 enum_value = NULL_TREE;
c2255bc4
AH
1686 enum_decl = build_enumerator (value_loc,
1687 &the_enum, enum_id, enum_value);
27bf414c
JM
1688 TREE_CHAIN (enum_decl) = values;
1689 values = enum_decl;
1690 seen_comma = false;
1691 if (c_parser_next_token_is (parser, CPP_COMMA))
1692 {
c7412148 1693 comma_loc = c_parser_peek_token (parser)->location;
27bf414c
JM
1694 seen_comma = true;
1695 c_parser_consume_token (parser);
1696 }
1697 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
1698 {
fcf73884 1699 if (seen_comma && !flag_isoc99)
509c9d60 1700 pedwarn (comma_loc, OPT_pedantic, "comma at end of enumerator list");
27bf414c
JM
1701 c_parser_consume_token (parser);
1702 break;
1703 }
1704 if (!seen_comma)
1705 {
1706 c_parser_error (parser, "expected %<,%> or %<}%>");
1707 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
1708 values = error_mark_node;
1709 break;
1710 }
1711 }
1712 postfix_attrs = c_parser_attributes (parser);
1713 ret.spec = finish_enum (type, nreverse (values),
1714 chainon (attrs, postfix_attrs));
1715 ret.kind = ctsk_tagdef;
928c19bb
JM
1716 ret.expr = NULL_TREE;
1717 ret.expr_const_operands = true;
27bf414c
JM
1718 return ret;
1719 }
1720 else if (!ident)
1721 {
1722 c_parser_error (parser, "expected %<{%>");
1723 ret.spec = error_mark_node;
1724 ret.kind = ctsk_tagref;
928c19bb
JM
1725 ret.expr = NULL_TREE;
1726 ret.expr_const_operands = true;
27bf414c
JM
1727 return ret;
1728 }
c2255bc4 1729 ret = parser_xref_tag (ident_loc, ENUMERAL_TYPE, ident);
27bf414c
JM
1730 /* In ISO C, enumerated types can be referred to only if already
1731 defined. */
1732 if (pedantic && !COMPLETE_TYPE_P (ret.spec))
c7412148
TT
1733 {
1734 gcc_assert (ident);
c2255bc4 1735 pedwarn (enum_loc, OPT_pedantic,
509c9d60 1736 "ISO C forbids forward references to %<enum%> types");
c7412148 1737 }
27bf414c
JM
1738 return ret;
1739}
1740
1741/* Parse a struct or union specifier (C90 6.5.2.1, C99 6.7.2.1).
1742
1743 struct-or-union-specifier:
1744 struct-or-union attributes[opt] identifier[opt]
1745 { struct-contents } attributes[opt]
1746 struct-or-union attributes[opt] identifier
1747
1748 struct-contents:
1749 struct-declaration-list
1750
1751 struct-declaration-list:
1752 struct-declaration ;
1753 struct-declaration-list struct-declaration ;
1754
1755 GNU extensions:
1756
1757 struct-contents:
1758 empty
1759 struct-declaration
1760 struct-declaration-list struct-declaration
1761
1762 struct-declaration-list:
1763 struct-declaration-list ;
1764 ;
1765
1766 (Note that in the syntax here, unlike that in ISO C, the semicolons
1767 are included here rather than in struct-declaration, in order to
1768 describe the syntax with extra semicolons and missing semicolon at
1769 end.)
1770
1771 Objective-C:
1772
1773 struct-declaration-list:
1774 @defs ( class-name )
1775
1776 (Note this does not include a trailing semicolon, but can be
1777 followed by further declarations, and gets a pedwarn-if-pedantic
1778 when followed by a semicolon.) */
1779
1780static struct c_typespec
1781c_parser_struct_or_union_specifier (c_parser *parser)
1782{
1783 struct c_typespec ret;
1784 tree attrs;
1785 tree ident = NULL_TREE;
24b97832
ILT
1786 location_t struct_loc;
1787 location_t ident_loc = UNKNOWN_LOCATION;
27bf414c
JM
1788 enum tree_code code;
1789 switch (c_parser_peek_token (parser)->keyword)
1790 {
1791 case RID_STRUCT:
1792 code = RECORD_TYPE;
1793 break;
1794 case RID_UNION:
1795 code = UNION_TYPE;
1796 break;
1797 default:
1798 gcc_unreachable ();
1799 }
24b97832 1800 struct_loc = c_parser_peek_token (parser)->location;
27bf414c
JM
1801 c_parser_consume_token (parser);
1802 attrs = c_parser_attributes (parser);
c2255bc4 1803
5af28c74
TT
1804 /* Set the location in case we create a decl now. */
1805 c_parser_set_source_position_from_token (c_parser_peek_token (parser));
c2255bc4 1806
27bf414c
JM
1807 if (c_parser_next_token_is (parser, CPP_NAME))
1808 {
1809 ident = c_parser_peek_token (parser)->value;
24b97832
ILT
1810 ident_loc = c_parser_peek_token (parser)->location;
1811 struct_loc = ident_loc;
27bf414c
JM
1812 c_parser_consume_token (parser);
1813 }
1814 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
1815 {
1816 /* Parse a struct or union definition. Start the scope of the
1817 tag before parsing components. */
24b97832
ILT
1818 bool in_struct;
1819 VEC(tree,heap) *struct_types;
c2255bc4
AH
1820 tree type = start_struct (struct_loc, code, ident,
1821 &in_struct, &struct_types);
27bf414c
JM
1822 tree postfix_attrs;
1823 /* We chain the components in reverse order, then put them in
1824 forward order at the end. Each struct-declaration may
1825 declare multiple components (comma-separated), so we must use
1826 chainon to join them, although when parsing each
1827 struct-declaration we can use TREE_CHAIN directly.
1828
1829 The theory behind all this is that there will be more
1830 semicolon separated fields than comma separated fields, and
1831 so we'll be minimizing the number of node traversals required
1832 by chainon. */
1833 tree contents = NULL_TREE;
1834 c_parser_consume_token (parser);
1835 /* Handle the Objective-C @defs construct,
1836 e.g. foo(sizeof(struct{ @defs(ClassName) }));. */
1837 if (c_parser_next_token_is_keyword (parser, RID_AT_DEFS))
1838 {
1839 tree name;
1840 gcc_assert (c_dialect_objc ());
1841 c_parser_consume_token (parser);
1842 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
1843 goto end_at_defs;
1844 if (c_parser_next_token_is (parser, CPP_NAME)
1845 && c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME)
1846 {
1847 name = c_parser_peek_token (parser)->value;
1848 c_parser_consume_token (parser);
1849 }
1850 else
1851 {
1852 c_parser_error (parser, "expected class name");
1853 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
1854 goto end_at_defs;
1855 }
1856 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
1857 "expected %<)%>");
1858 contents = nreverse (objc_get_class_ivars (name));
1859 }
1860 end_at_defs:
1861 /* Parse the struct-declarations and semicolons. Problems with
1862 semicolons are diagnosed here; empty structures are diagnosed
1863 elsewhere. */
1864 while (true)
1865 {
1866 tree decls;
1867 /* Parse any stray semicolon. */
1868 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1869 {
509c9d60
MLI
1870 pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic,
1871 "extra semicolon in struct or union specified");
27bf414c
JM
1872 c_parser_consume_token (parser);
1873 continue;
1874 }
1875 /* Stop if at the end of the struct or union contents. */
1876 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
1877 {
1878 c_parser_consume_token (parser);
1879 break;
1880 }
bc4071dd
RH
1881 /* Accept #pragmas at struct scope. */
1882 if (c_parser_next_token_is (parser, CPP_PRAGMA))
1883 {
1884 c_parser_pragma (parser, pragma_external);
1885 continue;
1886 }
27bf414c
JM
1887 /* Parse some comma-separated declarations, but not the
1888 trailing semicolon if any. */
1889 decls = c_parser_struct_declaration (parser);
1890 contents = chainon (decls, contents);
1891 /* If no semicolon follows, either we have a parse error or
1892 are at the end of the struct or union and should
1893 pedwarn. */
1894 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1895 c_parser_consume_token (parser);
1896 else
1897 {
1898 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
509c9d60
MLI
1899 pedwarn (c_parser_peek_token (parser)->location, 0,
1900 "no semicolon at end of struct or union");
27bf414c
JM
1901 else
1902 {
1903 c_parser_error (parser, "expected %<;%>");
1904 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
1905 break;
1906 }
1907 }
1908 }
1909 postfix_attrs = c_parser_attributes (parser);
c2255bc4 1910 ret.spec = finish_struct (struct_loc, type, nreverse (contents),
24b97832
ILT
1911 chainon (attrs, postfix_attrs),
1912 in_struct, struct_types);
27bf414c 1913 ret.kind = ctsk_tagdef;
928c19bb
JM
1914 ret.expr = NULL_TREE;
1915 ret.expr_const_operands = true;
27bf414c
JM
1916 return ret;
1917 }
1918 else if (!ident)
1919 {
1920 c_parser_error (parser, "expected %<{%>");
1921 ret.spec = error_mark_node;
1922 ret.kind = ctsk_tagref;
928c19bb
JM
1923 ret.expr = NULL_TREE;
1924 ret.expr_const_operands = true;
67c2939d 1925 return ret;
27bf414c 1926 }
c2255bc4 1927 ret = parser_xref_tag (ident_loc, code, ident);
27bf414c
JM
1928 return ret;
1929}
1930
1931/* Parse a struct-declaration (C90 6.5.2.1, C99 6.7.2.1), *without*
1932 the trailing semicolon.
1933
1934 struct-declaration:
1935 specifier-qualifier-list struct-declarator-list
1936
1937 specifier-qualifier-list:
1938 type-specifier specifier-qualifier-list[opt]
1939 type-qualifier specifier-qualifier-list[opt]
1940 attributes specifier-qualifier-list[opt]
1941
1942 struct-declarator-list:
1943 struct-declarator
1944 struct-declarator-list , attributes[opt] struct-declarator
1945
1946 struct-declarator:
1947 declarator attributes[opt]
1948 declarator[opt] : constant-expression attributes[opt]
1949
1950 GNU extensions:
1951
1952 struct-declaration:
1953 __extension__ struct-declaration
1954 specifier-qualifier-list
1955
1956 Unlike the ISO C syntax, semicolons are handled elsewhere. The use
1957 of attributes where shown is a GNU extension. In GNU C, we accept
1958 any expression without commas in the syntax (assignment
1959 expressions, not just conditional expressions); assignment
1960 expressions will be diagnosed as non-constant. */
1961
1962static tree
1963c_parser_struct_declaration (c_parser *parser)
1964{
1965 struct c_declspecs *specs;
1966 tree prefix_attrs;
1967 tree all_prefix_attrs;
1968 tree decls;
c7412148 1969 location_t decl_loc;
27bf414c
JM
1970 if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
1971 {
1972 int ext;
1973 tree decl;
1974 ext = disable_extension_diagnostics ();
1975 c_parser_consume_token (parser);
1976 decl = c_parser_struct_declaration (parser);
1977 restore_extension_diagnostics (ext);
1978 return decl;
1979 }
1980 specs = build_null_declspecs ();
c7412148 1981 decl_loc = c_parser_peek_token (parser)->location;
27bf414c
JM
1982 c_parser_declspecs (parser, specs, false, true, true);
1983 if (parser->error)
67c2939d 1984 return NULL_TREE;
27bf414c
JM
1985 if (!specs->declspecs_seen_p)
1986 {
1987 c_parser_error (parser, "expected specifier-qualifier-list");
1988 return NULL_TREE;
1989 }
1990 finish_declspecs (specs);
1991 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1992 {
1993 tree ret;
1994 if (!specs->type_seen_p)
1995 {
509c9d60
MLI
1996 pedwarn (decl_loc, OPT_pedantic,
1997 "ISO C forbids member declarations with no members");
27bf414c
JM
1998 shadow_tag_warned (specs, pedantic);
1999 ret = NULL_TREE;
2000 }
2001 else
2002 {
2003 /* Support for unnamed structs or unions as members of
2004 structs or unions (which is [a] useful and [b] supports
2005 MS P-SDK). */
b9baeecd 2006 tree attrs = NULL;
3d10ed6c
AH
2007
2008 ret = grokfield (c_parser_peek_token (parser)->location,
2009 build_id_declarator (NULL_TREE), specs,
b9baeecd 2010 NULL_TREE, &attrs);
0ad7e054
RS
2011 if (ret)
2012 decl_attributes (&ret, attrs, 0);
27bf414c
JM
2013 }
2014 return ret;
2015 }
2016 pending_xref_error ();
2017 prefix_attrs = specs->attrs;
2018 all_prefix_attrs = prefix_attrs;
2019 specs->attrs = NULL_TREE;
2020 decls = NULL_TREE;
2021 while (true)
2022 {
2023 /* Declaring one or more declarators or un-named bit-fields. */
2024 struct c_declarator *declarator;
2025 bool dummy = false;
2026 if (c_parser_next_token_is (parser, CPP_COLON))
2027 declarator = build_id_declarator (NULL_TREE);
2028 else
2029 declarator = c_parser_declarator (parser, specs->type_seen_p,
2030 C_DTR_NORMAL, &dummy);
2031 if (declarator == NULL)
2032 {
2033 c_parser_skip_to_end_of_block_or_statement (parser);
2034 break;
2035 }
2036 if (c_parser_next_token_is (parser, CPP_COLON)
2037 || c_parser_next_token_is (parser, CPP_COMMA)
2038 || c_parser_next_token_is (parser, CPP_SEMICOLON)
2039 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE)
2040 || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2041 {
2042 tree postfix_attrs = NULL_TREE;
2043 tree width = NULL_TREE;
2044 tree d;
2045 if (c_parser_next_token_is (parser, CPP_COLON))
2046 {
2047 c_parser_consume_token (parser);
2048 width = c_parser_expr_no_commas (parser, NULL).value;
2049 }
2050 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2051 postfix_attrs = c_parser_attributes (parser);
3d10ed6c
AH
2052 d = grokfield (c_parser_peek_token (parser)->location,
2053 declarator, specs, width, &all_prefix_attrs);
27bf414c
JM
2054 decl_attributes (&d, chainon (postfix_attrs,
2055 all_prefix_attrs), 0);
2056 TREE_CHAIN (d) = decls;
2057 decls = d;
2058 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2059 all_prefix_attrs = chainon (c_parser_attributes (parser),
2060 prefix_attrs);
2061 else
2062 all_prefix_attrs = prefix_attrs;
2063 if (c_parser_next_token_is (parser, CPP_COMMA))
2064 c_parser_consume_token (parser);
2065 else if (c_parser_next_token_is (parser, CPP_SEMICOLON)
2066 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2067 {
2068 /* Semicolon consumed in caller. */
2069 break;
2070 }
2071 else
2072 {
2073 c_parser_error (parser, "expected %<,%>, %<;%> or %<}%>");
2074 break;
2075 }
2076 }
2077 else
2078 {
2079 c_parser_error (parser,
2080 "expected %<:%>, %<,%>, %<;%>, %<}%> or "
2081 "%<__attribute__%>");
2082 break;
2083 }
2084 }
2085 return decls;
2086}
2087
2088/* Parse a typeof specifier (a GNU extension).
2089
2090 typeof-specifier:
2091 typeof ( expression )
2092 typeof ( type-name )
2093*/
2094
2095static struct c_typespec
2096c_parser_typeof_specifier (c_parser *parser)
2097{
2098 struct c_typespec ret;
2099 ret.kind = ctsk_typeof;
2100 ret.spec = error_mark_node;
928c19bb
JM
2101 ret.expr = NULL_TREE;
2102 ret.expr_const_operands = true;
27bf414c
JM
2103 gcc_assert (c_parser_next_token_is_keyword (parser, RID_TYPEOF));
2104 c_parser_consume_token (parser);
7d882b83 2105 c_inhibit_evaluation_warnings++;
27bf414c
JM
2106 in_typeof++;
2107 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2108 {
7d882b83 2109 c_inhibit_evaluation_warnings--;
27bf414c
JM
2110 in_typeof--;
2111 return ret;
2112 }
2113 if (c_parser_next_token_starts_typename (parser))
2114 {
2115 struct c_type_name *type = c_parser_type_name (parser);
7d882b83 2116 c_inhibit_evaluation_warnings--;
27bf414c
JM
2117 in_typeof--;
2118 if (type != NULL)
2119 {
928c19bb 2120 ret.spec = groktypename (type, &ret.expr, &ret.expr_const_operands);
27bf414c
JM
2121 pop_maybe_used (variably_modified_type_p (ret.spec, NULL_TREE));
2122 }
2123 }
2124 else
2125 {
52ffd86e 2126 bool was_vm;
c7412148 2127 location_t here = c_parser_peek_token (parser)->location;
27bf414c 2128 struct c_expr expr = c_parser_expression (parser);
7d882b83 2129 c_inhibit_evaluation_warnings--;
27bf414c
JM
2130 in_typeof--;
2131 if (TREE_CODE (expr.value) == COMPONENT_REF
2132 && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
3ba09659 2133 error_at (here, "%<typeof%> applied to a bit-field");
27bf414c 2134 ret.spec = TREE_TYPE (expr.value);
52ffd86e 2135 was_vm = variably_modified_type_p (ret.spec, NULL_TREE);
928c19bb
JM
2136 /* This is returned with the type so that when the type is
2137 evaluated, this can be evaluated. */
2138 if (was_vm)
2139 ret.expr = c_fully_fold (expr.value, false, &ret.expr_const_operands);
52ffd86e 2140 pop_maybe_used (was_vm);
27bf414c
JM
2141 }
2142 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
2143 return ret;
2144}
2145
2146/* Parse a declarator, possibly an abstract declarator (C90 6.5.4,
2147 6.5.5, C99 6.7.5, 6.7.6). If TYPE_SEEN_P then a typedef name may
2148 be redeclared; otherwise it may not. KIND indicates which kind of
2149 declarator is wanted. Returns a valid declarator except in the
2150 case of a syntax error in which case NULL is returned. *SEEN_ID is
2151 set to true if an identifier being declared is seen; this is used
2152 to diagnose bad forms of abstract array declarators and to
2153 determine whether an identifier list is syntactically permitted.
2154
2155 declarator:
2156 pointer[opt] direct-declarator
2157
2158 direct-declarator:
2159 identifier
2160 ( attributes[opt] declarator )
2161 direct-declarator array-declarator
2162 direct-declarator ( parameter-type-list )
2163 direct-declarator ( identifier-list[opt] )
2164
2165 pointer:
2166 * type-qualifier-list[opt]
2167 * type-qualifier-list[opt] pointer
2168
2169 type-qualifier-list:
2170 type-qualifier
2171 attributes
2172 type-qualifier-list type-qualifier
2173 type-qualifier-list attributes
2174
2175 parameter-type-list:
2176 parameter-list
2177 parameter-list , ...
2178
2179 parameter-list:
2180 parameter-declaration
2181 parameter-list , parameter-declaration
2182
2183 parameter-declaration:
2184 declaration-specifiers declarator attributes[opt]
2185 declaration-specifiers abstract-declarator[opt] attributes[opt]
2186
2187 identifier-list:
2188 identifier
2189 identifier-list , identifier
2190
2191 abstract-declarator:
2192 pointer
2193 pointer[opt] direct-abstract-declarator
2194
2195 direct-abstract-declarator:
2196 ( attributes[opt] abstract-declarator )
2197 direct-abstract-declarator[opt] array-declarator
2198 direct-abstract-declarator[opt] ( parameter-type-list[opt] )
2199
2200 GNU extensions:
2201
2202 direct-declarator:
2203 direct-declarator ( parameter-forward-declarations
2204 parameter-type-list[opt] )
2205
2206 direct-abstract-declarator:
c22cacf3 2207 direct-abstract-declarator[opt] ( parameter-forward-declarations
27bf414c
JM
2208 parameter-type-list[opt] )
2209
2210 parameter-forward-declarations:
2211 parameter-list ;
2212 parameter-forward-declarations parameter-list ;
2213
2214 The uses of attributes shown above are GNU extensions.
2215
2216 Some forms of array declarator are not included in C99 in the
2217 syntax for abstract declarators; these are disallowed elsewhere.
2218 This may be a defect (DR#289).
2219
2220 This function also accepts an omitted abstract declarator as being
2221 an abstract declarator, although not part of the formal syntax. */
2222
2223static struct c_declarator *
2224c_parser_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
2225 bool *seen_id)
2226{
2227 /* Parse any initial pointer part. */
2228 if (c_parser_next_token_is (parser, CPP_MULT))
2229 {
2230 struct c_declspecs *quals_attrs = build_null_declspecs ();
2231 struct c_declarator *inner;
2232 c_parser_consume_token (parser);
2233 c_parser_declspecs (parser, quals_attrs, false, false, true);
2234 inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
2235 if (inner == NULL)
2236 return NULL;
2237 else
2238 return make_pointer_declarator (quals_attrs, inner);
2239 }
2240 /* Now we have a direct declarator, direct abstract declarator or
2241 nothing (which counts as a direct abstract declarator here). */
2242 return c_parser_direct_declarator (parser, type_seen_p, kind, seen_id);
2243}
2244
2245/* Parse a direct declarator or direct abstract declarator; arguments
2246 as c_parser_declarator. */
2247
2248static struct c_declarator *
2249c_parser_direct_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
2250 bool *seen_id)
2251{
2252 /* The direct declarator must start with an identifier (possibly
2253 omitted) or a parenthesized declarator (possibly abstract). In
2254 an ordinary declarator, initial parentheses must start a
2255 parenthesized declarator. In an abstract declarator or parameter
2256 declarator, they could start a parenthesized declarator or a
2257 parameter list. To tell which, the open parenthesis and any
2258 following attributes must be read. If a declaration specifier
2259 follows, then it is a parameter list; if the specifier is a
2260 typedef name, there might be an ambiguity about redeclaring it,
2261 which is resolved in the direction of treating it as a typedef
2262 name. If a close parenthesis follows, it is also an empty
2263 parameter list, as the syntax does not permit empty abstract
0fa2e4df 2264 declarators. Otherwise, it is a parenthesized declarator (in
27bf414c
JM
2265 which case the analysis may be repeated inside it, recursively).
2266
2267 ??? There is an ambiguity in a parameter declaration "int
2268 (__attribute__((foo)) x)", where x is not a typedef name: it
2269 could be an abstract declarator for a function, or declare x with
2270 parentheses. The proper resolution of this ambiguity needs
2271 documenting. At present we follow an accident of the old
2272 parser's implementation, whereby the first parameter must have
2273 some declaration specifiers other than just attributes. Thus as
0fa2e4df 2274 a parameter declaration it is treated as a parenthesized
27bf414c
JM
2275 parameter named x, and as an abstract declarator it is
2276 rejected.
2277
2278 ??? Also following the old parser, attributes inside an empty
2279 parameter list are ignored, making it a list not yielding a
2280 prototype, rather than giving an error or making it have one
2281 parameter with implicit type int.
2282
2283 ??? Also following the old parser, typedef names may be
2284 redeclared in declarators, but not Objective-C class names. */
2285
2286 if (kind != C_DTR_ABSTRACT
2287 && c_parser_next_token_is (parser, CPP_NAME)
2288 && ((type_seen_p
2289 && c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME)
2290 || c_parser_peek_token (parser)->id_kind == C_ID_ID))
2291 {
2292 struct c_declarator *inner
2293 = build_id_declarator (c_parser_peek_token (parser)->value);
2294 *seen_id = true;
6037d88d 2295 inner->id_loc = c_parser_peek_token (parser)->location;
27bf414c
JM
2296 c_parser_consume_token (parser);
2297 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
2298 }
2299
2300 if (kind != C_DTR_NORMAL
2301 && c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
2302 {
2303 struct c_declarator *inner = build_id_declarator (NULL_TREE);
2304 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
2305 }
2306
2307 /* Either we are at the end of an abstract declarator, or we have
2308 parentheses. */
2309
2310 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
2311 {
2312 tree attrs;
2313 struct c_declarator *inner;
2314 c_parser_consume_token (parser);
2315 attrs = c_parser_attributes (parser);
2316 if (kind != C_DTR_NORMAL
2317 && (c_parser_next_token_starts_declspecs (parser)
2318 || c_parser_next_token_is (parser, CPP_CLOSE_PAREN)))
2319 {
2320 struct c_arg_info *args
2321 = c_parser_parms_declarator (parser, kind == C_DTR_NORMAL,
2322 attrs);
2323 if (args == NULL)
2324 return NULL;
2325 else
2326 {
2327 inner
2328 = build_function_declarator (args,
2329 build_id_declarator (NULL_TREE));
2330 return c_parser_direct_declarator_inner (parser, *seen_id,
2331 inner);
2332 }
2333 }
2334 /* A parenthesized declarator. */
2335 inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
2336 if (inner != NULL && attrs != NULL)
2337 inner = build_attrs_declarator (attrs, inner);
2338 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2339 {
2340 c_parser_consume_token (parser);
2341 if (inner == NULL)
2342 return NULL;
2343 else
2344 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
2345 }
2346 else
2347 {
2348 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2349 "expected %<)%>");
2350 return NULL;
2351 }
2352 }
2353 else
2354 {
2355 if (kind == C_DTR_NORMAL)
2356 {
2357 c_parser_error (parser, "expected identifier or %<(%>");
2358 return NULL;
2359 }
2360 else
2361 return build_id_declarator (NULL_TREE);
2362 }
2363}
2364
2365/* Parse part of a direct declarator or direct abstract declarator,
2366 given that some (in INNER) has already been parsed; ID_PRESENT is
2367 true if an identifier is present, false for an abstract
2368 declarator. */
2369
2370static struct c_declarator *
2371c_parser_direct_declarator_inner (c_parser *parser, bool id_present,
2372 struct c_declarator *inner)
2373{
2374 /* Parse a sequence of array declarators and parameter lists. */
2375 if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
2376 {
c2255bc4 2377 location_t brace_loc = c_parser_peek_token (parser)->location;
27bf414c
JM
2378 struct c_declarator *declarator;
2379 struct c_declspecs *quals_attrs = build_null_declspecs ();
2380 bool static_seen;
2381 bool star_seen;
2382 tree dimen;
2383 c_parser_consume_token (parser);
2384 c_parser_declspecs (parser, quals_attrs, false, false, true);
2385 static_seen = c_parser_next_token_is_keyword (parser, RID_STATIC);
2386 if (static_seen)
2387 c_parser_consume_token (parser);
2388 if (static_seen && !quals_attrs->declspecs_seen_p)
2389 c_parser_declspecs (parser, quals_attrs, false, false, true);
2390 if (!quals_attrs->declspecs_seen_p)
2391 quals_attrs = NULL;
2392 /* If "static" is present, there must be an array dimension.
2393 Otherwise, there may be a dimension, "*", or no
2394 dimension. */
2395 if (static_seen)
2396 {
2397 star_seen = false;
2398 dimen = c_parser_expr_no_commas (parser, NULL).value;
2399 }
2400 else
2401 {
2402 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
2403 {
2404 dimen = NULL_TREE;
2405 star_seen = false;
2406 }
2407 else if (c_parser_next_token_is (parser, CPP_MULT))
2408 {
2409 if (c_parser_peek_2nd_token (parser)->type == CPP_CLOSE_SQUARE)
2410 {
2411 dimen = NULL_TREE;
2412 star_seen = true;
2413 c_parser_consume_token (parser);
2414 }
2415 else
2416 {
2417 star_seen = false;
2418 dimen = c_parser_expr_no_commas (parser, NULL).value;
2419 }
2420 }
2421 else
2422 {
2423 star_seen = false;
2424 dimen = c_parser_expr_no_commas (parser, NULL).value;
2425 }
2426 }
2427 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
2428 c_parser_consume_token (parser);
2429 else
2430 {
2431 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
2432 "expected %<]%>");
2433 return NULL;
2434 }
c2255bc4
AH
2435 declarator = build_array_declarator (brace_loc, dimen, quals_attrs,
2436 static_seen, star_seen);
52ffd86e
MS
2437 if (declarator == NULL)
2438 return NULL;
6ac0194d 2439 inner = set_array_declarator_inner (declarator, inner);
27bf414c
JM
2440 return c_parser_direct_declarator_inner (parser, id_present, inner);
2441 }
2442 else if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
2443 {
2444 tree attrs;
2445 struct c_arg_info *args;
2446 c_parser_consume_token (parser);
2447 attrs = c_parser_attributes (parser);
2448 args = c_parser_parms_declarator (parser, id_present, attrs);
2449 if (args == NULL)
2450 return NULL;
2451 else
2452 {
2453 inner = build_function_declarator (args, inner);
2454 return c_parser_direct_declarator_inner (parser, id_present, inner);
2455 }
2456 }
2457 return inner;
2458}
2459
2460/* Parse a parameter list or identifier list, including the closing
2461 parenthesis but not the opening one. ATTRS are the attributes at
2462 the start of the list. ID_LIST_OK is true if an identifier list is
2463 acceptable; such a list must not have attributes at the start. */
2464
2465static struct c_arg_info *
2466c_parser_parms_declarator (c_parser *parser, bool id_list_ok, tree attrs)
2467{
2468 push_scope ();
2469 declare_parm_level ();
2470 /* If the list starts with an identifier, it is an identifier list.
2471 Otherwise, it is either a prototype list or an empty list. */
2472 if (id_list_ok
2473 && !attrs
2474 && c_parser_next_token_is (parser, CPP_NAME)
2475 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
2476 {
7fa3585c 2477 tree list = NULL_TREE, *nextp = &list;
27bf414c
JM
2478 while (c_parser_next_token_is (parser, CPP_NAME)
2479 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
2480 {
7fa3585c
GK
2481 *nextp = build_tree_list (NULL_TREE,
2482 c_parser_peek_token (parser)->value);
2483 nextp = & TREE_CHAIN (*nextp);
27bf414c
JM
2484 c_parser_consume_token (parser);
2485 if (c_parser_next_token_is_not (parser, CPP_COMMA))
2486 break;
2487 c_parser_consume_token (parser);
2488 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2489 {
2490 c_parser_error (parser, "expected identifier");
2491 break;
2492 }
2493 }
2494 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2495 {
2496 struct c_arg_info *ret = XOBNEW (&parser_obstack, struct c_arg_info);
2497 ret->parms = 0;
2498 ret->tags = 0;
2499 ret->types = list;
2500 ret->others = 0;
3542a5c0 2501 ret->pending_sizes = 0;
6a1f8611 2502 ret->had_vla_unspec = 0;
27bf414c
JM
2503 c_parser_consume_token (parser);
2504 pop_scope ();
2505 return ret;
2506 }
2507 else
2508 {
2509 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2510 "expected %<)%>");
2511 pop_scope ();
2512 return NULL;
2513 }
2514 }
2515 else
2516 {
2517 struct c_arg_info *ret = c_parser_parms_list_declarator (parser, attrs);
2518 pop_scope ();
2519 return ret;
2520 }
2521}
2522
2523/* Parse a parameter list (possibly empty), including the closing
2524 parenthesis but not the opening one. ATTRS are the attributes at
2525 the start of the list. */
2526
2527static struct c_arg_info *
2528c_parser_parms_list_declarator (c_parser *parser, tree attrs)
2529{
2530 bool good_parm = false;
2531 /* ??? Following the old parser, forward parameter declarations may
2532 use abstract declarators, and if no real parameter declarations
2533 follow the forward declarations then this is not diagnosed. Also
2534 note as above that attributes are ignored as the only contents of
2535 the parentheses, or as the only contents after forward
2536 declarations. */
2537 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2538 {
2539 struct c_arg_info *ret = XOBNEW (&parser_obstack, struct c_arg_info);
2540 ret->parms = 0;
2541 ret->tags = 0;
2542 ret->types = 0;
2543 ret->others = 0;
3542a5c0 2544 ret->pending_sizes = 0;
6a1f8611 2545 ret->had_vla_unspec = 0;
27bf414c
JM
2546 c_parser_consume_token (parser);
2547 return ret;
2548 }
2549 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
2550 {
2551 struct c_arg_info *ret = XOBNEW (&parser_obstack, struct c_arg_info);
2552 ret->parms = 0;
2553 ret->tags = 0;
2554 ret->others = 0;
3542a5c0 2555 ret->pending_sizes = 0;
6a1f8611 2556 ret->had_vla_unspec = 0;
27bf414c
JM
2557 /* Suppress -Wold-style-definition for this case. */
2558 ret->types = error_mark_node;
3ba09659
AH
2559 error_at (c_parser_peek_token (parser)->location,
2560 "ISO C requires a named argument before %<...%>");
27bf414c
JM
2561 c_parser_consume_token (parser);
2562 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2563 {
2564 c_parser_consume_token (parser);
2565 return ret;
2566 }
2567 else
2568 {
2569 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2570 "expected %<)%>");
2571 return NULL;
2572 }
2573 }
2574 /* Nonempty list of parameters, either terminated with semicolon
2575 (forward declarations; recurse) or with close parenthesis (normal
2576 function) or with ", ... )" (variadic function). */
2577 while (true)
2578 {
2579 /* Parse a parameter. */
2580 struct c_parm *parm = c_parser_parameter_declaration (parser, attrs);
2581 attrs = NULL_TREE;
2582 if (parm != NULL)
2583 {
2584 good_parm = true;
2585 push_parm_decl (parm);
2586 }
2587 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
2588 {
2589 tree new_attrs;
2590 c_parser_consume_token (parser);
91d975b8 2591 mark_forward_parm_decls ();
27bf414c
JM
2592 new_attrs = c_parser_attributes (parser);
2593 return c_parser_parms_list_declarator (parser, new_attrs);
2594 }
2595 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2596 {
2597 c_parser_consume_token (parser);
2598 if (good_parm)
2599 return get_parm_info (false);
2600 else
2601 {
2602 struct c_arg_info *ret
2603 = XOBNEW (&parser_obstack, struct c_arg_info);
2604 ret->parms = 0;
2605 ret->tags = 0;
2606 ret->types = 0;
2607 ret->others = 0;
3542a5c0 2608 ret->pending_sizes = 0;
6a1f8611 2609 ret->had_vla_unspec = 0;
27bf414c
JM
2610 return ret;
2611 }
2612 }
2613 if (!c_parser_require (parser, CPP_COMMA,
2614 "expected %<;%>, %<,%> or %<)%>"))
2615 {
2616 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
f007042e 2617 get_pending_sizes ();
27bf414c
JM
2618 return NULL;
2619 }
2620 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
2621 {
2622 c_parser_consume_token (parser);
2623 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2624 {
2625 c_parser_consume_token (parser);
2626 if (good_parm)
2627 return get_parm_info (true);
2628 else
2629 {
2630 struct c_arg_info *ret
2631 = XOBNEW (&parser_obstack, struct c_arg_info);
2632 ret->parms = 0;
2633 ret->tags = 0;
2634 ret->types = 0;
2635 ret->others = 0;
3542a5c0 2636 ret->pending_sizes = 0;
6a1f8611 2637 ret->had_vla_unspec = 0;
27bf414c
JM
2638 return ret;
2639 }
2640 }
2641 else
2642 {
2643 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2644 "expected %<)%>");
f007042e 2645 get_pending_sizes ();
27bf414c
JM
2646 return NULL;
2647 }
2648 }
2649 }
2650}
2651
2652/* Parse a parameter declaration. ATTRS are the attributes at the
2653 start of the declaration if it is the first parameter. */
2654
2655static struct c_parm *
2656c_parser_parameter_declaration (c_parser *parser, tree attrs)
2657{
2658 struct c_declspecs *specs;
2659 struct c_declarator *declarator;
2660 tree prefix_attrs;
2661 tree postfix_attrs = NULL_TREE;
2662 bool dummy = false;
2663 if (!c_parser_next_token_starts_declspecs (parser))
2664 {
2665 /* ??? In some Objective-C cases '...' isn't applicable so there
2666 should be a different message. */
2667 c_parser_error (parser,
2668 "expected declaration specifiers or %<...%>");
2669 c_parser_skip_to_end_of_parameter (parser);
2670 return NULL;
2671 }
2672 specs = build_null_declspecs ();
2673 if (attrs)
2674 {
2675 declspecs_add_attrs (specs, attrs);
2676 attrs = NULL_TREE;
2677 }
2678 c_parser_declspecs (parser, specs, true, true, true);
2679 finish_declspecs (specs);
2680 pending_xref_error ();
2681 prefix_attrs = specs->attrs;
2682 specs->attrs = NULL_TREE;
2683 declarator = c_parser_declarator (parser, specs->type_seen_p,
2684 C_DTR_PARM, &dummy);
2685 if (declarator == NULL)
2686 {
2687 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
2688 return NULL;
2689 }
2690 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2691 postfix_attrs = c_parser_attributes (parser);
2692 return build_c_parm (specs, chainon (postfix_attrs, prefix_attrs),
2693 declarator);
2694}
2695
2696/* Parse a string literal in an asm expression. It should not be
2697 translated, and wide string literals are an error although
2698 permitted by the syntax. This is a GNU extension.
2699
2700 asm-string-literal:
2701 string-literal
2702
2703 ??? At present, following the old parser, the caller needs to have
46c2514e
TT
2704 set lex_untranslated_string to 1. It would be better to follow the
2705 C++ parser rather than using this kludge. */
27bf414c
JM
2706
2707static tree
2708c_parser_asm_string_literal (c_parser *parser)
2709{
2710 tree str;
2711 if (c_parser_next_token_is (parser, CPP_STRING))
2712 {
2713 str = c_parser_peek_token (parser)->value;
2714 c_parser_consume_token (parser);
2715 }
2716 else if (c_parser_next_token_is (parser, CPP_WSTRING))
2717 {
3ba09659
AH
2718 error_at (c_parser_peek_token (parser)->location,
2719 "wide string literal in %<asm%>");
27bf414c
JM
2720 str = build_string (1, "");
2721 c_parser_consume_token (parser);
2722 }
2723 else
2724 {
2725 c_parser_error (parser, "expected string literal");
2726 str = NULL_TREE;
2727 }
2728 return str;
2729}
2730
2731/* Parse a simple asm expression. This is used in restricted
2732 contexts, where a full expression with inputs and outputs does not
2733 make sense. This is a GNU extension.
2734
2735 simple-asm-expr:
2736 asm ( asm-string-literal )
2737*/
2738
2739static tree
2740c_parser_simple_asm_expr (c_parser *parser)
2741{
2742 tree str;
2743 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
2744 /* ??? Follow the C++ parser rather than using the
46c2514e
TT
2745 lex_untranslated_string kludge. */
2746 parser->lex_untranslated_string = true;
27bf414c
JM
2747 c_parser_consume_token (parser);
2748 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2749 {
46c2514e 2750 parser->lex_untranslated_string = false;
27bf414c
JM
2751 return NULL_TREE;
2752 }
2753 str = c_parser_asm_string_literal (parser);
46c2514e 2754 parser->lex_untranslated_string = false;
27bf414c
JM
2755 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
2756 {
2757 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
2758 return NULL_TREE;
2759 }
2760 return str;
2761}
2762
2763/* Parse (possibly empty) attributes. This is a GNU extension.
2764
2765 attributes:
2766 empty
2767 attributes attribute
2768
2769 attribute:
2770 __attribute__ ( ( attribute-list ) )
2771
2772 attribute-list:
2773 attrib
2774 attribute_list , attrib
2775
2776 attrib:
2777 empty
2778 any-word
2779 any-word ( identifier )
2780 any-word ( identifier , nonempty-expr-list )
2781 any-word ( expr-list )
2782
2783 where the "identifier" must not be declared as a type, and
2784 "any-word" may be any identifier (including one declared as a
2785 type), a reserved word storage class specifier, type specifier or
2786 type qualifier. ??? This still leaves out most reserved keywords
2787 (following the old parser), shouldn't we include them, and why not
2788 allow identifiers declared as types to start the arguments? */
2789
2790static tree
2791c_parser_attributes (c_parser *parser)
2792{
2793 tree attrs = NULL_TREE;
2794 while (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2795 {
2796 /* ??? Follow the C++ parser rather than using the
46c2514e
TT
2797 lex_untranslated_string kludge. */
2798 parser->lex_untranslated_string = true;
27bf414c
JM
2799 c_parser_consume_token (parser);
2800 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2801 {
46c2514e 2802 parser->lex_untranslated_string = false;
27bf414c
JM
2803 return attrs;
2804 }
2805 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2806 {
46c2514e 2807 parser->lex_untranslated_string = false;
27bf414c
JM
2808 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
2809 return attrs;
2810 }
2811 /* Parse the attribute list. */
2812 while (c_parser_next_token_is (parser, CPP_COMMA)
2813 || c_parser_next_token_is (parser, CPP_NAME)
2814 || c_parser_next_token_is (parser, CPP_KEYWORD))
2815 {
2816 tree attr, attr_name, attr_args;
bbbbb16a 2817 VEC(tree,gc) *expr_list;
27bf414c
JM
2818 if (c_parser_next_token_is (parser, CPP_COMMA))
2819 {
2820 c_parser_consume_token (parser);
2821 continue;
2822 }
2823 if (c_parser_next_token_is (parser, CPP_KEYWORD))
2824 {
2825 /* ??? See comment above about what keywords are
2826 accepted here. */
2827 bool ok;
2828 switch (c_parser_peek_token (parser)->keyword)
2829 {
2830 case RID_STATIC:
2831 case RID_UNSIGNED:
2832 case RID_LONG:
2833 case RID_CONST:
2834 case RID_EXTERN:
2835 case RID_REGISTER:
2836 case RID_TYPEDEF:
2837 case RID_SHORT:
2838 case RID_INLINE:
2839 case RID_VOLATILE:
2840 case RID_SIGNED:
2841 case RID_AUTO:
2842 case RID_RESTRICT:
2843 case RID_COMPLEX:
2844 case RID_THREAD:
2845 case RID_INT:
2846 case RID_CHAR:
2847 case RID_FLOAT:
2848 case RID_DOUBLE:
2849 case RID_VOID:
9a8ce21f
JG
2850 case RID_DFLOAT32:
2851 case RID_DFLOAT64:
2852 case RID_DFLOAT128:
27bf414c 2853 case RID_BOOL:
ab22c1fa
CF
2854 case RID_FRACT:
2855 case RID_ACCUM:
2856 case RID_SAT:
27bf414c
JM
2857 ok = true;
2858 break;
2859 default:
2860 ok = false;
2861 break;
2862 }
2863 if (!ok)
2864 break;
5dc4a7f4
JJ
2865 /* Accept __attribute__((__const)) as __attribute__((const))
2866 etc. */
2867 attr_name
2868 = ridpointers[(int) c_parser_peek_token (parser)->keyword];
27bf414c 2869 }
5dc4a7f4
JJ
2870 else
2871 attr_name = c_parser_peek_token (parser)->value;
27bf414c
JM
2872 c_parser_consume_token (parser);
2873 if (c_parser_next_token_is_not (parser, CPP_OPEN_PAREN))
2874 {
2875 attr = build_tree_list (attr_name, NULL_TREE);
2876 attrs = chainon (attrs, attr);
2877 continue;
2878 }
2879 c_parser_consume_token (parser);
2880 /* Parse the attribute contents. If they start with an
2881 identifier which is followed by a comma or close
2882 parenthesis, then the arguments start with that
2883 identifier; otherwise they are an expression list. */
2884 if (c_parser_next_token_is (parser, CPP_NAME)
2885 && c_parser_peek_token (parser)->id_kind == C_ID_ID
2886 && ((c_parser_peek_2nd_token (parser)->type == CPP_COMMA)
2887 || (c_parser_peek_2nd_token (parser)->type
2888 == CPP_CLOSE_PAREN)))
2889 {
2890 tree arg1 = c_parser_peek_token (parser)->value;
2891 c_parser_consume_token (parser);
2892 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2893 attr_args = build_tree_list (NULL_TREE, arg1);
2894 else
2895 {
bbbbb16a 2896 tree tree_list;
27bf414c 2897 c_parser_consume_token (parser);
bbbbb16a 2898 expr_list = c_parser_expr_list (parser, false, true, NULL);
c166b898 2899 tree_list = build_tree_list_vec (expr_list);
bbbbb16a 2900 attr_args = tree_cons (NULL_TREE, arg1, tree_list);
c166b898 2901 release_tree_vector (expr_list);
27bf414c
JM
2902 }
2903 }
2904 else
2905 {
2906 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2907 attr_args = NULL_TREE;
2908 else
bbbbb16a
ILT
2909 {
2910 expr_list = c_parser_expr_list (parser, false, true, NULL);
c166b898
ILT
2911 attr_args = build_tree_list_vec (expr_list);
2912 release_tree_vector (expr_list);
bbbbb16a 2913 }
27bf414c
JM
2914 }
2915 attr = build_tree_list (attr_name, attr_args);
2916 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2917 c_parser_consume_token (parser);
2918 else
2919 {
46c2514e 2920 parser->lex_untranslated_string = false;
27bf414c
JM
2921 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2922 "expected %<)%>");
2923 return attrs;
2924 }
2925 attrs = chainon (attrs, attr);
2926 }
2927 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2928 c_parser_consume_token (parser);
2929 else
2930 {
46c2514e 2931 parser->lex_untranslated_string = false;
27bf414c
JM
2932 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2933 "expected %<)%>");
2934 return attrs;
2935 }
2936 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2937 c_parser_consume_token (parser);
2938 else
2939 {
46c2514e 2940 parser->lex_untranslated_string = false;
27bf414c
JM
2941 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2942 "expected %<)%>");
2943 return attrs;
2944 }
46c2514e 2945 parser->lex_untranslated_string = false;
27bf414c
JM
2946 }
2947 return attrs;
2948}
2949
2950/* Parse a type name (C90 6.5.5, C99 6.7.6).
2951
2952 type-name:
2953 specifier-qualifier-list abstract-declarator[opt]
2954*/
2955
2956static struct c_type_name *
2957c_parser_type_name (c_parser *parser)
2958{
2959 struct c_declspecs *specs = build_null_declspecs ();
2960 struct c_declarator *declarator;
2961 struct c_type_name *ret;
2962 bool dummy = false;
2963 c_parser_declspecs (parser, specs, false, true, true);
2964 if (!specs->declspecs_seen_p)
2965 {
2966 c_parser_error (parser, "expected specifier-qualifier-list");
2967 return NULL;
2968 }
2969 pending_xref_error ();
2970 finish_declspecs (specs);
2971 declarator = c_parser_declarator (parser, specs->type_seen_p,
2972 C_DTR_ABSTRACT, &dummy);
2973 if (declarator == NULL)
2974 return NULL;
2975 ret = XOBNEW (&parser_obstack, struct c_type_name);
2976 ret->specs = specs;
2977 ret->declarator = declarator;
2978 return ret;
2979}
2980
2981/* Parse an initializer (C90 6.5.7, C99 6.7.8).
2982
2983 initializer:
2984 assignment-expression
2985 { initializer-list }
2986 { initializer-list , }
2987
2988 initializer-list:
2989 designation[opt] initializer
2990 initializer-list , designation[opt] initializer
2991
2992 designation:
2993 designator-list =
2994
2995 designator-list:
2996 designator
2997 designator-list designator
2998
2999 designator:
3000 array-designator
3001 . identifier
3002
3003 array-designator:
3004 [ constant-expression ]
3005
3006 GNU extensions:
3007
3008 initializer:
3009 { }
3010
3011 designation:
3012 array-designator
3013 identifier :
3014
3015 array-designator:
3016 [ constant-expression ... constant-expression ]
3017
3018 Any expression without commas is accepted in the syntax for the
3019 constant-expressions, with non-constant expressions rejected later.
3020
3021 This function is only used for top-level initializers; for nested
3022 ones, see c_parser_initval. */
3023
3024static struct c_expr
3025c_parser_initializer (c_parser *parser)
3026{
3027 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
3028 return c_parser_braced_init (parser, NULL_TREE, false);
3029 else
46bdb9cf
JM
3030 {
3031 struct c_expr ret;
c2255bc4 3032 location_t loc = c_parser_peek_token (parser)->location;
46bdb9cf
JM
3033 ret = c_parser_expr_no_commas (parser, NULL);
3034 if (TREE_CODE (ret.value) != STRING_CST
3035 && TREE_CODE (ret.value) != COMPOUND_LITERAL_EXPR)
c2255bc4 3036 ret = default_function_array_conversion (loc, ret);
46bdb9cf
JM
3037 return ret;
3038 }
27bf414c
JM
3039}
3040
3041/* Parse a braced initializer list. TYPE is the type specified for a
3042 compound literal, and NULL_TREE for other initializers and for
3043 nested braced lists. NESTED_P is true for nested braced lists,
3044 false for the list of a compound literal or the list that is the
3045 top-level initializer in a declaration. */
3046
3047static struct c_expr
3048c_parser_braced_init (c_parser *parser, tree type, bool nested_p)
3049{
c7412148 3050 location_t brace_loc = c_parser_peek_token (parser)->location;
27bf414c
JM
3051 gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
3052 c_parser_consume_token (parser);
3053 if (nested_p)
3054 push_init_level (0);
3055 else
3056 really_start_incremental_init (type);
3057 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3058 {
509c9d60 3059 pedwarn (brace_loc, OPT_pedantic, "ISO C forbids empty initializer braces");
27bf414c
JM
3060 }
3061 else
3062 {
3063 /* Parse a non-empty initializer list, possibly with a trailing
3064 comma. */
3065 while (true)
3066 {
3067 c_parser_initelt (parser);
3068 if (parser->error)
3069 break;
3070 if (c_parser_next_token_is (parser, CPP_COMMA))
3071 c_parser_consume_token (parser);
3072 else
3073 break;
3074 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3075 break;
3076 }
3077 }
3078 if (c_parser_next_token_is_not (parser, CPP_CLOSE_BRACE))
3079 {
3080 struct c_expr ret;
3081 ret.value = error_mark_node;
3082 ret.original_code = ERROR_MARK;
6866c6e8 3083 ret.original_type = NULL;
27bf414c 3084 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, "expected %<}%>");
a47e20fd 3085 pop_init_level (0);
27bf414c
JM
3086 return ret;
3087 }
3088 c_parser_consume_token (parser);
3089 return pop_init_level (0);
3090}
3091
3092/* Parse a nested initializer, including designators. */
3093
3094static void
3095c_parser_initelt (c_parser *parser)
3096{
3097 /* Parse any designator or designator list. A single array
3098 designator may have the subsequent "=" omitted in GNU C, but a
3099 longer list or a structure member designator may not. */
3100 if (c_parser_next_token_is (parser, CPP_NAME)
3101 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
3102 {
3103 /* Old-style structure member designator. */
3104 set_init_label (c_parser_peek_token (parser)->value);
fcf73884 3105 /* Use the colon as the error location. */
509c9d60
MLI
3106 pedwarn (c_parser_peek_2nd_token (parser)->location, OPT_pedantic,
3107 "obsolete use of designated initializer with %<:%>");
27bf414c
JM
3108 c_parser_consume_token (parser);
3109 c_parser_consume_token (parser);
3110 }
3111 else
3112 {
3113 /* des_seen is 0 if there have been no designators, 1 if there
3114 has been a single array designator and 2 otherwise. */
3115 int des_seen = 0;
c7412148 3116 /* Location of a designator. */
922f2908 3117 location_t des_loc = UNKNOWN_LOCATION; /* Quiet warning. */
27bf414c
JM
3118 while (c_parser_next_token_is (parser, CPP_OPEN_SQUARE)
3119 || c_parser_next_token_is (parser, CPP_DOT))
3120 {
3121 int des_prev = des_seen;
c7412148
TT
3122 if (!des_seen)
3123 des_loc = c_parser_peek_token (parser)->location;
27bf414c
JM
3124 if (des_seen < 2)
3125 des_seen++;
3126 if (c_parser_next_token_is (parser, CPP_DOT))
3127 {
3128 des_seen = 2;
3129 c_parser_consume_token (parser);
3130 if (c_parser_next_token_is (parser, CPP_NAME))
3131 {
3132 set_init_label (c_parser_peek_token (parser)->value);
3133 c_parser_consume_token (parser);
3134 }
3135 else
3136 {
3137 struct c_expr init;
3138 init.value = error_mark_node;
3139 init.original_code = ERROR_MARK;
6866c6e8 3140 init.original_type = NULL;
27bf414c
JM
3141 c_parser_error (parser, "expected identifier");
3142 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
b295aee2 3143 process_init_element (init, false);
27bf414c
JM
3144 return;
3145 }
3146 }
3147 else
3148 {
3149 tree first, second;
922f2908 3150 location_t ellipsis_loc = UNKNOWN_LOCATION; /* Quiet warning. */
27bf414c
JM
3151 /* ??? Following the old parser, [ objc-receiver
3152 objc-message-args ] is accepted as an initializer,
3153 being distinguished from a designator by what follows
3154 the first assignment expression inside the square
3155 brackets, but after a first array designator a
3156 subsequent square bracket is for Objective-C taken to
3157 start an expression, using the obsolete form of
3158 designated initializer without '=', rather than
3159 possibly being a second level of designation: in LALR
3160 terms, the '[' is shifted rather than reducing
3161 designator to designator-list. */
3162 if (des_prev == 1 && c_dialect_objc ())
3163 {
3164 des_seen = des_prev;
3165 break;
3166 }
3167 if (des_prev == 0 && c_dialect_objc ())
3168 {
3169 /* This might be an array designator or an
3170 Objective-C message expression. If the former,
3171 continue parsing here; if the latter, parse the
3172 remainder of the initializer given the starting
3173 primary-expression. ??? It might make sense to
3174 distinguish when des_prev == 1 as well; see
3175 previous comment. */
3176 tree rec, args;
3177 struct c_expr mexpr;
3178 c_parser_consume_token (parser);
3179 if (c_parser_peek_token (parser)->type == CPP_NAME
3180 && ((c_parser_peek_token (parser)->id_kind
3181 == C_ID_TYPENAME)
3182 || (c_parser_peek_token (parser)->id_kind
3183 == C_ID_CLASSNAME)))
3184 {
3185 /* Type name receiver. */
3186 tree id = c_parser_peek_token (parser)->value;
3187 c_parser_consume_token (parser);
3188 rec = objc_get_class_reference (id);
3189 goto parse_message_args;
3190 }
3191 first = c_parser_expr_no_commas (parser, NULL).value;
3192 if (c_parser_next_token_is (parser, CPP_ELLIPSIS)
3193 || c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3194 goto array_desig_after_first;
3195 /* Expression receiver. So far only one part
3196 without commas has been parsed; there might be
3197 more of the expression. */
3198 rec = first;
3199 while (c_parser_next_token_is (parser, CPP_COMMA))
3200 {
f2a71bbc 3201 struct c_expr next;
c2255bc4
AH
3202 location_t comma_loc, exp_loc;
3203 comma_loc = c_parser_peek_token (parser)->location;
27bf414c 3204 c_parser_consume_token (parser);
c2255bc4 3205 exp_loc = c_parser_peek_token (parser)->location;
f2a71bbc 3206 next = c_parser_expr_no_commas (parser, NULL);
c2255bc4
AH
3207 next = default_function_array_conversion (exp_loc, next);
3208 rec = build_compound_expr (comma_loc, rec, next.value);
27bf414c
JM
3209 }
3210 parse_message_args:
3211 /* Now parse the objc-message-args. */
3212 args = c_parser_objc_message_args (parser);
3213 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
3214 "expected %<]%>");
3215 mexpr.value
3216 = objc_build_message_expr (build_tree_list (rec, args));
3217 mexpr.original_code = ERROR_MARK;
6866c6e8 3218 mexpr.original_type = NULL;
27bf414c
JM
3219 /* Now parse and process the remainder of the
3220 initializer, starting with this message
3221 expression as a primary-expression. */
3222 c_parser_initval (parser, &mexpr);
3223 return;
3224 }
3225 c_parser_consume_token (parser);
3226 first = c_parser_expr_no_commas (parser, NULL).value;
3227 array_desig_after_first:
3228 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3229 {
c7412148 3230 ellipsis_loc = c_parser_peek_token (parser)->location;
27bf414c
JM
3231 c_parser_consume_token (parser);
3232 second = c_parser_expr_no_commas (parser, NULL).value;
3233 }
3234 else
3235 second = NULL_TREE;
3236 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3237 {
3238 c_parser_consume_token (parser);
3239 set_init_index (first, second);
fcf73884 3240 if (second)
509c9d60
MLI
3241 pedwarn (ellipsis_loc, OPT_pedantic,
3242 "ISO C forbids specifying range of elements to initialize");
27bf414c
JM
3243 }
3244 else
3245 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
3246 "expected %<]%>");
3247 }
3248 }
3249 if (des_seen >= 1)
3250 {
3251 if (c_parser_next_token_is (parser, CPP_EQ))
3252 {
fcf73884 3253 if (!flag_isoc99)
509c9d60
MLI
3254 pedwarn (des_loc, OPT_pedantic,
3255 "ISO C90 forbids specifying subobject to initialize");
27bf414c
JM
3256 c_parser_consume_token (parser);
3257 }
3258 else
3259 {
3260 if (des_seen == 1)
509c9d60
MLI
3261 pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic,
3262 "obsolete use of designated initializer without %<=%>");
27bf414c
JM
3263 else
3264 {
3265 struct c_expr init;
3266 init.value = error_mark_node;
3267 init.original_code = ERROR_MARK;
6866c6e8 3268 init.original_type = NULL;
27bf414c
JM
3269 c_parser_error (parser, "expected %<=%>");
3270 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
b295aee2 3271 process_init_element (init, false);
27bf414c
JM
3272 return;
3273 }
3274 }
3275 }
3276 }
3277 c_parser_initval (parser, NULL);
3278}
3279
3280/* Parse a nested initializer; as c_parser_initializer but parses
3281 initializers within braced lists, after any designators have been
3282 applied. If AFTER is not NULL then it is an Objective-C message
3283 expression which is the primary-expression starting the
3284 initializer. */
3285
3286static void
3287c_parser_initval (c_parser *parser, struct c_expr *after)
3288{
3289 struct c_expr init;
3290 gcc_assert (!after || c_dialect_objc ());
3291 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE) && !after)
3292 init = c_parser_braced_init (parser, NULL_TREE, true);
3293 else
46bdb9cf 3294 {
c2255bc4 3295 location_t loc = c_parser_peek_token (parser)->location;
46bdb9cf
JM
3296 init = c_parser_expr_no_commas (parser, after);
3297 if (init.value != NULL_TREE
3298 && TREE_CODE (init.value) != STRING_CST
3299 && TREE_CODE (init.value) != COMPOUND_LITERAL_EXPR)
c2255bc4 3300 init = default_function_array_conversion (loc, init);
46bdb9cf 3301 }
b295aee2 3302 process_init_element (init, false);
27bf414c
JM
3303}
3304
3305/* Parse a compound statement (possibly a function body) (C90 6.6.2,
3306 C99 6.8.2).
3307
3308 compound-statement:
3309 { block-item-list[opt] }
3310 { label-declarations block-item-list }
3311
3312 block-item-list:
3313 block-item
3314 block-item-list block-item
3315
3316 block-item:
3317 nested-declaration
3318 statement
3319
3320 nested-declaration:
3321 declaration
3322
3323 GNU extensions:
3324
3325 compound-statement:
3326 { label-declarations block-item-list }
3327
3328 nested-declaration:
3329 __extension__ nested-declaration
3330 nested-function-definition
3331
3332 label-declarations:
3333 label-declaration
3334 label-declarations label-declaration
3335
3336 label-declaration:
3337 __label__ identifier-list ;
3338
3339 Allowing the mixing of declarations and code is new in C99. The
3340 GNU syntax also permits (not shown above) labels at the end of
3341 compound statements, which yield an error. We don't allow labels
3342 on declarations; this might seem like a natural extension, but
3343 there would be a conflict between attributes on the label and
3344 prefix attributes on the declaration. ??? The syntax follows the
3345 old parser in requiring something after label declarations.
3346 Although they are erroneous if the labels declared aren't defined,
953ff289
DN
3347 is it useful for the syntax to be this way?
3348
3349 OpenMP:
3350
3351 block-item:
3352 openmp-directive
3353
3354 openmp-directive:
3355 barrier-directive
3356 flush-directive */
27bf414c
JM
3357
3358static tree
3359c_parser_compound_statement (c_parser *parser)
3360{
3361 tree stmt;
c2255bc4
AH
3362 location_t brace_loc;
3363 brace_loc = c_parser_peek_token (parser)->location;
27bf414c 3364 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
5600f233
JM
3365 {
3366 /* Ensure a scope is entered and left anyway to avoid confusion
3367 if we have just prepared to enter a function body. */
3368 stmt = c_begin_compound_stmt (true);
c2255bc4 3369 c_end_compound_stmt (brace_loc, stmt, true);
5600f233
JM
3370 return error_mark_node;
3371 }
27bf414c
JM
3372 stmt = c_begin_compound_stmt (true);
3373 c_parser_compound_statement_nostart (parser);
c2255bc4 3374 return c_end_compound_stmt (brace_loc, stmt, true);
27bf414c
JM
3375}
3376
3377/* Parse a compound statement except for the opening brace. This is
3378 used for parsing both compound statements and statement expressions
3379 (which follow different paths to handling the opening). */
3380
3381static void
3382c_parser_compound_statement_nostart (c_parser *parser)
3383{
3384 bool last_stmt = false;
3385 bool last_label = false;
6ec637a4 3386 bool save_valid_for_pragma = valid_location_for_stdc_pragma_p ();
3ba09659 3387 location_t label_loc = UNKNOWN_LOCATION; /* Quiet warning. */
27bf414c
JM
3388 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3389 {
3390 c_parser_consume_token (parser);
3391 return;
3392 }
6ec637a4 3393 mark_valid_location_for_stdc_pragma (true);
27bf414c
JM
3394 if (c_parser_next_token_is_keyword (parser, RID_LABEL))
3395 {
3396 /* Read zero or more forward-declarations for labels that nested
3397 functions can jump to. */
6ec637a4 3398 mark_valid_location_for_stdc_pragma (false);
27bf414c
JM
3399 while (c_parser_next_token_is_keyword (parser, RID_LABEL))
3400 {
c2255bc4 3401 label_loc = c_parser_peek_token (parser)->location;
27bf414c
JM
3402 c_parser_consume_token (parser);
3403 /* Any identifiers, including those declared as type names,
3404 are OK here. */
3405 while (true)
3406 {
3407 tree label;
3408 if (c_parser_next_token_is_not (parser, CPP_NAME))
3409 {
3410 c_parser_error (parser, "expected identifier");
3411 break;
3412 }
3413 label
3414 = declare_label (c_parser_peek_token (parser)->value);
3415 C_DECLARED_LABEL_FLAG (label) = 1;
c2255bc4 3416 add_stmt (build_stmt (label_loc, DECL_EXPR, label));
27bf414c
JM
3417 c_parser_consume_token (parser);
3418 if (c_parser_next_token_is (parser, CPP_COMMA))
3419 c_parser_consume_token (parser);
3420 else
3421 break;
3422 }
3423 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
3424 }
c2255bc4 3425 pedwarn (label_loc, OPT_pedantic, "ISO C forbids label declarations");
27bf414c
JM
3426 }
3427 /* We must now have at least one statement, label or declaration. */
3428 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3429 {
6ec637a4 3430 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
27bf414c
JM
3431 c_parser_error (parser, "expected declaration or statement");
3432 c_parser_consume_token (parser);
3433 return;
3434 }
3435 while (c_parser_next_token_is_not (parser, CPP_CLOSE_BRACE))
3436 {
3437 location_t loc = c_parser_peek_token (parser)->location;
27bf414c
JM
3438 if (c_parser_next_token_is_keyword (parser, RID_CASE)
3439 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
3440 || (c_parser_next_token_is (parser, CPP_NAME)
3441 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
3442 {
c7412148
TT
3443 if (c_parser_next_token_is_keyword (parser, RID_CASE))
3444 label_loc = c_parser_peek_2nd_token (parser)->location;
3445 else
3446 label_loc = c_parser_peek_token (parser)->location;
27bf414c
JM
3447 last_label = true;
3448 last_stmt = false;
6ec637a4 3449 mark_valid_location_for_stdc_pragma (false);
27bf414c
JM
3450 c_parser_label (parser);
3451 }
3452 else if (!last_label
3453 && c_parser_next_token_starts_declspecs (parser))
3454 {
3455 last_label = false;
6ec637a4 3456 mark_valid_location_for_stdc_pragma (false);
27bf414c 3457 c_parser_declaration_or_fndef (parser, true, true, true, true);
fcf73884 3458 if (last_stmt)
509c9d60
MLI
3459 pedwarn_c90 (loc,
3460 (pedantic && !flag_isoc99)
fcf73884
MLI
3461 ? OPT_pedantic
3462 : OPT_Wdeclaration_after_statement,
509c9d60 3463 "ISO C90 forbids mixed declarations and code");
27bf414c
JM
3464 last_stmt = false;
3465 }
3466 else if (!last_label
3467 && c_parser_next_token_is_keyword (parser, RID_EXTENSION))
3468 {
3469 /* __extension__ can start a declaration, but is also an
3470 unary operator that can start an expression. Consume all
3471 but the last of a possible series of __extension__ to
3472 determine which. */
3473 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
3474 && (c_parser_peek_2nd_token (parser)->keyword
3475 == RID_EXTENSION))
3476 c_parser_consume_token (parser);
3477 if (c_token_starts_declspecs (c_parser_peek_2nd_token (parser)))
3478 {
3479 int ext;
3480 ext = disable_extension_diagnostics ();
3481 c_parser_consume_token (parser);
3482 last_label = false;
6ec637a4 3483 mark_valid_location_for_stdc_pragma (false);
27bf414c
JM
3484 c_parser_declaration_or_fndef (parser, true, true, true, true);
3485 /* Following the old parser, __extension__ does not
3486 disable this diagnostic. */
3487 restore_extension_diagnostics (ext);
fcf73884 3488 if (last_stmt)
509c9d60 3489 pedwarn_c90 (loc, (pedantic && !flag_isoc99)
fcf73884
MLI
3490 ? OPT_pedantic
3491 : OPT_Wdeclaration_after_statement,
509c9d60 3492 "ISO C90 forbids mixed declarations and code");
27bf414c
JM
3493 last_stmt = false;
3494 }
3495 else
3496 goto statement;
3497 }
bc4071dd
RH
3498 else if (c_parser_next_token_is (parser, CPP_PRAGMA))
3499 {
3500 /* External pragmas, and some omp pragmas, are not associated
3501 with regular c code, and so are not to be considered statements
3502 syntactically. This ensures that the user doesn't put them
3503 places that would turn into syntax errors if the directive
3504 were ignored. */
3505 if (c_parser_pragma (parser, pragma_compound))
3506 last_label = false, last_stmt = true;
3507 }
3508 else if (c_parser_next_token_is (parser, CPP_EOF))
3509 {
6ec637a4 3510 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
bc4071dd
RH
3511 c_parser_error (parser, "expected declaration or statement");
3512 return;
3513 }
b4b56033
MLI
3514 else if (c_parser_next_token_is_keyword (parser, RID_ELSE))
3515 {
3516 if (parser->in_if_block)
3517 {
6ec637a4 3518 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
3ba09659 3519 error_at (loc, """expected %<}%> before %<else%>");
b4b56033
MLI
3520 return;
3521 }
3522 else
3523 {
3ba09659 3524 error_at (loc, "%<else%> without a previous %<if%>");
b4b56033
MLI
3525 c_parser_consume_token (parser);
3526 continue;
3527 }
3528 }
27bf414c
JM
3529 else
3530 {
3531 statement:
3532 last_label = false;
3533 last_stmt = true;
6ec637a4 3534 mark_valid_location_for_stdc_pragma (false);
27bf414c
JM
3535 c_parser_statement_after_labels (parser);
3536 }
2c14ae9a
VR
3537
3538 parser->error = false;
27bf414c
JM
3539 }
3540 if (last_label)
3ba09659 3541 error_at (label_loc, "label at end of compound statement");
27bf414c 3542 c_parser_consume_token (parser);
6ec637a4
JJ
3543 /* Restore the value we started with. */
3544 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
27bf414c
JM
3545}
3546
3547/* Parse a label (C90 6.6.1, C99 6.8.1).
3548
3549 label:
3550 identifier : attributes[opt]
3551 case constant-expression :
3552 default :
3553
3554 GNU extensions:
3555
3556 label:
3557 case constant-expression ... constant-expression :
3558
3559 The use of attributes on labels is a GNU extension. The syntax in
3560 GNU C accepts any expressions without commas, non-constant
3561 expressions being rejected later. */
3562
3563static void
3564c_parser_label (c_parser *parser)
3565{
3566 location_t loc1 = c_parser_peek_token (parser)->location;
3567 tree label = NULL_TREE;
3568 if (c_parser_next_token_is_keyword (parser, RID_CASE))
3569 {
3570 tree exp1, exp2;
3571 c_parser_consume_token (parser);
3572 exp1 = c_parser_expr_no_commas (parser, NULL).value;
3573 if (c_parser_next_token_is (parser, CPP_COLON))
3574 {
3575 c_parser_consume_token (parser);
c2255bc4 3576 label = do_case (loc1, exp1, NULL_TREE);
27bf414c
JM
3577 }
3578 else if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3579 {
3580 c_parser_consume_token (parser);
3581 exp2 = c_parser_expr_no_commas (parser, NULL).value;
3582 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
c2255bc4 3583 label = do_case (loc1, exp1, exp2);
27bf414c
JM
3584 }
3585 else
3586 c_parser_error (parser, "expected %<:%> or %<...%>");
3587 }
3588 else if (c_parser_next_token_is_keyword (parser, RID_DEFAULT))
3589 {
3590 c_parser_consume_token (parser);
3591 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
c2255bc4 3592 label = do_case (loc1, NULL_TREE, NULL_TREE);
27bf414c
JM
3593 }
3594 else
3595 {
3596 tree name = c_parser_peek_token (parser)->value;
3597 tree tlab;
27bf414c 3598 tree attrs;
c7412148 3599 location_t loc2 = c_parser_peek_token (parser)->location;
27bf414c
JM
3600 gcc_assert (c_parser_next_token_is (parser, CPP_NAME));
3601 c_parser_consume_token (parser);
3602 gcc_assert (c_parser_next_token_is (parser, CPP_COLON));
27bf414c
JM
3603 c_parser_consume_token (parser);
3604 attrs = c_parser_attributes (parser);
3605 tlab = define_label (loc2, name);
3606 if (tlab)
3607 {
3608 decl_attributes (&tlab, attrs, 0);
c2255bc4 3609 label = add_stmt (build_stmt (loc1, LABEL_EXPR, tlab));
27bf414c
JM
3610 }
3611 }
3612 if (label)
3d57f0f0 3613 {
3d57f0f0
MLI
3614 if (c_parser_next_token_starts_declspecs (parser)
3615 && !(c_parser_next_token_is (parser, CPP_NAME)
3616 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
3617 {
3ba09659
AH
3618 error_at (c_parser_peek_token (parser)->location,
3619 "a label can only be part of a statement and "
3620 "a declaration is not a statement");
3d57f0f0
MLI
3621 c_parser_declaration_or_fndef (parser, /*fndef_ok*/ false,
3622 /*nested*/ true, /*empty_ok*/ false,
3623 /*start_attr_ok*/ true);
3624 }
3625 }
27bf414c
JM
3626}
3627
3628/* Parse a statement (C90 6.6, C99 6.8).
3629
3630 statement:
3631 labeled-statement
3632 compound-statement
3633 expression-statement
3634 selection-statement
3635 iteration-statement
3636 jump-statement
3637
3638 labeled-statement:
3639 label statement
3640
3641 expression-statement:
3642 expression[opt] ;
3643
3644 selection-statement:
3645 if-statement
3646 switch-statement
3647
3648 iteration-statement:
3649 while-statement
3650 do-statement
3651 for-statement
3652
3653 jump-statement:
3654 goto identifier ;
3655 continue ;
3656 break ;
3657 return expression[opt] ;
3658
3659 GNU extensions:
3660
3661 statement:
3662 asm-statement
3663
3664 jump-statement:
3665 goto * expression ;
3666
3667 Objective-C:
3668
3669 statement:
3670 objc-throw-statement
3671 objc-try-catch-statement
3672 objc-synchronized-statement
3673
3674 objc-throw-statement:
3675 @throw expression ;
3676 @throw ;
953ff289
DN
3677
3678 OpenMP:
3679
3680 statement:
3681 openmp-construct
3682
3683 openmp-construct:
3684 parallel-construct
3685 for-construct
3686 sections-construct
3687 single-construct
3688 parallel-for-construct
3689 parallel-sections-construct
3690 master-construct
3691 critical-construct
3692 atomic-construct
3693 ordered-construct
3694
3695 parallel-construct:
3696 parallel-directive structured-block
3697
3698 for-construct:
3699 for-directive iteration-statement
3700
3701 sections-construct:
3702 sections-directive section-scope
3703
3704 single-construct:
3705 single-directive structured-block
3706
3707 parallel-for-construct:
3708 parallel-for-directive iteration-statement
3709
3710 parallel-sections-construct:
3711 parallel-sections-directive section-scope
3712
3713 master-construct:
3714 master-directive structured-block
3715
3716 critical-construct:
3717 critical-directive structured-block
3718
3719 atomic-construct:
3720 atomic-directive expression-statement
3721
3722 ordered-construct:
3723 ordered-directive structured-block */
27bf414c
JM
3724
3725static void
3726c_parser_statement (c_parser *parser)
3727{
3728 while (c_parser_next_token_is_keyword (parser, RID_CASE)
3729 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
3730 || (c_parser_next_token_is (parser, CPP_NAME)
3731 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
3732 c_parser_label (parser);
3733 c_parser_statement_after_labels (parser);
3734}
3735
3736/* Parse a statement, other than a labeled statement. */
3737
3738static void
3739c_parser_statement_after_labels (c_parser *parser)
3740{
3741 location_t loc = c_parser_peek_token (parser)->location;
3742 tree stmt = NULL_TREE;
b4b56033
MLI
3743 bool in_if_block = parser->in_if_block;
3744 parser->in_if_block = false;
27bf414c
JM
3745 switch (c_parser_peek_token (parser)->type)
3746 {
3747 case CPP_OPEN_BRACE:
3748 add_stmt (c_parser_compound_statement (parser));
3749 break;
3750 case CPP_KEYWORD:
3751 switch (c_parser_peek_token (parser)->keyword)
3752 {
3753 case RID_IF:
3754 c_parser_if_statement (parser);
3755 break;
3756 case RID_SWITCH:
3757 c_parser_switch_statement (parser);
3758 break;
3759 case RID_WHILE:
3760 c_parser_while_statement (parser);
3761 break;
3762 case RID_DO:
3763 c_parser_do_statement (parser);
3764 break;
3765 case RID_FOR:
3766 c_parser_for_statement (parser);
3767 break;
3768 case RID_GOTO:
3769 c_parser_consume_token (parser);
3770 if (c_parser_next_token_is (parser, CPP_NAME))
3771 {
c2255bc4
AH
3772 stmt = c_finish_goto_label (loc,
3773 c_parser_peek_token (parser)->value);
27bf414c
JM
3774 c_parser_consume_token (parser);
3775 }
3776 else if (c_parser_next_token_is (parser, CPP_MULT))
3777 {
3778 c_parser_consume_token (parser);
c2255bc4
AH
3779 stmt = c_finish_goto_ptr (loc,
3780 c_parser_expression (parser).value);
27bf414c
JM
3781 }
3782 else
3783 c_parser_error (parser, "expected identifier or %<*%>");
3784 goto expect_semicolon;
3785 case RID_CONTINUE:
3786 c_parser_consume_token (parser);
c2255bc4 3787 stmt = c_finish_bc_stmt (loc, &c_cont_label, false);
27bf414c
JM
3788 goto expect_semicolon;
3789 case RID_BREAK:
3790 c_parser_consume_token (parser);
c2255bc4 3791 stmt = c_finish_bc_stmt (loc, &c_break_label, true);
27bf414c
JM
3792 goto expect_semicolon;
3793 case RID_RETURN:
3794 c_parser_consume_token (parser);
3795 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
3796 {
c2255bc4 3797 stmt = c_finish_return (loc, NULL_TREE, NULL_TREE);
27bf414c
JM
3798 c_parser_consume_token (parser);
3799 }
3800 else
3801 {
bbbbb16a 3802 struct c_expr expr = c_parser_expression_conv (parser);
c2255bc4 3803 stmt = c_finish_return (loc, expr.value, expr.original_type);
27bf414c
JM
3804 goto expect_semicolon;
3805 }
3806 break;
3807 case RID_ASM:
3808 stmt = c_parser_asm_statement (parser);
3809 break;
eea1139b 3810 case RID_THROW:
27bf414c
JM
3811 gcc_assert (c_dialect_objc ());
3812 c_parser_consume_token (parser);
3813 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
3814 {
c2255bc4 3815 stmt = objc_build_throw_stmt (loc, NULL_TREE);
27bf414c
JM
3816 c_parser_consume_token (parser);
3817 }
3818 else
3819 {
928c19bb
JM
3820 tree expr = c_parser_expression (parser).value;
3821 expr = c_fully_fold (expr, false, NULL);
c2255bc4 3822 stmt = objc_build_throw_stmt (loc, expr);
27bf414c
JM
3823 goto expect_semicolon;
3824 }
3825 break;
eea1139b 3826 case RID_TRY:
27bf414c
JM
3827 gcc_assert (c_dialect_objc ());
3828 c_parser_objc_try_catch_statement (parser);
3829 break;
3830 case RID_AT_SYNCHRONIZED:
3831 gcc_assert (c_dialect_objc ());
3832 c_parser_objc_synchronized_statement (parser);
3833 break;
3834 default:
3835 goto expr_stmt;
3836 }
3837 break;
3838 case CPP_SEMICOLON:
3839 c_parser_consume_token (parser);
3840 break;
3841 case CPP_CLOSE_PAREN:
3842 case CPP_CLOSE_SQUARE:
3843 /* Avoid infinite loop in error recovery:
3844 c_parser_skip_until_found stops at a closing nesting
3845 delimiter without consuming it, but here we need to consume
3846 it to proceed further. */
3847 c_parser_error (parser, "expected statement");
3848 c_parser_consume_token (parser);
3849 break;
bc4071dd
RH
3850 case CPP_PRAGMA:
3851 c_parser_pragma (parser, pragma_stmt);
3852 break;
27bf414c
JM
3853 default:
3854 expr_stmt:
c2255bc4 3855 stmt = c_finish_expr_stmt (loc, c_parser_expression_conv (parser).value);
27bf414c
JM
3856 expect_semicolon:
3857 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
3858 break;
3859 }
3860 /* Two cases cannot and do not have line numbers associated: If stmt
3861 is degenerate, such as "2;", then stmt is an INTEGER_CST, which
3862 cannot hold line numbers. But that's OK because the statement
3863 will either be changed to a MODIFY_EXPR during gimplification of
3864 the statement expr, or discarded. If stmt was compound, but
3865 without new variables, we will have skipped the creation of a
3866 BIND and will have a bare STATEMENT_LIST. But that's OK because
3867 (recursively) all of the component statements should already have
3868 line numbers assigned. ??? Can we discard no-op statements
3869 earlier? */
c2255bc4
AH
3870 if (CAN_HAVE_LOCATION_P (stmt)
3871 && EXPR_LOCATION (stmt) == UNKNOWN_LOCATION)
3872 SET_EXPR_LOCATION (stmt, loc);
b4b56033
MLI
3873
3874 parser->in_if_block = in_if_block;
27bf414c
JM
3875}
3876
ca085fd7
MLI
3877/* Parse the condition from an if, do, while or for statements. */
3878
3879static tree
3880c_parser_condition (c_parser *parser)
3881{
c2255bc4 3882 location_t loc = c_parser_peek_token (parser)->location;
ca085fd7 3883 tree cond;
928c19bb
JM
3884 cond = c_parser_expression_conv (parser).value;
3885 cond = c_objc_common_truthvalue_conversion (loc, cond);
3886 cond = c_fully_fold (cond, false, NULL);
ca085fd7
MLI
3887 if (warn_sequence_point)
3888 verify_sequence_points (cond);
3889 return cond;
3890}
3891
27bf414c
JM
3892/* Parse a parenthesized condition from an if, do or while statement.
3893
3894 condition:
3895 ( expression )
3896*/
3897static tree
3898c_parser_paren_condition (c_parser *parser)
3899{
27bf414c
JM
3900 tree cond;
3901 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3902 return error_mark_node;
ca085fd7 3903 cond = c_parser_condition (parser);
27bf414c
JM
3904 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
3905 return cond;
3906}
3907
3908/* Parse a statement which is a block in C99. */
3909
3910static tree
3911c_parser_c99_block_statement (c_parser *parser)
3912{
3913 tree block = c_begin_compound_stmt (flag_isoc99);
c2255bc4 3914 location_t loc = c_parser_peek_token (parser)->location;
27bf414c 3915 c_parser_statement (parser);
c2255bc4 3916 return c_end_compound_stmt (loc, block, flag_isoc99);
27bf414c
JM
3917}
3918
b4b56033
MLI
3919/* Parse the body of an if statement. This is just parsing a
3920 statement but (a) it is a block in C99, (b) we track whether the
3921 body is an if statement for the sake of -Wparentheses warnings, (c)
3922 we handle an empty body specially for the sake of -Wempty-body
3923 warnings, and (d) we call parser_compound_statement directly
3924 because c_parser_statement_after_labels resets
3925 parser->in_if_block. */
27bf414c
JM
3926
3927static tree
3928c_parser_if_body (c_parser *parser, bool *if_p)
3929{
3930 tree block = c_begin_compound_stmt (flag_isoc99);
c2255bc4 3931 location_t body_loc = c_parser_peek_token (parser)->location;
27bf414c
JM
3932 while (c_parser_next_token_is_keyword (parser, RID_CASE)
3933 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
3934 || (c_parser_next_token_is (parser, CPP_NAME)
3935 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
3936 c_parser_label (parser);
3937 *if_p = c_parser_next_token_is_keyword (parser, RID_IF);
62e00e94 3938 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
b4b56033 3939 {
626c34b5 3940 location_t loc = c_parser_peek_token (parser)->location;
c2255bc4 3941 add_stmt (build_empty_stmt (loc));
b4b56033 3942 c_parser_consume_token (parser);
626c34b5
PB
3943 if (!c_parser_next_token_is_keyword (parser, RID_ELSE))
3944 warning_at (loc, OPT_Wempty_body,
3945 "suggest braces around empty body in an %<if%> statement");
b4b56033
MLI
3946 }
3947 else if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
3948 add_stmt (c_parser_compound_statement (parser));
3949 else
3950 c_parser_statement_after_labels (parser);
c2255bc4 3951 return c_end_compound_stmt (body_loc, block, flag_isoc99);
b4b56033
MLI
3952}
3953
3954/* Parse the else body of an if statement. This is just parsing a
3955 statement but (a) it is a block in C99, (b) we handle an empty body
3956 specially for the sake of -Wempty-body warnings. */
3957
3958static tree
3959c_parser_else_body (c_parser *parser)
3960{
c2255bc4 3961 location_t else_loc = c_parser_peek_token (parser)->location;
b4b56033
MLI
3962 tree block = c_begin_compound_stmt (flag_isoc99);
3963 while (c_parser_next_token_is_keyword (parser, RID_CASE)
3964 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
3965 || (c_parser_next_token_is (parser, CPP_NAME)
3966 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
3967 c_parser_label (parser);
3968 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
3969 {
c2255bc4
AH
3970 location_t loc = c_parser_peek_token (parser)->location;
3971 warning_at (loc,
626c34b5
PB
3972 OPT_Wempty_body,
3973 "suggest braces around empty body in an %<else%> statement");
c2255bc4 3974 add_stmt (build_empty_stmt (loc));
b4b56033
MLI
3975 c_parser_consume_token (parser);
3976 }
3977 else
3978 c_parser_statement_after_labels (parser);
c2255bc4 3979 return c_end_compound_stmt (else_loc, block, flag_isoc99);
27bf414c
JM
3980}
3981
3982/* Parse an if statement (C90 6.6.4, C99 6.8.4).
3983
3984 if-statement:
3985 if ( expression ) statement
3986 if ( expression ) statement else statement
3987*/
3988
3989static void
3990c_parser_if_statement (c_parser *parser)
3991{
3992 tree block;
3993 location_t loc;
3994 tree cond;
b4b56033 3995 bool first_if = false;
27bf414c 3996 tree first_body, second_body;
b4b56033
MLI
3997 bool in_if_block;
3998
27bf414c
JM
3999 gcc_assert (c_parser_next_token_is_keyword (parser, RID_IF));
4000 c_parser_consume_token (parser);
4001 block = c_begin_compound_stmt (flag_isoc99);
4002 loc = c_parser_peek_token (parser)->location;
4003 cond = c_parser_paren_condition (parser);
b4b56033
MLI
4004 in_if_block = parser->in_if_block;
4005 parser->in_if_block = true;
27bf414c 4006 first_body = c_parser_if_body (parser, &first_if);
b4b56033 4007 parser->in_if_block = in_if_block;
27bf414c
JM
4008 if (c_parser_next_token_is_keyword (parser, RID_ELSE))
4009 {
4010 c_parser_consume_token (parser);
b4b56033 4011 second_body = c_parser_else_body (parser);
27bf414c
JM
4012 }
4013 else
4014 second_body = NULL_TREE;
4015 c_finish_if_stmt (loc, cond, first_body, second_body, first_if);
c2255bc4 4016 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
27bf414c
JM
4017}
4018
4019/* Parse a switch statement (C90 6.6.4, C99 6.8.4).
4020
4021 switch-statement:
4022 switch (expression) statement
4023*/
4024
4025static void
4026c_parser_switch_statement (c_parser *parser)
4027{
4028 tree block, expr, body, save_break;
c2255bc4
AH
4029 location_t switch_loc = c_parser_peek_token (parser)->location;
4030 location_t switch_cond_loc;
27bf414c
JM
4031 gcc_assert (c_parser_next_token_is_keyword (parser, RID_SWITCH));
4032 c_parser_consume_token (parser);
4033 block = c_begin_compound_stmt (flag_isoc99);
4034 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4035 {
c2255bc4 4036 switch_cond_loc = c_parser_peek_token (parser)->location;
27bf414c
JM
4037 expr = c_parser_expression (parser).value;
4038 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
4039 }
4040 else
c2255bc4
AH
4041 {
4042 switch_cond_loc = UNKNOWN_LOCATION;
4043 expr = error_mark_node;
4044 }
4045 c_start_case (switch_loc, switch_cond_loc, expr);
27bf414c
JM
4046 save_break = c_break_label;
4047 c_break_label = NULL_TREE;
4048 body = c_parser_c99_block_statement (parser);
4049 c_finish_case (body);
4050 if (c_break_label)
c2255bc4
AH
4051 {
4052 location_t here = c_parser_peek_token (parser)->location;
4053 tree t = build1 (LABEL_EXPR, void_type_node, c_break_label);
4054 SET_EXPR_LOCATION (t, here);
4055 add_stmt (t);
4056 }
27bf414c 4057 c_break_label = save_break;
c2255bc4 4058 add_stmt (c_end_compound_stmt (switch_loc, block, flag_isoc99));
27bf414c
JM
4059}
4060
4061/* Parse a while statement (C90 6.6.5, C99 6.8.5).
4062
4063 while-statement:
4064 while (expression) statement
4065*/
4066
4067static void
4068c_parser_while_statement (c_parser *parser)
4069{
4070 tree block, cond, body, save_break, save_cont;
4071 location_t loc;
4072 gcc_assert (c_parser_next_token_is_keyword (parser, RID_WHILE));
4073 c_parser_consume_token (parser);
4074 block = c_begin_compound_stmt (flag_isoc99);
4075 loc = c_parser_peek_token (parser)->location;
4076 cond = c_parser_paren_condition (parser);
4077 save_break = c_break_label;
4078 c_break_label = NULL_TREE;
4079 save_cont = c_cont_label;
4080 c_cont_label = NULL_TREE;
4081 body = c_parser_c99_block_statement (parser);
4082 c_finish_loop (loc, cond, NULL, body, c_break_label, c_cont_label, true);
c2255bc4 4083 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
27bf414c
JM
4084 c_break_label = save_break;
4085 c_cont_label = save_cont;
4086}
4087
4088/* Parse a do statement (C90 6.6.5, C99 6.8.5).
4089
4090 do-statement:
4091 do statement while ( expression ) ;
4092*/
4093
4094static void
4095c_parser_do_statement (c_parser *parser)
4096{
4097 tree block, cond, body, save_break, save_cont, new_break, new_cont;
4098 location_t loc;
4099 gcc_assert (c_parser_next_token_is_keyword (parser, RID_DO));
4100 c_parser_consume_token (parser);
62e00e94 4101 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
3ba09659
AH
4102 warning_at (c_parser_peek_token (parser)->location,
4103 OPT_Wempty_body,
4104 "suggest braces around empty body in %<do%> statement");
27bf414c
JM
4105 block = c_begin_compound_stmt (flag_isoc99);
4106 loc = c_parser_peek_token (parser)->location;
4107 save_break = c_break_label;
4108 c_break_label = NULL_TREE;
4109 save_cont = c_cont_label;
4110 c_cont_label = NULL_TREE;
4111 body = c_parser_c99_block_statement (parser);
4112 c_parser_require_keyword (parser, RID_WHILE, "expected %<while%>");
4113 new_break = c_break_label;
4114 c_break_label = save_break;
4115 new_cont = c_cont_label;
4116 c_cont_label = save_cont;
4117 cond = c_parser_paren_condition (parser);
4118 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
4119 c_parser_skip_to_end_of_block_or_statement (parser);
4120 c_finish_loop (loc, cond, NULL, body, new_break, new_cont, false);
c2255bc4 4121 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
27bf414c
JM
4122}
4123
4124/* Parse a for statement (C90 6.6.5, C99 6.8.5).
4125
4126 for-statement:
4127 for ( expression[opt] ; expression[opt] ; expression[opt] ) statement
4128 for ( nested-declaration expression[opt] ; expression[opt] ) statement
4129
4130 The form with a declaration is new in C99.
4131
4132 ??? In accordance with the old parser, the declaration may be a
4133 nested function, which is then rejected in check_for_loop_decls,
4134 but does it make any sense for this to be included in the grammar?
4135 Note in particular that the nested function does not include a
4136 trailing ';', whereas the "declaration" production includes one.
4137 Also, can we reject bad declarations earlier and cheaper than
4138 check_for_loop_decls? */
4139
4140static void
4141c_parser_for_statement (c_parser *parser)
4142{
4143 tree block, cond, incr, save_break, save_cont, body;
c2255bc4
AH
4144 location_t loc = c_parser_peek_token (parser)->location;
4145 location_t for_loc = c_parser_peek_token (parser)->location;
27bf414c
JM
4146 gcc_assert (c_parser_next_token_is_keyword (parser, RID_FOR));
4147 c_parser_consume_token (parser);
4148 block = c_begin_compound_stmt (flag_isoc99);
4149 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4150 {
4151 /* Parse the initialization declaration or expression. */
4152 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4153 {
4154 c_parser_consume_token (parser);
c2255bc4 4155 c_finish_expr_stmt (loc, NULL_TREE);
27bf414c
JM
4156 }
4157 else if (c_parser_next_token_starts_declspecs (parser))
4158 {
4159 c_parser_declaration_or_fndef (parser, true, true, true, true);
c2255bc4 4160 check_for_loop_decls (for_loc);
27bf414c
JM
4161 }
4162 else if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
4163 {
4164 /* __extension__ can start a declaration, but is also an
4165 unary operator that can start an expression. Consume all
4166 but the last of a possible series of __extension__ to
4167 determine which. */
4168 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
4169 && (c_parser_peek_2nd_token (parser)->keyword
4170 == RID_EXTENSION))
4171 c_parser_consume_token (parser);
4172 if (c_token_starts_declspecs (c_parser_peek_2nd_token (parser)))
4173 {
4174 int ext;
4175 ext = disable_extension_diagnostics ();
4176 c_parser_consume_token (parser);
4177 c_parser_declaration_or_fndef (parser, true, true, true, true);
4178 restore_extension_diagnostics (ext);
c2255bc4 4179 check_for_loop_decls (for_loc);
27bf414c
JM
4180 }
4181 else
4182 goto init_expr;
4183 }
4184 else
4185 {
4186 init_expr:
c2255bc4 4187 c_finish_expr_stmt (loc, c_parser_expression (parser).value);
27bf414c
JM
4188 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
4189 }
4190 /* Parse the loop condition. */
27bf414c
JM
4191 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4192 {
4193 c_parser_consume_token (parser);
4194 cond = NULL_TREE;
4195 }
4196 else
4197 {
ca085fd7 4198 cond = c_parser_condition (parser);
27bf414c
JM
4199 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
4200 }
4201 /* Parse the increment expression. */
4202 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
c2255bc4 4203 incr = c_process_expr_stmt (loc, NULL_TREE);
27bf414c 4204 else
c2255bc4 4205 incr = c_process_expr_stmt (loc, c_parser_expression (parser).value);
27bf414c
JM
4206 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
4207 }
4208 else
4209 {
4210 cond = error_mark_node;
4211 incr = error_mark_node;
4212 }
4213 save_break = c_break_label;
4214 c_break_label = NULL_TREE;
4215 save_cont = c_cont_label;
4216 c_cont_label = NULL_TREE;
4217 body = c_parser_c99_block_statement (parser);
4218 c_finish_loop (loc, cond, incr, body, c_break_label, c_cont_label, true);
c2255bc4 4219 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
27bf414c
JM
4220 c_break_label = save_break;
4221 c_cont_label = save_cont;
4222}
4223
4224/* Parse an asm statement, a GNU extension. This is a full-blown asm
4225 statement with inputs, outputs, clobbers, and volatile tag
4226 allowed.
4227
4228 asm-statement:
4229 asm type-qualifier[opt] ( asm-argument ) ;
4230
4231 asm-argument:
4232 asm-string-literal
4233 asm-string-literal : asm-operands[opt]
4234 asm-string-literal : asm-operands[opt] : asm-operands[opt]
4235 asm-string-literal : asm-operands[opt] : asm-operands[opt] : asm-clobbers
4236
4237 Qualifiers other than volatile are accepted in the syntax but
4238 warned for. */
4239
4240static tree
4241c_parser_asm_statement (c_parser *parser)
4242{
4243 tree quals, str, outputs, inputs, clobbers, ret;
4244 bool simple;
c2255bc4 4245 location_t asm_loc = c_parser_peek_token (parser)->location;
27bf414c
JM
4246 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
4247 c_parser_consume_token (parser);
4248 if (c_parser_next_token_is_keyword (parser, RID_VOLATILE))
4249 {
4250 quals = c_parser_peek_token (parser)->value;
4251 c_parser_consume_token (parser);
4252 }
4253 else if (c_parser_next_token_is_keyword (parser, RID_CONST)
4254 || c_parser_next_token_is_keyword (parser, RID_RESTRICT))
4255 {
3ba09659
AH
4256 warning_at (c_parser_peek_token (parser)->location,
4257 0,
4258 "%E qualifier ignored on asm",
4259 c_parser_peek_token (parser)->value);
27bf414c
JM
4260 quals = NULL_TREE;
4261 c_parser_consume_token (parser);
4262 }
4263 else
4264 quals = NULL_TREE;
4265 /* ??? Follow the C++ parser rather than using the
46c2514e
TT
4266 lex_untranslated_string kludge. */
4267 parser->lex_untranslated_string = true;
27bf414c
JM
4268 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4269 {
46c2514e 4270 parser->lex_untranslated_string = false;
27bf414c
JM
4271 return NULL_TREE;
4272 }
4273 str = c_parser_asm_string_literal (parser);
b85eb797
JJ
4274 if (str == NULL_TREE)
4275 {
4276 parser->lex_untranslated_string = false;
4277 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4278 return NULL_TREE;
4279 }
27bf414c
JM
4280 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4281 {
4282 simple = true;
4283 outputs = NULL_TREE;
4284 inputs = NULL_TREE;
4285 clobbers = NULL_TREE;
4286 goto done_asm;
4287 }
4288 if (!c_parser_require (parser, CPP_COLON, "expected %<:%> or %<)%>"))
4289 {
46c2514e 4290 parser->lex_untranslated_string = false;
27bf414c
JM
4291 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4292 return NULL_TREE;
4293 }
4294 simple = false;
4295 /* Parse outputs. */
4296 if (c_parser_next_token_is (parser, CPP_COLON)
4297 || c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4298 outputs = NULL_TREE;
4299 else
46bdb9cf 4300 outputs = c_parser_asm_operands (parser, false);
27bf414c
JM
4301 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4302 {
4303 inputs = NULL_TREE;
4304 clobbers = NULL_TREE;
4305 goto done_asm;
4306 }
4307 if (!c_parser_require (parser, CPP_COLON, "expected %<:%> or %<)%>"))
4308 {
46c2514e 4309 parser->lex_untranslated_string = false;
27bf414c
JM
4310 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4311 return NULL_TREE;
4312 }
4313 /* Parse inputs. */
4314 if (c_parser_next_token_is (parser, CPP_COLON)
4315 || c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4316 inputs = NULL_TREE;
4317 else
46bdb9cf 4318 inputs = c_parser_asm_operands (parser, true);
27bf414c
JM
4319 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4320 {
4321 clobbers = NULL_TREE;
4322 goto done_asm;
4323 }
4324 if (!c_parser_require (parser, CPP_COLON, "expected %<:%> or %<)%>"))
4325 {
46c2514e 4326 parser->lex_untranslated_string = false;
27bf414c
JM
4327 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4328 return NULL_TREE;
4329 }
4330 /* Parse clobbers. */
4331 clobbers = c_parser_asm_clobbers (parser);
4332 done_asm:
46c2514e 4333 parser->lex_untranslated_string = false;
27bf414c
JM
4334 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
4335 {
4336 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4337 return NULL_TREE;
4338 }
4339 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
4340 c_parser_skip_to_end_of_block_or_statement (parser);
c2255bc4 4341 ret = build_asm_stmt (quals, build_asm_expr (asm_loc, str, outputs, inputs,
27bf414c
JM
4342 clobbers, simple));
4343 return ret;
4344}
4345
46bdb9cf
JM
4346/* Parse asm operands, a GNU extension. If CONVERT_P (for inputs but
4347 not outputs), apply the default conversion of functions and arrays
4348 to pointers.
27bf414c
JM
4349
4350 asm-operands:
4351 asm-operand
4352 asm-operands , asm-operand
4353
4354 asm-operand:
4355 asm-string-literal ( expression )
4356 [ identifier ] asm-string-literal ( expression )
4357*/
4358
4359static tree
46bdb9cf 4360c_parser_asm_operands (c_parser *parser, bool convert_p)
27bf414c
JM
4361{
4362 tree list = NULL_TREE;
c2255bc4 4363 location_t loc;
27bf414c
JM
4364 while (true)
4365 {
f2a71bbc
JM
4366 tree name, str;
4367 struct c_expr expr;
27bf414c
JM
4368 if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
4369 {
4370 c_parser_consume_token (parser);
4371 if (c_parser_next_token_is (parser, CPP_NAME))
4372 {
4373 tree id = c_parser_peek_token (parser)->value;
4374 c_parser_consume_token (parser);
4375 name = build_string (IDENTIFIER_LENGTH (id),
4376 IDENTIFIER_POINTER (id));
4377 }
4378 else
4379 {
4380 c_parser_error (parser, "expected identifier");
4381 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
4382 return NULL_TREE;
4383 }
4384 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
4385 "expected %<]%>");
4386 }
4387 else
4388 name = NULL_TREE;
4389 str = c_parser_asm_string_literal (parser);
4390 if (str == NULL_TREE)
4391 return NULL_TREE;
46c2514e 4392 parser->lex_untranslated_string = false;
27bf414c
JM
4393 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4394 {
46c2514e 4395 parser->lex_untranslated_string = true;
27bf414c
JM
4396 return NULL_TREE;
4397 }
c2255bc4 4398 loc = c_parser_peek_token (parser)->location;
f2a71bbc 4399 expr = c_parser_expression (parser);
46bdb9cf 4400 if (convert_p)
c2255bc4 4401 expr = default_function_array_conversion (loc, expr);
928c19bb 4402 expr.value = c_fully_fold (expr.value, false, NULL);
46c2514e 4403 parser->lex_untranslated_string = true;
27bf414c
JM
4404 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
4405 {
4406 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4407 return NULL_TREE;
4408 }
4409 list = chainon (list, build_tree_list (build_tree_list (name, str),
f2a71bbc 4410 expr.value));
27bf414c
JM
4411 if (c_parser_next_token_is (parser, CPP_COMMA))
4412 c_parser_consume_token (parser);
4413 else
4414 break;
4415 }
4416 return list;
4417}
4418
4419/* Parse asm clobbers, a GNU extension.
4420
4421 asm-clobbers:
4422 asm-string-literal
4423 asm-clobbers , asm-string-literal
4424*/
4425
4426static tree
4427c_parser_asm_clobbers (c_parser *parser)
4428{
4429 tree list = NULL_TREE;
4430 while (true)
4431 {
4432 tree str = c_parser_asm_string_literal (parser);
4433 if (str)
4434 list = tree_cons (NULL_TREE, str, list);
4435 else
4436 return NULL_TREE;
4437 if (c_parser_next_token_is (parser, CPP_COMMA))
4438 c_parser_consume_token (parser);
4439 else
4440 break;
4441 }
4442 return list;
4443}
4444
4445/* Parse an expression other than a compound expression; that is, an
4446 assignment expression (C90 6.3.16, C99 6.5.16). If AFTER is not
4447 NULL then it is an Objective-C message expression which is the
4448 primary-expression starting the expression as an initializer.
4449
4450 assignment-expression:
4451 conditional-expression
4452 unary-expression assignment-operator assignment-expression
4453
4454 assignment-operator: one of
4455 = *= /= %= += -= <<= >>= &= ^= |=
4456
4457 In GNU C we accept any conditional expression on the LHS and
4458 diagnose the invalid lvalue rather than producing a syntax
4459 error. */
4460
4461static struct c_expr
4462c_parser_expr_no_commas (c_parser *parser, struct c_expr *after)
4463{
4464 struct c_expr lhs, rhs, ret;
4465 enum tree_code code;
c2255bc4 4466 location_t op_location, exp_location;
27bf414c
JM
4467 gcc_assert (!after || c_dialect_objc ());
4468 lhs = c_parser_conditional_expression (parser, after);
c9f9eb5d 4469 op_location = c_parser_peek_token (parser)->location;
27bf414c
JM
4470 switch (c_parser_peek_token (parser)->type)
4471 {
4472 case CPP_EQ:
4473 code = NOP_EXPR;
4474 break;
4475 case CPP_MULT_EQ:
4476 code = MULT_EXPR;
4477 break;
4478 case CPP_DIV_EQ:
4479 code = TRUNC_DIV_EXPR;
4480 break;
4481 case CPP_MOD_EQ:
4482 code = TRUNC_MOD_EXPR;
4483 break;
4484 case CPP_PLUS_EQ:
4485 code = PLUS_EXPR;
4486 break;
4487 case CPP_MINUS_EQ:
4488 code = MINUS_EXPR;
4489 break;
4490 case CPP_LSHIFT_EQ:
4491 code = LSHIFT_EXPR;
4492 break;
4493 case CPP_RSHIFT_EQ:
4494 code = RSHIFT_EXPR;
4495 break;
4496 case CPP_AND_EQ:
4497 code = BIT_AND_EXPR;
4498 break;
4499 case CPP_XOR_EQ:
4500 code = BIT_XOR_EXPR;
4501 break;
4502 case CPP_OR_EQ:
4503 code = BIT_IOR_EXPR;
4504 break;
4505 default:
4506 return lhs;
4507 }
4508 c_parser_consume_token (parser);
c2255bc4 4509 exp_location = c_parser_peek_token (parser)->location;
27bf414c 4510 rhs = c_parser_expr_no_commas (parser, NULL);
c2255bc4 4511 rhs = default_function_array_conversion (exp_location, rhs);
32e8bb8e 4512 ret.value = build_modify_expr (op_location, lhs.value, lhs.original_type,
c2255bc4
AH
4513 code, exp_location, rhs.value,
4514 rhs.original_type);
27bf414c
JM
4515 if (code == NOP_EXPR)
4516 ret.original_code = MODIFY_EXPR;
4517 else
4518 {
4519 TREE_NO_WARNING (ret.value) = 1;
4520 ret.original_code = ERROR_MARK;
4521 }
6866c6e8 4522 ret.original_type = NULL;
27bf414c
JM
4523 return ret;
4524}
4525
4526/* Parse a conditional expression (C90 6.3.15, C99 6.5.15). If AFTER
4527 is not NULL then it is an Objective-C message expression which is
4528 the primary-expression starting the expression as an initializer.
4529
4530 conditional-expression:
4531 logical-OR-expression
4532 logical-OR-expression ? expression : conditional-expression
4533
4534 GNU extensions:
4535
4536 conditional-expression:
4537 logical-OR-expression ? : conditional-expression
4538*/
4539
4540static struct c_expr
4541c_parser_conditional_expression (c_parser *parser, struct c_expr *after)
4542{
4543 struct c_expr cond, exp1, exp2, ret;
744aa42f 4544 location_t cond_loc, colon_loc;
ba47d38d 4545
27bf414c 4546 gcc_assert (!after || c_dialect_objc ());
ba47d38d 4547
27bf414c 4548 cond = c_parser_binary_expression (parser, after);
ba47d38d 4549
27bf414c
JM
4550 if (c_parser_next_token_is_not (parser, CPP_QUERY))
4551 return cond;
c2255bc4
AH
4552 cond_loc = c_parser_peek_token (parser)->location;
4553 cond = default_function_array_conversion (cond_loc, cond);
27bf414c
JM
4554 c_parser_consume_token (parser);
4555 if (c_parser_next_token_is (parser, CPP_COLON))
4556 {
8ce94e44 4557 tree eptype = NULL_TREE;
509c9d60
MLI
4558 pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic,
4559 "ISO C forbids omitting the middle term of a ?: expression");
8ce94e44
JM
4560 if (TREE_CODE (cond.value) == EXCESS_PRECISION_EXPR)
4561 {
4562 eptype = TREE_TYPE (cond.value);
4563 cond.value = TREE_OPERAND (cond.value, 0);
4564 }
27bf414c 4565 /* Make sure first operand is calculated only once. */
928c19bb 4566 exp1.value = c_save_expr (default_conversion (cond.value));
8ce94e44
JM
4567 if (eptype)
4568 exp1.value = build1 (EXCESS_PRECISION_EXPR, eptype, exp1.value);
6866c6e8 4569 exp1.original_type = NULL;
ba47d38d 4570 cond.value = c_objc_common_truthvalue_conversion (cond_loc, exp1.value);
7d882b83 4571 c_inhibit_evaluation_warnings += cond.value == truthvalue_true_node;
27bf414c
JM
4572 }
4573 else
4574 {
4575 cond.value
85498824 4576 = c_objc_common_truthvalue_conversion
ba47d38d 4577 (cond_loc, default_conversion (cond.value));
7d882b83 4578 c_inhibit_evaluation_warnings += cond.value == truthvalue_false_node;
46bdb9cf 4579 exp1 = c_parser_expression_conv (parser);
7d882b83
ILT
4580 c_inhibit_evaluation_warnings +=
4581 ((cond.value == truthvalue_true_node)
4582 - (cond.value == truthvalue_false_node));
27bf414c 4583 }
744aa42f
ILT
4584
4585 colon_loc = c_parser_peek_token (parser)->location;
27bf414c
JM
4586 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
4587 {
7d882b83 4588 c_inhibit_evaluation_warnings -= cond.value == truthvalue_true_node;
27bf414c
JM
4589 ret.value = error_mark_node;
4590 ret.original_code = ERROR_MARK;
6866c6e8 4591 ret.original_type = NULL;
27bf414c
JM
4592 return ret;
4593 }
c2255bc4
AH
4594 {
4595 location_t exp2_loc = c_parser_peek_token (parser)->location;
4596 exp2 = c_parser_conditional_expression (parser, NULL);
4597 exp2 = default_function_array_conversion (exp2_loc, exp2);
4598 }
7d882b83 4599 c_inhibit_evaluation_warnings -= cond.value == truthvalue_true_node;
744aa42f 4600 ret.value = build_conditional_expr (colon_loc, cond.value,
928c19bb
JM
4601 cond.original_code == C_MAYBE_CONST_EXPR,
4602 exp1.value, exp2.value);
27bf414c 4603 ret.original_code = ERROR_MARK;
6866c6e8
ILT
4604 if (exp1.value == error_mark_node || exp2.value == error_mark_node)
4605 ret.original_type = NULL;
4606 else
4607 {
4608 tree t1, t2;
4609
4610 /* If both sides are enum type, the default conversion will have
4611 made the type of the result be an integer type. We want to
4612 remember the enum types we started with. */
4613 t1 = exp1.original_type ? exp1.original_type : TREE_TYPE (exp1.value);
4614 t2 = exp2.original_type ? exp2.original_type : TREE_TYPE (exp2.value);
4615 ret.original_type = ((t1 != error_mark_node
4616 && t2 != error_mark_node
4617 && (TYPE_MAIN_VARIANT (t1)
4618 == TYPE_MAIN_VARIANT (t2)))
4619 ? t1
4620 : NULL);
4621 }
27bf414c
JM
4622 return ret;
4623}
4624
4625/* Parse a binary expression; that is, a logical-OR-expression (C90
4626 6.3.5-6.3.14, C99 6.5.5-6.5.14). If AFTER is not NULL then it is
4627 an Objective-C message expression which is the primary-expression
4628 starting the expression as an initializer.
4629
4630 multiplicative-expression:
4631 cast-expression
4632 multiplicative-expression * cast-expression
4633 multiplicative-expression / cast-expression
4634 multiplicative-expression % cast-expression
4635
4636 additive-expression:
4637 multiplicative-expression
4638 additive-expression + multiplicative-expression
4639 additive-expression - multiplicative-expression
4640
4641 shift-expression:
4642 additive-expression
4643 shift-expression << additive-expression
4644 shift-expression >> additive-expression
4645
4646 relational-expression:
4647 shift-expression
4648 relational-expression < shift-expression
4649 relational-expression > shift-expression
4650 relational-expression <= shift-expression
4651 relational-expression >= shift-expression
4652
4653 equality-expression:
4654 relational-expression
4655 equality-expression == relational-expression
4656 equality-expression != relational-expression
4657
4658 AND-expression:
4659 equality-expression
4660 AND-expression & equality-expression
4661
4662 exclusive-OR-expression:
4663 AND-expression
4664 exclusive-OR-expression ^ AND-expression
4665
4666 inclusive-OR-expression:
4667 exclusive-OR-expression
4668 inclusive-OR-expression | exclusive-OR-expression
4669
4670 logical-AND-expression:
4671 inclusive-OR-expression
4672 logical-AND-expression && inclusive-OR-expression
4673
4674 logical-OR-expression:
4675 logical-AND-expression
4676 logical-OR-expression || logical-AND-expression
4677*/
4678
4679static struct c_expr
4680c_parser_binary_expression (c_parser *parser, struct c_expr *after)
4681{
4682 /* A binary expression is parsed using operator-precedence parsing,
4683 with the operands being cast expressions. All the binary
4684 operators are left-associative. Thus a binary expression is of
4685 form:
4686
4687 E0 op1 E1 op2 E2 ...
4688
4689 which we represent on a stack. On the stack, the precedence
4690 levels are strictly increasing. When a new operator is
4691 encountered of higher precedence than that at the top of the
4692 stack, it is pushed; its LHS is the top expression, and its RHS
4693 is everything parsed until it is popped. When a new operator is
4694 encountered with precedence less than or equal to that at the top
4695 of the stack, triples E[i-1] op[i] E[i] are popped and replaced
4696 by the result of the operation until the operator at the top of
4697 the stack has lower precedence than the new operator or there is
4698 only one element on the stack; then the top expression is the LHS
4699 of the new operator. In the case of logical AND and OR
7d882b83
ILT
4700 expressions, we also need to adjust c_inhibit_evaluation_warnings
4701 as appropriate when the operators are pushed and popped. */
27bf414c
JM
4702
4703 /* The precedence levels, where 0 is a dummy lowest level used for
4704 the bottom of the stack. */
4705 enum prec {
4706 PREC_NONE,
4707 PREC_LOGOR,
4708 PREC_LOGAND,
4709 PREC_BITOR,
4710 PREC_BITXOR,
4711 PREC_BITAND,
4712 PREC_EQ,
4713 PREC_REL,
4714 PREC_SHIFT,
4715 PREC_ADD,
4716 PREC_MULT,
4717 NUM_PRECS
4718 };
4719 struct {
4720 /* The expression at this stack level. */
4721 struct c_expr expr;
4722 /* The precedence of the operator on its left, PREC_NONE at the
4723 bottom of the stack. */
4724 enum prec prec;
4725 /* The operation on its left. */
4726 enum tree_code op;
ca80e52b
EB
4727 /* The source location of this operation. */
4728 location_t loc;
27bf414c
JM
4729 } stack[NUM_PRECS];
4730 int sp;
ba47d38d 4731 /* Location of the binary operator. */
1f6d0c60 4732 location_t binary_loc = UNKNOWN_LOCATION; /* Quiet warning. */
27bf414c
JM
4733#define POP \
4734 do { \
4735 switch (stack[sp].op) \
4736 { \
4737 case TRUTH_ANDIF_EXPR: \
7d882b83
ILT
4738 c_inhibit_evaluation_warnings -= (stack[sp - 1].expr.value \
4739 == truthvalue_false_node); \
27bf414c
JM
4740 break; \
4741 case TRUTH_ORIF_EXPR: \
7d882b83
ILT
4742 c_inhibit_evaluation_warnings -= (stack[sp - 1].expr.value \
4743 == truthvalue_true_node); \
27bf414c
JM
4744 break; \
4745 default: \
4746 break; \
4747 } \
f2a71bbc 4748 stack[sp - 1].expr \
c2255bc4
AH
4749 = default_function_array_conversion (stack[sp - 1].loc, \
4750 stack[sp - 1].expr); \
f2a71bbc 4751 stack[sp].expr \
c2255bc4 4752 = default_function_array_conversion (stack[sp].loc, stack[sp].expr); \
ca80e52b 4753 stack[sp - 1].expr = parser_build_binary_op (stack[sp].loc, \
ba47d38d 4754 stack[sp].op, \
27bf414c
JM
4755 stack[sp - 1].expr, \
4756 stack[sp].expr); \
4757 sp--; \
4758 } while (0)
4759 gcc_assert (!after || c_dialect_objc ());
ca80e52b 4760 stack[0].loc = c_parser_peek_token (parser)->location;
27bf414c
JM
4761 stack[0].expr = c_parser_cast_expression (parser, after);
4762 stack[0].prec = PREC_NONE;
4763 sp = 0;
4764 while (true)
4765 {
4766 enum prec oprec;
4767 enum tree_code ocode;
4768 if (parser->error)
4769 goto out;
4770 switch (c_parser_peek_token (parser)->type)
4771 {
4772 case CPP_MULT:
4773 oprec = PREC_MULT;
4774 ocode = MULT_EXPR;
4775 break;
4776 case CPP_DIV:
4777 oprec = PREC_MULT;
4778 ocode = TRUNC_DIV_EXPR;
4779 break;
4780 case CPP_MOD:
4781 oprec = PREC_MULT;
4782 ocode = TRUNC_MOD_EXPR;
4783 break;
4784 case CPP_PLUS:
4785 oprec = PREC_ADD;
4786 ocode = PLUS_EXPR;
4787 break;
4788 case CPP_MINUS:
4789 oprec = PREC_ADD;
4790 ocode = MINUS_EXPR;
4791 break;
4792 case CPP_LSHIFT:
4793 oprec = PREC_SHIFT;
4794 ocode = LSHIFT_EXPR;
4795 break;
4796 case CPP_RSHIFT:
4797 oprec = PREC_SHIFT;
4798 ocode = RSHIFT_EXPR;
4799 break;
4800 case CPP_LESS:
4801 oprec = PREC_REL;
4802 ocode = LT_EXPR;
4803 break;
4804 case CPP_GREATER:
4805 oprec = PREC_REL;
4806 ocode = GT_EXPR;
4807 break;
4808 case CPP_LESS_EQ:
4809 oprec = PREC_REL;
4810 ocode = LE_EXPR;
4811 break;
4812 case CPP_GREATER_EQ:
4813 oprec = PREC_REL;
4814 ocode = GE_EXPR;
4815 break;
4816 case CPP_EQ_EQ:
4817 oprec = PREC_EQ;
4818 ocode = EQ_EXPR;
4819 break;
4820 case CPP_NOT_EQ:
4821 oprec = PREC_EQ;
4822 ocode = NE_EXPR;
4823 break;
4824 case CPP_AND:
4825 oprec = PREC_BITAND;
4826 ocode = BIT_AND_EXPR;
4827 break;
4828 case CPP_XOR:
4829 oprec = PREC_BITXOR;
4830 ocode = BIT_XOR_EXPR;
4831 break;
4832 case CPP_OR:
4833 oprec = PREC_BITOR;
4834 ocode = BIT_IOR_EXPR;
4835 break;
4836 case CPP_AND_AND:
4837 oprec = PREC_LOGAND;
4838 ocode = TRUTH_ANDIF_EXPR;
4839 break;
4840 case CPP_OR_OR:
4841 oprec = PREC_LOGOR;
4842 ocode = TRUTH_ORIF_EXPR;
4843 break;
4844 default:
4845 /* Not a binary operator, so end of the binary
4846 expression. */
4847 goto out;
4848 }
ba47d38d 4849 binary_loc = c_parser_peek_token (parser)->location;
27bf414c
JM
4850 c_parser_consume_token (parser);
4851 while (oprec <= stack[sp].prec)
4852 POP;
4853 switch (ocode)
4854 {
4855 case TRUTH_ANDIF_EXPR:
f2a71bbc 4856 stack[sp].expr
c2255bc4
AH
4857 = default_function_array_conversion (stack[sp].loc,
4858 stack[sp].expr);
85498824 4859 stack[sp].expr.value = c_objc_common_truthvalue_conversion
ca80e52b 4860 (stack[sp].loc, default_conversion (stack[sp].expr.value));
7d882b83
ILT
4861 c_inhibit_evaluation_warnings += (stack[sp].expr.value
4862 == truthvalue_false_node);
27bf414c
JM
4863 break;
4864 case TRUTH_ORIF_EXPR:
f2a71bbc 4865 stack[sp].expr
c2255bc4
AH
4866 = default_function_array_conversion (stack[sp].loc,
4867 stack[sp].expr);
85498824 4868 stack[sp].expr.value = c_objc_common_truthvalue_conversion
ca80e52b 4869 (stack[sp].loc, default_conversion (stack[sp].expr.value));
7d882b83
ILT
4870 c_inhibit_evaluation_warnings += (stack[sp].expr.value
4871 == truthvalue_true_node);
27bf414c
JM
4872 break;
4873 default:
4874 break;
4875 }
4876 sp++;
ca80e52b 4877 stack[sp].loc = binary_loc;
27bf414c
JM
4878 stack[sp].expr = c_parser_cast_expression (parser, NULL);
4879 stack[sp].prec = oprec;
4880 stack[sp].op = ocode;
c2255bc4 4881 stack[sp].loc = binary_loc;
27bf414c
JM
4882 }
4883 out:
4884 while (sp > 0)
4885 POP;
4886 return stack[0].expr;
4887#undef POP
4888}
4889
4890/* Parse a cast expression (C90 6.3.4, C99 6.5.4). If AFTER is not
4891 NULL then it is an Objective-C message expression which is the
4892 primary-expression starting the expression as an initializer.
4893
4894 cast-expression:
4895 unary-expression
4896 ( type-name ) unary-expression
4897*/
4898
4899static struct c_expr
4900c_parser_cast_expression (c_parser *parser, struct c_expr *after)
4901{
c2255bc4 4902 location_t cast_loc = c_parser_peek_token (parser)->location;
27bf414c
JM
4903 gcc_assert (!after || c_dialect_objc ());
4904 if (after)
c2255bc4
AH
4905 return c_parser_postfix_expression_after_primary (parser,
4906 cast_loc, *after);
27bf414c
JM
4907 /* If the expression begins with a parenthesized type name, it may
4908 be either a cast or a compound literal; we need to see whether
4909 the next character is '{' to tell the difference. If not, it is
4910 an unary expression. */
4911 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
4912 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
4913 {
4914 struct c_type_name *type_name;
4915 struct c_expr ret;
f2a71bbc 4916 struct c_expr expr;
27bf414c
JM
4917 c_parser_consume_token (parser);
4918 type_name = c_parser_type_name (parser);
4919 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
4920 if (type_name == NULL)
4921 {
4922 ret.value = error_mark_node;
4923 ret.original_code = ERROR_MARK;
6866c6e8 4924 ret.original_type = NULL;
27bf414c
JM
4925 return ret;
4926 }
33c9159e
AH
4927
4928 /* Save casted types in the function's used types hash table. */
8d8d1a28 4929 used_types_insert (type_name->specs->type);
33c9159e 4930
27bf414c 4931 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
24b97832 4932 return c_parser_postfix_expression_after_paren_type (parser, type_name,
c2255bc4
AH
4933 cast_loc);
4934 {
4935 location_t expr_loc = c_parser_peek_token (parser)->location;
4936 expr = c_parser_cast_expression (parser, NULL);
4937 expr = default_function_array_conversion (expr_loc, expr);
4938 }
4939 ret.value = c_cast_expr (cast_loc, type_name, expr.value);
27bf414c 4940 ret.original_code = ERROR_MARK;
6866c6e8 4941 ret.original_type = NULL;
27bf414c
JM
4942 return ret;
4943 }
4944 else
4945 return c_parser_unary_expression (parser);
4946}
4947
4948/* Parse an unary expression (C90 6.3.3, C99 6.5.3).
4949
4950 unary-expression:
4951 postfix-expression
4952 ++ unary-expression
4953 -- unary-expression
4954 unary-operator cast-expression
4955 sizeof unary-expression
4956 sizeof ( type-name )
4957
4958 unary-operator: one of
4959 & * + - ~ !
4960
4961 GNU extensions:
4962
4963 unary-expression:
4964 __alignof__ unary-expression
4965 __alignof__ ( type-name )
4966 && identifier
4967
4968 unary-operator: one of
4969 __extension__ __real__ __imag__
4970
4971 In addition, the GNU syntax treats ++ and -- as unary operators, so
4972 they may be applied to cast expressions with errors for non-lvalues
4973 given later. */
4974
4975static struct c_expr
4976c_parser_unary_expression (c_parser *parser)
4977{
4978 int ext;
46bdb9cf 4979 struct c_expr ret, op;
c2255bc4
AH
4980 location_t op_loc = c_parser_peek_token (parser)->location;
4981 location_t exp_loc;
6866c6e8
ILT
4982 ret.original_code = ERROR_MARK;
4983 ret.original_type = NULL;
27bf414c
JM
4984 switch (c_parser_peek_token (parser)->type)
4985 {
4986 case CPP_PLUS_PLUS:
4987 c_parser_consume_token (parser);
c2255bc4 4988 exp_loc = c_parser_peek_token (parser)->location;
46bdb9cf 4989 op = c_parser_cast_expression (parser, NULL);
c2255bc4
AH
4990 op = default_function_array_conversion (exp_loc, op);
4991 return parser_build_unary_op (op_loc, PREINCREMENT_EXPR, op);
27bf414c
JM
4992 case CPP_MINUS_MINUS:
4993 c_parser_consume_token (parser);
c2255bc4 4994 exp_loc = c_parser_peek_token (parser)->location;
46bdb9cf 4995 op = c_parser_cast_expression (parser, NULL);
c2255bc4
AH
4996 op = default_function_array_conversion (exp_loc, op);
4997 return parser_build_unary_op (op_loc, PREDECREMENT_EXPR, op);
27bf414c
JM
4998 case CPP_AND:
4999 c_parser_consume_token (parser);
c2255bc4
AH
5000 return parser_build_unary_op (op_loc, ADDR_EXPR,
5001 c_parser_cast_expression (parser, NULL));
27bf414c
JM
5002 case CPP_MULT:
5003 c_parser_consume_token (parser);
c2255bc4 5004 exp_loc = c_parser_peek_token (parser)->location;
46bdb9cf 5005 op = c_parser_cast_expression (parser, NULL);
c2255bc4
AH
5006 op = default_function_array_conversion (exp_loc, op);
5007 ret.value = build_indirect_ref (op_loc, op.value, "unary *");
27bf414c
JM
5008 return ret;
5009 case CPP_PLUS:
44c21c7f 5010 if (!c_dialect_objc () && !in_system_header)
c2255bc4 5011 warning_at (op_loc,
3ba09659
AH
5012 OPT_Wtraditional,
5013 "traditional C rejects the unary plus operator");
c7412148 5014 c_parser_consume_token (parser);
c2255bc4 5015 exp_loc = c_parser_peek_token (parser)->location;
46bdb9cf 5016 op = c_parser_cast_expression (parser, NULL);
c2255bc4
AH
5017 op = default_function_array_conversion (exp_loc, op);
5018 return parser_build_unary_op (op_loc, CONVERT_EXPR, op);
27bf414c
JM
5019 case CPP_MINUS:
5020 c_parser_consume_token (parser);
c2255bc4 5021 exp_loc = c_parser_peek_token (parser)->location;
46bdb9cf 5022 op = c_parser_cast_expression (parser, NULL);
c2255bc4
AH
5023 op = default_function_array_conversion (exp_loc, op);
5024 return parser_build_unary_op (op_loc, NEGATE_EXPR, op);
27bf414c
JM
5025 case CPP_COMPL:
5026 c_parser_consume_token (parser);
c2255bc4 5027 exp_loc = c_parser_peek_token (parser)->location;
46bdb9cf 5028 op = c_parser_cast_expression (parser, NULL);
c2255bc4
AH
5029 op = default_function_array_conversion (exp_loc, op);
5030 return parser_build_unary_op (op_loc, BIT_NOT_EXPR, op);
27bf414c
JM
5031 case CPP_NOT:
5032 c_parser_consume_token (parser);
c2255bc4 5033 exp_loc = c_parser_peek_token (parser)->location;
46bdb9cf 5034 op = c_parser_cast_expression (parser, NULL);
c2255bc4
AH
5035 op = default_function_array_conversion (exp_loc, op);
5036 return parser_build_unary_op (op_loc, TRUTH_NOT_EXPR, op);
27bf414c
JM
5037 case CPP_AND_AND:
5038 /* Refer to the address of a label as a pointer. */
5039 c_parser_consume_token (parser);
5040 if (c_parser_next_token_is (parser, CPP_NAME))
5041 {
5042 ret.value = finish_label_address_expr
c2255bc4 5043 (c_parser_peek_token (parser)->value, op_loc);
27bf414c 5044 c_parser_consume_token (parser);
27bf414c
JM
5045 }
5046 else
5047 {
5048 c_parser_error (parser, "expected identifier");
5049 ret.value = error_mark_node;
27bf414c 5050 }
43f6dfd3 5051 return ret;
27bf414c
JM
5052 case CPP_KEYWORD:
5053 switch (c_parser_peek_token (parser)->keyword)
5054 {
5055 case RID_SIZEOF:
5056 return c_parser_sizeof_expression (parser);
5057 case RID_ALIGNOF:
5058 return c_parser_alignof_expression (parser);
5059 case RID_EXTENSION:
5060 c_parser_consume_token (parser);
5061 ext = disable_extension_diagnostics ();
5062 ret = c_parser_cast_expression (parser, NULL);
5063 restore_extension_diagnostics (ext);
5064 return ret;
5065 case RID_REALPART:
5066 c_parser_consume_token (parser);
c2255bc4 5067 exp_loc = c_parser_peek_token (parser)->location;
46bdb9cf 5068 op = c_parser_cast_expression (parser, NULL);
c2255bc4
AH
5069 op = default_function_array_conversion (exp_loc, op);
5070 return parser_build_unary_op (op_loc, REALPART_EXPR, op);
27bf414c
JM
5071 case RID_IMAGPART:
5072 c_parser_consume_token (parser);
c2255bc4 5073 exp_loc = c_parser_peek_token (parser)->location;
46bdb9cf 5074 op = c_parser_cast_expression (parser, NULL);
c2255bc4
AH
5075 op = default_function_array_conversion (exp_loc, op);
5076 return parser_build_unary_op (op_loc, IMAGPART_EXPR, op);
27bf414c
JM
5077 default:
5078 return c_parser_postfix_expression (parser);
5079 }
5080 default:
5081 return c_parser_postfix_expression (parser);
5082 }
5083}
5084
5085/* Parse a sizeof expression. */
5086
5087static struct c_expr
5088c_parser_sizeof_expression (c_parser *parser)
5089{
5090 struct c_expr expr;
c7412148 5091 location_t expr_loc;
27bf414c
JM
5092 gcc_assert (c_parser_next_token_is_keyword (parser, RID_SIZEOF));
5093 c_parser_consume_token (parser);
7d882b83 5094 c_inhibit_evaluation_warnings++;
27bf414c
JM
5095 in_sizeof++;
5096 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
5097 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
5098 {
5099 /* Either sizeof ( type-name ) or sizeof unary-expression
5100 starting with a compound literal. */
5101 struct c_type_name *type_name;
5102 c_parser_consume_token (parser);
c7412148 5103 expr_loc = c_parser_peek_token (parser)->location;
27bf414c
JM
5104 type_name = c_parser_type_name (parser);
5105 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5106 if (type_name == NULL)
5107 {
5108 struct c_expr ret;
7d882b83 5109 c_inhibit_evaluation_warnings--;
27bf414c
JM
5110 in_sizeof--;
5111 ret.value = error_mark_node;
5112 ret.original_code = ERROR_MARK;
6866c6e8 5113 ret.original_type = NULL;
27bf414c
JM
5114 return ret;
5115 }
5116 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
5117 {
5118 expr = c_parser_postfix_expression_after_paren_type (parser,
24b97832
ILT
5119 type_name,
5120 expr_loc);
27bf414c
JM
5121 goto sizeof_expr;
5122 }
5123 /* sizeof ( type-name ). */
7d882b83 5124 c_inhibit_evaluation_warnings--;
27bf414c 5125 in_sizeof--;
c2255bc4 5126 return c_expr_sizeof_type (expr_loc, type_name);
27bf414c
JM
5127 }
5128 else
5129 {
c7412148 5130 expr_loc = c_parser_peek_token (parser)->location;
27bf414c
JM
5131 expr = c_parser_unary_expression (parser);
5132 sizeof_expr:
7d882b83 5133 c_inhibit_evaluation_warnings--;
27bf414c
JM
5134 in_sizeof--;
5135 if (TREE_CODE (expr.value) == COMPONENT_REF
5136 && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
3ba09659 5137 error_at (expr_loc, "%<sizeof%> applied to a bit-field");
c2255bc4 5138 return c_expr_sizeof_expr (expr_loc, expr);
27bf414c
JM
5139 }
5140}
5141
5142/* Parse an alignof expression. */
5143
5144static struct c_expr
5145c_parser_alignof_expression (c_parser *parser)
5146{
5147 struct c_expr expr;
c2255bc4 5148 location_t loc = c_parser_peek_token (parser)->location;
27bf414c
JM
5149 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ALIGNOF));
5150 c_parser_consume_token (parser);
7d882b83 5151 c_inhibit_evaluation_warnings++;
27bf414c
JM
5152 in_alignof++;
5153 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
5154 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
5155 {
5156 /* Either __alignof__ ( type-name ) or __alignof__
5157 unary-expression starting with a compound literal. */
24b97832 5158 location_t loc;
27bf414c
JM
5159 struct c_type_name *type_name;
5160 struct c_expr ret;
5161 c_parser_consume_token (parser);
24b97832 5162 loc = c_parser_peek_token (parser)->location;
27bf414c
JM
5163 type_name = c_parser_type_name (parser);
5164 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5165 if (type_name == NULL)
5166 {
5167 struct c_expr ret;
7d882b83 5168 c_inhibit_evaluation_warnings--;
27bf414c
JM
5169 in_alignof--;
5170 ret.value = error_mark_node;
5171 ret.original_code = ERROR_MARK;
6866c6e8 5172 ret.original_type = NULL;
27bf414c
JM
5173 return ret;
5174 }
5175 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
5176 {
5177 expr = c_parser_postfix_expression_after_paren_type (parser,
24b97832
ILT
5178 type_name,
5179 loc);
27bf414c
JM
5180 goto alignof_expr;
5181 }
5182 /* alignof ( type-name ). */
7d882b83 5183 c_inhibit_evaluation_warnings--;
27bf414c 5184 in_alignof--;
c2255bc4 5185 ret.value = c_alignof (loc, groktypename (type_name, NULL, NULL));
27bf414c 5186 ret.original_code = ERROR_MARK;
6866c6e8 5187 ret.original_type = NULL;
27bf414c
JM
5188 return ret;
5189 }
5190 else
5191 {
5192 struct c_expr ret;
5193 expr = c_parser_unary_expression (parser);
5194 alignof_expr:
7d882b83 5195 c_inhibit_evaluation_warnings--;
27bf414c 5196 in_alignof--;
c2255bc4 5197 ret.value = c_alignof_expr (loc, expr.value);
27bf414c 5198 ret.original_code = ERROR_MARK;
6866c6e8 5199 ret.original_type = NULL;
27bf414c
JM
5200 return ret;
5201 }
5202}
5203
5204/* Parse a postfix expression (C90 6.3.1-6.3.2, C99 6.5.1-6.5.2).
5205
5206 postfix-expression:
5207 primary-expression
5208 postfix-expression [ expression ]
5209 postfix-expression ( argument-expression-list[opt] )
5210 postfix-expression . identifier
5211 postfix-expression -> identifier
5212 postfix-expression ++
5213 postfix-expression --
5214 ( type-name ) { initializer-list }
5215 ( type-name ) { initializer-list , }
5216
5217 argument-expression-list:
5218 argument-expression
5219 argument-expression-list , argument-expression
5220
5221 primary-expression:
5222 identifier
5223 constant
5224 string-literal
5225 ( expression )
5226
5227 GNU extensions:
5228
5229 primary-expression:
5230 __func__
5231 (treated as a keyword in GNU C)
5232 __FUNCTION__
5233 __PRETTY_FUNCTION__
5234 ( compound-statement )
5235 __builtin_va_arg ( assignment-expression , type-name )
5236 __builtin_offsetof ( type-name , offsetof-member-designator )
5237 __builtin_choose_expr ( assignment-expression ,
5238 assignment-expression ,
5239 assignment-expression )
5240 __builtin_types_compatible_p ( type-name , type-name )
5241
5242 offsetof-member-designator:
5243 identifier
5244 offsetof-member-designator . identifier
5245 offsetof-member-designator [ expression ]
5246
5247 Objective-C:
5248
5249 primary-expression:
5250 [ objc-receiver objc-message-args ]
5251 @selector ( objc-selector-arg )
5252 @protocol ( identifier )
5253 @encode ( type-name )
5254 objc-string-literal
5255*/
5256
5257static struct c_expr
5258c_parser_postfix_expression (c_parser *parser)
5259{
5260 struct c_expr expr, e1, e2, e3;
5261 struct c_type_name *t1, *t2;
c2255bc4 5262 location_t loc = c_parser_peek_token (parser)->location;;
6866c6e8
ILT
5263 expr.original_code = ERROR_MARK;
5264 expr.original_type = NULL;
27bf414c
JM
5265 switch (c_parser_peek_token (parser)->type)
5266 {
5267 case CPP_NUMBER:
754ccf7c 5268 expr.value = c_parser_peek_token (parser)->value;
754ccf7c
JJ
5269 loc = c_parser_peek_token (parser)->location;
5270 c_parser_consume_token (parser);
5271 if (TREE_CODE (expr.value) == FIXED_CST
5272 && !targetm.fixed_point_supported_p ())
5273 {
5274 error_at (loc, "fixed-point types not supported for this target");
5275 expr.value = error_mark_node;
5276 }
5277 break;
27bf414c 5278 case CPP_CHAR:
b6baa67d
KVH
5279 case CPP_CHAR16:
5280 case CPP_CHAR32:
27bf414c
JM
5281 case CPP_WCHAR:
5282 expr.value = c_parser_peek_token (parser)->value;
27bf414c
JM
5283 c_parser_consume_token (parser);
5284 break;
5285 case CPP_STRING:
b6baa67d
KVH
5286 case CPP_STRING16:
5287 case CPP_STRING32:
27bf414c
JM
5288 case CPP_WSTRING:
5289 expr.value = c_parser_peek_token (parser)->value;
5290 expr.original_code = STRING_CST;
5291 c_parser_consume_token (parser);
5292 break;
5293 case CPP_OBJC_STRING:
5294 gcc_assert (c_dialect_objc ());
5295 expr.value
5296 = objc_build_string_object (c_parser_peek_token (parser)->value);
27bf414c
JM
5297 c_parser_consume_token (parser);
5298 break;
5299 case CPP_NAME:
5300 if (c_parser_peek_token (parser)->id_kind != C_ID_ID)
5301 {
5302 c_parser_error (parser, "expected expression");
5303 expr.value = error_mark_node;
27bf414c
JM
5304 break;
5305 }
5306 {
5307 tree id = c_parser_peek_token (parser)->value;
5308 c_parser_consume_token (parser);
c2255bc4 5309 expr.value = build_external_ref (loc, id,
27bf414c 5310 (c_parser_peek_token (parser)->type
c2255bc4 5311 == CPP_OPEN_PAREN),
6866c6e8 5312 &expr.original_type);
27bf414c
JM
5313 }
5314 break;
5315 case CPP_OPEN_PAREN:
5316 /* A parenthesized expression, statement expression or compound
5317 literal. */
5318 if (c_parser_peek_2nd_token (parser)->type == CPP_OPEN_BRACE)
5319 {
5320 /* A statement expression. */
5321 tree stmt;
c2255bc4 5322 location_t brace_loc;
27bf414c 5323 c_parser_consume_token (parser);
c2255bc4 5324 brace_loc = c_parser_peek_token (parser)->location;
27bf414c
JM
5325 c_parser_consume_token (parser);
5326 if (cur_stmt_list == NULL)
5327 {
c2255bc4 5328 error_at (loc, "braced-group within expression allowed "
3ba09659 5329 "only inside a function");
27bf414c
JM
5330 parser->error = true;
5331 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
5332 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5333 expr.value = error_mark_node;
27bf414c
JM
5334 break;
5335 }
5336 stmt = c_begin_stmt_expr ();
5337 c_parser_compound_statement_nostart (parser);
5338 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5339 "expected %<)%>");
c2255bc4 5340 pedwarn (loc, OPT_pedantic,
509c9d60 5341 "ISO C forbids braced-groups within expressions");
c2255bc4 5342 expr.value = c_finish_stmt_expr (brace_loc, stmt);
27bf414c
JM
5343 }
5344 else if (c_token_starts_typename (c_parser_peek_2nd_token (parser)))
5345 {
5346 /* A compound literal. ??? Can we actually get here rather
5347 than going directly to
5348 c_parser_postfix_expression_after_paren_type from
5349 elsewhere? */
24b97832 5350 location_t loc;
27bf414c
JM
5351 struct c_type_name *type_name;
5352 c_parser_consume_token (parser);
24b97832 5353 loc = c_parser_peek_token (parser)->location;
27bf414c
JM
5354 type_name = c_parser_type_name (parser);
5355 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5356 "expected %<)%>");
5357 if (type_name == NULL)
5358 {
5359 expr.value = error_mark_node;
27bf414c
JM
5360 }
5361 else
5362 expr = c_parser_postfix_expression_after_paren_type (parser,
24b97832
ILT
5363 type_name,
5364 loc);
27bf414c
JM
5365 }
5366 else
5367 {
5368 /* A parenthesized expression. */
5369 c_parser_consume_token (parser);
5370 expr = c_parser_expression (parser);
5371 if (TREE_CODE (expr.value) == MODIFY_EXPR)
5372 TREE_NO_WARNING (expr.value) = 1;
928c19bb
JM
5373 if (expr.original_code != C_MAYBE_CONST_EXPR)
5374 expr.original_code = ERROR_MARK;
6866c6e8 5375 /* Don't change EXPR.ORIGINAL_TYPE. */
27bf414c
JM
5376 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5377 "expected %<)%>");
5378 }
5379 break;
5380 case CPP_KEYWORD:
5381 switch (c_parser_peek_token (parser)->keyword)
5382 {
5383 case RID_FUNCTION_NAME:
5384 case RID_PRETTY_FUNCTION_NAME:
5385 case RID_C99_FUNCTION_NAME:
c2255bc4 5386 expr.value = fname_decl (loc,
3ba09659 5387 c_parser_peek_token (parser)->keyword,
27bf414c 5388 c_parser_peek_token (parser)->value);
27bf414c
JM
5389 c_parser_consume_token (parser);
5390 break;
5391 case RID_VA_ARG:
5392 c_parser_consume_token (parser);
5393 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5394 {
5395 expr.value = error_mark_node;
27bf414c
JM
5396 break;
5397 }
5398 e1 = c_parser_expr_no_commas (parser, NULL);
928c19bb 5399 e1.value = c_fully_fold (e1.value, false, NULL);
27bf414c
JM
5400 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
5401 {
5402 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5403 expr.value = error_mark_node;
27bf414c
JM
5404 break;
5405 }
72b5577d 5406 loc = c_parser_peek_token (parser)->location;
27bf414c
JM
5407 t1 = c_parser_type_name (parser);
5408 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5409 "expected %<)%>");
5410 if (t1 == NULL)
5411 {
5412 expr.value = error_mark_node;
27bf414c
JM
5413 }
5414 else
5415 {
928c19bb 5416 tree type_expr = NULL_TREE;
c2255bc4
AH
5417 expr.value = c_build_va_arg (loc, e1.value,
5418 groktypename (t1, &type_expr, NULL));
928c19bb
JM
5419 if (type_expr)
5420 {
5421 expr.value = build2 (C_MAYBE_CONST_EXPR,
5422 TREE_TYPE (expr.value), type_expr,
5423 expr.value);
5424 C_MAYBE_CONST_EXPR_NON_CONST (expr.value) = true;
5425 }
27bf414c
JM
5426 }
5427 break;
5428 case RID_OFFSETOF:
5429 c_parser_consume_token (parser);
5430 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5431 {
5432 expr.value = error_mark_node;
27bf414c
JM
5433 break;
5434 }
5435 t1 = c_parser_type_name (parser);
5436 if (t1 == NULL)
5437 {
5438 expr.value = error_mark_node;
27bf414c
JM
5439 break;
5440 }
5441 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
5442 {
5443 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5444 expr.value = error_mark_node;
27bf414c
JM
5445 break;
5446 }
5447 {
928c19bb 5448 tree type = groktypename (t1, NULL, NULL);
27bf414c
JM
5449 tree offsetof_ref;
5450 if (type == error_mark_node)
5451 offsetof_ref = error_mark_node;
5452 else
c2255bc4
AH
5453 {
5454 offsetof_ref = build1 (INDIRECT_REF, type, null_pointer_node);
5455 SET_EXPR_LOCATION (offsetof_ref, loc);
5456 }
27bf414c
JM
5457 /* Parse the second argument to __builtin_offsetof. We
5458 must have one identifier, and beyond that we want to
5459 accept sub structure and sub array references. */
5460 if (c_parser_next_token_is (parser, CPP_NAME))
5461 {
5462 offsetof_ref = build_component_ref
c2255bc4 5463 (loc, offsetof_ref, c_parser_peek_token (parser)->value);
27bf414c
JM
5464 c_parser_consume_token (parser);
5465 while (c_parser_next_token_is (parser, CPP_DOT)
5466 || c_parser_next_token_is (parser,
634b5df5
JJ
5467 CPP_OPEN_SQUARE)
5468 || c_parser_next_token_is (parser,
5469 CPP_DEREF))
27bf414c 5470 {
634b5df5
JJ
5471 if (c_parser_next_token_is (parser, CPP_DEREF))
5472 {
5473 loc = c_parser_peek_token (parser)->location;
c2255bc4
AH
5474 offsetof_ref = build_array_ref (loc,
5475 offsetof_ref,
5476 integer_zero_node);
634b5df5
JJ
5477 goto do_dot;
5478 }
5479 else if (c_parser_next_token_is (parser, CPP_DOT))
27bf414c 5480 {
634b5df5 5481 do_dot:
27bf414c
JM
5482 c_parser_consume_token (parser);
5483 if (c_parser_next_token_is_not (parser,
5484 CPP_NAME))
5485 {
5486 c_parser_error (parser, "expected identifier");
5487 break;
5488 }
5489 offsetof_ref = build_component_ref
c2255bc4 5490 (loc, offsetof_ref,
27bf414c
JM
5491 c_parser_peek_token (parser)->value);
5492 c_parser_consume_token (parser);
5493 }
5494 else
5495 {
5496 tree idx;
6a3799eb 5497 loc = c_parser_peek_token (parser)->location;
27bf414c
JM
5498 c_parser_consume_token (parser);
5499 idx = c_parser_expression (parser).value;
928c19bb 5500 idx = c_fully_fold (idx, false, NULL);
27bf414c
JM
5501 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
5502 "expected %<]%>");
c2255bc4 5503 offsetof_ref = build_array_ref (loc, offsetof_ref, idx);
27bf414c
JM
5504 }
5505 }
5506 }
5507 else
5508 c_parser_error (parser, "expected identifier");
5509 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5510 "expected %<)%>");
6d4d7b0e 5511 expr.value = fold_offsetof (offsetof_ref, NULL_TREE);
27bf414c
JM
5512 }
5513 break;
5514 case RID_CHOOSE_EXPR:
5515 c_parser_consume_token (parser);
5516 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5517 {
5518 expr.value = error_mark_node;
27bf414c
JM
5519 break;
5520 }
c7412148 5521 loc = c_parser_peek_token (parser)->location;
27bf414c
JM
5522 e1 = c_parser_expr_no_commas (parser, NULL);
5523 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
5524 {
5525 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5526 expr.value = error_mark_node;
27bf414c
JM
5527 break;
5528 }
5529 e2 = c_parser_expr_no_commas (parser, NULL);
5530 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
5531 {
5532 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5533 expr.value = error_mark_node;
27bf414c
JM
5534 break;
5535 }
5536 e3 = c_parser_expr_no_commas (parser, NULL);
5537 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5538 "expected %<)%>");
5539 {
5540 tree c;
5541
4d84fe7c 5542 c = e1.value;
928c19bb
JM
5543 if (TREE_CODE (c) != INTEGER_CST
5544 || !INTEGRAL_TYPE_P (TREE_TYPE (c)))
3ba09659
AH
5545 error_at (loc,
5546 "first argument to %<__builtin_choose_expr%> not"
5547 " a constant");
928c19bb 5548 constant_expression_warning (c);
27bf414c
JM
5549 expr = integer_zerop (c) ? e3 : e2;
5550 }
5551 break;
5552 case RID_TYPES_COMPATIBLE_P:
5553 c_parser_consume_token (parser);
5554 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5555 {
5556 expr.value = error_mark_node;
27bf414c
JM
5557 break;
5558 }
5559 t1 = c_parser_type_name (parser);
5560 if (t1 == NULL)
5561 {
5562 expr.value = error_mark_node;
27bf414c
JM
5563 break;
5564 }
5565 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
5566 {
5567 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5568 expr.value = error_mark_node;
27bf414c
JM
5569 break;
5570 }
5571 t2 = c_parser_type_name (parser);
5572 if (t2 == NULL)
5573 {
5574 expr.value = error_mark_node;
27bf414c
JM
5575 break;
5576 }
5577 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5578 "expected %<)%>");
5579 {
5580 tree e1, e2;
5581
928c19bb
JM
5582 e1 = TYPE_MAIN_VARIANT (groktypename (t1, NULL, NULL));
5583 e2 = TYPE_MAIN_VARIANT (groktypename (t2, NULL, NULL));
27bf414c
JM
5584
5585 expr.value = comptypes (e1, e2)
5586 ? build_int_cst (NULL_TREE, 1)
5587 : build_int_cst (NULL_TREE, 0);
27bf414c
JM
5588 }
5589 break;
5590 case RID_AT_SELECTOR:
5591 gcc_assert (c_dialect_objc ());
5592 c_parser_consume_token (parser);
5593 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5594 {
5595 expr.value = error_mark_node;
27bf414c
JM
5596 break;
5597 }
5598 {
5599 tree sel = c_parser_objc_selector_arg (parser);
5600 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5601 "expected %<)%>");
c2255bc4 5602 expr.value = objc_build_selector_expr (loc, sel);
27bf414c
JM
5603 }
5604 break;
5605 case RID_AT_PROTOCOL:
5606 gcc_assert (c_dialect_objc ());
5607 c_parser_consume_token (parser);
5608 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5609 {
5610 expr.value = error_mark_node;
27bf414c
JM
5611 break;
5612 }
5613 if (c_parser_next_token_is_not (parser, CPP_NAME))
5614 {
5615 c_parser_error (parser, "expected identifier");
5616 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5617 expr.value = error_mark_node;
27bf414c
JM
5618 break;
5619 }
5620 {
5621 tree id = c_parser_peek_token (parser)->value;
5622 c_parser_consume_token (parser);
5623 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5624 "expected %<)%>");
5625 expr.value = objc_build_protocol_expr (id);
27bf414c
JM
5626 }
5627 break;
5628 case RID_AT_ENCODE:
5629 /* Extension to support C-structures in the archiver. */
5630 gcc_assert (c_dialect_objc ());
5631 c_parser_consume_token (parser);
5632 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5633 {
5634 expr.value = error_mark_node;
27bf414c
JM
5635 break;
5636 }
5637 t1 = c_parser_type_name (parser);
5638 if (t1 == NULL)
5639 {
5640 expr.value = error_mark_node;
27bf414c
JM
5641 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5642 break;
5643 }
5644 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5645 "expected %<)%>");
5646 {
928c19bb 5647 tree type = groktypename (t1, NULL, NULL);
27bf414c 5648 expr.value = objc_build_encode_expr (type);
27bf414c
JM
5649 }
5650 break;
5651 default:
5652 c_parser_error (parser, "expected expression");
5653 expr.value = error_mark_node;
27bf414c
JM
5654 break;
5655 }
5656 break;
5657 case CPP_OPEN_SQUARE:
5658 if (c_dialect_objc ())
5659 {
5660 tree receiver, args;
5661 c_parser_consume_token (parser);
5662 receiver = c_parser_objc_receiver (parser);
5663 args = c_parser_objc_message_args (parser);
5664 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
5665 "expected %<]%>");
5666 expr.value = objc_build_message_expr (build_tree_list (receiver,
5667 args));
27bf414c
JM
5668 break;
5669 }
5670 /* Else fall through to report error. */
5671 default:
5672 c_parser_error (parser, "expected expression");
5673 expr.value = error_mark_node;
27bf414c
JM
5674 break;
5675 }
c2255bc4 5676 return c_parser_postfix_expression_after_primary (parser, loc, expr);
27bf414c
JM
5677}
5678
5679/* Parse a postfix expression after a parenthesized type name: the
5680 brace-enclosed initializer of a compound literal, possibly followed
5681 by some postfix operators. This is separate because it is not
5682 possible to tell until after the type name whether a cast
5683 expression has a cast or a compound literal, or whether the operand
5684 of sizeof is a parenthesized type name or starts with a compound
24b97832
ILT
5685 literal. TYPE_LOC is the location where TYPE_NAME starts--the
5686 location of the first token after the parentheses around the type
5687 name. */
27bf414c
JM
5688
5689static struct c_expr
5690c_parser_postfix_expression_after_paren_type (c_parser *parser,
24b97832
ILT
5691 struct c_type_name *type_name,
5692 location_t type_loc)
27bf414c
JM
5693{
5694 tree type;
5695 struct c_expr init;
928c19bb 5696 bool non_const;
27bf414c 5697 struct c_expr expr;
c7412148 5698 location_t start_loc;
928c19bb
JM
5699 tree type_expr = NULL_TREE;
5700 bool type_expr_const = true;
c2255bc4 5701 check_compound_literal_type (type_loc, type_name);
27bf414c 5702 start_init (NULL_TREE, NULL, 0);
928c19bb 5703 type = groktypename (type_name, &type_expr, &type_expr_const);
c7412148 5704 start_loc = c_parser_peek_token (parser)->location;
85cad37c 5705 if (type != error_mark_node && C_TYPE_VARIABLE_SIZE (type))
27bf414c 5706 {
24b97832 5707 error_at (type_loc, "compound literal has variable size");
27bf414c
JM
5708 type = error_mark_node;
5709 }
5710 init = c_parser_braced_init (parser, type, false);
5711 finish_init ();
5712 maybe_warn_string_init (type, init);
5713
fcf73884 5714 if (!flag_isoc99)
509c9d60 5715 pedwarn (start_loc, OPT_pedantic, "ISO C90 forbids compound literals");
928c19bb
JM
5716 non_const = ((init.value && TREE_CODE (init.value) == CONSTRUCTOR)
5717 ? CONSTRUCTOR_NON_CONST (init.value)
5718 : init.original_code == C_MAYBE_CONST_EXPR);
5719 non_const |= !type_expr_const;
c2255bc4 5720 expr.value = build_compound_literal (start_loc, type, init.value, non_const);
27bf414c 5721 expr.original_code = ERROR_MARK;
6866c6e8 5722 expr.original_type = NULL;
928c19bb
JM
5723 if (type_expr)
5724 {
5725 if (TREE_CODE (expr.value) == C_MAYBE_CONST_EXPR)
5726 {
5727 gcc_assert (C_MAYBE_CONST_EXPR_PRE (expr.value) == NULL_TREE);
5728 C_MAYBE_CONST_EXPR_PRE (expr.value) = type_expr;
5729 }
5730 else
5731 {
5732 gcc_assert (!non_const);
5733 expr.value = build2 (C_MAYBE_CONST_EXPR, type,
5734 type_expr, expr.value);
5735 }
5736 }
c2255bc4 5737 return c_parser_postfix_expression_after_primary (parser, start_loc, expr);
27bf414c
JM
5738}
5739
5740/* Parse a postfix expression after the initial primary or compound
c2255bc4
AH
5741 literal; that is, parse a series of postfix operators.
5742
5743 EXPR_LOC is the location of the primary expression. */
27bf414c
JM
5744
5745static struct c_expr
5746c_parser_postfix_expression_after_primary (c_parser *parser,
c2255bc4 5747 location_t expr_loc,
27bf414c
JM
5748 struct c_expr expr)
5749{
928c19bb 5750 struct c_expr orig_expr;
bbbbb16a
ILT
5751 tree ident, idx;
5752 VEC(tree,gc) *exprlist;
5753 VEC(tree,gc) *origtypes;
27bf414c
JM
5754 while (true)
5755 {
c2255bc4 5756 location_t op_loc = c_parser_peek_token (parser)->location;
27bf414c
JM
5757 switch (c_parser_peek_token (parser)->type)
5758 {
5759 case CPP_OPEN_SQUARE:
5760 /* Array reference. */
5761 c_parser_consume_token (parser);
5762 idx = c_parser_expression (parser).value;
5763 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
5764 "expected %<]%>");
c2255bc4 5765 expr.value = build_array_ref (op_loc, expr.value, idx);
27bf414c 5766 expr.original_code = ERROR_MARK;
6866c6e8 5767 expr.original_type = NULL;
27bf414c
JM
5768 break;
5769 case CPP_OPEN_PAREN:
5770 /* Function call. */
5771 c_parser_consume_token (parser);
5772 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
bbbbb16a 5773 exprlist = NULL;
27bf414c 5774 else
bbbbb16a 5775 exprlist = c_parser_expr_list (parser, true, false, &origtypes);
27bf414c
JM
5776 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5777 "expected %<)%>");
928c19bb 5778 orig_expr = expr;
c2255bc4
AH
5779 /* FIXME diagnostics: Ideally we want the FUNCNAME, not the
5780 "(" after the FUNCNAME, which is what we have now. */
5781 expr.value = build_function_call_vec (op_loc, expr.value, exprlist,
bbbbb16a 5782 origtypes);
27bf414c 5783 expr.original_code = ERROR_MARK;
928c19bb
JM
5784 if (TREE_CODE (expr.value) == INTEGER_CST
5785 && TREE_CODE (orig_expr.value) == FUNCTION_DECL
5786 && DECL_BUILT_IN_CLASS (orig_expr.value) == BUILT_IN_NORMAL
5787 && DECL_FUNCTION_CODE (orig_expr.value) == BUILT_IN_CONSTANT_P)
5788 expr.original_code = C_MAYBE_CONST_EXPR;
6866c6e8 5789 expr.original_type = NULL;
bbbbb16a
ILT
5790 if (exprlist != NULL)
5791 {
c166b898
ILT
5792 release_tree_vector (exprlist);
5793 release_tree_vector (origtypes);
bbbbb16a 5794 }
27bf414c
JM
5795 break;
5796 case CPP_DOT:
5797 /* Structure element reference. */
5798 c_parser_consume_token (parser);
c2255bc4 5799 expr = default_function_array_conversion (expr_loc, expr);
27bf414c
JM
5800 if (c_parser_next_token_is (parser, CPP_NAME))
5801 ident = c_parser_peek_token (parser)->value;
5802 else
5803 {
5804 c_parser_error (parser, "expected identifier");
5805 expr.value = error_mark_node;
5806 expr.original_code = ERROR_MARK;
6866c6e8 5807 expr.original_type = NULL;
27bf414c
JM
5808 return expr;
5809 }
5810 c_parser_consume_token (parser);
c2255bc4 5811 expr.value = build_component_ref (op_loc, expr.value, ident);
27bf414c 5812 expr.original_code = ERROR_MARK;
6866c6e8
ILT
5813 if (TREE_CODE (expr.value) != COMPONENT_REF)
5814 expr.original_type = NULL;
5815 else
5816 {
5817 /* Remember the original type of a bitfield. */
5818 tree field = TREE_OPERAND (expr.value, 1);
5819 if (TREE_CODE (field) != FIELD_DECL)
5820 expr.original_type = NULL;
5821 else
5822 expr.original_type = DECL_BIT_FIELD_TYPE (field);
5823 }
27bf414c
JM
5824 break;
5825 case CPP_DEREF:
5826 /* Structure element reference. */
5827 c_parser_consume_token (parser);
c2255bc4 5828 expr = default_function_array_conversion (expr_loc, expr);
27bf414c
JM
5829 if (c_parser_next_token_is (parser, CPP_NAME))
5830 ident = c_parser_peek_token (parser)->value;
5831 else
5832 {
5833 c_parser_error (parser, "expected identifier");
5834 expr.value = error_mark_node;
5835 expr.original_code = ERROR_MARK;
6866c6e8 5836 expr.original_type = NULL;
27bf414c
JM
5837 return expr;
5838 }
5839 c_parser_consume_token (parser);
c2255bc4
AH
5840 expr.value = build_component_ref (op_loc,
5841 build_indirect_ref (op_loc,
c9f9eb5d
AH
5842 expr.value,
5843 "->"),
6a3799eb 5844 ident);
27bf414c 5845 expr.original_code = ERROR_MARK;
6866c6e8
ILT
5846 if (TREE_CODE (expr.value) != COMPONENT_REF)
5847 expr.original_type = NULL;
5848 else
5849 {
5850 /* Remember the original type of a bitfield. */
5851 tree field = TREE_OPERAND (expr.value, 1);
5852 if (TREE_CODE (field) != FIELD_DECL)
5853 expr.original_type = NULL;
5854 else
5855 expr.original_type = DECL_BIT_FIELD_TYPE (field);
5856 }
27bf414c
JM
5857 break;
5858 case CPP_PLUS_PLUS:
5859 /* Postincrement. */
5860 c_parser_consume_token (parser);
c2255bc4
AH
5861 expr = default_function_array_conversion (expr_loc, expr);
5862 expr.value = build_unary_op (op_loc,
c9f9eb5d 5863 POSTINCREMENT_EXPR, expr.value, 0);
27bf414c 5864 expr.original_code = ERROR_MARK;
6866c6e8 5865 expr.original_type = NULL;
27bf414c
JM
5866 break;
5867 case CPP_MINUS_MINUS:
5868 /* Postdecrement. */
5869 c_parser_consume_token (parser);
c2255bc4
AH
5870 expr = default_function_array_conversion (expr_loc, expr);
5871 expr.value = build_unary_op (op_loc,
c9f9eb5d 5872 POSTDECREMENT_EXPR, expr.value, 0);
27bf414c 5873 expr.original_code = ERROR_MARK;
6866c6e8 5874 expr.original_type = NULL;
27bf414c
JM
5875 break;
5876 default:
5877 return expr;
5878 }
5879 }
5880}
5881
5882/* Parse an expression (C90 6.3.17, C99 6.5.17).
5883
5884 expression:
5885 assignment-expression
5886 expression , assignment-expression
5887*/
5888
5889static struct c_expr
5890c_parser_expression (c_parser *parser)
5891{
5892 struct c_expr expr;
5893 expr = c_parser_expr_no_commas (parser, NULL);
5894 while (c_parser_next_token_is (parser, CPP_COMMA))
5895 {
5896 struct c_expr next;
c2255bc4
AH
5897 location_t loc = c_parser_peek_token (parser)->location;
5898 location_t expr_loc;
27bf414c 5899 c_parser_consume_token (parser);
c2255bc4 5900 expr_loc = c_parser_peek_token (parser)->location;
27bf414c 5901 next = c_parser_expr_no_commas (parser, NULL);
c2255bc4
AH
5902 next = default_function_array_conversion (expr_loc, next);
5903 expr.value = build_compound_expr (loc, expr.value, next.value);
27bf414c 5904 expr.original_code = COMPOUND_EXPR;
81f40b79 5905 expr.original_type = next.original_type;
27bf414c
JM
5906 }
5907 return expr;
5908}
5909
46bdb9cf
JM
5910/* Parse an expression and convert functions or arrays to
5911 pointers. */
5912
5913static struct c_expr
5914c_parser_expression_conv (c_parser *parser)
5915{
5916 struct c_expr expr;
c2255bc4 5917 location_t loc = c_parser_peek_token (parser)->location;
46bdb9cf 5918 expr = c_parser_expression (parser);
c2255bc4 5919 expr = default_function_array_conversion (loc, expr);
46bdb9cf
JM
5920 return expr;
5921}
5922
5923/* Parse a non-empty list of expressions. If CONVERT_P, convert
928c19bb 5924 functions and arrays to pointers. If FOLD_P, fold the expressions.
27bf414c
JM
5925
5926 nonempty-expr-list:
5927 assignment-expression
5928 nonempty-expr-list , assignment-expression
5929*/
5930
bbbbb16a
ILT
5931static VEC(tree,gc) *
5932c_parser_expr_list (c_parser *parser, bool convert_p, bool fold_p,
5933 VEC(tree,gc) **p_orig_types)
27bf414c 5934{
bbbbb16a
ILT
5935 VEC(tree,gc) *ret;
5936 VEC(tree,gc) *orig_types;
27bf414c 5937 struct c_expr expr;
c2255bc4 5938 location_t loc = c_parser_peek_token (parser)->location;
bbbbb16a 5939
c166b898 5940 ret = make_tree_vector ();
bbbbb16a
ILT
5941 if (p_orig_types == NULL)
5942 orig_types = NULL;
5943 else
c166b898 5944 orig_types = make_tree_vector ();
bbbbb16a 5945
27bf414c 5946 expr = c_parser_expr_no_commas (parser, NULL);
46bdb9cf 5947 if (convert_p)
c2255bc4 5948 expr = default_function_array_conversion (loc, expr);
928c19bb
JM
5949 if (fold_p)
5950 expr.value = c_fully_fold (expr.value, false, NULL);
bbbbb16a
ILT
5951 VEC_quick_push (tree, ret, expr.value);
5952 if (orig_types != NULL)
5953 VEC_quick_push (tree, orig_types, expr.original_type);
27bf414c
JM
5954 while (c_parser_next_token_is (parser, CPP_COMMA))
5955 {
5956 c_parser_consume_token (parser);
c2255bc4 5957 loc = c_parser_peek_token (parser)->location;
27bf414c 5958 expr = c_parser_expr_no_commas (parser, NULL);
46bdb9cf 5959 if (convert_p)
c2255bc4 5960 expr = default_function_array_conversion (loc, expr);
928c19bb
JM
5961 if (fold_p)
5962 expr.value = c_fully_fold (expr.value, false, NULL);
bbbbb16a
ILT
5963 VEC_safe_push (tree, gc, ret, expr.value);
5964 if (orig_types != NULL)
5965 VEC_safe_push (tree, gc, orig_types, expr.original_type);
27bf414c 5966 }
bbbbb16a
ILT
5967 if (orig_types != NULL)
5968 *p_orig_types = orig_types;
27bf414c
JM
5969 return ret;
5970}
27bf414c
JM
5971\f
5972/* Parse Objective-C-specific constructs. */
5973
5974/* Parse an objc-class-definition.
5975
5976 objc-class-definition:
5977 @interface identifier objc-superclass[opt] objc-protocol-refs[opt]
5978 objc-class-instance-variables[opt] objc-methodprotolist @end
5979 @implementation identifier objc-superclass[opt]
5980 objc-class-instance-variables[opt]
5981 @interface identifier ( identifier ) objc-protocol-refs[opt]
5982 objc-methodprotolist @end
5983 @implementation identifier ( identifier )
5984
5985 objc-superclass:
5986 : identifier
5987
5988 "@interface identifier (" must start "@interface identifier (
5989 identifier ) ...": objc-methodprotolist in the first production may
0fa2e4df 5990 not start with a parenthesized identifier as a declarator of a data
27bf414c
JM
5991 definition with no declaration specifiers if the objc-superclass,
5992 objc-protocol-refs and objc-class-instance-variables are omitted. */
5993
5994static void
5995c_parser_objc_class_definition (c_parser *parser)
5996{
5997 bool iface_p;
5998 tree id1;
5999 tree superclass;
6000 if (c_parser_next_token_is_keyword (parser, RID_AT_INTERFACE))
6001 iface_p = true;
6002 else if (c_parser_next_token_is_keyword (parser, RID_AT_IMPLEMENTATION))
6003 iface_p = false;
6004 else
6005 gcc_unreachable ();
6006 c_parser_consume_token (parser);
6007 if (c_parser_next_token_is_not (parser, CPP_NAME))
6008 {
6009 c_parser_error (parser, "expected identifier");
6010 return;
6011 }
6012 id1 = c_parser_peek_token (parser)->value;
6013 c_parser_consume_token (parser);
6014 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
6015 {
6016 tree id2;
6017 tree proto = NULL_TREE;
6018 c_parser_consume_token (parser);
6019 if (c_parser_next_token_is_not (parser, CPP_NAME))
6020 {
6021 c_parser_error (parser, "expected identifier");
6022 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6023 return;
6024 }
6025 id2 = c_parser_peek_token (parser)->value;
6026 c_parser_consume_token (parser);
6027 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6028 if (!iface_p)
6029 {
6030 objc_start_category_implementation (id1, id2);
6031 return;
6032 }
6033 if (c_parser_next_token_is (parser, CPP_LESS))
6034 proto = c_parser_objc_protocol_refs (parser);
6035 objc_start_category_interface (id1, id2, proto);
6036 c_parser_objc_methodprotolist (parser);
6037 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
6038 objc_finish_interface ();
6039 return;
6040 }
6041 if (c_parser_next_token_is (parser, CPP_COLON))
6042 {
6043 c_parser_consume_token (parser);
6044 if (c_parser_next_token_is_not (parser, CPP_NAME))
6045 {
6046 c_parser_error (parser, "expected identifier");
6047 return;
6048 }
6049 superclass = c_parser_peek_token (parser)->value;
6050 c_parser_consume_token (parser);
6051 }
6052 else
6053 superclass = NULL_TREE;
6054 if (iface_p)
6055 {
6056 tree proto = NULL_TREE;
6057 if (c_parser_next_token_is (parser, CPP_LESS))
6058 proto = c_parser_objc_protocol_refs (parser);
6059 objc_start_class_interface (id1, superclass, proto);
6060 }
6061 else
6062 objc_start_class_implementation (id1, superclass);
6063 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
6064 c_parser_objc_class_instance_variables (parser);
6065 if (iface_p)
6066 {
6067 objc_continue_interface ();
6068 c_parser_objc_methodprotolist (parser);
6069 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
6070 objc_finish_interface ();
6071 }
6072 else
6073 {
6074 objc_continue_implementation ();
6075 return;
6076 }
6077}
6078
6079/* Parse objc-class-instance-variables.
6080
6081 objc-class-instance-variables:
6082 { objc-instance-variable-decl-list[opt] }
6083
6084 objc-instance-variable-decl-list:
6085 objc-visibility-spec
6086 objc-instance-variable-decl ;
6087 ;
6088 objc-instance-variable-decl-list objc-visibility-spec
6089 objc-instance-variable-decl-list objc-instance-variable-decl ;
6090 objc-instance-variable-decl-list ;
6091
6092 objc-visibility-spec:
6093 @private
6094 @protected
6095 @public
6096
6097 objc-instance-variable-decl:
6098 struct-declaration
6099*/
6100
6101static void
6102c_parser_objc_class_instance_variables (c_parser *parser)
6103{
6104 gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
6105 c_parser_consume_token (parser);
6106 while (c_parser_next_token_is_not (parser, CPP_EOF))
6107 {
6108 tree decls;
6109 /* Parse any stray semicolon. */
6110 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
6111 {
509c9d60
MLI
6112 pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic,
6113 "extra semicolon in struct or union specified");
27bf414c
JM
6114 c_parser_consume_token (parser);
6115 continue;
6116 }
6117 /* Stop if at the end of the instance variables. */
6118 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
6119 {
6120 c_parser_consume_token (parser);
6121 break;
6122 }
6123 /* Parse any objc-visibility-spec. */
eea1139b 6124 if (c_parser_next_token_is_keyword (parser, RID_PRIVATE))
27bf414c
JM
6125 {
6126 c_parser_consume_token (parser);
6127 objc_set_visibility (2);
6128 continue;
6129 }
eea1139b 6130 else if (c_parser_next_token_is_keyword (parser, RID_PROTECTED))
27bf414c
JM
6131 {
6132 c_parser_consume_token (parser);
6133 objc_set_visibility (0);
6134 continue;
6135 }
eea1139b 6136 else if (c_parser_next_token_is_keyword (parser, RID_PUBLIC))
27bf414c
JM
6137 {
6138 c_parser_consume_token (parser);
6139 objc_set_visibility (1);
6140 continue;
6141 }
bc4071dd
RH
6142 else if (c_parser_next_token_is (parser, CPP_PRAGMA))
6143 {
6144 c_parser_pragma (parser, pragma_external);
6145 continue;
6146 }
6147
27bf414c
JM
6148 /* Parse some comma-separated declarations. */
6149 decls = c_parser_struct_declaration (parser);
6150 {
6151 /* Comma-separated instance variables are chained together in
6152 reverse order; add them one by one. */
6153 tree ivar = nreverse (decls);
6154 for (; ivar; ivar = TREE_CHAIN (ivar))
6155 objc_add_instance_variable (copy_node (ivar));
6156 }
6157 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
6158 }
6159}
6160
6161/* Parse an objc-class-declaration.
6162
6163 objc-class-declaration:
6164 @class identifier-list ;
6165*/
6166
6167static void
6168c_parser_objc_class_declaration (c_parser *parser)
6169{
6170 tree list = NULL_TREE;
eea1139b 6171 gcc_assert (c_parser_next_token_is_keyword (parser, RID_CLASS));
27bf414c
JM
6172 c_parser_consume_token (parser);
6173 /* Any identifiers, including those declared as type names, are OK
6174 here. */
6175 while (true)
6176 {
6177 tree id;
6178 if (c_parser_next_token_is_not (parser, CPP_NAME))
6179 {
6180 c_parser_error (parser, "expected identifier");
6181 break;
6182 }
6183 id = c_parser_peek_token (parser)->value;
6184 list = chainon (list, build_tree_list (NULL_TREE, id));
6185 c_parser_consume_token (parser);
6186 if (c_parser_next_token_is (parser, CPP_COMMA))
6187 c_parser_consume_token (parser);
6188 else
6189 break;
6190 }
6191 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
6192 objc_declare_class (list);
6193}
6194
6195/* Parse an objc-alias-declaration.
6196
6197 objc-alias-declaration:
6198 @compatibility_alias identifier identifier ;
6199*/
6200
6201static void
6202c_parser_objc_alias_declaration (c_parser *parser)
6203{
6204 tree id1, id2;
6205 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_ALIAS));
6206 c_parser_consume_token (parser);
6207 if (c_parser_next_token_is_not (parser, CPP_NAME))
6208 {
6209 c_parser_error (parser, "expected identifier");
6210 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
6211 return;
6212 }
6213 id1 = c_parser_peek_token (parser)->value;
6214 c_parser_consume_token (parser);
6215 if (c_parser_next_token_is_not (parser, CPP_NAME))
6216 {
6217 c_parser_error (parser, "expected identifier");
6218 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
6219 return;
6220 }
6221 id2 = c_parser_peek_token (parser)->value;
6222 c_parser_consume_token (parser);
6223 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
6224 objc_declare_alias (id1, id2);
6225}
6226
6227/* Parse an objc-protocol-definition.
6228
6229 objc-protocol-definition:
6230 @protocol identifier objc-protocol-refs[opt] objc-methodprotolist @end
6231 @protocol identifier-list ;
6232
6233 "@protocol identifier ;" should be resolved as "@protocol
6234 identifier-list ;": objc-methodprotolist may not start with a
6235 semicolon in the first alternative if objc-protocol-refs are
6236 omitted. */
6237
6238static void
6239c_parser_objc_protocol_definition (c_parser *parser)
6240{
6241 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_PROTOCOL));
6242 c_parser_consume_token (parser);
6243 if (c_parser_next_token_is_not (parser, CPP_NAME))
6244 {
6245 c_parser_error (parser, "expected identifier");
6246 return;
6247 }
6248 if (c_parser_peek_2nd_token (parser)->type == CPP_COMMA
6249 || c_parser_peek_2nd_token (parser)->type == CPP_SEMICOLON)
6250 {
6251 tree list = NULL_TREE;
6252 /* Any identifiers, including those declared as type names, are
6253 OK here. */
6254 while (true)
6255 {
6256 tree id;
6257 if (c_parser_next_token_is_not (parser, CPP_NAME))
6258 {
6259 c_parser_error (parser, "expected identifier");
6260 break;
6261 }
6262 id = c_parser_peek_token (parser)->value;
6263 list = chainon (list, build_tree_list (NULL_TREE, id));
6264 c_parser_consume_token (parser);
6265 if (c_parser_next_token_is (parser, CPP_COMMA))
6266 c_parser_consume_token (parser);
6267 else
6268 break;
6269 }
6270 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
6271 objc_declare_protocols (list);
6272 }
6273 else
6274 {
6275 tree id = c_parser_peek_token (parser)->value;
6276 tree proto = NULL_TREE;
6277 c_parser_consume_token (parser);
6278 if (c_parser_next_token_is (parser, CPP_LESS))
6279 proto = c_parser_objc_protocol_refs (parser);
0bacb8c7 6280 parser->objc_pq_context = true;
27bf414c
JM
6281 objc_start_protocol (id, proto);
6282 c_parser_objc_methodprotolist (parser);
6283 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
0bacb8c7 6284 parser->objc_pq_context = false;
27bf414c
JM
6285 objc_finish_interface ();
6286 }
6287}
6288
6289/* Parse an objc-method-type.
6290
6291 objc-method-type:
6292 +
6293 -
6294*/
6295
6296static enum tree_code
6297c_parser_objc_method_type (c_parser *parser)
6298{
6299 switch (c_parser_peek_token (parser)->type)
6300 {
6301 case CPP_PLUS:
6302 c_parser_consume_token (parser);
6303 return PLUS_EXPR;
6304 case CPP_MINUS:
6305 c_parser_consume_token (parser);
6306 return MINUS_EXPR;
6307 default:
6308 gcc_unreachable ();
6309 }
6310}
6311
6312/* Parse an objc-method-definition.
6313
6314 objc-method-definition:
6315 objc-method-type objc-method-decl ;[opt] compound-statement
6316*/
6317
6318static void
6319c_parser_objc_method_definition (c_parser *parser)
6320{
6321 enum tree_code type = c_parser_objc_method_type (parser);
6322 tree decl;
6323 objc_set_method_type (type);
0bacb8c7 6324 parser->objc_pq_context = true;
27bf414c
JM
6325 decl = c_parser_objc_method_decl (parser);
6326 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
6327 {
6328 c_parser_consume_token (parser);
509c9d60
MLI
6329 pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic,
6330 "extra semicolon in method definition specified");
27bf414c 6331 }
8f078c08
AP
6332 if (!c_parser_next_token_is (parser, CPP_OPEN_BRACE))
6333 {
6334 c_parser_error (parser, "expected %<{%>");
6335 return;
6336 }
0bacb8c7 6337 parser->objc_pq_context = false;
27bf414c
JM
6338 objc_start_method_definition (decl);
6339 add_stmt (c_parser_compound_statement (parser));
6340 objc_finish_method_definition (current_function_decl);
6341}
6342
6343/* Parse an objc-methodprotolist.
6344
6345 objc-methodprotolist:
6346 empty
6347 objc-methodprotolist objc-methodproto
6348 objc-methodprotolist declaration
6349 objc-methodprotolist ;
6350
6351 The declaration is a data definition, which may be missing
6352 declaration specifiers under the same rules and diagnostics as
6353 other data definitions outside functions, and the stray semicolon
6354 is diagnosed the same way as a stray semicolon outside a
6355 function. */
6356
6357static void
6358c_parser_objc_methodprotolist (c_parser *parser)
6359{
6360 while (true)
6361 {
6362 /* The list is terminated by @end. */
6363 switch (c_parser_peek_token (parser)->type)
6364 {
6365 case CPP_SEMICOLON:
509c9d60
MLI
6366 pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic,
6367 "ISO C does not allow extra %<;%> outside of a function");
27bf414c
JM
6368 c_parser_consume_token (parser);
6369 break;
6370 case CPP_PLUS:
6371 case CPP_MINUS:
6372 c_parser_objc_methodproto (parser);
6373 break;
b9b58168
RH
6374 case CPP_PRAGMA:
6375 c_parser_pragma (parser, pragma_external);
6376 break;
27bf414c
JM
6377 case CPP_EOF:
6378 return;
6379 default:
6380 if (c_parser_next_token_is_keyword (parser, RID_AT_END))
6381 return;
6382 c_parser_declaration_or_fndef (parser, false, true, false, true);
6383 break;
6384 }
6385 }
6386}
6387
6388/* Parse an objc-methodproto.
6389
6390 objc-methodproto:
6391 objc-method-type objc-method-decl ;
6392*/
6393
6394static void
6395c_parser_objc_methodproto (c_parser *parser)
6396{
6397 enum tree_code type = c_parser_objc_method_type (parser);
6398 tree decl;
6399 objc_set_method_type (type);
6400 /* Remember protocol qualifiers in prototypes. */
0bacb8c7 6401 parser->objc_pq_context = true;
27bf414c
JM
6402 decl = c_parser_objc_method_decl (parser);
6403 /* Forget protocol qualifiers here. */
0bacb8c7 6404 parser->objc_pq_context = false;
27bf414c
JM
6405 objc_add_method_declaration (decl);
6406 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
6407}
6408
6409/* Parse an objc-method-decl.
6410
6411 objc-method-decl:
6412 ( objc-type-name ) objc-selector
6413 objc-selector
6414 ( objc-type-name ) objc-keyword-selector objc-optparmlist
6415 objc-keyword-selector objc-optparmlist
6416
6417 objc-keyword-selector:
6418 objc-keyword-decl
6419 objc-keyword-selector objc-keyword-decl
6420
6421 objc-keyword-decl:
6422 objc-selector : ( objc-type-name ) identifier
6423 objc-selector : identifier
6424 : ( objc-type-name ) identifier
6425 : identifier
6426
6427 objc-optparmlist:
6428 objc-optparms objc-optellipsis
6429
6430 objc-optparms:
6431 empty
6432 objc-opt-parms , parameter-declaration
6433
6434 objc-optellipsis:
6435 empty
6436 , ...
6437*/
6438
6439static tree
6440c_parser_objc_method_decl (c_parser *parser)
6441{
6442 tree type = NULL_TREE;
6443 tree sel;
6444 tree parms = NULL_TREE;
dbb74365
RS
6445 bool ellipsis = false;
6446
27bf414c
JM
6447 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
6448 {
6449 c_parser_consume_token (parser);
6450 type = c_parser_objc_type_name (parser);
6451 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6452 }
6453 sel = c_parser_objc_selector (parser);
6454 /* If there is no selector, or a colon follows, we have an
6455 objc-keyword-selector. If there is a selector, and a colon does
6456 not follow, that selector ends the objc-method-decl. */
6457 if (!sel || c_parser_next_token_is (parser, CPP_COLON))
6458 {
6459 tree tsel = sel;
6460 tree list = NULL_TREE;
27bf414c
JM
6461 while (true)
6462 {
6463 tree atype = NULL_TREE, id, keyworddecl;
6464 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
6465 break;
6466 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
6467 {
6468 c_parser_consume_token (parser);
6469 atype = c_parser_objc_type_name (parser);
6470 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6471 "expected %<)%>");
6472 }
6473 if (c_parser_next_token_is_not (parser, CPP_NAME))
6474 {
6475 c_parser_error (parser, "expected identifier");
6476 return error_mark_node;
6477 }
6478 id = c_parser_peek_token (parser)->value;
6479 c_parser_consume_token (parser);
6480 keyworddecl = objc_build_keyword_decl (tsel, atype, id);
6481 list = chainon (list, keyworddecl);
6482 tsel = c_parser_objc_selector (parser);
6483 if (!tsel && c_parser_next_token_is_not (parser, CPP_COLON))
6484 break;
6485 }
6486 /* Parse the optional parameter list. Optional Objective-C
6487 method parameters follow the C syntax, and may include '...'
6488 to denote a variable number of arguments. */
6489 parms = make_node (TREE_LIST);
27bf414c
JM
6490 while (c_parser_next_token_is (parser, CPP_COMMA))
6491 {
6492 struct c_parm *parm;
6493 c_parser_consume_token (parser);
6494 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
6495 {
6496 ellipsis = true;
6497 c_parser_consume_token (parser);
6498 break;
6499 }
6500 parm = c_parser_parameter_declaration (parser, NULL_TREE);
6501 if (parm == NULL)
6502 break;
6503 parms = chainon (parms,
6504 build_tree_list (NULL_TREE, grokparm (parm)));
6505 }
27bf414c
JM
6506 sel = list;
6507 }
dbb74365 6508 return objc_build_method_signature (type, sel, parms, ellipsis);
27bf414c
JM
6509}
6510
6511/* Parse an objc-type-name.
6512
6513 objc-type-name:
6514 objc-type-qualifiers[opt] type-name
6515 objc-type-qualifiers[opt]
6516
6517 objc-type-qualifiers:
6518 objc-type-qualifier
6519 objc-type-qualifiers objc-type-qualifier
6520
6521 objc-type-qualifier: one of
6522 in out inout bycopy byref oneway
6523*/
6524
6525static tree
6526c_parser_objc_type_name (c_parser *parser)
6527{
6528 tree quals = NULL_TREE;
d75d71e0 6529 struct c_type_name *type_name = NULL;
27bf414c
JM
6530 tree type = NULL_TREE;
6531 while (true)
6532 {
6533 c_token *token = c_parser_peek_token (parser);
6534 if (token->type == CPP_KEYWORD
6535 && (token->keyword == RID_IN
6536 || token->keyword == RID_OUT
6537 || token->keyword == RID_INOUT
6538 || token->keyword == RID_BYCOPY
6539 || token->keyword == RID_BYREF
6540 || token->keyword == RID_ONEWAY))
6541 {
6542 quals = chainon (quals, build_tree_list (NULL_TREE, token->value));
6543 c_parser_consume_token (parser);
6544 }
6545 else
6546 break;
6547 }
6548 if (c_parser_next_token_starts_typename (parser))
d75d71e0
ILT
6549 type_name = c_parser_type_name (parser);
6550 if (type_name)
928c19bb 6551 type = groktypename (type_name, NULL, NULL);
27bf414c
JM
6552 return build_tree_list (quals, type);
6553}
6554
6555/* Parse objc-protocol-refs.
6556
6557 objc-protocol-refs:
6558 < identifier-list >
6559*/
6560
6561static tree
6562c_parser_objc_protocol_refs (c_parser *parser)
6563{
6564 tree list = NULL_TREE;
6565 gcc_assert (c_parser_next_token_is (parser, CPP_LESS));
6566 c_parser_consume_token (parser);
6567 /* Any identifiers, including those declared as type names, are OK
6568 here. */
6569 while (true)
6570 {
6571 tree id;
6572 if (c_parser_next_token_is_not (parser, CPP_NAME))
6573 {
6574 c_parser_error (parser, "expected identifier");
6575 break;
6576 }
6577 id = c_parser_peek_token (parser)->value;
6578 list = chainon (list, build_tree_list (NULL_TREE, id));
6579 c_parser_consume_token (parser);
6580 if (c_parser_next_token_is (parser, CPP_COMMA))
6581 c_parser_consume_token (parser);
6582 else
6583 break;
6584 }
6585 c_parser_require (parser, CPP_GREATER, "expected %<>%>");
6586 return list;
6587}
6588
6589/* Parse an objc-try-catch-statement.
6590
6591 objc-try-catch-statement:
6592 @try compound-statement objc-catch-list[opt]
6593 @try compound-statement objc-catch-list[opt] @finally compound-statement
6594
6595 objc-catch-list:
6596 @catch ( parameter-declaration ) compound-statement
6597 objc-catch-list @catch ( parameter-declaration ) compound-statement
6598*/
6599
6600static void
6601c_parser_objc_try_catch_statement (c_parser *parser)
6602{
6603 location_t loc;
6604 tree stmt;
eea1139b 6605 gcc_assert (c_parser_next_token_is_keyword (parser, RID_TRY));
27bf414c
JM
6606 c_parser_consume_token (parser);
6607 loc = c_parser_peek_token (parser)->location;
6608 stmt = c_parser_compound_statement (parser);
6609 objc_begin_try_stmt (loc, stmt);
eea1139b 6610 while (c_parser_next_token_is_keyword (parser, RID_CATCH))
27bf414c
JM
6611 {
6612 struct c_parm *parm;
6613 c_parser_consume_token (parser);
6614 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6615 break;
6616 parm = c_parser_parameter_declaration (parser, NULL_TREE);
6617 if (parm == NULL)
6618 {
6619 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6620 break;
6621 }
6622 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6623 objc_begin_catch_clause (grokparm (parm));
6624 if (c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
6625 c_parser_compound_statement_nostart (parser);
6626 objc_finish_catch_clause ();
6627 }
6628 if (c_parser_next_token_is_keyword (parser, RID_AT_FINALLY))
6629 {
6630 location_t finloc;
6631 tree finstmt;
6632 c_parser_consume_token (parser);
6633 finloc = c_parser_peek_token (parser)->location;
6634 finstmt = c_parser_compound_statement (parser);
6635 objc_build_finally_clause (finloc, finstmt);
6636 }
6637 objc_finish_try_stmt ();
6638}
6639
6640/* Parse an objc-synchronized-statement.
6641
6642 objc-synchronized-statement:
6643 @synchronized ( expression ) compound-statement
6644*/
6645
6646static void
6647c_parser_objc_synchronized_statement (c_parser *parser)
6648{
6649 location_t loc;
6650 tree expr, stmt;
6651 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_SYNCHRONIZED));
6652 c_parser_consume_token (parser);
6653 loc = c_parser_peek_token (parser)->location;
6654 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6655 {
6656 expr = c_parser_expression (parser).value;
928c19bb 6657 expr = c_fully_fold (expr, false, NULL);
27bf414c
JM
6658 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6659 }
6660 else
6661 expr = error_mark_node;
6662 stmt = c_parser_compound_statement (parser);
6663 objc_build_synchronized (loc, expr, stmt);
6664}
6665
6666/* Parse an objc-selector; return NULL_TREE without an error if the
6667 next token is not an objc-selector.
6668
6669 objc-selector:
6670 identifier
6671 one of
6672 enum struct union if else while do for switch case default
6673 break continue return goto asm sizeof typeof __alignof
6674 unsigned long const short volatile signed restrict _Complex
6675 in out inout bycopy byref oneway int char float double void _Bool
6676
6677 ??? Why this selection of keywords but not, for example, storage
6678 class specifiers? */
6679
6680static tree
6681c_parser_objc_selector (c_parser *parser)
6682{
6683 c_token *token = c_parser_peek_token (parser);
6684 tree value = token->value;
6685 if (token->type == CPP_NAME)
6686 {
6687 c_parser_consume_token (parser);
6688 return value;
6689 }
6690 if (token->type != CPP_KEYWORD)
6691 return NULL_TREE;
6692 switch (token->keyword)
6693 {
6694 case RID_ENUM:
6695 case RID_STRUCT:
6696 case RID_UNION:
6697 case RID_IF:
6698 case RID_ELSE:
6699 case RID_WHILE:
6700 case RID_DO:
6701 case RID_FOR:
6702 case RID_SWITCH:
6703 case RID_CASE:
6704 case RID_DEFAULT:
6705 case RID_BREAK:
6706 case RID_CONTINUE:
6707 case RID_RETURN:
6708 case RID_GOTO:
6709 case RID_ASM:
6710 case RID_SIZEOF:
6711 case RID_TYPEOF:
6712 case RID_ALIGNOF:
6713 case RID_UNSIGNED:
6714 case RID_LONG:
6715 case RID_CONST:
6716 case RID_SHORT:
6717 case RID_VOLATILE:
6718 case RID_SIGNED:
6719 case RID_RESTRICT:
6720 case RID_COMPLEX:
6721 case RID_IN:
6722 case RID_OUT:
6723 case RID_INOUT:
6724 case RID_BYCOPY:
6725 case RID_BYREF:
6726 case RID_ONEWAY:
6727 case RID_INT:
6728 case RID_CHAR:
6729 case RID_FLOAT:
6730 case RID_DOUBLE:
6731 case RID_VOID:
6732 case RID_BOOL:
6733 c_parser_consume_token (parser);
6734 return value;
6735 default:
6736 return NULL_TREE;
6737 }
6738}
6739
6740/* Parse an objc-selector-arg.
6741
6742 objc-selector-arg:
6743 objc-selector
6744 objc-keywordname-list
6745
6746 objc-keywordname-list:
6747 objc-keywordname
6748 objc-keywordname-list objc-keywordname
6749
6750 objc-keywordname:
6751 objc-selector :
6752 :
6753*/
6754
6755static tree
6756c_parser_objc_selector_arg (c_parser *parser)
6757{
6758 tree sel = c_parser_objc_selector (parser);
6759 tree list = NULL_TREE;
6760 if (sel && c_parser_next_token_is_not (parser, CPP_COLON))
6761 return sel;
6762 while (true)
6763 {
6764 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
6765 return list;
6766 list = chainon (list, build_tree_list (sel, NULL_TREE));
6767 sel = c_parser_objc_selector (parser);
6768 if (!sel && c_parser_next_token_is_not (parser, CPP_COLON))
6769 break;
6770 }
6771 return list;
6772}
6773
6774/* Parse an objc-receiver.
6775
6776 objc-receiver:
6777 expression
6778 class-name
6779 type-name
6780*/
6781
6782static tree
6783c_parser_objc_receiver (c_parser *parser)
6784{
6785 if (c_parser_peek_token (parser)->type == CPP_NAME
6786 && (c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME
6787 || c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME))
6788 {
6789 tree id = c_parser_peek_token (parser)->value;
6790 c_parser_consume_token (parser);
6791 return objc_get_class_reference (id);
6792 }
928c19bb 6793 return c_fully_fold (c_parser_expression (parser).value, false, NULL);
27bf414c
JM
6794}
6795
6796/* Parse objc-message-args.
6797
6798 objc-message-args:
6799 objc-selector
6800 objc-keywordarg-list
6801
6802 objc-keywordarg-list:
6803 objc-keywordarg
6804 objc-keywordarg-list objc-keywordarg
6805
6806 objc-keywordarg:
6807 objc-selector : objc-keywordexpr
6808 : objc-keywordexpr
6809*/
6810
6811static tree
6812c_parser_objc_message_args (c_parser *parser)
6813{
6814 tree sel = c_parser_objc_selector (parser);
6815 tree list = NULL_TREE;
6816 if (sel && c_parser_next_token_is_not (parser, CPP_COLON))
6817 return sel;
6818 while (true)
6819 {
6820 tree keywordexpr;
6821 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
6822 return list;
6823 keywordexpr = c_parser_objc_keywordexpr (parser);
6824 list = chainon (list, build_tree_list (sel, keywordexpr));
6825 sel = c_parser_objc_selector (parser);
6826 if (!sel && c_parser_next_token_is_not (parser, CPP_COLON))
6827 break;
6828 }
6829 return list;
6830}
6831
6832/* Parse an objc-keywordexpr.
6833
6834 objc-keywordexpr:
6835 nonempty-expr-list
6836*/
6837
6838static tree
6839c_parser_objc_keywordexpr (c_parser *parser)
6840{
bbbbb16a
ILT
6841 tree ret;
6842 VEC(tree,gc) *expr_list = c_parser_expr_list (parser, true, true, NULL);
6843 if (VEC_length (tree, expr_list) == 1)
27bf414c
JM
6844 {
6845 /* Just return the expression, remove a level of
6846 indirection. */
bbbbb16a 6847 ret = VEC_index (tree, expr_list, 0);
27bf414c
JM
6848 }
6849 else
6850 {
6851 /* We have a comma expression, we will collapse later. */
c166b898 6852 ret = build_tree_list_vec (expr_list);
27bf414c 6853 }
c166b898 6854 release_tree_vector (expr_list);
bbbbb16a 6855 return ret;
27bf414c
JM
6856}
6857
6858\f
953ff289
DN
6859/* Handle pragmas. Some OpenMP pragmas are associated with, and therefore
6860 should be considered, statements. ALLOW_STMT is true if we're within
6861 the context of a function and such pragmas are to be allowed. Returns
6862 true if we actually parsed such a pragma. */
27bf414c 6863
bc4071dd 6864static bool
953ff289 6865c_parser_pragma (c_parser *parser, enum pragma_context context)
bc4071dd
RH
6866{
6867 unsigned int id;
6868
6869 id = c_parser_peek_token (parser)->pragma_kind;
6870 gcc_assert (id != PRAGMA_NONE);
6871
6872 switch (id)
6873 {
953ff289
DN
6874 case PRAGMA_OMP_BARRIER:
6875 if (context != pragma_compound)
6876 {
6877 if (context == pragma_stmt)
6878 c_parser_error (parser, "%<#pragma omp barrier%> may only be "
6879 "used in compound statements");
6880 goto bad_stmt;
6881 }
6882 c_parser_omp_barrier (parser);
6883 return false;
6884
6885 case PRAGMA_OMP_FLUSH:
6886 if (context != pragma_compound)
6887 {
6888 if (context == pragma_stmt)
6889 c_parser_error (parser, "%<#pragma omp flush%> may only be "
6890 "used in compound statements");
6891 goto bad_stmt;
6892 }
6893 c_parser_omp_flush (parser);
6894 return false;
6895
a68ab351
JJ
6896 case PRAGMA_OMP_TASKWAIT:
6897 if (context != pragma_compound)
6898 {
6899 if (context == pragma_stmt)
6900 c_parser_error (parser, "%<#pragma omp taskwait%> may only be "
6901 "used in compound statements");
6902 goto bad_stmt;
6903 }
6904 c_parser_omp_taskwait (parser);
6905 return false;
6906
953ff289
DN
6907 case PRAGMA_OMP_THREADPRIVATE:
6908 c_parser_omp_threadprivate (parser);
6909 return false;
6910
6911 case PRAGMA_OMP_SECTION:
3ba09659
AH
6912 error_at (c_parser_peek_token (parser)->location,
6913 "%<#pragma omp section%> may only be used in "
6914 "%<#pragma omp sections%> construct");
953ff289
DN
6915 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
6916 return false;
6917
bc4071dd
RH
6918 case PRAGMA_GCC_PCH_PREPROCESS:
6919 c_parser_error (parser, "%<#pragma GCC pch_preprocess%> must be first");
6920 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
6921 return false;
6922
6923 default:
953ff289
DN
6924 if (id < PRAGMA_FIRST_EXTERNAL)
6925 {
6926 if (context == pragma_external)
6927 {
6928 bad_stmt:
6929 c_parser_error (parser, "expected declaration specifiers");
6930 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
6931 return false;
6932 }
6933 c_parser_omp_construct (parser);
6934 return true;
6935 }
bc4071dd
RH
6936 break;
6937 }
6938
6939 c_parser_consume_pragma (parser);
6940 c_invoke_pragma_handler (id);
27bf414c 6941
bc4071dd
RH
6942 /* Skip to EOL, but suppress any error message. Those will have been
6943 generated by the handler routine through calling error, as opposed
6944 to calling c_parser_error. */
6945 parser->error = true;
6946 c_parser_skip_to_pragma_eol (parser);
6947
6948 return false;
6949}
6950
6951/* The interface the pragma parsers have to the lexer. */
6952
6953enum cpp_ttype
6954pragma_lex (tree *value)
6955{
6956 c_token *tok = c_parser_peek_token (the_parser);
6957 enum cpp_ttype ret = tok->type;
6958
6959 *value = tok->value;
6960 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
6961 ret = CPP_EOF;
6962 else
6963 {
6964 if (ret == CPP_KEYWORD)
6965 ret = CPP_NAME;
6966 c_parser_consume_token (the_parser);
6967 }
6968
6969 return ret;
6970}
6971
6972static void
6973c_parser_pragma_pch_preprocess (c_parser *parser)
6974{
6975 tree name = NULL;
6976
6977 c_parser_consume_pragma (parser);
6978 if (c_parser_next_token_is (parser, CPP_STRING))
6979 {
6980 name = c_parser_peek_token (parser)->value;
6981 c_parser_consume_token (parser);
6982 }
6983 else
6984 c_parser_error (parser, "expected string literal");
6985 c_parser_skip_to_pragma_eol (parser);
6986
6987 if (name)
6988 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
6989}
953ff289
DN
6990\f
6991/* OpenMP 2.5 parsing routines. */
6992
6993/* Returns name of the next clause.
6994 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
6995 the token is not consumed. Otherwise appropriate pragma_omp_clause is
6996 returned and the token is consumed. */
6997
6998static pragma_omp_clause
6999c_parser_omp_clause_name (c_parser *parser)
7000{
7001 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
7002
7003 if (c_parser_next_token_is_keyword (parser, RID_IF))
7004 result = PRAGMA_OMP_CLAUSE_IF;
7005 else if (c_parser_next_token_is_keyword (parser, RID_DEFAULT))
7006 result = PRAGMA_OMP_CLAUSE_DEFAULT;
7007 else if (c_parser_next_token_is (parser, CPP_NAME))
7008 {
7009 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
7010
7011 switch (p[0])
7012 {
7013 case 'c':
a68ab351
JJ
7014 if (!strcmp ("collapse", p))
7015 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
7016 else if (!strcmp ("copyin", p))
953ff289
DN
7017 result = PRAGMA_OMP_CLAUSE_COPYIN;
7018 else if (!strcmp ("copyprivate", p))
7019 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
7020 break;
7021 case 'f':
7022 if (!strcmp ("firstprivate", p))
7023 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
7024 break;
7025 case 'l':
7026 if (!strcmp ("lastprivate", p))
7027 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
7028 break;
7029 case 'n':
7030 if (!strcmp ("nowait", p))
7031 result = PRAGMA_OMP_CLAUSE_NOWAIT;
7032 else if (!strcmp ("num_threads", p))
7033 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
7034 break;
7035 case 'o':
7036 if (!strcmp ("ordered", p))
7037 result = PRAGMA_OMP_CLAUSE_ORDERED;
7038 break;
7039 case 'p':
7040 if (!strcmp ("private", p))
7041 result = PRAGMA_OMP_CLAUSE_PRIVATE;
7042 break;
7043 case 'r':
7044 if (!strcmp ("reduction", p))
7045 result = PRAGMA_OMP_CLAUSE_REDUCTION;
7046 break;
7047 case 's':
7048 if (!strcmp ("schedule", p))
7049 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
7050 else if (!strcmp ("shared", p))
7051 result = PRAGMA_OMP_CLAUSE_SHARED;
7052 break;
a68ab351
JJ
7053 case 'u':
7054 if (!strcmp ("untied", p))
7055 result = PRAGMA_OMP_CLAUSE_UNTIED;
7056 break;
953ff289
DN
7057 }
7058 }
7059
7060 if (result != PRAGMA_OMP_CLAUSE_NONE)
7061 c_parser_consume_token (parser);
7062
7063 return result;
7064}
7065
7066/* Validate that a clause of the given type does not already exist. */
7067
7068static void
d75d71e0
ILT
7069check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
7070 const char *name)
953ff289
DN
7071{
7072 tree c;
7073
7074 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
aaf46ef9 7075 if (OMP_CLAUSE_CODE (c) == code)
953ff289 7076 {
c2255bc4
AH
7077 location_t loc = OMP_CLAUSE_LOCATION (c);
7078 error_at (loc, "too many %qs clauses", name);
953ff289
DN
7079 break;
7080 }
7081}
7082
7083/* OpenMP 2.5:
7084 variable-list:
7085 identifier
7086 variable-list , identifier
7087
c2255bc4
AH
7088 If KIND is nonzero, create the appropriate node and install the
7089 decl in OMP_CLAUSE_DECL and add the node to the head of the list.
7090 If KIND is nonzero, CLAUSE_LOC is the location of the clause.
953ff289
DN
7091
7092 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
7093 return the list created. */
7094
7095static tree
c2255bc4
AH
7096c_parser_omp_variable_list (c_parser *parser,
7097 location_t clause_loc,
7098 enum omp_clause_code kind,
aaf46ef9 7099 tree list)
953ff289
DN
7100{
7101 if (c_parser_next_token_is_not (parser, CPP_NAME)
7102 || c_parser_peek_token (parser)->id_kind != C_ID_ID)
7103 c_parser_error (parser, "expected identifier");
7104
7105 while (c_parser_next_token_is (parser, CPP_NAME)
7106 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
7107 {
7108 tree t = lookup_name (c_parser_peek_token (parser)->value);
7109
7110 if (t == NULL_TREE)
c2255bc4
AH
7111 undeclared_variable (c_parser_peek_token (parser)->location,
7112 c_parser_peek_token (parser)->value);
953ff289
DN
7113 else if (t == error_mark_node)
7114 ;
7115 else if (kind != 0)
7116 {
c2255bc4 7117 tree u = build_omp_clause (clause_loc, kind);
953ff289
DN
7118 OMP_CLAUSE_DECL (u) = t;
7119 OMP_CLAUSE_CHAIN (u) = list;
7120 list = u;
7121 }
7122 else
7123 list = tree_cons (t, NULL_TREE, list);
7124
7125 c_parser_consume_token (parser);
7126
7127 if (c_parser_next_token_is_not (parser, CPP_COMMA))
7128 break;
7129
7130 c_parser_consume_token (parser);
7131 }
7132
7133 return list;
7134}
7135
7136/* Similarly, but expect leading and trailing parenthesis. This is a very
7137 common case for omp clauses. */
7138
7139static tree
d75d71e0
ILT
7140c_parser_omp_var_list_parens (c_parser *parser, enum omp_clause_code kind,
7141 tree list)
953ff289 7142{
c2255bc4
AH
7143 /* The clauses location. */
7144 location_t loc = c_parser_peek_token (parser)->location;
7145
953ff289
DN
7146 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7147 {
c2255bc4 7148 list = c_parser_omp_variable_list (parser, loc, kind, list);
953ff289
DN
7149 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
7150 }
7151 return list;
7152}
7153
a68ab351
JJ
7154/* OpenMP 3.0:
7155 collapse ( constant-expression ) */
7156
7157static tree
7158c_parser_omp_clause_collapse (c_parser *parser, tree list)
7159{
7160 tree c, num = error_mark_node;
7161 HOST_WIDE_INT n;
7162 location_t loc;
7163
7164 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse");
7165
7166 loc = c_parser_peek_token (parser)->location;
7167 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7168 {
7169 num = c_parser_expr_no_commas (parser, NULL).value;
7170 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
7171 }
7172 if (num == error_mark_node)
7173 return list;
7174 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
7175 || !host_integerp (num, 0)
7176 || (n = tree_low_cst (num, 0)) <= 0
7177 || (int) n != n)
7178 {
3ba09659
AH
7179 error_at (loc,
7180 "collapse argument needs positive constant integer expression");
a68ab351
JJ
7181 return list;
7182 }
c2255bc4 7183 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
a68ab351
JJ
7184 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
7185 OMP_CLAUSE_CHAIN (c) = list;
7186 return c;
7187}
7188
953ff289
DN
7189/* OpenMP 2.5:
7190 copyin ( variable-list ) */
7191
7192static tree
7193c_parser_omp_clause_copyin (c_parser *parser, tree list)
7194{
7195 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_COPYIN, list);
7196}
7197
7198/* OpenMP 2.5:
7199 copyprivate ( variable-list ) */
7200
7201static tree
7202c_parser_omp_clause_copyprivate (c_parser *parser, tree list)
7203{
7204 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_COPYPRIVATE, list);
7205}
7206
7207/* OpenMP 2.5:
7208 default ( shared | none ) */
7209
7210static tree
7211c_parser_omp_clause_default (c_parser *parser, tree list)
7212{
7213 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
c2255bc4 7214 location_t loc = c_parser_peek_token (parser)->location;
953ff289
DN
7215 tree c;
7216
7217 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7218 return list;
7219 if (c_parser_next_token_is (parser, CPP_NAME))
7220 {
7221 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
7222
7223 switch (p[0])
7224 {
7225 case 'n':
7226 if (strcmp ("none", p) != 0)
7227 goto invalid_kind;
7228 kind = OMP_CLAUSE_DEFAULT_NONE;
7229 break;
7230
7231 case 's':
7232 if (strcmp ("shared", p) != 0)
7233 goto invalid_kind;
7234 kind = OMP_CLAUSE_DEFAULT_SHARED;
7235 break;
7236
7237 default:
7238 goto invalid_kind;
7239 }
7240
7241 c_parser_consume_token (parser);
7242 }
7243 else
7244 {
7245 invalid_kind:
7246 c_parser_error (parser, "expected %<none%> or %<shared%>");
7247 }
7248 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
7249
7250 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
7251 return list;
7252
7253 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default");
c2255bc4 7254 c = build_omp_clause (loc, OMP_CLAUSE_DEFAULT);
953ff289
DN
7255 OMP_CLAUSE_CHAIN (c) = list;
7256 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
7257
7258 return c;
7259}
7260
7261/* OpenMP 2.5:
7262 firstprivate ( variable-list ) */
7263
7264static tree
7265c_parser_omp_clause_firstprivate (c_parser *parser, tree list)
7266{
7267 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_FIRSTPRIVATE, list);
7268}
7269
7270/* OpenMP 2.5:
7271 if ( expression ) */
7272
7273static tree
7274c_parser_omp_clause_if (c_parser *parser, tree list)
7275{
c2255bc4 7276 location_t loc = c_parser_peek_token (parser)->location;
953ff289
DN
7277 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
7278 {
7279 tree t = c_parser_paren_condition (parser);
7280 tree c;
7281
7282 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if");
7283
c2255bc4 7284 c = build_omp_clause (loc, OMP_CLAUSE_IF);
953ff289
DN
7285 OMP_CLAUSE_IF_EXPR (c) = t;
7286 OMP_CLAUSE_CHAIN (c) = list;
7287 list = c;
7288 }
7289 else
7290 c_parser_error (parser, "expected %<(%>");
7291
7292 return list;
7293}
7294
7295/* OpenMP 2.5:
7296 lastprivate ( variable-list ) */
7297
7298static tree
7299c_parser_omp_clause_lastprivate (c_parser *parser, tree list)
7300{
7301 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_LASTPRIVATE, list);
7302}
7303
7304/* OpenMP 2.5:
7305 nowait */
7306
7307static tree
7308c_parser_omp_clause_nowait (c_parser *parser ATTRIBUTE_UNUSED, tree list)
7309{
7310 tree c;
c2255bc4 7311 location_t loc = c_parser_peek_token (parser)->location;
953ff289
DN
7312
7313 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait");
7314
c2255bc4 7315 c = build_omp_clause (loc, OMP_CLAUSE_NOWAIT);
953ff289
DN
7316 OMP_CLAUSE_CHAIN (c) = list;
7317 return c;
7318}
7319
7320/* OpenMP 2.5:
7321 num_threads ( expression ) */
7322
7323static tree
7324c_parser_omp_clause_num_threads (c_parser *parser, tree list)
7325{
c2255bc4 7326 location_t num_threads_loc = c_parser_peek_token (parser)->location;
953ff289
DN
7327 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7328 {
c7412148 7329 location_t expr_loc = c_parser_peek_token (parser)->location;
953ff289 7330 tree c, t = c_parser_expression (parser).value;
928c19bb 7331 t = c_fully_fold (t, false, NULL);
953ff289
DN
7332
7333 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
7334
7335 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
7336 {
7337 c_parser_error (parser, "expected integer expression");
7338 return list;
7339 }
7340
7341 /* Attempt to statically determine when the number isn't positive. */
7342 c = fold_build2 (LE_EXPR, boolean_type_node, t,
7343 build_int_cst (TREE_TYPE (t), 0));
c2255bc4
AH
7344 if (CAN_HAVE_LOCATION_P (c))
7345 SET_EXPR_LOCATION (c, expr_loc);
953ff289
DN
7346 if (c == boolean_true_node)
7347 {
3ba09659
AH
7348 warning_at (expr_loc, 0,
7349 "%<num_threads%> value must be positive");
953ff289
DN
7350 t = integer_one_node;
7351 }
7352
7353 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS, "num_threads");
7354
c2255bc4 7355 c = build_omp_clause (num_threads_loc, OMP_CLAUSE_NUM_THREADS);
953ff289
DN
7356 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
7357 OMP_CLAUSE_CHAIN (c) = list;
7358 list = c;
7359 }
7360
7361 return list;
7362}
7363
7364/* OpenMP 2.5:
7365 ordered */
7366
7367static tree
c2255bc4 7368c_parser_omp_clause_ordered (c_parser *parser, tree list)
953ff289
DN
7369{
7370 tree c;
7371
7372 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED, "ordered");
7373
c2255bc4
AH
7374 c = build_omp_clause (c_parser_peek_token (parser)->location,
7375 OMP_CLAUSE_ORDERED);
953ff289 7376 OMP_CLAUSE_CHAIN (c) = list;
c2255bc4 7377
953ff289
DN
7378 return c;
7379}
7380
7381/* OpenMP 2.5:
7382 private ( variable-list ) */
7383
7384static tree
7385c_parser_omp_clause_private (c_parser *parser, tree list)
7386{
7387 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_PRIVATE, list);
7388}
7389
7390/* OpenMP 2.5:
7391 reduction ( reduction-operator : variable-list )
7392
7393 reduction-operator:
7394 One of: + * - & ^ | && || */
7395
7396static tree
7397c_parser_omp_clause_reduction (c_parser *parser, tree list)
7398{
c2255bc4 7399 location_t clause_loc = c_parser_peek_token (parser)->location;
953ff289
DN
7400 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7401 {
7402 enum tree_code code;
7403
7404 switch (c_parser_peek_token (parser)->type)
7405 {
7406 case CPP_PLUS:
7407 code = PLUS_EXPR;
7408 break;
7409 case CPP_MULT:
7410 code = MULT_EXPR;
7411 break;
7412 case CPP_MINUS:
7413 code = MINUS_EXPR;
7414 break;
7415 case CPP_AND:
7416 code = BIT_AND_EXPR;
7417 break;
7418 case CPP_XOR:
7419 code = BIT_XOR_EXPR;
7420 break;
7421 case CPP_OR:
7422 code = BIT_IOR_EXPR;
7423 break;
7424 case CPP_AND_AND:
7425 code = TRUTH_ANDIF_EXPR;
7426 break;
7427 case CPP_OR_OR:
7428 code = TRUTH_ORIF_EXPR;
7429 break;
7430 default:
7431 c_parser_error (parser,
7432 "expected %<+%>, %<*%>, %<-%>, %<&%>, "
7433 "%<^%>, %<|%>, %<&&%>, or %<||%>");
7434 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
7435 return list;
7436 }
7437 c_parser_consume_token (parser);
7438 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
7439 {
7440 tree nl, c;
7441
c2255bc4
AH
7442 nl = c_parser_omp_variable_list (parser, clause_loc,
7443 OMP_CLAUSE_REDUCTION, list);
953ff289
DN
7444 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
7445 OMP_CLAUSE_REDUCTION_CODE (c) = code;
7446
7447 list = nl;
7448 }
7449 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
7450 }
7451 return list;
7452}
7453
7454/* OpenMP 2.5:
7455 schedule ( schedule-kind )
7456 schedule ( schedule-kind , expression )
7457
7458 schedule-kind:
a68ab351 7459 static | dynamic | guided | runtime | auto
953ff289
DN
7460*/
7461
7462static tree
7463c_parser_omp_clause_schedule (c_parser *parser, tree list)
7464{
7465 tree c, t;
c2255bc4 7466 location_t loc = c_parser_peek_token (parser)->location;
953ff289
DN
7467
7468 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7469 return list;
7470
c2255bc4 7471 c = build_omp_clause (loc, OMP_CLAUSE_SCHEDULE);
953ff289
DN
7472
7473 if (c_parser_next_token_is (parser, CPP_NAME))
7474 {
7475 tree kind = c_parser_peek_token (parser)->value;
7476 const char *p = IDENTIFIER_POINTER (kind);
7477
7478 switch (p[0])
7479 {
7480 case 'd':
7481 if (strcmp ("dynamic", p) != 0)
7482 goto invalid_kind;
7483 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
7484 break;
7485
7486 case 'g':
7487 if (strcmp ("guided", p) != 0)
7488 goto invalid_kind;
7489 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
7490 break;
7491
7492 case 'r':
7493 if (strcmp ("runtime", p) != 0)
7494 goto invalid_kind;
7495 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
7496 break;
7497
7498 default:
7499 goto invalid_kind;
7500 }
7501 }
7502 else if (c_parser_next_token_is_keyword (parser, RID_STATIC))
7503 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
a68ab351
JJ
7504 else if (c_parser_next_token_is_keyword (parser, RID_AUTO))
7505 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
953ff289
DN
7506 else
7507 goto invalid_kind;
7508
7509 c_parser_consume_token (parser);
7510 if (c_parser_next_token_is (parser, CPP_COMMA))
7511 {
c7412148 7512 location_t here;
953ff289
DN
7513 c_parser_consume_token (parser);
7514
c7412148 7515 here = c_parser_peek_token (parser)->location;
953ff289 7516 t = c_parser_expr_no_commas (parser, NULL).value;
928c19bb 7517 t = c_fully_fold (t, false, NULL);
953ff289
DN
7518
7519 if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
3ba09659
AH
7520 error_at (here, "schedule %<runtime%> does not take "
7521 "a %<chunk_size%> parameter");
a68ab351 7522 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
3ba09659
AH
7523 error_at (here,
7524 "schedule %<auto%> does not take "
7525 "a %<chunk_size%> parameter");
953ff289
DN
7526 else if (TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE)
7527 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
7528 else
7529 c_parser_error (parser, "expected integer expression");
7530
7531 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
7532 }
7533 else
7534 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7535 "expected %<,%> or %<)%>");
7536
7537 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
7538 OMP_CLAUSE_CHAIN (c) = list;
7539 return c;
7540
7541 invalid_kind:
7542 c_parser_error (parser, "invalid schedule kind");
7543 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
7544 return list;
7545}
7546
7547/* OpenMP 2.5:
7548 shared ( variable-list ) */
7549
7550static tree
7551c_parser_omp_clause_shared (c_parser *parser, tree list)
7552{
7553 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_SHARED, list);
7554}
7555
a68ab351
JJ
7556/* OpenMP 3.0:
7557 untied */
7558
7559static tree
7560c_parser_omp_clause_untied (c_parser *parser ATTRIBUTE_UNUSED, tree list)
7561{
7562 tree c;
7563
7564 /* FIXME: Should we allow duplicates? */
7565 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied");
7566
c2255bc4
AH
7567 c = build_omp_clause (c_parser_peek_token (parser)->location,
7568 OMP_CLAUSE_UNTIED);
a68ab351 7569 OMP_CLAUSE_CHAIN (c) = list;
c2255bc4 7570
a68ab351
JJ
7571 return c;
7572}
7573
953ff289
DN
7574/* Parse all OpenMP clauses. The set clauses allowed by the directive
7575 is a bitmask in MASK. Return the list of clauses found; the result
7576 of clause default goes in *pdefault. */
7577
7578static tree
7579c_parser_omp_all_clauses (c_parser *parser, unsigned int mask,
7580 const char *where)
7581{
7582 tree clauses = NULL;
8085ca15 7583 bool first = true;
953ff289
DN
7584
7585 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
7586 {
8085ca15
JJ
7587 location_t here;
7588 pragma_omp_clause c_kind;
953ff289
DN
7589 const char *c_name;
7590 tree prev = clauses;
7591
8085ca15
JJ
7592 if (!first && c_parser_next_token_is (parser, CPP_COMMA))
7593 c_parser_consume_token (parser);
7594
7595 first = false;
7596 here = c_parser_peek_token (parser)->location;
7597 c_kind = c_parser_omp_clause_name (parser);
7598
953ff289
DN
7599 switch (c_kind)
7600 {
a68ab351
JJ
7601 case PRAGMA_OMP_CLAUSE_COLLAPSE:
7602 clauses = c_parser_omp_clause_collapse (parser, clauses);
7603 c_name = "collapse";
7604 break;
953ff289
DN
7605 case PRAGMA_OMP_CLAUSE_COPYIN:
7606 clauses = c_parser_omp_clause_copyin (parser, clauses);
7607 c_name = "copyin";
7608 break;
7609 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
7610 clauses = c_parser_omp_clause_copyprivate (parser, clauses);
7611 c_name = "copyprivate";
7612 break;
7613 case PRAGMA_OMP_CLAUSE_DEFAULT:
7614 clauses = c_parser_omp_clause_default (parser, clauses);
7615 c_name = "default";
7616 break;
7617 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
7618 clauses = c_parser_omp_clause_firstprivate (parser, clauses);
7619 c_name = "firstprivate";
7620 break;
7621 case PRAGMA_OMP_CLAUSE_IF:
7622 clauses = c_parser_omp_clause_if (parser, clauses);
7623 c_name = "if";
7624 break;
7625 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
7626 clauses = c_parser_omp_clause_lastprivate (parser, clauses);
7627 c_name = "lastprivate";
7628 break;
7629 case PRAGMA_OMP_CLAUSE_NOWAIT:
7630 clauses = c_parser_omp_clause_nowait (parser, clauses);
7631 c_name = "nowait";
7632 break;
7633 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
7634 clauses = c_parser_omp_clause_num_threads (parser, clauses);
7635 c_name = "num_threads";
7636 break;
7637 case PRAGMA_OMP_CLAUSE_ORDERED:
7638 clauses = c_parser_omp_clause_ordered (parser, clauses);
7639 c_name = "ordered";
7640 break;
7641 case PRAGMA_OMP_CLAUSE_PRIVATE:
7642 clauses = c_parser_omp_clause_private (parser, clauses);
7643 c_name = "private";
7644 break;
7645 case PRAGMA_OMP_CLAUSE_REDUCTION:
7646 clauses = c_parser_omp_clause_reduction (parser, clauses);
7647 c_name = "reduction";
7648 break;
7649 case PRAGMA_OMP_CLAUSE_SCHEDULE:
7650 clauses = c_parser_omp_clause_schedule (parser, clauses);
7651 c_name = "schedule";
7652 break;
7653 case PRAGMA_OMP_CLAUSE_SHARED:
7654 clauses = c_parser_omp_clause_shared (parser, clauses);
7655 c_name = "shared";
7656 break;
a68ab351
JJ
7657 case PRAGMA_OMP_CLAUSE_UNTIED:
7658 clauses = c_parser_omp_clause_untied (parser, clauses);
7659 c_name = "untied";
7660 break;
953ff289
DN
7661 default:
7662 c_parser_error (parser, "expected %<#pragma omp%> clause");
7663 goto saw_error;
7664 }
7665
7666 if (((mask >> c_kind) & 1) == 0 && !parser->error)
7667 {
7668 /* Remove the invalid clause(s) from the list to avoid
7669 confusing the rest of the compiler. */
7670 clauses = prev;
3ba09659 7671 error_at (here, "%qs is not valid for %qs", c_name, where);
953ff289
DN
7672 }
7673 }
7674
7675 saw_error:
7676 c_parser_skip_to_pragma_eol (parser);
7677
7678 return c_finish_omp_clauses (clauses);
7679}
7680
7681/* OpenMP 2.5:
7682 structured-block:
7683 statement
7684
7685 In practice, we're also interested in adding the statement to an
7686 outer node. So it is convenient if we work around the fact that
7687 c_parser_statement calls add_stmt. */
7688
7689static tree
7690c_parser_omp_structured_block (c_parser *parser)
7691{
7692 tree stmt = push_stmt_list ();
7693 c_parser_statement (parser);
7694 return pop_stmt_list (stmt);
7695}
7696
7697/* OpenMP 2.5:
7698 # pragma omp atomic new-line
7699 expression-stmt
7700
7701 expression-stmt:
7702 x binop= expr | x++ | ++x | x-- | --x
7703 binop:
7704 +, *, -, /, &, ^, |, <<, >>
7705
c2255bc4
AH
7706 where x is an lvalue expression with scalar type.
7707
7708 LOC is the location of the #pragma token. */
953ff289
DN
7709
7710static void
c2255bc4 7711c_parser_omp_atomic (location_t loc, c_parser *parser)
953ff289
DN
7712{
7713 tree lhs, rhs;
fe89d797 7714 tree stmt;
953ff289 7715 enum tree_code code;
79addd1f 7716 struct c_expr rhs_expr;
953ff289
DN
7717
7718 c_parser_skip_to_pragma_eol (parser);
7719
7720 lhs = c_parser_unary_expression (parser).value;
928c19bb 7721 lhs = c_fully_fold (lhs, false, NULL);
953ff289
DN
7722 switch (TREE_CODE (lhs))
7723 {
7724 case ERROR_MARK:
7725 saw_error:
7726 c_parser_skip_to_end_of_block_or_statement (parser);
7727 return;
7728
7729 case PREINCREMENT_EXPR:
7730 case POSTINCREMENT_EXPR:
7731 lhs = TREE_OPERAND (lhs, 0);
7732 code = PLUS_EXPR;
7733 rhs = integer_one_node;
7734 break;
7735
7736 case PREDECREMENT_EXPR:
7737 case POSTDECREMENT_EXPR:
7738 lhs = TREE_OPERAND (lhs, 0);
7739 code = MINUS_EXPR;
7740 rhs = integer_one_node;
7741 break;
7742
7743 default:
7744 switch (c_parser_peek_token (parser)->type)
7745 {
7746 case CPP_MULT_EQ:
7747 code = MULT_EXPR;
7748 break;
7749 case CPP_DIV_EQ:
7750 code = TRUNC_DIV_EXPR;
7751 break;
7752 case CPP_PLUS_EQ:
7753 code = PLUS_EXPR;
7754 break;
7755 case CPP_MINUS_EQ:
7756 code = MINUS_EXPR;
7757 break;
7758 case CPP_LSHIFT_EQ:
7759 code = LSHIFT_EXPR;
7760 break;
7761 case CPP_RSHIFT_EQ:
7762 code = RSHIFT_EXPR;
7763 break;
7764 case CPP_AND_EQ:
7765 code = BIT_AND_EXPR;
7766 break;
7767 case CPP_OR_EQ:
7768 code = BIT_IOR_EXPR;
7769 break;
7770 case CPP_XOR_EQ:
7771 code = BIT_XOR_EXPR;
7772 break;
7773 default:
7774 c_parser_error (parser,
7775 "invalid operator for %<#pragma omp atomic%>");
7776 goto saw_error;
7777 }
7778
7779 c_parser_consume_token (parser);
c2255bc4
AH
7780 {
7781 location_t rhs_loc = c_parser_peek_token (parser)->location;
7782 rhs_expr = c_parser_expression (parser);
7783 rhs_expr = default_function_array_conversion (rhs_loc, rhs_expr);
7784 }
79addd1f 7785 rhs = rhs_expr.value;
928c19bb 7786 rhs = c_fully_fold (rhs, false, NULL);
953ff289
DN
7787 break;
7788 }
c2255bc4 7789 stmt = c_finish_omp_atomic (loc, code, lhs, rhs);
fe89d797
MM
7790 if (stmt != error_mark_node)
7791 add_stmt (stmt);
953ff289
DN
7792 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
7793}
7794
7795
7796/* OpenMP 2.5:
7797 # pragma omp barrier new-line
7798*/
7799
7800static void
7801c_parser_omp_barrier (c_parser *parser)
7802{
c2255bc4 7803 location_t loc = c_parser_peek_token (parser)->location;
953ff289
DN
7804 c_parser_consume_pragma (parser);
7805 c_parser_skip_to_pragma_eol (parser);
7806
c2255bc4 7807 c_finish_omp_barrier (loc);
953ff289
DN
7808}
7809
7810/* OpenMP 2.5:
7811 # pragma omp critical [(name)] new-line
7812 structured-block
c2255bc4
AH
7813
7814 LOC is the location of the #pragma itself. */
953ff289
DN
7815
7816static tree
c2255bc4 7817c_parser_omp_critical (location_t loc, c_parser *parser)
953ff289
DN
7818{
7819 tree stmt, name = NULL;
7820
7821 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
7822 {
7823 c_parser_consume_token (parser);
7824 if (c_parser_next_token_is (parser, CPP_NAME))
7825 {
7826 name = c_parser_peek_token (parser)->value;
7827 c_parser_consume_token (parser);
7828 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
7829 }
7830 else
7831 c_parser_error (parser, "expected identifier");
7832 }
7833 else if (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
7834 c_parser_error (parser, "expected %<(%> or end of line");
7835 c_parser_skip_to_pragma_eol (parser);
7836
7837 stmt = c_parser_omp_structured_block (parser);
c2255bc4 7838 return c_finish_omp_critical (loc, stmt, name);
953ff289
DN
7839}
7840
7841/* OpenMP 2.5:
7842 # pragma omp flush flush-vars[opt] new-line
7843
7844 flush-vars:
7845 ( variable-list ) */
7846
7847static void
7848c_parser_omp_flush (c_parser *parser)
7849{
c2255bc4 7850 location_t loc = c_parser_peek_token (parser)->location;
953ff289
DN
7851 c_parser_consume_pragma (parser);
7852 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
d75d71e0 7853 c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
953ff289
DN
7854 else if (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
7855 c_parser_error (parser, "expected %<(%> or end of line");
7856 c_parser_skip_to_pragma_eol (parser);
7857
c2255bc4 7858 c_finish_omp_flush (loc);
953ff289
DN
7859}
7860
fa10beec 7861/* Parse the restricted form of the for statement allowed by OpenMP.
953ff289 7862 The real trick here is to determine the loop control variable early
c2255bc4
AH
7863 so that we can push a new decl if necessary to make it private.
7864 LOC is the location of the OMP in "#pragma omp". */
953ff289
DN
7865
7866static tree
c2255bc4
AH
7867c_parser_omp_for_loop (location_t loc,
7868 c_parser *parser, tree clauses, tree *par_clauses)
953ff289 7869{
a68ab351
JJ
7870 tree decl, cond, incr, save_break, save_cont, body, init, stmt, cl;
7871 tree declv, condv, incrv, initv, for_block = NULL, ret = NULL;
a68ab351
JJ
7872 bool fail = false, open_brace_parsed = false;
7873 int i, collapse = 1, nbraces = 0;
c2255bc4 7874 location_t for_loc;
a68ab351
JJ
7875
7876 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
7877 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
7878 collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
7879
7880 gcc_assert (collapse >= 1);
7881
7882 declv = make_tree_vec (collapse);
7883 initv = make_tree_vec (collapse);
7884 condv = make_tree_vec (collapse);
7885 incrv = make_tree_vec (collapse);
953ff289
DN
7886
7887 if (!c_parser_next_token_is_keyword (parser, RID_FOR))
7888 {
7889 c_parser_error (parser, "for statement expected");
7890 return NULL;
7891 }
c2255bc4 7892 for_loc = c_parser_peek_token (parser)->location;
953ff289
DN
7893 c_parser_consume_token (parser);
7894
a68ab351 7895 for (i = 0; i < collapse; i++)
953ff289 7896 {
a68ab351 7897 int bracecount = 0;
953ff289 7898
a68ab351
JJ
7899 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7900 goto pop_scopes;
953ff289 7901
a68ab351
JJ
7902 /* Parse the initialization declaration or expression. */
7903 if (c_parser_next_token_starts_declspecs (parser))
7904 {
7905 if (i > 0)
7906 for_block
7907 = tree_cons (NULL, c_begin_compound_stmt (true), for_block);
7908 c_parser_declaration_or_fndef (parser, true, true, true, true);
c2255bc4 7909 decl = check_for_loop_decls (for_loc);
a68ab351
JJ
7910 if (decl == NULL)
7911 goto error_init;
7912 if (DECL_INITIAL (decl) == error_mark_node)
7913 decl = error_mark_node;
7914 init = decl;
7915 }
7916 else if (c_parser_next_token_is (parser, CPP_NAME)
7917 && c_parser_peek_2nd_token (parser)->type == CPP_EQ)
7918 {
32e8bb8e 7919 struct c_expr decl_exp;
a68ab351 7920 struct c_expr init_exp;
c9f9eb5d 7921 location_t init_loc;
a68ab351 7922
32e8bb8e
ILT
7923 decl_exp = c_parser_postfix_expression (parser);
7924 decl = decl_exp.value;
a68ab351
JJ
7925
7926 c_parser_require (parser, CPP_EQ, "expected %<=%>");
7927
c2255bc4 7928 init_loc = c_parser_peek_token (parser)->location;
a68ab351 7929 init_exp = c_parser_expr_no_commas (parser, NULL);
c2255bc4 7930 init_exp = default_function_array_conversion (init_loc, init_exp);
32e8bb8e 7931 init = build_modify_expr (init_loc, decl, decl_exp.original_type,
c2255bc4 7932 NOP_EXPR, init_loc, init_exp.value,
bbbbb16a 7933 init_exp.original_type);
c2255bc4 7934 init = c_process_expr_stmt (init_loc, init);
953ff289 7935
a68ab351
JJ
7936 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
7937 }
7938 else
7939 {
7940 error_init:
7941 c_parser_error (parser,
7942 "expected iteration declaration or initialization");
7943 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7944 "expected %<)%>");
7945 fail = true;
7946 goto parse_next;
7947 }
7948
7949 /* Parse the loop condition. */
7950 cond = NULL_TREE;
7951 if (c_parser_next_token_is_not (parser, CPP_SEMICOLON))
7952 {
3ba09659 7953 location_t cond_loc = c_parser_peek_token (parser)->location;
c5cdb03f 7954 struct c_expr cond_expr = c_parser_binary_expression (parser, NULL);
3ba09659 7955
c5cdb03f 7956 cond = cond_expr.value;
3ba09659 7957 cond = c_objc_common_truthvalue_conversion (cond_loc, cond);
928c19bb 7958 cond = c_fully_fold (cond, false, NULL);
c5cdb03f
JJ
7959 switch (cond_expr.original_code)
7960 {
7961 case GT_EXPR:
7962 case GE_EXPR:
7963 case LT_EXPR:
7964 case LE_EXPR:
7965 break;
7966 default:
7967 /* Can't be cond = error_mark_node, because we want to preserve
7968 the location until c_finish_omp_for. */
7969 cond = build1 (NOP_EXPR, boolean_type_node, error_mark_node);
7970 break;
7971 }
c9f9eb5d 7972 protected_set_expr_location (cond, cond_loc);
a68ab351 7973 }
953ff289 7974 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
953ff289 7975
a68ab351
JJ
7976 /* Parse the increment expression. */
7977 incr = NULL_TREE;
7978 if (c_parser_next_token_is_not (parser, CPP_CLOSE_PAREN))
c9f9eb5d
AH
7979 {
7980 location_t incr_loc = c_parser_peek_token (parser)->location;
7981
c2255bc4
AH
7982 incr = c_process_expr_stmt (incr_loc,
7983 c_parser_expression (parser).value);
c9f9eb5d 7984 }
a68ab351 7985 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
953ff289 7986
a68ab351
JJ
7987 if (decl == NULL || decl == error_mark_node || init == error_mark_node)
7988 fail = true;
7989 else
7990 {
7991 TREE_VEC_ELT (declv, i) = decl;
7992 TREE_VEC_ELT (initv, i) = init;
7993 TREE_VEC_ELT (condv, i) = cond;
7994 TREE_VEC_ELT (incrv, i) = incr;
7995 }
7996
7997 parse_next:
7998 if (i == collapse - 1)
7999 break;
8000
8001 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
8002 in between the collapsed for loops to be still considered perfectly
8003 nested. Hopefully the final version clarifies this.
8004 For now handle (multiple) {'s and empty statements. */
8005 do
8006 {
8007 if (c_parser_next_token_is_keyword (parser, RID_FOR))
8008 {
8009 c_parser_consume_token (parser);
8010 break;
8011 }
8012 else if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
8013 {
8014 c_parser_consume_token (parser);
8015 bracecount++;
8016 }
8017 else if (bracecount
8018 && c_parser_next_token_is (parser, CPP_SEMICOLON))
8019 c_parser_consume_token (parser);
8020 else
8021 {
8022 c_parser_error (parser, "not enough perfectly nested loops");
8023 if (bracecount)
8024 {
8025 open_brace_parsed = true;
8026 bracecount--;
8027 }
8028 fail = true;
8029 collapse = 0;
8030 break;
8031 }
8032 }
8033 while (1);
8034
8035 nbraces += bracecount;
8036 }
953ff289 8037
953ff289
DN
8038 save_break = c_break_label;
8039 c_break_label = size_one_node;
8040 save_cont = c_cont_label;
8041 c_cont_label = NULL_TREE;
8042 body = push_stmt_list ();
8043
a68ab351
JJ
8044 if (open_brace_parsed)
8045 {
c2255bc4 8046 location_t here = c_parser_peek_token (parser)->location;
a68ab351
JJ
8047 stmt = c_begin_compound_stmt (true);
8048 c_parser_compound_statement_nostart (parser);
c2255bc4 8049 add_stmt (c_end_compound_stmt (here, stmt, true));
a68ab351
JJ
8050 }
8051 else
8052 add_stmt (c_parser_c99_block_statement (parser));
953ff289 8053 if (c_cont_label)
c2255bc4
AH
8054 {
8055 tree t = build1 (LABEL_EXPR, void_type_node, c_cont_label);
8056 SET_EXPR_LOCATION (t, loc);
8057 add_stmt (t);
8058 }
953ff289
DN
8059
8060 body = pop_stmt_list (body);
8061 c_break_label = save_break;
8062 c_cont_label = save_cont;
8063
a68ab351
JJ
8064 while (nbraces)
8065 {
8066 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
8067 {
8068 c_parser_consume_token (parser);
8069 nbraces--;
8070 }
8071 else if (c_parser_next_token_is (parser, CPP_SEMICOLON))
8072 c_parser_consume_token (parser);
8073 else
8074 {
8075 c_parser_error (parser, "collapsed loops not perfectly nested");
8076 while (nbraces)
8077 {
c2255bc4 8078 location_t here = c_parser_peek_token (parser)->location;
a68ab351
JJ
8079 stmt = c_begin_compound_stmt (true);
8080 add_stmt (body);
8081 c_parser_compound_statement_nostart (parser);
c2255bc4 8082 body = c_end_compound_stmt (here, stmt, true);
a68ab351
JJ
8083 nbraces--;
8084 }
8085 goto pop_scopes;
8086 }
8087 }
8088
61c3a446 8089 /* Only bother calling c_finish_omp_for if we haven't already generated
953ff289 8090 an error from the initialization parsing. */
a68ab351
JJ
8091 if (!fail)
8092 {
8093 stmt = c_finish_omp_for (loc, declv, initv, condv, incrv, body, NULL);
8094 if (stmt)
8095 {
8096 if (par_clauses != NULL)
8097 {
8098 tree *c;
8099 for (c = par_clauses; *c ; )
8100 if (OMP_CLAUSE_CODE (*c) != OMP_CLAUSE_FIRSTPRIVATE
8101 && OMP_CLAUSE_CODE (*c) != OMP_CLAUSE_LASTPRIVATE)
8102 c = &OMP_CLAUSE_CHAIN (*c);
8103 else
8104 {
8105 for (i = 0; i < collapse; i++)
8106 if (TREE_VEC_ELT (declv, i) == OMP_CLAUSE_DECL (*c))
8107 break;
8108 if (i == collapse)
8109 c = &OMP_CLAUSE_CHAIN (*c);
8110 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE)
8111 {
3ba09659
AH
8112 error_at (loc,
8113 "iteration variable %qD should not be firstprivate",
8114 OMP_CLAUSE_DECL (*c));
a68ab351
JJ
8115 *c = OMP_CLAUSE_CHAIN (*c);
8116 }
8117 else
8118 {
8119 /* Copy lastprivate (decl) clause to OMP_FOR_CLAUSES,
8120 change it to shared (decl) in
8121 OMP_PARALLEL_CLAUSES. */
c2255bc4
AH
8122 tree l = build_omp_clause (OMP_CLAUSE_LOCATION (*c),
8123 OMP_CLAUSE_LASTPRIVATE);
a68ab351
JJ
8124 OMP_CLAUSE_DECL (l) = OMP_CLAUSE_DECL (*c);
8125 OMP_CLAUSE_CHAIN (l) = clauses;
8126 clauses = l;
8127 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
8128 }
8129 }
8130 }
8131 OMP_FOR_CLAUSES (stmt) = clauses;
8132 }
8133 ret = stmt;
8134 }
8135pop_scopes:
8136 while (for_block)
8137 {
c2255bc4
AH
8138 /* FIXME diagnostics: LOC below should be the actual location of
8139 this particular for block. We need to build a list of
8140 locations to go along with FOR_BLOCK. */
8141 stmt = c_end_compound_stmt (loc, TREE_VALUE (for_block), true);
a68ab351
JJ
8142 add_stmt (stmt);
8143 for_block = TREE_CHAIN (for_block);
8144 }
8145 return ret;
953ff289
DN
8146}
8147
8148/* OpenMP 2.5:
8149 #pragma omp for for-clause[optseq] new-line
8150 for-loop
c2255bc4
AH
8151
8152 LOC is the location of the #pragma token.
953ff289
DN
8153*/
8154
8155#define OMP_FOR_CLAUSE_MASK \
8156 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
8157 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
8158 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
8159 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
8160 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \
8161 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \
a68ab351 8162 | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE) \
953ff289
DN
8163 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
8164
8165static tree
c2255bc4 8166c_parser_omp_for (location_t loc, c_parser *parser)
953ff289
DN
8167{
8168 tree block, clauses, ret;
8169
8170 clauses = c_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
8171 "#pragma omp for");
8172
8173 block = c_begin_compound_stmt (true);
c2255bc4
AH
8174 ret = c_parser_omp_for_loop (loc, parser, clauses, NULL);
8175 block = c_end_compound_stmt (loc, block, true);
953ff289
DN
8176 add_stmt (block);
8177
8178 return ret;
8179}
8180
8181/* OpenMP 2.5:
8182 # pragma omp master new-line
8183 structured-block
c2255bc4
AH
8184
8185 LOC is the location of the #pragma token.
953ff289
DN
8186*/
8187
8188static tree
c2255bc4 8189c_parser_omp_master (location_t loc, c_parser *parser)
953ff289
DN
8190{
8191 c_parser_skip_to_pragma_eol (parser);
c2255bc4 8192 return c_finish_omp_master (loc, c_parser_omp_structured_block (parser));
953ff289
DN
8193}
8194
8195/* OpenMP 2.5:
8196 # pragma omp ordered new-line
8197 structured-block
c2255bc4
AH
8198
8199 LOC is the location of the #pragma itself.
953ff289
DN
8200*/
8201
8202static tree
c2255bc4 8203c_parser_omp_ordered (location_t loc, c_parser *parser)
953ff289
DN
8204{
8205 c_parser_skip_to_pragma_eol (parser);
c2255bc4 8206 return c_finish_omp_ordered (loc, c_parser_omp_structured_block (parser));
953ff289
DN
8207}
8208
8209/* OpenMP 2.5:
8210
8211 section-scope:
8212 { section-sequence }
8213
8214 section-sequence:
8215 section-directive[opt] structured-block
c2255bc4
AH
8216 section-sequence section-directive structured-block
8217
8218 SECTIONS_LOC is the location of the #pragma omp sections. */
953ff289
DN
8219
8220static tree
c2255bc4 8221c_parser_omp_sections_scope (location_t sections_loc, c_parser *parser)
953ff289
DN
8222{
8223 tree stmt, substmt;
8224 bool error_suppress = false;
8225 location_t loc;
8226
c2255bc4 8227 loc = c_parser_peek_token (parser)->location;
953ff289
DN
8228 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
8229 {
8230 /* Avoid skipping until the end of the block. */
8231 parser->error = false;
8232 return NULL_TREE;
8233 }
8234
8235 stmt = push_stmt_list ();
8236
953ff289
DN
8237 if (c_parser_peek_token (parser)->pragma_kind != PRAGMA_OMP_SECTION)
8238 {
8239 substmt = push_stmt_list ();
8240
8241 while (1)
8242 {
8243 c_parser_statement (parser);
8244
8245 if (c_parser_peek_token (parser)->pragma_kind == PRAGMA_OMP_SECTION)
8246 break;
8247 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
8248 break;
8249 if (c_parser_next_token_is (parser, CPP_EOF))
8250 break;
8251 }
8252
8253 substmt = pop_stmt_list (substmt);
8254 substmt = build1 (OMP_SECTION, void_type_node, substmt);
8255 SET_EXPR_LOCATION (substmt, loc);
8256 add_stmt (substmt);
8257 }
8258
8259 while (1)
8260 {
8261 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
8262 break;
8263 if (c_parser_next_token_is (parser, CPP_EOF))
8264 break;
8265
8266 loc = c_parser_peek_token (parser)->location;
8267 if (c_parser_peek_token (parser)->pragma_kind == PRAGMA_OMP_SECTION)
8268 {
8269 c_parser_consume_pragma (parser);
8270 c_parser_skip_to_pragma_eol (parser);
8271 error_suppress = false;
8272 }
8273 else if (!error_suppress)
8274 {
3ba09659 8275 error_at (loc, "expected %<#pragma omp section%> or %<}%>");
953ff289
DN
8276 error_suppress = true;
8277 }
8278
8279 substmt = c_parser_omp_structured_block (parser);
8280 substmt = build1 (OMP_SECTION, void_type_node, substmt);
8281 SET_EXPR_LOCATION (substmt, loc);
8282 add_stmt (substmt);
8283 }
8284 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE,
8285 "expected %<#pragma omp section%> or %<}%>");
8286
8287 substmt = pop_stmt_list (stmt);
8288
8289 stmt = make_node (OMP_SECTIONS);
c2255bc4 8290 SET_EXPR_LOCATION (stmt, sections_loc);
953ff289
DN
8291 TREE_TYPE (stmt) = void_type_node;
8292 OMP_SECTIONS_BODY (stmt) = substmt;
8293
8294 return add_stmt (stmt);
8295}
8296
8297/* OpenMP 2.5:
8298 # pragma omp sections sections-clause[optseq] newline
8299 sections-scope
c2255bc4
AH
8300
8301 LOC is the location of the #pragma token.
953ff289
DN
8302*/
8303
8304#define OMP_SECTIONS_CLAUSE_MASK \
8305 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
8306 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
8307 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
8308 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
8309 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
8310
8311static tree
c2255bc4 8312c_parser_omp_sections (location_t loc, c_parser *parser)
953ff289
DN
8313{
8314 tree block, clauses, ret;
8315
8316 clauses = c_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
8317 "#pragma omp sections");
8318
8319 block = c_begin_compound_stmt (true);
c2255bc4 8320 ret = c_parser_omp_sections_scope (loc, parser);
953ff289
DN
8321 if (ret)
8322 OMP_SECTIONS_CLAUSES (ret) = clauses;
c2255bc4 8323 block = c_end_compound_stmt (loc, block, true);
953ff289
DN
8324 add_stmt (block);
8325
8326 return ret;
8327}
8328
8329/* OpenMP 2.5:
8330 # pragma parallel parallel-clause new-line
8331 # pragma parallel for parallel-for-clause new-line
8332 # pragma parallel sections parallel-sections-clause new-line
c2255bc4
AH
8333
8334 LOC is the location of the #pragma token.
953ff289
DN
8335*/
8336
8337#define OMP_PARALLEL_CLAUSE_MASK \
8338 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
8339 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
8340 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
8341 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
8342 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
8343 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \
8344 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
8345 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
8346
8347static tree
c2255bc4 8348c_parser_omp_parallel (location_t loc, c_parser *parser)
953ff289
DN
8349{
8350 enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
8351 const char *p_name = "#pragma omp parallel";
8352 tree stmt, clauses, par_clause, ws_clause, block;
8353 unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
8354
8355 if (c_parser_next_token_is_keyword (parser, RID_FOR))
8356 {
8357 c_parser_consume_token (parser);
8358 p_kind = PRAGMA_OMP_PARALLEL_FOR;
8359 p_name = "#pragma omp parallel for";
8360 mask |= OMP_FOR_CLAUSE_MASK;
8361 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
8362 }
8363 else if (c_parser_next_token_is (parser, CPP_NAME))
8364 {
8365 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
8366 if (strcmp (p, "sections") == 0)
8367 {
8368 c_parser_consume_token (parser);
8369 p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
8370 p_name = "#pragma omp parallel sections";
8371 mask |= OMP_SECTIONS_CLAUSE_MASK;
8372 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
8373 }
8374 }
8375
8376 clauses = c_parser_omp_all_clauses (parser, mask, p_name);
8377
8378 switch (p_kind)
8379 {
8380 case PRAGMA_OMP_PARALLEL:
8381 block = c_begin_omp_parallel ();
8382 c_parser_statement (parser);
c2255bc4 8383 stmt = c_finish_omp_parallel (loc, clauses, block);
953ff289
DN
8384 break;
8385
8386 case PRAGMA_OMP_PARALLEL_FOR:
8387 block = c_begin_omp_parallel ();
c2255bc4
AH
8388 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
8389 c_parser_omp_for_loop (loc, parser, ws_clause, &par_clause);
8390 stmt = c_finish_omp_parallel (loc, par_clause, block);
761041be 8391 OMP_PARALLEL_COMBINED (stmt) = 1;
953ff289
DN
8392 break;
8393
8394 case PRAGMA_OMP_PARALLEL_SECTIONS:
8395 block = c_begin_omp_parallel ();
c2255bc4
AH
8396 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
8397 stmt = c_parser_omp_sections_scope (loc, parser);
953ff289
DN
8398 if (stmt)
8399 OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
c2255bc4 8400 stmt = c_finish_omp_parallel (loc, par_clause, block);
761041be 8401 OMP_PARALLEL_COMBINED (stmt) = 1;
953ff289
DN
8402 break;
8403
8404 default:
8405 gcc_unreachable ();
8406 }
8407
8408 return stmt;
8409}
8410
8411/* OpenMP 2.5:
8412 # pragma omp single single-clause[optseq] new-line
8413 structured-block
c2255bc4
AH
8414
8415 LOC is the location of the #pragma.
953ff289
DN
8416*/
8417
8418#define OMP_SINGLE_CLAUSE_MASK \
8419 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
8420 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
8421 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
8422 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
8423
8424static tree
c2255bc4 8425c_parser_omp_single (location_t loc, c_parser *parser)
953ff289
DN
8426{
8427 tree stmt = make_node (OMP_SINGLE);
c2255bc4 8428 SET_EXPR_LOCATION (stmt, loc);
953ff289
DN
8429 TREE_TYPE (stmt) = void_type_node;
8430
8431 OMP_SINGLE_CLAUSES (stmt)
8432 = c_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
8433 "#pragma omp single");
8434 OMP_SINGLE_BODY (stmt) = c_parser_omp_structured_block (parser);
8435
8436 return add_stmt (stmt);
8437}
8438
a68ab351
JJ
8439/* OpenMP 3.0:
8440 # pragma omp task task-clause[optseq] new-line
c2255bc4
AH
8441
8442 LOC is the location of the #pragma.
a68ab351
JJ
8443*/
8444
8445#define OMP_TASK_CLAUSE_MASK \
8446 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
8447 | (1u << PRAGMA_OMP_CLAUSE_UNTIED) \
8448 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
8449 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
8450 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
8451 | (1u << PRAGMA_OMP_CLAUSE_SHARED))
8452
8453static tree
c2255bc4 8454c_parser_omp_task (location_t loc, c_parser *parser)
a68ab351
JJ
8455{
8456 tree clauses, block;
8457
8458 clauses = c_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
8459 "#pragma omp task");
8460
8461 block = c_begin_omp_task ();
8462 c_parser_statement (parser);
c2255bc4 8463 return c_finish_omp_task (loc, clauses, block);
a68ab351
JJ
8464}
8465
8466/* OpenMP 3.0:
8467 # pragma omp taskwait new-line
8468*/
8469
8470static void
8471c_parser_omp_taskwait (c_parser *parser)
8472{
c2255bc4 8473 location_t loc = c_parser_peek_token (parser)->location;
a68ab351
JJ
8474 c_parser_consume_pragma (parser);
8475 c_parser_skip_to_pragma_eol (parser);
8476
c2255bc4 8477 c_finish_omp_taskwait (loc);
a68ab351 8478}
953ff289
DN
8479
8480/* Main entry point to parsing most OpenMP pragmas. */
8481
8482static void
8483c_parser_omp_construct (c_parser *parser)
8484{
8485 enum pragma_kind p_kind;
8486 location_t loc;
8487 tree stmt;
8488
8489 loc = c_parser_peek_token (parser)->location;
8490 p_kind = c_parser_peek_token (parser)->pragma_kind;
8491 c_parser_consume_pragma (parser);
8492
5c386a95
JJ
8493 /* For all constructs below except #pragma omp atomic
8494 MUST_NOT_THROW catch handlers are needed when exceptions
8495 are enabled. */
8496 if (p_kind != PRAGMA_OMP_ATOMIC)
8497 c_maybe_initialize_eh ();
8498
953ff289
DN
8499 switch (p_kind)
8500 {
8501 case PRAGMA_OMP_ATOMIC:
c2255bc4 8502 c_parser_omp_atomic (loc, parser);
953ff289
DN
8503 return;
8504 case PRAGMA_OMP_CRITICAL:
c2255bc4 8505 stmt = c_parser_omp_critical (loc, parser);
953ff289
DN
8506 break;
8507 case PRAGMA_OMP_FOR:
c2255bc4 8508 stmt = c_parser_omp_for (loc, parser);
953ff289
DN
8509 break;
8510 case PRAGMA_OMP_MASTER:
c2255bc4 8511 stmt = c_parser_omp_master (loc, parser);
953ff289
DN
8512 break;
8513 case PRAGMA_OMP_ORDERED:
c2255bc4 8514 stmt = c_parser_omp_ordered (loc, parser);
953ff289
DN
8515 break;
8516 case PRAGMA_OMP_PARALLEL:
c2255bc4 8517 stmt = c_parser_omp_parallel (loc, parser);
953ff289
DN
8518 break;
8519 case PRAGMA_OMP_SECTIONS:
c2255bc4 8520 stmt = c_parser_omp_sections (loc, parser);
953ff289
DN
8521 break;
8522 case PRAGMA_OMP_SINGLE:
c2255bc4 8523 stmt = c_parser_omp_single (loc, parser);
953ff289 8524 break;
a68ab351 8525 case PRAGMA_OMP_TASK:
c2255bc4 8526 stmt = c_parser_omp_task (loc, parser);
a68ab351 8527 break;
953ff289
DN
8528 default:
8529 gcc_unreachable ();
8530 }
8531
8532 if (stmt)
c2255bc4 8533 gcc_assert (EXPR_LOCATION (stmt) != UNKNOWN_LOCATION);
953ff289
DN
8534}
8535
8536
8537/* OpenMP 2.5:
8538 # pragma omp threadprivate (variable-list) */
8539
8540static void
8541c_parser_omp_threadprivate (c_parser *parser)
8542{
8543 tree vars, t;
c2255bc4 8544 location_t loc;
953ff289
DN
8545
8546 c_parser_consume_pragma (parser);
c2255bc4 8547 loc = c_parser_peek_token (parser)->location;
d75d71e0 8548 vars = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
953ff289 8549
953ff289
DN
8550 /* Mark every variable in VARS to be assigned thread local storage. */
8551 for (t = vars; t; t = TREE_CHAIN (t))
8552 {
8553 tree v = TREE_PURPOSE (t);
8554
c2255bc4
AH
8555 /* FIXME diagnostics: Ideally we should keep individual
8556 locations for all the variables in the var list to make the
8557 following errors more precise. Perhaps
8558 c_parser_omp_var_list_parens() should construct a list of
8559 locations to go along with the var list. */
8560
953ff289
DN
8561 /* If V had already been marked threadprivate, it doesn't matter
8562 whether it had been used prior to this point. */
5df27e4a 8563 if (TREE_CODE (v) != VAR_DECL)
c2255bc4 8564 error_at (loc, "%qD is not a variable", v);
5df27e4a 8565 else if (TREE_USED (v) && !C_DECL_THREADPRIVATE_P (v))
c2255bc4 8566 error_at (loc, "%qE declared %<threadprivate%> after first use", v);
953ff289 8567 else if (! TREE_STATIC (v) && ! DECL_EXTERNAL (v))
c2255bc4 8568 error_at (loc, "automatic variable %qE cannot be %<threadprivate%>", v);
5df27e4a
JJ
8569 else if (TREE_TYPE (v) == error_mark_node)
8570 ;
953ff289 8571 else if (! COMPLETE_TYPE_P (TREE_TYPE (v)))
c2255bc4 8572 error_at (loc, "%<threadprivate%> %qE has incomplete type", v);
953ff289
DN
8573 else
8574 {
8575 if (! DECL_THREAD_LOCAL_P (v))
8576 {
8577 DECL_TLS_MODEL (v) = decl_default_tls_model (v);
8578 /* If rtl has been already set for this var, call
8579 make_decl_rtl once again, so that encode_section_info
8580 has a chance to look at the new decl flags. */
8581 if (DECL_RTL_SET_P (v))
8582 make_decl_rtl (v);
8583 }
8584 C_DECL_THREADPRIVATE_P (v) = 1;
8585 }
8586 }
8587
8588 c_parser_skip_to_pragma_eol (parser);
8589}
8590
bc4071dd 8591\f
27bf414c
JM
8592/* Parse a single source file. */
8593
8594void
8595c_parse_file (void)
8596{
bc4071dd
RH
8597 /* Use local storage to begin. If the first token is a pragma, parse it.
8598 If it is #pragma GCC pch_preprocess, then this will load a PCH file
8599 which will cause garbage collection. */
8600 c_parser tparser;
8601
8602 memset (&tparser, 0, sizeof tparser);
8603 the_parser = &tparser;
8604
8605 if (c_parser_peek_token (&tparser)->pragma_kind == PRAGMA_GCC_PCH_PREPROCESS)
8606 c_parser_pragma_pch_preprocess (&tparser);
8607
8608 the_parser = GGC_NEW (c_parser);
8609 *the_parser = tparser;
8610
27bf414c
JM
8611 c_parser_translation_unit (the_parser);
8612 the_parser = NULL;
8613}
8614
8615#include "gt-c-parser.h"
This page took 2.068658 seconds and 5 git commands to generate.