Merge trunk version 193617 into gupc branch.
[official-gcc.git] / gcc / c / c-parser.c
blobd2fed09c450129bef9caa9da5d3f31dc61d45b19
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 static void c_parser_external_declaration (c_parser *);
1136 static void c_parser_asm_definition (c_parser *);
1137 static void c_parser_declaration_or_fndef (c_parser *, bool, bool, bool,
1138 bool, bool, tree *);
1139 static void c_parser_static_assert_declaration_no_semi (c_parser *);
1140 static void c_parser_static_assert_declaration (c_parser *);
1141 static void c_parser_declspecs (c_parser *, struct c_declspecs *, bool, bool,
1142 bool, enum c_lookahead_kind);
1143 static struct c_typespec c_parser_enum_specifier (c_parser *);
1144 static struct c_typespec c_parser_struct_or_union_specifier (c_parser *);
1145 static tree c_parser_struct_declaration (c_parser *);
1146 static struct c_typespec c_parser_typeof_specifier (c_parser *);
1147 static tree c_parser_alignas_specifier (c_parser *);
1148 static struct c_declarator *c_parser_declarator (c_parser *, bool, c_dtr_syn,
1149 bool *);
1150 static struct c_declarator *c_parser_direct_declarator (c_parser *, bool,
1151 c_dtr_syn, bool *);
1152 static struct c_declarator *c_parser_direct_declarator_inner (c_parser *,
1153 bool,
1154 struct c_declarator *);
1155 static struct c_arg_info *c_parser_parms_declarator (c_parser *, bool, tree);
1156 static struct c_arg_info *c_parser_parms_list_declarator (c_parser *, tree,
1157 tree);
1158 static struct c_parm *c_parser_parameter_declaration (c_parser *, tree);
1159 static tree c_parser_simple_asm_expr (c_parser *);
1160 static tree c_parser_attributes (c_parser *);
1161 static struct c_type_name *c_parser_type_name (c_parser *);
1162 static struct c_expr c_parser_initializer (c_parser *);
1163 static struct c_expr c_parser_braced_init (c_parser *, tree, bool);
1164 static void c_parser_initelt (c_parser *, struct obstack *);
1165 static void c_parser_initval (c_parser *, struct c_expr *,
1166 struct obstack *);
1167 static tree c_parser_compound_statement (c_parser *);
1168 static void c_parser_compound_statement_nostart (c_parser *);
1169 static void c_parser_label (c_parser *);
1170 static void c_parser_statement (c_parser *);
1171 static void c_parser_statement_after_labels (c_parser *);
1172 static void c_parser_if_statement (c_parser *);
1173 static void c_parser_switch_statement (c_parser *);
1174 static void c_parser_while_statement (c_parser *);
1175 static void c_parser_do_statement (c_parser *);
1176 static void c_parser_for_statement (c_parser *);
1177 static tree c_parser_asm_statement (c_parser *);
1178 static tree c_parser_asm_operands (c_parser *, bool);
1179 static tree c_parser_asm_goto_operands (c_parser *);
1180 static tree c_parser_asm_clobbers (c_parser *);
1181 static struct c_expr c_parser_expr_no_commas (c_parser *, struct c_expr *);
1182 static struct c_expr c_parser_conditional_expression (c_parser *,
1183 struct c_expr *);
1184 static struct c_expr c_parser_binary_expression (c_parser *, struct c_expr *,
1185 enum c_parser_prec);
1186 static struct c_expr c_parser_cast_expression (c_parser *, struct c_expr *);
1187 static struct c_expr c_parser_unary_expression (c_parser *);
1188 static struct c_expr c_parser_sizeof_expression (c_parser *);
1189 static struct c_expr c_parser_alignof_expression (c_parser *);
1190 static struct c_expr c_parser_postfix_expression (c_parser *);
1191 static struct c_expr c_parser_postfix_expression_after_paren_type (c_parser *,
1192 struct c_type_name *,
1193 location_t);
1194 static struct c_expr c_parser_postfix_expression_after_primary (c_parser *,
1195 location_t loc,
1196 struct c_expr);
1197 static tree c_parser_transaction (c_parser *, enum rid);
1198 static struct c_expr c_parser_transaction_expression (c_parser *, enum rid);
1199 static tree c_parser_transaction_cancel (c_parser *);
1200 static struct c_expr c_parser_expression (c_parser *);
1201 static struct c_expr c_parser_expression_conv (c_parser *);
1202 static vec<tree, va_gc> *c_parser_expr_list (c_parser *, bool, bool,
1203 vec<tree, va_gc> **, location_t *,
1204 tree *);
1205 static void c_parser_omp_construct (c_parser *);
1206 static void c_parser_omp_threadprivate (c_parser *);
1207 static void c_parser_omp_barrier (c_parser *);
1208 static void c_parser_omp_flush (c_parser *);
1209 static void c_parser_omp_taskwait (c_parser *);
1210 static void c_parser_omp_taskyield (c_parser *);
1212 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
1213 static bool c_parser_pragma (c_parser *, enum pragma_context);
1215 /* These Objective-C parser functions are only ever called when
1216 compiling Objective-C. */
1217 static void c_parser_objc_class_definition (c_parser *, tree);
1218 static void c_parser_objc_class_instance_variables (c_parser *);
1219 static void c_parser_objc_class_declaration (c_parser *);
1220 static void c_parser_objc_alias_declaration (c_parser *);
1221 static void c_parser_objc_protocol_definition (c_parser *, tree);
1222 static bool c_parser_objc_method_type (c_parser *);
1223 static void c_parser_objc_method_definition (c_parser *);
1224 static void c_parser_objc_methodprotolist (c_parser *);
1225 static void c_parser_objc_methodproto (c_parser *);
1226 static tree c_parser_objc_method_decl (c_parser *, bool, tree *, tree *);
1227 static tree c_parser_objc_type_name (c_parser *);
1228 static tree c_parser_objc_protocol_refs (c_parser *);
1229 static void c_parser_objc_try_catch_finally_statement (c_parser *);
1230 static void c_parser_objc_synchronized_statement (c_parser *);
1231 static tree c_parser_objc_selector (c_parser *);
1232 static tree c_parser_objc_selector_arg (c_parser *);
1233 static tree c_parser_objc_receiver (c_parser *);
1234 static tree c_parser_objc_message_args (c_parser *);
1235 static tree c_parser_objc_keywordexpr (c_parser *);
1236 static void c_parser_objc_at_property_declaration (c_parser *);
1237 static void c_parser_objc_at_synthesize_declaration (c_parser *);
1238 static void c_parser_objc_at_dynamic_declaration (c_parser *);
1239 static bool c_parser_objc_diagnose_bad_element_prefix
1240 (c_parser *, struct c_declspecs *);
1242 /* These UPC parser functions are only ever called when
1243 compiling UPC. */
1244 static void c_parser_upc_forall_statement (c_parser *);
1245 static void c_parser_upc_sync_statement (c_parser *, int);
1246 static void c_parser_upc_shared_qual (source_location,
1247 c_parser *,
1248 struct c_declspecs *);
1250 /* Parse a translation unit (C90 6.7, C99 6.9).
1252 translation-unit:
1253 external-declarations
1255 external-declarations:
1256 external-declaration
1257 external-declarations external-declaration
1259 GNU extensions:
1261 translation-unit:
1262 empty
1265 static void
1266 c_parser_translation_unit (c_parser *parser)
1268 if (c_parser_next_token_is (parser, CPP_EOF))
1270 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
1271 "ISO C forbids an empty translation unit");
1273 else
1275 void *obstack_position = obstack_alloc (&parser_obstack, 0);
1276 mark_valid_location_for_stdc_pragma (false);
1279 ggc_collect ();
1280 c_parser_external_declaration (parser);
1281 obstack_free (&parser_obstack, obstack_position);
1283 while (c_parser_next_token_is_not (parser, CPP_EOF));
1287 /* Parse an external declaration (C90 6.7, C99 6.9).
1289 external-declaration:
1290 function-definition
1291 declaration
1293 GNU extensions:
1295 external-declaration:
1296 asm-definition
1298 __extension__ external-declaration
1300 Objective-C:
1302 external-declaration:
1303 objc-class-definition
1304 objc-class-declaration
1305 objc-alias-declaration
1306 objc-protocol-definition
1307 objc-method-definition
1308 @end
1311 static void
1312 c_parser_external_declaration (c_parser *parser)
1314 int ext;
1315 switch (c_parser_peek_token (parser)->type)
1317 case CPP_KEYWORD:
1318 switch (c_parser_peek_token (parser)->keyword)
1320 case RID_EXTENSION:
1321 ext = disable_extension_diagnostics ();
1322 c_parser_consume_token (parser);
1323 c_parser_external_declaration (parser);
1324 restore_extension_diagnostics (ext);
1325 break;
1326 case RID_ASM:
1327 c_parser_asm_definition (parser);
1328 break;
1329 case RID_AT_INTERFACE:
1330 case RID_AT_IMPLEMENTATION:
1331 gcc_assert (c_dialect_objc ());
1332 c_parser_objc_class_definition (parser, NULL_TREE);
1333 break;
1334 case RID_AT_CLASS:
1335 gcc_assert (c_dialect_objc ());
1336 c_parser_objc_class_declaration (parser);
1337 break;
1338 case RID_AT_ALIAS:
1339 gcc_assert (c_dialect_objc ());
1340 c_parser_objc_alias_declaration (parser);
1341 break;
1342 case RID_AT_PROTOCOL:
1343 gcc_assert (c_dialect_objc ());
1344 c_parser_objc_protocol_definition (parser, NULL_TREE);
1345 break;
1346 case RID_AT_PROPERTY:
1347 gcc_assert (c_dialect_objc ());
1348 c_parser_objc_at_property_declaration (parser);
1349 break;
1350 case RID_AT_SYNTHESIZE:
1351 gcc_assert (c_dialect_objc ());
1352 c_parser_objc_at_synthesize_declaration (parser);
1353 break;
1354 case RID_AT_DYNAMIC:
1355 gcc_assert (c_dialect_objc ());
1356 c_parser_objc_at_dynamic_declaration (parser);
1357 break;
1358 case RID_AT_END:
1359 gcc_assert (c_dialect_objc ());
1360 c_parser_consume_token (parser);
1361 objc_finish_implementation ();
1362 break;
1363 default:
1364 goto decl_or_fndef;
1366 break;
1367 case CPP_SEMICOLON:
1368 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
1369 "ISO C does not allow extra %<;%> outside of a function");
1370 c_parser_consume_token (parser);
1371 break;
1372 case CPP_PRAGMA:
1373 mark_valid_location_for_stdc_pragma (true);
1374 c_parser_pragma (parser, pragma_external);
1375 mark_valid_location_for_stdc_pragma (false);
1376 break;
1377 case CPP_PLUS:
1378 case CPP_MINUS:
1379 if (c_dialect_objc ())
1381 c_parser_objc_method_definition (parser);
1382 break;
1384 /* Else fall through, and yield a syntax error trying to parse
1385 as a declaration or function definition. */
1386 default:
1387 decl_or_fndef:
1388 /* A declaration or a function definition (or, in Objective-C,
1389 an @interface or @protocol with prefix attributes). We can
1390 only tell which after parsing the declaration specifiers, if
1391 any, and the first declarator. */
1392 c_parser_declaration_or_fndef (parser, true, true, true, false, true, NULL);
1393 break;
1397 /* Parse a declaration or function definition (C90 6.5, 6.7.1, C99
1398 6.7, 6.9.1). If FNDEF_OK is true, a function definition is
1399 accepted; otherwise (old-style parameter declarations) only other
1400 declarations are accepted. If STATIC_ASSERT_OK is true, a static
1401 assertion is accepted; otherwise (old-style parameter declarations)
1402 it is not. If NESTED is true, we are inside a function or parsing
1403 old-style parameter declarations; any functions encountered are
1404 nested functions and declaration specifiers are required; otherwise
1405 we are at top level and functions are normal functions and
1406 declaration specifiers may be optional. If EMPTY_OK is true, empty
1407 declarations are OK (subject to all other constraints); otherwise
1408 (old-style parameter declarations) they are diagnosed. If
1409 START_ATTR_OK is true, the declaration specifiers may start with
1410 attributes; otherwise they may not.
1411 OBJC_FOREACH_OBJECT_DECLARATION can be used to get back the parsed
1412 declaration when parsing an Objective-C foreach statement.
1414 declaration:
1415 declaration-specifiers init-declarator-list[opt] ;
1416 static_assert-declaration
1418 function-definition:
1419 declaration-specifiers[opt] declarator declaration-list[opt]
1420 compound-statement
1422 declaration-list:
1423 declaration
1424 declaration-list declaration
1426 init-declarator-list:
1427 init-declarator
1428 init-declarator-list , init-declarator
1430 init-declarator:
1431 declarator simple-asm-expr[opt] attributes[opt]
1432 declarator simple-asm-expr[opt] attributes[opt] = initializer
1434 GNU extensions:
1436 nested-function-definition:
1437 declaration-specifiers declarator declaration-list[opt]
1438 compound-statement
1440 Objective-C:
1441 attributes objc-class-definition
1442 attributes objc-category-definition
1443 attributes objc-protocol-definition
1445 The simple-asm-expr and attributes are GNU extensions.
1447 This function does not handle __extension__; that is handled in its
1448 callers. ??? Following the old parser, __extension__ may start
1449 external declarations, declarations in functions and declarations
1450 at the start of "for" loops, but not old-style parameter
1451 declarations.
1453 C99 requires declaration specifiers in a function definition; the
1454 absence is diagnosed through the diagnosis of implicit int. In GNU
1455 C we also allow but diagnose declarations without declaration
1456 specifiers, but only at top level (elsewhere they conflict with
1457 other syntax).
1459 In Objective-C, declarations of the looping variable in a foreach
1460 statement are exceptionally terminated by 'in' (for example, 'for
1461 (NSObject *object in array) { ... }').
1463 OpenMP:
1465 declaration:
1466 threadprivate-directive */
1468 static void
1469 c_parser_declaration_or_fndef (c_parser *parser, bool fndef_ok,
1470 bool static_assert_ok, bool empty_ok,
1471 bool nested, bool start_attr_ok,
1472 tree *objc_foreach_object_declaration)
1474 struct c_declspecs *specs;
1475 tree prefix_attrs;
1476 tree all_prefix_attrs;
1477 bool diagnosed_no_specs = false;
1478 location_t here = c_parser_peek_token (parser)->location;
1480 if (static_assert_ok
1481 && c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT))
1483 c_parser_static_assert_declaration (parser);
1484 return;
1486 specs = build_null_declspecs ();
1488 /* Try to detect an unknown type name when we have "A B" or "A *B". */
1489 if (c_parser_peek_token (parser)->type == CPP_NAME
1490 && c_parser_peek_token (parser)->id_kind == C_ID_ID
1491 && (c_parser_peek_2nd_token (parser)->type == CPP_NAME
1492 || c_parser_peek_2nd_token (parser)->type == CPP_MULT)
1493 && (!nested || !lookup_name (c_parser_peek_token (parser)->value)))
1495 error_at (here, "unknown type name %qE",
1496 c_parser_peek_token (parser)->value);
1498 /* Parse declspecs normally to get a correct pointer type, but avoid
1499 a further "fails to be a type name" error. Refuse nested functions
1500 since it is not how the user likely wants us to recover. */
1501 c_parser_peek_token (parser)->type = CPP_KEYWORD;
1502 c_parser_peek_token (parser)->keyword = RID_VOID;
1503 c_parser_peek_token (parser)->value = error_mark_node;
1504 fndef_ok = !nested;
1507 c_parser_declspecs (parser, specs, true, true, start_attr_ok, cla_nonabstract_decl);
1508 if (parser->error)
1510 c_parser_skip_to_end_of_block_or_statement (parser);
1511 return;
1513 if (nested && !specs->declspecs_seen_p)
1515 c_parser_error (parser, "expected declaration specifiers");
1516 c_parser_skip_to_end_of_block_or_statement (parser);
1517 return;
1519 finish_declspecs (specs);
1520 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1522 if (empty_ok)
1523 shadow_tag (specs);
1524 else
1526 shadow_tag_warned (specs, 1);
1527 pedwarn (here, 0, "empty declaration");
1529 c_parser_consume_token (parser);
1530 return;
1533 /* Provide better error recovery. Note that a type name here is usually
1534 better diagnosed as a redeclaration. */
1535 if (empty_ok
1536 && specs->typespec_kind == ctsk_tagdef
1537 && c_parser_next_token_starts_declspecs (parser)
1538 && !c_parser_next_token_is (parser, CPP_NAME))
1540 c_parser_error (parser, "expected %<;%>, identifier or %<(%>");
1541 parser->error = false;
1542 shadow_tag_warned (specs, 1);
1543 return;
1545 else if (c_dialect_objc ())
1547 /* Prefix attributes are an error on method decls. */
1548 switch (c_parser_peek_token (parser)->type)
1550 case CPP_PLUS:
1551 case CPP_MINUS:
1552 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1553 return;
1554 if (specs->attrs)
1556 warning_at (c_parser_peek_token (parser)->location,
1557 OPT_Wattributes,
1558 "prefix attributes are ignored for methods");
1559 specs->attrs = NULL_TREE;
1561 if (fndef_ok)
1562 c_parser_objc_method_definition (parser);
1563 else
1564 c_parser_objc_methodproto (parser);
1565 return;
1566 break;
1567 default:
1568 break;
1570 /* This is where we parse 'attributes @interface ...',
1571 'attributes @implementation ...', 'attributes @protocol ...'
1572 (where attributes could be, for example, __attribute__
1573 ((deprecated)).
1575 switch (c_parser_peek_token (parser)->keyword)
1577 case RID_AT_INTERFACE:
1579 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1580 return;
1581 c_parser_objc_class_definition (parser, specs->attrs);
1582 return;
1584 break;
1585 case RID_AT_IMPLEMENTATION:
1587 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1588 return;
1589 if (specs->attrs)
1591 warning_at (c_parser_peek_token (parser)->location,
1592 OPT_Wattributes,
1593 "prefix attributes are ignored for implementations");
1594 specs->attrs = NULL_TREE;
1596 c_parser_objc_class_definition (parser, NULL_TREE);
1597 return;
1599 break;
1600 case RID_AT_PROTOCOL:
1602 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1603 return;
1604 c_parser_objc_protocol_definition (parser, specs->attrs);
1605 return;
1607 break;
1608 case RID_AT_ALIAS:
1609 case RID_AT_CLASS:
1610 case RID_AT_END:
1611 case RID_AT_PROPERTY:
1612 if (specs->attrs)
1614 c_parser_error (parser, "unexpected attribute");
1615 specs->attrs = NULL;
1617 break;
1618 default:
1619 break;
1623 pending_xref_error ();
1624 prefix_attrs = specs->attrs;
1625 all_prefix_attrs = prefix_attrs;
1626 specs->attrs = NULL_TREE;
1627 while (true)
1629 struct c_declarator *declarator;
1630 bool dummy = false;
1631 timevar_id_t tv;
1632 tree fnbody;
1633 /* Declaring either one or more declarators (in which case we
1634 should diagnose if there were no declaration specifiers) or a
1635 function definition (in which case the diagnostic for
1636 implicit int suffices). */
1637 declarator = c_parser_declarator (parser,
1638 specs->typespec_kind != ctsk_none,
1639 C_DTR_NORMAL, &dummy);
1640 if (declarator == NULL)
1642 c_parser_skip_to_end_of_block_or_statement (parser);
1643 return;
1645 if (c_parser_next_token_is (parser, CPP_EQ)
1646 || c_parser_next_token_is (parser, CPP_COMMA)
1647 || c_parser_next_token_is (parser, CPP_SEMICOLON)
1648 || c_parser_next_token_is_keyword (parser, RID_ASM)
1649 || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE)
1650 || c_parser_next_token_is_keyword (parser, RID_IN))
1652 tree asm_name = NULL_TREE;
1653 tree postfix_attrs = NULL_TREE;
1654 if (!diagnosed_no_specs && !specs->declspecs_seen_p)
1656 diagnosed_no_specs = true;
1657 pedwarn (here, 0, "data definition has no type or storage class");
1659 /* Having seen a data definition, there cannot now be a
1660 function definition. */
1661 fndef_ok = false;
1662 if (c_parser_next_token_is_keyword (parser, RID_ASM))
1663 asm_name = c_parser_simple_asm_expr (parser);
1664 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1665 postfix_attrs = c_parser_attributes (parser);
1666 if (c_parser_next_token_is (parser, CPP_EQ))
1668 tree d;
1669 struct c_expr init;
1670 location_t init_loc;
1671 c_parser_consume_token (parser);
1672 /* The declaration of the variable is in effect while
1673 its initializer is parsed. */
1674 d = start_decl (declarator, specs, true,
1675 chainon (postfix_attrs, all_prefix_attrs));
1676 if (!d)
1677 d = error_mark_node;
1678 start_init (d, asm_name, global_bindings_p ());
1679 init_loc = c_parser_peek_token (parser)->location;
1680 init = c_parser_initializer (parser);
1681 finish_init ();
1682 if (d != error_mark_node)
1684 maybe_warn_string_init (TREE_TYPE (d), init);
1685 finish_decl (d, init_loc, init.value,
1686 init.original_type, asm_name);
1689 else
1691 tree d = start_decl (declarator, specs, false,
1692 chainon (postfix_attrs,
1693 all_prefix_attrs));
1694 if (d)
1695 finish_decl (d, UNKNOWN_LOCATION, NULL_TREE,
1696 NULL_TREE, asm_name);
1698 if (c_parser_next_token_is_keyword (parser, RID_IN))
1700 if (d)
1701 *objc_foreach_object_declaration = d;
1702 else
1703 *objc_foreach_object_declaration = error_mark_node;
1706 if (c_parser_next_token_is (parser, CPP_COMMA))
1708 c_parser_consume_token (parser);
1709 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1710 all_prefix_attrs = chainon (c_parser_attributes (parser),
1711 prefix_attrs);
1712 else
1713 all_prefix_attrs = prefix_attrs;
1714 continue;
1716 else if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1718 c_parser_consume_token (parser);
1719 return;
1721 else if (c_parser_next_token_is_keyword (parser, RID_IN))
1723 /* This can only happen in Objective-C: we found the
1724 'in' that terminates the declaration inside an
1725 Objective-C foreach statement. Do not consume the
1726 token, so that the caller can use it to determine
1727 that this indeed is a foreach context. */
1728 return;
1730 else
1732 c_parser_error (parser, "expected %<,%> or %<;%>");
1733 c_parser_skip_to_end_of_block_or_statement (parser);
1734 return;
1737 else if (!fndef_ok)
1739 c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, "
1740 "%<asm%> or %<__attribute__%>");
1741 c_parser_skip_to_end_of_block_or_statement (parser);
1742 return;
1744 /* Function definition (nested or otherwise). */
1745 if (nested)
1747 pedwarn (here, OPT_Wpedantic, "ISO C forbids nested functions");
1748 c_push_function_context ();
1750 if (!start_function (specs, declarator, all_prefix_attrs))
1752 /* This can appear in many cases looking nothing like a
1753 function definition, so we don't give a more specific
1754 error suggesting there was one. */
1755 c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, %<asm%> "
1756 "or %<__attribute__%>");
1757 if (nested)
1758 c_pop_function_context ();
1759 break;
1762 if (DECL_DECLARED_INLINE_P (current_function_decl))
1763 tv = TV_PARSE_INLINE;
1764 else
1765 tv = TV_PARSE_FUNC;
1766 timevar_push (tv);
1768 /* Parse old-style parameter declarations. ??? Attributes are
1769 not allowed to start declaration specifiers here because of a
1770 syntax conflict between a function declaration with attribute
1771 suffix and a function definition with an attribute prefix on
1772 first old-style parameter declaration. Following the old
1773 parser, they are not accepted on subsequent old-style
1774 parameter declarations either. However, there is no
1775 ambiguity after the first declaration, nor indeed on the
1776 first as long as we don't allow postfix attributes after a
1777 declarator with a nonempty identifier list in a definition;
1778 and postfix attributes have never been accepted here in
1779 function definitions either. */
1780 while (c_parser_next_token_is_not (parser, CPP_EOF)
1781 && c_parser_next_token_is_not (parser, CPP_OPEN_BRACE))
1782 c_parser_declaration_or_fndef (parser, false, false, false,
1783 true, false, NULL);
1784 store_parm_decls ();
1785 DECL_STRUCT_FUNCTION (current_function_decl)->function_start_locus
1786 = c_parser_peek_token (parser)->location;
1787 fnbody = c_parser_compound_statement (parser);
1788 if (nested)
1790 tree decl = current_function_decl;
1791 /* Mark nested functions as needing static-chain initially.
1792 lower_nested_functions will recompute it but the
1793 DECL_STATIC_CHAIN flag is also used before that happens,
1794 by initializer_constant_valid_p. See gcc.dg/nested-fn-2.c. */
1795 DECL_STATIC_CHAIN (decl) = 1;
1796 add_stmt (fnbody);
1797 finish_function ();
1798 c_pop_function_context ();
1799 add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl));
1801 else
1803 add_stmt (fnbody);
1804 finish_function ();
1807 timevar_pop (tv);
1808 break;
1812 /* Parse an asm-definition (asm() outside a function body). This is a
1813 GNU extension.
1815 asm-definition:
1816 simple-asm-expr ;
1819 static void
1820 c_parser_asm_definition (c_parser *parser)
1822 tree asm_str = c_parser_simple_asm_expr (parser);
1823 if (asm_str)
1824 add_asm_node (asm_str);
1825 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
1828 /* Parse a static assertion (C11 6.7.10).
1830 static_assert-declaration:
1831 static_assert-declaration-no-semi ;
1834 static void
1835 c_parser_static_assert_declaration (c_parser *parser)
1837 c_parser_static_assert_declaration_no_semi (parser);
1838 if (parser->error
1839 || !c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
1840 c_parser_skip_to_end_of_block_or_statement (parser);
1843 /* Parse a static assertion (C11 6.7.10), without the trailing
1844 semicolon.
1846 static_assert-declaration-no-semi:
1847 _Static_assert ( constant-expression , string-literal )
1850 static void
1851 c_parser_static_assert_declaration_no_semi (c_parser *parser)
1853 location_t assert_loc, value_loc;
1854 tree value;
1855 tree string;
1857 gcc_assert (c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT));
1858 assert_loc = c_parser_peek_token (parser)->location;
1859 if (!flag_isoc11)
1861 if (flag_isoc99)
1862 pedwarn (assert_loc, OPT_Wpedantic,
1863 "ISO C99 does not support %<_Static_assert%>");
1864 else
1865 pedwarn (assert_loc, OPT_Wpedantic,
1866 "ISO C90 does not support %<_Static_assert%>");
1868 c_parser_consume_token (parser);
1869 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
1870 return;
1871 value_loc = c_parser_peek_token (parser)->location;
1872 value = c_parser_expr_no_commas (parser, NULL).value;
1873 parser->lex_untranslated_string = true;
1874 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
1876 parser->lex_untranslated_string = false;
1877 return;
1879 switch (c_parser_peek_token (parser)->type)
1881 case CPP_STRING:
1882 case CPP_STRING16:
1883 case CPP_STRING32:
1884 case CPP_WSTRING:
1885 case CPP_UTF8STRING:
1886 string = c_parser_peek_token (parser)->value;
1887 c_parser_consume_token (parser);
1888 parser->lex_untranslated_string = false;
1889 break;
1890 default:
1891 c_parser_error (parser, "expected string literal");
1892 parser->lex_untranslated_string = false;
1893 return;
1895 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
1897 if (!INTEGRAL_TYPE_P (TREE_TYPE (value)))
1899 error_at (value_loc, "expression in static assertion is not an integer");
1900 return;
1902 if (TREE_CODE (value) != INTEGER_CST)
1904 value = c_fully_fold (value, false, NULL);
1905 if (TREE_CODE (value) == INTEGER_CST)
1906 pedwarn (value_loc, OPT_Wpedantic, "expression in static assertion "
1907 "is not an integer constant expression");
1909 if (TREE_CODE (value) != INTEGER_CST)
1911 error_at (value_loc, "expression in static assertion is not constant");
1912 return;
1914 constant_expression_warning (value);
1915 if (integer_zerop (value))
1916 error_at (assert_loc, "static assertion failed: %E", string);
1919 /* Parse some declaration specifiers (possibly none) (C90 6.5, C99
1920 6.7), adding them to SPECS (which may already include some).
1921 Storage class specifiers are accepted iff SCSPEC_OK; type
1922 specifiers are accepted iff TYPESPEC_OK; attributes are accepted at
1923 the start iff START_ATTR_OK.
1925 declaration-specifiers:
1926 storage-class-specifier declaration-specifiers[opt]
1927 type-specifier declaration-specifiers[opt]
1928 type-qualifier declaration-specifiers[opt]
1929 function-specifier declaration-specifiers[opt]
1930 alignment-specifier declaration-specifiers[opt]
1932 Function specifiers (inline) are from C99, and are currently
1933 handled as storage class specifiers, as is __thread. Alignment
1934 specifiers are from C11.
1936 C90 6.5.1, C99 6.7.1:
1937 storage-class-specifier:
1938 typedef
1939 extern
1940 static
1941 auto
1942 register
1944 C99 6.7.4:
1945 function-specifier:
1946 inline
1947 _Noreturn
1949 (_Noreturn is new in C11.)
1951 C90 6.5.2, C99 6.7.2:
1952 type-specifier:
1953 void
1954 char
1955 short
1957 long
1958 float
1959 double
1960 signed
1961 unsigned
1962 _Bool
1963 _Complex
1964 [_Imaginary removed in C99 TC2]
1965 struct-or-union-specifier
1966 enum-specifier
1967 typedef-name
1969 (_Bool and _Complex are new in C99.)
1971 C90 6.5.3, C99 6.7.3:
1973 type-qualifier:
1974 const
1975 restrict
1976 volatile
1977 address-space-qualifier
1979 (restrict is new in C99.)
1981 GNU extensions:
1983 declaration-specifiers:
1984 attributes declaration-specifiers[opt]
1986 type-qualifier:
1987 address-space
1989 address-space:
1990 identifier recognized by the target
1992 storage-class-specifier:
1993 __thread
1995 type-specifier:
1996 typeof-specifier
1997 __int128
1998 _Decimal32
1999 _Decimal64
2000 _Decimal128
2001 _Fract
2002 _Accum
2003 _Sat
2005 (_Fract, _Accum, and _Sat are new from ISO/IEC DTR 18037:
2006 http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1169.pdf)
2008 Objective-C:
2010 type-specifier:
2011 class-name objc-protocol-refs[opt]
2012 typedef-name objc-protocol-refs
2013 objc-protocol-refs
2016 static void
2017 c_parser_declspecs (c_parser *parser, struct c_declspecs *specs,
2018 bool scspec_ok, bool typespec_ok, bool start_attr_ok,
2019 enum c_lookahead_kind la)
2021 bool attrs_ok = start_attr_ok;
2022 bool seen_type = specs->typespec_kind != ctsk_none;
2024 if (!typespec_ok)
2025 gcc_assert (la == cla_prefer_id);
2027 while (c_parser_next_token_is (parser, CPP_NAME)
2028 || c_parser_next_token_is (parser, CPP_KEYWORD)
2029 || (c_dialect_objc () && c_parser_next_token_is (parser, CPP_LESS)))
2031 struct c_typespec t;
2032 tree attrs;
2033 tree align;
2034 location_t loc = c_parser_peek_token (parser)->location;
2036 /* If we cannot accept a type, exit if the next token must start
2037 one. Also, if we already have seen a tagged definition,
2038 a typename would be an error anyway and likely the user
2039 has simply forgotten a semicolon, so we exit. */
2040 if ((!typespec_ok || specs->typespec_kind == ctsk_tagdef)
2041 && c_parser_next_tokens_start_typename (parser, la)
2042 && !c_parser_next_token_is_qualifier (parser))
2043 break;
2045 if (c_parser_next_token_is (parser, CPP_NAME))
2047 c_token *name_token = c_parser_peek_token (parser);
2048 tree value = name_token->value;
2049 c_id_kind kind = name_token->id_kind;
2051 if (kind == C_ID_ADDRSPACE)
2053 addr_space_t as
2054 = name_token->keyword - RID_FIRST_ADDR_SPACE;
2055 declspecs_add_addrspace (name_token->location, specs, as);
2056 c_parser_consume_token (parser);
2057 attrs_ok = true;
2058 continue;
2061 gcc_assert (!c_parser_next_token_is_qualifier (parser));
2063 /* If we cannot accept a type, and the next token must start one,
2064 exit. Do the same if we already have seen a tagged definition,
2065 since it would be an error anyway and likely the user has simply
2066 forgotten a semicolon. */
2067 if (seen_type || !c_parser_next_tokens_start_typename (parser, la))
2068 break;
2070 /* Now at an unknown typename (C_ID_ID), a C_ID_TYPENAME or
2071 a C_ID_CLASSNAME. */
2072 c_parser_consume_token (parser);
2073 seen_type = true;
2074 attrs_ok = true;
2075 if (kind == C_ID_ID)
2077 error ("unknown type name %qE", value);
2078 t.kind = ctsk_typedef;
2079 t.spec = error_mark_node;
2081 else if (kind == C_ID_TYPENAME
2082 && (!c_dialect_objc ()
2083 || c_parser_next_token_is_not (parser, CPP_LESS)))
2085 t.kind = ctsk_typedef;
2086 /* For a typedef name, record the meaning, not the name.
2087 In case of 'foo foo, bar;'. */
2088 t.spec = lookup_name (value);
2090 else
2092 tree proto = NULL_TREE;
2093 gcc_assert (c_dialect_objc ());
2094 t.kind = ctsk_objc;
2095 if (c_parser_next_token_is (parser, CPP_LESS))
2096 proto = c_parser_objc_protocol_refs (parser);
2097 t.spec = objc_get_protocol_qualified_type (value, proto);
2099 t.expr = NULL_TREE;
2100 t.expr_const_operands = true;
2101 declspecs_add_type (name_token->location, specs, t);
2102 continue;
2104 if (c_parser_next_token_is (parser, CPP_LESS))
2106 /* Make "<SomeProtocol>" equivalent to "id <SomeProtocol>" -
2107 nisse@lysator.liu.se. */
2108 tree proto;
2109 gcc_assert (c_dialect_objc ());
2110 if (!typespec_ok || seen_type)
2111 break;
2112 proto = c_parser_objc_protocol_refs (parser);
2113 t.kind = ctsk_objc;
2114 t.spec = objc_get_protocol_qualified_type (NULL_TREE, proto);
2115 t.expr = NULL_TREE;
2116 t.expr_const_operands = true;
2117 declspecs_add_type (loc, specs, t);
2118 continue;
2120 gcc_assert (c_parser_next_token_is (parser, CPP_KEYWORD));
2121 switch (c_parser_peek_token (parser)->keyword)
2123 case RID_STATIC:
2124 case RID_EXTERN:
2125 case RID_REGISTER:
2126 case RID_TYPEDEF:
2127 case RID_INLINE:
2128 case RID_NORETURN:
2129 case RID_AUTO:
2130 case RID_THREAD:
2131 if (!scspec_ok)
2132 goto out;
2133 attrs_ok = true;
2134 /* TODO: Distinguish between function specifiers (inline, noreturn)
2135 and storage class specifiers, either here or in
2136 declspecs_add_scspec. */
2137 declspecs_add_scspec (loc, specs,
2138 c_parser_peek_token (parser)->value);
2139 c_parser_consume_token (parser);
2140 break;
2141 case RID_UNSIGNED:
2142 case RID_LONG:
2143 case RID_INT128:
2144 case RID_SHORT:
2145 case RID_SIGNED:
2146 case RID_COMPLEX:
2147 case RID_INT:
2148 case RID_CHAR:
2149 case RID_FLOAT:
2150 case RID_DOUBLE:
2151 case RID_VOID:
2152 case RID_DFLOAT32:
2153 case RID_DFLOAT64:
2154 case RID_DFLOAT128:
2155 case RID_BOOL:
2156 case RID_FRACT:
2157 case RID_ACCUM:
2158 case RID_SAT:
2159 if (!typespec_ok)
2160 goto out;
2161 attrs_ok = true;
2162 seen_type = true;
2163 if (c_dialect_objc ())
2164 parser->objc_need_raw_identifier = true;
2165 t.kind = ctsk_resword;
2166 t.spec = c_parser_peek_token (parser)->value;
2167 t.expr = NULL_TREE;
2168 t.expr_const_operands = true;
2169 declspecs_add_type (loc, specs, t);
2170 c_parser_consume_token (parser);
2171 break;
2172 case RID_ENUM:
2173 if (!typespec_ok)
2174 goto out;
2175 attrs_ok = true;
2176 seen_type = true;
2177 t = c_parser_enum_specifier (parser);
2178 declspecs_add_type (loc, specs, t);
2179 break;
2180 case RID_STRUCT:
2181 case RID_UNION:
2182 if (!typespec_ok)
2183 goto out;
2184 attrs_ok = true;
2185 seen_type = true;
2186 t = c_parser_struct_or_union_specifier (parser);
2187 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, t.spec);
2188 declspecs_add_type (loc, specs, t);
2189 break;
2190 case RID_TYPEOF:
2191 /* ??? The old parser rejected typeof after other type
2192 specifiers, but is a syntax error the best way of
2193 handling this? */
2194 if (!typespec_ok || seen_type)
2195 goto out;
2196 attrs_ok = true;
2197 seen_type = true;
2198 t = c_parser_typeof_specifier (parser);
2199 declspecs_add_type (loc, specs, t);
2200 break;
2201 case RID_CONST:
2202 case RID_VOLATILE:
2203 case RID_RESTRICT:
2204 attrs_ok = true;
2205 declspecs_add_qual (loc, specs, c_parser_peek_token (parser)->value);
2206 c_parser_consume_token (parser);
2207 break;
2208 /* UPC qualifiers */
2209 case RID_SHARED:
2210 attrs_ok = true;
2211 c_parser_upc_shared_qual (loc, parser, specs);
2212 break;
2213 case RID_STRICT:
2214 case RID_RELAXED:
2215 attrs_ok = true;
2216 declspecs_add_qual (loc, specs, c_parser_peek_token (parser)->value);
2217 c_parser_consume_token (parser);
2218 break;
2219 case RID_ATTRIBUTE:
2220 if (!attrs_ok)
2221 goto out;
2222 attrs = c_parser_attributes (parser);
2223 declspecs_add_attrs (loc, specs, attrs);
2224 break;
2225 case RID_ALIGNAS:
2226 align = c_parser_alignas_specifier (parser);
2227 declspecs_add_alignas (loc, specs, align);
2228 break;
2229 default:
2230 goto out;
2233 out: ;
2236 /* Parse an enum specifier (C90 6.5.2.2, C99 6.7.2.2).
2238 enum-specifier:
2239 enum attributes[opt] identifier[opt] { enumerator-list } attributes[opt]
2240 enum attributes[opt] identifier[opt] { enumerator-list , } attributes[opt]
2241 enum attributes[opt] identifier
2243 The form with trailing comma is new in C99. The forms with
2244 attributes are GNU extensions. In GNU C, we accept any expression
2245 without commas in the syntax (assignment expressions, not just
2246 conditional expressions); assignment expressions will be diagnosed
2247 as non-constant.
2249 enumerator-list:
2250 enumerator
2251 enumerator-list , enumerator
2253 enumerator:
2254 enumeration-constant
2255 enumeration-constant = constant-expression
2258 static struct c_typespec
2259 c_parser_enum_specifier (c_parser *parser)
2261 struct c_typespec ret;
2262 tree attrs;
2263 tree ident = NULL_TREE;
2264 location_t enum_loc;
2265 location_t ident_loc = UNKNOWN_LOCATION; /* Quiet warning. */
2266 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ENUM));
2267 enum_loc = c_parser_peek_token (parser)->location;
2268 c_parser_consume_token (parser);
2269 attrs = c_parser_attributes (parser);
2270 enum_loc = c_parser_peek_token (parser)->location;
2271 /* Set the location in case we create a decl now. */
2272 c_parser_set_source_position_from_token (c_parser_peek_token (parser));
2273 if (c_parser_next_token_is (parser, CPP_NAME))
2275 ident = c_parser_peek_token (parser)->value;
2276 ident_loc = c_parser_peek_token (parser)->location;
2277 enum_loc = ident_loc;
2278 c_parser_consume_token (parser);
2280 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
2282 /* Parse an enum definition. */
2283 struct c_enum_contents the_enum;
2284 tree type;
2285 tree postfix_attrs;
2286 /* We chain the enumerators in reverse order, then put them in
2287 forward order at the end. */
2288 tree values;
2289 timevar_push (TV_PARSE_ENUM);
2290 type = start_enum (enum_loc, &the_enum, ident);
2291 values = NULL_TREE;
2292 c_parser_consume_token (parser);
2293 while (true)
2295 tree enum_id;
2296 tree enum_value;
2297 tree enum_decl;
2298 bool seen_comma;
2299 c_token *token;
2300 location_t comma_loc = UNKNOWN_LOCATION; /* Quiet warning. */
2301 location_t decl_loc, value_loc;
2302 if (c_parser_next_token_is_not (parser, CPP_NAME))
2304 c_parser_error (parser, "expected identifier");
2305 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
2306 values = error_mark_node;
2307 break;
2309 token = c_parser_peek_token (parser);
2310 enum_id = token->value;
2311 /* Set the location in case we create a decl now. */
2312 c_parser_set_source_position_from_token (token);
2313 decl_loc = value_loc = token->location;
2314 c_parser_consume_token (parser);
2315 if (c_parser_next_token_is (parser, CPP_EQ))
2317 c_parser_consume_token (parser);
2318 value_loc = c_parser_peek_token (parser)->location;
2319 enum_value = c_parser_expr_no_commas (parser, NULL).value;
2321 else
2322 enum_value = NULL_TREE;
2323 enum_decl = build_enumerator (decl_loc, value_loc,
2324 &the_enum, enum_id, enum_value);
2325 TREE_CHAIN (enum_decl) = values;
2326 values = enum_decl;
2327 seen_comma = false;
2328 if (c_parser_next_token_is (parser, CPP_COMMA))
2330 comma_loc = c_parser_peek_token (parser)->location;
2331 seen_comma = true;
2332 c_parser_consume_token (parser);
2334 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2336 if (seen_comma && !flag_isoc99)
2337 pedwarn (comma_loc, OPT_Wpedantic, "comma at end of enumerator list");
2338 c_parser_consume_token (parser);
2339 break;
2341 if (!seen_comma)
2343 c_parser_error (parser, "expected %<,%> or %<}%>");
2344 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
2345 values = error_mark_node;
2346 break;
2349 postfix_attrs = c_parser_attributes (parser);
2350 ret.spec = finish_enum (type, nreverse (values),
2351 chainon (attrs, postfix_attrs));
2352 ret.kind = ctsk_tagdef;
2353 ret.expr = NULL_TREE;
2354 ret.expr_const_operands = true;
2355 timevar_pop (TV_PARSE_ENUM);
2356 return ret;
2358 else if (!ident)
2360 c_parser_error (parser, "expected %<{%>");
2361 ret.spec = error_mark_node;
2362 ret.kind = ctsk_tagref;
2363 ret.expr = NULL_TREE;
2364 ret.expr_const_operands = true;
2365 return ret;
2367 ret = parser_xref_tag (ident_loc, ENUMERAL_TYPE, ident);
2368 /* In ISO C, enumerated types can be referred to only if already
2369 defined. */
2370 if (pedantic && !COMPLETE_TYPE_P (ret.spec))
2372 gcc_assert (ident);
2373 pedwarn (enum_loc, OPT_Wpedantic,
2374 "ISO C forbids forward references to %<enum%> types");
2376 return ret;
2379 /* Parse a struct or union specifier (C90 6.5.2.1, C99 6.7.2.1).
2381 struct-or-union-specifier:
2382 struct-or-union attributes[opt] identifier[opt]
2383 { struct-contents } attributes[opt]
2384 struct-or-union attributes[opt] identifier
2386 struct-contents:
2387 struct-declaration-list
2389 struct-declaration-list:
2390 struct-declaration ;
2391 struct-declaration-list struct-declaration ;
2393 GNU extensions:
2395 struct-contents:
2396 empty
2397 struct-declaration
2398 struct-declaration-list struct-declaration
2400 struct-declaration-list:
2401 struct-declaration-list ;
2404 (Note that in the syntax here, unlike that in ISO C, the semicolons
2405 are included here rather than in struct-declaration, in order to
2406 describe the syntax with extra semicolons and missing semicolon at
2407 end.)
2409 Objective-C:
2411 struct-declaration-list:
2412 @defs ( class-name )
2414 (Note this does not include a trailing semicolon, but can be
2415 followed by further declarations, and gets a pedwarn-if-pedantic
2416 when followed by a semicolon.) */
2418 static struct c_typespec
2419 c_parser_struct_or_union_specifier (c_parser *parser)
2421 struct c_typespec ret;
2422 tree attrs;
2423 tree ident = NULL_TREE;
2424 location_t struct_loc;
2425 location_t ident_loc = UNKNOWN_LOCATION;
2426 enum tree_code code;
2427 switch (c_parser_peek_token (parser)->keyword)
2429 case RID_STRUCT:
2430 code = RECORD_TYPE;
2431 break;
2432 case RID_UNION:
2433 code = UNION_TYPE;
2434 break;
2435 default:
2436 gcc_unreachable ();
2438 struct_loc = c_parser_peek_token (parser)->location;
2439 c_parser_consume_token (parser);
2440 attrs = c_parser_attributes (parser);
2442 /* Set the location in case we create a decl now. */
2443 c_parser_set_source_position_from_token (c_parser_peek_token (parser));
2445 if (c_parser_next_token_is (parser, CPP_NAME))
2447 ident = c_parser_peek_token (parser)->value;
2448 ident_loc = c_parser_peek_token (parser)->location;
2449 struct_loc = ident_loc;
2450 c_parser_consume_token (parser);
2452 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
2454 /* Parse a struct or union definition. Start the scope of the
2455 tag before parsing components. */
2456 struct c_struct_parse_info *struct_info;
2457 tree type = start_struct (struct_loc, code, ident, &struct_info);
2458 tree postfix_attrs;
2459 /* We chain the components in reverse order, then put them in
2460 forward order at the end. Each struct-declaration may
2461 declare multiple components (comma-separated), so we must use
2462 chainon to join them, although when parsing each
2463 struct-declaration we can use TREE_CHAIN directly.
2465 The theory behind all this is that there will be more
2466 semicolon separated fields than comma separated fields, and
2467 so we'll be minimizing the number of node traversals required
2468 by chainon. */
2469 tree contents;
2470 timevar_push (TV_PARSE_STRUCT);
2471 contents = NULL_TREE;
2472 c_parser_consume_token (parser);
2473 /* Handle the Objective-C @defs construct,
2474 e.g. foo(sizeof(struct{ @defs(ClassName) }));. */
2475 if (c_parser_next_token_is_keyword (parser, RID_AT_DEFS))
2477 tree name;
2478 gcc_assert (c_dialect_objc ());
2479 c_parser_consume_token (parser);
2480 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2481 goto end_at_defs;
2482 if (c_parser_next_token_is (parser, CPP_NAME)
2483 && c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME)
2485 name = c_parser_peek_token (parser)->value;
2486 c_parser_consume_token (parser);
2488 else
2490 c_parser_error (parser, "expected class name");
2491 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
2492 goto end_at_defs;
2494 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2495 "expected %<)%>");
2496 contents = nreverse (objc_get_class_ivars (name));
2498 end_at_defs:
2499 /* Parse the struct-declarations and semicolons. Problems with
2500 semicolons are diagnosed here; empty structures are diagnosed
2501 elsewhere. */
2502 while (true)
2504 tree decls;
2505 /* Parse any stray semicolon. */
2506 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
2508 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
2509 "extra semicolon in struct or union specified");
2510 c_parser_consume_token (parser);
2511 continue;
2513 /* Stop if at the end of the struct or union contents. */
2514 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2516 c_parser_consume_token (parser);
2517 break;
2519 /* Accept #pragmas at struct scope. */
2520 if (c_parser_next_token_is (parser, CPP_PRAGMA))
2522 c_parser_pragma (parser, pragma_external);
2523 continue;
2525 /* Parse some comma-separated declarations, but not the
2526 trailing semicolon if any. */
2527 decls = c_parser_struct_declaration (parser);
2528 contents = chainon (decls, contents);
2529 /* If no semicolon follows, either we have a parse error or
2530 are at the end of the struct or union and should
2531 pedwarn. */
2532 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
2533 c_parser_consume_token (parser);
2534 else
2536 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2537 pedwarn (c_parser_peek_token (parser)->location, 0,
2538 "no semicolon at end of struct or union");
2539 else if (parser->error
2540 || !c_parser_next_token_starts_declspecs (parser))
2542 c_parser_error (parser, "expected %<;%>");
2543 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
2544 break;
2547 /* If we come here, we have already emitted an error
2548 for an expected `;', identifier or `(', and we also
2549 recovered already. Go on with the next field. */
2552 postfix_attrs = c_parser_attributes (parser);
2553 ret.spec = finish_struct (struct_loc, type, nreverse (contents),
2554 chainon (attrs, postfix_attrs), struct_info);
2555 ret.kind = ctsk_tagdef;
2556 ret.expr = NULL_TREE;
2557 ret.expr_const_operands = true;
2558 timevar_pop (TV_PARSE_STRUCT);
2559 return ret;
2561 else if (!ident)
2563 c_parser_error (parser, "expected %<{%>");
2564 ret.spec = error_mark_node;
2565 ret.kind = ctsk_tagref;
2566 ret.expr = NULL_TREE;
2567 ret.expr_const_operands = true;
2568 return ret;
2570 ret = parser_xref_tag (ident_loc, code, ident);
2571 return ret;
2574 /* Parse a struct-declaration (C90 6.5.2.1, C99 6.7.2.1), *without*
2575 the trailing semicolon.
2577 struct-declaration:
2578 specifier-qualifier-list struct-declarator-list
2579 static_assert-declaration-no-semi
2581 specifier-qualifier-list:
2582 type-specifier specifier-qualifier-list[opt]
2583 type-qualifier specifier-qualifier-list[opt]
2584 attributes specifier-qualifier-list[opt]
2586 struct-declarator-list:
2587 struct-declarator
2588 struct-declarator-list , attributes[opt] struct-declarator
2590 struct-declarator:
2591 declarator attributes[opt]
2592 declarator[opt] : constant-expression attributes[opt]
2594 GNU extensions:
2596 struct-declaration:
2597 __extension__ struct-declaration
2598 specifier-qualifier-list
2600 Unlike the ISO C syntax, semicolons are handled elsewhere. The use
2601 of attributes where shown is a GNU extension. In GNU C, we accept
2602 any expression without commas in the syntax (assignment
2603 expressions, not just conditional expressions); assignment
2604 expressions will be diagnosed as non-constant. */
2606 static tree
2607 c_parser_struct_declaration (c_parser *parser)
2609 struct c_declspecs *specs;
2610 tree prefix_attrs;
2611 tree all_prefix_attrs;
2612 tree decls;
2613 location_t decl_loc;
2614 if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
2616 int ext;
2617 tree decl;
2618 ext = disable_extension_diagnostics ();
2619 c_parser_consume_token (parser);
2620 decl = c_parser_struct_declaration (parser);
2621 restore_extension_diagnostics (ext);
2622 return decl;
2624 if (c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT))
2626 c_parser_static_assert_declaration_no_semi (parser);
2627 return NULL_TREE;
2629 specs = build_null_declspecs ();
2630 decl_loc = c_parser_peek_token (parser)->location;
2631 c_parser_declspecs (parser, specs, false, true, true, cla_nonabstract_decl);
2632 if (parser->error)
2633 return NULL_TREE;
2634 if (!specs->declspecs_seen_p)
2636 c_parser_error (parser, "expected specifier-qualifier-list");
2637 return NULL_TREE;
2639 finish_declspecs (specs);
2640 if (c_parser_next_token_is (parser, CPP_SEMICOLON)
2641 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2643 tree ret;
2644 if (specs->typespec_kind == ctsk_none)
2646 pedwarn (decl_loc, OPT_Wpedantic,
2647 "ISO C forbids member declarations with no members");
2648 shadow_tag_warned (specs, pedantic);
2649 ret = NULL_TREE;
2651 else
2653 /* Support for unnamed structs or unions as members of
2654 structs or unions (which is [a] useful and [b] supports
2655 MS P-SDK). */
2656 tree attrs = NULL;
2658 ret = grokfield (c_parser_peek_token (parser)->location,
2659 build_id_declarator (NULL_TREE), specs,
2660 NULL_TREE, &attrs);
2661 if (ret)
2662 decl_attributes (&ret, attrs, 0);
2664 return ret;
2667 /* Provide better error recovery. Note that a type name here is valid,
2668 and will be treated as a field name. */
2669 if (specs->typespec_kind == ctsk_tagdef
2670 && TREE_CODE (specs->type) != ENUMERAL_TYPE
2671 && c_parser_next_token_starts_declspecs (parser)
2672 && !c_parser_next_token_is (parser, CPP_NAME))
2674 c_parser_error (parser, "expected %<;%>, identifier or %<(%>");
2675 parser->error = false;
2676 return NULL_TREE;
2679 pending_xref_error ();
2680 prefix_attrs = specs->attrs;
2681 all_prefix_attrs = prefix_attrs;
2682 specs->attrs = NULL_TREE;
2683 decls = NULL_TREE;
2684 while (true)
2686 /* Declaring one or more declarators or un-named bit-fields. */
2687 struct c_declarator *declarator;
2688 bool dummy = false;
2689 if (c_parser_next_token_is (parser, CPP_COLON))
2690 declarator = build_id_declarator (NULL_TREE);
2691 else
2692 declarator = c_parser_declarator (parser,
2693 specs->typespec_kind != ctsk_none,
2694 C_DTR_NORMAL, &dummy);
2695 if (declarator == NULL)
2697 c_parser_skip_to_end_of_block_or_statement (parser);
2698 break;
2700 if (c_parser_next_token_is (parser, CPP_COLON)
2701 || c_parser_next_token_is (parser, CPP_COMMA)
2702 || c_parser_next_token_is (parser, CPP_SEMICOLON)
2703 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE)
2704 || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2706 tree postfix_attrs = NULL_TREE;
2707 tree width = NULL_TREE;
2708 tree d;
2709 if (c_parser_next_token_is (parser, CPP_COLON))
2711 c_parser_consume_token (parser);
2712 width = c_parser_expr_no_commas (parser, NULL).value;
2714 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2715 postfix_attrs = c_parser_attributes (parser);
2716 d = grokfield (c_parser_peek_token (parser)->location,
2717 declarator, specs, width, &all_prefix_attrs);
2718 decl_attributes (&d, chainon (postfix_attrs,
2719 all_prefix_attrs), 0);
2720 DECL_CHAIN (d) = decls;
2721 decls = d;
2722 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2723 all_prefix_attrs = chainon (c_parser_attributes (parser),
2724 prefix_attrs);
2725 else
2726 all_prefix_attrs = prefix_attrs;
2727 if (c_parser_next_token_is (parser, CPP_COMMA))
2728 c_parser_consume_token (parser);
2729 else if (c_parser_next_token_is (parser, CPP_SEMICOLON)
2730 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2732 /* Semicolon consumed in caller. */
2733 break;
2735 else
2737 c_parser_error (parser, "expected %<,%>, %<;%> or %<}%>");
2738 break;
2741 else
2743 c_parser_error (parser,
2744 "expected %<:%>, %<,%>, %<;%>, %<}%> or "
2745 "%<__attribute__%>");
2746 break;
2749 return decls;
2752 /* Parse a typeof specifier (a GNU extension).
2754 typeof-specifier:
2755 typeof ( expression )
2756 typeof ( type-name )
2759 static struct c_typespec
2760 c_parser_typeof_specifier (c_parser *parser)
2762 struct c_typespec ret;
2763 ret.kind = ctsk_typeof;
2764 ret.spec = error_mark_node;
2765 ret.expr = NULL_TREE;
2766 ret.expr_const_operands = true;
2767 gcc_assert (c_parser_next_token_is_keyword (parser, RID_TYPEOF));
2768 c_parser_consume_token (parser);
2769 c_inhibit_evaluation_warnings++;
2770 in_typeof++;
2771 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2773 c_inhibit_evaluation_warnings--;
2774 in_typeof--;
2775 return ret;
2777 if (c_parser_next_tokens_start_typename (parser, cla_prefer_id))
2779 struct c_type_name *type = c_parser_type_name (parser);
2780 c_inhibit_evaluation_warnings--;
2781 in_typeof--;
2782 if (type != NULL)
2784 ret.spec = groktypename (type, &ret.expr, &ret.expr_const_operands);
2785 pop_maybe_used (variably_modified_type_p (ret.spec, NULL_TREE));
2788 else
2790 bool was_vm;
2791 location_t here = c_parser_peek_token (parser)->location;
2792 struct c_expr expr = c_parser_expression (parser);
2793 c_inhibit_evaluation_warnings--;
2794 in_typeof--;
2795 if (TREE_CODE (expr.value) == COMPONENT_REF
2796 && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
2797 error_at (here, "%<typeof%> applied to a bit-field");
2798 mark_exp_read (expr.value);
2799 ret.spec = TREE_TYPE (expr.value);
2800 was_vm = variably_modified_type_p (ret.spec, NULL_TREE);
2801 /* This is returned with the type so that when the type is
2802 evaluated, this can be evaluated. */
2803 if (was_vm)
2804 ret.expr = c_fully_fold (expr.value, false, &ret.expr_const_operands);
2805 pop_maybe_used (was_vm);
2807 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
2808 return ret;
2811 /* Parse an alignment-specifier.
2813 C11 6.7.5:
2815 alignment-specifier:
2816 _Alignas ( type-name )
2817 _Alignas ( constant-expression )
2820 static tree
2821 c_parser_alignas_specifier (c_parser * parser)
2823 tree ret = error_mark_node;
2824 location_t loc = c_parser_peek_token (parser)->location;
2825 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ALIGNAS));
2826 c_parser_consume_token (parser);
2827 if (!flag_isoc11)
2829 if (flag_isoc99)
2830 pedwarn (loc, OPT_Wpedantic,
2831 "ISO C99 does not support %<_Alignas%>");
2832 else
2833 pedwarn (loc, OPT_Wpedantic,
2834 "ISO C90 does not support %<_Alignas%>");
2836 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2837 return ret;
2838 if (c_parser_next_tokens_start_typename (parser, cla_prefer_id))
2840 struct c_type_name *type = c_parser_type_name (parser);
2841 if (type != NULL)
2842 ret = c_alignof (loc, groktypename (type, NULL, NULL));
2844 else
2845 ret = c_parser_expr_no_commas (parser, NULL).value;
2846 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
2847 return ret;
2850 /* Parse a declarator, possibly an abstract declarator (C90 6.5.4,
2851 6.5.5, C99 6.7.5, 6.7.6). If TYPE_SEEN_P then a typedef name may
2852 be redeclared; otherwise it may not. KIND indicates which kind of
2853 declarator is wanted. Returns a valid declarator except in the
2854 case of a syntax error in which case NULL is returned. *SEEN_ID is
2855 set to true if an identifier being declared is seen; this is used
2856 to diagnose bad forms of abstract array declarators and to
2857 determine whether an identifier list is syntactically permitted.
2859 declarator:
2860 pointer[opt] direct-declarator
2862 direct-declarator:
2863 identifier
2864 ( attributes[opt] declarator )
2865 direct-declarator array-declarator
2866 direct-declarator ( parameter-type-list )
2867 direct-declarator ( identifier-list[opt] )
2869 pointer:
2870 * type-qualifier-list[opt]
2871 * type-qualifier-list[opt] pointer
2873 type-qualifier-list:
2874 type-qualifier
2875 attributes
2876 type-qualifier-list type-qualifier
2877 type-qualifier-list attributes
2879 parameter-type-list:
2880 parameter-list
2881 parameter-list , ...
2883 parameter-list:
2884 parameter-declaration
2885 parameter-list , parameter-declaration
2887 parameter-declaration:
2888 declaration-specifiers declarator attributes[opt]
2889 declaration-specifiers abstract-declarator[opt] attributes[opt]
2891 identifier-list:
2892 identifier
2893 identifier-list , identifier
2895 abstract-declarator:
2896 pointer
2897 pointer[opt] direct-abstract-declarator
2899 direct-abstract-declarator:
2900 ( attributes[opt] abstract-declarator )
2901 direct-abstract-declarator[opt] array-declarator
2902 direct-abstract-declarator[opt] ( parameter-type-list[opt] )
2904 GNU extensions:
2906 direct-declarator:
2907 direct-declarator ( parameter-forward-declarations
2908 parameter-type-list[opt] )
2910 direct-abstract-declarator:
2911 direct-abstract-declarator[opt] ( parameter-forward-declarations
2912 parameter-type-list[opt] )
2914 parameter-forward-declarations:
2915 parameter-list ;
2916 parameter-forward-declarations parameter-list ;
2918 The uses of attributes shown above are GNU extensions.
2920 Some forms of array declarator are not included in C99 in the
2921 syntax for abstract declarators; these are disallowed elsewhere.
2922 This may be a defect (DR#289).
2924 This function also accepts an omitted abstract declarator as being
2925 an abstract declarator, although not part of the formal syntax. */
2927 static struct c_declarator *
2928 c_parser_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
2929 bool *seen_id)
2931 /* Parse any initial pointer part. */
2932 if (c_parser_next_token_is (parser, CPP_MULT))
2934 struct c_declspecs *quals_attrs = build_null_declspecs ();
2935 struct c_declarator *inner;
2936 c_parser_consume_token (parser);
2937 c_parser_declspecs (parser, quals_attrs, false, false, true, cla_prefer_id);
2938 inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
2939 if (inner == NULL)
2940 return NULL;
2941 else
2942 return make_pointer_declarator (quals_attrs, inner);
2944 /* Now we have a direct declarator, direct abstract declarator or
2945 nothing (which counts as a direct abstract declarator here). */
2946 return c_parser_direct_declarator (parser, type_seen_p, kind, seen_id);
2949 /* Parse a direct declarator or direct abstract declarator; arguments
2950 as c_parser_declarator. */
2952 static struct c_declarator *
2953 c_parser_direct_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
2954 bool *seen_id)
2956 /* The direct declarator must start with an identifier (possibly
2957 omitted) or a parenthesized declarator (possibly abstract). In
2958 an ordinary declarator, initial parentheses must start a
2959 parenthesized declarator. In an abstract declarator or parameter
2960 declarator, they could start a parenthesized declarator or a
2961 parameter list. To tell which, the open parenthesis and any
2962 following attributes must be read. If a declaration specifier
2963 follows, then it is a parameter list; if the specifier is a
2964 typedef name, there might be an ambiguity about redeclaring it,
2965 which is resolved in the direction of treating it as a typedef
2966 name. If a close parenthesis follows, it is also an empty
2967 parameter list, as the syntax does not permit empty abstract
2968 declarators. Otherwise, it is a parenthesized declarator (in
2969 which case the analysis may be repeated inside it, recursively).
2971 ??? There is an ambiguity in a parameter declaration "int
2972 (__attribute__((foo)) x)", where x is not a typedef name: it
2973 could be an abstract declarator for a function, or declare x with
2974 parentheses. The proper resolution of this ambiguity needs
2975 documenting. At present we follow an accident of the old
2976 parser's implementation, whereby the first parameter must have
2977 some declaration specifiers other than just attributes. Thus as
2978 a parameter declaration it is treated as a parenthesized
2979 parameter named x, and as an abstract declarator it is
2980 rejected.
2982 ??? Also following the old parser, attributes inside an empty
2983 parameter list are ignored, making it a list not yielding a
2984 prototype, rather than giving an error or making it have one
2985 parameter with implicit type int.
2987 ??? Also following the old parser, typedef names may be
2988 redeclared in declarators, but not Objective-C class names. */
2990 if (kind != C_DTR_ABSTRACT
2991 && c_parser_next_token_is (parser, CPP_NAME)
2992 && ((type_seen_p
2993 && (c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME
2994 || c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME))
2995 || c_parser_peek_token (parser)->id_kind == C_ID_ID))
2997 struct c_declarator *inner
2998 = build_id_declarator (c_parser_peek_token (parser)->value);
2999 *seen_id = true;
3000 inner->id_loc = c_parser_peek_token (parser)->location;
3001 c_parser_consume_token (parser);
3002 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
3005 if (kind != C_DTR_NORMAL
3006 && c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
3008 struct c_declarator *inner = build_id_declarator (NULL_TREE);
3009 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
3012 /* Either we are at the end of an abstract declarator, or we have
3013 parentheses. */
3015 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
3017 tree attrs;
3018 struct c_declarator *inner;
3019 c_parser_consume_token (parser);
3020 attrs = c_parser_attributes (parser);
3021 if (kind != C_DTR_NORMAL
3022 && (c_parser_next_token_starts_declspecs (parser)
3023 || c_parser_next_token_is (parser, CPP_CLOSE_PAREN)))
3025 struct c_arg_info *args
3026 = c_parser_parms_declarator (parser, kind == C_DTR_NORMAL,
3027 attrs);
3028 if (args == NULL)
3029 return NULL;
3030 else
3032 inner
3033 = build_function_declarator (args,
3034 build_id_declarator (NULL_TREE));
3035 return c_parser_direct_declarator_inner (parser, *seen_id,
3036 inner);
3039 /* A parenthesized declarator. */
3040 inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
3041 if (inner != NULL && attrs != NULL)
3042 inner = build_attrs_declarator (attrs, inner);
3043 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3045 c_parser_consume_token (parser);
3046 if (inner == NULL)
3047 return NULL;
3048 else
3049 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
3051 else
3053 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3054 "expected %<)%>");
3055 return NULL;
3058 else
3060 if (kind == C_DTR_NORMAL)
3062 c_parser_error (parser, "expected identifier or %<(%>");
3063 return NULL;
3065 else
3066 return build_id_declarator (NULL_TREE);
3070 /* Parse part of a direct declarator or direct abstract declarator,
3071 given that some (in INNER) has already been parsed; ID_PRESENT is
3072 true if an identifier is present, false for an abstract
3073 declarator. */
3075 static struct c_declarator *
3076 c_parser_direct_declarator_inner (c_parser *parser, bool id_present,
3077 struct c_declarator *inner)
3079 /* Parse a sequence of array declarators and parameter lists. */
3080 if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
3082 location_t brace_loc = c_parser_peek_token (parser)->location;
3083 struct c_declarator *declarator;
3084 struct c_declspecs *quals_attrs = build_null_declspecs ();
3085 bool static_seen;
3086 bool star_seen;
3087 tree dimen;
3088 c_parser_consume_token (parser);
3089 c_parser_declspecs (parser, quals_attrs, false, false, true, cla_prefer_id);
3090 static_seen = c_parser_next_token_is_keyword (parser, RID_STATIC);
3091 if (static_seen)
3092 c_parser_consume_token (parser);
3093 if (static_seen && !quals_attrs->declspecs_seen_p)
3094 c_parser_declspecs (parser, quals_attrs, false, false, true, cla_prefer_id);
3095 if (!quals_attrs->declspecs_seen_p)
3096 quals_attrs = NULL;
3097 /* If "static" is present, there must be an array dimension.
3098 Otherwise, there may be a dimension, "*", or no
3099 dimension. */
3100 if (static_seen)
3102 star_seen = false;
3103 dimen = c_parser_expr_no_commas (parser, NULL).value;
3105 else
3107 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3109 dimen = NULL_TREE;
3110 star_seen = false;
3112 else if (c_parser_next_token_is (parser, CPP_MULT))
3114 if (c_parser_peek_2nd_token (parser)->type == CPP_CLOSE_SQUARE)
3116 dimen = NULL_TREE;
3117 star_seen = true;
3118 c_parser_consume_token (parser);
3120 else
3122 star_seen = false;
3123 dimen = c_parser_expr_no_commas (parser, NULL).value;
3126 else
3128 star_seen = false;
3129 dimen = c_parser_expr_no_commas (parser, NULL).value;
3132 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3133 c_parser_consume_token (parser);
3134 else
3136 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
3137 "expected %<]%>");
3138 return NULL;
3140 if (dimen)
3141 mark_exp_read (dimen);
3142 declarator = build_array_declarator (brace_loc, dimen, quals_attrs,
3143 static_seen, star_seen);
3144 if (declarator == NULL)
3145 return NULL;
3146 inner = set_array_declarator_inner (declarator, inner);
3147 return c_parser_direct_declarator_inner (parser, id_present, inner);
3149 else if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
3151 tree attrs;
3152 struct c_arg_info *args;
3153 c_parser_consume_token (parser);
3154 attrs = c_parser_attributes (parser);
3155 args = c_parser_parms_declarator (parser, id_present, attrs);
3156 if (args == NULL)
3157 return NULL;
3158 else
3160 inner = build_function_declarator (args, inner);
3161 return c_parser_direct_declarator_inner (parser, id_present, inner);
3164 return inner;
3167 /* Parse a parameter list or identifier list, including the closing
3168 parenthesis but not the opening one. ATTRS are the attributes at
3169 the start of the list. ID_LIST_OK is true if an identifier list is
3170 acceptable; such a list must not have attributes at the start. */
3172 static struct c_arg_info *
3173 c_parser_parms_declarator (c_parser *parser, bool id_list_ok, tree attrs)
3175 push_scope ();
3176 declare_parm_level ();
3177 /* If the list starts with an identifier, it is an identifier list.
3178 Otherwise, it is either a prototype list or an empty list. */
3179 if (id_list_ok
3180 && !attrs
3181 && c_parser_next_token_is (parser, CPP_NAME)
3182 && c_parser_peek_token (parser)->id_kind == C_ID_ID
3184 /* Look ahead to detect typos in type names. */
3185 && c_parser_peek_2nd_token (parser)->type != CPP_NAME
3186 && c_parser_peek_2nd_token (parser)->type != CPP_MULT
3187 && c_parser_peek_2nd_token (parser)->type != CPP_OPEN_PAREN
3188 && c_parser_peek_2nd_token (parser)->type != CPP_OPEN_SQUARE)
3190 tree list = NULL_TREE, *nextp = &list;
3191 while (c_parser_next_token_is (parser, CPP_NAME)
3192 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
3194 *nextp = build_tree_list (NULL_TREE,
3195 c_parser_peek_token (parser)->value);
3196 nextp = & TREE_CHAIN (*nextp);
3197 c_parser_consume_token (parser);
3198 if (c_parser_next_token_is_not (parser, CPP_COMMA))
3199 break;
3200 c_parser_consume_token (parser);
3201 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3203 c_parser_error (parser, "expected identifier");
3204 break;
3207 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3209 struct c_arg_info *ret = build_arg_info ();
3210 ret->types = list;
3211 c_parser_consume_token (parser);
3212 pop_scope ();
3213 return ret;
3215 else
3217 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3218 "expected %<)%>");
3219 pop_scope ();
3220 return NULL;
3223 else
3225 struct c_arg_info *ret = c_parser_parms_list_declarator (parser, attrs,
3226 NULL);
3227 pop_scope ();
3228 return ret;
3232 /* Parse a parameter list (possibly empty), including the closing
3233 parenthesis but not the opening one. ATTRS are the attributes at
3234 the start of the list. EXPR is NULL or an expression that needs to
3235 be evaluated for the side effects of array size expressions in the
3236 parameters. */
3238 static struct c_arg_info *
3239 c_parser_parms_list_declarator (c_parser *parser, tree attrs, tree expr)
3241 bool bad_parm = false;
3243 /* ??? Following the old parser, forward parameter declarations may
3244 use abstract declarators, and if no real parameter declarations
3245 follow the forward declarations then this is not diagnosed. Also
3246 note as above that attributes are ignored as the only contents of
3247 the parentheses, or as the only contents after forward
3248 declarations. */
3249 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3251 struct c_arg_info *ret = build_arg_info ();
3252 c_parser_consume_token (parser);
3253 return ret;
3255 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3257 struct c_arg_info *ret = build_arg_info ();
3259 if (flag_allow_parameterless_variadic_functions)
3261 /* F (...) is allowed. */
3262 ret->types = NULL_TREE;
3264 else
3266 /* Suppress -Wold-style-definition for this case. */
3267 ret->types = error_mark_node;
3268 error_at (c_parser_peek_token (parser)->location,
3269 "ISO C requires a named argument before %<...%>");
3271 c_parser_consume_token (parser);
3272 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3274 c_parser_consume_token (parser);
3275 return ret;
3277 else
3279 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3280 "expected %<)%>");
3281 return NULL;
3284 /* Nonempty list of parameters, either terminated with semicolon
3285 (forward declarations; recurse) or with close parenthesis (normal
3286 function) or with ", ... )" (variadic function). */
3287 while (true)
3289 /* Parse a parameter. */
3290 struct c_parm *parm = c_parser_parameter_declaration (parser, attrs);
3291 attrs = NULL_TREE;
3292 if (parm == NULL)
3293 bad_parm = true;
3294 else
3295 push_parm_decl (parm, &expr);
3296 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
3298 tree new_attrs;
3299 c_parser_consume_token (parser);
3300 mark_forward_parm_decls ();
3301 new_attrs = c_parser_attributes (parser);
3302 return c_parser_parms_list_declarator (parser, new_attrs, expr);
3304 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3306 c_parser_consume_token (parser);
3307 if (bad_parm)
3308 return NULL;
3309 else
3310 return get_parm_info (false, expr);
3312 if (!c_parser_require (parser, CPP_COMMA,
3313 "expected %<;%>, %<,%> or %<)%>"))
3315 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
3316 return NULL;
3318 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3320 c_parser_consume_token (parser);
3321 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3323 c_parser_consume_token (parser);
3324 if (bad_parm)
3325 return NULL;
3326 else
3327 return get_parm_info (true, expr);
3329 else
3331 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3332 "expected %<)%>");
3333 return NULL;
3339 /* Parse a parameter declaration. ATTRS are the attributes at the
3340 start of the declaration if it is the first parameter. */
3342 static struct c_parm *
3343 c_parser_parameter_declaration (c_parser *parser, tree attrs)
3345 struct c_declspecs *specs;
3346 struct c_declarator *declarator;
3347 tree prefix_attrs;
3348 tree postfix_attrs = NULL_TREE;
3349 bool dummy = false;
3351 /* Accept #pragmas between parameter declarations. */
3352 while (c_parser_next_token_is (parser, CPP_PRAGMA))
3353 c_parser_pragma (parser, pragma_external);
3355 if (!c_parser_next_token_starts_declspecs (parser))
3357 c_token *token = c_parser_peek_token (parser);
3358 if (parser->error)
3359 return NULL;
3360 c_parser_set_source_position_from_token (token);
3361 if (c_parser_next_tokens_start_typename (parser, cla_prefer_type))
3363 error ("unknown type name %qE", token->value);
3364 parser->error = true;
3366 /* ??? In some Objective-C cases '...' isn't applicable so there
3367 should be a different message. */
3368 else
3369 c_parser_error (parser,
3370 "expected declaration specifiers or %<...%>");
3371 c_parser_skip_to_end_of_parameter (parser);
3372 return NULL;
3374 specs = build_null_declspecs ();
3375 if (attrs)
3377 declspecs_add_attrs (input_location, specs, attrs);
3378 attrs = NULL_TREE;
3380 c_parser_declspecs (parser, specs, true, true, true, cla_nonabstract_decl);
3381 finish_declspecs (specs);
3382 pending_xref_error ();
3383 prefix_attrs = specs->attrs;
3384 specs->attrs = NULL_TREE;
3385 declarator = c_parser_declarator (parser,
3386 specs->typespec_kind != ctsk_none,
3387 C_DTR_PARM, &dummy);
3388 if (declarator == NULL)
3390 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
3391 return NULL;
3393 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
3394 postfix_attrs = c_parser_attributes (parser);
3395 return build_c_parm (specs, chainon (postfix_attrs, prefix_attrs),
3396 declarator);
3399 /* Parse a string literal in an asm expression. It should not be
3400 translated, and wide string literals are an error although
3401 permitted by the syntax. This is a GNU extension.
3403 asm-string-literal:
3404 string-literal
3406 ??? At present, following the old parser, the caller needs to have
3407 set lex_untranslated_string to 1. It would be better to follow the
3408 C++ parser rather than using this kludge. */
3410 static tree
3411 c_parser_asm_string_literal (c_parser *parser)
3413 tree str;
3414 int save_flag = warn_overlength_strings;
3415 warn_overlength_strings = 0;
3416 if (c_parser_next_token_is (parser, CPP_STRING))
3418 str = c_parser_peek_token (parser)->value;
3419 c_parser_consume_token (parser);
3421 else if (c_parser_next_token_is (parser, CPP_WSTRING))
3423 error_at (c_parser_peek_token (parser)->location,
3424 "wide string literal in %<asm%>");
3425 str = build_string (1, "");
3426 c_parser_consume_token (parser);
3428 else
3430 c_parser_error (parser, "expected string literal");
3431 str = NULL_TREE;
3433 warn_overlength_strings = save_flag;
3434 return str;
3437 /* Parse a simple asm expression. This is used in restricted
3438 contexts, where a full expression with inputs and outputs does not
3439 make sense. This is a GNU extension.
3441 simple-asm-expr:
3442 asm ( asm-string-literal )
3445 static tree
3446 c_parser_simple_asm_expr (c_parser *parser)
3448 tree str;
3449 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
3450 /* ??? Follow the C++ parser rather than using the
3451 lex_untranslated_string kludge. */
3452 parser->lex_untranslated_string = true;
3453 c_parser_consume_token (parser);
3454 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3456 parser->lex_untranslated_string = false;
3457 return NULL_TREE;
3459 str = c_parser_asm_string_literal (parser);
3460 parser->lex_untranslated_string = false;
3461 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
3463 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
3464 return NULL_TREE;
3466 return str;
3469 static tree
3470 c_parser_attribute_any_word (c_parser *parser)
3472 tree attr_name = NULL_TREE;
3474 if (c_parser_next_token_is (parser, CPP_KEYWORD))
3476 /* ??? See comment above about what keywords are accepted here. */
3477 bool ok;
3478 switch (c_parser_peek_token (parser)->keyword)
3480 case RID_STATIC:
3481 case RID_UNSIGNED:
3482 case RID_LONG:
3483 case RID_INT128:
3484 case RID_CONST:
3485 case RID_EXTERN:
3486 case RID_REGISTER:
3487 case RID_TYPEDEF:
3488 case RID_SHORT:
3489 case RID_INLINE:
3490 case RID_NORETURN:
3491 case RID_VOLATILE:
3492 case RID_SIGNED:
3493 case RID_AUTO:
3494 case RID_RESTRICT:
3495 case RID_COMPLEX:
3496 case RID_THREAD:
3497 case RID_INT:
3498 case RID_CHAR:
3499 case RID_FLOAT:
3500 case RID_DOUBLE:
3501 case RID_VOID:
3502 case RID_DFLOAT32:
3503 case RID_DFLOAT64:
3504 case RID_DFLOAT128:
3505 case RID_BOOL:
3506 case RID_FRACT:
3507 case RID_ACCUM:
3508 case RID_SAT:
3509 case RID_TRANSACTION_ATOMIC:
3510 case RID_TRANSACTION_CANCEL:
3511 ok = true;
3512 break;
3513 default:
3514 ok = false;
3515 break;
3517 if (!ok)
3518 return NULL_TREE;
3520 /* Accept __attribute__((__const)) as __attribute__((const)) etc. */
3521 attr_name = ridpointers[(int) c_parser_peek_token (parser)->keyword];
3523 else if (c_parser_next_token_is (parser, CPP_NAME))
3524 attr_name = c_parser_peek_token (parser)->value;
3526 return attr_name;
3529 /* Parse (possibly empty) attributes. This is a GNU extension.
3531 attributes:
3532 empty
3533 attributes attribute
3535 attribute:
3536 __attribute__ ( ( attribute-list ) )
3538 attribute-list:
3539 attrib
3540 attribute_list , attrib
3542 attrib:
3543 empty
3544 any-word
3545 any-word ( identifier )
3546 any-word ( identifier , nonempty-expr-list )
3547 any-word ( expr-list )
3549 where the "identifier" must not be declared as a type, and
3550 "any-word" may be any identifier (including one declared as a
3551 type), a reserved word storage class specifier, type specifier or
3552 type qualifier. ??? This still leaves out most reserved keywords
3553 (following the old parser), shouldn't we include them, and why not
3554 allow identifiers declared as types to start the arguments? */
3556 static tree
3557 c_parser_attributes (c_parser *parser)
3559 tree attrs = NULL_TREE;
3560 while (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
3562 /* ??? Follow the C++ parser rather than using the
3563 lex_untranslated_string kludge. */
3564 parser->lex_untranslated_string = true;
3565 c_parser_consume_token (parser);
3566 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3568 parser->lex_untranslated_string = false;
3569 return attrs;
3571 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3573 parser->lex_untranslated_string = false;
3574 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
3575 return attrs;
3577 /* Parse the attribute list. */
3578 while (c_parser_next_token_is (parser, CPP_COMMA)
3579 || c_parser_next_token_is (parser, CPP_NAME)
3580 || c_parser_next_token_is (parser, CPP_KEYWORD))
3582 tree attr, attr_name, attr_args;
3583 vec<tree, va_gc> *expr_list;
3584 if (c_parser_next_token_is (parser, CPP_COMMA))
3586 c_parser_consume_token (parser);
3587 continue;
3590 attr_name = c_parser_attribute_any_word (parser);
3591 if (attr_name == NULL)
3592 break;
3593 c_parser_consume_token (parser);
3594 if (c_parser_next_token_is_not (parser, CPP_OPEN_PAREN))
3596 attr = build_tree_list (attr_name, NULL_TREE);
3597 attrs = chainon (attrs, attr);
3598 continue;
3600 c_parser_consume_token (parser);
3601 /* Parse the attribute contents. If they start with an
3602 identifier which is followed by a comma or close
3603 parenthesis, then the arguments start with that
3604 identifier; otherwise they are an expression list.
3605 In objective-c the identifier may be a classname. */
3606 if (c_parser_next_token_is (parser, CPP_NAME)
3607 && (c_parser_peek_token (parser)->id_kind == C_ID_ID
3608 || (c_dialect_objc ()
3609 && c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME))
3610 && ((c_parser_peek_2nd_token (parser)->type == CPP_COMMA)
3611 || (c_parser_peek_2nd_token (parser)->type
3612 == CPP_CLOSE_PAREN)))
3614 tree arg1 = c_parser_peek_token (parser)->value;
3615 c_parser_consume_token (parser);
3616 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3617 attr_args = build_tree_list (NULL_TREE, arg1);
3618 else
3620 tree tree_list;
3621 c_parser_consume_token (parser);
3622 expr_list = c_parser_expr_list (parser, false, true,
3623 NULL, NULL, NULL);
3624 tree_list = build_tree_list_vec (expr_list);
3625 attr_args = tree_cons (NULL_TREE, arg1, tree_list);
3626 release_tree_vector (expr_list);
3629 else
3631 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3632 attr_args = NULL_TREE;
3633 else
3635 expr_list = c_parser_expr_list (parser, false, true,
3636 NULL, NULL, NULL);
3637 attr_args = build_tree_list_vec (expr_list);
3638 release_tree_vector (expr_list);
3641 attr = build_tree_list (attr_name, attr_args);
3642 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3643 c_parser_consume_token (parser);
3644 else
3646 parser->lex_untranslated_string = false;
3647 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3648 "expected %<)%>");
3649 return attrs;
3651 attrs = chainon (attrs, attr);
3653 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3654 c_parser_consume_token (parser);
3655 else
3657 parser->lex_untranslated_string = false;
3658 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3659 "expected %<)%>");
3660 return attrs;
3662 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3663 c_parser_consume_token (parser);
3664 else
3666 parser->lex_untranslated_string = false;
3667 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3668 "expected %<)%>");
3669 return attrs;
3671 parser->lex_untranslated_string = false;
3673 return attrs;
3676 /* Parse a type name (C90 6.5.5, C99 6.7.6).
3678 type-name:
3679 specifier-qualifier-list abstract-declarator[opt]
3682 static struct c_type_name *
3683 c_parser_type_name (c_parser *parser)
3685 struct c_declspecs *specs = build_null_declspecs ();
3686 struct c_declarator *declarator;
3687 struct c_type_name *ret;
3688 bool dummy = false;
3689 c_parser_declspecs (parser, specs, false, true, true, cla_prefer_type);
3690 if (!specs->declspecs_seen_p)
3692 c_parser_error (parser, "expected specifier-qualifier-list");
3693 return NULL;
3695 if (specs->type != error_mark_node)
3697 pending_xref_error ();
3698 finish_declspecs (specs);
3700 declarator = c_parser_declarator (parser,
3701 specs->typespec_kind != ctsk_none,
3702 C_DTR_ABSTRACT, &dummy);
3703 if (declarator == NULL)
3704 return NULL;
3705 ret = XOBNEW (&parser_obstack, struct c_type_name);
3706 ret->specs = specs;
3707 ret->declarator = declarator;
3708 return ret;
3711 /* Parse an initializer (C90 6.5.7, C99 6.7.8).
3713 initializer:
3714 assignment-expression
3715 { initializer-list }
3716 { initializer-list , }
3718 initializer-list:
3719 designation[opt] initializer
3720 initializer-list , designation[opt] initializer
3722 designation:
3723 designator-list =
3725 designator-list:
3726 designator
3727 designator-list designator
3729 designator:
3730 array-designator
3731 . identifier
3733 array-designator:
3734 [ constant-expression ]
3736 GNU extensions:
3738 initializer:
3741 designation:
3742 array-designator
3743 identifier :
3745 array-designator:
3746 [ constant-expression ... constant-expression ]
3748 Any expression without commas is accepted in the syntax for the
3749 constant-expressions, with non-constant expressions rejected later.
3751 This function is only used for top-level initializers; for nested
3752 ones, see c_parser_initval. */
3754 static struct c_expr
3755 c_parser_initializer (c_parser *parser)
3757 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
3758 return c_parser_braced_init (parser, NULL_TREE, false);
3759 else
3761 struct c_expr ret;
3762 location_t loc = c_parser_peek_token (parser)->location;
3763 ret = c_parser_expr_no_commas (parser, NULL);
3764 if (TREE_CODE (ret.value) != STRING_CST
3765 && TREE_CODE (ret.value) != COMPOUND_LITERAL_EXPR)
3766 ret = default_function_array_read_conversion (loc, ret);
3767 return ret;
3771 /* Parse a braced initializer list. TYPE is the type specified for a
3772 compound literal, and NULL_TREE for other initializers and for
3773 nested braced lists. NESTED_P is true for nested braced lists,
3774 false for the list of a compound literal or the list that is the
3775 top-level initializer in a declaration. */
3777 static struct c_expr
3778 c_parser_braced_init (c_parser *parser, tree type, bool nested_p)
3780 struct c_expr ret;
3781 struct obstack braced_init_obstack;
3782 location_t brace_loc = c_parser_peek_token (parser)->location;
3783 gcc_obstack_init (&braced_init_obstack);
3784 gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
3785 c_parser_consume_token (parser);
3786 if (nested_p)
3787 push_init_level (0, &braced_init_obstack);
3788 else
3789 really_start_incremental_init (type);
3790 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3792 pedwarn (brace_loc, OPT_Wpedantic, "ISO C forbids empty initializer braces");
3794 else
3796 /* Parse a non-empty initializer list, possibly with a trailing
3797 comma. */
3798 while (true)
3800 c_parser_initelt (parser, &braced_init_obstack);
3801 if (parser->error)
3802 break;
3803 if (c_parser_next_token_is (parser, CPP_COMMA))
3804 c_parser_consume_token (parser);
3805 else
3806 break;
3807 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3808 break;
3811 if (c_parser_next_token_is_not (parser, CPP_CLOSE_BRACE))
3813 ret.value = error_mark_node;
3814 ret.original_code = ERROR_MARK;
3815 ret.original_type = NULL;
3816 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, "expected %<}%>");
3817 pop_init_level (0, &braced_init_obstack);
3818 obstack_free (&braced_init_obstack, NULL);
3819 return ret;
3821 c_parser_consume_token (parser);
3822 ret = pop_init_level (0, &braced_init_obstack);
3823 obstack_free (&braced_init_obstack, NULL);
3824 return ret;
3827 /* Parse a nested initializer, including designators. */
3829 static void
3830 c_parser_initelt (c_parser *parser, struct obstack * braced_init_obstack)
3832 /* Parse any designator or designator list. A single array
3833 designator may have the subsequent "=" omitted in GNU C, but a
3834 longer list or a structure member designator may not. */
3835 if (c_parser_next_token_is (parser, CPP_NAME)
3836 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
3838 /* Old-style structure member designator. */
3839 set_init_label (c_parser_peek_token (parser)->value,
3840 braced_init_obstack);
3841 /* Use the colon as the error location. */
3842 pedwarn (c_parser_peek_2nd_token (parser)->location, OPT_Wpedantic,
3843 "obsolete use of designated initializer with %<:%>");
3844 c_parser_consume_token (parser);
3845 c_parser_consume_token (parser);
3847 else
3849 /* des_seen is 0 if there have been no designators, 1 if there
3850 has been a single array designator and 2 otherwise. */
3851 int des_seen = 0;
3852 /* Location of a designator. */
3853 location_t des_loc = UNKNOWN_LOCATION; /* Quiet warning. */
3854 while (c_parser_next_token_is (parser, CPP_OPEN_SQUARE)
3855 || c_parser_next_token_is (parser, CPP_DOT))
3857 int des_prev = des_seen;
3858 if (!des_seen)
3859 des_loc = c_parser_peek_token (parser)->location;
3860 if (des_seen < 2)
3861 des_seen++;
3862 if (c_parser_next_token_is (parser, CPP_DOT))
3864 des_seen = 2;
3865 c_parser_consume_token (parser);
3866 if (c_parser_next_token_is (parser, CPP_NAME))
3868 set_init_label (c_parser_peek_token (parser)->value,
3869 braced_init_obstack);
3870 c_parser_consume_token (parser);
3872 else
3874 struct c_expr init;
3875 init.value = error_mark_node;
3876 init.original_code = ERROR_MARK;
3877 init.original_type = NULL;
3878 c_parser_error (parser, "expected identifier");
3879 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
3880 process_init_element (init, false, braced_init_obstack);
3881 return;
3884 else
3886 tree first, second;
3887 location_t ellipsis_loc = UNKNOWN_LOCATION; /* Quiet warning. */
3888 /* ??? Following the old parser, [ objc-receiver
3889 objc-message-args ] is accepted as an initializer,
3890 being distinguished from a designator by what follows
3891 the first assignment expression inside the square
3892 brackets, but after a first array designator a
3893 subsequent square bracket is for Objective-C taken to
3894 start an expression, using the obsolete form of
3895 designated initializer without '=', rather than
3896 possibly being a second level of designation: in LALR
3897 terms, the '[' is shifted rather than reducing
3898 designator to designator-list. */
3899 if (des_prev == 1 && c_dialect_objc ())
3901 des_seen = des_prev;
3902 break;
3904 if (des_prev == 0 && c_dialect_objc ())
3906 /* This might be an array designator or an
3907 Objective-C message expression. If the former,
3908 continue parsing here; if the latter, parse the
3909 remainder of the initializer given the starting
3910 primary-expression. ??? It might make sense to
3911 distinguish when des_prev == 1 as well; see
3912 previous comment. */
3913 tree rec, args;
3914 struct c_expr mexpr;
3915 c_parser_consume_token (parser);
3916 if (c_parser_peek_token (parser)->type == CPP_NAME
3917 && ((c_parser_peek_token (parser)->id_kind
3918 == C_ID_TYPENAME)
3919 || (c_parser_peek_token (parser)->id_kind
3920 == C_ID_CLASSNAME)))
3922 /* Type name receiver. */
3923 tree id = c_parser_peek_token (parser)->value;
3924 c_parser_consume_token (parser);
3925 rec = objc_get_class_reference (id);
3926 goto parse_message_args;
3928 first = c_parser_expr_no_commas (parser, NULL).value;
3929 mark_exp_read (first);
3930 if (c_parser_next_token_is (parser, CPP_ELLIPSIS)
3931 || c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3932 goto array_desig_after_first;
3933 /* Expression receiver. So far only one part
3934 without commas has been parsed; there might be
3935 more of the expression. */
3936 rec = first;
3937 while (c_parser_next_token_is (parser, CPP_COMMA))
3939 struct c_expr next;
3940 location_t comma_loc, exp_loc;
3941 comma_loc = c_parser_peek_token (parser)->location;
3942 c_parser_consume_token (parser);
3943 exp_loc = c_parser_peek_token (parser)->location;
3944 next = c_parser_expr_no_commas (parser, NULL);
3945 next = default_function_array_read_conversion (exp_loc,
3946 next);
3947 rec = build_compound_expr (comma_loc, rec, next.value);
3949 parse_message_args:
3950 /* Now parse the objc-message-args. */
3951 args = c_parser_objc_message_args (parser);
3952 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
3953 "expected %<]%>");
3954 mexpr.value
3955 = objc_build_message_expr (rec, args);
3956 mexpr.original_code = ERROR_MARK;
3957 mexpr.original_type = NULL;
3958 /* Now parse and process the remainder of the
3959 initializer, starting with this message
3960 expression as a primary-expression. */
3961 c_parser_initval (parser, &mexpr, braced_init_obstack);
3962 return;
3964 c_parser_consume_token (parser);
3965 first = c_parser_expr_no_commas (parser, NULL).value;
3966 mark_exp_read (first);
3967 array_desig_after_first:
3968 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3970 ellipsis_loc = c_parser_peek_token (parser)->location;
3971 c_parser_consume_token (parser);
3972 second = c_parser_expr_no_commas (parser, NULL).value;
3973 mark_exp_read (second);
3975 else
3976 second = NULL_TREE;
3977 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3979 c_parser_consume_token (parser);
3980 set_init_index (first, second, braced_init_obstack);
3981 if (second)
3982 pedwarn (ellipsis_loc, OPT_Wpedantic,
3983 "ISO C forbids specifying range of elements to initialize");
3985 else
3986 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
3987 "expected %<]%>");
3990 if (des_seen >= 1)
3992 if (c_parser_next_token_is (parser, CPP_EQ))
3994 if (!flag_isoc99)
3995 pedwarn (des_loc, OPT_Wpedantic,
3996 "ISO C90 forbids specifying subobject to initialize");
3997 c_parser_consume_token (parser);
3999 else
4001 if (des_seen == 1)
4002 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
4003 "obsolete use of designated initializer without %<=%>");
4004 else
4006 struct c_expr init;
4007 init.value = error_mark_node;
4008 init.original_code = ERROR_MARK;
4009 init.original_type = NULL;
4010 c_parser_error (parser, "expected %<=%>");
4011 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
4012 process_init_element (init, false, braced_init_obstack);
4013 return;
4018 c_parser_initval (parser, NULL, braced_init_obstack);
4021 /* Parse a nested initializer; as c_parser_initializer but parses
4022 initializers within braced lists, after any designators have been
4023 applied. If AFTER is not NULL then it is an Objective-C message
4024 expression which is the primary-expression starting the
4025 initializer. */
4027 static void
4028 c_parser_initval (c_parser *parser, struct c_expr *after,
4029 struct obstack * braced_init_obstack)
4031 struct c_expr init;
4032 gcc_assert (!after || c_dialect_objc ());
4033 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE) && !after)
4034 init = c_parser_braced_init (parser, NULL_TREE, true);
4035 else
4037 location_t loc = c_parser_peek_token (parser)->location;
4038 init = c_parser_expr_no_commas (parser, after);
4039 if (init.value != NULL_TREE
4040 && TREE_CODE (init.value) != STRING_CST
4041 && TREE_CODE (init.value) != COMPOUND_LITERAL_EXPR)
4042 init = default_function_array_read_conversion (loc, init);
4044 process_init_element (init, false, braced_init_obstack);
4047 /* Parse a compound statement (possibly a function body) (C90 6.6.2,
4048 C99 6.8.2).
4050 compound-statement:
4051 { block-item-list[opt] }
4052 { label-declarations block-item-list }
4054 block-item-list:
4055 block-item
4056 block-item-list block-item
4058 block-item:
4059 nested-declaration
4060 statement
4062 nested-declaration:
4063 declaration
4065 GNU extensions:
4067 compound-statement:
4068 { label-declarations block-item-list }
4070 nested-declaration:
4071 __extension__ nested-declaration
4072 nested-function-definition
4074 label-declarations:
4075 label-declaration
4076 label-declarations label-declaration
4078 label-declaration:
4079 __label__ identifier-list ;
4081 Allowing the mixing of declarations and code is new in C99. The
4082 GNU syntax also permits (not shown above) labels at the end of
4083 compound statements, which yield an error. We don't allow labels
4084 on declarations; this might seem like a natural extension, but
4085 there would be a conflict between attributes on the label and
4086 prefix attributes on the declaration. ??? The syntax follows the
4087 old parser in requiring something after label declarations.
4088 Although they are erroneous if the labels declared aren't defined,
4089 is it useful for the syntax to be this way?
4091 OpenMP:
4093 block-item:
4094 openmp-directive
4096 openmp-directive:
4097 barrier-directive
4098 flush-directive */
4100 static tree
4101 c_parser_compound_statement (c_parser *parser)
4103 tree stmt;
4104 location_t brace_loc;
4105 brace_loc = c_parser_peek_token (parser)->location;
4106 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
4108 /* Ensure a scope is entered and left anyway to avoid confusion
4109 if we have just prepared to enter a function body. */
4110 stmt = c_begin_compound_stmt (true);
4111 c_end_compound_stmt (brace_loc, stmt, true);
4112 return error_mark_node;
4114 stmt = c_begin_compound_stmt (true);
4115 c_parser_compound_statement_nostart (parser);
4116 return c_end_compound_stmt (brace_loc, stmt, true);
4119 /* Parse a compound statement except for the opening brace. This is
4120 used for parsing both compound statements and statement expressions
4121 (which follow different paths to handling the opening). */
4123 static void
4124 c_parser_compound_statement_nostart (c_parser *parser)
4126 bool last_stmt = false;
4127 bool last_label = false;
4128 bool save_valid_for_pragma = valid_location_for_stdc_pragma_p ();
4129 location_t label_loc = UNKNOWN_LOCATION; /* Quiet warning. */
4130 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
4132 c_parser_consume_token (parser);
4133 return;
4135 mark_valid_location_for_stdc_pragma (true);
4136 if (c_parser_next_token_is_keyword (parser, RID_LABEL))
4138 /* Read zero or more forward-declarations for labels that nested
4139 functions can jump to. */
4140 mark_valid_location_for_stdc_pragma (false);
4141 while (c_parser_next_token_is_keyword (parser, RID_LABEL))
4143 label_loc = c_parser_peek_token (parser)->location;
4144 c_parser_consume_token (parser);
4145 /* Any identifiers, including those declared as type names,
4146 are OK here. */
4147 while (true)
4149 tree label;
4150 if (c_parser_next_token_is_not (parser, CPP_NAME))
4152 c_parser_error (parser, "expected identifier");
4153 break;
4155 label
4156 = declare_label (c_parser_peek_token (parser)->value);
4157 C_DECLARED_LABEL_FLAG (label) = 1;
4158 add_stmt (build_stmt (label_loc, DECL_EXPR, label));
4159 c_parser_consume_token (parser);
4160 if (c_parser_next_token_is (parser, CPP_COMMA))
4161 c_parser_consume_token (parser);
4162 else
4163 break;
4165 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
4167 pedwarn (label_loc, OPT_Wpedantic, "ISO C forbids label declarations");
4169 /* We must now have at least one statement, label or declaration. */
4170 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
4172 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
4173 c_parser_error (parser, "expected declaration or statement");
4174 c_parser_consume_token (parser);
4175 return;
4177 /* Process all #pragma's just after the opening brace. This
4178 handles #pragma upc, which can only appear just after
4179 the opening brace, when it appears within a function body. */
4180 push_upc_consistency_mode ();
4181 permit_pragma_upc ();
4182 while (c_parser_next_token_is (parser, CPP_PRAGMA))
4184 location_t loc ATTRIBUTE_UNUSED = c_parser_peek_token (parser)->location;
4185 if (c_parser_pragma (parser, pragma_compound))
4186 last_label = false, last_stmt = true;
4187 parser->error = false;
4189 deny_pragma_upc ();
4190 while (c_parser_next_token_is_not (parser, CPP_CLOSE_BRACE))
4192 location_t loc = c_parser_peek_token (parser)->location;
4193 if (c_parser_next_token_is_keyword (parser, RID_CASE)
4194 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
4195 || (c_parser_next_token_is (parser, CPP_NAME)
4196 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
4198 if (c_parser_next_token_is_keyword (parser, RID_CASE))
4199 label_loc = c_parser_peek_2nd_token (parser)->location;
4200 else
4201 label_loc = c_parser_peek_token (parser)->location;
4202 last_label = true;
4203 last_stmt = false;
4204 mark_valid_location_for_stdc_pragma (false);
4205 c_parser_label (parser);
4207 else if (!last_label
4208 && c_parser_next_tokens_start_declaration (parser))
4210 last_label = false;
4211 mark_valid_location_for_stdc_pragma (false);
4212 c_parser_declaration_or_fndef (parser, true, true, true, true, true, NULL);
4213 if (last_stmt)
4214 pedwarn_c90 (loc,
4215 (pedantic && !flag_isoc99)
4216 ? OPT_Wpedantic
4217 : OPT_Wdeclaration_after_statement,
4218 "ISO C90 forbids mixed declarations and code");
4219 last_stmt = false;
4221 else if (!last_label
4222 && c_parser_next_token_is_keyword (parser, RID_EXTENSION))
4224 /* __extension__ can start a declaration, but is also an
4225 unary operator that can start an expression. Consume all
4226 but the last of a possible series of __extension__ to
4227 determine which. */
4228 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
4229 && (c_parser_peek_2nd_token (parser)->keyword
4230 == RID_EXTENSION))
4231 c_parser_consume_token (parser);
4232 if (c_token_starts_declaration (c_parser_peek_2nd_token (parser)))
4234 int ext;
4235 ext = disable_extension_diagnostics ();
4236 c_parser_consume_token (parser);
4237 last_label = false;
4238 mark_valid_location_for_stdc_pragma (false);
4239 c_parser_declaration_or_fndef (parser, true, true, true, true,
4240 true, NULL);
4241 /* Following the old parser, __extension__ does not
4242 disable this diagnostic. */
4243 restore_extension_diagnostics (ext);
4244 if (last_stmt)
4245 pedwarn_c90 (loc, (pedantic && !flag_isoc99)
4246 ? OPT_Wpedantic
4247 : OPT_Wdeclaration_after_statement,
4248 "ISO C90 forbids mixed declarations and code");
4249 last_stmt = false;
4251 else
4252 goto statement;
4254 else if (c_parser_next_token_is (parser, CPP_PRAGMA))
4256 /* External pragmas, and some omp pragmas, are not associated
4257 with regular c code, and so are not to be considered statements
4258 syntactically. This ensures that the user doesn't put them
4259 places that would turn into syntax errors if the directive
4260 were ignored. */
4261 if (c_parser_pragma (parser, pragma_compound))
4262 last_label = false, last_stmt = true;
4264 else if (c_parser_next_token_is (parser, CPP_EOF))
4266 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
4267 c_parser_error (parser, "expected declaration or statement");
4268 return;
4270 else if (c_parser_next_token_is_keyword (parser, RID_ELSE))
4272 if (parser->in_if_block)
4274 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
4275 error_at (loc, """expected %<}%> before %<else%>");
4276 return;
4278 else
4280 error_at (loc, "%<else%> without a previous %<if%>");
4281 c_parser_consume_token (parser);
4282 continue;
4285 else
4287 statement:
4288 last_label = false;
4289 last_stmt = true;
4290 mark_valid_location_for_stdc_pragma (false);
4291 c_parser_statement_after_labels (parser);
4294 parser->error = false;
4296 if (last_label)
4297 error_at (label_loc, "label at end of compound statement");
4298 c_parser_consume_token (parser);
4299 pop_upc_consistency_mode ();
4300 /* Restore the value we started with. */
4301 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
4304 /* Parse a label (C90 6.6.1, C99 6.8.1).
4306 label:
4307 identifier : attributes[opt]
4308 case constant-expression :
4309 default :
4311 GNU extensions:
4313 label:
4314 case constant-expression ... constant-expression :
4316 The use of attributes on labels is a GNU extension. The syntax in
4317 GNU C accepts any expressions without commas, non-constant
4318 expressions being rejected later. */
4320 static void
4321 c_parser_label (c_parser *parser)
4323 location_t loc1 = c_parser_peek_token (parser)->location;
4324 tree label = NULL_TREE;
4325 if (c_parser_next_token_is_keyword (parser, RID_CASE))
4327 tree exp1, exp2;
4328 c_parser_consume_token (parser);
4329 exp1 = c_parser_expr_no_commas (parser, NULL).value;
4330 if (c_parser_next_token_is (parser, CPP_COLON))
4332 c_parser_consume_token (parser);
4333 label = do_case (loc1, exp1, NULL_TREE);
4335 else if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
4337 c_parser_consume_token (parser);
4338 exp2 = c_parser_expr_no_commas (parser, NULL).value;
4339 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
4340 label = do_case (loc1, exp1, exp2);
4342 else
4343 c_parser_error (parser, "expected %<:%> or %<...%>");
4345 else if (c_parser_next_token_is_keyword (parser, RID_DEFAULT))
4347 c_parser_consume_token (parser);
4348 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
4349 label = do_case (loc1, NULL_TREE, NULL_TREE);
4351 else
4353 tree name = c_parser_peek_token (parser)->value;
4354 tree tlab;
4355 tree attrs;
4356 location_t loc2 = c_parser_peek_token (parser)->location;
4357 gcc_assert (c_parser_next_token_is (parser, CPP_NAME));
4358 c_parser_consume_token (parser);
4359 gcc_assert (c_parser_next_token_is (parser, CPP_COLON));
4360 c_parser_consume_token (parser);
4361 attrs = c_parser_attributes (parser);
4362 tlab = define_label (loc2, name);
4363 if (tlab)
4365 decl_attributes (&tlab, attrs, 0);
4366 label = add_stmt (build_stmt (loc1, LABEL_EXPR, tlab));
4369 if (label)
4371 if (c_parser_next_tokens_start_declaration (parser))
4373 error_at (c_parser_peek_token (parser)->location,
4374 "a label can only be part of a statement and "
4375 "a declaration is not a statement");
4376 c_parser_declaration_or_fndef (parser, /*fndef_ok*/ false,
4377 /*static_assert_ok*/ true,
4378 /*empty_ok*/ true, /*nested*/ true,
4379 /*start_attr_ok*/ true, NULL);
4384 /* Parse a statement (C90 6.6, C99 6.8).
4386 statement:
4387 labeled-statement
4388 compound-statement
4389 expression-statement
4390 selection-statement
4391 iteration-statement
4392 jump-statement
4394 labeled-statement:
4395 label statement
4397 expression-statement:
4398 expression[opt] ;
4400 selection-statement:
4401 if-statement
4402 switch-statement
4404 iteration-statement:
4405 while-statement
4406 do-statement
4407 for-statement
4409 jump-statement:
4410 goto identifier ;
4411 continue ;
4412 break ;
4413 return expression[opt] ;
4415 GNU extensions:
4417 statement:
4418 asm-statement
4420 jump-statement:
4421 goto * expression ;
4423 Objective-C:
4425 statement:
4426 objc-throw-statement
4427 objc-try-catch-statement
4428 objc-synchronized-statement
4430 objc-throw-statement:
4431 @throw expression ;
4432 @throw ;
4434 OpenMP:
4436 statement:
4437 openmp-construct
4439 openmp-construct:
4440 parallel-construct
4441 for-construct
4442 sections-construct
4443 single-construct
4444 parallel-for-construct
4445 parallel-sections-construct
4446 master-construct
4447 critical-construct
4448 atomic-construct
4449 ordered-construct
4451 parallel-construct:
4452 parallel-directive structured-block
4454 for-construct:
4455 for-directive iteration-statement
4457 sections-construct:
4458 sections-directive section-scope
4460 single-construct:
4461 single-directive structured-block
4463 parallel-for-construct:
4464 parallel-for-directive iteration-statement
4466 parallel-sections-construct:
4467 parallel-sections-directive section-scope
4469 master-construct:
4470 master-directive structured-block
4472 critical-construct:
4473 critical-directive structured-block
4475 atomic-construct:
4476 atomic-directive expression-statement
4478 ordered-construct:
4479 ordered-directive structured-block
4481 Transactional Memory:
4483 statement:
4484 transaction-statement
4485 transaction-cancel-statement
4488 static void
4489 c_parser_statement (c_parser *parser)
4491 while (c_parser_next_token_is_keyword (parser, RID_CASE)
4492 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
4493 || (c_parser_next_token_is (parser, CPP_NAME)
4494 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
4495 c_parser_label (parser);
4496 c_parser_statement_after_labels (parser);
4499 /* Parse a statement, other than a labeled statement. */
4501 static void
4502 c_parser_statement_after_labels (c_parser *parser)
4504 location_t loc = c_parser_peek_token (parser)->location;
4505 tree stmt = NULL_TREE;
4506 bool in_if_block = parser->in_if_block;
4507 parser->in_if_block = false;
4508 switch (c_parser_peek_token (parser)->type)
4510 case CPP_OPEN_BRACE:
4511 add_stmt (c_parser_compound_statement (parser));
4512 break;
4513 case CPP_KEYWORD:
4514 switch (c_parser_peek_token (parser)->keyword)
4516 case RID_IF:
4517 c_parser_if_statement (parser);
4518 break;
4519 case RID_SWITCH:
4520 c_parser_switch_statement (parser);
4521 break;
4522 case RID_WHILE:
4523 c_parser_while_statement (parser);
4524 break;
4525 case RID_DO:
4526 c_parser_do_statement (parser);
4527 break;
4528 case RID_FOR:
4529 c_parser_for_statement (parser);
4530 break;
4531 case RID_GOTO:
4532 c_parser_consume_token (parser);
4533 if (c_parser_next_token_is (parser, CPP_NAME))
4535 stmt = c_finish_goto_label (loc,
4536 c_parser_peek_token (parser)->value);
4537 c_parser_consume_token (parser);
4539 else if (c_parser_next_token_is (parser, CPP_MULT))
4541 tree val;
4543 c_parser_consume_token (parser);
4544 val = c_parser_expression (parser).value;
4545 mark_exp_read (val);
4546 stmt = c_finish_goto_ptr (loc, val);
4548 else
4549 c_parser_error (parser, "expected identifier or %<*%>");
4550 goto expect_semicolon;
4551 case RID_CONTINUE:
4552 c_parser_consume_token (parser);
4553 stmt = c_finish_bc_stmt (loc, &c_cont_label, false);
4554 goto expect_semicolon;
4555 case RID_BREAK:
4556 c_parser_consume_token (parser);
4557 stmt = c_finish_bc_stmt (loc, &c_break_label, true);
4558 goto expect_semicolon;
4559 case RID_RETURN:
4560 c_parser_consume_token (parser);
4561 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4563 stmt = c_finish_return (loc, NULL_TREE, NULL_TREE);
4564 c_parser_consume_token (parser);
4566 else
4568 struct c_expr expr = c_parser_expression_conv (parser);
4569 mark_exp_read (expr.value);
4570 stmt = c_finish_return (loc, expr.value, expr.original_type);
4571 goto expect_semicolon;
4573 break;
4574 case RID_ASM:
4575 stmt = c_parser_asm_statement (parser);
4576 break;
4577 case RID_TRANSACTION_ATOMIC:
4578 case RID_TRANSACTION_RELAXED:
4579 stmt = c_parser_transaction (parser,
4580 c_parser_peek_token (parser)->keyword);
4581 break;
4582 case RID_TRANSACTION_CANCEL:
4583 stmt = c_parser_transaction_cancel (parser);
4584 goto expect_semicolon;
4585 case RID_AT_THROW:
4586 gcc_assert (c_dialect_objc ());
4587 c_parser_consume_token (parser);
4588 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4590 stmt = objc_build_throw_stmt (loc, NULL_TREE);
4591 c_parser_consume_token (parser);
4593 else
4595 tree expr = c_parser_expression (parser).value;
4596 expr = c_fully_fold (expr, false, NULL);
4597 stmt = objc_build_throw_stmt (loc, expr);
4598 goto expect_semicolon;
4600 break;
4601 case RID_AT_TRY:
4602 gcc_assert (c_dialect_objc ());
4603 c_parser_objc_try_catch_finally_statement (parser);
4604 break;
4605 case RID_AT_SYNCHRONIZED:
4606 gcc_assert (c_dialect_objc ());
4607 c_parser_objc_synchronized_statement (parser);
4608 break;
4609 case RID_UPC_FORALL:
4610 gcc_assert (c_dialect_upc ());
4611 c_parser_upc_forall_statement (parser);
4612 break;
4613 case RID_UPC_NOTIFY:
4614 gcc_assert (c_dialect_upc ());
4615 c_parser_upc_sync_statement (parser, UPC_SYNC_NOTIFY_OP);
4616 goto expect_semicolon;
4617 case RID_UPC_WAIT:
4618 gcc_assert (c_dialect_upc ());
4619 c_parser_upc_sync_statement (parser, UPC_SYNC_WAIT_OP);
4620 goto expect_semicolon;
4621 case RID_UPC_BARRIER:
4622 gcc_assert (c_dialect_upc ());
4623 c_parser_upc_sync_statement (parser, UPC_SYNC_BARRIER_OP);
4624 goto expect_semicolon;
4625 default:
4626 goto expr_stmt;
4628 break;
4629 case CPP_SEMICOLON:
4630 c_parser_consume_token (parser);
4631 break;
4632 case CPP_CLOSE_PAREN:
4633 case CPP_CLOSE_SQUARE:
4634 /* Avoid infinite loop in error recovery:
4635 c_parser_skip_until_found stops at a closing nesting
4636 delimiter without consuming it, but here we need to consume
4637 it to proceed further. */
4638 c_parser_error (parser, "expected statement");
4639 c_parser_consume_token (parser);
4640 break;
4641 case CPP_PRAGMA:
4642 c_parser_pragma (parser, pragma_stmt);
4643 break;
4644 default:
4645 expr_stmt:
4646 stmt = c_finish_expr_stmt (loc, c_parser_expression_conv (parser).value);
4647 expect_semicolon:
4648 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
4649 break;
4651 /* Two cases cannot and do not have line numbers associated: If stmt
4652 is degenerate, such as "2;", then stmt is an INTEGER_CST, which
4653 cannot hold line numbers. But that's OK because the statement
4654 will either be changed to a MODIFY_EXPR during gimplification of
4655 the statement expr, or discarded. If stmt was compound, but
4656 without new variables, we will have skipped the creation of a
4657 BIND and will have a bare STATEMENT_LIST. But that's OK because
4658 (recursively) all of the component statements should already have
4659 line numbers assigned. ??? Can we discard no-op statements
4660 earlier? */
4661 if (CAN_HAVE_LOCATION_P (stmt)
4662 && EXPR_LOCATION (stmt) == UNKNOWN_LOCATION)
4663 SET_EXPR_LOCATION (stmt, loc);
4665 parser->in_if_block = in_if_block;
4668 /* Parse the condition from an if, do, while or for statements. */
4670 static tree
4671 c_parser_condition (c_parser *parser)
4673 location_t loc ATTRIBUTE_UNUSED = c_parser_peek_token (parser)->location;
4674 tree cond;
4675 cond = c_parser_expression_conv (parser).value;
4676 cond = c_objc_common_truthvalue_conversion (loc, cond);
4677 cond = c_fully_fold (cond, false, NULL);
4678 if (warn_sequence_point)
4679 verify_sequence_points (cond);
4680 return cond;
4683 /* Parse a parenthesized condition from an if, do or while statement.
4685 condition:
4686 ( expression )
4688 static tree
4689 c_parser_paren_condition (c_parser *parser)
4691 tree cond;
4692 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4693 return error_mark_node;
4694 cond = c_parser_condition (parser);
4695 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
4696 return cond;
4699 /* Parse a statement which is a block in C99. */
4701 static tree
4702 c_parser_c99_block_statement (c_parser *parser)
4704 tree block = c_begin_compound_stmt (flag_isoc99);
4705 location_t loc = c_parser_peek_token (parser)->location;
4706 c_parser_statement (parser);
4707 return c_end_compound_stmt (loc, block, flag_isoc99);
4710 /* Parse the body of an if statement. This is just parsing a
4711 statement but (a) it is a block in C99, (b) we track whether the
4712 body is an if statement for the sake of -Wparentheses warnings, (c)
4713 we handle an empty body specially for the sake of -Wempty-body
4714 warnings, and (d) we call parser_compound_statement directly
4715 because c_parser_statement_after_labels resets
4716 parser->in_if_block. */
4718 static tree
4719 c_parser_if_body (c_parser *parser, bool *if_p)
4721 tree block = c_begin_compound_stmt (flag_isoc99);
4722 location_t body_loc = c_parser_peek_token (parser)->location;
4723 while (c_parser_next_token_is_keyword (parser, RID_CASE)
4724 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
4725 || (c_parser_next_token_is (parser, CPP_NAME)
4726 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
4727 c_parser_label (parser);
4728 *if_p = c_parser_next_token_is_keyword (parser, RID_IF);
4729 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4731 location_t loc = c_parser_peek_token (parser)->location;
4732 add_stmt (build_empty_stmt (loc));
4733 c_parser_consume_token (parser);
4734 if (!c_parser_next_token_is_keyword (parser, RID_ELSE))
4735 warning_at (loc, OPT_Wempty_body,
4736 "suggest braces around empty body in an %<if%> statement");
4738 else if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
4739 add_stmt (c_parser_compound_statement (parser));
4740 else
4741 c_parser_statement_after_labels (parser);
4742 return c_end_compound_stmt (body_loc, block, flag_isoc99);
4745 /* Parse the else body of an if statement. This is just parsing a
4746 statement but (a) it is a block in C99, (b) we handle an empty body
4747 specially for the sake of -Wempty-body warnings. */
4749 static tree
4750 c_parser_else_body (c_parser *parser)
4752 location_t else_loc = c_parser_peek_token (parser)->location;
4753 tree block = c_begin_compound_stmt (flag_isoc99);
4754 while (c_parser_next_token_is_keyword (parser, RID_CASE)
4755 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
4756 || (c_parser_next_token_is (parser, CPP_NAME)
4757 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
4758 c_parser_label (parser);
4759 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4761 location_t loc = c_parser_peek_token (parser)->location;
4762 warning_at (loc,
4763 OPT_Wempty_body,
4764 "suggest braces around empty body in an %<else%> statement");
4765 add_stmt (build_empty_stmt (loc));
4766 c_parser_consume_token (parser);
4768 else
4769 c_parser_statement_after_labels (parser);
4770 return c_end_compound_stmt (else_loc, block, flag_isoc99);
4773 /* Parse an if statement (C90 6.6.4, C99 6.8.4).
4775 if-statement:
4776 if ( expression ) statement
4777 if ( expression ) statement else statement
4780 static void
4781 c_parser_if_statement (c_parser *parser)
4783 tree block;
4784 location_t loc;
4785 tree cond;
4786 bool first_if = false;
4787 tree first_body, second_body;
4788 bool in_if_block;
4790 gcc_assert (c_parser_next_token_is_keyword (parser, RID_IF));
4791 c_parser_consume_token (parser);
4792 block = c_begin_compound_stmt (flag_isoc99);
4793 loc = c_parser_peek_token (parser)->location;
4794 cond = c_parser_paren_condition (parser);
4795 in_if_block = parser->in_if_block;
4796 parser->in_if_block = true;
4797 first_body = c_parser_if_body (parser, &first_if);
4798 parser->in_if_block = in_if_block;
4799 if (c_parser_next_token_is_keyword (parser, RID_ELSE))
4801 c_parser_consume_token (parser);
4802 second_body = c_parser_else_body (parser);
4804 else
4805 second_body = NULL_TREE;
4806 c_finish_if_stmt (loc, cond, first_body, second_body, first_if);
4807 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
4810 /* Parse a switch statement (C90 6.6.4, C99 6.8.4).
4812 switch-statement:
4813 switch (expression) statement
4816 static void
4817 c_parser_switch_statement (c_parser *parser)
4819 tree block, expr, body, save_break;
4820 location_t switch_loc = c_parser_peek_token (parser)->location;
4821 location_t switch_cond_loc;
4822 gcc_assert (c_parser_next_token_is_keyword (parser, RID_SWITCH));
4823 c_parser_consume_token (parser);
4824 block = c_begin_compound_stmt (flag_isoc99);
4825 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4827 switch_cond_loc = c_parser_peek_token (parser)->location;
4828 expr = c_parser_expression (parser).value;
4829 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
4831 else
4833 switch_cond_loc = UNKNOWN_LOCATION;
4834 expr = error_mark_node;
4836 c_start_case (switch_loc, switch_cond_loc, expr);
4837 save_break = c_break_label;
4838 c_break_label = NULL_TREE;
4839 body = c_parser_c99_block_statement (parser);
4840 c_finish_case (body);
4841 if (c_break_label)
4843 location_t here = c_parser_peek_token (parser)->location;
4844 tree t = build1 (LABEL_EXPR, void_type_node, c_break_label);
4845 SET_EXPR_LOCATION (t, here);
4846 add_stmt (t);
4848 c_break_label = save_break;
4849 add_stmt (c_end_compound_stmt (switch_loc, block, flag_isoc99));
4852 /* Parse a while statement (C90 6.6.5, C99 6.8.5).
4854 while-statement:
4855 while (expression) statement
4858 static void
4859 c_parser_while_statement (c_parser *parser)
4861 tree block, cond, body, save_break, save_cont;
4862 location_t loc;
4863 gcc_assert (c_parser_next_token_is_keyword (parser, RID_WHILE));
4864 c_parser_consume_token (parser);
4865 block = c_begin_compound_stmt (flag_isoc99);
4866 loc = c_parser_peek_token (parser)->location;
4867 cond = c_parser_paren_condition (parser);
4868 save_break = c_break_label;
4869 c_break_label = NULL_TREE;
4870 save_cont = c_cont_label;
4871 c_cont_label = NULL_TREE;
4872 body = c_parser_c99_block_statement (parser);
4873 c_finish_loop (loc, cond, NULL, body, c_break_label, c_cont_label, true);
4874 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
4875 c_break_label = save_break;
4876 c_cont_label = save_cont;
4879 /* Parse a do statement (C90 6.6.5, C99 6.8.5).
4881 do-statement:
4882 do statement while ( expression ) ;
4885 static void
4886 c_parser_do_statement (c_parser *parser)
4888 tree block, cond, body, save_break, save_cont, new_break, new_cont;
4889 location_t loc;
4890 gcc_assert (c_parser_next_token_is_keyword (parser, RID_DO));
4891 c_parser_consume_token (parser);
4892 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4893 warning_at (c_parser_peek_token (parser)->location,
4894 OPT_Wempty_body,
4895 "suggest braces around empty body in %<do%> statement");
4896 block = c_begin_compound_stmt (flag_isoc99);
4897 loc = c_parser_peek_token (parser)->location;
4898 save_break = c_break_label;
4899 c_break_label = NULL_TREE;
4900 save_cont = c_cont_label;
4901 c_cont_label = NULL_TREE;
4902 body = c_parser_c99_block_statement (parser);
4903 c_parser_require_keyword (parser, RID_WHILE, "expected %<while%>");
4904 new_break = c_break_label;
4905 c_break_label = save_break;
4906 new_cont = c_cont_label;
4907 c_cont_label = save_cont;
4908 cond = c_parser_paren_condition (parser);
4909 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
4910 c_parser_skip_to_end_of_block_or_statement (parser);
4911 c_finish_loop (loc, cond, NULL, body, new_break, new_cont, false);
4912 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
4915 /* Parse a for statement (C90 6.6.5, C99 6.8.5).
4917 for-statement:
4918 for ( expression[opt] ; expression[opt] ; expression[opt] ) statement
4919 for ( nested-declaration expression[opt] ; expression[opt] ) statement
4921 The form with a declaration is new in C99.
4923 ??? In accordance with the old parser, the declaration may be a
4924 nested function, which is then rejected in check_for_loop_decls,
4925 but does it make any sense for this to be included in the grammar?
4926 Note in particular that the nested function does not include a
4927 trailing ';', whereas the "declaration" production includes one.
4928 Also, can we reject bad declarations earlier and cheaper than
4929 check_for_loop_decls?
4931 In Objective-C, there are two additional variants:
4933 foreach-statement:
4934 for ( expression in expresssion ) statement
4935 for ( declaration in expression ) statement
4937 This is inconsistent with C, because the second variant is allowed
4938 even if c99 is not enabled.
4940 The rest of the comment documents these Objective-C foreach-statement.
4942 Here is the canonical example of the first variant:
4943 for (object in array) { do something with object }
4944 we call the first expression ("object") the "object_expression" and
4945 the second expression ("array") the "collection_expression".
4946 object_expression must be an lvalue of type "id" (a generic Objective-C
4947 object) because the loop works by assigning to object_expression the
4948 various objects from the collection_expression. collection_expression
4949 must evaluate to something of type "id" which responds to the method
4950 countByEnumeratingWithState:objects:count:.
4952 The canonical example of the second variant is:
4953 for (id object in array) { do something with object }
4954 which is completely equivalent to
4956 id object;
4957 for (object in array) { do something with object }
4959 Note that initizializing 'object' in some way (eg, "for ((object =
4960 xxx) in array) { do something with object }") is possibly
4961 technically valid, but completely pointless as 'object' will be
4962 assigned to something else as soon as the loop starts. We should
4963 most likely reject it (TODO).
4965 The beginning of the Objective-C foreach-statement looks exactly
4966 like the beginning of the for-statement, and we can tell it is a
4967 foreach-statement only because the initial declaration or
4968 expression is terminated by 'in' instead of ';'.
4971 static void
4972 c_parser_for_statement (c_parser *parser)
4974 tree block, cond, incr, save_break, save_cont, body;
4975 /* The following are only used when parsing an ObjC foreach statement. */
4976 tree object_expression;
4977 /* Silence the bogus uninitialized warning. */
4978 tree collection_expression = NULL;
4979 location_t loc = c_parser_peek_token (parser)->location;
4980 location_t for_loc = c_parser_peek_token (parser)->location;
4981 bool is_foreach_statement = false;
4982 gcc_assert (c_parser_next_token_is_keyword (parser, RID_FOR));
4983 c_parser_consume_token (parser);
4984 /* Open a compound statement in Objective-C as well, just in case this is
4985 as foreach expression. */
4986 block = c_begin_compound_stmt (flag_isoc99 || c_dialect_objc ());
4987 cond = error_mark_node;
4988 incr = error_mark_node;
4989 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4991 /* Parse the initialization declaration or expression. */
4992 object_expression = error_mark_node;
4993 parser->objc_could_be_foreach_context = c_dialect_objc ();
4994 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4996 parser->objc_could_be_foreach_context = false;
4997 c_parser_consume_token (parser);
4998 c_finish_expr_stmt (loc, NULL_TREE);
5000 else if (c_parser_next_tokens_start_declaration (parser))
5002 c_parser_declaration_or_fndef (parser, true, true, true, true, true,
5003 &object_expression);
5004 parser->objc_could_be_foreach_context = false;
5006 if (c_parser_next_token_is_keyword (parser, RID_IN))
5008 c_parser_consume_token (parser);
5009 is_foreach_statement = true;
5010 if (check_for_loop_decls (for_loc, true) == NULL_TREE)
5011 c_parser_error (parser, "multiple iterating variables in fast enumeration");
5013 else
5014 check_for_loop_decls (for_loc, flag_isoc99);
5016 else if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
5018 /* __extension__ can start a declaration, but is also an
5019 unary operator that can start an expression. Consume all
5020 but the last of a possible series of __extension__ to
5021 determine which. */
5022 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
5023 && (c_parser_peek_2nd_token (parser)->keyword
5024 == RID_EXTENSION))
5025 c_parser_consume_token (parser);
5026 if (c_token_starts_declaration (c_parser_peek_2nd_token (parser)))
5028 int ext;
5029 ext = disable_extension_diagnostics ();
5030 c_parser_consume_token (parser);
5031 c_parser_declaration_or_fndef (parser, true, true, true, true,
5032 true, &object_expression);
5033 parser->objc_could_be_foreach_context = false;
5035 restore_extension_diagnostics (ext);
5036 if (c_parser_next_token_is_keyword (parser, RID_IN))
5038 c_parser_consume_token (parser);
5039 is_foreach_statement = true;
5040 if (check_for_loop_decls (for_loc, true) == NULL_TREE)
5041 c_parser_error (parser, "multiple iterating variables in fast enumeration");
5043 else
5044 check_for_loop_decls (for_loc, flag_isoc99);
5046 else
5047 goto init_expr;
5049 else
5051 init_expr:
5053 tree init_expression;
5054 init_expression = c_parser_expression (parser).value;
5055 parser->objc_could_be_foreach_context = false;
5056 if (c_parser_next_token_is_keyword (parser, RID_IN))
5058 c_parser_consume_token (parser);
5059 is_foreach_statement = true;
5060 if (! lvalue_p (init_expression))
5061 c_parser_error (parser, "invalid iterating variable in fast enumeration");
5062 object_expression = c_fully_fold (init_expression, false, NULL);
5064 else
5066 c_finish_expr_stmt (loc, init_expression);
5067 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
5071 /* Parse the loop condition. In the case of a foreach
5072 statement, there is no loop condition. */
5073 gcc_assert (!parser->objc_could_be_foreach_context);
5074 if (!is_foreach_statement)
5076 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5078 c_parser_consume_token (parser);
5079 cond = NULL_TREE;
5081 else
5083 cond = c_parser_condition (parser);
5084 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
5087 /* Parse the increment expression (the third expression in a
5088 for-statement). In the case of a foreach-statement, this is
5089 the expression that follows the 'in'. */
5090 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
5092 if (is_foreach_statement)
5094 c_parser_error (parser, "missing collection in fast enumeration");
5095 collection_expression = error_mark_node;
5097 else
5098 incr = c_process_expr_stmt (loc, NULL_TREE);
5100 else
5102 if (is_foreach_statement)
5103 collection_expression = c_fully_fold (c_parser_expression (parser).value,
5104 false, NULL);
5105 else
5106 incr = c_process_expr_stmt (loc, c_parser_expression (parser).value);
5108 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5110 save_break = c_break_label;
5111 c_break_label = NULL_TREE;
5112 save_cont = c_cont_label;
5113 c_cont_label = NULL_TREE;
5114 body = c_parser_c99_block_statement (parser);
5115 if (is_foreach_statement)
5116 objc_finish_foreach_loop (loc, object_expression, collection_expression, body, c_break_label, c_cont_label);
5117 else
5118 c_finish_loop (loc, cond, incr, body, c_break_label, c_cont_label, true);
5119 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99 || c_dialect_objc ()));
5120 c_break_label = save_break;
5121 c_cont_label = save_cont;
5124 /* Parse an asm statement, a GNU extension. This is a full-blown asm
5125 statement with inputs, outputs, clobbers, and volatile tag
5126 allowed.
5128 asm-statement:
5129 asm type-qualifier[opt] ( asm-argument ) ;
5130 asm type-qualifier[opt] goto ( asm-goto-argument ) ;
5132 asm-argument:
5133 asm-string-literal
5134 asm-string-literal : asm-operands[opt]
5135 asm-string-literal : asm-operands[opt] : asm-operands[opt]
5136 asm-string-literal : asm-operands[opt] : asm-operands[opt] : asm-clobbers[opt]
5138 asm-goto-argument:
5139 asm-string-literal : : asm-operands[opt] : asm-clobbers[opt] \
5140 : asm-goto-operands
5142 Qualifiers other than volatile are accepted in the syntax but
5143 warned for. */
5145 static tree
5146 c_parser_asm_statement (c_parser *parser)
5148 tree quals, str, outputs, inputs, clobbers, labels, ret;
5149 bool simple, is_goto;
5150 location_t asm_loc = c_parser_peek_token (parser)->location;
5151 int section, nsections;
5153 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
5154 c_parser_consume_token (parser);
5155 if (c_parser_next_token_is_keyword (parser, RID_VOLATILE))
5157 quals = c_parser_peek_token (parser)->value;
5158 c_parser_consume_token (parser);
5160 else if (c_parser_next_token_is_keyword (parser, RID_CONST)
5161 || c_parser_next_token_is_keyword (parser, RID_RESTRICT))
5163 warning_at (c_parser_peek_token (parser)->location,
5165 "%E qualifier ignored on asm",
5166 c_parser_peek_token (parser)->value);
5167 quals = NULL_TREE;
5168 c_parser_consume_token (parser);
5170 else
5171 quals = NULL_TREE;
5173 is_goto = false;
5174 if (c_parser_next_token_is_keyword (parser, RID_GOTO))
5176 c_parser_consume_token (parser);
5177 is_goto = true;
5180 /* ??? Follow the C++ parser rather than using the
5181 lex_untranslated_string kludge. */
5182 parser->lex_untranslated_string = true;
5183 ret = NULL;
5185 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5186 goto error;
5188 str = c_parser_asm_string_literal (parser);
5189 if (str == NULL_TREE)
5190 goto error_close_paren;
5192 simple = true;
5193 outputs = NULL_TREE;
5194 inputs = NULL_TREE;
5195 clobbers = NULL_TREE;
5196 labels = NULL_TREE;
5198 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN) && !is_goto)
5199 goto done_asm;
5201 /* Parse each colon-delimited section of operands. */
5202 nsections = 3 + is_goto;
5203 for (section = 0; section < nsections; ++section)
5205 if (!c_parser_require (parser, CPP_COLON,
5206 is_goto
5207 ? "expected %<:%>"
5208 : "expected %<:%> or %<)%>"))
5209 goto error_close_paren;
5211 /* Once past any colon, we're no longer a simple asm. */
5212 simple = false;
5214 if ((!c_parser_next_token_is (parser, CPP_COLON)
5215 && !c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
5216 || section == 3)
5217 switch (section)
5219 case 0:
5220 /* For asm goto, we don't allow output operands, but reserve
5221 the slot for a future extension that does allow them. */
5222 if (!is_goto)
5223 outputs = c_parser_asm_operands (parser, false);
5224 break;
5225 case 1:
5226 inputs = c_parser_asm_operands (parser, true);
5227 break;
5228 case 2:
5229 clobbers = c_parser_asm_clobbers (parser);
5230 break;
5231 case 3:
5232 labels = c_parser_asm_goto_operands (parser);
5233 break;
5234 default:
5235 gcc_unreachable ();
5238 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN) && !is_goto)
5239 goto done_asm;
5242 done_asm:
5243 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
5245 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5246 goto error;
5249 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
5250 c_parser_skip_to_end_of_block_or_statement (parser);
5252 ret = build_asm_stmt (quals, build_asm_expr (asm_loc, str, outputs, inputs,
5253 clobbers, labels, simple));
5255 error:
5256 parser->lex_untranslated_string = false;
5257 return ret;
5259 error_close_paren:
5260 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5261 goto error;
5264 /* Parse asm operands, a GNU extension. If CONVERT_P (for inputs but
5265 not outputs), apply the default conversion of functions and arrays
5266 to pointers.
5268 asm-operands:
5269 asm-operand
5270 asm-operands , asm-operand
5272 asm-operand:
5273 asm-string-literal ( expression )
5274 [ identifier ] asm-string-literal ( expression )
5277 static tree
5278 c_parser_asm_operands (c_parser *parser, bool convert_p)
5280 tree list = NULL_TREE;
5281 location_t loc;
5282 while (true)
5284 tree name, str;
5285 struct c_expr expr;
5286 if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
5288 c_parser_consume_token (parser);
5289 if (c_parser_next_token_is (parser, CPP_NAME))
5291 tree id = c_parser_peek_token (parser)->value;
5292 c_parser_consume_token (parser);
5293 name = build_string (IDENTIFIER_LENGTH (id),
5294 IDENTIFIER_POINTER (id));
5296 else
5298 c_parser_error (parser, "expected identifier");
5299 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
5300 return NULL_TREE;
5302 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
5303 "expected %<]%>");
5305 else
5306 name = NULL_TREE;
5307 str = c_parser_asm_string_literal (parser);
5308 if (str == NULL_TREE)
5309 return NULL_TREE;
5310 parser->lex_untranslated_string = false;
5311 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5313 parser->lex_untranslated_string = true;
5314 return NULL_TREE;
5316 loc = c_parser_peek_token (parser)->location;
5317 expr = c_parser_expression (parser);
5318 mark_exp_read (expr.value);
5319 if (convert_p)
5320 expr = default_function_array_conversion (loc, expr);
5321 expr.value = c_fully_fold (expr.value, false, NULL);
5322 parser->lex_untranslated_string = true;
5323 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
5325 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5326 return NULL_TREE;
5328 list = chainon (list, build_tree_list (build_tree_list (name, str),
5329 expr.value));
5330 if (c_parser_next_token_is (parser, CPP_COMMA))
5331 c_parser_consume_token (parser);
5332 else
5333 break;
5335 return list;
5338 /* Parse asm clobbers, a GNU extension.
5340 asm-clobbers:
5341 asm-string-literal
5342 asm-clobbers , asm-string-literal
5345 static tree
5346 c_parser_asm_clobbers (c_parser *parser)
5348 tree list = NULL_TREE;
5349 while (true)
5351 tree str = c_parser_asm_string_literal (parser);
5352 if (str)
5353 list = tree_cons (NULL_TREE, str, list);
5354 else
5355 return NULL_TREE;
5356 if (c_parser_next_token_is (parser, CPP_COMMA))
5357 c_parser_consume_token (parser);
5358 else
5359 break;
5361 return list;
5364 /* Parse asm goto labels, a GNU extension.
5366 asm-goto-operands:
5367 identifier
5368 asm-goto-operands , identifier
5371 static tree
5372 c_parser_asm_goto_operands (c_parser *parser)
5374 tree list = NULL_TREE;
5375 while (true)
5377 tree name, label;
5379 if (c_parser_next_token_is (parser, CPP_NAME))
5381 c_token *tok = c_parser_peek_token (parser);
5382 name = tok->value;
5383 label = lookup_label_for_goto (tok->location, name);
5384 c_parser_consume_token (parser);
5385 TREE_USED (label) = 1;
5387 else
5389 c_parser_error (parser, "expected identifier");
5390 return NULL_TREE;
5393 name = build_string (IDENTIFIER_LENGTH (name),
5394 IDENTIFIER_POINTER (name));
5395 list = tree_cons (name, label, list);
5396 if (c_parser_next_token_is (parser, CPP_COMMA))
5397 c_parser_consume_token (parser);
5398 else
5399 return nreverse (list);
5403 /* Parse an expression other than a compound expression; that is, an
5404 assignment expression (C90 6.3.16, C99 6.5.16). If AFTER is not
5405 NULL then it is an Objective-C message expression which is the
5406 primary-expression starting the expression as an initializer.
5408 assignment-expression:
5409 conditional-expression
5410 unary-expression assignment-operator assignment-expression
5412 assignment-operator: one of
5413 = *= /= %= += -= <<= >>= &= ^= |=
5415 In GNU C we accept any conditional expression on the LHS and
5416 diagnose the invalid lvalue rather than producing a syntax
5417 error. */
5419 static struct c_expr
5420 c_parser_expr_no_commas (c_parser *parser, struct c_expr *after)
5422 struct c_expr lhs, rhs, ret;
5423 enum tree_code code;
5424 location_t op_location, exp_location;
5425 gcc_assert (!after || c_dialect_objc ());
5426 lhs = c_parser_conditional_expression (parser, after);
5427 op_location = c_parser_peek_token (parser)->location;
5428 switch (c_parser_peek_token (parser)->type)
5430 case CPP_EQ:
5431 code = NOP_EXPR;
5432 break;
5433 case CPP_MULT_EQ:
5434 code = MULT_EXPR;
5435 break;
5436 case CPP_DIV_EQ:
5437 code = TRUNC_DIV_EXPR;
5438 break;
5439 case CPP_MOD_EQ:
5440 code = TRUNC_MOD_EXPR;
5441 break;
5442 case CPP_PLUS_EQ:
5443 code = PLUS_EXPR;
5444 break;
5445 case CPP_MINUS_EQ:
5446 code = MINUS_EXPR;
5447 break;
5448 case CPP_LSHIFT_EQ:
5449 code = LSHIFT_EXPR;
5450 break;
5451 case CPP_RSHIFT_EQ:
5452 code = RSHIFT_EXPR;
5453 break;
5454 case CPP_AND_EQ:
5455 code = BIT_AND_EXPR;
5456 break;
5457 case CPP_XOR_EQ:
5458 code = BIT_XOR_EXPR;
5459 break;
5460 case CPP_OR_EQ:
5461 code = BIT_IOR_EXPR;
5462 break;
5463 default:
5464 return lhs;
5466 c_parser_consume_token (parser);
5467 exp_location = c_parser_peek_token (parser)->location;
5468 rhs = c_parser_expr_no_commas (parser, NULL);
5469 rhs = default_function_array_read_conversion (exp_location, rhs);
5470 ret.value = build_modify_expr (op_location, lhs.value, lhs.original_type,
5471 code, exp_location, rhs.value,
5472 rhs.original_type);
5473 if (code == NOP_EXPR)
5474 ret.original_code = MODIFY_EXPR;
5475 else
5477 TREE_NO_WARNING (ret.value) = 1;
5478 ret.original_code = ERROR_MARK;
5480 ret.original_type = NULL;
5481 return ret;
5484 /* Parse a conditional expression (C90 6.3.15, C99 6.5.15). If AFTER
5485 is not NULL then it is an Objective-C message expression which is
5486 the primary-expression starting the expression as an initializer.
5488 conditional-expression:
5489 logical-OR-expression
5490 logical-OR-expression ? expression : conditional-expression
5492 GNU extensions:
5494 conditional-expression:
5495 logical-OR-expression ? : conditional-expression
5498 static struct c_expr
5499 c_parser_conditional_expression (c_parser *parser, struct c_expr *after)
5501 struct c_expr cond, exp1, exp2, ret;
5502 location_t cond_loc, colon_loc, middle_loc;
5504 gcc_assert (!after || c_dialect_objc ());
5506 cond = c_parser_binary_expression (parser, after, PREC_NONE);
5508 if (c_parser_next_token_is_not (parser, CPP_QUERY))
5509 return cond;
5510 cond_loc = c_parser_peek_token (parser)->location;
5511 cond = default_function_array_read_conversion (cond_loc, cond);
5512 c_parser_consume_token (parser);
5513 if (c_parser_next_token_is (parser, CPP_COLON))
5515 tree eptype = NULL_TREE;
5517 middle_loc = c_parser_peek_token (parser)->location;
5518 pedwarn (middle_loc, OPT_Wpedantic,
5519 "ISO C forbids omitting the middle term of a ?: expression");
5520 warn_for_omitted_condop (middle_loc, cond.value);
5521 if (TREE_CODE (cond.value) == EXCESS_PRECISION_EXPR)
5523 eptype = TREE_TYPE (cond.value);
5524 cond.value = TREE_OPERAND (cond.value, 0);
5526 /* Make sure first operand is calculated only once. */
5527 exp1.value = c_save_expr (default_conversion (cond.value));
5528 if (eptype)
5529 exp1.value = build1 (EXCESS_PRECISION_EXPR, eptype, exp1.value);
5530 exp1.original_type = NULL;
5531 cond.value = c_objc_common_truthvalue_conversion (cond_loc, exp1.value);
5532 c_inhibit_evaluation_warnings += cond.value == truthvalue_true_node;
5534 else
5536 cond.value
5537 = c_objc_common_truthvalue_conversion
5538 (cond_loc, default_conversion (cond.value));
5539 c_inhibit_evaluation_warnings += cond.value == truthvalue_false_node;
5540 exp1 = c_parser_expression_conv (parser);
5541 mark_exp_read (exp1.value);
5542 c_inhibit_evaluation_warnings +=
5543 ((cond.value == truthvalue_true_node)
5544 - (cond.value == truthvalue_false_node));
5547 colon_loc = c_parser_peek_token (parser)->location;
5548 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
5550 c_inhibit_evaluation_warnings -= cond.value == truthvalue_true_node;
5551 ret.value = error_mark_node;
5552 ret.original_code = ERROR_MARK;
5553 ret.original_type = NULL;
5554 return ret;
5557 location_t exp2_loc = c_parser_peek_token (parser)->location;
5558 exp2 = c_parser_conditional_expression (parser, NULL);
5559 exp2 = default_function_array_read_conversion (exp2_loc, exp2);
5561 c_inhibit_evaluation_warnings -= cond.value == truthvalue_true_node;
5562 ret.value = build_conditional_expr (colon_loc, cond.value,
5563 cond.original_code == C_MAYBE_CONST_EXPR,
5564 exp1.value, exp1.original_type,
5565 exp2.value, exp2.original_type);
5566 ret.original_code = ERROR_MARK;
5567 if (exp1.value == error_mark_node || exp2.value == error_mark_node)
5568 ret.original_type = NULL;
5569 else
5571 tree t1, t2;
5573 /* If both sides are enum type, the default conversion will have
5574 made the type of the result be an integer type. We want to
5575 remember the enum types we started with. */
5576 t1 = exp1.original_type ? exp1.original_type : TREE_TYPE (exp1.value);
5577 t2 = exp2.original_type ? exp2.original_type : TREE_TYPE (exp2.value);
5578 ret.original_type = ((t1 != error_mark_node
5579 && t2 != error_mark_node
5580 && (TYPE_MAIN_VARIANT (t1)
5581 == TYPE_MAIN_VARIANT (t2)))
5582 ? t1
5583 : NULL);
5585 return ret;
5588 /* Parse a binary expression; that is, a logical-OR-expression (C90
5589 6.3.5-6.3.14, C99 6.5.5-6.5.14). If AFTER is not NULL then it is
5590 an Objective-C message expression which is the primary-expression
5591 starting the expression as an initializer. PREC is the starting
5592 precedence, usually PREC_NONE.
5594 multiplicative-expression:
5595 cast-expression
5596 multiplicative-expression * cast-expression
5597 multiplicative-expression / cast-expression
5598 multiplicative-expression % cast-expression
5600 additive-expression:
5601 multiplicative-expression
5602 additive-expression + multiplicative-expression
5603 additive-expression - multiplicative-expression
5605 shift-expression:
5606 additive-expression
5607 shift-expression << additive-expression
5608 shift-expression >> additive-expression
5610 relational-expression:
5611 shift-expression
5612 relational-expression < shift-expression
5613 relational-expression > shift-expression
5614 relational-expression <= shift-expression
5615 relational-expression >= shift-expression
5617 equality-expression:
5618 relational-expression
5619 equality-expression == relational-expression
5620 equality-expression != relational-expression
5622 AND-expression:
5623 equality-expression
5624 AND-expression & equality-expression
5626 exclusive-OR-expression:
5627 AND-expression
5628 exclusive-OR-expression ^ AND-expression
5630 inclusive-OR-expression:
5631 exclusive-OR-expression
5632 inclusive-OR-expression | exclusive-OR-expression
5634 logical-AND-expression:
5635 inclusive-OR-expression
5636 logical-AND-expression && inclusive-OR-expression
5638 logical-OR-expression:
5639 logical-AND-expression
5640 logical-OR-expression || logical-AND-expression
5643 static struct c_expr
5644 c_parser_binary_expression (c_parser *parser, struct c_expr *after,
5645 enum c_parser_prec prec)
5647 /* A binary expression is parsed using operator-precedence parsing,
5648 with the operands being cast expressions. All the binary
5649 operators are left-associative. Thus a binary expression is of
5650 form:
5652 E0 op1 E1 op2 E2 ...
5654 which we represent on a stack. On the stack, the precedence
5655 levels are strictly increasing. When a new operator is
5656 encountered of higher precedence than that at the top of the
5657 stack, it is pushed; its LHS is the top expression, and its RHS
5658 is everything parsed until it is popped. When a new operator is
5659 encountered with precedence less than or equal to that at the top
5660 of the stack, triples E[i-1] op[i] E[i] are popped and replaced
5661 by the result of the operation until the operator at the top of
5662 the stack has lower precedence than the new operator or there is
5663 only one element on the stack; then the top expression is the LHS
5664 of the new operator. In the case of logical AND and OR
5665 expressions, we also need to adjust c_inhibit_evaluation_warnings
5666 as appropriate when the operators are pushed and popped. */
5668 struct {
5669 /* The expression at this stack level. */
5670 struct c_expr expr;
5671 /* The precedence of the operator on its left, PREC_NONE at the
5672 bottom of the stack. */
5673 enum c_parser_prec prec;
5674 /* The operation on its left. */
5675 enum tree_code op;
5676 /* The source location of this operation. */
5677 location_t loc;
5678 } stack[NUM_PRECS];
5679 int sp;
5680 /* Location of the binary operator. */
5681 location_t binary_loc = UNKNOWN_LOCATION; /* Quiet warning. */
5682 #define POP \
5683 do { \
5684 switch (stack[sp].op) \
5686 case TRUTH_ANDIF_EXPR: \
5687 c_inhibit_evaluation_warnings -= (stack[sp - 1].expr.value \
5688 == truthvalue_false_node); \
5689 break; \
5690 case TRUTH_ORIF_EXPR: \
5691 c_inhibit_evaluation_warnings -= (stack[sp - 1].expr.value \
5692 == truthvalue_true_node); \
5693 break; \
5694 default: \
5695 break; \
5697 stack[sp - 1].expr \
5698 = default_function_array_read_conversion (stack[sp - 1].loc, \
5699 stack[sp - 1].expr); \
5700 stack[sp].expr \
5701 = default_function_array_read_conversion (stack[sp].loc, \
5702 stack[sp].expr); \
5703 stack[sp - 1].expr = parser_build_binary_op (stack[sp].loc, \
5704 stack[sp].op, \
5705 stack[sp - 1].expr, \
5706 stack[sp].expr); \
5707 sp--; \
5708 } while (0)
5709 gcc_assert (!after || c_dialect_objc ());
5710 stack[0].loc = c_parser_peek_token (parser)->location;
5711 stack[0].expr = c_parser_cast_expression (parser, after);
5712 stack[0].prec = prec;
5713 sp = 0;
5714 while (true)
5716 enum c_parser_prec oprec;
5717 enum tree_code ocode;
5718 if (parser->error)
5719 goto out;
5720 switch (c_parser_peek_token (parser)->type)
5722 case CPP_MULT:
5723 oprec = PREC_MULT;
5724 ocode = MULT_EXPR;
5725 break;
5726 case CPP_DIV:
5727 oprec = PREC_MULT;
5728 ocode = TRUNC_DIV_EXPR;
5729 break;
5730 case CPP_MOD:
5731 oprec = PREC_MULT;
5732 ocode = TRUNC_MOD_EXPR;
5733 break;
5734 case CPP_PLUS:
5735 oprec = PREC_ADD;
5736 ocode = PLUS_EXPR;
5737 break;
5738 case CPP_MINUS:
5739 oprec = PREC_ADD;
5740 ocode = MINUS_EXPR;
5741 break;
5742 case CPP_LSHIFT:
5743 oprec = PREC_SHIFT;
5744 ocode = LSHIFT_EXPR;
5745 break;
5746 case CPP_RSHIFT:
5747 oprec = PREC_SHIFT;
5748 ocode = RSHIFT_EXPR;
5749 break;
5750 case CPP_LESS:
5751 oprec = PREC_REL;
5752 ocode = LT_EXPR;
5753 break;
5754 case CPP_GREATER:
5755 oprec = PREC_REL;
5756 ocode = GT_EXPR;
5757 break;
5758 case CPP_LESS_EQ:
5759 oprec = PREC_REL;
5760 ocode = LE_EXPR;
5761 break;
5762 case CPP_GREATER_EQ:
5763 oprec = PREC_REL;
5764 ocode = GE_EXPR;
5765 break;
5766 case CPP_EQ_EQ:
5767 oprec = PREC_EQ;
5768 ocode = EQ_EXPR;
5769 break;
5770 case CPP_NOT_EQ:
5771 oprec = PREC_EQ;
5772 ocode = NE_EXPR;
5773 break;
5774 case CPP_AND:
5775 oprec = PREC_BITAND;
5776 ocode = BIT_AND_EXPR;
5777 break;
5778 case CPP_XOR:
5779 oprec = PREC_BITXOR;
5780 ocode = BIT_XOR_EXPR;
5781 break;
5782 case CPP_OR:
5783 oprec = PREC_BITOR;
5784 ocode = BIT_IOR_EXPR;
5785 break;
5786 case CPP_AND_AND:
5787 oprec = PREC_LOGAND;
5788 ocode = TRUTH_ANDIF_EXPR;
5789 break;
5790 case CPP_OR_OR:
5791 oprec = PREC_LOGOR;
5792 ocode = TRUTH_ORIF_EXPR;
5793 break;
5794 default:
5795 /* Not a binary operator, so end of the binary
5796 expression. */
5797 goto out;
5799 binary_loc = c_parser_peek_token (parser)->location;
5800 while (oprec <= stack[sp].prec)
5802 if (sp == 0)
5803 goto out;
5804 POP;
5806 c_parser_consume_token (parser);
5807 switch (ocode)
5809 case TRUTH_ANDIF_EXPR:
5810 stack[sp].expr
5811 = default_function_array_read_conversion (stack[sp].loc,
5812 stack[sp].expr);
5813 stack[sp].expr.value = c_objc_common_truthvalue_conversion
5814 (stack[sp].loc, default_conversion (stack[sp].expr.value));
5815 c_inhibit_evaluation_warnings += (stack[sp].expr.value
5816 == truthvalue_false_node);
5817 break;
5818 case TRUTH_ORIF_EXPR:
5819 stack[sp].expr
5820 = default_function_array_read_conversion (stack[sp].loc,
5821 stack[sp].expr);
5822 stack[sp].expr.value = c_objc_common_truthvalue_conversion
5823 (stack[sp].loc, default_conversion (stack[sp].expr.value));
5824 c_inhibit_evaluation_warnings += (stack[sp].expr.value
5825 == truthvalue_true_node);
5826 break;
5827 default:
5828 break;
5830 sp++;
5831 stack[sp].loc = binary_loc;
5832 stack[sp].expr = c_parser_cast_expression (parser, NULL);
5833 stack[sp].prec = oprec;
5834 stack[sp].op = ocode;
5835 stack[sp].loc = binary_loc;
5837 out:
5838 while (sp > 0)
5839 POP;
5840 return stack[0].expr;
5841 #undef POP
5844 /* Parse a cast expression (C90 6.3.4, C99 6.5.4). If AFTER is not
5845 NULL then it is an Objective-C message expression which is the
5846 primary-expression starting the expression as an initializer.
5848 cast-expression:
5849 unary-expression
5850 ( type-name ) unary-expression
5853 static struct c_expr
5854 c_parser_cast_expression (c_parser *parser, struct c_expr *after)
5856 location_t cast_loc = c_parser_peek_token (parser)->location;
5857 gcc_assert (!after || c_dialect_objc ());
5858 if (after)
5859 return c_parser_postfix_expression_after_primary (parser,
5860 cast_loc, *after);
5861 /* If the expression begins with a parenthesized type name, it may
5862 be either a cast or a compound literal; we need to see whether
5863 the next character is '{' to tell the difference. If not, it is
5864 an unary expression. Full detection of unknown typenames here
5865 would require a 3-token lookahead. */
5866 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
5867 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
5869 struct c_type_name *type_name;
5870 struct c_expr ret;
5871 struct c_expr expr;
5872 c_parser_consume_token (parser);
5873 type_name = c_parser_type_name (parser);
5874 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5875 if (type_name == NULL)
5877 ret.value = error_mark_node;
5878 ret.original_code = ERROR_MARK;
5879 ret.original_type = NULL;
5880 return ret;
5883 /* Save casted types in the function's used types hash table. */
5884 used_types_insert (type_name->specs->type);
5886 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
5887 return c_parser_postfix_expression_after_paren_type (parser, type_name,
5888 cast_loc);
5890 location_t expr_loc = c_parser_peek_token (parser)->location;
5891 expr = c_parser_cast_expression (parser, NULL);
5892 expr = default_function_array_read_conversion (expr_loc, expr);
5894 ret.value = c_cast_expr (cast_loc, type_name, expr.value);
5895 ret.original_code = ERROR_MARK;
5896 ret.original_type = NULL;
5897 return ret;
5899 else
5900 return c_parser_unary_expression (parser);
5903 /* Parse an unary expression (C90 6.3.3, C99 6.5.3).
5905 unary-expression:
5906 postfix-expression
5907 ++ unary-expression
5908 -- unary-expression
5909 unary-operator cast-expression
5910 sizeof unary-expression
5911 sizeof ( type-name )
5913 unary-operator: one of
5914 & * + - ~ !
5916 GNU extensions:
5918 unary-expression:
5919 __alignof__ unary-expression
5920 __alignof__ ( type-name )
5921 && identifier
5923 (C11 permits _Alignof with type names only.)
5925 unary-operator: one of
5926 __extension__ __real__ __imag__
5928 Transactional Memory:
5930 unary-expression:
5931 transaction-expression
5933 In addition, the GNU syntax treats ++ and -- as unary operators, so
5934 they may be applied to cast expressions with errors for non-lvalues
5935 given later. */
5937 static struct c_expr
5938 c_parser_unary_expression (c_parser *parser)
5940 int ext;
5941 struct c_expr ret, op;
5942 location_t op_loc = c_parser_peek_token (parser)->location;
5943 location_t exp_loc;
5944 ret.original_code = ERROR_MARK;
5945 ret.original_type = NULL;
5946 switch (c_parser_peek_token (parser)->type)
5948 case CPP_PLUS_PLUS:
5949 c_parser_consume_token (parser);
5950 exp_loc = c_parser_peek_token (parser)->location;
5951 op = c_parser_cast_expression (parser, NULL);
5952 op = default_function_array_read_conversion (exp_loc, op);
5953 return parser_build_unary_op (op_loc, PREINCREMENT_EXPR, op);
5954 case CPP_MINUS_MINUS:
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, PREDECREMENT_EXPR, op);
5960 case CPP_AND:
5961 c_parser_consume_token (parser);
5962 op = c_parser_cast_expression (parser, NULL);
5963 mark_exp_read (op.value);
5964 return parser_build_unary_op (op_loc, ADDR_EXPR, op);
5965 case CPP_MULT:
5966 c_parser_consume_token (parser);
5967 exp_loc = c_parser_peek_token (parser)->location;
5968 op = c_parser_cast_expression (parser, NULL);
5969 op = default_function_array_read_conversion (exp_loc, op);
5970 ret.value = build_indirect_ref (op_loc, op.value, RO_UNARY_STAR);
5971 return ret;
5972 case CPP_PLUS:
5973 if (!c_dialect_objc () && !in_system_header)
5974 warning_at (op_loc,
5975 OPT_Wtraditional,
5976 "traditional C rejects the unary plus operator");
5977 c_parser_consume_token (parser);
5978 exp_loc = c_parser_peek_token (parser)->location;
5979 op = c_parser_cast_expression (parser, NULL);
5980 op = default_function_array_read_conversion (exp_loc, op);
5981 return parser_build_unary_op (op_loc, CONVERT_EXPR, op);
5982 case CPP_MINUS:
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, NEGATE_EXPR, op);
5988 case CPP_COMPL:
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, BIT_NOT_EXPR, op);
5994 case CPP_NOT:
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, TRUTH_NOT_EXPR, op);
6000 case CPP_AND_AND:
6001 /* Refer to the address of a label as a pointer. */
6002 c_parser_consume_token (parser);
6003 if (c_parser_next_token_is (parser, CPP_NAME))
6005 ret.value = finish_label_address_expr
6006 (c_parser_peek_token (parser)->value, op_loc);
6007 c_parser_consume_token (parser);
6009 else
6011 c_parser_error (parser, "expected identifier");
6012 ret.value = error_mark_node;
6014 return ret;
6015 case CPP_KEYWORD:
6016 switch (c_parser_peek_token (parser)->keyword)
6018 case RID_SIZEOF:
6019 return c_parser_sizeof_expression (parser);
6020 case RID_UPC_BLOCKSIZEOF:
6021 case RID_UPC_ELEMSIZEOF:
6022 case RID_UPC_LOCALSIZEOF:
6023 gcc_assert (c_dialect_upc ());
6024 return c_parser_sizeof_expression (parser);
6025 case RID_ALIGNOF:
6026 return c_parser_alignof_expression (parser);
6027 case RID_EXTENSION:
6028 c_parser_consume_token (parser);
6029 ext = disable_extension_diagnostics ();
6030 ret = c_parser_cast_expression (parser, NULL);
6031 restore_extension_diagnostics (ext);
6032 return ret;
6033 case RID_REALPART:
6034 c_parser_consume_token (parser);
6035 exp_loc = c_parser_peek_token (parser)->location;
6036 op = c_parser_cast_expression (parser, NULL);
6037 op = default_function_array_conversion (exp_loc, op);
6038 return parser_build_unary_op (op_loc, REALPART_EXPR, op);
6039 case RID_IMAGPART:
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, IMAGPART_EXPR, op);
6045 case RID_TRANSACTION_ATOMIC:
6046 case RID_TRANSACTION_RELAXED:
6047 return c_parser_transaction_expression (parser,
6048 c_parser_peek_token (parser)->keyword);
6049 default:
6050 return c_parser_postfix_expression (parser);
6052 default:
6053 return c_parser_postfix_expression (parser);
6057 /* Return the result of upc_blocksizeof applied to EXPR. */
6059 static
6060 struct c_expr
6061 upc_blocksizeof_expr (location_t loc, struct c_expr expr)
6063 struct c_expr ret;
6064 ret.original_code = ERROR_MARK;
6065 ret.original_type = NULL_TREE;
6066 if (expr.value == error_mark_node)
6068 ret.value = error_mark_node;
6069 pop_maybe_used (false);
6071 else
6073 ret.value = upc_blocksizeof (loc, TREE_TYPE (expr.value));
6074 pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (expr.value)));
6076 return ret;
6079 /* Return the result of upc_blocksizeof applied to T, a structure
6080 for the type name passed to sizeof (rather than the type itself). */
6082 static
6083 struct c_expr
6084 upc_blocksizeof_type (location_t loc, struct c_type_name *t)
6086 tree type;
6087 struct c_expr ret;
6088 ret.original_code = ERROR_MARK;
6089 ret.original_type = NULL_TREE;
6090 type = groktypename (t, NULL, NULL);
6091 if (type == error_mark_node)
6093 ret.value = error_mark_node;
6094 pop_maybe_used (false);
6096 else
6098 ret.value = upc_blocksizeof (loc, type);
6099 pop_maybe_used (C_TYPE_VARIABLE_SIZE (type));
6101 return ret;
6104 /* Return the result of upc_elemsizeof applied to EXPR. */
6106 static
6107 struct c_expr
6108 upc_elemsizeof_expr (location_t loc, struct c_expr expr)
6110 struct c_expr ret;
6111 ret.original_code = ERROR_MARK;
6112 ret.original_type = NULL_TREE;
6113 if (expr.value == error_mark_node)
6115 ret.value = error_mark_node;
6116 pop_maybe_used (false);
6118 else
6120 ret.value = upc_elemsizeof (loc, TREE_TYPE (expr.value));
6121 pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (expr.value)));
6123 return ret;
6126 /* Return the result of upc_elemsizeof applied to T, a structure
6127 for the type name passed to sizeof (rather than the type itself). */
6129 static
6130 struct c_expr
6131 upc_elemsizeof_type (location_t loc, struct c_type_name *t)
6133 tree type;
6134 struct c_expr ret;
6135 ret.original_code = ERROR_MARK;
6136 ret.original_type = NULL_TREE;
6137 type = groktypename (t, NULL, NULL);
6138 if (type == error_mark_node)
6140 ret.value = error_mark_node;
6141 pop_maybe_used (false);
6143 else
6145 ret.value = upc_elemsizeof (loc, type);
6146 pop_maybe_used (C_TYPE_VARIABLE_SIZE (type));
6148 return ret;
6151 /* Return the result of upc_localsizeof applied to EXPR. */
6153 static
6154 struct c_expr
6155 upc_localsizeof_expr (location_t loc, struct c_expr expr)
6157 struct c_expr ret;
6158 ret.original_code = ERROR_MARK;
6159 ret.original_type = NULL_TREE;
6160 if (expr.value == error_mark_node)
6162 ret.value = error_mark_node;
6163 pop_maybe_used (false);
6165 else
6167 ret.value = upc_localsizeof (loc, TREE_TYPE (expr.value));
6168 pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (expr.value)));
6170 return ret;
6173 /* Return the result of upc_localsizeof applied to T, a structure
6174 for the type name passed to sizeof (rather than the type itself). */
6176 static
6177 struct c_expr
6178 upc_localsizeof_type (location_t loc, struct c_type_name *t)
6180 tree type;
6181 struct c_expr ret;
6182 ret.original_code = ERROR_MARK;
6183 ret.original_type = NULL_TREE;
6184 type = groktypename (t, NULL, NULL);
6185 if (type == error_mark_node)
6187 ret.value = error_mark_node;
6188 pop_maybe_used (false);
6190 else
6192 ret.value = upc_localsizeof (loc, type);
6193 pop_maybe_used (C_TYPE_VARIABLE_SIZE (type));
6195 return ret;
6198 /* Parse a sizeof expression. */
6200 static struct c_expr
6201 c_parser_sizeof_expression (c_parser *parser)
6203 struct c_expr expr;
6204 location_t expr_loc;
6205 enum rid keyword = c_parser_peek_token (parser)->keyword;
6206 c_parser_consume_token (parser);
6207 c_inhibit_evaluation_warnings++;
6208 in_sizeof++;
6209 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
6210 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
6212 /* Either sizeof ( type-name ) or sizeof unary-expression
6213 starting with a compound literal. */
6214 struct c_type_name *type_name;
6215 c_parser_consume_token (parser);
6216 expr_loc = c_parser_peek_token (parser)->location;
6217 type_name = c_parser_type_name (parser);
6218 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6219 if (type_name == NULL)
6221 struct c_expr ret;
6222 c_inhibit_evaluation_warnings--;
6223 in_sizeof--;
6224 ret.value = error_mark_node;
6225 ret.original_code = ERROR_MARK;
6226 ret.original_type = NULL;
6227 return ret;
6229 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
6231 expr = c_parser_postfix_expression_after_paren_type (parser,
6232 type_name,
6233 expr_loc);
6234 goto sizeof_expr;
6236 /* sizeof ( type-name ). */
6237 c_inhibit_evaluation_warnings--;
6238 in_sizeof--;
6239 /* Handle upc_*_sizeof (type) operations. */
6240 switch (keyword)
6242 case RID_UPC_BLOCKSIZEOF:
6243 return upc_blocksizeof_type (expr_loc, type_name);
6244 case RID_UPC_ELEMSIZEOF:
6245 return upc_elemsizeof_type (expr_loc, type_name);
6246 case RID_UPC_LOCALSIZEOF:
6247 return upc_localsizeof_type (expr_loc, type_name);
6248 default: break;
6250 return c_expr_sizeof_type (expr_loc, type_name);
6252 else
6254 expr_loc = c_parser_peek_token (parser)->location;
6255 expr = c_parser_unary_expression (parser);
6256 sizeof_expr:
6257 c_inhibit_evaluation_warnings--;
6258 in_sizeof--;
6259 mark_exp_read (expr.value);
6260 if (TREE_CODE (expr.value) == COMPONENT_REF
6261 && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
6262 error_at (expr_loc, "%<sizeof%> applied to a bit-field");
6263 /* Handle upc_*_sizeof (expr) operations. */
6264 switch (keyword)
6266 case RID_UPC_BLOCKSIZEOF:
6267 return upc_blocksizeof_expr (expr_loc, expr);
6268 case RID_UPC_ELEMSIZEOF:
6269 return upc_elemsizeof_expr (expr_loc, expr);
6270 case RID_UPC_LOCALSIZEOF:
6271 return upc_localsizeof_expr (expr_loc, expr);
6272 case RID_SIZEOF:
6273 return c_expr_sizeof_expr (expr_loc, expr);
6274 default: break;
6276 return c_expr_sizeof_expr (expr_loc, expr);
6280 /* Parse an alignof expression. */
6282 static struct c_expr
6283 c_parser_alignof_expression (c_parser *parser)
6285 struct c_expr expr;
6286 location_t loc = c_parser_peek_token (parser)->location;
6287 tree alignof_spelling = c_parser_peek_token (parser)->value;
6288 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ALIGNOF));
6289 /* A diagnostic is not required for the use of this identifier in
6290 the implementation namespace; only diagnose it for the C11
6291 spelling because of existing code using the other spellings. */
6292 if (!flag_isoc11
6293 && strcmp (IDENTIFIER_POINTER (alignof_spelling), "_Alignof") == 0)
6295 if (flag_isoc99)
6296 pedwarn (loc, OPT_Wpedantic, "ISO C99 does not support %qE",
6297 alignof_spelling);
6298 else
6299 pedwarn (loc, OPT_Wpedantic, "ISO C90 does not support %qE",
6300 alignof_spelling);
6302 c_parser_consume_token (parser);
6303 c_inhibit_evaluation_warnings++;
6304 in_alignof++;
6305 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
6306 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
6308 /* Either __alignof__ ( type-name ) or __alignof__
6309 unary-expression starting with a compound literal. */
6310 location_t loc;
6311 struct c_type_name *type_name;
6312 struct c_expr ret;
6313 c_parser_consume_token (parser);
6314 loc = c_parser_peek_token (parser)->location;
6315 type_name = c_parser_type_name (parser);
6316 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6317 if (type_name == NULL)
6319 struct c_expr ret;
6320 c_inhibit_evaluation_warnings--;
6321 in_alignof--;
6322 ret.value = error_mark_node;
6323 ret.original_code = ERROR_MARK;
6324 ret.original_type = NULL;
6325 return ret;
6327 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
6329 expr = c_parser_postfix_expression_after_paren_type (parser,
6330 type_name,
6331 loc);
6332 goto alignof_expr;
6334 /* alignof ( type-name ). */
6335 c_inhibit_evaluation_warnings--;
6336 in_alignof--;
6337 ret.value = c_alignof (loc, groktypename (type_name, NULL, NULL));
6338 ret.original_code = ERROR_MARK;
6339 ret.original_type = NULL;
6340 return ret;
6342 else
6344 struct c_expr ret;
6345 expr = c_parser_unary_expression (parser);
6346 alignof_expr:
6347 mark_exp_read (expr.value);
6348 c_inhibit_evaluation_warnings--;
6349 in_alignof--;
6350 pedwarn (loc, OPT_Wpedantic, "ISO C does not allow %<%E (expression)%>",
6351 alignof_spelling);
6352 ret.value = c_alignof_expr (loc, expr.value);
6353 ret.original_code = ERROR_MARK;
6354 ret.original_type = NULL;
6355 return ret;
6359 /* Helper function to read arguments of builtins which are interfaces
6360 for the middle-end nodes like COMPLEX_EXPR, VEC_PERM_EXPR and
6361 others. The name of the builtin is passed using BNAME parameter.
6362 Function returns true if there were no errors while parsing and
6363 stores the arguments in CEXPR_LIST. */
6364 static bool
6365 c_parser_get_builtin_args (c_parser *parser, const char *bname,
6366 vec<c_expr_t, va_gc> **ret_cexpr_list)
6368 location_t loc = c_parser_peek_token (parser)->location;
6369 vec<c_expr_t, va_gc> *cexpr_list;
6370 c_expr_t expr;
6372 *ret_cexpr_list = NULL;
6373 if (c_parser_next_token_is_not (parser, CPP_OPEN_PAREN))
6375 error_at (loc, "cannot take address of %qs", bname);
6376 return false;
6379 c_parser_consume_token (parser);
6381 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
6383 c_parser_consume_token (parser);
6384 return true;
6387 expr = c_parser_expr_no_commas (parser, NULL);
6388 vec_alloc (cexpr_list, 1);
6389 C_EXPR_APPEND (cexpr_list, expr);
6390 while (c_parser_next_token_is (parser, CPP_COMMA))
6392 c_parser_consume_token (parser);
6393 expr = c_parser_expr_no_commas (parser, NULL);
6394 C_EXPR_APPEND (cexpr_list, expr);
6397 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
6398 return false;
6400 *ret_cexpr_list = cexpr_list;
6401 return true;
6405 /* Parse a postfix expression (C90 6.3.1-6.3.2, C99 6.5.1-6.5.2).
6407 postfix-expression:
6408 primary-expression
6409 postfix-expression [ expression ]
6410 postfix-expression ( argument-expression-list[opt] )
6411 postfix-expression . identifier
6412 postfix-expression -> identifier
6413 postfix-expression ++
6414 postfix-expression --
6415 ( type-name ) { initializer-list }
6416 ( type-name ) { initializer-list , }
6418 argument-expression-list:
6419 argument-expression
6420 argument-expression-list , argument-expression
6422 primary-expression:
6423 identifier
6424 constant
6425 string-literal
6426 ( expression )
6428 GNU extensions:
6430 primary-expression:
6431 __func__
6432 (treated as a keyword in GNU C)
6433 __FUNCTION__
6434 __PRETTY_FUNCTION__
6435 ( compound-statement )
6436 __builtin_va_arg ( assignment-expression , type-name )
6437 __builtin_offsetof ( type-name , offsetof-member-designator )
6438 __builtin_choose_expr ( assignment-expression ,
6439 assignment-expression ,
6440 assignment-expression )
6441 __builtin_types_compatible_p ( type-name , type-name )
6442 __builtin_complex ( assignment-expression , assignment-expression )
6443 __builtin_shuffle ( assignment-expression , assignment-expression )
6444 __builtin_shuffle ( assignment-expression ,
6445 assignment-expression ,
6446 assignment-expression, )
6448 offsetof-member-designator:
6449 identifier
6450 offsetof-member-designator . identifier
6451 offsetof-member-designator [ expression ]
6453 Objective-C:
6455 primary-expression:
6456 [ objc-receiver objc-message-args ]
6457 @selector ( objc-selector-arg )
6458 @protocol ( identifier )
6459 @encode ( type-name )
6460 objc-string-literal
6461 Classname . identifier
6464 static struct c_expr
6465 c_parser_postfix_expression (c_parser *parser)
6467 struct c_expr expr, e1;
6468 struct c_type_name *t1, *t2;
6469 location_t loc = c_parser_peek_token (parser)->location;;
6470 expr.original_code = ERROR_MARK;
6471 expr.original_type = NULL;
6472 switch (c_parser_peek_token (parser)->type)
6474 case CPP_NUMBER:
6475 expr.value = c_parser_peek_token (parser)->value;
6476 loc = c_parser_peek_token (parser)->location;
6477 c_parser_consume_token (parser);
6478 if (TREE_CODE (expr.value) == FIXED_CST
6479 && !targetm.fixed_point_supported_p ())
6481 error_at (loc, "fixed-point types not supported for this target");
6482 expr.value = error_mark_node;
6484 break;
6485 case CPP_CHAR:
6486 case CPP_CHAR16:
6487 case CPP_CHAR32:
6488 case CPP_WCHAR:
6489 expr.value = c_parser_peek_token (parser)->value;
6490 c_parser_consume_token (parser);
6491 break;
6492 case CPP_STRING:
6493 case CPP_STRING16:
6494 case CPP_STRING32:
6495 case CPP_WSTRING:
6496 case CPP_UTF8STRING:
6497 expr.value = c_parser_peek_token (parser)->value;
6498 expr.original_code = STRING_CST;
6499 c_parser_consume_token (parser);
6500 break;
6501 case CPP_OBJC_STRING:
6502 gcc_assert (c_dialect_objc ());
6503 expr.value
6504 = objc_build_string_object (c_parser_peek_token (parser)->value);
6505 c_parser_consume_token (parser);
6506 break;
6507 case CPP_NAME:
6508 switch (c_parser_peek_token (parser)->id_kind)
6510 case C_ID_ID:
6512 tree id = c_parser_peek_token (parser)->value;
6513 c_parser_consume_token (parser);
6514 expr.value = build_external_ref (loc, id,
6515 (c_parser_peek_token (parser)->type
6516 == CPP_OPEN_PAREN),
6517 &expr.original_type);
6518 break;
6520 case C_ID_CLASSNAME:
6522 /* Here we parse the Objective-C 2.0 Class.name dot
6523 syntax. */
6524 tree class_name = c_parser_peek_token (parser)->value;
6525 tree component;
6526 c_parser_consume_token (parser);
6527 gcc_assert (c_dialect_objc ());
6528 if (!c_parser_require (parser, CPP_DOT, "expected %<.%>"))
6530 expr.value = error_mark_node;
6531 break;
6533 if (c_parser_next_token_is_not (parser, CPP_NAME))
6535 c_parser_error (parser, "expected identifier");
6536 expr.value = error_mark_node;
6537 break;
6539 component = c_parser_peek_token (parser)->value;
6540 c_parser_consume_token (parser);
6541 expr.value = objc_build_class_component_ref (class_name,
6542 component);
6543 break;
6545 default:
6546 c_parser_error (parser, "expected expression");
6547 expr.value = error_mark_node;
6548 break;
6550 break;
6551 case CPP_OPEN_PAREN:
6552 /* A parenthesized expression, statement expression or compound
6553 literal. */
6554 if (c_parser_peek_2nd_token (parser)->type == CPP_OPEN_BRACE)
6556 /* A statement expression. */
6557 tree stmt;
6558 location_t brace_loc;
6559 c_parser_consume_token (parser);
6560 brace_loc = c_parser_peek_token (parser)->location;
6561 c_parser_consume_token (parser);
6562 if (!building_stmt_list_p ())
6564 error_at (loc, "braced-group within expression allowed "
6565 "only inside a function");
6566 parser->error = true;
6567 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
6568 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6569 expr.value = error_mark_node;
6570 break;
6572 stmt = c_begin_stmt_expr ();
6573 c_parser_compound_statement_nostart (parser);
6574 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6575 "expected %<)%>");
6576 pedwarn (loc, OPT_Wpedantic,
6577 "ISO C forbids braced-groups within expressions");
6578 expr.value = c_finish_stmt_expr (brace_loc, stmt);
6579 mark_exp_read (expr.value);
6581 else if (c_token_starts_typename (c_parser_peek_2nd_token (parser)))
6583 /* A compound literal. ??? Can we actually get here rather
6584 than going directly to
6585 c_parser_postfix_expression_after_paren_type from
6586 elsewhere? */
6587 location_t loc;
6588 struct c_type_name *type_name;
6589 c_parser_consume_token (parser);
6590 loc = c_parser_peek_token (parser)->location;
6591 type_name = c_parser_type_name (parser);
6592 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6593 "expected %<)%>");
6594 if (type_name == NULL)
6596 expr.value = error_mark_node;
6598 else
6599 expr = c_parser_postfix_expression_after_paren_type (parser,
6600 type_name,
6601 loc);
6603 else
6605 /* A parenthesized expression. */
6606 c_parser_consume_token (parser);
6607 expr = c_parser_expression (parser);
6608 if (TREE_CODE (expr.value) == MODIFY_EXPR)
6609 TREE_NO_WARNING (expr.value) = 1;
6610 if (expr.original_code != C_MAYBE_CONST_EXPR)
6611 expr.original_code = ERROR_MARK;
6612 /* Don't change EXPR.ORIGINAL_TYPE. */
6613 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6614 "expected %<)%>");
6616 break;
6617 case CPP_KEYWORD:
6618 switch (c_parser_peek_token (parser)->keyword)
6620 case RID_FUNCTION_NAME:
6621 case RID_PRETTY_FUNCTION_NAME:
6622 case RID_C99_FUNCTION_NAME:
6623 expr.value = fname_decl (loc,
6624 c_parser_peek_token (parser)->keyword,
6625 c_parser_peek_token (parser)->value);
6626 c_parser_consume_token (parser);
6627 break;
6628 case RID_VA_ARG:
6629 c_parser_consume_token (parser);
6630 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6632 expr.value = error_mark_node;
6633 break;
6635 e1 = c_parser_expr_no_commas (parser, NULL);
6636 mark_exp_read (e1.value);
6637 e1.value = c_fully_fold (e1.value, false, NULL);
6638 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
6640 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6641 expr.value = error_mark_node;
6642 break;
6644 loc = c_parser_peek_token (parser)->location;
6645 t1 = c_parser_type_name (parser);
6646 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6647 "expected %<)%>");
6648 if (t1 == NULL)
6650 expr.value = error_mark_node;
6652 else
6654 tree type_expr = NULL_TREE;
6655 expr.value = c_build_va_arg (loc, e1.value,
6656 groktypename (t1, &type_expr, NULL));
6657 if (type_expr)
6659 expr.value = build2 (C_MAYBE_CONST_EXPR,
6660 TREE_TYPE (expr.value), type_expr,
6661 expr.value);
6662 C_MAYBE_CONST_EXPR_NON_CONST (expr.value) = true;
6665 break;
6666 case RID_OFFSETOF:
6667 c_parser_consume_token (parser);
6668 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6670 expr.value = error_mark_node;
6671 break;
6673 t1 = c_parser_type_name (parser);
6674 if (t1 == NULL)
6675 parser->error = true;
6676 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
6677 gcc_assert (parser->error);
6678 if (parser->error)
6680 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6681 expr.value = error_mark_node;
6682 break;
6686 tree type = groktypename (t1, NULL, NULL);
6687 tree offsetof_ref;
6688 if (type == error_mark_node)
6689 offsetof_ref = error_mark_node;
6690 else
6692 offsetof_ref = build1 (INDIRECT_REF, type, null_pointer_node);
6693 SET_EXPR_LOCATION (offsetof_ref, loc);
6695 /* Parse the second argument to __builtin_offsetof. We
6696 must have one identifier, and beyond that we want to
6697 accept sub structure and sub array references. */
6698 if (c_parser_next_token_is (parser, CPP_NAME))
6700 offsetof_ref = build_component_ref
6701 (loc, offsetof_ref, c_parser_peek_token (parser)->value);
6702 c_parser_consume_token (parser);
6703 while (c_parser_next_token_is (parser, CPP_DOT)
6704 || c_parser_next_token_is (parser,
6705 CPP_OPEN_SQUARE)
6706 || c_parser_next_token_is (parser,
6707 CPP_DEREF))
6709 if (c_parser_next_token_is (parser, CPP_DEREF))
6711 loc = c_parser_peek_token (parser)->location;
6712 offsetof_ref = build_array_ref (loc,
6713 offsetof_ref,
6714 integer_zero_node);
6715 goto do_dot;
6717 else if (c_parser_next_token_is (parser, CPP_DOT))
6719 do_dot:
6720 c_parser_consume_token (parser);
6721 if (c_parser_next_token_is_not (parser,
6722 CPP_NAME))
6724 c_parser_error (parser, "expected identifier");
6725 break;
6727 offsetof_ref = build_component_ref
6728 (loc, offsetof_ref,
6729 c_parser_peek_token (parser)->value);
6730 c_parser_consume_token (parser);
6732 else
6734 tree idx;
6735 loc = c_parser_peek_token (parser)->location;
6736 c_parser_consume_token (parser);
6737 idx = c_parser_expression (parser).value;
6738 idx = c_fully_fold (idx, false, NULL);
6739 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
6740 "expected %<]%>");
6741 offsetof_ref = build_array_ref (loc, offsetof_ref, idx);
6745 else
6746 c_parser_error (parser, "expected identifier");
6747 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6748 "expected %<)%>");
6749 expr.value = fold_offsetof (offsetof_ref);
6751 break;
6752 case RID_CHOOSE_EXPR:
6754 vec<c_expr_t, va_gc> *cexpr_list;
6755 c_expr_t *e1_p, *e2_p, *e3_p;
6756 tree c;
6758 c_parser_consume_token (parser);
6759 if (!c_parser_get_builtin_args (parser,
6760 "__builtin_choose_expr",
6761 &cexpr_list))
6763 expr.value = error_mark_node;
6764 break;
6767 if (vec_safe_length (cexpr_list) != 3)
6769 error_at (loc, "wrong number of arguments to "
6770 "%<__builtin_choose_expr%>");
6771 expr.value = error_mark_node;
6772 break;
6775 e1_p = &(*cexpr_list)[0];
6776 e2_p = &(*cexpr_list)[1];
6777 e3_p = &(*cexpr_list)[2];
6779 c = e1_p->value;
6780 mark_exp_read (e2_p->value);
6781 mark_exp_read (e3_p->value);
6782 if (TREE_CODE (c) != INTEGER_CST
6783 || !INTEGRAL_TYPE_P (TREE_TYPE (c)))
6784 error_at (loc,
6785 "first argument to %<__builtin_choose_expr%> not"
6786 " a constant");
6787 constant_expression_warning (c);
6788 expr = integer_zerop (c) ? *e3_p : *e2_p;
6789 break;
6791 case RID_TYPES_COMPATIBLE_P:
6792 c_parser_consume_token (parser);
6793 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6795 expr.value = error_mark_node;
6796 break;
6798 t1 = c_parser_type_name (parser);
6799 if (t1 == NULL)
6801 expr.value = error_mark_node;
6802 break;
6804 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
6806 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6807 expr.value = error_mark_node;
6808 break;
6810 t2 = c_parser_type_name (parser);
6811 if (t2 == NULL)
6813 expr.value = error_mark_node;
6814 break;
6816 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6817 "expected %<)%>");
6819 tree e1, e2;
6820 e1 = groktypename (t1, NULL, NULL);
6821 e2 = groktypename (t2, NULL, NULL);
6822 if (e1 == error_mark_node || e2 == error_mark_node)
6824 expr.value = error_mark_node;
6825 break;
6828 e1 = TYPE_MAIN_VARIANT (e1);
6829 e2 = TYPE_MAIN_VARIANT (e2);
6831 expr.value
6832 = comptypes (e1, e2) ? integer_one_node : integer_zero_node;
6834 break;
6835 case RID_BUILTIN_COMPLEX:
6837 vec<c_expr_t, va_gc> *cexpr_list;
6838 c_expr_t *e1_p, *e2_p;
6840 c_parser_consume_token (parser);
6841 if (!c_parser_get_builtin_args (parser,
6842 "__builtin_complex",
6843 &cexpr_list))
6845 expr.value = error_mark_node;
6846 break;
6849 if (vec_safe_length (cexpr_list) != 2)
6851 error_at (loc, "wrong number of arguments to "
6852 "%<__builtin_complex%>");
6853 expr.value = error_mark_node;
6854 break;
6857 e1_p = &(*cexpr_list)[0];
6858 e2_p = &(*cexpr_list)[1];
6860 mark_exp_read (e1_p->value);
6861 if (TREE_CODE (e1_p->value) == EXCESS_PRECISION_EXPR)
6862 e1_p->value = convert (TREE_TYPE (e1_p->value),
6863 TREE_OPERAND (e1_p->value, 0));
6864 mark_exp_read (e2_p->value);
6865 if (TREE_CODE (e2_p->value) == EXCESS_PRECISION_EXPR)
6866 e2_p->value = convert (TREE_TYPE (e2_p->value),
6867 TREE_OPERAND (e2_p->value, 0));
6868 if (!SCALAR_FLOAT_TYPE_P (TREE_TYPE (e1_p->value))
6869 || DECIMAL_FLOAT_TYPE_P (TREE_TYPE (e1_p->value))
6870 || !SCALAR_FLOAT_TYPE_P (TREE_TYPE (e2_p->value))
6871 || DECIMAL_FLOAT_TYPE_P (TREE_TYPE (e2_p->value)))
6873 error_at (loc, "%<__builtin_complex%> operand "
6874 "not of real binary floating-point type");
6875 expr.value = error_mark_node;
6876 break;
6878 if (TYPE_MAIN_VARIANT (TREE_TYPE (e1_p->value))
6879 != TYPE_MAIN_VARIANT (TREE_TYPE (e2_p->value)))
6881 error_at (loc,
6882 "%<__builtin_complex%> operands of different types");
6883 expr.value = error_mark_node;
6884 break;
6886 if (!flag_isoc99)
6887 pedwarn (loc, OPT_Wpedantic,
6888 "ISO C90 does not support complex types");
6889 expr.value = build2 (COMPLEX_EXPR,
6890 build_complex_type
6891 (TYPE_MAIN_VARIANT
6892 (TREE_TYPE (e1_p->value))),
6893 e1_p->value, e2_p->value);
6894 break;
6896 case RID_BUILTIN_SHUFFLE:
6898 vec<c_expr_t, va_gc> *cexpr_list;
6899 unsigned int i;
6900 c_expr_t *p;
6902 c_parser_consume_token (parser);
6903 if (!c_parser_get_builtin_args (parser,
6904 "__builtin_shuffle",
6905 &cexpr_list))
6907 expr.value = error_mark_node;
6908 break;
6911 FOR_EACH_VEC_SAFE_ELT (cexpr_list, i, p)
6912 mark_exp_read (p->value);
6914 if (vec_safe_length (cexpr_list) == 2)
6915 expr.value =
6916 c_build_vec_perm_expr
6917 (loc, (*cexpr_list)[0].value,
6918 NULL_TREE, (*cexpr_list)[1].value);
6920 else if (vec_safe_length (cexpr_list) == 3)
6921 expr.value =
6922 c_build_vec_perm_expr
6923 (loc, (*cexpr_list)[0].value,
6924 (*cexpr_list)[1].value,
6925 (*cexpr_list)[2].value);
6926 else
6928 error_at (loc, "wrong number of arguments to "
6929 "%<__builtin_shuffle%>");
6930 expr.value = error_mark_node;
6932 break;
6934 case RID_AT_SELECTOR:
6935 gcc_assert (c_dialect_objc ());
6936 c_parser_consume_token (parser);
6937 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6939 expr.value = error_mark_node;
6940 break;
6943 tree sel = c_parser_objc_selector_arg (parser);
6944 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6945 "expected %<)%>");
6946 expr.value = objc_build_selector_expr (loc, sel);
6948 break;
6949 case RID_AT_PROTOCOL:
6950 gcc_assert (c_dialect_objc ());
6951 c_parser_consume_token (parser);
6952 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6954 expr.value = error_mark_node;
6955 break;
6957 if (c_parser_next_token_is_not (parser, CPP_NAME))
6959 c_parser_error (parser, "expected identifier");
6960 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6961 expr.value = error_mark_node;
6962 break;
6965 tree id = c_parser_peek_token (parser)->value;
6966 c_parser_consume_token (parser);
6967 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6968 "expected %<)%>");
6969 expr.value = objc_build_protocol_expr (id);
6971 break;
6972 case RID_AT_ENCODE:
6973 /* Extension to support C-structures in the archiver. */
6974 gcc_assert (c_dialect_objc ());
6975 c_parser_consume_token (parser);
6976 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6978 expr.value = error_mark_node;
6979 break;
6981 t1 = c_parser_type_name (parser);
6982 if (t1 == NULL)
6984 expr.value = error_mark_node;
6985 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6986 break;
6988 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6989 "expected %<)%>");
6991 tree type = groktypename (t1, NULL, NULL);
6992 expr.value = objc_build_encode_expr (type);
6994 break;
6995 default:
6996 c_parser_error (parser, "expected expression");
6997 expr.value = error_mark_node;
6998 break;
7000 break;
7001 case CPP_OPEN_SQUARE:
7002 if (c_dialect_objc ())
7004 tree receiver, args;
7005 c_parser_consume_token (parser);
7006 receiver = c_parser_objc_receiver (parser);
7007 args = c_parser_objc_message_args (parser);
7008 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
7009 "expected %<]%>");
7010 expr.value = objc_build_message_expr (receiver, args);
7011 break;
7013 /* Else fall through to report error. */
7014 default:
7015 c_parser_error (parser, "expected expression");
7016 expr.value = error_mark_node;
7017 break;
7019 return c_parser_postfix_expression_after_primary (parser, loc, expr);
7022 /* Parse a postfix expression after a parenthesized type name: the
7023 brace-enclosed initializer of a compound literal, possibly followed
7024 by some postfix operators. This is separate because it is not
7025 possible to tell until after the type name whether a cast
7026 expression has a cast or a compound literal, or whether the operand
7027 of sizeof is a parenthesized type name or starts with a compound
7028 literal. TYPE_LOC is the location where TYPE_NAME starts--the
7029 location of the first token after the parentheses around the type
7030 name. */
7032 static struct c_expr
7033 c_parser_postfix_expression_after_paren_type (c_parser *parser,
7034 struct c_type_name *type_name,
7035 location_t type_loc)
7037 tree type;
7038 struct c_expr init;
7039 bool non_const;
7040 struct c_expr expr;
7041 location_t start_loc;
7042 tree type_expr = NULL_TREE;
7043 bool type_expr_const = true;
7044 check_compound_literal_type (type_loc, type_name);
7045 start_init (NULL_TREE, NULL, 0);
7046 type = groktypename (type_name, &type_expr, &type_expr_const);
7047 start_loc = c_parser_peek_token (parser)->location;
7048 if (type != error_mark_node && C_TYPE_VARIABLE_SIZE (type))
7050 error_at (type_loc, "compound literal has variable size");
7051 type = error_mark_node;
7053 init = c_parser_braced_init (parser, type, false);
7054 finish_init ();
7055 maybe_warn_string_init (type, init);
7057 if (type != error_mark_node
7058 && !ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (type))
7059 && current_function_decl)
7061 error ("compound literal qualified by address-space qualifier");
7062 type = error_mark_node;
7065 if (!flag_isoc99)
7066 pedwarn (start_loc, OPT_Wpedantic, "ISO C90 forbids compound literals");
7067 non_const = ((init.value && TREE_CODE (init.value) == CONSTRUCTOR)
7068 ? CONSTRUCTOR_NON_CONST (init.value)
7069 : init.original_code == C_MAYBE_CONST_EXPR);
7070 non_const |= !type_expr_const;
7071 expr.value = build_compound_literal (start_loc, type, init.value, non_const);
7072 expr.original_code = ERROR_MARK;
7073 expr.original_type = NULL;
7074 if (type_expr)
7076 if (TREE_CODE (expr.value) == C_MAYBE_CONST_EXPR)
7078 gcc_assert (C_MAYBE_CONST_EXPR_PRE (expr.value) == NULL_TREE);
7079 C_MAYBE_CONST_EXPR_PRE (expr.value) = type_expr;
7081 else
7083 gcc_assert (!non_const);
7084 expr.value = build2 (C_MAYBE_CONST_EXPR, type,
7085 type_expr, expr.value);
7088 return c_parser_postfix_expression_after_primary (parser, start_loc, expr);
7091 /* Callback function for sizeof_pointer_memaccess_warning to compare
7092 types. */
7094 static bool
7095 sizeof_ptr_memacc_comptypes (tree type1, tree type2)
7097 return comptypes (type1, type2) == 1;
7100 /* Parse a postfix expression after the initial primary or compound
7101 literal; that is, parse a series of postfix operators.
7103 EXPR_LOC is the location of the primary expression. */
7105 static struct c_expr
7106 c_parser_postfix_expression_after_primary (c_parser *parser,
7107 location_t expr_loc,
7108 struct c_expr expr)
7110 struct c_expr orig_expr;
7111 tree ident, idx;
7112 location_t sizeof_arg_loc[3];
7113 tree sizeof_arg[3];
7114 unsigned int i;
7115 vec<tree, va_gc> *exprlist;
7116 vec<tree, va_gc> *origtypes;
7117 while (true)
7119 location_t op_loc = c_parser_peek_token (parser)->location;
7120 switch (c_parser_peek_token (parser)->type)
7122 case CPP_OPEN_SQUARE:
7123 /* Array reference. */
7124 c_parser_consume_token (parser);
7125 idx = c_parser_expression (parser).value;
7126 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
7127 "expected %<]%>");
7128 expr.value = build_array_ref (op_loc, expr.value, idx);
7129 expr.original_code = ERROR_MARK;
7130 expr.original_type = NULL;
7131 break;
7132 case CPP_OPEN_PAREN:
7133 /* Function call. */
7134 c_parser_consume_token (parser);
7135 for (i = 0; i < 3; i++)
7137 sizeof_arg[i] = NULL_TREE;
7138 sizeof_arg_loc[i] = UNKNOWN_LOCATION;
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_loc, 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_pointer_memaccess_warning (sizeof_arg_loc,
7151 expr.value, exprlist,
7152 sizeof_arg,
7153 sizeof_ptr_memacc_comptypes);
7154 /* FIXME diagnostics: Ideally we want the FUNCNAME, not the
7155 "(" after the FUNCNAME, which is what we have now. */
7156 expr.value = build_function_call_vec (op_loc, expr.value, exprlist,
7157 origtypes);
7158 expr.original_code = ERROR_MARK;
7159 if (TREE_CODE (expr.value) == INTEGER_CST
7160 && TREE_CODE (orig_expr.value) == FUNCTION_DECL
7161 && DECL_BUILT_IN_CLASS (orig_expr.value) == BUILT_IN_NORMAL
7162 && DECL_FUNCTION_CODE (orig_expr.value) == BUILT_IN_CONSTANT_P)
7163 expr.original_code = C_MAYBE_CONST_EXPR;
7164 expr.original_type = NULL;
7165 if (exprlist)
7167 release_tree_vector (exprlist);
7168 release_tree_vector (origtypes);
7170 break;
7171 case CPP_DOT:
7172 /* Structure element reference. */
7173 c_parser_consume_token (parser);
7174 expr = default_function_array_conversion (expr_loc, expr);
7175 if (c_parser_next_token_is (parser, CPP_NAME))
7176 ident = c_parser_peek_token (parser)->value;
7177 else
7179 c_parser_error (parser, "expected identifier");
7180 expr.value = error_mark_node;
7181 expr.original_code = ERROR_MARK;
7182 expr.original_type = NULL;
7183 return expr;
7185 c_parser_consume_token (parser);
7186 expr.value = build_component_ref (op_loc, expr.value, ident);
7187 expr.original_code = ERROR_MARK;
7188 if (TREE_CODE (expr.value) != COMPONENT_REF)
7189 expr.original_type = NULL;
7190 else
7192 /* Remember the original type of a bitfield. */
7193 tree field = TREE_OPERAND (expr.value, 1);
7194 if (TREE_CODE (field) != FIELD_DECL)
7195 expr.original_type = NULL;
7196 else
7197 expr.original_type = DECL_BIT_FIELD_TYPE (field);
7199 break;
7200 case CPP_DEREF:
7201 /* Structure element reference. */
7202 c_parser_consume_token (parser);
7203 expr = default_function_array_conversion (expr_loc, expr);
7204 if (c_parser_next_token_is (parser, CPP_NAME))
7205 ident = c_parser_peek_token (parser)->value;
7206 else
7208 c_parser_error (parser, "expected identifier");
7209 expr.value = error_mark_node;
7210 expr.original_code = ERROR_MARK;
7211 expr.original_type = NULL;
7212 return expr;
7214 c_parser_consume_token (parser);
7215 expr.value = build_component_ref (op_loc,
7216 build_indirect_ref (op_loc,
7217 expr.value,
7218 RO_ARROW),
7219 ident);
7220 expr.original_code = ERROR_MARK;
7221 if (TREE_CODE (expr.value) != COMPONENT_REF)
7222 expr.original_type = NULL;
7223 else
7225 /* Remember the original type of a bitfield. */
7226 tree field = TREE_OPERAND (expr.value, 1);
7227 if (TREE_CODE (field) != FIELD_DECL)
7228 expr.original_type = NULL;
7229 else
7230 expr.original_type = DECL_BIT_FIELD_TYPE (field);
7232 break;
7233 case CPP_PLUS_PLUS:
7234 /* Postincrement. */
7235 c_parser_consume_token (parser);
7236 expr = default_function_array_read_conversion (expr_loc, expr);
7237 expr.value = build_unary_op (op_loc,
7238 POSTINCREMENT_EXPR, expr.value, 0);
7239 expr.original_code = ERROR_MARK;
7240 expr.original_type = NULL;
7241 break;
7242 case CPP_MINUS_MINUS:
7243 /* Postdecrement. */
7244 c_parser_consume_token (parser);
7245 expr = default_function_array_read_conversion (expr_loc, expr);
7246 expr.value = build_unary_op (op_loc,
7247 POSTDECREMENT_EXPR, expr.value, 0);
7248 expr.original_code = ERROR_MARK;
7249 expr.original_type = NULL;
7250 break;
7251 default:
7252 return expr;
7257 /* Parse an expression (C90 6.3.17, C99 6.5.17).
7259 expression:
7260 assignment-expression
7261 expression , assignment-expression
7264 static struct c_expr
7265 c_parser_expression (c_parser *parser)
7267 struct c_expr expr;
7268 expr = c_parser_expr_no_commas (parser, NULL);
7269 while (c_parser_next_token_is (parser, CPP_COMMA))
7271 struct c_expr next;
7272 tree lhsval;
7273 location_t loc = c_parser_peek_token (parser)->location;
7274 location_t expr_loc;
7275 c_parser_consume_token (parser);
7276 expr_loc = c_parser_peek_token (parser)->location;
7277 lhsval = expr.value;
7278 while (TREE_CODE (lhsval) == COMPOUND_EXPR)
7279 lhsval = TREE_OPERAND (lhsval, 1);
7280 if (DECL_P (lhsval) || handled_component_p (lhsval))
7281 mark_exp_read (lhsval);
7282 next = c_parser_expr_no_commas (parser, NULL);
7283 next = default_function_array_conversion (expr_loc, next);
7284 expr.value = build_compound_expr (loc, expr.value, next.value);
7285 expr.original_code = COMPOUND_EXPR;
7286 expr.original_type = next.original_type;
7288 return expr;
7291 /* Parse an expression and convert functions or arrays to
7292 pointers. */
7294 static struct c_expr
7295 c_parser_expression_conv (c_parser *parser)
7297 struct c_expr expr;
7298 location_t loc = c_parser_peek_token (parser)->location;
7299 expr = c_parser_expression (parser);
7300 expr = default_function_array_conversion (loc, expr);
7301 return expr;
7304 /* Parse a non-empty list of expressions. If CONVERT_P, convert
7305 functions and arrays to pointers. If FOLD_P, fold the expressions.
7307 nonempty-expr-list:
7308 assignment-expression
7309 nonempty-expr-list , assignment-expression
7312 static vec<tree, va_gc> *
7313 c_parser_expr_list (c_parser *parser, bool convert_p, bool fold_p,
7314 vec<tree, va_gc> **p_orig_types,
7315 location_t *sizeof_arg_loc, tree *sizeof_arg)
7317 vec<tree, va_gc> *ret;
7318 vec<tree, va_gc> *orig_types;
7319 struct c_expr expr;
7320 location_t loc = c_parser_peek_token (parser)->location;
7321 location_t cur_sizeof_arg_loc = UNKNOWN_LOCATION;
7322 unsigned int idx = 0;
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 cur_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 ret->quick_push (expr.value);
7339 if (orig_types)
7340 orig_types->quick_push (expr.original_type);
7341 if (sizeof_arg != NULL
7342 && cur_sizeof_arg_loc != UNKNOWN_LOCATION
7343 && expr.original_code == SIZEOF_EXPR)
7345 sizeof_arg[0] = c_last_sizeof_arg;
7346 sizeof_arg_loc[0] = cur_sizeof_arg_loc;
7348 while (c_parser_next_token_is (parser, CPP_COMMA))
7350 c_parser_consume_token (parser);
7351 loc = c_parser_peek_token (parser)->location;
7352 if (sizeof_arg != NULL
7353 && c_parser_next_token_is_keyword (parser, RID_SIZEOF))
7354 cur_sizeof_arg_loc = c_parser_peek_2nd_token (parser)->location;
7355 else
7356 cur_sizeof_arg_loc = UNKNOWN_LOCATION;
7357 expr = c_parser_expr_no_commas (parser, NULL);
7358 if (convert_p)
7359 expr = default_function_array_read_conversion (loc, expr);
7360 if (fold_p)
7361 expr.value = c_fully_fold (expr.value, false, NULL);
7362 vec_safe_push (ret, expr.value);
7363 if (orig_types)
7364 vec_safe_push (orig_types, expr.original_type);
7365 if (++idx < 3
7366 && sizeof_arg != NULL
7367 && cur_sizeof_arg_loc != UNKNOWN_LOCATION
7368 && expr.original_code == SIZEOF_EXPR)
7370 sizeof_arg[idx] = c_last_sizeof_arg;
7371 sizeof_arg_loc[idx] = cur_sizeof_arg_loc;
7374 if (orig_types)
7375 *p_orig_types = orig_types;
7376 return ret;
7379 /* Parse Objective-C-specific constructs. */
7381 /* Parse an objc-class-definition.
7383 objc-class-definition:
7384 @interface identifier objc-superclass[opt] objc-protocol-refs[opt]
7385 objc-class-instance-variables[opt] objc-methodprotolist @end
7386 @implementation identifier objc-superclass[opt]
7387 objc-class-instance-variables[opt]
7388 @interface identifier ( identifier ) objc-protocol-refs[opt]
7389 objc-methodprotolist @end
7390 @interface identifier ( ) objc-protocol-refs[opt]
7391 objc-methodprotolist @end
7392 @implementation identifier ( identifier )
7394 objc-superclass:
7395 : identifier
7397 "@interface identifier (" must start "@interface identifier (
7398 identifier ) ...": objc-methodprotolist in the first production may
7399 not start with a parenthesized identifier as a declarator of a data
7400 definition with no declaration specifiers if the objc-superclass,
7401 objc-protocol-refs and objc-class-instance-variables are omitted. */
7403 static void
7404 c_parser_objc_class_definition (c_parser *parser, tree attributes)
7406 bool iface_p;
7407 tree id1;
7408 tree superclass;
7409 if (c_parser_next_token_is_keyword (parser, RID_AT_INTERFACE))
7410 iface_p = true;
7411 else if (c_parser_next_token_is_keyword (parser, RID_AT_IMPLEMENTATION))
7412 iface_p = false;
7413 else
7414 gcc_unreachable ();
7416 c_parser_consume_token (parser);
7417 if (c_parser_next_token_is_not (parser, CPP_NAME))
7419 c_parser_error (parser, "expected identifier");
7420 return;
7422 id1 = c_parser_peek_token (parser)->value;
7423 c_parser_consume_token (parser);
7424 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
7426 /* We have a category or class extension. */
7427 tree id2;
7428 tree proto = NULL_TREE;
7429 c_parser_consume_token (parser);
7430 if (c_parser_next_token_is_not (parser, CPP_NAME))
7432 if (iface_p && c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
7434 /* We have a class extension. */
7435 id2 = NULL_TREE;
7437 else
7439 c_parser_error (parser, "expected identifier or %<)%>");
7440 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7441 return;
7444 else
7446 id2 = c_parser_peek_token (parser)->value;
7447 c_parser_consume_token (parser);
7449 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
7450 if (!iface_p)
7452 objc_start_category_implementation (id1, id2);
7453 return;
7455 if (c_parser_next_token_is (parser, CPP_LESS))
7456 proto = c_parser_objc_protocol_refs (parser);
7457 objc_start_category_interface (id1, id2, proto, attributes);
7458 c_parser_objc_methodprotolist (parser);
7459 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
7460 objc_finish_interface ();
7461 return;
7463 if (c_parser_next_token_is (parser, CPP_COLON))
7465 c_parser_consume_token (parser);
7466 if (c_parser_next_token_is_not (parser, CPP_NAME))
7468 c_parser_error (parser, "expected identifier");
7469 return;
7471 superclass = c_parser_peek_token (parser)->value;
7472 c_parser_consume_token (parser);
7474 else
7475 superclass = NULL_TREE;
7476 if (iface_p)
7478 tree proto = NULL_TREE;
7479 if (c_parser_next_token_is (parser, CPP_LESS))
7480 proto = c_parser_objc_protocol_refs (parser);
7481 objc_start_class_interface (id1, superclass, proto, attributes);
7483 else
7484 objc_start_class_implementation (id1, superclass);
7485 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
7486 c_parser_objc_class_instance_variables (parser);
7487 if (iface_p)
7489 objc_continue_interface ();
7490 c_parser_objc_methodprotolist (parser);
7491 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
7492 objc_finish_interface ();
7494 else
7496 objc_continue_implementation ();
7497 return;
7501 /* Parse objc-class-instance-variables.
7503 objc-class-instance-variables:
7504 { objc-instance-variable-decl-list[opt] }
7506 objc-instance-variable-decl-list:
7507 objc-visibility-spec
7508 objc-instance-variable-decl ;
7510 objc-instance-variable-decl-list objc-visibility-spec
7511 objc-instance-variable-decl-list objc-instance-variable-decl ;
7512 objc-instance-variable-decl-list ;
7514 objc-visibility-spec:
7515 @private
7516 @protected
7517 @public
7519 objc-instance-variable-decl:
7520 struct-declaration
7523 static void
7524 c_parser_objc_class_instance_variables (c_parser *parser)
7526 gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
7527 c_parser_consume_token (parser);
7528 while (c_parser_next_token_is_not (parser, CPP_EOF))
7530 tree decls;
7531 /* Parse any stray semicolon. */
7532 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
7534 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
7535 "extra semicolon");
7536 c_parser_consume_token (parser);
7537 continue;
7539 /* Stop if at the end of the instance variables. */
7540 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
7542 c_parser_consume_token (parser);
7543 break;
7545 /* Parse any objc-visibility-spec. */
7546 if (c_parser_next_token_is_keyword (parser, RID_AT_PRIVATE))
7548 c_parser_consume_token (parser);
7549 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
7550 continue;
7552 else if (c_parser_next_token_is_keyword (parser, RID_AT_PROTECTED))
7554 c_parser_consume_token (parser);
7555 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
7556 continue;
7558 else if (c_parser_next_token_is_keyword (parser, RID_AT_PUBLIC))
7560 c_parser_consume_token (parser);
7561 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
7562 continue;
7564 else if (c_parser_next_token_is_keyword (parser, RID_AT_PACKAGE))
7566 c_parser_consume_token (parser);
7567 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
7568 continue;
7570 else if (c_parser_next_token_is (parser, CPP_PRAGMA))
7572 c_parser_pragma (parser, pragma_external);
7573 continue;
7576 /* Parse some comma-separated declarations. */
7577 decls = c_parser_struct_declaration (parser);
7578 if (decls == NULL)
7580 /* There is a syntax error. We want to skip the offending
7581 tokens up to the next ';' (included) or '}'
7582 (excluded). */
7584 /* First, skip manually a ')' or ']'. This is because they
7585 reduce the nesting level, so c_parser_skip_until_found()
7586 wouldn't be able to skip past them. */
7587 c_token *token = c_parser_peek_token (parser);
7588 if (token->type == CPP_CLOSE_PAREN || token->type == CPP_CLOSE_SQUARE)
7589 c_parser_consume_token (parser);
7591 /* Then, do the standard skipping. */
7592 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
7594 /* We hopefully recovered. Start normal parsing again. */
7595 parser->error = false;
7596 continue;
7598 else
7600 /* Comma-separated instance variables are chained together
7601 in reverse order; add them one by one. */
7602 tree ivar = nreverse (decls);
7603 for (; ivar; ivar = DECL_CHAIN (ivar))
7604 objc_add_instance_variable (copy_node (ivar));
7606 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
7610 /* Parse an objc-class-declaration.
7612 objc-class-declaration:
7613 @class identifier-list ;
7616 static void
7617 c_parser_objc_class_declaration (c_parser *parser)
7619 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_CLASS));
7620 c_parser_consume_token (parser);
7621 /* Any identifiers, including those declared as type names, are OK
7622 here. */
7623 while (true)
7625 tree id;
7626 if (c_parser_next_token_is_not (parser, CPP_NAME))
7628 c_parser_error (parser, "expected identifier");
7629 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
7630 parser->error = false;
7631 return;
7633 id = c_parser_peek_token (parser)->value;
7634 objc_declare_class (id);
7635 c_parser_consume_token (parser);
7636 if (c_parser_next_token_is (parser, CPP_COMMA))
7637 c_parser_consume_token (parser);
7638 else
7639 break;
7641 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
7644 /* Parse an objc-alias-declaration.
7646 objc-alias-declaration:
7647 @compatibility_alias identifier identifier ;
7650 static void
7651 c_parser_objc_alias_declaration (c_parser *parser)
7653 tree id1, id2;
7654 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_ALIAS));
7655 c_parser_consume_token (parser);
7656 if (c_parser_next_token_is_not (parser, CPP_NAME))
7658 c_parser_error (parser, "expected identifier");
7659 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
7660 return;
7662 id1 = c_parser_peek_token (parser)->value;
7663 c_parser_consume_token (parser);
7664 if (c_parser_next_token_is_not (parser, CPP_NAME))
7666 c_parser_error (parser, "expected identifier");
7667 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
7668 return;
7670 id2 = c_parser_peek_token (parser)->value;
7671 c_parser_consume_token (parser);
7672 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
7673 objc_declare_alias (id1, id2);
7676 /* Parse an objc-protocol-definition.
7678 objc-protocol-definition:
7679 @protocol identifier objc-protocol-refs[opt] objc-methodprotolist @end
7680 @protocol identifier-list ;
7682 "@protocol identifier ;" should be resolved as "@protocol
7683 identifier-list ;": objc-methodprotolist may not start with a
7684 semicolon in the first alternative if objc-protocol-refs are
7685 omitted. */
7687 static void
7688 c_parser_objc_protocol_definition (c_parser *parser, tree attributes)
7690 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_PROTOCOL));
7692 c_parser_consume_token (parser);
7693 if (c_parser_next_token_is_not (parser, CPP_NAME))
7695 c_parser_error (parser, "expected identifier");
7696 return;
7698 if (c_parser_peek_2nd_token (parser)->type == CPP_COMMA
7699 || c_parser_peek_2nd_token (parser)->type == CPP_SEMICOLON)
7701 /* Any identifiers, including those declared as type names, are
7702 OK here. */
7703 while (true)
7705 tree id;
7706 if (c_parser_next_token_is_not (parser, CPP_NAME))
7708 c_parser_error (parser, "expected identifier");
7709 break;
7711 id = c_parser_peek_token (parser)->value;
7712 objc_declare_protocol (id, attributes);
7713 c_parser_consume_token (parser);
7714 if (c_parser_next_token_is (parser, CPP_COMMA))
7715 c_parser_consume_token (parser);
7716 else
7717 break;
7719 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
7721 else
7723 tree id = c_parser_peek_token (parser)->value;
7724 tree proto = NULL_TREE;
7725 c_parser_consume_token (parser);
7726 if (c_parser_next_token_is (parser, CPP_LESS))
7727 proto = c_parser_objc_protocol_refs (parser);
7728 parser->objc_pq_context = true;
7729 objc_start_protocol (id, proto, attributes);
7730 c_parser_objc_methodprotolist (parser);
7731 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
7732 parser->objc_pq_context = false;
7733 objc_finish_interface ();
7737 /* Parse an objc-method-type.
7739 objc-method-type:
7743 Return true if it is a class method (+) and false if it is
7744 an instance method (-).
7746 static inline bool
7747 c_parser_objc_method_type (c_parser *parser)
7749 switch (c_parser_peek_token (parser)->type)
7751 case CPP_PLUS:
7752 c_parser_consume_token (parser);
7753 return true;
7754 case CPP_MINUS:
7755 c_parser_consume_token (parser);
7756 return false;
7757 default:
7758 gcc_unreachable ();
7762 /* Parse an objc-method-definition.
7764 objc-method-definition:
7765 objc-method-type objc-method-decl ;[opt] compound-statement
7768 static void
7769 c_parser_objc_method_definition (c_parser *parser)
7771 bool is_class_method = c_parser_objc_method_type (parser);
7772 tree decl, attributes = NULL_TREE, expr = NULL_TREE;
7773 parser->objc_pq_context = true;
7774 decl = c_parser_objc_method_decl (parser, is_class_method, &attributes,
7775 &expr);
7776 if (decl == error_mark_node)
7777 return; /* Bail here. */
7779 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
7781 c_parser_consume_token (parser);
7782 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
7783 "extra semicolon in method definition specified");
7786 if (!c_parser_next_token_is (parser, CPP_OPEN_BRACE))
7788 c_parser_error (parser, "expected %<{%>");
7789 return;
7792 parser->objc_pq_context = false;
7793 if (objc_start_method_definition (is_class_method, decl, attributes, expr))
7795 add_stmt (c_parser_compound_statement (parser));
7796 objc_finish_method_definition (current_function_decl);
7798 else
7800 /* This code is executed when we find a method definition
7801 outside of an @implementation context (or invalid for other
7802 reasons). Parse the method (to keep going) but do not emit
7803 any code.
7805 c_parser_compound_statement (parser);
7809 /* Parse an objc-methodprotolist.
7811 objc-methodprotolist:
7812 empty
7813 objc-methodprotolist objc-methodproto
7814 objc-methodprotolist declaration
7815 objc-methodprotolist ;
7816 @optional
7817 @required
7819 The declaration is a data definition, which may be missing
7820 declaration specifiers under the same rules and diagnostics as
7821 other data definitions outside functions, and the stray semicolon
7822 is diagnosed the same way as a stray semicolon outside a
7823 function. */
7825 static void
7826 c_parser_objc_methodprotolist (c_parser *parser)
7828 while (true)
7830 /* The list is terminated by @end. */
7831 switch (c_parser_peek_token (parser)->type)
7833 case CPP_SEMICOLON:
7834 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
7835 "ISO C does not allow extra %<;%> outside of a function");
7836 c_parser_consume_token (parser);
7837 break;
7838 case CPP_PLUS:
7839 case CPP_MINUS:
7840 c_parser_objc_methodproto (parser);
7841 break;
7842 case CPP_PRAGMA:
7843 c_parser_pragma (parser, pragma_external);
7844 break;
7845 case CPP_EOF:
7846 return;
7847 default:
7848 if (c_parser_next_token_is_keyword (parser, RID_AT_END))
7849 return;
7850 else if (c_parser_next_token_is_keyword (parser, RID_AT_PROPERTY))
7851 c_parser_objc_at_property_declaration (parser);
7852 else if (c_parser_next_token_is_keyword (parser, RID_AT_OPTIONAL))
7854 objc_set_method_opt (true);
7855 c_parser_consume_token (parser);
7857 else if (c_parser_next_token_is_keyword (parser, RID_AT_REQUIRED))
7859 objc_set_method_opt (false);
7860 c_parser_consume_token (parser);
7862 else
7863 c_parser_declaration_or_fndef (parser, false, false, true,
7864 false, true, NULL);
7865 break;
7870 /* Parse an objc-methodproto.
7872 objc-methodproto:
7873 objc-method-type objc-method-decl ;
7876 static void
7877 c_parser_objc_methodproto (c_parser *parser)
7879 bool is_class_method = c_parser_objc_method_type (parser);
7880 tree decl, attributes = NULL_TREE;
7882 /* Remember protocol qualifiers in prototypes. */
7883 parser->objc_pq_context = true;
7884 decl = c_parser_objc_method_decl (parser, is_class_method, &attributes,
7885 NULL);
7886 /* Forget protocol qualifiers now. */
7887 parser->objc_pq_context = false;
7889 /* Do not allow the presence of attributes to hide an erroneous
7890 method implementation in the interface section. */
7891 if (!c_parser_next_token_is (parser, CPP_SEMICOLON))
7893 c_parser_error (parser, "expected %<;%>");
7894 return;
7897 if (decl != error_mark_node)
7898 objc_add_method_declaration (is_class_method, decl, attributes);
7900 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
7903 /* If we are at a position that method attributes may be present, check that
7904 there are not any parsed already (a syntax error) and then collect any
7905 specified at the current location. Finally, if new attributes were present,
7906 check that the next token is legal ( ';' for decls and '{' for defs). */
7908 static bool
7909 c_parser_objc_maybe_method_attributes (c_parser* parser, tree* attributes)
7911 bool bad = false;
7912 if (*attributes)
7914 c_parser_error (parser,
7915 "method attributes must be specified at the end only");
7916 *attributes = NULL_TREE;
7917 bad = true;
7920 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
7921 *attributes = c_parser_attributes (parser);
7923 /* If there were no attributes here, just report any earlier error. */
7924 if (*attributes == NULL_TREE || bad)
7925 return bad;
7927 /* If the attributes are followed by a ; or {, then just report any earlier
7928 error. */
7929 if (c_parser_next_token_is (parser, CPP_SEMICOLON)
7930 || c_parser_next_token_is (parser, CPP_OPEN_BRACE))
7931 return bad;
7933 /* We've got attributes, but not at the end. */
7934 c_parser_error (parser,
7935 "expected %<;%> or %<{%> after method attribute definition");
7936 return true;
7939 /* Parse an objc-method-decl.
7941 objc-method-decl:
7942 ( objc-type-name ) objc-selector
7943 objc-selector
7944 ( objc-type-name ) objc-keyword-selector objc-optparmlist
7945 objc-keyword-selector objc-optparmlist
7946 attributes
7948 objc-keyword-selector:
7949 objc-keyword-decl
7950 objc-keyword-selector objc-keyword-decl
7952 objc-keyword-decl:
7953 objc-selector : ( objc-type-name ) identifier
7954 objc-selector : identifier
7955 : ( objc-type-name ) identifier
7956 : identifier
7958 objc-optparmlist:
7959 objc-optparms objc-optellipsis
7961 objc-optparms:
7962 empty
7963 objc-opt-parms , parameter-declaration
7965 objc-optellipsis:
7966 empty
7967 , ...
7970 static tree
7971 c_parser_objc_method_decl (c_parser *parser, bool is_class_method,
7972 tree *attributes, tree *expr)
7974 tree type = NULL_TREE;
7975 tree sel;
7976 tree parms = NULL_TREE;
7977 bool ellipsis = false;
7978 bool attr_err = false;
7980 *attributes = NULL_TREE;
7981 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
7983 c_parser_consume_token (parser);
7984 type = c_parser_objc_type_name (parser);
7985 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
7987 sel = c_parser_objc_selector (parser);
7988 /* If there is no selector, or a colon follows, we have an
7989 objc-keyword-selector. If there is a selector, and a colon does
7990 not follow, that selector ends the objc-method-decl. */
7991 if (!sel || c_parser_next_token_is (parser, CPP_COLON))
7993 tree tsel = sel;
7994 tree list = NULL_TREE;
7995 while (true)
7997 tree atype = NULL_TREE, id, keyworddecl;
7998 tree param_attr = NULL_TREE;
7999 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
8000 break;
8001 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
8003 c_parser_consume_token (parser);
8004 atype = c_parser_objc_type_name (parser);
8005 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
8006 "expected %<)%>");
8008 /* New ObjC allows attributes on method parameters. */
8009 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
8010 param_attr = c_parser_attributes (parser);
8011 if (c_parser_next_token_is_not (parser, CPP_NAME))
8013 c_parser_error (parser, "expected identifier");
8014 return error_mark_node;
8016 id = c_parser_peek_token (parser)->value;
8017 c_parser_consume_token (parser);
8018 keyworddecl = objc_build_keyword_decl (tsel, atype, id, param_attr);
8019 list = chainon (list, keyworddecl);
8020 tsel = c_parser_objc_selector (parser);
8021 if (!tsel && c_parser_next_token_is_not (parser, CPP_COLON))
8022 break;
8025 attr_err |= c_parser_objc_maybe_method_attributes (parser, attributes) ;
8027 /* Parse the optional parameter list. Optional Objective-C
8028 method parameters follow the C syntax, and may include '...'
8029 to denote a variable number of arguments. */
8030 parms = make_node (TREE_LIST);
8031 while (c_parser_next_token_is (parser, CPP_COMMA))
8033 struct c_parm *parm;
8034 c_parser_consume_token (parser);
8035 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
8037 ellipsis = true;
8038 c_parser_consume_token (parser);
8039 attr_err |= c_parser_objc_maybe_method_attributes
8040 (parser, attributes) ;
8041 break;
8043 parm = c_parser_parameter_declaration (parser, NULL_TREE);
8044 if (parm == NULL)
8045 break;
8046 parms = chainon (parms,
8047 build_tree_list (NULL_TREE, grokparm (parm, expr)));
8049 sel = list;
8051 else
8052 attr_err |= c_parser_objc_maybe_method_attributes (parser, attributes) ;
8054 if (sel == NULL)
8056 c_parser_error (parser, "objective-c method declaration is expected");
8057 return error_mark_node;
8060 if (attr_err)
8061 return error_mark_node;
8063 return objc_build_method_signature (is_class_method, type, sel, parms, ellipsis);
8066 /* Parse an objc-type-name.
8068 objc-type-name:
8069 objc-type-qualifiers[opt] type-name
8070 objc-type-qualifiers[opt]
8072 objc-type-qualifiers:
8073 objc-type-qualifier
8074 objc-type-qualifiers objc-type-qualifier
8076 objc-type-qualifier: one of
8077 in out inout bycopy byref oneway
8080 static tree
8081 c_parser_objc_type_name (c_parser *parser)
8083 tree quals = NULL_TREE;
8084 struct c_type_name *type_name = NULL;
8085 tree type = NULL_TREE;
8086 while (true)
8088 c_token *token = c_parser_peek_token (parser);
8089 if (token->type == CPP_KEYWORD
8090 && (token->keyword == RID_IN
8091 || token->keyword == RID_OUT
8092 || token->keyword == RID_INOUT
8093 || token->keyword == RID_BYCOPY
8094 || token->keyword == RID_BYREF
8095 || token->keyword == RID_ONEWAY))
8097 quals = chainon (build_tree_list (NULL_TREE, token->value), quals);
8098 c_parser_consume_token (parser);
8100 else
8101 break;
8103 if (c_parser_next_tokens_start_typename (parser, cla_prefer_type))
8104 type_name = c_parser_type_name (parser);
8105 if (type_name)
8106 type = groktypename (type_name, NULL, NULL);
8108 /* If the type is unknown, and error has already been produced and
8109 we need to recover from the error. In that case, use NULL_TREE
8110 for the type, as if no type had been specified; this will use the
8111 default type ('id') which is good for error recovery. */
8112 if (type == error_mark_node)
8113 type = NULL_TREE;
8115 return build_tree_list (quals, type);
8118 /* Parse objc-protocol-refs.
8120 objc-protocol-refs:
8121 < identifier-list >
8124 static tree
8125 c_parser_objc_protocol_refs (c_parser *parser)
8127 tree list = NULL_TREE;
8128 gcc_assert (c_parser_next_token_is (parser, CPP_LESS));
8129 c_parser_consume_token (parser);
8130 /* Any identifiers, including those declared as type names, are OK
8131 here. */
8132 while (true)
8134 tree id;
8135 if (c_parser_next_token_is_not (parser, CPP_NAME))
8137 c_parser_error (parser, "expected identifier");
8138 break;
8140 id = c_parser_peek_token (parser)->value;
8141 list = chainon (list, build_tree_list (NULL_TREE, id));
8142 c_parser_consume_token (parser);
8143 if (c_parser_next_token_is (parser, CPP_COMMA))
8144 c_parser_consume_token (parser);
8145 else
8146 break;
8148 c_parser_require (parser, CPP_GREATER, "expected %<>%>");
8149 return list;
8152 /* Parse an objc-try-catch-finally-statement.
8154 objc-try-catch-finally-statement:
8155 @try compound-statement objc-catch-list[opt]
8156 @try compound-statement objc-catch-list[opt] @finally compound-statement
8158 objc-catch-list:
8159 @catch ( objc-catch-parameter-declaration ) compound-statement
8160 objc-catch-list @catch ( objc-catch-parameter-declaration ) compound-statement
8162 objc-catch-parameter-declaration:
8163 parameter-declaration
8164 '...'
8166 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
8168 PS: This function is identical to cp_parser_objc_try_catch_finally_statement
8169 for C++. Keep them in sync. */
8171 static void
8172 c_parser_objc_try_catch_finally_statement (c_parser *parser)
8174 location_t location;
8175 tree stmt;
8177 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_TRY));
8178 c_parser_consume_token (parser);
8179 location = c_parser_peek_token (parser)->location;
8180 objc_maybe_warn_exceptions (location);
8181 stmt = c_parser_compound_statement (parser);
8182 objc_begin_try_stmt (location, stmt);
8184 while (c_parser_next_token_is_keyword (parser, RID_AT_CATCH))
8186 struct c_parm *parm;
8187 tree parameter_declaration = error_mark_node;
8188 bool seen_open_paren = false;
8190 c_parser_consume_token (parser);
8191 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
8192 seen_open_paren = true;
8193 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
8195 /* We have "@catch (...)" (where the '...' are literally
8196 what is in the code). Skip the '...'.
8197 parameter_declaration is set to NULL_TREE, and
8198 objc_being_catch_clauses() knows that that means
8199 '...'. */
8200 c_parser_consume_token (parser);
8201 parameter_declaration = NULL_TREE;
8203 else
8205 /* We have "@catch (NSException *exception)" or something
8206 like that. Parse the parameter declaration. */
8207 parm = c_parser_parameter_declaration (parser, NULL_TREE);
8208 if (parm == NULL)
8209 parameter_declaration = error_mark_node;
8210 else
8211 parameter_declaration = grokparm (parm, NULL);
8213 if (seen_open_paren)
8214 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8215 else
8217 /* If there was no open parenthesis, we are recovering from
8218 an error, and we are trying to figure out what mistake
8219 the user has made. */
8221 /* If there is an immediate closing parenthesis, the user
8222 probably forgot the opening one (ie, they typed "@catch
8223 NSException *e)". Parse the closing parenthesis and keep
8224 going. */
8225 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
8226 c_parser_consume_token (parser);
8228 /* If these is no immediate closing parenthesis, the user
8229 probably doesn't know that parenthesis are required at
8230 all (ie, they typed "@catch NSException *e"). So, just
8231 forget about the closing parenthesis and keep going. */
8233 objc_begin_catch_clause (parameter_declaration);
8234 if (c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
8235 c_parser_compound_statement_nostart (parser);
8236 objc_finish_catch_clause ();
8238 if (c_parser_next_token_is_keyword (parser, RID_AT_FINALLY))
8240 c_parser_consume_token (parser);
8241 location = c_parser_peek_token (parser)->location;
8242 stmt = c_parser_compound_statement (parser);
8243 objc_build_finally_clause (location, stmt);
8245 objc_finish_try_stmt ();
8248 /* Parse an objc-synchronized-statement.
8250 objc-synchronized-statement:
8251 @synchronized ( expression ) compound-statement
8254 static void
8255 c_parser_objc_synchronized_statement (c_parser *parser)
8257 location_t loc;
8258 tree expr, stmt;
8259 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_SYNCHRONIZED));
8260 c_parser_consume_token (parser);
8261 loc = c_parser_peek_token (parser)->location;
8262 objc_maybe_warn_exceptions (loc);
8263 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
8265 expr = c_parser_expression (parser).value;
8266 expr = c_fully_fold (expr, false, NULL);
8267 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8269 else
8270 expr = error_mark_node;
8271 stmt = c_parser_compound_statement (parser);
8272 objc_build_synchronized (loc, expr, stmt);
8275 /* Parse an objc-selector; return NULL_TREE without an error if the
8276 next token is not an objc-selector.
8278 objc-selector:
8279 identifier
8280 one of
8281 enum struct union if else while do for switch case default
8282 break continue return goto asm sizeof typeof __alignof
8283 unsigned long const short volatile signed restrict _Complex
8284 in out inout bycopy byref oneway int char float double void _Bool
8286 ??? Why this selection of keywords but not, for example, storage
8287 class specifiers? */
8289 static tree
8290 c_parser_objc_selector (c_parser *parser)
8292 c_token *token = c_parser_peek_token (parser);
8293 tree value = token->value;
8294 if (token->type == CPP_NAME)
8296 c_parser_consume_token (parser);
8297 return value;
8299 if (token->type != CPP_KEYWORD)
8300 return NULL_TREE;
8301 switch (token->keyword)
8303 case RID_ENUM:
8304 case RID_STRUCT:
8305 case RID_UNION:
8306 case RID_IF:
8307 case RID_ELSE:
8308 case RID_WHILE:
8309 case RID_DO:
8310 case RID_FOR:
8311 case RID_SWITCH:
8312 case RID_CASE:
8313 case RID_DEFAULT:
8314 case RID_BREAK:
8315 case RID_CONTINUE:
8316 case RID_RETURN:
8317 case RID_GOTO:
8318 case RID_ASM:
8319 case RID_SIZEOF:
8320 case RID_TYPEOF:
8321 case RID_ALIGNOF:
8322 case RID_UNSIGNED:
8323 case RID_LONG:
8324 case RID_INT128:
8325 case RID_CONST:
8326 case RID_SHORT:
8327 case RID_VOLATILE:
8328 case RID_SIGNED:
8329 case RID_RESTRICT:
8330 case RID_COMPLEX:
8331 case RID_IN:
8332 case RID_OUT:
8333 case RID_INOUT:
8334 case RID_BYCOPY:
8335 case RID_BYREF:
8336 case RID_ONEWAY:
8337 case RID_INT:
8338 case RID_CHAR:
8339 case RID_FLOAT:
8340 case RID_DOUBLE:
8341 case RID_VOID:
8342 case RID_BOOL:
8343 c_parser_consume_token (parser);
8344 return value;
8345 default:
8346 return NULL_TREE;
8350 /* Parse an objc-selector-arg.
8352 objc-selector-arg:
8353 objc-selector
8354 objc-keywordname-list
8356 objc-keywordname-list:
8357 objc-keywordname
8358 objc-keywordname-list objc-keywordname
8360 objc-keywordname:
8361 objc-selector :
8365 static tree
8366 c_parser_objc_selector_arg (c_parser *parser)
8368 tree sel = c_parser_objc_selector (parser);
8369 tree list = NULL_TREE;
8370 if (sel && c_parser_next_token_is_not (parser, CPP_COLON))
8371 return sel;
8372 while (true)
8374 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
8375 return list;
8376 list = chainon (list, build_tree_list (sel, NULL_TREE));
8377 sel = c_parser_objc_selector (parser);
8378 if (!sel && c_parser_next_token_is_not (parser, CPP_COLON))
8379 break;
8381 return list;
8384 /* Parse an objc-receiver.
8386 objc-receiver:
8387 expression
8388 class-name
8389 type-name
8392 static tree
8393 c_parser_objc_receiver (c_parser *parser)
8395 if (c_parser_peek_token (parser)->type == CPP_NAME
8396 && (c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME
8397 || c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME))
8399 tree id = c_parser_peek_token (parser)->value;
8400 c_parser_consume_token (parser);
8401 return objc_get_class_reference (id);
8403 return c_fully_fold (c_parser_expression (parser).value, false, NULL);
8406 /* Parse objc-message-args.
8408 objc-message-args:
8409 objc-selector
8410 objc-keywordarg-list
8412 objc-keywordarg-list:
8413 objc-keywordarg
8414 objc-keywordarg-list objc-keywordarg
8416 objc-keywordarg:
8417 objc-selector : objc-keywordexpr
8418 : objc-keywordexpr
8421 static tree
8422 c_parser_objc_message_args (c_parser *parser)
8424 tree sel = c_parser_objc_selector (parser);
8425 tree list = NULL_TREE;
8426 if (sel && c_parser_next_token_is_not (parser, CPP_COLON))
8427 return sel;
8428 while (true)
8430 tree keywordexpr;
8431 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
8432 return error_mark_node;
8433 keywordexpr = c_parser_objc_keywordexpr (parser);
8434 list = chainon (list, build_tree_list (sel, keywordexpr));
8435 sel = c_parser_objc_selector (parser);
8436 if (!sel && c_parser_next_token_is_not (parser, CPP_COLON))
8437 break;
8439 return list;
8442 /* Parse an objc-keywordexpr.
8444 objc-keywordexpr:
8445 nonempty-expr-list
8448 static tree
8449 c_parser_objc_keywordexpr (c_parser *parser)
8451 tree ret;
8452 vec<tree, va_gc> *expr_list = c_parser_expr_list (parser, true, true,
8453 NULL, NULL, NULL);
8454 if (vec_safe_length (expr_list) == 1)
8456 /* Just return the expression, remove a level of
8457 indirection. */
8458 ret = (*expr_list)[0];
8460 else
8462 /* We have a comma expression, we will collapse later. */
8463 ret = build_tree_list_vec (expr_list);
8465 release_tree_vector (expr_list);
8466 return ret;
8469 /* A check, needed in several places, that ObjC interface, implementation or
8470 method definitions are not prefixed by incorrect items. */
8471 static bool
8472 c_parser_objc_diagnose_bad_element_prefix (c_parser *parser,
8473 struct c_declspecs *specs)
8475 if (!specs->declspecs_seen_p || specs->non_sc_seen_p
8476 || specs->typespec_kind != ctsk_none)
8478 c_parser_error (parser,
8479 "no type or storage class may be specified here,");
8480 c_parser_skip_to_end_of_block_or_statement (parser);
8481 return true;
8483 return false;
8486 /* Parse an Objective-C @property declaration. The syntax is:
8488 objc-property-declaration:
8489 '@property' objc-property-attributes[opt] struct-declaration ;
8491 objc-property-attributes:
8492 '(' objc-property-attribute-list ')'
8494 objc-property-attribute-list:
8495 objc-property-attribute
8496 objc-property-attribute-list, objc-property-attribute
8498 objc-property-attribute
8499 'getter' = identifier
8500 'setter' = identifier
8501 'readonly'
8502 'readwrite'
8503 'assign'
8504 'retain'
8505 'copy'
8506 'nonatomic'
8508 For example:
8509 @property NSString *name;
8510 @property (readonly) id object;
8511 @property (retain, nonatomic, getter=getTheName) id name;
8512 @property int a, b, c;
8514 PS: This function is identical to cp_parser_objc_at_propery_declaration
8515 for C++. Keep them in sync. */
8516 static void
8517 c_parser_objc_at_property_declaration (c_parser *parser)
8519 /* The following variables hold the attributes of the properties as
8520 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
8521 seen. When we see an attribute, we set them to 'true' (if they
8522 are boolean properties) or to the identifier (if they have an
8523 argument, ie, for getter and setter). Note that here we only
8524 parse the list of attributes, check the syntax and accumulate the
8525 attributes that we find. objc_add_property_declaration() will
8526 then process the information. */
8527 bool property_assign = false;
8528 bool property_copy = false;
8529 tree property_getter_ident = NULL_TREE;
8530 bool property_nonatomic = false;
8531 bool property_readonly = false;
8532 bool property_readwrite = false;
8533 bool property_retain = false;
8534 tree property_setter_ident = NULL_TREE;
8536 /* 'properties' is the list of properties that we read. Usually a
8537 single one, but maybe more (eg, in "@property int a, b, c;" there
8538 are three). */
8539 tree properties;
8540 location_t loc;
8542 loc = c_parser_peek_token (parser)->location;
8543 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_PROPERTY));
8545 c_parser_consume_token (parser); /* Eat '@property'. */
8547 /* Parse the optional attribute list... */
8548 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
8550 /* Eat the '(' */
8551 c_parser_consume_token (parser);
8553 /* Property attribute keywords are valid now. */
8554 parser->objc_property_attr_context = true;
8556 while (true)
8558 bool syntax_error = false;
8559 c_token *token = c_parser_peek_token (parser);
8560 enum rid keyword;
8562 if (token->type != CPP_KEYWORD)
8564 if (token->type == CPP_CLOSE_PAREN)
8565 c_parser_error (parser, "expected identifier");
8566 else
8568 c_parser_consume_token (parser);
8569 c_parser_error (parser, "unknown property attribute");
8571 break;
8573 keyword = token->keyword;
8574 c_parser_consume_token (parser);
8575 switch (keyword)
8577 case RID_ASSIGN: property_assign = true; break;
8578 case RID_COPY: property_copy = true; break;
8579 case RID_NONATOMIC: property_nonatomic = true; break;
8580 case RID_READONLY: property_readonly = true; break;
8581 case RID_READWRITE: property_readwrite = true; break;
8582 case RID_RETAIN: property_retain = true; break;
8584 case RID_GETTER:
8585 case RID_SETTER:
8586 if (c_parser_next_token_is_not (parser, CPP_EQ))
8588 if (keyword == RID_GETTER)
8589 c_parser_error (parser,
8590 "missing %<=%> (after %<getter%> attribute)");
8591 else
8592 c_parser_error (parser,
8593 "missing %<=%> (after %<setter%> attribute)");
8594 syntax_error = true;
8595 break;
8597 c_parser_consume_token (parser); /* eat the = */
8598 if (c_parser_next_token_is_not (parser, CPP_NAME))
8600 c_parser_error (parser, "expected identifier");
8601 syntax_error = true;
8602 break;
8604 if (keyword == RID_SETTER)
8606 if (property_setter_ident != NULL_TREE)
8607 c_parser_error (parser, "the %<setter%> attribute may only be specified once");
8608 else
8609 property_setter_ident = c_parser_peek_token (parser)->value;
8610 c_parser_consume_token (parser);
8611 if (c_parser_next_token_is_not (parser, CPP_COLON))
8612 c_parser_error (parser, "setter name must terminate with %<:%>");
8613 else
8614 c_parser_consume_token (parser);
8616 else
8618 if (property_getter_ident != NULL_TREE)
8619 c_parser_error (parser, "the %<getter%> attribute may only be specified once");
8620 else
8621 property_getter_ident = c_parser_peek_token (parser)->value;
8622 c_parser_consume_token (parser);
8624 break;
8625 default:
8626 c_parser_error (parser, "unknown property attribute");
8627 syntax_error = true;
8628 break;
8631 if (syntax_error)
8632 break;
8634 if (c_parser_next_token_is (parser, CPP_COMMA))
8635 c_parser_consume_token (parser);
8636 else
8637 break;
8639 parser->objc_property_attr_context = false;
8640 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8642 /* ... and the property declaration(s). */
8643 properties = c_parser_struct_declaration (parser);
8645 if (properties == error_mark_node)
8647 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
8648 parser->error = false;
8649 return;
8652 if (properties == NULL_TREE)
8653 c_parser_error (parser, "expected identifier");
8654 else
8656 /* Comma-separated properties are chained together in
8657 reverse order; add them one by one. */
8658 properties = nreverse (properties);
8660 for (; properties; properties = TREE_CHAIN (properties))
8661 objc_add_property_declaration (loc, copy_node (properties),
8662 property_readonly, property_readwrite,
8663 property_assign, property_retain,
8664 property_copy, property_nonatomic,
8665 property_getter_ident, property_setter_ident);
8668 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8669 parser->error = false;
8672 /* Parse an Objective-C @synthesize declaration. The syntax is:
8674 objc-synthesize-declaration:
8675 @synthesize objc-synthesize-identifier-list ;
8677 objc-synthesize-identifier-list:
8678 objc-synthesize-identifier
8679 objc-synthesize-identifier-list, objc-synthesize-identifier
8681 objc-synthesize-identifier
8682 identifier
8683 identifier = identifier
8685 For example:
8686 @synthesize MyProperty;
8687 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
8689 PS: This function is identical to cp_parser_objc_at_synthesize_declaration
8690 for C++. Keep them in sync.
8692 static void
8693 c_parser_objc_at_synthesize_declaration (c_parser *parser)
8695 tree list = NULL_TREE;
8696 location_t loc;
8697 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_SYNTHESIZE));
8698 loc = c_parser_peek_token (parser)->location;
8700 c_parser_consume_token (parser);
8701 while (true)
8703 tree property, ivar;
8704 if (c_parser_next_token_is_not (parser, CPP_NAME))
8706 c_parser_error (parser, "expected identifier");
8707 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
8708 /* Once we find the semicolon, we can resume normal parsing.
8709 We have to reset parser->error manually because
8710 c_parser_skip_until_found() won't reset it for us if the
8711 next token is precisely a semicolon. */
8712 parser->error = false;
8713 return;
8715 property = c_parser_peek_token (parser)->value;
8716 c_parser_consume_token (parser);
8717 if (c_parser_next_token_is (parser, CPP_EQ))
8719 c_parser_consume_token (parser);
8720 if (c_parser_next_token_is_not (parser, CPP_NAME))
8722 c_parser_error (parser, "expected identifier");
8723 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
8724 parser->error = false;
8725 return;
8727 ivar = c_parser_peek_token (parser)->value;
8728 c_parser_consume_token (parser);
8730 else
8731 ivar = NULL_TREE;
8732 list = chainon (list, build_tree_list (ivar, property));
8733 if (c_parser_next_token_is (parser, CPP_COMMA))
8734 c_parser_consume_token (parser);
8735 else
8736 break;
8738 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8739 objc_add_synthesize_declaration (loc, list);
8742 /* Parse an Objective-C @dynamic declaration. The syntax is:
8744 objc-dynamic-declaration:
8745 @dynamic identifier-list ;
8747 For example:
8748 @dynamic MyProperty;
8749 @dynamic MyProperty, AnotherProperty;
8751 PS: This function is identical to cp_parser_objc_at_dynamic_declaration
8752 for C++. Keep them in sync.
8754 static void
8755 c_parser_objc_at_dynamic_declaration (c_parser *parser)
8757 tree list = NULL_TREE;
8758 location_t loc;
8759 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_DYNAMIC));
8760 loc = c_parser_peek_token (parser)->location;
8762 c_parser_consume_token (parser);
8763 while (true)
8765 tree property;
8766 if (c_parser_next_token_is_not (parser, CPP_NAME))
8768 c_parser_error (parser, "expected identifier");
8769 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
8770 parser->error = false;
8771 return;
8773 property = c_parser_peek_token (parser)->value;
8774 list = chainon (list, build_tree_list (NULL_TREE, property));
8775 c_parser_consume_token (parser);
8776 if (c_parser_next_token_is (parser, CPP_COMMA))
8777 c_parser_consume_token (parser);
8778 else
8779 break;
8781 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8782 objc_add_dynamic_declaration (loc, list);
8785 /* Parse UPC shared qualifier
8787 shared-type-qualifier: shared layout-qualifier-opt
8788 layout-qualifier: [ constant-expression-opt ] | [ * ]
8791 static void
8792 c_parser_upc_shared_qual (source_location loc,
8793 c_parser *parser,
8794 struct c_declspecs *specs)
8796 tree array_qual, arg1;
8798 /* consume "shared" part */
8799 c_parser_consume_token (parser);
8801 /* check for shared array layout specifier */
8802 if (!c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
8804 declspecs_add_qual (loc, specs, ridpointers[RID_SHARED]);
8805 return;
8807 c_parser_consume_token (parser);
8808 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
8810 /* [] layout specifier */
8811 arg1 = size_zero_node;
8813 else if (c_parser_next_token_is (parser, CPP_MULT))
8815 /* [*] layout specifier */
8816 arg1 = build1 (INDIRECT_REF, NULL_TREE, NULL_TREE);
8817 c_parser_consume_token (parser);
8819 else
8821 /* [ expression ] layout specifier */
8822 arg1 = c_parser_expression (parser).value;
8824 array_qual = build4 (ARRAY_REF, NULL_TREE, NULL_TREE,
8825 arg1, NULL_TREE, NULL_TREE);
8826 declspecs_add_qual (loc, specs, array_qual);
8828 if (!c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
8830 c_parser_error (parser, "expected ]");
8832 c_parser_consume_token (parser);
8835 /* Parse a UPC upc_forall statement
8837 upc_forall-statement:
8838 upc_forall ( expression[opt] ; expression[opt] ;
8839 expression[opt] ; affinity[opt] ) statement
8840 affinity: experssion | continue */
8842 static void
8843 c_parser_upc_forall_statement (c_parser *parser)
8845 tree block, cond, incr, save_break, save_cont, body;
8846 tree affinity;
8847 location_t loc = c_parser_peek_token (parser)->location;
8848 location_t affinity_loc = UNKNOWN_LOCATION;
8849 const int profile_upc_forall = flag_upc_instrument && get_upc_pupc_mode();
8850 gcc_assert (c_parser_next_token_is_keyword (parser, RID_UPC_FORALL));
8851 c_parser_consume_token (parser);
8852 block = c_begin_compound_stmt (flag_isoc99);
8853 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
8855 /* Parse the initialization declaration or expression. */
8856 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
8858 c_parser_consume_token (parser);
8859 c_finish_expr_stmt (loc, NULL_TREE);
8861 else if (c_parser_next_token_starts_declspecs (parser))
8863 c_parser_declaration_or_fndef (parser, true, true, true,
8864 true, true, NULL);
8865 check_for_loop_decls (loc, true);
8867 else if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
8869 /* __extension__ can start a declaration, but is also an
8870 unary operator that can start an expression. Consume all
8871 but the last of a possible series of __extension__ to
8872 determine which. */
8873 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
8874 && (c_parser_peek_2nd_token (parser)->keyword
8875 == RID_EXTENSION))
8876 c_parser_consume_token (parser);
8877 if (c_token_starts_declspecs (c_parser_peek_2nd_token (parser)))
8879 int ext;
8880 ext = disable_extension_diagnostics ();
8881 c_parser_consume_token (parser);
8882 c_parser_declaration_or_fndef (parser, true, true, true, true, true, NULL);
8883 restore_extension_diagnostics (ext);
8884 check_for_loop_decls (loc, true);
8886 else
8887 goto init_expr;
8889 else
8891 init_expr:
8892 c_finish_expr_stmt (loc, c_parser_expression (parser).value);
8893 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8895 /* Parse the loop condition. */
8896 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
8898 c_parser_consume_token (parser);
8899 cond = NULL_TREE;
8901 else
8903 cond = c_parser_condition (parser);
8904 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8906 /* Parse the increment expression. */
8907 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
8908 incr = c_process_expr_stmt (loc, NULL_TREE);
8909 else
8910 incr = c_process_expr_stmt (loc, c_parser_expression (parser).value);
8911 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8912 /* Parse the UPC affinity expression. */
8913 affinity_loc = c_parser_peek_token (parser)->location;
8914 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
8916 affinity = NULL_TREE;
8918 else if (c_parser_peek_token (parser)->type == CPP_KEYWORD
8919 && c_parser_peek_token (parser)->keyword == RID_CONTINUE)
8921 affinity = NULL_TREE;
8922 c_parser_consume_token (parser);
8924 else
8926 affinity = c_parser_expression_conv (parser).value;
8927 affinity = c_fully_fold (affinity, false, NULL);
8929 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8930 if (affinity)
8931 affinity = upc_affinity_test (affinity_loc, affinity);
8933 else
8935 cond = error_mark_node;
8936 incr = error_mark_node;
8937 affinity = error_mark_node;
8939 save_break = c_break_label;
8940 c_break_label = NULL_TREE;
8941 save_cont = c_cont_label;
8942 c_cont_label = NULL_TREE;
8943 body = c_parser_c99_block_statement (parser);
8944 if (profile_upc_forall)
8946 const tree gasp_start = upc_instrument_forall (loc, 1 /* start */);
8947 add_stmt (gasp_start);
8949 loc = c_parser_peek_token (parser)->location;
8950 if (affinity != NULL_TREE && affinity != error_mark_node)
8952 tree upc_forall_depth = upc_rts_forall_depth_var ();
8953 tree inc_depth, depth_gt_one;
8954 inc_depth = build_unary_op (loc, PREINCREMENT_EXPR, upc_forall_depth, 0);
8955 c_finish_expr_stmt (loc, inc_depth);
8956 depth_gt_one = build_binary_op (affinity_loc,
8957 GT_EXPR, upc_forall_depth, integer_one_node, 0);
8958 depth_gt_one = c_objc_common_truthvalue_conversion (affinity_loc, depth_gt_one);
8959 depth_gt_one = c_fully_fold (depth_gt_one, false, NULL);
8960 affinity = build_binary_op (affinity_loc, TRUTH_OR_EXPR,
8961 depth_gt_one, affinity, 0);
8962 body = build3 (COND_EXPR, void_type_node, affinity,
8963 body, NULL_TREE);
8964 c_finish_loop (loc, cond, incr, body, c_break_label, c_cont_label, true);
8965 c_finish_expr_stmt (loc,
8966 build_unary_op (loc, PREDECREMENT_EXPR, upc_forall_depth, 0));
8968 else
8969 c_finish_loop (loc, cond, incr, body, c_break_label, c_cont_label, true);
8970 if (profile_upc_forall)
8972 const tree gasp_end = upc_instrument_forall (loc, 0 /* start */);
8973 add_stmt (gasp_end);
8975 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
8976 c_break_label = save_break;
8977 c_cont_label = save_cont;
8980 /* Parse an upc-sync-statement.
8982 upc_barrier, upc_wait, upc_notify
8985 static void
8986 c_parser_upc_sync_statement (c_parser *parser, int sync_kind)
8988 location_t loc;
8989 tree expr = NULL_TREE;
8990 tree stmt;
8991 gcc_assert (c_parser_next_token_is_keyword (parser, RID_UPC_BARRIER) ||
8992 c_parser_next_token_is_keyword (parser, RID_UPC_NOTIFY) ||
8993 c_parser_next_token_is_keyword (parser, RID_UPC_WAIT));
8994 loc = c_parser_peek_token (parser)->location;
8995 c_parser_consume_token (parser);
8996 if (c_parser_peek_token (parser)->type != CPP_SEMICOLON)
8998 expr = c_parser_expression (parser).value;
8999 if (expr == error_mark_node)
9000 expr = NULL;
9001 if (!INTEGRAL_TYPE_P (TREE_TYPE (expr)))
9003 c_parser_error (parser, "expected integer expression");
9004 expr = NULL;
9007 stmt = size_int (sync_kind);
9008 (void) upc_build_sync_stmt (loc, stmt, expr);
9012 /* Handle pragmas. Some OpenMP pragmas are associated with, and therefore
9013 should be considered, statements. ALLOW_STMT is true if we're within
9014 the context of a function and such pragmas are to be allowed. Returns
9015 true if we actually parsed such a pragma. */
9017 static bool
9018 c_parser_pragma (c_parser *parser, enum pragma_context context)
9020 unsigned int id;
9022 id = c_parser_peek_token (parser)->pragma_kind;
9023 gcc_assert (id != PRAGMA_NONE);
9025 switch (id)
9027 case PRAGMA_OMP_BARRIER:
9028 if (context != pragma_compound)
9030 if (context == pragma_stmt)
9031 c_parser_error (parser, "%<#pragma omp barrier%> may only be "
9032 "used in compound statements");
9033 goto bad_stmt;
9035 c_parser_omp_barrier (parser);
9036 return false;
9038 case PRAGMA_OMP_FLUSH:
9039 if (context != pragma_compound)
9041 if (context == pragma_stmt)
9042 c_parser_error (parser, "%<#pragma omp flush%> may only be "
9043 "used in compound statements");
9044 goto bad_stmt;
9046 c_parser_omp_flush (parser);
9047 return false;
9049 case PRAGMA_OMP_TASKWAIT:
9050 if (context != pragma_compound)
9052 if (context == pragma_stmt)
9053 c_parser_error (parser, "%<#pragma omp taskwait%> may only be "
9054 "used in compound statements");
9055 goto bad_stmt;
9057 c_parser_omp_taskwait (parser);
9058 return false;
9060 case PRAGMA_OMP_TASKYIELD:
9061 if (context != pragma_compound)
9063 if (context == pragma_stmt)
9064 c_parser_error (parser, "%<#pragma omp taskyield%> may only be "
9065 "used in compound statements");
9066 goto bad_stmt;
9068 c_parser_omp_taskyield (parser);
9069 return false;
9071 case PRAGMA_OMP_THREADPRIVATE:
9072 c_parser_omp_threadprivate (parser);
9073 return false;
9075 case PRAGMA_OMP_SECTION:
9076 error_at (c_parser_peek_token (parser)->location,
9077 "%<#pragma omp section%> may only be used in "
9078 "%<#pragma omp sections%> construct");
9079 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
9080 return false;
9082 case PRAGMA_GCC_PCH_PREPROCESS:
9083 c_parser_error (parser, "%<#pragma GCC pch_preprocess%> must be first");
9084 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
9085 return false;
9087 default:
9088 if (id < PRAGMA_FIRST_EXTERNAL)
9090 if (context == pragma_external)
9092 bad_stmt:
9093 c_parser_error (parser, "expected declaration specifiers");
9094 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
9095 return false;
9097 c_parser_omp_construct (parser);
9098 return true;
9100 break;
9103 c_parser_consume_pragma (parser);
9104 c_invoke_pragma_handler (id);
9106 /* Skip to EOL, but suppress any error message. Those will have been
9107 generated by the handler routine through calling error, as opposed
9108 to calling c_parser_error. */
9109 parser->error = true;
9110 c_parser_skip_to_pragma_eol (parser);
9112 return false;
9115 /* The interface the pragma parsers have to the lexer. */
9117 enum cpp_ttype
9118 pragma_lex (tree *value)
9120 c_token *tok = c_parser_peek_token (the_parser);
9121 enum cpp_ttype ret = tok->type;
9123 *value = tok->value;
9124 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
9125 ret = CPP_EOF;
9126 else
9128 if (ret == CPP_KEYWORD)
9129 ret = CPP_NAME;
9130 c_parser_consume_token (the_parser);
9133 return ret;
9136 static void
9137 c_parser_pragma_pch_preprocess (c_parser *parser)
9139 tree name = NULL;
9141 c_parser_consume_pragma (parser);
9142 if (c_parser_next_token_is (parser, CPP_STRING))
9144 name = c_parser_peek_token (parser)->value;
9145 c_parser_consume_token (parser);
9147 else
9148 c_parser_error (parser, "expected string literal");
9149 c_parser_skip_to_pragma_eol (parser);
9151 if (name)
9152 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
9155 /* OpenMP 2.5 parsing routines. */
9157 /* Returns name of the next clause.
9158 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
9159 the token is not consumed. Otherwise appropriate pragma_omp_clause is
9160 returned and the token is consumed. */
9162 static pragma_omp_clause
9163 c_parser_omp_clause_name (c_parser *parser)
9165 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
9167 if (c_parser_next_token_is_keyword (parser, RID_IF))
9168 result = PRAGMA_OMP_CLAUSE_IF;
9169 else if (c_parser_next_token_is_keyword (parser, RID_DEFAULT))
9170 result = PRAGMA_OMP_CLAUSE_DEFAULT;
9171 else if (c_parser_next_token_is (parser, CPP_NAME))
9173 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
9175 switch (p[0])
9177 case 'c':
9178 if (!strcmp ("collapse", p))
9179 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
9180 else if (!strcmp ("copyin", p))
9181 result = PRAGMA_OMP_CLAUSE_COPYIN;
9182 else if (!strcmp ("copyprivate", p))
9183 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
9184 break;
9185 case 'f':
9186 if (!strcmp ("final", p))
9187 result = PRAGMA_OMP_CLAUSE_FINAL;
9188 else if (!strcmp ("firstprivate", p))
9189 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
9190 break;
9191 case 'l':
9192 if (!strcmp ("lastprivate", p))
9193 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
9194 break;
9195 case 'm':
9196 if (!strcmp ("mergeable", p))
9197 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
9198 break;
9199 case 'n':
9200 if (!strcmp ("nowait", p))
9201 result = PRAGMA_OMP_CLAUSE_NOWAIT;
9202 else if (!strcmp ("num_threads", p))
9203 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
9204 break;
9205 case 'o':
9206 if (!strcmp ("ordered", p))
9207 result = PRAGMA_OMP_CLAUSE_ORDERED;
9208 break;
9209 case 'p':
9210 if (!strcmp ("private", p))
9211 result = PRAGMA_OMP_CLAUSE_PRIVATE;
9212 break;
9213 case 'r':
9214 if (!strcmp ("reduction", p))
9215 result = PRAGMA_OMP_CLAUSE_REDUCTION;
9216 break;
9217 case 's':
9218 if (!strcmp ("schedule", p))
9219 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
9220 else if (!strcmp ("shared", p))
9221 result = PRAGMA_OMP_CLAUSE_SHARED;
9222 break;
9223 case 'u':
9224 if (!strcmp ("untied", p))
9225 result = PRAGMA_OMP_CLAUSE_UNTIED;
9226 break;
9230 if (result != PRAGMA_OMP_CLAUSE_NONE)
9231 c_parser_consume_token (parser);
9233 return result;
9236 /* Validate that a clause of the given type does not already exist. */
9238 static void
9239 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
9240 const char *name)
9242 tree c;
9244 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
9245 if (OMP_CLAUSE_CODE (c) == code)
9247 location_t loc = OMP_CLAUSE_LOCATION (c);
9248 error_at (loc, "too many %qs clauses", name);
9249 break;
9253 /* OpenMP 2.5:
9254 variable-list:
9255 identifier
9256 variable-list , identifier
9258 If KIND is nonzero, create the appropriate node and install the
9259 decl in OMP_CLAUSE_DECL and add the node to the head of the list.
9260 If KIND is nonzero, CLAUSE_LOC is the location of the clause.
9262 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
9263 return the list created. */
9265 static tree
9266 c_parser_omp_variable_list (c_parser *parser,
9267 location_t clause_loc,
9268 enum omp_clause_code kind,
9269 tree list)
9271 if (c_parser_next_token_is_not (parser, CPP_NAME)
9272 || c_parser_peek_token (parser)->id_kind != C_ID_ID)
9273 c_parser_error (parser, "expected identifier");
9275 while (c_parser_next_token_is (parser, CPP_NAME)
9276 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
9278 tree t = lookup_name (c_parser_peek_token (parser)->value);
9280 if (t == NULL_TREE)
9281 undeclared_variable (c_parser_peek_token (parser)->location,
9282 c_parser_peek_token (parser)->value);
9283 else if (t == error_mark_node)
9285 else if (kind != 0)
9287 tree u = build_omp_clause (clause_loc, kind);
9288 OMP_CLAUSE_DECL (u) = t;
9289 OMP_CLAUSE_CHAIN (u) = list;
9290 list = u;
9292 else
9293 list = tree_cons (t, NULL_TREE, list);
9295 c_parser_consume_token (parser);
9297 if (c_parser_next_token_is_not (parser, CPP_COMMA))
9298 break;
9300 c_parser_consume_token (parser);
9303 return list;
9306 /* Similarly, but expect leading and trailing parenthesis. This is a very
9307 common case for omp clauses. */
9309 static tree
9310 c_parser_omp_var_list_parens (c_parser *parser, enum omp_clause_code kind,
9311 tree list)
9313 /* The clauses location. */
9314 location_t loc = c_parser_peek_token (parser)->location;
9316 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
9318 list = c_parser_omp_variable_list (parser, loc, kind, list);
9319 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
9321 return list;
9324 /* OpenMP 3.0:
9325 collapse ( constant-expression ) */
9327 static tree
9328 c_parser_omp_clause_collapse (c_parser *parser, tree list)
9330 tree c, num = error_mark_node;
9331 HOST_WIDE_INT n;
9332 location_t loc;
9334 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse");
9336 loc = c_parser_peek_token (parser)->location;
9337 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
9339 num = c_parser_expr_no_commas (parser, NULL).value;
9340 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
9342 if (num == error_mark_node)
9343 return list;
9344 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
9345 || !host_integerp (num, 0)
9346 || (n = tree_low_cst (num, 0)) <= 0
9347 || (int) n != n)
9349 error_at (loc,
9350 "collapse argument needs positive constant integer expression");
9351 return list;
9353 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
9354 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
9355 OMP_CLAUSE_CHAIN (c) = list;
9356 return c;
9359 /* OpenMP 2.5:
9360 copyin ( variable-list ) */
9362 static tree
9363 c_parser_omp_clause_copyin (c_parser *parser, tree list)
9365 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_COPYIN, list);
9368 /* OpenMP 2.5:
9369 copyprivate ( variable-list ) */
9371 static tree
9372 c_parser_omp_clause_copyprivate (c_parser *parser, tree list)
9374 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_COPYPRIVATE, list);
9377 /* OpenMP 2.5:
9378 default ( shared | none ) */
9380 static tree
9381 c_parser_omp_clause_default (c_parser *parser, tree list)
9383 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
9384 location_t loc = c_parser_peek_token (parser)->location;
9385 tree c;
9387 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
9388 return list;
9389 if (c_parser_next_token_is (parser, CPP_NAME))
9391 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
9393 switch (p[0])
9395 case 'n':
9396 if (strcmp ("none", p) != 0)
9397 goto invalid_kind;
9398 kind = OMP_CLAUSE_DEFAULT_NONE;
9399 break;
9401 case 's':
9402 if (strcmp ("shared", p) != 0)
9403 goto invalid_kind;
9404 kind = OMP_CLAUSE_DEFAULT_SHARED;
9405 break;
9407 default:
9408 goto invalid_kind;
9411 c_parser_consume_token (parser);
9413 else
9415 invalid_kind:
9416 c_parser_error (parser, "expected %<none%> or %<shared%>");
9418 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
9420 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
9421 return list;
9423 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default");
9424 c = build_omp_clause (loc, OMP_CLAUSE_DEFAULT);
9425 OMP_CLAUSE_CHAIN (c) = list;
9426 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
9428 return c;
9431 /* OpenMP 2.5:
9432 firstprivate ( variable-list ) */
9434 static tree
9435 c_parser_omp_clause_firstprivate (c_parser *parser, tree list)
9437 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_FIRSTPRIVATE, list);
9440 /* OpenMP 3.1:
9441 final ( expression ) */
9443 static tree
9444 c_parser_omp_clause_final (c_parser *parser, tree list)
9446 location_t loc = c_parser_peek_token (parser)->location;
9447 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
9449 tree t = c_parser_paren_condition (parser);
9450 tree c;
9452 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final");
9454 c = build_omp_clause (loc, OMP_CLAUSE_FINAL);
9455 OMP_CLAUSE_FINAL_EXPR (c) = t;
9456 OMP_CLAUSE_CHAIN (c) = list;
9457 list = c;
9459 else
9460 c_parser_error (parser, "expected %<(%>");
9462 return list;
9465 /* OpenMP 2.5:
9466 if ( expression ) */
9468 static tree
9469 c_parser_omp_clause_if (c_parser *parser, tree list)
9471 location_t loc = c_parser_peek_token (parser)->location;
9472 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
9474 tree t = c_parser_paren_condition (parser);
9475 tree c;
9477 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if");
9479 c = build_omp_clause (loc, OMP_CLAUSE_IF);
9480 OMP_CLAUSE_IF_EXPR (c) = t;
9481 OMP_CLAUSE_CHAIN (c) = list;
9482 list = c;
9484 else
9485 c_parser_error (parser, "expected %<(%>");
9487 return list;
9490 /* OpenMP 2.5:
9491 lastprivate ( variable-list ) */
9493 static tree
9494 c_parser_omp_clause_lastprivate (c_parser *parser, tree list)
9496 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_LASTPRIVATE, list);
9499 /* OpenMP 3.1:
9500 mergeable */
9502 static tree
9503 c_parser_omp_clause_mergeable (c_parser *parser ATTRIBUTE_UNUSED, tree list)
9505 tree c;
9507 /* FIXME: Should we allow duplicates? */
9508 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable");
9510 c = build_omp_clause (c_parser_peek_token (parser)->location,
9511 OMP_CLAUSE_MERGEABLE);
9512 OMP_CLAUSE_CHAIN (c) = list;
9514 return c;
9517 /* OpenMP 2.5:
9518 nowait */
9520 static tree
9521 c_parser_omp_clause_nowait (c_parser *parser ATTRIBUTE_UNUSED, tree list)
9523 tree c;
9524 location_t loc = c_parser_peek_token (parser)->location;
9526 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait");
9528 c = build_omp_clause (loc, OMP_CLAUSE_NOWAIT);
9529 OMP_CLAUSE_CHAIN (c) = list;
9530 return c;
9533 /* OpenMP 2.5:
9534 num_threads ( expression ) */
9536 static tree
9537 c_parser_omp_clause_num_threads (c_parser *parser, tree list)
9539 location_t num_threads_loc = c_parser_peek_token (parser)->location;
9540 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
9542 location_t expr_loc = c_parser_peek_token (parser)->location;
9543 tree c, t = c_parser_expression (parser).value;
9544 mark_exp_read (t);
9545 t = c_fully_fold (t, false, NULL);
9547 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
9549 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
9551 c_parser_error (parser, "expected integer expression");
9552 return list;
9555 /* Attempt to statically determine when the number isn't positive. */
9556 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
9557 build_int_cst (TREE_TYPE (t), 0));
9558 if (CAN_HAVE_LOCATION_P (c))
9559 SET_EXPR_LOCATION (c, expr_loc);
9560 if (c == boolean_true_node)
9562 warning_at (expr_loc, 0,
9563 "%<num_threads%> value must be positive");
9564 t = integer_one_node;
9567 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS, "num_threads");
9569 c = build_omp_clause (num_threads_loc, OMP_CLAUSE_NUM_THREADS);
9570 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
9571 OMP_CLAUSE_CHAIN (c) = list;
9572 list = c;
9575 return list;
9578 /* OpenMP 2.5:
9579 ordered */
9581 static tree
9582 c_parser_omp_clause_ordered (c_parser *parser, tree list)
9584 tree c;
9586 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED, "ordered");
9588 c = build_omp_clause (c_parser_peek_token (parser)->location,
9589 OMP_CLAUSE_ORDERED);
9590 OMP_CLAUSE_CHAIN (c) = list;
9592 return c;
9595 /* OpenMP 2.5:
9596 private ( variable-list ) */
9598 static tree
9599 c_parser_omp_clause_private (c_parser *parser, tree list)
9601 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_PRIVATE, list);
9604 /* OpenMP 2.5:
9605 reduction ( reduction-operator : variable-list )
9607 reduction-operator:
9608 One of: + * - & ^ | && ||
9610 OpenMP 3.1:
9612 reduction-operator:
9613 One of: + * - & ^ | && || max min */
9615 static tree
9616 c_parser_omp_clause_reduction (c_parser *parser, tree list)
9618 location_t clause_loc = c_parser_peek_token (parser)->location;
9619 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
9621 enum tree_code code;
9623 switch (c_parser_peek_token (parser)->type)
9625 case CPP_PLUS:
9626 code = PLUS_EXPR;
9627 break;
9628 case CPP_MULT:
9629 code = MULT_EXPR;
9630 break;
9631 case CPP_MINUS:
9632 code = MINUS_EXPR;
9633 break;
9634 case CPP_AND:
9635 code = BIT_AND_EXPR;
9636 break;
9637 case CPP_XOR:
9638 code = BIT_XOR_EXPR;
9639 break;
9640 case CPP_OR:
9641 code = BIT_IOR_EXPR;
9642 break;
9643 case CPP_AND_AND:
9644 code = TRUTH_ANDIF_EXPR;
9645 break;
9646 case CPP_OR_OR:
9647 code = TRUTH_ORIF_EXPR;
9648 break;
9649 case CPP_NAME:
9651 const char *p
9652 = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
9653 if (strcmp (p, "min") == 0)
9655 code = MIN_EXPR;
9656 break;
9658 if (strcmp (p, "max") == 0)
9660 code = MAX_EXPR;
9661 break;
9664 /* FALLTHRU */
9665 default:
9666 c_parser_error (parser,
9667 "expected %<+%>, %<*%>, %<-%>, %<&%>, "
9668 "%<^%>, %<|%>, %<&&%>, %<||%>, %<min%> or %<max%>");
9669 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
9670 return list;
9672 c_parser_consume_token (parser);
9673 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
9675 tree nl, c;
9677 nl = c_parser_omp_variable_list (parser, clause_loc,
9678 OMP_CLAUSE_REDUCTION, list);
9679 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
9680 OMP_CLAUSE_REDUCTION_CODE (c) = code;
9682 list = nl;
9684 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
9686 return list;
9689 /* OpenMP 2.5:
9690 schedule ( schedule-kind )
9691 schedule ( schedule-kind , expression )
9693 schedule-kind:
9694 static | dynamic | guided | runtime | auto
9697 static tree
9698 c_parser_omp_clause_schedule (c_parser *parser, tree list)
9700 tree c, t;
9701 location_t loc = c_parser_peek_token (parser)->location;
9703 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
9704 return list;
9706 c = build_omp_clause (loc, OMP_CLAUSE_SCHEDULE);
9708 if (c_parser_next_token_is (parser, CPP_NAME))
9710 tree kind = c_parser_peek_token (parser)->value;
9711 const char *p = IDENTIFIER_POINTER (kind);
9713 switch (p[0])
9715 case 'd':
9716 if (strcmp ("dynamic", p) != 0)
9717 goto invalid_kind;
9718 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
9719 break;
9721 case 'g':
9722 if (strcmp ("guided", p) != 0)
9723 goto invalid_kind;
9724 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
9725 break;
9727 case 'r':
9728 if (strcmp ("runtime", p) != 0)
9729 goto invalid_kind;
9730 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
9731 break;
9733 default:
9734 goto invalid_kind;
9737 else if (c_parser_next_token_is_keyword (parser, RID_STATIC))
9738 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
9739 else if (c_parser_next_token_is_keyword (parser, RID_AUTO))
9740 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
9741 else
9742 goto invalid_kind;
9744 c_parser_consume_token (parser);
9745 if (c_parser_next_token_is (parser, CPP_COMMA))
9747 location_t here;
9748 c_parser_consume_token (parser);
9750 here = c_parser_peek_token (parser)->location;
9751 t = c_parser_expr_no_commas (parser, NULL).value;
9752 mark_exp_read (t);
9753 t = c_fully_fold (t, false, NULL);
9755 if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
9756 error_at (here, "schedule %<runtime%> does not take "
9757 "a %<chunk_size%> parameter");
9758 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
9759 error_at (here,
9760 "schedule %<auto%> does not take "
9761 "a %<chunk_size%> parameter");
9762 else if (TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE)
9763 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
9764 else
9765 c_parser_error (parser, "expected integer expression");
9767 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
9769 else
9770 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
9771 "expected %<,%> or %<)%>");
9773 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
9774 OMP_CLAUSE_CHAIN (c) = list;
9775 return c;
9777 invalid_kind:
9778 c_parser_error (parser, "invalid schedule kind");
9779 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
9780 return list;
9783 /* OpenMP 2.5:
9784 shared ( variable-list ) */
9786 static tree
9787 c_parser_omp_clause_shared (c_parser *parser, tree list)
9789 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_SHARED, list);
9792 /* OpenMP 3.0:
9793 untied */
9795 static tree
9796 c_parser_omp_clause_untied (c_parser *parser ATTRIBUTE_UNUSED, tree list)
9798 tree c;
9800 /* FIXME: Should we allow duplicates? */
9801 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied");
9803 c = build_omp_clause (c_parser_peek_token (parser)->location,
9804 OMP_CLAUSE_UNTIED);
9805 OMP_CLAUSE_CHAIN (c) = list;
9807 return c;
9810 /* Parse all OpenMP clauses. The set clauses allowed by the directive
9811 is a bitmask in MASK. Return the list of clauses found; the result
9812 of clause default goes in *pdefault. */
9814 static tree
9815 c_parser_omp_all_clauses (c_parser *parser, unsigned int mask,
9816 const char *where)
9818 tree clauses = NULL;
9819 bool first = true;
9821 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
9823 location_t here;
9824 pragma_omp_clause c_kind;
9825 const char *c_name;
9826 tree prev = clauses;
9828 if (!first && c_parser_next_token_is (parser, CPP_COMMA))
9829 c_parser_consume_token (parser);
9831 first = false;
9832 here = c_parser_peek_token (parser)->location;
9833 c_kind = c_parser_omp_clause_name (parser);
9835 switch (c_kind)
9837 case PRAGMA_OMP_CLAUSE_COLLAPSE:
9838 clauses = c_parser_omp_clause_collapse (parser, clauses);
9839 c_name = "collapse";
9840 break;
9841 case PRAGMA_OMP_CLAUSE_COPYIN:
9842 clauses = c_parser_omp_clause_copyin (parser, clauses);
9843 c_name = "copyin";
9844 break;
9845 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
9846 clauses = c_parser_omp_clause_copyprivate (parser, clauses);
9847 c_name = "copyprivate";
9848 break;
9849 case PRAGMA_OMP_CLAUSE_DEFAULT:
9850 clauses = c_parser_omp_clause_default (parser, clauses);
9851 c_name = "default";
9852 break;
9853 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
9854 clauses = c_parser_omp_clause_firstprivate (parser, clauses);
9855 c_name = "firstprivate";
9856 break;
9857 case PRAGMA_OMP_CLAUSE_FINAL:
9858 clauses = c_parser_omp_clause_final (parser, clauses);
9859 c_name = "final";
9860 break;
9861 case PRAGMA_OMP_CLAUSE_IF:
9862 clauses = c_parser_omp_clause_if (parser, clauses);
9863 c_name = "if";
9864 break;
9865 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
9866 clauses = c_parser_omp_clause_lastprivate (parser, clauses);
9867 c_name = "lastprivate";
9868 break;
9869 case PRAGMA_OMP_CLAUSE_MERGEABLE:
9870 clauses = c_parser_omp_clause_mergeable (parser, clauses);
9871 c_name = "mergeable";
9872 break;
9873 case PRAGMA_OMP_CLAUSE_NOWAIT:
9874 clauses = c_parser_omp_clause_nowait (parser, clauses);
9875 c_name = "nowait";
9876 break;
9877 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
9878 clauses = c_parser_omp_clause_num_threads (parser, clauses);
9879 c_name = "num_threads";
9880 break;
9881 case PRAGMA_OMP_CLAUSE_ORDERED:
9882 clauses = c_parser_omp_clause_ordered (parser, clauses);
9883 c_name = "ordered";
9884 break;
9885 case PRAGMA_OMP_CLAUSE_PRIVATE:
9886 clauses = c_parser_omp_clause_private (parser, clauses);
9887 c_name = "private";
9888 break;
9889 case PRAGMA_OMP_CLAUSE_REDUCTION:
9890 clauses = c_parser_omp_clause_reduction (parser, clauses);
9891 c_name = "reduction";
9892 break;
9893 case PRAGMA_OMP_CLAUSE_SCHEDULE:
9894 clauses = c_parser_omp_clause_schedule (parser, clauses);
9895 c_name = "schedule";
9896 break;
9897 case PRAGMA_OMP_CLAUSE_SHARED:
9898 clauses = c_parser_omp_clause_shared (parser, clauses);
9899 c_name = "shared";
9900 break;
9901 case PRAGMA_OMP_CLAUSE_UNTIED:
9902 clauses = c_parser_omp_clause_untied (parser, clauses);
9903 c_name = "untied";
9904 break;
9905 default:
9906 c_parser_error (parser, "expected %<#pragma omp%> clause");
9907 goto saw_error;
9910 if (((mask >> c_kind) & 1) == 0 && !parser->error)
9912 /* Remove the invalid clause(s) from the list to avoid
9913 confusing the rest of the compiler. */
9914 clauses = prev;
9915 error_at (here, "%qs is not valid for %qs", c_name, where);
9919 saw_error:
9920 c_parser_skip_to_pragma_eol (parser);
9922 return c_finish_omp_clauses (clauses);
9925 /* OpenMP 2.5:
9926 structured-block:
9927 statement
9929 In practice, we're also interested in adding the statement to an
9930 outer node. So it is convenient if we work around the fact that
9931 c_parser_statement calls add_stmt. */
9933 static tree
9934 c_parser_omp_structured_block (c_parser *parser)
9936 tree stmt = push_stmt_list ();
9937 c_parser_statement (parser);
9938 return pop_stmt_list (stmt);
9941 /* OpenMP 2.5:
9942 # pragma omp atomic new-line
9943 expression-stmt
9945 expression-stmt:
9946 x binop= expr | x++ | ++x | x-- | --x
9947 binop:
9948 +, *, -, /, &, ^, |, <<, >>
9950 where x is an lvalue expression with scalar type.
9952 OpenMP 3.1:
9953 # pragma omp atomic new-line
9954 update-stmt
9956 # pragma omp atomic read new-line
9957 read-stmt
9959 # pragma omp atomic write new-line
9960 write-stmt
9962 # pragma omp atomic update new-line
9963 update-stmt
9965 # pragma omp atomic capture new-line
9966 capture-stmt
9968 # pragma omp atomic capture new-line
9969 capture-block
9971 read-stmt:
9972 v = x
9973 write-stmt:
9974 x = expr
9975 update-stmt:
9976 expression-stmt | x = x binop expr
9977 capture-stmt:
9978 v = x binop= expr | v = x++ | v = ++x | v = x-- | v = --x
9979 capture-block:
9980 { v = x; update-stmt; } | { update-stmt; v = x; }
9982 where x and v are lvalue expressions with scalar type.
9984 LOC is the location of the #pragma token. */
9986 static void
9987 c_parser_omp_atomic (location_t loc, c_parser *parser)
9989 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE;
9990 tree lhs1 = NULL_TREE, rhs1 = NULL_TREE;
9991 tree stmt, orig_lhs;
9992 enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
9993 struct c_expr rhs_expr;
9994 bool structured_block = false;
9996 if (c_parser_next_token_is (parser, CPP_NAME))
9998 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
10000 if (!strcmp (p, "read"))
10001 code = OMP_ATOMIC_READ;
10002 else if (!strcmp (p, "write"))
10003 code = NOP_EXPR;
10004 else if (!strcmp (p, "update"))
10005 code = OMP_ATOMIC;
10006 else if (!strcmp (p, "capture"))
10007 code = OMP_ATOMIC_CAPTURE_NEW;
10008 else
10009 p = NULL;
10010 if (p)
10011 c_parser_consume_token (parser);
10013 c_parser_skip_to_pragma_eol (parser);
10015 switch (code)
10017 case OMP_ATOMIC_READ:
10018 case NOP_EXPR: /* atomic write */
10019 v = c_parser_unary_expression (parser).value;
10020 v = c_fully_fold (v, false, NULL);
10021 if (v == error_mark_node)
10022 goto saw_error;
10023 loc = c_parser_peek_token (parser)->location;
10024 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
10025 goto saw_error;
10026 if (code == NOP_EXPR)
10027 lhs = c_parser_expression (parser).value;
10028 else
10029 lhs = c_parser_unary_expression (parser).value;
10030 lhs = c_fully_fold (lhs, false, NULL);
10031 if (lhs == error_mark_node)
10032 goto saw_error;
10033 if (code == NOP_EXPR)
10035 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
10036 opcode. */
10037 code = OMP_ATOMIC;
10038 rhs = lhs;
10039 lhs = v;
10040 v = NULL_TREE;
10042 goto done;
10043 case OMP_ATOMIC_CAPTURE_NEW:
10044 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
10046 c_parser_consume_token (parser);
10047 structured_block = true;
10049 else
10051 v = c_parser_unary_expression (parser).value;
10052 v = c_fully_fold (v, false, NULL);
10053 if (v == error_mark_node)
10054 goto saw_error;
10055 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
10056 goto saw_error;
10058 break;
10059 default:
10060 break;
10063 /* For structured_block case we don't know yet whether
10064 old or new x should be captured. */
10065 restart:
10066 lhs = c_parser_unary_expression (parser).value;
10067 lhs = c_fully_fold (lhs, false, NULL);
10068 orig_lhs = lhs;
10069 switch (TREE_CODE (lhs))
10071 case ERROR_MARK:
10072 saw_error:
10073 c_parser_skip_to_end_of_block_or_statement (parser);
10074 if (structured_block)
10076 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
10077 c_parser_consume_token (parser);
10078 else if (code == OMP_ATOMIC_CAPTURE_NEW)
10080 c_parser_skip_to_end_of_block_or_statement (parser);
10081 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
10082 c_parser_consume_token (parser);
10085 return;
10087 case POSTINCREMENT_EXPR:
10088 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
10089 code = OMP_ATOMIC_CAPTURE_OLD;
10090 /* FALLTHROUGH */
10091 case PREINCREMENT_EXPR:
10092 lhs = TREE_OPERAND (lhs, 0);
10093 opcode = PLUS_EXPR;
10094 rhs = integer_one_node;
10095 break;
10097 case POSTDECREMENT_EXPR:
10098 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
10099 code = OMP_ATOMIC_CAPTURE_OLD;
10100 /* FALLTHROUGH */
10101 case PREDECREMENT_EXPR:
10102 lhs = TREE_OPERAND (lhs, 0);
10103 opcode = MINUS_EXPR;
10104 rhs = integer_one_node;
10105 break;
10107 case COMPOUND_EXPR:
10108 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
10109 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
10110 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
10111 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
10112 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
10113 (TREE_OPERAND (lhs, 1), 0), 0)))
10114 == BOOLEAN_TYPE)
10115 /* Undo effects of boolean_increment for post {in,de}crement. */
10116 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
10117 /* FALLTHRU */
10118 case MODIFY_EXPR:
10119 if (TREE_CODE (lhs) == MODIFY_EXPR
10120 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
10122 /* Undo effects of boolean_increment. */
10123 if (integer_onep (TREE_OPERAND (lhs, 1)))
10125 /* This is pre or post increment. */
10126 rhs = TREE_OPERAND (lhs, 1);
10127 lhs = TREE_OPERAND (lhs, 0);
10128 opcode = NOP_EXPR;
10129 if (code == OMP_ATOMIC_CAPTURE_NEW
10130 && !structured_block
10131 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
10132 code = OMP_ATOMIC_CAPTURE_OLD;
10133 break;
10135 if (TREE_CODE (TREE_OPERAND (lhs, 1)) == TRUTH_NOT_EXPR
10136 && TREE_OPERAND (lhs, 0)
10137 == TREE_OPERAND (TREE_OPERAND (lhs, 1), 0))
10139 /* This is pre or post decrement. */
10140 rhs = TREE_OPERAND (lhs, 1);
10141 lhs = TREE_OPERAND (lhs, 0);
10142 opcode = NOP_EXPR;
10143 if (code == OMP_ATOMIC_CAPTURE_NEW
10144 && !structured_block
10145 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
10146 code = OMP_ATOMIC_CAPTURE_OLD;
10147 break;
10150 /* FALLTHRU */
10151 default:
10152 switch (c_parser_peek_token (parser)->type)
10154 case CPP_MULT_EQ:
10155 opcode = MULT_EXPR;
10156 break;
10157 case CPP_DIV_EQ:
10158 opcode = TRUNC_DIV_EXPR;
10159 break;
10160 case CPP_PLUS_EQ:
10161 opcode = PLUS_EXPR;
10162 break;
10163 case CPP_MINUS_EQ:
10164 opcode = MINUS_EXPR;
10165 break;
10166 case CPP_LSHIFT_EQ:
10167 opcode = LSHIFT_EXPR;
10168 break;
10169 case CPP_RSHIFT_EQ:
10170 opcode = RSHIFT_EXPR;
10171 break;
10172 case CPP_AND_EQ:
10173 opcode = BIT_AND_EXPR;
10174 break;
10175 case CPP_OR_EQ:
10176 opcode = BIT_IOR_EXPR;
10177 break;
10178 case CPP_XOR_EQ:
10179 opcode = BIT_XOR_EXPR;
10180 break;
10181 case CPP_EQ:
10182 if (structured_block || code == OMP_ATOMIC)
10184 location_t aloc = c_parser_peek_token (parser)->location;
10185 location_t rhs_loc;
10186 enum c_parser_prec oprec = PREC_NONE;
10188 c_parser_consume_token (parser);
10189 rhs1 = c_parser_unary_expression (parser).value;
10190 rhs1 = c_fully_fold (rhs1, false, NULL);
10191 if (rhs1 == error_mark_node)
10192 goto saw_error;
10193 switch (c_parser_peek_token (parser)->type)
10195 case CPP_SEMICOLON:
10196 if (code == OMP_ATOMIC_CAPTURE_NEW)
10198 code = OMP_ATOMIC_CAPTURE_OLD;
10199 v = lhs;
10200 lhs = NULL_TREE;
10201 lhs1 = rhs1;
10202 rhs1 = NULL_TREE;
10203 c_parser_consume_token (parser);
10204 goto restart;
10206 c_parser_error (parser,
10207 "invalid form of %<#pragma omp atomic%>");
10208 goto saw_error;
10209 case CPP_MULT:
10210 opcode = MULT_EXPR;
10211 oprec = PREC_MULT;
10212 break;
10213 case CPP_DIV:
10214 opcode = TRUNC_DIV_EXPR;
10215 oprec = PREC_MULT;
10216 break;
10217 case CPP_PLUS:
10218 opcode = PLUS_EXPR;
10219 oprec = PREC_ADD;
10220 break;
10221 case CPP_MINUS:
10222 opcode = MINUS_EXPR;
10223 oprec = PREC_ADD;
10224 break;
10225 case CPP_LSHIFT:
10226 opcode = LSHIFT_EXPR;
10227 oprec = PREC_SHIFT;
10228 break;
10229 case CPP_RSHIFT:
10230 opcode = RSHIFT_EXPR;
10231 oprec = PREC_SHIFT;
10232 break;
10233 case CPP_AND:
10234 opcode = BIT_AND_EXPR;
10235 oprec = PREC_BITAND;
10236 break;
10237 case CPP_OR:
10238 opcode = BIT_IOR_EXPR;
10239 oprec = PREC_BITOR;
10240 break;
10241 case CPP_XOR:
10242 opcode = BIT_XOR_EXPR;
10243 oprec = PREC_BITXOR;
10244 break;
10245 default:
10246 c_parser_error (parser,
10247 "invalid operator for %<#pragma omp atomic%>");
10248 goto saw_error;
10250 loc = aloc;
10251 c_parser_consume_token (parser);
10252 rhs_loc = c_parser_peek_token (parser)->location;
10253 if (commutative_tree_code (opcode))
10254 oprec = (enum c_parser_prec) (oprec - 1);
10255 rhs_expr = c_parser_binary_expression (parser, NULL, oprec);
10256 rhs_expr = default_function_array_read_conversion (rhs_loc,
10257 rhs_expr);
10258 rhs = rhs_expr.value;
10259 rhs = c_fully_fold (rhs, false, NULL);
10260 goto stmt_done;
10262 /* FALLTHROUGH */
10263 default:
10264 c_parser_error (parser,
10265 "invalid operator for %<#pragma omp atomic%>");
10266 goto saw_error;
10269 /* Arrange to pass the location of the assignment operator to
10270 c_finish_omp_atomic. */
10271 loc = c_parser_peek_token (parser)->location;
10272 c_parser_consume_token (parser);
10274 location_t rhs_loc = c_parser_peek_token (parser)->location;
10275 rhs_expr = c_parser_expression (parser);
10276 rhs_expr = default_function_array_read_conversion (rhs_loc, rhs_expr);
10278 rhs = rhs_expr.value;
10279 rhs = c_fully_fold (rhs, false, NULL);
10280 break;
10282 stmt_done:
10283 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
10285 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
10286 goto saw_error;
10287 v = c_parser_unary_expression (parser).value;
10288 v = c_fully_fold (v, false, NULL);
10289 if (v == error_mark_node)
10290 goto saw_error;
10291 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
10292 goto saw_error;
10293 lhs1 = c_parser_unary_expression (parser).value;
10294 lhs1 = c_fully_fold (lhs1, false, NULL);
10295 if (lhs1 == error_mark_node)
10296 goto saw_error;
10298 if (structured_block)
10300 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
10301 c_parser_require (parser, CPP_CLOSE_BRACE, "expected %<}%>");
10303 done:
10304 stmt = c_finish_omp_atomic (loc, code, opcode, lhs, rhs, v, lhs1, rhs1);
10305 if (stmt != error_mark_node)
10306 add_stmt (stmt);
10308 if (!structured_block)
10309 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
10313 /* OpenMP 2.5:
10314 # pragma omp barrier new-line
10317 static void
10318 c_parser_omp_barrier (c_parser *parser)
10320 location_t loc = c_parser_peek_token (parser)->location;
10321 c_parser_consume_pragma (parser);
10322 c_parser_skip_to_pragma_eol (parser);
10324 c_finish_omp_barrier (loc);
10327 /* OpenMP 2.5:
10328 # pragma omp critical [(name)] new-line
10329 structured-block
10331 LOC is the location of the #pragma itself. */
10333 static tree
10334 c_parser_omp_critical (location_t loc, c_parser *parser)
10336 tree stmt, name = NULL;
10338 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
10340 c_parser_consume_token (parser);
10341 if (c_parser_next_token_is (parser, CPP_NAME))
10343 name = c_parser_peek_token (parser)->value;
10344 c_parser_consume_token (parser);
10345 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10347 else
10348 c_parser_error (parser, "expected identifier");
10350 else if (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
10351 c_parser_error (parser, "expected %<(%> or end of line");
10352 c_parser_skip_to_pragma_eol (parser);
10354 stmt = c_parser_omp_structured_block (parser);
10355 return c_finish_omp_critical (loc, stmt, name);
10358 /* OpenMP 2.5:
10359 # pragma omp flush flush-vars[opt] new-line
10361 flush-vars:
10362 ( variable-list ) */
10364 static void
10365 c_parser_omp_flush (c_parser *parser)
10367 location_t loc = c_parser_peek_token (parser)->location;
10368 c_parser_consume_pragma (parser);
10369 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
10370 c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
10371 else if (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
10372 c_parser_error (parser, "expected %<(%> or end of line");
10373 c_parser_skip_to_pragma_eol (parser);
10375 c_finish_omp_flush (loc);
10378 /* Parse the restricted form of the for statement allowed by OpenMP.
10379 The real trick here is to determine the loop control variable early
10380 so that we can push a new decl if necessary to make it private.
10381 LOC is the location of the OMP in "#pragma omp". */
10383 static tree
10384 c_parser_omp_for_loop (location_t loc,
10385 c_parser *parser, tree clauses, tree *par_clauses)
10387 tree decl, cond, incr, save_break, save_cont, body, init, stmt, cl;
10388 tree declv, condv, incrv, initv, ret = NULL;
10389 bool fail = false, open_brace_parsed = false;
10390 int i, collapse = 1, nbraces = 0;
10391 location_t for_loc;
10392 vec<tree, va_gc> *for_block = make_tree_vector ();
10394 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
10395 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
10396 collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
10398 gcc_assert (collapse >= 1);
10400 declv = make_tree_vec (collapse);
10401 initv = make_tree_vec (collapse);
10402 condv = make_tree_vec (collapse);
10403 incrv = make_tree_vec (collapse);
10405 if (!c_parser_next_token_is_keyword (parser, RID_FOR))
10407 c_parser_error (parser, "for statement expected");
10408 return NULL;
10410 for_loc = c_parser_peek_token (parser)->location;
10411 c_parser_consume_token (parser);
10413 for (i = 0; i < collapse; i++)
10415 int bracecount = 0;
10417 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10418 goto pop_scopes;
10420 /* Parse the initialization declaration or expression. */
10421 if (c_parser_next_tokens_start_declaration (parser))
10423 if (i > 0)
10424 vec_safe_push (for_block, c_begin_compound_stmt (true));
10425 c_parser_declaration_or_fndef (parser, true, true, true, true, true, NULL);
10426 decl = check_for_loop_decls (for_loc, flag_isoc99);
10427 if (decl == NULL)
10428 goto error_init;
10429 if (DECL_INITIAL (decl) == error_mark_node)
10430 decl = error_mark_node;
10431 init = decl;
10433 else if (c_parser_next_token_is (parser, CPP_NAME)
10434 && c_parser_peek_2nd_token (parser)->type == CPP_EQ)
10436 struct c_expr decl_exp;
10437 struct c_expr init_exp;
10438 location_t init_loc;
10440 decl_exp = c_parser_postfix_expression (parser);
10441 decl = decl_exp.value;
10443 c_parser_require (parser, CPP_EQ, "expected %<=%>");
10445 init_loc = c_parser_peek_token (parser)->location;
10446 init_exp = c_parser_expr_no_commas (parser, NULL);
10447 init_exp = default_function_array_read_conversion (init_loc,
10448 init_exp);
10449 init = build_modify_expr (init_loc, decl, decl_exp.original_type,
10450 NOP_EXPR, init_loc, init_exp.value,
10451 init_exp.original_type);
10452 init = c_process_expr_stmt (init_loc, init);
10454 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
10456 else
10458 error_init:
10459 c_parser_error (parser,
10460 "expected iteration declaration or initialization");
10461 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
10462 "expected %<)%>");
10463 fail = true;
10464 goto parse_next;
10467 /* Parse the loop condition. */
10468 cond = NULL_TREE;
10469 if (c_parser_next_token_is_not (parser, CPP_SEMICOLON))
10471 location_t cond_loc = c_parser_peek_token (parser)->location;
10472 struct c_expr cond_expr = c_parser_binary_expression (parser, NULL,
10473 PREC_NONE);
10475 cond = cond_expr.value;
10476 cond = c_objc_common_truthvalue_conversion (cond_loc, cond);
10477 cond = c_fully_fold (cond, false, NULL);
10478 switch (cond_expr.original_code)
10480 case GT_EXPR:
10481 case GE_EXPR:
10482 case LT_EXPR:
10483 case LE_EXPR:
10484 break;
10485 default:
10486 /* Can't be cond = error_mark_node, because we want to preserve
10487 the location until c_finish_omp_for. */
10488 cond = build1 (NOP_EXPR, boolean_type_node, error_mark_node);
10489 break;
10491 protected_set_expr_location (cond, cond_loc);
10493 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
10495 /* Parse the increment expression. */
10496 incr = NULL_TREE;
10497 if (c_parser_next_token_is_not (parser, CPP_CLOSE_PAREN))
10499 location_t incr_loc = c_parser_peek_token (parser)->location;
10501 incr = c_process_expr_stmt (incr_loc,
10502 c_parser_expression (parser).value);
10504 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10506 if (decl == NULL || decl == error_mark_node || init == error_mark_node)
10507 fail = true;
10508 else
10510 TREE_VEC_ELT (declv, i) = decl;
10511 TREE_VEC_ELT (initv, i) = init;
10512 TREE_VEC_ELT (condv, i) = cond;
10513 TREE_VEC_ELT (incrv, i) = incr;
10516 parse_next:
10517 if (i == collapse - 1)
10518 break;
10520 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
10521 in between the collapsed for loops to be still considered perfectly
10522 nested. Hopefully the final version clarifies this.
10523 For now handle (multiple) {'s and empty statements. */
10526 if (c_parser_next_token_is_keyword (parser, RID_FOR))
10528 c_parser_consume_token (parser);
10529 break;
10531 else if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
10533 c_parser_consume_token (parser);
10534 bracecount++;
10536 else if (bracecount
10537 && c_parser_next_token_is (parser, CPP_SEMICOLON))
10538 c_parser_consume_token (parser);
10539 else
10541 c_parser_error (parser, "not enough perfectly nested loops");
10542 if (bracecount)
10544 open_brace_parsed = true;
10545 bracecount--;
10547 fail = true;
10548 collapse = 0;
10549 break;
10552 while (1);
10554 nbraces += bracecount;
10557 save_break = c_break_label;
10558 c_break_label = size_one_node;
10559 save_cont = c_cont_label;
10560 c_cont_label = NULL_TREE;
10561 body = push_stmt_list ();
10563 if (open_brace_parsed)
10565 location_t here = c_parser_peek_token (parser)->location;
10566 stmt = c_begin_compound_stmt (true);
10567 c_parser_compound_statement_nostart (parser);
10568 add_stmt (c_end_compound_stmt (here, stmt, true));
10570 else
10571 add_stmt (c_parser_c99_block_statement (parser));
10572 if (c_cont_label)
10574 tree t = build1 (LABEL_EXPR, void_type_node, c_cont_label);
10575 SET_EXPR_LOCATION (t, loc);
10576 add_stmt (t);
10579 body = pop_stmt_list (body);
10580 c_break_label = save_break;
10581 c_cont_label = save_cont;
10583 while (nbraces)
10585 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
10587 c_parser_consume_token (parser);
10588 nbraces--;
10590 else if (c_parser_next_token_is (parser, CPP_SEMICOLON))
10591 c_parser_consume_token (parser);
10592 else
10594 c_parser_error (parser, "collapsed loops not perfectly nested");
10595 while (nbraces)
10597 location_t here = c_parser_peek_token (parser)->location;
10598 stmt = c_begin_compound_stmt (true);
10599 add_stmt (body);
10600 c_parser_compound_statement_nostart (parser);
10601 body = c_end_compound_stmt (here, stmt, true);
10602 nbraces--;
10604 goto pop_scopes;
10608 /* Only bother calling c_finish_omp_for if we haven't already generated
10609 an error from the initialization parsing. */
10610 if (!fail)
10612 stmt = c_finish_omp_for (loc, declv, initv, condv, incrv, body, NULL);
10613 if (stmt)
10615 if (par_clauses != NULL)
10617 tree *c;
10618 for (c = par_clauses; *c ; )
10619 if (OMP_CLAUSE_CODE (*c) != OMP_CLAUSE_FIRSTPRIVATE
10620 && OMP_CLAUSE_CODE (*c) != OMP_CLAUSE_LASTPRIVATE)
10621 c = &OMP_CLAUSE_CHAIN (*c);
10622 else
10624 for (i = 0; i < collapse; i++)
10625 if (TREE_VEC_ELT (declv, i) == OMP_CLAUSE_DECL (*c))
10626 break;
10627 if (i == collapse)
10628 c = &OMP_CLAUSE_CHAIN (*c);
10629 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE)
10631 error_at (loc,
10632 "iteration variable %qD should not be firstprivate",
10633 OMP_CLAUSE_DECL (*c));
10634 *c = OMP_CLAUSE_CHAIN (*c);
10636 else
10638 /* Copy lastprivate (decl) clause to OMP_FOR_CLAUSES,
10639 change it to shared (decl) in
10640 OMP_PARALLEL_CLAUSES. */
10641 tree l = build_omp_clause (OMP_CLAUSE_LOCATION (*c),
10642 OMP_CLAUSE_LASTPRIVATE);
10643 OMP_CLAUSE_DECL (l) = OMP_CLAUSE_DECL (*c);
10644 OMP_CLAUSE_CHAIN (l) = clauses;
10645 clauses = l;
10646 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
10650 OMP_FOR_CLAUSES (stmt) = clauses;
10652 ret = stmt;
10654 pop_scopes:
10655 while (!for_block->is_empty ())
10657 /* FIXME diagnostics: LOC below should be the actual location of
10658 this particular for block. We need to build a list of
10659 locations to go along with FOR_BLOCK. */
10660 stmt = c_end_compound_stmt (loc, for_block->pop (), true);
10661 add_stmt (stmt);
10663 release_tree_vector (for_block);
10664 return ret;
10667 /* OpenMP 2.5:
10668 #pragma omp for for-clause[optseq] new-line
10669 for-loop
10671 LOC is the location of the #pragma token.
10674 #define OMP_FOR_CLAUSE_MASK \
10675 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
10676 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
10677 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
10678 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
10679 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \
10680 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \
10681 | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE) \
10682 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
10684 static tree
10685 c_parser_omp_for (location_t loc, c_parser *parser)
10687 tree block, clauses, ret;
10689 clauses = c_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
10690 "#pragma omp for");
10692 block = c_begin_compound_stmt (true);
10693 ret = c_parser_omp_for_loop (loc, parser, clauses, NULL);
10694 block = c_end_compound_stmt (loc, block, true);
10695 add_stmt (block);
10697 return ret;
10700 /* OpenMP 2.5:
10701 # pragma omp master new-line
10702 structured-block
10704 LOC is the location of the #pragma token.
10707 static tree
10708 c_parser_omp_master (location_t loc, c_parser *parser)
10710 c_parser_skip_to_pragma_eol (parser);
10711 return c_finish_omp_master (loc, c_parser_omp_structured_block (parser));
10714 /* OpenMP 2.5:
10715 # pragma omp ordered new-line
10716 structured-block
10718 LOC is the location of the #pragma itself.
10721 static tree
10722 c_parser_omp_ordered (location_t loc, c_parser *parser)
10724 c_parser_skip_to_pragma_eol (parser);
10725 return c_finish_omp_ordered (loc, c_parser_omp_structured_block (parser));
10728 /* OpenMP 2.5:
10730 section-scope:
10731 { section-sequence }
10733 section-sequence:
10734 section-directive[opt] structured-block
10735 section-sequence section-directive structured-block
10737 SECTIONS_LOC is the location of the #pragma omp sections. */
10739 static tree
10740 c_parser_omp_sections_scope (location_t sections_loc, c_parser *parser)
10742 tree stmt, substmt;
10743 bool error_suppress = false;
10744 location_t loc;
10746 loc = c_parser_peek_token (parser)->location;
10747 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
10749 /* Avoid skipping until the end of the block. */
10750 parser->error = false;
10751 return NULL_TREE;
10754 stmt = push_stmt_list ();
10756 if (c_parser_peek_token (parser)->pragma_kind != PRAGMA_OMP_SECTION)
10758 substmt = push_stmt_list ();
10760 while (1)
10762 c_parser_statement (parser);
10764 if (c_parser_peek_token (parser)->pragma_kind == PRAGMA_OMP_SECTION)
10765 break;
10766 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
10767 break;
10768 if (c_parser_next_token_is (parser, CPP_EOF))
10769 break;
10772 substmt = pop_stmt_list (substmt);
10773 substmt = build1 (OMP_SECTION, void_type_node, substmt);
10774 SET_EXPR_LOCATION (substmt, loc);
10775 add_stmt (substmt);
10778 while (1)
10780 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
10781 break;
10782 if (c_parser_next_token_is (parser, CPP_EOF))
10783 break;
10785 loc = c_parser_peek_token (parser)->location;
10786 if (c_parser_peek_token (parser)->pragma_kind == PRAGMA_OMP_SECTION)
10788 c_parser_consume_pragma (parser);
10789 c_parser_skip_to_pragma_eol (parser);
10790 error_suppress = false;
10792 else if (!error_suppress)
10794 error_at (loc, "expected %<#pragma omp section%> or %<}%>");
10795 error_suppress = true;
10798 substmt = c_parser_omp_structured_block (parser);
10799 substmt = build1 (OMP_SECTION, void_type_node, substmt);
10800 SET_EXPR_LOCATION (substmt, loc);
10801 add_stmt (substmt);
10803 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE,
10804 "expected %<#pragma omp section%> or %<}%>");
10806 substmt = pop_stmt_list (stmt);
10808 stmt = make_node (OMP_SECTIONS);
10809 SET_EXPR_LOCATION (stmt, sections_loc);
10810 TREE_TYPE (stmt) = void_type_node;
10811 OMP_SECTIONS_BODY (stmt) = substmt;
10813 return add_stmt (stmt);
10816 /* OpenMP 2.5:
10817 # pragma omp sections sections-clause[optseq] newline
10818 sections-scope
10820 LOC is the location of the #pragma token.
10823 #define OMP_SECTIONS_CLAUSE_MASK \
10824 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
10825 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
10826 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
10827 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
10828 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
10830 static tree
10831 c_parser_omp_sections (location_t loc, c_parser *parser)
10833 tree block, clauses, ret;
10835 clauses = c_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
10836 "#pragma omp sections");
10838 block = c_begin_compound_stmt (true);
10839 ret = c_parser_omp_sections_scope (loc, parser);
10840 if (ret)
10841 OMP_SECTIONS_CLAUSES (ret) = clauses;
10842 block = c_end_compound_stmt (loc, block, true);
10843 add_stmt (block);
10845 return ret;
10848 /* OpenMP 2.5:
10849 # pragma parallel parallel-clause new-line
10850 # pragma parallel for parallel-for-clause new-line
10851 # pragma parallel sections parallel-sections-clause new-line
10853 LOC is the location of the #pragma token.
10856 #define OMP_PARALLEL_CLAUSE_MASK \
10857 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
10858 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
10859 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
10860 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
10861 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
10862 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \
10863 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
10864 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
10866 static tree
10867 c_parser_omp_parallel (location_t loc, c_parser *parser)
10869 enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
10870 const char *p_name = "#pragma omp parallel";
10871 tree stmt, clauses, par_clause, ws_clause, block;
10872 unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
10874 if (c_parser_next_token_is_keyword (parser, RID_FOR))
10876 c_parser_consume_token (parser);
10877 p_kind = PRAGMA_OMP_PARALLEL_FOR;
10878 p_name = "#pragma omp parallel for";
10879 mask |= OMP_FOR_CLAUSE_MASK;
10880 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
10882 else if (c_parser_next_token_is (parser, CPP_NAME))
10884 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
10885 if (strcmp (p, "sections") == 0)
10887 c_parser_consume_token (parser);
10888 p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
10889 p_name = "#pragma omp parallel sections";
10890 mask |= OMP_SECTIONS_CLAUSE_MASK;
10891 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
10895 clauses = c_parser_omp_all_clauses (parser, mask, p_name);
10897 switch (p_kind)
10899 case PRAGMA_OMP_PARALLEL:
10900 block = c_begin_omp_parallel ();
10901 c_parser_statement (parser);
10902 stmt = c_finish_omp_parallel (loc, clauses, block);
10903 break;
10905 case PRAGMA_OMP_PARALLEL_FOR:
10906 block = c_begin_omp_parallel ();
10907 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
10908 c_parser_omp_for_loop (loc, parser, ws_clause, &par_clause);
10909 stmt = c_finish_omp_parallel (loc, par_clause, block);
10910 OMP_PARALLEL_COMBINED (stmt) = 1;
10911 break;
10913 case PRAGMA_OMP_PARALLEL_SECTIONS:
10914 block = c_begin_omp_parallel ();
10915 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
10916 stmt = c_parser_omp_sections_scope (loc, parser);
10917 if (stmt)
10918 OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
10919 stmt = c_finish_omp_parallel (loc, par_clause, block);
10920 OMP_PARALLEL_COMBINED (stmt) = 1;
10921 break;
10923 default:
10924 gcc_unreachable ();
10927 return stmt;
10930 /* OpenMP 2.5:
10931 # pragma omp single single-clause[optseq] new-line
10932 structured-block
10934 LOC is the location of the #pragma.
10937 #define OMP_SINGLE_CLAUSE_MASK \
10938 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
10939 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
10940 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
10941 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
10943 static tree
10944 c_parser_omp_single (location_t loc, c_parser *parser)
10946 tree stmt = make_node (OMP_SINGLE);
10947 SET_EXPR_LOCATION (stmt, loc);
10948 TREE_TYPE (stmt) = void_type_node;
10950 OMP_SINGLE_CLAUSES (stmt)
10951 = c_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
10952 "#pragma omp single");
10953 OMP_SINGLE_BODY (stmt) = c_parser_omp_structured_block (parser);
10955 return add_stmt (stmt);
10958 /* OpenMP 3.0:
10959 # pragma omp task task-clause[optseq] new-line
10961 LOC is the location of the #pragma.
10964 #define OMP_TASK_CLAUSE_MASK \
10965 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
10966 | (1u << PRAGMA_OMP_CLAUSE_UNTIED) \
10967 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
10968 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
10969 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
10970 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
10971 | (1u << PRAGMA_OMP_CLAUSE_FINAL) \
10972 | (1u << PRAGMA_OMP_CLAUSE_MERGEABLE))
10974 static tree
10975 c_parser_omp_task (location_t loc, c_parser *parser)
10977 tree clauses, block;
10979 clauses = c_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
10980 "#pragma omp task");
10982 block = c_begin_omp_task ();
10983 c_parser_statement (parser);
10984 return c_finish_omp_task (loc, clauses, block);
10987 /* OpenMP 3.0:
10988 # pragma omp taskwait new-line
10991 static void
10992 c_parser_omp_taskwait (c_parser *parser)
10994 location_t loc = c_parser_peek_token (parser)->location;
10995 c_parser_consume_pragma (parser);
10996 c_parser_skip_to_pragma_eol (parser);
10998 c_finish_omp_taskwait (loc);
11001 /* OpenMP 3.1:
11002 # pragma omp taskyield new-line
11005 static void
11006 c_parser_omp_taskyield (c_parser *parser)
11008 location_t loc = c_parser_peek_token (parser)->location;
11009 c_parser_consume_pragma (parser);
11010 c_parser_skip_to_pragma_eol (parser);
11012 c_finish_omp_taskyield (loc);
11015 /* Main entry point to parsing most OpenMP pragmas. */
11017 static void
11018 c_parser_omp_construct (c_parser *parser)
11020 enum pragma_kind p_kind;
11021 location_t loc;
11022 tree stmt;
11024 loc = c_parser_peek_token (parser)->location;
11025 p_kind = c_parser_peek_token (parser)->pragma_kind;
11026 c_parser_consume_pragma (parser);
11028 switch (p_kind)
11030 case PRAGMA_OMP_ATOMIC:
11031 c_parser_omp_atomic (loc, parser);
11032 return;
11033 case PRAGMA_OMP_CRITICAL:
11034 stmt = c_parser_omp_critical (loc, parser);
11035 break;
11036 case PRAGMA_OMP_FOR:
11037 stmt = c_parser_omp_for (loc, parser);
11038 break;
11039 case PRAGMA_OMP_MASTER:
11040 stmt = c_parser_omp_master (loc, parser);
11041 break;
11042 case PRAGMA_OMP_ORDERED:
11043 stmt = c_parser_omp_ordered (loc, parser);
11044 break;
11045 case PRAGMA_OMP_PARALLEL:
11046 stmt = c_parser_omp_parallel (loc, parser);
11047 break;
11048 case PRAGMA_OMP_SECTIONS:
11049 stmt = c_parser_omp_sections (loc, parser);
11050 break;
11051 case PRAGMA_OMP_SINGLE:
11052 stmt = c_parser_omp_single (loc, parser);
11053 break;
11054 case PRAGMA_OMP_TASK:
11055 stmt = c_parser_omp_task (loc, parser);
11056 break;
11057 default:
11058 gcc_unreachable ();
11061 if (stmt)
11062 gcc_assert (EXPR_LOCATION (stmt) != UNKNOWN_LOCATION);
11066 /* OpenMP 2.5:
11067 # pragma omp threadprivate (variable-list) */
11069 static void
11070 c_parser_omp_threadprivate (c_parser *parser)
11072 tree vars, t;
11073 location_t loc;
11075 c_parser_consume_pragma (parser);
11076 loc = c_parser_peek_token (parser)->location;
11077 vars = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
11079 /* Mark every variable in VARS to be assigned thread local storage. */
11080 for (t = vars; t; t = TREE_CHAIN (t))
11082 tree v = TREE_PURPOSE (t);
11084 /* FIXME diagnostics: Ideally we should keep individual
11085 locations for all the variables in the var list to make the
11086 following errors more precise. Perhaps
11087 c_parser_omp_var_list_parens() should construct a list of
11088 locations to go along with the var list. */
11090 /* If V had already been marked threadprivate, it doesn't matter
11091 whether it had been used prior to this point. */
11092 if (TREE_CODE (v) != VAR_DECL)
11093 error_at (loc, "%qD is not a variable", v);
11094 else if (TREE_USED (v) && !C_DECL_THREADPRIVATE_P (v))
11095 error_at (loc, "%qE declared %<threadprivate%> after first use", v);
11096 else if (! TREE_STATIC (v) && ! DECL_EXTERNAL (v))
11097 error_at (loc, "automatic variable %qE cannot be %<threadprivate%>", v);
11098 else if (TREE_TYPE (v) == error_mark_node)
11100 else if (! COMPLETE_TYPE_P (TREE_TYPE (v)))
11101 error_at (loc, "%<threadprivate%> %qE has incomplete type", v);
11102 else
11104 if (! DECL_THREAD_LOCAL_P (v))
11106 DECL_TLS_MODEL (v) = decl_default_tls_model (v);
11107 /* If rtl has been already set for this var, call
11108 make_decl_rtl once again, so that encode_section_info
11109 has a chance to look at the new decl flags. */
11110 if (DECL_RTL_SET_P (v))
11111 make_decl_rtl (v);
11113 C_DECL_THREADPRIVATE_P (v) = 1;
11117 c_parser_skip_to_pragma_eol (parser);
11120 /* Parse a transaction attribute (GCC Extension).
11122 transaction-attribute:
11123 attributes
11124 [ [ any-word ] ]
11126 The transactional memory language description is written for C++,
11127 and uses the C++0x attribute syntax. For compatibility, allow the
11128 bracket style for transactions in C as well. */
11130 static tree
11131 c_parser_transaction_attributes (c_parser *parser)
11133 tree attr_name, attr = NULL;
11135 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
11136 return c_parser_attributes (parser);
11138 if (!c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
11139 return NULL_TREE;
11140 c_parser_consume_token (parser);
11141 if (!c_parser_require (parser, CPP_OPEN_SQUARE, "expected %<[%>"))
11142 goto error1;
11144 attr_name = c_parser_attribute_any_word (parser);
11145 if (attr_name)
11147 c_parser_consume_token (parser);
11148 attr = build_tree_list (attr_name, NULL_TREE);
11150 else
11151 c_parser_error (parser, "expected identifier");
11153 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, "expected %<]%>");
11154 error1:
11155 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, "expected %<]%>");
11156 return attr;
11159 /* Parse a __transaction_atomic or __transaction_relaxed statement
11160 (GCC Extension).
11162 transaction-statement:
11163 __transaction_atomic transaction-attribute[opt] compound-statement
11164 __transaction_relaxed compound-statement
11166 Note that the only valid attribute is: "outer".
11169 static tree
11170 c_parser_transaction (c_parser *parser, enum rid keyword)
11172 unsigned int old_in = parser->in_transaction;
11173 unsigned int this_in = 1, new_in;
11174 location_t loc = c_parser_peek_token (parser)->location;
11175 tree stmt, attrs;
11177 gcc_assert ((keyword == RID_TRANSACTION_ATOMIC
11178 || keyword == RID_TRANSACTION_RELAXED)
11179 && c_parser_next_token_is_keyword (parser, keyword));
11180 c_parser_consume_token (parser);
11182 if (keyword == RID_TRANSACTION_RELAXED)
11183 this_in |= TM_STMT_ATTR_RELAXED;
11184 else
11186 attrs = c_parser_transaction_attributes (parser);
11187 if (attrs)
11188 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
11191 /* Keep track if we're in the lexical scope of an outer transaction. */
11192 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
11194 parser->in_transaction = new_in;
11195 stmt = c_parser_compound_statement (parser);
11196 parser->in_transaction = old_in;
11198 if (flag_tm)
11199 stmt = c_finish_transaction (loc, stmt, this_in);
11200 else
11201 error_at (loc, (keyword == RID_TRANSACTION_ATOMIC ?
11202 "%<__transaction_atomic%> without transactional memory support enabled"
11203 : "%<__transaction_relaxed %> "
11204 "without transactional memory support enabled"));
11206 return stmt;
11209 /* Parse a __transaction_atomic or __transaction_relaxed expression
11210 (GCC Extension).
11212 transaction-expression:
11213 __transaction_atomic ( expression )
11214 __transaction_relaxed ( expression )
11217 static struct c_expr
11218 c_parser_transaction_expression (c_parser *parser, enum rid keyword)
11220 struct c_expr ret;
11221 unsigned int old_in = parser->in_transaction;
11222 unsigned int this_in = 1;
11223 location_t loc = c_parser_peek_token (parser)->location;
11224 tree attrs;
11226 gcc_assert ((keyword == RID_TRANSACTION_ATOMIC
11227 || keyword == RID_TRANSACTION_RELAXED)
11228 && c_parser_next_token_is_keyword (parser, keyword));
11229 c_parser_consume_token (parser);
11231 if (keyword == RID_TRANSACTION_RELAXED)
11232 this_in |= TM_STMT_ATTR_RELAXED;
11233 else
11235 attrs = c_parser_transaction_attributes (parser);
11236 if (attrs)
11237 this_in |= parse_tm_stmt_attr (attrs, 0);
11240 parser->in_transaction = this_in;
11241 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11243 tree expr = c_parser_expression (parser).value;
11244 ret.original_type = TREE_TYPE (expr);
11245 ret.value = build1 (TRANSACTION_EXPR, ret.original_type, expr);
11246 if (this_in & TM_STMT_ATTR_RELAXED)
11247 TRANSACTION_EXPR_RELAXED (ret.value) = 1;
11248 SET_EXPR_LOCATION (ret.value, loc);
11249 ret.original_code = TRANSACTION_EXPR;
11250 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
11252 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
11253 goto error;
11256 else
11258 error:
11259 ret.value = error_mark_node;
11260 ret.original_code = ERROR_MARK;
11261 ret.original_type = NULL;
11263 parser->in_transaction = old_in;
11265 if (!flag_tm)
11266 error_at (loc, (keyword == RID_TRANSACTION_ATOMIC ?
11267 "%<__transaction_atomic%> without transactional memory support enabled"
11268 : "%<__transaction_relaxed %> "
11269 "without transactional memory support enabled"));
11271 return ret;
11274 /* Parse a __transaction_cancel statement (GCC Extension).
11276 transaction-cancel-statement:
11277 __transaction_cancel transaction-attribute[opt] ;
11279 Note that the only valid attribute is "outer".
11282 static tree
11283 c_parser_transaction_cancel(c_parser *parser)
11285 location_t loc = c_parser_peek_token (parser)->location;
11286 tree attrs;
11287 bool is_outer = false;
11289 gcc_assert (c_parser_next_token_is_keyword (parser, RID_TRANSACTION_CANCEL));
11290 c_parser_consume_token (parser);
11292 attrs = c_parser_transaction_attributes (parser);
11293 if (attrs)
11294 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
11296 if (!flag_tm)
11298 error_at (loc, "%<__transaction_cancel%> without "
11299 "transactional memory support enabled");
11300 goto ret_error;
11302 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
11304 error_at (loc, "%<__transaction_cancel%> within a "
11305 "%<__transaction_relaxed%>");
11306 goto ret_error;
11308 else if (is_outer)
11310 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
11311 && !is_tm_may_cancel_outer (current_function_decl))
11313 error_at (loc, "outer %<__transaction_cancel%> not "
11314 "within outer %<__transaction_atomic%>");
11315 error_at (loc, " or a %<transaction_may_cancel_outer%> function");
11316 goto ret_error;
11319 else if (parser->in_transaction == 0)
11321 error_at (loc, "%<__transaction_cancel%> not within "
11322 "%<__transaction_atomic%>");
11323 goto ret_error;
11326 return add_stmt (build_tm_abort_call (loc, is_outer));
11328 ret_error:
11329 return build1 (NOP_EXPR, void_type_node, error_mark_node);
11332 /* Parse a single source file. */
11334 void
11335 c_parse_file (void)
11337 /* Use local storage to begin. If the first token is a pragma, parse it.
11338 If it is #pragma GCC pch_preprocess, then this will load a PCH file
11339 which will cause garbage collection. */
11340 c_parser tparser;
11342 memset (&tparser, 0, sizeof tparser);
11343 the_parser = &tparser;
11345 if (c_parser_peek_token (&tparser)->pragma_kind == PRAGMA_GCC_PCH_PREPROCESS)
11346 c_parser_pragma_pch_preprocess (&tparser);
11348 the_parser = ggc_alloc_c_parser ();
11349 *the_parser = tparser;
11351 /* Initialize EH, if we've been told to do so. */
11352 if (flag_exceptions)
11353 using_eh_for_cleanups ();
11355 c_parser_translation_unit (the_parser);
11356 the_parser = NULL;
11359 #include "gt-c-c-parser.h"