]> gcc.gnu.org Git - gcc.git/blame - gcc/c-parser.c
mips-protos.h (mips_cfun_has_cprestore_slot_p): Declare.
[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"
f9417da1 61#include "except.h"
27bf414c
JM
62
63\f
27bf414c
JM
64/* Initialization routine for this file. */
65
66void
67c_parse_init (void)
68{
69 /* The only initialization required is of the reserved word
70 identifiers. */
71 unsigned int i;
72 tree id;
eea1139b 73 int mask = 0;
27bf414c 74
eea1139b
ILT
75 mask |= D_CXXONLY;
76 if (!flag_isoc99)
77 mask |= D_C99;
78 if (flag_no_asm)
79 {
80 mask |= D_ASM | D_EXT;
81 if (!flag_isoc99)
82 mask |= D_EXT89;
83 }
27bf414c 84 if (!c_dialect_objc ())
eea1139b 85 mask |= D_OBJC | D_CXX_OBJC;
27bf414c
JM
86
87 ridpointers = GGC_CNEWVEC (tree, (int) RID_MAX);
eea1139b 88 for (i = 0; i < num_c_common_reswords; i++)
27bf414c
JM
89 {
90 /* If a keyword is disabled, do not enter it into the table
91 and so create a canonical spelling that isn't a keyword. */
eea1139b
ILT
92 if (c_common_reswords[i].disable & mask)
93 {
94 if (warn_cxx_compat
95 && (c_common_reswords[i].disable & D_CXXWARN))
96 {
97 id = get_identifier (c_common_reswords[i].word);
98 C_SET_RID_CODE (id, RID_CXX_COMPAT_WARN);
99 C_IS_RESERVED_WORD (id) = 1;
100 }
101 continue;
102 }
27bf414c 103
eea1139b
ILT
104 id = get_identifier (c_common_reswords[i].word);
105 C_SET_RID_CODE (id, c_common_reswords[i].rid);
27bf414c 106 C_IS_RESERVED_WORD (id) = 1;
eea1139b 107 ridpointers [(int) c_common_reswords[i].rid] = id;
27bf414c
JM
108 }
109}
110\f
111/* The C lexer intermediates between the lexer in cpplib and c-lex.c
112 and the C parser. Unlike the C++ lexer, the parser structure
113 stores the lexer information instead of using a separate structure.
114 Identifiers are separated into ordinary identifiers, type names,
115 keywords and some other Objective-C types of identifiers, and some
116 look-ahead is maintained.
117
118 ??? It might be a good idea to lex the whole file up front (as for
119 C++). It would then be possible to share more of the C and C++
120 lexer code, if desired. */
121
122/* The following local token type is used. */
123
124/* A keyword. */
125#define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
126
27bf414c
JM
127/* More information about the type of a CPP_NAME token. */
128typedef enum c_id_kind {
129 /* An ordinary identifier. */
130 C_ID_ID,
131 /* An identifier declared as a typedef name. */
132 C_ID_TYPENAME,
133 /* An identifier declared as an Objective-C class name. */
134 C_ID_CLASSNAME,
135 /* Not an identifier. */
136 C_ID_NONE
137} c_id_kind;
138
139/* A single C token after string literal concatenation and conversion
140 of preprocessing tokens to tokens. */
d1b38208 141typedef struct GTY (()) c_token {
27bf414c
JM
142 /* The kind of token. */
143 ENUM_BITFIELD (cpp_ttype) type : 8;
144 /* If this token is a CPP_NAME, this value indicates whether also
145 declared as some kind of type. Otherwise, it is C_ID_NONE. */
146 ENUM_BITFIELD (c_id_kind) id_kind : 8;
147 /* If this token is a keyword, this value indicates which keyword.
148 Otherwise, this value is RID_MAX. */
149 ENUM_BITFIELD (rid) keyword : 8;
bc4071dd
RH
150 /* If this token is a CPP_PRAGMA, this indicates the pragma that
151 was seen. Otherwise it is PRAGMA_NONE. */
13fa1171 152 ENUM_BITFIELD (pragma_kind) pragma_kind : 8;
27bf414c
JM
153 /* The value associated with this token, if any. */
154 tree value;
155 /* The location at which this token was found. */
156 location_t location;
157} c_token;
158
159/* A parser structure recording information about the state and
160 context of parsing. Includes lexer information with up to two
161 tokens of look-ahead; more are not needed for C. */
d1b38208 162typedef struct GTY(()) c_parser {
27bf414c
JM
163 /* The look-ahead tokens. */
164 c_token tokens[2];
165 /* How many look-ahead tokens are available (0, 1 or 2). */
166 short tokens_avail;
167 /* True if a syntax error is being recovered from; false otherwise.
168 c_parser_error sets this flag. It should clear this flag when
169 enough tokens have been consumed to recover from the error. */
170 BOOL_BITFIELD error : 1;
bc4071dd
RH
171 /* True if we're processing a pragma, and shouldn't automatically
172 consume CPP_PRAGMA_EOL. */
173 BOOL_BITFIELD in_pragma : 1;
b4b56033
MLI
174 /* True if we're parsing the outermost block of an if statement. */
175 BOOL_BITFIELD in_if_block : 1;
46c2514e
TT
176 /* True if we want to lex an untranslated string. */
177 BOOL_BITFIELD lex_untranslated_string : 1;
0bacb8c7
TT
178 /* Objective-C specific parser/lexer information. */
179 BOOL_BITFIELD objc_pq_context : 1;
180 /* The following flag is needed to contextualize Objective-C lexical
181 analysis. In some cases (e.g., 'int NSObject;'), it is
182 undesirable to bind an identifier to an Objective-C class, even
183 if a class with that name exists. */
184 BOOL_BITFIELD objc_need_raw_identifier : 1;
27bf414c
JM
185} c_parser;
186
bc4071dd
RH
187
188/* The actual parser and external interface. ??? Does this need to be
189 garbage-collected? */
190
191static GTY (()) c_parser *the_parser;
192
193
27bf414c
JM
194/* Read in and lex a single token, storing it in *TOKEN. */
195
196static void
0bacb8c7 197c_lex_one_token (c_parser *parser, c_token *token)
27bf414c
JM
198{
199 timevar_push (TV_LEX);
bc4071dd 200
46c2514e
TT
201 token->type = c_lex_with_flags (&token->value, &token->location, NULL,
202 (parser->lex_untranslated_string
203 ? C_LEX_STRING_NO_TRANSLATE : 0));
bc4071dd
RH
204 token->id_kind = C_ID_NONE;
205 token->keyword = RID_MAX;
206 token->pragma_kind = PRAGMA_NONE;
bc4071dd 207
27bf414c
JM
208 switch (token->type)
209 {
210 case CPP_NAME:
27bf414c
JM
211 {
212 tree decl;
213
0bacb8c7
TT
214 bool objc_force_identifier = parser->objc_need_raw_identifier;
215 if (c_dialect_objc ())
216 parser->objc_need_raw_identifier = false;
27bf414c
JM
217
218 if (C_IS_RESERVED_WORD (token->value))
219 {
220 enum rid rid_code = C_RID_CODE (token->value);
221
eea1139b
ILT
222 if (rid_code == RID_CXX_COMPAT_WARN)
223 {
3ba09659
AH
224 warning_at (token->location,
225 OPT_Wc___compat,
88388a52
JM
226 "identifier %qE conflicts with C++ keyword",
227 token->value);
eea1139b
ILT
228 }
229 else if (c_dialect_objc ())
27bf414c 230 {
eea1139b 231 if (!objc_is_reserved_word (token->value)
0bacb8c7
TT
232 && (!OBJC_IS_PQ_KEYWORD (rid_code)
233 || parser->objc_pq_context))
27bf414c
JM
234 {
235 /* Return the canonical spelling for this keyword. */
236 token->value = ridpointers[(int) rid_code];
237 token->type = CPP_KEYWORD;
238 token->keyword = rid_code;
239 break;
240 }
241 }
242 else
243 {
27bf414c
JM
244 token->type = CPP_KEYWORD;
245 token->keyword = rid_code;
246 break;
247 }
248 }
249
250 decl = lookup_name (token->value);
251 if (decl)
252 {
253 if (TREE_CODE (decl) == TYPE_DECL)
254 {
255 token->id_kind = C_ID_TYPENAME;
256 break;
257 }
258 }
259 else if (c_dialect_objc ())
260 {
261 tree objc_interface_decl = objc_is_class_name (token->value);
262 /* Objective-C class names are in the same namespace as
263 variables and typedefs, and hence are shadowed by local
264 declarations. */
265 if (objc_interface_decl
266 && (global_bindings_p ()
267 || (!objc_force_identifier && !decl)))
268 {
269 token->value = objc_interface_decl;
270 token->id_kind = C_ID_CLASSNAME;
271 break;
272 }
273 }
bc4071dd 274 token->id_kind = C_ID_ID;
27bf414c 275 }
27bf414c
JM
276 break;
277 case CPP_AT_NAME:
278 /* This only happens in Objective-C; it must be a keyword. */
279 token->type = CPP_KEYWORD;
27bf414c
JM
280 token->keyword = C_RID_CODE (token->value);
281 break;
282 case CPP_COLON:
283 case CPP_COMMA:
284 case CPP_CLOSE_PAREN:
285 case CPP_SEMICOLON:
286 /* These tokens may affect the interpretation of any identifiers
287 following, if doing Objective-C. */
0bacb8c7
TT
288 if (c_dialect_objc ())
289 parser->objc_need_raw_identifier = false;
bc4071dd
RH
290 break;
291 case CPP_PRAGMA:
292 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
d75d71e0 293 token->pragma_kind = (enum pragma_kind) TREE_INT_CST_LOW (token->value);
bc4071dd 294 token->value = NULL;
27bf414c
JM
295 break;
296 default:
27bf414c
JM
297 break;
298 }
299 timevar_pop (TV_LEX);
300}
301
302/* Return a pointer to the next token from PARSER, reading it in if
303 necessary. */
304
305static inline c_token *
306c_parser_peek_token (c_parser *parser)
307{
308 if (parser->tokens_avail == 0)
309 {
0bacb8c7 310 c_lex_one_token (parser, &parser->tokens[0]);
27bf414c
JM
311 parser->tokens_avail = 1;
312 }
313 return &parser->tokens[0];
314}
315
316/* Return true if the next token from PARSER has the indicated
317 TYPE. */
318
319static inline bool
320c_parser_next_token_is (c_parser *parser, enum cpp_ttype type)
321{
322 return c_parser_peek_token (parser)->type == type;
323}
324
325/* Return true if the next token from PARSER does not have the
326 indicated TYPE. */
327
328static inline bool
329c_parser_next_token_is_not (c_parser *parser, enum cpp_ttype type)
330{
331 return !c_parser_next_token_is (parser, type);
332}
333
334/* Return true if the next token from PARSER is the indicated
335 KEYWORD. */
336
337static inline bool
338c_parser_next_token_is_keyword (c_parser *parser, enum rid keyword)
339{
dbc518f0 340 return c_parser_peek_token (parser)->keyword == keyword;
27bf414c
JM
341}
342
343/* Return true if TOKEN can start a type name,
344 false otherwise. */
345static bool
346c_token_starts_typename (c_token *token)
347{
348 switch (token->type)
349 {
350 case CPP_NAME:
351 switch (token->id_kind)
352 {
353 case C_ID_ID:
354 return false;
355 case C_ID_TYPENAME:
356 return true;
357 case C_ID_CLASSNAME:
358 gcc_assert (c_dialect_objc ());
359 return true;
360 default:
361 gcc_unreachable ();
362 }
363 case CPP_KEYWORD:
364 switch (token->keyword)
365 {
366 case RID_UNSIGNED:
367 case RID_LONG:
368 case RID_SHORT:
369 case RID_SIGNED:
370 case RID_COMPLEX:
371 case RID_INT:
372 case RID_CHAR:
373 case RID_FLOAT:
374 case RID_DOUBLE:
375 case RID_VOID:
9a8ce21f
JG
376 case RID_DFLOAT32:
377 case RID_DFLOAT64:
378 case RID_DFLOAT128:
27bf414c
JM
379 case RID_BOOL:
380 case RID_ENUM:
381 case RID_STRUCT:
382 case RID_UNION:
383 case RID_TYPEOF:
384 case RID_CONST:
385 case RID_VOLATILE:
386 case RID_RESTRICT:
387 case RID_ATTRIBUTE:
ab22c1fa
CF
388 case RID_FRACT:
389 case RID_ACCUM:
390 case RID_SAT:
27bf414c
JM
391 return true;
392 default:
393 return false;
394 }
395 case CPP_LESS:
396 if (c_dialect_objc ())
397 return true;
398 return false;
399 default:
400 return false;
401 }
402}
403
404/* Return true if the next token from PARSER can start a type name,
405 false otherwise. */
406static inline bool
407c_parser_next_token_starts_typename (c_parser *parser)
408{
409 c_token *token = c_parser_peek_token (parser);
410 return c_token_starts_typename (token);
411}
412
413/* Return true if TOKEN can start declaration specifiers, false
414 otherwise. */
415static bool
416c_token_starts_declspecs (c_token *token)
417{
418 switch (token->type)
419 {
420 case CPP_NAME:
421 switch (token->id_kind)
422 {
423 case C_ID_ID:
424 return false;
425 case C_ID_TYPENAME:
426 return true;
427 case C_ID_CLASSNAME:
428 gcc_assert (c_dialect_objc ());
429 return true;
430 default:
431 gcc_unreachable ();
432 }
433 case CPP_KEYWORD:
434 switch (token->keyword)
435 {
436 case RID_STATIC:
437 case RID_EXTERN:
438 case RID_REGISTER:
439 case RID_TYPEDEF:
440 case RID_INLINE:
441 case RID_AUTO:
442 case RID_THREAD:
443 case RID_UNSIGNED:
444 case RID_LONG:
445 case RID_SHORT:
446 case RID_SIGNED:
447 case RID_COMPLEX:
448 case RID_INT:
449 case RID_CHAR:
450 case RID_FLOAT:
451 case RID_DOUBLE:
452 case RID_VOID:
9a8ce21f
JG
453 case RID_DFLOAT32:
454 case RID_DFLOAT64:
455 case RID_DFLOAT128:
27bf414c
JM
456 case RID_BOOL:
457 case RID_ENUM:
458 case RID_STRUCT:
459 case RID_UNION:
460 case RID_TYPEOF:
461 case RID_CONST:
462 case RID_VOLATILE:
463 case RID_RESTRICT:
464 case RID_ATTRIBUTE:
ab22c1fa
CF
465 case RID_FRACT:
466 case RID_ACCUM:
467 case RID_SAT:
27bf414c
JM
468 return true;
469 default:
470 return false;
471 }
472 case CPP_LESS:
473 if (c_dialect_objc ())
474 return true;
475 return false;
476 default:
477 return false;
478 }
479}
480
481/* Return true if the next token from PARSER can start declaration
482 specifiers, false otherwise. */
483static inline bool
484c_parser_next_token_starts_declspecs (c_parser *parser)
485{
486 c_token *token = c_parser_peek_token (parser);
487 return c_token_starts_declspecs (token);
488}
489
490/* Return a pointer to the next-but-one token from PARSER, reading it
491 in if necessary. The next token is already read in. */
492
493static c_token *
494c_parser_peek_2nd_token (c_parser *parser)
495{
496 if (parser->tokens_avail >= 2)
497 return &parser->tokens[1];
498 gcc_assert (parser->tokens_avail == 1);
499 gcc_assert (parser->tokens[0].type != CPP_EOF);
bc4071dd 500 gcc_assert (parser->tokens[0].type != CPP_PRAGMA_EOL);
0bacb8c7 501 c_lex_one_token (parser, &parser->tokens[1]);
27bf414c
JM
502 parser->tokens_avail = 2;
503 return &parser->tokens[1];
504}
505
506/* Consume the next token from PARSER. */
507
508static void
509c_parser_consume_token (c_parser *parser)
510{
bc4071dd
RH
511 gcc_assert (parser->tokens_avail >= 1);
512 gcc_assert (parser->tokens[0].type != CPP_EOF);
513 gcc_assert (!parser->in_pragma || parser->tokens[0].type != CPP_PRAGMA_EOL);
514 gcc_assert (parser->error || parser->tokens[0].type != CPP_PRAGMA);
27bf414c
JM
515 if (parser->tokens_avail == 2)
516 parser->tokens[0] = parser->tokens[1];
27bf414c
JM
517 parser->tokens_avail--;
518}
519
bc4071dd
RH
520/* Expect the current token to be a #pragma. Consume it and remember
521 that we've begun parsing a pragma. */
522
523static void
524c_parser_consume_pragma (c_parser *parser)
525{
526 gcc_assert (!parser->in_pragma);
527 gcc_assert (parser->tokens_avail >= 1);
528 gcc_assert (parser->tokens[0].type == CPP_PRAGMA);
529 if (parser->tokens_avail == 2)
530 parser->tokens[0] = parser->tokens[1];
531 parser->tokens_avail--;
532 parser->in_pragma = true;
533}
534
27bf414c
JM
535/* Update the globals input_location and in_system_header from
536 TOKEN. */
537static inline void
538c_parser_set_source_position_from_token (c_token *token)
539{
540 if (token->type != CPP_EOF)
541 {
542 input_location = token->location;
27bf414c
JM
543 }
544}
545
27bf414c
JM
546/* Issue a diagnostic of the form
547 FILE:LINE: MESSAGE before TOKEN
548 where TOKEN is the next token in the input stream of PARSER.
549 MESSAGE (specified by the caller) is usually of the form "expected
550 OTHER-TOKEN".
551
552 Do not issue a diagnostic if still recovering from an error.
553
554 ??? This is taken from the C++ parser, but building up messages in
555 this way is not i18n-friendly and some other approach should be
556 used. */
557
558static void
4b794eaf 559c_parser_error (c_parser *parser, const char *gmsgid)
27bf414c
JM
560{
561 c_token *token = c_parser_peek_token (parser);
562 if (parser->error)
563 return;
564 parser->error = true;
4b794eaf 565 if (!gmsgid)
27bf414c
JM
566 return;
567 /* This diagnostic makes more sense if it is tagged to the line of
568 the token we just peeked at. */
569 c_parser_set_source_position_from_token (token);
4b794eaf 570 c_parse_error (gmsgid,
27bf414c
JM
571 /* Because c_parse_error does not understand
572 CPP_KEYWORD, keywords are treated like
573 identifiers. */
574 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
cfc93532
MLI
575 /* ??? The C parser does not save the cpp flags of a
576 token, we need to pass 0 here and we will not get
577 the source spelling of some tokens but rather the
578 canonical spelling. */
579 token->value, /*flags=*/0);
27bf414c
JM
580}
581
582/* If the next token is of the indicated TYPE, consume it. Otherwise,
583 issue the error MSGID. If MSGID is NULL then a message has already
584 been produced and no message will be produced this time. Returns
585 true if found, false otherwise. */
586
587static bool
588c_parser_require (c_parser *parser,
589 enum cpp_ttype type,
590 const char *msgid)
591{
592 if (c_parser_next_token_is (parser, type))
593 {
594 c_parser_consume_token (parser);
595 return true;
596 }
597 else
598 {
599 c_parser_error (parser, msgid);
600 return false;
601 }
602}
603
604/* If the next token is the indicated keyword, consume it. Otherwise,
605 issue the error MSGID. Returns true if found, false otherwise. */
606
607static bool
608c_parser_require_keyword (c_parser *parser,
609 enum rid keyword,
610 const char *msgid)
611{
612 if (c_parser_next_token_is_keyword (parser, keyword))
613 {
614 c_parser_consume_token (parser);
615 return true;
616 }
617 else
618 {
619 c_parser_error (parser, msgid);
620 return false;
621 }
622}
623
624/* Like c_parser_require, except that tokens will be skipped until the
625 desired token is found. An error message is still produced if the
626 next token is not as expected. If MSGID is NULL then a message has
627 already been produced and no message will be produced this
628 time. */
629
630static void
631c_parser_skip_until_found (c_parser *parser,
632 enum cpp_ttype type,
633 const char *msgid)
634{
635 unsigned nesting_depth = 0;
636
637 if (c_parser_require (parser, type, msgid))
638 return;
639
640 /* Skip tokens until the desired token is found. */
641 while (true)
642 {
643 /* Peek at the next token. */
644 c_token *token = c_parser_peek_token (parser);
645 /* If we've reached the token we want, consume it and stop. */
646 if (token->type == type && !nesting_depth)
647 {
648 c_parser_consume_token (parser);
649 break;
650 }
bc4071dd 651
27bf414c
JM
652 /* If we've run out of tokens, stop. */
653 if (token->type == CPP_EOF)
654 return;
bc4071dd
RH
655 if (token->type == CPP_PRAGMA_EOL && parser->in_pragma)
656 return;
27bf414c
JM
657 if (token->type == CPP_OPEN_BRACE
658 || token->type == CPP_OPEN_PAREN
659 || token->type == CPP_OPEN_SQUARE)
660 ++nesting_depth;
661 else if (token->type == CPP_CLOSE_BRACE
662 || token->type == CPP_CLOSE_PAREN
663 || token->type == CPP_CLOSE_SQUARE)
664 {
665 if (nesting_depth-- == 0)
666 break;
667 }
668 /* Consume this token. */
669 c_parser_consume_token (parser);
670 }
671 parser->error = false;
672}
673
674/* Skip tokens until the end of a parameter is found, but do not
675 consume the comma, semicolon or closing delimiter. */
676
677static void
678c_parser_skip_to_end_of_parameter (c_parser *parser)
679{
680 unsigned nesting_depth = 0;
681
682 while (true)
683 {
684 c_token *token = c_parser_peek_token (parser);
685 if ((token->type == CPP_COMMA || token->type == CPP_SEMICOLON)
686 && !nesting_depth)
687 break;
688 /* If we've run out of tokens, stop. */
689 if (token->type == CPP_EOF)
690 return;
bc4071dd
RH
691 if (token->type == CPP_PRAGMA_EOL && parser->in_pragma)
692 return;
27bf414c
JM
693 if (token->type == CPP_OPEN_BRACE
694 || token->type == CPP_OPEN_PAREN
695 || token->type == CPP_OPEN_SQUARE)
696 ++nesting_depth;
697 else if (token->type == CPP_CLOSE_BRACE
698 || token->type == CPP_CLOSE_PAREN
699 || token->type == CPP_CLOSE_SQUARE)
700 {
701 if (nesting_depth-- == 0)
702 break;
703 }
704 /* Consume this token. */
705 c_parser_consume_token (parser);
706 }
707 parser->error = false;
708}
709
bc4071dd
RH
710/* Expect to be at the end of the pragma directive and consume an
711 end of line marker. */
712
713static void
714c_parser_skip_to_pragma_eol (c_parser *parser)
715{
716 gcc_assert (parser->in_pragma);
717 parser->in_pragma = false;
718
719 if (!c_parser_require (parser, CPP_PRAGMA_EOL, "expected end of line"))
720 while (true)
721 {
722 c_token *token = c_parser_peek_token (parser);
723 if (token->type == CPP_EOF)
724 break;
725 if (token->type == CPP_PRAGMA_EOL)
726 {
727 c_parser_consume_token (parser);
728 break;
729 }
730 c_parser_consume_token (parser);
731 }
732
733 parser->error = false;
734}
27bf414c 735
2a83cc52
RH
736/* Skip tokens until we have consumed an entire block, or until we
737 have consumed a non-nested ';'. */
738
739static void
740c_parser_skip_to_end_of_block_or_statement (c_parser *parser)
741{
742 unsigned nesting_depth = 0;
743 bool save_error = parser->error;
744
745 while (true)
746 {
747 c_token *token;
748
749 /* Peek at the next token. */
750 token = c_parser_peek_token (parser);
751
752 switch (token->type)
753 {
754 case CPP_EOF:
755 return;
756
757 case CPP_PRAGMA_EOL:
758 if (parser->in_pragma)
759 return;
760 break;
761
762 case CPP_SEMICOLON:
763 /* If the next token is a ';', we have reached the
764 end of the statement. */
765 if (!nesting_depth)
766 {
767 /* Consume the ';'. */
768 c_parser_consume_token (parser);
769 goto finished;
770 }
771 break;
772
773 case CPP_CLOSE_BRACE:
774 /* If the next token is a non-nested '}', then we have
775 reached the end of the current block. */
776 if (nesting_depth == 0 || --nesting_depth == 0)
777 {
778 c_parser_consume_token (parser);
779 goto finished;
780 }
781 break;
782
783 case CPP_OPEN_BRACE:
784 /* If it the next token is a '{', then we are entering a new
785 block. Consume the entire block. */
786 ++nesting_depth;
787 break;
788
789 case CPP_PRAGMA:
790 /* If we see a pragma, consume the whole thing at once. We
791 have some safeguards against consuming pragmas willy-nilly.
792 Normally, we'd expect to be here with parser->error set,
793 which disables these safeguards. But it's possible to get
794 here for secondary error recovery, after parser->error has
795 been cleared. */
796 c_parser_consume_pragma (parser);
797 c_parser_skip_to_pragma_eol (parser);
798 parser->error = save_error;
799 continue;
9e33de05
RS
800
801 default:
802 break;
2a83cc52
RH
803 }
804
805 c_parser_consume_token (parser);
806 }
807
808 finished:
809 parser->error = false;
810}
811
d2e796ad
MLI
812/* CPP's options (initialized by c-opts.c). */
813extern cpp_options *cpp_opts;
814
27bf414c
JM
815/* Save the warning flags which are controlled by __extension__. */
816
817static inline int
818disable_extension_diagnostics (void)
819{
820 int ret = (pedantic
821 | (warn_pointer_arith << 1)
822 | (warn_traditional << 2)
d2e796ad 823 | (flag_iso << 3)
24b97832
ILT
824 | (warn_long_long << 4)
825 | (warn_cxx_compat << 5));
d2e796ad 826 cpp_opts->pedantic = pedantic = 0;
27bf414c 827 warn_pointer_arith = 0;
d2e796ad 828 cpp_opts->warn_traditional = warn_traditional = 0;
27bf414c 829 flag_iso = 0;
9c650d90 830 cpp_opts->warn_long_long = warn_long_long = 0;
24b97832 831 warn_cxx_compat = 0;
27bf414c
JM
832 return ret;
833}
834
835/* Restore the warning flags which are controlled by __extension__.
836 FLAGS is the return value from disable_extension_diagnostics. */
837
838static inline void
839restore_extension_diagnostics (int flags)
840{
d2e796ad 841 cpp_opts->pedantic = pedantic = flags & 1;
27bf414c 842 warn_pointer_arith = (flags >> 1) & 1;
d2e796ad 843 cpp_opts->warn_traditional = warn_traditional = (flags >> 2) & 1;
27bf414c 844 flag_iso = (flags >> 3) & 1;
9c650d90 845 cpp_opts->warn_long_long = warn_long_long = (flags >> 4) & 1;
24b97832 846 warn_cxx_compat = (flags >> 5) & 1;
27bf414c
JM
847}
848
849/* Possibly kinds of declarator to parse. */
850typedef enum c_dtr_syn {
851 /* A normal declarator with an identifier. */
852 C_DTR_NORMAL,
853 /* An abstract declarator (maybe empty). */
854 C_DTR_ABSTRACT,
855 /* A parameter declarator: may be either, but after a type name does
856 not redeclare a typedef name as an identifier if it can
857 alternatively be interpreted as a typedef name; see DR#009,
858 applied in C90 TC1, omitted from C99 and reapplied in C99 TC2
859 following DR#249. For example, given a typedef T, "int T" and
860 "int *T" are valid parameter declarations redeclaring T, while
861 "int (T)" and "int * (T)" and "int (T[])" and "int (T (int))" are
862 abstract declarators rather than involving redundant parentheses;
863 the same applies with attributes inside the parentheses before
864 "T". */
865 C_DTR_PARM
866} c_dtr_syn;
867
868static void c_parser_external_declaration (c_parser *);
869static void c_parser_asm_definition (c_parser *);
870static void c_parser_declaration_or_fndef (c_parser *, bool, bool, bool, bool);
871static void c_parser_declspecs (c_parser *, struct c_declspecs *, bool, bool,
872 bool);
873static struct c_typespec c_parser_enum_specifier (c_parser *);
874static struct c_typespec c_parser_struct_or_union_specifier (c_parser *);
875static tree c_parser_struct_declaration (c_parser *);
876static struct c_typespec c_parser_typeof_specifier (c_parser *);
877static struct c_declarator *c_parser_declarator (c_parser *, bool, c_dtr_syn,
878 bool *);
879static struct c_declarator *c_parser_direct_declarator (c_parser *, bool,
880 c_dtr_syn, bool *);
881static struct c_declarator *c_parser_direct_declarator_inner (c_parser *,
882 bool,
883 struct c_declarator *);
884static struct c_arg_info *c_parser_parms_declarator (c_parser *, bool, tree);
885static struct c_arg_info *c_parser_parms_list_declarator (c_parser *, tree);
886static struct c_parm *c_parser_parameter_declaration (c_parser *, tree);
887static tree c_parser_simple_asm_expr (c_parser *);
888static tree c_parser_attributes (c_parser *);
889static struct c_type_name *c_parser_type_name (c_parser *);
890static struct c_expr c_parser_initializer (c_parser *);
891static struct c_expr c_parser_braced_init (c_parser *, tree, bool);
892static void c_parser_initelt (c_parser *);
893static void c_parser_initval (c_parser *, struct c_expr *);
894static tree c_parser_compound_statement (c_parser *);
895static void c_parser_compound_statement_nostart (c_parser *);
896static void c_parser_label (c_parser *);
897static void c_parser_statement (c_parser *);
898static void c_parser_statement_after_labels (c_parser *);
899static void c_parser_if_statement (c_parser *);
900static void c_parser_switch_statement (c_parser *);
901static void c_parser_while_statement (c_parser *);
902static void c_parser_do_statement (c_parser *);
903static void c_parser_for_statement (c_parser *);
904static tree c_parser_asm_statement (c_parser *);
46bdb9cf 905static tree c_parser_asm_operands (c_parser *, bool);
27bf414c
JM
906static tree c_parser_asm_clobbers (c_parser *);
907static struct c_expr c_parser_expr_no_commas (c_parser *, struct c_expr *);
908static struct c_expr c_parser_conditional_expression (c_parser *,
909 struct c_expr *);
910static struct c_expr c_parser_binary_expression (c_parser *, struct c_expr *);
911static struct c_expr c_parser_cast_expression (c_parser *, struct c_expr *);
912static struct c_expr c_parser_unary_expression (c_parser *);
913static struct c_expr c_parser_sizeof_expression (c_parser *);
914static struct c_expr c_parser_alignof_expression (c_parser *);
915static struct c_expr c_parser_postfix_expression (c_parser *);
916static struct c_expr c_parser_postfix_expression_after_paren_type (c_parser *,
24b97832
ILT
917 struct c_type_name *,
918 location_t);
27bf414c 919static struct c_expr c_parser_postfix_expression_after_primary (c_parser *,
c2255bc4 920 location_t loc,
27bf414c
JM
921 struct c_expr);
922static struct c_expr c_parser_expression (c_parser *);
46bdb9cf 923static struct c_expr c_parser_expression_conv (c_parser *);
bbbbb16a
ILT
924static VEC(tree,gc) *c_parser_expr_list (c_parser *, bool, bool,
925 VEC(tree,gc) **);
953ff289
DN
926static void c_parser_omp_construct (c_parser *);
927static void c_parser_omp_threadprivate (c_parser *);
928static void c_parser_omp_barrier (c_parser *);
929static void c_parser_omp_flush (c_parser *);
a68ab351 930static void c_parser_omp_taskwait (c_parser *);
27bf414c 931
bc4071dd
RH
932enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
933static bool c_parser_pragma (c_parser *, enum pragma_context);
934
27bf414c
JM
935/* These Objective-C parser functions are only ever called when
936 compiling Objective-C. */
937static void c_parser_objc_class_definition (c_parser *);
938static void c_parser_objc_class_instance_variables (c_parser *);
939static void c_parser_objc_class_declaration (c_parser *);
940static void c_parser_objc_alias_declaration (c_parser *);
941static void c_parser_objc_protocol_definition (c_parser *);
942static enum tree_code c_parser_objc_method_type (c_parser *);
943static void c_parser_objc_method_definition (c_parser *);
944static void c_parser_objc_methodprotolist (c_parser *);
945static void c_parser_objc_methodproto (c_parser *);
946static tree c_parser_objc_method_decl (c_parser *);
947static tree c_parser_objc_type_name (c_parser *);
948static tree c_parser_objc_protocol_refs (c_parser *);
949static void c_parser_objc_try_catch_statement (c_parser *);
950static void c_parser_objc_synchronized_statement (c_parser *);
951static tree c_parser_objc_selector (c_parser *);
952static tree c_parser_objc_selector_arg (c_parser *);
953static tree c_parser_objc_receiver (c_parser *);
954static tree c_parser_objc_message_args (c_parser *);
955static tree c_parser_objc_keywordexpr (c_parser *);
956
957/* Parse a translation unit (C90 6.7, C99 6.9).
958
959 translation-unit:
960 external-declarations
961
962 external-declarations:
963 external-declaration
964 external-declarations external-declaration
965
966 GNU extensions:
967
968 translation-unit:
969 empty
970*/
971
972static void
973c_parser_translation_unit (c_parser *parser)
974{
975 if (c_parser_next_token_is (parser, CPP_EOF))
976 {
509c9d60
MLI
977 pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic,
978 "ISO C forbids an empty translation unit");
27bf414c
JM
979 }
980 else
981 {
982 void *obstack_position = obstack_alloc (&parser_obstack, 0);
6ec637a4 983 mark_valid_location_for_stdc_pragma (false);
27bf414c
JM
984 do
985 {
986 ggc_collect ();
987 c_parser_external_declaration (parser);
988 obstack_free (&parser_obstack, obstack_position);
989 }
990 while (c_parser_next_token_is_not (parser, CPP_EOF));
991 }
992}
993
994/* Parse an external declaration (C90 6.7, C99 6.9).
995
996 external-declaration:
997 function-definition
998 declaration
999
1000 GNU extensions:
1001
1002 external-declaration:
1003 asm-definition
1004 ;
1005 __extension__ external-declaration
1006
1007 Objective-C:
1008
1009 external-declaration:
1010 objc-class-definition
1011 objc-class-declaration
1012 objc-alias-declaration
1013 objc-protocol-definition
1014 objc-method-definition
1015 @end
1016*/
1017
1018static void
1019c_parser_external_declaration (c_parser *parser)
1020{
1021 int ext;
1022 switch (c_parser_peek_token (parser)->type)
1023 {
1024 case CPP_KEYWORD:
1025 switch (c_parser_peek_token (parser)->keyword)
1026 {
1027 case RID_EXTENSION:
1028 ext = disable_extension_diagnostics ();
1029 c_parser_consume_token (parser);
1030 c_parser_external_declaration (parser);
1031 restore_extension_diagnostics (ext);
1032 break;
1033 case RID_ASM:
1034 c_parser_asm_definition (parser);
1035 break;
1036 case RID_AT_INTERFACE:
1037 case RID_AT_IMPLEMENTATION:
1038 gcc_assert (c_dialect_objc ());
1039 c_parser_objc_class_definition (parser);
1040 break;
eea1139b 1041 case RID_CLASS:
27bf414c
JM
1042 gcc_assert (c_dialect_objc ());
1043 c_parser_objc_class_declaration (parser);
1044 break;
1045 case RID_AT_ALIAS:
1046 gcc_assert (c_dialect_objc ());
1047 c_parser_objc_alias_declaration (parser);
1048 break;
1049 case RID_AT_PROTOCOL:
1050 gcc_assert (c_dialect_objc ());
1051 c_parser_objc_protocol_definition (parser);
1052 break;
1053 case RID_AT_END:
1054 gcc_assert (c_dialect_objc ());
1055 c_parser_consume_token (parser);
1056 objc_finish_implementation ();
1057 break;
1058 default:
1059 goto decl_or_fndef;
1060 }
1061 break;
1062 case CPP_SEMICOLON:
509c9d60
MLI
1063 pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic,
1064 "ISO C does not allow extra %<;%> outside of a function");
27bf414c
JM
1065 c_parser_consume_token (parser);
1066 break;
bc4071dd 1067 case CPP_PRAGMA:
6ec637a4 1068 mark_valid_location_for_stdc_pragma (true);
bc4071dd 1069 c_parser_pragma (parser, pragma_external);
6ec637a4 1070 mark_valid_location_for_stdc_pragma (false);
bc4071dd 1071 break;
27bf414c
JM
1072 case CPP_PLUS:
1073 case CPP_MINUS:
1074 if (c_dialect_objc ())
1075 {
1076 c_parser_objc_method_definition (parser);
1077 break;
1078 }
1079 /* Else fall through, and yield a syntax error trying to parse
1080 as a declaration or function definition. */
1081 default:
1082 decl_or_fndef:
1083 /* A declaration or a function definition. We can only tell
1084 which after parsing the declaration specifiers, if any, and
1085 the first declarator. */
1086 c_parser_declaration_or_fndef (parser, true, true, false, true);
1087 break;
1088 }
1089}
1090
bc4071dd 1091
27bf414c
JM
1092/* Parse a declaration or function definition (C90 6.5, 6.7.1, C99
1093 6.7, 6.9.1). If FNDEF_OK is true, a function definition is
1094 accepted; otherwise (old-style parameter declarations) only other
1095 declarations are accepted. If NESTED is true, we are inside a
1096 function or parsing old-style parameter declarations; any functions
1097 encountered are nested functions and declaration specifiers are
1098 required; otherwise we are at top level and functions are normal
1099 functions and declaration specifiers may be optional. If EMPTY_OK
1100 is true, empty declarations are OK (subject to all other
1101 constraints); otherwise (old-style parameter declarations) they are
1102 diagnosed. If START_ATTR_OK is true, the declaration specifiers
1103 may start with attributes; otherwise they may not.
1104
1105 declaration:
1106 declaration-specifiers init-declarator-list[opt] ;
1107
1108 function-definition:
1109 declaration-specifiers[opt] declarator declaration-list[opt]
1110 compound-statement
1111
1112 declaration-list:
1113 declaration
1114 declaration-list declaration
1115
1116 init-declarator-list:
1117 init-declarator
1118 init-declarator-list , init-declarator
1119
1120 init-declarator:
1121 declarator simple-asm-expr[opt] attributes[opt]
1122 declarator simple-asm-expr[opt] attributes[opt] = initializer
1123
1124 GNU extensions:
1125
1126 nested-function-definition:
1127 declaration-specifiers declarator declaration-list[opt]
1128 compound-statement
1129
1130 The simple-asm-expr and attributes are GNU extensions.
1131
1132 This function does not handle __extension__; that is handled in its
1133 callers. ??? Following the old parser, __extension__ may start
1134 external declarations, declarations in functions and declarations
1135 at the start of "for" loops, but not old-style parameter
1136 declarations.
1137
1138 C99 requires declaration specifiers in a function definition; the
1139 absence is diagnosed through the diagnosis of implicit int. In GNU
1140 C we also allow but diagnose declarations without declaration
1141 specifiers, but only at top level (elsewhere they conflict with
953ff289
DN
1142 other syntax).
1143
1144 OpenMP:
1145
1146 declaration:
1147 threadprivate-directive */
27bf414c
JM
1148
1149static void
1150c_parser_declaration_or_fndef (c_parser *parser, bool fndef_ok, bool empty_ok,
1151 bool nested, bool start_attr_ok)
1152{
1153 struct c_declspecs *specs;
1154 tree prefix_attrs;
1155 tree all_prefix_attrs;
1156 bool diagnosed_no_specs = false;
c7412148 1157 location_t here = c_parser_peek_token (parser)->location;
bc4071dd 1158
27bf414c
JM
1159 specs = build_null_declspecs ();
1160 c_parser_declspecs (parser, specs, true, true, start_attr_ok);
1161 if (parser->error)
1162 {
1163 c_parser_skip_to_end_of_block_or_statement (parser);
1164 return;
1165 }
1166 if (nested && !specs->declspecs_seen_p)
1167 {
1168 c_parser_error (parser, "expected declaration specifiers");
1169 c_parser_skip_to_end_of_block_or_statement (parser);
1170 return;
1171 }
1172 finish_declspecs (specs);
1173 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1174 {
1175 if (empty_ok)
1176 shadow_tag (specs);
1177 else
1178 {
1179 shadow_tag_warned (specs, 1);
509c9d60 1180 pedwarn (here, 0, "empty declaration");
27bf414c
JM
1181 }
1182 c_parser_consume_token (parser);
1183 return;
1184 }
1185 pending_xref_error ();
1186 prefix_attrs = specs->attrs;
1187 all_prefix_attrs = prefix_attrs;
1188 specs->attrs = NULL_TREE;
1189 while (true)
1190 {
1191 struct c_declarator *declarator;
1192 bool dummy = false;
1193 tree fnbody;
1194 /* Declaring either one or more declarators (in which case we
1195 should diagnose if there were no declaration specifiers) or a
1196 function definition (in which case the diagnostic for
1197 implicit int suffices). */
1198 declarator = c_parser_declarator (parser, specs->type_seen_p,
1199 C_DTR_NORMAL, &dummy);
1200 if (declarator == NULL)
1201 {
1202 c_parser_skip_to_end_of_block_or_statement (parser);
1203 return;
1204 }
1205 if (c_parser_next_token_is (parser, CPP_EQ)
1206 || c_parser_next_token_is (parser, CPP_COMMA)
1207 || c_parser_next_token_is (parser, CPP_SEMICOLON)
1208 || c_parser_next_token_is_keyword (parser, RID_ASM)
1209 || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1210 {
1211 tree asm_name = NULL_TREE;
1212 tree postfix_attrs = NULL_TREE;
1213 if (!diagnosed_no_specs && !specs->declspecs_seen_p)
1214 {
1215 diagnosed_no_specs = true;
509c9d60 1216 pedwarn (here, 0, "data definition has no type or storage class");
27bf414c
JM
1217 }
1218 /* Having seen a data definition, there cannot now be a
1219 function definition. */
1220 fndef_ok = false;
1221 if (c_parser_next_token_is_keyword (parser, RID_ASM))
1222 asm_name = c_parser_simple_asm_expr (parser);
1223 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1224 postfix_attrs = c_parser_attributes (parser);
1225 if (c_parser_next_token_is (parser, CPP_EQ))
1226 {
1227 tree d;
1228 struct c_expr init;
c2255bc4 1229 location_t init_loc;
27bf414c
JM
1230 c_parser_consume_token (parser);
1231 /* The declaration of the variable is in effect while
1232 its initializer is parsed. */
1233 d = start_decl (declarator, specs, true,
1234 chainon (postfix_attrs, all_prefix_attrs));
1235 if (!d)
1236 d = error_mark_node;
1237 start_init (d, asm_name, global_bindings_p ());
c2255bc4 1238 init_loc = c_parser_peek_token (parser)->location;
27bf414c
JM
1239 init = c_parser_initializer (parser);
1240 finish_init ();
1241 if (d != error_mark_node)
1242 {
1243 maybe_warn_string_init (TREE_TYPE (d), init);
c2255bc4
AH
1244 finish_decl (d, init_loc, init.value,
1245 init.original_type, asm_name);
27bf414c
JM
1246 }
1247 }
1248 else
1249 {
1250 tree d = start_decl (declarator, specs, false,
1251 chainon (postfix_attrs,
1252 all_prefix_attrs));
1253 if (d)
c2255bc4
AH
1254 finish_decl (d, UNKNOWN_LOCATION, NULL_TREE,
1255 NULL_TREE, asm_name);
27bf414c
JM
1256 }
1257 if (c_parser_next_token_is (parser, CPP_COMMA))
1258 {
1259 c_parser_consume_token (parser);
1260 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1261 all_prefix_attrs = chainon (c_parser_attributes (parser),
1262 prefix_attrs);
1263 else
1264 all_prefix_attrs = prefix_attrs;
1265 continue;
1266 }
1267 else if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1268 {
1269 c_parser_consume_token (parser);
1270 return;
1271 }
1272 else
1273 {
1274 c_parser_error (parser, "expected %<,%> or %<;%>");
1275 c_parser_skip_to_end_of_block_or_statement (parser);
1276 return;
1277 }
1278 }
1279 else if (!fndef_ok)
1280 {
1281 c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, "
1282 "%<asm%> or %<__attribute__%>");
1283 c_parser_skip_to_end_of_block_or_statement (parser);
1284 return;
1285 }
1286 /* Function definition (nested or otherwise). */
1287 if (nested)
1288 {
509c9d60 1289 pedwarn (here, OPT_pedantic, "ISO C forbids nested functions");
d2784db4 1290 c_push_function_context ();
27bf414c
JM
1291 }
1292 if (!start_function (specs, declarator, all_prefix_attrs))
1293 {
1294 /* This can appear in many cases looking nothing like a
1295 function definition, so we don't give a more specific
1296 error suggesting there was one. */
1297 c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, %<asm%> "
1298 "or %<__attribute__%>");
1299 if (nested)
d2784db4 1300 c_pop_function_context ();
27bf414c
JM
1301 break;
1302 }
1303 /* Parse old-style parameter declarations. ??? Attributes are
1304 not allowed to start declaration specifiers here because of a
1305 syntax conflict between a function declaration with attribute
1306 suffix and a function definition with an attribute prefix on
1307 first old-style parameter declaration. Following the old
1308 parser, they are not accepted on subsequent old-style
1309 parameter declarations either. However, there is no
1310 ambiguity after the first declaration, nor indeed on the
1311 first as long as we don't allow postfix attributes after a
1312 declarator with a nonempty identifier list in a definition;
1313 and postfix attributes have never been accepted here in
1314 function definitions either. */
1315 while (c_parser_next_token_is_not (parser, CPP_EOF)
1316 && c_parser_next_token_is_not (parser, CPP_OPEN_BRACE))
1317 c_parser_declaration_or_fndef (parser, false, false, true, false);
27bf414c 1318 store_parm_decls ();
1751ecd6
AH
1319 DECL_STRUCT_FUNCTION (current_function_decl)->function_start_locus
1320 = c_parser_peek_token (parser)->location;
27bf414c
JM
1321 fnbody = c_parser_compound_statement (parser);
1322 if (nested)
1323 {
1324 tree decl = current_function_decl;
1325 add_stmt (fnbody);
1326 finish_function ();
d2784db4 1327 c_pop_function_context ();
c2255bc4 1328 add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl));
27bf414c
JM
1329 }
1330 else
1331 {
1332 add_stmt (fnbody);
1333 finish_function ();
1334 }
1335 break;
1336 }
1337}
1338
1339/* Parse an asm-definition (asm() outside a function body). This is a
1340 GNU extension.
1341
1342 asm-definition:
1343 simple-asm-expr ;
1344*/
1345
1346static void
1347c_parser_asm_definition (c_parser *parser)
1348{
1349 tree asm_str = c_parser_simple_asm_expr (parser);
27bf414c 1350 if (asm_str)
474eccc6 1351 cgraph_add_asm_node (asm_str);
27bf414c
JM
1352 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
1353}
1354
1355/* Parse some declaration specifiers (possibly none) (C90 6.5, C99
1356 6.7), adding them to SPECS (which may already include some).
1357 Storage class specifiers are accepted iff SCSPEC_OK; type
1358 specifiers are accepted iff TYPESPEC_OK; attributes are accepted at
1359 the start iff START_ATTR_OK.
1360
1361 declaration-specifiers:
1362 storage-class-specifier declaration-specifiers[opt]
1363 type-specifier declaration-specifiers[opt]
1364 type-qualifier declaration-specifiers[opt]
1365 function-specifier declaration-specifiers[opt]
1366
1367 Function specifiers (inline) are from C99, and are currently
1368 handled as storage class specifiers, as is __thread.
1369
1370 C90 6.5.1, C99 6.7.1:
1371 storage-class-specifier:
1372 typedef
1373 extern
1374 static
1375 auto
1376 register
1377
1378 C99 6.7.4:
1379 function-specifier:
1380 inline
1381
1382 C90 6.5.2, C99 6.7.2:
1383 type-specifier:
1384 void
1385 char
1386 short
1387 int
1388 long
1389 float
1390 double
1391 signed
1392 unsigned
1393 _Bool
1394 _Complex
1395 [_Imaginary removed in C99 TC2]
1396 struct-or-union-specifier
1397 enum-specifier
1398 typedef-name
1399
1400 (_Bool and _Complex are new in C99.)
1401
1402 C90 6.5.3, C99 6.7.3:
1403
1404 type-qualifier:
1405 const
1406 restrict
1407 volatile
1408
1409 (restrict is new in C99.)
1410
1411 GNU extensions:
1412
1413 declaration-specifiers:
1414 attributes declaration-specifiers[opt]
1415
1416 storage-class-specifier:
1417 __thread
1418
1419 type-specifier:
1420 typeof-specifier
9a8ce21f
JG
1421 _Decimal32
1422 _Decimal64
1423 _Decimal128
ab22c1fa
CF
1424 _Fract
1425 _Accum
1426 _Sat
1427
1428 (_Fract, _Accum, and _Sat are new from ISO/IEC DTR 18037:
1429 http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1169.pdf)
27bf414c
JM
1430
1431 Objective-C:
1432
1433 type-specifier:
1434 class-name objc-protocol-refs[opt]
1435 typedef-name objc-protocol-refs
1436 objc-protocol-refs
1437*/
1438
1439static void
1440c_parser_declspecs (c_parser *parser, struct c_declspecs *specs,
1441 bool scspec_ok, bool typespec_ok, bool start_attr_ok)
1442{
1443 bool attrs_ok = start_attr_ok;
1444 bool seen_type = specs->type_seen_p;
1445 while (c_parser_next_token_is (parser, CPP_NAME)
1446 || c_parser_next_token_is (parser, CPP_KEYWORD)
1447 || (c_dialect_objc () && c_parser_next_token_is (parser, CPP_LESS)))
1448 {
1449 struct c_typespec t;
1450 tree attrs;
dc491a25 1451 location_t loc = c_parser_peek_token (parser)->location;
27bf414c
JM
1452 if (c_parser_next_token_is (parser, CPP_NAME))
1453 {
1454 tree value = c_parser_peek_token (parser)->value;
1455 c_id_kind kind = c_parser_peek_token (parser)->id_kind;
1456 /* This finishes the specifiers unless a type name is OK, it
1457 is declared as a type name and a type name hasn't yet
1458 been seen. */
1459 if (!typespec_ok || seen_type
1460 || (kind != C_ID_TYPENAME && kind != C_ID_CLASSNAME))
1461 break;
1462 c_parser_consume_token (parser);
1463 seen_type = true;
1464 attrs_ok = true;
1465 if (kind == C_ID_TYPENAME
1466 && (!c_dialect_objc ()
1467 || c_parser_next_token_is_not (parser, CPP_LESS)))
1468 {
1469 t.kind = ctsk_typedef;
1470 /* For a typedef name, record the meaning, not the name.
1471 In case of 'foo foo, bar;'. */
1472 t.spec = lookup_name (value);
928c19bb
JM
1473 t.expr = NULL_TREE;
1474 t.expr_const_operands = true;
27bf414c
JM
1475 }
1476 else
1477 {
1478 tree proto = NULL_TREE;
1479 gcc_assert (c_dialect_objc ());
1480 t.kind = ctsk_objc;
1481 if (c_parser_next_token_is (parser, CPP_LESS))
1482 proto = c_parser_objc_protocol_refs (parser);
1483 t.spec = objc_get_protocol_qualified_type (value, proto);
928c19bb
JM
1484 t.expr = NULL_TREE;
1485 t.expr_const_operands = true;
27bf414c 1486 }
dc491a25 1487 declspecs_add_type (loc, specs, t);
27bf414c
JM
1488 continue;
1489 }
1490 if (c_parser_next_token_is (parser, CPP_LESS))
1491 {
1492 /* Make "<SomeProtocol>" equivalent to "id <SomeProtocol>" -
1493 nisse@lysator.liu.se. */
1494 tree proto;
1495 gcc_assert (c_dialect_objc ());
1496 if (!typespec_ok || seen_type)
1497 break;
1498 proto = c_parser_objc_protocol_refs (parser);
1499 t.kind = ctsk_objc;
1500 t.spec = objc_get_protocol_qualified_type (NULL_TREE, proto);
928c19bb
JM
1501 t.expr = NULL_TREE;
1502 t.expr_const_operands = true;
dc491a25 1503 declspecs_add_type (loc, specs, t);
27bf414c
JM
1504 continue;
1505 }
1506 gcc_assert (c_parser_next_token_is (parser, CPP_KEYWORD));
1507 switch (c_parser_peek_token (parser)->keyword)
1508 {
1509 case RID_STATIC:
1510 case RID_EXTERN:
1511 case RID_REGISTER:
1512 case RID_TYPEDEF:
1513 case RID_INLINE:
1514 case RID_AUTO:
1515 case RID_THREAD:
1516 if (!scspec_ok)
1517 goto out;
1518 attrs_ok = true;
1519 /* TODO: Distinguish between function specifiers (inline)
1520 and storage class specifiers, either here or in
1521 declspecs_add_scspec. */
1522 declspecs_add_scspec (specs, c_parser_peek_token (parser)->value);
1523 c_parser_consume_token (parser);
1524 break;
1525 case RID_UNSIGNED:
1526 case RID_LONG:
1527 case RID_SHORT:
1528 case RID_SIGNED:
1529 case RID_COMPLEX:
1530 case RID_INT:
1531 case RID_CHAR:
1532 case RID_FLOAT:
1533 case RID_DOUBLE:
1534 case RID_VOID:
9a8ce21f
JG
1535 case RID_DFLOAT32:
1536 case RID_DFLOAT64:
1537 case RID_DFLOAT128:
27bf414c 1538 case RID_BOOL:
ab22c1fa
CF
1539 case RID_FRACT:
1540 case RID_ACCUM:
1541 case RID_SAT:
27bf414c
JM
1542 if (!typespec_ok)
1543 goto out;
1544 attrs_ok = true;
1545 seen_type = true;
0bacb8c7
TT
1546 if (c_dialect_objc ())
1547 parser->objc_need_raw_identifier = true;
27bf414c
JM
1548 t.kind = ctsk_resword;
1549 t.spec = c_parser_peek_token (parser)->value;
928c19bb
JM
1550 t.expr = NULL_TREE;
1551 t.expr_const_operands = true;
dc491a25 1552 declspecs_add_type (loc, specs, t);
27bf414c
JM
1553 c_parser_consume_token (parser);
1554 break;
1555 case RID_ENUM:
1556 if (!typespec_ok)
1557 goto out;
1558 attrs_ok = true;
1559 seen_type = true;
1560 t = c_parser_enum_specifier (parser);
dc491a25 1561 declspecs_add_type (loc, specs, t);
27bf414c
JM
1562 break;
1563 case RID_STRUCT:
1564 case RID_UNION:
1565 if (!typespec_ok)
1566 goto out;
1567 attrs_ok = true;
1568 seen_type = true;
1569 t = c_parser_struct_or_union_specifier (parser);
dab71827 1570 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, t.spec);
dc491a25 1571 declspecs_add_type (loc, specs, t);
27bf414c
JM
1572 break;
1573 case RID_TYPEOF:
1574 /* ??? The old parser rejected typeof after other type
1575 specifiers, but is a syntax error the best way of
1576 handling this? */
1577 if (!typespec_ok || seen_type)
1578 goto out;
1579 attrs_ok = true;
1580 seen_type = true;
1581 t = c_parser_typeof_specifier (parser);
dc491a25 1582 declspecs_add_type (loc, specs, t);
27bf414c
JM
1583 break;
1584 case RID_CONST:
1585 case RID_VOLATILE:
1586 case RID_RESTRICT:
1587 attrs_ok = true;
1588 declspecs_add_qual (specs, c_parser_peek_token (parser)->value);
1589 c_parser_consume_token (parser);
1590 break;
1591 case RID_ATTRIBUTE:
1592 if (!attrs_ok)
1593 goto out;
1594 attrs = c_parser_attributes (parser);
1595 declspecs_add_attrs (specs, attrs);
1596 break;
1597 default:
1598 goto out;
1599 }
1600 }
1601 out: ;
1602}
1603
1604/* Parse an enum specifier (C90 6.5.2.2, C99 6.7.2.2).
1605
1606 enum-specifier:
1607 enum attributes[opt] identifier[opt] { enumerator-list } attributes[opt]
1608 enum attributes[opt] identifier[opt] { enumerator-list , } attributes[opt]
1609 enum attributes[opt] identifier
1610
1611 The form with trailing comma is new in C99. The forms with
1612 attributes are GNU extensions. In GNU C, we accept any expression
1613 without commas in the syntax (assignment expressions, not just
1614 conditional expressions); assignment expressions will be diagnosed
1615 as non-constant.
1616
1617 enumerator-list:
1618 enumerator
1619 enumerator-list , enumerator
1620
1621 enumerator:
1622 enumeration-constant
1623 enumeration-constant = constant-expression
1624*/
1625
1626static struct c_typespec
1627c_parser_enum_specifier (c_parser *parser)
1628{
1629 struct c_typespec ret;
1630 tree attrs;
1631 tree ident = NULL_TREE;
24b97832 1632 location_t enum_loc;
922f2908 1633 location_t ident_loc = UNKNOWN_LOCATION; /* Quiet warning. */
27bf414c 1634 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ENUM));
24b97832 1635 enum_loc = c_parser_peek_token (parser)->location;
27bf414c
JM
1636 c_parser_consume_token (parser);
1637 attrs = c_parser_attributes (parser);
c2255bc4 1638 enum_loc = c_parser_peek_token (parser)->location;
5af28c74
TT
1639 /* Set the location in case we create a decl now. */
1640 c_parser_set_source_position_from_token (c_parser_peek_token (parser));
27bf414c
JM
1641 if (c_parser_next_token_is (parser, CPP_NAME))
1642 {
1643 ident = c_parser_peek_token (parser)->value;
c7412148 1644 ident_loc = c_parser_peek_token (parser)->location;
24b97832 1645 enum_loc = ident_loc;
27bf414c
JM
1646 c_parser_consume_token (parser);
1647 }
1648 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
1649 {
1650 /* Parse an enum definition. */
7114359f 1651 struct c_enum_contents the_enum;
c2255bc4 1652 tree type = start_enum (enum_loc, &the_enum, ident);
27bf414c
JM
1653 tree postfix_attrs;
1654 /* We chain the enumerators in reverse order, then put them in
1655 forward order at the end. */
1656 tree values = NULL_TREE;
1657 c_parser_consume_token (parser);
1658 while (true)
1659 {
1660 tree enum_id;
1661 tree enum_value;
1662 tree enum_decl;
1663 bool seen_comma;
5af28c74 1664 c_token *token;
922f2908 1665 location_t comma_loc = UNKNOWN_LOCATION; /* Quiet warning. */
85790e66 1666 location_t value_loc;
27bf414c
JM
1667 if (c_parser_next_token_is_not (parser, CPP_NAME))
1668 {
1669 c_parser_error (parser, "expected identifier");
1670 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
1671 values = error_mark_node;
1672 break;
1673 }
5af28c74
TT
1674 token = c_parser_peek_token (parser);
1675 enum_id = token->value;
1676 /* Set the location in case we create a decl now. */
1677 c_parser_set_source_position_from_token (token);
85790e66 1678 value_loc = token->location;
27bf414c
JM
1679 c_parser_consume_token (parser);
1680 if (c_parser_next_token_is (parser, CPP_EQ))
1681 {
1682 c_parser_consume_token (parser);
85790e66 1683 value_loc = c_parser_peek_token (parser)->location;
27bf414c
JM
1684 enum_value = c_parser_expr_no_commas (parser, NULL).value;
1685 }
1686 else
1687 enum_value = NULL_TREE;
c2255bc4
AH
1688 enum_decl = build_enumerator (value_loc,
1689 &the_enum, enum_id, enum_value);
27bf414c
JM
1690 TREE_CHAIN (enum_decl) = values;
1691 values = enum_decl;
1692 seen_comma = false;
1693 if (c_parser_next_token_is (parser, CPP_COMMA))
1694 {
c7412148 1695 comma_loc = c_parser_peek_token (parser)->location;
27bf414c
JM
1696 seen_comma = true;
1697 c_parser_consume_token (parser);
1698 }
1699 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
1700 {
fcf73884 1701 if (seen_comma && !flag_isoc99)
509c9d60 1702 pedwarn (comma_loc, OPT_pedantic, "comma at end of enumerator list");
27bf414c
JM
1703 c_parser_consume_token (parser);
1704 break;
1705 }
1706 if (!seen_comma)
1707 {
1708 c_parser_error (parser, "expected %<,%> or %<}%>");
1709 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
1710 values = error_mark_node;
1711 break;
1712 }
1713 }
1714 postfix_attrs = c_parser_attributes (parser);
1715 ret.spec = finish_enum (type, nreverse (values),
1716 chainon (attrs, postfix_attrs));
1717 ret.kind = ctsk_tagdef;
928c19bb
JM
1718 ret.expr = NULL_TREE;
1719 ret.expr_const_operands = true;
27bf414c
JM
1720 return ret;
1721 }
1722 else if (!ident)
1723 {
1724 c_parser_error (parser, "expected %<{%>");
1725 ret.spec = error_mark_node;
1726 ret.kind = ctsk_tagref;
928c19bb
JM
1727 ret.expr = NULL_TREE;
1728 ret.expr_const_operands = true;
27bf414c
JM
1729 return ret;
1730 }
c2255bc4 1731 ret = parser_xref_tag (ident_loc, ENUMERAL_TYPE, ident);
27bf414c
JM
1732 /* In ISO C, enumerated types can be referred to only if already
1733 defined. */
1734 if (pedantic && !COMPLETE_TYPE_P (ret.spec))
c7412148
TT
1735 {
1736 gcc_assert (ident);
c2255bc4 1737 pedwarn (enum_loc, OPT_pedantic,
509c9d60 1738 "ISO C forbids forward references to %<enum%> types");
c7412148 1739 }
27bf414c
JM
1740 return ret;
1741}
1742
1743/* Parse a struct or union specifier (C90 6.5.2.1, C99 6.7.2.1).
1744
1745 struct-or-union-specifier:
1746 struct-or-union attributes[opt] identifier[opt]
1747 { struct-contents } attributes[opt]
1748 struct-or-union attributes[opt] identifier
1749
1750 struct-contents:
1751 struct-declaration-list
1752
1753 struct-declaration-list:
1754 struct-declaration ;
1755 struct-declaration-list struct-declaration ;
1756
1757 GNU extensions:
1758
1759 struct-contents:
1760 empty
1761 struct-declaration
1762 struct-declaration-list struct-declaration
1763
1764 struct-declaration-list:
1765 struct-declaration-list ;
1766 ;
1767
1768 (Note that in the syntax here, unlike that in ISO C, the semicolons
1769 are included here rather than in struct-declaration, in order to
1770 describe the syntax with extra semicolons and missing semicolon at
1771 end.)
1772
1773 Objective-C:
1774
1775 struct-declaration-list:
1776 @defs ( class-name )
1777
1778 (Note this does not include a trailing semicolon, but can be
1779 followed by further declarations, and gets a pedwarn-if-pedantic
1780 when followed by a semicolon.) */
1781
1782static struct c_typespec
1783c_parser_struct_or_union_specifier (c_parser *parser)
1784{
1785 struct c_typespec ret;
1786 tree attrs;
1787 tree ident = NULL_TREE;
24b97832
ILT
1788 location_t struct_loc;
1789 location_t ident_loc = UNKNOWN_LOCATION;
27bf414c
JM
1790 enum tree_code code;
1791 switch (c_parser_peek_token (parser)->keyword)
1792 {
1793 case RID_STRUCT:
1794 code = RECORD_TYPE;
1795 break;
1796 case RID_UNION:
1797 code = UNION_TYPE;
1798 break;
1799 default:
1800 gcc_unreachable ();
1801 }
24b97832 1802 struct_loc = c_parser_peek_token (parser)->location;
27bf414c
JM
1803 c_parser_consume_token (parser);
1804 attrs = c_parser_attributes (parser);
c2255bc4 1805
5af28c74
TT
1806 /* Set the location in case we create a decl now. */
1807 c_parser_set_source_position_from_token (c_parser_peek_token (parser));
c2255bc4 1808
27bf414c
JM
1809 if (c_parser_next_token_is (parser, CPP_NAME))
1810 {
1811 ident = c_parser_peek_token (parser)->value;
24b97832
ILT
1812 ident_loc = c_parser_peek_token (parser)->location;
1813 struct_loc = ident_loc;
27bf414c
JM
1814 c_parser_consume_token (parser);
1815 }
1816 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
1817 {
1818 /* Parse a struct or union definition. Start the scope of the
1819 tag before parsing components. */
dc491a25
ILT
1820 struct c_struct_parse_info *struct_info;
1821 tree type = start_struct (struct_loc, code, ident, &struct_info);
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),
dc491a25 1911 chainon (attrs, postfix_attrs), struct_info);
27bf414c 1912 ret.kind = ctsk_tagdef;
928c19bb
JM
1913 ret.expr = NULL_TREE;
1914 ret.expr_const_operands = true;
27bf414c
JM
1915 return ret;
1916 }
1917 else if (!ident)
1918 {
1919 c_parser_error (parser, "expected %<{%>");
1920 ret.spec = error_mark_node;
1921 ret.kind = ctsk_tagref;
928c19bb
JM
1922 ret.expr = NULL_TREE;
1923 ret.expr_const_operands = true;
67c2939d 1924 return ret;
27bf414c 1925 }
c2255bc4 1926 ret = parser_xref_tag (ident_loc, code, ident);
27bf414c
JM
1927 return ret;
1928}
1929
1930/* Parse a struct-declaration (C90 6.5.2.1, C99 6.7.2.1), *without*
1931 the trailing semicolon.
1932
1933 struct-declaration:
1934 specifier-qualifier-list struct-declarator-list
1935
1936 specifier-qualifier-list:
1937 type-specifier specifier-qualifier-list[opt]
1938 type-qualifier specifier-qualifier-list[opt]
1939 attributes specifier-qualifier-list[opt]
1940
1941 struct-declarator-list:
1942 struct-declarator
1943 struct-declarator-list , attributes[opt] struct-declarator
1944
1945 struct-declarator:
1946 declarator attributes[opt]
1947 declarator[opt] : constant-expression attributes[opt]
1948
1949 GNU extensions:
1950
1951 struct-declaration:
1952 __extension__ struct-declaration
1953 specifier-qualifier-list
1954
1955 Unlike the ISO C syntax, semicolons are handled elsewhere. The use
1956 of attributes where shown is a GNU extension. In GNU C, we accept
1957 any expression without commas in the syntax (assignment
1958 expressions, not just conditional expressions); assignment
1959 expressions will be diagnosed as non-constant. */
1960
1961static tree
1962c_parser_struct_declaration (c_parser *parser)
1963{
1964 struct c_declspecs *specs;
1965 tree prefix_attrs;
1966 tree all_prefix_attrs;
1967 tree decls;
c7412148 1968 location_t decl_loc;
27bf414c
JM
1969 if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
1970 {
1971 int ext;
1972 tree decl;
1973 ext = disable_extension_diagnostics ();
1974 c_parser_consume_token (parser);
1975 decl = c_parser_struct_declaration (parser);
1976 restore_extension_diagnostics (ext);
1977 return decl;
1978 }
1979 specs = build_null_declspecs ();
c7412148 1980 decl_loc = c_parser_peek_token (parser)->location;
27bf414c
JM
1981 c_parser_declspecs (parser, specs, false, true, true);
1982 if (parser->error)
67c2939d 1983 return NULL_TREE;
27bf414c
JM
1984 if (!specs->declspecs_seen_p)
1985 {
1986 c_parser_error (parser, "expected specifier-qualifier-list");
1987 return NULL_TREE;
1988 }
1989 finish_declspecs (specs);
1990 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1991 {
1992 tree ret;
1993 if (!specs->type_seen_p)
1994 {
509c9d60
MLI
1995 pedwarn (decl_loc, OPT_pedantic,
1996 "ISO C forbids member declarations with no members");
27bf414c
JM
1997 shadow_tag_warned (specs, pedantic);
1998 ret = NULL_TREE;
1999 }
2000 else
2001 {
2002 /* Support for unnamed structs or unions as members of
2003 structs or unions (which is [a] useful and [b] supports
2004 MS P-SDK). */
b9baeecd 2005 tree attrs = NULL;
3d10ed6c
AH
2006
2007 ret = grokfield (c_parser_peek_token (parser)->location,
2008 build_id_declarator (NULL_TREE), specs,
b9baeecd 2009 NULL_TREE, &attrs);
0ad7e054
RS
2010 if (ret)
2011 decl_attributes (&ret, attrs, 0);
27bf414c
JM
2012 }
2013 return ret;
2014 }
2015 pending_xref_error ();
2016 prefix_attrs = specs->attrs;
2017 all_prefix_attrs = prefix_attrs;
2018 specs->attrs = NULL_TREE;
2019 decls = NULL_TREE;
2020 while (true)
2021 {
2022 /* Declaring one or more declarators or un-named bit-fields. */
2023 struct c_declarator *declarator;
2024 bool dummy = false;
2025 if (c_parser_next_token_is (parser, CPP_COLON))
2026 declarator = build_id_declarator (NULL_TREE);
2027 else
2028 declarator = c_parser_declarator (parser, specs->type_seen_p,
2029 C_DTR_NORMAL, &dummy);
2030 if (declarator == NULL)
2031 {
2032 c_parser_skip_to_end_of_block_or_statement (parser);
2033 break;
2034 }
2035 if (c_parser_next_token_is (parser, CPP_COLON)
2036 || c_parser_next_token_is (parser, CPP_COMMA)
2037 || c_parser_next_token_is (parser, CPP_SEMICOLON)
2038 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE)
2039 || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2040 {
2041 tree postfix_attrs = NULL_TREE;
2042 tree width = NULL_TREE;
2043 tree d;
2044 if (c_parser_next_token_is (parser, CPP_COLON))
2045 {
2046 c_parser_consume_token (parser);
2047 width = c_parser_expr_no_commas (parser, NULL).value;
2048 }
2049 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2050 postfix_attrs = c_parser_attributes (parser);
3d10ed6c
AH
2051 d = grokfield (c_parser_peek_token (parser)->location,
2052 declarator, specs, width, &all_prefix_attrs);
27bf414c
JM
2053 decl_attributes (&d, chainon (postfix_attrs,
2054 all_prefix_attrs), 0);
2055 TREE_CHAIN (d) = decls;
2056 decls = d;
2057 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2058 all_prefix_attrs = chainon (c_parser_attributes (parser),
2059 prefix_attrs);
2060 else
2061 all_prefix_attrs = prefix_attrs;
2062 if (c_parser_next_token_is (parser, CPP_COMMA))
2063 c_parser_consume_token (parser);
2064 else if (c_parser_next_token_is (parser, CPP_SEMICOLON)
2065 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2066 {
2067 /* Semicolon consumed in caller. */
2068 break;
2069 }
2070 else
2071 {
2072 c_parser_error (parser, "expected %<,%>, %<;%> or %<}%>");
2073 break;
2074 }
2075 }
2076 else
2077 {
2078 c_parser_error (parser,
2079 "expected %<:%>, %<,%>, %<;%>, %<}%> or "
2080 "%<__attribute__%>");
2081 break;
2082 }
2083 }
2084 return decls;
2085}
2086
2087/* Parse a typeof specifier (a GNU extension).
2088
2089 typeof-specifier:
2090 typeof ( expression )
2091 typeof ( type-name )
2092*/
2093
2094static struct c_typespec
2095c_parser_typeof_specifier (c_parser *parser)
2096{
2097 struct c_typespec ret;
2098 ret.kind = ctsk_typeof;
2099 ret.spec = error_mark_node;
928c19bb
JM
2100 ret.expr = NULL_TREE;
2101 ret.expr_const_operands = true;
27bf414c
JM
2102 gcc_assert (c_parser_next_token_is_keyword (parser, RID_TYPEOF));
2103 c_parser_consume_token (parser);
7d882b83 2104 c_inhibit_evaluation_warnings++;
27bf414c
JM
2105 in_typeof++;
2106 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2107 {
7d882b83 2108 c_inhibit_evaluation_warnings--;
27bf414c
JM
2109 in_typeof--;
2110 return ret;
2111 }
2112 if (c_parser_next_token_starts_typename (parser))
2113 {
2114 struct c_type_name *type = c_parser_type_name (parser);
7d882b83 2115 c_inhibit_evaluation_warnings--;
27bf414c
JM
2116 in_typeof--;
2117 if (type != NULL)
2118 {
928c19bb 2119 ret.spec = groktypename (type, &ret.expr, &ret.expr_const_operands);
27bf414c
JM
2120 pop_maybe_used (variably_modified_type_p (ret.spec, NULL_TREE));
2121 }
2122 }
2123 else
2124 {
52ffd86e 2125 bool was_vm;
c7412148 2126 location_t here = c_parser_peek_token (parser)->location;
27bf414c 2127 struct c_expr expr = c_parser_expression (parser);
7d882b83 2128 c_inhibit_evaluation_warnings--;
27bf414c
JM
2129 in_typeof--;
2130 if (TREE_CODE (expr.value) == COMPONENT_REF
2131 && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
3ba09659 2132 error_at (here, "%<typeof%> applied to a bit-field");
27bf414c 2133 ret.spec = TREE_TYPE (expr.value);
52ffd86e 2134 was_vm = variably_modified_type_p (ret.spec, NULL_TREE);
928c19bb
JM
2135 /* This is returned with the type so that when the type is
2136 evaluated, this can be evaluated. */
2137 if (was_vm)
2138 ret.expr = c_fully_fold (expr.value, false, &ret.expr_const_operands);
52ffd86e 2139 pop_maybe_used (was_vm);
27bf414c
JM
2140 }
2141 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
2142 return ret;
2143}
2144
2145/* Parse a declarator, possibly an abstract declarator (C90 6.5.4,
2146 6.5.5, C99 6.7.5, 6.7.6). If TYPE_SEEN_P then a typedef name may
2147 be redeclared; otherwise it may not. KIND indicates which kind of
2148 declarator is wanted. Returns a valid declarator except in the
2149 case of a syntax error in which case NULL is returned. *SEEN_ID is
2150 set to true if an identifier being declared is seen; this is used
2151 to diagnose bad forms of abstract array declarators and to
2152 determine whether an identifier list is syntactically permitted.
2153
2154 declarator:
2155 pointer[opt] direct-declarator
2156
2157 direct-declarator:
2158 identifier
2159 ( attributes[opt] declarator )
2160 direct-declarator array-declarator
2161 direct-declarator ( parameter-type-list )
2162 direct-declarator ( identifier-list[opt] )
2163
2164 pointer:
2165 * type-qualifier-list[opt]
2166 * type-qualifier-list[opt] pointer
2167
2168 type-qualifier-list:
2169 type-qualifier
2170 attributes
2171 type-qualifier-list type-qualifier
2172 type-qualifier-list attributes
2173
2174 parameter-type-list:
2175 parameter-list
2176 parameter-list , ...
2177
2178 parameter-list:
2179 parameter-declaration
2180 parameter-list , parameter-declaration
2181
2182 parameter-declaration:
2183 declaration-specifiers declarator attributes[opt]
2184 declaration-specifiers abstract-declarator[opt] attributes[opt]
2185
2186 identifier-list:
2187 identifier
2188 identifier-list , identifier
2189
2190 abstract-declarator:
2191 pointer
2192 pointer[opt] direct-abstract-declarator
2193
2194 direct-abstract-declarator:
2195 ( attributes[opt] abstract-declarator )
2196 direct-abstract-declarator[opt] array-declarator
2197 direct-abstract-declarator[opt] ( parameter-type-list[opt] )
2198
2199 GNU extensions:
2200
2201 direct-declarator:
2202 direct-declarator ( parameter-forward-declarations
2203 parameter-type-list[opt] )
2204
2205 direct-abstract-declarator:
c22cacf3 2206 direct-abstract-declarator[opt] ( parameter-forward-declarations
27bf414c
JM
2207 parameter-type-list[opt] )
2208
2209 parameter-forward-declarations:
2210 parameter-list ;
2211 parameter-forward-declarations parameter-list ;
2212
2213 The uses of attributes shown above are GNU extensions.
2214
2215 Some forms of array declarator are not included in C99 in the
2216 syntax for abstract declarators; these are disallowed elsewhere.
2217 This may be a defect (DR#289).
2218
2219 This function also accepts an omitted abstract declarator as being
2220 an abstract declarator, although not part of the formal syntax. */
2221
2222static struct c_declarator *
2223c_parser_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
2224 bool *seen_id)
2225{
2226 /* Parse any initial pointer part. */
2227 if (c_parser_next_token_is (parser, CPP_MULT))
2228 {
2229 struct c_declspecs *quals_attrs = build_null_declspecs ();
2230 struct c_declarator *inner;
2231 c_parser_consume_token (parser);
2232 c_parser_declspecs (parser, quals_attrs, false, false, true);
2233 inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
2234 if (inner == NULL)
2235 return NULL;
2236 else
2237 return make_pointer_declarator (quals_attrs, inner);
2238 }
2239 /* Now we have a direct declarator, direct abstract declarator or
2240 nothing (which counts as a direct abstract declarator here). */
2241 return c_parser_direct_declarator (parser, type_seen_p, kind, seen_id);
2242}
2243
2244/* Parse a direct declarator or direct abstract declarator; arguments
2245 as c_parser_declarator. */
2246
2247static struct c_declarator *
2248c_parser_direct_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
2249 bool *seen_id)
2250{
2251 /* The direct declarator must start with an identifier (possibly
2252 omitted) or a parenthesized declarator (possibly abstract). In
2253 an ordinary declarator, initial parentheses must start a
2254 parenthesized declarator. In an abstract declarator or parameter
2255 declarator, they could start a parenthesized declarator or a
2256 parameter list. To tell which, the open parenthesis and any
2257 following attributes must be read. If a declaration specifier
2258 follows, then it is a parameter list; if the specifier is a
2259 typedef name, there might be an ambiguity about redeclaring it,
2260 which is resolved in the direction of treating it as a typedef
2261 name. If a close parenthesis follows, it is also an empty
2262 parameter list, as the syntax does not permit empty abstract
0fa2e4df 2263 declarators. Otherwise, it is a parenthesized declarator (in
27bf414c
JM
2264 which case the analysis may be repeated inside it, recursively).
2265
2266 ??? There is an ambiguity in a parameter declaration "int
2267 (__attribute__((foo)) x)", where x is not a typedef name: it
2268 could be an abstract declarator for a function, or declare x with
2269 parentheses. The proper resolution of this ambiguity needs
2270 documenting. At present we follow an accident of the old
2271 parser's implementation, whereby the first parameter must have
2272 some declaration specifiers other than just attributes. Thus as
0fa2e4df 2273 a parameter declaration it is treated as a parenthesized
27bf414c
JM
2274 parameter named x, and as an abstract declarator it is
2275 rejected.
2276
2277 ??? Also following the old parser, attributes inside an empty
2278 parameter list are ignored, making it a list not yielding a
2279 prototype, rather than giving an error or making it have one
2280 parameter with implicit type int.
2281
2282 ??? Also following the old parser, typedef names may be
2283 redeclared in declarators, but not Objective-C class names. */
2284
2285 if (kind != C_DTR_ABSTRACT
2286 && c_parser_next_token_is (parser, CPP_NAME)
2287 && ((type_seen_p
2288 && c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME)
2289 || c_parser_peek_token (parser)->id_kind == C_ID_ID))
2290 {
2291 struct c_declarator *inner
2292 = build_id_declarator (c_parser_peek_token (parser)->value);
2293 *seen_id = true;
6037d88d 2294 inner->id_loc = c_parser_peek_token (parser)->location;
27bf414c
JM
2295 c_parser_consume_token (parser);
2296 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
2297 }
2298
2299 if (kind != C_DTR_NORMAL
2300 && c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
2301 {
2302 struct c_declarator *inner = build_id_declarator (NULL_TREE);
2303 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
2304 }
2305
2306 /* Either we are at the end of an abstract declarator, or we have
2307 parentheses. */
2308
2309 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
2310 {
2311 tree attrs;
2312 struct c_declarator *inner;
2313 c_parser_consume_token (parser);
2314 attrs = c_parser_attributes (parser);
2315 if (kind != C_DTR_NORMAL
2316 && (c_parser_next_token_starts_declspecs (parser)
2317 || c_parser_next_token_is (parser, CPP_CLOSE_PAREN)))
2318 {
2319 struct c_arg_info *args
2320 = c_parser_parms_declarator (parser, kind == C_DTR_NORMAL,
2321 attrs);
2322 if (args == NULL)
2323 return NULL;
2324 else
2325 {
2326 inner
2327 = build_function_declarator (args,
2328 build_id_declarator (NULL_TREE));
2329 return c_parser_direct_declarator_inner (parser, *seen_id,
2330 inner);
2331 }
2332 }
2333 /* A parenthesized declarator. */
2334 inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
2335 if (inner != NULL && attrs != NULL)
2336 inner = build_attrs_declarator (attrs, inner);
2337 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2338 {
2339 c_parser_consume_token (parser);
2340 if (inner == NULL)
2341 return NULL;
2342 else
2343 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
2344 }
2345 else
2346 {
2347 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2348 "expected %<)%>");
2349 return NULL;
2350 }
2351 }
2352 else
2353 {
2354 if (kind == C_DTR_NORMAL)
2355 {
2356 c_parser_error (parser, "expected identifier or %<(%>");
2357 return NULL;
2358 }
2359 else
2360 return build_id_declarator (NULL_TREE);
2361 }
2362}
2363
2364/* Parse part of a direct declarator or direct abstract declarator,
2365 given that some (in INNER) has already been parsed; ID_PRESENT is
2366 true if an identifier is present, false for an abstract
2367 declarator. */
2368
2369static struct c_declarator *
2370c_parser_direct_declarator_inner (c_parser *parser, bool id_present,
2371 struct c_declarator *inner)
2372{
2373 /* Parse a sequence of array declarators and parameter lists. */
2374 if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
2375 {
c2255bc4 2376 location_t brace_loc = c_parser_peek_token (parser)->location;
27bf414c
JM
2377 struct c_declarator *declarator;
2378 struct c_declspecs *quals_attrs = build_null_declspecs ();
2379 bool static_seen;
2380 bool star_seen;
2381 tree dimen;
2382 c_parser_consume_token (parser);
2383 c_parser_declspecs (parser, quals_attrs, false, false, true);
2384 static_seen = c_parser_next_token_is_keyword (parser, RID_STATIC);
2385 if (static_seen)
2386 c_parser_consume_token (parser);
2387 if (static_seen && !quals_attrs->declspecs_seen_p)
2388 c_parser_declspecs (parser, quals_attrs, false, false, true);
2389 if (!quals_attrs->declspecs_seen_p)
2390 quals_attrs = NULL;
2391 /* If "static" is present, there must be an array dimension.
2392 Otherwise, there may be a dimension, "*", or no
2393 dimension. */
2394 if (static_seen)
2395 {
2396 star_seen = false;
2397 dimen = c_parser_expr_no_commas (parser, NULL).value;
2398 }
2399 else
2400 {
2401 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
2402 {
2403 dimen = NULL_TREE;
2404 star_seen = false;
2405 }
2406 else if (c_parser_next_token_is (parser, CPP_MULT))
2407 {
2408 if (c_parser_peek_2nd_token (parser)->type == CPP_CLOSE_SQUARE)
2409 {
2410 dimen = NULL_TREE;
2411 star_seen = true;
2412 c_parser_consume_token (parser);
2413 }
2414 else
2415 {
2416 star_seen = false;
2417 dimen = c_parser_expr_no_commas (parser, NULL).value;
2418 }
2419 }
2420 else
2421 {
2422 star_seen = false;
2423 dimen = c_parser_expr_no_commas (parser, NULL).value;
2424 }
2425 }
2426 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
2427 c_parser_consume_token (parser);
2428 else
2429 {
2430 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
2431 "expected %<]%>");
2432 return NULL;
2433 }
c2255bc4
AH
2434 declarator = build_array_declarator (brace_loc, dimen, quals_attrs,
2435 static_seen, star_seen);
52ffd86e
MS
2436 if (declarator == NULL)
2437 return NULL;
6ac0194d 2438 inner = set_array_declarator_inner (declarator, inner);
27bf414c
JM
2439 return c_parser_direct_declarator_inner (parser, id_present, inner);
2440 }
2441 else if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
2442 {
2443 tree attrs;
2444 struct c_arg_info *args;
2445 c_parser_consume_token (parser);
2446 attrs = c_parser_attributes (parser);
2447 args = c_parser_parms_declarator (parser, id_present, attrs);
2448 if (args == NULL)
2449 return NULL;
2450 else
2451 {
2452 inner = build_function_declarator (args, inner);
2453 return c_parser_direct_declarator_inner (parser, id_present, inner);
2454 }
2455 }
2456 return inner;
2457}
2458
2459/* Parse a parameter list or identifier list, including the closing
2460 parenthesis but not the opening one. ATTRS are the attributes at
2461 the start of the list. ID_LIST_OK is true if an identifier list is
2462 acceptable; such a list must not have attributes at the start. */
2463
2464static struct c_arg_info *
2465c_parser_parms_declarator (c_parser *parser, bool id_list_ok, tree attrs)
2466{
2467 push_scope ();
2468 declare_parm_level ();
2469 /* If the list starts with an identifier, it is an identifier list.
2470 Otherwise, it is either a prototype list or an empty list. */
2471 if (id_list_ok
2472 && !attrs
2473 && c_parser_next_token_is (parser, CPP_NAME)
2474 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
2475 {
7fa3585c 2476 tree list = NULL_TREE, *nextp = &list;
27bf414c
JM
2477 while (c_parser_next_token_is (parser, CPP_NAME)
2478 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
2479 {
7fa3585c
GK
2480 *nextp = build_tree_list (NULL_TREE,
2481 c_parser_peek_token (parser)->value);
2482 nextp = & TREE_CHAIN (*nextp);
27bf414c
JM
2483 c_parser_consume_token (parser);
2484 if (c_parser_next_token_is_not (parser, CPP_COMMA))
2485 break;
2486 c_parser_consume_token (parser);
2487 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2488 {
2489 c_parser_error (parser, "expected identifier");
2490 break;
2491 }
2492 }
2493 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2494 {
2495 struct c_arg_info *ret = XOBNEW (&parser_obstack, struct c_arg_info);
2496 ret->parms = 0;
2497 ret->tags = 0;
2498 ret->types = list;
2499 ret->others = 0;
3542a5c0 2500 ret->pending_sizes = 0;
6a1f8611 2501 ret->had_vla_unspec = 0;
27bf414c
JM
2502 c_parser_consume_token (parser);
2503 pop_scope ();
2504 return ret;
2505 }
2506 else
2507 {
2508 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2509 "expected %<)%>");
2510 pop_scope ();
2511 return NULL;
2512 }
2513 }
2514 else
2515 {
2516 struct c_arg_info *ret = c_parser_parms_list_declarator (parser, attrs);
2517 pop_scope ();
2518 return ret;
2519 }
2520}
2521
2522/* Parse a parameter list (possibly empty), including the closing
2523 parenthesis but not the opening one. ATTRS are the attributes at
2524 the start of the list. */
2525
2526static struct c_arg_info *
2527c_parser_parms_list_declarator (c_parser *parser, tree attrs)
2528{
2529 bool good_parm = false;
2530 /* ??? Following the old parser, forward parameter declarations may
2531 use abstract declarators, and if no real parameter declarations
2532 follow the forward declarations then this is not diagnosed. Also
2533 note as above that attributes are ignored as the only contents of
2534 the parentheses, or as the only contents after forward
2535 declarations. */
2536 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2537 {
2538 struct c_arg_info *ret = XOBNEW (&parser_obstack, struct c_arg_info);
2539 ret->parms = 0;
2540 ret->tags = 0;
2541 ret->types = 0;
2542 ret->others = 0;
3542a5c0 2543 ret->pending_sizes = 0;
6a1f8611 2544 ret->had_vla_unspec = 0;
27bf414c
JM
2545 c_parser_consume_token (parser);
2546 return ret;
2547 }
2548 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
2549 {
2550 struct c_arg_info *ret = XOBNEW (&parser_obstack, struct c_arg_info);
2551 ret->parms = 0;
2552 ret->tags = 0;
2553 ret->others = 0;
3542a5c0 2554 ret->pending_sizes = 0;
6a1f8611 2555 ret->had_vla_unspec = 0;
27bf414c
JM
2556 /* Suppress -Wold-style-definition for this case. */
2557 ret->types = error_mark_node;
3ba09659
AH
2558 error_at (c_parser_peek_token (parser)->location,
2559 "ISO C requires a named argument before %<...%>");
27bf414c
JM
2560 c_parser_consume_token (parser);
2561 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2562 {
2563 c_parser_consume_token (parser);
2564 return ret;
2565 }
2566 else
2567 {
2568 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2569 "expected %<)%>");
2570 return NULL;
2571 }
2572 }
2573 /* Nonempty list of parameters, either terminated with semicolon
2574 (forward declarations; recurse) or with close parenthesis (normal
2575 function) or with ", ... )" (variadic function). */
2576 while (true)
2577 {
2578 /* Parse a parameter. */
2579 struct c_parm *parm = c_parser_parameter_declaration (parser, attrs);
2580 attrs = NULL_TREE;
2581 if (parm != NULL)
2582 {
2583 good_parm = true;
2584 push_parm_decl (parm);
2585 }
2586 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
2587 {
2588 tree new_attrs;
2589 c_parser_consume_token (parser);
91d975b8 2590 mark_forward_parm_decls ();
27bf414c
JM
2591 new_attrs = c_parser_attributes (parser);
2592 return c_parser_parms_list_declarator (parser, new_attrs);
2593 }
2594 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2595 {
2596 c_parser_consume_token (parser);
2597 if (good_parm)
2598 return get_parm_info (false);
2599 else
2600 {
2601 struct c_arg_info *ret
2602 = XOBNEW (&parser_obstack, struct c_arg_info);
2603 ret->parms = 0;
2604 ret->tags = 0;
2605 ret->types = 0;
2606 ret->others = 0;
3542a5c0 2607 ret->pending_sizes = 0;
6a1f8611 2608 ret->had_vla_unspec = 0;
27bf414c
JM
2609 return ret;
2610 }
2611 }
2612 if (!c_parser_require (parser, CPP_COMMA,
2613 "expected %<;%>, %<,%> or %<)%>"))
2614 {
2615 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
f007042e 2616 get_pending_sizes ();
27bf414c
JM
2617 return NULL;
2618 }
2619 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
2620 {
2621 c_parser_consume_token (parser);
2622 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2623 {
2624 c_parser_consume_token (parser);
2625 if (good_parm)
2626 return get_parm_info (true);
2627 else
2628 {
2629 struct c_arg_info *ret
2630 = XOBNEW (&parser_obstack, struct c_arg_info);
2631 ret->parms = 0;
2632 ret->tags = 0;
2633 ret->types = 0;
2634 ret->others = 0;
3542a5c0 2635 ret->pending_sizes = 0;
6a1f8611 2636 ret->had_vla_unspec = 0;
27bf414c
JM
2637 return ret;
2638 }
2639 }
2640 else
2641 {
2642 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2643 "expected %<)%>");
f007042e 2644 get_pending_sizes ();
27bf414c
JM
2645 return NULL;
2646 }
2647 }
2648 }
2649}
2650
2651/* Parse a parameter declaration. ATTRS are the attributes at the
2652 start of the declaration if it is the first parameter. */
2653
2654static struct c_parm *
2655c_parser_parameter_declaration (c_parser *parser, tree attrs)
2656{
2657 struct c_declspecs *specs;
2658 struct c_declarator *declarator;
2659 tree prefix_attrs;
2660 tree postfix_attrs = NULL_TREE;
2661 bool dummy = false;
2662 if (!c_parser_next_token_starts_declspecs (parser))
2663 {
2664 /* ??? In some Objective-C cases '...' isn't applicable so there
2665 should be a different message. */
2666 c_parser_error (parser,
2667 "expected declaration specifiers or %<...%>");
2668 c_parser_skip_to_end_of_parameter (parser);
2669 return NULL;
2670 }
2671 specs = build_null_declspecs ();
2672 if (attrs)
2673 {
2674 declspecs_add_attrs (specs, attrs);
2675 attrs = NULL_TREE;
2676 }
2677 c_parser_declspecs (parser, specs, true, true, true);
2678 finish_declspecs (specs);
2679 pending_xref_error ();
2680 prefix_attrs = specs->attrs;
2681 specs->attrs = NULL_TREE;
2682 declarator = c_parser_declarator (parser, specs->type_seen_p,
2683 C_DTR_PARM, &dummy);
2684 if (declarator == NULL)
2685 {
2686 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
2687 return NULL;
2688 }
2689 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2690 postfix_attrs = c_parser_attributes (parser);
2691 return build_c_parm (specs, chainon (postfix_attrs, prefix_attrs),
2692 declarator);
2693}
2694
2695/* Parse a string literal in an asm expression. It should not be
2696 translated, and wide string literals are an error although
2697 permitted by the syntax. This is a GNU extension.
2698
2699 asm-string-literal:
2700 string-literal
2701
2702 ??? At present, following the old parser, the caller needs to have
46c2514e
TT
2703 set lex_untranslated_string to 1. It would be better to follow the
2704 C++ parser rather than using this kludge. */
27bf414c
JM
2705
2706static tree
2707c_parser_asm_string_literal (c_parser *parser)
2708{
2709 tree str;
2710 if (c_parser_next_token_is (parser, CPP_STRING))
2711 {
2712 str = c_parser_peek_token (parser)->value;
2713 c_parser_consume_token (parser);
2714 }
2715 else if (c_parser_next_token_is (parser, CPP_WSTRING))
2716 {
3ba09659
AH
2717 error_at (c_parser_peek_token (parser)->location,
2718 "wide string literal in %<asm%>");
27bf414c
JM
2719 str = build_string (1, "");
2720 c_parser_consume_token (parser);
2721 }
2722 else
2723 {
2724 c_parser_error (parser, "expected string literal");
2725 str = NULL_TREE;
2726 }
2727 return str;
2728}
2729
2730/* Parse a simple asm expression. This is used in restricted
2731 contexts, where a full expression with inputs and outputs does not
2732 make sense. This is a GNU extension.
2733
2734 simple-asm-expr:
2735 asm ( asm-string-literal )
2736*/
2737
2738static tree
2739c_parser_simple_asm_expr (c_parser *parser)
2740{
2741 tree str;
2742 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
2743 /* ??? Follow the C++ parser rather than using the
46c2514e
TT
2744 lex_untranslated_string kludge. */
2745 parser->lex_untranslated_string = true;
27bf414c
JM
2746 c_parser_consume_token (parser);
2747 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2748 {
46c2514e 2749 parser->lex_untranslated_string = false;
27bf414c
JM
2750 return NULL_TREE;
2751 }
2752 str = c_parser_asm_string_literal (parser);
46c2514e 2753 parser->lex_untranslated_string = false;
27bf414c
JM
2754 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
2755 {
2756 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
2757 return NULL_TREE;
2758 }
2759 return str;
2760}
2761
2762/* Parse (possibly empty) attributes. This is a GNU extension.
2763
2764 attributes:
2765 empty
2766 attributes attribute
2767
2768 attribute:
2769 __attribute__ ( ( attribute-list ) )
2770
2771 attribute-list:
2772 attrib
2773 attribute_list , attrib
2774
2775 attrib:
2776 empty
2777 any-word
2778 any-word ( identifier )
2779 any-word ( identifier , nonempty-expr-list )
2780 any-word ( expr-list )
2781
2782 where the "identifier" must not be declared as a type, and
2783 "any-word" may be any identifier (including one declared as a
2784 type), a reserved word storage class specifier, type specifier or
2785 type qualifier. ??? This still leaves out most reserved keywords
2786 (following the old parser), shouldn't we include them, and why not
2787 allow identifiers declared as types to start the arguments? */
2788
2789static tree
2790c_parser_attributes (c_parser *parser)
2791{
2792 tree attrs = NULL_TREE;
2793 while (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2794 {
2795 /* ??? Follow the C++ parser rather than using the
46c2514e
TT
2796 lex_untranslated_string kludge. */
2797 parser->lex_untranslated_string = true;
27bf414c
JM
2798 c_parser_consume_token (parser);
2799 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2800 {
46c2514e 2801 parser->lex_untranslated_string = false;
27bf414c
JM
2802 return attrs;
2803 }
2804 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2805 {
46c2514e 2806 parser->lex_untranslated_string = false;
27bf414c
JM
2807 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
2808 return attrs;
2809 }
2810 /* Parse the attribute list. */
2811 while (c_parser_next_token_is (parser, CPP_COMMA)
2812 || c_parser_next_token_is (parser, CPP_NAME)
2813 || c_parser_next_token_is (parser, CPP_KEYWORD))
2814 {
2815 tree attr, attr_name, attr_args;
bbbbb16a 2816 VEC(tree,gc) *expr_list;
27bf414c
JM
2817 if (c_parser_next_token_is (parser, CPP_COMMA))
2818 {
2819 c_parser_consume_token (parser);
2820 continue;
2821 }
2822 if (c_parser_next_token_is (parser, CPP_KEYWORD))
2823 {
2824 /* ??? See comment above about what keywords are
2825 accepted here. */
2826 bool ok;
2827 switch (c_parser_peek_token (parser)->keyword)
2828 {
2829 case RID_STATIC:
2830 case RID_UNSIGNED:
2831 case RID_LONG:
2832 case RID_CONST:
2833 case RID_EXTERN:
2834 case RID_REGISTER:
2835 case RID_TYPEDEF:
2836 case RID_SHORT:
2837 case RID_INLINE:
2838 case RID_VOLATILE:
2839 case RID_SIGNED:
2840 case RID_AUTO:
2841 case RID_RESTRICT:
2842 case RID_COMPLEX:
2843 case RID_THREAD:
2844 case RID_INT:
2845 case RID_CHAR:
2846 case RID_FLOAT:
2847 case RID_DOUBLE:
2848 case RID_VOID:
9a8ce21f
JG
2849 case RID_DFLOAT32:
2850 case RID_DFLOAT64:
2851 case RID_DFLOAT128:
27bf414c 2852 case RID_BOOL:
ab22c1fa
CF
2853 case RID_FRACT:
2854 case RID_ACCUM:
2855 case RID_SAT:
27bf414c
JM
2856 ok = true;
2857 break;
2858 default:
2859 ok = false;
2860 break;
2861 }
2862 if (!ok)
2863 break;
5dc4a7f4
JJ
2864 /* Accept __attribute__((__const)) as __attribute__((const))
2865 etc. */
2866 attr_name
2867 = ridpointers[(int) c_parser_peek_token (parser)->keyword];
27bf414c 2868 }
5dc4a7f4
JJ
2869 else
2870 attr_name = c_parser_peek_token (parser)->value;
27bf414c
JM
2871 c_parser_consume_token (parser);
2872 if (c_parser_next_token_is_not (parser, CPP_OPEN_PAREN))
2873 {
2874 attr = build_tree_list (attr_name, NULL_TREE);
2875 attrs = chainon (attrs, attr);
2876 continue;
2877 }
2878 c_parser_consume_token (parser);
2879 /* Parse the attribute contents. If they start with an
2880 identifier which is followed by a comma or close
2881 parenthesis, then the arguments start with that
2882 identifier; otherwise they are an expression list. */
2883 if (c_parser_next_token_is (parser, CPP_NAME)
2884 && c_parser_peek_token (parser)->id_kind == C_ID_ID
2885 && ((c_parser_peek_2nd_token (parser)->type == CPP_COMMA)
2886 || (c_parser_peek_2nd_token (parser)->type
2887 == CPP_CLOSE_PAREN)))
2888 {
2889 tree arg1 = c_parser_peek_token (parser)->value;
2890 c_parser_consume_token (parser);
2891 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2892 attr_args = build_tree_list (NULL_TREE, arg1);
2893 else
2894 {
bbbbb16a 2895 tree tree_list;
27bf414c 2896 c_parser_consume_token (parser);
bbbbb16a 2897 expr_list = c_parser_expr_list (parser, false, true, NULL);
c166b898 2898 tree_list = build_tree_list_vec (expr_list);
bbbbb16a 2899 attr_args = tree_cons (NULL_TREE, arg1, tree_list);
c166b898 2900 release_tree_vector (expr_list);
27bf414c
JM
2901 }
2902 }
2903 else
2904 {
2905 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2906 attr_args = NULL_TREE;
2907 else
bbbbb16a
ILT
2908 {
2909 expr_list = c_parser_expr_list (parser, false, true, NULL);
c166b898
ILT
2910 attr_args = build_tree_list_vec (expr_list);
2911 release_tree_vector (expr_list);
bbbbb16a 2912 }
27bf414c
JM
2913 }
2914 attr = build_tree_list (attr_name, attr_args);
2915 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2916 c_parser_consume_token (parser);
2917 else
2918 {
46c2514e 2919 parser->lex_untranslated_string = false;
27bf414c
JM
2920 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2921 "expected %<)%>");
2922 return attrs;
2923 }
2924 attrs = chainon (attrs, attr);
2925 }
2926 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2927 c_parser_consume_token (parser);
2928 else
2929 {
46c2514e 2930 parser->lex_untranslated_string = false;
27bf414c
JM
2931 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2932 "expected %<)%>");
2933 return attrs;
2934 }
2935 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2936 c_parser_consume_token (parser);
2937 else
2938 {
46c2514e 2939 parser->lex_untranslated_string = false;
27bf414c
JM
2940 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2941 "expected %<)%>");
2942 return attrs;
2943 }
46c2514e 2944 parser->lex_untranslated_string = false;
27bf414c
JM
2945 }
2946 return attrs;
2947}
2948
2949/* Parse a type name (C90 6.5.5, C99 6.7.6).
2950
2951 type-name:
2952 specifier-qualifier-list abstract-declarator[opt]
2953*/
2954
2955static struct c_type_name *
2956c_parser_type_name (c_parser *parser)
2957{
2958 struct c_declspecs *specs = build_null_declspecs ();
2959 struct c_declarator *declarator;
2960 struct c_type_name *ret;
2961 bool dummy = false;
2962 c_parser_declspecs (parser, specs, false, true, true);
2963 if (!specs->declspecs_seen_p)
2964 {
2965 c_parser_error (parser, "expected specifier-qualifier-list");
2966 return NULL;
2967 }
2968 pending_xref_error ();
2969 finish_declspecs (specs);
2970 declarator = c_parser_declarator (parser, specs->type_seen_p,
2971 C_DTR_ABSTRACT, &dummy);
2972 if (declarator == NULL)
2973 return NULL;
2974 ret = XOBNEW (&parser_obstack, struct c_type_name);
2975 ret->specs = specs;
2976 ret->declarator = declarator;
2977 return ret;
2978}
2979
2980/* Parse an initializer (C90 6.5.7, C99 6.7.8).
2981
2982 initializer:
2983 assignment-expression
2984 { initializer-list }
2985 { initializer-list , }
2986
2987 initializer-list:
2988 designation[opt] initializer
2989 initializer-list , designation[opt] initializer
2990
2991 designation:
2992 designator-list =
2993
2994 designator-list:
2995 designator
2996 designator-list designator
2997
2998 designator:
2999 array-designator
3000 . identifier
3001
3002 array-designator:
3003 [ constant-expression ]
3004
3005 GNU extensions:
3006
3007 initializer:
3008 { }
3009
3010 designation:
3011 array-designator
3012 identifier :
3013
3014 array-designator:
3015 [ constant-expression ... constant-expression ]
3016
3017 Any expression without commas is accepted in the syntax for the
3018 constant-expressions, with non-constant expressions rejected later.
3019
3020 This function is only used for top-level initializers; for nested
3021 ones, see c_parser_initval. */
3022
3023static struct c_expr
3024c_parser_initializer (c_parser *parser)
3025{
3026 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
3027 return c_parser_braced_init (parser, NULL_TREE, false);
3028 else
46bdb9cf
JM
3029 {
3030 struct c_expr ret;
c2255bc4 3031 location_t loc = c_parser_peek_token (parser)->location;
46bdb9cf
JM
3032 ret = c_parser_expr_no_commas (parser, NULL);
3033 if (TREE_CODE (ret.value) != STRING_CST
3034 && TREE_CODE (ret.value) != COMPOUND_LITERAL_EXPR)
c2255bc4 3035 ret = default_function_array_conversion (loc, ret);
46bdb9cf
JM
3036 return ret;
3037 }
27bf414c
JM
3038}
3039
3040/* Parse a braced initializer list. TYPE is the type specified for a
3041 compound literal, and NULL_TREE for other initializers and for
3042 nested braced lists. NESTED_P is true for nested braced lists,
3043 false for the list of a compound literal or the list that is the
3044 top-level initializer in a declaration. */
3045
3046static struct c_expr
3047c_parser_braced_init (c_parser *parser, tree type, bool nested_p)
3048{
c7412148 3049 location_t brace_loc = c_parser_peek_token (parser)->location;
27bf414c
JM
3050 gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
3051 c_parser_consume_token (parser);
3052 if (nested_p)
3053 push_init_level (0);
3054 else
3055 really_start_incremental_init (type);
3056 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3057 {
509c9d60 3058 pedwarn (brace_loc, OPT_pedantic, "ISO C forbids empty initializer braces");
27bf414c
JM
3059 }
3060 else
3061 {
3062 /* Parse a non-empty initializer list, possibly with a trailing
3063 comma. */
3064 while (true)
3065 {
3066 c_parser_initelt (parser);
3067 if (parser->error)
3068 break;
3069 if (c_parser_next_token_is (parser, CPP_COMMA))
3070 c_parser_consume_token (parser);
3071 else
3072 break;
3073 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3074 break;
3075 }
3076 }
3077 if (c_parser_next_token_is_not (parser, CPP_CLOSE_BRACE))
3078 {
3079 struct c_expr ret;
3080 ret.value = error_mark_node;
3081 ret.original_code = ERROR_MARK;
6866c6e8 3082 ret.original_type = NULL;
27bf414c 3083 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, "expected %<}%>");
a47e20fd 3084 pop_init_level (0);
27bf414c
JM
3085 return ret;
3086 }
3087 c_parser_consume_token (parser);
3088 return pop_init_level (0);
3089}
3090
3091/* Parse a nested initializer, including designators. */
3092
3093static void
3094c_parser_initelt (c_parser *parser)
3095{
3096 /* Parse any designator or designator list. A single array
3097 designator may have the subsequent "=" omitted in GNU C, but a
3098 longer list or a structure member designator may not. */
3099 if (c_parser_next_token_is (parser, CPP_NAME)
3100 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
3101 {
3102 /* Old-style structure member designator. */
3103 set_init_label (c_parser_peek_token (parser)->value);
fcf73884 3104 /* Use the colon as the error location. */
509c9d60
MLI
3105 pedwarn (c_parser_peek_2nd_token (parser)->location, OPT_pedantic,
3106 "obsolete use of designated initializer with %<:%>");
27bf414c
JM
3107 c_parser_consume_token (parser);
3108 c_parser_consume_token (parser);
3109 }
3110 else
3111 {
3112 /* des_seen is 0 if there have been no designators, 1 if there
3113 has been a single array designator and 2 otherwise. */
3114 int des_seen = 0;
c7412148 3115 /* Location of a designator. */
922f2908 3116 location_t des_loc = UNKNOWN_LOCATION; /* Quiet warning. */
27bf414c
JM
3117 while (c_parser_next_token_is (parser, CPP_OPEN_SQUARE)
3118 || c_parser_next_token_is (parser, CPP_DOT))
3119 {
3120 int des_prev = des_seen;
c7412148
TT
3121 if (!des_seen)
3122 des_loc = c_parser_peek_token (parser)->location;
27bf414c
JM
3123 if (des_seen < 2)
3124 des_seen++;
3125 if (c_parser_next_token_is (parser, CPP_DOT))
3126 {
3127 des_seen = 2;
3128 c_parser_consume_token (parser);
3129 if (c_parser_next_token_is (parser, CPP_NAME))
3130 {
3131 set_init_label (c_parser_peek_token (parser)->value);
3132 c_parser_consume_token (parser);
3133 }
3134 else
3135 {
3136 struct c_expr init;
3137 init.value = error_mark_node;
3138 init.original_code = ERROR_MARK;
6866c6e8 3139 init.original_type = NULL;
27bf414c
JM
3140 c_parser_error (parser, "expected identifier");
3141 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
b295aee2 3142 process_init_element (init, false);
27bf414c
JM
3143 return;
3144 }
3145 }
3146 else
3147 {
3148 tree first, second;
922f2908 3149 location_t ellipsis_loc = UNKNOWN_LOCATION; /* Quiet warning. */
27bf414c
JM
3150 /* ??? Following the old parser, [ objc-receiver
3151 objc-message-args ] is accepted as an initializer,
3152 being distinguished from a designator by what follows
3153 the first assignment expression inside the square
3154 brackets, but after a first array designator a
3155 subsequent square bracket is for Objective-C taken to
3156 start an expression, using the obsolete form of
3157 designated initializer without '=', rather than
3158 possibly being a second level of designation: in LALR
3159 terms, the '[' is shifted rather than reducing
3160 designator to designator-list. */
3161 if (des_prev == 1 && c_dialect_objc ())
3162 {
3163 des_seen = des_prev;
3164 break;
3165 }
3166 if (des_prev == 0 && c_dialect_objc ())
3167 {
3168 /* This might be an array designator or an
3169 Objective-C message expression. If the former,
3170 continue parsing here; if the latter, parse the
3171 remainder of the initializer given the starting
3172 primary-expression. ??? It might make sense to
3173 distinguish when des_prev == 1 as well; see
3174 previous comment. */
3175 tree rec, args;
3176 struct c_expr mexpr;
3177 c_parser_consume_token (parser);
3178 if (c_parser_peek_token (parser)->type == CPP_NAME
3179 && ((c_parser_peek_token (parser)->id_kind
3180 == C_ID_TYPENAME)
3181 || (c_parser_peek_token (parser)->id_kind
3182 == C_ID_CLASSNAME)))
3183 {
3184 /* Type name receiver. */
3185 tree id = c_parser_peek_token (parser)->value;
3186 c_parser_consume_token (parser);
3187 rec = objc_get_class_reference (id);
3188 goto parse_message_args;
3189 }
3190 first = c_parser_expr_no_commas (parser, NULL).value;
3191 if (c_parser_next_token_is (parser, CPP_ELLIPSIS)
3192 || c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3193 goto array_desig_after_first;
3194 /* Expression receiver. So far only one part
3195 without commas has been parsed; there might be
3196 more of the expression. */
3197 rec = first;
3198 while (c_parser_next_token_is (parser, CPP_COMMA))
3199 {
f2a71bbc 3200 struct c_expr next;
c2255bc4
AH
3201 location_t comma_loc, exp_loc;
3202 comma_loc = c_parser_peek_token (parser)->location;
27bf414c 3203 c_parser_consume_token (parser);
c2255bc4 3204 exp_loc = c_parser_peek_token (parser)->location;
f2a71bbc 3205 next = c_parser_expr_no_commas (parser, NULL);
c2255bc4
AH
3206 next = default_function_array_conversion (exp_loc, next);
3207 rec = build_compound_expr (comma_loc, rec, next.value);
27bf414c
JM
3208 }
3209 parse_message_args:
3210 /* Now parse the objc-message-args. */
3211 args = c_parser_objc_message_args (parser);
3212 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
3213 "expected %<]%>");
3214 mexpr.value
3215 = objc_build_message_expr (build_tree_list (rec, args));
3216 mexpr.original_code = ERROR_MARK;
6866c6e8 3217 mexpr.original_type = NULL;
27bf414c
JM
3218 /* Now parse and process the remainder of the
3219 initializer, starting with this message
3220 expression as a primary-expression. */
3221 c_parser_initval (parser, &mexpr);
3222 return;
3223 }
3224 c_parser_consume_token (parser);
3225 first = c_parser_expr_no_commas (parser, NULL).value;
3226 array_desig_after_first:
3227 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3228 {
c7412148 3229 ellipsis_loc = c_parser_peek_token (parser)->location;
27bf414c
JM
3230 c_parser_consume_token (parser);
3231 second = c_parser_expr_no_commas (parser, NULL).value;
3232 }
3233 else
3234 second = NULL_TREE;
3235 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3236 {
3237 c_parser_consume_token (parser);
3238 set_init_index (first, second);
fcf73884 3239 if (second)
509c9d60
MLI
3240 pedwarn (ellipsis_loc, OPT_pedantic,
3241 "ISO C forbids specifying range of elements to initialize");
27bf414c
JM
3242 }
3243 else
3244 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
3245 "expected %<]%>");
3246 }
3247 }
3248 if (des_seen >= 1)
3249 {
3250 if (c_parser_next_token_is (parser, CPP_EQ))
3251 {
fcf73884 3252 if (!flag_isoc99)
509c9d60
MLI
3253 pedwarn (des_loc, OPT_pedantic,
3254 "ISO C90 forbids specifying subobject to initialize");
27bf414c
JM
3255 c_parser_consume_token (parser);
3256 }
3257 else
3258 {
3259 if (des_seen == 1)
509c9d60
MLI
3260 pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic,
3261 "obsolete use of designated initializer without %<=%>");
27bf414c
JM
3262 else
3263 {
3264 struct c_expr init;
3265 init.value = error_mark_node;
3266 init.original_code = ERROR_MARK;
6866c6e8 3267 init.original_type = NULL;
27bf414c
JM
3268 c_parser_error (parser, "expected %<=%>");
3269 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
b295aee2 3270 process_init_element (init, false);
27bf414c
JM
3271 return;
3272 }
3273 }
3274 }
3275 }
3276 c_parser_initval (parser, NULL);
3277}
3278
3279/* Parse a nested initializer; as c_parser_initializer but parses
3280 initializers within braced lists, after any designators have been
3281 applied. If AFTER is not NULL then it is an Objective-C message
3282 expression which is the primary-expression starting the
3283 initializer. */
3284
3285static void
3286c_parser_initval (c_parser *parser, struct c_expr *after)
3287{
3288 struct c_expr init;
3289 gcc_assert (!after || c_dialect_objc ());
3290 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE) && !after)
3291 init = c_parser_braced_init (parser, NULL_TREE, true);
3292 else
46bdb9cf 3293 {
c2255bc4 3294 location_t loc = c_parser_peek_token (parser)->location;
46bdb9cf
JM
3295 init = c_parser_expr_no_commas (parser, after);
3296 if (init.value != NULL_TREE
3297 && TREE_CODE (init.value) != STRING_CST
3298 && TREE_CODE (init.value) != COMPOUND_LITERAL_EXPR)
c2255bc4 3299 init = default_function_array_conversion (loc, init);
46bdb9cf 3300 }
b295aee2 3301 process_init_element (init, false);
27bf414c
JM
3302}
3303
3304/* Parse a compound statement (possibly a function body) (C90 6.6.2,
3305 C99 6.8.2).
3306
3307 compound-statement:
3308 { block-item-list[opt] }
3309 { label-declarations block-item-list }
3310
3311 block-item-list:
3312 block-item
3313 block-item-list block-item
3314
3315 block-item:
3316 nested-declaration
3317 statement
3318
3319 nested-declaration:
3320 declaration
3321
3322 GNU extensions:
3323
3324 compound-statement:
3325 { label-declarations block-item-list }
3326
3327 nested-declaration:
3328 __extension__ nested-declaration
3329 nested-function-definition
3330
3331 label-declarations:
3332 label-declaration
3333 label-declarations label-declaration
3334
3335 label-declaration:
3336 __label__ identifier-list ;
3337
3338 Allowing the mixing of declarations and code is new in C99. The
3339 GNU syntax also permits (not shown above) labels at the end of
3340 compound statements, which yield an error. We don't allow labels
3341 on declarations; this might seem like a natural extension, but
3342 there would be a conflict between attributes on the label and
3343 prefix attributes on the declaration. ??? The syntax follows the
3344 old parser in requiring something after label declarations.
3345 Although they are erroneous if the labels declared aren't defined,
953ff289
DN
3346 is it useful for the syntax to be this way?
3347
3348 OpenMP:
3349
3350 block-item:
3351 openmp-directive
3352
3353 openmp-directive:
3354 barrier-directive
3355 flush-directive */
27bf414c
JM
3356
3357static tree
3358c_parser_compound_statement (c_parser *parser)
3359{
3360 tree stmt;
c2255bc4
AH
3361 location_t brace_loc;
3362 brace_loc = c_parser_peek_token (parser)->location;
27bf414c 3363 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
5600f233
JM
3364 {
3365 /* Ensure a scope is entered and left anyway to avoid confusion
3366 if we have just prepared to enter a function body. */
3367 stmt = c_begin_compound_stmt (true);
c2255bc4 3368 c_end_compound_stmt (brace_loc, stmt, true);
5600f233
JM
3369 return error_mark_node;
3370 }
27bf414c
JM
3371 stmt = c_begin_compound_stmt (true);
3372 c_parser_compound_statement_nostart (parser);
c2255bc4 3373 return c_end_compound_stmt (brace_loc, stmt, true);
27bf414c
JM
3374}
3375
3376/* Parse a compound statement except for the opening brace. This is
3377 used for parsing both compound statements and statement expressions
3378 (which follow different paths to handling the opening). */
3379
3380static void
3381c_parser_compound_statement_nostart (c_parser *parser)
3382{
3383 bool last_stmt = false;
3384 bool last_label = false;
6ec637a4 3385 bool save_valid_for_pragma = valid_location_for_stdc_pragma_p ();
3ba09659 3386 location_t label_loc = UNKNOWN_LOCATION; /* Quiet warning. */
27bf414c
JM
3387 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3388 {
3389 c_parser_consume_token (parser);
3390 return;
3391 }
6ec637a4 3392 mark_valid_location_for_stdc_pragma (true);
27bf414c
JM
3393 if (c_parser_next_token_is_keyword (parser, RID_LABEL))
3394 {
3395 /* Read zero or more forward-declarations for labels that nested
3396 functions can jump to. */
6ec637a4 3397 mark_valid_location_for_stdc_pragma (false);
27bf414c
JM
3398 while (c_parser_next_token_is_keyword (parser, RID_LABEL))
3399 {
c2255bc4 3400 label_loc = c_parser_peek_token (parser)->location;
27bf414c
JM
3401 c_parser_consume_token (parser);
3402 /* Any identifiers, including those declared as type names,
3403 are OK here. */
3404 while (true)
3405 {
3406 tree label;
3407 if (c_parser_next_token_is_not (parser, CPP_NAME))
3408 {
3409 c_parser_error (parser, "expected identifier");
3410 break;
3411 }
3412 label
3413 = declare_label (c_parser_peek_token (parser)->value);
3414 C_DECLARED_LABEL_FLAG (label) = 1;
c2255bc4 3415 add_stmt (build_stmt (label_loc, DECL_EXPR, label));
27bf414c
JM
3416 c_parser_consume_token (parser);
3417 if (c_parser_next_token_is (parser, CPP_COMMA))
3418 c_parser_consume_token (parser);
3419 else
3420 break;
3421 }
3422 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
3423 }
c2255bc4 3424 pedwarn (label_loc, OPT_pedantic, "ISO C forbids label declarations");
27bf414c
JM
3425 }
3426 /* We must now have at least one statement, label or declaration. */
3427 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3428 {
6ec637a4 3429 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
27bf414c
JM
3430 c_parser_error (parser, "expected declaration or statement");
3431 c_parser_consume_token (parser);
3432 return;
3433 }
3434 while (c_parser_next_token_is_not (parser, CPP_CLOSE_BRACE))
3435 {
3436 location_t loc = c_parser_peek_token (parser)->location;
27bf414c
JM
3437 if (c_parser_next_token_is_keyword (parser, RID_CASE)
3438 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
3439 || (c_parser_next_token_is (parser, CPP_NAME)
3440 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
3441 {
c7412148
TT
3442 if (c_parser_next_token_is_keyword (parser, RID_CASE))
3443 label_loc = c_parser_peek_2nd_token (parser)->location;
3444 else
3445 label_loc = c_parser_peek_token (parser)->location;
27bf414c
JM
3446 last_label = true;
3447 last_stmt = false;
6ec637a4 3448 mark_valid_location_for_stdc_pragma (false);
27bf414c
JM
3449 c_parser_label (parser);
3450 }
3451 else if (!last_label
3452 && c_parser_next_token_starts_declspecs (parser))
3453 {
3454 last_label = false;
6ec637a4 3455 mark_valid_location_for_stdc_pragma (false);
27bf414c 3456 c_parser_declaration_or_fndef (parser, true, true, true, true);
fcf73884 3457 if (last_stmt)
509c9d60
MLI
3458 pedwarn_c90 (loc,
3459 (pedantic && !flag_isoc99)
fcf73884
MLI
3460 ? OPT_pedantic
3461 : OPT_Wdeclaration_after_statement,
509c9d60 3462 "ISO C90 forbids mixed declarations and code");
27bf414c
JM
3463 last_stmt = false;
3464 }
3465 else if (!last_label
3466 && c_parser_next_token_is_keyword (parser, RID_EXTENSION))
3467 {
3468 /* __extension__ can start a declaration, but is also an
3469 unary operator that can start an expression. Consume all
3470 but the last of a possible series of __extension__ to
3471 determine which. */
3472 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
3473 && (c_parser_peek_2nd_token (parser)->keyword
3474 == RID_EXTENSION))
3475 c_parser_consume_token (parser);
3476 if (c_token_starts_declspecs (c_parser_peek_2nd_token (parser)))
3477 {
3478 int ext;
3479 ext = disable_extension_diagnostics ();
3480 c_parser_consume_token (parser);
3481 last_label = false;
6ec637a4 3482 mark_valid_location_for_stdc_pragma (false);
27bf414c
JM
3483 c_parser_declaration_or_fndef (parser, true, true, true, true);
3484 /* Following the old parser, __extension__ does not
3485 disable this diagnostic. */
3486 restore_extension_diagnostics (ext);
fcf73884 3487 if (last_stmt)
509c9d60 3488 pedwarn_c90 (loc, (pedantic && !flag_isoc99)
fcf73884
MLI
3489 ? OPT_pedantic
3490 : OPT_Wdeclaration_after_statement,
509c9d60 3491 "ISO C90 forbids mixed declarations and code");
27bf414c
JM
3492 last_stmt = false;
3493 }
3494 else
3495 goto statement;
3496 }
bc4071dd
RH
3497 else if (c_parser_next_token_is (parser, CPP_PRAGMA))
3498 {
3499 /* External pragmas, and some omp pragmas, are not associated
3500 with regular c code, and so are not to be considered statements
3501 syntactically. This ensures that the user doesn't put them
3502 places that would turn into syntax errors if the directive
3503 were ignored. */
3504 if (c_parser_pragma (parser, pragma_compound))
3505 last_label = false, last_stmt = true;
3506 }
3507 else if (c_parser_next_token_is (parser, CPP_EOF))
3508 {
6ec637a4 3509 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
bc4071dd
RH
3510 c_parser_error (parser, "expected declaration or statement");
3511 return;
3512 }
b4b56033
MLI
3513 else if (c_parser_next_token_is_keyword (parser, RID_ELSE))
3514 {
3515 if (parser->in_if_block)
3516 {
6ec637a4 3517 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
3ba09659 3518 error_at (loc, """expected %<}%> before %<else%>");
b4b56033
MLI
3519 return;
3520 }
3521 else
3522 {
3ba09659 3523 error_at (loc, "%<else%> without a previous %<if%>");
b4b56033
MLI
3524 c_parser_consume_token (parser);
3525 continue;
3526 }
3527 }
27bf414c
JM
3528 else
3529 {
3530 statement:
3531 last_label = false;
3532 last_stmt = true;
6ec637a4 3533 mark_valid_location_for_stdc_pragma (false);
27bf414c
JM
3534 c_parser_statement_after_labels (parser);
3535 }
2c14ae9a
VR
3536
3537 parser->error = false;
27bf414c
JM
3538 }
3539 if (last_label)
3ba09659 3540 error_at (label_loc, "label at end of compound statement");
27bf414c 3541 c_parser_consume_token (parser);
6ec637a4
JJ
3542 /* Restore the value we started with. */
3543 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
27bf414c
JM
3544}
3545
3546/* Parse a label (C90 6.6.1, C99 6.8.1).
3547
3548 label:
3549 identifier : attributes[opt]
3550 case constant-expression :
3551 default :
3552
3553 GNU extensions:
3554
3555 label:
3556 case constant-expression ... constant-expression :
3557
3558 The use of attributes on labels is a GNU extension. The syntax in
3559 GNU C accepts any expressions without commas, non-constant
3560 expressions being rejected later. */
3561
3562static void
3563c_parser_label (c_parser *parser)
3564{
3565 location_t loc1 = c_parser_peek_token (parser)->location;
3566 tree label = NULL_TREE;
3567 if (c_parser_next_token_is_keyword (parser, RID_CASE))
3568 {
3569 tree exp1, exp2;
3570 c_parser_consume_token (parser);
3571 exp1 = c_parser_expr_no_commas (parser, NULL).value;
3572 if (c_parser_next_token_is (parser, CPP_COLON))
3573 {
3574 c_parser_consume_token (parser);
c2255bc4 3575 label = do_case (loc1, exp1, NULL_TREE);
27bf414c
JM
3576 }
3577 else if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3578 {
3579 c_parser_consume_token (parser);
3580 exp2 = c_parser_expr_no_commas (parser, NULL).value;
3581 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
c2255bc4 3582 label = do_case (loc1, exp1, exp2);
27bf414c
JM
3583 }
3584 else
3585 c_parser_error (parser, "expected %<:%> or %<...%>");
3586 }
3587 else if (c_parser_next_token_is_keyword (parser, RID_DEFAULT))
3588 {
3589 c_parser_consume_token (parser);
3590 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
c2255bc4 3591 label = do_case (loc1, NULL_TREE, NULL_TREE);
27bf414c
JM
3592 }
3593 else
3594 {
3595 tree name = c_parser_peek_token (parser)->value;
3596 tree tlab;
27bf414c 3597 tree attrs;
c7412148 3598 location_t loc2 = c_parser_peek_token (parser)->location;
27bf414c
JM
3599 gcc_assert (c_parser_next_token_is (parser, CPP_NAME));
3600 c_parser_consume_token (parser);
3601 gcc_assert (c_parser_next_token_is (parser, CPP_COLON));
27bf414c
JM
3602 c_parser_consume_token (parser);
3603 attrs = c_parser_attributes (parser);
3604 tlab = define_label (loc2, name);
3605 if (tlab)
3606 {
3607 decl_attributes (&tlab, attrs, 0);
c2255bc4 3608 label = add_stmt (build_stmt (loc1, LABEL_EXPR, tlab));
27bf414c
JM
3609 }
3610 }
3611 if (label)
3d57f0f0 3612 {
3d57f0f0
MLI
3613 if (c_parser_next_token_starts_declspecs (parser)
3614 && !(c_parser_next_token_is (parser, CPP_NAME)
3615 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
3616 {
3ba09659
AH
3617 error_at (c_parser_peek_token (parser)->location,
3618 "a label can only be part of a statement and "
3619 "a declaration is not a statement");
3d57f0f0
MLI
3620 c_parser_declaration_or_fndef (parser, /*fndef_ok*/ false,
3621 /*nested*/ true, /*empty_ok*/ false,
3622 /*start_attr_ok*/ true);
3623 }
3624 }
27bf414c
JM
3625}
3626
3627/* Parse a statement (C90 6.6, C99 6.8).
3628
3629 statement:
3630 labeled-statement
3631 compound-statement
3632 expression-statement
3633 selection-statement
3634 iteration-statement
3635 jump-statement
3636
3637 labeled-statement:
3638 label statement
3639
3640 expression-statement:
3641 expression[opt] ;
3642
3643 selection-statement:
3644 if-statement
3645 switch-statement
3646
3647 iteration-statement:
3648 while-statement
3649 do-statement
3650 for-statement
3651
3652 jump-statement:
3653 goto identifier ;
3654 continue ;
3655 break ;
3656 return expression[opt] ;
3657
3658 GNU extensions:
3659
3660 statement:
3661 asm-statement
3662
3663 jump-statement:
3664 goto * expression ;
3665
3666 Objective-C:
3667
3668 statement:
3669 objc-throw-statement
3670 objc-try-catch-statement
3671 objc-synchronized-statement
3672
3673 objc-throw-statement:
3674 @throw expression ;
3675 @throw ;
953ff289
DN
3676
3677 OpenMP:
3678
3679 statement:
3680 openmp-construct
3681
3682 openmp-construct:
3683 parallel-construct
3684 for-construct
3685 sections-construct
3686 single-construct
3687 parallel-for-construct
3688 parallel-sections-construct
3689 master-construct
3690 critical-construct
3691 atomic-construct
3692 ordered-construct
3693
3694 parallel-construct:
3695 parallel-directive structured-block
3696
3697 for-construct:
3698 for-directive iteration-statement
3699
3700 sections-construct:
3701 sections-directive section-scope
3702
3703 single-construct:
3704 single-directive structured-block
3705
3706 parallel-for-construct:
3707 parallel-for-directive iteration-statement
3708
3709 parallel-sections-construct:
3710 parallel-sections-directive section-scope
3711
3712 master-construct:
3713 master-directive structured-block
3714
3715 critical-construct:
3716 critical-directive structured-block
3717
3718 atomic-construct:
3719 atomic-directive expression-statement
3720
3721 ordered-construct:
3722 ordered-directive structured-block */
27bf414c
JM
3723
3724static void
3725c_parser_statement (c_parser *parser)
3726{
3727 while (c_parser_next_token_is_keyword (parser, RID_CASE)
3728 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
3729 || (c_parser_next_token_is (parser, CPP_NAME)
3730 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
3731 c_parser_label (parser);
3732 c_parser_statement_after_labels (parser);
3733}
3734
3735/* Parse a statement, other than a labeled statement. */
3736
3737static void
3738c_parser_statement_after_labels (c_parser *parser)
3739{
3740 location_t loc = c_parser_peek_token (parser)->location;
3741 tree stmt = NULL_TREE;
b4b56033
MLI
3742 bool in_if_block = parser->in_if_block;
3743 parser->in_if_block = false;
27bf414c
JM
3744 switch (c_parser_peek_token (parser)->type)
3745 {
3746 case CPP_OPEN_BRACE:
3747 add_stmt (c_parser_compound_statement (parser));
3748 break;
3749 case CPP_KEYWORD:
3750 switch (c_parser_peek_token (parser)->keyword)
3751 {
3752 case RID_IF:
3753 c_parser_if_statement (parser);
3754 break;
3755 case RID_SWITCH:
3756 c_parser_switch_statement (parser);
3757 break;
3758 case RID_WHILE:
3759 c_parser_while_statement (parser);
3760 break;
3761 case RID_DO:
3762 c_parser_do_statement (parser);
3763 break;
3764 case RID_FOR:
3765 c_parser_for_statement (parser);
3766 break;
3767 case RID_GOTO:
3768 c_parser_consume_token (parser);
3769 if (c_parser_next_token_is (parser, CPP_NAME))
3770 {
c2255bc4
AH
3771 stmt = c_finish_goto_label (loc,
3772 c_parser_peek_token (parser)->value);
27bf414c
JM
3773 c_parser_consume_token (parser);
3774 }
3775 else if (c_parser_next_token_is (parser, CPP_MULT))
3776 {
3777 c_parser_consume_token (parser);
c2255bc4
AH
3778 stmt = c_finish_goto_ptr (loc,
3779 c_parser_expression (parser).value);
27bf414c
JM
3780 }
3781 else
3782 c_parser_error (parser, "expected identifier or %<*%>");
3783 goto expect_semicolon;
3784 case RID_CONTINUE:
3785 c_parser_consume_token (parser);
c2255bc4 3786 stmt = c_finish_bc_stmt (loc, &c_cont_label, false);
27bf414c
JM
3787 goto expect_semicolon;
3788 case RID_BREAK:
3789 c_parser_consume_token (parser);
c2255bc4 3790 stmt = c_finish_bc_stmt (loc, &c_break_label, true);
27bf414c
JM
3791 goto expect_semicolon;
3792 case RID_RETURN:
3793 c_parser_consume_token (parser);
3794 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
3795 {
c2255bc4 3796 stmt = c_finish_return (loc, NULL_TREE, NULL_TREE);
27bf414c
JM
3797 c_parser_consume_token (parser);
3798 }
3799 else
3800 {
bbbbb16a 3801 struct c_expr expr = c_parser_expression_conv (parser);
c2255bc4 3802 stmt = c_finish_return (loc, expr.value, expr.original_type);
27bf414c
JM
3803 goto expect_semicolon;
3804 }
3805 break;
3806 case RID_ASM:
3807 stmt = c_parser_asm_statement (parser);
3808 break;
eea1139b 3809 case RID_THROW:
27bf414c
JM
3810 gcc_assert (c_dialect_objc ());
3811 c_parser_consume_token (parser);
3812 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
3813 {
c2255bc4 3814 stmt = objc_build_throw_stmt (loc, NULL_TREE);
27bf414c
JM
3815 c_parser_consume_token (parser);
3816 }
3817 else
3818 {
928c19bb
JM
3819 tree expr = c_parser_expression (parser).value;
3820 expr = c_fully_fold (expr, false, NULL);
c2255bc4 3821 stmt = objc_build_throw_stmt (loc, expr);
27bf414c
JM
3822 goto expect_semicolon;
3823 }
3824 break;
eea1139b 3825 case RID_TRY:
27bf414c
JM
3826 gcc_assert (c_dialect_objc ());
3827 c_parser_objc_try_catch_statement (parser);
3828 break;
3829 case RID_AT_SYNCHRONIZED:
3830 gcc_assert (c_dialect_objc ());
3831 c_parser_objc_synchronized_statement (parser);
3832 break;
3833 default:
3834 goto expr_stmt;
3835 }
3836 break;
3837 case CPP_SEMICOLON:
3838 c_parser_consume_token (parser);
3839 break;
3840 case CPP_CLOSE_PAREN:
3841 case CPP_CLOSE_SQUARE:
3842 /* Avoid infinite loop in error recovery:
3843 c_parser_skip_until_found stops at a closing nesting
3844 delimiter without consuming it, but here we need to consume
3845 it to proceed further. */
3846 c_parser_error (parser, "expected statement");
3847 c_parser_consume_token (parser);
3848 break;
bc4071dd
RH
3849 case CPP_PRAGMA:
3850 c_parser_pragma (parser, pragma_stmt);
3851 break;
27bf414c
JM
3852 default:
3853 expr_stmt:
c2255bc4 3854 stmt = c_finish_expr_stmt (loc, c_parser_expression_conv (parser).value);
27bf414c
JM
3855 expect_semicolon:
3856 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
3857 break;
3858 }
3859 /* Two cases cannot and do not have line numbers associated: If stmt
3860 is degenerate, such as "2;", then stmt is an INTEGER_CST, which
3861 cannot hold line numbers. But that's OK because the statement
3862 will either be changed to a MODIFY_EXPR during gimplification of
3863 the statement expr, or discarded. If stmt was compound, but
3864 without new variables, we will have skipped the creation of a
3865 BIND and will have a bare STATEMENT_LIST. But that's OK because
3866 (recursively) all of the component statements should already have
3867 line numbers assigned. ??? Can we discard no-op statements
3868 earlier? */
c2255bc4
AH
3869 if (CAN_HAVE_LOCATION_P (stmt)
3870 && EXPR_LOCATION (stmt) == UNKNOWN_LOCATION)
3871 SET_EXPR_LOCATION (stmt, loc);
b4b56033
MLI
3872
3873 parser->in_if_block = in_if_block;
27bf414c
JM
3874}
3875
ca085fd7
MLI
3876/* Parse the condition from an if, do, while or for statements. */
3877
3878static tree
3879c_parser_condition (c_parser *parser)
3880{
c2255bc4 3881 location_t loc = c_parser_peek_token (parser)->location;
ca085fd7 3882 tree cond;
928c19bb
JM
3883 cond = c_parser_expression_conv (parser).value;
3884 cond = c_objc_common_truthvalue_conversion (loc, cond);
3885 cond = c_fully_fold (cond, false, NULL);
ca085fd7
MLI
3886 if (warn_sequence_point)
3887 verify_sequence_points (cond);
3888 return cond;
3889}
3890
27bf414c
JM
3891/* Parse a parenthesized condition from an if, do or while statement.
3892
3893 condition:
3894 ( expression )
3895*/
3896static tree
3897c_parser_paren_condition (c_parser *parser)
3898{
27bf414c
JM
3899 tree cond;
3900 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3901 return error_mark_node;
ca085fd7 3902 cond = c_parser_condition (parser);
27bf414c
JM
3903 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
3904 return cond;
3905}
3906
3907/* Parse a statement which is a block in C99. */
3908
3909static tree
3910c_parser_c99_block_statement (c_parser *parser)
3911{
3912 tree block = c_begin_compound_stmt (flag_isoc99);
c2255bc4 3913 location_t loc = c_parser_peek_token (parser)->location;
27bf414c 3914 c_parser_statement (parser);
c2255bc4 3915 return c_end_compound_stmt (loc, block, flag_isoc99);
27bf414c
JM
3916}
3917
b4b56033
MLI
3918/* Parse the body of an if statement. This is just parsing a
3919 statement but (a) it is a block in C99, (b) we track whether the
3920 body is an if statement for the sake of -Wparentheses warnings, (c)
3921 we handle an empty body specially for the sake of -Wempty-body
3922 warnings, and (d) we call parser_compound_statement directly
3923 because c_parser_statement_after_labels resets
3924 parser->in_if_block. */
27bf414c
JM
3925
3926static tree
3927c_parser_if_body (c_parser *parser, bool *if_p)
3928{
3929 tree block = c_begin_compound_stmt (flag_isoc99);
c2255bc4 3930 location_t body_loc = c_parser_peek_token (parser)->location;
27bf414c
JM
3931 while (c_parser_next_token_is_keyword (parser, RID_CASE)
3932 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
3933 || (c_parser_next_token_is (parser, CPP_NAME)
3934 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
3935 c_parser_label (parser);
3936 *if_p = c_parser_next_token_is_keyword (parser, RID_IF);
62e00e94 3937 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
b4b56033 3938 {
626c34b5 3939 location_t loc = c_parser_peek_token (parser)->location;
c2255bc4 3940 add_stmt (build_empty_stmt (loc));
b4b56033 3941 c_parser_consume_token (parser);
626c34b5
PB
3942 if (!c_parser_next_token_is_keyword (parser, RID_ELSE))
3943 warning_at (loc, OPT_Wempty_body,
3944 "suggest braces around empty body in an %<if%> statement");
b4b56033
MLI
3945 }
3946 else if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
3947 add_stmt (c_parser_compound_statement (parser));
3948 else
3949 c_parser_statement_after_labels (parser);
c2255bc4 3950 return c_end_compound_stmt (body_loc, block, flag_isoc99);
b4b56033
MLI
3951}
3952
3953/* Parse the else body of an if statement. This is just parsing a
3954 statement but (a) it is a block in C99, (b) we handle an empty body
3955 specially for the sake of -Wempty-body warnings. */
3956
3957static tree
3958c_parser_else_body (c_parser *parser)
3959{
c2255bc4 3960 location_t else_loc = c_parser_peek_token (parser)->location;
b4b56033
MLI
3961 tree block = c_begin_compound_stmt (flag_isoc99);
3962 while (c_parser_next_token_is_keyword (parser, RID_CASE)
3963 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
3964 || (c_parser_next_token_is (parser, CPP_NAME)
3965 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
3966 c_parser_label (parser);
3967 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
3968 {
c2255bc4
AH
3969 location_t loc = c_parser_peek_token (parser)->location;
3970 warning_at (loc,
626c34b5
PB
3971 OPT_Wempty_body,
3972 "suggest braces around empty body in an %<else%> statement");
c2255bc4 3973 add_stmt (build_empty_stmt (loc));
b4b56033
MLI
3974 c_parser_consume_token (parser);
3975 }
3976 else
3977 c_parser_statement_after_labels (parser);
c2255bc4 3978 return c_end_compound_stmt (else_loc, block, flag_isoc99);
27bf414c
JM
3979}
3980
3981/* Parse an if statement (C90 6.6.4, C99 6.8.4).
3982
3983 if-statement:
3984 if ( expression ) statement
3985 if ( expression ) statement else statement
3986*/
3987
3988static void
3989c_parser_if_statement (c_parser *parser)
3990{
3991 tree block;
3992 location_t loc;
3993 tree cond;
b4b56033 3994 bool first_if = false;
27bf414c 3995 tree first_body, second_body;
b4b56033
MLI
3996 bool in_if_block;
3997
27bf414c
JM
3998 gcc_assert (c_parser_next_token_is_keyword (parser, RID_IF));
3999 c_parser_consume_token (parser);
4000 block = c_begin_compound_stmt (flag_isoc99);
4001 loc = c_parser_peek_token (parser)->location;
4002 cond = c_parser_paren_condition (parser);
b4b56033
MLI
4003 in_if_block = parser->in_if_block;
4004 parser->in_if_block = true;
27bf414c 4005 first_body = c_parser_if_body (parser, &first_if);
b4b56033 4006 parser->in_if_block = in_if_block;
27bf414c
JM
4007 if (c_parser_next_token_is_keyword (parser, RID_ELSE))
4008 {
4009 c_parser_consume_token (parser);
b4b56033 4010 second_body = c_parser_else_body (parser);
27bf414c
JM
4011 }
4012 else
4013 second_body = NULL_TREE;
4014 c_finish_if_stmt (loc, cond, first_body, second_body, first_if);
c2255bc4 4015 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
27bf414c
JM
4016}
4017
4018/* Parse a switch statement (C90 6.6.4, C99 6.8.4).
4019
4020 switch-statement:
4021 switch (expression) statement
4022*/
4023
4024static void
4025c_parser_switch_statement (c_parser *parser)
4026{
4027 tree block, expr, body, save_break;
c2255bc4
AH
4028 location_t switch_loc = c_parser_peek_token (parser)->location;
4029 location_t switch_cond_loc;
27bf414c
JM
4030 gcc_assert (c_parser_next_token_is_keyword (parser, RID_SWITCH));
4031 c_parser_consume_token (parser);
4032 block = c_begin_compound_stmt (flag_isoc99);
4033 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4034 {
c2255bc4 4035 switch_cond_loc = c_parser_peek_token (parser)->location;
27bf414c
JM
4036 expr = c_parser_expression (parser).value;
4037 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
4038 }
4039 else
c2255bc4
AH
4040 {
4041 switch_cond_loc = UNKNOWN_LOCATION;
4042 expr = error_mark_node;
4043 }
4044 c_start_case (switch_loc, switch_cond_loc, expr);
27bf414c
JM
4045 save_break = c_break_label;
4046 c_break_label = NULL_TREE;
4047 body = c_parser_c99_block_statement (parser);
4048 c_finish_case (body);
4049 if (c_break_label)
c2255bc4
AH
4050 {
4051 location_t here = c_parser_peek_token (parser)->location;
4052 tree t = build1 (LABEL_EXPR, void_type_node, c_break_label);
4053 SET_EXPR_LOCATION (t, here);
4054 add_stmt (t);
4055 }
27bf414c 4056 c_break_label = save_break;
c2255bc4 4057 add_stmt (c_end_compound_stmt (switch_loc, block, flag_isoc99));
27bf414c
JM
4058}
4059
4060/* Parse a while statement (C90 6.6.5, C99 6.8.5).
4061
4062 while-statement:
4063 while (expression) statement
4064*/
4065
4066static void
4067c_parser_while_statement (c_parser *parser)
4068{
4069 tree block, cond, body, save_break, save_cont;
4070 location_t loc;
4071 gcc_assert (c_parser_next_token_is_keyword (parser, RID_WHILE));
4072 c_parser_consume_token (parser);
4073 block = c_begin_compound_stmt (flag_isoc99);
4074 loc = c_parser_peek_token (parser)->location;
4075 cond = c_parser_paren_condition (parser);
4076 save_break = c_break_label;
4077 c_break_label = NULL_TREE;
4078 save_cont = c_cont_label;
4079 c_cont_label = NULL_TREE;
4080 body = c_parser_c99_block_statement (parser);
4081 c_finish_loop (loc, cond, NULL, body, c_break_label, c_cont_label, true);
c2255bc4 4082 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
27bf414c
JM
4083 c_break_label = save_break;
4084 c_cont_label = save_cont;
4085}
4086
4087/* Parse a do statement (C90 6.6.5, C99 6.8.5).
4088
4089 do-statement:
4090 do statement while ( expression ) ;
4091*/
4092
4093static void
4094c_parser_do_statement (c_parser *parser)
4095{
4096 tree block, cond, body, save_break, save_cont, new_break, new_cont;
4097 location_t loc;
4098 gcc_assert (c_parser_next_token_is_keyword (parser, RID_DO));
4099 c_parser_consume_token (parser);
62e00e94 4100 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
3ba09659
AH
4101 warning_at (c_parser_peek_token (parser)->location,
4102 OPT_Wempty_body,
4103 "suggest braces around empty body in %<do%> statement");
27bf414c
JM
4104 block = c_begin_compound_stmt (flag_isoc99);
4105 loc = c_parser_peek_token (parser)->location;
4106 save_break = c_break_label;
4107 c_break_label = NULL_TREE;
4108 save_cont = c_cont_label;
4109 c_cont_label = NULL_TREE;
4110 body = c_parser_c99_block_statement (parser);
4111 c_parser_require_keyword (parser, RID_WHILE, "expected %<while%>");
4112 new_break = c_break_label;
4113 c_break_label = save_break;
4114 new_cont = c_cont_label;
4115 c_cont_label = save_cont;
4116 cond = c_parser_paren_condition (parser);
4117 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
4118 c_parser_skip_to_end_of_block_or_statement (parser);
4119 c_finish_loop (loc, cond, NULL, body, new_break, new_cont, false);
c2255bc4 4120 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
27bf414c
JM
4121}
4122
4123/* Parse a for statement (C90 6.6.5, C99 6.8.5).
4124
4125 for-statement:
4126 for ( expression[opt] ; expression[opt] ; expression[opt] ) statement
4127 for ( nested-declaration expression[opt] ; expression[opt] ) statement
4128
4129 The form with a declaration is new in C99.
4130
4131 ??? In accordance with the old parser, the declaration may be a
4132 nested function, which is then rejected in check_for_loop_decls,
4133 but does it make any sense for this to be included in the grammar?
4134 Note in particular that the nested function does not include a
4135 trailing ';', whereas the "declaration" production includes one.
4136 Also, can we reject bad declarations earlier and cheaper than
4137 check_for_loop_decls? */
4138
4139static void
4140c_parser_for_statement (c_parser *parser)
4141{
4142 tree block, cond, incr, save_break, save_cont, body;
c2255bc4
AH
4143 location_t loc = c_parser_peek_token (parser)->location;
4144 location_t for_loc = c_parser_peek_token (parser)->location;
27bf414c
JM
4145 gcc_assert (c_parser_next_token_is_keyword (parser, RID_FOR));
4146 c_parser_consume_token (parser);
4147 block = c_begin_compound_stmt (flag_isoc99);
4148 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4149 {
4150 /* Parse the initialization declaration or expression. */
4151 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4152 {
4153 c_parser_consume_token (parser);
c2255bc4 4154 c_finish_expr_stmt (loc, NULL_TREE);
27bf414c
JM
4155 }
4156 else if (c_parser_next_token_starts_declspecs (parser))
4157 {
4158 c_parser_declaration_or_fndef (parser, true, true, true, true);
c2255bc4 4159 check_for_loop_decls (for_loc);
27bf414c
JM
4160 }
4161 else if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
4162 {
4163 /* __extension__ can start a declaration, but is also an
4164 unary operator that can start an expression. Consume all
4165 but the last of a possible series of __extension__ to
4166 determine which. */
4167 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
4168 && (c_parser_peek_2nd_token (parser)->keyword
4169 == RID_EXTENSION))
4170 c_parser_consume_token (parser);
4171 if (c_token_starts_declspecs (c_parser_peek_2nd_token (parser)))
4172 {
4173 int ext;
4174 ext = disable_extension_diagnostics ();
4175 c_parser_consume_token (parser);
4176 c_parser_declaration_or_fndef (parser, true, true, true, true);
4177 restore_extension_diagnostics (ext);
c2255bc4 4178 check_for_loop_decls (for_loc);
27bf414c
JM
4179 }
4180 else
4181 goto init_expr;
4182 }
4183 else
4184 {
4185 init_expr:
c2255bc4 4186 c_finish_expr_stmt (loc, c_parser_expression (parser).value);
27bf414c
JM
4187 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
4188 }
4189 /* Parse the loop condition. */
27bf414c
JM
4190 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4191 {
4192 c_parser_consume_token (parser);
4193 cond = NULL_TREE;
4194 }
4195 else
4196 {
ca085fd7 4197 cond = c_parser_condition (parser);
27bf414c
JM
4198 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
4199 }
4200 /* Parse the increment expression. */
4201 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
c2255bc4 4202 incr = c_process_expr_stmt (loc, NULL_TREE);
27bf414c 4203 else
c2255bc4 4204 incr = c_process_expr_stmt (loc, c_parser_expression (parser).value);
27bf414c
JM
4205 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
4206 }
4207 else
4208 {
4209 cond = error_mark_node;
4210 incr = error_mark_node;
4211 }
4212 save_break = c_break_label;
4213 c_break_label = NULL_TREE;
4214 save_cont = c_cont_label;
4215 c_cont_label = NULL_TREE;
4216 body = c_parser_c99_block_statement (parser);
4217 c_finish_loop (loc, cond, incr, body, c_break_label, c_cont_label, true);
c2255bc4 4218 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
27bf414c
JM
4219 c_break_label = save_break;
4220 c_cont_label = save_cont;
4221}
4222
4223/* Parse an asm statement, a GNU extension. This is a full-blown asm
4224 statement with inputs, outputs, clobbers, and volatile tag
4225 allowed.
4226
4227 asm-statement:
4228 asm type-qualifier[opt] ( asm-argument ) ;
4229
4230 asm-argument:
4231 asm-string-literal
4232 asm-string-literal : asm-operands[opt]
4233 asm-string-literal : asm-operands[opt] : asm-operands[opt]
4234 asm-string-literal : asm-operands[opt] : asm-operands[opt] : asm-clobbers
4235
4236 Qualifiers other than volatile are accepted in the syntax but
4237 warned for. */
4238
4239static tree
4240c_parser_asm_statement (c_parser *parser)
4241{
4242 tree quals, str, outputs, inputs, clobbers, ret;
4243 bool simple;
c2255bc4 4244 location_t asm_loc = c_parser_peek_token (parser)->location;
27bf414c
JM
4245 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
4246 c_parser_consume_token (parser);
4247 if (c_parser_next_token_is_keyword (parser, RID_VOLATILE))
4248 {
4249 quals = c_parser_peek_token (parser)->value;
4250 c_parser_consume_token (parser);
4251 }
4252 else if (c_parser_next_token_is_keyword (parser, RID_CONST)
4253 || c_parser_next_token_is_keyword (parser, RID_RESTRICT))
4254 {
3ba09659
AH
4255 warning_at (c_parser_peek_token (parser)->location,
4256 0,
4257 "%E qualifier ignored on asm",
4258 c_parser_peek_token (parser)->value);
27bf414c
JM
4259 quals = NULL_TREE;
4260 c_parser_consume_token (parser);
4261 }
4262 else
4263 quals = NULL_TREE;
4264 /* ??? Follow the C++ parser rather than using the
46c2514e
TT
4265 lex_untranslated_string kludge. */
4266 parser->lex_untranslated_string = true;
27bf414c
JM
4267 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4268 {
46c2514e 4269 parser->lex_untranslated_string = false;
27bf414c
JM
4270 return NULL_TREE;
4271 }
4272 str = c_parser_asm_string_literal (parser);
b85eb797
JJ
4273 if (str == NULL_TREE)
4274 {
4275 parser->lex_untranslated_string = false;
4276 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4277 return NULL_TREE;
4278 }
27bf414c
JM
4279 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4280 {
4281 simple = true;
4282 outputs = NULL_TREE;
4283 inputs = NULL_TREE;
4284 clobbers = NULL_TREE;
4285 goto done_asm;
4286 }
4287 if (!c_parser_require (parser, CPP_COLON, "expected %<:%> or %<)%>"))
4288 {
46c2514e 4289 parser->lex_untranslated_string = false;
27bf414c
JM
4290 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4291 return NULL_TREE;
4292 }
4293 simple = false;
4294 /* Parse outputs. */
4295 if (c_parser_next_token_is (parser, CPP_COLON)
4296 || c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4297 outputs = NULL_TREE;
4298 else
46bdb9cf 4299 outputs = c_parser_asm_operands (parser, false);
27bf414c
JM
4300 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4301 {
4302 inputs = NULL_TREE;
4303 clobbers = NULL_TREE;
4304 goto done_asm;
4305 }
4306 if (!c_parser_require (parser, CPP_COLON, "expected %<:%> or %<)%>"))
4307 {
46c2514e 4308 parser->lex_untranslated_string = false;
27bf414c
JM
4309 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4310 return NULL_TREE;
4311 }
4312 /* Parse inputs. */
4313 if (c_parser_next_token_is (parser, CPP_COLON)
4314 || c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4315 inputs = NULL_TREE;
4316 else
46bdb9cf 4317 inputs = c_parser_asm_operands (parser, true);
27bf414c
JM
4318 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4319 {
4320 clobbers = NULL_TREE;
4321 goto done_asm;
4322 }
4323 if (!c_parser_require (parser, CPP_COLON, "expected %<:%> or %<)%>"))
4324 {
46c2514e 4325 parser->lex_untranslated_string = false;
27bf414c
JM
4326 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4327 return NULL_TREE;
4328 }
4329 /* Parse clobbers. */
4330 clobbers = c_parser_asm_clobbers (parser);
4331 done_asm:
46c2514e 4332 parser->lex_untranslated_string = false;
27bf414c
JM
4333 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
4334 {
4335 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4336 return NULL_TREE;
4337 }
4338 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
4339 c_parser_skip_to_end_of_block_or_statement (parser);
c2255bc4 4340 ret = build_asm_stmt (quals, build_asm_expr (asm_loc, str, outputs, inputs,
27bf414c
JM
4341 clobbers, simple));
4342 return ret;
4343}
4344
46bdb9cf
JM
4345/* Parse asm operands, a GNU extension. If CONVERT_P (for inputs but
4346 not outputs), apply the default conversion of functions and arrays
4347 to pointers.
27bf414c
JM
4348
4349 asm-operands:
4350 asm-operand
4351 asm-operands , asm-operand
4352
4353 asm-operand:
4354 asm-string-literal ( expression )
4355 [ identifier ] asm-string-literal ( expression )
4356*/
4357
4358static tree
46bdb9cf 4359c_parser_asm_operands (c_parser *parser, bool convert_p)
27bf414c
JM
4360{
4361 tree list = NULL_TREE;
c2255bc4 4362 location_t loc;
27bf414c
JM
4363 while (true)
4364 {
f2a71bbc
JM
4365 tree name, str;
4366 struct c_expr expr;
27bf414c
JM
4367 if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
4368 {
4369 c_parser_consume_token (parser);
4370 if (c_parser_next_token_is (parser, CPP_NAME))
4371 {
4372 tree id = c_parser_peek_token (parser)->value;
4373 c_parser_consume_token (parser);
4374 name = build_string (IDENTIFIER_LENGTH (id),
4375 IDENTIFIER_POINTER (id));
4376 }
4377 else
4378 {
4379 c_parser_error (parser, "expected identifier");
4380 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
4381 return NULL_TREE;
4382 }
4383 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
4384 "expected %<]%>");
4385 }
4386 else
4387 name = NULL_TREE;
4388 str = c_parser_asm_string_literal (parser);
4389 if (str == NULL_TREE)
4390 return NULL_TREE;
46c2514e 4391 parser->lex_untranslated_string = false;
27bf414c
JM
4392 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4393 {
46c2514e 4394 parser->lex_untranslated_string = true;
27bf414c
JM
4395 return NULL_TREE;
4396 }
c2255bc4 4397 loc = c_parser_peek_token (parser)->location;
f2a71bbc 4398 expr = c_parser_expression (parser);
46bdb9cf 4399 if (convert_p)
c2255bc4 4400 expr = default_function_array_conversion (loc, expr);
928c19bb 4401 expr.value = c_fully_fold (expr.value, false, NULL);
46c2514e 4402 parser->lex_untranslated_string = true;
27bf414c
JM
4403 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
4404 {
4405 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4406 return NULL_TREE;
4407 }
4408 list = chainon (list, build_tree_list (build_tree_list (name, str),
f2a71bbc 4409 expr.value));
27bf414c
JM
4410 if (c_parser_next_token_is (parser, CPP_COMMA))
4411 c_parser_consume_token (parser);
4412 else
4413 break;
4414 }
4415 return list;
4416}
4417
4418/* Parse asm clobbers, a GNU extension.
4419
4420 asm-clobbers:
4421 asm-string-literal
4422 asm-clobbers , asm-string-literal
4423*/
4424
4425static tree
4426c_parser_asm_clobbers (c_parser *parser)
4427{
4428 tree list = NULL_TREE;
4429 while (true)
4430 {
4431 tree str = c_parser_asm_string_literal (parser);
4432 if (str)
4433 list = tree_cons (NULL_TREE, str, list);
4434 else
4435 return NULL_TREE;
4436 if (c_parser_next_token_is (parser, CPP_COMMA))
4437 c_parser_consume_token (parser);
4438 else
4439 break;
4440 }
4441 return list;
4442}
4443
4444/* Parse an expression other than a compound expression; that is, an
4445 assignment expression (C90 6.3.16, C99 6.5.16). If AFTER is not
4446 NULL then it is an Objective-C message expression which is the
4447 primary-expression starting the expression as an initializer.
4448
4449 assignment-expression:
4450 conditional-expression
4451 unary-expression assignment-operator assignment-expression
4452
4453 assignment-operator: one of
4454 = *= /= %= += -= <<= >>= &= ^= |=
4455
4456 In GNU C we accept any conditional expression on the LHS and
4457 diagnose the invalid lvalue rather than producing a syntax
4458 error. */
4459
4460static struct c_expr
4461c_parser_expr_no_commas (c_parser *parser, struct c_expr *after)
4462{
4463 struct c_expr lhs, rhs, ret;
4464 enum tree_code code;
c2255bc4 4465 location_t op_location, exp_location;
27bf414c
JM
4466 gcc_assert (!after || c_dialect_objc ());
4467 lhs = c_parser_conditional_expression (parser, after);
c9f9eb5d 4468 op_location = c_parser_peek_token (parser)->location;
27bf414c
JM
4469 switch (c_parser_peek_token (parser)->type)
4470 {
4471 case CPP_EQ:
4472 code = NOP_EXPR;
4473 break;
4474 case CPP_MULT_EQ:
4475 code = MULT_EXPR;
4476 break;
4477 case CPP_DIV_EQ:
4478 code = TRUNC_DIV_EXPR;
4479 break;
4480 case CPP_MOD_EQ:
4481 code = TRUNC_MOD_EXPR;
4482 break;
4483 case CPP_PLUS_EQ:
4484 code = PLUS_EXPR;
4485 break;
4486 case CPP_MINUS_EQ:
4487 code = MINUS_EXPR;
4488 break;
4489 case CPP_LSHIFT_EQ:
4490 code = LSHIFT_EXPR;
4491 break;
4492 case CPP_RSHIFT_EQ:
4493 code = RSHIFT_EXPR;
4494 break;
4495 case CPP_AND_EQ:
4496 code = BIT_AND_EXPR;
4497 break;
4498 case CPP_XOR_EQ:
4499 code = BIT_XOR_EXPR;
4500 break;
4501 case CPP_OR_EQ:
4502 code = BIT_IOR_EXPR;
4503 break;
4504 default:
4505 return lhs;
4506 }
4507 c_parser_consume_token (parser);
c2255bc4 4508 exp_location = c_parser_peek_token (parser)->location;
27bf414c 4509 rhs = c_parser_expr_no_commas (parser, NULL);
c2255bc4 4510 rhs = default_function_array_conversion (exp_location, rhs);
32e8bb8e 4511 ret.value = build_modify_expr (op_location, lhs.value, lhs.original_type,
c2255bc4
AH
4512 code, exp_location, rhs.value,
4513 rhs.original_type);
27bf414c
JM
4514 if (code == NOP_EXPR)
4515 ret.original_code = MODIFY_EXPR;
4516 else
4517 {
4518 TREE_NO_WARNING (ret.value) = 1;
4519 ret.original_code = ERROR_MARK;
4520 }
6866c6e8 4521 ret.original_type = NULL;
27bf414c
JM
4522 return ret;
4523}
4524
4525/* Parse a conditional expression (C90 6.3.15, C99 6.5.15). If AFTER
4526 is not NULL then it is an Objective-C message expression which is
4527 the primary-expression starting the expression as an initializer.
4528
4529 conditional-expression:
4530 logical-OR-expression
4531 logical-OR-expression ? expression : conditional-expression
4532
4533 GNU extensions:
4534
4535 conditional-expression:
4536 logical-OR-expression ? : conditional-expression
4537*/
4538
4539static struct c_expr
4540c_parser_conditional_expression (c_parser *parser, struct c_expr *after)
4541{
4542 struct c_expr cond, exp1, exp2, ret;
744aa42f 4543 location_t cond_loc, colon_loc;
ba47d38d 4544
27bf414c 4545 gcc_assert (!after || c_dialect_objc ());
ba47d38d 4546
27bf414c 4547 cond = c_parser_binary_expression (parser, after);
ba47d38d 4548
27bf414c
JM
4549 if (c_parser_next_token_is_not (parser, CPP_QUERY))
4550 return cond;
c2255bc4
AH
4551 cond_loc = c_parser_peek_token (parser)->location;
4552 cond = default_function_array_conversion (cond_loc, cond);
27bf414c
JM
4553 c_parser_consume_token (parser);
4554 if (c_parser_next_token_is (parser, CPP_COLON))
4555 {
8ce94e44 4556 tree eptype = NULL_TREE;
509c9d60
MLI
4557 pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic,
4558 "ISO C forbids omitting the middle term of a ?: expression");
8ce94e44
JM
4559 if (TREE_CODE (cond.value) == EXCESS_PRECISION_EXPR)
4560 {
4561 eptype = TREE_TYPE (cond.value);
4562 cond.value = TREE_OPERAND (cond.value, 0);
4563 }
27bf414c 4564 /* Make sure first operand is calculated only once. */
928c19bb 4565 exp1.value = c_save_expr (default_conversion (cond.value));
8ce94e44
JM
4566 if (eptype)
4567 exp1.value = build1 (EXCESS_PRECISION_EXPR, eptype, exp1.value);
6866c6e8 4568 exp1.original_type = NULL;
ba47d38d 4569 cond.value = c_objc_common_truthvalue_conversion (cond_loc, exp1.value);
7d882b83 4570 c_inhibit_evaluation_warnings += cond.value == truthvalue_true_node;
27bf414c
JM
4571 }
4572 else
4573 {
4574 cond.value
85498824 4575 = c_objc_common_truthvalue_conversion
ba47d38d 4576 (cond_loc, default_conversion (cond.value));
7d882b83 4577 c_inhibit_evaluation_warnings += cond.value == truthvalue_false_node;
46bdb9cf 4578 exp1 = c_parser_expression_conv (parser);
7d882b83
ILT
4579 c_inhibit_evaluation_warnings +=
4580 ((cond.value == truthvalue_true_node)
4581 - (cond.value == truthvalue_false_node));
27bf414c 4582 }
744aa42f
ILT
4583
4584 colon_loc = c_parser_peek_token (parser)->location;
27bf414c
JM
4585 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
4586 {
7d882b83 4587 c_inhibit_evaluation_warnings -= cond.value == truthvalue_true_node;
27bf414c
JM
4588 ret.value = error_mark_node;
4589 ret.original_code = ERROR_MARK;
6866c6e8 4590 ret.original_type = NULL;
27bf414c
JM
4591 return ret;
4592 }
c2255bc4
AH
4593 {
4594 location_t exp2_loc = c_parser_peek_token (parser)->location;
4595 exp2 = c_parser_conditional_expression (parser, NULL);
4596 exp2 = default_function_array_conversion (exp2_loc, exp2);
4597 }
7d882b83 4598 c_inhibit_evaluation_warnings -= cond.value == truthvalue_true_node;
744aa42f 4599 ret.value = build_conditional_expr (colon_loc, cond.value,
928c19bb 4600 cond.original_code == C_MAYBE_CONST_EXPR,
d130ae11
ILT
4601 exp1.value, exp1.original_type,
4602 exp2.value, exp2.original_type);
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 %<:%>"))
0a7d7dea 6822 return error_mark_node;
27bf414c
JM
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. */
db3927fb 7342 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
953ff289 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
8493 switch (p_kind)
8494 {
8495 case PRAGMA_OMP_ATOMIC:
c2255bc4 8496 c_parser_omp_atomic (loc, parser);
953ff289
DN
8497 return;
8498 case PRAGMA_OMP_CRITICAL:
c2255bc4 8499 stmt = c_parser_omp_critical (loc, parser);
953ff289
DN
8500 break;
8501 case PRAGMA_OMP_FOR:
c2255bc4 8502 stmt = c_parser_omp_for (loc, parser);
953ff289
DN
8503 break;
8504 case PRAGMA_OMP_MASTER:
c2255bc4 8505 stmt = c_parser_omp_master (loc, parser);
953ff289
DN
8506 break;
8507 case PRAGMA_OMP_ORDERED:
c2255bc4 8508 stmt = c_parser_omp_ordered (loc, parser);
953ff289
DN
8509 break;
8510 case PRAGMA_OMP_PARALLEL:
c2255bc4 8511 stmt = c_parser_omp_parallel (loc, parser);
953ff289
DN
8512 break;
8513 case PRAGMA_OMP_SECTIONS:
c2255bc4 8514 stmt = c_parser_omp_sections (loc, parser);
953ff289
DN
8515 break;
8516 case PRAGMA_OMP_SINGLE:
c2255bc4 8517 stmt = c_parser_omp_single (loc, parser);
953ff289 8518 break;
a68ab351 8519 case PRAGMA_OMP_TASK:
c2255bc4 8520 stmt = c_parser_omp_task (loc, parser);
a68ab351 8521 break;
953ff289
DN
8522 default:
8523 gcc_unreachable ();
8524 }
8525
8526 if (stmt)
c2255bc4 8527 gcc_assert (EXPR_LOCATION (stmt) != UNKNOWN_LOCATION);
953ff289
DN
8528}
8529
8530
8531/* OpenMP 2.5:
8532 # pragma omp threadprivate (variable-list) */
8533
8534static void
8535c_parser_omp_threadprivate (c_parser *parser)
8536{
8537 tree vars, t;
c2255bc4 8538 location_t loc;
953ff289
DN
8539
8540 c_parser_consume_pragma (parser);
c2255bc4 8541 loc = c_parser_peek_token (parser)->location;
d75d71e0 8542 vars = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
953ff289 8543
953ff289
DN
8544 /* Mark every variable in VARS to be assigned thread local storage. */
8545 for (t = vars; t; t = TREE_CHAIN (t))
8546 {
8547 tree v = TREE_PURPOSE (t);
8548
c2255bc4
AH
8549 /* FIXME diagnostics: Ideally we should keep individual
8550 locations for all the variables in the var list to make the
8551 following errors more precise. Perhaps
8552 c_parser_omp_var_list_parens() should construct a list of
8553 locations to go along with the var list. */
8554
953ff289
DN
8555 /* If V had already been marked threadprivate, it doesn't matter
8556 whether it had been used prior to this point. */
5df27e4a 8557 if (TREE_CODE (v) != VAR_DECL)
c2255bc4 8558 error_at (loc, "%qD is not a variable", v);
5df27e4a 8559 else if (TREE_USED (v) && !C_DECL_THREADPRIVATE_P (v))
c2255bc4 8560 error_at (loc, "%qE declared %<threadprivate%> after first use", v);
953ff289 8561 else if (! TREE_STATIC (v) && ! DECL_EXTERNAL (v))
c2255bc4 8562 error_at (loc, "automatic variable %qE cannot be %<threadprivate%>", v);
5df27e4a
JJ
8563 else if (TREE_TYPE (v) == error_mark_node)
8564 ;
953ff289 8565 else if (! COMPLETE_TYPE_P (TREE_TYPE (v)))
c2255bc4 8566 error_at (loc, "%<threadprivate%> %qE has incomplete type", v);
953ff289
DN
8567 else
8568 {
8569 if (! DECL_THREAD_LOCAL_P (v))
8570 {
8571 DECL_TLS_MODEL (v) = decl_default_tls_model (v);
8572 /* If rtl has been already set for this var, call
8573 make_decl_rtl once again, so that encode_section_info
8574 has a chance to look at the new decl flags. */
8575 if (DECL_RTL_SET_P (v))
8576 make_decl_rtl (v);
8577 }
8578 C_DECL_THREADPRIVATE_P (v) = 1;
8579 }
8580 }
8581
8582 c_parser_skip_to_pragma_eol (parser);
8583}
8584
bc4071dd 8585\f
27bf414c
JM
8586/* Parse a single source file. */
8587
8588void
8589c_parse_file (void)
8590{
bc4071dd
RH
8591 /* Use local storage to begin. If the first token is a pragma, parse it.
8592 If it is #pragma GCC pch_preprocess, then this will load a PCH file
8593 which will cause garbage collection. */
8594 c_parser tparser;
8595
8596 memset (&tparser, 0, sizeof tparser);
8597 the_parser = &tparser;
8598
8599 if (c_parser_peek_token (&tparser)->pragma_kind == PRAGMA_GCC_PCH_PREPROCESS)
8600 c_parser_pragma_pch_preprocess (&tparser);
8601
8602 the_parser = GGC_NEW (c_parser);
8603 *the_parser = tparser;
8604
f9417da1
RG
8605 /* Initialize EH, if we've been told to do so. */
8606 if (flag_exceptions)
8607 {
8608 default_init_unwind_resume_libfunc ();
8609 using_eh_for_cleanups ();
8610 }
8611
27bf414c
JM
8612 c_parser_translation_unit (the_parser);
8613 the_parser = NULL;
8614}
8615
8616#include "gt-c-parser.h"
This page took 1.852233 seconds and 5 git commands to generate.