2006-08-07 Andrew John Hughes <gnu_andrew@member.fsf.org>
[official-gcc.git] / gcc / c-parser.c
blob8afe79c3703eadf5cf58e5633b9391c65227156a
1 /* Parser for C and Objective-C.
2 Copyright (C) 1987, 1988, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
5 Parser actions based on the old Bison parser; structure somewhat
6 influenced by and fragments based on the C++ parser.
8 This file is part of GCC.
10 GCC is free software; you can redistribute it and/or modify it under
11 the terms of the GNU General Public License as published by the Free
12 Software Foundation; either version 2, or (at your option) any later
13 version.
15 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
16 WARRANTY; without even the implied warranty of MERCHANTABILITY or
17 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 for more details.
20 You should have received a copy of the GNU General Public License
21 along with GCC; see the file COPYING. If not, write to the Free
22 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
23 02110-1301, USA. */
25 /* TODO:
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.
31 Add testcases covering every input symbol in every state in old and
32 new parsers.
34 Include full syntax for GNU C, including erroneous cases accepted
35 with error messages, in syntax productions in comments.
37 Make more diagnostics in the front end generally take an explicit
38 location rather than implicitly using input_location. */
40 #include "config.h"
41 #include "system.h"
42 #include "coretypes.h"
43 #include "tm.h"
44 #include "tree.h"
45 #include "rtl.h"
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"
57 #include "vec.h"
58 #include "target.h"
59 #include "cgraph.h"
62 /* Miscellaneous data and functions needed for the parser. */
64 int yydebug;
66 /* Objective-C specific parser/lexer information. */
68 static int objc_pq_context = 0;
70 /* The following flag is needed to contextualize Objective-C lexical
71 analysis. In some cases (e.g., 'int NSObject;'), it is undesirable
72 to bind an identifier to an Objective-C class, even if a class with
73 that name exists. */
74 static int objc_need_raw_identifier = 0;
75 #define OBJC_NEED_RAW_IDENTIFIER(VAL) \
76 do { \
77 if (c_dialect_objc ()) \
78 objc_need_raw_identifier = VAL; \
79 } while (0)
81 /* The reserved keyword table. */
82 struct resword
84 const char *word;
85 ENUM_BITFIELD(rid) rid : 16;
86 unsigned int disable : 16;
89 /* Disable mask. Keywords are disabled if (reswords[i].disable &
90 mask) is _true_. */
91 #define D_C89 0x01 /* not in C89 */
92 #define D_EXT 0x02 /* GCC extension */
93 #define D_EXT89 0x04 /* GCC extension incorporated in C99 */
94 #define D_OBJC 0x08 /* Objective C only */
96 static const struct resword reswords[] =
98 { "_Bool", RID_BOOL, 0 },
99 { "_Complex", RID_COMPLEX, 0 },
100 { "_Decimal32", RID_DFLOAT32, D_EXT },
101 { "_Decimal64", RID_DFLOAT64, D_EXT },
102 { "_Decimal128", RID_DFLOAT128, D_EXT },
103 { "__FUNCTION__", RID_FUNCTION_NAME, 0 },
104 { "__PRETTY_FUNCTION__", RID_PRETTY_FUNCTION_NAME, 0 },
105 { "__alignof", RID_ALIGNOF, 0 },
106 { "__alignof__", RID_ALIGNOF, 0 },
107 { "__asm", RID_ASM, 0 },
108 { "__asm__", RID_ASM, 0 },
109 { "__attribute", RID_ATTRIBUTE, 0 },
110 { "__attribute__", RID_ATTRIBUTE, 0 },
111 { "__builtin_choose_expr", RID_CHOOSE_EXPR, 0 },
112 { "__builtin_offsetof", RID_OFFSETOF, 0 },
113 { "__builtin_types_compatible_p", RID_TYPES_COMPATIBLE_P, 0 },
114 { "__builtin_va_arg", RID_VA_ARG, 0 },
115 { "__complex", RID_COMPLEX, 0 },
116 { "__complex__", RID_COMPLEX, 0 },
117 { "__const", RID_CONST, 0 },
118 { "__const__", RID_CONST, 0 },
119 { "__extension__", RID_EXTENSION, 0 },
120 { "__func__", RID_C99_FUNCTION_NAME, 0 },
121 { "__imag", RID_IMAGPART, 0 },
122 { "__imag__", RID_IMAGPART, 0 },
123 { "__inline", RID_INLINE, 0 },
124 { "__inline__", RID_INLINE, 0 },
125 { "__label__", RID_LABEL, 0 },
126 { "__real", RID_REALPART, 0 },
127 { "__real__", RID_REALPART, 0 },
128 { "__restrict", RID_RESTRICT, 0 },
129 { "__restrict__", RID_RESTRICT, 0 },
130 { "__signed", RID_SIGNED, 0 },
131 { "__signed__", RID_SIGNED, 0 },
132 { "__thread", RID_THREAD, 0 },
133 { "__typeof", RID_TYPEOF, 0 },
134 { "__typeof__", RID_TYPEOF, 0 },
135 { "__volatile", RID_VOLATILE, 0 },
136 { "__volatile__", RID_VOLATILE, 0 },
137 { "asm", RID_ASM, D_EXT },
138 { "auto", RID_AUTO, 0 },
139 { "break", RID_BREAK, 0 },
140 { "case", RID_CASE, 0 },
141 { "char", RID_CHAR, 0 },
142 { "const", RID_CONST, 0 },
143 { "continue", RID_CONTINUE, 0 },
144 { "default", RID_DEFAULT, 0 },
145 { "do", RID_DO, 0 },
146 { "double", RID_DOUBLE, 0 },
147 { "else", RID_ELSE, 0 },
148 { "enum", RID_ENUM, 0 },
149 { "extern", RID_EXTERN, 0 },
150 { "float", RID_FLOAT, 0 },
151 { "for", RID_FOR, 0 },
152 { "goto", RID_GOTO, 0 },
153 { "if", RID_IF, 0 },
154 { "inline", RID_INLINE, D_EXT89 },
155 { "int", RID_INT, 0 },
156 { "long", RID_LONG, 0 },
157 { "register", RID_REGISTER, 0 },
158 { "restrict", RID_RESTRICT, D_C89 },
159 { "return", RID_RETURN, 0 },
160 { "short", RID_SHORT, 0 },
161 { "signed", RID_SIGNED, 0 },
162 { "sizeof", RID_SIZEOF, 0 },
163 { "static", RID_STATIC, 0 },
164 { "struct", RID_STRUCT, 0 },
165 { "switch", RID_SWITCH, 0 },
166 { "typedef", RID_TYPEDEF, 0 },
167 { "typeof", RID_TYPEOF, D_EXT },
168 { "union", RID_UNION, 0 },
169 { "unsigned", RID_UNSIGNED, 0 },
170 { "void", RID_VOID, 0 },
171 { "volatile", RID_VOLATILE, 0 },
172 { "while", RID_WHILE, 0 },
173 /* These Objective-C keywords are recognized only immediately after
174 an '@'. */
175 { "class", RID_AT_CLASS, D_OBJC },
176 { "compatibility_alias", RID_AT_ALIAS, D_OBJC },
177 { "defs", RID_AT_DEFS, D_OBJC },
178 { "encode", RID_AT_ENCODE, D_OBJC },
179 { "end", RID_AT_END, D_OBJC },
180 { "implementation", RID_AT_IMPLEMENTATION, D_OBJC },
181 { "interface", RID_AT_INTERFACE, D_OBJC },
182 { "private", RID_AT_PRIVATE, D_OBJC },
183 { "protected", RID_AT_PROTECTED, D_OBJC },
184 { "protocol", RID_AT_PROTOCOL, D_OBJC },
185 { "public", RID_AT_PUBLIC, D_OBJC },
186 { "selector", RID_AT_SELECTOR, D_OBJC },
187 { "throw", RID_AT_THROW, D_OBJC },
188 { "try", RID_AT_TRY, D_OBJC },
189 { "catch", RID_AT_CATCH, D_OBJC },
190 { "finally", RID_AT_FINALLY, D_OBJC },
191 { "synchronized", RID_AT_SYNCHRONIZED, D_OBJC },
192 /* These are recognized only in protocol-qualifier context
193 (see above) */
194 { "bycopy", RID_BYCOPY, D_OBJC },
195 { "byref", RID_BYREF, D_OBJC },
196 { "in", RID_IN, D_OBJC },
197 { "inout", RID_INOUT, D_OBJC },
198 { "oneway", RID_ONEWAY, D_OBJC },
199 { "out", RID_OUT, D_OBJC },
201 #define N_reswords (sizeof reswords / sizeof (struct resword))
203 /* All OpenMP clauses. OpenMP 2.5. */
204 typedef enum pragma_omp_clause {
205 PRAGMA_OMP_CLAUSE_NONE = 0,
207 PRAGMA_OMP_CLAUSE_COPYIN,
208 PRAGMA_OMP_CLAUSE_COPYPRIVATE,
209 PRAGMA_OMP_CLAUSE_DEFAULT,
210 PRAGMA_OMP_CLAUSE_FIRSTPRIVATE,
211 PRAGMA_OMP_CLAUSE_IF,
212 PRAGMA_OMP_CLAUSE_LASTPRIVATE,
213 PRAGMA_OMP_CLAUSE_NOWAIT,
214 PRAGMA_OMP_CLAUSE_NUM_THREADS,
215 PRAGMA_OMP_CLAUSE_ORDERED,
216 PRAGMA_OMP_CLAUSE_PRIVATE,
217 PRAGMA_OMP_CLAUSE_REDUCTION,
218 PRAGMA_OMP_CLAUSE_SCHEDULE,
219 PRAGMA_OMP_CLAUSE_SHARED
220 } pragma_omp_clause;
223 /* Initialization routine for this file. */
225 void
226 c_parse_init (void)
228 /* The only initialization required is of the reserved word
229 identifiers. */
230 unsigned int i;
231 tree id;
232 int mask = (flag_isoc99 ? 0 : D_C89)
233 | (flag_no_asm ? (flag_isoc99 ? D_EXT : D_EXT|D_EXT89) : 0);
235 if (!c_dialect_objc ())
236 mask |= D_OBJC;
238 ridpointers = GGC_CNEWVEC (tree, (int) RID_MAX);
239 for (i = 0; i < N_reswords; i++)
241 /* If a keyword is disabled, do not enter it into the table
242 and so create a canonical spelling that isn't a keyword. */
243 if (reswords[i].disable & mask)
244 continue;
246 id = get_identifier (reswords[i].word);
247 C_RID_CODE (id) = reswords[i].rid;
248 C_IS_RESERVED_WORD (id) = 1;
249 ridpointers [(int) reswords[i].rid] = id;
253 /* The C lexer intermediates between the lexer in cpplib and c-lex.c
254 and the C parser. Unlike the C++ lexer, the parser structure
255 stores the lexer information instead of using a separate structure.
256 Identifiers are separated into ordinary identifiers, type names,
257 keywords and some other Objective-C types of identifiers, and some
258 look-ahead is maintained.
260 ??? It might be a good idea to lex the whole file up front (as for
261 C++). It would then be possible to share more of the C and C++
262 lexer code, if desired. */
264 /* The following local token type is used. */
266 /* A keyword. */
267 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
269 /* More information about the type of a CPP_NAME token. */
270 typedef enum c_id_kind {
271 /* An ordinary identifier. */
272 C_ID_ID,
273 /* An identifier declared as a typedef name. */
274 C_ID_TYPENAME,
275 /* An identifier declared as an Objective-C class name. */
276 C_ID_CLASSNAME,
277 /* Not an identifier. */
278 C_ID_NONE
279 } c_id_kind;
281 /* A single C token after string literal concatenation and conversion
282 of preprocessing tokens to tokens. */
283 typedef struct c_token GTY (())
285 /* The kind of token. */
286 ENUM_BITFIELD (cpp_ttype) type : 8;
287 /* If this token is a CPP_NAME, this value indicates whether also
288 declared as some kind of type. Otherwise, it is C_ID_NONE. */
289 ENUM_BITFIELD (c_id_kind) id_kind : 8;
290 /* If this token is a keyword, this value indicates which keyword.
291 Otherwise, this value is RID_MAX. */
292 ENUM_BITFIELD (rid) keyword : 8;
293 /* If this token is a CPP_PRAGMA, this indicates the pragma that
294 was seen. Otherwise it is PRAGMA_NONE. */
295 ENUM_BITFIELD (pragma_kind) pragma_kind : 7;
296 /* True if this token is from a system header. */
297 BOOL_BITFIELD in_system_header : 1;
298 /* The value associated with this token, if any. */
299 tree value;
300 /* The location at which this token was found. */
301 location_t location;
302 } c_token;
304 /* A parser structure recording information about the state and
305 context of parsing. Includes lexer information with up to two
306 tokens of look-ahead; more are not needed for C. */
307 typedef struct c_parser GTY(())
309 /* The look-ahead tokens. */
310 c_token tokens[2];
311 /* How many look-ahead tokens are available (0, 1 or 2). */
312 short tokens_avail;
313 /* True if a syntax error is being recovered from; false otherwise.
314 c_parser_error sets this flag. It should clear this flag when
315 enough tokens have been consumed to recover from the error. */
316 BOOL_BITFIELD error : 1;
317 /* True if we're processing a pragma, and shouldn't automatically
318 consume CPP_PRAGMA_EOL. */
319 BOOL_BITFIELD in_pragma : 1;
320 } c_parser;
323 /* The actual parser and external interface. ??? Does this need to be
324 garbage-collected? */
326 static GTY (()) c_parser *the_parser;
329 /* Read in and lex a single token, storing it in *TOKEN. */
331 static void
332 c_lex_one_token (c_token *token)
334 timevar_push (TV_LEX);
336 token->type = c_lex_with_flags (&token->value, &token->location, NULL);
337 token->id_kind = C_ID_NONE;
338 token->keyword = RID_MAX;
339 token->pragma_kind = PRAGMA_NONE;
340 token->in_system_header = in_system_header;
342 switch (token->type)
344 case CPP_NAME:
346 tree decl;
348 int objc_force_identifier = objc_need_raw_identifier;
349 OBJC_NEED_RAW_IDENTIFIER (0);
351 if (C_IS_RESERVED_WORD (token->value))
353 enum rid rid_code = C_RID_CODE (token->value);
355 if (c_dialect_objc ())
357 if (!OBJC_IS_AT_KEYWORD (rid_code)
358 && (!OBJC_IS_PQ_KEYWORD (rid_code) || objc_pq_context))
360 /* Return the canonical spelling for this keyword. */
361 token->value = ridpointers[(int) rid_code];
362 token->type = CPP_KEYWORD;
363 token->keyword = rid_code;
364 break;
367 else
369 /* Return the canonical spelling for this keyword. */
370 token->value = ridpointers[(int) rid_code];
371 token->type = CPP_KEYWORD;
372 token->keyword = rid_code;
373 break;
377 decl = lookup_name (token->value);
378 if (decl)
380 if (TREE_CODE (decl) == TYPE_DECL)
382 token->id_kind = C_ID_TYPENAME;
383 break;
386 else if (c_dialect_objc ())
388 tree objc_interface_decl = objc_is_class_name (token->value);
389 /* Objective-C class names are in the same namespace as
390 variables and typedefs, and hence are shadowed by local
391 declarations. */
392 if (objc_interface_decl
393 && (global_bindings_p ()
394 || (!objc_force_identifier && !decl)))
396 token->value = objc_interface_decl;
397 token->id_kind = C_ID_CLASSNAME;
398 break;
401 token->id_kind = C_ID_ID;
403 break;
404 case CPP_AT_NAME:
405 /* This only happens in Objective-C; it must be a keyword. */
406 token->type = CPP_KEYWORD;
407 token->keyword = C_RID_CODE (token->value);
408 break;
409 case CPP_COLON:
410 case CPP_COMMA:
411 case CPP_CLOSE_PAREN:
412 case CPP_SEMICOLON:
413 /* These tokens may affect the interpretation of any identifiers
414 following, if doing Objective-C. */
415 OBJC_NEED_RAW_IDENTIFIER (0);
416 break;
417 case CPP_PRAGMA:
418 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
419 token->pragma_kind = TREE_INT_CST_LOW (token->value);
420 token->value = NULL;
421 break;
422 default:
423 break;
425 timevar_pop (TV_LEX);
428 /* Return a pointer to the next token from PARSER, reading it in if
429 necessary. */
431 static inline c_token *
432 c_parser_peek_token (c_parser *parser)
434 if (parser->tokens_avail == 0)
436 c_lex_one_token (&parser->tokens[0]);
437 parser->tokens_avail = 1;
439 return &parser->tokens[0];
442 /* Return true if the next token from PARSER has the indicated
443 TYPE. */
445 static inline bool
446 c_parser_next_token_is (c_parser *parser, enum cpp_ttype type)
448 return c_parser_peek_token (parser)->type == type;
451 /* Return true if the next token from PARSER does not have the
452 indicated TYPE. */
454 static inline bool
455 c_parser_next_token_is_not (c_parser *parser, enum cpp_ttype type)
457 return !c_parser_next_token_is (parser, type);
460 /* Return true if the next token from PARSER is the indicated
461 KEYWORD. */
463 static inline bool
464 c_parser_next_token_is_keyword (c_parser *parser, enum rid keyword)
466 c_token *token;
468 /* Peek at the next token. */
469 token = c_parser_peek_token (parser);
470 /* Check to see if it is the indicated keyword. */
471 return token->keyword == keyword;
474 /* Return true if TOKEN can start a type name,
475 false otherwise. */
476 static bool
477 c_token_starts_typename (c_token *token)
479 switch (token->type)
481 case CPP_NAME:
482 switch (token->id_kind)
484 case C_ID_ID:
485 return false;
486 case C_ID_TYPENAME:
487 return true;
488 case C_ID_CLASSNAME:
489 gcc_assert (c_dialect_objc ());
490 return true;
491 default:
492 gcc_unreachable ();
494 case CPP_KEYWORD:
495 switch (token->keyword)
497 case RID_UNSIGNED:
498 case RID_LONG:
499 case RID_SHORT:
500 case RID_SIGNED:
501 case RID_COMPLEX:
502 case RID_INT:
503 case RID_CHAR:
504 case RID_FLOAT:
505 case RID_DOUBLE:
506 case RID_VOID:
507 case RID_DFLOAT32:
508 case RID_DFLOAT64:
509 case RID_DFLOAT128:
510 case RID_BOOL:
511 case RID_ENUM:
512 case RID_STRUCT:
513 case RID_UNION:
514 case RID_TYPEOF:
515 case RID_CONST:
516 case RID_VOLATILE:
517 case RID_RESTRICT:
518 case RID_ATTRIBUTE:
519 return true;
520 default:
521 return false;
523 case CPP_LESS:
524 if (c_dialect_objc ())
525 return true;
526 return false;
527 default:
528 return false;
532 /* Return true if the next token from PARSER can start a type name,
533 false otherwise. */
534 static inline bool
535 c_parser_next_token_starts_typename (c_parser *parser)
537 c_token *token = c_parser_peek_token (parser);
538 return c_token_starts_typename (token);
541 /* Return true if TOKEN can start declaration specifiers, false
542 otherwise. */
543 static bool
544 c_token_starts_declspecs (c_token *token)
546 switch (token->type)
548 case CPP_NAME:
549 switch (token->id_kind)
551 case C_ID_ID:
552 return false;
553 case C_ID_TYPENAME:
554 return true;
555 case C_ID_CLASSNAME:
556 gcc_assert (c_dialect_objc ());
557 return true;
558 default:
559 gcc_unreachable ();
561 case CPP_KEYWORD:
562 switch (token->keyword)
564 case RID_STATIC:
565 case RID_EXTERN:
566 case RID_REGISTER:
567 case RID_TYPEDEF:
568 case RID_INLINE:
569 case RID_AUTO:
570 case RID_THREAD:
571 case RID_UNSIGNED:
572 case RID_LONG:
573 case RID_SHORT:
574 case RID_SIGNED:
575 case RID_COMPLEX:
576 case RID_INT:
577 case RID_CHAR:
578 case RID_FLOAT:
579 case RID_DOUBLE:
580 case RID_VOID:
581 case RID_DFLOAT32:
582 case RID_DFLOAT64:
583 case RID_DFLOAT128:
584 case RID_BOOL:
585 case RID_ENUM:
586 case RID_STRUCT:
587 case RID_UNION:
588 case RID_TYPEOF:
589 case RID_CONST:
590 case RID_VOLATILE:
591 case RID_RESTRICT:
592 case RID_ATTRIBUTE:
593 return true;
594 default:
595 return false;
597 case CPP_LESS:
598 if (c_dialect_objc ())
599 return true;
600 return false;
601 default:
602 return false;
606 /* Return true if the next token from PARSER can start declaration
607 specifiers, false otherwise. */
608 static inline bool
609 c_parser_next_token_starts_declspecs (c_parser *parser)
611 c_token *token = c_parser_peek_token (parser);
612 return c_token_starts_declspecs (token);
615 /* Return a pointer to the next-but-one token from PARSER, reading it
616 in if necessary. The next token is already read in. */
618 static c_token *
619 c_parser_peek_2nd_token (c_parser *parser)
621 if (parser->tokens_avail >= 2)
622 return &parser->tokens[1];
623 gcc_assert (parser->tokens_avail == 1);
624 gcc_assert (parser->tokens[0].type != CPP_EOF);
625 gcc_assert (parser->tokens[0].type != CPP_PRAGMA_EOL);
626 c_lex_one_token (&parser->tokens[1]);
627 parser->tokens_avail = 2;
628 return &parser->tokens[1];
631 /* Consume the next token from PARSER. */
633 static void
634 c_parser_consume_token (c_parser *parser)
636 gcc_assert (parser->tokens_avail >= 1);
637 gcc_assert (parser->tokens[0].type != CPP_EOF);
638 gcc_assert (!parser->in_pragma || parser->tokens[0].type != CPP_PRAGMA_EOL);
639 gcc_assert (parser->error || parser->tokens[0].type != CPP_PRAGMA);
640 if (parser->tokens_avail == 2)
641 parser->tokens[0] = parser->tokens[1];
642 parser->tokens_avail--;
645 /* Expect the current token to be a #pragma. Consume it and remember
646 that we've begun parsing a pragma. */
648 static void
649 c_parser_consume_pragma (c_parser *parser)
651 gcc_assert (!parser->in_pragma);
652 gcc_assert (parser->tokens_avail >= 1);
653 gcc_assert (parser->tokens[0].type == CPP_PRAGMA);
654 if (parser->tokens_avail == 2)
655 parser->tokens[0] = parser->tokens[1];
656 parser->tokens_avail--;
657 parser->in_pragma = true;
660 /* Update the globals input_location and in_system_header from
661 TOKEN. */
662 static inline void
663 c_parser_set_source_position_from_token (c_token *token)
665 if (token->type != CPP_EOF)
667 input_location = token->location;
668 in_system_header = token->in_system_header;
672 /* Issue a diagnostic of the form
673 FILE:LINE: MESSAGE before TOKEN
674 where TOKEN is the next token in the input stream of PARSER.
675 MESSAGE (specified by the caller) is usually of the form "expected
676 OTHER-TOKEN".
678 Do not issue a diagnostic if still recovering from an error.
680 ??? This is taken from the C++ parser, but building up messages in
681 this way is not i18n-friendly and some other approach should be
682 used. */
684 static void
685 c_parser_error (c_parser *parser, const char *gmsgid)
687 c_token *token = c_parser_peek_token (parser);
688 if (parser->error)
689 return;
690 parser->error = true;
691 if (!gmsgid)
692 return;
693 /* This diagnostic makes more sense if it is tagged to the line of
694 the token we just peeked at. */
695 c_parser_set_source_position_from_token (token);
696 c_parse_error (gmsgid,
697 /* Because c_parse_error does not understand
698 CPP_KEYWORD, keywords are treated like
699 identifiers. */
700 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
701 token->value);
704 /* If the next token is of the indicated TYPE, consume it. Otherwise,
705 issue the error MSGID. If MSGID is NULL then a message has already
706 been produced and no message will be produced this time. Returns
707 true if found, false otherwise. */
709 static bool
710 c_parser_require (c_parser *parser,
711 enum cpp_ttype type,
712 const char *msgid)
714 if (c_parser_next_token_is (parser, type))
716 c_parser_consume_token (parser);
717 return true;
719 else
721 c_parser_error (parser, msgid);
722 return false;
726 /* If the next token is the indicated keyword, consume it. Otherwise,
727 issue the error MSGID. Returns true if found, false otherwise. */
729 static bool
730 c_parser_require_keyword (c_parser *parser,
731 enum rid keyword,
732 const char *msgid)
734 if (c_parser_next_token_is_keyword (parser, keyword))
736 c_parser_consume_token (parser);
737 return true;
739 else
741 c_parser_error (parser, msgid);
742 return false;
746 /* Like c_parser_require, except that tokens will be skipped until the
747 desired token is found. An error message is still produced if the
748 next token is not as expected. If MSGID is NULL then a message has
749 already been produced and no message will be produced this
750 time. */
752 static void
753 c_parser_skip_until_found (c_parser *parser,
754 enum cpp_ttype type,
755 const char *msgid)
757 unsigned nesting_depth = 0;
759 if (c_parser_require (parser, type, msgid))
760 return;
762 /* Skip tokens until the desired token is found. */
763 while (true)
765 /* Peek at the next token. */
766 c_token *token = c_parser_peek_token (parser);
767 /* If we've reached the token we want, consume it and stop. */
768 if (token->type == type && !nesting_depth)
770 c_parser_consume_token (parser);
771 break;
774 /* If we've run out of tokens, stop. */
775 if (token->type == CPP_EOF)
776 return;
777 if (token->type == CPP_PRAGMA_EOL && parser->in_pragma)
778 return;
779 if (token->type == CPP_OPEN_BRACE
780 || token->type == CPP_OPEN_PAREN
781 || token->type == CPP_OPEN_SQUARE)
782 ++nesting_depth;
783 else if (token->type == CPP_CLOSE_BRACE
784 || token->type == CPP_CLOSE_PAREN
785 || token->type == CPP_CLOSE_SQUARE)
787 if (nesting_depth-- == 0)
788 break;
790 /* Consume this token. */
791 c_parser_consume_token (parser);
793 parser->error = false;
796 /* Skip tokens until the end of a parameter is found, but do not
797 consume the comma, semicolon or closing delimiter. */
799 static void
800 c_parser_skip_to_end_of_parameter (c_parser *parser)
802 unsigned nesting_depth = 0;
804 while (true)
806 c_token *token = c_parser_peek_token (parser);
807 if ((token->type == CPP_COMMA || token->type == CPP_SEMICOLON)
808 && !nesting_depth)
809 break;
810 /* If we've run out of tokens, stop. */
811 if (token->type == CPP_EOF)
812 return;
813 if (token->type == CPP_PRAGMA_EOL && parser->in_pragma)
814 return;
815 if (token->type == CPP_OPEN_BRACE
816 || token->type == CPP_OPEN_PAREN
817 || token->type == CPP_OPEN_SQUARE)
818 ++nesting_depth;
819 else if (token->type == CPP_CLOSE_BRACE
820 || token->type == CPP_CLOSE_PAREN
821 || token->type == CPP_CLOSE_SQUARE)
823 if (nesting_depth-- == 0)
824 break;
826 /* Consume this token. */
827 c_parser_consume_token (parser);
829 parser->error = false;
832 /* Expect to be at the end of the pragma directive and consume an
833 end of line marker. */
835 static void
836 c_parser_skip_to_pragma_eol (c_parser *parser)
838 gcc_assert (parser->in_pragma);
839 parser->in_pragma = false;
841 if (!c_parser_require (parser, CPP_PRAGMA_EOL, "expected end of line"))
842 while (true)
844 c_token *token = c_parser_peek_token (parser);
845 if (token->type == CPP_EOF)
846 break;
847 if (token->type == CPP_PRAGMA_EOL)
849 c_parser_consume_token (parser);
850 break;
852 c_parser_consume_token (parser);
855 parser->error = false;
858 /* Skip tokens until we have consumed an entire block, or until we
859 have consumed a non-nested ';'. */
861 static void
862 c_parser_skip_to_end_of_block_or_statement (c_parser *parser)
864 unsigned nesting_depth = 0;
865 bool save_error = parser->error;
867 while (true)
869 c_token *token;
871 /* Peek at the next token. */
872 token = c_parser_peek_token (parser);
874 switch (token->type)
876 case CPP_EOF:
877 return;
879 case CPP_PRAGMA_EOL:
880 if (parser->in_pragma)
881 return;
882 break;
884 case CPP_SEMICOLON:
885 /* If the next token is a ';', we have reached the
886 end of the statement. */
887 if (!nesting_depth)
889 /* Consume the ';'. */
890 c_parser_consume_token (parser);
891 goto finished;
893 break;
895 case CPP_CLOSE_BRACE:
896 /* If the next token is a non-nested '}', then we have
897 reached the end of the current block. */
898 if (nesting_depth == 0 || --nesting_depth == 0)
900 c_parser_consume_token (parser);
901 goto finished;
903 break;
905 case CPP_OPEN_BRACE:
906 /* If it the next token is a '{', then we are entering a new
907 block. Consume the entire block. */
908 ++nesting_depth;
909 break;
911 case CPP_PRAGMA:
912 /* If we see a pragma, consume the whole thing at once. We
913 have some safeguards against consuming pragmas willy-nilly.
914 Normally, we'd expect to be here with parser->error set,
915 which disables these safeguards. But it's possible to get
916 here for secondary error recovery, after parser->error has
917 been cleared. */
918 c_parser_consume_pragma (parser);
919 c_parser_skip_to_pragma_eol (parser);
920 parser->error = save_error;
921 continue;
924 c_parser_consume_token (parser);
927 finished:
928 parser->error = false;
931 /* Save the warning flags which are controlled by __extension__. */
933 static inline int
934 disable_extension_diagnostics (void)
936 int ret = (pedantic
937 | (warn_pointer_arith << 1)
938 | (warn_traditional << 2)
939 | (flag_iso << 3));
940 pedantic = 0;
941 warn_pointer_arith = 0;
942 warn_traditional = 0;
943 flag_iso = 0;
944 return ret;
947 /* Restore the warning flags which are controlled by __extension__.
948 FLAGS is the return value from disable_extension_diagnostics. */
950 static inline void
951 restore_extension_diagnostics (int flags)
953 pedantic = flags & 1;
954 warn_pointer_arith = (flags >> 1) & 1;
955 warn_traditional = (flags >> 2) & 1;
956 flag_iso = (flags >> 3) & 1;
959 /* Possibly kinds of declarator to parse. */
960 typedef enum c_dtr_syn {
961 /* A normal declarator with an identifier. */
962 C_DTR_NORMAL,
963 /* An abstract declarator (maybe empty). */
964 C_DTR_ABSTRACT,
965 /* A parameter declarator: may be either, but after a type name does
966 not redeclare a typedef name as an identifier if it can
967 alternatively be interpreted as a typedef name; see DR#009,
968 applied in C90 TC1, omitted from C99 and reapplied in C99 TC2
969 following DR#249. For example, given a typedef T, "int T" and
970 "int *T" are valid parameter declarations redeclaring T, while
971 "int (T)" and "int * (T)" and "int (T[])" and "int (T (int))" are
972 abstract declarators rather than involving redundant parentheses;
973 the same applies with attributes inside the parentheses before
974 "T". */
975 C_DTR_PARM
976 } c_dtr_syn;
978 static void c_parser_external_declaration (c_parser *);
979 static void c_parser_asm_definition (c_parser *);
980 static void c_parser_declaration_or_fndef (c_parser *, bool, bool, bool, bool);
981 static void c_parser_declspecs (c_parser *, struct c_declspecs *, bool, bool,
982 bool);
983 static struct c_typespec c_parser_enum_specifier (c_parser *);
984 static struct c_typespec c_parser_struct_or_union_specifier (c_parser *);
985 static tree c_parser_struct_declaration (c_parser *);
986 static struct c_typespec c_parser_typeof_specifier (c_parser *);
987 static struct c_declarator *c_parser_declarator (c_parser *, bool, c_dtr_syn,
988 bool *);
989 static struct c_declarator *c_parser_direct_declarator (c_parser *, bool,
990 c_dtr_syn, bool *);
991 static struct c_declarator *c_parser_direct_declarator_inner (c_parser *,
992 bool,
993 struct c_declarator *);
994 static struct c_arg_info *c_parser_parms_declarator (c_parser *, bool, tree);
995 static struct c_arg_info *c_parser_parms_list_declarator (c_parser *, tree);
996 static struct c_parm *c_parser_parameter_declaration (c_parser *, tree);
997 static tree c_parser_simple_asm_expr (c_parser *);
998 static tree c_parser_attributes (c_parser *);
999 static struct c_type_name *c_parser_type_name (c_parser *);
1000 static struct c_expr c_parser_initializer (c_parser *);
1001 static struct c_expr c_parser_braced_init (c_parser *, tree, bool);
1002 static void c_parser_initelt (c_parser *);
1003 static void c_parser_initval (c_parser *, struct c_expr *);
1004 static tree c_parser_compound_statement (c_parser *);
1005 static void c_parser_compound_statement_nostart (c_parser *);
1006 static void c_parser_label (c_parser *);
1007 static void c_parser_statement (c_parser *);
1008 static void c_parser_statement_after_labels (c_parser *);
1009 static void c_parser_if_statement (c_parser *);
1010 static void c_parser_switch_statement (c_parser *);
1011 static void c_parser_while_statement (c_parser *);
1012 static void c_parser_do_statement (c_parser *);
1013 static void c_parser_for_statement (c_parser *);
1014 static tree c_parser_asm_statement (c_parser *);
1015 static tree c_parser_asm_operands (c_parser *, bool);
1016 static tree c_parser_asm_clobbers (c_parser *);
1017 static struct c_expr c_parser_expr_no_commas (c_parser *, struct c_expr *);
1018 static struct c_expr c_parser_conditional_expression (c_parser *,
1019 struct c_expr *);
1020 static struct c_expr c_parser_binary_expression (c_parser *, struct c_expr *);
1021 static struct c_expr c_parser_cast_expression (c_parser *, struct c_expr *);
1022 static struct c_expr c_parser_unary_expression (c_parser *);
1023 static struct c_expr c_parser_sizeof_expression (c_parser *);
1024 static struct c_expr c_parser_alignof_expression (c_parser *);
1025 static struct c_expr c_parser_postfix_expression (c_parser *);
1026 static struct c_expr c_parser_postfix_expression_after_paren_type (c_parser *,
1027 struct c_type_name *);
1028 static struct c_expr c_parser_postfix_expression_after_primary (c_parser *,
1029 struct c_expr);
1030 static struct c_expr c_parser_expression (c_parser *);
1031 static struct c_expr c_parser_expression_conv (c_parser *);
1032 static tree c_parser_expr_list (c_parser *, bool);
1033 static void c_parser_omp_construct (c_parser *);
1034 static void c_parser_omp_threadprivate (c_parser *);
1035 static void c_parser_omp_barrier (c_parser *);
1036 static void c_parser_omp_flush (c_parser *);
1038 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
1039 static bool c_parser_pragma (c_parser *, enum pragma_context);
1041 /* These Objective-C parser functions are only ever called when
1042 compiling Objective-C. */
1043 static void c_parser_objc_class_definition (c_parser *);
1044 static void c_parser_objc_class_instance_variables (c_parser *);
1045 static void c_parser_objc_class_declaration (c_parser *);
1046 static void c_parser_objc_alias_declaration (c_parser *);
1047 static void c_parser_objc_protocol_definition (c_parser *);
1048 static enum tree_code c_parser_objc_method_type (c_parser *);
1049 static void c_parser_objc_method_definition (c_parser *);
1050 static void c_parser_objc_methodprotolist (c_parser *);
1051 static void c_parser_objc_methodproto (c_parser *);
1052 static tree c_parser_objc_method_decl (c_parser *);
1053 static tree c_parser_objc_type_name (c_parser *);
1054 static tree c_parser_objc_protocol_refs (c_parser *);
1055 static void c_parser_objc_try_catch_statement (c_parser *);
1056 static void c_parser_objc_synchronized_statement (c_parser *);
1057 static tree c_parser_objc_selector (c_parser *);
1058 static tree c_parser_objc_selector_arg (c_parser *);
1059 static tree c_parser_objc_receiver (c_parser *);
1060 static tree c_parser_objc_message_args (c_parser *);
1061 static tree c_parser_objc_keywordexpr (c_parser *);
1063 /* Parse a translation unit (C90 6.7, C99 6.9).
1065 translation-unit:
1066 external-declarations
1068 external-declarations:
1069 external-declaration
1070 external-declarations external-declaration
1072 GNU extensions:
1074 translation-unit:
1075 empty
1078 static void
1079 c_parser_translation_unit (c_parser *parser)
1081 if (c_parser_next_token_is (parser, CPP_EOF))
1083 if (pedantic)
1084 pedwarn ("ISO C forbids an empty source file");
1086 else
1088 void *obstack_position = obstack_alloc (&parser_obstack, 0);
1091 ggc_collect ();
1092 c_parser_external_declaration (parser);
1093 obstack_free (&parser_obstack, obstack_position);
1095 while (c_parser_next_token_is_not (parser, CPP_EOF));
1099 /* Parse an external declaration (C90 6.7, C99 6.9).
1101 external-declaration:
1102 function-definition
1103 declaration
1105 GNU extensions:
1107 external-declaration:
1108 asm-definition
1110 __extension__ external-declaration
1112 Objective-C:
1114 external-declaration:
1115 objc-class-definition
1116 objc-class-declaration
1117 objc-alias-declaration
1118 objc-protocol-definition
1119 objc-method-definition
1120 @end
1123 static void
1124 c_parser_external_declaration (c_parser *parser)
1126 int ext;
1127 switch (c_parser_peek_token (parser)->type)
1129 case CPP_KEYWORD:
1130 switch (c_parser_peek_token (parser)->keyword)
1132 case RID_EXTENSION:
1133 ext = disable_extension_diagnostics ();
1134 c_parser_consume_token (parser);
1135 c_parser_external_declaration (parser);
1136 restore_extension_diagnostics (ext);
1137 break;
1138 case RID_ASM:
1139 c_parser_asm_definition (parser);
1140 break;
1141 case RID_AT_INTERFACE:
1142 case RID_AT_IMPLEMENTATION:
1143 gcc_assert (c_dialect_objc ());
1144 c_parser_objc_class_definition (parser);
1145 break;
1146 case RID_AT_CLASS:
1147 gcc_assert (c_dialect_objc ());
1148 c_parser_objc_class_declaration (parser);
1149 break;
1150 case RID_AT_ALIAS:
1151 gcc_assert (c_dialect_objc ());
1152 c_parser_objc_alias_declaration (parser);
1153 break;
1154 case RID_AT_PROTOCOL:
1155 gcc_assert (c_dialect_objc ());
1156 c_parser_objc_protocol_definition (parser);
1157 break;
1158 case RID_AT_END:
1159 gcc_assert (c_dialect_objc ());
1160 c_parser_consume_token (parser);
1161 objc_finish_implementation ();
1162 break;
1163 default:
1164 goto decl_or_fndef;
1166 break;
1167 case CPP_SEMICOLON:
1168 if (pedantic)
1169 pedwarn ("ISO C does not allow extra %<;%> outside of a function");
1170 c_parser_consume_token (parser);
1171 break;
1172 case CPP_PRAGMA:
1173 c_parser_pragma (parser, pragma_external);
1174 break;
1175 case CPP_PLUS:
1176 case CPP_MINUS:
1177 if (c_dialect_objc ())
1179 c_parser_objc_method_definition (parser);
1180 break;
1182 /* Else fall through, and yield a syntax error trying to parse
1183 as a declaration or function definition. */
1184 default:
1185 decl_or_fndef:
1186 /* A declaration or a function definition. We can only tell
1187 which after parsing the declaration specifiers, if any, and
1188 the first declarator. */
1189 c_parser_declaration_or_fndef (parser, true, true, false, true);
1190 break;
1195 /* Parse a declaration or function definition (C90 6.5, 6.7.1, C99
1196 6.7, 6.9.1). If FNDEF_OK is true, a function definition is
1197 accepted; otherwise (old-style parameter declarations) only other
1198 declarations are accepted. If NESTED is true, we are inside a
1199 function or parsing old-style parameter declarations; any functions
1200 encountered are nested functions and declaration specifiers are
1201 required; otherwise we are at top level and functions are normal
1202 functions and declaration specifiers may be optional. If EMPTY_OK
1203 is true, empty declarations are OK (subject to all other
1204 constraints); otherwise (old-style parameter declarations) they are
1205 diagnosed. If START_ATTR_OK is true, the declaration specifiers
1206 may start with attributes; otherwise they may not.
1208 declaration:
1209 declaration-specifiers init-declarator-list[opt] ;
1211 function-definition:
1212 declaration-specifiers[opt] declarator declaration-list[opt]
1213 compound-statement
1215 declaration-list:
1216 declaration
1217 declaration-list declaration
1219 init-declarator-list:
1220 init-declarator
1221 init-declarator-list , init-declarator
1223 init-declarator:
1224 declarator simple-asm-expr[opt] attributes[opt]
1225 declarator simple-asm-expr[opt] attributes[opt] = initializer
1227 GNU extensions:
1229 nested-function-definition:
1230 declaration-specifiers declarator declaration-list[opt]
1231 compound-statement
1233 The simple-asm-expr and attributes are GNU extensions.
1235 This function does not handle __extension__; that is handled in its
1236 callers. ??? Following the old parser, __extension__ may start
1237 external declarations, declarations in functions and declarations
1238 at the start of "for" loops, but not old-style parameter
1239 declarations.
1241 C99 requires declaration specifiers in a function definition; the
1242 absence is diagnosed through the diagnosis of implicit int. In GNU
1243 C we also allow but diagnose declarations without declaration
1244 specifiers, but only at top level (elsewhere they conflict with
1245 other syntax).
1247 OpenMP:
1249 declaration:
1250 threadprivate-directive */
1252 static void
1253 c_parser_declaration_or_fndef (c_parser *parser, bool fndef_ok, bool empty_ok,
1254 bool nested, bool start_attr_ok)
1256 struct c_declspecs *specs;
1257 tree prefix_attrs;
1258 tree all_prefix_attrs;
1259 bool diagnosed_no_specs = false;
1261 specs = build_null_declspecs ();
1262 c_parser_declspecs (parser, specs, true, true, start_attr_ok);
1263 if (parser->error)
1265 c_parser_skip_to_end_of_block_or_statement (parser);
1266 return;
1268 if (nested && !specs->declspecs_seen_p)
1270 c_parser_error (parser, "expected declaration specifiers");
1271 c_parser_skip_to_end_of_block_or_statement (parser);
1272 return;
1274 finish_declspecs (specs);
1275 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1277 if (empty_ok)
1278 shadow_tag (specs);
1279 else
1281 shadow_tag_warned (specs, 1);
1282 pedwarn ("empty declaration");
1284 c_parser_consume_token (parser);
1285 return;
1287 pending_xref_error ();
1288 prefix_attrs = specs->attrs;
1289 all_prefix_attrs = prefix_attrs;
1290 specs->attrs = NULL_TREE;
1291 while (true)
1293 struct c_declarator *declarator;
1294 bool dummy = false;
1295 tree fnbody;
1296 /* Declaring either one or more declarators (in which case we
1297 should diagnose if there were no declaration specifiers) or a
1298 function definition (in which case the diagnostic for
1299 implicit int suffices). */
1300 declarator = c_parser_declarator (parser, specs->type_seen_p,
1301 C_DTR_NORMAL, &dummy);
1302 if (declarator == NULL)
1304 c_parser_skip_to_end_of_block_or_statement (parser);
1305 return;
1307 if (c_parser_next_token_is (parser, CPP_EQ)
1308 || c_parser_next_token_is (parser, CPP_COMMA)
1309 || c_parser_next_token_is (parser, CPP_SEMICOLON)
1310 || c_parser_next_token_is_keyword (parser, RID_ASM)
1311 || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1313 tree asm_name = NULL_TREE;
1314 tree postfix_attrs = NULL_TREE;
1315 if (!diagnosed_no_specs && !specs->declspecs_seen_p)
1317 diagnosed_no_specs = true;
1318 pedwarn ("data definition has no type or storage class");
1320 /* Having seen a data definition, there cannot now be a
1321 function definition. */
1322 fndef_ok = false;
1323 if (c_parser_next_token_is_keyword (parser, RID_ASM))
1324 asm_name = c_parser_simple_asm_expr (parser);
1325 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1326 postfix_attrs = c_parser_attributes (parser);
1327 if (c_parser_next_token_is (parser, CPP_EQ))
1329 tree d;
1330 struct c_expr init;
1331 c_parser_consume_token (parser);
1332 /* The declaration of the variable is in effect while
1333 its initializer is parsed. */
1334 d = start_decl (declarator, specs, true,
1335 chainon (postfix_attrs, all_prefix_attrs));
1336 if (!d)
1337 d = error_mark_node;
1338 start_init (d, asm_name, global_bindings_p ());
1339 init = c_parser_initializer (parser);
1340 finish_init ();
1341 if (d != error_mark_node)
1343 maybe_warn_string_init (TREE_TYPE (d), init);
1344 finish_decl (d, init.value, asm_name);
1347 else
1349 tree d = start_decl (declarator, specs, false,
1350 chainon (postfix_attrs,
1351 all_prefix_attrs));
1352 if (d)
1353 finish_decl (d, NULL_TREE, asm_name);
1355 if (c_parser_next_token_is (parser, CPP_COMMA))
1357 c_parser_consume_token (parser);
1358 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1359 all_prefix_attrs = chainon (c_parser_attributes (parser),
1360 prefix_attrs);
1361 else
1362 all_prefix_attrs = prefix_attrs;
1363 continue;
1365 else if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1367 c_parser_consume_token (parser);
1368 return;
1370 else
1372 c_parser_error (parser, "expected %<,%> or %<;%>");
1373 c_parser_skip_to_end_of_block_or_statement (parser);
1374 return;
1377 else if (!fndef_ok)
1379 c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, "
1380 "%<asm%> or %<__attribute__%>");
1381 c_parser_skip_to_end_of_block_or_statement (parser);
1382 return;
1384 /* Function definition (nested or otherwise). */
1385 if (nested)
1387 if (pedantic)
1388 pedwarn ("ISO C forbids nested functions");
1389 push_function_context ();
1391 if (!start_function (specs, declarator, all_prefix_attrs))
1393 /* This can appear in many cases looking nothing like a
1394 function definition, so we don't give a more specific
1395 error suggesting there was one. */
1396 c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, %<asm%> "
1397 "or %<__attribute__%>");
1398 if (nested)
1399 pop_function_context ();
1400 break;
1402 /* Parse old-style parameter declarations. ??? Attributes are
1403 not allowed to start declaration specifiers here because of a
1404 syntax conflict between a function declaration with attribute
1405 suffix and a function definition with an attribute prefix on
1406 first old-style parameter declaration. Following the old
1407 parser, they are not accepted on subsequent old-style
1408 parameter declarations either. However, there is no
1409 ambiguity after the first declaration, nor indeed on the
1410 first as long as we don't allow postfix attributes after a
1411 declarator with a nonempty identifier list in a definition;
1412 and postfix attributes have never been accepted here in
1413 function definitions either. */
1414 while (c_parser_next_token_is_not (parser, CPP_EOF)
1415 && c_parser_next_token_is_not (parser, CPP_OPEN_BRACE))
1416 c_parser_declaration_or_fndef (parser, false, false, true, false);
1417 DECL_SOURCE_LOCATION (current_function_decl)
1418 = c_parser_peek_token (parser)->location;
1419 store_parm_decls ();
1420 fnbody = c_parser_compound_statement (parser);
1421 if (nested)
1423 tree decl = current_function_decl;
1424 add_stmt (fnbody);
1425 finish_function ();
1426 pop_function_context ();
1427 add_stmt (build_stmt (DECL_EXPR, decl));
1429 else
1431 add_stmt (fnbody);
1432 finish_function ();
1434 break;
1438 /* Parse an asm-definition (asm() outside a function body). This is a
1439 GNU extension.
1441 asm-definition:
1442 simple-asm-expr ;
1445 static void
1446 c_parser_asm_definition (c_parser *parser)
1448 tree asm_str = c_parser_simple_asm_expr (parser);
1449 if (asm_str)
1450 cgraph_add_asm_node (asm_str);
1451 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
1454 /* Parse some declaration specifiers (possibly none) (C90 6.5, C99
1455 6.7), adding them to SPECS (which may already include some).
1456 Storage class specifiers are accepted iff SCSPEC_OK; type
1457 specifiers are accepted iff TYPESPEC_OK; attributes are accepted at
1458 the start iff START_ATTR_OK.
1460 declaration-specifiers:
1461 storage-class-specifier declaration-specifiers[opt]
1462 type-specifier declaration-specifiers[opt]
1463 type-qualifier declaration-specifiers[opt]
1464 function-specifier declaration-specifiers[opt]
1466 Function specifiers (inline) are from C99, and are currently
1467 handled as storage class specifiers, as is __thread.
1469 C90 6.5.1, C99 6.7.1:
1470 storage-class-specifier:
1471 typedef
1472 extern
1473 static
1474 auto
1475 register
1477 C99 6.7.4:
1478 function-specifier:
1479 inline
1481 C90 6.5.2, C99 6.7.2:
1482 type-specifier:
1483 void
1484 char
1485 short
1487 long
1488 float
1489 double
1490 signed
1491 unsigned
1492 _Bool
1493 _Complex
1494 [_Imaginary removed in C99 TC2]
1495 struct-or-union-specifier
1496 enum-specifier
1497 typedef-name
1499 (_Bool and _Complex are new in C99.)
1501 C90 6.5.3, C99 6.7.3:
1503 type-qualifier:
1504 const
1505 restrict
1506 volatile
1508 (restrict is new in C99.)
1510 GNU extensions:
1512 declaration-specifiers:
1513 attributes declaration-specifiers[opt]
1515 storage-class-specifier:
1516 __thread
1518 type-specifier:
1519 typeof-specifier
1520 _Decimal32
1521 _Decimal64
1522 _Decimal128
1524 Objective-C:
1526 type-specifier:
1527 class-name objc-protocol-refs[opt]
1528 typedef-name objc-protocol-refs
1529 objc-protocol-refs
1532 static void
1533 c_parser_declspecs (c_parser *parser, struct c_declspecs *specs,
1534 bool scspec_ok, bool typespec_ok, bool start_attr_ok)
1536 bool attrs_ok = start_attr_ok;
1537 bool seen_type = specs->type_seen_p;
1538 while (c_parser_next_token_is (parser, CPP_NAME)
1539 || c_parser_next_token_is (parser, CPP_KEYWORD)
1540 || (c_dialect_objc () && c_parser_next_token_is (parser, CPP_LESS)))
1542 struct c_typespec t;
1543 tree attrs;
1544 if (c_parser_next_token_is (parser, CPP_NAME))
1546 tree value = c_parser_peek_token (parser)->value;
1547 c_id_kind kind = c_parser_peek_token (parser)->id_kind;
1548 /* This finishes the specifiers unless a type name is OK, it
1549 is declared as a type name and a type name hasn't yet
1550 been seen. */
1551 if (!typespec_ok || seen_type
1552 || (kind != C_ID_TYPENAME && kind != C_ID_CLASSNAME))
1553 break;
1554 c_parser_consume_token (parser);
1555 seen_type = true;
1556 attrs_ok = true;
1557 if (kind == C_ID_TYPENAME
1558 && (!c_dialect_objc ()
1559 || c_parser_next_token_is_not (parser, CPP_LESS)))
1561 t.kind = ctsk_typedef;
1562 /* For a typedef name, record the meaning, not the name.
1563 In case of 'foo foo, bar;'. */
1564 t.spec = lookup_name (value);
1566 else
1568 tree proto = NULL_TREE;
1569 gcc_assert (c_dialect_objc ());
1570 t.kind = ctsk_objc;
1571 if (c_parser_next_token_is (parser, CPP_LESS))
1572 proto = c_parser_objc_protocol_refs (parser);
1573 t.spec = objc_get_protocol_qualified_type (value, proto);
1575 declspecs_add_type (specs, t);
1576 continue;
1578 if (c_parser_next_token_is (parser, CPP_LESS))
1580 /* Make "<SomeProtocol>" equivalent to "id <SomeProtocol>" -
1581 nisse@lysator.liu.se. */
1582 tree proto;
1583 gcc_assert (c_dialect_objc ());
1584 if (!typespec_ok || seen_type)
1585 break;
1586 proto = c_parser_objc_protocol_refs (parser);
1587 t.kind = ctsk_objc;
1588 t.spec = objc_get_protocol_qualified_type (NULL_TREE, proto);
1589 declspecs_add_type (specs, t);
1590 continue;
1592 gcc_assert (c_parser_next_token_is (parser, CPP_KEYWORD));
1593 switch (c_parser_peek_token (parser)->keyword)
1595 case RID_STATIC:
1596 case RID_EXTERN:
1597 case RID_REGISTER:
1598 case RID_TYPEDEF:
1599 case RID_INLINE:
1600 case RID_AUTO:
1601 case RID_THREAD:
1602 if (!scspec_ok)
1603 goto out;
1604 attrs_ok = true;
1605 /* TODO: Distinguish between function specifiers (inline)
1606 and storage class specifiers, either here or in
1607 declspecs_add_scspec. */
1608 declspecs_add_scspec (specs, c_parser_peek_token (parser)->value);
1609 c_parser_consume_token (parser);
1610 break;
1611 case RID_UNSIGNED:
1612 case RID_LONG:
1613 case RID_SHORT:
1614 case RID_SIGNED:
1615 case RID_COMPLEX:
1616 case RID_INT:
1617 case RID_CHAR:
1618 case RID_FLOAT:
1619 case RID_DOUBLE:
1620 case RID_VOID:
1621 case RID_DFLOAT32:
1622 case RID_DFLOAT64:
1623 case RID_DFLOAT128:
1624 case RID_BOOL:
1625 if (!typespec_ok)
1626 goto out;
1627 attrs_ok = true;
1628 seen_type = true;
1629 OBJC_NEED_RAW_IDENTIFIER (1);
1630 t.kind = ctsk_resword;
1631 t.spec = c_parser_peek_token (parser)->value;
1632 declspecs_add_type (specs, t);
1633 c_parser_consume_token (parser);
1634 break;
1635 case RID_ENUM:
1636 if (!typespec_ok)
1637 goto out;
1638 attrs_ok = true;
1639 seen_type = true;
1640 t = c_parser_enum_specifier (parser);
1641 declspecs_add_type (specs, t);
1642 break;
1643 case RID_STRUCT:
1644 case RID_UNION:
1645 if (!typespec_ok)
1646 goto out;
1647 attrs_ok = true;
1648 seen_type = true;
1649 t = c_parser_struct_or_union_specifier (parser);
1650 declspecs_add_type (specs, t);
1651 break;
1652 case RID_TYPEOF:
1653 /* ??? The old parser rejected typeof after other type
1654 specifiers, but is a syntax error the best way of
1655 handling this? */
1656 if (!typespec_ok || seen_type)
1657 goto out;
1658 attrs_ok = true;
1659 seen_type = true;
1660 t = c_parser_typeof_specifier (parser);
1661 declspecs_add_type (specs, t);
1662 break;
1663 case RID_CONST:
1664 case RID_VOLATILE:
1665 case RID_RESTRICT:
1666 attrs_ok = true;
1667 declspecs_add_qual (specs, c_parser_peek_token (parser)->value);
1668 c_parser_consume_token (parser);
1669 break;
1670 case RID_ATTRIBUTE:
1671 if (!attrs_ok)
1672 goto out;
1673 attrs = c_parser_attributes (parser);
1674 declspecs_add_attrs (specs, attrs);
1675 break;
1676 default:
1677 goto out;
1680 out: ;
1683 /* Parse an enum specifier (C90 6.5.2.2, C99 6.7.2.2).
1685 enum-specifier:
1686 enum attributes[opt] identifier[opt] { enumerator-list } attributes[opt]
1687 enum attributes[opt] identifier[opt] { enumerator-list , } attributes[opt]
1688 enum attributes[opt] identifier
1690 The form with trailing comma is new in C99. The forms with
1691 attributes are GNU extensions. In GNU C, we accept any expression
1692 without commas in the syntax (assignment expressions, not just
1693 conditional expressions); assignment expressions will be diagnosed
1694 as non-constant.
1696 enumerator-list:
1697 enumerator
1698 enumerator-list , enumerator
1700 enumerator:
1701 enumeration-constant
1702 enumeration-constant = constant-expression
1705 static struct c_typespec
1706 c_parser_enum_specifier (c_parser *parser)
1708 struct c_typespec ret;
1709 tree attrs;
1710 tree ident = NULL_TREE;
1711 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ENUM));
1712 c_parser_consume_token (parser);
1713 attrs = c_parser_attributes (parser);
1714 if (c_parser_next_token_is (parser, CPP_NAME))
1716 ident = c_parser_peek_token (parser)->value;
1717 c_parser_consume_token (parser);
1719 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
1721 /* Parse an enum definition. */
1722 tree type = start_enum (ident);
1723 tree postfix_attrs;
1724 /* We chain the enumerators in reverse order, then put them in
1725 forward order at the end. */
1726 tree values = NULL_TREE;
1727 c_parser_consume_token (parser);
1728 while (true)
1730 tree enum_id;
1731 tree enum_value;
1732 tree enum_decl;
1733 bool seen_comma;
1734 if (c_parser_next_token_is_not (parser, CPP_NAME))
1736 c_parser_error (parser, "expected identifier");
1737 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
1738 values = error_mark_node;
1739 break;
1741 enum_id = c_parser_peek_token (parser)->value;
1742 c_parser_consume_token (parser);
1743 if (c_parser_next_token_is (parser, CPP_EQ))
1745 c_parser_consume_token (parser);
1746 enum_value = c_parser_expr_no_commas (parser, NULL).value;
1748 else
1749 enum_value = NULL_TREE;
1750 enum_decl = build_enumerator (enum_id, enum_value);
1751 TREE_CHAIN (enum_decl) = values;
1752 values = enum_decl;
1753 seen_comma = false;
1754 if (c_parser_next_token_is (parser, CPP_COMMA))
1756 seen_comma = true;
1757 c_parser_consume_token (parser);
1759 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
1761 if (seen_comma && pedantic && !flag_isoc99)
1762 pedwarn ("comma at end of enumerator list");
1763 c_parser_consume_token (parser);
1764 break;
1766 if (!seen_comma)
1768 c_parser_error (parser, "expected %<,%> or %<}%>");
1769 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
1770 values = error_mark_node;
1771 break;
1774 postfix_attrs = c_parser_attributes (parser);
1775 ret.spec = finish_enum (type, nreverse (values),
1776 chainon (attrs, postfix_attrs));
1777 ret.kind = ctsk_tagdef;
1778 return ret;
1780 else if (!ident)
1782 c_parser_error (parser, "expected %<{%>");
1783 ret.spec = error_mark_node;
1784 ret.kind = ctsk_tagref;
1785 return ret;
1787 ret = parser_xref_tag (ENUMERAL_TYPE, ident);
1788 /* In ISO C, enumerated types can be referred to only if already
1789 defined. */
1790 if (pedantic && !COMPLETE_TYPE_P (ret.spec))
1791 pedwarn ("ISO C forbids forward references to %<enum%> types");
1792 return ret;
1795 /* Parse a struct or union specifier (C90 6.5.2.1, C99 6.7.2.1).
1797 struct-or-union-specifier:
1798 struct-or-union attributes[opt] identifier[opt]
1799 { struct-contents } attributes[opt]
1800 struct-or-union attributes[opt] identifier
1802 struct-contents:
1803 struct-declaration-list
1805 struct-declaration-list:
1806 struct-declaration ;
1807 struct-declaration-list struct-declaration ;
1809 GNU extensions:
1811 struct-contents:
1812 empty
1813 struct-declaration
1814 struct-declaration-list struct-declaration
1816 struct-declaration-list:
1817 struct-declaration-list ;
1820 (Note that in the syntax here, unlike that in ISO C, the semicolons
1821 are included here rather than in struct-declaration, in order to
1822 describe the syntax with extra semicolons and missing semicolon at
1823 end.)
1825 Objective-C:
1827 struct-declaration-list:
1828 @defs ( class-name )
1830 (Note this does not include a trailing semicolon, but can be
1831 followed by further declarations, and gets a pedwarn-if-pedantic
1832 when followed by a semicolon.) */
1834 static struct c_typespec
1835 c_parser_struct_or_union_specifier (c_parser *parser)
1837 struct c_typespec ret;
1838 tree attrs;
1839 tree ident = NULL_TREE;
1840 enum tree_code code;
1841 switch (c_parser_peek_token (parser)->keyword)
1843 case RID_STRUCT:
1844 code = RECORD_TYPE;
1845 break;
1846 case RID_UNION:
1847 code = UNION_TYPE;
1848 break;
1849 default:
1850 gcc_unreachable ();
1852 c_parser_consume_token (parser);
1853 attrs = c_parser_attributes (parser);
1854 if (c_parser_next_token_is (parser, CPP_NAME))
1856 ident = c_parser_peek_token (parser)->value;
1857 c_parser_consume_token (parser);
1859 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
1861 /* Parse a struct or union definition. Start the scope of the
1862 tag before parsing components. */
1863 tree type = start_struct (code, ident);
1864 tree postfix_attrs;
1865 /* We chain the components in reverse order, then put them in
1866 forward order at the end. Each struct-declaration may
1867 declare multiple components (comma-separated), so we must use
1868 chainon to join them, although when parsing each
1869 struct-declaration we can use TREE_CHAIN directly.
1871 The theory behind all this is that there will be more
1872 semicolon separated fields than comma separated fields, and
1873 so we'll be minimizing the number of node traversals required
1874 by chainon. */
1875 tree contents = NULL_TREE;
1876 c_parser_consume_token (parser);
1877 /* Handle the Objective-C @defs construct,
1878 e.g. foo(sizeof(struct{ @defs(ClassName) }));. */
1879 if (c_parser_next_token_is_keyword (parser, RID_AT_DEFS))
1881 tree name;
1882 gcc_assert (c_dialect_objc ());
1883 c_parser_consume_token (parser);
1884 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
1885 goto end_at_defs;
1886 if (c_parser_next_token_is (parser, CPP_NAME)
1887 && c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME)
1889 name = c_parser_peek_token (parser)->value;
1890 c_parser_consume_token (parser);
1892 else
1894 c_parser_error (parser, "expected class name");
1895 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
1896 goto end_at_defs;
1898 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
1899 "expected %<)%>");
1900 contents = nreverse (objc_get_class_ivars (name));
1902 end_at_defs:
1903 /* Parse the struct-declarations and semicolons. Problems with
1904 semicolons are diagnosed here; empty structures are diagnosed
1905 elsewhere. */
1906 while (true)
1908 tree decls;
1909 /* Parse any stray semicolon. */
1910 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1912 if (pedantic)
1913 pedwarn ("extra semicolon in struct or union specified");
1914 c_parser_consume_token (parser);
1915 continue;
1917 /* Stop if at the end of the struct or union contents. */
1918 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
1920 c_parser_consume_token (parser);
1921 break;
1923 /* Accept #pragmas at struct scope. */
1924 if (c_parser_next_token_is (parser, CPP_PRAGMA))
1926 c_parser_pragma (parser, pragma_external);
1927 continue;
1929 /* Parse some comma-separated declarations, but not the
1930 trailing semicolon if any. */
1931 decls = c_parser_struct_declaration (parser);
1932 contents = chainon (decls, contents);
1933 /* If no semicolon follows, either we have a parse error or
1934 are at the end of the struct or union and should
1935 pedwarn. */
1936 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1937 c_parser_consume_token (parser);
1938 else
1940 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
1941 pedwarn ("no semicolon at end of struct or union");
1942 else
1944 c_parser_error (parser, "expected %<;%>");
1945 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
1946 break;
1950 postfix_attrs = c_parser_attributes (parser);
1951 ret.spec = finish_struct (type, nreverse (contents),
1952 chainon (attrs, postfix_attrs));
1953 ret.kind = ctsk_tagdef;
1954 return ret;
1956 else if (!ident)
1958 c_parser_error (parser, "expected %<{%>");
1959 ret.spec = error_mark_node;
1960 ret.kind = ctsk_tagref;
1961 return ret;
1963 ret = parser_xref_tag (code, ident);
1964 return ret;
1967 /* Parse a struct-declaration (C90 6.5.2.1, C99 6.7.2.1), *without*
1968 the trailing semicolon.
1970 struct-declaration:
1971 specifier-qualifier-list struct-declarator-list
1973 specifier-qualifier-list:
1974 type-specifier specifier-qualifier-list[opt]
1975 type-qualifier specifier-qualifier-list[opt]
1976 attributes specifier-qualifier-list[opt]
1978 struct-declarator-list:
1979 struct-declarator
1980 struct-declarator-list , attributes[opt] struct-declarator
1982 struct-declarator:
1983 declarator attributes[opt]
1984 declarator[opt] : constant-expression attributes[opt]
1986 GNU extensions:
1988 struct-declaration:
1989 __extension__ struct-declaration
1990 specifier-qualifier-list
1992 Unlike the ISO C syntax, semicolons are handled elsewhere. The use
1993 of attributes where shown is a GNU extension. In GNU C, we accept
1994 any expression without commas in the syntax (assignment
1995 expressions, not just conditional expressions); assignment
1996 expressions will be diagnosed as non-constant. */
1998 static tree
1999 c_parser_struct_declaration (c_parser *parser)
2001 struct c_declspecs *specs;
2002 tree prefix_attrs;
2003 tree all_prefix_attrs;
2004 tree decls;
2005 if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
2007 int ext;
2008 tree decl;
2009 ext = disable_extension_diagnostics ();
2010 c_parser_consume_token (parser);
2011 decl = c_parser_struct_declaration (parser);
2012 restore_extension_diagnostics (ext);
2013 return decl;
2015 specs = build_null_declspecs ();
2016 c_parser_declspecs (parser, specs, false, true, true);
2017 if (parser->error)
2018 return NULL_TREE;
2019 if (!specs->declspecs_seen_p)
2021 c_parser_error (parser, "expected specifier-qualifier-list");
2022 return NULL_TREE;
2024 finish_declspecs (specs);
2025 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
2027 tree ret;
2028 if (!specs->type_seen_p)
2030 if (pedantic)
2031 pedwarn ("ISO C forbids member declarations with no members");
2032 shadow_tag_warned (specs, pedantic);
2033 ret = NULL_TREE;
2035 else
2037 /* Support for unnamed structs or unions as members of
2038 structs or unions (which is [a] useful and [b] supports
2039 MS P-SDK). */
2040 ret = grokfield (build_id_declarator (NULL_TREE), specs, NULL_TREE);
2042 return ret;
2044 pending_xref_error ();
2045 prefix_attrs = specs->attrs;
2046 all_prefix_attrs = prefix_attrs;
2047 specs->attrs = NULL_TREE;
2048 decls = NULL_TREE;
2049 while (true)
2051 /* Declaring one or more declarators or un-named bit-fields. */
2052 struct c_declarator *declarator;
2053 bool dummy = false;
2054 if (c_parser_next_token_is (parser, CPP_COLON))
2055 declarator = build_id_declarator (NULL_TREE);
2056 else
2057 declarator = c_parser_declarator (parser, specs->type_seen_p,
2058 C_DTR_NORMAL, &dummy);
2059 if (declarator == NULL)
2061 c_parser_skip_to_end_of_block_or_statement (parser);
2062 break;
2064 if (c_parser_next_token_is (parser, CPP_COLON)
2065 || c_parser_next_token_is (parser, CPP_COMMA)
2066 || c_parser_next_token_is (parser, CPP_SEMICOLON)
2067 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE)
2068 || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2070 tree postfix_attrs = NULL_TREE;
2071 tree width = NULL_TREE;
2072 tree d;
2073 if (c_parser_next_token_is (parser, CPP_COLON))
2075 c_parser_consume_token (parser);
2076 width = c_parser_expr_no_commas (parser, NULL).value;
2078 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2079 postfix_attrs = c_parser_attributes (parser);
2080 d = grokfield (declarator, specs, width);
2081 decl_attributes (&d, chainon (postfix_attrs,
2082 all_prefix_attrs), 0);
2083 TREE_CHAIN (d) = decls;
2084 decls = d;
2085 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2086 all_prefix_attrs = chainon (c_parser_attributes (parser),
2087 prefix_attrs);
2088 else
2089 all_prefix_attrs = prefix_attrs;
2090 if (c_parser_next_token_is (parser, CPP_COMMA))
2091 c_parser_consume_token (parser);
2092 else if (c_parser_next_token_is (parser, CPP_SEMICOLON)
2093 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2095 /* Semicolon consumed in caller. */
2096 break;
2098 else
2100 c_parser_error (parser, "expected %<,%>, %<;%> or %<}%>");
2101 break;
2104 else
2106 c_parser_error (parser,
2107 "expected %<:%>, %<,%>, %<;%>, %<}%> or "
2108 "%<__attribute__%>");
2109 break;
2112 return decls;
2115 /* Parse a typeof specifier (a GNU extension).
2117 typeof-specifier:
2118 typeof ( expression )
2119 typeof ( type-name )
2122 static struct c_typespec
2123 c_parser_typeof_specifier (c_parser *parser)
2125 struct c_typespec ret;
2126 ret.kind = ctsk_typeof;
2127 ret.spec = error_mark_node;
2128 gcc_assert (c_parser_next_token_is_keyword (parser, RID_TYPEOF));
2129 c_parser_consume_token (parser);
2130 skip_evaluation++;
2131 in_typeof++;
2132 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2134 skip_evaluation--;
2135 in_typeof--;
2136 return ret;
2138 if (c_parser_next_token_starts_typename (parser))
2140 struct c_type_name *type = c_parser_type_name (parser);
2141 skip_evaluation--;
2142 in_typeof--;
2143 if (type != NULL)
2145 ret.spec = groktypename (type);
2146 pop_maybe_used (variably_modified_type_p (ret.spec, NULL_TREE));
2149 else
2151 bool was_vm;
2152 struct c_expr expr = c_parser_expression (parser);
2153 skip_evaluation--;
2154 in_typeof--;
2155 if (TREE_CODE (expr.value) == COMPONENT_REF
2156 && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
2157 error ("%<typeof%> applied to a bit-field");
2158 ret.spec = TREE_TYPE (expr.value);
2159 was_vm = variably_modified_type_p (ret.spec, NULL_TREE);
2160 /* This should be returned with the type so that when the type
2161 is evaluated, this can be evaluated. For now, we avoid
2162 evaluation when the context might. */
2163 if (!skip_evaluation && was_vm)
2164 c_finish_expr_stmt (expr.value);
2165 pop_maybe_used (was_vm);
2167 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
2168 return ret;
2171 /* Parse a declarator, possibly an abstract declarator (C90 6.5.4,
2172 6.5.5, C99 6.7.5, 6.7.6). If TYPE_SEEN_P then a typedef name may
2173 be redeclared; otherwise it may not. KIND indicates which kind of
2174 declarator is wanted. Returns a valid declarator except in the
2175 case of a syntax error in which case NULL is returned. *SEEN_ID is
2176 set to true if an identifier being declared is seen; this is used
2177 to diagnose bad forms of abstract array declarators and to
2178 determine whether an identifier list is syntactically permitted.
2180 declarator:
2181 pointer[opt] direct-declarator
2183 direct-declarator:
2184 identifier
2185 ( attributes[opt] declarator )
2186 direct-declarator array-declarator
2187 direct-declarator ( parameter-type-list )
2188 direct-declarator ( identifier-list[opt] )
2190 pointer:
2191 * type-qualifier-list[opt]
2192 * type-qualifier-list[opt] pointer
2194 type-qualifier-list:
2195 type-qualifier
2196 attributes
2197 type-qualifier-list type-qualifier
2198 type-qualifier-list attributes
2200 parameter-type-list:
2201 parameter-list
2202 parameter-list , ...
2204 parameter-list:
2205 parameter-declaration
2206 parameter-list , parameter-declaration
2208 parameter-declaration:
2209 declaration-specifiers declarator attributes[opt]
2210 declaration-specifiers abstract-declarator[opt] attributes[opt]
2212 identifier-list:
2213 identifier
2214 identifier-list , identifier
2216 abstract-declarator:
2217 pointer
2218 pointer[opt] direct-abstract-declarator
2220 direct-abstract-declarator:
2221 ( attributes[opt] abstract-declarator )
2222 direct-abstract-declarator[opt] array-declarator
2223 direct-abstract-declarator[opt] ( parameter-type-list[opt] )
2225 GNU extensions:
2227 direct-declarator:
2228 direct-declarator ( parameter-forward-declarations
2229 parameter-type-list[opt] )
2231 direct-abstract-declarator:
2232 direct-abstract-declarator[opt] ( parameter-forward-declarations
2233 parameter-type-list[opt] )
2235 parameter-forward-declarations:
2236 parameter-list ;
2237 parameter-forward-declarations parameter-list ;
2239 The uses of attributes shown above are GNU extensions.
2241 Some forms of array declarator are not included in C99 in the
2242 syntax for abstract declarators; these are disallowed elsewhere.
2243 This may be a defect (DR#289).
2245 This function also accepts an omitted abstract declarator as being
2246 an abstract declarator, although not part of the formal syntax. */
2248 static struct c_declarator *
2249 c_parser_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
2250 bool *seen_id)
2252 /* Parse any initial pointer part. */
2253 if (c_parser_next_token_is (parser, CPP_MULT))
2255 struct c_declspecs *quals_attrs = build_null_declspecs ();
2256 struct c_declarator *inner;
2257 c_parser_consume_token (parser);
2258 c_parser_declspecs (parser, quals_attrs, false, false, true);
2259 inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
2260 if (inner == NULL)
2261 return NULL;
2262 else
2263 return make_pointer_declarator (quals_attrs, inner);
2265 /* Now we have a direct declarator, direct abstract declarator or
2266 nothing (which counts as a direct abstract declarator here). */
2267 return c_parser_direct_declarator (parser, type_seen_p, kind, seen_id);
2270 /* Parse a direct declarator or direct abstract declarator; arguments
2271 as c_parser_declarator. */
2273 static struct c_declarator *
2274 c_parser_direct_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
2275 bool *seen_id)
2277 /* The direct declarator must start with an identifier (possibly
2278 omitted) or a parenthesized declarator (possibly abstract). In
2279 an ordinary declarator, initial parentheses must start a
2280 parenthesized declarator. In an abstract declarator or parameter
2281 declarator, they could start a parenthesized declarator or a
2282 parameter list. To tell which, the open parenthesis and any
2283 following attributes must be read. If a declaration specifier
2284 follows, then it is a parameter list; if the specifier is a
2285 typedef name, there might be an ambiguity about redeclaring it,
2286 which is resolved in the direction of treating it as a typedef
2287 name. If a close parenthesis follows, it is also an empty
2288 parameter list, as the syntax does not permit empty abstract
2289 declarators. Otherwise, it is a parenthesized declarator (in
2290 which case the analysis may be repeated inside it, recursively).
2292 ??? There is an ambiguity in a parameter declaration "int
2293 (__attribute__((foo)) x)", where x is not a typedef name: it
2294 could be an abstract declarator for a function, or declare x with
2295 parentheses. The proper resolution of this ambiguity needs
2296 documenting. At present we follow an accident of the old
2297 parser's implementation, whereby the first parameter must have
2298 some declaration specifiers other than just attributes. Thus as
2299 a parameter declaration it is treated as a parenthesized
2300 parameter named x, and as an abstract declarator it is
2301 rejected.
2303 ??? Also following the old parser, attributes inside an empty
2304 parameter list are ignored, making it a list not yielding a
2305 prototype, rather than giving an error or making it have one
2306 parameter with implicit type int.
2308 ??? Also following the old parser, typedef names may be
2309 redeclared in declarators, but not Objective-C class names. */
2311 if (kind != C_DTR_ABSTRACT
2312 && c_parser_next_token_is (parser, CPP_NAME)
2313 && ((type_seen_p
2314 && c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME)
2315 || c_parser_peek_token (parser)->id_kind == C_ID_ID))
2317 struct c_declarator *inner
2318 = build_id_declarator (c_parser_peek_token (parser)->value);
2319 *seen_id = true;
2320 inner->id_loc = c_parser_peek_token (parser)->location;
2321 c_parser_consume_token (parser);
2322 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
2325 if (kind != C_DTR_NORMAL
2326 && c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
2328 struct c_declarator *inner = build_id_declarator (NULL_TREE);
2329 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
2332 /* Either we are at the end of an abstract declarator, or we have
2333 parentheses. */
2335 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
2337 tree attrs;
2338 struct c_declarator *inner;
2339 c_parser_consume_token (parser);
2340 attrs = c_parser_attributes (parser);
2341 if (kind != C_DTR_NORMAL
2342 && (c_parser_next_token_starts_declspecs (parser)
2343 || c_parser_next_token_is (parser, CPP_CLOSE_PAREN)))
2345 struct c_arg_info *args
2346 = c_parser_parms_declarator (parser, kind == C_DTR_NORMAL,
2347 attrs);
2348 if (args == NULL)
2349 return NULL;
2350 else
2352 inner
2353 = build_function_declarator (args,
2354 build_id_declarator (NULL_TREE));
2355 return c_parser_direct_declarator_inner (parser, *seen_id,
2356 inner);
2359 /* A parenthesized declarator. */
2360 inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
2361 if (inner != NULL && attrs != NULL)
2362 inner = build_attrs_declarator (attrs, inner);
2363 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2365 c_parser_consume_token (parser);
2366 if (inner == NULL)
2367 return NULL;
2368 else
2369 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
2371 else
2373 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2374 "expected %<)%>");
2375 return NULL;
2378 else
2380 if (kind == C_DTR_NORMAL)
2382 c_parser_error (parser, "expected identifier or %<(%>");
2383 return NULL;
2385 else
2386 return build_id_declarator (NULL_TREE);
2390 /* Parse part of a direct declarator or direct abstract declarator,
2391 given that some (in INNER) has already been parsed; ID_PRESENT is
2392 true if an identifier is present, false for an abstract
2393 declarator. */
2395 static struct c_declarator *
2396 c_parser_direct_declarator_inner (c_parser *parser, bool id_present,
2397 struct c_declarator *inner)
2399 /* Parse a sequence of array declarators and parameter lists. */
2400 if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
2402 struct c_declarator *declarator;
2403 struct c_declspecs *quals_attrs = build_null_declspecs ();
2404 bool static_seen;
2405 bool star_seen;
2406 tree dimen;
2407 c_parser_consume_token (parser);
2408 c_parser_declspecs (parser, quals_attrs, false, false, true);
2409 static_seen = c_parser_next_token_is_keyword (parser, RID_STATIC);
2410 if (static_seen)
2411 c_parser_consume_token (parser);
2412 if (static_seen && !quals_attrs->declspecs_seen_p)
2413 c_parser_declspecs (parser, quals_attrs, false, false, true);
2414 if (!quals_attrs->declspecs_seen_p)
2415 quals_attrs = NULL;
2416 /* If "static" is present, there must be an array dimension.
2417 Otherwise, there may be a dimension, "*", or no
2418 dimension. */
2419 if (static_seen)
2421 star_seen = false;
2422 dimen = c_parser_expr_no_commas (parser, NULL).value;
2424 else
2426 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
2428 dimen = NULL_TREE;
2429 star_seen = false;
2431 else if (c_parser_next_token_is (parser, CPP_MULT))
2433 if (c_parser_peek_2nd_token (parser)->type == CPP_CLOSE_SQUARE)
2435 dimen = NULL_TREE;
2436 star_seen = true;
2437 c_parser_consume_token (parser);
2439 else
2441 star_seen = false;
2442 dimen = c_parser_expr_no_commas (parser, NULL).value;
2445 else
2447 star_seen = false;
2448 dimen = c_parser_expr_no_commas (parser, NULL).value;
2451 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
2452 c_parser_consume_token (parser);
2453 else
2455 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
2456 "expected %<]%>");
2457 return NULL;
2459 declarator = build_array_declarator (dimen, quals_attrs, static_seen,
2460 star_seen);
2461 if (declarator == NULL)
2462 return NULL;
2463 inner = set_array_declarator_inner (declarator, inner, !id_present);
2464 return c_parser_direct_declarator_inner (parser, id_present, inner);
2466 else if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
2468 tree attrs;
2469 struct c_arg_info *args;
2470 c_parser_consume_token (parser);
2471 attrs = c_parser_attributes (parser);
2472 args = c_parser_parms_declarator (parser, id_present, attrs);
2473 if (args == NULL)
2474 return NULL;
2475 else
2477 inner = build_function_declarator (args, inner);
2478 return c_parser_direct_declarator_inner (parser, id_present, inner);
2481 return inner;
2484 /* Parse a parameter list or identifier list, including the closing
2485 parenthesis but not the opening one. ATTRS are the attributes at
2486 the start of the list. ID_LIST_OK is true if an identifier list is
2487 acceptable; such a list must not have attributes at the start. */
2489 static struct c_arg_info *
2490 c_parser_parms_declarator (c_parser *parser, bool id_list_ok, tree attrs)
2492 push_scope ();
2493 declare_parm_level ();
2494 /* If the list starts with an identifier, it is an identifier list.
2495 Otherwise, it is either a prototype list or an empty list. */
2496 if (id_list_ok
2497 && !attrs
2498 && c_parser_next_token_is (parser, CPP_NAME)
2499 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
2501 tree list = NULL_TREE, *nextp = &list;
2502 while (c_parser_next_token_is (parser, CPP_NAME)
2503 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
2505 *nextp = build_tree_list (NULL_TREE,
2506 c_parser_peek_token (parser)->value);
2507 nextp = & TREE_CHAIN (*nextp);
2508 c_parser_consume_token (parser);
2509 if (c_parser_next_token_is_not (parser, CPP_COMMA))
2510 break;
2511 c_parser_consume_token (parser);
2512 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2514 c_parser_error (parser, "expected identifier");
2515 break;
2518 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2520 struct c_arg_info *ret = XOBNEW (&parser_obstack, struct c_arg_info);
2521 ret->parms = 0;
2522 ret->tags = 0;
2523 ret->types = list;
2524 ret->others = 0;
2525 ret->had_vla_unspec = 0;
2526 c_parser_consume_token (parser);
2527 pop_scope ();
2528 return ret;
2530 else
2532 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2533 "expected %<)%>");
2534 pop_scope ();
2535 return NULL;
2538 else
2540 struct c_arg_info *ret = c_parser_parms_list_declarator (parser, attrs);
2541 pop_scope ();
2542 return ret;
2546 /* Parse a parameter list (possibly empty), including the closing
2547 parenthesis but not the opening one. ATTRS are the attributes at
2548 the start of the list. */
2550 static struct c_arg_info *
2551 c_parser_parms_list_declarator (c_parser *parser, tree attrs)
2553 bool good_parm = false;
2554 /* ??? Following the old parser, forward parameter declarations may
2555 use abstract declarators, and if no real parameter declarations
2556 follow the forward declarations then this is not diagnosed. Also
2557 note as above that attributes are ignored as the only contents of
2558 the parentheses, or as the only contents after forward
2559 declarations. */
2560 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2562 struct c_arg_info *ret = XOBNEW (&parser_obstack, struct c_arg_info);
2563 ret->parms = 0;
2564 ret->tags = 0;
2565 ret->types = 0;
2566 ret->others = 0;
2567 ret->had_vla_unspec = 0;
2568 c_parser_consume_token (parser);
2569 return ret;
2571 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
2573 struct c_arg_info *ret = XOBNEW (&parser_obstack, struct c_arg_info);
2574 ret->parms = 0;
2575 ret->tags = 0;
2576 ret->others = 0;
2577 ret->had_vla_unspec = 0;
2578 /* Suppress -Wold-style-definition for this case. */
2579 ret->types = error_mark_node;
2580 error ("ISO C requires a named argument before %<...%>");
2581 c_parser_consume_token (parser);
2582 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2584 c_parser_consume_token (parser);
2585 return ret;
2587 else
2589 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2590 "expected %<)%>");
2591 return NULL;
2594 /* Nonempty list of parameters, either terminated with semicolon
2595 (forward declarations; recurse) or with close parenthesis (normal
2596 function) or with ", ... )" (variadic function). */
2597 while (true)
2599 /* Parse a parameter. */
2600 struct c_parm *parm = c_parser_parameter_declaration (parser, attrs);
2601 attrs = NULL_TREE;
2602 if (parm != NULL)
2604 good_parm = true;
2605 push_parm_decl (parm);
2607 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
2609 tree new_attrs;
2610 c_parser_consume_token (parser);
2611 mark_forward_parm_decls ();
2612 new_attrs = c_parser_attributes (parser);
2613 return c_parser_parms_list_declarator (parser, new_attrs);
2615 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2617 c_parser_consume_token (parser);
2618 if (good_parm)
2619 return get_parm_info (false);
2620 else
2622 struct c_arg_info *ret
2623 = XOBNEW (&parser_obstack, struct c_arg_info);
2624 ret->parms = 0;
2625 ret->tags = 0;
2626 ret->types = 0;
2627 ret->others = 0;
2628 ret->had_vla_unspec = 0;
2629 return ret;
2632 if (!c_parser_require (parser, CPP_COMMA,
2633 "expected %<;%>, %<,%> or %<)%>"))
2635 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
2636 return NULL;
2638 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
2640 c_parser_consume_token (parser);
2641 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2643 c_parser_consume_token (parser);
2644 if (good_parm)
2645 return get_parm_info (true);
2646 else
2648 struct c_arg_info *ret
2649 = XOBNEW (&parser_obstack, struct c_arg_info);
2650 ret->parms = 0;
2651 ret->tags = 0;
2652 ret->types = 0;
2653 ret->others = 0;
2654 ret->had_vla_unspec = 0;
2655 return ret;
2658 else
2660 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2661 "expected %<)%>");
2662 return NULL;
2668 /* Parse a parameter declaration. ATTRS are the attributes at the
2669 start of the declaration if it is the first parameter. */
2671 static struct c_parm *
2672 c_parser_parameter_declaration (c_parser *parser, tree attrs)
2674 struct c_declspecs *specs;
2675 struct c_declarator *declarator;
2676 tree prefix_attrs;
2677 tree postfix_attrs = NULL_TREE;
2678 bool dummy = false;
2679 if (!c_parser_next_token_starts_declspecs (parser))
2681 /* ??? In some Objective-C cases '...' isn't applicable so there
2682 should be a different message. */
2683 c_parser_error (parser,
2684 "expected declaration specifiers or %<...%>");
2685 c_parser_skip_to_end_of_parameter (parser);
2686 return NULL;
2688 specs = build_null_declspecs ();
2689 if (attrs)
2691 declspecs_add_attrs (specs, attrs);
2692 attrs = NULL_TREE;
2694 c_parser_declspecs (parser, specs, true, true, true);
2695 finish_declspecs (specs);
2696 pending_xref_error ();
2697 prefix_attrs = specs->attrs;
2698 specs->attrs = NULL_TREE;
2699 declarator = c_parser_declarator (parser, specs->type_seen_p,
2700 C_DTR_PARM, &dummy);
2701 if (declarator == NULL)
2703 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
2704 return NULL;
2706 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2707 postfix_attrs = c_parser_attributes (parser);
2708 return build_c_parm (specs, chainon (postfix_attrs, prefix_attrs),
2709 declarator);
2712 /* Parse a string literal in an asm expression. It should not be
2713 translated, and wide string literals are an error although
2714 permitted by the syntax. This is a GNU extension.
2716 asm-string-literal:
2717 string-literal
2719 ??? At present, following the old parser, the caller needs to have
2720 set c_lex_string_translate to 0. It would be better to follow the
2721 C++ parser rather than using the c_lex_string_translate kludge. */
2723 static tree
2724 c_parser_asm_string_literal (c_parser *parser)
2726 tree str;
2727 if (c_parser_next_token_is (parser, CPP_STRING))
2729 str = c_parser_peek_token (parser)->value;
2730 c_parser_consume_token (parser);
2732 else if (c_parser_next_token_is (parser, CPP_WSTRING))
2734 error ("wide string literal in %<asm%>");
2735 str = build_string (1, "");
2736 c_parser_consume_token (parser);
2738 else
2740 c_parser_error (parser, "expected string literal");
2741 str = NULL_TREE;
2743 return str;
2746 /* Parse a simple asm expression. This is used in restricted
2747 contexts, where a full expression with inputs and outputs does not
2748 make sense. This is a GNU extension.
2750 simple-asm-expr:
2751 asm ( asm-string-literal )
2754 static tree
2755 c_parser_simple_asm_expr (c_parser *parser)
2757 tree str;
2758 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
2759 /* ??? Follow the C++ parser rather than using the
2760 c_lex_string_translate kludge. */
2761 c_lex_string_translate = 0;
2762 c_parser_consume_token (parser);
2763 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2765 c_lex_string_translate = 1;
2766 return NULL_TREE;
2768 str = c_parser_asm_string_literal (parser);
2769 c_lex_string_translate = 1;
2770 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
2772 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
2773 return NULL_TREE;
2775 return str;
2778 /* Parse (possibly empty) attributes. This is a GNU extension.
2780 attributes:
2781 empty
2782 attributes attribute
2784 attribute:
2785 __attribute__ ( ( attribute-list ) )
2787 attribute-list:
2788 attrib
2789 attribute_list , attrib
2791 attrib:
2792 empty
2793 any-word
2794 any-word ( identifier )
2795 any-word ( identifier , nonempty-expr-list )
2796 any-word ( expr-list )
2798 where the "identifier" must not be declared as a type, and
2799 "any-word" may be any identifier (including one declared as a
2800 type), a reserved word storage class specifier, type specifier or
2801 type qualifier. ??? This still leaves out most reserved keywords
2802 (following the old parser), shouldn't we include them, and why not
2803 allow identifiers declared as types to start the arguments? */
2805 static tree
2806 c_parser_attributes (c_parser *parser)
2808 tree attrs = NULL_TREE;
2809 while (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2811 /* ??? Follow the C++ parser rather than using the
2812 c_lex_string_translate kludge. */
2813 c_lex_string_translate = 0;
2814 c_parser_consume_token (parser);
2815 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2817 c_lex_string_translate = 1;
2818 return attrs;
2820 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2822 c_lex_string_translate = 1;
2823 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
2824 return attrs;
2826 /* Parse the attribute list. */
2827 while (c_parser_next_token_is (parser, CPP_COMMA)
2828 || c_parser_next_token_is (parser, CPP_NAME)
2829 || c_parser_next_token_is (parser, CPP_KEYWORD))
2831 tree attr, attr_name, attr_args;
2832 if (c_parser_next_token_is (parser, CPP_COMMA))
2834 c_parser_consume_token (parser);
2835 continue;
2837 if (c_parser_next_token_is (parser, CPP_KEYWORD))
2839 /* ??? See comment above about what keywords are
2840 accepted here. */
2841 bool ok;
2842 switch (c_parser_peek_token (parser)->keyword)
2844 case RID_STATIC:
2845 case RID_UNSIGNED:
2846 case RID_LONG:
2847 case RID_CONST:
2848 case RID_EXTERN:
2849 case RID_REGISTER:
2850 case RID_TYPEDEF:
2851 case RID_SHORT:
2852 case RID_INLINE:
2853 case RID_VOLATILE:
2854 case RID_SIGNED:
2855 case RID_AUTO:
2856 case RID_RESTRICT:
2857 case RID_COMPLEX:
2858 case RID_THREAD:
2859 case RID_INT:
2860 case RID_CHAR:
2861 case RID_FLOAT:
2862 case RID_DOUBLE:
2863 case RID_VOID:
2864 case RID_DFLOAT32:
2865 case RID_DFLOAT64:
2866 case RID_DFLOAT128:
2867 case RID_BOOL:
2868 ok = true;
2869 break;
2870 default:
2871 ok = false;
2872 break;
2874 if (!ok)
2875 break;
2877 attr_name = c_parser_peek_token (parser)->value;
2878 c_parser_consume_token (parser);
2879 if (c_parser_next_token_is_not (parser, CPP_OPEN_PAREN))
2881 attr = build_tree_list (attr_name, NULL_TREE);
2882 attrs = chainon (attrs, attr);
2883 continue;
2885 c_parser_consume_token (parser);
2886 /* Parse the attribute contents. If they start with an
2887 identifier which is followed by a comma or close
2888 parenthesis, then the arguments start with that
2889 identifier; otherwise they are an expression list. */
2890 if (c_parser_next_token_is (parser, CPP_NAME)
2891 && c_parser_peek_token (parser)->id_kind == C_ID_ID
2892 && ((c_parser_peek_2nd_token (parser)->type == CPP_COMMA)
2893 || (c_parser_peek_2nd_token (parser)->type
2894 == CPP_CLOSE_PAREN)))
2896 tree arg1 = c_parser_peek_token (parser)->value;
2897 c_parser_consume_token (parser);
2898 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2899 attr_args = build_tree_list (NULL_TREE, arg1);
2900 else
2902 c_parser_consume_token (parser);
2903 attr_args = tree_cons (NULL_TREE, arg1,
2904 c_parser_expr_list (parser, false));
2907 else
2909 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2910 attr_args = NULL_TREE;
2911 else
2912 attr_args = c_parser_expr_list (parser, false);
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
2919 c_lex_string_translate = 1;
2920 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2921 "expected %<)%>");
2922 return attrs;
2924 attrs = chainon (attrs, attr);
2926 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2927 c_parser_consume_token (parser);
2928 else
2930 c_lex_string_translate = 1;
2931 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2932 "expected %<)%>");
2933 return attrs;
2935 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2936 c_parser_consume_token (parser);
2937 else
2939 c_lex_string_translate = 1;
2940 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2941 "expected %<)%>");
2942 return attrs;
2944 c_lex_string_translate = 1;
2946 return attrs;
2949 /* Parse a type name (C90 6.5.5, C99 6.7.6).
2951 type-name:
2952 specifier-qualifier-list abstract-declarator[opt]
2955 static struct c_type_name *
2956 c_parser_type_name (c_parser *parser)
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)
2965 c_parser_error (parser, "expected specifier-qualifier-list");
2966 return NULL;
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;
2980 /* Parse an initializer (C90 6.5.7, C99 6.7.8).
2982 initializer:
2983 assignment-expression
2984 { initializer-list }
2985 { initializer-list , }
2987 initializer-list:
2988 designation[opt] initializer
2989 initializer-list , designation[opt] initializer
2991 designation:
2992 designator-list =
2994 designator-list:
2995 designator
2996 designator-list designator
2998 designator:
2999 array-designator
3000 . identifier
3002 array-designator:
3003 [ constant-expression ]
3005 GNU extensions:
3007 initializer:
3010 designation:
3011 array-designator
3012 identifier :
3014 array-designator:
3015 [ constant-expression ... constant-expression ]
3017 Any expression without commas is accepted in the syntax for the
3018 constant-expressions, with non-constant expressions rejected later.
3020 This function is only used for top-level initializers; for nested
3021 ones, see c_parser_initval. */
3023 static struct c_expr
3024 c_parser_initializer (c_parser *parser)
3026 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
3027 return c_parser_braced_init (parser, NULL_TREE, false);
3028 else
3030 struct c_expr ret;
3031 ret = c_parser_expr_no_commas (parser, NULL);
3032 if (TREE_CODE (ret.value) != STRING_CST
3033 && TREE_CODE (ret.value) != COMPOUND_LITERAL_EXPR)
3034 ret = default_function_array_conversion (ret);
3035 return ret;
3039 /* Parse a braced initializer list. TYPE is the type specified for a
3040 compound literal, and NULL_TREE for other initializers and for
3041 nested braced lists. NESTED_P is true for nested braced lists,
3042 false for the list of a compound literal or the list that is the
3043 top-level initializer in a declaration. */
3045 static struct c_expr
3046 c_parser_braced_init (c_parser *parser, tree type, bool nested_p)
3048 gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
3049 c_parser_consume_token (parser);
3050 if (nested_p)
3051 push_init_level (0);
3052 else
3053 really_start_incremental_init (type);
3054 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3056 if (pedantic)
3057 pedwarn ("ISO C forbids empty initializer braces");
3059 else
3061 /* Parse a non-empty initializer list, possibly with a trailing
3062 comma. */
3063 while (true)
3065 c_parser_initelt (parser);
3066 if (parser->error)
3067 break;
3068 if (c_parser_next_token_is (parser, CPP_COMMA))
3069 c_parser_consume_token (parser);
3070 else
3071 break;
3072 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3073 break;
3076 if (c_parser_next_token_is_not (parser, CPP_CLOSE_BRACE))
3078 struct c_expr ret;
3079 ret.value = error_mark_node;
3080 ret.original_code = ERROR_MARK;
3081 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, "expected %<}%>");
3082 return ret;
3084 c_parser_consume_token (parser);
3085 return pop_init_level (0);
3088 /* Parse a nested initializer, including designators. */
3090 static void
3091 c_parser_initelt (c_parser *parser)
3093 /* Parse any designator or designator list. A single array
3094 designator may have the subsequent "=" omitted in GNU C, but a
3095 longer list or a structure member designator may not. */
3096 if (c_parser_next_token_is (parser, CPP_NAME)
3097 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
3099 /* Old-style structure member designator. */
3100 set_init_label (c_parser_peek_token (parser)->value);
3101 if (pedantic)
3102 pedwarn ("obsolete use of designated initializer with %<:%>");
3103 c_parser_consume_token (parser);
3104 c_parser_consume_token (parser);
3106 else
3108 /* des_seen is 0 if there have been no designators, 1 if there
3109 has been a single array designator and 2 otherwise. */
3110 int des_seen = 0;
3111 while (c_parser_next_token_is (parser, CPP_OPEN_SQUARE)
3112 || c_parser_next_token_is (parser, CPP_DOT))
3114 int des_prev = des_seen;
3115 if (des_seen < 2)
3116 des_seen++;
3117 if (c_parser_next_token_is (parser, CPP_DOT))
3119 des_seen = 2;
3120 c_parser_consume_token (parser);
3121 if (c_parser_next_token_is (parser, CPP_NAME))
3123 set_init_label (c_parser_peek_token (parser)->value);
3124 c_parser_consume_token (parser);
3126 else
3128 struct c_expr init;
3129 init.value = error_mark_node;
3130 init.original_code = ERROR_MARK;
3131 c_parser_error (parser, "expected identifier");
3132 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
3133 process_init_element (init);
3134 return;
3137 else
3139 tree first, second;
3140 /* ??? Following the old parser, [ objc-receiver
3141 objc-message-args ] is accepted as an initializer,
3142 being distinguished from a designator by what follows
3143 the first assignment expression inside the square
3144 brackets, but after a first array designator a
3145 subsequent square bracket is for Objective-C taken to
3146 start an expression, using the obsolete form of
3147 designated initializer without '=', rather than
3148 possibly being a second level of designation: in LALR
3149 terms, the '[' is shifted rather than reducing
3150 designator to designator-list. */
3151 if (des_prev == 1 && c_dialect_objc ())
3153 des_seen = des_prev;
3154 break;
3156 if (des_prev == 0 && c_dialect_objc ())
3158 /* This might be an array designator or an
3159 Objective-C message expression. If the former,
3160 continue parsing here; if the latter, parse the
3161 remainder of the initializer given the starting
3162 primary-expression. ??? It might make sense to
3163 distinguish when des_prev == 1 as well; see
3164 previous comment. */
3165 tree rec, args;
3166 struct c_expr mexpr;
3167 c_parser_consume_token (parser);
3168 if (c_parser_peek_token (parser)->type == CPP_NAME
3169 && ((c_parser_peek_token (parser)->id_kind
3170 == C_ID_TYPENAME)
3171 || (c_parser_peek_token (parser)->id_kind
3172 == C_ID_CLASSNAME)))
3174 /* Type name receiver. */
3175 tree id = c_parser_peek_token (parser)->value;
3176 c_parser_consume_token (parser);
3177 rec = objc_get_class_reference (id);
3178 goto parse_message_args;
3180 first = c_parser_expr_no_commas (parser, NULL).value;
3181 if (c_parser_next_token_is (parser, CPP_ELLIPSIS)
3182 || c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3183 goto array_desig_after_first;
3184 /* Expression receiver. So far only one part
3185 without commas has been parsed; there might be
3186 more of the expression. */
3187 rec = first;
3188 while (c_parser_next_token_is (parser, CPP_COMMA))
3190 struct c_expr next;
3191 c_parser_consume_token (parser);
3192 next = c_parser_expr_no_commas (parser, NULL);
3193 next = default_function_array_conversion (next);
3194 rec = build_compound_expr (rec, next.value);
3196 parse_message_args:
3197 /* Now parse the objc-message-args. */
3198 args = c_parser_objc_message_args (parser);
3199 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
3200 "expected %<]%>");
3201 mexpr.value
3202 = objc_build_message_expr (build_tree_list (rec, args));
3203 mexpr.original_code = ERROR_MARK;
3204 /* Now parse and process the remainder of the
3205 initializer, starting with this message
3206 expression as a primary-expression. */
3207 c_parser_initval (parser, &mexpr);
3208 return;
3210 c_parser_consume_token (parser);
3211 first = c_parser_expr_no_commas (parser, NULL).value;
3212 array_desig_after_first:
3213 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3215 c_parser_consume_token (parser);
3216 second = c_parser_expr_no_commas (parser, NULL).value;
3218 else
3219 second = NULL_TREE;
3220 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3222 c_parser_consume_token (parser);
3223 set_init_index (first, second);
3224 if (pedantic && second)
3225 pedwarn ("ISO C forbids specifying range of "
3226 "elements to initialize");
3228 else
3229 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
3230 "expected %<]%>");
3233 if (des_seen >= 1)
3235 if (c_parser_next_token_is (parser, CPP_EQ))
3237 if (pedantic && !flag_isoc99)
3238 pedwarn ("ISO C90 forbids specifying subobject to initialize");
3239 c_parser_consume_token (parser);
3241 else
3243 if (des_seen == 1)
3245 if (pedantic)
3246 pedwarn ("obsolete use of designated initializer "
3247 "without %<=%>");
3249 else
3251 struct c_expr init;
3252 init.value = error_mark_node;
3253 init.original_code = ERROR_MARK;
3254 c_parser_error (parser, "expected %<=%>");
3255 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
3256 process_init_element (init);
3257 return;
3262 c_parser_initval (parser, NULL);
3265 /* Parse a nested initializer; as c_parser_initializer but parses
3266 initializers within braced lists, after any designators have been
3267 applied. If AFTER is not NULL then it is an Objective-C message
3268 expression which is the primary-expression starting the
3269 initializer. */
3271 static void
3272 c_parser_initval (c_parser *parser, struct c_expr *after)
3274 struct c_expr init;
3275 gcc_assert (!after || c_dialect_objc ());
3276 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE) && !after)
3277 init = c_parser_braced_init (parser, NULL_TREE, true);
3278 else
3280 init = c_parser_expr_no_commas (parser, after);
3281 if (init.value != NULL_TREE
3282 && TREE_CODE (init.value) != STRING_CST
3283 && TREE_CODE (init.value) != COMPOUND_LITERAL_EXPR)
3284 init = default_function_array_conversion (init);
3286 process_init_element (init);
3289 /* Parse a compound statement (possibly a function body) (C90 6.6.2,
3290 C99 6.8.2).
3292 compound-statement:
3293 { block-item-list[opt] }
3294 { label-declarations block-item-list }
3296 block-item-list:
3297 block-item
3298 block-item-list block-item
3300 block-item:
3301 nested-declaration
3302 statement
3304 nested-declaration:
3305 declaration
3307 GNU extensions:
3309 compound-statement:
3310 { label-declarations block-item-list }
3312 nested-declaration:
3313 __extension__ nested-declaration
3314 nested-function-definition
3316 label-declarations:
3317 label-declaration
3318 label-declarations label-declaration
3320 label-declaration:
3321 __label__ identifier-list ;
3323 Allowing the mixing of declarations and code is new in C99. The
3324 GNU syntax also permits (not shown above) labels at the end of
3325 compound statements, which yield an error. We don't allow labels
3326 on declarations; this might seem like a natural extension, but
3327 there would be a conflict between attributes on the label and
3328 prefix attributes on the declaration. ??? The syntax follows the
3329 old parser in requiring something after label declarations.
3330 Although they are erroneous if the labels declared aren't defined,
3331 is it useful for the syntax to be this way?
3333 OpenMP:
3335 block-item:
3336 openmp-directive
3338 openmp-directive:
3339 barrier-directive
3340 flush-directive */
3342 static tree
3343 c_parser_compound_statement (c_parser *parser)
3345 tree stmt;
3346 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
3347 return error_mark_node;
3348 stmt = c_begin_compound_stmt (true);
3349 c_parser_compound_statement_nostart (parser);
3350 return c_end_compound_stmt (stmt, true);
3353 /* Parse a compound statement except for the opening brace. This is
3354 used for parsing both compound statements and statement expressions
3355 (which follow different paths to handling the opening). */
3357 static void
3358 c_parser_compound_statement_nostart (c_parser *parser)
3360 bool last_stmt = false;
3361 bool last_label = false;
3362 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3364 c_parser_consume_token (parser);
3365 return;
3367 if (c_parser_next_token_is_keyword (parser, RID_LABEL))
3369 /* Read zero or more forward-declarations for labels that nested
3370 functions can jump to. */
3371 while (c_parser_next_token_is_keyword (parser, RID_LABEL))
3373 c_parser_consume_token (parser);
3374 /* Any identifiers, including those declared as type names,
3375 are OK here. */
3376 while (true)
3378 tree label;
3379 if (c_parser_next_token_is_not (parser, CPP_NAME))
3381 c_parser_error (parser, "expected identifier");
3382 break;
3384 label
3385 = declare_label (c_parser_peek_token (parser)->value);
3386 C_DECLARED_LABEL_FLAG (label) = 1;
3387 add_stmt (build_stmt (DECL_EXPR, label));
3388 c_parser_consume_token (parser);
3389 if (c_parser_next_token_is (parser, CPP_COMMA))
3390 c_parser_consume_token (parser);
3391 else
3392 break;
3394 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
3396 /* ??? Locating this diagnostic on the token after the
3397 declarations end follows the old parser, but it might be
3398 better to locate it where the declarations start instead. */
3399 if (pedantic)
3400 pedwarn ("ISO C forbids label declarations");
3402 /* We must now have at least one statement, label or declaration. */
3403 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3405 c_parser_error (parser, "expected declaration or statement");
3406 c_parser_consume_token (parser);
3407 return;
3409 while (c_parser_next_token_is_not (parser, CPP_CLOSE_BRACE))
3411 location_t loc = c_parser_peek_token (parser)->location;
3412 if (c_parser_next_token_is_keyword (parser, RID_CASE)
3413 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
3414 || (c_parser_next_token_is (parser, CPP_NAME)
3415 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
3417 last_label = true;
3418 last_stmt = false;
3419 c_parser_label (parser);
3421 else if (!last_label
3422 && c_parser_next_token_starts_declspecs (parser))
3424 last_label = false;
3425 c_parser_declaration_or_fndef (parser, true, true, true, true);
3426 if (last_stmt
3427 && ((pedantic && !flag_isoc99)
3428 || warn_declaration_after_statement))
3429 pedwarn_c90 ("%HISO C90 forbids mixed declarations and code",
3430 &loc);
3431 last_stmt = false;
3433 else if (!last_label
3434 && c_parser_next_token_is_keyword (parser, RID_EXTENSION))
3436 /* __extension__ can start a declaration, but is also an
3437 unary operator that can start an expression. Consume all
3438 but the last of a possible series of __extension__ to
3439 determine which. */
3440 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
3441 && (c_parser_peek_2nd_token (parser)->keyword
3442 == RID_EXTENSION))
3443 c_parser_consume_token (parser);
3444 if (c_token_starts_declspecs (c_parser_peek_2nd_token (parser)))
3446 int ext;
3447 ext = disable_extension_diagnostics ();
3448 c_parser_consume_token (parser);
3449 last_label = false;
3450 c_parser_declaration_or_fndef (parser, true, true, true, true);
3451 /* Following the old parser, __extension__ does not
3452 disable this diagnostic. */
3453 restore_extension_diagnostics (ext);
3454 if (last_stmt
3455 && ((pedantic && !flag_isoc99)
3456 || warn_declaration_after_statement))
3457 pedwarn_c90 ("%HISO C90 forbids mixed declarations and code",
3458 &loc);
3459 last_stmt = false;
3461 else
3462 goto statement;
3464 else if (c_parser_next_token_is (parser, CPP_PRAGMA))
3466 /* External pragmas, and some omp pragmas, are not associated
3467 with regular c code, and so are not to be considered statements
3468 syntactically. This ensures that the user doesn't put them
3469 places that would turn into syntax errors if the directive
3470 were ignored. */
3471 if (c_parser_pragma (parser, pragma_compound))
3472 last_label = false, last_stmt = true;
3474 else if (c_parser_next_token_is (parser, CPP_EOF))
3476 c_parser_error (parser, "expected declaration or statement");
3477 return;
3479 else
3481 statement:
3482 last_label = false;
3483 last_stmt = true;
3484 c_parser_statement_after_labels (parser);
3487 if (last_label)
3488 error ("label at end of compound statement");
3489 c_parser_consume_token (parser);
3492 /* Parse a label (C90 6.6.1, C99 6.8.1).
3494 label:
3495 identifier : attributes[opt]
3496 case constant-expression :
3497 default :
3499 GNU extensions:
3501 label:
3502 case constant-expression ... constant-expression :
3504 The use of attributes on labels is a GNU extension. The syntax in
3505 GNU C accepts any expressions without commas, non-constant
3506 expressions being rejected later. */
3508 static void
3509 c_parser_label (c_parser *parser)
3511 location_t loc1 = c_parser_peek_token (parser)->location;
3512 tree label = NULL_TREE;
3513 if (c_parser_next_token_is_keyword (parser, RID_CASE))
3515 tree exp1, exp2;
3516 c_parser_consume_token (parser);
3517 exp1 = c_parser_expr_no_commas (parser, NULL).value;
3518 if (c_parser_next_token_is (parser, CPP_COLON))
3520 c_parser_consume_token (parser);
3521 label = do_case (exp1, NULL_TREE);
3523 else if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3525 c_parser_consume_token (parser);
3526 exp2 = c_parser_expr_no_commas (parser, NULL).value;
3527 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
3528 label = do_case (exp1, exp2);
3530 else
3531 c_parser_error (parser, "expected %<:%> or %<...%>");
3533 else if (c_parser_next_token_is_keyword (parser, RID_DEFAULT))
3535 c_parser_consume_token (parser);
3536 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
3537 label = do_case (NULL_TREE, NULL_TREE);
3539 else
3541 tree name = c_parser_peek_token (parser)->value;
3542 tree tlab;
3543 location_t loc2;
3544 tree attrs;
3545 gcc_assert (c_parser_next_token_is (parser, CPP_NAME));
3546 c_parser_consume_token (parser);
3547 gcc_assert (c_parser_next_token_is (parser, CPP_COLON));
3548 loc2 = c_parser_peek_token (parser)->location;
3549 c_parser_consume_token (parser);
3550 attrs = c_parser_attributes (parser);
3551 tlab = define_label (loc2, name);
3552 if (tlab)
3554 decl_attributes (&tlab, attrs, 0);
3555 label = add_stmt (build_stmt (LABEL_EXPR, tlab));
3558 if (label)
3559 SET_EXPR_LOCATION (label, loc1);
3562 /* Parse a statement (C90 6.6, C99 6.8).
3564 statement:
3565 labeled-statement
3566 compound-statement
3567 expression-statement
3568 selection-statement
3569 iteration-statement
3570 jump-statement
3572 labeled-statement:
3573 label statement
3575 expression-statement:
3576 expression[opt] ;
3578 selection-statement:
3579 if-statement
3580 switch-statement
3582 iteration-statement:
3583 while-statement
3584 do-statement
3585 for-statement
3587 jump-statement:
3588 goto identifier ;
3589 continue ;
3590 break ;
3591 return expression[opt] ;
3593 GNU extensions:
3595 statement:
3596 asm-statement
3598 jump-statement:
3599 goto * expression ;
3601 Objective-C:
3603 statement:
3604 objc-throw-statement
3605 objc-try-catch-statement
3606 objc-synchronized-statement
3608 objc-throw-statement:
3609 @throw expression ;
3610 @throw ;
3612 OpenMP:
3614 statement:
3615 openmp-construct
3617 openmp-construct:
3618 parallel-construct
3619 for-construct
3620 sections-construct
3621 single-construct
3622 parallel-for-construct
3623 parallel-sections-construct
3624 master-construct
3625 critical-construct
3626 atomic-construct
3627 ordered-construct
3629 parallel-construct:
3630 parallel-directive structured-block
3632 for-construct:
3633 for-directive iteration-statement
3635 sections-construct:
3636 sections-directive section-scope
3638 single-construct:
3639 single-directive structured-block
3641 parallel-for-construct:
3642 parallel-for-directive iteration-statement
3644 parallel-sections-construct:
3645 parallel-sections-directive section-scope
3647 master-construct:
3648 master-directive structured-block
3650 critical-construct:
3651 critical-directive structured-block
3653 atomic-construct:
3654 atomic-directive expression-statement
3656 ordered-construct:
3657 ordered-directive structured-block */
3659 static void
3660 c_parser_statement (c_parser *parser)
3662 while (c_parser_next_token_is_keyword (parser, RID_CASE)
3663 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
3664 || (c_parser_next_token_is (parser, CPP_NAME)
3665 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
3666 c_parser_label (parser);
3667 c_parser_statement_after_labels (parser);
3670 /* Parse a statement, other than a labeled statement. */
3672 static void
3673 c_parser_statement_after_labels (c_parser *parser)
3675 location_t loc = c_parser_peek_token (parser)->location;
3676 tree stmt = NULL_TREE;
3677 switch (c_parser_peek_token (parser)->type)
3679 case CPP_OPEN_BRACE:
3680 add_stmt (c_parser_compound_statement (parser));
3681 break;
3682 case CPP_KEYWORD:
3683 switch (c_parser_peek_token (parser)->keyword)
3685 case RID_IF:
3686 c_parser_if_statement (parser);
3687 break;
3688 case RID_SWITCH:
3689 c_parser_switch_statement (parser);
3690 break;
3691 case RID_WHILE:
3692 c_parser_while_statement (parser);
3693 break;
3694 case RID_DO:
3695 c_parser_do_statement (parser);
3696 break;
3697 case RID_FOR:
3698 c_parser_for_statement (parser);
3699 break;
3700 case RID_GOTO:
3701 c_parser_consume_token (parser);
3702 if (c_parser_next_token_is (parser, CPP_NAME))
3704 stmt = c_finish_goto_label (c_parser_peek_token (parser)->value);
3705 c_parser_consume_token (parser);
3707 else if (c_parser_next_token_is (parser, CPP_MULT))
3709 c_parser_consume_token (parser);
3710 stmt = c_finish_goto_ptr (c_parser_expression (parser).value);
3712 else
3713 c_parser_error (parser, "expected identifier or %<*%>");
3714 goto expect_semicolon;
3715 case RID_CONTINUE:
3716 c_parser_consume_token (parser);
3717 stmt = c_finish_bc_stmt (&c_cont_label, false);
3718 goto expect_semicolon;
3719 case RID_BREAK:
3720 c_parser_consume_token (parser);
3721 stmt = c_finish_bc_stmt (&c_break_label, true);
3722 goto expect_semicolon;
3723 case RID_RETURN:
3724 c_parser_consume_token (parser);
3725 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
3727 stmt = c_finish_return (NULL_TREE);
3728 c_parser_consume_token (parser);
3730 else
3732 stmt = c_finish_return (c_parser_expression_conv (parser).value);
3733 goto expect_semicolon;
3735 break;
3736 case RID_ASM:
3737 stmt = c_parser_asm_statement (parser);
3738 break;
3739 case RID_AT_THROW:
3740 gcc_assert (c_dialect_objc ());
3741 c_parser_consume_token (parser);
3742 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
3744 stmt = objc_build_throw_stmt (NULL_TREE);
3745 c_parser_consume_token (parser);
3747 else
3749 stmt
3750 = objc_build_throw_stmt (c_parser_expression (parser).value);
3751 goto expect_semicolon;
3753 break;
3754 case RID_AT_TRY:
3755 gcc_assert (c_dialect_objc ());
3756 c_parser_objc_try_catch_statement (parser);
3757 break;
3758 case RID_AT_SYNCHRONIZED:
3759 gcc_assert (c_dialect_objc ());
3760 c_parser_objc_synchronized_statement (parser);
3761 break;
3762 default:
3763 goto expr_stmt;
3765 break;
3766 case CPP_SEMICOLON:
3767 c_parser_consume_token (parser);
3768 break;
3769 case CPP_CLOSE_PAREN:
3770 case CPP_CLOSE_SQUARE:
3771 /* Avoid infinite loop in error recovery:
3772 c_parser_skip_until_found stops at a closing nesting
3773 delimiter without consuming it, but here we need to consume
3774 it to proceed further. */
3775 c_parser_error (parser, "expected statement");
3776 c_parser_consume_token (parser);
3777 break;
3778 case CPP_PRAGMA:
3779 c_parser_pragma (parser, pragma_stmt);
3780 break;
3781 default:
3782 expr_stmt:
3783 stmt = c_finish_expr_stmt (c_parser_expression_conv (parser).value);
3784 expect_semicolon:
3785 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
3786 break;
3788 /* Two cases cannot and do not have line numbers associated: If stmt
3789 is degenerate, such as "2;", then stmt is an INTEGER_CST, which
3790 cannot hold line numbers. But that's OK because the statement
3791 will either be changed to a MODIFY_EXPR during gimplification of
3792 the statement expr, or discarded. If stmt was compound, but
3793 without new variables, we will have skipped the creation of a
3794 BIND and will have a bare STATEMENT_LIST. But that's OK because
3795 (recursively) all of the component statements should already have
3796 line numbers assigned. ??? Can we discard no-op statements
3797 earlier? */
3798 if (stmt && EXPR_P (stmt))
3799 SET_EXPR_LOCATION (stmt, loc);
3802 /* Parse a parenthesized condition from an if, do or while statement.
3804 condition:
3805 ( expression )
3807 static tree
3808 c_parser_paren_condition (c_parser *parser)
3810 location_t loc;
3811 tree cond;
3812 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3813 return error_mark_node;
3814 loc = c_parser_peek_token (parser)->location;
3815 cond = c_objc_common_truthvalue_conversion
3816 (c_parser_expression_conv (parser).value);
3817 if (EXPR_P (cond))
3818 SET_EXPR_LOCATION (cond, loc);
3819 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
3820 return cond;
3823 /* Parse a statement which is a block in C99. */
3825 static tree
3826 c_parser_c99_block_statement (c_parser *parser)
3828 tree block = c_begin_compound_stmt (flag_isoc99);
3829 c_parser_statement (parser);
3830 return c_end_compound_stmt (block, flag_isoc99);
3833 /* Parse the body of an if statement or the else half thereof. This
3834 is just parsing a statement but (a) it is a block in C99, (b) we
3835 track whether the body is an if statement for the sake of
3836 -Wparentheses warnings, (c) we handle an empty body specially for
3837 the sake of -Wextra warnings. */
3839 static tree
3840 c_parser_if_body (c_parser *parser, bool *if_p)
3842 tree block = c_begin_compound_stmt (flag_isoc99);
3843 while (c_parser_next_token_is_keyword (parser, RID_CASE)
3844 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
3845 || (c_parser_next_token_is (parser, CPP_NAME)
3846 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
3847 c_parser_label (parser);
3848 *if_p = c_parser_next_token_is_keyword (parser, RID_IF);
3849 if (extra_warnings && c_parser_next_token_is (parser, CPP_SEMICOLON))
3850 add_stmt (build_empty_stmt ());
3851 c_parser_statement_after_labels (parser);
3852 return c_end_compound_stmt (block, flag_isoc99);
3855 /* Parse an if statement (C90 6.6.4, C99 6.8.4).
3857 if-statement:
3858 if ( expression ) statement
3859 if ( expression ) statement else statement
3862 static void
3863 c_parser_if_statement (c_parser *parser)
3865 tree block;
3866 location_t loc;
3867 tree cond;
3868 bool first_if = false, second_if = false;
3869 tree first_body, second_body;
3870 gcc_assert (c_parser_next_token_is_keyword (parser, RID_IF));
3871 c_parser_consume_token (parser);
3872 block = c_begin_compound_stmt (flag_isoc99);
3873 loc = c_parser_peek_token (parser)->location;
3874 cond = c_parser_paren_condition (parser);
3875 first_body = c_parser_if_body (parser, &first_if);
3876 if (c_parser_next_token_is_keyword (parser, RID_ELSE))
3878 c_parser_consume_token (parser);
3879 second_body = c_parser_if_body (parser, &second_if);
3881 else
3882 second_body = NULL_TREE;
3883 c_finish_if_stmt (loc, cond, first_body, second_body, first_if);
3884 add_stmt (c_end_compound_stmt (block, flag_isoc99));
3887 /* Parse a switch statement (C90 6.6.4, C99 6.8.4).
3889 switch-statement:
3890 switch (expression) statement
3893 static void
3894 c_parser_switch_statement (c_parser *parser)
3896 tree block, expr, body, save_break;
3897 gcc_assert (c_parser_next_token_is_keyword (parser, RID_SWITCH));
3898 c_parser_consume_token (parser);
3899 block = c_begin_compound_stmt (flag_isoc99);
3900 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3902 expr = c_parser_expression (parser).value;
3903 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
3905 else
3906 expr = error_mark_node;
3907 c_start_case (expr);
3908 save_break = c_break_label;
3909 c_break_label = NULL_TREE;
3910 body = c_parser_c99_block_statement (parser);
3911 c_finish_case (body);
3912 if (c_break_label)
3913 add_stmt (build1 (LABEL_EXPR, void_type_node, c_break_label));
3914 c_break_label = save_break;
3915 add_stmt (c_end_compound_stmt (block, flag_isoc99));
3918 /* Parse a while statement (C90 6.6.5, C99 6.8.5).
3920 while-statement:
3921 while (expression) statement
3924 static void
3925 c_parser_while_statement (c_parser *parser)
3927 tree block, cond, body, save_break, save_cont;
3928 location_t loc;
3929 gcc_assert (c_parser_next_token_is_keyword (parser, RID_WHILE));
3930 c_parser_consume_token (parser);
3931 block = c_begin_compound_stmt (flag_isoc99);
3932 loc = c_parser_peek_token (parser)->location;
3933 cond = c_parser_paren_condition (parser);
3934 save_break = c_break_label;
3935 c_break_label = NULL_TREE;
3936 save_cont = c_cont_label;
3937 c_cont_label = NULL_TREE;
3938 body = c_parser_c99_block_statement (parser);
3939 c_finish_loop (loc, cond, NULL, body, c_break_label, c_cont_label, true);
3940 add_stmt (c_end_compound_stmt (block, flag_isoc99));
3941 c_break_label = save_break;
3942 c_cont_label = save_cont;
3945 /* Parse a do statement (C90 6.6.5, C99 6.8.5).
3947 do-statement:
3948 do statement while ( expression ) ;
3951 static void
3952 c_parser_do_statement (c_parser *parser)
3954 tree block, cond, body, save_break, save_cont, new_break, new_cont;
3955 location_t loc;
3956 gcc_assert (c_parser_next_token_is_keyword (parser, RID_DO));
3957 c_parser_consume_token (parser);
3958 block = c_begin_compound_stmt (flag_isoc99);
3959 loc = c_parser_peek_token (parser)->location;
3960 save_break = c_break_label;
3961 c_break_label = NULL_TREE;
3962 save_cont = c_cont_label;
3963 c_cont_label = NULL_TREE;
3964 body = c_parser_c99_block_statement (parser);
3965 c_parser_require_keyword (parser, RID_WHILE, "expected %<while%>");
3966 new_break = c_break_label;
3967 c_break_label = save_break;
3968 new_cont = c_cont_label;
3969 c_cont_label = save_cont;
3970 cond = c_parser_paren_condition (parser);
3971 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
3972 c_parser_skip_to_end_of_block_or_statement (parser);
3973 c_finish_loop (loc, cond, NULL, body, new_break, new_cont, false);
3974 add_stmt (c_end_compound_stmt (block, flag_isoc99));
3977 /* Parse a for statement (C90 6.6.5, C99 6.8.5).
3979 for-statement:
3980 for ( expression[opt] ; expression[opt] ; expression[opt] ) statement
3981 for ( nested-declaration expression[opt] ; expression[opt] ) statement
3983 The form with a declaration is new in C99.
3985 ??? In accordance with the old parser, the declaration may be a
3986 nested function, which is then rejected in check_for_loop_decls,
3987 but does it make any sense for this to be included in the grammar?
3988 Note in particular that the nested function does not include a
3989 trailing ';', whereas the "declaration" production includes one.
3990 Also, can we reject bad declarations earlier and cheaper than
3991 check_for_loop_decls? */
3993 static void
3994 c_parser_for_statement (c_parser *parser)
3996 tree block, cond, incr, save_break, save_cont, body;
3997 location_t loc;
3998 gcc_assert (c_parser_next_token_is_keyword (parser, RID_FOR));
3999 loc = c_parser_peek_token (parser)->location;
4000 c_parser_consume_token (parser);
4001 block = c_begin_compound_stmt (flag_isoc99);
4002 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4004 /* Parse the initialization declaration or expression. */
4005 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4007 c_parser_consume_token (parser);
4008 c_finish_expr_stmt (NULL_TREE);
4010 else if (c_parser_next_token_starts_declspecs (parser))
4012 c_parser_declaration_or_fndef (parser, true, true, true, true);
4013 check_for_loop_decls ();
4015 else if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
4017 /* __extension__ can start a declaration, but is also an
4018 unary operator that can start an expression. Consume all
4019 but the last of a possible series of __extension__ to
4020 determine which. */
4021 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
4022 && (c_parser_peek_2nd_token (parser)->keyword
4023 == RID_EXTENSION))
4024 c_parser_consume_token (parser);
4025 if (c_token_starts_declspecs (c_parser_peek_2nd_token (parser)))
4027 int ext;
4028 ext = disable_extension_diagnostics ();
4029 c_parser_consume_token (parser);
4030 c_parser_declaration_or_fndef (parser, true, true, true, true);
4031 restore_extension_diagnostics (ext);
4032 check_for_loop_decls ();
4034 else
4035 goto init_expr;
4037 else
4039 init_expr:
4040 c_finish_expr_stmt (c_parser_expression (parser).value);
4041 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
4043 /* Parse the loop condition. */
4044 loc = c_parser_peek_token (parser)->location;
4045 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4047 c_parser_consume_token (parser);
4048 cond = NULL_TREE;
4050 else
4052 tree ocond = c_parser_expression_conv (parser).value;
4053 cond = c_objc_common_truthvalue_conversion (ocond);
4054 if (EXPR_P (cond))
4055 SET_EXPR_LOCATION (cond, loc);
4056 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
4058 /* Parse the increment expression. */
4059 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4060 incr = c_process_expr_stmt (NULL_TREE);
4061 else
4062 incr = c_process_expr_stmt (c_parser_expression (parser).value);
4063 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
4065 else
4067 cond = error_mark_node;
4068 incr = error_mark_node;
4070 save_break = c_break_label;
4071 c_break_label = NULL_TREE;
4072 save_cont = c_cont_label;
4073 c_cont_label = NULL_TREE;
4074 body = c_parser_c99_block_statement (parser);
4075 c_finish_loop (loc, cond, incr, body, c_break_label, c_cont_label, true);
4076 add_stmt (c_end_compound_stmt (block, flag_isoc99));
4077 c_break_label = save_break;
4078 c_cont_label = save_cont;
4081 /* Parse an asm statement, a GNU extension. This is a full-blown asm
4082 statement with inputs, outputs, clobbers, and volatile tag
4083 allowed.
4085 asm-statement:
4086 asm type-qualifier[opt] ( asm-argument ) ;
4088 asm-argument:
4089 asm-string-literal
4090 asm-string-literal : asm-operands[opt]
4091 asm-string-literal : asm-operands[opt] : asm-operands[opt]
4092 asm-string-literal : asm-operands[opt] : asm-operands[opt] : asm-clobbers
4094 Qualifiers other than volatile are accepted in the syntax but
4095 warned for. */
4097 static tree
4098 c_parser_asm_statement (c_parser *parser)
4100 tree quals, str, outputs, inputs, clobbers, ret;
4101 bool simple;
4102 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
4103 c_parser_consume_token (parser);
4104 if (c_parser_next_token_is_keyword (parser, RID_VOLATILE))
4106 quals = c_parser_peek_token (parser)->value;
4107 c_parser_consume_token (parser);
4109 else if (c_parser_next_token_is_keyword (parser, RID_CONST)
4110 || c_parser_next_token_is_keyword (parser, RID_RESTRICT))
4112 warning (0, "%E qualifier ignored on asm",
4113 c_parser_peek_token (parser)->value);
4114 quals = NULL_TREE;
4115 c_parser_consume_token (parser);
4117 else
4118 quals = NULL_TREE;
4119 /* ??? Follow the C++ parser rather than using the
4120 c_lex_string_translate kludge. */
4121 c_lex_string_translate = 0;
4122 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4124 c_lex_string_translate = 1;
4125 return NULL_TREE;
4127 str = c_parser_asm_string_literal (parser);
4128 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4130 simple = true;
4131 outputs = NULL_TREE;
4132 inputs = NULL_TREE;
4133 clobbers = NULL_TREE;
4134 goto done_asm;
4136 if (!c_parser_require (parser, CPP_COLON, "expected %<:%> or %<)%>"))
4138 c_lex_string_translate = 1;
4139 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4140 return NULL_TREE;
4142 simple = false;
4143 /* Parse outputs. */
4144 if (c_parser_next_token_is (parser, CPP_COLON)
4145 || c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4146 outputs = NULL_TREE;
4147 else
4148 outputs = c_parser_asm_operands (parser, false);
4149 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4151 inputs = NULL_TREE;
4152 clobbers = NULL_TREE;
4153 goto done_asm;
4155 if (!c_parser_require (parser, CPP_COLON, "expected %<:%> or %<)%>"))
4157 c_lex_string_translate = 1;
4158 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4159 return NULL_TREE;
4161 /* Parse inputs. */
4162 if (c_parser_next_token_is (parser, CPP_COLON)
4163 || c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4164 inputs = NULL_TREE;
4165 else
4166 inputs = c_parser_asm_operands (parser, true);
4167 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4169 clobbers = NULL_TREE;
4170 goto done_asm;
4172 if (!c_parser_require (parser, CPP_COLON, "expected %<:%> or %<)%>"))
4174 c_lex_string_translate = 1;
4175 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4176 return NULL_TREE;
4178 /* Parse clobbers. */
4179 clobbers = c_parser_asm_clobbers (parser);
4180 done_asm:
4181 c_lex_string_translate = 1;
4182 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
4184 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4185 return NULL_TREE;
4187 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
4188 c_parser_skip_to_end_of_block_or_statement (parser);
4189 ret = build_asm_stmt (quals, build_asm_expr (str, outputs, inputs,
4190 clobbers, simple));
4191 return ret;
4194 /* Parse asm operands, a GNU extension. If CONVERT_P (for inputs but
4195 not outputs), apply the default conversion of functions and arrays
4196 to pointers.
4198 asm-operands:
4199 asm-operand
4200 asm-operands , asm-operand
4202 asm-operand:
4203 asm-string-literal ( expression )
4204 [ identifier ] asm-string-literal ( expression )
4207 static tree
4208 c_parser_asm_operands (c_parser *parser, bool convert_p)
4210 tree list = NULL_TREE;
4211 while (true)
4213 tree name, str;
4214 struct c_expr expr;
4215 if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
4217 c_parser_consume_token (parser);
4218 if (c_parser_next_token_is (parser, CPP_NAME))
4220 tree id = c_parser_peek_token (parser)->value;
4221 c_parser_consume_token (parser);
4222 name = build_string (IDENTIFIER_LENGTH (id),
4223 IDENTIFIER_POINTER (id));
4225 else
4227 c_parser_error (parser, "expected identifier");
4228 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
4229 return NULL_TREE;
4231 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
4232 "expected %<]%>");
4234 else
4235 name = NULL_TREE;
4236 str = c_parser_asm_string_literal (parser);
4237 if (str == NULL_TREE)
4238 return NULL_TREE;
4239 c_lex_string_translate = 1;
4240 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4242 c_lex_string_translate = 0;
4243 return NULL_TREE;
4245 expr = c_parser_expression (parser);
4246 if (convert_p)
4247 expr = default_function_array_conversion (expr);
4248 c_lex_string_translate = 0;
4249 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
4251 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4252 return NULL_TREE;
4254 list = chainon (list, build_tree_list (build_tree_list (name, str),
4255 expr.value));
4256 if (c_parser_next_token_is (parser, CPP_COMMA))
4257 c_parser_consume_token (parser);
4258 else
4259 break;
4261 return list;
4264 /* Parse asm clobbers, a GNU extension.
4266 asm-clobbers:
4267 asm-string-literal
4268 asm-clobbers , asm-string-literal
4271 static tree
4272 c_parser_asm_clobbers (c_parser *parser)
4274 tree list = NULL_TREE;
4275 while (true)
4277 tree str = c_parser_asm_string_literal (parser);
4278 if (str)
4279 list = tree_cons (NULL_TREE, str, list);
4280 else
4281 return NULL_TREE;
4282 if (c_parser_next_token_is (parser, CPP_COMMA))
4283 c_parser_consume_token (parser);
4284 else
4285 break;
4287 return list;
4290 /* Parse an expression other than a compound expression; that is, an
4291 assignment expression (C90 6.3.16, C99 6.5.16). If AFTER is not
4292 NULL then it is an Objective-C message expression which is the
4293 primary-expression starting the expression as an initializer.
4295 assignment-expression:
4296 conditional-expression
4297 unary-expression assignment-operator assignment-expression
4299 assignment-operator: one of
4300 = *= /= %= += -= <<= >>= &= ^= |=
4302 In GNU C we accept any conditional expression on the LHS and
4303 diagnose the invalid lvalue rather than producing a syntax
4304 error. */
4306 static struct c_expr
4307 c_parser_expr_no_commas (c_parser *parser, struct c_expr *after)
4309 struct c_expr lhs, rhs, ret;
4310 enum tree_code code;
4311 gcc_assert (!after || c_dialect_objc ());
4312 lhs = c_parser_conditional_expression (parser, after);
4313 switch (c_parser_peek_token (parser)->type)
4315 case CPP_EQ:
4316 code = NOP_EXPR;
4317 break;
4318 case CPP_MULT_EQ:
4319 code = MULT_EXPR;
4320 break;
4321 case CPP_DIV_EQ:
4322 code = TRUNC_DIV_EXPR;
4323 break;
4324 case CPP_MOD_EQ:
4325 code = TRUNC_MOD_EXPR;
4326 break;
4327 case CPP_PLUS_EQ:
4328 code = PLUS_EXPR;
4329 break;
4330 case CPP_MINUS_EQ:
4331 code = MINUS_EXPR;
4332 break;
4333 case CPP_LSHIFT_EQ:
4334 code = LSHIFT_EXPR;
4335 break;
4336 case CPP_RSHIFT_EQ:
4337 code = RSHIFT_EXPR;
4338 break;
4339 case CPP_AND_EQ:
4340 code = BIT_AND_EXPR;
4341 break;
4342 case CPP_XOR_EQ:
4343 code = BIT_XOR_EXPR;
4344 break;
4345 case CPP_OR_EQ:
4346 code = BIT_IOR_EXPR;
4347 break;
4348 default:
4349 return lhs;
4351 c_parser_consume_token (parser);
4352 rhs = c_parser_expr_no_commas (parser, NULL);
4353 rhs = default_function_array_conversion (rhs);
4354 ret.value = build_modify_expr (lhs.value, code, rhs.value);
4355 if (code == NOP_EXPR)
4356 ret.original_code = MODIFY_EXPR;
4357 else
4359 TREE_NO_WARNING (ret.value) = 1;
4360 ret.original_code = ERROR_MARK;
4362 return ret;
4365 /* Parse a conditional expression (C90 6.3.15, C99 6.5.15). If AFTER
4366 is not NULL then it is an Objective-C message expression which is
4367 the primary-expression starting the expression as an initializer.
4369 conditional-expression:
4370 logical-OR-expression
4371 logical-OR-expression ? expression : conditional-expression
4373 GNU extensions:
4375 conditional-expression:
4376 logical-OR-expression ? : conditional-expression
4379 static struct c_expr
4380 c_parser_conditional_expression (c_parser *parser, struct c_expr *after)
4382 struct c_expr cond, exp1, exp2, ret;
4383 gcc_assert (!after || c_dialect_objc ());
4384 cond = c_parser_binary_expression (parser, after);
4385 if (c_parser_next_token_is_not (parser, CPP_QUERY))
4386 return cond;
4387 cond = default_function_array_conversion (cond);
4388 c_parser_consume_token (parser);
4389 if (c_parser_next_token_is (parser, CPP_COLON))
4391 if (pedantic)
4392 pedwarn ("ISO C forbids omitting the middle term of a ?: expression");
4393 /* Make sure first operand is calculated only once. */
4394 exp1.value = save_expr (default_conversion (cond.value));
4395 cond.value = c_objc_common_truthvalue_conversion (exp1.value);
4396 skip_evaluation += cond.value == truthvalue_true_node;
4398 else
4400 cond.value
4401 = c_objc_common_truthvalue_conversion
4402 (default_conversion (cond.value));
4403 skip_evaluation += cond.value == truthvalue_false_node;
4404 exp1 = c_parser_expression_conv (parser);
4405 skip_evaluation += ((cond.value == truthvalue_true_node)
4406 - (cond.value == truthvalue_false_node));
4408 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
4410 skip_evaluation -= cond.value == truthvalue_true_node;
4411 ret.value = error_mark_node;
4412 ret.original_code = ERROR_MARK;
4413 return ret;
4415 exp2 = c_parser_conditional_expression (parser, NULL);
4416 exp2 = default_function_array_conversion (exp2);
4417 skip_evaluation -= cond.value == truthvalue_true_node;
4418 ret.value = build_conditional_expr (cond.value, exp1.value, exp2.value);
4419 ret.original_code = ERROR_MARK;
4420 return ret;
4423 /* Parse a binary expression; that is, a logical-OR-expression (C90
4424 6.3.5-6.3.14, C99 6.5.5-6.5.14). If AFTER is not NULL then it is
4425 an Objective-C message expression which is the primary-expression
4426 starting the expression as an initializer.
4428 multiplicative-expression:
4429 cast-expression
4430 multiplicative-expression * cast-expression
4431 multiplicative-expression / cast-expression
4432 multiplicative-expression % cast-expression
4434 additive-expression:
4435 multiplicative-expression
4436 additive-expression + multiplicative-expression
4437 additive-expression - multiplicative-expression
4439 shift-expression:
4440 additive-expression
4441 shift-expression << additive-expression
4442 shift-expression >> additive-expression
4444 relational-expression:
4445 shift-expression
4446 relational-expression < shift-expression
4447 relational-expression > shift-expression
4448 relational-expression <= shift-expression
4449 relational-expression >= shift-expression
4451 equality-expression:
4452 relational-expression
4453 equality-expression == relational-expression
4454 equality-expression != relational-expression
4456 AND-expression:
4457 equality-expression
4458 AND-expression & equality-expression
4460 exclusive-OR-expression:
4461 AND-expression
4462 exclusive-OR-expression ^ AND-expression
4464 inclusive-OR-expression:
4465 exclusive-OR-expression
4466 inclusive-OR-expression | exclusive-OR-expression
4468 logical-AND-expression:
4469 inclusive-OR-expression
4470 logical-AND-expression && inclusive-OR-expression
4472 logical-OR-expression:
4473 logical-AND-expression
4474 logical-OR-expression || logical-AND-expression
4477 static struct c_expr
4478 c_parser_binary_expression (c_parser *parser, struct c_expr *after)
4480 /* A binary expression is parsed using operator-precedence parsing,
4481 with the operands being cast expressions. All the binary
4482 operators are left-associative. Thus a binary expression is of
4483 form:
4485 E0 op1 E1 op2 E2 ...
4487 which we represent on a stack. On the stack, the precedence
4488 levels are strictly increasing. When a new operator is
4489 encountered of higher precedence than that at the top of the
4490 stack, it is pushed; its LHS is the top expression, and its RHS
4491 is everything parsed until it is popped. When a new operator is
4492 encountered with precedence less than or equal to that at the top
4493 of the stack, triples E[i-1] op[i] E[i] are popped and replaced
4494 by the result of the operation until the operator at the top of
4495 the stack has lower precedence than the new operator or there is
4496 only one element on the stack; then the top expression is the LHS
4497 of the new operator. In the case of logical AND and OR
4498 expressions, we also need to adjust skip_evaluation as
4499 appropriate when the operators are pushed and popped. */
4501 /* The precedence levels, where 0 is a dummy lowest level used for
4502 the bottom of the stack. */
4503 enum prec {
4504 PREC_NONE,
4505 PREC_LOGOR,
4506 PREC_LOGAND,
4507 PREC_BITOR,
4508 PREC_BITXOR,
4509 PREC_BITAND,
4510 PREC_EQ,
4511 PREC_REL,
4512 PREC_SHIFT,
4513 PREC_ADD,
4514 PREC_MULT,
4515 NUM_PRECS
4517 struct {
4518 /* The expression at this stack level. */
4519 struct c_expr expr;
4520 /* The precedence of the operator on its left, PREC_NONE at the
4521 bottom of the stack. */
4522 enum prec prec;
4523 /* The operation on its left. */
4524 enum tree_code op;
4525 } stack[NUM_PRECS];
4526 int sp;
4527 #define POP \
4528 do { \
4529 switch (stack[sp].op) \
4531 case TRUTH_ANDIF_EXPR: \
4532 skip_evaluation -= stack[sp - 1].expr.value == truthvalue_false_node; \
4533 break; \
4534 case TRUTH_ORIF_EXPR: \
4535 skip_evaluation -= stack[sp - 1].expr.value == truthvalue_true_node; \
4536 break; \
4537 default: \
4538 break; \
4540 stack[sp - 1].expr \
4541 = default_function_array_conversion (stack[sp - 1].expr); \
4542 stack[sp].expr \
4543 = default_function_array_conversion (stack[sp].expr); \
4544 stack[sp - 1].expr = parser_build_binary_op (stack[sp].op, \
4545 stack[sp - 1].expr, \
4546 stack[sp].expr); \
4547 sp--; \
4548 } while (0)
4549 gcc_assert (!after || c_dialect_objc ());
4550 stack[0].expr = c_parser_cast_expression (parser, after);
4551 stack[0].prec = PREC_NONE;
4552 sp = 0;
4553 while (true)
4555 enum prec oprec;
4556 enum tree_code ocode;
4557 if (parser->error)
4558 goto out;
4559 switch (c_parser_peek_token (parser)->type)
4561 case CPP_MULT:
4562 oprec = PREC_MULT;
4563 ocode = MULT_EXPR;
4564 break;
4565 case CPP_DIV:
4566 oprec = PREC_MULT;
4567 ocode = TRUNC_DIV_EXPR;
4568 break;
4569 case CPP_MOD:
4570 oprec = PREC_MULT;
4571 ocode = TRUNC_MOD_EXPR;
4572 break;
4573 case CPP_PLUS:
4574 oprec = PREC_ADD;
4575 ocode = PLUS_EXPR;
4576 break;
4577 case CPP_MINUS:
4578 oprec = PREC_ADD;
4579 ocode = MINUS_EXPR;
4580 break;
4581 case CPP_LSHIFT:
4582 oprec = PREC_SHIFT;
4583 ocode = LSHIFT_EXPR;
4584 break;
4585 case CPP_RSHIFT:
4586 oprec = PREC_SHIFT;
4587 ocode = RSHIFT_EXPR;
4588 break;
4589 case CPP_LESS:
4590 oprec = PREC_REL;
4591 ocode = LT_EXPR;
4592 break;
4593 case CPP_GREATER:
4594 oprec = PREC_REL;
4595 ocode = GT_EXPR;
4596 break;
4597 case CPP_LESS_EQ:
4598 oprec = PREC_REL;
4599 ocode = LE_EXPR;
4600 break;
4601 case CPP_GREATER_EQ:
4602 oprec = PREC_REL;
4603 ocode = GE_EXPR;
4604 break;
4605 case CPP_EQ_EQ:
4606 oprec = PREC_EQ;
4607 ocode = EQ_EXPR;
4608 break;
4609 case CPP_NOT_EQ:
4610 oprec = PREC_EQ;
4611 ocode = NE_EXPR;
4612 break;
4613 case CPP_AND:
4614 oprec = PREC_BITAND;
4615 ocode = BIT_AND_EXPR;
4616 break;
4617 case CPP_XOR:
4618 oprec = PREC_BITXOR;
4619 ocode = BIT_XOR_EXPR;
4620 break;
4621 case CPP_OR:
4622 oprec = PREC_BITOR;
4623 ocode = BIT_IOR_EXPR;
4624 break;
4625 case CPP_AND_AND:
4626 oprec = PREC_LOGAND;
4627 ocode = TRUTH_ANDIF_EXPR;
4628 break;
4629 case CPP_OR_OR:
4630 oprec = PREC_LOGOR;
4631 ocode = TRUTH_ORIF_EXPR;
4632 break;
4633 default:
4634 /* Not a binary operator, so end of the binary
4635 expression. */
4636 goto out;
4638 c_parser_consume_token (parser);
4639 while (oprec <= stack[sp].prec)
4640 POP;
4641 switch (ocode)
4643 case TRUTH_ANDIF_EXPR:
4644 stack[sp].expr
4645 = default_function_array_conversion (stack[sp].expr);
4646 stack[sp].expr.value = c_objc_common_truthvalue_conversion
4647 (default_conversion (stack[sp].expr.value));
4648 skip_evaluation += stack[sp].expr.value == truthvalue_false_node;
4649 break;
4650 case TRUTH_ORIF_EXPR:
4651 stack[sp].expr
4652 = default_function_array_conversion (stack[sp].expr);
4653 stack[sp].expr.value = c_objc_common_truthvalue_conversion
4654 (default_conversion (stack[sp].expr.value));
4655 skip_evaluation += stack[sp].expr.value == truthvalue_true_node;
4656 break;
4657 default:
4658 break;
4660 sp++;
4661 stack[sp].expr = c_parser_cast_expression (parser, NULL);
4662 stack[sp].prec = oprec;
4663 stack[sp].op = ocode;
4665 out:
4666 while (sp > 0)
4667 POP;
4668 return stack[0].expr;
4669 #undef POP
4672 /* Parse a cast expression (C90 6.3.4, C99 6.5.4). If AFTER is not
4673 NULL then it is an Objective-C message expression which is the
4674 primary-expression starting the expression as an initializer.
4676 cast-expression:
4677 unary-expression
4678 ( type-name ) unary-expression
4681 static struct c_expr
4682 c_parser_cast_expression (c_parser *parser, struct c_expr *after)
4684 gcc_assert (!after || c_dialect_objc ());
4685 if (after)
4686 return c_parser_postfix_expression_after_primary (parser, *after);
4687 /* If the expression begins with a parenthesized type name, it may
4688 be either a cast or a compound literal; we need to see whether
4689 the next character is '{' to tell the difference. If not, it is
4690 an unary expression. */
4691 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
4692 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
4694 struct c_type_name *type_name;
4695 struct c_expr ret;
4696 struct c_expr expr;
4697 c_parser_consume_token (parser);
4698 type_name = c_parser_type_name (parser);
4699 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
4700 if (type_name == NULL)
4702 ret.value = error_mark_node;
4703 ret.original_code = ERROR_MARK;
4704 return ret;
4707 /* Save casted types in the function's used types hash table. */
4708 used_types_insert (type_name->specs->type);
4710 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
4711 return c_parser_postfix_expression_after_paren_type (parser,
4712 type_name);
4713 expr = c_parser_cast_expression (parser, NULL);
4714 expr = default_function_array_conversion (expr);
4715 ret.value = c_cast_expr (type_name, expr.value);
4716 ret.original_code = ERROR_MARK;
4717 return ret;
4719 else
4720 return c_parser_unary_expression (parser);
4723 /* Parse an unary expression (C90 6.3.3, C99 6.5.3).
4725 unary-expression:
4726 postfix-expression
4727 ++ unary-expression
4728 -- unary-expression
4729 unary-operator cast-expression
4730 sizeof unary-expression
4731 sizeof ( type-name )
4733 unary-operator: one of
4734 & * + - ~ !
4736 GNU extensions:
4738 unary-expression:
4739 __alignof__ unary-expression
4740 __alignof__ ( type-name )
4741 && identifier
4743 unary-operator: one of
4744 __extension__ __real__ __imag__
4746 In addition, the GNU syntax treats ++ and -- as unary operators, so
4747 they may be applied to cast expressions with errors for non-lvalues
4748 given later. */
4750 static struct c_expr
4751 c_parser_unary_expression (c_parser *parser)
4753 int ext;
4754 struct c_expr ret, op;
4755 switch (c_parser_peek_token (parser)->type)
4757 case CPP_PLUS_PLUS:
4758 c_parser_consume_token (parser);
4759 op = c_parser_cast_expression (parser, NULL);
4760 op = default_function_array_conversion (op);
4761 return parser_build_unary_op (PREINCREMENT_EXPR, op);
4762 case CPP_MINUS_MINUS:
4763 c_parser_consume_token (parser);
4764 op = c_parser_cast_expression (parser, NULL);
4765 op = default_function_array_conversion (op);
4766 return parser_build_unary_op (PREDECREMENT_EXPR, op);
4767 case CPP_AND:
4768 c_parser_consume_token (parser);
4769 return parser_build_unary_op (ADDR_EXPR,
4770 c_parser_cast_expression (parser, NULL));
4771 case CPP_MULT:
4772 c_parser_consume_token (parser);
4773 op = c_parser_cast_expression (parser, NULL);
4774 op = default_function_array_conversion (op);
4775 ret.value = build_indirect_ref (op.value, "unary *");
4776 ret.original_code = ERROR_MARK;
4777 return ret;
4778 case CPP_PLUS:
4779 c_parser_consume_token (parser);
4780 if (!c_dialect_objc () && !in_system_header)
4781 warning (OPT_Wtraditional,
4782 "traditional C rejects the unary plus operator");
4783 op = c_parser_cast_expression (parser, NULL);
4784 op = default_function_array_conversion (op);
4785 return parser_build_unary_op (CONVERT_EXPR, op);
4786 case CPP_MINUS:
4787 c_parser_consume_token (parser);
4788 op = c_parser_cast_expression (parser, NULL);
4789 op = default_function_array_conversion (op);
4790 return parser_build_unary_op (NEGATE_EXPR, op);
4791 case CPP_COMPL:
4792 c_parser_consume_token (parser);
4793 op = c_parser_cast_expression (parser, NULL);
4794 op = default_function_array_conversion (op);
4795 return parser_build_unary_op (BIT_NOT_EXPR, op);
4796 case CPP_NOT:
4797 c_parser_consume_token (parser);
4798 op = c_parser_cast_expression (parser, NULL);
4799 op = default_function_array_conversion (op);
4800 return parser_build_unary_op (TRUTH_NOT_EXPR, op);
4801 case CPP_AND_AND:
4802 /* Refer to the address of a label as a pointer. */
4803 c_parser_consume_token (parser);
4804 if (c_parser_next_token_is (parser, CPP_NAME))
4806 ret.value = finish_label_address_expr
4807 (c_parser_peek_token (parser)->value);
4808 c_parser_consume_token (parser);
4810 else
4812 c_parser_error (parser, "expected identifier");
4813 ret.value = error_mark_node;
4815 ret.original_code = ERROR_MARK;
4816 return ret;
4817 case CPP_KEYWORD:
4818 switch (c_parser_peek_token (parser)->keyword)
4820 case RID_SIZEOF:
4821 return c_parser_sizeof_expression (parser);
4822 case RID_ALIGNOF:
4823 return c_parser_alignof_expression (parser);
4824 case RID_EXTENSION:
4825 c_parser_consume_token (parser);
4826 ext = disable_extension_diagnostics ();
4827 ret = c_parser_cast_expression (parser, NULL);
4828 restore_extension_diagnostics (ext);
4829 return ret;
4830 case RID_REALPART:
4831 c_parser_consume_token (parser);
4832 op = c_parser_cast_expression (parser, NULL);
4833 op = default_function_array_conversion (op);
4834 return parser_build_unary_op (REALPART_EXPR, op);
4835 case RID_IMAGPART:
4836 c_parser_consume_token (parser);
4837 op = c_parser_cast_expression (parser, NULL);
4838 op = default_function_array_conversion (op);
4839 return parser_build_unary_op (IMAGPART_EXPR, op);
4840 default:
4841 return c_parser_postfix_expression (parser);
4843 default:
4844 return c_parser_postfix_expression (parser);
4848 /* Parse a sizeof expression. */
4850 static struct c_expr
4851 c_parser_sizeof_expression (c_parser *parser)
4853 struct c_expr expr;
4854 gcc_assert (c_parser_next_token_is_keyword (parser, RID_SIZEOF));
4855 c_parser_consume_token (parser);
4856 skip_evaluation++;
4857 in_sizeof++;
4858 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
4859 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
4861 /* Either sizeof ( type-name ) or sizeof unary-expression
4862 starting with a compound literal. */
4863 struct c_type_name *type_name;
4864 c_parser_consume_token (parser);
4865 type_name = c_parser_type_name (parser);
4866 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
4867 if (type_name == NULL)
4869 struct c_expr ret;
4870 skip_evaluation--;
4871 in_sizeof--;
4872 ret.value = error_mark_node;
4873 ret.original_code = ERROR_MARK;
4874 return ret;
4876 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
4878 expr = c_parser_postfix_expression_after_paren_type (parser,
4879 type_name);
4880 goto sizeof_expr;
4882 /* sizeof ( type-name ). */
4883 skip_evaluation--;
4884 in_sizeof--;
4885 if (type_name->declarator->kind == cdk_array
4886 && type_name->declarator->u.array.vla_unspec_p)
4888 /* C99 6.7.5.2p4 */
4889 error ("%<[*]%> not allowed in other than a declaration");
4891 return c_expr_sizeof_type (type_name);
4893 else
4895 expr = c_parser_unary_expression (parser);
4896 sizeof_expr:
4897 skip_evaluation--;
4898 in_sizeof--;
4899 if (TREE_CODE (expr.value) == COMPONENT_REF
4900 && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
4901 error ("%<sizeof%> applied to a bit-field");
4902 return c_expr_sizeof_expr (expr);
4906 /* Parse an alignof expression. */
4908 static struct c_expr
4909 c_parser_alignof_expression (c_parser *parser)
4911 struct c_expr expr;
4912 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ALIGNOF));
4913 c_parser_consume_token (parser);
4914 skip_evaluation++;
4915 in_alignof++;
4916 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
4917 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
4919 /* Either __alignof__ ( type-name ) or __alignof__
4920 unary-expression starting with a compound literal. */
4921 struct c_type_name *type_name;
4922 struct c_expr ret;
4923 c_parser_consume_token (parser);
4924 type_name = c_parser_type_name (parser);
4925 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
4926 if (type_name == NULL)
4928 struct c_expr ret;
4929 skip_evaluation--;
4930 in_alignof--;
4931 ret.value = error_mark_node;
4932 ret.original_code = ERROR_MARK;
4933 return ret;
4935 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
4937 expr = c_parser_postfix_expression_after_paren_type (parser,
4938 type_name);
4939 goto alignof_expr;
4941 /* alignof ( type-name ). */
4942 skip_evaluation--;
4943 in_alignof--;
4944 ret.value = c_alignof (groktypename (type_name));
4945 ret.original_code = ERROR_MARK;
4946 return ret;
4948 else
4950 struct c_expr ret;
4951 expr = c_parser_unary_expression (parser);
4952 alignof_expr:
4953 skip_evaluation--;
4954 in_alignof--;
4955 ret.value = c_alignof_expr (expr.value);
4956 ret.original_code = ERROR_MARK;
4957 return ret;
4961 /* Parse a postfix expression (C90 6.3.1-6.3.2, C99 6.5.1-6.5.2).
4963 postfix-expression:
4964 primary-expression
4965 postfix-expression [ expression ]
4966 postfix-expression ( argument-expression-list[opt] )
4967 postfix-expression . identifier
4968 postfix-expression -> identifier
4969 postfix-expression ++
4970 postfix-expression --
4971 ( type-name ) { initializer-list }
4972 ( type-name ) { initializer-list , }
4974 argument-expression-list:
4975 argument-expression
4976 argument-expression-list , argument-expression
4978 primary-expression:
4979 identifier
4980 constant
4981 string-literal
4982 ( expression )
4984 GNU extensions:
4986 primary-expression:
4987 __func__
4988 (treated as a keyword in GNU C)
4989 __FUNCTION__
4990 __PRETTY_FUNCTION__
4991 ( compound-statement )
4992 __builtin_va_arg ( assignment-expression , type-name )
4993 __builtin_offsetof ( type-name , offsetof-member-designator )
4994 __builtin_choose_expr ( assignment-expression ,
4995 assignment-expression ,
4996 assignment-expression )
4997 __builtin_types_compatible_p ( type-name , type-name )
4999 offsetof-member-designator:
5000 identifier
5001 offsetof-member-designator . identifier
5002 offsetof-member-designator [ expression ]
5004 Objective-C:
5006 primary-expression:
5007 [ objc-receiver objc-message-args ]
5008 @selector ( objc-selector-arg )
5009 @protocol ( identifier )
5010 @encode ( type-name )
5011 objc-string-literal
5014 static struct c_expr
5015 c_parser_postfix_expression (c_parser *parser)
5017 struct c_expr expr, e1, e2, e3;
5018 struct c_type_name *t1, *t2;
5019 switch (c_parser_peek_token (parser)->type)
5021 case CPP_NUMBER:
5022 case CPP_CHAR:
5023 case CPP_WCHAR:
5024 expr.value = c_parser_peek_token (parser)->value;
5025 expr.original_code = ERROR_MARK;
5026 c_parser_consume_token (parser);
5027 break;
5028 case CPP_STRING:
5029 case CPP_WSTRING:
5030 expr.value = c_parser_peek_token (parser)->value;
5031 expr.original_code = STRING_CST;
5032 c_parser_consume_token (parser);
5033 break;
5034 case CPP_OBJC_STRING:
5035 gcc_assert (c_dialect_objc ());
5036 expr.value
5037 = objc_build_string_object (c_parser_peek_token (parser)->value);
5038 expr.original_code = ERROR_MARK;
5039 c_parser_consume_token (parser);
5040 break;
5041 case CPP_NAME:
5042 if (c_parser_peek_token (parser)->id_kind != C_ID_ID)
5044 c_parser_error (parser, "expected expression");
5045 expr.value = error_mark_node;
5046 expr.original_code = ERROR_MARK;
5047 break;
5050 tree id = c_parser_peek_token (parser)->value;
5051 location_t loc = c_parser_peek_token (parser)->location;
5052 c_parser_consume_token (parser);
5053 expr.value = build_external_ref (id,
5054 (c_parser_peek_token (parser)->type
5055 == CPP_OPEN_PAREN), loc);
5056 expr.original_code = ERROR_MARK;
5058 break;
5059 case CPP_OPEN_PAREN:
5060 /* A parenthesized expression, statement expression or compound
5061 literal. */
5062 if (c_parser_peek_2nd_token (parser)->type == CPP_OPEN_BRACE)
5064 /* A statement expression. */
5065 tree stmt;
5066 c_parser_consume_token (parser);
5067 c_parser_consume_token (parser);
5068 if (cur_stmt_list == NULL)
5070 error ("braced-group within expression allowed "
5071 "only inside a function");
5072 parser->error = true;
5073 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
5074 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5075 expr.value = error_mark_node;
5076 expr.original_code = ERROR_MARK;
5077 break;
5079 stmt = c_begin_stmt_expr ();
5080 c_parser_compound_statement_nostart (parser);
5081 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5082 "expected %<)%>");
5083 if (pedantic)
5084 pedwarn ("ISO C forbids braced-groups within expressions");
5085 expr.value = c_finish_stmt_expr (stmt);
5086 expr.original_code = ERROR_MARK;
5088 else if (c_token_starts_typename (c_parser_peek_2nd_token (parser)))
5090 /* A compound literal. ??? Can we actually get here rather
5091 than going directly to
5092 c_parser_postfix_expression_after_paren_type from
5093 elsewhere? */
5094 struct c_type_name *type_name;
5095 c_parser_consume_token (parser);
5096 type_name = c_parser_type_name (parser);
5097 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5098 "expected %<)%>");
5099 if (type_name == NULL)
5101 expr.value = error_mark_node;
5102 expr.original_code = ERROR_MARK;
5104 else
5105 expr = c_parser_postfix_expression_after_paren_type (parser,
5106 type_name);
5108 else
5110 /* A parenthesized expression. */
5111 c_parser_consume_token (parser);
5112 expr = c_parser_expression (parser);
5113 if (TREE_CODE (expr.value) == MODIFY_EXPR)
5114 TREE_NO_WARNING (expr.value) = 1;
5115 expr.original_code = ERROR_MARK;
5116 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5117 "expected %<)%>");
5119 break;
5120 case CPP_KEYWORD:
5121 switch (c_parser_peek_token (parser)->keyword)
5123 case RID_FUNCTION_NAME:
5124 case RID_PRETTY_FUNCTION_NAME:
5125 case RID_C99_FUNCTION_NAME:
5126 expr.value = fname_decl (c_parser_peek_token (parser)->keyword,
5127 c_parser_peek_token (parser)->value);
5128 expr.original_code = ERROR_MARK;
5129 c_parser_consume_token (parser);
5130 break;
5131 case RID_VA_ARG:
5132 c_parser_consume_token (parser);
5133 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5135 expr.value = error_mark_node;
5136 expr.original_code = ERROR_MARK;
5137 break;
5139 e1 = c_parser_expr_no_commas (parser, NULL);
5140 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
5142 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5143 expr.value = error_mark_node;
5144 expr.original_code = ERROR_MARK;
5145 break;
5147 t1 = c_parser_type_name (parser);
5148 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5149 "expected %<)%>");
5150 if (t1 == NULL)
5152 expr.value = error_mark_node;
5153 expr.original_code = ERROR_MARK;
5155 else
5157 expr.value = build_va_arg (e1.value, groktypename (t1));
5158 expr.original_code = ERROR_MARK;
5160 break;
5161 case RID_OFFSETOF:
5162 c_parser_consume_token (parser);
5163 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5165 expr.value = error_mark_node;
5166 expr.original_code = ERROR_MARK;
5167 break;
5169 t1 = c_parser_type_name (parser);
5170 if (t1 == NULL)
5172 expr.value = error_mark_node;
5173 expr.original_code = ERROR_MARK;
5174 break;
5176 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
5178 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5179 expr.value = error_mark_node;
5180 expr.original_code = ERROR_MARK;
5181 break;
5184 tree type = groktypename (t1);
5185 tree offsetof_ref;
5186 if (type == error_mark_node)
5187 offsetof_ref = error_mark_node;
5188 else
5189 offsetof_ref = build1 (INDIRECT_REF, type, NULL);
5190 /* Parse the second argument to __builtin_offsetof. We
5191 must have one identifier, and beyond that we want to
5192 accept sub structure and sub array references. */
5193 if (c_parser_next_token_is (parser, CPP_NAME))
5195 offsetof_ref = build_component_ref
5196 (offsetof_ref, c_parser_peek_token (parser)->value);
5197 c_parser_consume_token (parser);
5198 while (c_parser_next_token_is (parser, CPP_DOT)
5199 || c_parser_next_token_is (parser,
5200 CPP_OPEN_SQUARE))
5202 if (c_parser_next_token_is (parser, CPP_DOT))
5204 c_parser_consume_token (parser);
5205 if (c_parser_next_token_is_not (parser,
5206 CPP_NAME))
5208 c_parser_error (parser, "expected identifier");
5209 break;
5211 offsetof_ref = build_component_ref
5212 (offsetof_ref,
5213 c_parser_peek_token (parser)->value);
5214 c_parser_consume_token (parser);
5216 else
5218 tree idx;
5219 c_parser_consume_token (parser);
5220 idx = c_parser_expression (parser).value;
5221 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
5222 "expected %<]%>");
5223 offsetof_ref = build_array_ref (offsetof_ref, idx);
5227 else
5228 c_parser_error (parser, "expected identifier");
5229 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5230 "expected %<)%>");
5231 expr.value = fold_offsetof (offsetof_ref);
5232 expr.original_code = ERROR_MARK;
5234 break;
5235 case RID_CHOOSE_EXPR:
5236 c_parser_consume_token (parser);
5237 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5239 expr.value = error_mark_node;
5240 expr.original_code = ERROR_MARK;
5241 break;
5243 e1 = c_parser_expr_no_commas (parser, NULL);
5244 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
5246 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5247 expr.value = error_mark_node;
5248 expr.original_code = ERROR_MARK;
5249 break;
5251 e2 = c_parser_expr_no_commas (parser, NULL);
5252 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
5254 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5255 expr.value = error_mark_node;
5256 expr.original_code = ERROR_MARK;
5257 break;
5259 e3 = c_parser_expr_no_commas (parser, NULL);
5260 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5261 "expected %<)%>");
5263 tree c;
5265 c = fold (e1.value);
5266 if (TREE_CODE (c) != INTEGER_CST)
5267 error ("first argument to %<__builtin_choose_expr%> not"
5268 " a constant");
5269 expr = integer_zerop (c) ? e3 : e2;
5271 break;
5272 case RID_TYPES_COMPATIBLE_P:
5273 c_parser_consume_token (parser);
5274 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5276 expr.value = error_mark_node;
5277 expr.original_code = ERROR_MARK;
5278 break;
5280 t1 = c_parser_type_name (parser);
5281 if (t1 == NULL)
5283 expr.value = error_mark_node;
5284 expr.original_code = ERROR_MARK;
5285 break;
5287 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
5289 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5290 expr.value = error_mark_node;
5291 expr.original_code = ERROR_MARK;
5292 break;
5294 t2 = c_parser_type_name (parser);
5295 if (t2 == NULL)
5297 expr.value = error_mark_node;
5298 expr.original_code = ERROR_MARK;
5299 break;
5301 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5302 "expected %<)%>");
5304 tree e1, e2;
5306 e1 = TYPE_MAIN_VARIANT (groktypename (t1));
5307 e2 = TYPE_MAIN_VARIANT (groktypename (t2));
5309 expr.value = comptypes (e1, e2)
5310 ? build_int_cst (NULL_TREE, 1)
5311 : build_int_cst (NULL_TREE, 0);
5312 expr.original_code = ERROR_MARK;
5314 break;
5315 case RID_AT_SELECTOR:
5316 gcc_assert (c_dialect_objc ());
5317 c_parser_consume_token (parser);
5318 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5320 expr.value = error_mark_node;
5321 expr.original_code = ERROR_MARK;
5322 break;
5325 tree sel = c_parser_objc_selector_arg (parser);
5326 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5327 "expected %<)%>");
5328 expr.value = objc_build_selector_expr (sel);
5329 expr.original_code = ERROR_MARK;
5331 break;
5332 case RID_AT_PROTOCOL:
5333 gcc_assert (c_dialect_objc ());
5334 c_parser_consume_token (parser);
5335 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5337 expr.value = error_mark_node;
5338 expr.original_code = ERROR_MARK;
5339 break;
5341 if (c_parser_next_token_is_not (parser, CPP_NAME))
5343 c_parser_error (parser, "expected identifier");
5344 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5345 expr.value = error_mark_node;
5346 expr.original_code = ERROR_MARK;
5347 break;
5350 tree id = c_parser_peek_token (parser)->value;
5351 c_parser_consume_token (parser);
5352 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5353 "expected %<)%>");
5354 expr.value = objc_build_protocol_expr (id);
5355 expr.original_code = ERROR_MARK;
5357 break;
5358 case RID_AT_ENCODE:
5359 /* Extension to support C-structures in the archiver. */
5360 gcc_assert (c_dialect_objc ());
5361 c_parser_consume_token (parser);
5362 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5364 expr.value = error_mark_node;
5365 expr.original_code = ERROR_MARK;
5366 break;
5368 t1 = c_parser_type_name (parser);
5369 if (t1 == NULL)
5371 expr.value = error_mark_node;
5372 expr.original_code = ERROR_MARK;
5373 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5374 break;
5376 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5377 "expected %<)%>");
5379 tree type = groktypename (t1);
5380 expr.value = objc_build_encode_expr (type);
5381 expr.original_code = ERROR_MARK;
5383 break;
5384 default:
5385 c_parser_error (parser, "expected expression");
5386 expr.value = error_mark_node;
5387 expr.original_code = ERROR_MARK;
5388 break;
5390 break;
5391 case CPP_OPEN_SQUARE:
5392 if (c_dialect_objc ())
5394 tree receiver, args;
5395 c_parser_consume_token (parser);
5396 receiver = c_parser_objc_receiver (parser);
5397 args = c_parser_objc_message_args (parser);
5398 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
5399 "expected %<]%>");
5400 expr.value = objc_build_message_expr (build_tree_list (receiver,
5401 args));
5402 expr.original_code = ERROR_MARK;
5403 break;
5405 /* Else fall through to report error. */
5406 default:
5407 c_parser_error (parser, "expected expression");
5408 expr.value = error_mark_node;
5409 expr.original_code = ERROR_MARK;
5410 break;
5412 return c_parser_postfix_expression_after_primary (parser, expr);
5415 /* Parse a postfix expression after a parenthesized type name: the
5416 brace-enclosed initializer of a compound literal, possibly followed
5417 by some postfix operators. This is separate because it is not
5418 possible to tell until after the type name whether a cast
5419 expression has a cast or a compound literal, or whether the operand
5420 of sizeof is a parenthesized type name or starts with a compound
5421 literal. */
5423 static struct c_expr
5424 c_parser_postfix_expression_after_paren_type (c_parser *parser,
5425 struct c_type_name *type_name)
5427 tree type;
5428 struct c_expr init;
5429 struct c_expr expr;
5430 start_init (NULL_TREE, NULL, 0);
5431 type = groktypename (type_name);
5432 if (C_TYPE_VARIABLE_SIZE (type))
5434 error ("compound literal has variable size");
5435 type = error_mark_node;
5437 init = c_parser_braced_init (parser, type, false);
5438 finish_init ();
5439 maybe_warn_string_init (type, init);
5441 if (pedantic && !flag_isoc99)
5442 pedwarn ("ISO C90 forbids compound literals");
5443 expr.value = build_compound_literal (type, init.value);
5444 expr.original_code = ERROR_MARK;
5445 return c_parser_postfix_expression_after_primary (parser, expr);
5448 /* Parse a postfix expression after the initial primary or compound
5449 literal; that is, parse a series of postfix operators. */
5451 static struct c_expr
5452 c_parser_postfix_expression_after_primary (c_parser *parser,
5453 struct c_expr expr)
5455 tree ident, idx, exprlist;
5456 while (true)
5458 switch (c_parser_peek_token (parser)->type)
5460 case CPP_OPEN_SQUARE:
5461 /* Array reference. */
5462 c_parser_consume_token (parser);
5463 idx = c_parser_expression (parser).value;
5464 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
5465 "expected %<]%>");
5466 expr.value = build_array_ref (expr.value, idx);
5467 expr.original_code = ERROR_MARK;
5468 break;
5469 case CPP_OPEN_PAREN:
5470 /* Function call. */
5471 c_parser_consume_token (parser);
5472 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
5473 exprlist = NULL_TREE;
5474 else
5475 exprlist = c_parser_expr_list (parser, true);
5476 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5477 "expected %<)%>");
5478 expr.value = build_function_call (expr.value, exprlist);
5479 expr.original_code = ERROR_MARK;
5480 break;
5481 case CPP_DOT:
5482 /* Structure element reference. */
5483 c_parser_consume_token (parser);
5484 expr = default_function_array_conversion (expr);
5485 if (c_parser_next_token_is (parser, CPP_NAME))
5486 ident = c_parser_peek_token (parser)->value;
5487 else
5489 c_parser_error (parser, "expected identifier");
5490 expr.value = error_mark_node;
5491 expr.original_code = ERROR_MARK;
5492 return expr;
5494 c_parser_consume_token (parser);
5495 expr.value = build_component_ref (expr.value, ident);
5496 expr.original_code = ERROR_MARK;
5497 break;
5498 case CPP_DEREF:
5499 /* Structure element reference. */
5500 c_parser_consume_token (parser);
5501 expr = default_function_array_conversion (expr);
5502 if (c_parser_next_token_is (parser, CPP_NAME))
5503 ident = c_parser_peek_token (parser)->value;
5504 else
5506 c_parser_error (parser, "expected identifier");
5507 expr.value = error_mark_node;
5508 expr.original_code = ERROR_MARK;
5509 return expr;
5511 c_parser_consume_token (parser);
5512 expr.value = build_component_ref (build_indirect_ref (expr.value,
5513 "->"), ident);
5514 expr.original_code = ERROR_MARK;
5515 break;
5516 case CPP_PLUS_PLUS:
5517 /* Postincrement. */
5518 c_parser_consume_token (parser);
5519 expr = default_function_array_conversion (expr);
5520 expr.value = build_unary_op (POSTINCREMENT_EXPR, expr.value, 0);
5521 expr.original_code = ERROR_MARK;
5522 break;
5523 case CPP_MINUS_MINUS:
5524 /* Postdecrement. */
5525 c_parser_consume_token (parser);
5526 expr = default_function_array_conversion (expr);
5527 expr.value = build_unary_op (POSTDECREMENT_EXPR, expr.value, 0);
5528 expr.original_code = ERROR_MARK;
5529 break;
5530 default:
5531 return expr;
5536 /* Parse an expression (C90 6.3.17, C99 6.5.17).
5538 expression:
5539 assignment-expression
5540 expression , assignment-expression
5543 static struct c_expr
5544 c_parser_expression (c_parser *parser)
5546 struct c_expr expr;
5547 expr = c_parser_expr_no_commas (parser, NULL);
5548 while (c_parser_next_token_is (parser, CPP_COMMA))
5550 struct c_expr next;
5551 c_parser_consume_token (parser);
5552 next = c_parser_expr_no_commas (parser, NULL);
5553 next = default_function_array_conversion (next);
5554 expr.value = build_compound_expr (expr.value, next.value);
5555 expr.original_code = COMPOUND_EXPR;
5557 return expr;
5560 /* Parse an expression and convert functions or arrays to
5561 pointers. */
5563 static struct c_expr
5564 c_parser_expression_conv (c_parser *parser)
5566 struct c_expr expr;
5567 expr = c_parser_expression (parser);
5568 expr = default_function_array_conversion (expr);
5569 return expr;
5572 /* Parse a non-empty list of expressions. If CONVERT_P, convert
5573 functions and arrays to pointers.
5575 nonempty-expr-list:
5576 assignment-expression
5577 nonempty-expr-list , assignment-expression
5580 static tree
5581 c_parser_expr_list (c_parser *parser, bool convert_p)
5583 struct c_expr expr;
5584 tree ret, cur;
5585 expr = c_parser_expr_no_commas (parser, NULL);
5586 if (convert_p)
5587 expr = default_function_array_conversion (expr);
5588 ret = cur = build_tree_list (NULL_TREE, expr.value);
5589 while (c_parser_next_token_is (parser, CPP_COMMA))
5591 c_parser_consume_token (parser);
5592 expr = c_parser_expr_no_commas (parser, NULL);
5593 if (convert_p)
5594 expr = default_function_array_conversion (expr);
5595 cur = TREE_CHAIN (cur) = build_tree_list (NULL_TREE, expr.value);
5597 return ret;
5601 /* Parse Objective-C-specific constructs. */
5603 /* Parse an objc-class-definition.
5605 objc-class-definition:
5606 @interface identifier objc-superclass[opt] objc-protocol-refs[opt]
5607 objc-class-instance-variables[opt] objc-methodprotolist @end
5608 @implementation identifier objc-superclass[opt]
5609 objc-class-instance-variables[opt]
5610 @interface identifier ( identifier ) objc-protocol-refs[opt]
5611 objc-methodprotolist @end
5612 @implementation identifier ( identifier )
5614 objc-superclass:
5615 : identifier
5617 "@interface identifier (" must start "@interface identifier (
5618 identifier ) ...": objc-methodprotolist in the first production may
5619 not start with a parenthesized identifier as a declarator of a data
5620 definition with no declaration specifiers if the objc-superclass,
5621 objc-protocol-refs and objc-class-instance-variables are omitted. */
5623 static void
5624 c_parser_objc_class_definition (c_parser *parser)
5626 bool iface_p;
5627 tree id1;
5628 tree superclass;
5629 if (c_parser_next_token_is_keyword (parser, RID_AT_INTERFACE))
5630 iface_p = true;
5631 else if (c_parser_next_token_is_keyword (parser, RID_AT_IMPLEMENTATION))
5632 iface_p = false;
5633 else
5634 gcc_unreachable ();
5635 c_parser_consume_token (parser);
5636 if (c_parser_next_token_is_not (parser, CPP_NAME))
5638 c_parser_error (parser, "expected identifier");
5639 return;
5641 id1 = c_parser_peek_token (parser)->value;
5642 c_parser_consume_token (parser);
5643 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
5645 tree id2;
5646 tree proto = NULL_TREE;
5647 c_parser_consume_token (parser);
5648 if (c_parser_next_token_is_not (parser, CPP_NAME))
5650 c_parser_error (parser, "expected identifier");
5651 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5652 return;
5654 id2 = c_parser_peek_token (parser)->value;
5655 c_parser_consume_token (parser);
5656 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5657 if (!iface_p)
5659 objc_start_category_implementation (id1, id2);
5660 return;
5662 if (c_parser_next_token_is (parser, CPP_LESS))
5663 proto = c_parser_objc_protocol_refs (parser);
5664 objc_start_category_interface (id1, id2, proto);
5665 c_parser_objc_methodprotolist (parser);
5666 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
5667 objc_finish_interface ();
5668 return;
5670 if (c_parser_next_token_is (parser, CPP_COLON))
5672 c_parser_consume_token (parser);
5673 if (c_parser_next_token_is_not (parser, CPP_NAME))
5675 c_parser_error (parser, "expected identifier");
5676 return;
5678 superclass = c_parser_peek_token (parser)->value;
5679 c_parser_consume_token (parser);
5681 else
5682 superclass = NULL_TREE;
5683 if (iface_p)
5685 tree proto = NULL_TREE;
5686 if (c_parser_next_token_is (parser, CPP_LESS))
5687 proto = c_parser_objc_protocol_refs (parser);
5688 objc_start_class_interface (id1, superclass, proto);
5690 else
5691 objc_start_class_implementation (id1, superclass);
5692 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
5693 c_parser_objc_class_instance_variables (parser);
5694 if (iface_p)
5696 objc_continue_interface ();
5697 c_parser_objc_methodprotolist (parser);
5698 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
5699 objc_finish_interface ();
5701 else
5703 objc_continue_implementation ();
5704 return;
5708 /* Parse objc-class-instance-variables.
5710 objc-class-instance-variables:
5711 { objc-instance-variable-decl-list[opt] }
5713 objc-instance-variable-decl-list:
5714 objc-visibility-spec
5715 objc-instance-variable-decl ;
5717 objc-instance-variable-decl-list objc-visibility-spec
5718 objc-instance-variable-decl-list objc-instance-variable-decl ;
5719 objc-instance-variable-decl-list ;
5721 objc-visibility-spec:
5722 @private
5723 @protected
5724 @public
5726 objc-instance-variable-decl:
5727 struct-declaration
5730 static void
5731 c_parser_objc_class_instance_variables (c_parser *parser)
5733 gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
5734 c_parser_consume_token (parser);
5735 while (c_parser_next_token_is_not (parser, CPP_EOF))
5737 tree decls;
5738 /* Parse any stray semicolon. */
5739 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5741 if (pedantic)
5742 pedwarn ("extra semicolon in struct or union specified");
5743 c_parser_consume_token (parser);
5744 continue;
5746 /* Stop if at the end of the instance variables. */
5747 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
5749 c_parser_consume_token (parser);
5750 break;
5752 /* Parse any objc-visibility-spec. */
5753 if (c_parser_next_token_is_keyword (parser, RID_AT_PRIVATE))
5755 c_parser_consume_token (parser);
5756 objc_set_visibility (2);
5757 continue;
5759 else if (c_parser_next_token_is_keyword (parser, RID_AT_PROTECTED))
5761 c_parser_consume_token (parser);
5762 objc_set_visibility (0);
5763 continue;
5765 else if (c_parser_next_token_is_keyword (parser, RID_AT_PUBLIC))
5767 c_parser_consume_token (parser);
5768 objc_set_visibility (1);
5769 continue;
5771 else if (c_parser_next_token_is (parser, CPP_PRAGMA))
5773 c_parser_pragma (parser, pragma_external);
5774 continue;
5777 /* Parse some comma-separated declarations. */
5778 decls = c_parser_struct_declaration (parser);
5780 /* Comma-separated instance variables are chained together in
5781 reverse order; add them one by one. */
5782 tree ivar = nreverse (decls);
5783 for (; ivar; ivar = TREE_CHAIN (ivar))
5784 objc_add_instance_variable (copy_node (ivar));
5786 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
5790 /* Parse an objc-class-declaration.
5792 objc-class-declaration:
5793 @class identifier-list ;
5796 static void
5797 c_parser_objc_class_declaration (c_parser *parser)
5799 tree list = NULL_TREE;
5800 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_CLASS));
5801 c_parser_consume_token (parser);
5802 /* Any identifiers, including those declared as type names, are OK
5803 here. */
5804 while (true)
5806 tree id;
5807 if (c_parser_next_token_is_not (parser, CPP_NAME))
5809 c_parser_error (parser, "expected identifier");
5810 break;
5812 id = c_parser_peek_token (parser)->value;
5813 list = chainon (list, build_tree_list (NULL_TREE, id));
5814 c_parser_consume_token (parser);
5815 if (c_parser_next_token_is (parser, CPP_COMMA))
5816 c_parser_consume_token (parser);
5817 else
5818 break;
5820 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
5821 objc_declare_class (list);
5824 /* Parse an objc-alias-declaration.
5826 objc-alias-declaration:
5827 @compatibility_alias identifier identifier ;
5830 static void
5831 c_parser_objc_alias_declaration (c_parser *parser)
5833 tree id1, id2;
5834 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_ALIAS));
5835 c_parser_consume_token (parser);
5836 if (c_parser_next_token_is_not (parser, CPP_NAME))
5838 c_parser_error (parser, "expected identifier");
5839 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
5840 return;
5842 id1 = c_parser_peek_token (parser)->value;
5843 c_parser_consume_token (parser);
5844 if (c_parser_next_token_is_not (parser, CPP_NAME))
5846 c_parser_error (parser, "expected identifier");
5847 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
5848 return;
5850 id2 = c_parser_peek_token (parser)->value;
5851 c_parser_consume_token (parser);
5852 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
5853 objc_declare_alias (id1, id2);
5856 /* Parse an objc-protocol-definition.
5858 objc-protocol-definition:
5859 @protocol identifier objc-protocol-refs[opt] objc-methodprotolist @end
5860 @protocol identifier-list ;
5862 "@protocol identifier ;" should be resolved as "@protocol
5863 identifier-list ;": objc-methodprotolist may not start with a
5864 semicolon in the first alternative if objc-protocol-refs are
5865 omitted. */
5867 static void
5868 c_parser_objc_protocol_definition (c_parser *parser)
5870 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_PROTOCOL));
5871 c_parser_consume_token (parser);
5872 if (c_parser_next_token_is_not (parser, CPP_NAME))
5874 c_parser_error (parser, "expected identifier");
5875 return;
5877 if (c_parser_peek_2nd_token (parser)->type == CPP_COMMA
5878 || c_parser_peek_2nd_token (parser)->type == CPP_SEMICOLON)
5880 tree list = NULL_TREE;
5881 /* Any identifiers, including those declared as type names, are
5882 OK here. */
5883 while (true)
5885 tree id;
5886 if (c_parser_next_token_is_not (parser, CPP_NAME))
5888 c_parser_error (parser, "expected identifier");
5889 break;
5891 id = c_parser_peek_token (parser)->value;
5892 list = chainon (list, build_tree_list (NULL_TREE, id));
5893 c_parser_consume_token (parser);
5894 if (c_parser_next_token_is (parser, CPP_COMMA))
5895 c_parser_consume_token (parser);
5896 else
5897 break;
5899 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
5900 objc_declare_protocols (list);
5902 else
5904 tree id = c_parser_peek_token (parser)->value;
5905 tree proto = NULL_TREE;
5906 c_parser_consume_token (parser);
5907 if (c_parser_next_token_is (parser, CPP_LESS))
5908 proto = c_parser_objc_protocol_refs (parser);
5909 objc_pq_context = 1;
5910 objc_start_protocol (id, proto);
5911 c_parser_objc_methodprotolist (parser);
5912 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
5913 objc_pq_context = 0;
5914 objc_finish_interface ();
5918 /* Parse an objc-method-type.
5920 objc-method-type:
5925 static enum tree_code
5926 c_parser_objc_method_type (c_parser *parser)
5928 switch (c_parser_peek_token (parser)->type)
5930 case CPP_PLUS:
5931 c_parser_consume_token (parser);
5932 return PLUS_EXPR;
5933 case CPP_MINUS:
5934 c_parser_consume_token (parser);
5935 return MINUS_EXPR;
5936 default:
5937 gcc_unreachable ();
5941 /* Parse an objc-method-definition.
5943 objc-method-definition:
5944 objc-method-type objc-method-decl ;[opt] compound-statement
5947 static void
5948 c_parser_objc_method_definition (c_parser *parser)
5950 enum tree_code type = c_parser_objc_method_type (parser);
5951 tree decl;
5952 objc_set_method_type (type);
5953 objc_pq_context = 1;
5954 decl = c_parser_objc_method_decl (parser);
5955 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5957 c_parser_consume_token (parser);
5958 if (pedantic)
5959 pedwarn ("extra semicolon in method definition specified");
5961 if (!c_parser_next_token_is (parser, CPP_OPEN_BRACE))
5963 c_parser_error (parser, "expected %<{%>");
5964 return;
5966 objc_pq_context = 0;
5967 objc_start_method_definition (decl);
5968 add_stmt (c_parser_compound_statement (parser));
5969 objc_finish_method_definition (current_function_decl);
5972 /* Parse an objc-methodprotolist.
5974 objc-methodprotolist:
5975 empty
5976 objc-methodprotolist objc-methodproto
5977 objc-methodprotolist declaration
5978 objc-methodprotolist ;
5980 The declaration is a data definition, which may be missing
5981 declaration specifiers under the same rules and diagnostics as
5982 other data definitions outside functions, and the stray semicolon
5983 is diagnosed the same way as a stray semicolon outside a
5984 function. */
5986 static void
5987 c_parser_objc_methodprotolist (c_parser *parser)
5989 while (true)
5991 /* The list is terminated by @end. */
5992 switch (c_parser_peek_token (parser)->type)
5994 case CPP_SEMICOLON:
5995 if (pedantic)
5996 pedwarn ("ISO C does not allow extra %<;%> outside of a function");
5997 c_parser_consume_token (parser);
5998 break;
5999 case CPP_PLUS:
6000 case CPP_MINUS:
6001 c_parser_objc_methodproto (parser);
6002 break;
6003 case CPP_PRAGMA:
6004 c_parser_pragma (parser, pragma_external);
6005 break;
6006 case CPP_EOF:
6007 return;
6008 default:
6009 if (c_parser_next_token_is_keyword (parser, RID_AT_END))
6010 return;
6011 c_parser_declaration_or_fndef (parser, false, true, false, true);
6012 break;
6017 /* Parse an objc-methodproto.
6019 objc-methodproto:
6020 objc-method-type objc-method-decl ;
6023 static void
6024 c_parser_objc_methodproto (c_parser *parser)
6026 enum tree_code type = c_parser_objc_method_type (parser);
6027 tree decl;
6028 objc_set_method_type (type);
6029 /* Remember protocol qualifiers in prototypes. */
6030 objc_pq_context = 1;
6031 decl = c_parser_objc_method_decl (parser);
6032 /* Forget protocol qualifiers here. */
6033 objc_pq_context = 0;
6034 objc_add_method_declaration (decl);
6035 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
6038 /* Parse an objc-method-decl.
6040 objc-method-decl:
6041 ( objc-type-name ) objc-selector
6042 objc-selector
6043 ( objc-type-name ) objc-keyword-selector objc-optparmlist
6044 objc-keyword-selector objc-optparmlist
6046 objc-keyword-selector:
6047 objc-keyword-decl
6048 objc-keyword-selector objc-keyword-decl
6050 objc-keyword-decl:
6051 objc-selector : ( objc-type-name ) identifier
6052 objc-selector : identifier
6053 : ( objc-type-name ) identifier
6054 : identifier
6056 objc-optparmlist:
6057 objc-optparms objc-optellipsis
6059 objc-optparms:
6060 empty
6061 objc-opt-parms , parameter-declaration
6063 objc-optellipsis:
6064 empty
6065 , ...
6068 static tree
6069 c_parser_objc_method_decl (c_parser *parser)
6071 tree type = NULL_TREE;
6072 tree sel;
6073 tree parms = NULL_TREE;
6074 bool ellipsis = false;
6076 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
6078 c_parser_consume_token (parser);
6079 type = c_parser_objc_type_name (parser);
6080 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6082 sel = c_parser_objc_selector (parser);
6083 /* If there is no selector, or a colon follows, we have an
6084 objc-keyword-selector. If there is a selector, and a colon does
6085 not follow, that selector ends the objc-method-decl. */
6086 if (!sel || c_parser_next_token_is (parser, CPP_COLON))
6088 tree tsel = sel;
6089 tree list = NULL_TREE;
6090 while (true)
6092 tree atype = NULL_TREE, id, keyworddecl;
6093 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
6094 break;
6095 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
6097 c_parser_consume_token (parser);
6098 atype = c_parser_objc_type_name (parser);
6099 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6100 "expected %<)%>");
6102 if (c_parser_next_token_is_not (parser, CPP_NAME))
6104 c_parser_error (parser, "expected identifier");
6105 return error_mark_node;
6107 id = c_parser_peek_token (parser)->value;
6108 c_parser_consume_token (parser);
6109 keyworddecl = objc_build_keyword_decl (tsel, atype, id);
6110 list = chainon (list, keyworddecl);
6111 tsel = c_parser_objc_selector (parser);
6112 if (!tsel && c_parser_next_token_is_not (parser, CPP_COLON))
6113 break;
6115 /* Parse the optional parameter list. Optional Objective-C
6116 method parameters follow the C syntax, and may include '...'
6117 to denote a variable number of arguments. */
6118 parms = make_node (TREE_LIST);
6119 while (c_parser_next_token_is (parser, CPP_COMMA))
6121 struct c_parm *parm;
6122 c_parser_consume_token (parser);
6123 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
6125 ellipsis = true;
6126 c_parser_consume_token (parser);
6127 break;
6129 parm = c_parser_parameter_declaration (parser, NULL_TREE);
6130 if (parm == NULL)
6131 break;
6132 parms = chainon (parms,
6133 build_tree_list (NULL_TREE, grokparm (parm)));
6135 sel = list;
6137 return objc_build_method_signature (type, sel, parms, ellipsis);
6140 /* Parse an objc-type-name.
6142 objc-type-name:
6143 objc-type-qualifiers[opt] type-name
6144 objc-type-qualifiers[opt]
6146 objc-type-qualifiers:
6147 objc-type-qualifier
6148 objc-type-qualifiers objc-type-qualifier
6150 objc-type-qualifier: one of
6151 in out inout bycopy byref oneway
6154 static tree
6155 c_parser_objc_type_name (c_parser *parser)
6157 tree quals = NULL_TREE;
6158 struct c_type_name *typename = NULL;
6159 tree type = NULL_TREE;
6160 while (true)
6162 c_token *token = c_parser_peek_token (parser);
6163 if (token->type == CPP_KEYWORD
6164 && (token->keyword == RID_IN
6165 || token->keyword == RID_OUT
6166 || token->keyword == RID_INOUT
6167 || token->keyword == RID_BYCOPY
6168 || token->keyword == RID_BYREF
6169 || token->keyword == RID_ONEWAY))
6171 quals = chainon (quals, build_tree_list (NULL_TREE, token->value));
6172 c_parser_consume_token (parser);
6174 else
6175 break;
6177 if (c_parser_next_token_starts_typename (parser))
6178 typename = c_parser_type_name (parser);
6179 if (typename)
6180 type = groktypename (typename);
6181 return build_tree_list (quals, type);
6184 /* Parse objc-protocol-refs.
6186 objc-protocol-refs:
6187 < identifier-list >
6190 static tree
6191 c_parser_objc_protocol_refs (c_parser *parser)
6193 tree list = NULL_TREE;
6194 gcc_assert (c_parser_next_token_is (parser, CPP_LESS));
6195 c_parser_consume_token (parser);
6196 /* Any identifiers, including those declared as type names, are OK
6197 here. */
6198 while (true)
6200 tree id;
6201 if (c_parser_next_token_is_not (parser, CPP_NAME))
6203 c_parser_error (parser, "expected identifier");
6204 break;
6206 id = c_parser_peek_token (parser)->value;
6207 list = chainon (list, build_tree_list (NULL_TREE, id));
6208 c_parser_consume_token (parser);
6209 if (c_parser_next_token_is (parser, CPP_COMMA))
6210 c_parser_consume_token (parser);
6211 else
6212 break;
6214 c_parser_require (parser, CPP_GREATER, "expected %<>%>");
6215 return list;
6218 /* Parse an objc-try-catch-statement.
6220 objc-try-catch-statement:
6221 @try compound-statement objc-catch-list[opt]
6222 @try compound-statement objc-catch-list[opt] @finally compound-statement
6224 objc-catch-list:
6225 @catch ( parameter-declaration ) compound-statement
6226 objc-catch-list @catch ( parameter-declaration ) compound-statement
6229 static void
6230 c_parser_objc_try_catch_statement (c_parser *parser)
6232 location_t loc;
6233 tree stmt;
6234 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_TRY));
6235 c_parser_consume_token (parser);
6236 loc = c_parser_peek_token (parser)->location;
6237 stmt = c_parser_compound_statement (parser);
6238 objc_begin_try_stmt (loc, stmt);
6239 while (c_parser_next_token_is_keyword (parser, RID_AT_CATCH))
6241 struct c_parm *parm;
6242 c_parser_consume_token (parser);
6243 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6244 break;
6245 parm = c_parser_parameter_declaration (parser, NULL_TREE);
6246 if (parm == NULL)
6248 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6249 break;
6251 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6252 objc_begin_catch_clause (grokparm (parm));
6253 if (c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
6254 c_parser_compound_statement_nostart (parser);
6255 objc_finish_catch_clause ();
6257 if (c_parser_next_token_is_keyword (parser, RID_AT_FINALLY))
6259 location_t finloc;
6260 tree finstmt;
6261 c_parser_consume_token (parser);
6262 finloc = c_parser_peek_token (parser)->location;
6263 finstmt = c_parser_compound_statement (parser);
6264 objc_build_finally_clause (finloc, finstmt);
6266 objc_finish_try_stmt ();
6269 /* Parse an objc-synchronized-statement.
6271 objc-synchronized-statement:
6272 @synchronized ( expression ) compound-statement
6275 static void
6276 c_parser_objc_synchronized_statement (c_parser *parser)
6278 location_t loc;
6279 tree expr, stmt;
6280 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_SYNCHRONIZED));
6281 c_parser_consume_token (parser);
6282 loc = c_parser_peek_token (parser)->location;
6283 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6285 expr = c_parser_expression (parser).value;
6286 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6288 else
6289 expr = error_mark_node;
6290 stmt = c_parser_compound_statement (parser);
6291 objc_build_synchronized (loc, expr, stmt);
6294 /* Parse an objc-selector; return NULL_TREE without an error if the
6295 next token is not an objc-selector.
6297 objc-selector:
6298 identifier
6299 one of
6300 enum struct union if else while do for switch case default
6301 break continue return goto asm sizeof typeof __alignof
6302 unsigned long const short volatile signed restrict _Complex
6303 in out inout bycopy byref oneway int char float double void _Bool
6305 ??? Why this selection of keywords but not, for example, storage
6306 class specifiers? */
6308 static tree
6309 c_parser_objc_selector (c_parser *parser)
6311 c_token *token = c_parser_peek_token (parser);
6312 tree value = token->value;
6313 if (token->type == CPP_NAME)
6315 c_parser_consume_token (parser);
6316 return value;
6318 if (token->type != CPP_KEYWORD)
6319 return NULL_TREE;
6320 switch (token->keyword)
6322 case RID_ENUM:
6323 case RID_STRUCT:
6324 case RID_UNION:
6325 case RID_IF:
6326 case RID_ELSE:
6327 case RID_WHILE:
6328 case RID_DO:
6329 case RID_FOR:
6330 case RID_SWITCH:
6331 case RID_CASE:
6332 case RID_DEFAULT:
6333 case RID_BREAK:
6334 case RID_CONTINUE:
6335 case RID_RETURN:
6336 case RID_GOTO:
6337 case RID_ASM:
6338 case RID_SIZEOF:
6339 case RID_TYPEOF:
6340 case RID_ALIGNOF:
6341 case RID_UNSIGNED:
6342 case RID_LONG:
6343 case RID_CONST:
6344 case RID_SHORT:
6345 case RID_VOLATILE:
6346 case RID_SIGNED:
6347 case RID_RESTRICT:
6348 case RID_COMPLEX:
6349 case RID_IN:
6350 case RID_OUT:
6351 case RID_INOUT:
6352 case RID_BYCOPY:
6353 case RID_BYREF:
6354 case RID_ONEWAY:
6355 case RID_INT:
6356 case RID_CHAR:
6357 case RID_FLOAT:
6358 case RID_DOUBLE:
6359 case RID_VOID:
6360 case RID_BOOL:
6361 c_parser_consume_token (parser);
6362 return value;
6363 default:
6364 return NULL_TREE;
6368 /* Parse an objc-selector-arg.
6370 objc-selector-arg:
6371 objc-selector
6372 objc-keywordname-list
6374 objc-keywordname-list:
6375 objc-keywordname
6376 objc-keywordname-list objc-keywordname
6378 objc-keywordname:
6379 objc-selector :
6383 static tree
6384 c_parser_objc_selector_arg (c_parser *parser)
6386 tree sel = c_parser_objc_selector (parser);
6387 tree list = NULL_TREE;
6388 if (sel && c_parser_next_token_is_not (parser, CPP_COLON))
6389 return sel;
6390 while (true)
6392 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
6393 return list;
6394 list = chainon (list, build_tree_list (sel, NULL_TREE));
6395 sel = c_parser_objc_selector (parser);
6396 if (!sel && c_parser_next_token_is_not (parser, CPP_COLON))
6397 break;
6399 return list;
6402 /* Parse an objc-receiver.
6404 objc-receiver:
6405 expression
6406 class-name
6407 type-name
6410 static tree
6411 c_parser_objc_receiver (c_parser *parser)
6413 if (c_parser_peek_token (parser)->type == CPP_NAME
6414 && (c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME
6415 || c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME))
6417 tree id = c_parser_peek_token (parser)->value;
6418 c_parser_consume_token (parser);
6419 return objc_get_class_reference (id);
6421 return c_parser_expression (parser).value;
6424 /* Parse objc-message-args.
6426 objc-message-args:
6427 objc-selector
6428 objc-keywordarg-list
6430 objc-keywordarg-list:
6431 objc-keywordarg
6432 objc-keywordarg-list objc-keywordarg
6434 objc-keywordarg:
6435 objc-selector : objc-keywordexpr
6436 : objc-keywordexpr
6439 static tree
6440 c_parser_objc_message_args (c_parser *parser)
6442 tree sel = c_parser_objc_selector (parser);
6443 tree list = NULL_TREE;
6444 if (sel && c_parser_next_token_is_not (parser, CPP_COLON))
6445 return sel;
6446 while (true)
6448 tree keywordexpr;
6449 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
6450 return list;
6451 keywordexpr = c_parser_objc_keywordexpr (parser);
6452 list = chainon (list, build_tree_list (sel, keywordexpr));
6453 sel = c_parser_objc_selector (parser);
6454 if (!sel && c_parser_next_token_is_not (parser, CPP_COLON))
6455 break;
6457 return list;
6460 /* Parse an objc-keywordexpr.
6462 objc-keywordexpr:
6463 nonempty-expr-list
6466 static tree
6467 c_parser_objc_keywordexpr (c_parser *parser)
6469 tree list = c_parser_expr_list (parser, true);
6470 if (TREE_CHAIN (list) == NULL_TREE)
6472 /* Just return the expression, remove a level of
6473 indirection. */
6474 return TREE_VALUE (list);
6476 else
6478 /* We have a comma expression, we will collapse later. */
6479 return list;
6484 /* Handle pragmas. Some OpenMP pragmas are associated with, and therefore
6485 should be considered, statements. ALLOW_STMT is true if we're within
6486 the context of a function and such pragmas are to be allowed. Returns
6487 true if we actually parsed such a pragma. */
6489 static bool
6490 c_parser_pragma (c_parser *parser, enum pragma_context context)
6492 unsigned int id;
6494 id = c_parser_peek_token (parser)->pragma_kind;
6495 gcc_assert (id != PRAGMA_NONE);
6497 switch (id)
6499 case PRAGMA_OMP_BARRIER:
6500 if (context != pragma_compound)
6502 if (context == pragma_stmt)
6503 c_parser_error (parser, "%<#pragma omp barrier%> may only be "
6504 "used in compound statements");
6505 goto bad_stmt;
6507 c_parser_omp_barrier (parser);
6508 return false;
6510 case PRAGMA_OMP_FLUSH:
6511 if (context != pragma_compound)
6513 if (context == pragma_stmt)
6514 c_parser_error (parser, "%<#pragma omp flush%> may only be "
6515 "used in compound statements");
6516 goto bad_stmt;
6518 c_parser_omp_flush (parser);
6519 return false;
6521 case PRAGMA_OMP_THREADPRIVATE:
6522 c_parser_omp_threadprivate (parser);
6523 return false;
6525 case PRAGMA_OMP_SECTION:
6526 error ("%<#pragma omp section%> may only be used in "
6527 "%<#pragma omp sections%> construct");
6528 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
6529 return false;
6531 case PRAGMA_GCC_PCH_PREPROCESS:
6532 c_parser_error (parser, "%<#pragma GCC pch_preprocess%> must be first");
6533 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
6534 return false;
6536 default:
6537 if (id < PRAGMA_FIRST_EXTERNAL)
6539 if (context == pragma_external)
6541 bad_stmt:
6542 c_parser_error (parser, "expected declaration specifiers");
6543 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
6544 return false;
6546 c_parser_omp_construct (parser);
6547 return true;
6549 break;
6552 c_parser_consume_pragma (parser);
6553 c_invoke_pragma_handler (id);
6555 /* Skip to EOL, but suppress any error message. Those will have been
6556 generated by the handler routine through calling error, as opposed
6557 to calling c_parser_error. */
6558 parser->error = true;
6559 c_parser_skip_to_pragma_eol (parser);
6561 return false;
6564 /* The interface the pragma parsers have to the lexer. */
6566 enum cpp_ttype
6567 pragma_lex (tree *value)
6569 c_token *tok = c_parser_peek_token (the_parser);
6570 enum cpp_ttype ret = tok->type;
6572 *value = tok->value;
6573 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
6574 ret = CPP_EOF;
6575 else
6577 if (ret == CPP_KEYWORD)
6578 ret = CPP_NAME;
6579 c_parser_consume_token (the_parser);
6582 return ret;
6585 static void
6586 c_parser_pragma_pch_preprocess (c_parser *parser)
6588 tree name = NULL;
6590 c_parser_consume_pragma (parser);
6591 if (c_parser_next_token_is (parser, CPP_STRING))
6593 name = c_parser_peek_token (parser)->value;
6594 c_parser_consume_token (parser);
6596 else
6597 c_parser_error (parser, "expected string literal");
6598 c_parser_skip_to_pragma_eol (parser);
6600 if (name)
6601 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
6604 /* OpenMP 2.5 parsing routines. */
6606 /* Returns name of the next clause.
6607 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
6608 the token is not consumed. Otherwise appropriate pragma_omp_clause is
6609 returned and the token is consumed. */
6611 static pragma_omp_clause
6612 c_parser_omp_clause_name (c_parser *parser)
6614 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
6616 if (c_parser_next_token_is_keyword (parser, RID_IF))
6617 result = PRAGMA_OMP_CLAUSE_IF;
6618 else if (c_parser_next_token_is_keyword (parser, RID_DEFAULT))
6619 result = PRAGMA_OMP_CLAUSE_DEFAULT;
6620 else if (c_parser_next_token_is (parser, CPP_NAME))
6622 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
6624 switch (p[0])
6626 case 'c':
6627 if (!strcmp ("copyin", p))
6628 result = PRAGMA_OMP_CLAUSE_COPYIN;
6629 else if (!strcmp ("copyprivate", p))
6630 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
6631 break;
6632 case 'f':
6633 if (!strcmp ("firstprivate", p))
6634 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
6635 break;
6636 case 'l':
6637 if (!strcmp ("lastprivate", p))
6638 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
6639 break;
6640 case 'n':
6641 if (!strcmp ("nowait", p))
6642 result = PRAGMA_OMP_CLAUSE_NOWAIT;
6643 else if (!strcmp ("num_threads", p))
6644 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
6645 break;
6646 case 'o':
6647 if (!strcmp ("ordered", p))
6648 result = PRAGMA_OMP_CLAUSE_ORDERED;
6649 break;
6650 case 'p':
6651 if (!strcmp ("private", p))
6652 result = PRAGMA_OMP_CLAUSE_PRIVATE;
6653 break;
6654 case 'r':
6655 if (!strcmp ("reduction", p))
6656 result = PRAGMA_OMP_CLAUSE_REDUCTION;
6657 break;
6658 case 's':
6659 if (!strcmp ("schedule", p))
6660 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
6661 else if (!strcmp ("shared", p))
6662 result = PRAGMA_OMP_CLAUSE_SHARED;
6663 break;
6667 if (result != PRAGMA_OMP_CLAUSE_NONE)
6668 c_parser_consume_token (parser);
6670 return result;
6673 /* Validate that a clause of the given type does not already exist. */
6675 static void
6676 check_no_duplicate_clause (tree clauses, enum tree_code code, const char *name)
6678 tree c;
6680 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
6681 if (OMP_CLAUSE_CODE (c) == code)
6683 error ("too many %qs clauses", name);
6684 break;
6688 /* OpenMP 2.5:
6689 variable-list:
6690 identifier
6691 variable-list , identifier
6693 If KIND is nonzero, create the appropriate node and install the decl
6694 in OMP_CLAUSE_DECL and add the node to the head of the list.
6696 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
6697 return the list created. */
6699 static tree
6700 c_parser_omp_variable_list (c_parser *parser, enum omp_clause_code kind,
6701 tree list)
6703 if (c_parser_next_token_is_not (parser, CPP_NAME)
6704 || c_parser_peek_token (parser)->id_kind != C_ID_ID)
6705 c_parser_error (parser, "expected identifier");
6707 while (c_parser_next_token_is (parser, CPP_NAME)
6708 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
6710 tree t = lookup_name (c_parser_peek_token (parser)->value);
6712 if (t == NULL_TREE)
6713 undeclared_variable (c_parser_peek_token (parser)->value,
6714 c_parser_peek_token (parser)->location);
6715 else if (t == error_mark_node)
6717 else if (kind != 0)
6719 tree u = build_omp_clause (kind);
6720 OMP_CLAUSE_DECL (u) = t;
6721 OMP_CLAUSE_CHAIN (u) = list;
6722 list = u;
6724 else
6725 list = tree_cons (t, NULL_TREE, list);
6727 c_parser_consume_token (parser);
6729 if (c_parser_next_token_is_not (parser, CPP_COMMA))
6730 break;
6732 c_parser_consume_token (parser);
6735 return list;
6738 /* Similarly, but expect leading and trailing parenthesis. This is a very
6739 common case for omp clauses. */
6741 static tree
6742 c_parser_omp_var_list_parens (c_parser *parser, enum tree_code kind, tree list)
6744 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6746 list = c_parser_omp_variable_list (parser, kind, list);
6747 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6749 return list;
6752 /* OpenMP 2.5:
6753 copyin ( variable-list ) */
6755 static tree
6756 c_parser_omp_clause_copyin (c_parser *parser, tree list)
6758 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_COPYIN, list);
6761 /* OpenMP 2.5:
6762 copyprivate ( variable-list ) */
6764 static tree
6765 c_parser_omp_clause_copyprivate (c_parser *parser, tree list)
6767 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_COPYPRIVATE, list);
6770 /* OpenMP 2.5:
6771 default ( shared | none ) */
6773 static tree
6774 c_parser_omp_clause_default (c_parser *parser, tree list)
6776 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
6777 tree c;
6779 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6780 return list;
6781 if (c_parser_next_token_is (parser, CPP_NAME))
6783 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
6785 switch (p[0])
6787 case 'n':
6788 if (strcmp ("none", p) != 0)
6789 goto invalid_kind;
6790 kind = OMP_CLAUSE_DEFAULT_NONE;
6791 break;
6793 case 's':
6794 if (strcmp ("shared", p) != 0)
6795 goto invalid_kind;
6796 kind = OMP_CLAUSE_DEFAULT_SHARED;
6797 break;
6799 default:
6800 goto invalid_kind;
6803 c_parser_consume_token (parser);
6805 else
6807 invalid_kind:
6808 c_parser_error (parser, "expected %<none%> or %<shared%>");
6810 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6812 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
6813 return list;
6815 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default");
6816 c = build_omp_clause (OMP_CLAUSE_DEFAULT);
6817 OMP_CLAUSE_CHAIN (c) = list;
6818 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
6820 return c;
6823 /* OpenMP 2.5:
6824 firstprivate ( variable-list ) */
6826 static tree
6827 c_parser_omp_clause_firstprivate (c_parser *parser, tree list)
6829 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_FIRSTPRIVATE, list);
6832 /* OpenMP 2.5:
6833 if ( expression ) */
6835 static tree
6836 c_parser_omp_clause_if (c_parser *parser, tree list)
6838 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
6840 tree t = c_parser_paren_condition (parser);
6841 tree c;
6843 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if");
6845 c = build_omp_clause (OMP_CLAUSE_IF);
6846 OMP_CLAUSE_IF_EXPR (c) = t;
6847 OMP_CLAUSE_CHAIN (c) = list;
6848 list = c;
6850 else
6851 c_parser_error (parser, "expected %<(%>");
6853 return list;
6856 /* OpenMP 2.5:
6857 lastprivate ( variable-list ) */
6859 static tree
6860 c_parser_omp_clause_lastprivate (c_parser *parser, tree list)
6862 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_LASTPRIVATE, list);
6865 /* OpenMP 2.5:
6866 nowait */
6868 static tree
6869 c_parser_omp_clause_nowait (c_parser *parser ATTRIBUTE_UNUSED, tree list)
6871 tree c;
6873 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait");
6875 c = build_omp_clause (OMP_CLAUSE_NOWAIT);
6876 OMP_CLAUSE_CHAIN (c) = list;
6877 return c;
6880 /* OpenMP 2.5:
6881 num_threads ( expression ) */
6883 static tree
6884 c_parser_omp_clause_num_threads (c_parser *parser, tree list)
6886 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6888 tree c, t = c_parser_expression (parser).value;
6890 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6892 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
6894 c_parser_error (parser, "expected integer expression");
6895 return list;
6898 /* Attempt to statically determine when the number isn't positive. */
6899 c = fold_build2 (LE_EXPR, boolean_type_node, t,
6900 build_int_cst (TREE_TYPE (t), 0));
6901 if (c == boolean_true_node)
6903 warning (0, "%<num_threads%> value must be positive");
6904 t = integer_one_node;
6907 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS, "num_threads");
6909 c = build_omp_clause (OMP_CLAUSE_NUM_THREADS);
6910 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
6911 OMP_CLAUSE_CHAIN (c) = list;
6912 list = c;
6915 return list;
6918 /* OpenMP 2.5:
6919 ordered */
6921 static tree
6922 c_parser_omp_clause_ordered (c_parser *parser ATTRIBUTE_UNUSED, tree list)
6924 tree c;
6926 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED, "ordered");
6928 c = build_omp_clause (OMP_CLAUSE_ORDERED);
6929 OMP_CLAUSE_CHAIN (c) = list;
6930 return c;
6933 /* OpenMP 2.5:
6934 private ( variable-list ) */
6936 static tree
6937 c_parser_omp_clause_private (c_parser *parser, tree list)
6939 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_PRIVATE, list);
6942 /* OpenMP 2.5:
6943 reduction ( reduction-operator : variable-list )
6945 reduction-operator:
6946 One of: + * - & ^ | && || */
6948 static tree
6949 c_parser_omp_clause_reduction (c_parser *parser, tree list)
6951 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6953 enum tree_code code;
6955 switch (c_parser_peek_token (parser)->type)
6957 case CPP_PLUS:
6958 code = PLUS_EXPR;
6959 break;
6960 case CPP_MULT:
6961 code = MULT_EXPR;
6962 break;
6963 case CPP_MINUS:
6964 code = MINUS_EXPR;
6965 break;
6966 case CPP_AND:
6967 code = BIT_AND_EXPR;
6968 break;
6969 case CPP_XOR:
6970 code = BIT_XOR_EXPR;
6971 break;
6972 case CPP_OR:
6973 code = BIT_IOR_EXPR;
6974 break;
6975 case CPP_AND_AND:
6976 code = TRUTH_ANDIF_EXPR;
6977 break;
6978 case CPP_OR_OR:
6979 code = TRUTH_ORIF_EXPR;
6980 break;
6981 default:
6982 c_parser_error (parser,
6983 "expected %<+%>, %<*%>, %<-%>, %<&%>, "
6984 "%<^%>, %<|%>, %<&&%>, or %<||%>");
6985 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
6986 return list;
6988 c_parser_consume_token (parser);
6989 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
6991 tree nl, c;
6993 nl = c_parser_omp_variable_list (parser, OMP_CLAUSE_REDUCTION, list);
6994 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
6995 OMP_CLAUSE_REDUCTION_CODE (c) = code;
6997 list = nl;
6999 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
7001 return list;
7004 /* OpenMP 2.5:
7005 schedule ( schedule-kind )
7006 schedule ( schedule-kind , expression )
7008 schedule-kind:
7009 static | dynamic | guided | runtime
7012 static tree
7013 c_parser_omp_clause_schedule (c_parser *parser, tree list)
7015 tree c, t;
7017 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7018 return list;
7020 c = build_omp_clause (OMP_CLAUSE_SCHEDULE);
7022 if (c_parser_next_token_is (parser, CPP_NAME))
7024 tree kind = c_parser_peek_token (parser)->value;
7025 const char *p = IDENTIFIER_POINTER (kind);
7027 switch (p[0])
7029 case 'd':
7030 if (strcmp ("dynamic", p) != 0)
7031 goto invalid_kind;
7032 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
7033 break;
7035 case 'g':
7036 if (strcmp ("guided", p) != 0)
7037 goto invalid_kind;
7038 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
7039 break;
7041 case 'r':
7042 if (strcmp ("runtime", p) != 0)
7043 goto invalid_kind;
7044 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
7045 break;
7047 default:
7048 goto invalid_kind;
7051 else if (c_parser_next_token_is_keyword (parser, RID_STATIC))
7052 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
7053 else
7054 goto invalid_kind;
7056 c_parser_consume_token (parser);
7057 if (c_parser_next_token_is (parser, CPP_COMMA))
7059 c_parser_consume_token (parser);
7061 t = c_parser_expr_no_commas (parser, NULL).value;
7063 if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
7064 error ("schedule %<runtime%> does not take "
7065 "a %<chunk_size%> parameter");
7066 else if (TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE)
7067 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
7068 else
7069 c_parser_error (parser, "expected integer expression");
7071 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
7073 else
7074 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7075 "expected %<,%> or %<)%>");
7077 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
7078 OMP_CLAUSE_CHAIN (c) = list;
7079 return c;
7081 invalid_kind:
7082 c_parser_error (parser, "invalid schedule kind");
7083 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
7084 return list;
7087 /* OpenMP 2.5:
7088 shared ( variable-list ) */
7090 static tree
7091 c_parser_omp_clause_shared (c_parser *parser, tree list)
7093 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_SHARED, list);
7096 /* Parse all OpenMP clauses. The set clauses allowed by the directive
7097 is a bitmask in MASK. Return the list of clauses found; the result
7098 of clause default goes in *pdefault. */
7100 static tree
7101 c_parser_omp_all_clauses (c_parser *parser, unsigned int mask,
7102 const char *where)
7104 tree clauses = NULL;
7106 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
7108 const pragma_omp_clause c_kind = c_parser_omp_clause_name (parser);
7109 const char *c_name;
7110 tree prev = clauses;
7112 switch (c_kind)
7114 case PRAGMA_OMP_CLAUSE_COPYIN:
7115 clauses = c_parser_omp_clause_copyin (parser, clauses);
7116 c_name = "copyin";
7117 break;
7118 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
7119 clauses = c_parser_omp_clause_copyprivate (parser, clauses);
7120 c_name = "copyprivate";
7121 break;
7122 case PRAGMA_OMP_CLAUSE_DEFAULT:
7123 clauses = c_parser_omp_clause_default (parser, clauses);
7124 c_name = "default";
7125 break;
7126 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
7127 clauses = c_parser_omp_clause_firstprivate (parser, clauses);
7128 c_name = "firstprivate";
7129 break;
7130 case PRAGMA_OMP_CLAUSE_IF:
7131 clauses = c_parser_omp_clause_if (parser, clauses);
7132 c_name = "if";
7133 break;
7134 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
7135 clauses = c_parser_omp_clause_lastprivate (parser, clauses);
7136 c_name = "lastprivate";
7137 break;
7138 case PRAGMA_OMP_CLAUSE_NOWAIT:
7139 clauses = c_parser_omp_clause_nowait (parser, clauses);
7140 c_name = "nowait";
7141 break;
7142 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
7143 clauses = c_parser_omp_clause_num_threads (parser, clauses);
7144 c_name = "num_threads";
7145 break;
7146 case PRAGMA_OMP_CLAUSE_ORDERED:
7147 clauses = c_parser_omp_clause_ordered (parser, clauses);
7148 c_name = "ordered";
7149 break;
7150 case PRAGMA_OMP_CLAUSE_PRIVATE:
7151 clauses = c_parser_omp_clause_private (parser, clauses);
7152 c_name = "private";
7153 break;
7154 case PRAGMA_OMP_CLAUSE_REDUCTION:
7155 clauses = c_parser_omp_clause_reduction (parser, clauses);
7156 c_name = "reduction";
7157 break;
7158 case PRAGMA_OMP_CLAUSE_SCHEDULE:
7159 clauses = c_parser_omp_clause_schedule (parser, clauses);
7160 c_name = "schedule";
7161 break;
7162 case PRAGMA_OMP_CLAUSE_SHARED:
7163 clauses = c_parser_omp_clause_shared (parser, clauses);
7164 c_name = "shared";
7165 break;
7166 default:
7167 c_parser_error (parser, "expected %<#pragma omp%> clause");
7168 goto saw_error;
7171 if (((mask >> c_kind) & 1) == 0 && !parser->error)
7173 /* Remove the invalid clause(s) from the list to avoid
7174 confusing the rest of the compiler. */
7175 clauses = prev;
7176 error ("%qs is not valid for %qs", c_name, where);
7180 saw_error:
7181 c_parser_skip_to_pragma_eol (parser);
7183 return c_finish_omp_clauses (clauses);
7186 /* OpenMP 2.5:
7187 structured-block:
7188 statement
7190 In practice, we're also interested in adding the statement to an
7191 outer node. So it is convenient if we work around the fact that
7192 c_parser_statement calls add_stmt. */
7194 static tree
7195 c_parser_omp_structured_block (c_parser *parser)
7197 tree stmt = push_stmt_list ();
7198 c_parser_statement (parser);
7199 return pop_stmt_list (stmt);
7202 /* OpenMP 2.5:
7203 # pragma omp atomic new-line
7204 expression-stmt
7206 expression-stmt:
7207 x binop= expr | x++ | ++x | x-- | --x
7208 binop:
7209 +, *, -, /, &, ^, |, <<, >>
7211 where x is an lvalue expression with scalar type. */
7213 static void
7214 c_parser_omp_atomic (c_parser *parser)
7216 tree lhs, rhs;
7217 enum tree_code code;
7219 c_parser_skip_to_pragma_eol (parser);
7221 lhs = c_parser_unary_expression (parser).value;
7222 switch (TREE_CODE (lhs))
7224 case ERROR_MARK:
7225 saw_error:
7226 c_parser_skip_to_end_of_block_or_statement (parser);
7227 return;
7229 case PREINCREMENT_EXPR:
7230 case POSTINCREMENT_EXPR:
7231 lhs = TREE_OPERAND (lhs, 0);
7232 code = PLUS_EXPR;
7233 rhs = integer_one_node;
7234 break;
7236 case PREDECREMENT_EXPR:
7237 case POSTDECREMENT_EXPR:
7238 lhs = TREE_OPERAND (lhs, 0);
7239 code = MINUS_EXPR;
7240 rhs = integer_one_node;
7241 break;
7243 default:
7244 switch (c_parser_peek_token (parser)->type)
7246 case CPP_MULT_EQ:
7247 code = MULT_EXPR;
7248 break;
7249 case CPP_DIV_EQ:
7250 code = TRUNC_DIV_EXPR;
7251 break;
7252 case CPP_PLUS_EQ:
7253 code = PLUS_EXPR;
7254 break;
7255 case CPP_MINUS_EQ:
7256 code = MINUS_EXPR;
7257 break;
7258 case CPP_LSHIFT_EQ:
7259 code = LSHIFT_EXPR;
7260 break;
7261 case CPP_RSHIFT_EQ:
7262 code = RSHIFT_EXPR;
7263 break;
7264 case CPP_AND_EQ:
7265 code = BIT_AND_EXPR;
7266 break;
7267 case CPP_OR_EQ:
7268 code = BIT_IOR_EXPR;
7269 break;
7270 case CPP_XOR_EQ:
7271 code = BIT_XOR_EXPR;
7272 break;
7273 default:
7274 c_parser_error (parser,
7275 "invalid operator for %<#pragma omp atomic%>");
7276 goto saw_error;
7279 c_parser_consume_token (parser);
7280 rhs = c_parser_expression (parser).value;
7281 break;
7283 c_finish_omp_atomic (code, lhs, rhs);
7284 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
7288 /* OpenMP 2.5:
7289 # pragma omp barrier new-line
7292 static void
7293 c_parser_omp_barrier (c_parser *parser)
7295 c_parser_consume_pragma (parser);
7296 c_parser_skip_to_pragma_eol (parser);
7298 c_finish_omp_barrier ();
7301 /* OpenMP 2.5:
7302 # pragma omp critical [(name)] new-line
7303 structured-block
7306 static tree
7307 c_parser_omp_critical (c_parser *parser)
7309 tree stmt, name = NULL;
7311 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
7313 c_parser_consume_token (parser);
7314 if (c_parser_next_token_is (parser, CPP_NAME))
7316 name = c_parser_peek_token (parser)->value;
7317 c_parser_consume_token (parser);
7318 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
7320 else
7321 c_parser_error (parser, "expected identifier");
7323 else if (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
7324 c_parser_error (parser, "expected %<(%> or end of line");
7325 c_parser_skip_to_pragma_eol (parser);
7327 stmt = c_parser_omp_structured_block (parser);
7328 return c_finish_omp_critical (stmt, name);
7331 /* OpenMP 2.5:
7332 # pragma omp flush flush-vars[opt] new-line
7334 flush-vars:
7335 ( variable-list ) */
7337 static void
7338 c_parser_omp_flush (c_parser *parser)
7340 c_parser_consume_pragma (parser);
7341 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
7342 c_parser_omp_var_list_parens (parser, 0, NULL);
7343 else if (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
7344 c_parser_error (parser, "expected %<(%> or end of line");
7345 c_parser_skip_to_pragma_eol (parser);
7347 c_finish_omp_flush ();
7350 /* Parse the restricted form of the for statment allowed by OpenMP.
7351 The real trick here is to determine the loop control variable early
7352 so that we can push a new decl if necessary to make it private. */
7354 static tree
7355 c_parser_omp_for_loop (c_parser *parser)
7357 tree decl, cond, incr, save_break, save_cont, body, init;
7358 location_t loc;
7360 if (!c_parser_next_token_is_keyword (parser, RID_FOR))
7362 c_parser_error (parser, "for statement expected");
7363 return NULL;
7365 loc = c_parser_peek_token (parser)->location;
7366 c_parser_consume_token (parser);
7368 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7369 return NULL;
7371 /* Parse the initialization declaration or expression. */
7372 if (c_parser_next_token_starts_declspecs (parser))
7374 c_parser_declaration_or_fndef (parser, true, true, true, true);
7375 decl = check_for_loop_decls ();
7376 if (decl == NULL)
7377 goto error_init;
7378 init = decl;
7380 else if (c_parser_next_token_is (parser, CPP_NAME)
7381 && c_parser_peek_2nd_token (parser)->type == CPP_EQ)
7383 decl = c_parser_postfix_expression (parser).value;
7385 c_parser_require (parser, CPP_EQ, "expected %<=%>");
7387 init = c_parser_expr_no_commas (parser, NULL).value;
7388 init = build_modify_expr (decl, NOP_EXPR, init);
7389 init = c_process_expr_stmt (init);
7391 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
7393 else
7394 goto error_init;
7396 /* Parse the loop condition. */
7397 cond = NULL_TREE;
7398 if (c_parser_next_token_is_not (parser, CPP_SEMICOLON))
7400 cond = c_parser_expression_conv (parser).value;
7401 cond = c_objc_common_truthvalue_conversion (cond);
7402 if (EXPR_P (cond))
7403 SET_EXPR_LOCATION (cond, input_location);
7405 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
7407 /* Parse the increment expression. */
7408 incr = NULL_TREE;
7409 if (c_parser_next_token_is_not (parser, CPP_CLOSE_PAREN))
7410 incr = c_process_expr_stmt (c_parser_expression (parser).value);
7411 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
7413 parse_body:
7414 save_break = c_break_label;
7415 c_break_label = size_one_node;
7416 save_cont = c_cont_label;
7417 c_cont_label = NULL_TREE;
7418 body = push_stmt_list ();
7420 add_stmt (c_parser_c99_block_statement (parser));
7421 if (c_cont_label)
7422 add_stmt (build1 (LABEL_EXPR, void_type_node, c_cont_label));
7424 body = pop_stmt_list (body);
7425 c_break_label = save_break;
7426 c_cont_label = save_cont;
7428 /* Only bother calling c_finish_omp_for if we havn't already generated
7429 an error from the initialization parsing. */
7430 if (decl != NULL && decl != error_mark_node && init != error_mark_node)
7431 return c_finish_omp_for (loc, decl, init, cond, incr, body, NULL);
7432 return NULL;
7434 error_init:
7435 c_parser_error (parser, "expected iteration declaration or initialization");
7436 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
7437 decl = init = cond = incr = NULL_TREE;
7438 goto parse_body;
7441 /* OpenMP 2.5:
7442 #pragma omp for for-clause[optseq] new-line
7443 for-loop
7446 #define OMP_FOR_CLAUSE_MASK \
7447 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
7448 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
7449 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
7450 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
7451 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \
7452 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \
7453 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
7455 static tree
7456 c_parser_omp_for (c_parser *parser)
7458 tree block, clauses, ret;
7460 clauses = c_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
7461 "#pragma omp for");
7463 block = c_begin_compound_stmt (true);
7464 ret = c_parser_omp_for_loop (parser);
7465 if (ret)
7466 OMP_FOR_CLAUSES (ret) = clauses;
7467 block = c_end_compound_stmt (block, true);
7468 add_stmt (block);
7470 return ret;
7473 /* OpenMP 2.5:
7474 # pragma omp master new-line
7475 structured-block
7478 static tree
7479 c_parser_omp_master (c_parser *parser)
7481 c_parser_skip_to_pragma_eol (parser);
7482 return c_finish_omp_master (c_parser_omp_structured_block (parser));
7485 /* OpenMP 2.5:
7486 # pragma omp ordered new-line
7487 structured-block
7490 static tree
7491 c_parser_omp_ordered (c_parser *parser)
7493 c_parser_skip_to_pragma_eol (parser);
7494 return c_finish_omp_ordered (c_parser_omp_structured_block (parser));
7497 /* OpenMP 2.5:
7499 section-scope:
7500 { section-sequence }
7502 section-sequence:
7503 section-directive[opt] structured-block
7504 section-sequence section-directive structured-block */
7506 static tree
7507 c_parser_omp_sections_scope (c_parser *parser)
7509 tree stmt, substmt;
7510 bool error_suppress = false;
7511 location_t loc;
7513 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
7515 /* Avoid skipping until the end of the block. */
7516 parser->error = false;
7517 return NULL_TREE;
7520 stmt = push_stmt_list ();
7522 loc = c_parser_peek_token (parser)->location;
7523 if (c_parser_peek_token (parser)->pragma_kind != PRAGMA_OMP_SECTION)
7525 substmt = push_stmt_list ();
7527 while (1)
7529 c_parser_statement (parser);
7531 if (c_parser_peek_token (parser)->pragma_kind == PRAGMA_OMP_SECTION)
7532 break;
7533 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
7534 break;
7535 if (c_parser_next_token_is (parser, CPP_EOF))
7536 break;
7539 substmt = pop_stmt_list (substmt);
7540 substmt = build1 (OMP_SECTION, void_type_node, substmt);
7541 SET_EXPR_LOCATION (substmt, loc);
7542 add_stmt (substmt);
7545 while (1)
7547 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
7548 break;
7549 if (c_parser_next_token_is (parser, CPP_EOF))
7550 break;
7552 loc = c_parser_peek_token (parser)->location;
7553 if (c_parser_peek_token (parser)->pragma_kind == PRAGMA_OMP_SECTION)
7555 c_parser_consume_pragma (parser);
7556 c_parser_skip_to_pragma_eol (parser);
7557 error_suppress = false;
7559 else if (!error_suppress)
7561 error ("expected %<#pragma omp section%> or %<}%>");
7562 error_suppress = true;
7565 substmt = c_parser_omp_structured_block (parser);
7566 substmt = build1 (OMP_SECTION, void_type_node, substmt);
7567 SET_EXPR_LOCATION (substmt, loc);
7568 add_stmt (substmt);
7570 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE,
7571 "expected %<#pragma omp section%> or %<}%>");
7573 substmt = pop_stmt_list (stmt);
7575 stmt = make_node (OMP_SECTIONS);
7576 TREE_TYPE (stmt) = void_type_node;
7577 OMP_SECTIONS_BODY (stmt) = substmt;
7579 return add_stmt (stmt);
7582 /* OpenMP 2.5:
7583 # pragma omp sections sections-clause[optseq] newline
7584 sections-scope
7587 #define OMP_SECTIONS_CLAUSE_MASK \
7588 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
7589 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
7590 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
7591 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
7592 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
7594 static tree
7595 c_parser_omp_sections (c_parser *parser)
7597 tree block, clauses, ret;
7599 clauses = c_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
7600 "#pragma omp sections");
7602 block = c_begin_compound_stmt (true);
7603 ret = c_parser_omp_sections_scope (parser);
7604 if (ret)
7605 OMP_SECTIONS_CLAUSES (ret) = clauses;
7606 block = c_end_compound_stmt (block, true);
7607 add_stmt (block);
7609 return ret;
7612 /* OpenMP 2.5:
7613 # pragma parallel parallel-clause new-line
7614 # pragma parallel for parallel-for-clause new-line
7615 # pragma parallel sections parallel-sections-clause new-line
7618 #define OMP_PARALLEL_CLAUSE_MASK \
7619 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
7620 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
7621 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
7622 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
7623 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
7624 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \
7625 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
7626 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
7628 static tree
7629 c_parser_omp_parallel (c_parser *parser)
7631 enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
7632 const char *p_name = "#pragma omp parallel";
7633 tree stmt, clauses, par_clause, ws_clause, block;
7634 unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
7636 if (c_parser_next_token_is_keyword (parser, RID_FOR))
7638 c_parser_consume_token (parser);
7639 p_kind = PRAGMA_OMP_PARALLEL_FOR;
7640 p_name = "#pragma omp parallel for";
7641 mask |= OMP_FOR_CLAUSE_MASK;
7642 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
7644 else if (c_parser_next_token_is (parser, CPP_NAME))
7646 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
7647 if (strcmp (p, "sections") == 0)
7649 c_parser_consume_token (parser);
7650 p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
7651 p_name = "#pragma omp parallel sections";
7652 mask |= OMP_SECTIONS_CLAUSE_MASK;
7653 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
7657 clauses = c_parser_omp_all_clauses (parser, mask, p_name);
7659 switch (p_kind)
7661 case PRAGMA_OMP_PARALLEL:
7662 block = c_begin_omp_parallel ();
7663 c_parser_statement (parser);
7664 stmt = c_finish_omp_parallel (clauses, block);
7665 break;
7667 case PRAGMA_OMP_PARALLEL_FOR:
7668 block = c_begin_omp_parallel ();
7669 c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
7670 stmt = c_parser_omp_for_loop (parser);
7671 if (stmt)
7672 OMP_FOR_CLAUSES (stmt) = ws_clause;
7673 stmt = c_finish_omp_parallel (par_clause, block);
7674 OMP_PARALLEL_COMBINED (stmt) = 1;
7675 break;
7677 case PRAGMA_OMP_PARALLEL_SECTIONS:
7678 block = c_begin_omp_parallel ();
7679 c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
7680 stmt = c_parser_omp_sections_scope (parser);
7681 if (stmt)
7682 OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
7683 stmt = c_finish_omp_parallel (par_clause, block);
7684 OMP_PARALLEL_COMBINED (stmt) = 1;
7685 break;
7687 default:
7688 gcc_unreachable ();
7691 return stmt;
7694 /* OpenMP 2.5:
7695 # pragma omp single single-clause[optseq] new-line
7696 structured-block
7699 #define OMP_SINGLE_CLAUSE_MASK \
7700 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
7701 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
7702 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
7703 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
7705 static tree
7706 c_parser_omp_single (c_parser *parser)
7708 tree stmt = make_node (OMP_SINGLE);
7709 TREE_TYPE (stmt) = void_type_node;
7711 OMP_SINGLE_CLAUSES (stmt)
7712 = c_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
7713 "#pragma omp single");
7714 OMP_SINGLE_BODY (stmt) = c_parser_omp_structured_block (parser);
7716 return add_stmt (stmt);
7720 /* Main entry point to parsing most OpenMP pragmas. */
7722 static void
7723 c_parser_omp_construct (c_parser *parser)
7725 enum pragma_kind p_kind;
7726 location_t loc;
7727 tree stmt;
7729 loc = c_parser_peek_token (parser)->location;
7730 p_kind = c_parser_peek_token (parser)->pragma_kind;
7731 c_parser_consume_pragma (parser);
7733 switch (p_kind)
7735 case PRAGMA_OMP_ATOMIC:
7736 c_parser_omp_atomic (parser);
7737 return;
7738 case PRAGMA_OMP_CRITICAL:
7739 stmt = c_parser_omp_critical (parser);
7740 break;
7741 case PRAGMA_OMP_FOR:
7742 stmt = c_parser_omp_for (parser);
7743 break;
7744 case PRAGMA_OMP_MASTER:
7745 stmt = c_parser_omp_master (parser);
7746 break;
7747 case PRAGMA_OMP_ORDERED:
7748 stmt = c_parser_omp_ordered (parser);
7749 break;
7750 case PRAGMA_OMP_PARALLEL:
7751 stmt = c_parser_omp_parallel (parser);
7752 break;
7753 case PRAGMA_OMP_SECTIONS:
7754 stmt = c_parser_omp_sections (parser);
7755 break;
7756 case PRAGMA_OMP_SINGLE:
7757 stmt = c_parser_omp_single (parser);
7758 break;
7759 default:
7760 gcc_unreachable ();
7763 if (stmt)
7764 SET_EXPR_LOCATION (stmt, loc);
7768 /* OpenMP 2.5:
7769 # pragma omp threadprivate (variable-list) */
7771 static void
7772 c_parser_omp_threadprivate (c_parser *parser)
7774 tree vars, t;
7776 c_parser_consume_pragma (parser);
7777 vars = c_parser_omp_var_list_parens (parser, 0, NULL);
7779 if (!targetm.have_tls)
7780 sorry ("threadprivate variables not supported in this target");
7782 /* Mark every variable in VARS to be assigned thread local storage. */
7783 for (t = vars; t; t = TREE_CHAIN (t))
7785 tree v = TREE_PURPOSE (t);
7787 /* If V had already been marked threadprivate, it doesn't matter
7788 whether it had been used prior to this point. */
7789 if (TREE_USED (v) && !C_DECL_THREADPRIVATE_P (v))
7790 error ("%qE declared %<threadprivate%> after first use", v);
7791 else if (! TREE_STATIC (v) && ! DECL_EXTERNAL (v))
7792 error ("automatic variable %qE cannot be %<threadprivate%>", v);
7793 else if (! COMPLETE_TYPE_P (TREE_TYPE (v)))
7794 error ("%<threadprivate%> %qE has incomplete type", v);
7795 else
7797 if (! DECL_THREAD_LOCAL_P (v))
7799 DECL_TLS_MODEL (v) = decl_default_tls_model (v);
7800 /* If rtl has been already set for this var, call
7801 make_decl_rtl once again, so that encode_section_info
7802 has a chance to look at the new decl flags. */
7803 if (DECL_RTL_SET_P (v))
7804 make_decl_rtl (v);
7806 C_DECL_THREADPRIVATE_P (v) = 1;
7810 c_parser_skip_to_pragma_eol (parser);
7814 /* Parse a single source file. */
7816 void
7817 c_parse_file (void)
7819 /* Use local storage to begin. If the first token is a pragma, parse it.
7820 If it is #pragma GCC pch_preprocess, then this will load a PCH file
7821 which will cause garbage collection. */
7822 c_parser tparser;
7824 memset (&tparser, 0, sizeof tparser);
7825 the_parser = &tparser;
7827 if (c_parser_peek_token (&tparser)->pragma_kind == PRAGMA_GCC_PCH_PREPROCESS)
7828 c_parser_pragma_pch_preprocess (&tparser);
7830 the_parser = GGC_NEW (c_parser);
7831 *the_parser = tparser;
7833 c_parser_translation_unit (the_parser);
7834 the_parser = NULL;
7837 #include "gt-c-parser.h"