Merge trunk version 190707 into gupc branch.
[official-gcc.git] / gcc / c / c-parser.c
blobde74d7221fb9eb26db4de1e8866723ee5980abc4
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, 2007, 2008, 2009, 2010, 2011,
4 2012 Free Software Foundation, Inc.
6 Parser actions based on the old Bison parser; structure somewhat
7 influenced by and fragments based on the C++ parser.
9 This file is part of GCC.
11 GCC is free software; you can redistribute it and/or modify it under
12 the terms of the GNU General Public License as published by the Free
13 Software Foundation; either version 3, or (at your option) any later
14 version.
16 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
17 WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19 for more details.
21 You should have received a copy of the GNU General Public License
22 along with GCC; see the file COPYING3. If not see
23 <http://www.gnu.org/licenses/>. */
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" /* For rtl.h: needs enum reg_class. */
44 #include "tree.h"
45 #include "langhooks.h"
46 #include "input.h"
47 #include "cpplib.h"
48 #include "timevar.h"
49 #include "c-family/c-pragma.h"
50 #include "c-tree.h"
51 #include "flags.h"
52 #include "ggc.h"
53 #include "c-family/c-common.h"
54 #include "c-family/c-objc.h"
55 #include "c-family/c-upc.h"
56 #include "vec.h"
57 #include "target.h"
58 #include "cgraph.h"
59 #include "plugin.h"
61 #include "upc/upc-tree.h"
64 /* Initialization routine for this file. */
66 void
67 c_parse_init (void)
69 /* The only initialization required is of the reserved word
70 identifiers. */
71 unsigned int i;
72 tree id;
73 int mask = 0;
75 /* Make sure RID_MAX hasn't grown past the 8 bits used to hold the keyword in
76 the c_token structure. */
77 gcc_assert (RID_MAX <= 255);
79 mask |= D_CXXONLY;
80 if (!flag_isoc99)
81 mask |= D_C99;
82 if (flag_no_asm)
84 mask |= D_ASM | D_EXT;
85 if (!flag_isoc99)
86 mask |= D_EXT89;
88 if (!c_dialect_objc ())
89 mask |= D_OBJC | D_CXX_OBJC;
91 if (!c_dialect_upc ())
92 mask |= D_UPC;
94 ridpointers = ggc_alloc_cleared_vec_tree ((int) RID_MAX);
95 for (i = 0; i < num_c_common_reswords; i++)
97 /* If a keyword is disabled, do not enter it into the table
98 and so create a canonical spelling that isn't a keyword. */
99 if (c_common_reswords[i].disable & mask)
101 if (warn_cxx_compat
102 && (c_common_reswords[i].disable & D_CXXWARN))
104 id = get_identifier (c_common_reswords[i].word);
105 C_SET_RID_CODE (id, RID_CXX_COMPAT_WARN);
106 C_IS_RESERVED_WORD (id) = 1;
108 continue;
111 id = get_identifier (c_common_reswords[i].word);
112 C_SET_RID_CODE (id, c_common_reswords[i].rid);
113 C_IS_RESERVED_WORD (id) = 1;
114 ridpointers [(int) c_common_reswords[i].rid] = id;
118 /* The C lexer intermediates between the lexer in cpplib and c-lex.c
119 and the C parser. Unlike the C++ lexer, the parser structure
120 stores the lexer information instead of using a separate structure.
121 Identifiers are separated into ordinary identifiers, type names,
122 keywords and some other Objective-C types of identifiers, and some
123 look-ahead is maintained.
125 ??? It might be a good idea to lex the whole file up front (as for
126 C++). It would then be possible to share more of the C and C++
127 lexer code, if desired. */
129 /* The following local token type is used. */
131 /* A keyword. */
132 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
134 /* More information about the type of a CPP_NAME token. */
135 typedef enum c_id_kind {
136 /* An ordinary identifier. */
137 C_ID_ID,
138 /* An identifier declared as a typedef name. */
139 C_ID_TYPENAME,
140 /* An identifier declared as an Objective-C class name. */
141 C_ID_CLASSNAME,
142 /* An address space identifier. */
143 C_ID_ADDRSPACE,
144 /* Not an identifier. */
145 C_ID_NONE
146 } c_id_kind;
148 /* A single C token after string literal concatenation and conversion
149 of preprocessing tokens to tokens. */
150 typedef struct GTY (()) c_token {
151 /* The kind of token. */
152 ENUM_BITFIELD (cpp_ttype) type : 8;
153 /* If this token is a CPP_NAME, this value indicates whether also
154 declared as some kind of type. Otherwise, it is C_ID_NONE. */
155 ENUM_BITFIELD (c_id_kind) id_kind : 8;
156 /* If this token is a keyword, this value indicates which keyword.
157 Otherwise, this value is RID_MAX. */
158 ENUM_BITFIELD (rid) keyword : 8;
159 /* If this token is a CPP_PRAGMA, this indicates the pragma that
160 was seen. Otherwise it is PRAGMA_NONE. */
161 ENUM_BITFIELD (pragma_kind) pragma_kind : 8;
162 /* The location at which this token was found. */
163 location_t location;
164 /* The value associated with this token, if any. */
165 tree value;
166 } c_token;
168 /* A parser structure recording information about the state and
169 context of parsing. Includes lexer information with up to two
170 tokens of look-ahead; more are not needed for C. */
171 typedef struct GTY(()) c_parser {
172 /* The look-ahead tokens. */
173 c_token tokens[2];
174 /* How many look-ahead tokens are available (0, 1 or 2). */
175 short tokens_avail;
176 /* True if a syntax error is being recovered from; false otherwise.
177 c_parser_error sets this flag. It should clear this flag when
178 enough tokens have been consumed to recover from the error. */
179 BOOL_BITFIELD error : 1;
180 /* True if we're processing a pragma, and shouldn't automatically
181 consume CPP_PRAGMA_EOL. */
182 BOOL_BITFIELD in_pragma : 1;
183 /* True if we're parsing the outermost block of an if statement. */
184 BOOL_BITFIELD in_if_block : 1;
185 /* True if we want to lex an untranslated string. */
186 BOOL_BITFIELD lex_untranslated_string : 1;
188 /* Objective-C specific parser/lexer information. */
190 /* True if we are in a context where the Objective-C "PQ" keywords
191 are considered keywords. */
192 BOOL_BITFIELD objc_pq_context : 1;
193 /* True if we are parsing a (potential) Objective-C foreach
194 statement. This is set to true after we parsed 'for (' and while
195 we wait for 'in' or ';' to decide if it's a standard C for loop or an
196 Objective-C foreach loop. */
197 BOOL_BITFIELD objc_could_be_foreach_context : 1;
198 /* The following flag is needed to contextualize Objective-C lexical
199 analysis. In some cases (e.g., 'int NSObject;'), it is
200 undesirable to bind an identifier to an Objective-C class, even
201 if a class with that name exists. */
202 BOOL_BITFIELD objc_need_raw_identifier : 1;
203 /* Nonzero if we're processing a __transaction statement. The value
204 is 1 | TM_STMT_ATTR_*. */
205 unsigned int in_transaction : 4;
206 /* True if we are in a context where the Objective-C "Property attribute"
207 keywords are valid. */
208 BOOL_BITFIELD objc_property_attr_context : 1;
209 } c_parser;
212 /* The actual parser and external interface. ??? Does this need to be
213 garbage-collected? */
215 static GTY (()) c_parser *the_parser;
217 /* Read in and lex a single token, storing it in *TOKEN. */
219 static void
220 c_lex_one_token (c_parser *parser, c_token *token)
222 timevar_push (TV_LEX);
224 token->type = c_lex_with_flags (&token->value, &token->location, NULL,
225 (parser->lex_untranslated_string
226 ? C_LEX_STRING_NO_TRANSLATE : 0));
227 token->id_kind = C_ID_NONE;
228 token->keyword = RID_MAX;
229 token->pragma_kind = PRAGMA_NONE;
231 switch (token->type)
233 case CPP_NAME:
235 tree decl;
237 bool objc_force_identifier = parser->objc_need_raw_identifier;
238 if (c_dialect_objc ())
239 parser->objc_need_raw_identifier = false;
241 if (C_IS_RESERVED_WORD (token->value))
243 enum rid rid_code = C_RID_CODE (token->value);
245 if (rid_code == RID_CXX_COMPAT_WARN)
247 warning_at (token->location,
248 OPT_Wc___compat,
249 "identifier %qE conflicts with C++ keyword",
250 token->value);
252 else if (rid_code >= RID_FIRST_ADDR_SPACE
253 && rid_code <= RID_LAST_ADDR_SPACE)
255 token->id_kind = C_ID_ADDRSPACE;
256 token->keyword = rid_code;
257 break;
259 else if (c_dialect_objc () && OBJC_IS_PQ_KEYWORD (rid_code))
261 /* We found an Objective-C "pq" keyword (in, out,
262 inout, bycopy, byref, oneway). They need special
263 care because the interpretation depends on the
264 context. */
265 if (parser->objc_pq_context)
267 token->type = CPP_KEYWORD;
268 token->keyword = rid_code;
269 break;
271 else if (parser->objc_could_be_foreach_context
272 && rid_code == RID_IN)
274 /* We are in Objective-C, inside a (potential)
275 foreach context (which means after having
276 parsed 'for (', but before having parsed ';'),
277 and we found 'in'. We consider it the keyword
278 which terminates the declaration at the
279 beginning of a foreach-statement. Note that
280 this means you can't use 'in' for anything else
281 in that context; in particular, in Objective-C
282 you can't use 'in' as the name of the running
283 variable in a C for loop. We could potentially
284 try to add code here to disambiguate, but it
285 seems a reasonable limitation. */
286 token->type = CPP_KEYWORD;
287 token->keyword = rid_code;
288 break;
290 /* Else, "pq" keywords outside of the "pq" context are
291 not keywords, and we fall through to the code for
292 normal tokens. */
294 else if (c_dialect_objc () && OBJC_IS_PATTR_KEYWORD (rid_code))
296 /* We found an Objective-C "property attribute"
297 keyword (getter, setter, readonly, etc). These are
298 only valid in the property context. */
299 if (parser->objc_property_attr_context)
301 token->type = CPP_KEYWORD;
302 token->keyword = rid_code;
303 break;
305 /* Else they are not special keywords.
308 else if (c_dialect_objc ()
309 && (OBJC_IS_AT_KEYWORD (rid_code)
310 || OBJC_IS_CXX_KEYWORD (rid_code)))
312 /* We found one of the Objective-C "@" keywords (defs,
313 selector, synchronized, etc) or one of the
314 Objective-C "cxx" keywords (class, private,
315 protected, public, try, catch, throw) without a
316 preceding '@' sign. Do nothing and fall through to
317 the code for normal tokens (in C++ we would still
318 consider the CXX ones keywords, but not in C). */
321 else
323 token->type = CPP_KEYWORD;
324 token->keyword = rid_code;
325 break;
329 decl = lookup_name (token->value);
330 if (decl)
332 if (TREE_CODE (decl) == TYPE_DECL)
334 token->id_kind = C_ID_TYPENAME;
335 break;
338 else if (c_dialect_objc ())
340 tree objc_interface_decl = objc_is_class_name (token->value);
341 /* Objective-C class names are in the same namespace as
342 variables and typedefs, and hence are shadowed by local
343 declarations. */
344 if (objc_interface_decl
345 && (!objc_force_identifier || global_bindings_p ()))
347 token->value = objc_interface_decl;
348 token->id_kind = C_ID_CLASSNAME;
349 break;
352 token->id_kind = C_ID_ID;
354 break;
355 case CPP_AT_NAME:
356 /* This only happens in Objective-C; it must be a keyword. */
357 token->type = CPP_KEYWORD;
358 switch (C_RID_CODE (token->value))
360 /* Replace 'class' with '@class', 'private' with '@private',
361 etc. This prevents confusion with the C++ keyword
362 'class', and makes the tokens consistent with other
363 Objective-C 'AT' keywords. For example '@class' is
364 reported as RID_AT_CLASS which is consistent with
365 '@synchronized', which is reported as
366 RID_AT_SYNCHRONIZED.
368 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
369 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
370 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
371 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
372 case RID_THROW: token->keyword = RID_AT_THROW; break;
373 case RID_TRY: token->keyword = RID_AT_TRY; break;
374 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
375 default: token->keyword = C_RID_CODE (token->value);
377 break;
378 case CPP_COLON:
379 case CPP_COMMA:
380 case CPP_CLOSE_PAREN:
381 case CPP_SEMICOLON:
382 /* These tokens may affect the interpretation of any identifiers
383 following, if doing Objective-C. */
384 if (c_dialect_objc ())
385 parser->objc_need_raw_identifier = false;
386 break;
387 case CPP_PRAGMA:
388 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
389 token->pragma_kind = (enum pragma_kind) TREE_INT_CST_LOW (token->value);
390 token->value = NULL;
391 break;
392 default:
393 break;
395 timevar_pop (TV_LEX);
398 /* Return a pointer to the next token from PARSER, reading it in if
399 necessary. */
401 static inline c_token *
402 c_parser_peek_token (c_parser *parser)
404 if (parser->tokens_avail == 0)
406 c_lex_one_token (parser, &parser->tokens[0]);
407 parser->tokens_avail = 1;
409 return &parser->tokens[0];
412 /* Return true if the next token from PARSER has the indicated
413 TYPE. */
415 static inline bool
416 c_parser_next_token_is (c_parser *parser, enum cpp_ttype type)
418 return c_parser_peek_token (parser)->type == type;
421 /* Return true if the next token from PARSER does not have the
422 indicated TYPE. */
424 static inline bool
425 c_parser_next_token_is_not (c_parser *parser, enum cpp_ttype type)
427 return !c_parser_next_token_is (parser, type);
430 /* Return true if the next token from PARSER is the indicated
431 KEYWORD. */
433 static inline bool
434 c_parser_next_token_is_keyword (c_parser *parser, enum rid keyword)
436 return c_parser_peek_token (parser)->keyword == keyword;
439 /* Return a pointer to the next-but-one token from PARSER, reading it
440 in if necessary. The next token is already read in. */
442 static c_token *
443 c_parser_peek_2nd_token (c_parser *parser)
445 if (parser->tokens_avail >= 2)
446 return &parser->tokens[1];
447 gcc_assert (parser->tokens_avail == 1);
448 gcc_assert (parser->tokens[0].type != CPP_EOF);
449 gcc_assert (parser->tokens[0].type != CPP_PRAGMA_EOL);
450 c_lex_one_token (parser, &parser->tokens[1]);
451 parser->tokens_avail = 2;
452 return &parser->tokens[1];
455 /* Return true if TOKEN can start a type name,
456 false otherwise. */
457 static bool
458 c_token_starts_typename (c_token *token)
460 switch (token->type)
462 case CPP_NAME:
463 switch (token->id_kind)
465 case C_ID_ID:
466 return false;
467 case C_ID_ADDRSPACE:
468 return true;
469 case C_ID_TYPENAME:
470 return true;
471 case C_ID_CLASSNAME:
472 gcc_assert (c_dialect_objc ());
473 return true;
474 default:
475 gcc_unreachable ();
477 case CPP_KEYWORD:
478 switch (token->keyword)
480 case RID_UNSIGNED:
481 case RID_LONG:
482 case RID_INT128:
483 case RID_SHORT:
484 case RID_SIGNED:
485 case RID_COMPLEX:
486 case RID_INT:
487 case RID_CHAR:
488 case RID_FLOAT:
489 case RID_DOUBLE:
490 case RID_VOID:
491 case RID_DFLOAT32:
492 case RID_DFLOAT64:
493 case RID_DFLOAT128:
494 case RID_BOOL:
495 case RID_ENUM:
496 case RID_STRUCT:
497 case RID_UNION:
498 case RID_TYPEOF:
499 case RID_CONST:
500 case RID_VOLATILE:
501 case RID_RESTRICT:
502 case RID_ATTRIBUTE:
503 case RID_FRACT:
504 case RID_ACCUM:
505 case RID_SAT:
506 return true;
507 /* UPC qualifiers */
508 case RID_SHARED:
509 case RID_STRICT:
510 case RID_RELAXED:
511 return true;
512 default:
513 return false;
515 case CPP_LESS:
516 if (c_dialect_objc ())
517 return true;
518 return false;
519 default:
520 return false;
524 enum c_lookahead_kind {
525 /* Always treat unknown identifiers as typenames. */
526 cla_prefer_type,
528 /* Could be parsing a nonabstract declarator. Only treat an identifier
529 as a typename if followed by another identifier or a star. */
530 cla_nonabstract_decl,
532 /* Never treat identifiers as typenames. */
533 cla_prefer_id
536 /* Return true if the next token from PARSER can start a type name,
537 false otherwise. LA specifies how to do lookahead in order to
538 detect unknown type names. If unsure, pick CLA_PREFER_ID. */
540 static inline bool
541 c_parser_next_tokens_start_typename (c_parser *parser, enum c_lookahead_kind la)
543 c_token *token = c_parser_peek_token (parser);
544 if (c_token_starts_typename (token))
545 return true;
547 /* Try a bit harder to detect an unknown typename. */
548 if (la != cla_prefer_id
549 && token->type == CPP_NAME
550 && token->id_kind == C_ID_ID
552 /* Do not try too hard when we could have "object in array". */
553 && !parser->objc_could_be_foreach_context
555 && (la == cla_prefer_type
556 || c_parser_peek_2nd_token (parser)->type == CPP_NAME
557 || c_parser_peek_2nd_token (parser)->type == CPP_MULT)
559 /* Only unknown identifiers. */
560 && !lookup_name (token->value))
561 return true;
563 return false;
566 /* Return true if TOKEN is a type qualifier, false otherwise. */
567 static bool
568 c_token_is_qualifier (c_token *token)
570 switch (token->type)
572 case CPP_NAME:
573 switch (token->id_kind)
575 case C_ID_ADDRSPACE:
576 return true;
577 default:
578 return false;
580 case CPP_KEYWORD:
581 switch (token->keyword)
583 case RID_CONST:
584 case RID_VOLATILE:
585 case RID_RESTRICT:
586 case RID_ATTRIBUTE:
587 return true;
588 case RID_SHARED:
589 case RID_STRICT:
590 case RID_RELAXED:
591 /* UPC qualifiers */
592 return true;
593 default:
594 return false;
596 case CPP_LESS:
597 return false;
598 default:
599 gcc_unreachable ();
603 /* Return true if the next token from PARSER is a type qualifier,
604 false otherwise. */
605 static inline bool
606 c_parser_next_token_is_qualifier (c_parser *parser)
608 c_token *token = c_parser_peek_token (parser);
609 return c_token_is_qualifier (token);
612 /* Return true if TOKEN can start declaration specifiers, false
613 otherwise. */
614 static bool
615 c_token_starts_declspecs (c_token *token)
617 switch (token->type)
619 case CPP_NAME:
620 switch (token->id_kind)
622 case C_ID_ID:
623 return false;
624 case C_ID_ADDRSPACE:
625 return true;
626 case C_ID_TYPENAME:
627 return true;
628 case C_ID_CLASSNAME:
629 gcc_assert (c_dialect_objc ());
630 return true;
631 default:
632 gcc_unreachable ();
634 case CPP_KEYWORD:
635 switch (token->keyword)
637 case RID_STATIC:
638 case RID_EXTERN:
639 case RID_REGISTER:
640 case RID_TYPEDEF:
641 case RID_INLINE:
642 case RID_NORETURN:
643 case RID_AUTO:
644 case RID_THREAD:
645 case RID_UNSIGNED:
646 case RID_LONG:
647 case RID_INT128:
648 case RID_SHORT:
649 case RID_SIGNED:
650 case RID_COMPLEX:
651 case RID_INT:
652 case RID_CHAR:
653 case RID_FLOAT:
654 case RID_DOUBLE:
655 case RID_VOID:
656 case RID_DFLOAT32:
657 case RID_DFLOAT64:
658 case RID_DFLOAT128:
659 case RID_BOOL:
660 case RID_ENUM:
661 case RID_STRUCT:
662 case RID_UNION:
663 case RID_TYPEOF:
664 case RID_CONST:
665 case RID_VOLATILE:
666 case RID_RESTRICT:
667 case RID_ATTRIBUTE:
668 case RID_FRACT:
669 case RID_ACCUM:
670 case RID_SAT:
671 case RID_ALIGNAS:
672 return true;
673 /* UPC qualifiers */
674 case RID_SHARED:
675 case RID_STRICT:
676 case RID_RELAXED:
677 return true;
678 default:
679 return false;
681 case CPP_LESS:
682 if (c_dialect_objc ())
683 return true;
684 return false;
685 default:
686 return false;
691 /* Return true if TOKEN can start declaration specifiers or a static
692 assertion, false otherwise. */
693 static bool
694 c_token_starts_declaration (c_token *token)
696 if (c_token_starts_declspecs (token)
697 || token->keyword == RID_STATIC_ASSERT)
698 return true;
699 else
700 return false;
703 /* Return true if the next token from PARSER can start declaration
704 specifiers, false otherwise. */
705 static inline bool
706 c_parser_next_token_starts_declspecs (c_parser *parser)
708 c_token *token = c_parser_peek_token (parser);
710 /* In Objective-C, a classname normally starts a declspecs unless it
711 is immediately followed by a dot. In that case, it is the
712 Objective-C 2.0 "dot-syntax" for class objects, ie, calls the
713 setter/getter on the class. c_token_starts_declspecs() can't
714 differentiate between the two cases because it only checks the
715 current token, so we have a special check here. */
716 if (c_dialect_objc ()
717 && token->type == CPP_NAME
718 && token->id_kind == C_ID_CLASSNAME
719 && c_parser_peek_2nd_token (parser)->type == CPP_DOT)
720 return false;
722 return c_token_starts_declspecs (token);
725 /* Return true if the next tokens from PARSER can start declaration
726 specifiers or a static assertion, false otherwise. */
727 static inline bool
728 c_parser_next_tokens_start_declaration (c_parser *parser)
730 c_token *token = c_parser_peek_token (parser);
732 /* Same as above. */
733 if (c_dialect_objc ()
734 && token->type == CPP_NAME
735 && token->id_kind == C_ID_CLASSNAME
736 && c_parser_peek_2nd_token (parser)->type == CPP_DOT)
737 return false;
739 /* Labels do not start declarations. */
740 if (token->type == CPP_NAME
741 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
742 return false;
744 if (c_token_starts_declaration (token))
745 return true;
747 if (c_parser_next_tokens_start_typename (parser, cla_nonabstract_decl))
748 return true;
750 return false;
753 /* Consume the next token from PARSER. */
755 static void
756 c_parser_consume_token (c_parser *parser)
758 gcc_assert (parser->tokens_avail >= 1);
759 gcc_assert (parser->tokens[0].type != CPP_EOF);
760 gcc_assert (!parser->in_pragma || parser->tokens[0].type != CPP_PRAGMA_EOL);
761 gcc_assert (parser->error || parser->tokens[0].type != CPP_PRAGMA);
762 if (parser->tokens_avail == 2)
763 parser->tokens[0] = parser->tokens[1];
764 parser->tokens_avail--;
767 /* Expect the current token to be a #pragma. Consume it and remember
768 that we've begun parsing a pragma. */
770 static void
771 c_parser_consume_pragma (c_parser *parser)
773 gcc_assert (!parser->in_pragma);
774 gcc_assert (parser->tokens_avail >= 1);
775 gcc_assert (parser->tokens[0].type == CPP_PRAGMA);
776 if (parser->tokens_avail == 2)
777 parser->tokens[0] = parser->tokens[1];
778 parser->tokens_avail--;
779 parser->in_pragma = true;
782 /* Update the globals input_location and in_system_header from
783 TOKEN. */
784 static inline void
785 c_parser_set_source_position_from_token (c_token *token)
787 if (token->type != CPP_EOF)
789 input_location = token->location;
793 /* Issue a diagnostic of the form
794 FILE:LINE: MESSAGE before TOKEN
795 where TOKEN is the next token in the input stream of PARSER.
796 MESSAGE (specified by the caller) is usually of the form "expected
797 OTHER-TOKEN".
799 Do not issue a diagnostic if still recovering from an error.
801 ??? This is taken from the C++ parser, but building up messages in
802 this way is not i18n-friendly and some other approach should be
803 used. */
805 static void
806 c_parser_error (c_parser *parser, const char *gmsgid)
808 c_token *token = c_parser_peek_token (parser);
809 if (parser->error)
810 return;
811 parser->error = true;
812 if (!gmsgid)
813 return;
814 /* This diagnostic makes more sense if it is tagged to the line of
815 the token we just peeked at. */
816 c_parser_set_source_position_from_token (token);
817 c_parse_error (gmsgid,
818 /* Because c_parse_error does not understand
819 CPP_KEYWORD, keywords are treated like
820 identifiers. */
821 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
822 /* ??? The C parser does not save the cpp flags of a
823 token, we need to pass 0 here and we will not get
824 the source spelling of some tokens but rather the
825 canonical spelling. */
826 token->value, /*flags=*/0);
829 /* If the next token is of the indicated TYPE, consume it. Otherwise,
830 issue the error MSGID. If MSGID is NULL then a message has already
831 been produced and no message will be produced this time. Returns
832 true if found, false otherwise. */
834 static bool
835 c_parser_require (c_parser *parser,
836 enum cpp_ttype type,
837 const char *msgid)
839 if (c_parser_next_token_is (parser, type))
841 c_parser_consume_token (parser);
842 return true;
844 else
846 c_parser_error (parser, msgid);
847 return false;
851 /* If the next token is the indicated keyword, consume it. Otherwise,
852 issue the error MSGID. Returns true if found, false otherwise. */
854 static bool
855 c_parser_require_keyword (c_parser *parser,
856 enum rid keyword,
857 const char *msgid)
859 if (c_parser_next_token_is_keyword (parser, keyword))
861 c_parser_consume_token (parser);
862 return true;
864 else
866 c_parser_error (parser, msgid);
867 return false;
871 /* Like c_parser_require, except that tokens will be skipped until the
872 desired token is found. An error message is still produced if the
873 next token is not as expected. If MSGID is NULL then a message has
874 already been produced and no message will be produced this
875 time. */
877 static void
878 c_parser_skip_until_found (c_parser *parser,
879 enum cpp_ttype type,
880 const char *msgid)
882 unsigned nesting_depth = 0;
884 if (c_parser_require (parser, type, msgid))
885 return;
887 /* Skip tokens until the desired token is found. */
888 while (true)
890 /* Peek at the next token. */
891 c_token *token = c_parser_peek_token (parser);
892 /* If we've reached the token we want, consume it and stop. */
893 if (token->type == type && !nesting_depth)
895 c_parser_consume_token (parser);
896 break;
899 /* If we've run out of tokens, stop. */
900 if (token->type == CPP_EOF)
901 return;
902 if (token->type == CPP_PRAGMA_EOL && parser->in_pragma)
903 return;
904 if (token->type == CPP_OPEN_BRACE
905 || token->type == CPP_OPEN_PAREN
906 || token->type == CPP_OPEN_SQUARE)
907 ++nesting_depth;
908 else if (token->type == CPP_CLOSE_BRACE
909 || token->type == CPP_CLOSE_PAREN
910 || token->type == CPP_CLOSE_SQUARE)
912 if (nesting_depth-- == 0)
913 break;
915 /* Consume this token. */
916 c_parser_consume_token (parser);
918 parser->error = false;
921 /* Skip tokens until the end of a parameter is found, but do not
922 consume the comma, semicolon or closing delimiter. */
924 static void
925 c_parser_skip_to_end_of_parameter (c_parser *parser)
927 unsigned nesting_depth = 0;
929 while (true)
931 c_token *token = c_parser_peek_token (parser);
932 if ((token->type == CPP_COMMA || token->type == CPP_SEMICOLON)
933 && !nesting_depth)
934 break;
935 /* If we've run out of tokens, stop. */
936 if (token->type == CPP_EOF)
937 return;
938 if (token->type == CPP_PRAGMA_EOL && parser->in_pragma)
939 return;
940 if (token->type == CPP_OPEN_BRACE
941 || token->type == CPP_OPEN_PAREN
942 || token->type == CPP_OPEN_SQUARE)
943 ++nesting_depth;
944 else if (token->type == CPP_CLOSE_BRACE
945 || token->type == CPP_CLOSE_PAREN
946 || token->type == CPP_CLOSE_SQUARE)
948 if (nesting_depth-- == 0)
949 break;
951 /* Consume this token. */
952 c_parser_consume_token (parser);
954 parser->error = false;
957 /* Expect to be at the end of the pragma directive and consume an
958 end of line marker. */
960 static void
961 c_parser_skip_to_pragma_eol (c_parser *parser)
963 gcc_assert (parser->in_pragma);
964 parser->in_pragma = false;
966 if (!c_parser_require (parser, CPP_PRAGMA_EOL, "expected end of line"))
967 while (true)
969 c_token *token = c_parser_peek_token (parser);
970 if (token->type == CPP_EOF)
971 break;
972 if (token->type == CPP_PRAGMA_EOL)
974 c_parser_consume_token (parser);
975 break;
977 c_parser_consume_token (parser);
980 parser->error = false;
983 /* Skip tokens until we have consumed an entire block, or until we
984 have consumed a non-nested ';'. */
986 static void
987 c_parser_skip_to_end_of_block_or_statement (c_parser *parser)
989 unsigned nesting_depth = 0;
990 bool save_error = parser->error;
992 while (true)
994 c_token *token;
996 /* Peek at the next token. */
997 token = c_parser_peek_token (parser);
999 switch (token->type)
1001 case CPP_EOF:
1002 return;
1004 case CPP_PRAGMA_EOL:
1005 if (parser->in_pragma)
1006 return;
1007 break;
1009 case CPP_SEMICOLON:
1010 /* If the next token is a ';', we have reached the
1011 end of the statement. */
1012 if (!nesting_depth)
1014 /* Consume the ';'. */
1015 c_parser_consume_token (parser);
1016 goto finished;
1018 break;
1020 case CPP_CLOSE_BRACE:
1021 /* If the next token is a non-nested '}', then we have
1022 reached the end of the current block. */
1023 if (nesting_depth == 0 || --nesting_depth == 0)
1025 c_parser_consume_token (parser);
1026 goto finished;
1028 break;
1030 case CPP_OPEN_BRACE:
1031 /* If it the next token is a '{', then we are entering a new
1032 block. Consume the entire block. */
1033 ++nesting_depth;
1034 break;
1036 case CPP_PRAGMA:
1037 /* If we see a pragma, consume the whole thing at once. We
1038 have some safeguards against consuming pragmas willy-nilly.
1039 Normally, we'd expect to be here with parser->error set,
1040 which disables these safeguards. But it's possible to get
1041 here for secondary error recovery, after parser->error has
1042 been cleared. */
1043 c_parser_consume_pragma (parser);
1044 c_parser_skip_to_pragma_eol (parser);
1045 parser->error = save_error;
1046 continue;
1048 default:
1049 break;
1052 c_parser_consume_token (parser);
1055 finished:
1056 parser->error = false;
1059 /* CPP's options (initialized by c-opts.c). */
1060 extern cpp_options *cpp_opts;
1062 /* Save the warning flags which are controlled by __extension__. */
1064 static inline int
1065 disable_extension_diagnostics (void)
1067 int ret = (pedantic
1068 | (warn_pointer_arith << 1)
1069 | (warn_traditional << 2)
1070 | (flag_iso << 3)
1071 | (warn_long_long << 4)
1072 | (warn_cxx_compat << 5)
1073 | (warn_overlength_strings << 6));
1074 cpp_opts->cpp_pedantic = pedantic = 0;
1075 warn_pointer_arith = 0;
1076 cpp_opts->cpp_warn_traditional = warn_traditional = 0;
1077 flag_iso = 0;
1078 cpp_opts->cpp_warn_long_long = warn_long_long = 0;
1079 warn_cxx_compat = 0;
1080 warn_overlength_strings = 0;
1081 return ret;
1084 /* Restore the warning flags which are controlled by __extension__.
1085 FLAGS is the return value from disable_extension_diagnostics. */
1087 static inline void
1088 restore_extension_diagnostics (int flags)
1090 cpp_opts->cpp_pedantic = pedantic = flags & 1;
1091 warn_pointer_arith = (flags >> 1) & 1;
1092 cpp_opts->cpp_warn_traditional = warn_traditional = (flags >> 2) & 1;
1093 flag_iso = (flags >> 3) & 1;
1094 cpp_opts->cpp_warn_long_long = warn_long_long = (flags >> 4) & 1;
1095 warn_cxx_compat = (flags >> 5) & 1;
1096 warn_overlength_strings = (flags >> 6) & 1;
1099 /* Possibly kinds of declarator to parse. */
1100 typedef enum c_dtr_syn {
1101 /* A normal declarator with an identifier. */
1102 C_DTR_NORMAL,
1103 /* An abstract declarator (maybe empty). */
1104 C_DTR_ABSTRACT,
1105 /* A parameter declarator: may be either, but after a type name does
1106 not redeclare a typedef name as an identifier if it can
1107 alternatively be interpreted as a typedef name; see DR#009,
1108 applied in C90 TC1, omitted from C99 and reapplied in C99 TC2
1109 following DR#249. For example, given a typedef T, "int T" and
1110 "int *T" are valid parameter declarations redeclaring T, while
1111 "int (T)" and "int * (T)" and "int (T[])" and "int (T (int))" are
1112 abstract declarators rather than involving redundant parentheses;
1113 the same applies with attributes inside the parentheses before
1114 "T". */
1115 C_DTR_PARM
1116 } c_dtr_syn;
1118 /* The binary operation precedence levels, where 0 is a dummy lowest level
1119 used for the bottom of the stack. */
1120 enum c_parser_prec {
1121 PREC_NONE,
1122 PREC_LOGOR,
1123 PREC_LOGAND,
1124 PREC_BITOR,
1125 PREC_BITXOR,
1126 PREC_BITAND,
1127 PREC_EQ,
1128 PREC_REL,
1129 PREC_SHIFT,
1130 PREC_ADD,
1131 PREC_MULT,
1132 NUM_PRECS
1135 /* Expression and its location. */
1136 struct c_tree_loc_pair {
1137 tree expr;
1138 location_t loc;
1141 static void c_parser_external_declaration (c_parser *);
1142 static void c_parser_asm_definition (c_parser *);
1143 static void c_parser_declaration_or_fndef (c_parser *, bool, bool, bool,
1144 bool, bool, tree *);
1145 static void c_parser_static_assert_declaration_no_semi (c_parser *);
1146 static void c_parser_static_assert_declaration (c_parser *);
1147 static void c_parser_declspecs (c_parser *, struct c_declspecs *, bool, bool,
1148 bool, enum c_lookahead_kind);
1149 static struct c_typespec c_parser_enum_specifier (c_parser *);
1150 static struct c_typespec c_parser_struct_or_union_specifier (c_parser *);
1151 static tree c_parser_struct_declaration (c_parser *);
1152 static struct c_typespec c_parser_typeof_specifier (c_parser *);
1153 static tree c_parser_alignas_specifier (c_parser *);
1154 static struct c_declarator *c_parser_declarator (c_parser *, bool, c_dtr_syn,
1155 bool *);
1156 static struct c_declarator *c_parser_direct_declarator (c_parser *, bool,
1157 c_dtr_syn, bool *);
1158 static struct c_declarator *c_parser_direct_declarator_inner (c_parser *,
1159 bool,
1160 struct c_declarator *);
1161 static struct c_arg_info *c_parser_parms_declarator (c_parser *, bool, tree);
1162 static struct c_arg_info *c_parser_parms_list_declarator (c_parser *, tree,
1163 tree);
1164 static struct c_parm *c_parser_parameter_declaration (c_parser *, tree);
1165 static tree c_parser_simple_asm_expr (c_parser *);
1166 static tree c_parser_attributes (c_parser *);
1167 static struct c_type_name *c_parser_type_name (c_parser *);
1168 static struct c_expr c_parser_initializer (c_parser *);
1169 static struct c_expr c_parser_braced_init (c_parser *, tree, bool);
1170 static void c_parser_initelt (c_parser *, struct obstack *);
1171 static void c_parser_initval (c_parser *, struct c_expr *,
1172 struct obstack *);
1173 static tree c_parser_compound_statement (c_parser *);
1174 static void c_parser_compound_statement_nostart (c_parser *);
1175 static void c_parser_label (c_parser *);
1176 static void c_parser_statement (c_parser *);
1177 static void c_parser_statement_after_labels (c_parser *);
1178 static void c_parser_if_statement (c_parser *);
1179 static void c_parser_switch_statement (c_parser *);
1180 static void c_parser_while_statement (c_parser *);
1181 static void c_parser_do_statement (c_parser *);
1182 static void c_parser_for_statement (c_parser *);
1183 static tree c_parser_asm_statement (c_parser *);
1184 static tree c_parser_asm_operands (c_parser *, bool);
1185 static tree c_parser_asm_goto_operands (c_parser *);
1186 static tree c_parser_asm_clobbers (c_parser *);
1187 static struct c_expr c_parser_expr_no_commas (c_parser *, struct c_expr *);
1188 static struct c_expr c_parser_conditional_expression (c_parser *,
1189 struct c_expr *);
1190 static struct c_expr c_parser_binary_expression (c_parser *, struct c_expr *,
1191 enum c_parser_prec);
1192 static struct c_expr c_parser_cast_expression (c_parser *, struct c_expr *);
1193 static struct c_expr c_parser_unary_expression (c_parser *);
1194 static struct c_expr c_parser_sizeof_expression (c_parser *);
1195 static struct c_expr c_parser_alignof_expression (c_parser *);
1196 static struct c_expr c_parser_postfix_expression (c_parser *);
1197 static struct c_expr c_parser_postfix_expression_after_paren_type (c_parser *,
1198 struct c_type_name *,
1199 location_t);
1200 static struct c_expr c_parser_postfix_expression_after_primary (c_parser *,
1201 location_t loc,
1202 struct c_expr);
1203 static tree c_parser_transaction (c_parser *, enum rid);
1204 static struct c_expr c_parser_transaction_expression (c_parser *, enum rid);
1205 static tree c_parser_transaction_cancel (c_parser *);
1206 static struct c_expr c_parser_expression (c_parser *);
1207 static struct c_expr c_parser_expression_conv (c_parser *);
1208 static VEC(tree,gc) *c_parser_expr_list (c_parser *, bool, bool,
1209 VEC(tree,gc) **,
1210 struct c_tree_loc_pair *);
1211 static void c_parser_omp_construct (c_parser *);
1212 static void c_parser_omp_threadprivate (c_parser *);
1213 static void c_parser_omp_barrier (c_parser *);
1214 static void c_parser_omp_flush (c_parser *);
1215 static void c_parser_omp_taskwait (c_parser *);
1216 static void c_parser_omp_taskyield (c_parser *);
1218 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
1219 static bool c_parser_pragma (c_parser *, enum pragma_context);
1221 /* These Objective-C parser functions are only ever called when
1222 compiling Objective-C. */
1223 static void c_parser_objc_class_definition (c_parser *, tree);
1224 static void c_parser_objc_class_instance_variables (c_parser *);
1225 static void c_parser_objc_class_declaration (c_parser *);
1226 static void c_parser_objc_alias_declaration (c_parser *);
1227 static void c_parser_objc_protocol_definition (c_parser *, tree);
1228 static bool c_parser_objc_method_type (c_parser *);
1229 static void c_parser_objc_method_definition (c_parser *);
1230 static void c_parser_objc_methodprotolist (c_parser *);
1231 static void c_parser_objc_methodproto (c_parser *);
1232 static tree c_parser_objc_method_decl (c_parser *, bool, tree *, tree *);
1233 static tree c_parser_objc_type_name (c_parser *);
1234 static tree c_parser_objc_protocol_refs (c_parser *);
1235 static void c_parser_objc_try_catch_finally_statement (c_parser *);
1236 static void c_parser_objc_synchronized_statement (c_parser *);
1237 static tree c_parser_objc_selector (c_parser *);
1238 static tree c_parser_objc_selector_arg (c_parser *);
1239 static tree c_parser_objc_receiver (c_parser *);
1240 static tree c_parser_objc_message_args (c_parser *);
1241 static tree c_parser_objc_keywordexpr (c_parser *);
1242 static void c_parser_objc_at_property_declaration (c_parser *);
1243 static void c_parser_objc_at_synthesize_declaration (c_parser *);
1244 static void c_parser_objc_at_dynamic_declaration (c_parser *);
1245 static bool c_parser_objc_diagnose_bad_element_prefix
1246 (c_parser *, struct c_declspecs *);
1248 /* These UPC parser functions are only ever called when
1249 compiling UPC. */
1250 static void c_parser_upc_forall_statement (c_parser *);
1251 static void c_parser_upc_sync_statement (c_parser *, int);
1252 static void c_parser_upc_shared_qual (source_location,
1253 c_parser *,
1254 struct c_declspecs *);
1256 /* Parse a translation unit (C90 6.7, C99 6.9).
1258 translation-unit:
1259 external-declarations
1261 external-declarations:
1262 external-declaration
1263 external-declarations external-declaration
1265 GNU extensions:
1267 translation-unit:
1268 empty
1271 static void
1272 c_parser_translation_unit (c_parser *parser)
1274 if (c_parser_next_token_is (parser, CPP_EOF))
1276 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
1277 "ISO C forbids an empty translation unit");
1279 else
1281 void *obstack_position = obstack_alloc (&parser_obstack, 0);
1282 mark_valid_location_for_stdc_pragma (false);
1285 ggc_collect ();
1286 c_parser_external_declaration (parser);
1287 obstack_free (&parser_obstack, obstack_position);
1289 while (c_parser_next_token_is_not (parser, CPP_EOF));
1293 /* Parse an external declaration (C90 6.7, C99 6.9).
1295 external-declaration:
1296 function-definition
1297 declaration
1299 GNU extensions:
1301 external-declaration:
1302 asm-definition
1304 __extension__ external-declaration
1306 Objective-C:
1308 external-declaration:
1309 objc-class-definition
1310 objc-class-declaration
1311 objc-alias-declaration
1312 objc-protocol-definition
1313 objc-method-definition
1314 @end
1317 static void
1318 c_parser_external_declaration (c_parser *parser)
1320 int ext;
1321 switch (c_parser_peek_token (parser)->type)
1323 case CPP_KEYWORD:
1324 switch (c_parser_peek_token (parser)->keyword)
1326 case RID_EXTENSION:
1327 ext = disable_extension_diagnostics ();
1328 c_parser_consume_token (parser);
1329 c_parser_external_declaration (parser);
1330 restore_extension_diagnostics (ext);
1331 break;
1332 case RID_ASM:
1333 c_parser_asm_definition (parser);
1334 break;
1335 case RID_AT_INTERFACE:
1336 case RID_AT_IMPLEMENTATION:
1337 gcc_assert (c_dialect_objc ());
1338 c_parser_objc_class_definition (parser, NULL_TREE);
1339 break;
1340 case RID_AT_CLASS:
1341 gcc_assert (c_dialect_objc ());
1342 c_parser_objc_class_declaration (parser);
1343 break;
1344 case RID_AT_ALIAS:
1345 gcc_assert (c_dialect_objc ());
1346 c_parser_objc_alias_declaration (parser);
1347 break;
1348 case RID_AT_PROTOCOL:
1349 gcc_assert (c_dialect_objc ());
1350 c_parser_objc_protocol_definition (parser, NULL_TREE);
1351 break;
1352 case RID_AT_PROPERTY:
1353 gcc_assert (c_dialect_objc ());
1354 c_parser_objc_at_property_declaration (parser);
1355 break;
1356 case RID_AT_SYNTHESIZE:
1357 gcc_assert (c_dialect_objc ());
1358 c_parser_objc_at_synthesize_declaration (parser);
1359 break;
1360 case RID_AT_DYNAMIC:
1361 gcc_assert (c_dialect_objc ());
1362 c_parser_objc_at_dynamic_declaration (parser);
1363 break;
1364 case RID_AT_END:
1365 gcc_assert (c_dialect_objc ());
1366 c_parser_consume_token (parser);
1367 objc_finish_implementation ();
1368 break;
1369 default:
1370 goto decl_or_fndef;
1372 break;
1373 case CPP_SEMICOLON:
1374 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
1375 "ISO C does not allow extra %<;%> outside of a function");
1376 c_parser_consume_token (parser);
1377 break;
1378 case CPP_PRAGMA:
1379 mark_valid_location_for_stdc_pragma (true);
1380 c_parser_pragma (parser, pragma_external);
1381 mark_valid_location_for_stdc_pragma (false);
1382 break;
1383 case CPP_PLUS:
1384 case CPP_MINUS:
1385 if (c_dialect_objc ())
1387 c_parser_objc_method_definition (parser);
1388 break;
1390 /* Else fall through, and yield a syntax error trying to parse
1391 as a declaration or function definition. */
1392 default:
1393 decl_or_fndef:
1394 /* A declaration or a function definition (or, in Objective-C,
1395 an @interface or @protocol with prefix attributes). We can
1396 only tell which after parsing the declaration specifiers, if
1397 any, and the first declarator. */
1398 c_parser_declaration_or_fndef (parser, true, true, true, false, true, NULL);
1399 break;
1403 /* Parse a declaration or function definition (C90 6.5, 6.7.1, C99
1404 6.7, 6.9.1). If FNDEF_OK is true, a function definition is
1405 accepted; otherwise (old-style parameter declarations) only other
1406 declarations are accepted. If STATIC_ASSERT_OK is true, a static
1407 assertion is accepted; otherwise (old-style parameter declarations)
1408 it is not. If NESTED is true, we are inside a function or parsing
1409 old-style parameter declarations; any functions encountered are
1410 nested functions and declaration specifiers are required; otherwise
1411 we are at top level and functions are normal functions and
1412 declaration specifiers may be optional. If EMPTY_OK is true, empty
1413 declarations are OK (subject to all other constraints); otherwise
1414 (old-style parameter declarations) they are diagnosed. If
1415 START_ATTR_OK is true, the declaration specifiers may start with
1416 attributes; otherwise they may not.
1417 OBJC_FOREACH_OBJECT_DECLARATION can be used to get back the parsed
1418 declaration when parsing an Objective-C foreach statement.
1420 declaration:
1421 declaration-specifiers init-declarator-list[opt] ;
1422 static_assert-declaration
1424 function-definition:
1425 declaration-specifiers[opt] declarator declaration-list[opt]
1426 compound-statement
1428 declaration-list:
1429 declaration
1430 declaration-list declaration
1432 init-declarator-list:
1433 init-declarator
1434 init-declarator-list , init-declarator
1436 init-declarator:
1437 declarator simple-asm-expr[opt] attributes[opt]
1438 declarator simple-asm-expr[opt] attributes[opt] = initializer
1440 GNU extensions:
1442 nested-function-definition:
1443 declaration-specifiers declarator declaration-list[opt]
1444 compound-statement
1446 Objective-C:
1447 attributes objc-class-definition
1448 attributes objc-category-definition
1449 attributes objc-protocol-definition
1451 The simple-asm-expr and attributes are GNU extensions.
1453 This function does not handle __extension__; that is handled in its
1454 callers. ??? Following the old parser, __extension__ may start
1455 external declarations, declarations in functions and declarations
1456 at the start of "for" loops, but not old-style parameter
1457 declarations.
1459 C99 requires declaration specifiers in a function definition; the
1460 absence is diagnosed through the diagnosis of implicit int. In GNU
1461 C we also allow but diagnose declarations without declaration
1462 specifiers, but only at top level (elsewhere they conflict with
1463 other syntax).
1465 In Objective-C, declarations of the looping variable in a foreach
1466 statement are exceptionally terminated by 'in' (for example, 'for
1467 (NSObject *object in array) { ... }').
1469 OpenMP:
1471 declaration:
1472 threadprivate-directive */
1474 static void
1475 c_parser_declaration_or_fndef (c_parser *parser, bool fndef_ok,
1476 bool static_assert_ok, bool empty_ok,
1477 bool nested, bool start_attr_ok,
1478 tree *objc_foreach_object_declaration)
1480 struct c_declspecs *specs;
1481 tree prefix_attrs;
1482 tree all_prefix_attrs;
1483 bool diagnosed_no_specs = false;
1484 location_t here = c_parser_peek_token (parser)->location;
1486 if (static_assert_ok
1487 && c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT))
1489 c_parser_static_assert_declaration (parser);
1490 return;
1492 specs = build_null_declspecs ();
1494 /* Try to detect an unknown type name when we have "A B" or "A *B". */
1495 if (c_parser_peek_token (parser)->type == CPP_NAME
1496 && c_parser_peek_token (parser)->id_kind == C_ID_ID
1497 && (c_parser_peek_2nd_token (parser)->type == CPP_NAME
1498 || c_parser_peek_2nd_token (parser)->type == CPP_MULT)
1499 && (!nested || !lookup_name (c_parser_peek_token (parser)->value)))
1501 error_at (here, "unknown type name %qE",
1502 c_parser_peek_token (parser)->value);
1504 /* Parse declspecs normally to get a correct pointer type, but avoid
1505 a further "fails to be a type name" error. Refuse nested functions
1506 since it is not how the user likely wants us to recover. */
1507 c_parser_peek_token (parser)->type = CPP_KEYWORD;
1508 c_parser_peek_token (parser)->keyword = RID_VOID;
1509 c_parser_peek_token (parser)->value = error_mark_node;
1510 fndef_ok = !nested;
1513 c_parser_declspecs (parser, specs, true, true, start_attr_ok, cla_nonabstract_decl);
1514 if (parser->error)
1516 c_parser_skip_to_end_of_block_or_statement (parser);
1517 return;
1519 if (nested && !specs->declspecs_seen_p)
1521 c_parser_error (parser, "expected declaration specifiers");
1522 c_parser_skip_to_end_of_block_or_statement (parser);
1523 return;
1525 finish_declspecs (specs);
1526 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1528 if (empty_ok)
1529 shadow_tag (specs);
1530 else
1532 shadow_tag_warned (specs, 1);
1533 pedwarn (here, 0, "empty declaration");
1535 c_parser_consume_token (parser);
1536 return;
1539 /* Provide better error recovery. Note that a type name here is usually
1540 better diagnosed as a redeclaration. */
1541 if (empty_ok
1542 && specs->typespec_kind == ctsk_tagdef
1543 && c_parser_next_token_starts_declspecs (parser)
1544 && !c_parser_next_token_is (parser, CPP_NAME))
1546 c_parser_error (parser, "expected %<;%>, identifier or %<(%>");
1547 parser->error = false;
1548 shadow_tag_warned (specs, 1);
1549 return;
1551 else if (c_dialect_objc ())
1553 /* Prefix attributes are an error on method decls. */
1554 switch (c_parser_peek_token (parser)->type)
1556 case CPP_PLUS:
1557 case CPP_MINUS:
1558 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1559 return;
1560 if (specs->attrs)
1562 warning_at (c_parser_peek_token (parser)->location,
1563 OPT_Wattributes,
1564 "prefix attributes are ignored for methods");
1565 specs->attrs = NULL_TREE;
1567 if (fndef_ok)
1568 c_parser_objc_method_definition (parser);
1569 else
1570 c_parser_objc_methodproto (parser);
1571 return;
1572 break;
1573 default:
1574 break;
1576 /* This is where we parse 'attributes @interface ...',
1577 'attributes @implementation ...', 'attributes @protocol ...'
1578 (where attributes could be, for example, __attribute__
1579 ((deprecated)).
1581 switch (c_parser_peek_token (parser)->keyword)
1583 case RID_AT_INTERFACE:
1585 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1586 return;
1587 c_parser_objc_class_definition (parser, specs->attrs);
1588 return;
1590 break;
1591 case RID_AT_IMPLEMENTATION:
1593 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1594 return;
1595 if (specs->attrs)
1597 warning_at (c_parser_peek_token (parser)->location,
1598 OPT_Wattributes,
1599 "prefix attributes are ignored for implementations");
1600 specs->attrs = NULL_TREE;
1602 c_parser_objc_class_definition (parser, NULL_TREE);
1603 return;
1605 break;
1606 case RID_AT_PROTOCOL:
1608 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1609 return;
1610 c_parser_objc_protocol_definition (parser, specs->attrs);
1611 return;
1613 break;
1614 case RID_AT_ALIAS:
1615 case RID_AT_CLASS:
1616 case RID_AT_END:
1617 case RID_AT_PROPERTY:
1618 if (specs->attrs)
1620 c_parser_error (parser, "unexpected attribute");
1621 specs->attrs = NULL;
1623 break;
1624 default:
1625 break;
1629 pending_xref_error ();
1630 prefix_attrs = specs->attrs;
1631 all_prefix_attrs = prefix_attrs;
1632 specs->attrs = NULL_TREE;
1633 while (true)
1635 struct c_declarator *declarator;
1636 bool dummy = false;
1637 timevar_id_t tv;
1638 tree fnbody;
1639 /* Declaring either one or more declarators (in which case we
1640 should diagnose if there were no declaration specifiers) or a
1641 function definition (in which case the diagnostic for
1642 implicit int suffices). */
1643 declarator = c_parser_declarator (parser,
1644 specs->typespec_kind != ctsk_none,
1645 C_DTR_NORMAL, &dummy);
1646 if (declarator == NULL)
1648 c_parser_skip_to_end_of_block_or_statement (parser);
1649 return;
1651 if (c_parser_next_token_is (parser, CPP_EQ)
1652 || c_parser_next_token_is (parser, CPP_COMMA)
1653 || c_parser_next_token_is (parser, CPP_SEMICOLON)
1654 || c_parser_next_token_is_keyword (parser, RID_ASM)
1655 || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE)
1656 || c_parser_next_token_is_keyword (parser, RID_IN))
1658 tree asm_name = NULL_TREE;
1659 tree postfix_attrs = NULL_TREE;
1660 if (!diagnosed_no_specs && !specs->declspecs_seen_p)
1662 diagnosed_no_specs = true;
1663 pedwarn (here, 0, "data definition has no type or storage class");
1665 /* Having seen a data definition, there cannot now be a
1666 function definition. */
1667 fndef_ok = false;
1668 if (c_parser_next_token_is_keyword (parser, RID_ASM))
1669 asm_name = c_parser_simple_asm_expr (parser);
1670 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1671 postfix_attrs = c_parser_attributes (parser);
1672 if (c_parser_next_token_is (parser, CPP_EQ))
1674 tree d;
1675 struct c_expr init;
1676 location_t init_loc;
1677 c_parser_consume_token (parser);
1678 /* The declaration of the variable is in effect while
1679 its initializer is parsed. */
1680 d = start_decl (declarator, specs, true,
1681 chainon (postfix_attrs, all_prefix_attrs));
1682 if (!d)
1683 d = error_mark_node;
1684 start_init (d, asm_name, global_bindings_p ());
1685 init_loc = c_parser_peek_token (parser)->location;
1686 init = c_parser_initializer (parser);
1687 finish_init ();
1688 if (d != error_mark_node)
1690 maybe_warn_string_init (TREE_TYPE (d), init);
1691 finish_decl (d, init_loc, init.value,
1692 init.original_type, asm_name);
1695 else
1697 tree d = start_decl (declarator, specs, false,
1698 chainon (postfix_attrs,
1699 all_prefix_attrs));
1700 if (d)
1701 finish_decl (d, UNKNOWN_LOCATION, NULL_TREE,
1702 NULL_TREE, asm_name);
1704 if (c_parser_next_token_is_keyword (parser, RID_IN))
1706 if (d)
1707 *objc_foreach_object_declaration = d;
1708 else
1709 *objc_foreach_object_declaration = error_mark_node;
1712 if (c_parser_next_token_is (parser, CPP_COMMA))
1714 c_parser_consume_token (parser);
1715 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1716 all_prefix_attrs = chainon (c_parser_attributes (parser),
1717 prefix_attrs);
1718 else
1719 all_prefix_attrs = prefix_attrs;
1720 continue;
1722 else if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1724 c_parser_consume_token (parser);
1725 return;
1727 else if (c_parser_next_token_is_keyword (parser, RID_IN))
1729 /* This can only happen in Objective-C: we found the
1730 'in' that terminates the declaration inside an
1731 Objective-C foreach statement. Do not consume the
1732 token, so that the caller can use it to determine
1733 that this indeed is a foreach context. */
1734 return;
1736 else
1738 c_parser_error (parser, "expected %<,%> or %<;%>");
1739 c_parser_skip_to_end_of_block_or_statement (parser);
1740 return;
1743 else if (!fndef_ok)
1745 c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, "
1746 "%<asm%> or %<__attribute__%>");
1747 c_parser_skip_to_end_of_block_or_statement (parser);
1748 return;
1750 /* Function definition (nested or otherwise). */
1751 if (nested)
1753 pedwarn (here, OPT_Wpedantic, "ISO C forbids nested functions");
1754 c_push_function_context ();
1756 if (!start_function (specs, declarator, all_prefix_attrs))
1758 /* This can appear in many cases looking nothing like a
1759 function definition, so we don't give a more specific
1760 error suggesting there was one. */
1761 c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, %<asm%> "
1762 "or %<__attribute__%>");
1763 if (nested)
1764 c_pop_function_context ();
1765 break;
1768 if (DECL_DECLARED_INLINE_P (current_function_decl))
1769 tv = TV_PARSE_INLINE;
1770 else
1771 tv = TV_PARSE_FUNC;
1772 timevar_push (tv);
1774 /* Parse old-style parameter declarations. ??? Attributes are
1775 not allowed to start declaration specifiers here because of a
1776 syntax conflict between a function declaration with attribute
1777 suffix and a function definition with an attribute prefix on
1778 first old-style parameter declaration. Following the old
1779 parser, they are not accepted on subsequent old-style
1780 parameter declarations either. However, there is no
1781 ambiguity after the first declaration, nor indeed on the
1782 first as long as we don't allow postfix attributes after a
1783 declarator with a nonempty identifier list in a definition;
1784 and postfix attributes have never been accepted here in
1785 function definitions either. */
1786 while (c_parser_next_token_is_not (parser, CPP_EOF)
1787 && c_parser_next_token_is_not (parser, CPP_OPEN_BRACE))
1788 c_parser_declaration_or_fndef (parser, false, false, false,
1789 true, false, NULL);
1790 store_parm_decls ();
1791 DECL_STRUCT_FUNCTION (current_function_decl)->function_start_locus
1792 = c_parser_peek_token (parser)->location;
1793 fnbody = c_parser_compound_statement (parser);
1794 if (nested)
1796 tree decl = current_function_decl;
1797 /* Mark nested functions as needing static-chain initially.
1798 lower_nested_functions will recompute it but the
1799 DECL_STATIC_CHAIN flag is also used before that happens,
1800 by initializer_constant_valid_p. See gcc.dg/nested-fn-2.c. */
1801 DECL_STATIC_CHAIN (decl) = 1;
1802 add_stmt (fnbody);
1803 finish_function ();
1804 c_pop_function_context ();
1805 add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl));
1807 else
1809 add_stmt (fnbody);
1810 finish_function ();
1813 timevar_pop (tv);
1814 break;
1818 /* Parse an asm-definition (asm() outside a function body). This is a
1819 GNU extension.
1821 asm-definition:
1822 simple-asm-expr ;
1825 static void
1826 c_parser_asm_definition (c_parser *parser)
1828 tree asm_str = c_parser_simple_asm_expr (parser);
1829 if (asm_str)
1830 add_asm_node (asm_str);
1831 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
1834 /* Parse a static assertion (C11 6.7.10).
1836 static_assert-declaration:
1837 static_assert-declaration-no-semi ;
1840 static void
1841 c_parser_static_assert_declaration (c_parser *parser)
1843 c_parser_static_assert_declaration_no_semi (parser);
1844 if (parser->error
1845 || !c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
1846 c_parser_skip_to_end_of_block_or_statement (parser);
1849 /* Parse a static assertion (C11 6.7.10), without the trailing
1850 semicolon.
1852 static_assert-declaration-no-semi:
1853 _Static_assert ( constant-expression , string-literal )
1856 static void
1857 c_parser_static_assert_declaration_no_semi (c_parser *parser)
1859 location_t assert_loc, value_loc;
1860 tree value;
1861 tree string;
1863 gcc_assert (c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT));
1864 assert_loc = c_parser_peek_token (parser)->location;
1865 if (!flag_isoc11)
1867 if (flag_isoc99)
1868 pedwarn (assert_loc, OPT_Wpedantic,
1869 "ISO C99 does not support %<_Static_assert%>");
1870 else
1871 pedwarn (assert_loc, OPT_Wpedantic,
1872 "ISO C90 does not support %<_Static_assert%>");
1874 c_parser_consume_token (parser);
1875 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
1876 return;
1877 value_loc = c_parser_peek_token (parser)->location;
1878 value = c_parser_expr_no_commas (parser, NULL).value;
1879 parser->lex_untranslated_string = true;
1880 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
1882 parser->lex_untranslated_string = false;
1883 return;
1885 switch (c_parser_peek_token (parser)->type)
1887 case CPP_STRING:
1888 case CPP_STRING16:
1889 case CPP_STRING32:
1890 case CPP_WSTRING:
1891 case CPP_UTF8STRING:
1892 string = c_parser_peek_token (parser)->value;
1893 c_parser_consume_token (parser);
1894 parser->lex_untranslated_string = false;
1895 break;
1896 default:
1897 c_parser_error (parser, "expected string literal");
1898 parser->lex_untranslated_string = false;
1899 return;
1901 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
1903 if (!INTEGRAL_TYPE_P (TREE_TYPE (value)))
1905 error_at (value_loc, "expression in static assertion is not an integer");
1906 return;
1908 if (TREE_CODE (value) != INTEGER_CST)
1910 value = c_fully_fold (value, false, NULL);
1911 if (TREE_CODE (value) == INTEGER_CST)
1912 pedwarn (value_loc, OPT_Wpedantic, "expression in static assertion "
1913 "is not an integer constant expression");
1915 if (TREE_CODE (value) != INTEGER_CST)
1917 error_at (value_loc, "expression in static assertion is not constant");
1918 return;
1920 constant_expression_warning (value);
1921 if (integer_zerop (value))
1922 error_at (assert_loc, "static assertion failed: %E", string);
1925 /* Parse some declaration specifiers (possibly none) (C90 6.5, C99
1926 6.7), adding them to SPECS (which may already include some).
1927 Storage class specifiers are accepted iff SCSPEC_OK; type
1928 specifiers are accepted iff TYPESPEC_OK; attributes are accepted at
1929 the start iff START_ATTR_OK.
1931 declaration-specifiers:
1932 storage-class-specifier declaration-specifiers[opt]
1933 type-specifier declaration-specifiers[opt]
1934 type-qualifier declaration-specifiers[opt]
1935 function-specifier declaration-specifiers[opt]
1936 alignment-specifier declaration-specifiers[opt]
1938 Function specifiers (inline) are from C99, and are currently
1939 handled as storage class specifiers, as is __thread. Alignment
1940 specifiers are from C11.
1942 C90 6.5.1, C99 6.7.1:
1943 storage-class-specifier:
1944 typedef
1945 extern
1946 static
1947 auto
1948 register
1950 C99 6.7.4:
1951 function-specifier:
1952 inline
1953 _Noreturn
1955 (_Noreturn is new in C11.)
1957 C90 6.5.2, C99 6.7.2:
1958 type-specifier:
1959 void
1960 char
1961 short
1963 long
1964 float
1965 double
1966 signed
1967 unsigned
1968 _Bool
1969 _Complex
1970 [_Imaginary removed in C99 TC2]
1971 struct-or-union-specifier
1972 enum-specifier
1973 typedef-name
1975 (_Bool and _Complex are new in C99.)
1977 C90 6.5.3, C99 6.7.3:
1979 type-qualifier:
1980 const
1981 restrict
1982 volatile
1983 address-space-qualifier
1985 (restrict is new in C99.)
1987 GNU extensions:
1989 declaration-specifiers:
1990 attributes declaration-specifiers[opt]
1992 type-qualifier:
1993 address-space
1995 address-space:
1996 identifier recognized by the target
1998 storage-class-specifier:
1999 __thread
2001 type-specifier:
2002 typeof-specifier
2003 __int128
2004 _Decimal32
2005 _Decimal64
2006 _Decimal128
2007 _Fract
2008 _Accum
2009 _Sat
2011 (_Fract, _Accum, and _Sat are new from ISO/IEC DTR 18037:
2012 http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1169.pdf)
2014 Objective-C:
2016 type-specifier:
2017 class-name objc-protocol-refs[opt]
2018 typedef-name objc-protocol-refs
2019 objc-protocol-refs
2022 static void
2023 c_parser_declspecs (c_parser *parser, struct c_declspecs *specs,
2024 bool scspec_ok, bool typespec_ok, bool start_attr_ok,
2025 enum c_lookahead_kind la)
2027 bool attrs_ok = start_attr_ok;
2028 bool seen_type = specs->typespec_kind != ctsk_none;
2030 if (!typespec_ok)
2031 gcc_assert (la == cla_prefer_id);
2033 while (c_parser_next_token_is (parser, CPP_NAME)
2034 || c_parser_next_token_is (parser, CPP_KEYWORD)
2035 || (c_dialect_objc () && c_parser_next_token_is (parser, CPP_LESS)))
2037 struct c_typespec t;
2038 tree attrs;
2039 tree align;
2040 location_t loc = c_parser_peek_token (parser)->location;
2042 /* If we cannot accept a type, exit if the next token must start
2043 one. Also, if we already have seen a tagged definition,
2044 a typename would be an error anyway and likely the user
2045 has simply forgotten a semicolon, so we exit. */
2046 if ((!typespec_ok || specs->typespec_kind == ctsk_tagdef)
2047 && c_parser_next_tokens_start_typename (parser, la)
2048 && !c_parser_next_token_is_qualifier (parser))
2049 break;
2051 if (c_parser_next_token_is (parser, CPP_NAME))
2053 c_token *name_token = c_parser_peek_token (parser);
2054 tree value = name_token->value;
2055 c_id_kind kind = name_token->id_kind;
2057 if (kind == C_ID_ADDRSPACE)
2059 addr_space_t as
2060 = name_token->keyword - RID_FIRST_ADDR_SPACE;
2061 declspecs_add_addrspace (name_token->location, specs, as);
2062 c_parser_consume_token (parser);
2063 attrs_ok = true;
2064 continue;
2067 gcc_assert (!c_parser_next_token_is_qualifier (parser));
2069 /* If we cannot accept a type, and the next token must start one,
2070 exit. Do the same if we already have seen a tagged definition,
2071 since it would be an error anyway and likely the user has simply
2072 forgotten a semicolon. */
2073 if (seen_type || !c_parser_next_tokens_start_typename (parser, la))
2074 break;
2076 /* Now at an unknown typename (C_ID_ID), a C_ID_TYPENAME or
2077 a C_ID_CLASSNAME. */
2078 c_parser_consume_token (parser);
2079 seen_type = true;
2080 attrs_ok = true;
2081 if (kind == C_ID_ID)
2083 error ("unknown type name %qE", value);
2084 t.kind = ctsk_typedef;
2085 t.spec = error_mark_node;
2087 else if (kind == C_ID_TYPENAME
2088 && (!c_dialect_objc ()
2089 || c_parser_next_token_is_not (parser, CPP_LESS)))
2091 t.kind = ctsk_typedef;
2092 /* For a typedef name, record the meaning, not the name.
2093 In case of 'foo foo, bar;'. */
2094 t.spec = lookup_name (value);
2096 else
2098 tree proto = NULL_TREE;
2099 gcc_assert (c_dialect_objc ());
2100 t.kind = ctsk_objc;
2101 if (c_parser_next_token_is (parser, CPP_LESS))
2102 proto = c_parser_objc_protocol_refs (parser);
2103 t.spec = objc_get_protocol_qualified_type (value, proto);
2105 t.expr = NULL_TREE;
2106 t.expr_const_operands = true;
2107 declspecs_add_type (name_token->location, specs, t);
2108 continue;
2110 if (c_parser_next_token_is (parser, CPP_LESS))
2112 /* Make "<SomeProtocol>" equivalent to "id <SomeProtocol>" -
2113 nisse@lysator.liu.se. */
2114 tree proto;
2115 gcc_assert (c_dialect_objc ());
2116 if (!typespec_ok || seen_type)
2117 break;
2118 proto = c_parser_objc_protocol_refs (parser);
2119 t.kind = ctsk_objc;
2120 t.spec = objc_get_protocol_qualified_type (NULL_TREE, proto);
2121 t.expr = NULL_TREE;
2122 t.expr_const_operands = true;
2123 declspecs_add_type (loc, specs, t);
2124 continue;
2126 gcc_assert (c_parser_next_token_is (parser, CPP_KEYWORD));
2127 switch (c_parser_peek_token (parser)->keyword)
2129 case RID_STATIC:
2130 case RID_EXTERN:
2131 case RID_REGISTER:
2132 case RID_TYPEDEF:
2133 case RID_INLINE:
2134 case RID_NORETURN:
2135 case RID_AUTO:
2136 case RID_THREAD:
2137 if (!scspec_ok)
2138 goto out;
2139 attrs_ok = true;
2140 /* TODO: Distinguish between function specifiers (inline, noreturn)
2141 and storage class specifiers, either here or in
2142 declspecs_add_scspec. */
2143 declspecs_add_scspec (loc, specs,
2144 c_parser_peek_token (parser)->value);
2145 c_parser_consume_token (parser);
2146 break;
2147 case RID_UNSIGNED:
2148 case RID_LONG:
2149 case RID_INT128:
2150 case RID_SHORT:
2151 case RID_SIGNED:
2152 case RID_COMPLEX:
2153 case RID_INT:
2154 case RID_CHAR:
2155 case RID_FLOAT:
2156 case RID_DOUBLE:
2157 case RID_VOID:
2158 case RID_DFLOAT32:
2159 case RID_DFLOAT64:
2160 case RID_DFLOAT128:
2161 case RID_BOOL:
2162 case RID_FRACT:
2163 case RID_ACCUM:
2164 case RID_SAT:
2165 if (!typespec_ok)
2166 goto out;
2167 attrs_ok = true;
2168 seen_type = true;
2169 if (c_dialect_objc ())
2170 parser->objc_need_raw_identifier = true;
2171 t.kind = ctsk_resword;
2172 t.spec = c_parser_peek_token (parser)->value;
2173 t.expr = NULL_TREE;
2174 t.expr_const_operands = true;
2175 declspecs_add_type (loc, specs, t);
2176 c_parser_consume_token (parser);
2177 break;
2178 case RID_ENUM:
2179 if (!typespec_ok)
2180 goto out;
2181 attrs_ok = true;
2182 seen_type = true;
2183 t = c_parser_enum_specifier (parser);
2184 declspecs_add_type (loc, specs, t);
2185 break;
2186 case RID_STRUCT:
2187 case RID_UNION:
2188 if (!typespec_ok)
2189 goto out;
2190 attrs_ok = true;
2191 seen_type = true;
2192 t = c_parser_struct_or_union_specifier (parser);
2193 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, t.spec);
2194 declspecs_add_type (loc, specs, t);
2195 break;
2196 case RID_TYPEOF:
2197 /* ??? The old parser rejected typeof after other type
2198 specifiers, but is a syntax error the best way of
2199 handling this? */
2200 if (!typespec_ok || seen_type)
2201 goto out;
2202 attrs_ok = true;
2203 seen_type = true;
2204 t = c_parser_typeof_specifier (parser);
2205 declspecs_add_type (loc, specs, t);
2206 break;
2207 case RID_CONST:
2208 case RID_VOLATILE:
2209 case RID_RESTRICT:
2210 attrs_ok = true;
2211 declspecs_add_qual (loc, specs, c_parser_peek_token (parser)->value);
2212 c_parser_consume_token (parser);
2213 break;
2214 /* UPC qualifiers */
2215 case RID_SHARED:
2216 attrs_ok = true;
2217 c_parser_upc_shared_qual (loc, parser, specs);
2218 break;
2219 case RID_STRICT:
2220 case RID_RELAXED:
2221 attrs_ok = true;
2222 declspecs_add_qual (loc, specs, c_parser_peek_token (parser)->value);
2223 c_parser_consume_token (parser);
2224 break;
2225 case RID_ATTRIBUTE:
2226 if (!attrs_ok)
2227 goto out;
2228 attrs = c_parser_attributes (parser);
2229 declspecs_add_attrs (loc, specs, attrs);
2230 break;
2231 case RID_ALIGNAS:
2232 align = c_parser_alignas_specifier (parser);
2233 declspecs_add_alignas (loc, specs, align);
2234 break;
2235 default:
2236 goto out;
2239 out: ;
2242 /* Parse an enum specifier (C90 6.5.2.2, C99 6.7.2.2).
2244 enum-specifier:
2245 enum attributes[opt] identifier[opt] { enumerator-list } attributes[opt]
2246 enum attributes[opt] identifier[opt] { enumerator-list , } attributes[opt]
2247 enum attributes[opt] identifier
2249 The form with trailing comma is new in C99. The forms with
2250 attributes are GNU extensions. In GNU C, we accept any expression
2251 without commas in the syntax (assignment expressions, not just
2252 conditional expressions); assignment expressions will be diagnosed
2253 as non-constant.
2255 enumerator-list:
2256 enumerator
2257 enumerator-list , enumerator
2259 enumerator:
2260 enumeration-constant
2261 enumeration-constant = constant-expression
2264 static struct c_typespec
2265 c_parser_enum_specifier (c_parser *parser)
2267 struct c_typespec ret;
2268 tree attrs;
2269 tree ident = NULL_TREE;
2270 location_t enum_loc;
2271 location_t ident_loc = UNKNOWN_LOCATION; /* Quiet warning. */
2272 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ENUM));
2273 enum_loc = c_parser_peek_token (parser)->location;
2274 c_parser_consume_token (parser);
2275 attrs = c_parser_attributes (parser);
2276 enum_loc = c_parser_peek_token (parser)->location;
2277 /* Set the location in case we create a decl now. */
2278 c_parser_set_source_position_from_token (c_parser_peek_token (parser));
2279 if (c_parser_next_token_is (parser, CPP_NAME))
2281 ident = c_parser_peek_token (parser)->value;
2282 ident_loc = c_parser_peek_token (parser)->location;
2283 enum_loc = ident_loc;
2284 c_parser_consume_token (parser);
2286 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
2288 /* Parse an enum definition. */
2289 struct c_enum_contents the_enum;
2290 tree type;
2291 tree postfix_attrs;
2292 /* We chain the enumerators in reverse order, then put them in
2293 forward order at the end. */
2294 tree values;
2295 timevar_push (TV_PARSE_ENUM);
2296 type = start_enum (enum_loc, &the_enum, ident);
2297 values = NULL_TREE;
2298 c_parser_consume_token (parser);
2299 while (true)
2301 tree enum_id;
2302 tree enum_value;
2303 tree enum_decl;
2304 bool seen_comma;
2305 c_token *token;
2306 location_t comma_loc = UNKNOWN_LOCATION; /* Quiet warning. */
2307 location_t decl_loc, value_loc;
2308 if (c_parser_next_token_is_not (parser, CPP_NAME))
2310 c_parser_error (parser, "expected identifier");
2311 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
2312 values = error_mark_node;
2313 break;
2315 token = c_parser_peek_token (parser);
2316 enum_id = token->value;
2317 /* Set the location in case we create a decl now. */
2318 c_parser_set_source_position_from_token (token);
2319 decl_loc = value_loc = token->location;
2320 c_parser_consume_token (parser);
2321 if (c_parser_next_token_is (parser, CPP_EQ))
2323 c_parser_consume_token (parser);
2324 value_loc = c_parser_peek_token (parser)->location;
2325 enum_value = c_parser_expr_no_commas (parser, NULL).value;
2327 else
2328 enum_value = NULL_TREE;
2329 enum_decl = build_enumerator (decl_loc, value_loc,
2330 &the_enum, enum_id, enum_value);
2331 TREE_CHAIN (enum_decl) = values;
2332 values = enum_decl;
2333 seen_comma = false;
2334 if (c_parser_next_token_is (parser, CPP_COMMA))
2336 comma_loc = c_parser_peek_token (parser)->location;
2337 seen_comma = true;
2338 c_parser_consume_token (parser);
2340 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2342 if (seen_comma && !flag_isoc99)
2343 pedwarn (comma_loc, OPT_Wpedantic, "comma at end of enumerator list");
2344 c_parser_consume_token (parser);
2345 break;
2347 if (!seen_comma)
2349 c_parser_error (parser, "expected %<,%> or %<}%>");
2350 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
2351 values = error_mark_node;
2352 break;
2355 postfix_attrs = c_parser_attributes (parser);
2356 ret.spec = finish_enum (type, nreverse (values),
2357 chainon (attrs, postfix_attrs));
2358 ret.kind = ctsk_tagdef;
2359 ret.expr = NULL_TREE;
2360 ret.expr_const_operands = true;
2361 timevar_pop (TV_PARSE_ENUM);
2362 return ret;
2364 else if (!ident)
2366 c_parser_error (parser, "expected %<{%>");
2367 ret.spec = error_mark_node;
2368 ret.kind = ctsk_tagref;
2369 ret.expr = NULL_TREE;
2370 ret.expr_const_operands = true;
2371 return ret;
2373 ret = parser_xref_tag (ident_loc, ENUMERAL_TYPE, ident);
2374 /* In ISO C, enumerated types can be referred to only if already
2375 defined. */
2376 if (pedantic && !COMPLETE_TYPE_P (ret.spec))
2378 gcc_assert (ident);
2379 pedwarn (enum_loc, OPT_Wpedantic,
2380 "ISO C forbids forward references to %<enum%> types");
2382 return ret;
2385 /* Parse a struct or union specifier (C90 6.5.2.1, C99 6.7.2.1).
2387 struct-or-union-specifier:
2388 struct-or-union attributes[opt] identifier[opt]
2389 { struct-contents } attributes[opt]
2390 struct-or-union attributes[opt] identifier
2392 struct-contents:
2393 struct-declaration-list
2395 struct-declaration-list:
2396 struct-declaration ;
2397 struct-declaration-list struct-declaration ;
2399 GNU extensions:
2401 struct-contents:
2402 empty
2403 struct-declaration
2404 struct-declaration-list struct-declaration
2406 struct-declaration-list:
2407 struct-declaration-list ;
2410 (Note that in the syntax here, unlike that in ISO C, the semicolons
2411 are included here rather than in struct-declaration, in order to
2412 describe the syntax with extra semicolons and missing semicolon at
2413 end.)
2415 Objective-C:
2417 struct-declaration-list:
2418 @defs ( class-name )
2420 (Note this does not include a trailing semicolon, but can be
2421 followed by further declarations, and gets a pedwarn-if-pedantic
2422 when followed by a semicolon.) */
2424 static struct c_typespec
2425 c_parser_struct_or_union_specifier (c_parser *parser)
2427 struct c_typespec ret;
2428 tree attrs;
2429 tree ident = NULL_TREE;
2430 location_t struct_loc;
2431 location_t ident_loc = UNKNOWN_LOCATION;
2432 enum tree_code code;
2433 switch (c_parser_peek_token (parser)->keyword)
2435 case RID_STRUCT:
2436 code = RECORD_TYPE;
2437 break;
2438 case RID_UNION:
2439 code = UNION_TYPE;
2440 break;
2441 default:
2442 gcc_unreachable ();
2444 struct_loc = c_parser_peek_token (parser)->location;
2445 c_parser_consume_token (parser);
2446 attrs = c_parser_attributes (parser);
2448 /* Set the location in case we create a decl now. */
2449 c_parser_set_source_position_from_token (c_parser_peek_token (parser));
2451 if (c_parser_next_token_is (parser, CPP_NAME))
2453 ident = c_parser_peek_token (parser)->value;
2454 ident_loc = c_parser_peek_token (parser)->location;
2455 struct_loc = ident_loc;
2456 c_parser_consume_token (parser);
2458 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
2460 /* Parse a struct or union definition. Start the scope of the
2461 tag before parsing components. */
2462 struct c_struct_parse_info *struct_info;
2463 tree type = start_struct (struct_loc, code, ident, &struct_info);
2464 tree postfix_attrs;
2465 /* We chain the components in reverse order, then put them in
2466 forward order at the end. Each struct-declaration may
2467 declare multiple components (comma-separated), so we must use
2468 chainon to join them, although when parsing each
2469 struct-declaration we can use TREE_CHAIN directly.
2471 The theory behind all this is that there will be more
2472 semicolon separated fields than comma separated fields, and
2473 so we'll be minimizing the number of node traversals required
2474 by chainon. */
2475 tree contents;
2476 timevar_push (TV_PARSE_STRUCT);
2477 contents = NULL_TREE;
2478 c_parser_consume_token (parser);
2479 /* Handle the Objective-C @defs construct,
2480 e.g. foo(sizeof(struct{ @defs(ClassName) }));. */
2481 if (c_parser_next_token_is_keyword (parser, RID_AT_DEFS))
2483 tree name;
2484 gcc_assert (c_dialect_objc ());
2485 c_parser_consume_token (parser);
2486 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2487 goto end_at_defs;
2488 if (c_parser_next_token_is (parser, CPP_NAME)
2489 && c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME)
2491 name = c_parser_peek_token (parser)->value;
2492 c_parser_consume_token (parser);
2494 else
2496 c_parser_error (parser, "expected class name");
2497 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
2498 goto end_at_defs;
2500 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2501 "expected %<)%>");
2502 contents = nreverse (objc_get_class_ivars (name));
2504 end_at_defs:
2505 /* Parse the struct-declarations and semicolons. Problems with
2506 semicolons are diagnosed here; empty structures are diagnosed
2507 elsewhere. */
2508 while (true)
2510 tree decls;
2511 /* Parse any stray semicolon. */
2512 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
2514 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
2515 "extra semicolon in struct or union specified");
2516 c_parser_consume_token (parser);
2517 continue;
2519 /* Stop if at the end of the struct or union contents. */
2520 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2522 c_parser_consume_token (parser);
2523 break;
2525 /* Accept #pragmas at struct scope. */
2526 if (c_parser_next_token_is (parser, CPP_PRAGMA))
2528 c_parser_pragma (parser, pragma_external);
2529 continue;
2531 /* Parse some comma-separated declarations, but not the
2532 trailing semicolon if any. */
2533 decls = c_parser_struct_declaration (parser);
2534 contents = chainon (decls, contents);
2535 /* If no semicolon follows, either we have a parse error or
2536 are at the end of the struct or union and should
2537 pedwarn. */
2538 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
2539 c_parser_consume_token (parser);
2540 else
2542 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2543 pedwarn (c_parser_peek_token (parser)->location, 0,
2544 "no semicolon at end of struct or union");
2545 else if (parser->error
2546 || !c_parser_next_token_starts_declspecs (parser))
2548 c_parser_error (parser, "expected %<;%>");
2549 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
2550 break;
2553 /* If we come here, we have already emitted an error
2554 for an expected `;', identifier or `(', and we also
2555 recovered already. Go on with the next field. */
2558 postfix_attrs = c_parser_attributes (parser);
2559 ret.spec = finish_struct (struct_loc, type, nreverse (contents),
2560 chainon (attrs, postfix_attrs), struct_info);
2561 ret.kind = ctsk_tagdef;
2562 ret.expr = NULL_TREE;
2563 ret.expr_const_operands = true;
2564 timevar_pop (TV_PARSE_STRUCT);
2565 return ret;
2567 else if (!ident)
2569 c_parser_error (parser, "expected %<{%>");
2570 ret.spec = error_mark_node;
2571 ret.kind = ctsk_tagref;
2572 ret.expr = NULL_TREE;
2573 ret.expr_const_operands = true;
2574 return ret;
2576 ret = parser_xref_tag (ident_loc, code, ident);
2577 return ret;
2580 /* Parse a struct-declaration (C90 6.5.2.1, C99 6.7.2.1), *without*
2581 the trailing semicolon.
2583 struct-declaration:
2584 specifier-qualifier-list struct-declarator-list
2585 static_assert-declaration-no-semi
2587 specifier-qualifier-list:
2588 type-specifier specifier-qualifier-list[opt]
2589 type-qualifier specifier-qualifier-list[opt]
2590 attributes specifier-qualifier-list[opt]
2592 struct-declarator-list:
2593 struct-declarator
2594 struct-declarator-list , attributes[opt] struct-declarator
2596 struct-declarator:
2597 declarator attributes[opt]
2598 declarator[opt] : constant-expression attributes[opt]
2600 GNU extensions:
2602 struct-declaration:
2603 __extension__ struct-declaration
2604 specifier-qualifier-list
2606 Unlike the ISO C syntax, semicolons are handled elsewhere. The use
2607 of attributes where shown is a GNU extension. In GNU C, we accept
2608 any expression without commas in the syntax (assignment
2609 expressions, not just conditional expressions); assignment
2610 expressions will be diagnosed as non-constant. */
2612 static tree
2613 c_parser_struct_declaration (c_parser *parser)
2615 struct c_declspecs *specs;
2616 tree prefix_attrs;
2617 tree all_prefix_attrs;
2618 tree decls;
2619 location_t decl_loc;
2620 if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
2622 int ext;
2623 tree decl;
2624 ext = disable_extension_diagnostics ();
2625 c_parser_consume_token (parser);
2626 decl = c_parser_struct_declaration (parser);
2627 restore_extension_diagnostics (ext);
2628 return decl;
2630 if (c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT))
2632 c_parser_static_assert_declaration_no_semi (parser);
2633 return NULL_TREE;
2635 specs = build_null_declspecs ();
2636 decl_loc = c_parser_peek_token (parser)->location;
2637 c_parser_declspecs (parser, specs, false, true, true, cla_nonabstract_decl);
2638 if (parser->error)
2639 return NULL_TREE;
2640 if (!specs->declspecs_seen_p)
2642 c_parser_error (parser, "expected specifier-qualifier-list");
2643 return NULL_TREE;
2645 finish_declspecs (specs);
2646 if (c_parser_next_token_is (parser, CPP_SEMICOLON)
2647 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2649 tree ret;
2650 if (specs->typespec_kind == ctsk_none)
2652 pedwarn (decl_loc, OPT_Wpedantic,
2653 "ISO C forbids member declarations with no members");
2654 shadow_tag_warned (specs, pedantic);
2655 ret = NULL_TREE;
2657 else
2659 /* Support for unnamed structs or unions as members of
2660 structs or unions (which is [a] useful and [b] supports
2661 MS P-SDK). */
2662 tree attrs = NULL;
2664 ret = grokfield (c_parser_peek_token (parser)->location,
2665 build_id_declarator (NULL_TREE), specs,
2666 NULL_TREE, &attrs);
2667 if (ret)
2668 decl_attributes (&ret, attrs, 0);
2670 return ret;
2673 /* Provide better error recovery. Note that a type name here is valid,
2674 and will be treated as a field name. */
2675 if (specs->typespec_kind == ctsk_tagdef
2676 && TREE_CODE (specs->type) != ENUMERAL_TYPE
2677 && c_parser_next_token_starts_declspecs (parser)
2678 && !c_parser_next_token_is (parser, CPP_NAME))
2680 c_parser_error (parser, "expected %<;%>, identifier or %<(%>");
2681 parser->error = false;
2682 return NULL_TREE;
2685 pending_xref_error ();
2686 prefix_attrs = specs->attrs;
2687 all_prefix_attrs = prefix_attrs;
2688 specs->attrs = NULL_TREE;
2689 decls = NULL_TREE;
2690 while (true)
2692 /* Declaring one or more declarators or un-named bit-fields. */
2693 struct c_declarator *declarator;
2694 bool dummy = false;
2695 if (c_parser_next_token_is (parser, CPP_COLON))
2696 declarator = build_id_declarator (NULL_TREE);
2697 else
2698 declarator = c_parser_declarator (parser,
2699 specs->typespec_kind != ctsk_none,
2700 C_DTR_NORMAL, &dummy);
2701 if (declarator == NULL)
2703 c_parser_skip_to_end_of_block_or_statement (parser);
2704 break;
2706 if (c_parser_next_token_is (parser, CPP_COLON)
2707 || c_parser_next_token_is (parser, CPP_COMMA)
2708 || c_parser_next_token_is (parser, CPP_SEMICOLON)
2709 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE)
2710 || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2712 tree postfix_attrs = NULL_TREE;
2713 tree width = NULL_TREE;
2714 tree d;
2715 if (c_parser_next_token_is (parser, CPP_COLON))
2717 c_parser_consume_token (parser);
2718 width = c_parser_expr_no_commas (parser, NULL).value;
2720 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2721 postfix_attrs = c_parser_attributes (parser);
2722 d = grokfield (c_parser_peek_token (parser)->location,
2723 declarator, specs, width, &all_prefix_attrs);
2724 decl_attributes (&d, chainon (postfix_attrs,
2725 all_prefix_attrs), 0);
2726 DECL_CHAIN (d) = decls;
2727 decls = d;
2728 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2729 all_prefix_attrs = chainon (c_parser_attributes (parser),
2730 prefix_attrs);
2731 else
2732 all_prefix_attrs = prefix_attrs;
2733 if (c_parser_next_token_is (parser, CPP_COMMA))
2734 c_parser_consume_token (parser);
2735 else if (c_parser_next_token_is (parser, CPP_SEMICOLON)
2736 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2738 /* Semicolon consumed in caller. */
2739 break;
2741 else
2743 c_parser_error (parser, "expected %<,%>, %<;%> or %<}%>");
2744 break;
2747 else
2749 c_parser_error (parser,
2750 "expected %<:%>, %<,%>, %<;%>, %<}%> or "
2751 "%<__attribute__%>");
2752 break;
2755 return decls;
2758 /* Parse a typeof specifier (a GNU extension).
2760 typeof-specifier:
2761 typeof ( expression )
2762 typeof ( type-name )
2765 static struct c_typespec
2766 c_parser_typeof_specifier (c_parser *parser)
2768 struct c_typespec ret;
2769 ret.kind = ctsk_typeof;
2770 ret.spec = error_mark_node;
2771 ret.expr = NULL_TREE;
2772 ret.expr_const_operands = true;
2773 gcc_assert (c_parser_next_token_is_keyword (parser, RID_TYPEOF));
2774 c_parser_consume_token (parser);
2775 c_inhibit_evaluation_warnings++;
2776 in_typeof++;
2777 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2779 c_inhibit_evaluation_warnings--;
2780 in_typeof--;
2781 return ret;
2783 if (c_parser_next_tokens_start_typename (parser, cla_prefer_id))
2785 struct c_type_name *type = c_parser_type_name (parser);
2786 c_inhibit_evaluation_warnings--;
2787 in_typeof--;
2788 if (type != NULL)
2790 ret.spec = groktypename (type, &ret.expr, &ret.expr_const_operands);
2791 pop_maybe_used (variably_modified_type_p (ret.spec, NULL_TREE));
2794 else
2796 bool was_vm;
2797 location_t here = c_parser_peek_token (parser)->location;
2798 struct c_expr expr = c_parser_expression (parser);
2799 c_inhibit_evaluation_warnings--;
2800 in_typeof--;
2801 if (TREE_CODE (expr.value) == COMPONENT_REF
2802 && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
2803 error_at (here, "%<typeof%> applied to a bit-field");
2804 mark_exp_read (expr.value);
2805 ret.spec = TREE_TYPE (expr.value);
2806 was_vm = variably_modified_type_p (ret.spec, NULL_TREE);
2807 /* This is returned with the type so that when the type is
2808 evaluated, this can be evaluated. */
2809 if (was_vm)
2810 ret.expr = c_fully_fold (expr.value, false, &ret.expr_const_operands);
2811 pop_maybe_used (was_vm);
2813 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
2814 return ret;
2817 /* Parse an alignment-specifier.
2819 C11 6.7.5:
2821 alignment-specifier:
2822 _Alignas ( type-name )
2823 _Alignas ( constant-expression )
2826 static tree
2827 c_parser_alignas_specifier (c_parser * parser)
2829 tree ret = error_mark_node;
2830 location_t loc = c_parser_peek_token (parser)->location;
2831 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ALIGNAS));
2832 c_parser_consume_token (parser);
2833 if (!flag_isoc11)
2835 if (flag_isoc99)
2836 pedwarn (loc, OPT_Wpedantic,
2837 "ISO C99 does not support %<_Alignas%>");
2838 else
2839 pedwarn (loc, OPT_Wpedantic,
2840 "ISO C90 does not support %<_Alignas%>");
2842 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2843 return ret;
2844 if (c_parser_next_tokens_start_typename (parser, cla_prefer_id))
2846 struct c_type_name *type = c_parser_type_name (parser);
2847 if (type != NULL)
2848 ret = c_alignof (loc, groktypename (type, NULL, NULL));
2850 else
2851 ret = c_parser_expr_no_commas (parser, NULL).value;
2852 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
2853 return ret;
2856 /* Parse a declarator, possibly an abstract declarator (C90 6.5.4,
2857 6.5.5, C99 6.7.5, 6.7.6). If TYPE_SEEN_P then a typedef name may
2858 be redeclared; otherwise it may not. KIND indicates which kind of
2859 declarator is wanted. Returns a valid declarator except in the
2860 case of a syntax error in which case NULL is returned. *SEEN_ID is
2861 set to true if an identifier being declared is seen; this is used
2862 to diagnose bad forms of abstract array declarators and to
2863 determine whether an identifier list is syntactically permitted.
2865 declarator:
2866 pointer[opt] direct-declarator
2868 direct-declarator:
2869 identifier
2870 ( attributes[opt] declarator )
2871 direct-declarator array-declarator
2872 direct-declarator ( parameter-type-list )
2873 direct-declarator ( identifier-list[opt] )
2875 pointer:
2876 * type-qualifier-list[opt]
2877 * type-qualifier-list[opt] pointer
2879 type-qualifier-list:
2880 type-qualifier
2881 attributes
2882 type-qualifier-list type-qualifier
2883 type-qualifier-list attributes
2885 parameter-type-list:
2886 parameter-list
2887 parameter-list , ...
2889 parameter-list:
2890 parameter-declaration
2891 parameter-list , parameter-declaration
2893 parameter-declaration:
2894 declaration-specifiers declarator attributes[opt]
2895 declaration-specifiers abstract-declarator[opt] attributes[opt]
2897 identifier-list:
2898 identifier
2899 identifier-list , identifier
2901 abstract-declarator:
2902 pointer
2903 pointer[opt] direct-abstract-declarator
2905 direct-abstract-declarator:
2906 ( attributes[opt] abstract-declarator )
2907 direct-abstract-declarator[opt] array-declarator
2908 direct-abstract-declarator[opt] ( parameter-type-list[opt] )
2910 GNU extensions:
2912 direct-declarator:
2913 direct-declarator ( parameter-forward-declarations
2914 parameter-type-list[opt] )
2916 direct-abstract-declarator:
2917 direct-abstract-declarator[opt] ( parameter-forward-declarations
2918 parameter-type-list[opt] )
2920 parameter-forward-declarations:
2921 parameter-list ;
2922 parameter-forward-declarations parameter-list ;
2924 The uses of attributes shown above are GNU extensions.
2926 Some forms of array declarator are not included in C99 in the
2927 syntax for abstract declarators; these are disallowed elsewhere.
2928 This may be a defect (DR#289).
2930 This function also accepts an omitted abstract declarator as being
2931 an abstract declarator, although not part of the formal syntax. */
2933 static struct c_declarator *
2934 c_parser_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
2935 bool *seen_id)
2937 /* Parse any initial pointer part. */
2938 if (c_parser_next_token_is (parser, CPP_MULT))
2940 struct c_declspecs *quals_attrs = build_null_declspecs ();
2941 struct c_declarator *inner;
2942 c_parser_consume_token (parser);
2943 c_parser_declspecs (parser, quals_attrs, false, false, true, cla_prefer_id);
2944 inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
2945 if (inner == NULL)
2946 return NULL;
2947 else
2948 return make_pointer_declarator (quals_attrs, inner);
2950 /* Now we have a direct declarator, direct abstract declarator or
2951 nothing (which counts as a direct abstract declarator here). */
2952 return c_parser_direct_declarator (parser, type_seen_p, kind, seen_id);
2955 /* Parse a direct declarator or direct abstract declarator; arguments
2956 as c_parser_declarator. */
2958 static struct c_declarator *
2959 c_parser_direct_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
2960 bool *seen_id)
2962 /* The direct declarator must start with an identifier (possibly
2963 omitted) or a parenthesized declarator (possibly abstract). In
2964 an ordinary declarator, initial parentheses must start a
2965 parenthesized declarator. In an abstract declarator or parameter
2966 declarator, they could start a parenthesized declarator or a
2967 parameter list. To tell which, the open parenthesis and any
2968 following attributes must be read. If a declaration specifier
2969 follows, then it is a parameter list; if the specifier is a
2970 typedef name, there might be an ambiguity about redeclaring it,
2971 which is resolved in the direction of treating it as a typedef
2972 name. If a close parenthesis follows, it is also an empty
2973 parameter list, as the syntax does not permit empty abstract
2974 declarators. Otherwise, it is a parenthesized declarator (in
2975 which case the analysis may be repeated inside it, recursively).
2977 ??? There is an ambiguity in a parameter declaration "int
2978 (__attribute__((foo)) x)", where x is not a typedef name: it
2979 could be an abstract declarator for a function, or declare x with
2980 parentheses. The proper resolution of this ambiguity needs
2981 documenting. At present we follow an accident of the old
2982 parser's implementation, whereby the first parameter must have
2983 some declaration specifiers other than just attributes. Thus as
2984 a parameter declaration it is treated as a parenthesized
2985 parameter named x, and as an abstract declarator it is
2986 rejected.
2988 ??? Also following the old parser, attributes inside an empty
2989 parameter list are ignored, making it a list not yielding a
2990 prototype, rather than giving an error or making it have one
2991 parameter with implicit type int.
2993 ??? Also following the old parser, typedef names may be
2994 redeclared in declarators, but not Objective-C class names. */
2996 if (kind != C_DTR_ABSTRACT
2997 && c_parser_next_token_is (parser, CPP_NAME)
2998 && ((type_seen_p
2999 && (c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME
3000 || c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME))
3001 || c_parser_peek_token (parser)->id_kind == C_ID_ID))
3003 struct c_declarator *inner
3004 = build_id_declarator (c_parser_peek_token (parser)->value);
3005 *seen_id = true;
3006 inner->id_loc = c_parser_peek_token (parser)->location;
3007 c_parser_consume_token (parser);
3008 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
3011 if (kind != C_DTR_NORMAL
3012 && c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
3014 struct c_declarator *inner = build_id_declarator (NULL_TREE);
3015 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
3018 /* Either we are at the end of an abstract declarator, or we have
3019 parentheses. */
3021 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
3023 tree attrs;
3024 struct c_declarator *inner;
3025 c_parser_consume_token (parser);
3026 attrs = c_parser_attributes (parser);
3027 if (kind != C_DTR_NORMAL
3028 && (c_parser_next_token_starts_declspecs (parser)
3029 || c_parser_next_token_is (parser, CPP_CLOSE_PAREN)))
3031 struct c_arg_info *args
3032 = c_parser_parms_declarator (parser, kind == C_DTR_NORMAL,
3033 attrs);
3034 if (args == NULL)
3035 return NULL;
3036 else
3038 inner
3039 = build_function_declarator (args,
3040 build_id_declarator (NULL_TREE));
3041 return c_parser_direct_declarator_inner (parser, *seen_id,
3042 inner);
3045 /* A parenthesized declarator. */
3046 inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
3047 if (inner != NULL && attrs != NULL)
3048 inner = build_attrs_declarator (attrs, inner);
3049 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3051 c_parser_consume_token (parser);
3052 if (inner == NULL)
3053 return NULL;
3054 else
3055 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
3057 else
3059 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3060 "expected %<)%>");
3061 return NULL;
3064 else
3066 if (kind == C_DTR_NORMAL)
3068 c_parser_error (parser, "expected identifier or %<(%>");
3069 return NULL;
3071 else
3072 return build_id_declarator (NULL_TREE);
3076 /* Parse part of a direct declarator or direct abstract declarator,
3077 given that some (in INNER) has already been parsed; ID_PRESENT is
3078 true if an identifier is present, false for an abstract
3079 declarator. */
3081 static struct c_declarator *
3082 c_parser_direct_declarator_inner (c_parser *parser, bool id_present,
3083 struct c_declarator *inner)
3085 /* Parse a sequence of array declarators and parameter lists. */
3086 if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
3088 location_t brace_loc = c_parser_peek_token (parser)->location;
3089 struct c_declarator *declarator;
3090 struct c_declspecs *quals_attrs = build_null_declspecs ();
3091 bool static_seen;
3092 bool star_seen;
3093 tree dimen;
3094 c_parser_consume_token (parser);
3095 c_parser_declspecs (parser, quals_attrs, false, false, true, cla_prefer_id);
3096 static_seen = c_parser_next_token_is_keyword (parser, RID_STATIC);
3097 if (static_seen)
3098 c_parser_consume_token (parser);
3099 if (static_seen && !quals_attrs->declspecs_seen_p)
3100 c_parser_declspecs (parser, quals_attrs, false, false, true, cla_prefer_id);
3101 if (!quals_attrs->declspecs_seen_p)
3102 quals_attrs = NULL;
3103 /* If "static" is present, there must be an array dimension.
3104 Otherwise, there may be a dimension, "*", or no
3105 dimension. */
3106 if (static_seen)
3108 star_seen = false;
3109 dimen = c_parser_expr_no_commas (parser, NULL).value;
3111 else
3113 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3115 dimen = NULL_TREE;
3116 star_seen = false;
3118 else if (c_parser_next_token_is (parser, CPP_MULT))
3120 if (c_parser_peek_2nd_token (parser)->type == CPP_CLOSE_SQUARE)
3122 dimen = NULL_TREE;
3123 star_seen = true;
3124 c_parser_consume_token (parser);
3126 else
3128 star_seen = false;
3129 dimen = c_parser_expr_no_commas (parser, NULL).value;
3132 else
3134 star_seen = false;
3135 dimen = c_parser_expr_no_commas (parser, NULL).value;
3138 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3139 c_parser_consume_token (parser);
3140 else
3142 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
3143 "expected %<]%>");
3144 return NULL;
3146 if (dimen)
3147 mark_exp_read (dimen);
3148 declarator = build_array_declarator (brace_loc, dimen, quals_attrs,
3149 static_seen, star_seen);
3150 if (declarator == NULL)
3151 return NULL;
3152 inner = set_array_declarator_inner (declarator, inner);
3153 return c_parser_direct_declarator_inner (parser, id_present, inner);
3155 else if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
3157 tree attrs;
3158 struct c_arg_info *args;
3159 c_parser_consume_token (parser);
3160 attrs = c_parser_attributes (parser);
3161 args = c_parser_parms_declarator (parser, id_present, attrs);
3162 if (args == NULL)
3163 return NULL;
3164 else
3166 inner = build_function_declarator (args, inner);
3167 return c_parser_direct_declarator_inner (parser, id_present, inner);
3170 return inner;
3173 /* Parse a parameter list or identifier list, including the closing
3174 parenthesis but not the opening one. ATTRS are the attributes at
3175 the start of the list. ID_LIST_OK is true if an identifier list is
3176 acceptable; such a list must not have attributes at the start. */
3178 static struct c_arg_info *
3179 c_parser_parms_declarator (c_parser *parser, bool id_list_ok, tree attrs)
3181 push_scope ();
3182 declare_parm_level ();
3183 /* If the list starts with an identifier, it is an identifier list.
3184 Otherwise, it is either a prototype list or an empty list. */
3185 if (id_list_ok
3186 && !attrs
3187 && c_parser_next_token_is (parser, CPP_NAME)
3188 && c_parser_peek_token (parser)->id_kind == C_ID_ID
3190 /* Look ahead to detect typos in type names. */
3191 && c_parser_peek_2nd_token (parser)->type != CPP_NAME
3192 && c_parser_peek_2nd_token (parser)->type != CPP_MULT
3193 && c_parser_peek_2nd_token (parser)->type != CPP_OPEN_PAREN
3194 && c_parser_peek_2nd_token (parser)->type != CPP_OPEN_SQUARE)
3196 tree list = NULL_TREE, *nextp = &list;
3197 while (c_parser_next_token_is (parser, CPP_NAME)
3198 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
3200 *nextp = build_tree_list (NULL_TREE,
3201 c_parser_peek_token (parser)->value);
3202 nextp = & TREE_CHAIN (*nextp);
3203 c_parser_consume_token (parser);
3204 if (c_parser_next_token_is_not (parser, CPP_COMMA))
3205 break;
3206 c_parser_consume_token (parser);
3207 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3209 c_parser_error (parser, "expected identifier");
3210 break;
3213 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3215 struct c_arg_info *ret = build_arg_info ();
3216 ret->types = list;
3217 c_parser_consume_token (parser);
3218 pop_scope ();
3219 return ret;
3221 else
3223 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3224 "expected %<)%>");
3225 pop_scope ();
3226 return NULL;
3229 else
3231 struct c_arg_info *ret = c_parser_parms_list_declarator (parser, attrs,
3232 NULL);
3233 pop_scope ();
3234 return ret;
3238 /* Parse a parameter list (possibly empty), including the closing
3239 parenthesis but not the opening one. ATTRS are the attributes at
3240 the start of the list. EXPR is NULL or an expression that needs to
3241 be evaluated for the side effects of array size expressions in the
3242 parameters. */
3244 static struct c_arg_info *
3245 c_parser_parms_list_declarator (c_parser *parser, tree attrs, tree expr)
3247 bool bad_parm = false;
3249 /* ??? Following the old parser, forward parameter declarations may
3250 use abstract declarators, and if no real parameter declarations
3251 follow the forward declarations then this is not diagnosed. Also
3252 note as above that attributes are ignored as the only contents of
3253 the parentheses, or as the only contents after forward
3254 declarations. */
3255 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3257 struct c_arg_info *ret = build_arg_info ();
3258 c_parser_consume_token (parser);
3259 return ret;
3261 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3263 struct c_arg_info *ret = build_arg_info ();
3265 if (flag_allow_parameterless_variadic_functions)
3267 /* F (...) is allowed. */
3268 ret->types = NULL_TREE;
3270 else
3272 /* Suppress -Wold-style-definition for this case. */
3273 ret->types = error_mark_node;
3274 error_at (c_parser_peek_token (parser)->location,
3275 "ISO C requires a named argument before %<...%>");
3277 c_parser_consume_token (parser);
3278 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3280 c_parser_consume_token (parser);
3281 return ret;
3283 else
3285 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3286 "expected %<)%>");
3287 return NULL;
3290 /* Nonempty list of parameters, either terminated with semicolon
3291 (forward declarations; recurse) or with close parenthesis (normal
3292 function) or with ", ... )" (variadic function). */
3293 while (true)
3295 /* Parse a parameter. */
3296 struct c_parm *parm = c_parser_parameter_declaration (parser, attrs);
3297 attrs = NULL_TREE;
3298 if (parm == NULL)
3299 bad_parm = true;
3300 else
3301 push_parm_decl (parm, &expr);
3302 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
3304 tree new_attrs;
3305 c_parser_consume_token (parser);
3306 mark_forward_parm_decls ();
3307 new_attrs = c_parser_attributes (parser);
3308 return c_parser_parms_list_declarator (parser, new_attrs, expr);
3310 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3312 c_parser_consume_token (parser);
3313 if (bad_parm)
3314 return NULL;
3315 else
3316 return get_parm_info (false, expr);
3318 if (!c_parser_require (parser, CPP_COMMA,
3319 "expected %<;%>, %<,%> or %<)%>"))
3321 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
3322 return NULL;
3324 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3326 c_parser_consume_token (parser);
3327 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3329 c_parser_consume_token (parser);
3330 if (bad_parm)
3331 return NULL;
3332 else
3333 return get_parm_info (true, expr);
3335 else
3337 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3338 "expected %<)%>");
3339 return NULL;
3345 /* Parse a parameter declaration. ATTRS are the attributes at the
3346 start of the declaration if it is the first parameter. */
3348 static struct c_parm *
3349 c_parser_parameter_declaration (c_parser *parser, tree attrs)
3351 struct c_declspecs *specs;
3352 struct c_declarator *declarator;
3353 tree prefix_attrs;
3354 tree postfix_attrs = NULL_TREE;
3355 bool dummy = false;
3357 /* Accept #pragmas between parameter declarations. */
3358 while (c_parser_next_token_is (parser, CPP_PRAGMA))
3359 c_parser_pragma (parser, pragma_external);
3361 if (!c_parser_next_token_starts_declspecs (parser))
3363 c_token *token = c_parser_peek_token (parser);
3364 if (parser->error)
3365 return NULL;
3366 c_parser_set_source_position_from_token (token);
3367 if (c_parser_next_tokens_start_typename (parser, cla_prefer_type))
3369 error ("unknown type name %qE", token->value);
3370 parser->error = true;
3372 /* ??? In some Objective-C cases '...' isn't applicable so there
3373 should be a different message. */
3374 else
3375 c_parser_error (parser,
3376 "expected declaration specifiers or %<...%>");
3377 c_parser_skip_to_end_of_parameter (parser);
3378 return NULL;
3380 specs = build_null_declspecs ();
3381 if (attrs)
3383 declspecs_add_attrs (input_location, specs, attrs);
3384 attrs = NULL_TREE;
3386 c_parser_declspecs (parser, specs, true, true, true, cla_nonabstract_decl);
3387 finish_declspecs (specs);
3388 pending_xref_error ();
3389 prefix_attrs = specs->attrs;
3390 specs->attrs = NULL_TREE;
3391 declarator = c_parser_declarator (parser,
3392 specs->typespec_kind != ctsk_none,
3393 C_DTR_PARM, &dummy);
3394 if (declarator == NULL)
3396 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
3397 return NULL;
3399 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
3400 postfix_attrs = c_parser_attributes (parser);
3401 return build_c_parm (specs, chainon (postfix_attrs, prefix_attrs),
3402 declarator);
3405 /* Parse a string literal in an asm expression. It should not be
3406 translated, and wide string literals are an error although
3407 permitted by the syntax. This is a GNU extension.
3409 asm-string-literal:
3410 string-literal
3412 ??? At present, following the old parser, the caller needs to have
3413 set lex_untranslated_string to 1. It would be better to follow the
3414 C++ parser rather than using this kludge. */
3416 static tree
3417 c_parser_asm_string_literal (c_parser *parser)
3419 tree str;
3420 int save_flag = warn_overlength_strings;
3421 warn_overlength_strings = 0;
3422 if (c_parser_next_token_is (parser, CPP_STRING))
3424 str = c_parser_peek_token (parser)->value;
3425 c_parser_consume_token (parser);
3427 else if (c_parser_next_token_is (parser, CPP_WSTRING))
3429 error_at (c_parser_peek_token (parser)->location,
3430 "wide string literal in %<asm%>");
3431 str = build_string (1, "");
3432 c_parser_consume_token (parser);
3434 else
3436 c_parser_error (parser, "expected string literal");
3437 str = NULL_TREE;
3439 warn_overlength_strings = save_flag;
3440 return str;
3443 /* Parse a simple asm expression. This is used in restricted
3444 contexts, where a full expression with inputs and outputs does not
3445 make sense. This is a GNU extension.
3447 simple-asm-expr:
3448 asm ( asm-string-literal )
3451 static tree
3452 c_parser_simple_asm_expr (c_parser *parser)
3454 tree str;
3455 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
3456 /* ??? Follow the C++ parser rather than using the
3457 lex_untranslated_string kludge. */
3458 parser->lex_untranslated_string = true;
3459 c_parser_consume_token (parser);
3460 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3462 parser->lex_untranslated_string = false;
3463 return NULL_TREE;
3465 str = c_parser_asm_string_literal (parser);
3466 parser->lex_untranslated_string = false;
3467 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
3469 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
3470 return NULL_TREE;
3472 return str;
3475 static tree
3476 c_parser_attribute_any_word (c_parser *parser)
3478 tree attr_name = NULL_TREE;
3480 if (c_parser_next_token_is (parser, CPP_KEYWORD))
3482 /* ??? See comment above about what keywords are accepted here. */
3483 bool ok;
3484 switch (c_parser_peek_token (parser)->keyword)
3486 case RID_STATIC:
3487 case RID_UNSIGNED:
3488 case RID_LONG:
3489 case RID_INT128:
3490 case RID_CONST:
3491 case RID_EXTERN:
3492 case RID_REGISTER:
3493 case RID_TYPEDEF:
3494 case RID_SHORT:
3495 case RID_INLINE:
3496 case RID_NORETURN:
3497 case RID_VOLATILE:
3498 case RID_SIGNED:
3499 case RID_AUTO:
3500 case RID_RESTRICT:
3501 case RID_COMPLEX:
3502 case RID_THREAD:
3503 case RID_INT:
3504 case RID_CHAR:
3505 case RID_FLOAT:
3506 case RID_DOUBLE:
3507 case RID_VOID:
3508 case RID_DFLOAT32:
3509 case RID_DFLOAT64:
3510 case RID_DFLOAT128:
3511 case RID_BOOL:
3512 case RID_FRACT:
3513 case RID_ACCUM:
3514 case RID_SAT:
3515 case RID_TRANSACTION_ATOMIC:
3516 case RID_TRANSACTION_CANCEL:
3517 ok = true;
3518 break;
3519 default:
3520 ok = false;
3521 break;
3523 if (!ok)
3524 return NULL_TREE;
3526 /* Accept __attribute__((__const)) as __attribute__((const)) etc. */
3527 attr_name = ridpointers[(int) c_parser_peek_token (parser)->keyword];
3529 else if (c_parser_next_token_is (parser, CPP_NAME))
3530 attr_name = c_parser_peek_token (parser)->value;
3532 return attr_name;
3535 /* Parse (possibly empty) attributes. This is a GNU extension.
3537 attributes:
3538 empty
3539 attributes attribute
3541 attribute:
3542 __attribute__ ( ( attribute-list ) )
3544 attribute-list:
3545 attrib
3546 attribute_list , attrib
3548 attrib:
3549 empty
3550 any-word
3551 any-word ( identifier )
3552 any-word ( identifier , nonempty-expr-list )
3553 any-word ( expr-list )
3555 where the "identifier" must not be declared as a type, and
3556 "any-word" may be any identifier (including one declared as a
3557 type), a reserved word storage class specifier, type specifier or
3558 type qualifier. ??? This still leaves out most reserved keywords
3559 (following the old parser), shouldn't we include them, and why not
3560 allow identifiers declared as types to start the arguments? */
3562 static tree
3563 c_parser_attributes (c_parser *parser)
3565 tree attrs = NULL_TREE;
3566 while (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
3568 /* ??? Follow the C++ parser rather than using the
3569 lex_untranslated_string kludge. */
3570 parser->lex_untranslated_string = true;
3571 c_parser_consume_token (parser);
3572 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3574 parser->lex_untranslated_string = false;
3575 return attrs;
3577 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3579 parser->lex_untranslated_string = false;
3580 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
3581 return attrs;
3583 /* Parse the attribute list. */
3584 while (c_parser_next_token_is (parser, CPP_COMMA)
3585 || c_parser_next_token_is (parser, CPP_NAME)
3586 || c_parser_next_token_is (parser, CPP_KEYWORD))
3588 tree attr, attr_name, attr_args;
3589 VEC(tree,gc) *expr_list;
3590 if (c_parser_next_token_is (parser, CPP_COMMA))
3592 c_parser_consume_token (parser);
3593 continue;
3596 attr_name = c_parser_attribute_any_word (parser);
3597 if (attr_name == NULL)
3598 break;
3599 c_parser_consume_token (parser);
3600 if (c_parser_next_token_is_not (parser, CPP_OPEN_PAREN))
3602 attr = build_tree_list (attr_name, NULL_TREE);
3603 attrs = chainon (attrs, attr);
3604 continue;
3606 c_parser_consume_token (parser);
3607 /* Parse the attribute contents. If they start with an
3608 identifier which is followed by a comma or close
3609 parenthesis, then the arguments start with that
3610 identifier; otherwise they are an expression list.
3611 In objective-c the identifier may be a classname. */
3612 if (c_parser_next_token_is (parser, CPP_NAME)
3613 && (c_parser_peek_token (parser)->id_kind == C_ID_ID
3614 || (c_dialect_objc ()
3615 && c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME))
3616 && ((c_parser_peek_2nd_token (parser)->type == CPP_COMMA)
3617 || (c_parser_peek_2nd_token (parser)->type
3618 == CPP_CLOSE_PAREN)))
3620 tree arg1 = c_parser_peek_token (parser)->value;
3621 c_parser_consume_token (parser);
3622 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3623 attr_args = build_tree_list (NULL_TREE, arg1);
3624 else
3626 tree tree_list;
3627 c_parser_consume_token (parser);
3628 expr_list = c_parser_expr_list (parser, false, true,
3629 NULL, NULL);
3630 tree_list = build_tree_list_vec (expr_list);
3631 attr_args = tree_cons (NULL_TREE, arg1, tree_list);
3632 release_tree_vector (expr_list);
3635 else
3637 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3638 attr_args = NULL_TREE;
3639 else
3641 expr_list = c_parser_expr_list (parser, false, true,
3642 NULL, NULL);
3643 attr_args = build_tree_list_vec (expr_list);
3644 release_tree_vector (expr_list);
3647 attr = build_tree_list (attr_name, attr_args);
3648 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3649 c_parser_consume_token (parser);
3650 else
3652 parser->lex_untranslated_string = false;
3653 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3654 "expected %<)%>");
3655 return attrs;
3657 attrs = chainon (attrs, attr);
3659 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3660 c_parser_consume_token (parser);
3661 else
3663 parser->lex_untranslated_string = false;
3664 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3665 "expected %<)%>");
3666 return attrs;
3668 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3669 c_parser_consume_token (parser);
3670 else
3672 parser->lex_untranslated_string = false;
3673 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3674 "expected %<)%>");
3675 return attrs;
3677 parser->lex_untranslated_string = false;
3679 return attrs;
3682 /* Parse a type name (C90 6.5.5, C99 6.7.6).
3684 type-name:
3685 specifier-qualifier-list abstract-declarator[opt]
3688 static struct c_type_name *
3689 c_parser_type_name (c_parser *parser)
3691 struct c_declspecs *specs = build_null_declspecs ();
3692 struct c_declarator *declarator;
3693 struct c_type_name *ret;
3694 bool dummy = false;
3695 c_parser_declspecs (parser, specs, false, true, true, cla_prefer_type);
3696 if (!specs->declspecs_seen_p)
3698 c_parser_error (parser, "expected specifier-qualifier-list");
3699 return NULL;
3701 if (specs->type != error_mark_node)
3703 pending_xref_error ();
3704 finish_declspecs (specs);
3706 declarator = c_parser_declarator (parser,
3707 specs->typespec_kind != ctsk_none,
3708 C_DTR_ABSTRACT, &dummy);
3709 if (declarator == NULL)
3710 return NULL;
3711 ret = XOBNEW (&parser_obstack, struct c_type_name);
3712 ret->specs = specs;
3713 ret->declarator = declarator;
3714 return ret;
3717 /* Parse an initializer (C90 6.5.7, C99 6.7.8).
3719 initializer:
3720 assignment-expression
3721 { initializer-list }
3722 { initializer-list , }
3724 initializer-list:
3725 designation[opt] initializer
3726 initializer-list , designation[opt] initializer
3728 designation:
3729 designator-list =
3731 designator-list:
3732 designator
3733 designator-list designator
3735 designator:
3736 array-designator
3737 . identifier
3739 array-designator:
3740 [ constant-expression ]
3742 GNU extensions:
3744 initializer:
3747 designation:
3748 array-designator
3749 identifier :
3751 array-designator:
3752 [ constant-expression ... constant-expression ]
3754 Any expression without commas is accepted in the syntax for the
3755 constant-expressions, with non-constant expressions rejected later.
3757 This function is only used for top-level initializers; for nested
3758 ones, see c_parser_initval. */
3760 static struct c_expr
3761 c_parser_initializer (c_parser *parser)
3763 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
3764 return c_parser_braced_init (parser, NULL_TREE, false);
3765 else
3767 struct c_expr ret;
3768 location_t loc = c_parser_peek_token (parser)->location;
3769 ret = c_parser_expr_no_commas (parser, NULL);
3770 if (TREE_CODE (ret.value) != STRING_CST
3771 && TREE_CODE (ret.value) != COMPOUND_LITERAL_EXPR)
3772 ret = default_function_array_read_conversion (loc, ret);
3773 return ret;
3777 /* Parse a braced initializer list. TYPE is the type specified for a
3778 compound literal, and NULL_TREE for other initializers and for
3779 nested braced lists. NESTED_P is true for nested braced lists,
3780 false for the list of a compound literal or the list that is the
3781 top-level initializer in a declaration. */
3783 static struct c_expr
3784 c_parser_braced_init (c_parser *parser, tree type, bool nested_p)
3786 struct c_expr ret;
3787 struct obstack braced_init_obstack;
3788 location_t brace_loc = c_parser_peek_token (parser)->location;
3789 gcc_obstack_init (&braced_init_obstack);
3790 gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
3791 c_parser_consume_token (parser);
3792 if (nested_p)
3793 push_init_level (0, &braced_init_obstack);
3794 else
3795 really_start_incremental_init (type);
3796 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3798 pedwarn (brace_loc, OPT_Wpedantic, "ISO C forbids empty initializer braces");
3800 else
3802 /* Parse a non-empty initializer list, possibly with a trailing
3803 comma. */
3804 while (true)
3806 c_parser_initelt (parser, &braced_init_obstack);
3807 if (parser->error)
3808 break;
3809 if (c_parser_next_token_is (parser, CPP_COMMA))
3810 c_parser_consume_token (parser);
3811 else
3812 break;
3813 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3814 break;
3817 if (c_parser_next_token_is_not (parser, CPP_CLOSE_BRACE))
3819 ret.value = error_mark_node;
3820 ret.original_code = ERROR_MARK;
3821 ret.original_type = NULL;
3822 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, "expected %<}%>");
3823 pop_init_level (0, &braced_init_obstack);
3824 obstack_free (&braced_init_obstack, NULL);
3825 return ret;
3827 c_parser_consume_token (parser);
3828 ret = pop_init_level (0, &braced_init_obstack);
3829 obstack_free (&braced_init_obstack, NULL);
3830 return ret;
3833 /* Parse a nested initializer, including designators. */
3835 static void
3836 c_parser_initelt (c_parser *parser, struct obstack * braced_init_obstack)
3838 /* Parse any designator or designator list. A single array
3839 designator may have the subsequent "=" omitted in GNU C, but a
3840 longer list or a structure member designator may not. */
3841 if (c_parser_next_token_is (parser, CPP_NAME)
3842 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
3844 /* Old-style structure member designator. */
3845 set_init_label (c_parser_peek_token (parser)->value,
3846 braced_init_obstack);
3847 /* Use the colon as the error location. */
3848 pedwarn (c_parser_peek_2nd_token (parser)->location, OPT_Wpedantic,
3849 "obsolete use of designated initializer with %<:%>");
3850 c_parser_consume_token (parser);
3851 c_parser_consume_token (parser);
3853 else
3855 /* des_seen is 0 if there have been no designators, 1 if there
3856 has been a single array designator and 2 otherwise. */
3857 int des_seen = 0;
3858 /* Location of a designator. */
3859 location_t des_loc = UNKNOWN_LOCATION; /* Quiet warning. */
3860 while (c_parser_next_token_is (parser, CPP_OPEN_SQUARE)
3861 || c_parser_next_token_is (parser, CPP_DOT))
3863 int des_prev = des_seen;
3864 if (!des_seen)
3865 des_loc = c_parser_peek_token (parser)->location;
3866 if (des_seen < 2)
3867 des_seen++;
3868 if (c_parser_next_token_is (parser, CPP_DOT))
3870 des_seen = 2;
3871 c_parser_consume_token (parser);
3872 if (c_parser_next_token_is (parser, CPP_NAME))
3874 set_init_label (c_parser_peek_token (parser)->value,
3875 braced_init_obstack);
3876 c_parser_consume_token (parser);
3878 else
3880 struct c_expr init;
3881 init.value = error_mark_node;
3882 init.original_code = ERROR_MARK;
3883 init.original_type = NULL;
3884 c_parser_error (parser, "expected identifier");
3885 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
3886 process_init_element (init, false, braced_init_obstack);
3887 return;
3890 else
3892 tree first, second;
3893 location_t ellipsis_loc = UNKNOWN_LOCATION; /* Quiet warning. */
3894 /* ??? Following the old parser, [ objc-receiver
3895 objc-message-args ] is accepted as an initializer,
3896 being distinguished from a designator by what follows
3897 the first assignment expression inside the square
3898 brackets, but after a first array designator a
3899 subsequent square bracket is for Objective-C taken to
3900 start an expression, using the obsolete form of
3901 designated initializer without '=', rather than
3902 possibly being a second level of designation: in LALR
3903 terms, the '[' is shifted rather than reducing
3904 designator to designator-list. */
3905 if (des_prev == 1 && c_dialect_objc ())
3907 des_seen = des_prev;
3908 break;
3910 if (des_prev == 0 && c_dialect_objc ())
3912 /* This might be an array designator or an
3913 Objective-C message expression. If the former,
3914 continue parsing here; if the latter, parse the
3915 remainder of the initializer given the starting
3916 primary-expression. ??? It might make sense to
3917 distinguish when des_prev == 1 as well; see
3918 previous comment. */
3919 tree rec, args;
3920 struct c_expr mexpr;
3921 c_parser_consume_token (parser);
3922 if (c_parser_peek_token (parser)->type == CPP_NAME
3923 && ((c_parser_peek_token (parser)->id_kind
3924 == C_ID_TYPENAME)
3925 || (c_parser_peek_token (parser)->id_kind
3926 == C_ID_CLASSNAME)))
3928 /* Type name receiver. */
3929 tree id = c_parser_peek_token (parser)->value;
3930 c_parser_consume_token (parser);
3931 rec = objc_get_class_reference (id);
3932 goto parse_message_args;
3934 first = c_parser_expr_no_commas (parser, NULL).value;
3935 mark_exp_read (first);
3936 if (c_parser_next_token_is (parser, CPP_ELLIPSIS)
3937 || c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3938 goto array_desig_after_first;
3939 /* Expression receiver. So far only one part
3940 without commas has been parsed; there might be
3941 more of the expression. */
3942 rec = first;
3943 while (c_parser_next_token_is (parser, CPP_COMMA))
3945 struct c_expr next;
3946 location_t comma_loc, exp_loc;
3947 comma_loc = c_parser_peek_token (parser)->location;
3948 c_parser_consume_token (parser);
3949 exp_loc = c_parser_peek_token (parser)->location;
3950 next = c_parser_expr_no_commas (parser, NULL);
3951 next = default_function_array_read_conversion (exp_loc,
3952 next);
3953 rec = build_compound_expr (comma_loc, rec, next.value);
3955 parse_message_args:
3956 /* Now parse the objc-message-args. */
3957 args = c_parser_objc_message_args (parser);
3958 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
3959 "expected %<]%>");
3960 mexpr.value
3961 = objc_build_message_expr (rec, args);
3962 mexpr.original_code = ERROR_MARK;
3963 mexpr.original_type = NULL;
3964 /* Now parse and process the remainder of the
3965 initializer, starting with this message
3966 expression as a primary-expression. */
3967 c_parser_initval (parser, &mexpr, braced_init_obstack);
3968 return;
3970 c_parser_consume_token (parser);
3971 first = c_parser_expr_no_commas (parser, NULL).value;
3972 mark_exp_read (first);
3973 array_desig_after_first:
3974 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3976 ellipsis_loc = c_parser_peek_token (parser)->location;
3977 c_parser_consume_token (parser);
3978 second = c_parser_expr_no_commas (parser, NULL).value;
3979 mark_exp_read (second);
3981 else
3982 second = NULL_TREE;
3983 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3985 c_parser_consume_token (parser);
3986 set_init_index (first, second, braced_init_obstack);
3987 if (second)
3988 pedwarn (ellipsis_loc, OPT_Wpedantic,
3989 "ISO C forbids specifying range of elements to initialize");
3991 else
3992 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
3993 "expected %<]%>");
3996 if (des_seen >= 1)
3998 if (c_parser_next_token_is (parser, CPP_EQ))
4000 if (!flag_isoc99)
4001 pedwarn (des_loc, OPT_Wpedantic,
4002 "ISO C90 forbids specifying subobject to initialize");
4003 c_parser_consume_token (parser);
4005 else
4007 if (des_seen == 1)
4008 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
4009 "obsolete use of designated initializer without %<=%>");
4010 else
4012 struct c_expr init;
4013 init.value = error_mark_node;
4014 init.original_code = ERROR_MARK;
4015 init.original_type = NULL;
4016 c_parser_error (parser, "expected %<=%>");
4017 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
4018 process_init_element (init, false, braced_init_obstack);
4019 return;
4024 c_parser_initval (parser, NULL, braced_init_obstack);
4027 /* Parse a nested initializer; as c_parser_initializer but parses
4028 initializers within braced lists, after any designators have been
4029 applied. If AFTER is not NULL then it is an Objective-C message
4030 expression which is the primary-expression starting the
4031 initializer. */
4033 static void
4034 c_parser_initval (c_parser *parser, struct c_expr *after,
4035 struct obstack * braced_init_obstack)
4037 struct c_expr init;
4038 gcc_assert (!after || c_dialect_objc ());
4039 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE) && !after)
4040 init = c_parser_braced_init (parser, NULL_TREE, true);
4041 else
4043 location_t loc = c_parser_peek_token (parser)->location;
4044 init = c_parser_expr_no_commas (parser, after);
4045 if (init.value != NULL_TREE
4046 && TREE_CODE (init.value) != STRING_CST
4047 && TREE_CODE (init.value) != COMPOUND_LITERAL_EXPR)
4048 init = default_function_array_read_conversion (loc, init);
4050 process_init_element (init, false, braced_init_obstack);
4053 /* Parse a compound statement (possibly a function body) (C90 6.6.2,
4054 C99 6.8.2).
4056 compound-statement:
4057 { block-item-list[opt] }
4058 { label-declarations block-item-list }
4060 block-item-list:
4061 block-item
4062 block-item-list block-item
4064 block-item:
4065 nested-declaration
4066 statement
4068 nested-declaration:
4069 declaration
4071 GNU extensions:
4073 compound-statement:
4074 { label-declarations block-item-list }
4076 nested-declaration:
4077 __extension__ nested-declaration
4078 nested-function-definition
4080 label-declarations:
4081 label-declaration
4082 label-declarations label-declaration
4084 label-declaration:
4085 __label__ identifier-list ;
4087 Allowing the mixing of declarations and code is new in C99. The
4088 GNU syntax also permits (not shown above) labels at the end of
4089 compound statements, which yield an error. We don't allow labels
4090 on declarations; this might seem like a natural extension, but
4091 there would be a conflict between attributes on the label and
4092 prefix attributes on the declaration. ??? The syntax follows the
4093 old parser in requiring something after label declarations.
4094 Although they are erroneous if the labels declared aren't defined,
4095 is it useful for the syntax to be this way?
4097 OpenMP:
4099 block-item:
4100 openmp-directive
4102 openmp-directive:
4103 barrier-directive
4104 flush-directive */
4106 static tree
4107 c_parser_compound_statement (c_parser *parser)
4109 tree stmt;
4110 location_t brace_loc;
4111 brace_loc = c_parser_peek_token (parser)->location;
4112 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
4114 /* Ensure a scope is entered and left anyway to avoid confusion
4115 if we have just prepared to enter a function body. */
4116 stmt = c_begin_compound_stmt (true);
4117 c_end_compound_stmt (brace_loc, stmt, true);
4118 return error_mark_node;
4120 stmt = c_begin_compound_stmt (true);
4121 c_parser_compound_statement_nostart (parser);
4122 return c_end_compound_stmt (brace_loc, stmt, true);
4125 /* Parse a compound statement except for the opening brace. This is
4126 used for parsing both compound statements and statement expressions
4127 (which follow different paths to handling the opening). */
4129 static void
4130 c_parser_compound_statement_nostart (c_parser *parser)
4132 bool last_stmt = false;
4133 bool last_label = false;
4134 bool save_valid_for_pragma = valid_location_for_stdc_pragma_p ();
4135 location_t label_loc = UNKNOWN_LOCATION; /* Quiet warning. */
4136 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
4138 c_parser_consume_token (parser);
4139 return;
4141 mark_valid_location_for_stdc_pragma (true);
4142 if (c_parser_next_token_is_keyword (parser, RID_LABEL))
4144 /* Read zero or more forward-declarations for labels that nested
4145 functions can jump to. */
4146 mark_valid_location_for_stdc_pragma (false);
4147 while (c_parser_next_token_is_keyword (parser, RID_LABEL))
4149 label_loc = c_parser_peek_token (parser)->location;
4150 c_parser_consume_token (parser);
4151 /* Any identifiers, including those declared as type names,
4152 are OK here. */
4153 while (true)
4155 tree label;
4156 if (c_parser_next_token_is_not (parser, CPP_NAME))
4158 c_parser_error (parser, "expected identifier");
4159 break;
4161 label
4162 = declare_label (c_parser_peek_token (parser)->value);
4163 C_DECLARED_LABEL_FLAG (label) = 1;
4164 add_stmt (build_stmt (label_loc, DECL_EXPR, label));
4165 c_parser_consume_token (parser);
4166 if (c_parser_next_token_is (parser, CPP_COMMA))
4167 c_parser_consume_token (parser);
4168 else
4169 break;
4171 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
4173 pedwarn (label_loc, OPT_Wpedantic, "ISO C forbids label declarations");
4175 /* We must now have at least one statement, label or declaration. */
4176 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
4178 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
4179 c_parser_error (parser, "expected declaration or statement");
4180 c_parser_consume_token (parser);
4181 return;
4183 /* Process all #pragma's just after the opening brace. This
4184 handles #pragma upc, which can only appear just after
4185 the opening brace, when it appears within a function body. */
4186 push_upc_consistency_mode ();
4187 permit_pragma_upc ();
4188 while (c_parser_next_token_is (parser, CPP_PRAGMA))
4190 location_t loc ATTRIBUTE_UNUSED = c_parser_peek_token (parser)->location;
4191 if (c_parser_pragma (parser, pragma_compound))
4192 last_label = false, last_stmt = true;
4193 parser->error = false;
4195 deny_pragma_upc ();
4196 while (c_parser_next_token_is_not (parser, CPP_CLOSE_BRACE))
4198 location_t loc = c_parser_peek_token (parser)->location;
4199 if (c_parser_next_token_is_keyword (parser, RID_CASE)
4200 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
4201 || (c_parser_next_token_is (parser, CPP_NAME)
4202 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
4204 if (c_parser_next_token_is_keyword (parser, RID_CASE))
4205 label_loc = c_parser_peek_2nd_token (parser)->location;
4206 else
4207 label_loc = c_parser_peek_token (parser)->location;
4208 last_label = true;
4209 last_stmt = false;
4210 mark_valid_location_for_stdc_pragma (false);
4211 c_parser_label (parser);
4213 else if (!last_label
4214 && c_parser_next_tokens_start_declaration (parser))
4216 last_label = false;
4217 mark_valid_location_for_stdc_pragma (false);
4218 c_parser_declaration_or_fndef (parser, true, true, true, true, true, NULL);
4219 if (last_stmt)
4220 pedwarn_c90 (loc,
4221 (pedantic && !flag_isoc99)
4222 ? OPT_Wpedantic
4223 : OPT_Wdeclaration_after_statement,
4224 "ISO C90 forbids mixed declarations and code");
4225 last_stmt = false;
4227 else if (!last_label
4228 && c_parser_next_token_is_keyword (parser, RID_EXTENSION))
4230 /* __extension__ can start a declaration, but is also an
4231 unary operator that can start an expression. Consume all
4232 but the last of a possible series of __extension__ to
4233 determine which. */
4234 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
4235 && (c_parser_peek_2nd_token (parser)->keyword
4236 == RID_EXTENSION))
4237 c_parser_consume_token (parser);
4238 if (c_token_starts_declaration (c_parser_peek_2nd_token (parser)))
4240 int ext;
4241 ext = disable_extension_diagnostics ();
4242 c_parser_consume_token (parser);
4243 last_label = false;
4244 mark_valid_location_for_stdc_pragma (false);
4245 c_parser_declaration_or_fndef (parser, true, true, true, true,
4246 true, NULL);
4247 /* Following the old parser, __extension__ does not
4248 disable this diagnostic. */
4249 restore_extension_diagnostics (ext);
4250 if (last_stmt)
4251 pedwarn_c90 (loc, (pedantic && !flag_isoc99)
4252 ? OPT_Wpedantic
4253 : OPT_Wdeclaration_after_statement,
4254 "ISO C90 forbids mixed declarations and code");
4255 last_stmt = false;
4257 else
4258 goto statement;
4260 else if (c_parser_next_token_is (parser, CPP_PRAGMA))
4262 /* External pragmas, and some omp pragmas, are not associated
4263 with regular c code, and so are not to be considered statements
4264 syntactically. This ensures that the user doesn't put them
4265 places that would turn into syntax errors if the directive
4266 were ignored. */
4267 if (c_parser_pragma (parser, pragma_compound))
4268 last_label = false, last_stmt = true;
4270 else if (c_parser_next_token_is (parser, CPP_EOF))
4272 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
4273 c_parser_error (parser, "expected declaration or statement");
4274 return;
4276 else if (c_parser_next_token_is_keyword (parser, RID_ELSE))
4278 if (parser->in_if_block)
4280 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
4281 error_at (loc, """expected %<}%> before %<else%>");
4282 return;
4284 else
4286 error_at (loc, "%<else%> without a previous %<if%>");
4287 c_parser_consume_token (parser);
4288 continue;
4291 else
4293 statement:
4294 last_label = false;
4295 last_stmt = true;
4296 mark_valid_location_for_stdc_pragma (false);
4297 c_parser_statement_after_labels (parser);
4300 parser->error = false;
4302 if (last_label)
4303 error_at (label_loc, "label at end of compound statement");
4304 c_parser_consume_token (parser);
4305 pop_upc_consistency_mode ();
4306 /* Restore the value we started with. */
4307 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
4310 /* Parse a label (C90 6.6.1, C99 6.8.1).
4312 label:
4313 identifier : attributes[opt]
4314 case constant-expression :
4315 default :
4317 GNU extensions:
4319 label:
4320 case constant-expression ... constant-expression :
4322 The use of attributes on labels is a GNU extension. The syntax in
4323 GNU C accepts any expressions without commas, non-constant
4324 expressions being rejected later. */
4326 static void
4327 c_parser_label (c_parser *parser)
4329 location_t loc1 = c_parser_peek_token (parser)->location;
4330 tree label = NULL_TREE;
4331 if (c_parser_next_token_is_keyword (parser, RID_CASE))
4333 tree exp1, exp2;
4334 c_parser_consume_token (parser);
4335 exp1 = c_parser_expr_no_commas (parser, NULL).value;
4336 if (c_parser_next_token_is (parser, CPP_COLON))
4338 c_parser_consume_token (parser);
4339 label = do_case (loc1, exp1, NULL_TREE);
4341 else if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
4343 c_parser_consume_token (parser);
4344 exp2 = c_parser_expr_no_commas (parser, NULL).value;
4345 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
4346 label = do_case (loc1, exp1, exp2);
4348 else
4349 c_parser_error (parser, "expected %<:%> or %<...%>");
4351 else if (c_parser_next_token_is_keyword (parser, RID_DEFAULT))
4353 c_parser_consume_token (parser);
4354 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
4355 label = do_case (loc1, NULL_TREE, NULL_TREE);
4357 else
4359 tree name = c_parser_peek_token (parser)->value;
4360 tree tlab;
4361 tree attrs;
4362 location_t loc2 = c_parser_peek_token (parser)->location;
4363 gcc_assert (c_parser_next_token_is (parser, CPP_NAME));
4364 c_parser_consume_token (parser);
4365 gcc_assert (c_parser_next_token_is (parser, CPP_COLON));
4366 c_parser_consume_token (parser);
4367 attrs = c_parser_attributes (parser);
4368 tlab = define_label (loc2, name);
4369 if (tlab)
4371 decl_attributes (&tlab, attrs, 0);
4372 label = add_stmt (build_stmt (loc1, LABEL_EXPR, tlab));
4375 if (label)
4377 if (c_parser_next_tokens_start_declaration (parser))
4379 error_at (c_parser_peek_token (parser)->location,
4380 "a label can only be part of a statement and "
4381 "a declaration is not a statement");
4382 c_parser_declaration_or_fndef (parser, /*fndef_ok*/ false,
4383 /*static_assert_ok*/ true,
4384 /*empty_ok*/ true, /*nested*/ true,
4385 /*start_attr_ok*/ true, NULL);
4390 /* Parse a statement (C90 6.6, C99 6.8).
4392 statement:
4393 labeled-statement
4394 compound-statement
4395 expression-statement
4396 selection-statement
4397 iteration-statement
4398 jump-statement
4400 labeled-statement:
4401 label statement
4403 expression-statement:
4404 expression[opt] ;
4406 selection-statement:
4407 if-statement
4408 switch-statement
4410 iteration-statement:
4411 while-statement
4412 do-statement
4413 for-statement
4415 jump-statement:
4416 goto identifier ;
4417 continue ;
4418 break ;
4419 return expression[opt] ;
4421 GNU extensions:
4423 statement:
4424 asm-statement
4426 jump-statement:
4427 goto * expression ;
4429 Objective-C:
4431 statement:
4432 objc-throw-statement
4433 objc-try-catch-statement
4434 objc-synchronized-statement
4436 objc-throw-statement:
4437 @throw expression ;
4438 @throw ;
4440 OpenMP:
4442 statement:
4443 openmp-construct
4445 openmp-construct:
4446 parallel-construct
4447 for-construct
4448 sections-construct
4449 single-construct
4450 parallel-for-construct
4451 parallel-sections-construct
4452 master-construct
4453 critical-construct
4454 atomic-construct
4455 ordered-construct
4457 parallel-construct:
4458 parallel-directive structured-block
4460 for-construct:
4461 for-directive iteration-statement
4463 sections-construct:
4464 sections-directive section-scope
4466 single-construct:
4467 single-directive structured-block
4469 parallel-for-construct:
4470 parallel-for-directive iteration-statement
4472 parallel-sections-construct:
4473 parallel-sections-directive section-scope
4475 master-construct:
4476 master-directive structured-block
4478 critical-construct:
4479 critical-directive structured-block
4481 atomic-construct:
4482 atomic-directive expression-statement
4484 ordered-construct:
4485 ordered-directive structured-block
4487 Transactional Memory:
4489 statement:
4490 transaction-statement
4491 transaction-cancel-statement
4494 static void
4495 c_parser_statement (c_parser *parser)
4497 while (c_parser_next_token_is_keyword (parser, RID_CASE)
4498 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
4499 || (c_parser_next_token_is (parser, CPP_NAME)
4500 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
4501 c_parser_label (parser);
4502 c_parser_statement_after_labels (parser);
4505 /* Parse a statement, other than a labeled statement. */
4507 static void
4508 c_parser_statement_after_labels (c_parser *parser)
4510 location_t loc = c_parser_peek_token (parser)->location;
4511 tree stmt = NULL_TREE;
4512 bool in_if_block = parser->in_if_block;
4513 parser->in_if_block = false;
4514 switch (c_parser_peek_token (parser)->type)
4516 case CPP_OPEN_BRACE:
4517 add_stmt (c_parser_compound_statement (parser));
4518 break;
4519 case CPP_KEYWORD:
4520 switch (c_parser_peek_token (parser)->keyword)
4522 case RID_IF:
4523 c_parser_if_statement (parser);
4524 break;
4525 case RID_SWITCH:
4526 c_parser_switch_statement (parser);
4527 break;
4528 case RID_WHILE:
4529 c_parser_while_statement (parser);
4530 break;
4531 case RID_DO:
4532 c_parser_do_statement (parser);
4533 break;
4534 case RID_FOR:
4535 c_parser_for_statement (parser);
4536 break;
4537 case RID_GOTO:
4538 c_parser_consume_token (parser);
4539 if (c_parser_next_token_is (parser, CPP_NAME))
4541 stmt = c_finish_goto_label (loc,
4542 c_parser_peek_token (parser)->value);
4543 c_parser_consume_token (parser);
4545 else if (c_parser_next_token_is (parser, CPP_MULT))
4547 tree val;
4549 c_parser_consume_token (parser);
4550 val = c_parser_expression (parser).value;
4551 mark_exp_read (val);
4552 stmt = c_finish_goto_ptr (loc, val);
4554 else
4555 c_parser_error (parser, "expected identifier or %<*%>");
4556 goto expect_semicolon;
4557 case RID_CONTINUE:
4558 c_parser_consume_token (parser);
4559 stmt = c_finish_bc_stmt (loc, &c_cont_label, false);
4560 goto expect_semicolon;
4561 case RID_BREAK:
4562 c_parser_consume_token (parser);
4563 stmt = c_finish_bc_stmt (loc, &c_break_label, true);
4564 goto expect_semicolon;
4565 case RID_RETURN:
4566 c_parser_consume_token (parser);
4567 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4569 stmt = c_finish_return (loc, NULL_TREE, NULL_TREE);
4570 c_parser_consume_token (parser);
4572 else
4574 struct c_expr expr = c_parser_expression_conv (parser);
4575 mark_exp_read (expr.value);
4576 stmt = c_finish_return (loc, expr.value, expr.original_type);
4577 goto expect_semicolon;
4579 break;
4580 case RID_ASM:
4581 stmt = c_parser_asm_statement (parser);
4582 break;
4583 case RID_TRANSACTION_ATOMIC:
4584 case RID_TRANSACTION_RELAXED:
4585 stmt = c_parser_transaction (parser,
4586 c_parser_peek_token (parser)->keyword);
4587 break;
4588 case RID_TRANSACTION_CANCEL:
4589 stmt = c_parser_transaction_cancel (parser);
4590 goto expect_semicolon;
4591 case RID_AT_THROW:
4592 gcc_assert (c_dialect_objc ());
4593 c_parser_consume_token (parser);
4594 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4596 stmt = objc_build_throw_stmt (loc, NULL_TREE);
4597 c_parser_consume_token (parser);
4599 else
4601 tree expr = c_parser_expression (parser).value;
4602 expr = c_fully_fold (expr, false, NULL);
4603 stmt = objc_build_throw_stmt (loc, expr);
4604 goto expect_semicolon;
4606 break;
4607 case RID_AT_TRY:
4608 gcc_assert (c_dialect_objc ());
4609 c_parser_objc_try_catch_finally_statement (parser);
4610 break;
4611 case RID_AT_SYNCHRONIZED:
4612 gcc_assert (c_dialect_objc ());
4613 c_parser_objc_synchronized_statement (parser);
4614 break;
4615 case RID_UPC_FORALL:
4616 gcc_assert (c_dialect_upc ());
4617 c_parser_upc_forall_statement (parser);
4618 break;
4619 case RID_UPC_NOTIFY:
4620 gcc_assert (c_dialect_upc ());
4621 c_parser_upc_sync_statement (parser, UPC_SYNC_NOTIFY_OP);
4622 goto expect_semicolon;
4623 case RID_UPC_WAIT:
4624 gcc_assert (c_dialect_upc ());
4625 c_parser_upc_sync_statement (parser, UPC_SYNC_WAIT_OP);
4626 goto expect_semicolon;
4627 case RID_UPC_BARRIER:
4628 gcc_assert (c_dialect_upc ());
4629 c_parser_upc_sync_statement (parser, UPC_SYNC_BARRIER_OP);
4630 goto expect_semicolon;
4631 default:
4632 goto expr_stmt;
4634 break;
4635 case CPP_SEMICOLON:
4636 c_parser_consume_token (parser);
4637 break;
4638 case CPP_CLOSE_PAREN:
4639 case CPP_CLOSE_SQUARE:
4640 /* Avoid infinite loop in error recovery:
4641 c_parser_skip_until_found stops at a closing nesting
4642 delimiter without consuming it, but here we need to consume
4643 it to proceed further. */
4644 c_parser_error (parser, "expected statement");
4645 c_parser_consume_token (parser);
4646 break;
4647 case CPP_PRAGMA:
4648 c_parser_pragma (parser, pragma_stmt);
4649 break;
4650 default:
4651 expr_stmt:
4652 stmt = c_finish_expr_stmt (loc, c_parser_expression_conv (parser).value);
4653 expect_semicolon:
4654 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
4655 break;
4657 /* Two cases cannot and do not have line numbers associated: If stmt
4658 is degenerate, such as "2;", then stmt is an INTEGER_CST, which
4659 cannot hold line numbers. But that's OK because the statement
4660 will either be changed to a MODIFY_EXPR during gimplification of
4661 the statement expr, or discarded. If stmt was compound, but
4662 without new variables, we will have skipped the creation of a
4663 BIND and will have a bare STATEMENT_LIST. But that's OK because
4664 (recursively) all of the component statements should already have
4665 line numbers assigned. ??? Can we discard no-op statements
4666 earlier? */
4667 if (CAN_HAVE_LOCATION_P (stmt)
4668 && EXPR_LOCATION (stmt) == UNKNOWN_LOCATION)
4669 SET_EXPR_LOCATION (stmt, loc);
4671 parser->in_if_block = in_if_block;
4674 /* Parse the condition from an if, do, while or for statements. */
4676 static tree
4677 c_parser_condition (c_parser *parser)
4679 location_t loc ATTRIBUTE_UNUSED = c_parser_peek_token (parser)->location;
4680 tree cond;
4681 cond = c_parser_expression_conv (parser).value;
4682 cond = c_objc_common_truthvalue_conversion (loc, cond);
4683 cond = c_fully_fold (cond, false, NULL);
4684 if (warn_sequence_point)
4685 verify_sequence_points (cond);
4686 return cond;
4689 /* Parse a parenthesized condition from an if, do or while statement.
4691 condition:
4692 ( expression )
4694 static tree
4695 c_parser_paren_condition (c_parser *parser)
4697 tree cond;
4698 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4699 return error_mark_node;
4700 cond = c_parser_condition (parser);
4701 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
4702 return cond;
4705 /* Parse a statement which is a block in C99. */
4707 static tree
4708 c_parser_c99_block_statement (c_parser *parser)
4710 tree block = c_begin_compound_stmt (flag_isoc99);
4711 location_t loc = c_parser_peek_token (parser)->location;
4712 c_parser_statement (parser);
4713 return c_end_compound_stmt (loc, block, flag_isoc99);
4716 /* Parse the body of an if statement. This is just parsing a
4717 statement but (a) it is a block in C99, (b) we track whether the
4718 body is an if statement for the sake of -Wparentheses warnings, (c)
4719 we handle an empty body specially for the sake of -Wempty-body
4720 warnings, and (d) we call parser_compound_statement directly
4721 because c_parser_statement_after_labels resets
4722 parser->in_if_block. */
4724 static tree
4725 c_parser_if_body (c_parser *parser, bool *if_p)
4727 tree block = c_begin_compound_stmt (flag_isoc99);
4728 location_t body_loc = c_parser_peek_token (parser)->location;
4729 while (c_parser_next_token_is_keyword (parser, RID_CASE)
4730 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
4731 || (c_parser_next_token_is (parser, CPP_NAME)
4732 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
4733 c_parser_label (parser);
4734 *if_p = c_parser_next_token_is_keyword (parser, RID_IF);
4735 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4737 location_t loc = c_parser_peek_token (parser)->location;
4738 add_stmt (build_empty_stmt (loc));
4739 c_parser_consume_token (parser);
4740 if (!c_parser_next_token_is_keyword (parser, RID_ELSE))
4741 warning_at (loc, OPT_Wempty_body,
4742 "suggest braces around empty body in an %<if%> statement");
4744 else if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
4745 add_stmt (c_parser_compound_statement (parser));
4746 else
4747 c_parser_statement_after_labels (parser);
4748 return c_end_compound_stmt (body_loc, block, flag_isoc99);
4751 /* Parse the else body of an if statement. This is just parsing a
4752 statement but (a) it is a block in C99, (b) we handle an empty body
4753 specially for the sake of -Wempty-body warnings. */
4755 static tree
4756 c_parser_else_body (c_parser *parser)
4758 location_t else_loc = c_parser_peek_token (parser)->location;
4759 tree block = c_begin_compound_stmt (flag_isoc99);
4760 while (c_parser_next_token_is_keyword (parser, RID_CASE)
4761 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
4762 || (c_parser_next_token_is (parser, CPP_NAME)
4763 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
4764 c_parser_label (parser);
4765 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4767 location_t loc = c_parser_peek_token (parser)->location;
4768 warning_at (loc,
4769 OPT_Wempty_body,
4770 "suggest braces around empty body in an %<else%> statement");
4771 add_stmt (build_empty_stmt (loc));
4772 c_parser_consume_token (parser);
4774 else
4775 c_parser_statement_after_labels (parser);
4776 return c_end_compound_stmt (else_loc, block, flag_isoc99);
4779 /* Parse an if statement (C90 6.6.4, C99 6.8.4).
4781 if-statement:
4782 if ( expression ) statement
4783 if ( expression ) statement else statement
4786 static void
4787 c_parser_if_statement (c_parser *parser)
4789 tree block;
4790 location_t loc;
4791 tree cond;
4792 bool first_if = false;
4793 tree first_body, second_body;
4794 bool in_if_block;
4796 gcc_assert (c_parser_next_token_is_keyword (parser, RID_IF));
4797 c_parser_consume_token (parser);
4798 block = c_begin_compound_stmt (flag_isoc99);
4799 loc = c_parser_peek_token (parser)->location;
4800 cond = c_parser_paren_condition (parser);
4801 in_if_block = parser->in_if_block;
4802 parser->in_if_block = true;
4803 first_body = c_parser_if_body (parser, &first_if);
4804 parser->in_if_block = in_if_block;
4805 if (c_parser_next_token_is_keyword (parser, RID_ELSE))
4807 c_parser_consume_token (parser);
4808 second_body = c_parser_else_body (parser);
4810 else
4811 second_body = NULL_TREE;
4812 c_finish_if_stmt (loc, cond, first_body, second_body, first_if);
4813 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
4816 /* Parse a switch statement (C90 6.6.4, C99 6.8.4).
4818 switch-statement:
4819 switch (expression) statement
4822 static void
4823 c_parser_switch_statement (c_parser *parser)
4825 tree block, expr, body, save_break;
4826 location_t switch_loc = c_parser_peek_token (parser)->location;
4827 location_t switch_cond_loc;
4828 gcc_assert (c_parser_next_token_is_keyword (parser, RID_SWITCH));
4829 c_parser_consume_token (parser);
4830 block = c_begin_compound_stmt (flag_isoc99);
4831 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4833 switch_cond_loc = c_parser_peek_token (parser)->location;
4834 expr = c_parser_expression (parser).value;
4835 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
4837 else
4839 switch_cond_loc = UNKNOWN_LOCATION;
4840 expr = error_mark_node;
4842 c_start_case (switch_loc, switch_cond_loc, expr);
4843 save_break = c_break_label;
4844 c_break_label = NULL_TREE;
4845 body = c_parser_c99_block_statement (parser);
4846 c_finish_case (body);
4847 if (c_break_label)
4849 location_t here = c_parser_peek_token (parser)->location;
4850 tree t = build1 (LABEL_EXPR, void_type_node, c_break_label);
4851 SET_EXPR_LOCATION (t, here);
4852 add_stmt (t);
4854 c_break_label = save_break;
4855 add_stmt (c_end_compound_stmt (switch_loc, block, flag_isoc99));
4858 /* Parse a while statement (C90 6.6.5, C99 6.8.5).
4860 while-statement:
4861 while (expression) statement
4864 static void
4865 c_parser_while_statement (c_parser *parser)
4867 tree block, cond, body, save_break, save_cont;
4868 location_t loc;
4869 gcc_assert (c_parser_next_token_is_keyword (parser, RID_WHILE));
4870 c_parser_consume_token (parser);
4871 block = c_begin_compound_stmt (flag_isoc99);
4872 loc = c_parser_peek_token (parser)->location;
4873 cond = c_parser_paren_condition (parser);
4874 save_break = c_break_label;
4875 c_break_label = NULL_TREE;
4876 save_cont = c_cont_label;
4877 c_cont_label = NULL_TREE;
4878 body = c_parser_c99_block_statement (parser);
4879 c_finish_loop (loc, cond, NULL, body, c_break_label, c_cont_label, true);
4880 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
4881 c_break_label = save_break;
4882 c_cont_label = save_cont;
4885 /* Parse a do statement (C90 6.6.5, C99 6.8.5).
4887 do-statement:
4888 do statement while ( expression ) ;
4891 static void
4892 c_parser_do_statement (c_parser *parser)
4894 tree block, cond, body, save_break, save_cont, new_break, new_cont;
4895 location_t loc;
4896 gcc_assert (c_parser_next_token_is_keyword (parser, RID_DO));
4897 c_parser_consume_token (parser);
4898 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4899 warning_at (c_parser_peek_token (parser)->location,
4900 OPT_Wempty_body,
4901 "suggest braces around empty body in %<do%> statement");
4902 block = c_begin_compound_stmt (flag_isoc99);
4903 loc = c_parser_peek_token (parser)->location;
4904 save_break = c_break_label;
4905 c_break_label = NULL_TREE;
4906 save_cont = c_cont_label;
4907 c_cont_label = NULL_TREE;
4908 body = c_parser_c99_block_statement (parser);
4909 c_parser_require_keyword (parser, RID_WHILE, "expected %<while%>");
4910 new_break = c_break_label;
4911 c_break_label = save_break;
4912 new_cont = c_cont_label;
4913 c_cont_label = save_cont;
4914 cond = c_parser_paren_condition (parser);
4915 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
4916 c_parser_skip_to_end_of_block_or_statement (parser);
4917 c_finish_loop (loc, cond, NULL, body, new_break, new_cont, false);
4918 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
4921 /* Parse a for statement (C90 6.6.5, C99 6.8.5).
4923 for-statement:
4924 for ( expression[opt] ; expression[opt] ; expression[opt] ) statement
4925 for ( nested-declaration expression[opt] ; expression[opt] ) statement
4927 The form with a declaration is new in C99.
4929 ??? In accordance with the old parser, the declaration may be a
4930 nested function, which is then rejected in check_for_loop_decls,
4931 but does it make any sense for this to be included in the grammar?
4932 Note in particular that the nested function does not include a
4933 trailing ';', whereas the "declaration" production includes one.
4934 Also, can we reject bad declarations earlier and cheaper than
4935 check_for_loop_decls?
4937 In Objective-C, there are two additional variants:
4939 foreach-statement:
4940 for ( expression in expresssion ) statement
4941 for ( declaration in expression ) statement
4943 This is inconsistent with C, because the second variant is allowed
4944 even if c99 is not enabled.
4946 The rest of the comment documents these Objective-C foreach-statement.
4948 Here is the canonical example of the first variant:
4949 for (object in array) { do something with object }
4950 we call the first expression ("object") the "object_expression" and
4951 the second expression ("array") the "collection_expression".
4952 object_expression must be an lvalue of type "id" (a generic Objective-C
4953 object) because the loop works by assigning to object_expression the
4954 various objects from the collection_expression. collection_expression
4955 must evaluate to something of type "id" which responds to the method
4956 countByEnumeratingWithState:objects:count:.
4958 The canonical example of the second variant is:
4959 for (id object in array) { do something with object }
4960 which is completely equivalent to
4962 id object;
4963 for (object in array) { do something with object }
4965 Note that initizializing 'object' in some way (eg, "for ((object =
4966 xxx) in array) { do something with object }") is possibly
4967 technically valid, but completely pointless as 'object' will be
4968 assigned to something else as soon as the loop starts. We should
4969 most likely reject it (TODO).
4971 The beginning of the Objective-C foreach-statement looks exactly
4972 like the beginning of the for-statement, and we can tell it is a
4973 foreach-statement only because the initial declaration or
4974 expression is terminated by 'in' instead of ';'.
4977 static void
4978 c_parser_for_statement (c_parser *parser)
4980 tree block, cond, incr, save_break, save_cont, body;
4981 /* The following are only used when parsing an ObjC foreach statement. */
4982 tree object_expression;
4983 /* Silence the bogus uninitialized warning. */
4984 tree collection_expression = NULL;
4985 location_t loc = c_parser_peek_token (parser)->location;
4986 location_t for_loc = c_parser_peek_token (parser)->location;
4987 bool is_foreach_statement = false;
4988 gcc_assert (c_parser_next_token_is_keyword (parser, RID_FOR));
4989 c_parser_consume_token (parser);
4990 /* Open a compound statement in Objective-C as well, just in case this is
4991 as foreach expression. */
4992 block = c_begin_compound_stmt (flag_isoc99 || c_dialect_objc ());
4993 cond = error_mark_node;
4994 incr = error_mark_node;
4995 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4997 /* Parse the initialization declaration or expression. */
4998 object_expression = error_mark_node;
4999 parser->objc_could_be_foreach_context = c_dialect_objc ();
5000 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5002 parser->objc_could_be_foreach_context = false;
5003 c_parser_consume_token (parser);
5004 c_finish_expr_stmt (loc, NULL_TREE);
5006 else if (c_parser_next_tokens_start_declaration (parser))
5008 c_parser_declaration_or_fndef (parser, true, true, true, true, true,
5009 &object_expression);
5010 parser->objc_could_be_foreach_context = false;
5012 if (c_parser_next_token_is_keyword (parser, RID_IN))
5014 c_parser_consume_token (parser);
5015 is_foreach_statement = true;
5016 if (check_for_loop_decls (for_loc, true) == NULL_TREE)
5017 c_parser_error (parser, "multiple iterating variables in fast enumeration");
5019 else
5020 check_for_loop_decls (for_loc, flag_isoc99);
5022 else if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
5024 /* __extension__ can start a declaration, but is also an
5025 unary operator that can start an expression. Consume all
5026 but the last of a possible series of __extension__ to
5027 determine which. */
5028 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
5029 && (c_parser_peek_2nd_token (parser)->keyword
5030 == RID_EXTENSION))
5031 c_parser_consume_token (parser);
5032 if (c_token_starts_declaration (c_parser_peek_2nd_token (parser)))
5034 int ext;
5035 ext = disable_extension_diagnostics ();
5036 c_parser_consume_token (parser);
5037 c_parser_declaration_or_fndef (parser, true, true, true, true,
5038 true, &object_expression);
5039 parser->objc_could_be_foreach_context = false;
5041 restore_extension_diagnostics (ext);
5042 if (c_parser_next_token_is_keyword (parser, RID_IN))
5044 c_parser_consume_token (parser);
5045 is_foreach_statement = true;
5046 if (check_for_loop_decls (for_loc, true) == NULL_TREE)
5047 c_parser_error (parser, "multiple iterating variables in fast enumeration");
5049 else
5050 check_for_loop_decls (for_loc, flag_isoc99);
5052 else
5053 goto init_expr;
5055 else
5057 init_expr:
5059 tree init_expression;
5060 init_expression = c_parser_expression (parser).value;
5061 parser->objc_could_be_foreach_context = false;
5062 if (c_parser_next_token_is_keyword (parser, RID_IN))
5064 c_parser_consume_token (parser);
5065 is_foreach_statement = true;
5066 if (! lvalue_p (init_expression))
5067 c_parser_error (parser, "invalid iterating variable in fast enumeration");
5068 object_expression = c_fully_fold (init_expression, false, NULL);
5070 else
5072 c_finish_expr_stmt (loc, init_expression);
5073 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
5077 /* Parse the loop condition. In the case of a foreach
5078 statement, there is no loop condition. */
5079 gcc_assert (!parser->objc_could_be_foreach_context);
5080 if (!is_foreach_statement)
5082 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5084 c_parser_consume_token (parser);
5085 cond = NULL_TREE;
5087 else
5089 cond = c_parser_condition (parser);
5090 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
5093 /* Parse the increment expression (the third expression in a
5094 for-statement). In the case of a foreach-statement, this is
5095 the expression that follows the 'in'. */
5096 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
5098 if (is_foreach_statement)
5100 c_parser_error (parser, "missing collection in fast enumeration");
5101 collection_expression = error_mark_node;
5103 else
5104 incr = c_process_expr_stmt (loc, NULL_TREE);
5106 else
5108 if (is_foreach_statement)
5109 collection_expression = c_fully_fold (c_parser_expression (parser).value,
5110 false, NULL);
5111 else
5112 incr = c_process_expr_stmt (loc, c_parser_expression (parser).value);
5114 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5116 save_break = c_break_label;
5117 c_break_label = NULL_TREE;
5118 save_cont = c_cont_label;
5119 c_cont_label = NULL_TREE;
5120 body = c_parser_c99_block_statement (parser);
5121 if (is_foreach_statement)
5122 objc_finish_foreach_loop (loc, object_expression, collection_expression, body, c_break_label, c_cont_label);
5123 else
5124 c_finish_loop (loc, cond, incr, body, c_break_label, c_cont_label, true);
5125 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99 || c_dialect_objc ()));
5126 c_break_label = save_break;
5127 c_cont_label = save_cont;
5130 /* Parse an asm statement, a GNU extension. This is a full-blown asm
5131 statement with inputs, outputs, clobbers, and volatile tag
5132 allowed.
5134 asm-statement:
5135 asm type-qualifier[opt] ( asm-argument ) ;
5136 asm type-qualifier[opt] goto ( asm-goto-argument ) ;
5138 asm-argument:
5139 asm-string-literal
5140 asm-string-literal : asm-operands[opt]
5141 asm-string-literal : asm-operands[opt] : asm-operands[opt]
5142 asm-string-literal : asm-operands[opt] : asm-operands[opt] : asm-clobbers[opt]
5144 asm-goto-argument:
5145 asm-string-literal : : asm-operands[opt] : asm-clobbers[opt] \
5146 : asm-goto-operands
5148 Qualifiers other than volatile are accepted in the syntax but
5149 warned for. */
5151 static tree
5152 c_parser_asm_statement (c_parser *parser)
5154 tree quals, str, outputs, inputs, clobbers, labels, ret;
5155 bool simple, is_goto;
5156 location_t asm_loc = c_parser_peek_token (parser)->location;
5157 int section, nsections;
5159 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
5160 c_parser_consume_token (parser);
5161 if (c_parser_next_token_is_keyword (parser, RID_VOLATILE))
5163 quals = c_parser_peek_token (parser)->value;
5164 c_parser_consume_token (parser);
5166 else if (c_parser_next_token_is_keyword (parser, RID_CONST)
5167 || c_parser_next_token_is_keyword (parser, RID_RESTRICT))
5169 warning_at (c_parser_peek_token (parser)->location,
5171 "%E qualifier ignored on asm",
5172 c_parser_peek_token (parser)->value);
5173 quals = NULL_TREE;
5174 c_parser_consume_token (parser);
5176 else
5177 quals = NULL_TREE;
5179 is_goto = false;
5180 if (c_parser_next_token_is_keyword (parser, RID_GOTO))
5182 c_parser_consume_token (parser);
5183 is_goto = true;
5186 /* ??? Follow the C++ parser rather than using the
5187 lex_untranslated_string kludge. */
5188 parser->lex_untranslated_string = true;
5189 ret = NULL;
5191 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5192 goto error;
5194 str = c_parser_asm_string_literal (parser);
5195 if (str == NULL_TREE)
5196 goto error_close_paren;
5198 simple = true;
5199 outputs = NULL_TREE;
5200 inputs = NULL_TREE;
5201 clobbers = NULL_TREE;
5202 labels = NULL_TREE;
5204 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN) && !is_goto)
5205 goto done_asm;
5207 /* Parse each colon-delimited section of operands. */
5208 nsections = 3 + is_goto;
5209 for (section = 0; section < nsections; ++section)
5211 if (!c_parser_require (parser, CPP_COLON,
5212 is_goto
5213 ? "expected %<:%>"
5214 : "expected %<:%> or %<)%>"))
5215 goto error_close_paren;
5217 /* Once past any colon, we're no longer a simple asm. */
5218 simple = false;
5220 if ((!c_parser_next_token_is (parser, CPP_COLON)
5221 && !c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
5222 || section == 3)
5223 switch (section)
5225 case 0:
5226 /* For asm goto, we don't allow output operands, but reserve
5227 the slot for a future extension that does allow them. */
5228 if (!is_goto)
5229 outputs = c_parser_asm_operands (parser, false);
5230 break;
5231 case 1:
5232 inputs = c_parser_asm_operands (parser, true);
5233 break;
5234 case 2:
5235 clobbers = c_parser_asm_clobbers (parser);
5236 break;
5237 case 3:
5238 labels = c_parser_asm_goto_operands (parser);
5239 break;
5240 default:
5241 gcc_unreachable ();
5244 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN) && !is_goto)
5245 goto done_asm;
5248 done_asm:
5249 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
5251 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5252 goto error;
5255 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
5256 c_parser_skip_to_end_of_block_or_statement (parser);
5258 ret = build_asm_stmt (quals, build_asm_expr (asm_loc, str, outputs, inputs,
5259 clobbers, labels, simple));
5261 error:
5262 parser->lex_untranslated_string = false;
5263 return ret;
5265 error_close_paren:
5266 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5267 goto error;
5270 /* Parse asm operands, a GNU extension. If CONVERT_P (for inputs but
5271 not outputs), apply the default conversion of functions and arrays
5272 to pointers.
5274 asm-operands:
5275 asm-operand
5276 asm-operands , asm-operand
5278 asm-operand:
5279 asm-string-literal ( expression )
5280 [ identifier ] asm-string-literal ( expression )
5283 static tree
5284 c_parser_asm_operands (c_parser *parser, bool convert_p)
5286 tree list = NULL_TREE;
5287 location_t loc;
5288 while (true)
5290 tree name, str;
5291 struct c_expr expr;
5292 if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
5294 c_parser_consume_token (parser);
5295 if (c_parser_next_token_is (parser, CPP_NAME))
5297 tree id = c_parser_peek_token (parser)->value;
5298 c_parser_consume_token (parser);
5299 name = build_string (IDENTIFIER_LENGTH (id),
5300 IDENTIFIER_POINTER (id));
5302 else
5304 c_parser_error (parser, "expected identifier");
5305 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
5306 return NULL_TREE;
5308 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
5309 "expected %<]%>");
5311 else
5312 name = NULL_TREE;
5313 str = c_parser_asm_string_literal (parser);
5314 if (str == NULL_TREE)
5315 return NULL_TREE;
5316 parser->lex_untranslated_string = false;
5317 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5319 parser->lex_untranslated_string = true;
5320 return NULL_TREE;
5322 loc = c_parser_peek_token (parser)->location;
5323 expr = c_parser_expression (parser);
5324 mark_exp_read (expr.value);
5325 if (convert_p)
5326 expr = default_function_array_conversion (loc, expr);
5327 expr.value = c_fully_fold (expr.value, false, NULL);
5328 parser->lex_untranslated_string = true;
5329 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
5331 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5332 return NULL_TREE;
5334 list = chainon (list, build_tree_list (build_tree_list (name, str),
5335 expr.value));
5336 if (c_parser_next_token_is (parser, CPP_COMMA))
5337 c_parser_consume_token (parser);
5338 else
5339 break;
5341 return list;
5344 /* Parse asm clobbers, a GNU extension.
5346 asm-clobbers:
5347 asm-string-literal
5348 asm-clobbers , asm-string-literal
5351 static tree
5352 c_parser_asm_clobbers (c_parser *parser)
5354 tree list = NULL_TREE;
5355 while (true)
5357 tree str = c_parser_asm_string_literal (parser);
5358 if (str)
5359 list = tree_cons (NULL_TREE, str, list);
5360 else
5361 return NULL_TREE;
5362 if (c_parser_next_token_is (parser, CPP_COMMA))
5363 c_parser_consume_token (parser);
5364 else
5365 break;
5367 return list;
5370 /* Parse asm goto labels, a GNU extension.
5372 asm-goto-operands:
5373 identifier
5374 asm-goto-operands , identifier
5377 static tree
5378 c_parser_asm_goto_operands (c_parser *parser)
5380 tree list = NULL_TREE;
5381 while (true)
5383 tree name, label;
5385 if (c_parser_next_token_is (parser, CPP_NAME))
5387 c_token *tok = c_parser_peek_token (parser);
5388 name = tok->value;
5389 label = lookup_label_for_goto (tok->location, name);
5390 c_parser_consume_token (parser);
5391 TREE_USED (label) = 1;
5393 else
5395 c_parser_error (parser, "expected identifier");
5396 return NULL_TREE;
5399 name = build_string (IDENTIFIER_LENGTH (name),
5400 IDENTIFIER_POINTER (name));
5401 list = tree_cons (name, label, list);
5402 if (c_parser_next_token_is (parser, CPP_COMMA))
5403 c_parser_consume_token (parser);
5404 else
5405 return nreverse (list);
5409 /* Parse an expression other than a compound expression; that is, an
5410 assignment expression (C90 6.3.16, C99 6.5.16). If AFTER is not
5411 NULL then it is an Objective-C message expression which is the
5412 primary-expression starting the expression as an initializer.
5414 assignment-expression:
5415 conditional-expression
5416 unary-expression assignment-operator assignment-expression
5418 assignment-operator: one of
5419 = *= /= %= += -= <<= >>= &= ^= |=
5421 In GNU C we accept any conditional expression on the LHS and
5422 diagnose the invalid lvalue rather than producing a syntax
5423 error. */
5425 static struct c_expr
5426 c_parser_expr_no_commas (c_parser *parser, struct c_expr *after)
5428 struct c_expr lhs, rhs, ret;
5429 enum tree_code code;
5430 location_t op_location, exp_location;
5431 gcc_assert (!after || c_dialect_objc ());
5432 lhs = c_parser_conditional_expression (parser, after);
5433 op_location = c_parser_peek_token (parser)->location;
5434 switch (c_parser_peek_token (parser)->type)
5436 case CPP_EQ:
5437 code = NOP_EXPR;
5438 break;
5439 case CPP_MULT_EQ:
5440 code = MULT_EXPR;
5441 break;
5442 case CPP_DIV_EQ:
5443 code = TRUNC_DIV_EXPR;
5444 break;
5445 case CPP_MOD_EQ:
5446 code = TRUNC_MOD_EXPR;
5447 break;
5448 case CPP_PLUS_EQ:
5449 code = PLUS_EXPR;
5450 break;
5451 case CPP_MINUS_EQ:
5452 code = MINUS_EXPR;
5453 break;
5454 case CPP_LSHIFT_EQ:
5455 code = LSHIFT_EXPR;
5456 break;
5457 case CPP_RSHIFT_EQ:
5458 code = RSHIFT_EXPR;
5459 break;
5460 case CPP_AND_EQ:
5461 code = BIT_AND_EXPR;
5462 break;
5463 case CPP_XOR_EQ:
5464 code = BIT_XOR_EXPR;
5465 break;
5466 case CPP_OR_EQ:
5467 code = BIT_IOR_EXPR;
5468 break;
5469 default:
5470 return lhs;
5472 c_parser_consume_token (parser);
5473 exp_location = c_parser_peek_token (parser)->location;
5474 rhs = c_parser_expr_no_commas (parser, NULL);
5475 rhs = default_function_array_read_conversion (exp_location, rhs);
5476 ret.value = build_modify_expr (op_location, lhs.value, lhs.original_type,
5477 code, exp_location, rhs.value,
5478 rhs.original_type);
5479 if (code == NOP_EXPR)
5480 ret.original_code = MODIFY_EXPR;
5481 else
5483 TREE_NO_WARNING (ret.value) = 1;
5484 ret.original_code = ERROR_MARK;
5486 ret.original_type = NULL;
5487 return ret;
5490 /* Parse a conditional expression (C90 6.3.15, C99 6.5.15). If AFTER
5491 is not NULL then it is an Objective-C message expression which is
5492 the primary-expression starting the expression as an initializer.
5494 conditional-expression:
5495 logical-OR-expression
5496 logical-OR-expression ? expression : conditional-expression
5498 GNU extensions:
5500 conditional-expression:
5501 logical-OR-expression ? : conditional-expression
5504 static struct c_expr
5505 c_parser_conditional_expression (c_parser *parser, struct c_expr *after)
5507 struct c_expr cond, exp1, exp2, ret;
5508 location_t cond_loc, colon_loc, middle_loc;
5510 gcc_assert (!after || c_dialect_objc ());
5512 cond = c_parser_binary_expression (parser, after, PREC_NONE);
5514 if (c_parser_next_token_is_not (parser, CPP_QUERY))
5515 return cond;
5516 cond_loc = c_parser_peek_token (parser)->location;
5517 cond = default_function_array_read_conversion (cond_loc, cond);
5518 c_parser_consume_token (parser);
5519 if (c_parser_next_token_is (parser, CPP_COLON))
5521 tree eptype = NULL_TREE;
5523 middle_loc = c_parser_peek_token (parser)->location;
5524 pedwarn (middle_loc, OPT_Wpedantic,
5525 "ISO C forbids omitting the middle term of a ?: expression");
5526 warn_for_omitted_condop (middle_loc, cond.value);
5527 if (TREE_CODE (cond.value) == EXCESS_PRECISION_EXPR)
5529 eptype = TREE_TYPE (cond.value);
5530 cond.value = TREE_OPERAND (cond.value, 0);
5532 /* Make sure first operand is calculated only once. */
5533 exp1.value = c_save_expr (default_conversion (cond.value));
5534 if (eptype)
5535 exp1.value = build1 (EXCESS_PRECISION_EXPR, eptype, exp1.value);
5536 exp1.original_type = NULL;
5537 cond.value = c_objc_common_truthvalue_conversion (cond_loc, exp1.value);
5538 c_inhibit_evaluation_warnings += cond.value == truthvalue_true_node;
5540 else
5542 cond.value
5543 = c_objc_common_truthvalue_conversion
5544 (cond_loc, default_conversion (cond.value));
5545 c_inhibit_evaluation_warnings += cond.value == truthvalue_false_node;
5546 exp1 = c_parser_expression_conv (parser);
5547 mark_exp_read (exp1.value);
5548 c_inhibit_evaluation_warnings +=
5549 ((cond.value == truthvalue_true_node)
5550 - (cond.value == truthvalue_false_node));
5553 colon_loc = c_parser_peek_token (parser)->location;
5554 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
5556 c_inhibit_evaluation_warnings -= cond.value == truthvalue_true_node;
5557 ret.value = error_mark_node;
5558 ret.original_code = ERROR_MARK;
5559 ret.original_type = NULL;
5560 return ret;
5563 location_t exp2_loc = c_parser_peek_token (parser)->location;
5564 exp2 = c_parser_conditional_expression (parser, NULL);
5565 exp2 = default_function_array_read_conversion (exp2_loc, exp2);
5567 c_inhibit_evaluation_warnings -= cond.value == truthvalue_true_node;
5568 ret.value = build_conditional_expr (colon_loc, cond.value,
5569 cond.original_code == C_MAYBE_CONST_EXPR,
5570 exp1.value, exp1.original_type,
5571 exp2.value, exp2.original_type);
5572 ret.original_code = ERROR_MARK;
5573 if (exp1.value == error_mark_node || exp2.value == error_mark_node)
5574 ret.original_type = NULL;
5575 else
5577 tree t1, t2;
5579 /* If both sides are enum type, the default conversion will have
5580 made the type of the result be an integer type. We want to
5581 remember the enum types we started with. */
5582 t1 = exp1.original_type ? exp1.original_type : TREE_TYPE (exp1.value);
5583 t2 = exp2.original_type ? exp2.original_type : TREE_TYPE (exp2.value);
5584 ret.original_type = ((t1 != error_mark_node
5585 && t2 != error_mark_node
5586 && (TYPE_MAIN_VARIANT (t1)
5587 == TYPE_MAIN_VARIANT (t2)))
5588 ? t1
5589 : NULL);
5591 return ret;
5594 /* Parse a binary expression; that is, a logical-OR-expression (C90
5595 6.3.5-6.3.14, C99 6.5.5-6.5.14). If AFTER is not NULL then it is
5596 an Objective-C message expression which is the primary-expression
5597 starting the expression as an initializer. PREC is the starting
5598 precedence, usually PREC_NONE.
5600 multiplicative-expression:
5601 cast-expression
5602 multiplicative-expression * cast-expression
5603 multiplicative-expression / cast-expression
5604 multiplicative-expression % cast-expression
5606 additive-expression:
5607 multiplicative-expression
5608 additive-expression + multiplicative-expression
5609 additive-expression - multiplicative-expression
5611 shift-expression:
5612 additive-expression
5613 shift-expression << additive-expression
5614 shift-expression >> additive-expression
5616 relational-expression:
5617 shift-expression
5618 relational-expression < shift-expression
5619 relational-expression > shift-expression
5620 relational-expression <= shift-expression
5621 relational-expression >= shift-expression
5623 equality-expression:
5624 relational-expression
5625 equality-expression == relational-expression
5626 equality-expression != relational-expression
5628 AND-expression:
5629 equality-expression
5630 AND-expression & equality-expression
5632 exclusive-OR-expression:
5633 AND-expression
5634 exclusive-OR-expression ^ AND-expression
5636 inclusive-OR-expression:
5637 exclusive-OR-expression
5638 inclusive-OR-expression | exclusive-OR-expression
5640 logical-AND-expression:
5641 inclusive-OR-expression
5642 logical-AND-expression && inclusive-OR-expression
5644 logical-OR-expression:
5645 logical-AND-expression
5646 logical-OR-expression || logical-AND-expression
5649 static struct c_expr
5650 c_parser_binary_expression (c_parser *parser, struct c_expr *after,
5651 enum c_parser_prec prec)
5653 /* A binary expression is parsed using operator-precedence parsing,
5654 with the operands being cast expressions. All the binary
5655 operators are left-associative. Thus a binary expression is of
5656 form:
5658 E0 op1 E1 op2 E2 ...
5660 which we represent on a stack. On the stack, the precedence
5661 levels are strictly increasing. When a new operator is
5662 encountered of higher precedence than that at the top of the
5663 stack, it is pushed; its LHS is the top expression, and its RHS
5664 is everything parsed until it is popped. When a new operator is
5665 encountered with precedence less than or equal to that at the top
5666 of the stack, triples E[i-1] op[i] E[i] are popped and replaced
5667 by the result of the operation until the operator at the top of
5668 the stack has lower precedence than the new operator or there is
5669 only one element on the stack; then the top expression is the LHS
5670 of the new operator. In the case of logical AND and OR
5671 expressions, we also need to adjust c_inhibit_evaluation_warnings
5672 as appropriate when the operators are pushed and popped. */
5674 struct {
5675 /* The expression at this stack level. */
5676 struct c_expr expr;
5677 /* The precedence of the operator on its left, PREC_NONE at the
5678 bottom of the stack. */
5679 enum c_parser_prec prec;
5680 /* The operation on its left. */
5681 enum tree_code op;
5682 /* The source location of this operation. */
5683 location_t loc;
5684 } stack[NUM_PRECS];
5685 int sp;
5686 /* Location of the binary operator. */
5687 location_t binary_loc = UNKNOWN_LOCATION; /* Quiet warning. */
5688 #define POP \
5689 do { \
5690 switch (stack[sp].op) \
5692 case TRUTH_ANDIF_EXPR: \
5693 c_inhibit_evaluation_warnings -= (stack[sp - 1].expr.value \
5694 == truthvalue_false_node); \
5695 break; \
5696 case TRUTH_ORIF_EXPR: \
5697 c_inhibit_evaluation_warnings -= (stack[sp - 1].expr.value \
5698 == truthvalue_true_node); \
5699 break; \
5700 default: \
5701 break; \
5703 stack[sp - 1].expr \
5704 = default_function_array_read_conversion (stack[sp - 1].loc, \
5705 stack[sp - 1].expr); \
5706 stack[sp].expr \
5707 = default_function_array_read_conversion (stack[sp].loc, \
5708 stack[sp].expr); \
5709 stack[sp - 1].expr = parser_build_binary_op (stack[sp].loc, \
5710 stack[sp].op, \
5711 stack[sp - 1].expr, \
5712 stack[sp].expr); \
5713 sp--; \
5714 } while (0)
5715 gcc_assert (!after || c_dialect_objc ());
5716 stack[0].loc = c_parser_peek_token (parser)->location;
5717 stack[0].expr = c_parser_cast_expression (parser, after);
5718 stack[0].prec = prec;
5719 sp = 0;
5720 while (true)
5722 enum c_parser_prec oprec;
5723 enum tree_code ocode;
5724 if (parser->error)
5725 goto out;
5726 switch (c_parser_peek_token (parser)->type)
5728 case CPP_MULT:
5729 oprec = PREC_MULT;
5730 ocode = MULT_EXPR;
5731 break;
5732 case CPP_DIV:
5733 oprec = PREC_MULT;
5734 ocode = TRUNC_DIV_EXPR;
5735 break;
5736 case CPP_MOD:
5737 oprec = PREC_MULT;
5738 ocode = TRUNC_MOD_EXPR;
5739 break;
5740 case CPP_PLUS:
5741 oprec = PREC_ADD;
5742 ocode = PLUS_EXPR;
5743 break;
5744 case CPP_MINUS:
5745 oprec = PREC_ADD;
5746 ocode = MINUS_EXPR;
5747 break;
5748 case CPP_LSHIFT:
5749 oprec = PREC_SHIFT;
5750 ocode = LSHIFT_EXPR;
5751 break;
5752 case CPP_RSHIFT:
5753 oprec = PREC_SHIFT;
5754 ocode = RSHIFT_EXPR;
5755 break;
5756 case CPP_LESS:
5757 oprec = PREC_REL;
5758 ocode = LT_EXPR;
5759 break;
5760 case CPP_GREATER:
5761 oprec = PREC_REL;
5762 ocode = GT_EXPR;
5763 break;
5764 case CPP_LESS_EQ:
5765 oprec = PREC_REL;
5766 ocode = LE_EXPR;
5767 break;
5768 case CPP_GREATER_EQ:
5769 oprec = PREC_REL;
5770 ocode = GE_EXPR;
5771 break;
5772 case CPP_EQ_EQ:
5773 oprec = PREC_EQ;
5774 ocode = EQ_EXPR;
5775 break;
5776 case CPP_NOT_EQ:
5777 oprec = PREC_EQ;
5778 ocode = NE_EXPR;
5779 break;
5780 case CPP_AND:
5781 oprec = PREC_BITAND;
5782 ocode = BIT_AND_EXPR;
5783 break;
5784 case CPP_XOR:
5785 oprec = PREC_BITXOR;
5786 ocode = BIT_XOR_EXPR;
5787 break;
5788 case CPP_OR:
5789 oprec = PREC_BITOR;
5790 ocode = BIT_IOR_EXPR;
5791 break;
5792 case CPP_AND_AND:
5793 oprec = PREC_LOGAND;
5794 ocode = TRUTH_ANDIF_EXPR;
5795 break;
5796 case CPP_OR_OR:
5797 oprec = PREC_LOGOR;
5798 ocode = TRUTH_ORIF_EXPR;
5799 break;
5800 default:
5801 /* Not a binary operator, so end of the binary
5802 expression. */
5803 goto out;
5805 binary_loc = c_parser_peek_token (parser)->location;
5806 while (oprec <= stack[sp].prec)
5808 if (sp == 0)
5809 goto out;
5810 POP;
5812 c_parser_consume_token (parser);
5813 switch (ocode)
5815 case TRUTH_ANDIF_EXPR:
5816 stack[sp].expr
5817 = default_function_array_read_conversion (stack[sp].loc,
5818 stack[sp].expr);
5819 stack[sp].expr.value = c_objc_common_truthvalue_conversion
5820 (stack[sp].loc, default_conversion (stack[sp].expr.value));
5821 c_inhibit_evaluation_warnings += (stack[sp].expr.value
5822 == truthvalue_false_node);
5823 break;
5824 case TRUTH_ORIF_EXPR:
5825 stack[sp].expr
5826 = default_function_array_read_conversion (stack[sp].loc,
5827 stack[sp].expr);
5828 stack[sp].expr.value = c_objc_common_truthvalue_conversion
5829 (stack[sp].loc, default_conversion (stack[sp].expr.value));
5830 c_inhibit_evaluation_warnings += (stack[sp].expr.value
5831 == truthvalue_true_node);
5832 break;
5833 default:
5834 break;
5836 sp++;
5837 stack[sp].loc = binary_loc;
5838 stack[sp].expr = c_parser_cast_expression (parser, NULL);
5839 stack[sp].prec = oprec;
5840 stack[sp].op = ocode;
5841 stack[sp].loc = binary_loc;
5843 out:
5844 while (sp > 0)
5845 POP;
5846 return stack[0].expr;
5847 #undef POP
5850 /* Parse a cast expression (C90 6.3.4, C99 6.5.4). If AFTER is not
5851 NULL then it is an Objective-C message expression which is the
5852 primary-expression starting the expression as an initializer.
5854 cast-expression:
5855 unary-expression
5856 ( type-name ) unary-expression
5859 static struct c_expr
5860 c_parser_cast_expression (c_parser *parser, struct c_expr *after)
5862 location_t cast_loc = c_parser_peek_token (parser)->location;
5863 gcc_assert (!after || c_dialect_objc ());
5864 if (after)
5865 return c_parser_postfix_expression_after_primary (parser,
5866 cast_loc, *after);
5867 /* If the expression begins with a parenthesized type name, it may
5868 be either a cast or a compound literal; we need to see whether
5869 the next character is '{' to tell the difference. If not, it is
5870 an unary expression. Full detection of unknown typenames here
5871 would require a 3-token lookahead. */
5872 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
5873 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
5875 struct c_type_name *type_name;
5876 struct c_expr ret;
5877 struct c_expr expr;
5878 c_parser_consume_token (parser);
5879 type_name = c_parser_type_name (parser);
5880 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5881 if (type_name == NULL)
5883 ret.value = error_mark_node;
5884 ret.original_code = ERROR_MARK;
5885 ret.original_type = NULL;
5886 return ret;
5889 /* Save casted types in the function's used types hash table. */
5890 used_types_insert (type_name->specs->type);
5892 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
5893 return c_parser_postfix_expression_after_paren_type (parser, type_name,
5894 cast_loc);
5896 location_t expr_loc = c_parser_peek_token (parser)->location;
5897 expr = c_parser_cast_expression (parser, NULL);
5898 expr = default_function_array_read_conversion (expr_loc, expr);
5900 ret.value = c_cast_expr (cast_loc, type_name, expr.value);
5901 ret.original_code = ERROR_MARK;
5902 ret.original_type = NULL;
5903 return ret;
5905 else
5906 return c_parser_unary_expression (parser);
5909 /* Parse an unary expression (C90 6.3.3, C99 6.5.3).
5911 unary-expression:
5912 postfix-expression
5913 ++ unary-expression
5914 -- unary-expression
5915 unary-operator cast-expression
5916 sizeof unary-expression
5917 sizeof ( type-name )
5919 unary-operator: one of
5920 & * + - ~ !
5922 GNU extensions:
5924 unary-expression:
5925 __alignof__ unary-expression
5926 __alignof__ ( type-name )
5927 && identifier
5929 (C11 permits _Alignof with type names only.)
5931 unary-operator: one of
5932 __extension__ __real__ __imag__
5934 Transactional Memory:
5936 unary-expression:
5937 transaction-expression
5939 In addition, the GNU syntax treats ++ and -- as unary operators, so
5940 they may be applied to cast expressions with errors for non-lvalues
5941 given later. */
5943 static struct c_expr
5944 c_parser_unary_expression (c_parser *parser)
5946 int ext;
5947 struct c_expr ret, op;
5948 location_t op_loc = c_parser_peek_token (parser)->location;
5949 location_t exp_loc;
5950 ret.original_code = ERROR_MARK;
5951 ret.original_type = NULL;
5952 switch (c_parser_peek_token (parser)->type)
5954 case CPP_PLUS_PLUS:
5955 c_parser_consume_token (parser);
5956 exp_loc = c_parser_peek_token (parser)->location;
5957 op = c_parser_cast_expression (parser, NULL);
5958 op = default_function_array_read_conversion (exp_loc, op);
5959 return parser_build_unary_op (op_loc, PREINCREMENT_EXPR, op);
5960 case CPP_MINUS_MINUS:
5961 c_parser_consume_token (parser);
5962 exp_loc = c_parser_peek_token (parser)->location;
5963 op = c_parser_cast_expression (parser, NULL);
5964 op = default_function_array_read_conversion (exp_loc, op);
5965 return parser_build_unary_op (op_loc, PREDECREMENT_EXPR, op);
5966 case CPP_AND:
5967 c_parser_consume_token (parser);
5968 op = c_parser_cast_expression (parser, NULL);
5969 mark_exp_read (op.value);
5970 return parser_build_unary_op (op_loc, ADDR_EXPR, op);
5971 case CPP_MULT:
5972 c_parser_consume_token (parser);
5973 exp_loc = c_parser_peek_token (parser)->location;
5974 op = c_parser_cast_expression (parser, NULL);
5975 op = default_function_array_read_conversion (exp_loc, op);
5976 ret.value = build_indirect_ref (op_loc, op.value, RO_UNARY_STAR);
5977 return ret;
5978 case CPP_PLUS:
5979 if (!c_dialect_objc () && !in_system_header)
5980 warning_at (op_loc,
5981 OPT_Wtraditional,
5982 "traditional C rejects the unary plus operator");
5983 c_parser_consume_token (parser);
5984 exp_loc = c_parser_peek_token (parser)->location;
5985 op = c_parser_cast_expression (parser, NULL);
5986 op = default_function_array_read_conversion (exp_loc, op);
5987 return parser_build_unary_op (op_loc, CONVERT_EXPR, op);
5988 case CPP_MINUS:
5989 c_parser_consume_token (parser);
5990 exp_loc = c_parser_peek_token (parser)->location;
5991 op = c_parser_cast_expression (parser, NULL);
5992 op = default_function_array_read_conversion (exp_loc, op);
5993 return parser_build_unary_op (op_loc, NEGATE_EXPR, op);
5994 case CPP_COMPL:
5995 c_parser_consume_token (parser);
5996 exp_loc = c_parser_peek_token (parser)->location;
5997 op = c_parser_cast_expression (parser, NULL);
5998 op = default_function_array_read_conversion (exp_loc, op);
5999 return parser_build_unary_op (op_loc, BIT_NOT_EXPR, op);
6000 case CPP_NOT:
6001 c_parser_consume_token (parser);
6002 exp_loc = c_parser_peek_token (parser)->location;
6003 op = c_parser_cast_expression (parser, NULL);
6004 op = default_function_array_read_conversion (exp_loc, op);
6005 return parser_build_unary_op (op_loc, TRUTH_NOT_EXPR, op);
6006 case CPP_AND_AND:
6007 /* Refer to the address of a label as a pointer. */
6008 c_parser_consume_token (parser);
6009 if (c_parser_next_token_is (parser, CPP_NAME))
6011 ret.value = finish_label_address_expr
6012 (c_parser_peek_token (parser)->value, op_loc);
6013 c_parser_consume_token (parser);
6015 else
6017 c_parser_error (parser, "expected identifier");
6018 ret.value = error_mark_node;
6020 return ret;
6021 case CPP_KEYWORD:
6022 switch (c_parser_peek_token (parser)->keyword)
6024 case RID_SIZEOF:
6025 return c_parser_sizeof_expression (parser);
6026 case RID_UPC_BLOCKSIZEOF:
6027 case RID_UPC_ELEMSIZEOF:
6028 case RID_UPC_LOCALSIZEOF:
6029 gcc_assert (c_dialect_upc ());
6030 return c_parser_sizeof_expression (parser);
6031 case RID_ALIGNOF:
6032 return c_parser_alignof_expression (parser);
6033 case RID_EXTENSION:
6034 c_parser_consume_token (parser);
6035 ext = disable_extension_diagnostics ();
6036 ret = c_parser_cast_expression (parser, NULL);
6037 restore_extension_diagnostics (ext);
6038 return ret;
6039 case RID_REALPART:
6040 c_parser_consume_token (parser);
6041 exp_loc = c_parser_peek_token (parser)->location;
6042 op = c_parser_cast_expression (parser, NULL);
6043 op = default_function_array_conversion (exp_loc, op);
6044 return parser_build_unary_op (op_loc, REALPART_EXPR, op);
6045 case RID_IMAGPART:
6046 c_parser_consume_token (parser);
6047 exp_loc = c_parser_peek_token (parser)->location;
6048 op = c_parser_cast_expression (parser, NULL);
6049 op = default_function_array_conversion (exp_loc, op);
6050 return parser_build_unary_op (op_loc, IMAGPART_EXPR, op);
6051 case RID_TRANSACTION_ATOMIC:
6052 case RID_TRANSACTION_RELAXED:
6053 return c_parser_transaction_expression (parser,
6054 c_parser_peek_token (parser)->keyword);
6055 default:
6056 return c_parser_postfix_expression (parser);
6058 default:
6059 return c_parser_postfix_expression (parser);
6063 /* Return the result of upc_blocksizeof applied to EXPR. */
6065 static
6066 struct c_expr
6067 upc_blocksizeof_expr (location_t loc, struct c_expr expr)
6069 struct c_expr ret;
6070 ret.original_code = ERROR_MARK;
6071 ret.original_type = NULL_TREE;
6072 if (expr.value == error_mark_node)
6074 ret.value = error_mark_node;
6075 pop_maybe_used (false);
6077 else
6079 ret.value = upc_blocksizeof (loc, TREE_TYPE (expr.value));
6080 pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (expr.value)));
6082 return ret;
6085 /* Return the result of upc_blocksizeof applied to T, a structure
6086 for the type name passed to sizeof (rather than the type itself). */
6088 static
6089 struct c_expr
6090 upc_blocksizeof_type (location_t loc, struct c_type_name *t)
6092 tree type;
6093 struct c_expr ret;
6094 ret.original_code = ERROR_MARK;
6095 ret.original_type = NULL_TREE;
6096 type = groktypename (t, NULL, NULL);
6097 if (type == error_mark_node)
6099 ret.value = error_mark_node;
6100 pop_maybe_used (false);
6102 else
6104 ret.value = upc_blocksizeof (loc, type);
6105 pop_maybe_used (C_TYPE_VARIABLE_SIZE (type));
6107 return ret;
6110 /* Return the result of upc_elemsizeof applied to EXPR. */
6112 static
6113 struct c_expr
6114 upc_elemsizeof_expr (location_t loc, struct c_expr expr)
6116 struct c_expr ret;
6117 ret.original_code = ERROR_MARK;
6118 ret.original_type = NULL_TREE;
6119 if (expr.value == error_mark_node)
6121 ret.value = error_mark_node;
6122 pop_maybe_used (false);
6124 else
6126 ret.value = upc_elemsizeof (loc, TREE_TYPE (expr.value));
6127 pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (expr.value)));
6129 return ret;
6132 /* Return the result of upc_elemsizeof applied to T, a structure
6133 for the type name passed to sizeof (rather than the type itself). */
6135 static
6136 struct c_expr
6137 upc_elemsizeof_type (location_t loc, struct c_type_name *t)
6139 tree type;
6140 struct c_expr ret;
6141 ret.original_code = ERROR_MARK;
6142 ret.original_type = NULL_TREE;
6143 type = groktypename (t, NULL, NULL);
6144 if (type == error_mark_node)
6146 ret.value = error_mark_node;
6147 pop_maybe_used (false);
6149 else
6151 ret.value = upc_elemsizeof (loc, type);
6152 pop_maybe_used (C_TYPE_VARIABLE_SIZE (type));
6154 return ret;
6157 /* Return the result of upc_localsizeof applied to EXPR. */
6159 static
6160 struct c_expr
6161 upc_localsizeof_expr (location_t loc, struct c_expr expr)
6163 struct c_expr ret;
6164 ret.original_code = ERROR_MARK;
6165 ret.original_type = NULL_TREE;
6166 if (expr.value == error_mark_node)
6168 ret.value = error_mark_node;
6169 pop_maybe_used (false);
6171 else
6173 ret.value = upc_localsizeof (loc, TREE_TYPE (expr.value));
6174 pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (expr.value)));
6176 return ret;
6179 /* Return the result of upc_localsizeof applied to T, a structure
6180 for the type name passed to sizeof (rather than the type itself). */
6182 static
6183 struct c_expr
6184 upc_localsizeof_type (location_t loc, struct c_type_name *t)
6186 tree type;
6187 struct c_expr ret;
6188 ret.original_code = ERROR_MARK;
6189 ret.original_type = NULL_TREE;
6190 type = groktypename (t, NULL, NULL);
6191 if (type == error_mark_node)
6193 ret.value = error_mark_node;
6194 pop_maybe_used (false);
6196 else
6198 ret.value = upc_localsizeof (loc, type);
6199 pop_maybe_used (C_TYPE_VARIABLE_SIZE (type));
6201 return ret;
6204 /* Parse a sizeof expression. */
6206 static struct c_expr
6207 c_parser_sizeof_expression (c_parser *parser)
6209 struct c_expr expr;
6210 location_t expr_loc;
6211 enum rid keyword = c_parser_peek_token (parser)->keyword;
6212 c_parser_consume_token (parser);
6213 c_inhibit_evaluation_warnings++;
6214 in_sizeof++;
6215 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
6216 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
6218 /* Either sizeof ( type-name ) or sizeof unary-expression
6219 starting with a compound literal. */
6220 struct c_type_name *type_name;
6221 c_parser_consume_token (parser);
6222 expr_loc = c_parser_peek_token (parser)->location;
6223 type_name = c_parser_type_name (parser);
6224 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6225 if (type_name == NULL)
6227 struct c_expr ret;
6228 c_inhibit_evaluation_warnings--;
6229 in_sizeof--;
6230 ret.value = error_mark_node;
6231 ret.original_code = ERROR_MARK;
6232 ret.original_type = NULL;
6233 return ret;
6235 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
6237 expr = c_parser_postfix_expression_after_paren_type (parser,
6238 type_name,
6239 expr_loc);
6240 goto sizeof_expr;
6242 /* sizeof ( type-name ). */
6243 c_inhibit_evaluation_warnings--;
6244 in_sizeof--;
6245 /* Handle upc_*_sizeof (type) operations. */
6246 switch (keyword)
6248 case RID_UPC_BLOCKSIZEOF:
6249 return upc_blocksizeof_type (expr_loc, type_name);
6250 case RID_UPC_ELEMSIZEOF:
6251 return upc_elemsizeof_type (expr_loc, type_name);
6252 case RID_UPC_LOCALSIZEOF:
6253 return upc_localsizeof_type (expr_loc, type_name);
6254 default: break;
6256 return c_expr_sizeof_type (expr_loc, type_name);
6258 else
6260 expr_loc = c_parser_peek_token (parser)->location;
6261 expr = c_parser_unary_expression (parser);
6262 sizeof_expr:
6263 c_inhibit_evaluation_warnings--;
6264 in_sizeof--;
6265 mark_exp_read (expr.value);
6266 if (TREE_CODE (expr.value) == COMPONENT_REF
6267 && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
6268 error_at (expr_loc, "%<sizeof%> applied to a bit-field");
6269 /* Handle upc_*_sizeof (expr) operations. */
6270 switch (keyword)
6272 case RID_UPC_BLOCKSIZEOF:
6273 return upc_blocksizeof_expr (expr_loc, expr);
6274 case RID_UPC_ELEMSIZEOF:
6275 return upc_elemsizeof_expr (expr_loc, expr);
6276 case RID_UPC_LOCALSIZEOF:
6277 return upc_localsizeof_expr (expr_loc, expr);
6278 case RID_SIZEOF:
6279 return c_expr_sizeof_expr (expr_loc, expr);
6280 default: break;
6282 return c_expr_sizeof_expr (expr_loc, expr);
6286 /* Parse an alignof expression. */
6288 static struct c_expr
6289 c_parser_alignof_expression (c_parser *parser)
6291 struct c_expr expr;
6292 location_t loc = c_parser_peek_token (parser)->location;
6293 tree alignof_spelling = c_parser_peek_token (parser)->value;
6294 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ALIGNOF));
6295 /* A diagnostic is not required for the use of this identifier in
6296 the implementation namespace; only diagnose it for the C11
6297 spelling because of existing code using the other spellings. */
6298 if (!flag_isoc11
6299 && strcmp (IDENTIFIER_POINTER (alignof_spelling), "_Alignof") == 0)
6301 if (flag_isoc99)
6302 pedwarn (loc, OPT_Wpedantic, "ISO C99 does not support %qE",
6303 alignof_spelling);
6304 else
6305 pedwarn (loc, OPT_Wpedantic, "ISO C90 does not support %qE",
6306 alignof_spelling);
6308 c_parser_consume_token (parser);
6309 c_inhibit_evaluation_warnings++;
6310 in_alignof++;
6311 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
6312 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
6314 /* Either __alignof__ ( type-name ) or __alignof__
6315 unary-expression starting with a compound literal. */
6316 location_t loc;
6317 struct c_type_name *type_name;
6318 struct c_expr ret;
6319 c_parser_consume_token (parser);
6320 loc = c_parser_peek_token (parser)->location;
6321 type_name = c_parser_type_name (parser);
6322 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6323 if (type_name == NULL)
6325 struct c_expr ret;
6326 c_inhibit_evaluation_warnings--;
6327 in_alignof--;
6328 ret.value = error_mark_node;
6329 ret.original_code = ERROR_MARK;
6330 ret.original_type = NULL;
6331 return ret;
6333 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
6335 expr = c_parser_postfix_expression_after_paren_type (parser,
6336 type_name,
6337 loc);
6338 goto alignof_expr;
6340 /* alignof ( type-name ). */
6341 c_inhibit_evaluation_warnings--;
6342 in_alignof--;
6343 ret.value = c_alignof (loc, groktypename (type_name, NULL, NULL));
6344 ret.original_code = ERROR_MARK;
6345 ret.original_type = NULL;
6346 return ret;
6348 else
6350 struct c_expr ret;
6351 expr = c_parser_unary_expression (parser);
6352 alignof_expr:
6353 mark_exp_read (expr.value);
6354 c_inhibit_evaluation_warnings--;
6355 in_alignof--;
6356 pedwarn (loc, OPT_Wpedantic, "ISO C does not allow %<%E (expression)%>",
6357 alignof_spelling);
6358 ret.value = c_alignof_expr (loc, expr.value);
6359 ret.original_code = ERROR_MARK;
6360 ret.original_type = NULL;
6361 return ret;
6365 /* Helper function to read arguments of builtins which are interfaces
6366 for the middle-end nodes like COMPLEX_EXPR, VEC_PERM_EXPR and
6367 others. The name of the builtin is passed using BNAME parameter.
6368 Function returns true if there were no errors while parsing and
6369 stores the arguments in CEXPR_LIST. */
6370 static bool
6371 c_parser_get_builtin_args (c_parser *parser, const char *bname,
6372 VEC(c_expr_t,gc) **ret_cexpr_list)
6374 location_t loc = c_parser_peek_token (parser)->location;
6375 VEC (c_expr_t,gc) *cexpr_list;
6376 c_expr_t expr;
6378 *ret_cexpr_list = NULL;
6379 if (c_parser_next_token_is_not (parser, CPP_OPEN_PAREN))
6381 error_at (loc, "cannot take address of %qs", bname);
6382 return false;
6385 c_parser_consume_token (parser);
6387 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
6389 c_parser_consume_token (parser);
6390 return true;
6393 expr = c_parser_expr_no_commas (parser, NULL);
6394 cexpr_list = VEC_alloc (c_expr_t, gc, 1);
6395 C_EXPR_APPEND (cexpr_list, expr);
6396 while (c_parser_next_token_is (parser, CPP_COMMA))
6398 c_parser_consume_token (parser);
6399 expr = c_parser_expr_no_commas (parser, NULL);
6400 C_EXPR_APPEND (cexpr_list, expr);
6403 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
6404 return false;
6406 *ret_cexpr_list = cexpr_list;
6407 return true;
6411 /* Parse a postfix expression (C90 6.3.1-6.3.2, C99 6.5.1-6.5.2).
6413 postfix-expression:
6414 primary-expression
6415 postfix-expression [ expression ]
6416 postfix-expression ( argument-expression-list[opt] )
6417 postfix-expression . identifier
6418 postfix-expression -> identifier
6419 postfix-expression ++
6420 postfix-expression --
6421 ( type-name ) { initializer-list }
6422 ( type-name ) { initializer-list , }
6424 argument-expression-list:
6425 argument-expression
6426 argument-expression-list , argument-expression
6428 primary-expression:
6429 identifier
6430 constant
6431 string-literal
6432 ( expression )
6434 GNU extensions:
6436 primary-expression:
6437 __func__
6438 (treated as a keyword in GNU C)
6439 __FUNCTION__
6440 __PRETTY_FUNCTION__
6441 ( compound-statement )
6442 __builtin_va_arg ( assignment-expression , type-name )
6443 __builtin_offsetof ( type-name , offsetof-member-designator )
6444 __builtin_choose_expr ( assignment-expression ,
6445 assignment-expression ,
6446 assignment-expression )
6447 __builtin_types_compatible_p ( type-name , type-name )
6448 __builtin_complex ( assignment-expression , assignment-expression )
6449 __builtin_shuffle ( assignment-expression , assignment-expression )
6450 __builtin_shuffle ( assignment-expression ,
6451 assignment-expression ,
6452 assignment-expression, )
6454 offsetof-member-designator:
6455 identifier
6456 offsetof-member-designator . identifier
6457 offsetof-member-designator [ expression ]
6459 Objective-C:
6461 primary-expression:
6462 [ objc-receiver objc-message-args ]
6463 @selector ( objc-selector-arg )
6464 @protocol ( identifier )
6465 @encode ( type-name )
6466 objc-string-literal
6467 Classname . identifier
6470 static struct c_expr
6471 c_parser_postfix_expression (c_parser *parser)
6473 struct c_expr expr, e1;
6474 struct c_type_name *t1, *t2;
6475 location_t loc = c_parser_peek_token (parser)->location;;
6476 expr.original_code = ERROR_MARK;
6477 expr.original_type = NULL;
6478 switch (c_parser_peek_token (parser)->type)
6480 case CPP_NUMBER:
6481 expr.value = c_parser_peek_token (parser)->value;
6482 loc = c_parser_peek_token (parser)->location;
6483 c_parser_consume_token (parser);
6484 if (TREE_CODE (expr.value) == FIXED_CST
6485 && !targetm.fixed_point_supported_p ())
6487 error_at (loc, "fixed-point types not supported for this target");
6488 expr.value = error_mark_node;
6490 break;
6491 case CPP_CHAR:
6492 case CPP_CHAR16:
6493 case CPP_CHAR32:
6494 case CPP_WCHAR:
6495 expr.value = c_parser_peek_token (parser)->value;
6496 c_parser_consume_token (parser);
6497 break;
6498 case CPP_STRING:
6499 case CPP_STRING16:
6500 case CPP_STRING32:
6501 case CPP_WSTRING:
6502 case CPP_UTF8STRING:
6503 expr.value = c_parser_peek_token (parser)->value;
6504 expr.original_code = STRING_CST;
6505 c_parser_consume_token (parser);
6506 break;
6507 case CPP_OBJC_STRING:
6508 gcc_assert (c_dialect_objc ());
6509 expr.value
6510 = objc_build_string_object (c_parser_peek_token (parser)->value);
6511 c_parser_consume_token (parser);
6512 break;
6513 case CPP_NAME:
6514 switch (c_parser_peek_token (parser)->id_kind)
6516 case C_ID_ID:
6518 tree id = c_parser_peek_token (parser)->value;
6519 c_parser_consume_token (parser);
6520 expr.value = build_external_ref (loc, id,
6521 (c_parser_peek_token (parser)->type
6522 == CPP_OPEN_PAREN),
6523 &expr.original_type);
6524 break;
6526 case C_ID_CLASSNAME:
6528 /* Here we parse the Objective-C 2.0 Class.name dot
6529 syntax. */
6530 tree class_name = c_parser_peek_token (parser)->value;
6531 tree component;
6532 c_parser_consume_token (parser);
6533 gcc_assert (c_dialect_objc ());
6534 if (!c_parser_require (parser, CPP_DOT, "expected %<.%>"))
6536 expr.value = error_mark_node;
6537 break;
6539 if (c_parser_next_token_is_not (parser, CPP_NAME))
6541 c_parser_error (parser, "expected identifier");
6542 expr.value = error_mark_node;
6543 break;
6545 component = c_parser_peek_token (parser)->value;
6546 c_parser_consume_token (parser);
6547 expr.value = objc_build_class_component_ref (class_name,
6548 component);
6549 break;
6551 default:
6552 c_parser_error (parser, "expected expression");
6553 expr.value = error_mark_node;
6554 break;
6556 break;
6557 case CPP_OPEN_PAREN:
6558 /* A parenthesized expression, statement expression or compound
6559 literal. */
6560 if (c_parser_peek_2nd_token (parser)->type == CPP_OPEN_BRACE)
6562 /* A statement expression. */
6563 tree stmt;
6564 location_t brace_loc;
6565 c_parser_consume_token (parser);
6566 brace_loc = c_parser_peek_token (parser)->location;
6567 c_parser_consume_token (parser);
6568 if (!building_stmt_list_p ())
6570 error_at (loc, "braced-group within expression allowed "
6571 "only inside a function");
6572 parser->error = true;
6573 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
6574 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6575 expr.value = error_mark_node;
6576 break;
6578 stmt = c_begin_stmt_expr ();
6579 c_parser_compound_statement_nostart (parser);
6580 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6581 "expected %<)%>");
6582 pedwarn (loc, OPT_Wpedantic,
6583 "ISO C forbids braced-groups within expressions");
6584 expr.value = c_finish_stmt_expr (brace_loc, stmt);
6585 mark_exp_read (expr.value);
6587 else if (c_token_starts_typename (c_parser_peek_2nd_token (parser)))
6589 /* A compound literal. ??? Can we actually get here rather
6590 than going directly to
6591 c_parser_postfix_expression_after_paren_type from
6592 elsewhere? */
6593 location_t loc;
6594 struct c_type_name *type_name;
6595 c_parser_consume_token (parser);
6596 loc = c_parser_peek_token (parser)->location;
6597 type_name = c_parser_type_name (parser);
6598 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6599 "expected %<)%>");
6600 if (type_name == NULL)
6602 expr.value = error_mark_node;
6604 else
6605 expr = c_parser_postfix_expression_after_paren_type (parser,
6606 type_name,
6607 loc);
6609 else
6611 /* A parenthesized expression. */
6612 c_parser_consume_token (parser);
6613 expr = c_parser_expression (parser);
6614 if (TREE_CODE (expr.value) == MODIFY_EXPR)
6615 TREE_NO_WARNING (expr.value) = 1;
6616 if (expr.original_code != C_MAYBE_CONST_EXPR)
6617 expr.original_code = ERROR_MARK;
6618 /* Don't change EXPR.ORIGINAL_TYPE. */
6619 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6620 "expected %<)%>");
6622 break;
6623 case CPP_KEYWORD:
6624 switch (c_parser_peek_token (parser)->keyword)
6626 case RID_FUNCTION_NAME:
6627 case RID_PRETTY_FUNCTION_NAME:
6628 case RID_C99_FUNCTION_NAME:
6629 expr.value = fname_decl (loc,
6630 c_parser_peek_token (parser)->keyword,
6631 c_parser_peek_token (parser)->value);
6632 c_parser_consume_token (parser);
6633 break;
6634 case RID_VA_ARG:
6635 c_parser_consume_token (parser);
6636 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6638 expr.value = error_mark_node;
6639 break;
6641 e1 = c_parser_expr_no_commas (parser, NULL);
6642 mark_exp_read (e1.value);
6643 e1.value = c_fully_fold (e1.value, false, NULL);
6644 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
6646 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6647 expr.value = error_mark_node;
6648 break;
6650 loc = c_parser_peek_token (parser)->location;
6651 t1 = c_parser_type_name (parser);
6652 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6653 "expected %<)%>");
6654 if (t1 == NULL)
6656 expr.value = error_mark_node;
6658 else
6660 tree type_expr = NULL_TREE;
6661 expr.value = c_build_va_arg (loc, e1.value,
6662 groktypename (t1, &type_expr, NULL));
6663 if (type_expr)
6665 expr.value = build2 (C_MAYBE_CONST_EXPR,
6666 TREE_TYPE (expr.value), type_expr,
6667 expr.value);
6668 C_MAYBE_CONST_EXPR_NON_CONST (expr.value) = true;
6671 break;
6672 case RID_OFFSETOF:
6673 c_parser_consume_token (parser);
6674 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6676 expr.value = error_mark_node;
6677 break;
6679 t1 = c_parser_type_name (parser);
6680 if (t1 == NULL)
6681 parser->error = true;
6682 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
6683 gcc_assert (parser->error);
6684 if (parser->error)
6686 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6687 expr.value = error_mark_node;
6688 break;
6692 tree type = groktypename (t1, NULL, NULL);
6693 tree offsetof_ref;
6694 if (type == error_mark_node)
6695 offsetof_ref = error_mark_node;
6696 else
6698 offsetof_ref = build1 (INDIRECT_REF, type, null_pointer_node);
6699 SET_EXPR_LOCATION (offsetof_ref, loc);
6701 /* Parse the second argument to __builtin_offsetof. We
6702 must have one identifier, and beyond that we want to
6703 accept sub structure and sub array references. */
6704 if (c_parser_next_token_is (parser, CPP_NAME))
6706 offsetof_ref = build_component_ref
6707 (loc, offsetof_ref, c_parser_peek_token (parser)->value);
6708 c_parser_consume_token (parser);
6709 while (c_parser_next_token_is (parser, CPP_DOT)
6710 || c_parser_next_token_is (parser,
6711 CPP_OPEN_SQUARE)
6712 || c_parser_next_token_is (parser,
6713 CPP_DEREF))
6715 if (c_parser_next_token_is (parser, CPP_DEREF))
6717 loc = c_parser_peek_token (parser)->location;
6718 offsetof_ref = build_array_ref (loc,
6719 offsetof_ref,
6720 integer_zero_node);
6721 goto do_dot;
6723 else if (c_parser_next_token_is (parser, CPP_DOT))
6725 do_dot:
6726 c_parser_consume_token (parser);
6727 if (c_parser_next_token_is_not (parser,
6728 CPP_NAME))
6730 c_parser_error (parser, "expected identifier");
6731 break;
6733 offsetof_ref = build_component_ref
6734 (loc, offsetof_ref,
6735 c_parser_peek_token (parser)->value);
6736 c_parser_consume_token (parser);
6738 else
6740 tree idx;
6741 loc = c_parser_peek_token (parser)->location;
6742 c_parser_consume_token (parser);
6743 idx = c_parser_expression (parser).value;
6744 idx = c_fully_fold (idx, false, NULL);
6745 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
6746 "expected %<]%>");
6747 offsetof_ref = build_array_ref (loc, offsetof_ref, idx);
6751 else
6752 c_parser_error (parser, "expected identifier");
6753 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6754 "expected %<)%>");
6755 expr.value = fold_offsetof (offsetof_ref);
6757 break;
6758 case RID_CHOOSE_EXPR:
6760 VEC (c_expr_t, gc) *cexpr_list;
6761 c_expr_t *e1_p, *e2_p, *e3_p;
6762 tree c;
6764 c_parser_consume_token (parser);
6765 if (!c_parser_get_builtin_args (parser,
6766 "__builtin_choose_expr",
6767 &cexpr_list))
6769 expr.value = error_mark_node;
6770 break;
6773 if (VEC_length (c_expr_t, cexpr_list) != 3)
6775 error_at (loc, "wrong number of arguments to "
6776 "%<__builtin_choose_expr%>");
6777 expr.value = error_mark_node;
6778 break;
6781 e1_p = &VEC_index (c_expr_t, cexpr_list, 0);
6782 e2_p = &VEC_index (c_expr_t, cexpr_list, 1);
6783 e3_p = &VEC_index (c_expr_t, cexpr_list, 2);
6785 c = e1_p->value;
6786 mark_exp_read (e2_p->value);
6787 mark_exp_read (e3_p->value);
6788 if (TREE_CODE (c) != INTEGER_CST
6789 || !INTEGRAL_TYPE_P (TREE_TYPE (c)))
6790 error_at (loc,
6791 "first argument to %<__builtin_choose_expr%> not"
6792 " a constant");
6793 constant_expression_warning (c);
6794 expr = integer_zerop (c) ? *e3_p : *e2_p;
6795 break;
6797 case RID_TYPES_COMPATIBLE_P:
6798 c_parser_consume_token (parser);
6799 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6801 expr.value = error_mark_node;
6802 break;
6804 t1 = c_parser_type_name (parser);
6805 if (t1 == NULL)
6807 expr.value = error_mark_node;
6808 break;
6810 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
6812 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6813 expr.value = error_mark_node;
6814 break;
6816 t2 = c_parser_type_name (parser);
6817 if (t2 == NULL)
6819 expr.value = error_mark_node;
6820 break;
6822 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6823 "expected %<)%>");
6825 tree e1, e2;
6826 e1 = groktypename (t1, NULL, NULL);
6827 e2 = groktypename (t2, NULL, NULL);
6828 if (e1 == error_mark_node || e2 == error_mark_node)
6830 expr.value = error_mark_node;
6831 break;
6834 e1 = TYPE_MAIN_VARIANT (e1);
6835 e2 = TYPE_MAIN_VARIANT (e2);
6837 expr.value
6838 = comptypes (e1, e2) ? integer_one_node : integer_zero_node;
6840 break;
6841 case RID_BUILTIN_COMPLEX:
6843 VEC(c_expr_t, gc) *cexpr_list;
6844 c_expr_t *e1_p, *e2_p;
6846 c_parser_consume_token (parser);
6847 if (!c_parser_get_builtin_args (parser,
6848 "__builtin_complex",
6849 &cexpr_list))
6851 expr.value = error_mark_node;
6852 break;
6855 if (VEC_length (c_expr_t, cexpr_list) != 2)
6857 error_at (loc, "wrong number of arguments to "
6858 "%<__builtin_complex%>");
6859 expr.value = error_mark_node;
6860 break;
6863 e1_p = &VEC_index (c_expr_t, cexpr_list, 0);
6864 e2_p = &VEC_index (c_expr_t, cexpr_list, 1);
6866 mark_exp_read (e1_p->value);
6867 if (TREE_CODE (e1_p->value) == EXCESS_PRECISION_EXPR)
6868 e1_p->value = convert (TREE_TYPE (e1_p->value),
6869 TREE_OPERAND (e1_p->value, 0));
6870 mark_exp_read (e2_p->value);
6871 if (TREE_CODE (e2_p->value) == EXCESS_PRECISION_EXPR)
6872 e2_p->value = convert (TREE_TYPE (e2_p->value),
6873 TREE_OPERAND (e2_p->value, 0));
6874 if (!SCALAR_FLOAT_TYPE_P (TREE_TYPE (e1_p->value))
6875 || DECIMAL_FLOAT_TYPE_P (TREE_TYPE (e1_p->value))
6876 || !SCALAR_FLOAT_TYPE_P (TREE_TYPE (e2_p->value))
6877 || DECIMAL_FLOAT_TYPE_P (TREE_TYPE (e2_p->value)))
6879 error_at (loc, "%<__builtin_complex%> operand "
6880 "not of real binary floating-point type");
6881 expr.value = error_mark_node;
6882 break;
6884 if (TYPE_MAIN_VARIANT (TREE_TYPE (e1_p->value))
6885 != TYPE_MAIN_VARIANT (TREE_TYPE (e2_p->value)))
6887 error_at (loc,
6888 "%<__builtin_complex%> operands of different types");
6889 expr.value = error_mark_node;
6890 break;
6892 if (!flag_isoc99)
6893 pedwarn (loc, OPT_Wpedantic,
6894 "ISO C90 does not support complex types");
6895 expr.value = build2 (COMPLEX_EXPR,
6896 build_complex_type
6897 (TYPE_MAIN_VARIANT
6898 (TREE_TYPE (e1_p->value))),
6899 e1_p->value, e2_p->value);
6900 break;
6902 case RID_BUILTIN_SHUFFLE:
6904 VEC(c_expr_t,gc) *cexpr_list;
6905 unsigned int i;
6906 c_expr_t *p;
6908 c_parser_consume_token (parser);
6909 if (!c_parser_get_builtin_args (parser,
6910 "__builtin_shuffle",
6911 &cexpr_list))
6913 expr.value = error_mark_node;
6914 break;
6917 FOR_EACH_VEC_ELT (c_expr_t, cexpr_list, i, p)
6918 mark_exp_read (p->value);
6920 if (VEC_length (c_expr_t, cexpr_list) == 2)
6921 expr.value =
6922 c_build_vec_perm_expr
6923 (loc, VEC_index (c_expr_t, cexpr_list, 0).value,
6924 NULL_TREE, VEC_index (c_expr_t, cexpr_list, 1).value);
6926 else if (VEC_length (c_expr_t, cexpr_list) == 3)
6927 expr.value =
6928 c_build_vec_perm_expr
6929 (loc, VEC_index (c_expr_t, cexpr_list, 0).value,
6930 VEC_index (c_expr_t, cexpr_list, 1).value,
6931 VEC_index (c_expr_t, cexpr_list, 2).value);
6932 else
6934 error_at (loc, "wrong number of arguments to "
6935 "%<__builtin_shuffle%>");
6936 expr.value = error_mark_node;
6938 break;
6940 case RID_AT_SELECTOR:
6941 gcc_assert (c_dialect_objc ());
6942 c_parser_consume_token (parser);
6943 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6945 expr.value = error_mark_node;
6946 break;
6949 tree sel = c_parser_objc_selector_arg (parser);
6950 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6951 "expected %<)%>");
6952 expr.value = objc_build_selector_expr (loc, sel);
6954 break;
6955 case RID_AT_PROTOCOL:
6956 gcc_assert (c_dialect_objc ());
6957 c_parser_consume_token (parser);
6958 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6960 expr.value = error_mark_node;
6961 break;
6963 if (c_parser_next_token_is_not (parser, CPP_NAME))
6965 c_parser_error (parser, "expected identifier");
6966 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6967 expr.value = error_mark_node;
6968 break;
6971 tree id = c_parser_peek_token (parser)->value;
6972 c_parser_consume_token (parser);
6973 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6974 "expected %<)%>");
6975 expr.value = objc_build_protocol_expr (id);
6977 break;
6978 case RID_AT_ENCODE:
6979 /* Extension to support C-structures in the archiver. */
6980 gcc_assert (c_dialect_objc ());
6981 c_parser_consume_token (parser);
6982 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6984 expr.value = error_mark_node;
6985 break;
6987 t1 = c_parser_type_name (parser);
6988 if (t1 == NULL)
6990 expr.value = error_mark_node;
6991 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6992 break;
6994 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6995 "expected %<)%>");
6997 tree type = groktypename (t1, NULL, NULL);
6998 expr.value = objc_build_encode_expr (type);
7000 break;
7001 default:
7002 c_parser_error (parser, "expected expression");
7003 expr.value = error_mark_node;
7004 break;
7006 break;
7007 case CPP_OPEN_SQUARE:
7008 if (c_dialect_objc ())
7010 tree receiver, args;
7011 c_parser_consume_token (parser);
7012 receiver = c_parser_objc_receiver (parser);
7013 args = c_parser_objc_message_args (parser);
7014 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
7015 "expected %<]%>");
7016 expr.value = objc_build_message_expr (receiver, args);
7017 break;
7019 /* Else fall through to report error. */
7020 default:
7021 c_parser_error (parser, "expected expression");
7022 expr.value = error_mark_node;
7023 break;
7025 return c_parser_postfix_expression_after_primary (parser, loc, expr);
7028 /* Parse a postfix expression after a parenthesized type name: the
7029 brace-enclosed initializer of a compound literal, possibly followed
7030 by some postfix operators. This is separate because it is not
7031 possible to tell until after the type name whether a cast
7032 expression has a cast or a compound literal, or whether the operand
7033 of sizeof is a parenthesized type name or starts with a compound
7034 literal. TYPE_LOC is the location where TYPE_NAME starts--the
7035 location of the first token after the parentheses around the type
7036 name. */
7038 static struct c_expr
7039 c_parser_postfix_expression_after_paren_type (c_parser *parser,
7040 struct c_type_name *type_name,
7041 location_t type_loc)
7043 tree type;
7044 struct c_expr init;
7045 bool non_const;
7046 struct c_expr expr;
7047 location_t start_loc;
7048 tree type_expr = NULL_TREE;
7049 bool type_expr_const = true;
7050 check_compound_literal_type (type_loc, type_name);
7051 start_init (NULL_TREE, NULL, 0);
7052 type = groktypename (type_name, &type_expr, &type_expr_const);
7053 start_loc = c_parser_peek_token (parser)->location;
7054 if (type != error_mark_node && C_TYPE_VARIABLE_SIZE (type))
7056 error_at (type_loc, "compound literal has variable size");
7057 type = error_mark_node;
7059 init = c_parser_braced_init (parser, type, false);
7060 finish_init ();
7061 maybe_warn_string_init (type, init);
7063 if (type != error_mark_node
7064 && !ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (type))
7065 && current_function_decl)
7067 error ("compound literal qualified by address-space qualifier");
7068 type = error_mark_node;
7071 if (!flag_isoc99)
7072 pedwarn (start_loc, OPT_Wpedantic, "ISO C90 forbids compound literals");
7073 non_const = ((init.value && TREE_CODE (init.value) == CONSTRUCTOR)
7074 ? CONSTRUCTOR_NON_CONST (init.value)
7075 : init.original_code == C_MAYBE_CONST_EXPR);
7076 non_const |= !type_expr_const;
7077 expr.value = build_compound_literal (start_loc, type, init.value, non_const);
7078 expr.original_code = ERROR_MARK;
7079 expr.original_type = NULL;
7080 if (type_expr)
7082 if (TREE_CODE (expr.value) == C_MAYBE_CONST_EXPR)
7084 gcc_assert (C_MAYBE_CONST_EXPR_PRE (expr.value) == NULL_TREE);
7085 C_MAYBE_CONST_EXPR_PRE (expr.value) = type_expr;
7087 else
7089 gcc_assert (!non_const);
7090 expr.value = build2 (C_MAYBE_CONST_EXPR, type,
7091 type_expr, expr.value);
7094 return c_parser_postfix_expression_after_primary (parser, start_loc, expr);
7097 /* Callback function for sizeof_pointer_memaccess_warning to compare
7098 types. */
7100 static bool
7101 sizeof_ptr_memacc_comptypes (tree type1, tree type2)
7103 return comptypes (type1, type2) == 1;
7106 /* Parse a postfix expression after the initial primary or compound
7107 literal; that is, parse a series of postfix operators.
7109 EXPR_LOC is the location of the primary expression. */
7111 static struct c_expr
7112 c_parser_postfix_expression_after_primary (c_parser *parser,
7113 location_t expr_loc,
7114 struct c_expr expr)
7116 struct c_expr orig_expr;
7117 tree ident, idx;
7118 struct c_tree_loc_pair sizeof_arg;
7119 VEC(tree,gc) *exprlist;
7120 VEC(tree,gc) *origtypes;
7121 while (true)
7123 location_t op_loc = c_parser_peek_token (parser)->location;
7124 switch (c_parser_peek_token (parser)->type)
7126 case CPP_OPEN_SQUARE:
7127 /* Array reference. */
7128 c_parser_consume_token (parser);
7129 idx = c_parser_expression (parser).value;
7130 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
7131 "expected %<]%>");
7132 expr.value = build_array_ref (op_loc, expr.value, idx);
7133 expr.original_code = ERROR_MARK;
7134 expr.original_type = NULL;
7135 break;
7136 case CPP_OPEN_PAREN:
7137 /* Function call. */
7138 c_parser_consume_token (parser);
7139 sizeof_arg.expr = NULL_TREE;
7140 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
7141 exprlist = NULL;
7142 else
7143 exprlist = c_parser_expr_list (parser, true, false, &origtypes,
7144 &sizeof_arg);
7145 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7146 "expected %<)%>");
7147 orig_expr = expr;
7148 mark_exp_read (expr.value);
7149 if (warn_sizeof_pointer_memaccess
7150 && sizeof_arg.expr != NULL_TREE)
7151 sizeof_pointer_memaccess_warning (sizeof_arg.loc,
7152 expr.value, exprlist,
7153 sizeof_arg.expr,
7154 sizeof_ptr_memacc_comptypes);
7155 /* FIXME diagnostics: Ideally we want the FUNCNAME, not the
7156 "(" after the FUNCNAME, which is what we have now. */
7157 expr.value = build_function_call_vec (op_loc, expr.value, exprlist,
7158 origtypes);
7159 expr.original_code = ERROR_MARK;
7160 if (TREE_CODE (expr.value) == INTEGER_CST
7161 && TREE_CODE (orig_expr.value) == FUNCTION_DECL
7162 && DECL_BUILT_IN_CLASS (orig_expr.value) == BUILT_IN_NORMAL
7163 && DECL_FUNCTION_CODE (orig_expr.value) == BUILT_IN_CONSTANT_P)
7164 expr.original_code = C_MAYBE_CONST_EXPR;
7165 expr.original_type = NULL;
7166 if (exprlist != NULL)
7168 release_tree_vector (exprlist);
7169 release_tree_vector (origtypes);
7171 break;
7172 case CPP_DOT:
7173 /* Structure element reference. */
7174 c_parser_consume_token (parser);
7175 expr = default_function_array_conversion (expr_loc, expr);
7176 if (c_parser_next_token_is (parser, CPP_NAME))
7177 ident = c_parser_peek_token (parser)->value;
7178 else
7180 c_parser_error (parser, "expected identifier");
7181 expr.value = error_mark_node;
7182 expr.original_code = ERROR_MARK;
7183 expr.original_type = NULL;
7184 return expr;
7186 c_parser_consume_token (parser);
7187 expr.value = build_component_ref (op_loc, expr.value, ident);
7188 expr.original_code = ERROR_MARK;
7189 if (TREE_CODE (expr.value) != COMPONENT_REF)
7190 expr.original_type = NULL;
7191 else
7193 /* Remember the original type of a bitfield. */
7194 tree field = TREE_OPERAND (expr.value, 1);
7195 if (TREE_CODE (field) != FIELD_DECL)
7196 expr.original_type = NULL;
7197 else
7198 expr.original_type = DECL_BIT_FIELD_TYPE (field);
7200 break;
7201 case CPP_DEREF:
7202 /* Structure element reference. */
7203 c_parser_consume_token (parser);
7204 expr = default_function_array_conversion (expr_loc, expr);
7205 if (c_parser_next_token_is (parser, CPP_NAME))
7206 ident = c_parser_peek_token (parser)->value;
7207 else
7209 c_parser_error (parser, "expected identifier");
7210 expr.value = error_mark_node;
7211 expr.original_code = ERROR_MARK;
7212 expr.original_type = NULL;
7213 return expr;
7215 c_parser_consume_token (parser);
7216 expr.value = build_component_ref (op_loc,
7217 build_indirect_ref (op_loc,
7218 expr.value,
7219 RO_ARROW),
7220 ident);
7221 expr.original_code = ERROR_MARK;
7222 if (TREE_CODE (expr.value) != COMPONENT_REF)
7223 expr.original_type = NULL;
7224 else
7226 /* Remember the original type of a bitfield. */
7227 tree field = TREE_OPERAND (expr.value, 1);
7228 if (TREE_CODE (field) != FIELD_DECL)
7229 expr.original_type = NULL;
7230 else
7231 expr.original_type = DECL_BIT_FIELD_TYPE (field);
7233 break;
7234 case CPP_PLUS_PLUS:
7235 /* Postincrement. */
7236 c_parser_consume_token (parser);
7237 expr = default_function_array_read_conversion (expr_loc, expr);
7238 expr.value = build_unary_op (op_loc,
7239 POSTINCREMENT_EXPR, expr.value, 0);
7240 expr.original_code = ERROR_MARK;
7241 expr.original_type = NULL;
7242 break;
7243 case CPP_MINUS_MINUS:
7244 /* Postdecrement. */
7245 c_parser_consume_token (parser);
7246 expr = default_function_array_read_conversion (expr_loc, expr);
7247 expr.value = build_unary_op (op_loc,
7248 POSTDECREMENT_EXPR, expr.value, 0);
7249 expr.original_code = ERROR_MARK;
7250 expr.original_type = NULL;
7251 break;
7252 default:
7253 return expr;
7258 /* Parse an expression (C90 6.3.17, C99 6.5.17).
7260 expression:
7261 assignment-expression
7262 expression , assignment-expression
7265 static struct c_expr
7266 c_parser_expression (c_parser *parser)
7268 struct c_expr expr;
7269 expr = c_parser_expr_no_commas (parser, NULL);
7270 while (c_parser_next_token_is (parser, CPP_COMMA))
7272 struct c_expr next;
7273 tree lhsval;
7274 location_t loc = c_parser_peek_token (parser)->location;
7275 location_t expr_loc;
7276 c_parser_consume_token (parser);
7277 expr_loc = c_parser_peek_token (parser)->location;
7278 lhsval = expr.value;
7279 while (TREE_CODE (lhsval) == COMPOUND_EXPR)
7280 lhsval = TREE_OPERAND (lhsval, 1);
7281 if (DECL_P (lhsval) || handled_component_p (lhsval))
7282 mark_exp_read (lhsval);
7283 next = c_parser_expr_no_commas (parser, NULL);
7284 next = default_function_array_conversion (expr_loc, next);
7285 expr.value = build_compound_expr (loc, expr.value, next.value);
7286 expr.original_code = COMPOUND_EXPR;
7287 expr.original_type = next.original_type;
7289 return expr;
7292 /* Parse an expression and convert functions or arrays to
7293 pointers. */
7295 static struct c_expr
7296 c_parser_expression_conv (c_parser *parser)
7298 struct c_expr expr;
7299 location_t loc = c_parser_peek_token (parser)->location;
7300 expr = c_parser_expression (parser);
7301 expr = default_function_array_conversion (loc, expr);
7302 return expr;
7305 /* Parse a non-empty list of expressions. If CONVERT_P, convert
7306 functions and arrays to pointers. If FOLD_P, fold the expressions.
7308 nonempty-expr-list:
7309 assignment-expression
7310 nonempty-expr-list , assignment-expression
7313 static VEC(tree,gc) *
7314 c_parser_expr_list (c_parser *parser, bool convert_p, bool fold_p,
7315 VEC(tree,gc) **p_orig_types,
7316 struct c_tree_loc_pair *sizeof_arg)
7318 VEC(tree,gc) *ret;
7319 VEC(tree,gc) *orig_types;
7320 struct c_expr expr;
7321 location_t loc = c_parser_peek_token (parser)->location;
7322 location_t sizeof_arg_loc = UNKNOWN_LOCATION;
7324 ret = make_tree_vector ();
7325 if (p_orig_types == NULL)
7326 orig_types = NULL;
7327 else
7328 orig_types = make_tree_vector ();
7330 if (sizeof_arg != NULL
7331 && c_parser_next_token_is_keyword (parser, RID_SIZEOF))
7332 sizeof_arg_loc = c_parser_peek_2nd_token (parser)->location;
7333 expr = c_parser_expr_no_commas (parser, NULL);
7334 if (convert_p)
7335 expr = default_function_array_read_conversion (loc, expr);
7336 if (fold_p)
7337 expr.value = c_fully_fold (expr.value, false, NULL);
7338 VEC_quick_push (tree, ret, expr.value);
7339 if (orig_types != NULL)
7340 VEC_quick_push (tree, orig_types, expr.original_type);
7341 while (c_parser_next_token_is (parser, CPP_COMMA))
7343 c_parser_consume_token (parser);
7344 loc = c_parser_peek_token (parser)->location;
7345 if (sizeof_arg != NULL
7346 && c_parser_next_token_is_keyword (parser, RID_SIZEOF))
7347 sizeof_arg_loc = c_parser_peek_2nd_token (parser)->location;
7348 else
7349 sizeof_arg_loc = UNKNOWN_LOCATION;
7350 expr = c_parser_expr_no_commas (parser, NULL);
7351 if (convert_p)
7352 expr = default_function_array_read_conversion (loc, expr);
7353 if (fold_p)
7354 expr.value = c_fully_fold (expr.value, false, NULL);
7355 VEC_safe_push (tree, gc, ret, expr.value);
7356 if (orig_types != NULL)
7357 VEC_safe_push (tree, gc, orig_types, expr.original_type);
7359 if (sizeof_arg != NULL)
7361 if (sizeof_arg_loc != UNKNOWN_LOCATION
7362 && expr.original_code == SIZEOF_EXPR)
7364 sizeof_arg->expr = c_last_sizeof_arg;
7365 sizeof_arg->loc = sizeof_arg_loc;
7367 else
7369 sizeof_arg->expr = NULL_TREE;
7370 sizeof_arg->loc = UNKNOWN_LOCATION;
7373 if (orig_types != NULL)
7374 *p_orig_types = orig_types;
7375 return ret;
7378 /* Parse Objective-C-specific constructs. */
7380 /* Parse an objc-class-definition.
7382 objc-class-definition:
7383 @interface identifier objc-superclass[opt] objc-protocol-refs[opt]
7384 objc-class-instance-variables[opt] objc-methodprotolist @end
7385 @implementation identifier objc-superclass[opt]
7386 objc-class-instance-variables[opt]
7387 @interface identifier ( identifier ) objc-protocol-refs[opt]
7388 objc-methodprotolist @end
7389 @interface identifier ( ) objc-protocol-refs[opt]
7390 objc-methodprotolist @end
7391 @implementation identifier ( identifier )
7393 objc-superclass:
7394 : identifier
7396 "@interface identifier (" must start "@interface identifier (
7397 identifier ) ...": objc-methodprotolist in the first production may
7398 not start with a parenthesized identifier as a declarator of a data
7399 definition with no declaration specifiers if the objc-superclass,
7400 objc-protocol-refs and objc-class-instance-variables are omitted. */
7402 static void
7403 c_parser_objc_class_definition (c_parser *parser, tree attributes)
7405 bool iface_p;
7406 tree id1;
7407 tree superclass;
7408 if (c_parser_next_token_is_keyword (parser, RID_AT_INTERFACE))
7409 iface_p = true;
7410 else if (c_parser_next_token_is_keyword (parser, RID_AT_IMPLEMENTATION))
7411 iface_p = false;
7412 else
7413 gcc_unreachable ();
7415 c_parser_consume_token (parser);
7416 if (c_parser_next_token_is_not (parser, CPP_NAME))
7418 c_parser_error (parser, "expected identifier");
7419 return;
7421 id1 = c_parser_peek_token (parser)->value;
7422 c_parser_consume_token (parser);
7423 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
7425 /* We have a category or class extension. */
7426 tree id2;
7427 tree proto = NULL_TREE;
7428 c_parser_consume_token (parser);
7429 if (c_parser_next_token_is_not (parser, CPP_NAME))
7431 if (iface_p && c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
7433 /* We have a class extension. */
7434 id2 = NULL_TREE;
7436 else
7438 c_parser_error (parser, "expected identifier or %<)%>");
7439 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7440 return;
7443 else
7445 id2 = c_parser_peek_token (parser)->value;
7446 c_parser_consume_token (parser);
7448 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
7449 if (!iface_p)
7451 objc_start_category_implementation (id1, id2);
7452 return;
7454 if (c_parser_next_token_is (parser, CPP_LESS))
7455 proto = c_parser_objc_protocol_refs (parser);
7456 objc_start_category_interface (id1, id2, proto, attributes);
7457 c_parser_objc_methodprotolist (parser);
7458 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
7459 objc_finish_interface ();
7460 return;
7462 if (c_parser_next_token_is (parser, CPP_COLON))
7464 c_parser_consume_token (parser);
7465 if (c_parser_next_token_is_not (parser, CPP_NAME))
7467 c_parser_error (parser, "expected identifier");
7468 return;
7470 superclass = c_parser_peek_token (parser)->value;
7471 c_parser_consume_token (parser);
7473 else
7474 superclass = NULL_TREE;
7475 if (iface_p)
7477 tree proto = NULL_TREE;
7478 if (c_parser_next_token_is (parser, CPP_LESS))
7479 proto = c_parser_objc_protocol_refs (parser);
7480 objc_start_class_interface (id1, superclass, proto, attributes);
7482 else
7483 objc_start_class_implementation (id1, superclass);
7484 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
7485 c_parser_objc_class_instance_variables (parser);
7486 if (iface_p)
7488 objc_continue_interface ();
7489 c_parser_objc_methodprotolist (parser);
7490 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
7491 objc_finish_interface ();
7493 else
7495 objc_continue_implementation ();
7496 return;
7500 /* Parse objc-class-instance-variables.
7502 objc-class-instance-variables:
7503 { objc-instance-variable-decl-list[opt] }
7505 objc-instance-variable-decl-list:
7506 objc-visibility-spec
7507 objc-instance-variable-decl ;
7509 objc-instance-variable-decl-list objc-visibility-spec
7510 objc-instance-variable-decl-list objc-instance-variable-decl ;
7511 objc-instance-variable-decl-list ;
7513 objc-visibility-spec:
7514 @private
7515 @protected
7516 @public
7518 objc-instance-variable-decl:
7519 struct-declaration
7522 static void
7523 c_parser_objc_class_instance_variables (c_parser *parser)
7525 gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
7526 c_parser_consume_token (parser);
7527 while (c_parser_next_token_is_not (parser, CPP_EOF))
7529 tree decls;
7530 /* Parse any stray semicolon. */
7531 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
7533 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
7534 "extra semicolon");
7535 c_parser_consume_token (parser);
7536 continue;
7538 /* Stop if at the end of the instance variables. */
7539 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
7541 c_parser_consume_token (parser);
7542 break;
7544 /* Parse any objc-visibility-spec. */
7545 if (c_parser_next_token_is_keyword (parser, RID_AT_PRIVATE))
7547 c_parser_consume_token (parser);
7548 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
7549 continue;
7551 else if (c_parser_next_token_is_keyword (parser, RID_AT_PROTECTED))
7553 c_parser_consume_token (parser);
7554 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
7555 continue;
7557 else if (c_parser_next_token_is_keyword (parser, RID_AT_PUBLIC))
7559 c_parser_consume_token (parser);
7560 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
7561 continue;
7563 else if (c_parser_next_token_is_keyword (parser, RID_AT_PACKAGE))
7565 c_parser_consume_token (parser);
7566 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
7567 continue;
7569 else if (c_parser_next_token_is (parser, CPP_PRAGMA))
7571 c_parser_pragma (parser, pragma_external);
7572 continue;
7575 /* Parse some comma-separated declarations. */
7576 decls = c_parser_struct_declaration (parser);
7577 if (decls == NULL)
7579 /* There is a syntax error. We want to skip the offending
7580 tokens up to the next ';' (included) or '}'
7581 (excluded). */
7583 /* First, skip manually a ')' or ']'. This is because they
7584 reduce the nesting level, so c_parser_skip_until_found()
7585 wouldn't be able to skip past them. */
7586 c_token *token = c_parser_peek_token (parser);
7587 if (token->type == CPP_CLOSE_PAREN || token->type == CPP_CLOSE_SQUARE)
7588 c_parser_consume_token (parser);
7590 /* Then, do the standard skipping. */
7591 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
7593 /* We hopefully recovered. Start normal parsing again. */
7594 parser->error = false;
7595 continue;
7597 else
7599 /* Comma-separated instance variables are chained together
7600 in reverse order; add them one by one. */
7601 tree ivar = nreverse (decls);
7602 for (; ivar; ivar = DECL_CHAIN (ivar))
7603 objc_add_instance_variable (copy_node (ivar));
7605 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
7609 /* Parse an objc-class-declaration.
7611 objc-class-declaration:
7612 @class identifier-list ;
7615 static void
7616 c_parser_objc_class_declaration (c_parser *parser)
7618 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_CLASS));
7619 c_parser_consume_token (parser);
7620 /* Any identifiers, including those declared as type names, are OK
7621 here. */
7622 while (true)
7624 tree id;
7625 if (c_parser_next_token_is_not (parser, CPP_NAME))
7627 c_parser_error (parser, "expected identifier");
7628 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
7629 parser->error = false;
7630 return;
7632 id = c_parser_peek_token (parser)->value;
7633 objc_declare_class (id);
7634 c_parser_consume_token (parser);
7635 if (c_parser_next_token_is (parser, CPP_COMMA))
7636 c_parser_consume_token (parser);
7637 else
7638 break;
7640 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
7643 /* Parse an objc-alias-declaration.
7645 objc-alias-declaration:
7646 @compatibility_alias identifier identifier ;
7649 static void
7650 c_parser_objc_alias_declaration (c_parser *parser)
7652 tree id1, id2;
7653 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_ALIAS));
7654 c_parser_consume_token (parser);
7655 if (c_parser_next_token_is_not (parser, CPP_NAME))
7657 c_parser_error (parser, "expected identifier");
7658 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
7659 return;
7661 id1 = c_parser_peek_token (parser)->value;
7662 c_parser_consume_token (parser);
7663 if (c_parser_next_token_is_not (parser, CPP_NAME))
7665 c_parser_error (parser, "expected identifier");
7666 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
7667 return;
7669 id2 = c_parser_peek_token (parser)->value;
7670 c_parser_consume_token (parser);
7671 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
7672 objc_declare_alias (id1, id2);
7675 /* Parse an objc-protocol-definition.
7677 objc-protocol-definition:
7678 @protocol identifier objc-protocol-refs[opt] objc-methodprotolist @end
7679 @protocol identifier-list ;
7681 "@protocol identifier ;" should be resolved as "@protocol
7682 identifier-list ;": objc-methodprotolist may not start with a
7683 semicolon in the first alternative if objc-protocol-refs are
7684 omitted. */
7686 static void
7687 c_parser_objc_protocol_definition (c_parser *parser, tree attributes)
7689 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_PROTOCOL));
7691 c_parser_consume_token (parser);
7692 if (c_parser_next_token_is_not (parser, CPP_NAME))
7694 c_parser_error (parser, "expected identifier");
7695 return;
7697 if (c_parser_peek_2nd_token (parser)->type == CPP_COMMA
7698 || c_parser_peek_2nd_token (parser)->type == CPP_SEMICOLON)
7700 /* Any identifiers, including those declared as type names, are
7701 OK here. */
7702 while (true)
7704 tree id;
7705 if (c_parser_next_token_is_not (parser, CPP_NAME))
7707 c_parser_error (parser, "expected identifier");
7708 break;
7710 id = c_parser_peek_token (parser)->value;
7711 objc_declare_protocol (id, attributes);
7712 c_parser_consume_token (parser);
7713 if (c_parser_next_token_is (parser, CPP_COMMA))
7714 c_parser_consume_token (parser);
7715 else
7716 break;
7718 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
7720 else
7722 tree id = c_parser_peek_token (parser)->value;
7723 tree proto = NULL_TREE;
7724 c_parser_consume_token (parser);
7725 if (c_parser_next_token_is (parser, CPP_LESS))
7726 proto = c_parser_objc_protocol_refs (parser);
7727 parser->objc_pq_context = true;
7728 objc_start_protocol (id, proto, attributes);
7729 c_parser_objc_methodprotolist (parser);
7730 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
7731 parser->objc_pq_context = false;
7732 objc_finish_interface ();
7736 /* Parse an objc-method-type.
7738 objc-method-type:
7742 Return true if it is a class method (+) and false if it is
7743 an instance method (-).
7745 static inline bool
7746 c_parser_objc_method_type (c_parser *parser)
7748 switch (c_parser_peek_token (parser)->type)
7750 case CPP_PLUS:
7751 c_parser_consume_token (parser);
7752 return true;
7753 case CPP_MINUS:
7754 c_parser_consume_token (parser);
7755 return false;
7756 default:
7757 gcc_unreachable ();
7761 /* Parse an objc-method-definition.
7763 objc-method-definition:
7764 objc-method-type objc-method-decl ;[opt] compound-statement
7767 static void
7768 c_parser_objc_method_definition (c_parser *parser)
7770 bool is_class_method = c_parser_objc_method_type (parser);
7771 tree decl, attributes = NULL_TREE, expr = NULL_TREE;
7772 parser->objc_pq_context = true;
7773 decl = c_parser_objc_method_decl (parser, is_class_method, &attributes,
7774 &expr);
7775 if (decl == error_mark_node)
7776 return; /* Bail here. */
7778 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
7780 c_parser_consume_token (parser);
7781 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
7782 "extra semicolon in method definition specified");
7785 if (!c_parser_next_token_is (parser, CPP_OPEN_BRACE))
7787 c_parser_error (parser, "expected %<{%>");
7788 return;
7791 parser->objc_pq_context = false;
7792 if (objc_start_method_definition (is_class_method, decl, attributes, expr))
7794 add_stmt (c_parser_compound_statement (parser));
7795 objc_finish_method_definition (current_function_decl);
7797 else
7799 /* This code is executed when we find a method definition
7800 outside of an @implementation context (or invalid for other
7801 reasons). Parse the method (to keep going) but do not emit
7802 any code.
7804 c_parser_compound_statement (parser);
7808 /* Parse an objc-methodprotolist.
7810 objc-methodprotolist:
7811 empty
7812 objc-methodprotolist objc-methodproto
7813 objc-methodprotolist declaration
7814 objc-methodprotolist ;
7815 @optional
7816 @required
7818 The declaration is a data definition, which may be missing
7819 declaration specifiers under the same rules and diagnostics as
7820 other data definitions outside functions, and the stray semicolon
7821 is diagnosed the same way as a stray semicolon outside a
7822 function. */
7824 static void
7825 c_parser_objc_methodprotolist (c_parser *parser)
7827 while (true)
7829 /* The list is terminated by @end. */
7830 switch (c_parser_peek_token (parser)->type)
7832 case CPP_SEMICOLON:
7833 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
7834 "ISO C does not allow extra %<;%> outside of a function");
7835 c_parser_consume_token (parser);
7836 break;
7837 case CPP_PLUS:
7838 case CPP_MINUS:
7839 c_parser_objc_methodproto (parser);
7840 break;
7841 case CPP_PRAGMA:
7842 c_parser_pragma (parser, pragma_external);
7843 break;
7844 case CPP_EOF:
7845 return;
7846 default:
7847 if (c_parser_next_token_is_keyword (parser, RID_AT_END))
7848 return;
7849 else if (c_parser_next_token_is_keyword (parser, RID_AT_PROPERTY))
7850 c_parser_objc_at_property_declaration (parser);
7851 else if (c_parser_next_token_is_keyword (parser, RID_AT_OPTIONAL))
7853 objc_set_method_opt (true);
7854 c_parser_consume_token (parser);
7856 else if (c_parser_next_token_is_keyword (parser, RID_AT_REQUIRED))
7858 objc_set_method_opt (false);
7859 c_parser_consume_token (parser);
7861 else
7862 c_parser_declaration_or_fndef (parser, false, false, true,
7863 false, true, NULL);
7864 break;
7869 /* Parse an objc-methodproto.
7871 objc-methodproto:
7872 objc-method-type objc-method-decl ;
7875 static void
7876 c_parser_objc_methodproto (c_parser *parser)
7878 bool is_class_method = c_parser_objc_method_type (parser);
7879 tree decl, attributes = NULL_TREE;
7881 /* Remember protocol qualifiers in prototypes. */
7882 parser->objc_pq_context = true;
7883 decl = c_parser_objc_method_decl (parser, is_class_method, &attributes,
7884 NULL);
7885 /* Forget protocol qualifiers now. */
7886 parser->objc_pq_context = false;
7888 /* Do not allow the presence of attributes to hide an erroneous
7889 method implementation in the interface section. */
7890 if (!c_parser_next_token_is (parser, CPP_SEMICOLON))
7892 c_parser_error (parser, "expected %<;%>");
7893 return;
7896 if (decl != error_mark_node)
7897 objc_add_method_declaration (is_class_method, decl, attributes);
7899 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
7902 /* If we are at a position that method attributes may be present, check that
7903 there are not any parsed already (a syntax error) and then collect any
7904 specified at the current location. Finally, if new attributes were present,
7905 check that the next token is legal ( ';' for decls and '{' for defs). */
7907 static bool
7908 c_parser_objc_maybe_method_attributes (c_parser* parser, tree* attributes)
7910 bool bad = false;
7911 if (*attributes)
7913 c_parser_error (parser,
7914 "method attributes must be specified at the end only");
7915 *attributes = NULL_TREE;
7916 bad = true;
7919 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
7920 *attributes = c_parser_attributes (parser);
7922 /* If there were no attributes here, just report any earlier error. */
7923 if (*attributes == NULL_TREE || bad)
7924 return bad;
7926 /* If the attributes are followed by a ; or {, then just report any earlier
7927 error. */
7928 if (c_parser_next_token_is (parser, CPP_SEMICOLON)
7929 || c_parser_next_token_is (parser, CPP_OPEN_BRACE))
7930 return bad;
7932 /* We've got attributes, but not at the end. */
7933 c_parser_error (parser,
7934 "expected %<;%> or %<{%> after method attribute definition");
7935 return true;
7938 /* Parse an objc-method-decl.
7940 objc-method-decl:
7941 ( objc-type-name ) objc-selector
7942 objc-selector
7943 ( objc-type-name ) objc-keyword-selector objc-optparmlist
7944 objc-keyword-selector objc-optparmlist
7945 attributes
7947 objc-keyword-selector:
7948 objc-keyword-decl
7949 objc-keyword-selector objc-keyword-decl
7951 objc-keyword-decl:
7952 objc-selector : ( objc-type-name ) identifier
7953 objc-selector : identifier
7954 : ( objc-type-name ) identifier
7955 : identifier
7957 objc-optparmlist:
7958 objc-optparms objc-optellipsis
7960 objc-optparms:
7961 empty
7962 objc-opt-parms , parameter-declaration
7964 objc-optellipsis:
7965 empty
7966 , ...
7969 static tree
7970 c_parser_objc_method_decl (c_parser *parser, bool is_class_method,
7971 tree *attributes, tree *expr)
7973 tree type = NULL_TREE;
7974 tree sel;
7975 tree parms = NULL_TREE;
7976 bool ellipsis = false;
7977 bool attr_err = false;
7979 *attributes = NULL_TREE;
7980 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
7982 c_parser_consume_token (parser);
7983 type = c_parser_objc_type_name (parser);
7984 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
7986 sel = c_parser_objc_selector (parser);
7987 /* If there is no selector, or a colon follows, we have an
7988 objc-keyword-selector. If there is a selector, and a colon does
7989 not follow, that selector ends the objc-method-decl. */
7990 if (!sel || c_parser_next_token_is (parser, CPP_COLON))
7992 tree tsel = sel;
7993 tree list = NULL_TREE;
7994 while (true)
7996 tree atype = NULL_TREE, id, keyworddecl;
7997 tree param_attr = NULL_TREE;
7998 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
7999 break;
8000 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
8002 c_parser_consume_token (parser);
8003 atype = c_parser_objc_type_name (parser);
8004 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
8005 "expected %<)%>");
8007 /* New ObjC allows attributes on method parameters. */
8008 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
8009 param_attr = c_parser_attributes (parser);
8010 if (c_parser_next_token_is_not (parser, CPP_NAME))
8012 c_parser_error (parser, "expected identifier");
8013 return error_mark_node;
8015 id = c_parser_peek_token (parser)->value;
8016 c_parser_consume_token (parser);
8017 keyworddecl = objc_build_keyword_decl (tsel, atype, id, param_attr);
8018 list = chainon (list, keyworddecl);
8019 tsel = c_parser_objc_selector (parser);
8020 if (!tsel && c_parser_next_token_is_not (parser, CPP_COLON))
8021 break;
8024 attr_err |= c_parser_objc_maybe_method_attributes (parser, attributes) ;
8026 /* Parse the optional parameter list. Optional Objective-C
8027 method parameters follow the C syntax, and may include '...'
8028 to denote a variable number of arguments. */
8029 parms = make_node (TREE_LIST);
8030 while (c_parser_next_token_is (parser, CPP_COMMA))
8032 struct c_parm *parm;
8033 c_parser_consume_token (parser);
8034 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
8036 ellipsis = true;
8037 c_parser_consume_token (parser);
8038 attr_err |= c_parser_objc_maybe_method_attributes
8039 (parser, attributes) ;
8040 break;
8042 parm = c_parser_parameter_declaration (parser, NULL_TREE);
8043 if (parm == NULL)
8044 break;
8045 parms = chainon (parms,
8046 build_tree_list (NULL_TREE, grokparm (parm, expr)));
8048 sel = list;
8050 else
8051 attr_err |= c_parser_objc_maybe_method_attributes (parser, attributes) ;
8053 if (sel == NULL)
8055 c_parser_error (parser, "objective-c method declaration is expected");
8056 return error_mark_node;
8059 if (attr_err)
8060 return error_mark_node;
8062 return objc_build_method_signature (is_class_method, type, sel, parms, ellipsis);
8065 /* Parse an objc-type-name.
8067 objc-type-name:
8068 objc-type-qualifiers[opt] type-name
8069 objc-type-qualifiers[opt]
8071 objc-type-qualifiers:
8072 objc-type-qualifier
8073 objc-type-qualifiers objc-type-qualifier
8075 objc-type-qualifier: one of
8076 in out inout bycopy byref oneway
8079 static tree
8080 c_parser_objc_type_name (c_parser *parser)
8082 tree quals = NULL_TREE;
8083 struct c_type_name *type_name = NULL;
8084 tree type = NULL_TREE;
8085 while (true)
8087 c_token *token = c_parser_peek_token (parser);
8088 if (token->type == CPP_KEYWORD
8089 && (token->keyword == RID_IN
8090 || token->keyword == RID_OUT
8091 || token->keyword == RID_INOUT
8092 || token->keyword == RID_BYCOPY
8093 || token->keyword == RID_BYREF
8094 || token->keyword == RID_ONEWAY))
8096 quals = chainon (build_tree_list (NULL_TREE, token->value), quals);
8097 c_parser_consume_token (parser);
8099 else
8100 break;
8102 if (c_parser_next_tokens_start_typename (parser, cla_prefer_type))
8103 type_name = c_parser_type_name (parser);
8104 if (type_name)
8105 type = groktypename (type_name, NULL, NULL);
8107 /* If the type is unknown, and error has already been produced and
8108 we need to recover from the error. In that case, use NULL_TREE
8109 for the type, as if no type had been specified; this will use the
8110 default type ('id') which is good for error recovery. */
8111 if (type == error_mark_node)
8112 type = NULL_TREE;
8114 return build_tree_list (quals, type);
8117 /* Parse objc-protocol-refs.
8119 objc-protocol-refs:
8120 < identifier-list >
8123 static tree
8124 c_parser_objc_protocol_refs (c_parser *parser)
8126 tree list = NULL_TREE;
8127 gcc_assert (c_parser_next_token_is (parser, CPP_LESS));
8128 c_parser_consume_token (parser);
8129 /* Any identifiers, including those declared as type names, are OK
8130 here. */
8131 while (true)
8133 tree id;
8134 if (c_parser_next_token_is_not (parser, CPP_NAME))
8136 c_parser_error (parser, "expected identifier");
8137 break;
8139 id = c_parser_peek_token (parser)->value;
8140 list = chainon (list, build_tree_list (NULL_TREE, id));
8141 c_parser_consume_token (parser);
8142 if (c_parser_next_token_is (parser, CPP_COMMA))
8143 c_parser_consume_token (parser);
8144 else
8145 break;
8147 c_parser_require (parser, CPP_GREATER, "expected %<>%>");
8148 return list;
8151 /* Parse an objc-try-catch-finally-statement.
8153 objc-try-catch-finally-statement:
8154 @try compound-statement objc-catch-list[opt]
8155 @try compound-statement objc-catch-list[opt] @finally compound-statement
8157 objc-catch-list:
8158 @catch ( objc-catch-parameter-declaration ) compound-statement
8159 objc-catch-list @catch ( objc-catch-parameter-declaration ) compound-statement
8161 objc-catch-parameter-declaration:
8162 parameter-declaration
8163 '...'
8165 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
8167 PS: This function is identical to cp_parser_objc_try_catch_finally_statement
8168 for C++. Keep them in sync. */
8170 static void
8171 c_parser_objc_try_catch_finally_statement (c_parser *parser)
8173 location_t location;
8174 tree stmt;
8176 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_TRY));
8177 c_parser_consume_token (parser);
8178 location = c_parser_peek_token (parser)->location;
8179 objc_maybe_warn_exceptions (location);
8180 stmt = c_parser_compound_statement (parser);
8181 objc_begin_try_stmt (location, stmt);
8183 while (c_parser_next_token_is_keyword (parser, RID_AT_CATCH))
8185 struct c_parm *parm;
8186 tree parameter_declaration = error_mark_node;
8187 bool seen_open_paren = false;
8189 c_parser_consume_token (parser);
8190 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
8191 seen_open_paren = true;
8192 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
8194 /* We have "@catch (...)" (where the '...' are literally
8195 what is in the code). Skip the '...'.
8196 parameter_declaration is set to NULL_TREE, and
8197 objc_being_catch_clauses() knows that that means
8198 '...'. */
8199 c_parser_consume_token (parser);
8200 parameter_declaration = NULL_TREE;
8202 else
8204 /* We have "@catch (NSException *exception)" or something
8205 like that. Parse the parameter declaration. */
8206 parm = c_parser_parameter_declaration (parser, NULL_TREE);
8207 if (parm == NULL)
8208 parameter_declaration = error_mark_node;
8209 else
8210 parameter_declaration = grokparm (parm, NULL);
8212 if (seen_open_paren)
8213 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8214 else
8216 /* If there was no open parenthesis, we are recovering from
8217 an error, and we are trying to figure out what mistake
8218 the user has made. */
8220 /* If there is an immediate closing parenthesis, the user
8221 probably forgot the opening one (ie, they typed "@catch
8222 NSException *e)". Parse the closing parenthesis and keep
8223 going. */
8224 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
8225 c_parser_consume_token (parser);
8227 /* If these is no immediate closing parenthesis, the user
8228 probably doesn't know that parenthesis are required at
8229 all (ie, they typed "@catch NSException *e"). So, just
8230 forget about the closing parenthesis and keep going. */
8232 objc_begin_catch_clause (parameter_declaration);
8233 if (c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
8234 c_parser_compound_statement_nostart (parser);
8235 objc_finish_catch_clause ();
8237 if (c_parser_next_token_is_keyword (parser, RID_AT_FINALLY))
8239 c_parser_consume_token (parser);
8240 location = c_parser_peek_token (parser)->location;
8241 stmt = c_parser_compound_statement (parser);
8242 objc_build_finally_clause (location, stmt);
8244 objc_finish_try_stmt ();
8247 /* Parse an objc-synchronized-statement.
8249 objc-synchronized-statement:
8250 @synchronized ( expression ) compound-statement
8253 static void
8254 c_parser_objc_synchronized_statement (c_parser *parser)
8256 location_t loc;
8257 tree expr, stmt;
8258 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_SYNCHRONIZED));
8259 c_parser_consume_token (parser);
8260 loc = c_parser_peek_token (parser)->location;
8261 objc_maybe_warn_exceptions (loc);
8262 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
8264 expr = c_parser_expression (parser).value;
8265 expr = c_fully_fold (expr, false, NULL);
8266 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8268 else
8269 expr = error_mark_node;
8270 stmt = c_parser_compound_statement (parser);
8271 objc_build_synchronized (loc, expr, stmt);
8274 /* Parse an objc-selector; return NULL_TREE without an error if the
8275 next token is not an objc-selector.
8277 objc-selector:
8278 identifier
8279 one of
8280 enum struct union if else while do for switch case default
8281 break continue return goto asm sizeof typeof __alignof
8282 unsigned long const short volatile signed restrict _Complex
8283 in out inout bycopy byref oneway int char float double void _Bool
8285 ??? Why this selection of keywords but not, for example, storage
8286 class specifiers? */
8288 static tree
8289 c_parser_objc_selector (c_parser *parser)
8291 c_token *token = c_parser_peek_token (parser);
8292 tree value = token->value;
8293 if (token->type == CPP_NAME)
8295 c_parser_consume_token (parser);
8296 return value;
8298 if (token->type != CPP_KEYWORD)
8299 return NULL_TREE;
8300 switch (token->keyword)
8302 case RID_ENUM:
8303 case RID_STRUCT:
8304 case RID_UNION:
8305 case RID_IF:
8306 case RID_ELSE:
8307 case RID_WHILE:
8308 case RID_DO:
8309 case RID_FOR:
8310 case RID_SWITCH:
8311 case RID_CASE:
8312 case RID_DEFAULT:
8313 case RID_BREAK:
8314 case RID_CONTINUE:
8315 case RID_RETURN:
8316 case RID_GOTO:
8317 case RID_ASM:
8318 case RID_SIZEOF:
8319 case RID_TYPEOF:
8320 case RID_ALIGNOF:
8321 case RID_UNSIGNED:
8322 case RID_LONG:
8323 case RID_INT128:
8324 case RID_CONST:
8325 case RID_SHORT:
8326 case RID_VOLATILE:
8327 case RID_SIGNED:
8328 case RID_RESTRICT:
8329 case RID_COMPLEX:
8330 case RID_IN:
8331 case RID_OUT:
8332 case RID_INOUT:
8333 case RID_BYCOPY:
8334 case RID_BYREF:
8335 case RID_ONEWAY:
8336 case RID_INT:
8337 case RID_CHAR:
8338 case RID_FLOAT:
8339 case RID_DOUBLE:
8340 case RID_VOID:
8341 case RID_BOOL:
8342 c_parser_consume_token (parser);
8343 return value;
8344 default:
8345 return NULL_TREE;
8349 /* Parse an objc-selector-arg.
8351 objc-selector-arg:
8352 objc-selector
8353 objc-keywordname-list
8355 objc-keywordname-list:
8356 objc-keywordname
8357 objc-keywordname-list objc-keywordname
8359 objc-keywordname:
8360 objc-selector :
8364 static tree
8365 c_parser_objc_selector_arg (c_parser *parser)
8367 tree sel = c_parser_objc_selector (parser);
8368 tree list = NULL_TREE;
8369 if (sel && c_parser_next_token_is_not (parser, CPP_COLON))
8370 return sel;
8371 while (true)
8373 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
8374 return list;
8375 list = chainon (list, build_tree_list (sel, NULL_TREE));
8376 sel = c_parser_objc_selector (parser);
8377 if (!sel && c_parser_next_token_is_not (parser, CPP_COLON))
8378 break;
8380 return list;
8383 /* Parse an objc-receiver.
8385 objc-receiver:
8386 expression
8387 class-name
8388 type-name
8391 static tree
8392 c_parser_objc_receiver (c_parser *parser)
8394 if (c_parser_peek_token (parser)->type == CPP_NAME
8395 && (c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME
8396 || c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME))
8398 tree id = c_parser_peek_token (parser)->value;
8399 c_parser_consume_token (parser);
8400 return objc_get_class_reference (id);
8402 return c_fully_fold (c_parser_expression (parser).value, false, NULL);
8405 /* Parse objc-message-args.
8407 objc-message-args:
8408 objc-selector
8409 objc-keywordarg-list
8411 objc-keywordarg-list:
8412 objc-keywordarg
8413 objc-keywordarg-list objc-keywordarg
8415 objc-keywordarg:
8416 objc-selector : objc-keywordexpr
8417 : objc-keywordexpr
8420 static tree
8421 c_parser_objc_message_args (c_parser *parser)
8423 tree sel = c_parser_objc_selector (parser);
8424 tree list = NULL_TREE;
8425 if (sel && c_parser_next_token_is_not (parser, CPP_COLON))
8426 return sel;
8427 while (true)
8429 tree keywordexpr;
8430 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
8431 return error_mark_node;
8432 keywordexpr = c_parser_objc_keywordexpr (parser);
8433 list = chainon (list, build_tree_list (sel, keywordexpr));
8434 sel = c_parser_objc_selector (parser);
8435 if (!sel && c_parser_next_token_is_not (parser, CPP_COLON))
8436 break;
8438 return list;
8441 /* Parse an objc-keywordexpr.
8443 objc-keywordexpr:
8444 nonempty-expr-list
8447 static tree
8448 c_parser_objc_keywordexpr (c_parser *parser)
8450 tree ret;
8451 VEC(tree,gc) *expr_list = c_parser_expr_list (parser, true, true,
8452 NULL, NULL);
8453 if (VEC_length (tree, expr_list) == 1)
8455 /* Just return the expression, remove a level of
8456 indirection. */
8457 ret = VEC_index (tree, expr_list, 0);
8459 else
8461 /* We have a comma expression, we will collapse later. */
8462 ret = build_tree_list_vec (expr_list);
8464 release_tree_vector (expr_list);
8465 return ret;
8468 /* A check, needed in several places, that ObjC interface, implementation or
8469 method definitions are not prefixed by incorrect items. */
8470 static bool
8471 c_parser_objc_diagnose_bad_element_prefix (c_parser *parser,
8472 struct c_declspecs *specs)
8474 if (!specs->declspecs_seen_p || specs->non_sc_seen_p
8475 || specs->typespec_kind != ctsk_none)
8477 c_parser_error (parser,
8478 "no type or storage class may be specified here,");
8479 c_parser_skip_to_end_of_block_or_statement (parser);
8480 return true;
8482 return false;
8485 /* Parse an Objective-C @property declaration. The syntax is:
8487 objc-property-declaration:
8488 '@property' objc-property-attributes[opt] struct-declaration ;
8490 objc-property-attributes:
8491 '(' objc-property-attribute-list ')'
8493 objc-property-attribute-list:
8494 objc-property-attribute
8495 objc-property-attribute-list, objc-property-attribute
8497 objc-property-attribute
8498 'getter' = identifier
8499 'setter' = identifier
8500 'readonly'
8501 'readwrite'
8502 'assign'
8503 'retain'
8504 'copy'
8505 'nonatomic'
8507 For example:
8508 @property NSString *name;
8509 @property (readonly) id object;
8510 @property (retain, nonatomic, getter=getTheName) id name;
8511 @property int a, b, c;
8513 PS: This function is identical to cp_parser_objc_at_propery_declaration
8514 for C++. Keep them in sync. */
8515 static void
8516 c_parser_objc_at_property_declaration (c_parser *parser)
8518 /* The following variables hold the attributes of the properties as
8519 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
8520 seen. When we see an attribute, we set them to 'true' (if they
8521 are boolean properties) or to the identifier (if they have an
8522 argument, ie, for getter and setter). Note that here we only
8523 parse the list of attributes, check the syntax and accumulate the
8524 attributes that we find. objc_add_property_declaration() will
8525 then process the information. */
8526 bool property_assign = false;
8527 bool property_copy = false;
8528 tree property_getter_ident = NULL_TREE;
8529 bool property_nonatomic = false;
8530 bool property_readonly = false;
8531 bool property_readwrite = false;
8532 bool property_retain = false;
8533 tree property_setter_ident = NULL_TREE;
8535 /* 'properties' is the list of properties that we read. Usually a
8536 single one, but maybe more (eg, in "@property int a, b, c;" there
8537 are three). */
8538 tree properties;
8539 location_t loc;
8541 loc = c_parser_peek_token (parser)->location;
8542 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_PROPERTY));
8544 c_parser_consume_token (parser); /* Eat '@property'. */
8546 /* Parse the optional attribute list... */
8547 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
8549 /* Eat the '(' */
8550 c_parser_consume_token (parser);
8552 /* Property attribute keywords are valid now. */
8553 parser->objc_property_attr_context = true;
8555 while (true)
8557 bool syntax_error = false;
8558 c_token *token = c_parser_peek_token (parser);
8559 enum rid keyword;
8561 if (token->type != CPP_KEYWORD)
8563 if (token->type == CPP_CLOSE_PAREN)
8564 c_parser_error (parser, "expected identifier");
8565 else
8567 c_parser_consume_token (parser);
8568 c_parser_error (parser, "unknown property attribute");
8570 break;
8572 keyword = token->keyword;
8573 c_parser_consume_token (parser);
8574 switch (keyword)
8576 case RID_ASSIGN: property_assign = true; break;
8577 case RID_COPY: property_copy = true; break;
8578 case RID_NONATOMIC: property_nonatomic = true; break;
8579 case RID_READONLY: property_readonly = true; break;
8580 case RID_READWRITE: property_readwrite = true; break;
8581 case RID_RETAIN: property_retain = true; break;
8583 case RID_GETTER:
8584 case RID_SETTER:
8585 if (c_parser_next_token_is_not (parser, CPP_EQ))
8587 if (keyword == RID_GETTER)
8588 c_parser_error (parser,
8589 "missing %<=%> (after %<getter%> attribute)");
8590 else
8591 c_parser_error (parser,
8592 "missing %<=%> (after %<setter%> attribute)");
8593 syntax_error = true;
8594 break;
8596 c_parser_consume_token (parser); /* eat the = */
8597 if (c_parser_next_token_is_not (parser, CPP_NAME))
8599 c_parser_error (parser, "expected identifier");
8600 syntax_error = true;
8601 break;
8603 if (keyword == RID_SETTER)
8605 if (property_setter_ident != NULL_TREE)
8606 c_parser_error (parser, "the %<setter%> attribute may only be specified once");
8607 else
8608 property_setter_ident = c_parser_peek_token (parser)->value;
8609 c_parser_consume_token (parser);
8610 if (c_parser_next_token_is_not (parser, CPP_COLON))
8611 c_parser_error (parser, "setter name must terminate with %<:%>");
8612 else
8613 c_parser_consume_token (parser);
8615 else
8617 if (property_getter_ident != NULL_TREE)
8618 c_parser_error (parser, "the %<getter%> attribute may only be specified once");
8619 else
8620 property_getter_ident = c_parser_peek_token (parser)->value;
8621 c_parser_consume_token (parser);
8623 break;
8624 default:
8625 c_parser_error (parser, "unknown property attribute");
8626 syntax_error = true;
8627 break;
8630 if (syntax_error)
8631 break;
8633 if (c_parser_next_token_is (parser, CPP_COMMA))
8634 c_parser_consume_token (parser);
8635 else
8636 break;
8638 parser->objc_property_attr_context = false;
8639 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8641 /* ... and the property declaration(s). */
8642 properties = c_parser_struct_declaration (parser);
8644 if (properties == error_mark_node)
8646 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
8647 parser->error = false;
8648 return;
8651 if (properties == NULL_TREE)
8652 c_parser_error (parser, "expected identifier");
8653 else
8655 /* Comma-separated properties are chained together in
8656 reverse order; add them one by one. */
8657 properties = nreverse (properties);
8659 for (; properties; properties = TREE_CHAIN (properties))
8660 objc_add_property_declaration (loc, copy_node (properties),
8661 property_readonly, property_readwrite,
8662 property_assign, property_retain,
8663 property_copy, property_nonatomic,
8664 property_getter_ident, property_setter_ident);
8667 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8668 parser->error = false;
8671 /* Parse an Objective-C @synthesize declaration. The syntax is:
8673 objc-synthesize-declaration:
8674 @synthesize objc-synthesize-identifier-list ;
8676 objc-synthesize-identifier-list:
8677 objc-synthesize-identifier
8678 objc-synthesize-identifier-list, objc-synthesize-identifier
8680 objc-synthesize-identifier
8681 identifier
8682 identifier = identifier
8684 For example:
8685 @synthesize MyProperty;
8686 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
8688 PS: This function is identical to cp_parser_objc_at_synthesize_declaration
8689 for C++. Keep them in sync.
8691 static void
8692 c_parser_objc_at_synthesize_declaration (c_parser *parser)
8694 tree list = NULL_TREE;
8695 location_t loc;
8696 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_SYNTHESIZE));
8697 loc = c_parser_peek_token (parser)->location;
8699 c_parser_consume_token (parser);
8700 while (true)
8702 tree property, ivar;
8703 if (c_parser_next_token_is_not (parser, CPP_NAME))
8705 c_parser_error (parser, "expected identifier");
8706 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
8707 /* Once we find the semicolon, we can resume normal parsing.
8708 We have to reset parser->error manually because
8709 c_parser_skip_until_found() won't reset it for us if the
8710 next token is precisely a semicolon. */
8711 parser->error = false;
8712 return;
8714 property = c_parser_peek_token (parser)->value;
8715 c_parser_consume_token (parser);
8716 if (c_parser_next_token_is (parser, CPP_EQ))
8718 c_parser_consume_token (parser);
8719 if (c_parser_next_token_is_not (parser, CPP_NAME))
8721 c_parser_error (parser, "expected identifier");
8722 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
8723 parser->error = false;
8724 return;
8726 ivar = c_parser_peek_token (parser)->value;
8727 c_parser_consume_token (parser);
8729 else
8730 ivar = NULL_TREE;
8731 list = chainon (list, build_tree_list (ivar, property));
8732 if (c_parser_next_token_is (parser, CPP_COMMA))
8733 c_parser_consume_token (parser);
8734 else
8735 break;
8737 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8738 objc_add_synthesize_declaration (loc, list);
8741 /* Parse an Objective-C @dynamic declaration. The syntax is:
8743 objc-dynamic-declaration:
8744 @dynamic identifier-list ;
8746 For example:
8747 @dynamic MyProperty;
8748 @dynamic MyProperty, AnotherProperty;
8750 PS: This function is identical to cp_parser_objc_at_dynamic_declaration
8751 for C++. Keep them in sync.
8753 static void
8754 c_parser_objc_at_dynamic_declaration (c_parser *parser)
8756 tree list = NULL_TREE;
8757 location_t loc;
8758 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_DYNAMIC));
8759 loc = c_parser_peek_token (parser)->location;
8761 c_parser_consume_token (parser);
8762 while (true)
8764 tree property;
8765 if (c_parser_next_token_is_not (parser, CPP_NAME))
8767 c_parser_error (parser, "expected identifier");
8768 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
8769 parser->error = false;
8770 return;
8772 property = c_parser_peek_token (parser)->value;
8773 list = chainon (list, build_tree_list (NULL_TREE, property));
8774 c_parser_consume_token (parser);
8775 if (c_parser_next_token_is (parser, CPP_COMMA))
8776 c_parser_consume_token (parser);
8777 else
8778 break;
8780 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8781 objc_add_dynamic_declaration (loc, list);
8784 /* Parse UPC shared qualifier
8786 shared-type-qualifier: shared layout-qualifier-opt
8787 layout-qualifier: [ constant-expression-opt ] | [ * ]
8790 static void
8791 c_parser_upc_shared_qual (source_location loc,
8792 c_parser *parser,
8793 struct c_declspecs *specs)
8795 tree array_qual, arg1;
8797 /* consume "shared" part */
8798 c_parser_consume_token (parser);
8800 /* check for shared array layout specifier */
8801 if (!c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
8803 declspecs_add_qual (loc, specs, ridpointers[RID_SHARED]);
8804 return;
8806 c_parser_consume_token (parser);
8807 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
8809 /* [] layout specifier */
8810 arg1 = size_zero_node;
8812 else if (c_parser_next_token_is (parser, CPP_MULT))
8814 /* [*] layout specifier */
8815 arg1 = build1 (INDIRECT_REF, NULL_TREE, NULL_TREE);
8816 c_parser_consume_token (parser);
8818 else
8820 /* [ expression ] layout specifier */
8821 arg1 = c_parser_expression (parser).value;
8823 array_qual = build4 (ARRAY_REF, NULL_TREE, NULL_TREE,
8824 arg1, NULL_TREE, NULL_TREE);
8825 declspecs_add_qual (loc, specs, array_qual);
8827 if (!c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
8829 c_parser_error (parser, "expected ]");
8831 c_parser_consume_token (parser);
8834 /* Parse a UPC upc_forall statement
8836 upc_forall-statement:
8837 upc_forall ( expression[opt] ; expression[opt] ;
8838 expression[opt] ; affinity[opt] ) statement
8839 affinity: experssion | continue */
8841 static void
8842 c_parser_upc_forall_statement (c_parser *parser)
8844 tree block, cond, incr, save_break, save_cont, body;
8845 tree affinity;
8846 location_t loc = c_parser_peek_token (parser)->location;
8847 location_t affinity_loc = UNKNOWN_LOCATION;
8848 const int profile_upc_forall = flag_upc_instrument && get_upc_pupc_mode();
8849 gcc_assert (c_parser_next_token_is_keyword (parser, RID_UPC_FORALL));
8850 c_parser_consume_token (parser);
8851 block = c_begin_compound_stmt (flag_isoc99);
8852 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
8854 /* Parse the initialization declaration or expression. */
8855 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
8857 c_parser_consume_token (parser);
8858 c_finish_expr_stmt (loc, NULL_TREE);
8860 else if (c_parser_next_token_starts_declspecs (parser))
8862 c_parser_declaration_or_fndef (parser, true, true, true,
8863 true, true, NULL);
8864 check_for_loop_decls (loc, true);
8866 else if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
8868 /* __extension__ can start a declaration, but is also an
8869 unary operator that can start an expression. Consume all
8870 but the last of a possible series of __extension__ to
8871 determine which. */
8872 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
8873 && (c_parser_peek_2nd_token (parser)->keyword
8874 == RID_EXTENSION))
8875 c_parser_consume_token (parser);
8876 if (c_token_starts_declspecs (c_parser_peek_2nd_token (parser)))
8878 int ext;
8879 ext = disable_extension_diagnostics ();
8880 c_parser_consume_token (parser);
8881 c_parser_declaration_or_fndef (parser, true, true, true, true, true, NULL);
8882 restore_extension_diagnostics (ext);
8883 check_for_loop_decls (loc, true);
8885 else
8886 goto init_expr;
8888 else
8890 init_expr:
8891 c_finish_expr_stmt (loc, c_parser_expression (parser).value);
8892 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8894 /* Parse the loop condition. */
8895 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
8897 c_parser_consume_token (parser);
8898 cond = NULL_TREE;
8900 else
8902 cond = c_parser_condition (parser);
8903 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8905 /* Parse the increment expression. */
8906 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
8907 incr = c_process_expr_stmt (loc, NULL_TREE);
8908 else
8909 incr = c_process_expr_stmt (loc, c_parser_expression (parser).value);
8910 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8911 /* Parse the UPC affinity expression. */
8912 affinity_loc = c_parser_peek_token (parser)->location;
8913 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
8915 affinity = NULL_TREE;
8917 else if (c_parser_peek_token (parser)->type == CPP_KEYWORD
8918 && c_parser_peek_token (parser)->keyword == RID_CONTINUE)
8920 affinity = NULL_TREE;
8921 c_parser_consume_token (parser);
8923 else
8925 affinity = c_parser_expression_conv (parser).value;
8926 affinity = c_fully_fold (affinity, false, NULL);
8928 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8929 if (affinity)
8930 affinity = upc_affinity_test (affinity_loc, affinity);
8932 else
8934 cond = error_mark_node;
8935 incr = error_mark_node;
8936 affinity = error_mark_node;
8938 save_break = c_break_label;
8939 c_break_label = NULL_TREE;
8940 save_cont = c_cont_label;
8941 c_cont_label = NULL_TREE;
8942 body = c_parser_c99_block_statement (parser);
8943 if (profile_upc_forall)
8945 const tree gasp_start = upc_instrument_forall (loc, 1 /* start */);
8946 add_stmt (gasp_start);
8948 loc = c_parser_peek_token (parser)->location;
8949 if (affinity != NULL_TREE && affinity != error_mark_node)
8951 tree upc_forall_depth = upc_rts_forall_depth_var ();
8952 tree inc_depth, depth_gt_one;
8953 inc_depth = build_unary_op (loc, PREINCREMENT_EXPR, upc_forall_depth, 0);
8954 c_finish_expr_stmt (loc, inc_depth);
8955 depth_gt_one = build_binary_op (affinity_loc,
8956 GT_EXPR, upc_forall_depth, integer_one_node, 0);
8957 depth_gt_one = c_objc_common_truthvalue_conversion (affinity_loc, depth_gt_one);
8958 depth_gt_one = c_fully_fold (depth_gt_one, false, NULL);
8959 affinity = build_binary_op (affinity_loc, TRUTH_OR_EXPR,
8960 depth_gt_one, affinity, 0);
8961 body = build3 (COND_EXPR, void_type_node, affinity,
8962 body, NULL_TREE);
8963 c_finish_loop (loc, cond, incr, body, c_break_label, c_cont_label, true);
8964 c_finish_expr_stmt (loc,
8965 build_unary_op (loc, PREDECREMENT_EXPR, upc_forall_depth, 0));
8967 else
8968 c_finish_loop (loc, cond, incr, body, c_break_label, c_cont_label, true);
8969 if (profile_upc_forall)
8971 const tree gasp_end = upc_instrument_forall (loc, 0 /* start */);
8972 add_stmt (gasp_end);
8974 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
8975 c_break_label = save_break;
8976 c_cont_label = save_cont;
8979 /* Parse an upc-sync-statement.
8981 upc_barrier, upc_wait, upc_notify
8984 static void
8985 c_parser_upc_sync_statement (c_parser *parser, int sync_kind)
8987 location_t loc;
8988 tree expr = NULL_TREE;
8989 tree stmt;
8990 gcc_assert (c_parser_next_token_is_keyword (parser, RID_UPC_BARRIER) ||
8991 c_parser_next_token_is_keyword (parser, RID_UPC_NOTIFY) ||
8992 c_parser_next_token_is_keyword (parser, RID_UPC_WAIT));
8993 loc = c_parser_peek_token (parser)->location;
8994 c_parser_consume_token (parser);
8995 if (c_parser_peek_token (parser)->type != CPP_SEMICOLON)
8997 expr = c_parser_expression (parser).value;
8998 if (expr == error_mark_node)
8999 expr = NULL;
9000 if (!INTEGRAL_TYPE_P (TREE_TYPE (expr)))
9002 c_parser_error (parser, "expected integer expression");
9003 expr = NULL;
9006 stmt = size_int (sync_kind);
9007 (void) upc_build_sync_stmt (loc, stmt, expr);
9011 /* Handle pragmas. Some OpenMP pragmas are associated with, and therefore
9012 should be considered, statements. ALLOW_STMT is true if we're within
9013 the context of a function and such pragmas are to be allowed. Returns
9014 true if we actually parsed such a pragma. */
9016 static bool
9017 c_parser_pragma (c_parser *parser, enum pragma_context context)
9019 unsigned int id;
9021 id = c_parser_peek_token (parser)->pragma_kind;
9022 gcc_assert (id != PRAGMA_NONE);
9024 switch (id)
9026 case PRAGMA_OMP_BARRIER:
9027 if (context != pragma_compound)
9029 if (context == pragma_stmt)
9030 c_parser_error (parser, "%<#pragma omp barrier%> may only be "
9031 "used in compound statements");
9032 goto bad_stmt;
9034 c_parser_omp_barrier (parser);
9035 return false;
9037 case PRAGMA_OMP_FLUSH:
9038 if (context != pragma_compound)
9040 if (context == pragma_stmt)
9041 c_parser_error (parser, "%<#pragma omp flush%> may only be "
9042 "used in compound statements");
9043 goto bad_stmt;
9045 c_parser_omp_flush (parser);
9046 return false;
9048 case PRAGMA_OMP_TASKWAIT:
9049 if (context != pragma_compound)
9051 if (context == pragma_stmt)
9052 c_parser_error (parser, "%<#pragma omp taskwait%> may only be "
9053 "used in compound statements");
9054 goto bad_stmt;
9056 c_parser_omp_taskwait (parser);
9057 return false;
9059 case PRAGMA_OMP_TASKYIELD:
9060 if (context != pragma_compound)
9062 if (context == pragma_stmt)
9063 c_parser_error (parser, "%<#pragma omp taskyield%> may only be "
9064 "used in compound statements");
9065 goto bad_stmt;
9067 c_parser_omp_taskyield (parser);
9068 return false;
9070 case PRAGMA_OMP_THREADPRIVATE:
9071 c_parser_omp_threadprivate (parser);
9072 return false;
9074 case PRAGMA_OMP_SECTION:
9075 error_at (c_parser_peek_token (parser)->location,
9076 "%<#pragma omp section%> may only be used in "
9077 "%<#pragma omp sections%> construct");
9078 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
9079 return false;
9081 case PRAGMA_GCC_PCH_PREPROCESS:
9082 c_parser_error (parser, "%<#pragma GCC pch_preprocess%> must be first");
9083 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
9084 return false;
9086 default:
9087 if (id < PRAGMA_FIRST_EXTERNAL)
9089 if (context == pragma_external)
9091 bad_stmt:
9092 c_parser_error (parser, "expected declaration specifiers");
9093 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
9094 return false;
9096 c_parser_omp_construct (parser);
9097 return true;
9099 break;
9102 c_parser_consume_pragma (parser);
9103 c_invoke_pragma_handler (id);
9105 /* Skip to EOL, but suppress any error message. Those will have been
9106 generated by the handler routine through calling error, as opposed
9107 to calling c_parser_error. */
9108 parser->error = true;
9109 c_parser_skip_to_pragma_eol (parser);
9111 return false;
9114 /* The interface the pragma parsers have to the lexer. */
9116 enum cpp_ttype
9117 pragma_lex (tree *value)
9119 c_token *tok = c_parser_peek_token (the_parser);
9120 enum cpp_ttype ret = tok->type;
9122 *value = tok->value;
9123 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
9124 ret = CPP_EOF;
9125 else
9127 if (ret == CPP_KEYWORD)
9128 ret = CPP_NAME;
9129 c_parser_consume_token (the_parser);
9132 return ret;
9135 static void
9136 c_parser_pragma_pch_preprocess (c_parser *parser)
9138 tree name = NULL;
9140 c_parser_consume_pragma (parser);
9141 if (c_parser_next_token_is (parser, CPP_STRING))
9143 name = c_parser_peek_token (parser)->value;
9144 c_parser_consume_token (parser);
9146 else
9147 c_parser_error (parser, "expected string literal");
9148 c_parser_skip_to_pragma_eol (parser);
9150 if (name)
9151 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
9154 /* OpenMP 2.5 parsing routines. */
9156 /* Returns name of the next clause.
9157 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
9158 the token is not consumed. Otherwise appropriate pragma_omp_clause is
9159 returned and the token is consumed. */
9161 static pragma_omp_clause
9162 c_parser_omp_clause_name (c_parser *parser)
9164 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
9166 if (c_parser_next_token_is_keyword (parser, RID_IF))
9167 result = PRAGMA_OMP_CLAUSE_IF;
9168 else if (c_parser_next_token_is_keyword (parser, RID_DEFAULT))
9169 result = PRAGMA_OMP_CLAUSE_DEFAULT;
9170 else if (c_parser_next_token_is (parser, CPP_NAME))
9172 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
9174 switch (p[0])
9176 case 'c':
9177 if (!strcmp ("collapse", p))
9178 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
9179 else if (!strcmp ("copyin", p))
9180 result = PRAGMA_OMP_CLAUSE_COPYIN;
9181 else if (!strcmp ("copyprivate", p))
9182 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
9183 break;
9184 case 'f':
9185 if (!strcmp ("final", p))
9186 result = PRAGMA_OMP_CLAUSE_FINAL;
9187 else if (!strcmp ("firstprivate", p))
9188 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
9189 break;
9190 case 'l':
9191 if (!strcmp ("lastprivate", p))
9192 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
9193 break;
9194 case 'm':
9195 if (!strcmp ("mergeable", p))
9196 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
9197 break;
9198 case 'n':
9199 if (!strcmp ("nowait", p))
9200 result = PRAGMA_OMP_CLAUSE_NOWAIT;
9201 else if (!strcmp ("num_threads", p))
9202 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
9203 break;
9204 case 'o':
9205 if (!strcmp ("ordered", p))
9206 result = PRAGMA_OMP_CLAUSE_ORDERED;
9207 break;
9208 case 'p':
9209 if (!strcmp ("private", p))
9210 result = PRAGMA_OMP_CLAUSE_PRIVATE;
9211 break;
9212 case 'r':
9213 if (!strcmp ("reduction", p))
9214 result = PRAGMA_OMP_CLAUSE_REDUCTION;
9215 break;
9216 case 's':
9217 if (!strcmp ("schedule", p))
9218 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
9219 else if (!strcmp ("shared", p))
9220 result = PRAGMA_OMP_CLAUSE_SHARED;
9221 break;
9222 case 'u':
9223 if (!strcmp ("untied", p))
9224 result = PRAGMA_OMP_CLAUSE_UNTIED;
9225 break;
9229 if (result != PRAGMA_OMP_CLAUSE_NONE)
9230 c_parser_consume_token (parser);
9232 return result;
9235 /* Validate that a clause of the given type does not already exist. */
9237 static void
9238 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
9239 const char *name)
9241 tree c;
9243 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
9244 if (OMP_CLAUSE_CODE (c) == code)
9246 location_t loc = OMP_CLAUSE_LOCATION (c);
9247 error_at (loc, "too many %qs clauses", name);
9248 break;
9252 /* OpenMP 2.5:
9253 variable-list:
9254 identifier
9255 variable-list , identifier
9257 If KIND is nonzero, create the appropriate node and install the
9258 decl in OMP_CLAUSE_DECL and add the node to the head of the list.
9259 If KIND is nonzero, CLAUSE_LOC is the location of the clause.
9261 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
9262 return the list created. */
9264 static tree
9265 c_parser_omp_variable_list (c_parser *parser,
9266 location_t clause_loc,
9267 enum omp_clause_code kind,
9268 tree list)
9270 if (c_parser_next_token_is_not (parser, CPP_NAME)
9271 || c_parser_peek_token (parser)->id_kind != C_ID_ID)
9272 c_parser_error (parser, "expected identifier");
9274 while (c_parser_next_token_is (parser, CPP_NAME)
9275 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
9277 tree t = lookup_name (c_parser_peek_token (parser)->value);
9279 if (t == NULL_TREE)
9280 undeclared_variable (c_parser_peek_token (parser)->location,
9281 c_parser_peek_token (parser)->value);
9282 else if (t == error_mark_node)
9284 else if (kind != 0)
9286 tree u = build_omp_clause (clause_loc, kind);
9287 OMP_CLAUSE_DECL (u) = t;
9288 OMP_CLAUSE_CHAIN (u) = list;
9289 list = u;
9291 else
9292 list = tree_cons (t, NULL_TREE, list);
9294 c_parser_consume_token (parser);
9296 if (c_parser_next_token_is_not (parser, CPP_COMMA))
9297 break;
9299 c_parser_consume_token (parser);
9302 return list;
9305 /* Similarly, but expect leading and trailing parenthesis. This is a very
9306 common case for omp clauses. */
9308 static tree
9309 c_parser_omp_var_list_parens (c_parser *parser, enum omp_clause_code kind,
9310 tree list)
9312 /* The clauses location. */
9313 location_t loc = c_parser_peek_token (parser)->location;
9315 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
9317 list = c_parser_omp_variable_list (parser, loc, kind, list);
9318 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
9320 return list;
9323 /* OpenMP 3.0:
9324 collapse ( constant-expression ) */
9326 static tree
9327 c_parser_omp_clause_collapse (c_parser *parser, tree list)
9329 tree c, num = error_mark_node;
9330 HOST_WIDE_INT n;
9331 location_t loc;
9333 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse");
9335 loc = c_parser_peek_token (parser)->location;
9336 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
9338 num = c_parser_expr_no_commas (parser, NULL).value;
9339 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
9341 if (num == error_mark_node)
9342 return list;
9343 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
9344 || !host_integerp (num, 0)
9345 || (n = tree_low_cst (num, 0)) <= 0
9346 || (int) n != n)
9348 error_at (loc,
9349 "collapse argument needs positive constant integer expression");
9350 return list;
9352 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
9353 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
9354 OMP_CLAUSE_CHAIN (c) = list;
9355 return c;
9358 /* OpenMP 2.5:
9359 copyin ( variable-list ) */
9361 static tree
9362 c_parser_omp_clause_copyin (c_parser *parser, tree list)
9364 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_COPYIN, list);
9367 /* OpenMP 2.5:
9368 copyprivate ( variable-list ) */
9370 static tree
9371 c_parser_omp_clause_copyprivate (c_parser *parser, tree list)
9373 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_COPYPRIVATE, list);
9376 /* OpenMP 2.5:
9377 default ( shared | none ) */
9379 static tree
9380 c_parser_omp_clause_default (c_parser *parser, tree list)
9382 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
9383 location_t loc = c_parser_peek_token (parser)->location;
9384 tree c;
9386 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
9387 return list;
9388 if (c_parser_next_token_is (parser, CPP_NAME))
9390 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
9392 switch (p[0])
9394 case 'n':
9395 if (strcmp ("none", p) != 0)
9396 goto invalid_kind;
9397 kind = OMP_CLAUSE_DEFAULT_NONE;
9398 break;
9400 case 's':
9401 if (strcmp ("shared", p) != 0)
9402 goto invalid_kind;
9403 kind = OMP_CLAUSE_DEFAULT_SHARED;
9404 break;
9406 default:
9407 goto invalid_kind;
9410 c_parser_consume_token (parser);
9412 else
9414 invalid_kind:
9415 c_parser_error (parser, "expected %<none%> or %<shared%>");
9417 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
9419 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
9420 return list;
9422 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default");
9423 c = build_omp_clause (loc, OMP_CLAUSE_DEFAULT);
9424 OMP_CLAUSE_CHAIN (c) = list;
9425 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
9427 return c;
9430 /* OpenMP 2.5:
9431 firstprivate ( variable-list ) */
9433 static tree
9434 c_parser_omp_clause_firstprivate (c_parser *parser, tree list)
9436 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_FIRSTPRIVATE, list);
9439 /* OpenMP 3.1:
9440 final ( expression ) */
9442 static tree
9443 c_parser_omp_clause_final (c_parser *parser, tree list)
9445 location_t loc = c_parser_peek_token (parser)->location;
9446 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
9448 tree t = c_parser_paren_condition (parser);
9449 tree c;
9451 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final");
9453 c = build_omp_clause (loc, OMP_CLAUSE_FINAL);
9454 OMP_CLAUSE_FINAL_EXPR (c) = t;
9455 OMP_CLAUSE_CHAIN (c) = list;
9456 list = c;
9458 else
9459 c_parser_error (parser, "expected %<(%>");
9461 return list;
9464 /* OpenMP 2.5:
9465 if ( expression ) */
9467 static tree
9468 c_parser_omp_clause_if (c_parser *parser, tree list)
9470 location_t loc = c_parser_peek_token (parser)->location;
9471 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
9473 tree t = c_parser_paren_condition (parser);
9474 tree c;
9476 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if");
9478 c = build_omp_clause (loc, OMP_CLAUSE_IF);
9479 OMP_CLAUSE_IF_EXPR (c) = t;
9480 OMP_CLAUSE_CHAIN (c) = list;
9481 list = c;
9483 else
9484 c_parser_error (parser, "expected %<(%>");
9486 return list;
9489 /* OpenMP 2.5:
9490 lastprivate ( variable-list ) */
9492 static tree
9493 c_parser_omp_clause_lastprivate (c_parser *parser, tree list)
9495 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_LASTPRIVATE, list);
9498 /* OpenMP 3.1:
9499 mergeable */
9501 static tree
9502 c_parser_omp_clause_mergeable (c_parser *parser ATTRIBUTE_UNUSED, tree list)
9504 tree c;
9506 /* FIXME: Should we allow duplicates? */
9507 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable");
9509 c = build_omp_clause (c_parser_peek_token (parser)->location,
9510 OMP_CLAUSE_MERGEABLE);
9511 OMP_CLAUSE_CHAIN (c) = list;
9513 return c;
9516 /* OpenMP 2.5:
9517 nowait */
9519 static tree
9520 c_parser_omp_clause_nowait (c_parser *parser ATTRIBUTE_UNUSED, tree list)
9522 tree c;
9523 location_t loc = c_parser_peek_token (parser)->location;
9525 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait");
9527 c = build_omp_clause (loc, OMP_CLAUSE_NOWAIT);
9528 OMP_CLAUSE_CHAIN (c) = list;
9529 return c;
9532 /* OpenMP 2.5:
9533 num_threads ( expression ) */
9535 static tree
9536 c_parser_omp_clause_num_threads (c_parser *parser, tree list)
9538 location_t num_threads_loc = c_parser_peek_token (parser)->location;
9539 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
9541 location_t expr_loc = c_parser_peek_token (parser)->location;
9542 tree c, t = c_parser_expression (parser).value;
9543 mark_exp_read (t);
9544 t = c_fully_fold (t, false, NULL);
9546 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
9548 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
9550 c_parser_error (parser, "expected integer expression");
9551 return list;
9554 /* Attempt to statically determine when the number isn't positive. */
9555 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
9556 build_int_cst (TREE_TYPE (t), 0));
9557 if (CAN_HAVE_LOCATION_P (c))
9558 SET_EXPR_LOCATION (c, expr_loc);
9559 if (c == boolean_true_node)
9561 warning_at (expr_loc, 0,
9562 "%<num_threads%> value must be positive");
9563 t = integer_one_node;
9566 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS, "num_threads");
9568 c = build_omp_clause (num_threads_loc, OMP_CLAUSE_NUM_THREADS);
9569 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
9570 OMP_CLAUSE_CHAIN (c) = list;
9571 list = c;
9574 return list;
9577 /* OpenMP 2.5:
9578 ordered */
9580 static tree
9581 c_parser_omp_clause_ordered (c_parser *parser, tree list)
9583 tree c;
9585 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED, "ordered");
9587 c = build_omp_clause (c_parser_peek_token (parser)->location,
9588 OMP_CLAUSE_ORDERED);
9589 OMP_CLAUSE_CHAIN (c) = list;
9591 return c;
9594 /* OpenMP 2.5:
9595 private ( variable-list ) */
9597 static tree
9598 c_parser_omp_clause_private (c_parser *parser, tree list)
9600 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_PRIVATE, list);
9603 /* OpenMP 2.5:
9604 reduction ( reduction-operator : variable-list )
9606 reduction-operator:
9607 One of: + * - & ^ | && ||
9609 OpenMP 3.1:
9611 reduction-operator:
9612 One of: + * - & ^ | && || max min */
9614 static tree
9615 c_parser_omp_clause_reduction (c_parser *parser, tree list)
9617 location_t clause_loc = c_parser_peek_token (parser)->location;
9618 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
9620 enum tree_code code;
9622 switch (c_parser_peek_token (parser)->type)
9624 case CPP_PLUS:
9625 code = PLUS_EXPR;
9626 break;
9627 case CPP_MULT:
9628 code = MULT_EXPR;
9629 break;
9630 case CPP_MINUS:
9631 code = MINUS_EXPR;
9632 break;
9633 case CPP_AND:
9634 code = BIT_AND_EXPR;
9635 break;
9636 case CPP_XOR:
9637 code = BIT_XOR_EXPR;
9638 break;
9639 case CPP_OR:
9640 code = BIT_IOR_EXPR;
9641 break;
9642 case CPP_AND_AND:
9643 code = TRUTH_ANDIF_EXPR;
9644 break;
9645 case CPP_OR_OR:
9646 code = TRUTH_ORIF_EXPR;
9647 break;
9648 case CPP_NAME:
9650 const char *p
9651 = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
9652 if (strcmp (p, "min") == 0)
9654 code = MIN_EXPR;
9655 break;
9657 if (strcmp (p, "max") == 0)
9659 code = MAX_EXPR;
9660 break;
9663 /* FALLTHRU */
9664 default:
9665 c_parser_error (parser,
9666 "expected %<+%>, %<*%>, %<-%>, %<&%>, "
9667 "%<^%>, %<|%>, %<&&%>, %<||%>, %<min%> or %<max%>");
9668 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
9669 return list;
9671 c_parser_consume_token (parser);
9672 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
9674 tree nl, c;
9676 nl = c_parser_omp_variable_list (parser, clause_loc,
9677 OMP_CLAUSE_REDUCTION, list);
9678 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
9679 OMP_CLAUSE_REDUCTION_CODE (c) = code;
9681 list = nl;
9683 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
9685 return list;
9688 /* OpenMP 2.5:
9689 schedule ( schedule-kind )
9690 schedule ( schedule-kind , expression )
9692 schedule-kind:
9693 static | dynamic | guided | runtime | auto
9696 static tree
9697 c_parser_omp_clause_schedule (c_parser *parser, tree list)
9699 tree c, t;
9700 location_t loc = c_parser_peek_token (parser)->location;
9702 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
9703 return list;
9705 c = build_omp_clause (loc, OMP_CLAUSE_SCHEDULE);
9707 if (c_parser_next_token_is (parser, CPP_NAME))
9709 tree kind = c_parser_peek_token (parser)->value;
9710 const char *p = IDENTIFIER_POINTER (kind);
9712 switch (p[0])
9714 case 'd':
9715 if (strcmp ("dynamic", p) != 0)
9716 goto invalid_kind;
9717 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
9718 break;
9720 case 'g':
9721 if (strcmp ("guided", p) != 0)
9722 goto invalid_kind;
9723 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
9724 break;
9726 case 'r':
9727 if (strcmp ("runtime", p) != 0)
9728 goto invalid_kind;
9729 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
9730 break;
9732 default:
9733 goto invalid_kind;
9736 else if (c_parser_next_token_is_keyword (parser, RID_STATIC))
9737 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
9738 else if (c_parser_next_token_is_keyword (parser, RID_AUTO))
9739 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
9740 else
9741 goto invalid_kind;
9743 c_parser_consume_token (parser);
9744 if (c_parser_next_token_is (parser, CPP_COMMA))
9746 location_t here;
9747 c_parser_consume_token (parser);
9749 here = c_parser_peek_token (parser)->location;
9750 t = c_parser_expr_no_commas (parser, NULL).value;
9751 mark_exp_read (t);
9752 t = c_fully_fold (t, false, NULL);
9754 if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
9755 error_at (here, "schedule %<runtime%> does not take "
9756 "a %<chunk_size%> parameter");
9757 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
9758 error_at (here,
9759 "schedule %<auto%> does not take "
9760 "a %<chunk_size%> parameter");
9761 else if (TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE)
9762 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
9763 else
9764 c_parser_error (parser, "expected integer expression");
9766 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
9768 else
9769 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
9770 "expected %<,%> or %<)%>");
9772 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
9773 OMP_CLAUSE_CHAIN (c) = list;
9774 return c;
9776 invalid_kind:
9777 c_parser_error (parser, "invalid schedule kind");
9778 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
9779 return list;
9782 /* OpenMP 2.5:
9783 shared ( variable-list ) */
9785 static tree
9786 c_parser_omp_clause_shared (c_parser *parser, tree list)
9788 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_SHARED, list);
9791 /* OpenMP 3.0:
9792 untied */
9794 static tree
9795 c_parser_omp_clause_untied (c_parser *parser ATTRIBUTE_UNUSED, tree list)
9797 tree c;
9799 /* FIXME: Should we allow duplicates? */
9800 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied");
9802 c = build_omp_clause (c_parser_peek_token (parser)->location,
9803 OMP_CLAUSE_UNTIED);
9804 OMP_CLAUSE_CHAIN (c) = list;
9806 return c;
9809 /* Parse all OpenMP clauses. The set clauses allowed by the directive
9810 is a bitmask in MASK. Return the list of clauses found; the result
9811 of clause default goes in *pdefault. */
9813 static tree
9814 c_parser_omp_all_clauses (c_parser *parser, unsigned int mask,
9815 const char *where)
9817 tree clauses = NULL;
9818 bool first = true;
9820 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
9822 location_t here;
9823 pragma_omp_clause c_kind;
9824 const char *c_name;
9825 tree prev = clauses;
9827 if (!first && c_parser_next_token_is (parser, CPP_COMMA))
9828 c_parser_consume_token (parser);
9830 first = false;
9831 here = c_parser_peek_token (parser)->location;
9832 c_kind = c_parser_omp_clause_name (parser);
9834 switch (c_kind)
9836 case PRAGMA_OMP_CLAUSE_COLLAPSE:
9837 clauses = c_parser_omp_clause_collapse (parser, clauses);
9838 c_name = "collapse";
9839 break;
9840 case PRAGMA_OMP_CLAUSE_COPYIN:
9841 clauses = c_parser_omp_clause_copyin (parser, clauses);
9842 c_name = "copyin";
9843 break;
9844 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
9845 clauses = c_parser_omp_clause_copyprivate (parser, clauses);
9846 c_name = "copyprivate";
9847 break;
9848 case PRAGMA_OMP_CLAUSE_DEFAULT:
9849 clauses = c_parser_omp_clause_default (parser, clauses);
9850 c_name = "default";
9851 break;
9852 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
9853 clauses = c_parser_omp_clause_firstprivate (parser, clauses);
9854 c_name = "firstprivate";
9855 break;
9856 case PRAGMA_OMP_CLAUSE_FINAL:
9857 clauses = c_parser_omp_clause_final (parser, clauses);
9858 c_name = "final";
9859 break;
9860 case PRAGMA_OMP_CLAUSE_IF:
9861 clauses = c_parser_omp_clause_if (parser, clauses);
9862 c_name = "if";
9863 break;
9864 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
9865 clauses = c_parser_omp_clause_lastprivate (parser, clauses);
9866 c_name = "lastprivate";
9867 break;
9868 case PRAGMA_OMP_CLAUSE_MERGEABLE:
9869 clauses = c_parser_omp_clause_mergeable (parser, clauses);
9870 c_name = "mergeable";
9871 break;
9872 case PRAGMA_OMP_CLAUSE_NOWAIT:
9873 clauses = c_parser_omp_clause_nowait (parser, clauses);
9874 c_name = "nowait";
9875 break;
9876 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
9877 clauses = c_parser_omp_clause_num_threads (parser, clauses);
9878 c_name = "num_threads";
9879 break;
9880 case PRAGMA_OMP_CLAUSE_ORDERED:
9881 clauses = c_parser_omp_clause_ordered (parser, clauses);
9882 c_name = "ordered";
9883 break;
9884 case PRAGMA_OMP_CLAUSE_PRIVATE:
9885 clauses = c_parser_omp_clause_private (parser, clauses);
9886 c_name = "private";
9887 break;
9888 case PRAGMA_OMP_CLAUSE_REDUCTION:
9889 clauses = c_parser_omp_clause_reduction (parser, clauses);
9890 c_name = "reduction";
9891 break;
9892 case PRAGMA_OMP_CLAUSE_SCHEDULE:
9893 clauses = c_parser_omp_clause_schedule (parser, clauses);
9894 c_name = "schedule";
9895 break;
9896 case PRAGMA_OMP_CLAUSE_SHARED:
9897 clauses = c_parser_omp_clause_shared (parser, clauses);
9898 c_name = "shared";
9899 break;
9900 case PRAGMA_OMP_CLAUSE_UNTIED:
9901 clauses = c_parser_omp_clause_untied (parser, clauses);
9902 c_name = "untied";
9903 break;
9904 default:
9905 c_parser_error (parser, "expected %<#pragma omp%> clause");
9906 goto saw_error;
9909 if (((mask >> c_kind) & 1) == 0 && !parser->error)
9911 /* Remove the invalid clause(s) from the list to avoid
9912 confusing the rest of the compiler. */
9913 clauses = prev;
9914 error_at (here, "%qs is not valid for %qs", c_name, where);
9918 saw_error:
9919 c_parser_skip_to_pragma_eol (parser);
9921 return c_finish_omp_clauses (clauses);
9924 /* OpenMP 2.5:
9925 structured-block:
9926 statement
9928 In practice, we're also interested in adding the statement to an
9929 outer node. So it is convenient if we work around the fact that
9930 c_parser_statement calls add_stmt. */
9932 static tree
9933 c_parser_omp_structured_block (c_parser *parser)
9935 tree stmt = push_stmt_list ();
9936 c_parser_statement (parser);
9937 return pop_stmt_list (stmt);
9940 /* OpenMP 2.5:
9941 # pragma omp atomic new-line
9942 expression-stmt
9944 expression-stmt:
9945 x binop= expr | x++ | ++x | x-- | --x
9946 binop:
9947 +, *, -, /, &, ^, |, <<, >>
9949 where x is an lvalue expression with scalar type.
9951 OpenMP 3.1:
9952 # pragma omp atomic new-line
9953 update-stmt
9955 # pragma omp atomic read new-line
9956 read-stmt
9958 # pragma omp atomic write new-line
9959 write-stmt
9961 # pragma omp atomic update new-line
9962 update-stmt
9964 # pragma omp atomic capture new-line
9965 capture-stmt
9967 # pragma omp atomic capture new-line
9968 capture-block
9970 read-stmt:
9971 v = x
9972 write-stmt:
9973 x = expr
9974 update-stmt:
9975 expression-stmt | x = x binop expr
9976 capture-stmt:
9977 v = x binop= expr | v = x++ | v = ++x | v = x-- | v = --x
9978 capture-block:
9979 { v = x; update-stmt; } | { update-stmt; v = x; }
9981 where x and v are lvalue expressions with scalar type.
9983 LOC is the location of the #pragma token. */
9985 static void
9986 c_parser_omp_atomic (location_t loc, c_parser *parser)
9988 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE;
9989 tree lhs1 = NULL_TREE, rhs1 = NULL_TREE;
9990 tree stmt, orig_lhs;
9991 enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
9992 struct c_expr rhs_expr;
9993 bool structured_block = false;
9995 if (c_parser_next_token_is (parser, CPP_NAME))
9997 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
9999 if (!strcmp (p, "read"))
10000 code = OMP_ATOMIC_READ;
10001 else if (!strcmp (p, "write"))
10002 code = NOP_EXPR;
10003 else if (!strcmp (p, "update"))
10004 code = OMP_ATOMIC;
10005 else if (!strcmp (p, "capture"))
10006 code = OMP_ATOMIC_CAPTURE_NEW;
10007 else
10008 p = NULL;
10009 if (p)
10010 c_parser_consume_token (parser);
10012 c_parser_skip_to_pragma_eol (parser);
10014 switch (code)
10016 case OMP_ATOMIC_READ:
10017 case NOP_EXPR: /* atomic write */
10018 v = c_parser_unary_expression (parser).value;
10019 v = c_fully_fold (v, false, NULL);
10020 if (v == error_mark_node)
10021 goto saw_error;
10022 loc = c_parser_peek_token (parser)->location;
10023 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
10024 goto saw_error;
10025 if (code == NOP_EXPR)
10026 lhs = c_parser_expression (parser).value;
10027 else
10028 lhs = c_parser_unary_expression (parser).value;
10029 lhs = c_fully_fold (lhs, false, NULL);
10030 if (lhs == error_mark_node)
10031 goto saw_error;
10032 if (code == NOP_EXPR)
10034 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
10035 opcode. */
10036 code = OMP_ATOMIC;
10037 rhs = lhs;
10038 lhs = v;
10039 v = NULL_TREE;
10041 goto done;
10042 case OMP_ATOMIC_CAPTURE_NEW:
10043 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
10045 c_parser_consume_token (parser);
10046 structured_block = true;
10048 else
10050 v = c_parser_unary_expression (parser).value;
10051 v = c_fully_fold (v, false, NULL);
10052 if (v == error_mark_node)
10053 goto saw_error;
10054 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
10055 goto saw_error;
10057 break;
10058 default:
10059 break;
10062 /* For structured_block case we don't know yet whether
10063 old or new x should be captured. */
10064 restart:
10065 lhs = c_parser_unary_expression (parser).value;
10066 lhs = c_fully_fold (lhs, false, NULL);
10067 orig_lhs = lhs;
10068 switch (TREE_CODE (lhs))
10070 case ERROR_MARK:
10071 saw_error:
10072 c_parser_skip_to_end_of_block_or_statement (parser);
10073 if (structured_block)
10075 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
10076 c_parser_consume_token (parser);
10077 else if (code == OMP_ATOMIC_CAPTURE_NEW)
10079 c_parser_skip_to_end_of_block_or_statement (parser);
10080 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
10081 c_parser_consume_token (parser);
10084 return;
10086 case POSTINCREMENT_EXPR:
10087 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
10088 code = OMP_ATOMIC_CAPTURE_OLD;
10089 /* FALLTHROUGH */
10090 case PREINCREMENT_EXPR:
10091 lhs = TREE_OPERAND (lhs, 0);
10092 opcode = PLUS_EXPR;
10093 rhs = integer_one_node;
10094 break;
10096 case POSTDECREMENT_EXPR:
10097 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
10098 code = OMP_ATOMIC_CAPTURE_OLD;
10099 /* FALLTHROUGH */
10100 case PREDECREMENT_EXPR:
10101 lhs = TREE_OPERAND (lhs, 0);
10102 opcode = MINUS_EXPR;
10103 rhs = integer_one_node;
10104 break;
10106 case COMPOUND_EXPR:
10107 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
10108 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
10109 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
10110 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
10111 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
10112 (TREE_OPERAND (lhs, 1), 0), 0)))
10113 == BOOLEAN_TYPE)
10114 /* Undo effects of boolean_increment for post {in,de}crement. */
10115 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
10116 /* FALLTHRU */
10117 case MODIFY_EXPR:
10118 if (TREE_CODE (lhs) == MODIFY_EXPR
10119 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
10121 /* Undo effects of boolean_increment. */
10122 if (integer_onep (TREE_OPERAND (lhs, 1)))
10124 /* This is pre or post increment. */
10125 rhs = TREE_OPERAND (lhs, 1);
10126 lhs = TREE_OPERAND (lhs, 0);
10127 opcode = NOP_EXPR;
10128 if (code == OMP_ATOMIC_CAPTURE_NEW
10129 && !structured_block
10130 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
10131 code = OMP_ATOMIC_CAPTURE_OLD;
10132 break;
10134 if (TREE_CODE (TREE_OPERAND (lhs, 1)) == TRUTH_NOT_EXPR
10135 && TREE_OPERAND (lhs, 0)
10136 == TREE_OPERAND (TREE_OPERAND (lhs, 1), 0))
10138 /* This is pre or post decrement. */
10139 rhs = TREE_OPERAND (lhs, 1);
10140 lhs = TREE_OPERAND (lhs, 0);
10141 opcode = NOP_EXPR;
10142 if (code == OMP_ATOMIC_CAPTURE_NEW
10143 && !structured_block
10144 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
10145 code = OMP_ATOMIC_CAPTURE_OLD;
10146 break;
10149 /* FALLTHRU */
10150 default:
10151 switch (c_parser_peek_token (parser)->type)
10153 case CPP_MULT_EQ:
10154 opcode = MULT_EXPR;
10155 break;
10156 case CPP_DIV_EQ:
10157 opcode = TRUNC_DIV_EXPR;
10158 break;
10159 case CPP_PLUS_EQ:
10160 opcode = PLUS_EXPR;
10161 break;
10162 case CPP_MINUS_EQ:
10163 opcode = MINUS_EXPR;
10164 break;
10165 case CPP_LSHIFT_EQ:
10166 opcode = LSHIFT_EXPR;
10167 break;
10168 case CPP_RSHIFT_EQ:
10169 opcode = RSHIFT_EXPR;
10170 break;
10171 case CPP_AND_EQ:
10172 opcode = BIT_AND_EXPR;
10173 break;
10174 case CPP_OR_EQ:
10175 opcode = BIT_IOR_EXPR;
10176 break;
10177 case CPP_XOR_EQ:
10178 opcode = BIT_XOR_EXPR;
10179 break;
10180 case CPP_EQ:
10181 if (structured_block || code == OMP_ATOMIC)
10183 location_t aloc = c_parser_peek_token (parser)->location;
10184 location_t rhs_loc;
10185 enum c_parser_prec oprec = PREC_NONE;
10187 c_parser_consume_token (parser);
10188 rhs1 = c_parser_unary_expression (parser).value;
10189 rhs1 = c_fully_fold (rhs1, false, NULL);
10190 if (rhs1 == error_mark_node)
10191 goto saw_error;
10192 switch (c_parser_peek_token (parser)->type)
10194 case CPP_SEMICOLON:
10195 if (code == OMP_ATOMIC_CAPTURE_NEW)
10197 code = OMP_ATOMIC_CAPTURE_OLD;
10198 v = lhs;
10199 lhs = NULL_TREE;
10200 lhs1 = rhs1;
10201 rhs1 = NULL_TREE;
10202 c_parser_consume_token (parser);
10203 goto restart;
10205 c_parser_error (parser,
10206 "invalid form of %<#pragma omp atomic%>");
10207 goto saw_error;
10208 case CPP_MULT:
10209 opcode = MULT_EXPR;
10210 oprec = PREC_MULT;
10211 break;
10212 case CPP_DIV:
10213 opcode = TRUNC_DIV_EXPR;
10214 oprec = PREC_MULT;
10215 break;
10216 case CPP_PLUS:
10217 opcode = PLUS_EXPR;
10218 oprec = PREC_ADD;
10219 break;
10220 case CPP_MINUS:
10221 opcode = MINUS_EXPR;
10222 oprec = PREC_ADD;
10223 break;
10224 case CPP_LSHIFT:
10225 opcode = LSHIFT_EXPR;
10226 oprec = PREC_SHIFT;
10227 break;
10228 case CPP_RSHIFT:
10229 opcode = RSHIFT_EXPR;
10230 oprec = PREC_SHIFT;
10231 break;
10232 case CPP_AND:
10233 opcode = BIT_AND_EXPR;
10234 oprec = PREC_BITAND;
10235 break;
10236 case CPP_OR:
10237 opcode = BIT_IOR_EXPR;
10238 oprec = PREC_BITOR;
10239 break;
10240 case CPP_XOR:
10241 opcode = BIT_XOR_EXPR;
10242 oprec = PREC_BITXOR;
10243 break;
10244 default:
10245 c_parser_error (parser,
10246 "invalid operator for %<#pragma omp atomic%>");
10247 goto saw_error;
10249 loc = aloc;
10250 c_parser_consume_token (parser);
10251 rhs_loc = c_parser_peek_token (parser)->location;
10252 if (commutative_tree_code (opcode))
10253 oprec = (enum c_parser_prec) (oprec - 1);
10254 rhs_expr = c_parser_binary_expression (parser, NULL, oprec);
10255 rhs_expr = default_function_array_read_conversion (rhs_loc,
10256 rhs_expr);
10257 rhs = rhs_expr.value;
10258 rhs = c_fully_fold (rhs, false, NULL);
10259 goto stmt_done;
10261 /* FALLTHROUGH */
10262 default:
10263 c_parser_error (parser,
10264 "invalid operator for %<#pragma omp atomic%>");
10265 goto saw_error;
10268 /* Arrange to pass the location of the assignment operator to
10269 c_finish_omp_atomic. */
10270 loc = c_parser_peek_token (parser)->location;
10271 c_parser_consume_token (parser);
10273 location_t rhs_loc = c_parser_peek_token (parser)->location;
10274 rhs_expr = c_parser_expression (parser);
10275 rhs_expr = default_function_array_read_conversion (rhs_loc, rhs_expr);
10277 rhs = rhs_expr.value;
10278 rhs = c_fully_fold (rhs, false, NULL);
10279 break;
10281 stmt_done:
10282 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
10284 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
10285 goto saw_error;
10286 v = c_parser_unary_expression (parser).value;
10287 v = c_fully_fold (v, false, NULL);
10288 if (v == error_mark_node)
10289 goto saw_error;
10290 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
10291 goto saw_error;
10292 lhs1 = c_parser_unary_expression (parser).value;
10293 lhs1 = c_fully_fold (lhs1, false, NULL);
10294 if (lhs1 == error_mark_node)
10295 goto saw_error;
10297 if (structured_block)
10299 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
10300 c_parser_require (parser, CPP_CLOSE_BRACE, "expected %<}%>");
10302 done:
10303 stmt = c_finish_omp_atomic (loc, code, opcode, lhs, rhs, v, lhs1, rhs1);
10304 if (stmt != error_mark_node)
10305 add_stmt (stmt);
10307 if (!structured_block)
10308 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
10312 /* OpenMP 2.5:
10313 # pragma omp barrier new-line
10316 static void
10317 c_parser_omp_barrier (c_parser *parser)
10319 location_t loc = c_parser_peek_token (parser)->location;
10320 c_parser_consume_pragma (parser);
10321 c_parser_skip_to_pragma_eol (parser);
10323 c_finish_omp_barrier (loc);
10326 /* OpenMP 2.5:
10327 # pragma omp critical [(name)] new-line
10328 structured-block
10330 LOC is the location of the #pragma itself. */
10332 static tree
10333 c_parser_omp_critical (location_t loc, c_parser *parser)
10335 tree stmt, name = NULL;
10337 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
10339 c_parser_consume_token (parser);
10340 if (c_parser_next_token_is (parser, CPP_NAME))
10342 name = c_parser_peek_token (parser)->value;
10343 c_parser_consume_token (parser);
10344 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10346 else
10347 c_parser_error (parser, "expected identifier");
10349 else if (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
10350 c_parser_error (parser, "expected %<(%> or end of line");
10351 c_parser_skip_to_pragma_eol (parser);
10353 stmt = c_parser_omp_structured_block (parser);
10354 return c_finish_omp_critical (loc, stmt, name);
10357 /* OpenMP 2.5:
10358 # pragma omp flush flush-vars[opt] new-line
10360 flush-vars:
10361 ( variable-list ) */
10363 static void
10364 c_parser_omp_flush (c_parser *parser)
10366 location_t loc = c_parser_peek_token (parser)->location;
10367 c_parser_consume_pragma (parser);
10368 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
10369 c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
10370 else if (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
10371 c_parser_error (parser, "expected %<(%> or end of line");
10372 c_parser_skip_to_pragma_eol (parser);
10374 c_finish_omp_flush (loc);
10377 /* Parse the restricted form of the for statement allowed by OpenMP.
10378 The real trick here is to determine the loop control variable early
10379 so that we can push a new decl if necessary to make it private.
10380 LOC is the location of the OMP in "#pragma omp". */
10382 static tree
10383 c_parser_omp_for_loop (location_t loc,
10384 c_parser *parser, tree clauses, tree *par_clauses)
10386 tree decl, cond, incr, save_break, save_cont, body, init, stmt, cl;
10387 tree declv, condv, incrv, initv, ret = NULL;
10388 bool fail = false, open_brace_parsed = false;
10389 int i, collapse = 1, nbraces = 0;
10390 location_t for_loc;
10391 VEC(tree,gc) *for_block = make_tree_vector ();
10393 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
10394 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
10395 collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
10397 gcc_assert (collapse >= 1);
10399 declv = make_tree_vec (collapse);
10400 initv = make_tree_vec (collapse);
10401 condv = make_tree_vec (collapse);
10402 incrv = make_tree_vec (collapse);
10404 if (!c_parser_next_token_is_keyword (parser, RID_FOR))
10406 c_parser_error (parser, "for statement expected");
10407 return NULL;
10409 for_loc = c_parser_peek_token (parser)->location;
10410 c_parser_consume_token (parser);
10412 for (i = 0; i < collapse; i++)
10414 int bracecount = 0;
10416 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10417 goto pop_scopes;
10419 /* Parse the initialization declaration or expression. */
10420 if (c_parser_next_tokens_start_declaration (parser))
10422 if (i > 0)
10423 VEC_safe_push (tree, gc, for_block, c_begin_compound_stmt (true));
10424 c_parser_declaration_or_fndef (parser, true, true, true, true, true, NULL);
10425 decl = check_for_loop_decls (for_loc, flag_isoc99);
10426 if (decl == NULL)
10427 goto error_init;
10428 if (DECL_INITIAL (decl) == error_mark_node)
10429 decl = error_mark_node;
10430 init = decl;
10432 else if (c_parser_next_token_is (parser, CPP_NAME)
10433 && c_parser_peek_2nd_token (parser)->type == CPP_EQ)
10435 struct c_expr decl_exp;
10436 struct c_expr init_exp;
10437 location_t init_loc;
10439 decl_exp = c_parser_postfix_expression (parser);
10440 decl = decl_exp.value;
10442 c_parser_require (parser, CPP_EQ, "expected %<=%>");
10444 init_loc = c_parser_peek_token (parser)->location;
10445 init_exp = c_parser_expr_no_commas (parser, NULL);
10446 init_exp = default_function_array_read_conversion (init_loc,
10447 init_exp);
10448 init = build_modify_expr (init_loc, decl, decl_exp.original_type,
10449 NOP_EXPR, init_loc, init_exp.value,
10450 init_exp.original_type);
10451 init = c_process_expr_stmt (init_loc, init);
10453 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
10455 else
10457 error_init:
10458 c_parser_error (parser,
10459 "expected iteration declaration or initialization");
10460 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
10461 "expected %<)%>");
10462 fail = true;
10463 goto parse_next;
10466 /* Parse the loop condition. */
10467 cond = NULL_TREE;
10468 if (c_parser_next_token_is_not (parser, CPP_SEMICOLON))
10470 location_t cond_loc = c_parser_peek_token (parser)->location;
10471 struct c_expr cond_expr = c_parser_binary_expression (parser, NULL,
10472 PREC_NONE);
10474 cond = cond_expr.value;
10475 cond = c_objc_common_truthvalue_conversion (cond_loc, cond);
10476 cond = c_fully_fold (cond, false, NULL);
10477 switch (cond_expr.original_code)
10479 case GT_EXPR:
10480 case GE_EXPR:
10481 case LT_EXPR:
10482 case LE_EXPR:
10483 break;
10484 default:
10485 /* Can't be cond = error_mark_node, because we want to preserve
10486 the location until c_finish_omp_for. */
10487 cond = build1 (NOP_EXPR, boolean_type_node, error_mark_node);
10488 break;
10490 protected_set_expr_location (cond, cond_loc);
10492 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
10494 /* Parse the increment expression. */
10495 incr = NULL_TREE;
10496 if (c_parser_next_token_is_not (parser, CPP_CLOSE_PAREN))
10498 location_t incr_loc = c_parser_peek_token (parser)->location;
10500 incr = c_process_expr_stmt (incr_loc,
10501 c_parser_expression (parser).value);
10503 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10505 if (decl == NULL || decl == error_mark_node || init == error_mark_node)
10506 fail = true;
10507 else
10509 TREE_VEC_ELT (declv, i) = decl;
10510 TREE_VEC_ELT (initv, i) = init;
10511 TREE_VEC_ELT (condv, i) = cond;
10512 TREE_VEC_ELT (incrv, i) = incr;
10515 parse_next:
10516 if (i == collapse - 1)
10517 break;
10519 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
10520 in between the collapsed for loops to be still considered perfectly
10521 nested. Hopefully the final version clarifies this.
10522 For now handle (multiple) {'s and empty statements. */
10525 if (c_parser_next_token_is_keyword (parser, RID_FOR))
10527 c_parser_consume_token (parser);
10528 break;
10530 else if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
10532 c_parser_consume_token (parser);
10533 bracecount++;
10535 else if (bracecount
10536 && c_parser_next_token_is (parser, CPP_SEMICOLON))
10537 c_parser_consume_token (parser);
10538 else
10540 c_parser_error (parser, "not enough perfectly nested loops");
10541 if (bracecount)
10543 open_brace_parsed = true;
10544 bracecount--;
10546 fail = true;
10547 collapse = 0;
10548 break;
10551 while (1);
10553 nbraces += bracecount;
10556 save_break = c_break_label;
10557 c_break_label = size_one_node;
10558 save_cont = c_cont_label;
10559 c_cont_label = NULL_TREE;
10560 body = push_stmt_list ();
10562 if (open_brace_parsed)
10564 location_t here = c_parser_peek_token (parser)->location;
10565 stmt = c_begin_compound_stmt (true);
10566 c_parser_compound_statement_nostart (parser);
10567 add_stmt (c_end_compound_stmt (here, stmt, true));
10569 else
10570 add_stmt (c_parser_c99_block_statement (parser));
10571 if (c_cont_label)
10573 tree t = build1 (LABEL_EXPR, void_type_node, c_cont_label);
10574 SET_EXPR_LOCATION (t, loc);
10575 add_stmt (t);
10578 body = pop_stmt_list (body);
10579 c_break_label = save_break;
10580 c_cont_label = save_cont;
10582 while (nbraces)
10584 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
10586 c_parser_consume_token (parser);
10587 nbraces--;
10589 else if (c_parser_next_token_is (parser, CPP_SEMICOLON))
10590 c_parser_consume_token (parser);
10591 else
10593 c_parser_error (parser, "collapsed loops not perfectly nested");
10594 while (nbraces)
10596 location_t here = c_parser_peek_token (parser)->location;
10597 stmt = c_begin_compound_stmt (true);
10598 add_stmt (body);
10599 c_parser_compound_statement_nostart (parser);
10600 body = c_end_compound_stmt (here, stmt, true);
10601 nbraces--;
10603 goto pop_scopes;
10607 /* Only bother calling c_finish_omp_for if we haven't already generated
10608 an error from the initialization parsing. */
10609 if (!fail)
10611 stmt = c_finish_omp_for (loc, declv, initv, condv, incrv, body, NULL);
10612 if (stmt)
10614 if (par_clauses != NULL)
10616 tree *c;
10617 for (c = par_clauses; *c ; )
10618 if (OMP_CLAUSE_CODE (*c) != OMP_CLAUSE_FIRSTPRIVATE
10619 && OMP_CLAUSE_CODE (*c) != OMP_CLAUSE_LASTPRIVATE)
10620 c = &OMP_CLAUSE_CHAIN (*c);
10621 else
10623 for (i = 0; i < collapse; i++)
10624 if (TREE_VEC_ELT (declv, i) == OMP_CLAUSE_DECL (*c))
10625 break;
10626 if (i == collapse)
10627 c = &OMP_CLAUSE_CHAIN (*c);
10628 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE)
10630 error_at (loc,
10631 "iteration variable %qD should not be firstprivate",
10632 OMP_CLAUSE_DECL (*c));
10633 *c = OMP_CLAUSE_CHAIN (*c);
10635 else
10637 /* Copy lastprivate (decl) clause to OMP_FOR_CLAUSES,
10638 change it to shared (decl) in
10639 OMP_PARALLEL_CLAUSES. */
10640 tree l = build_omp_clause (OMP_CLAUSE_LOCATION (*c),
10641 OMP_CLAUSE_LASTPRIVATE);
10642 OMP_CLAUSE_DECL (l) = OMP_CLAUSE_DECL (*c);
10643 OMP_CLAUSE_CHAIN (l) = clauses;
10644 clauses = l;
10645 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
10649 OMP_FOR_CLAUSES (stmt) = clauses;
10651 ret = stmt;
10653 pop_scopes:
10654 while (!VEC_empty (tree, for_block))
10656 /* FIXME diagnostics: LOC below should be the actual location of
10657 this particular for block. We need to build a list of
10658 locations to go along with FOR_BLOCK. */
10659 stmt = c_end_compound_stmt (loc, VEC_pop (tree, for_block), true);
10660 add_stmt (stmt);
10662 release_tree_vector (for_block);
10663 return ret;
10666 /* OpenMP 2.5:
10667 #pragma omp for for-clause[optseq] new-line
10668 for-loop
10670 LOC is the location of the #pragma token.
10673 #define OMP_FOR_CLAUSE_MASK \
10674 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
10675 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
10676 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
10677 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
10678 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \
10679 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \
10680 | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE) \
10681 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
10683 static tree
10684 c_parser_omp_for (location_t loc, c_parser *parser)
10686 tree block, clauses, ret;
10688 clauses = c_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
10689 "#pragma omp for");
10691 block = c_begin_compound_stmt (true);
10692 ret = c_parser_omp_for_loop (loc, parser, clauses, NULL);
10693 block = c_end_compound_stmt (loc, block, true);
10694 add_stmt (block);
10696 return ret;
10699 /* OpenMP 2.5:
10700 # pragma omp master new-line
10701 structured-block
10703 LOC is the location of the #pragma token.
10706 static tree
10707 c_parser_omp_master (location_t loc, c_parser *parser)
10709 c_parser_skip_to_pragma_eol (parser);
10710 return c_finish_omp_master (loc, c_parser_omp_structured_block (parser));
10713 /* OpenMP 2.5:
10714 # pragma omp ordered new-line
10715 structured-block
10717 LOC is the location of the #pragma itself.
10720 static tree
10721 c_parser_omp_ordered (location_t loc, c_parser *parser)
10723 c_parser_skip_to_pragma_eol (parser);
10724 return c_finish_omp_ordered (loc, c_parser_omp_structured_block (parser));
10727 /* OpenMP 2.5:
10729 section-scope:
10730 { section-sequence }
10732 section-sequence:
10733 section-directive[opt] structured-block
10734 section-sequence section-directive structured-block
10736 SECTIONS_LOC is the location of the #pragma omp sections. */
10738 static tree
10739 c_parser_omp_sections_scope (location_t sections_loc, c_parser *parser)
10741 tree stmt, substmt;
10742 bool error_suppress = false;
10743 location_t loc;
10745 loc = c_parser_peek_token (parser)->location;
10746 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
10748 /* Avoid skipping until the end of the block. */
10749 parser->error = false;
10750 return NULL_TREE;
10753 stmt = push_stmt_list ();
10755 if (c_parser_peek_token (parser)->pragma_kind != PRAGMA_OMP_SECTION)
10757 substmt = push_stmt_list ();
10759 while (1)
10761 c_parser_statement (parser);
10763 if (c_parser_peek_token (parser)->pragma_kind == PRAGMA_OMP_SECTION)
10764 break;
10765 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
10766 break;
10767 if (c_parser_next_token_is (parser, CPP_EOF))
10768 break;
10771 substmt = pop_stmt_list (substmt);
10772 substmt = build1 (OMP_SECTION, void_type_node, substmt);
10773 SET_EXPR_LOCATION (substmt, loc);
10774 add_stmt (substmt);
10777 while (1)
10779 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
10780 break;
10781 if (c_parser_next_token_is (parser, CPP_EOF))
10782 break;
10784 loc = c_parser_peek_token (parser)->location;
10785 if (c_parser_peek_token (parser)->pragma_kind == PRAGMA_OMP_SECTION)
10787 c_parser_consume_pragma (parser);
10788 c_parser_skip_to_pragma_eol (parser);
10789 error_suppress = false;
10791 else if (!error_suppress)
10793 error_at (loc, "expected %<#pragma omp section%> or %<}%>");
10794 error_suppress = true;
10797 substmt = c_parser_omp_structured_block (parser);
10798 substmt = build1 (OMP_SECTION, void_type_node, substmt);
10799 SET_EXPR_LOCATION (substmt, loc);
10800 add_stmt (substmt);
10802 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE,
10803 "expected %<#pragma omp section%> or %<}%>");
10805 substmt = pop_stmt_list (stmt);
10807 stmt = make_node (OMP_SECTIONS);
10808 SET_EXPR_LOCATION (stmt, sections_loc);
10809 TREE_TYPE (stmt) = void_type_node;
10810 OMP_SECTIONS_BODY (stmt) = substmt;
10812 return add_stmt (stmt);
10815 /* OpenMP 2.5:
10816 # pragma omp sections sections-clause[optseq] newline
10817 sections-scope
10819 LOC is the location of the #pragma token.
10822 #define OMP_SECTIONS_CLAUSE_MASK \
10823 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
10824 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
10825 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
10826 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
10827 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
10829 static tree
10830 c_parser_omp_sections (location_t loc, c_parser *parser)
10832 tree block, clauses, ret;
10834 clauses = c_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
10835 "#pragma omp sections");
10837 block = c_begin_compound_stmt (true);
10838 ret = c_parser_omp_sections_scope (loc, parser);
10839 if (ret)
10840 OMP_SECTIONS_CLAUSES (ret) = clauses;
10841 block = c_end_compound_stmt (loc, block, true);
10842 add_stmt (block);
10844 return ret;
10847 /* OpenMP 2.5:
10848 # pragma parallel parallel-clause new-line
10849 # pragma parallel for parallel-for-clause new-line
10850 # pragma parallel sections parallel-sections-clause new-line
10852 LOC is the location of the #pragma token.
10855 #define OMP_PARALLEL_CLAUSE_MASK \
10856 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
10857 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
10858 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
10859 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
10860 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
10861 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \
10862 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
10863 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
10865 static tree
10866 c_parser_omp_parallel (location_t loc, c_parser *parser)
10868 enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
10869 const char *p_name = "#pragma omp parallel";
10870 tree stmt, clauses, par_clause, ws_clause, block;
10871 unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
10873 if (c_parser_next_token_is_keyword (parser, RID_FOR))
10875 c_parser_consume_token (parser);
10876 p_kind = PRAGMA_OMP_PARALLEL_FOR;
10877 p_name = "#pragma omp parallel for";
10878 mask |= OMP_FOR_CLAUSE_MASK;
10879 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
10881 else if (c_parser_next_token_is (parser, CPP_NAME))
10883 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
10884 if (strcmp (p, "sections") == 0)
10886 c_parser_consume_token (parser);
10887 p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
10888 p_name = "#pragma omp parallel sections";
10889 mask |= OMP_SECTIONS_CLAUSE_MASK;
10890 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
10894 clauses = c_parser_omp_all_clauses (parser, mask, p_name);
10896 switch (p_kind)
10898 case PRAGMA_OMP_PARALLEL:
10899 block = c_begin_omp_parallel ();
10900 c_parser_statement (parser);
10901 stmt = c_finish_omp_parallel (loc, clauses, block);
10902 break;
10904 case PRAGMA_OMP_PARALLEL_FOR:
10905 block = c_begin_omp_parallel ();
10906 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
10907 c_parser_omp_for_loop (loc, parser, ws_clause, &par_clause);
10908 stmt = c_finish_omp_parallel (loc, par_clause, block);
10909 OMP_PARALLEL_COMBINED (stmt) = 1;
10910 break;
10912 case PRAGMA_OMP_PARALLEL_SECTIONS:
10913 block = c_begin_omp_parallel ();
10914 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
10915 stmt = c_parser_omp_sections_scope (loc, parser);
10916 if (stmt)
10917 OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
10918 stmt = c_finish_omp_parallel (loc, par_clause, block);
10919 OMP_PARALLEL_COMBINED (stmt) = 1;
10920 break;
10922 default:
10923 gcc_unreachable ();
10926 return stmt;
10929 /* OpenMP 2.5:
10930 # pragma omp single single-clause[optseq] new-line
10931 structured-block
10933 LOC is the location of the #pragma.
10936 #define OMP_SINGLE_CLAUSE_MASK \
10937 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
10938 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
10939 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
10940 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
10942 static tree
10943 c_parser_omp_single (location_t loc, c_parser *parser)
10945 tree stmt = make_node (OMP_SINGLE);
10946 SET_EXPR_LOCATION (stmt, loc);
10947 TREE_TYPE (stmt) = void_type_node;
10949 OMP_SINGLE_CLAUSES (stmt)
10950 = c_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
10951 "#pragma omp single");
10952 OMP_SINGLE_BODY (stmt) = c_parser_omp_structured_block (parser);
10954 return add_stmt (stmt);
10957 /* OpenMP 3.0:
10958 # pragma omp task task-clause[optseq] new-line
10960 LOC is the location of the #pragma.
10963 #define OMP_TASK_CLAUSE_MASK \
10964 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
10965 | (1u << PRAGMA_OMP_CLAUSE_UNTIED) \
10966 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
10967 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
10968 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
10969 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
10970 | (1u << PRAGMA_OMP_CLAUSE_FINAL) \
10971 | (1u << PRAGMA_OMP_CLAUSE_MERGEABLE))
10973 static tree
10974 c_parser_omp_task (location_t loc, c_parser *parser)
10976 tree clauses, block;
10978 clauses = c_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
10979 "#pragma omp task");
10981 block = c_begin_omp_task ();
10982 c_parser_statement (parser);
10983 return c_finish_omp_task (loc, clauses, block);
10986 /* OpenMP 3.0:
10987 # pragma omp taskwait new-line
10990 static void
10991 c_parser_omp_taskwait (c_parser *parser)
10993 location_t loc = c_parser_peek_token (parser)->location;
10994 c_parser_consume_pragma (parser);
10995 c_parser_skip_to_pragma_eol (parser);
10997 c_finish_omp_taskwait (loc);
11000 /* OpenMP 3.1:
11001 # pragma omp taskyield new-line
11004 static void
11005 c_parser_omp_taskyield (c_parser *parser)
11007 location_t loc = c_parser_peek_token (parser)->location;
11008 c_parser_consume_pragma (parser);
11009 c_parser_skip_to_pragma_eol (parser);
11011 c_finish_omp_taskyield (loc);
11014 /* Main entry point to parsing most OpenMP pragmas. */
11016 static void
11017 c_parser_omp_construct (c_parser *parser)
11019 enum pragma_kind p_kind;
11020 location_t loc;
11021 tree stmt;
11023 loc = c_parser_peek_token (parser)->location;
11024 p_kind = c_parser_peek_token (parser)->pragma_kind;
11025 c_parser_consume_pragma (parser);
11027 switch (p_kind)
11029 case PRAGMA_OMP_ATOMIC:
11030 c_parser_omp_atomic (loc, parser);
11031 return;
11032 case PRAGMA_OMP_CRITICAL:
11033 stmt = c_parser_omp_critical (loc, parser);
11034 break;
11035 case PRAGMA_OMP_FOR:
11036 stmt = c_parser_omp_for (loc, parser);
11037 break;
11038 case PRAGMA_OMP_MASTER:
11039 stmt = c_parser_omp_master (loc, parser);
11040 break;
11041 case PRAGMA_OMP_ORDERED:
11042 stmt = c_parser_omp_ordered (loc, parser);
11043 break;
11044 case PRAGMA_OMP_PARALLEL:
11045 stmt = c_parser_omp_parallel (loc, parser);
11046 break;
11047 case PRAGMA_OMP_SECTIONS:
11048 stmt = c_parser_omp_sections (loc, parser);
11049 break;
11050 case PRAGMA_OMP_SINGLE:
11051 stmt = c_parser_omp_single (loc, parser);
11052 break;
11053 case PRAGMA_OMP_TASK:
11054 stmt = c_parser_omp_task (loc, parser);
11055 break;
11056 default:
11057 gcc_unreachable ();
11060 if (stmt)
11061 gcc_assert (EXPR_LOCATION (stmt) != UNKNOWN_LOCATION);
11065 /* OpenMP 2.5:
11066 # pragma omp threadprivate (variable-list) */
11068 static void
11069 c_parser_omp_threadprivate (c_parser *parser)
11071 tree vars, t;
11072 location_t loc;
11074 c_parser_consume_pragma (parser);
11075 loc = c_parser_peek_token (parser)->location;
11076 vars = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
11078 /* Mark every variable in VARS to be assigned thread local storage. */
11079 for (t = vars; t; t = TREE_CHAIN (t))
11081 tree v = TREE_PURPOSE (t);
11083 /* FIXME diagnostics: Ideally we should keep individual
11084 locations for all the variables in the var list to make the
11085 following errors more precise. Perhaps
11086 c_parser_omp_var_list_parens() should construct a list of
11087 locations to go along with the var list. */
11089 /* If V had already been marked threadprivate, it doesn't matter
11090 whether it had been used prior to this point. */
11091 if (TREE_CODE (v) != VAR_DECL)
11092 error_at (loc, "%qD is not a variable", v);
11093 else if (TREE_USED (v) && !C_DECL_THREADPRIVATE_P (v))
11094 error_at (loc, "%qE declared %<threadprivate%> after first use", v);
11095 else if (! TREE_STATIC (v) && ! DECL_EXTERNAL (v))
11096 error_at (loc, "automatic variable %qE cannot be %<threadprivate%>", v);
11097 else if (TREE_TYPE (v) == error_mark_node)
11099 else if (! COMPLETE_TYPE_P (TREE_TYPE (v)))
11100 error_at (loc, "%<threadprivate%> %qE has incomplete type", v);
11101 else
11103 if (! DECL_THREAD_LOCAL_P (v))
11105 DECL_TLS_MODEL (v) = decl_default_tls_model (v);
11106 /* If rtl has been already set for this var, call
11107 make_decl_rtl once again, so that encode_section_info
11108 has a chance to look at the new decl flags. */
11109 if (DECL_RTL_SET_P (v))
11110 make_decl_rtl (v);
11112 C_DECL_THREADPRIVATE_P (v) = 1;
11116 c_parser_skip_to_pragma_eol (parser);
11119 /* Parse a transaction attribute (GCC Extension).
11121 transaction-attribute:
11122 attributes
11123 [ [ any-word ] ]
11125 The transactional memory language description is written for C++,
11126 and uses the C++0x attribute syntax. For compatibility, allow the
11127 bracket style for transactions in C as well. */
11129 static tree
11130 c_parser_transaction_attributes (c_parser *parser)
11132 tree attr_name, attr = NULL;
11134 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
11135 return c_parser_attributes (parser);
11137 if (!c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
11138 return NULL_TREE;
11139 c_parser_consume_token (parser);
11140 if (!c_parser_require (parser, CPP_OPEN_SQUARE, "expected %<[%>"))
11141 goto error1;
11143 attr_name = c_parser_attribute_any_word (parser);
11144 if (attr_name)
11146 c_parser_consume_token (parser);
11147 attr = build_tree_list (attr_name, NULL_TREE);
11149 else
11150 c_parser_error (parser, "expected identifier");
11152 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, "expected %<]%>");
11153 error1:
11154 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, "expected %<]%>");
11155 return attr;
11158 /* Parse a __transaction_atomic or __transaction_relaxed statement
11159 (GCC Extension).
11161 transaction-statement:
11162 __transaction_atomic transaction-attribute[opt] compound-statement
11163 __transaction_relaxed compound-statement
11165 Note that the only valid attribute is: "outer".
11168 static tree
11169 c_parser_transaction (c_parser *parser, enum rid keyword)
11171 unsigned int old_in = parser->in_transaction;
11172 unsigned int this_in = 1, new_in;
11173 location_t loc = c_parser_peek_token (parser)->location;
11174 tree stmt, attrs;
11176 gcc_assert ((keyword == RID_TRANSACTION_ATOMIC
11177 || keyword == RID_TRANSACTION_RELAXED)
11178 && c_parser_next_token_is_keyword (parser, keyword));
11179 c_parser_consume_token (parser);
11181 if (keyword == RID_TRANSACTION_RELAXED)
11182 this_in |= TM_STMT_ATTR_RELAXED;
11183 else
11185 attrs = c_parser_transaction_attributes (parser);
11186 if (attrs)
11187 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
11190 /* Keep track if we're in the lexical scope of an outer transaction. */
11191 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
11193 parser->in_transaction = new_in;
11194 stmt = c_parser_compound_statement (parser);
11195 parser->in_transaction = old_in;
11197 if (flag_tm)
11198 stmt = c_finish_transaction (loc, stmt, this_in);
11199 else
11200 error_at (loc, (keyword == RID_TRANSACTION_ATOMIC ?
11201 "%<__transaction_atomic%> without transactional memory support enabled"
11202 : "%<__transaction_relaxed %> "
11203 "without transactional memory support enabled"));
11205 return stmt;
11208 /* Parse a __transaction_atomic or __transaction_relaxed expression
11209 (GCC Extension).
11211 transaction-expression:
11212 __transaction_atomic ( expression )
11213 __transaction_relaxed ( expression )
11216 static struct c_expr
11217 c_parser_transaction_expression (c_parser *parser, enum rid keyword)
11219 struct c_expr ret;
11220 unsigned int old_in = parser->in_transaction;
11221 unsigned int this_in = 1;
11222 location_t loc = c_parser_peek_token (parser)->location;
11223 tree attrs;
11225 gcc_assert ((keyword == RID_TRANSACTION_ATOMIC
11226 || keyword == RID_TRANSACTION_RELAXED)
11227 && c_parser_next_token_is_keyword (parser, keyword));
11228 c_parser_consume_token (parser);
11230 if (keyword == RID_TRANSACTION_RELAXED)
11231 this_in |= TM_STMT_ATTR_RELAXED;
11232 else
11234 attrs = c_parser_transaction_attributes (parser);
11235 if (attrs)
11236 this_in |= parse_tm_stmt_attr (attrs, 0);
11239 parser->in_transaction = this_in;
11240 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11242 tree expr = c_parser_expression (parser).value;
11243 ret.original_type = TREE_TYPE (expr);
11244 ret.value = build1 (TRANSACTION_EXPR, ret.original_type, expr);
11245 if (this_in & TM_STMT_ATTR_RELAXED)
11246 TRANSACTION_EXPR_RELAXED (ret.value) = 1;
11247 SET_EXPR_LOCATION (ret.value, loc);
11248 ret.original_code = TRANSACTION_EXPR;
11249 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
11251 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
11252 goto error;
11255 else
11257 error:
11258 ret.value = error_mark_node;
11259 ret.original_code = ERROR_MARK;
11260 ret.original_type = NULL;
11262 parser->in_transaction = old_in;
11264 if (!flag_tm)
11265 error_at (loc, (keyword == RID_TRANSACTION_ATOMIC ?
11266 "%<__transaction_atomic%> without transactional memory support enabled"
11267 : "%<__transaction_relaxed %> "
11268 "without transactional memory support enabled"));
11270 return ret;
11273 /* Parse a __transaction_cancel statement (GCC Extension).
11275 transaction-cancel-statement:
11276 __transaction_cancel transaction-attribute[opt] ;
11278 Note that the only valid attribute is "outer".
11281 static tree
11282 c_parser_transaction_cancel(c_parser *parser)
11284 location_t loc = c_parser_peek_token (parser)->location;
11285 tree attrs;
11286 bool is_outer = false;
11288 gcc_assert (c_parser_next_token_is_keyword (parser, RID_TRANSACTION_CANCEL));
11289 c_parser_consume_token (parser);
11291 attrs = c_parser_transaction_attributes (parser);
11292 if (attrs)
11293 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
11295 if (!flag_tm)
11297 error_at (loc, "%<__transaction_cancel%> without "
11298 "transactional memory support enabled");
11299 goto ret_error;
11301 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
11303 error_at (loc, "%<__transaction_cancel%> within a "
11304 "%<__transaction_relaxed%>");
11305 goto ret_error;
11307 else if (is_outer)
11309 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
11310 && !is_tm_may_cancel_outer (current_function_decl))
11312 error_at (loc, "outer %<__transaction_cancel%> not "
11313 "within outer %<__transaction_atomic%>");
11314 error_at (loc, " or a %<transaction_may_cancel_outer%> function");
11315 goto ret_error;
11318 else if (parser->in_transaction == 0)
11320 error_at (loc, "%<__transaction_cancel%> not within "
11321 "%<__transaction_atomic%>");
11322 goto ret_error;
11325 return add_stmt (build_tm_abort_call (loc, is_outer));
11327 ret_error:
11328 return build1 (NOP_EXPR, void_type_node, error_mark_node);
11331 /* Parse a single source file. */
11333 void
11334 c_parse_file (void)
11336 /* Use local storage to begin. If the first token is a pragma, parse it.
11337 If it is #pragma GCC pch_preprocess, then this will load a PCH file
11338 which will cause garbage collection. */
11339 c_parser tparser;
11341 memset (&tparser, 0, sizeof tparser);
11342 the_parser = &tparser;
11344 if (c_parser_peek_token (&tparser)->pragma_kind == PRAGMA_GCC_PCH_PREPROCESS)
11345 c_parser_pragma_pch_preprocess (&tparser);
11347 the_parser = ggc_alloc_c_parser ();
11348 *the_parser = tparser;
11350 /* Initialize EH, if we've been told to do so. */
11351 if (flag_exceptions)
11352 using_eh_for_cleanups ();
11354 c_parser_translation_unit (the_parser);
11355 the_parser = NULL;
11358 #include "gt-c-c-parser.h"