Merge trunk version 208955 into gupc branch.
[official-gcc.git] / gcc / c / c-parser.c
blobfc70583cd40d71a777f9a0cdf5dce49b9857bad1
1 /* Parser for C and Objective-C.
2 Copyright (C) 1987-2014 Free Software Foundation, Inc.
4 Parser actions based on the old Bison parser; structure somewhat
5 influenced by and fragments based on the C++ parser.
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 3, or (at your option) any later
12 version.
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 for more details.
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
23 /* TODO:
25 Make sure all relevant comments, and all relevant code from all
26 actions, brought over from old parser. Verify exact correspondence
27 of syntax accepted.
29 Add testcases covering every input symbol in every state in old and
30 new parsers.
32 Include full syntax for GNU C, including erroneous cases accepted
33 with error messages, in syntax productions in comments.
35 Make more diagnostics in the front end generally take an explicit
36 location rather than implicitly using input_location. */
38 #include "config.h"
39 #include "system.h"
40 #include "coretypes.h"
41 #include "tm.h" /* For rtl.h: needs enum reg_class. */
42 #include "tree.h"
43 #include "stringpool.h"
44 #include "attribs.h"
45 #include "stor-layout.h"
46 #include "varasm.h"
47 #include "trans-mem.h"
48 #include "langhooks.h"
49 #include "input.h"
50 #include "cpplib.h"
51 #include "timevar.h"
52 #include "c-family/c-pragma.h"
53 #include "c-tree.h"
54 #include "c-lang.h"
55 #include "flags.h"
56 #include "ggc.h"
57 #include "c-family/c-common.h"
58 #include "c-family/c-objc.h"
59 #include "c-family/c-upc.h"
60 #include "c-family/c-upc-gasp.h"
61 #include "c-family/c-upc-pts-ops.h"
62 #include "output.h"
63 #include "vec.h"
64 #include "target.h"
65 #include "cgraph.h"
66 #include "plugin.h"
67 #include "omp-low.h"
70 /* Initialization routine for this file. */
72 void
73 c_parse_init (void)
75 /* The only initialization required is of the reserved word
76 identifiers. */
77 unsigned int i;
78 tree id;
79 int mask = 0;
81 /* Make sure RID_MAX hasn't grown past the 8 bits used to hold the keyword in
82 the c_token structure. */
83 gcc_assert (RID_MAX <= 255);
85 mask |= D_CXXONLY;
86 if (!flag_isoc99)
87 mask |= D_C99;
88 if (flag_no_asm)
90 mask |= D_ASM | D_EXT;
91 if (!flag_isoc99)
92 mask |= D_EXT89;
94 if (!c_dialect_objc ())
95 mask |= D_OBJC | D_CXX_OBJC;
97 if (!flag_upc)
98 mask |= D_UPC;
100 ridpointers = ggc_alloc_cleared_vec_tree ((int) RID_MAX);
101 for (i = 0; i < num_c_common_reswords; i++)
103 /* If a keyword is disabled, do not enter it into the table
104 and so create a canonical spelling that isn't a keyword. */
105 if (c_common_reswords[i].disable & mask)
107 if (warn_cxx_compat
108 && (c_common_reswords[i].disable & D_CXXWARN))
110 id = get_identifier (c_common_reswords[i].word);
111 C_SET_RID_CODE (id, RID_CXX_COMPAT_WARN);
112 C_IS_RESERVED_WORD (id) = 1;
114 continue;
117 id = get_identifier (c_common_reswords[i].word);
118 C_SET_RID_CODE (id, c_common_reswords[i].rid);
119 C_IS_RESERVED_WORD (id) = 1;
120 ridpointers [(int) c_common_reswords[i].rid] = id;
124 /* The C lexer intermediates between the lexer in cpplib and c-lex.c
125 and the C parser. Unlike the C++ lexer, the parser structure
126 stores the lexer information instead of using a separate structure.
127 Identifiers are separated into ordinary identifiers, type names,
128 keywords and some other Objective-C types of identifiers, and some
129 look-ahead is maintained.
131 ??? It might be a good idea to lex the whole file up front (as for
132 C++). It would then be possible to share more of the C and C++
133 lexer code, if desired. */
135 /* The following local token type is used. */
137 /* A keyword. */
138 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
140 /* More information about the type of a CPP_NAME token. */
141 typedef enum c_id_kind {
142 /* An ordinary identifier. */
143 C_ID_ID,
144 /* An identifier declared as a typedef name. */
145 C_ID_TYPENAME,
146 /* An identifier declared as an Objective-C class name. */
147 C_ID_CLASSNAME,
148 /* An address space identifier. */
149 C_ID_ADDRSPACE,
150 /* Not an identifier. */
151 C_ID_NONE
152 } c_id_kind;
154 /* A single C token after string literal concatenation and conversion
155 of preprocessing tokens to tokens. */
156 typedef struct GTY (()) c_token {
157 /* The kind of token. */
158 ENUM_BITFIELD (cpp_ttype) type : 8;
159 /* If this token is a CPP_NAME, this value indicates whether also
160 declared as some kind of type. Otherwise, it is C_ID_NONE. */
161 ENUM_BITFIELD (c_id_kind) id_kind : 8;
162 /* If this token is a keyword, this value indicates which keyword.
163 Otherwise, this value is RID_MAX. */
164 ENUM_BITFIELD (rid) keyword : 8;
165 /* If this token is a CPP_PRAGMA, this indicates the pragma that
166 was seen. Otherwise it is PRAGMA_NONE. */
167 ENUM_BITFIELD (pragma_kind) pragma_kind : 8;
168 /* The location at which this token was found. */
169 location_t location;
170 /* The value associated with this token, if any. */
171 tree value;
172 } c_token;
174 /* A parser structure recording information about the state and
175 context of parsing. Includes lexer information with up to two
176 tokens of look-ahead; more are not needed for C. */
177 typedef struct GTY(()) c_parser {
178 /* The look-ahead tokens. */
179 c_token * GTY((skip)) tokens;
180 /* Buffer for look-ahead tokens. */
181 c_token tokens_buf[2];
182 /* How many look-ahead tokens are available (0, 1 or 2, or
183 more if parsing from pre-lexed tokens). */
184 unsigned int tokens_avail;
185 /* True if a syntax error is being recovered from; false otherwise.
186 c_parser_error sets this flag. It should clear this flag when
187 enough tokens have been consumed to recover from the error. */
188 BOOL_BITFIELD error : 1;
189 /* True if we're processing a pragma, and shouldn't automatically
190 consume CPP_PRAGMA_EOL. */
191 BOOL_BITFIELD in_pragma : 1;
192 /* True if we're parsing the outermost block of an if statement. */
193 BOOL_BITFIELD in_if_block : 1;
194 /* True if we want to lex an untranslated string. */
195 BOOL_BITFIELD lex_untranslated_string : 1;
197 /* Objective-C specific parser/lexer information. */
199 /* True if we are in a context where the Objective-C "PQ" keywords
200 are considered keywords. */
201 BOOL_BITFIELD objc_pq_context : 1;
202 /* True if we are parsing a (potential) Objective-C foreach
203 statement. This is set to true after we parsed 'for (' and while
204 we wait for 'in' or ';' to decide if it's a standard C for loop or an
205 Objective-C foreach loop. */
206 BOOL_BITFIELD objc_could_be_foreach_context : 1;
207 /* The following flag is needed to contextualize Objective-C lexical
208 analysis. In some cases (e.g., 'int NSObject;'), it is
209 undesirable to bind an identifier to an Objective-C class, even
210 if a class with that name exists. */
211 BOOL_BITFIELD objc_need_raw_identifier : 1;
212 /* Nonzero if we're processing a __transaction statement. The value
213 is 1 | TM_STMT_ATTR_*. */
214 unsigned int in_transaction : 4;
215 /* True if we are in a context where the Objective-C "Property attribute"
216 keywords are valid. */
217 BOOL_BITFIELD objc_property_attr_context : 1;
219 /* Cilk Plus specific parser/lexer information. */
221 /* Buffer to hold all the tokens from parsing the vector attribute for the
222 SIMD-enabled functions (formerly known as elemental functions). */
223 vec <c_token, va_gc> *cilk_simd_fn_tokens;
224 } c_parser;
227 /* The actual parser and external interface. ??? Does this need to be
228 garbage-collected? */
230 static GTY (()) c_parser *the_parser;
232 /* Read in and lex a single token, storing it in *TOKEN. */
234 static void
235 c_lex_one_token (c_parser *parser, c_token *token)
237 timevar_push (TV_LEX);
239 token->type = c_lex_with_flags (&token->value, &token->location, NULL,
240 (parser->lex_untranslated_string
241 ? C_LEX_STRING_NO_TRANSLATE : 0));
242 token->id_kind = C_ID_NONE;
243 token->keyword = RID_MAX;
244 token->pragma_kind = PRAGMA_NONE;
246 switch (token->type)
248 case CPP_NAME:
250 tree decl;
252 bool objc_force_identifier = parser->objc_need_raw_identifier;
253 if (c_dialect_objc ())
254 parser->objc_need_raw_identifier = false;
256 if (C_IS_RESERVED_WORD (token->value))
258 enum rid rid_code = C_RID_CODE (token->value);
260 if (rid_code == RID_CXX_COMPAT_WARN)
262 warning_at (token->location,
263 OPT_Wc___compat,
264 "identifier %qE conflicts with C++ keyword",
265 token->value);
267 else if (rid_code >= RID_FIRST_ADDR_SPACE
268 && rid_code <= RID_LAST_ADDR_SPACE)
270 token->id_kind = C_ID_ADDRSPACE;
271 token->keyword = rid_code;
272 break;
274 else if (c_dialect_objc () && OBJC_IS_PQ_KEYWORD (rid_code))
276 /* We found an Objective-C "pq" keyword (in, out,
277 inout, bycopy, byref, oneway). They need special
278 care because the interpretation depends on the
279 context. */
280 if (parser->objc_pq_context)
282 token->type = CPP_KEYWORD;
283 token->keyword = rid_code;
284 break;
286 else if (parser->objc_could_be_foreach_context
287 && rid_code == RID_IN)
289 /* We are in Objective-C, inside a (potential)
290 foreach context (which means after having
291 parsed 'for (', but before having parsed ';'),
292 and we found 'in'. We consider it the keyword
293 which terminates the declaration at the
294 beginning of a foreach-statement. Note that
295 this means you can't use 'in' for anything else
296 in that context; in particular, in Objective-C
297 you can't use 'in' as the name of the running
298 variable in a C for loop. We could potentially
299 try to add code here to disambiguate, but it
300 seems a reasonable limitation. */
301 token->type = CPP_KEYWORD;
302 token->keyword = rid_code;
303 break;
305 /* Else, "pq" keywords outside of the "pq" context are
306 not keywords, and we fall through to the code for
307 normal tokens. */
309 else if (c_dialect_objc () && OBJC_IS_PATTR_KEYWORD (rid_code))
311 /* We found an Objective-C "property attribute"
312 keyword (getter, setter, readonly, etc). These are
313 only valid in the property context. */
314 if (parser->objc_property_attr_context)
316 token->type = CPP_KEYWORD;
317 token->keyword = rid_code;
318 break;
320 /* Else they are not special keywords.
323 else if (c_dialect_objc ()
324 && (OBJC_IS_AT_KEYWORD (rid_code)
325 || OBJC_IS_CXX_KEYWORD (rid_code)))
327 /* We found one of the Objective-C "@" keywords (defs,
328 selector, synchronized, etc) or one of the
329 Objective-C "cxx" keywords (class, private,
330 protected, public, try, catch, throw) without a
331 preceding '@' sign. Do nothing and fall through to
332 the code for normal tokens (in C++ we would still
333 consider the CXX ones keywords, but not in C). */
336 else
338 token->type = CPP_KEYWORD;
339 token->keyword = rid_code;
340 break;
344 decl = lookup_name (token->value);
345 if (decl)
347 if (TREE_CODE (decl) == TYPE_DECL)
349 token->id_kind = C_ID_TYPENAME;
350 break;
353 else if (c_dialect_objc ())
355 tree objc_interface_decl = objc_is_class_name (token->value);
356 /* Objective-C class names are in the same namespace as
357 variables and typedefs, and hence are shadowed by local
358 declarations. */
359 if (objc_interface_decl
360 && (!objc_force_identifier || global_bindings_p ()))
362 token->value = objc_interface_decl;
363 token->id_kind = C_ID_CLASSNAME;
364 break;
367 token->id_kind = C_ID_ID;
369 break;
370 case CPP_AT_NAME:
371 /* This only happens in Objective-C; it must be a keyword. */
372 token->type = CPP_KEYWORD;
373 switch (C_RID_CODE (token->value))
375 /* Replace 'class' with '@class', 'private' with '@private',
376 etc. This prevents confusion with the C++ keyword
377 'class', and makes the tokens consistent with other
378 Objective-C 'AT' keywords. For example '@class' is
379 reported as RID_AT_CLASS which is consistent with
380 '@synchronized', which is reported as
381 RID_AT_SYNCHRONIZED.
383 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
384 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
385 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
386 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
387 case RID_THROW: token->keyword = RID_AT_THROW; break;
388 case RID_TRY: token->keyword = RID_AT_TRY; break;
389 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
390 default: token->keyword = C_RID_CODE (token->value);
392 break;
393 case CPP_COLON:
394 case CPP_COMMA:
395 case CPP_CLOSE_PAREN:
396 case CPP_SEMICOLON:
397 /* These tokens may affect the interpretation of any identifiers
398 following, if doing Objective-C. */
399 if (c_dialect_objc ())
400 parser->objc_need_raw_identifier = false;
401 break;
402 case CPP_PRAGMA:
403 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
404 token->pragma_kind = (enum pragma_kind) TREE_INT_CST_LOW (token->value);
405 token->value = NULL;
406 break;
407 default:
408 break;
410 timevar_pop (TV_LEX);
413 /* Return a pointer to the next token from PARSER, reading it in if
414 necessary. */
416 static inline c_token *
417 c_parser_peek_token (c_parser *parser)
419 if (parser->tokens_avail == 0)
421 c_lex_one_token (parser, &parser->tokens[0]);
422 parser->tokens_avail = 1;
424 return &parser->tokens[0];
427 /* Return true if the next token from PARSER has the indicated
428 TYPE. */
430 static inline bool
431 c_parser_next_token_is (c_parser *parser, enum cpp_ttype type)
433 return c_parser_peek_token (parser)->type == type;
436 /* Return true if the next token from PARSER does not have the
437 indicated TYPE. */
439 static inline bool
440 c_parser_next_token_is_not (c_parser *parser, enum cpp_ttype type)
442 return !c_parser_next_token_is (parser, type);
445 /* Return true if the next token from PARSER is the indicated
446 KEYWORD. */
448 static inline bool
449 c_parser_next_token_is_keyword (c_parser *parser, enum rid keyword)
451 return c_parser_peek_token (parser)->keyword == keyword;
454 /* Return a pointer to the next-but-one token from PARSER, reading it
455 in if necessary. The next token is already read in. */
457 static c_token *
458 c_parser_peek_2nd_token (c_parser *parser)
460 if (parser->tokens_avail >= 2)
461 return &parser->tokens[1];
462 gcc_assert (parser->tokens_avail == 1);
463 gcc_assert (parser->tokens[0].type != CPP_EOF);
464 gcc_assert (parser->tokens[0].type != CPP_PRAGMA_EOL);
465 c_lex_one_token (parser, &parser->tokens[1]);
466 parser->tokens_avail = 2;
467 return &parser->tokens[1];
470 /* Return true if TOKEN can start a type name,
471 false otherwise. */
472 static bool
473 c_token_starts_typename (c_token *token)
475 switch (token->type)
477 case CPP_NAME:
478 switch (token->id_kind)
480 case C_ID_ID:
481 return false;
482 case C_ID_ADDRSPACE:
483 return true;
484 case C_ID_TYPENAME:
485 return true;
486 case C_ID_CLASSNAME:
487 gcc_assert (c_dialect_objc ());
488 return true;
489 default:
490 gcc_unreachable ();
492 case CPP_KEYWORD:
493 switch (token->keyword)
495 case RID_UNSIGNED:
496 case RID_LONG:
497 case RID_INT128:
498 case RID_SHORT:
499 case RID_SIGNED:
500 case RID_COMPLEX:
501 case RID_INT:
502 case RID_CHAR:
503 case RID_FLOAT:
504 case RID_DOUBLE:
505 case RID_VOID:
506 case RID_DFLOAT32:
507 case RID_DFLOAT64:
508 case RID_DFLOAT128:
509 case RID_BOOL:
510 case RID_ENUM:
511 case RID_STRUCT:
512 case RID_UNION:
513 case RID_TYPEOF:
514 case RID_CONST:
515 case RID_ATOMIC:
516 case RID_VOLATILE:
517 case RID_RESTRICT:
518 case RID_ATTRIBUTE:
519 case RID_FRACT:
520 case RID_ACCUM:
521 case RID_SAT:
522 case RID_AUTO_TYPE:
523 return true;
524 /* UPC qualifiers */
525 case RID_SHARED:
526 case RID_STRICT:
527 case RID_RELAXED:
528 return true;
529 default:
530 return false;
532 case CPP_LESS:
533 if (c_dialect_objc ())
534 return true;
535 return false;
536 default:
537 return false;
541 enum c_lookahead_kind {
542 /* Always treat unknown identifiers as typenames. */
543 cla_prefer_type,
545 /* Could be parsing a nonabstract declarator. Only treat an identifier
546 as a typename if followed by another identifier or a star. */
547 cla_nonabstract_decl,
549 /* Never treat identifiers as typenames. */
550 cla_prefer_id
553 /* Return true if the next token from PARSER can start a type name,
554 false otherwise. LA specifies how to do lookahead in order to
555 detect unknown type names. If unsure, pick CLA_PREFER_ID. */
557 static inline bool
558 c_parser_next_tokens_start_typename (c_parser *parser, enum c_lookahead_kind la)
560 c_token *token = c_parser_peek_token (parser);
561 if (c_token_starts_typename (token))
562 return true;
564 /* Try a bit harder to detect an unknown typename. */
565 if (la != cla_prefer_id
566 && token->type == CPP_NAME
567 && token->id_kind == C_ID_ID
569 /* Do not try too hard when we could have "object in array". */
570 && !parser->objc_could_be_foreach_context
572 && (la == cla_prefer_type
573 || c_parser_peek_2nd_token (parser)->type == CPP_NAME
574 || c_parser_peek_2nd_token (parser)->type == CPP_MULT)
576 /* Only unknown identifiers. */
577 && !lookup_name (token->value))
578 return true;
580 return false;
583 /* Return true if TOKEN is a type qualifier, false otherwise. */
584 static bool
585 c_token_is_qualifier (c_token *token)
587 switch (token->type)
589 case CPP_NAME:
590 switch (token->id_kind)
592 case C_ID_ADDRSPACE:
593 return true;
594 default:
595 return false;
597 case CPP_KEYWORD:
598 switch (token->keyword)
600 case RID_CONST:
601 case RID_VOLATILE:
602 case RID_RESTRICT:
603 case RID_ATTRIBUTE:
604 case RID_ATOMIC:
605 return true;
606 case RID_SHARED:
607 case RID_STRICT:
608 case RID_RELAXED:
609 /* UPC qualifiers */
610 return true;
611 default:
612 return false;
614 case CPP_LESS:
615 return false;
616 default:
617 gcc_unreachable ();
621 /* Return true if the next token from PARSER is a type qualifier,
622 false otherwise. */
623 static inline bool
624 c_parser_next_token_is_qualifier (c_parser *parser)
626 c_token *token = c_parser_peek_token (parser);
627 return c_token_is_qualifier (token);
630 /* Return true if TOKEN can start declaration specifiers, false
631 otherwise. */
632 static bool
633 c_token_starts_declspecs (c_token *token)
635 switch (token->type)
637 case CPP_NAME:
638 switch (token->id_kind)
640 case C_ID_ID:
641 return false;
642 case C_ID_ADDRSPACE:
643 return true;
644 case C_ID_TYPENAME:
645 return true;
646 case C_ID_CLASSNAME:
647 gcc_assert (c_dialect_objc ());
648 return true;
649 default:
650 gcc_unreachable ();
652 case CPP_KEYWORD:
653 switch (token->keyword)
655 case RID_STATIC:
656 case RID_EXTERN:
657 case RID_REGISTER:
658 case RID_TYPEDEF:
659 case RID_INLINE:
660 case RID_NORETURN:
661 case RID_AUTO:
662 case RID_THREAD:
663 case RID_UNSIGNED:
664 case RID_LONG:
665 case RID_INT128:
666 case RID_SHORT:
667 case RID_SIGNED:
668 case RID_COMPLEX:
669 case RID_INT:
670 case RID_CHAR:
671 case RID_FLOAT:
672 case RID_DOUBLE:
673 case RID_VOID:
674 case RID_DFLOAT32:
675 case RID_DFLOAT64:
676 case RID_DFLOAT128:
677 case RID_BOOL:
678 case RID_ENUM:
679 case RID_STRUCT:
680 case RID_UNION:
681 case RID_TYPEOF:
682 case RID_CONST:
683 case RID_VOLATILE:
684 case RID_RESTRICT:
685 case RID_ATTRIBUTE:
686 case RID_FRACT:
687 case RID_ACCUM:
688 case RID_SAT:
689 case RID_ALIGNAS:
690 case RID_ATOMIC:
691 case RID_AUTO_TYPE:
692 return true;
693 /* UPC qualifiers */
694 case RID_SHARED:
695 case RID_STRICT:
696 case RID_RELAXED:
697 return true;
698 default:
699 return false;
701 case CPP_LESS:
702 if (c_dialect_objc ())
703 return true;
704 return false;
705 default:
706 return false;
711 /* Return true if TOKEN can start declaration specifiers or a static
712 assertion, false otherwise. */
713 static bool
714 c_token_starts_declaration (c_token *token)
716 if (c_token_starts_declspecs (token)
717 || token->keyword == RID_STATIC_ASSERT)
718 return true;
719 else
720 return false;
723 /* Return true if the next token from PARSER can start declaration
724 specifiers, false otherwise. */
725 static inline bool
726 c_parser_next_token_starts_declspecs (c_parser *parser)
728 c_token *token = c_parser_peek_token (parser);
730 /* In Objective-C, a classname normally starts a declspecs unless it
731 is immediately followed by a dot. In that case, it is the
732 Objective-C 2.0 "dot-syntax" for class objects, ie, calls the
733 setter/getter on the class. c_token_starts_declspecs() can't
734 differentiate between the two cases because it only checks the
735 current token, so we have a special check here. */
736 if (c_dialect_objc ()
737 && token->type == CPP_NAME
738 && token->id_kind == C_ID_CLASSNAME
739 && c_parser_peek_2nd_token (parser)->type == CPP_DOT)
740 return false;
742 return c_token_starts_declspecs (token);
745 /* Return true if the next tokens from PARSER can start declaration
746 specifiers or a static assertion, false otherwise. */
747 static inline bool
748 c_parser_next_tokens_start_declaration (c_parser *parser)
750 c_token *token = c_parser_peek_token (parser);
752 /* Same as above. */
753 if (c_dialect_objc ()
754 && token->type == CPP_NAME
755 && token->id_kind == C_ID_CLASSNAME
756 && c_parser_peek_2nd_token (parser)->type == CPP_DOT)
757 return false;
759 /* Labels do not start declarations. */
760 if (token->type == CPP_NAME
761 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
762 return false;
764 if (c_token_starts_declaration (token))
765 return true;
767 if (c_parser_next_tokens_start_typename (parser, cla_nonabstract_decl))
768 return true;
770 return false;
773 /* Consume the next token from PARSER. */
775 static void
776 c_parser_consume_token (c_parser *parser)
778 gcc_assert (parser->tokens_avail >= 1);
779 gcc_assert (parser->tokens[0].type != CPP_EOF);
780 gcc_assert (!parser->in_pragma || parser->tokens[0].type != CPP_PRAGMA_EOL);
781 gcc_assert (parser->error || parser->tokens[0].type != CPP_PRAGMA);
782 if (parser->tokens != &parser->tokens_buf[0])
783 parser->tokens++;
784 else if (parser->tokens_avail == 2)
785 parser->tokens[0] = parser->tokens[1];
786 parser->tokens_avail--;
789 /* Expect the current token to be a #pragma. Consume it and remember
790 that we've begun parsing a pragma. */
792 static void
793 c_parser_consume_pragma (c_parser *parser)
795 gcc_assert (!parser->in_pragma);
796 gcc_assert (parser->tokens_avail >= 1);
797 gcc_assert (parser->tokens[0].type == CPP_PRAGMA);
798 if (parser->tokens != &parser->tokens_buf[0])
799 parser->tokens++;
800 else if (parser->tokens_avail == 2)
801 parser->tokens[0] = parser->tokens[1];
802 parser->tokens_avail--;
803 parser->in_pragma = true;
806 /* Update the global input_location from TOKEN. */
807 static inline void
808 c_parser_set_source_position_from_token (c_token *token)
810 if (token->type != CPP_EOF)
812 input_location = token->location;
816 /* Issue a diagnostic of the form
817 FILE:LINE: MESSAGE before TOKEN
818 where TOKEN is the next token in the input stream of PARSER.
819 MESSAGE (specified by the caller) is usually of the form "expected
820 OTHER-TOKEN".
822 Do not issue a diagnostic if still recovering from an error.
824 ??? This is taken from the C++ parser, but building up messages in
825 this way is not i18n-friendly and some other approach should be
826 used. */
828 static void
829 c_parser_error (c_parser *parser, const char *gmsgid)
831 c_token *token = c_parser_peek_token (parser);
832 if (parser->error)
833 return;
834 parser->error = true;
835 if (!gmsgid)
836 return;
837 /* This diagnostic makes more sense if it is tagged to the line of
838 the token we just peeked at. */
839 c_parser_set_source_position_from_token (token);
840 c_parse_error (gmsgid,
841 /* Because c_parse_error does not understand
842 CPP_KEYWORD, keywords are treated like
843 identifiers. */
844 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
845 /* ??? The C parser does not save the cpp flags of a
846 token, we need to pass 0 here and we will not get
847 the source spelling of some tokens but rather the
848 canonical spelling. */
849 token->value, /*flags=*/0);
852 /* If the next token is of the indicated TYPE, consume it. Otherwise,
853 issue the error MSGID. If MSGID is NULL then a message has already
854 been produced and no message will be produced this time. Returns
855 true if found, false otherwise. */
857 static bool
858 c_parser_require (c_parser *parser,
859 enum cpp_ttype type,
860 const char *msgid)
862 if (c_parser_next_token_is (parser, type))
864 c_parser_consume_token (parser);
865 return true;
867 else
869 c_parser_error (parser, msgid);
870 return false;
874 /* If the next token is the indicated keyword, consume it. Otherwise,
875 issue the error MSGID. Returns true if found, false otherwise. */
877 static bool
878 c_parser_require_keyword (c_parser *parser,
879 enum rid keyword,
880 const char *msgid)
882 if (c_parser_next_token_is_keyword (parser, keyword))
884 c_parser_consume_token (parser);
885 return true;
887 else
889 c_parser_error (parser, msgid);
890 return false;
894 /* Like c_parser_require, except that tokens will be skipped until the
895 desired token is found. An error message is still produced if the
896 next token is not as expected. If MSGID is NULL then a message has
897 already been produced and no message will be produced this
898 time. */
900 static void
901 c_parser_skip_until_found (c_parser *parser,
902 enum cpp_ttype type,
903 const char *msgid)
905 unsigned nesting_depth = 0;
907 if (c_parser_require (parser, type, msgid))
908 return;
910 /* Skip tokens until the desired token is found. */
911 while (true)
913 /* Peek at the next token. */
914 c_token *token = c_parser_peek_token (parser);
915 /* If we've reached the token we want, consume it and stop. */
916 if (token->type == type && !nesting_depth)
918 c_parser_consume_token (parser);
919 break;
922 /* If we've run out of tokens, stop. */
923 if (token->type == CPP_EOF)
924 return;
925 if (token->type == CPP_PRAGMA_EOL && parser->in_pragma)
926 return;
927 if (token->type == CPP_OPEN_BRACE
928 || token->type == CPP_OPEN_PAREN
929 || token->type == CPP_OPEN_SQUARE)
930 ++nesting_depth;
931 else if (token->type == CPP_CLOSE_BRACE
932 || token->type == CPP_CLOSE_PAREN
933 || token->type == CPP_CLOSE_SQUARE)
935 if (nesting_depth-- == 0)
936 break;
938 /* Consume this token. */
939 c_parser_consume_token (parser);
941 parser->error = false;
944 /* Skip tokens until the end of a parameter is found, but do not
945 consume the comma, semicolon or closing delimiter. */
947 static void
948 c_parser_skip_to_end_of_parameter (c_parser *parser)
950 unsigned nesting_depth = 0;
952 while (true)
954 c_token *token = c_parser_peek_token (parser);
955 if ((token->type == CPP_COMMA || token->type == CPP_SEMICOLON)
956 && !nesting_depth)
957 break;
958 /* If we've run out of tokens, stop. */
959 if (token->type == CPP_EOF)
960 return;
961 if (token->type == CPP_PRAGMA_EOL && parser->in_pragma)
962 return;
963 if (token->type == CPP_OPEN_BRACE
964 || token->type == CPP_OPEN_PAREN
965 || token->type == CPP_OPEN_SQUARE)
966 ++nesting_depth;
967 else if (token->type == CPP_CLOSE_BRACE
968 || token->type == CPP_CLOSE_PAREN
969 || token->type == CPP_CLOSE_SQUARE)
971 if (nesting_depth-- == 0)
972 break;
974 /* Consume this token. */
975 c_parser_consume_token (parser);
977 parser->error = false;
980 /* Expect to be at the end of the pragma directive and consume an
981 end of line marker. */
983 static void
984 c_parser_skip_to_pragma_eol (c_parser *parser)
986 gcc_assert (parser->in_pragma);
987 parser->in_pragma = false;
989 if (!c_parser_require (parser, CPP_PRAGMA_EOL, "expected end of line"))
990 while (true)
992 c_token *token = c_parser_peek_token (parser);
993 if (token->type == CPP_EOF)
994 break;
995 if (token->type == CPP_PRAGMA_EOL)
997 c_parser_consume_token (parser);
998 break;
1000 c_parser_consume_token (parser);
1003 parser->error = false;
1006 /* Skip tokens until we have consumed an entire block, or until we
1007 have consumed a non-nested ';'. */
1009 static void
1010 c_parser_skip_to_end_of_block_or_statement (c_parser *parser)
1012 unsigned nesting_depth = 0;
1013 bool save_error = parser->error;
1015 while (true)
1017 c_token *token;
1019 /* Peek at the next token. */
1020 token = c_parser_peek_token (parser);
1022 switch (token->type)
1024 case CPP_EOF:
1025 return;
1027 case CPP_PRAGMA_EOL:
1028 if (parser->in_pragma)
1029 return;
1030 break;
1032 case CPP_SEMICOLON:
1033 /* If the next token is a ';', we have reached the
1034 end of the statement. */
1035 if (!nesting_depth)
1037 /* Consume the ';'. */
1038 c_parser_consume_token (parser);
1039 goto finished;
1041 break;
1043 case CPP_CLOSE_BRACE:
1044 /* If the next token is a non-nested '}', then we have
1045 reached the end of the current block. */
1046 if (nesting_depth == 0 || --nesting_depth == 0)
1048 c_parser_consume_token (parser);
1049 goto finished;
1051 break;
1053 case CPP_OPEN_BRACE:
1054 /* If it the next token is a '{', then we are entering a new
1055 block. Consume the entire block. */
1056 ++nesting_depth;
1057 break;
1059 case CPP_PRAGMA:
1060 /* If we see a pragma, consume the whole thing at once. We
1061 have some safeguards against consuming pragmas willy-nilly.
1062 Normally, we'd expect to be here with parser->error set,
1063 which disables these safeguards. But it's possible to get
1064 here for secondary error recovery, after parser->error has
1065 been cleared. */
1066 c_parser_consume_pragma (parser);
1067 c_parser_skip_to_pragma_eol (parser);
1068 parser->error = save_error;
1069 continue;
1071 default:
1072 break;
1075 c_parser_consume_token (parser);
1078 finished:
1079 parser->error = false;
1082 /* CPP's options (initialized by c-opts.c). */
1083 extern cpp_options *cpp_opts;
1085 /* Save the warning flags which are controlled by __extension__. */
1087 static inline int
1088 disable_extension_diagnostics (void)
1090 int ret = (pedantic
1091 | (warn_pointer_arith << 1)
1092 | (warn_traditional << 2)
1093 | (flag_iso << 3)
1094 | (warn_long_long << 4)
1095 | (warn_cxx_compat << 5)
1096 | (warn_overlength_strings << 6));
1097 cpp_opts->cpp_pedantic = pedantic = 0;
1098 warn_pointer_arith = 0;
1099 cpp_opts->cpp_warn_traditional = warn_traditional = 0;
1100 flag_iso = 0;
1101 cpp_opts->cpp_warn_long_long = warn_long_long = 0;
1102 warn_cxx_compat = 0;
1103 warn_overlength_strings = 0;
1104 return ret;
1107 /* Restore the warning flags which are controlled by __extension__.
1108 FLAGS is the return value from disable_extension_diagnostics. */
1110 static inline void
1111 restore_extension_diagnostics (int flags)
1113 cpp_opts->cpp_pedantic = pedantic = flags & 1;
1114 warn_pointer_arith = (flags >> 1) & 1;
1115 cpp_opts->cpp_warn_traditional = warn_traditional = (flags >> 2) & 1;
1116 flag_iso = (flags >> 3) & 1;
1117 cpp_opts->cpp_warn_long_long = warn_long_long = (flags >> 4) & 1;
1118 warn_cxx_compat = (flags >> 5) & 1;
1119 warn_overlength_strings = (flags >> 6) & 1;
1122 /* Possibly kinds of declarator to parse. */
1123 typedef enum c_dtr_syn {
1124 /* A normal declarator with an identifier. */
1125 C_DTR_NORMAL,
1126 /* An abstract declarator (maybe empty). */
1127 C_DTR_ABSTRACT,
1128 /* A parameter declarator: may be either, but after a type name does
1129 not redeclare a typedef name as an identifier if it can
1130 alternatively be interpreted as a typedef name; see DR#009,
1131 applied in C90 TC1, omitted from C99 and reapplied in C99 TC2
1132 following DR#249. For example, given a typedef T, "int T" and
1133 "int *T" are valid parameter declarations redeclaring T, while
1134 "int (T)" and "int * (T)" and "int (T[])" and "int (T (int))" are
1135 abstract declarators rather than involving redundant parentheses;
1136 the same applies with attributes inside the parentheses before
1137 "T". */
1138 C_DTR_PARM
1139 } c_dtr_syn;
1141 /* The binary operation precedence levels, where 0 is a dummy lowest level
1142 used for the bottom of the stack. */
1143 enum c_parser_prec {
1144 PREC_NONE,
1145 PREC_LOGOR,
1146 PREC_LOGAND,
1147 PREC_BITOR,
1148 PREC_BITXOR,
1149 PREC_BITAND,
1150 PREC_EQ,
1151 PREC_REL,
1152 PREC_SHIFT,
1153 PREC_ADD,
1154 PREC_MULT,
1155 NUM_PRECS
1158 static void c_parser_external_declaration (c_parser *);
1159 static void c_parser_asm_definition (c_parser *);
1160 static void c_parser_declaration_or_fndef (c_parser *, bool, bool, bool,
1161 bool, bool, tree *, vec<c_token>);
1162 static void c_parser_static_assert_declaration_no_semi (c_parser *);
1163 static void c_parser_static_assert_declaration (c_parser *);
1164 static void c_parser_declspecs (c_parser *, struct c_declspecs *, bool, bool,
1165 bool, bool, bool, enum c_lookahead_kind);
1166 static struct c_typespec c_parser_enum_specifier (c_parser *);
1167 static struct c_typespec c_parser_struct_or_union_specifier (c_parser *);
1168 static tree c_parser_struct_declaration (c_parser *);
1169 static struct c_typespec c_parser_typeof_specifier (c_parser *);
1170 static tree c_parser_alignas_specifier (c_parser *);
1171 static struct c_declarator *c_parser_declarator (c_parser *, bool, c_dtr_syn,
1172 bool *);
1173 static struct c_declarator *c_parser_direct_declarator (c_parser *, bool,
1174 c_dtr_syn, bool *);
1175 static struct c_declarator *c_parser_direct_declarator_inner (c_parser *,
1176 bool,
1177 struct c_declarator *);
1178 static struct c_arg_info *c_parser_parms_declarator (c_parser *, bool, tree);
1179 static struct c_arg_info *c_parser_parms_list_declarator (c_parser *, tree,
1180 tree);
1181 static struct c_parm *c_parser_parameter_declaration (c_parser *, tree);
1182 static tree c_parser_simple_asm_expr (c_parser *);
1183 static tree c_parser_attributes (c_parser *);
1184 static struct c_type_name *c_parser_type_name (c_parser *);
1185 static struct c_expr c_parser_initializer (c_parser *);
1186 static struct c_expr c_parser_braced_init (c_parser *, tree, bool);
1187 static void c_parser_initelt (c_parser *, struct obstack *);
1188 static void c_parser_initval (c_parser *, struct c_expr *,
1189 struct obstack *);
1190 static tree c_parser_compound_statement (c_parser *);
1191 static void c_parser_compound_statement_nostart (c_parser *);
1192 static void c_parser_label (c_parser *);
1193 static void c_parser_statement (c_parser *);
1194 static void c_parser_statement_after_labels (c_parser *);
1195 static void c_parser_if_statement (c_parser *);
1196 static void c_parser_switch_statement (c_parser *);
1197 static void c_parser_while_statement (c_parser *, bool);
1198 static void c_parser_do_statement (c_parser *, bool);
1199 static void c_parser_for_statement (c_parser *, bool);
1200 static tree c_parser_asm_statement (c_parser *);
1201 static tree c_parser_asm_operands (c_parser *);
1202 static tree c_parser_asm_goto_operands (c_parser *);
1203 static tree c_parser_asm_clobbers (c_parser *);
1204 static struct c_expr c_parser_expr_no_commas (c_parser *, struct c_expr *,
1205 tree = NULL_TREE);
1206 static struct c_expr c_parser_conditional_expression (c_parser *,
1207 struct c_expr *, tree);
1208 static struct c_expr c_parser_binary_expression (c_parser *, struct c_expr *,
1209 tree);
1210 static struct c_expr c_parser_cast_expression (c_parser *, struct c_expr *);
1211 static struct c_expr c_parser_unary_expression (c_parser *);
1212 static struct c_expr c_parser_sizeof_expression (c_parser *);
1213 static struct c_expr c_parser_alignof_expression (c_parser *);
1214 static struct c_expr c_parser_postfix_expression (c_parser *);
1215 static struct c_expr c_parser_postfix_expression_after_paren_type (c_parser *,
1216 struct c_type_name *,
1217 location_t);
1218 static struct c_expr c_parser_postfix_expression_after_primary (c_parser *,
1219 location_t loc,
1220 struct c_expr);
1221 static tree c_parser_transaction (c_parser *, enum rid);
1222 static struct c_expr c_parser_transaction_expression (c_parser *, enum rid);
1223 static tree c_parser_transaction_cancel (c_parser *);
1224 static struct c_expr c_parser_expression (c_parser *);
1225 static struct c_expr c_parser_expression_conv (c_parser *);
1226 static vec<tree, va_gc> *c_parser_expr_list (c_parser *, bool, bool,
1227 vec<tree, va_gc> **, location_t *,
1228 tree *, vec<location_t> *);
1229 static void c_parser_omp_construct (c_parser *);
1230 static void c_parser_omp_threadprivate (c_parser *);
1231 static void c_parser_omp_barrier (c_parser *);
1232 static void c_parser_omp_flush (c_parser *);
1233 static void c_parser_omp_taskwait (c_parser *);
1234 static void c_parser_omp_taskyield (c_parser *);
1235 static void c_parser_omp_cancel (c_parser *);
1236 static void c_parser_omp_cancellation_point (c_parser *);
1238 enum pragma_context { pragma_external, pragma_struct, pragma_param,
1239 pragma_stmt, pragma_compound };
1240 static bool c_parser_pragma (c_parser *, enum pragma_context);
1241 static bool c_parser_omp_target (c_parser *, enum pragma_context);
1242 static void c_parser_omp_end_declare_target (c_parser *);
1243 static void c_parser_omp_declare (c_parser *, enum pragma_context);
1245 /* These Objective-C parser functions are only ever called when
1246 compiling Objective-C. */
1247 static void c_parser_objc_class_definition (c_parser *, tree);
1248 static void c_parser_objc_class_instance_variables (c_parser *);
1249 static void c_parser_objc_class_declaration (c_parser *);
1250 static void c_parser_objc_alias_declaration (c_parser *);
1251 static void c_parser_objc_protocol_definition (c_parser *, tree);
1252 static bool c_parser_objc_method_type (c_parser *);
1253 static void c_parser_objc_method_definition (c_parser *);
1254 static void c_parser_objc_methodprotolist (c_parser *);
1255 static void c_parser_objc_methodproto (c_parser *);
1256 static tree c_parser_objc_method_decl (c_parser *, bool, tree *, tree *);
1257 static tree c_parser_objc_type_name (c_parser *);
1258 static tree c_parser_objc_protocol_refs (c_parser *);
1259 static void c_parser_objc_try_catch_finally_statement (c_parser *);
1260 static void c_parser_objc_synchronized_statement (c_parser *);
1261 static tree c_parser_objc_selector (c_parser *);
1262 static tree c_parser_objc_selector_arg (c_parser *);
1263 static tree c_parser_objc_receiver (c_parser *);
1264 static tree c_parser_objc_message_args (c_parser *);
1265 static tree c_parser_objc_keywordexpr (c_parser *);
1266 static void c_parser_objc_at_property_declaration (c_parser *);
1267 static void c_parser_objc_at_synthesize_declaration (c_parser *);
1268 static void c_parser_objc_at_dynamic_declaration (c_parser *);
1269 static bool c_parser_objc_diagnose_bad_element_prefix
1270 (c_parser *, struct c_declspecs *);
1272 /* Cilk Plus supporting routines. */
1273 static void c_parser_cilk_simd (c_parser *);
1274 static bool c_parser_cilk_verify_simd (c_parser *, enum pragma_context);
1275 static tree c_parser_array_notation (location_t, c_parser *, tree, tree);
1276 static tree c_parser_cilk_clause_vectorlength (c_parser *, tree, bool);
1278 /* These UPC parser functions are only ever called when
1279 compiling UPC. */
1280 static void c_parser_upc_forall_statement (c_parser *);
1281 static void c_parser_upc_sync_statement (c_parser *, int);
1282 static void c_parser_upc_shared_qual (source_location,
1283 c_parser *,
1284 struct c_declspecs *);
1286 /* Parse a translation unit (C90 6.7, C99 6.9).
1288 translation-unit:
1289 external-declarations
1291 external-declarations:
1292 external-declaration
1293 external-declarations external-declaration
1295 GNU extensions:
1297 translation-unit:
1298 empty
1301 static void
1302 c_parser_translation_unit (c_parser *parser)
1304 if (c_parser_next_token_is (parser, CPP_EOF))
1306 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
1307 "ISO C forbids an empty translation unit");
1309 else
1311 void *obstack_position = obstack_alloc (&parser_obstack, 0);
1312 mark_valid_location_for_stdc_pragma (false);
1315 ggc_collect ();
1316 c_parser_external_declaration (parser);
1317 obstack_free (&parser_obstack, obstack_position);
1319 while (c_parser_next_token_is_not (parser, CPP_EOF));
1323 /* Parse an external declaration (C90 6.7, C99 6.9).
1325 external-declaration:
1326 function-definition
1327 declaration
1329 GNU extensions:
1331 external-declaration:
1332 asm-definition
1334 __extension__ external-declaration
1336 Objective-C:
1338 external-declaration:
1339 objc-class-definition
1340 objc-class-declaration
1341 objc-alias-declaration
1342 objc-protocol-definition
1343 objc-method-definition
1344 @end
1347 static void
1348 c_parser_external_declaration (c_parser *parser)
1350 int ext;
1351 switch (c_parser_peek_token (parser)->type)
1353 case CPP_KEYWORD:
1354 switch (c_parser_peek_token (parser)->keyword)
1356 case RID_EXTENSION:
1357 ext = disable_extension_diagnostics ();
1358 c_parser_consume_token (parser);
1359 c_parser_external_declaration (parser);
1360 restore_extension_diagnostics (ext);
1361 break;
1362 case RID_ASM:
1363 c_parser_asm_definition (parser);
1364 break;
1365 case RID_AT_INTERFACE:
1366 case RID_AT_IMPLEMENTATION:
1367 gcc_assert (c_dialect_objc ());
1368 c_parser_objc_class_definition (parser, NULL_TREE);
1369 break;
1370 case RID_AT_CLASS:
1371 gcc_assert (c_dialect_objc ());
1372 c_parser_objc_class_declaration (parser);
1373 break;
1374 case RID_AT_ALIAS:
1375 gcc_assert (c_dialect_objc ());
1376 c_parser_objc_alias_declaration (parser);
1377 break;
1378 case RID_AT_PROTOCOL:
1379 gcc_assert (c_dialect_objc ());
1380 c_parser_objc_protocol_definition (parser, NULL_TREE);
1381 break;
1382 case RID_AT_PROPERTY:
1383 gcc_assert (c_dialect_objc ());
1384 c_parser_objc_at_property_declaration (parser);
1385 break;
1386 case RID_AT_SYNTHESIZE:
1387 gcc_assert (c_dialect_objc ());
1388 c_parser_objc_at_synthesize_declaration (parser);
1389 break;
1390 case RID_AT_DYNAMIC:
1391 gcc_assert (c_dialect_objc ());
1392 c_parser_objc_at_dynamic_declaration (parser);
1393 break;
1394 case RID_AT_END:
1395 gcc_assert (c_dialect_objc ());
1396 c_parser_consume_token (parser);
1397 objc_finish_implementation ();
1398 break;
1399 default:
1400 goto decl_or_fndef;
1402 break;
1403 case CPP_SEMICOLON:
1404 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
1405 "ISO C does not allow extra %<;%> outside of a function");
1406 c_parser_consume_token (parser);
1407 break;
1408 case CPP_PRAGMA:
1409 mark_valid_location_for_stdc_pragma (true);
1410 c_parser_pragma (parser, pragma_external);
1411 mark_valid_location_for_stdc_pragma (false);
1412 break;
1413 case CPP_PLUS:
1414 case CPP_MINUS:
1415 if (c_dialect_objc ())
1417 c_parser_objc_method_definition (parser);
1418 break;
1420 /* Else fall through, and yield a syntax error trying to parse
1421 as a declaration or function definition. */
1422 default:
1423 decl_or_fndef:
1424 /* A declaration or a function definition (or, in Objective-C,
1425 an @interface or @protocol with prefix attributes). We can
1426 only tell which after parsing the declaration specifiers, if
1427 any, and the first declarator. */
1428 c_parser_declaration_or_fndef (parser, true, true, true, false, true,
1429 NULL, vNULL);
1430 break;
1434 static void c_finish_omp_declare_simd (c_parser *, tree, tree, vec<c_token>);
1436 /* Parse a declaration or function definition (C90 6.5, 6.7.1, C99
1437 6.7, 6.9.1). If FNDEF_OK is true, a function definition is
1438 accepted; otherwise (old-style parameter declarations) only other
1439 declarations are accepted. If STATIC_ASSERT_OK is true, a static
1440 assertion is accepted; otherwise (old-style parameter declarations)
1441 it is not. If NESTED is true, we are inside a function or parsing
1442 old-style parameter declarations; any functions encountered are
1443 nested functions and declaration specifiers are required; otherwise
1444 we are at top level and functions are normal functions and
1445 declaration specifiers may be optional. If EMPTY_OK is true, empty
1446 declarations are OK (subject to all other constraints); otherwise
1447 (old-style parameter declarations) they are diagnosed. If
1448 START_ATTR_OK is true, the declaration specifiers may start with
1449 attributes; otherwise they may not.
1450 OBJC_FOREACH_OBJECT_DECLARATION can be used to get back the parsed
1451 declaration when parsing an Objective-C foreach statement.
1453 declaration:
1454 declaration-specifiers init-declarator-list[opt] ;
1455 static_assert-declaration
1457 function-definition:
1458 declaration-specifiers[opt] declarator declaration-list[opt]
1459 compound-statement
1461 declaration-list:
1462 declaration
1463 declaration-list declaration
1465 init-declarator-list:
1466 init-declarator
1467 init-declarator-list , init-declarator
1469 init-declarator:
1470 declarator simple-asm-expr[opt] attributes[opt]
1471 declarator simple-asm-expr[opt] attributes[opt] = initializer
1473 GNU extensions:
1475 nested-function-definition:
1476 declaration-specifiers declarator declaration-list[opt]
1477 compound-statement
1479 Objective-C:
1480 attributes objc-class-definition
1481 attributes objc-category-definition
1482 attributes objc-protocol-definition
1484 The simple-asm-expr and attributes are GNU extensions.
1486 This function does not handle __extension__; that is handled in its
1487 callers. ??? Following the old parser, __extension__ may start
1488 external declarations, declarations in functions and declarations
1489 at the start of "for" loops, but not old-style parameter
1490 declarations.
1492 C99 requires declaration specifiers in a function definition; the
1493 absence is diagnosed through the diagnosis of implicit int. In GNU
1494 C we also allow but diagnose declarations without declaration
1495 specifiers, but only at top level (elsewhere they conflict with
1496 other syntax).
1498 In Objective-C, declarations of the looping variable in a foreach
1499 statement are exceptionally terminated by 'in' (for example, 'for
1500 (NSObject *object in array) { ... }').
1502 OpenMP:
1504 declaration:
1505 threadprivate-directive */
1507 static void
1508 c_parser_declaration_or_fndef (c_parser *parser, bool fndef_ok,
1509 bool static_assert_ok, bool empty_ok,
1510 bool nested, bool start_attr_ok,
1511 tree *objc_foreach_object_declaration,
1512 vec<c_token> omp_declare_simd_clauses)
1514 struct c_declspecs *specs;
1515 tree prefix_attrs;
1516 tree all_prefix_attrs;
1517 bool diagnosed_no_specs = false;
1518 location_t here = c_parser_peek_token (parser)->location;
1520 if (static_assert_ok
1521 && c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT))
1523 c_parser_static_assert_declaration (parser);
1524 return;
1526 specs = build_null_declspecs ();
1528 /* Try to detect an unknown type name when we have "A B" or "A *B". */
1529 if (c_parser_peek_token (parser)->type == CPP_NAME
1530 && c_parser_peek_token (parser)->id_kind == C_ID_ID
1531 && (c_parser_peek_2nd_token (parser)->type == CPP_NAME
1532 || c_parser_peek_2nd_token (parser)->type == CPP_MULT)
1533 && (!nested || !lookup_name (c_parser_peek_token (parser)->value)))
1535 error_at (here, "unknown type name %qE",
1536 c_parser_peek_token (parser)->value);
1538 /* Parse declspecs normally to get a correct pointer type, but avoid
1539 a further "fails to be a type name" error. Refuse nested functions
1540 since it is not how the user likely wants us to recover. */
1541 c_parser_peek_token (parser)->type = CPP_KEYWORD;
1542 c_parser_peek_token (parser)->keyword = RID_VOID;
1543 c_parser_peek_token (parser)->value = error_mark_node;
1544 fndef_ok = !nested;
1547 c_parser_declspecs (parser, specs, true, true, start_attr_ok,
1548 true, true, cla_nonabstract_decl);
1549 if (parser->error)
1551 c_parser_skip_to_end_of_block_or_statement (parser);
1552 return;
1554 if (nested && !specs->declspecs_seen_p)
1556 c_parser_error (parser, "expected declaration specifiers");
1557 c_parser_skip_to_end_of_block_or_statement (parser);
1558 return;
1560 finish_declspecs (specs);
1561 bool auto_type_p = specs->typespec_word == cts_auto_type;
1562 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1564 if (auto_type_p)
1565 error_at (here, "%<__auto_type%> in empty declaration");
1566 else if (empty_ok)
1567 shadow_tag (specs);
1568 else
1570 shadow_tag_warned (specs, 1);
1571 pedwarn (here, 0, "empty declaration");
1573 c_parser_consume_token (parser);
1574 return;
1577 /* Provide better error recovery. Note that a type name here is usually
1578 better diagnosed as a redeclaration. */
1579 if (empty_ok
1580 && specs->typespec_kind == ctsk_tagdef
1581 && c_parser_next_token_starts_declspecs (parser)
1582 && !c_parser_next_token_is (parser, CPP_NAME))
1584 c_parser_error (parser, "expected %<;%>, identifier or %<(%>");
1585 parser->error = false;
1586 shadow_tag_warned (specs, 1);
1587 return;
1589 else if (c_dialect_objc () && !auto_type_p)
1591 /* Prefix attributes are an error on method decls. */
1592 switch (c_parser_peek_token (parser)->type)
1594 case CPP_PLUS:
1595 case CPP_MINUS:
1596 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1597 return;
1598 if (specs->attrs)
1600 warning_at (c_parser_peek_token (parser)->location,
1601 OPT_Wattributes,
1602 "prefix attributes are ignored for methods");
1603 specs->attrs = NULL_TREE;
1605 if (fndef_ok)
1606 c_parser_objc_method_definition (parser);
1607 else
1608 c_parser_objc_methodproto (parser);
1609 return;
1610 break;
1611 default:
1612 break;
1614 /* This is where we parse 'attributes @interface ...',
1615 'attributes @implementation ...', 'attributes @protocol ...'
1616 (where attributes could be, for example, __attribute__
1617 ((deprecated)).
1619 switch (c_parser_peek_token (parser)->keyword)
1621 case RID_AT_INTERFACE:
1623 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1624 return;
1625 c_parser_objc_class_definition (parser, specs->attrs);
1626 return;
1628 break;
1629 case RID_AT_IMPLEMENTATION:
1631 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1632 return;
1633 if (specs->attrs)
1635 warning_at (c_parser_peek_token (parser)->location,
1636 OPT_Wattributes,
1637 "prefix attributes are ignored for implementations");
1638 specs->attrs = NULL_TREE;
1640 c_parser_objc_class_definition (parser, NULL_TREE);
1641 return;
1643 break;
1644 case RID_AT_PROTOCOL:
1646 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1647 return;
1648 c_parser_objc_protocol_definition (parser, specs->attrs);
1649 return;
1651 break;
1652 case RID_AT_ALIAS:
1653 case RID_AT_CLASS:
1654 case RID_AT_END:
1655 case RID_AT_PROPERTY:
1656 if (specs->attrs)
1658 c_parser_error (parser, "unexpected attribute");
1659 specs->attrs = NULL;
1661 break;
1662 default:
1663 break;
1667 pending_xref_error ();
1668 prefix_attrs = specs->attrs;
1669 all_prefix_attrs = prefix_attrs;
1670 specs->attrs = NULL_TREE;
1671 while (true)
1673 struct c_declarator *declarator;
1674 bool dummy = false;
1675 timevar_id_t tv;
1676 tree fnbody;
1677 /* Declaring either one or more declarators (in which case we
1678 should diagnose if there were no declaration specifiers) or a
1679 function definition (in which case the diagnostic for
1680 implicit int suffices). */
1681 declarator = c_parser_declarator (parser,
1682 specs->typespec_kind != ctsk_none,
1683 C_DTR_NORMAL, &dummy);
1684 if (declarator == NULL)
1686 if (omp_declare_simd_clauses.exists ()
1687 || !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
1688 c_finish_omp_declare_simd (parser, NULL_TREE, NULL_TREE,
1689 omp_declare_simd_clauses);
1690 c_parser_skip_to_end_of_block_or_statement (parser);
1691 return;
1693 if (auto_type_p && declarator->kind != cdk_id)
1695 error_at (here,
1696 "%<__auto_type%> requires a plain identifier"
1697 " as declarator");
1698 c_parser_skip_to_end_of_block_or_statement (parser);
1699 return;
1701 if (c_parser_next_token_is (parser, CPP_EQ)
1702 || c_parser_next_token_is (parser, CPP_COMMA)
1703 || c_parser_next_token_is (parser, CPP_SEMICOLON)
1704 || c_parser_next_token_is_keyword (parser, RID_ASM)
1705 || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE)
1706 || c_parser_next_token_is_keyword (parser, RID_IN))
1708 tree asm_name = NULL_TREE;
1709 tree postfix_attrs = NULL_TREE;
1710 if (!diagnosed_no_specs && !specs->declspecs_seen_p)
1712 diagnosed_no_specs = true;
1713 pedwarn (here, 0, "data definition has no type or storage class");
1715 /* Having seen a data definition, there cannot now be a
1716 function definition. */
1717 fndef_ok = false;
1718 if (c_parser_next_token_is_keyword (parser, RID_ASM))
1719 asm_name = c_parser_simple_asm_expr (parser);
1720 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1721 postfix_attrs = c_parser_attributes (parser);
1722 if (c_parser_next_token_is (parser, CPP_EQ))
1724 tree d;
1725 struct c_expr init;
1726 location_t init_loc;
1727 c_parser_consume_token (parser);
1728 if (auto_type_p)
1730 start_init (NULL_TREE, asm_name, global_bindings_p ());
1731 init_loc = c_parser_peek_token (parser)->location;
1732 init = c_parser_expr_no_commas (parser, NULL);
1733 if (TREE_CODE (init.value) == COMPONENT_REF
1734 && DECL_C_BIT_FIELD (TREE_OPERAND (init.value, 1)))
1735 error_at (here,
1736 "%<__auto_type%> used with a bit-field"
1737 " initializer");
1738 init = convert_lvalue_to_rvalue (init_loc, init, true, true);
1739 tree init_type = TREE_TYPE (init.value);
1740 /* As with typeof, remove _Atomic and const
1741 qualifiers from atomic types. */
1742 if (init_type != error_mark_node && TYPE_ATOMIC (init_type))
1743 init_type
1744 = c_build_qualified_type (init_type,
1745 (TYPE_QUALS (init_type)
1746 & ~(TYPE_QUAL_ATOMIC
1747 | TYPE_QUAL_CONST)));
1748 bool vm_type = variably_modified_type_p (init_type,
1749 NULL_TREE);
1750 if (vm_type)
1751 init.value = c_save_expr (init.value);
1752 finish_init ();
1753 specs->typespec_kind = ctsk_typeof;
1754 specs->locations[cdw_typedef] = init_loc;
1755 specs->typedef_p = true;
1756 specs->type = init_type;
1757 if (vm_type)
1759 bool maybe_const = true;
1760 tree type_expr = c_fully_fold (init.value, false,
1761 &maybe_const);
1762 specs->expr_const_operands &= maybe_const;
1763 if (specs->expr)
1764 specs->expr = build2 (COMPOUND_EXPR,
1765 TREE_TYPE (type_expr),
1766 specs->expr, type_expr);
1767 else
1768 specs->expr = type_expr;
1770 d = start_decl (declarator, specs, true,
1771 chainon (postfix_attrs, all_prefix_attrs));
1772 if (!d)
1773 d = error_mark_node;
1774 if (omp_declare_simd_clauses.exists ()
1775 || !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
1776 c_finish_omp_declare_simd (parser, d, NULL_TREE,
1777 omp_declare_simd_clauses);
1779 else
1781 /* The declaration of the variable is in effect while
1782 its initializer is parsed. */
1783 d = start_decl (declarator, specs, true,
1784 chainon (postfix_attrs, all_prefix_attrs));
1785 if (!d)
1786 d = error_mark_node;
1787 if (omp_declare_simd_clauses.exists ()
1788 || !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
1789 c_finish_omp_declare_simd (parser, d, NULL_TREE,
1790 omp_declare_simd_clauses);
1791 start_init (d, asm_name, global_bindings_p ());
1792 init_loc = c_parser_peek_token (parser)->location;
1793 init = c_parser_initializer (parser);
1794 finish_init ();
1796 if (d != error_mark_node)
1798 maybe_warn_string_init (TREE_TYPE (d), init);
1799 finish_decl (d, init_loc, init.value,
1800 init.original_type, asm_name);
1803 else
1805 if (auto_type_p)
1807 error_at (here,
1808 "%<__auto_type%> requires an initialized "
1809 "data declaration");
1810 c_parser_skip_to_end_of_block_or_statement (parser);
1811 return;
1813 tree d = start_decl (declarator, specs, false,
1814 chainon (postfix_attrs,
1815 all_prefix_attrs));
1816 if (omp_declare_simd_clauses.exists ()
1817 || !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
1819 tree parms = NULL_TREE;
1820 if (d && TREE_CODE (d) == FUNCTION_DECL)
1822 struct c_declarator *ce = declarator;
1823 while (ce != NULL)
1824 if (ce->kind == cdk_function)
1826 parms = ce->u.arg_info->parms;
1827 break;
1829 else
1830 ce = ce->declarator;
1832 if (parms)
1833 temp_store_parm_decls (d, parms);
1834 c_finish_omp_declare_simd (parser, d, parms,
1835 omp_declare_simd_clauses);
1836 if (parms)
1837 temp_pop_parm_decls ();
1839 if (d)
1840 finish_decl (d, UNKNOWN_LOCATION, NULL_TREE,
1841 NULL_TREE, asm_name);
1843 if (c_parser_next_token_is_keyword (parser, RID_IN))
1845 if (d)
1846 *objc_foreach_object_declaration = d;
1847 else
1848 *objc_foreach_object_declaration = error_mark_node;
1851 if (c_parser_next_token_is (parser, CPP_COMMA))
1853 if (auto_type_p)
1855 error_at (here,
1856 "%<__auto_type%> may only be used with"
1857 " a single declarator");
1858 c_parser_skip_to_end_of_block_or_statement (parser);
1859 return;
1861 c_parser_consume_token (parser);
1862 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1863 all_prefix_attrs = chainon (c_parser_attributes (parser),
1864 prefix_attrs);
1865 else
1866 all_prefix_attrs = prefix_attrs;
1867 continue;
1869 else if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1871 c_parser_consume_token (parser);
1872 return;
1874 else if (c_parser_next_token_is_keyword (parser, RID_IN))
1876 /* This can only happen in Objective-C: we found the
1877 'in' that terminates the declaration inside an
1878 Objective-C foreach statement. Do not consume the
1879 token, so that the caller can use it to determine
1880 that this indeed is a foreach context. */
1881 return;
1883 else
1885 c_parser_error (parser, "expected %<,%> or %<;%>");
1886 c_parser_skip_to_end_of_block_or_statement (parser);
1887 return;
1890 else if (auto_type_p)
1892 error_at (here,
1893 "%<__auto_type%> requires an initialized data declaration");
1894 c_parser_skip_to_end_of_block_or_statement (parser);
1895 return;
1897 else if (!fndef_ok)
1899 c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, "
1900 "%<asm%> or %<__attribute__%>");
1901 c_parser_skip_to_end_of_block_or_statement (parser);
1902 return;
1904 /* Function definition (nested or otherwise). */
1905 if (nested)
1907 pedwarn (here, OPT_Wpedantic, "ISO C forbids nested functions");
1908 c_push_function_context ();
1910 if (!start_function (specs, declarator, all_prefix_attrs))
1912 /* This can appear in many cases looking nothing like a
1913 function definition, so we don't give a more specific
1914 error suggesting there was one. */
1915 c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, %<asm%> "
1916 "or %<__attribute__%>");
1917 if (nested)
1918 c_pop_function_context ();
1919 break;
1922 if (DECL_DECLARED_INLINE_P (current_function_decl))
1923 tv = TV_PARSE_INLINE;
1924 else
1925 tv = TV_PARSE_FUNC;
1926 timevar_push (tv);
1928 /* Parse old-style parameter declarations. ??? Attributes are
1929 not allowed to start declaration specifiers here because of a
1930 syntax conflict between a function declaration with attribute
1931 suffix and a function definition with an attribute prefix on
1932 first old-style parameter declaration. Following the old
1933 parser, they are not accepted on subsequent old-style
1934 parameter declarations either. However, there is no
1935 ambiguity after the first declaration, nor indeed on the
1936 first as long as we don't allow postfix attributes after a
1937 declarator with a nonempty identifier list in a definition;
1938 and postfix attributes have never been accepted here in
1939 function definitions either. */
1940 while (c_parser_next_token_is_not (parser, CPP_EOF)
1941 && c_parser_next_token_is_not (parser, CPP_OPEN_BRACE))
1942 c_parser_declaration_or_fndef (parser, false, false, false,
1943 true, false, NULL, vNULL);
1944 store_parm_decls ();
1945 if (omp_declare_simd_clauses.exists ()
1946 || !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
1947 c_finish_omp_declare_simd (parser, current_function_decl, NULL_TREE,
1948 omp_declare_simd_clauses);
1949 DECL_STRUCT_FUNCTION (current_function_decl)->function_start_locus
1950 = c_parser_peek_token (parser)->location;
1951 fnbody = c_parser_compound_statement (parser);
1952 if (flag_cilkplus && contains_array_notation_expr (fnbody))
1953 fnbody = expand_array_notation_exprs (fnbody);
1954 if (nested)
1956 tree decl = current_function_decl;
1957 /* Mark nested functions as needing static-chain initially.
1958 lower_nested_functions will recompute it but the
1959 DECL_STATIC_CHAIN flag is also used before that happens,
1960 by initializer_constant_valid_p. See gcc.dg/nested-fn-2.c. */
1961 DECL_STATIC_CHAIN (decl) = 1;
1962 add_stmt (fnbody);
1963 finish_function ();
1964 c_pop_function_context ();
1965 add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl));
1967 else
1969 add_stmt (fnbody);
1970 finish_function ();
1973 timevar_pop (tv);
1974 break;
1978 /* Parse an asm-definition (asm() outside a function body). This is a
1979 GNU extension.
1981 asm-definition:
1982 simple-asm-expr ;
1985 static void
1986 c_parser_asm_definition (c_parser *parser)
1988 tree asm_str = c_parser_simple_asm_expr (parser);
1989 if (asm_str)
1990 add_asm_node (asm_str);
1991 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
1994 /* Parse a static assertion (C11 6.7.10).
1996 static_assert-declaration:
1997 static_assert-declaration-no-semi ;
2000 static void
2001 c_parser_static_assert_declaration (c_parser *parser)
2003 c_parser_static_assert_declaration_no_semi (parser);
2004 if (parser->error
2005 || !c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
2006 c_parser_skip_to_end_of_block_or_statement (parser);
2009 /* Parse a static assertion (C11 6.7.10), without the trailing
2010 semicolon.
2012 static_assert-declaration-no-semi:
2013 _Static_assert ( constant-expression , string-literal )
2016 static void
2017 c_parser_static_assert_declaration_no_semi (c_parser *parser)
2019 location_t assert_loc, value_loc;
2020 tree value;
2021 tree string;
2023 gcc_assert (c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT));
2024 assert_loc = c_parser_peek_token (parser)->location;
2025 if (!flag_isoc11)
2027 if (flag_isoc99)
2028 pedwarn (assert_loc, OPT_Wpedantic,
2029 "ISO C99 does not support %<_Static_assert%>");
2030 else
2031 pedwarn (assert_loc, OPT_Wpedantic,
2032 "ISO C90 does not support %<_Static_assert%>");
2034 c_parser_consume_token (parser);
2035 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2036 return;
2037 value_loc = c_parser_peek_token (parser)->location;
2038 value = c_parser_expr_no_commas (parser, NULL).value;
2039 parser->lex_untranslated_string = true;
2040 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
2042 parser->lex_untranslated_string = false;
2043 return;
2045 switch (c_parser_peek_token (parser)->type)
2047 case CPP_STRING:
2048 case CPP_STRING16:
2049 case CPP_STRING32:
2050 case CPP_WSTRING:
2051 case CPP_UTF8STRING:
2052 string = c_parser_peek_token (parser)->value;
2053 c_parser_consume_token (parser);
2054 parser->lex_untranslated_string = false;
2055 break;
2056 default:
2057 c_parser_error (parser, "expected string literal");
2058 parser->lex_untranslated_string = false;
2059 return;
2061 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
2063 if (!INTEGRAL_TYPE_P (TREE_TYPE (value)))
2065 error_at (value_loc, "expression in static assertion is not an integer");
2066 return;
2068 if (TREE_CODE (value) != INTEGER_CST)
2070 value = c_fully_fold (value, false, NULL);
2071 if (TREE_CODE (value) == INTEGER_CST)
2072 pedwarn (value_loc, OPT_Wpedantic, "expression in static assertion "
2073 "is not an integer constant expression");
2075 if (TREE_CODE (value) != INTEGER_CST)
2077 error_at (value_loc, "expression in static assertion is not constant");
2078 return;
2080 constant_expression_warning (value);
2081 if (integer_zerop (value))
2082 error_at (assert_loc, "static assertion failed: %E", string);
2085 /* Parse some declaration specifiers (possibly none) (C90 6.5, C99
2086 6.7), adding them to SPECS (which may already include some).
2087 Storage class specifiers are accepted iff SCSPEC_OK; type
2088 specifiers are accepted iff TYPESPEC_OK; alignment specifiers are
2089 accepted iff ALIGNSPEC_OK; attributes are accepted at the start
2090 iff START_ATTR_OK; __auto_type is accepted iff AUTO_TYPE_OK.
2092 declaration-specifiers:
2093 storage-class-specifier declaration-specifiers[opt]
2094 type-specifier declaration-specifiers[opt]
2095 type-qualifier declaration-specifiers[opt]
2096 function-specifier declaration-specifiers[opt]
2097 alignment-specifier declaration-specifiers[opt]
2099 Function specifiers (inline) are from C99, and are currently
2100 handled as storage class specifiers, as is __thread. Alignment
2101 specifiers are from C11.
2103 C90 6.5.1, C99 6.7.1:
2104 storage-class-specifier:
2105 typedef
2106 extern
2107 static
2108 auto
2109 register
2110 _Thread_local
2112 (_Thread_local is new in C11.)
2114 C99 6.7.4:
2115 function-specifier:
2116 inline
2117 _Noreturn
2119 (_Noreturn is new in C11.)
2121 C90 6.5.2, C99 6.7.2:
2122 type-specifier:
2123 void
2124 char
2125 short
2127 long
2128 float
2129 double
2130 signed
2131 unsigned
2132 _Bool
2133 _Complex
2134 [_Imaginary removed in C99 TC2]
2135 struct-or-union-specifier
2136 enum-specifier
2137 typedef-name
2138 atomic-type-specifier
2140 (_Bool and _Complex are new in C99.)
2141 (atomic-type-specifier is new in C11.)
2143 C90 6.5.3, C99 6.7.3:
2145 type-qualifier:
2146 const
2147 restrict
2148 volatile
2149 address-space-qualifier
2150 _Atomic
2152 (restrict is new in C99.)
2153 (_Atomic is new in C11.)
2155 GNU extensions:
2157 declaration-specifiers:
2158 attributes declaration-specifiers[opt]
2160 type-qualifier:
2161 address-space
2163 address-space:
2164 identifier recognized by the target
2166 storage-class-specifier:
2167 __thread
2169 type-specifier:
2170 typeof-specifier
2171 __auto_type
2172 __int128
2173 _Decimal32
2174 _Decimal64
2175 _Decimal128
2176 _Fract
2177 _Accum
2178 _Sat
2180 (_Fract, _Accum, and _Sat are new from ISO/IEC DTR 18037:
2181 http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1169.pdf)
2183 atomic-type-specifier
2184 _Atomic ( type-name )
2186 Objective-C:
2188 type-specifier:
2189 class-name objc-protocol-refs[opt]
2190 typedef-name objc-protocol-refs
2191 objc-protocol-refs
2194 static void
2195 c_parser_declspecs (c_parser *parser, struct c_declspecs *specs,
2196 bool scspec_ok, bool typespec_ok, bool start_attr_ok,
2197 bool alignspec_ok, bool auto_type_ok,
2198 enum c_lookahead_kind la)
2200 bool attrs_ok = start_attr_ok;
2201 bool seen_type = specs->typespec_kind != ctsk_none;
2203 if (!typespec_ok)
2204 gcc_assert (la == cla_prefer_id);
2206 while (c_parser_next_token_is (parser, CPP_NAME)
2207 || c_parser_next_token_is (parser, CPP_KEYWORD)
2208 || (c_dialect_objc () && c_parser_next_token_is (parser, CPP_LESS)))
2210 struct c_typespec t;
2211 tree attrs;
2212 tree align;
2213 location_t loc = c_parser_peek_token (parser)->location;
2215 /* If we cannot accept a type, exit if the next token must start
2216 one. Also, if we already have seen a tagged definition,
2217 a typename would be an error anyway and likely the user
2218 has simply forgotten a semicolon, so we exit. */
2219 if ((!typespec_ok || specs->typespec_kind == ctsk_tagdef)
2220 && c_parser_next_tokens_start_typename (parser, la)
2221 && !c_parser_next_token_is_qualifier (parser))
2222 break;
2224 if (c_parser_next_token_is (parser, CPP_NAME))
2226 c_token *name_token = c_parser_peek_token (parser);
2227 tree value = name_token->value;
2228 c_id_kind kind = name_token->id_kind;
2230 if (kind == C_ID_ADDRSPACE)
2232 addr_space_t as
2233 = name_token->keyword - RID_FIRST_ADDR_SPACE;
2234 declspecs_add_addrspace (name_token->location, specs, as);
2235 c_parser_consume_token (parser);
2236 attrs_ok = true;
2237 continue;
2240 gcc_assert (!c_parser_next_token_is_qualifier (parser));
2242 /* If we cannot accept a type, and the next token must start one,
2243 exit. Do the same if we already have seen a tagged definition,
2244 since it would be an error anyway and likely the user has simply
2245 forgotten a semicolon. */
2246 if (seen_type || !c_parser_next_tokens_start_typename (parser, la))
2247 break;
2249 /* Now at an unknown typename (C_ID_ID), a C_ID_TYPENAME or
2250 a C_ID_CLASSNAME. */
2251 c_parser_consume_token (parser);
2252 seen_type = true;
2253 attrs_ok = true;
2254 if (kind == C_ID_ID)
2256 error_at (loc, "unknown type name %qE", value);
2257 t.kind = ctsk_typedef;
2258 t.spec = error_mark_node;
2260 else if (kind == C_ID_TYPENAME
2261 && (!c_dialect_objc ()
2262 || c_parser_next_token_is_not (parser, CPP_LESS)))
2264 t.kind = ctsk_typedef;
2265 /* For a typedef name, record the meaning, not the name.
2266 In case of 'foo foo, bar;'. */
2267 t.spec = lookup_name (value);
2269 else
2271 tree proto = NULL_TREE;
2272 gcc_assert (c_dialect_objc ());
2273 t.kind = ctsk_objc;
2274 if (c_parser_next_token_is (parser, CPP_LESS))
2275 proto = c_parser_objc_protocol_refs (parser);
2276 t.spec = objc_get_protocol_qualified_type (value, proto);
2278 t.expr = NULL_TREE;
2279 t.expr_const_operands = true;
2280 declspecs_add_type (name_token->location, specs, t);
2281 continue;
2283 if (c_parser_next_token_is (parser, CPP_LESS))
2285 /* Make "<SomeProtocol>" equivalent to "id <SomeProtocol>" -
2286 nisse@lysator.liu.se. */
2287 tree proto;
2288 gcc_assert (c_dialect_objc ());
2289 if (!typespec_ok || seen_type)
2290 break;
2291 proto = c_parser_objc_protocol_refs (parser);
2292 t.kind = ctsk_objc;
2293 t.spec = objc_get_protocol_qualified_type (NULL_TREE, proto);
2294 t.expr = NULL_TREE;
2295 t.expr_const_operands = true;
2296 declspecs_add_type (loc, specs, t);
2297 continue;
2299 gcc_assert (c_parser_next_token_is (parser, CPP_KEYWORD));
2300 switch (c_parser_peek_token (parser)->keyword)
2302 case RID_STATIC:
2303 case RID_EXTERN:
2304 case RID_REGISTER:
2305 case RID_TYPEDEF:
2306 case RID_INLINE:
2307 case RID_NORETURN:
2308 case RID_AUTO:
2309 case RID_THREAD:
2310 if (!scspec_ok)
2311 goto out;
2312 attrs_ok = true;
2313 /* TODO: Distinguish between function specifiers (inline, noreturn)
2314 and storage class specifiers, either here or in
2315 declspecs_add_scspec. */
2316 declspecs_add_scspec (loc, specs,
2317 c_parser_peek_token (parser)->value);
2318 c_parser_consume_token (parser);
2319 break;
2320 case RID_AUTO_TYPE:
2321 if (!auto_type_ok)
2322 goto out;
2323 /* Fall through. */
2324 case RID_UNSIGNED:
2325 case RID_LONG:
2326 case RID_INT128:
2327 case RID_SHORT:
2328 case RID_SIGNED:
2329 case RID_COMPLEX:
2330 case RID_INT:
2331 case RID_CHAR:
2332 case RID_FLOAT:
2333 case RID_DOUBLE:
2334 case RID_VOID:
2335 case RID_DFLOAT32:
2336 case RID_DFLOAT64:
2337 case RID_DFLOAT128:
2338 case RID_BOOL:
2339 case RID_FRACT:
2340 case RID_ACCUM:
2341 case RID_SAT:
2342 if (!typespec_ok)
2343 goto out;
2344 attrs_ok = true;
2345 seen_type = true;
2346 if (c_dialect_objc ())
2347 parser->objc_need_raw_identifier = true;
2348 t.kind = ctsk_resword;
2349 t.spec = c_parser_peek_token (parser)->value;
2350 t.expr = NULL_TREE;
2351 t.expr_const_operands = true;
2352 declspecs_add_type (loc, specs, t);
2353 c_parser_consume_token (parser);
2354 break;
2355 case RID_ENUM:
2356 if (!typespec_ok)
2357 goto out;
2358 attrs_ok = true;
2359 seen_type = true;
2360 t = c_parser_enum_specifier (parser);
2361 declspecs_add_type (loc, specs, t);
2362 break;
2363 case RID_STRUCT:
2364 case RID_UNION:
2365 if (!typespec_ok)
2366 goto out;
2367 attrs_ok = true;
2368 seen_type = true;
2369 t = c_parser_struct_or_union_specifier (parser);
2370 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, t.spec);
2371 declspecs_add_type (loc, specs, t);
2372 break;
2373 case RID_TYPEOF:
2374 /* ??? The old parser rejected typeof after other type
2375 specifiers, but is a syntax error the best way of
2376 handling this? */
2377 if (!typespec_ok || seen_type)
2378 goto out;
2379 attrs_ok = true;
2380 seen_type = true;
2381 t = c_parser_typeof_specifier (parser);
2382 declspecs_add_type (loc, specs, t);
2383 break;
2384 case RID_ATOMIC:
2385 /* C parser handling of Objective-C constructs needs
2386 checking for correct lvalue-to-rvalue conversions, and
2387 the code in build_modify_expr handling various
2388 Objective-C cases, and that in build_unary_op handling
2389 Objective-C cases for increment / decrement, also needs
2390 updating; uses of TYPE_MAIN_VARIANT in objc_compare_types
2391 and objc_types_are_equivalent may also need updates. */
2392 if (c_dialect_objc ())
2393 sorry ("%<_Atomic%> in Objective-C");
2394 /* C parser handling of OpenMP constructs needs checking for
2395 correct lvalue-to-rvalue conversions. */
2396 if (flag_openmp)
2397 sorry ("%<_Atomic%> with OpenMP");
2398 if (!flag_isoc11)
2400 if (flag_isoc99)
2401 pedwarn (loc, OPT_Wpedantic,
2402 "ISO C99 does not support the %<_Atomic%> qualifier");
2403 else
2404 pedwarn (loc, OPT_Wpedantic,
2405 "ISO C90 does not support the %<_Atomic%> qualifier");
2407 attrs_ok = true;
2408 tree value;
2409 value = c_parser_peek_token (parser)->value;
2410 c_parser_consume_token (parser);
2411 if (typespec_ok && c_parser_next_token_is (parser, CPP_OPEN_PAREN))
2413 /* _Atomic ( type-name ). */
2414 seen_type = true;
2415 c_parser_consume_token (parser);
2416 struct c_type_name *type = c_parser_type_name (parser);
2417 t.kind = ctsk_typeof;
2418 t.spec = error_mark_node;
2419 t.expr = NULL_TREE;
2420 t.expr_const_operands = true;
2421 if (type != NULL)
2422 t.spec = groktypename (type, &t.expr,
2423 &t.expr_const_operands);
2424 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2425 "expected %<)%>");
2426 if (t.spec != error_mark_node)
2428 if (TREE_CODE (t.spec) == ARRAY_TYPE)
2429 error_at (loc, "%<_Atomic%>-qualified array type");
2430 else if (TREE_CODE (t.spec) == FUNCTION_TYPE)
2431 error_at (loc, "%<_Atomic%>-qualified function type");
2432 else if (TYPE_QUALS (t.spec) != TYPE_UNQUALIFIED)
2433 error_at (loc, "%<_Atomic%> applied to a qualified type");
2434 else
2435 t.spec = c_build_qualified_type (t.spec, TYPE_QUAL_ATOMIC);
2437 declspecs_add_type (loc, specs, t);
2439 else
2440 declspecs_add_qual (loc, specs, value);
2441 break;
2442 case RID_CONST:
2443 case RID_VOLATILE:
2444 case RID_RESTRICT:
2445 attrs_ok = true;
2446 declspecs_add_qual (loc, specs, c_parser_peek_token (parser)->value);
2447 c_parser_consume_token (parser);
2448 break;
2449 /* UPC qualifiers */
2450 case RID_SHARED:
2451 attrs_ok = true;
2452 c_parser_upc_shared_qual (loc, parser, specs);
2453 break;
2454 case RID_STRICT:
2455 case RID_RELAXED:
2456 attrs_ok = true;
2457 declspecs_add_qual (loc, specs, c_parser_peek_token (parser)->value);
2458 c_parser_consume_token (parser);
2459 break;
2460 case RID_ATTRIBUTE:
2461 if (!attrs_ok)
2462 goto out;
2463 attrs = c_parser_attributes (parser);
2464 declspecs_add_attrs (loc, specs, attrs);
2465 break;
2466 case RID_ALIGNAS:
2467 if (!alignspec_ok)
2468 goto out;
2469 align = c_parser_alignas_specifier (parser);
2470 declspecs_add_alignas (loc, specs, align);
2471 break;
2472 default:
2473 goto out;
2476 out: ;
2479 /* Parse an enum specifier (C90 6.5.2.2, C99 6.7.2.2).
2481 enum-specifier:
2482 enum attributes[opt] identifier[opt] { enumerator-list } attributes[opt]
2483 enum attributes[opt] identifier[opt] { enumerator-list , } attributes[opt]
2484 enum attributes[opt] identifier
2486 The form with trailing comma is new in C99. The forms with
2487 attributes are GNU extensions. In GNU C, we accept any expression
2488 without commas in the syntax (assignment expressions, not just
2489 conditional expressions); assignment expressions will be diagnosed
2490 as non-constant.
2492 enumerator-list:
2493 enumerator
2494 enumerator-list , enumerator
2496 enumerator:
2497 enumeration-constant
2498 enumeration-constant = constant-expression
2501 static struct c_typespec
2502 c_parser_enum_specifier (c_parser *parser)
2504 struct c_typespec ret;
2505 tree attrs;
2506 tree ident = NULL_TREE;
2507 location_t enum_loc;
2508 location_t ident_loc = UNKNOWN_LOCATION; /* Quiet warning. */
2509 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ENUM));
2510 enum_loc = c_parser_peek_token (parser)->location;
2511 c_parser_consume_token (parser);
2512 attrs = c_parser_attributes (parser);
2513 enum_loc = c_parser_peek_token (parser)->location;
2514 /* Set the location in case we create a decl now. */
2515 c_parser_set_source_position_from_token (c_parser_peek_token (parser));
2516 if (c_parser_next_token_is (parser, CPP_NAME))
2518 ident = c_parser_peek_token (parser)->value;
2519 ident_loc = c_parser_peek_token (parser)->location;
2520 enum_loc = ident_loc;
2521 c_parser_consume_token (parser);
2523 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
2525 /* Parse an enum definition. */
2526 struct c_enum_contents the_enum;
2527 tree type;
2528 tree postfix_attrs;
2529 /* We chain the enumerators in reverse order, then put them in
2530 forward order at the end. */
2531 tree values;
2532 timevar_push (TV_PARSE_ENUM);
2533 type = start_enum (enum_loc, &the_enum, ident);
2534 values = NULL_TREE;
2535 c_parser_consume_token (parser);
2536 while (true)
2538 tree enum_id;
2539 tree enum_value;
2540 tree enum_decl;
2541 bool seen_comma;
2542 c_token *token;
2543 location_t comma_loc = UNKNOWN_LOCATION; /* Quiet warning. */
2544 location_t decl_loc, value_loc;
2545 if (c_parser_next_token_is_not (parser, CPP_NAME))
2547 c_parser_error (parser, "expected identifier");
2548 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
2549 values = error_mark_node;
2550 break;
2552 token = c_parser_peek_token (parser);
2553 enum_id = token->value;
2554 /* Set the location in case we create a decl now. */
2555 c_parser_set_source_position_from_token (token);
2556 decl_loc = value_loc = token->location;
2557 c_parser_consume_token (parser);
2558 if (c_parser_next_token_is (parser, CPP_EQ))
2560 c_parser_consume_token (parser);
2561 value_loc = c_parser_peek_token (parser)->location;
2562 enum_value = c_parser_expr_no_commas (parser, NULL).value;
2564 else
2565 enum_value = NULL_TREE;
2566 enum_decl = build_enumerator (decl_loc, value_loc,
2567 &the_enum, enum_id, enum_value);
2568 TREE_CHAIN (enum_decl) = values;
2569 values = enum_decl;
2570 seen_comma = false;
2571 if (c_parser_next_token_is (parser, CPP_COMMA))
2573 comma_loc = c_parser_peek_token (parser)->location;
2574 seen_comma = true;
2575 c_parser_consume_token (parser);
2577 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2579 if (seen_comma && !flag_isoc99)
2580 pedwarn (comma_loc, OPT_Wpedantic, "comma at end of enumerator list");
2581 c_parser_consume_token (parser);
2582 break;
2584 if (!seen_comma)
2586 c_parser_error (parser, "expected %<,%> or %<}%>");
2587 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
2588 values = error_mark_node;
2589 break;
2592 postfix_attrs = c_parser_attributes (parser);
2593 ret.spec = finish_enum (type, nreverse (values),
2594 chainon (attrs, postfix_attrs));
2595 ret.kind = ctsk_tagdef;
2596 ret.expr = NULL_TREE;
2597 ret.expr_const_operands = true;
2598 timevar_pop (TV_PARSE_ENUM);
2599 return ret;
2601 else if (!ident)
2603 c_parser_error (parser, "expected %<{%>");
2604 ret.spec = error_mark_node;
2605 ret.kind = ctsk_tagref;
2606 ret.expr = NULL_TREE;
2607 ret.expr_const_operands = true;
2608 return ret;
2610 ret = parser_xref_tag (ident_loc, ENUMERAL_TYPE, ident);
2611 /* In ISO C, enumerated types can be referred to only if already
2612 defined. */
2613 if (pedantic && !COMPLETE_TYPE_P (ret.spec))
2615 gcc_assert (ident);
2616 pedwarn (enum_loc, OPT_Wpedantic,
2617 "ISO C forbids forward references to %<enum%> types");
2619 return ret;
2622 /* Parse a struct or union specifier (C90 6.5.2.1, C99 6.7.2.1).
2624 struct-or-union-specifier:
2625 struct-or-union attributes[opt] identifier[opt]
2626 { struct-contents } attributes[opt]
2627 struct-or-union attributes[opt] identifier
2629 struct-contents:
2630 struct-declaration-list
2632 struct-declaration-list:
2633 struct-declaration ;
2634 struct-declaration-list struct-declaration ;
2636 GNU extensions:
2638 struct-contents:
2639 empty
2640 struct-declaration
2641 struct-declaration-list struct-declaration
2643 struct-declaration-list:
2644 struct-declaration-list ;
2647 (Note that in the syntax here, unlike that in ISO C, the semicolons
2648 are included here rather than in struct-declaration, in order to
2649 describe the syntax with extra semicolons and missing semicolon at
2650 end.)
2652 Objective-C:
2654 struct-declaration-list:
2655 @defs ( class-name )
2657 (Note this does not include a trailing semicolon, but can be
2658 followed by further declarations, and gets a pedwarn-if-pedantic
2659 when followed by a semicolon.) */
2661 static struct c_typespec
2662 c_parser_struct_or_union_specifier (c_parser *parser)
2664 struct c_typespec ret;
2665 tree attrs;
2666 tree ident = NULL_TREE;
2667 location_t struct_loc;
2668 location_t ident_loc = UNKNOWN_LOCATION;
2669 enum tree_code code;
2670 switch (c_parser_peek_token (parser)->keyword)
2672 case RID_STRUCT:
2673 code = RECORD_TYPE;
2674 break;
2675 case RID_UNION:
2676 code = UNION_TYPE;
2677 break;
2678 default:
2679 gcc_unreachable ();
2681 struct_loc = c_parser_peek_token (parser)->location;
2682 c_parser_consume_token (parser);
2683 attrs = c_parser_attributes (parser);
2685 /* Set the location in case we create a decl now. */
2686 c_parser_set_source_position_from_token (c_parser_peek_token (parser));
2688 if (c_parser_next_token_is (parser, CPP_NAME))
2690 ident = c_parser_peek_token (parser)->value;
2691 ident_loc = c_parser_peek_token (parser)->location;
2692 struct_loc = ident_loc;
2693 c_parser_consume_token (parser);
2695 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
2697 /* Parse a struct or union definition. Start the scope of the
2698 tag before parsing components. */
2699 struct c_struct_parse_info *struct_info;
2700 tree type = start_struct (struct_loc, code, ident, &struct_info);
2701 tree postfix_attrs;
2702 /* We chain the components in reverse order, then put them in
2703 forward order at the end. Each struct-declaration may
2704 declare multiple components (comma-separated), so we must use
2705 chainon to join them, although when parsing each
2706 struct-declaration we can use TREE_CHAIN directly.
2708 The theory behind all this is that there will be more
2709 semicolon separated fields than comma separated fields, and
2710 so we'll be minimizing the number of node traversals required
2711 by chainon. */
2712 tree contents;
2713 timevar_push (TV_PARSE_STRUCT);
2714 contents = NULL_TREE;
2715 c_parser_consume_token (parser);
2716 /* Handle the Objective-C @defs construct,
2717 e.g. foo(sizeof(struct{ @defs(ClassName) }));. */
2718 if (c_parser_next_token_is_keyword (parser, RID_AT_DEFS))
2720 tree name;
2721 gcc_assert (c_dialect_objc ());
2722 c_parser_consume_token (parser);
2723 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2724 goto end_at_defs;
2725 if (c_parser_next_token_is (parser, CPP_NAME)
2726 && c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME)
2728 name = c_parser_peek_token (parser)->value;
2729 c_parser_consume_token (parser);
2731 else
2733 c_parser_error (parser, "expected class name");
2734 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
2735 goto end_at_defs;
2737 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2738 "expected %<)%>");
2739 contents = nreverse (objc_get_class_ivars (name));
2741 end_at_defs:
2742 /* Parse the struct-declarations and semicolons. Problems with
2743 semicolons are diagnosed here; empty structures are diagnosed
2744 elsewhere. */
2745 while (true)
2747 tree decls;
2748 /* Parse any stray semicolon. */
2749 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
2751 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
2752 "extra semicolon in struct or union specified");
2753 c_parser_consume_token (parser);
2754 continue;
2756 /* Stop if at the end of the struct or union contents. */
2757 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2759 c_parser_consume_token (parser);
2760 break;
2762 /* Accept #pragmas at struct scope. */
2763 if (c_parser_next_token_is (parser, CPP_PRAGMA))
2765 c_parser_pragma (parser, pragma_struct);
2766 continue;
2768 /* Parse some comma-separated declarations, but not the
2769 trailing semicolon if any. */
2770 decls = c_parser_struct_declaration (parser);
2771 contents = chainon (decls, contents);
2772 /* If no semicolon follows, either we have a parse error or
2773 are at the end of the struct or union and should
2774 pedwarn. */
2775 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
2776 c_parser_consume_token (parser);
2777 else
2779 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2780 pedwarn (c_parser_peek_token (parser)->location, 0,
2781 "no semicolon at end of struct or union");
2782 else if (parser->error
2783 || !c_parser_next_token_starts_declspecs (parser))
2785 c_parser_error (parser, "expected %<;%>");
2786 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
2787 break;
2790 /* If we come here, we have already emitted an error
2791 for an expected `;', identifier or `(', and we also
2792 recovered already. Go on with the next field. */
2795 postfix_attrs = c_parser_attributes (parser);
2796 ret.spec = finish_struct (struct_loc, type, nreverse (contents),
2797 chainon (attrs, postfix_attrs), struct_info);
2798 ret.kind = ctsk_tagdef;
2799 ret.expr = NULL_TREE;
2800 ret.expr_const_operands = true;
2801 timevar_pop (TV_PARSE_STRUCT);
2802 return ret;
2804 else if (!ident)
2806 c_parser_error (parser, "expected %<{%>");
2807 ret.spec = error_mark_node;
2808 ret.kind = ctsk_tagref;
2809 ret.expr = NULL_TREE;
2810 ret.expr_const_operands = true;
2811 return ret;
2813 ret = parser_xref_tag (ident_loc, code, ident);
2814 return ret;
2817 /* Parse a struct-declaration (C90 6.5.2.1, C99 6.7.2.1), *without*
2818 the trailing semicolon.
2820 struct-declaration:
2821 specifier-qualifier-list struct-declarator-list
2822 static_assert-declaration-no-semi
2824 specifier-qualifier-list:
2825 type-specifier specifier-qualifier-list[opt]
2826 type-qualifier specifier-qualifier-list[opt]
2827 attributes specifier-qualifier-list[opt]
2829 struct-declarator-list:
2830 struct-declarator
2831 struct-declarator-list , attributes[opt] struct-declarator
2833 struct-declarator:
2834 declarator attributes[opt]
2835 declarator[opt] : constant-expression attributes[opt]
2837 GNU extensions:
2839 struct-declaration:
2840 __extension__ struct-declaration
2841 specifier-qualifier-list
2843 Unlike the ISO C syntax, semicolons are handled elsewhere. The use
2844 of attributes where shown is a GNU extension. In GNU C, we accept
2845 any expression without commas in the syntax (assignment
2846 expressions, not just conditional expressions); assignment
2847 expressions will be diagnosed as non-constant. */
2849 static tree
2850 c_parser_struct_declaration (c_parser *parser)
2852 struct c_declspecs *specs;
2853 tree prefix_attrs;
2854 tree all_prefix_attrs;
2855 tree decls;
2856 location_t decl_loc;
2857 if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
2859 int ext;
2860 tree decl;
2861 ext = disable_extension_diagnostics ();
2862 c_parser_consume_token (parser);
2863 decl = c_parser_struct_declaration (parser);
2864 restore_extension_diagnostics (ext);
2865 return decl;
2867 if (c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT))
2869 c_parser_static_assert_declaration_no_semi (parser);
2870 return NULL_TREE;
2872 specs = build_null_declspecs ();
2873 decl_loc = c_parser_peek_token (parser)->location;
2874 /* Strictly by the standard, we shouldn't allow _Alignas here,
2875 but it appears to have been intended to allow it there, so
2876 we're keeping it as it is until WG14 reaches a conclusion
2877 of N1731.
2878 <http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1731.pdf> */
2879 c_parser_declspecs (parser, specs, false, true, true,
2880 true, false, cla_nonabstract_decl);
2881 if (parser->error)
2882 return NULL_TREE;
2883 if (!specs->declspecs_seen_p)
2885 c_parser_error (parser, "expected specifier-qualifier-list");
2886 return NULL_TREE;
2888 finish_declspecs (specs);
2889 if (c_parser_next_token_is (parser, CPP_SEMICOLON)
2890 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2892 tree ret;
2893 if (specs->typespec_kind == ctsk_none)
2895 pedwarn (decl_loc, OPT_Wpedantic,
2896 "ISO C forbids member declarations with no members");
2897 shadow_tag_warned (specs, pedantic);
2898 ret = NULL_TREE;
2900 else
2902 /* Support for unnamed structs or unions as members of
2903 structs or unions (which is [a] useful and [b] supports
2904 MS P-SDK). */
2905 tree attrs = NULL;
2907 ret = grokfield (c_parser_peek_token (parser)->location,
2908 build_id_declarator (NULL_TREE), specs,
2909 NULL_TREE, &attrs);
2910 if (ret)
2911 decl_attributes (&ret, attrs, 0);
2913 return ret;
2916 /* Provide better error recovery. Note that a type name here is valid,
2917 and will be treated as a field name. */
2918 if (specs->typespec_kind == ctsk_tagdef
2919 && TREE_CODE (specs->type) != ENUMERAL_TYPE
2920 && c_parser_next_token_starts_declspecs (parser)
2921 && !c_parser_next_token_is (parser, CPP_NAME))
2923 c_parser_error (parser, "expected %<;%>, identifier or %<(%>");
2924 parser->error = false;
2925 return NULL_TREE;
2928 pending_xref_error ();
2929 prefix_attrs = specs->attrs;
2930 all_prefix_attrs = prefix_attrs;
2931 specs->attrs = NULL_TREE;
2932 decls = NULL_TREE;
2933 while (true)
2935 /* Declaring one or more declarators or un-named bit-fields. */
2936 struct c_declarator *declarator;
2937 bool dummy = false;
2938 if (c_parser_next_token_is (parser, CPP_COLON))
2939 declarator = build_id_declarator (NULL_TREE);
2940 else
2941 declarator = c_parser_declarator (parser,
2942 specs->typespec_kind != ctsk_none,
2943 C_DTR_NORMAL, &dummy);
2944 if (declarator == NULL)
2946 c_parser_skip_to_end_of_block_or_statement (parser);
2947 break;
2949 if (c_parser_next_token_is (parser, CPP_COLON)
2950 || c_parser_next_token_is (parser, CPP_COMMA)
2951 || c_parser_next_token_is (parser, CPP_SEMICOLON)
2952 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE)
2953 || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2955 tree postfix_attrs = NULL_TREE;
2956 tree width = NULL_TREE;
2957 tree d;
2958 if (c_parser_next_token_is (parser, CPP_COLON))
2960 c_parser_consume_token (parser);
2961 width = c_parser_expr_no_commas (parser, NULL).value;
2963 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2964 postfix_attrs = c_parser_attributes (parser);
2965 d = grokfield (c_parser_peek_token (parser)->location,
2966 declarator, specs, width, &all_prefix_attrs);
2967 decl_attributes (&d, chainon (postfix_attrs,
2968 all_prefix_attrs), 0);
2969 DECL_CHAIN (d) = decls;
2970 decls = d;
2971 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2972 all_prefix_attrs = chainon (c_parser_attributes (parser),
2973 prefix_attrs);
2974 else
2975 all_prefix_attrs = prefix_attrs;
2976 if (c_parser_next_token_is (parser, CPP_COMMA))
2977 c_parser_consume_token (parser);
2978 else if (c_parser_next_token_is (parser, CPP_SEMICOLON)
2979 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2981 /* Semicolon consumed in caller. */
2982 break;
2984 else
2986 c_parser_error (parser, "expected %<,%>, %<;%> or %<}%>");
2987 break;
2990 else
2992 c_parser_error (parser,
2993 "expected %<:%>, %<,%>, %<;%>, %<}%> or "
2994 "%<__attribute__%>");
2995 break;
2998 return decls;
3001 /* Parse a typeof specifier (a GNU extension).
3003 typeof-specifier:
3004 typeof ( expression )
3005 typeof ( type-name )
3008 static struct c_typespec
3009 c_parser_typeof_specifier (c_parser *parser)
3011 struct c_typespec ret;
3012 ret.kind = ctsk_typeof;
3013 ret.spec = error_mark_node;
3014 ret.expr = NULL_TREE;
3015 ret.expr_const_operands = true;
3016 gcc_assert (c_parser_next_token_is_keyword (parser, RID_TYPEOF));
3017 c_parser_consume_token (parser);
3018 c_inhibit_evaluation_warnings++;
3019 in_typeof++;
3020 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3022 c_inhibit_evaluation_warnings--;
3023 in_typeof--;
3024 return ret;
3026 if (c_parser_next_tokens_start_typename (parser, cla_prefer_id))
3028 struct c_type_name *type = c_parser_type_name (parser);
3029 c_inhibit_evaluation_warnings--;
3030 in_typeof--;
3031 if (type != NULL)
3033 ret.spec = groktypename (type, &ret.expr, &ret.expr_const_operands);
3034 pop_maybe_used (variably_modified_type_p (ret.spec, NULL_TREE));
3037 else
3039 bool was_vm;
3040 location_t here = c_parser_peek_token (parser)->location;
3041 struct c_expr expr = c_parser_expression (parser);
3042 c_inhibit_evaluation_warnings--;
3043 in_typeof--;
3044 if (TREE_CODE (expr.value) == COMPONENT_REF
3045 && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
3046 error_at (here, "%<typeof%> applied to a bit-field");
3047 mark_exp_read (expr.value);
3048 ret.spec = TREE_TYPE (expr.value);
3049 was_vm = variably_modified_type_p (ret.spec, NULL_TREE);
3050 /* This is returned with the type so that when the type is
3051 evaluated, this can be evaluated. */
3052 if (was_vm)
3053 ret.expr = c_fully_fold (expr.value, false, &ret.expr_const_operands);
3054 pop_maybe_used (was_vm);
3055 /* For use in macros such as those in <stdatomic.h>, remove
3056 _Atomic and const qualifiers from atomic types. (Possibly
3057 all qualifiers should be removed; const can be an issue for
3058 more macros using typeof than just the <stdatomic.h>
3059 ones.) */
3060 if (ret.spec != error_mark_node && TYPE_ATOMIC (ret.spec))
3061 ret.spec = c_build_qualified_type (ret.spec,
3062 (TYPE_QUALS (ret.spec)
3063 & ~(TYPE_QUAL_ATOMIC
3064 | TYPE_QUAL_CONST)));
3066 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
3067 return ret;
3070 /* Parse an alignment-specifier.
3072 C11 6.7.5:
3074 alignment-specifier:
3075 _Alignas ( type-name )
3076 _Alignas ( constant-expression )
3079 static tree
3080 c_parser_alignas_specifier (c_parser * parser)
3082 tree ret = error_mark_node;
3083 location_t loc = c_parser_peek_token (parser)->location;
3084 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ALIGNAS));
3085 c_parser_consume_token (parser);
3086 if (!flag_isoc11)
3088 if (flag_isoc99)
3089 pedwarn (loc, OPT_Wpedantic,
3090 "ISO C99 does not support %<_Alignas%>");
3091 else
3092 pedwarn (loc, OPT_Wpedantic,
3093 "ISO C90 does not support %<_Alignas%>");
3095 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3096 return ret;
3097 if (c_parser_next_tokens_start_typename (parser, cla_prefer_id))
3099 struct c_type_name *type = c_parser_type_name (parser);
3100 if (type != NULL)
3101 ret = c_sizeof_or_alignof_type (loc, groktypename (type, NULL, NULL),
3102 false, true, 1);
3104 else
3105 ret = c_parser_expr_no_commas (parser, NULL).value;
3106 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
3107 return ret;
3110 /* Parse a declarator, possibly an abstract declarator (C90 6.5.4,
3111 6.5.5, C99 6.7.5, 6.7.6). If TYPE_SEEN_P then a typedef name may
3112 be redeclared; otherwise it may not. KIND indicates which kind of
3113 declarator is wanted. Returns a valid declarator except in the
3114 case of a syntax error in which case NULL is returned. *SEEN_ID is
3115 set to true if an identifier being declared is seen; this is used
3116 to diagnose bad forms of abstract array declarators and to
3117 determine whether an identifier list is syntactically permitted.
3119 declarator:
3120 pointer[opt] direct-declarator
3122 direct-declarator:
3123 identifier
3124 ( attributes[opt] declarator )
3125 direct-declarator array-declarator
3126 direct-declarator ( parameter-type-list )
3127 direct-declarator ( identifier-list[opt] )
3129 pointer:
3130 * type-qualifier-list[opt]
3131 * type-qualifier-list[opt] pointer
3133 type-qualifier-list:
3134 type-qualifier
3135 attributes
3136 type-qualifier-list type-qualifier
3137 type-qualifier-list attributes
3139 array-declarator:
3140 [ type-qualifier-list[opt] assignment-expression[opt] ]
3141 [ static type-qualifier-list[opt] assignment-expression ]
3142 [ type-qualifier-list static assignment-expression ]
3143 [ type-qualifier-list[opt] * ]
3145 parameter-type-list:
3146 parameter-list
3147 parameter-list , ...
3149 parameter-list:
3150 parameter-declaration
3151 parameter-list , parameter-declaration
3153 parameter-declaration:
3154 declaration-specifiers declarator attributes[opt]
3155 declaration-specifiers abstract-declarator[opt] attributes[opt]
3157 identifier-list:
3158 identifier
3159 identifier-list , identifier
3161 abstract-declarator:
3162 pointer
3163 pointer[opt] direct-abstract-declarator
3165 direct-abstract-declarator:
3166 ( attributes[opt] abstract-declarator )
3167 direct-abstract-declarator[opt] array-declarator
3168 direct-abstract-declarator[opt] ( parameter-type-list[opt] )
3170 GNU extensions:
3172 direct-declarator:
3173 direct-declarator ( parameter-forward-declarations
3174 parameter-type-list[opt] )
3176 direct-abstract-declarator:
3177 direct-abstract-declarator[opt] ( parameter-forward-declarations
3178 parameter-type-list[opt] )
3180 parameter-forward-declarations:
3181 parameter-list ;
3182 parameter-forward-declarations parameter-list ;
3184 The uses of attributes shown above are GNU extensions.
3186 Some forms of array declarator are not included in C99 in the
3187 syntax for abstract declarators; these are disallowed elsewhere.
3188 This may be a defect (DR#289).
3190 This function also accepts an omitted abstract declarator as being
3191 an abstract declarator, although not part of the formal syntax. */
3193 static struct c_declarator *
3194 c_parser_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
3195 bool *seen_id)
3197 /* Parse any initial pointer part. */
3198 if (c_parser_next_token_is (parser, CPP_MULT))
3200 struct c_declspecs *quals_attrs = build_null_declspecs ();
3201 struct c_declarator *inner;
3202 c_parser_consume_token (parser);
3203 c_parser_declspecs (parser, quals_attrs, false, false, true,
3204 false, false, cla_prefer_id);
3205 inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
3206 if (inner == NULL)
3207 return NULL;
3208 else
3209 return make_pointer_declarator (quals_attrs, inner);
3211 /* Now we have a direct declarator, direct abstract declarator or
3212 nothing (which counts as a direct abstract declarator here). */
3213 return c_parser_direct_declarator (parser, type_seen_p, kind, seen_id);
3216 /* Parse a direct declarator or direct abstract declarator; arguments
3217 as c_parser_declarator. */
3219 static struct c_declarator *
3220 c_parser_direct_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
3221 bool *seen_id)
3223 /* The direct declarator must start with an identifier (possibly
3224 omitted) or a parenthesized declarator (possibly abstract). In
3225 an ordinary declarator, initial parentheses must start a
3226 parenthesized declarator. In an abstract declarator or parameter
3227 declarator, they could start a parenthesized declarator or a
3228 parameter list. To tell which, the open parenthesis and any
3229 following attributes must be read. If a declaration specifier
3230 follows, then it is a parameter list; if the specifier is a
3231 typedef name, there might be an ambiguity about redeclaring it,
3232 which is resolved in the direction of treating it as a typedef
3233 name. If a close parenthesis follows, it is also an empty
3234 parameter list, as the syntax does not permit empty abstract
3235 declarators. Otherwise, it is a parenthesized declarator (in
3236 which case the analysis may be repeated inside it, recursively).
3238 ??? There is an ambiguity in a parameter declaration "int
3239 (__attribute__((foo)) x)", where x is not a typedef name: it
3240 could be an abstract declarator for a function, or declare x with
3241 parentheses. The proper resolution of this ambiguity needs
3242 documenting. At present we follow an accident of the old
3243 parser's implementation, whereby the first parameter must have
3244 some declaration specifiers other than just attributes. Thus as
3245 a parameter declaration it is treated as a parenthesized
3246 parameter named x, and as an abstract declarator it is
3247 rejected.
3249 ??? Also following the old parser, attributes inside an empty
3250 parameter list are ignored, making it a list not yielding a
3251 prototype, rather than giving an error or making it have one
3252 parameter with implicit type int.
3254 ??? Also following the old parser, typedef names may be
3255 redeclared in declarators, but not Objective-C class names. */
3257 if (kind != C_DTR_ABSTRACT
3258 && c_parser_next_token_is (parser, CPP_NAME)
3259 && ((type_seen_p
3260 && (c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME
3261 || c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME))
3262 || c_parser_peek_token (parser)->id_kind == C_ID_ID))
3264 struct c_declarator *inner
3265 = build_id_declarator (c_parser_peek_token (parser)->value);
3266 *seen_id = true;
3267 inner->id_loc = c_parser_peek_token (parser)->location;
3268 c_parser_consume_token (parser);
3269 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
3272 if (kind != C_DTR_NORMAL
3273 && c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
3275 struct c_declarator *inner = build_id_declarator (NULL_TREE);
3276 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
3279 /* Either we are at the end of an abstract declarator, or we have
3280 parentheses. */
3282 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
3284 tree attrs;
3285 struct c_declarator *inner;
3286 c_parser_consume_token (parser);
3287 attrs = c_parser_attributes (parser);
3288 if (kind != C_DTR_NORMAL
3289 && (c_parser_next_token_starts_declspecs (parser)
3290 || c_parser_next_token_is (parser, CPP_CLOSE_PAREN)))
3292 struct c_arg_info *args
3293 = c_parser_parms_declarator (parser, kind == C_DTR_NORMAL,
3294 attrs);
3295 if (args == NULL)
3296 return NULL;
3297 else
3299 inner
3300 = build_function_declarator (args,
3301 build_id_declarator (NULL_TREE));
3302 return c_parser_direct_declarator_inner (parser, *seen_id,
3303 inner);
3306 /* A parenthesized declarator. */
3307 inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
3308 if (inner != NULL && attrs != NULL)
3309 inner = build_attrs_declarator (attrs, inner);
3310 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3312 c_parser_consume_token (parser);
3313 if (inner == NULL)
3314 return NULL;
3315 else
3316 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
3318 else
3320 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3321 "expected %<)%>");
3322 return NULL;
3325 else
3327 if (kind == C_DTR_NORMAL)
3329 c_parser_error (parser, "expected identifier or %<(%>");
3330 return NULL;
3332 else
3333 return build_id_declarator (NULL_TREE);
3337 /* Parse part of a direct declarator or direct abstract declarator,
3338 given that some (in INNER) has already been parsed; ID_PRESENT is
3339 true if an identifier is present, false for an abstract
3340 declarator. */
3342 static struct c_declarator *
3343 c_parser_direct_declarator_inner (c_parser *parser, bool id_present,
3344 struct c_declarator *inner)
3346 /* Parse a sequence of array declarators and parameter lists. */
3347 if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
3349 location_t brace_loc = c_parser_peek_token (parser)->location;
3350 struct c_declarator *declarator;
3351 struct c_declspecs *quals_attrs = build_null_declspecs ();
3352 bool static_seen;
3353 bool star_seen;
3354 struct c_expr dimen;
3355 dimen.value = NULL_TREE;
3356 dimen.original_code = ERROR_MARK;
3357 dimen.original_type = NULL_TREE;
3358 c_parser_consume_token (parser);
3359 c_parser_declspecs (parser, quals_attrs, false, false, true,
3360 false, false, cla_prefer_id);
3361 static_seen = c_parser_next_token_is_keyword (parser, RID_STATIC);
3362 if (static_seen)
3363 c_parser_consume_token (parser);
3364 if (static_seen && !quals_attrs->declspecs_seen_p)
3365 c_parser_declspecs (parser, quals_attrs, false, false, true,
3366 false, false, cla_prefer_id);
3367 if (!quals_attrs->declspecs_seen_p)
3368 quals_attrs = NULL;
3369 /* If "static" is present, there must be an array dimension.
3370 Otherwise, there may be a dimension, "*", or no
3371 dimension. */
3372 if (static_seen)
3374 star_seen = false;
3375 dimen = c_parser_expr_no_commas (parser, NULL);
3377 else
3379 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3381 dimen.value = NULL_TREE;
3382 star_seen = false;
3384 else if (flag_cilkplus
3385 && c_parser_next_token_is (parser, CPP_COLON))
3387 dimen.value = error_mark_node;
3388 star_seen = false;
3389 error_at (c_parser_peek_token (parser)->location,
3390 "array notations cannot be used in declaration");
3391 c_parser_consume_token (parser);
3393 else if (c_parser_next_token_is (parser, CPP_MULT))
3395 if (c_parser_peek_2nd_token (parser)->type == CPP_CLOSE_SQUARE)
3397 dimen.value = NULL_TREE;
3398 star_seen = true;
3399 c_parser_consume_token (parser);
3401 else
3403 star_seen = false;
3404 dimen = c_parser_expr_no_commas (parser, NULL);
3407 else
3409 star_seen = false;
3410 dimen = c_parser_expr_no_commas (parser, NULL);
3413 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3414 c_parser_consume_token (parser);
3415 else if (flag_cilkplus
3416 && c_parser_next_token_is (parser, CPP_COLON))
3418 error_at (c_parser_peek_token (parser)->location,
3419 "array notations cannot be used in declaration");
3420 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
3421 return NULL;
3423 else
3425 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
3426 "expected %<]%>");
3427 return NULL;
3429 if (dimen.value)
3430 dimen = convert_lvalue_to_rvalue (brace_loc, dimen, true, true);
3431 declarator = build_array_declarator (brace_loc, dimen.value, quals_attrs,
3432 static_seen, star_seen);
3433 if (declarator == NULL)
3434 return NULL;
3435 inner = set_array_declarator_inner (declarator, inner);
3436 return c_parser_direct_declarator_inner (parser, id_present, inner);
3438 else if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
3440 tree attrs;
3441 struct c_arg_info *args;
3442 c_parser_consume_token (parser);
3443 attrs = c_parser_attributes (parser);
3444 args = c_parser_parms_declarator (parser, id_present, attrs);
3445 if (args == NULL)
3446 return NULL;
3447 else
3449 inner = build_function_declarator (args, inner);
3450 return c_parser_direct_declarator_inner (parser, id_present, inner);
3453 return inner;
3456 /* Parse a parameter list or identifier list, including the closing
3457 parenthesis but not the opening one. ATTRS are the attributes at
3458 the start of the list. ID_LIST_OK is true if an identifier list is
3459 acceptable; such a list must not have attributes at the start. */
3461 static struct c_arg_info *
3462 c_parser_parms_declarator (c_parser *parser, bool id_list_ok, tree attrs)
3464 push_scope ();
3465 declare_parm_level ();
3466 /* If the list starts with an identifier, it is an identifier list.
3467 Otherwise, it is either a prototype list or an empty list. */
3468 if (id_list_ok
3469 && !attrs
3470 && c_parser_next_token_is (parser, CPP_NAME)
3471 && c_parser_peek_token (parser)->id_kind == C_ID_ID
3473 /* Look ahead to detect typos in type names. */
3474 && c_parser_peek_2nd_token (parser)->type != CPP_NAME
3475 && c_parser_peek_2nd_token (parser)->type != CPP_MULT
3476 && c_parser_peek_2nd_token (parser)->type != CPP_OPEN_PAREN
3477 && c_parser_peek_2nd_token (parser)->type != CPP_OPEN_SQUARE)
3479 tree list = NULL_TREE, *nextp = &list;
3480 while (c_parser_next_token_is (parser, CPP_NAME)
3481 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
3483 *nextp = build_tree_list (NULL_TREE,
3484 c_parser_peek_token (parser)->value);
3485 nextp = & TREE_CHAIN (*nextp);
3486 c_parser_consume_token (parser);
3487 if (c_parser_next_token_is_not (parser, CPP_COMMA))
3488 break;
3489 c_parser_consume_token (parser);
3490 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3492 c_parser_error (parser, "expected identifier");
3493 break;
3496 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3498 struct c_arg_info *ret = build_arg_info ();
3499 ret->types = list;
3500 c_parser_consume_token (parser);
3501 pop_scope ();
3502 return ret;
3504 else
3506 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3507 "expected %<)%>");
3508 pop_scope ();
3509 return NULL;
3512 else
3514 struct c_arg_info *ret = c_parser_parms_list_declarator (parser, attrs,
3515 NULL);
3516 pop_scope ();
3517 return ret;
3521 /* Parse a parameter list (possibly empty), including the closing
3522 parenthesis but not the opening one. ATTRS are the attributes at
3523 the start of the list. EXPR is NULL or an expression that needs to
3524 be evaluated for the side effects of array size expressions in the
3525 parameters. */
3527 static struct c_arg_info *
3528 c_parser_parms_list_declarator (c_parser *parser, tree attrs, tree expr)
3530 bool bad_parm = false;
3532 /* ??? Following the old parser, forward parameter declarations may
3533 use abstract declarators, and if no real parameter declarations
3534 follow the forward declarations then this is not diagnosed. Also
3535 note as above that attributes are ignored as the only contents of
3536 the parentheses, or as the only contents after forward
3537 declarations. */
3538 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3540 struct c_arg_info *ret = build_arg_info ();
3541 c_parser_consume_token (parser);
3542 return ret;
3544 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3546 struct c_arg_info *ret = build_arg_info ();
3548 if (flag_allow_parameterless_variadic_functions)
3550 /* F (...) is allowed. */
3551 ret->types = NULL_TREE;
3553 else
3555 /* Suppress -Wold-style-definition for this case. */
3556 ret->types = error_mark_node;
3557 error_at (c_parser_peek_token (parser)->location,
3558 "ISO C requires a named argument before %<...%>");
3560 c_parser_consume_token (parser);
3561 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3563 c_parser_consume_token (parser);
3564 return ret;
3566 else
3568 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3569 "expected %<)%>");
3570 return NULL;
3573 /* Nonempty list of parameters, either terminated with semicolon
3574 (forward declarations; recurse) or with close parenthesis (normal
3575 function) or with ", ... )" (variadic function). */
3576 while (true)
3578 /* Parse a parameter. */
3579 struct c_parm *parm = c_parser_parameter_declaration (parser, attrs);
3580 attrs = NULL_TREE;
3581 if (parm == NULL)
3582 bad_parm = true;
3583 else
3584 push_parm_decl (parm, &expr);
3585 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
3587 tree new_attrs;
3588 c_parser_consume_token (parser);
3589 mark_forward_parm_decls ();
3590 new_attrs = c_parser_attributes (parser);
3591 return c_parser_parms_list_declarator (parser, new_attrs, expr);
3593 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3595 c_parser_consume_token (parser);
3596 if (bad_parm)
3597 return NULL;
3598 else
3599 return get_parm_info (false, expr);
3601 if (!c_parser_require (parser, CPP_COMMA,
3602 "expected %<;%>, %<,%> or %<)%>"))
3604 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
3605 return NULL;
3607 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3609 c_parser_consume_token (parser);
3610 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3612 c_parser_consume_token (parser);
3613 if (bad_parm)
3614 return NULL;
3615 else
3616 return get_parm_info (true, expr);
3618 else
3620 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3621 "expected %<)%>");
3622 return NULL;
3628 /* Parse a parameter declaration. ATTRS are the attributes at the
3629 start of the declaration if it is the first parameter. */
3631 static struct c_parm *
3632 c_parser_parameter_declaration (c_parser *parser, tree attrs)
3634 struct c_declspecs *specs;
3635 struct c_declarator *declarator;
3636 tree prefix_attrs;
3637 tree postfix_attrs = NULL_TREE;
3638 bool dummy = false;
3640 /* Accept #pragmas between parameter declarations. */
3641 while (c_parser_next_token_is (parser, CPP_PRAGMA))
3642 c_parser_pragma (parser, pragma_param);
3644 if (!c_parser_next_token_starts_declspecs (parser))
3646 c_token *token = c_parser_peek_token (parser);
3647 if (parser->error)
3648 return NULL;
3649 c_parser_set_source_position_from_token (token);
3650 if (c_parser_next_tokens_start_typename (parser, cla_prefer_type))
3652 error_at (token->location, "unknown type name %qE", token->value);
3653 parser->error = true;
3655 /* ??? In some Objective-C cases '...' isn't applicable so there
3656 should be a different message. */
3657 else
3658 c_parser_error (parser,
3659 "expected declaration specifiers or %<...%>");
3660 c_parser_skip_to_end_of_parameter (parser);
3661 return NULL;
3663 specs = build_null_declspecs ();
3664 if (attrs)
3666 declspecs_add_attrs (input_location, specs, attrs);
3667 attrs = NULL_TREE;
3669 c_parser_declspecs (parser, specs, true, true, true, true, false,
3670 cla_nonabstract_decl);
3671 finish_declspecs (specs);
3672 pending_xref_error ();
3673 prefix_attrs = specs->attrs;
3674 specs->attrs = NULL_TREE;
3675 declarator = c_parser_declarator (parser,
3676 specs->typespec_kind != ctsk_none,
3677 C_DTR_PARM, &dummy);
3678 if (declarator == NULL)
3680 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
3681 return NULL;
3683 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
3684 postfix_attrs = c_parser_attributes (parser);
3685 return build_c_parm (specs, chainon (postfix_attrs, prefix_attrs),
3686 declarator);
3689 /* Parse a string literal in an asm expression. It should not be
3690 translated, and wide string literals are an error although
3691 permitted by the syntax. This is a GNU extension.
3693 asm-string-literal:
3694 string-literal
3696 ??? At present, following the old parser, the caller needs to have
3697 set lex_untranslated_string to 1. It would be better to follow the
3698 C++ parser rather than using this kludge. */
3700 static tree
3701 c_parser_asm_string_literal (c_parser *parser)
3703 tree str;
3704 int save_flag = warn_overlength_strings;
3705 warn_overlength_strings = 0;
3706 if (c_parser_next_token_is (parser, CPP_STRING))
3708 str = c_parser_peek_token (parser)->value;
3709 c_parser_consume_token (parser);
3711 else if (c_parser_next_token_is (parser, CPP_WSTRING))
3713 error_at (c_parser_peek_token (parser)->location,
3714 "wide string literal in %<asm%>");
3715 str = build_string (1, "");
3716 c_parser_consume_token (parser);
3718 else
3720 c_parser_error (parser, "expected string literal");
3721 str = NULL_TREE;
3723 warn_overlength_strings = save_flag;
3724 return str;
3727 /* Parse a simple asm expression. This is used in restricted
3728 contexts, where a full expression with inputs and outputs does not
3729 make sense. This is a GNU extension.
3731 simple-asm-expr:
3732 asm ( asm-string-literal )
3735 static tree
3736 c_parser_simple_asm_expr (c_parser *parser)
3738 tree str;
3739 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
3740 /* ??? Follow the C++ parser rather than using the
3741 lex_untranslated_string kludge. */
3742 parser->lex_untranslated_string = true;
3743 c_parser_consume_token (parser);
3744 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3746 parser->lex_untranslated_string = false;
3747 return NULL_TREE;
3749 str = c_parser_asm_string_literal (parser);
3750 parser->lex_untranslated_string = false;
3751 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
3753 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
3754 return NULL_TREE;
3756 return str;
3759 static tree
3760 c_parser_attribute_any_word (c_parser *parser)
3762 tree attr_name = NULL_TREE;
3764 if (c_parser_next_token_is (parser, CPP_KEYWORD))
3766 /* ??? See comment above about what keywords are accepted here. */
3767 bool ok;
3768 switch (c_parser_peek_token (parser)->keyword)
3770 case RID_STATIC:
3771 case RID_UNSIGNED:
3772 case RID_LONG:
3773 case RID_INT128:
3774 case RID_CONST:
3775 case RID_EXTERN:
3776 case RID_REGISTER:
3777 case RID_TYPEDEF:
3778 case RID_SHORT:
3779 case RID_INLINE:
3780 case RID_NORETURN:
3781 case RID_VOLATILE:
3782 case RID_SIGNED:
3783 case RID_AUTO:
3784 case RID_RESTRICT:
3785 case RID_COMPLEX:
3786 case RID_THREAD:
3787 case RID_INT:
3788 case RID_CHAR:
3789 case RID_FLOAT:
3790 case RID_DOUBLE:
3791 case RID_VOID:
3792 case RID_DFLOAT32:
3793 case RID_DFLOAT64:
3794 case RID_DFLOAT128:
3795 case RID_BOOL:
3796 case RID_FRACT:
3797 case RID_ACCUM:
3798 case RID_SAT:
3799 case RID_TRANSACTION_ATOMIC:
3800 case RID_TRANSACTION_CANCEL:
3801 case RID_ATOMIC:
3802 case RID_AUTO_TYPE:
3803 ok = true;
3804 break;
3805 default:
3806 ok = false;
3807 break;
3809 if (!ok)
3810 return NULL_TREE;
3812 /* Accept __attribute__((__const)) as __attribute__((const)) etc. */
3813 attr_name = ridpointers[(int) c_parser_peek_token (parser)->keyword];
3815 else if (c_parser_next_token_is (parser, CPP_NAME))
3816 attr_name = c_parser_peek_token (parser)->value;
3818 return attr_name;
3821 /* Returns true of NAME is an IDENTIFIER_NODE with identiifer "vector,"
3822 "__vector" or "__vector__." */
3824 static inline bool
3825 is_cilkplus_vector_p (tree name)
3827 if (flag_cilkplus && is_attribute_p ("vector", name))
3828 return true;
3829 return false;
3832 #define CILK_SIMD_FN_CLAUSE_MASK \
3833 ((OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_VECTORLENGTH) \
3834 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_LINEAR) \
3835 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_UNIFORM) \
3836 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_MASK) \
3837 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_NOMASK))
3839 /* Parses the vector attribute of SIMD enabled functions in Cilk Plus.
3840 VEC_TOKEN is the "vector" token that is replaced with "simd" and
3841 pushed into the token list.
3842 Syntax:
3843 vector
3844 vector (<vector attributes>). */
3846 static void
3847 c_parser_cilk_simd_fn_vector_attrs (c_parser *parser, c_token vec_token)
3849 gcc_assert (is_cilkplus_vector_p (vec_token.value));
3851 int paren_scope = 0;
3852 vec_safe_push (parser->cilk_simd_fn_tokens, vec_token);
3853 /* Consume the "vector" token. */
3854 c_parser_consume_token (parser);
3856 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
3858 c_parser_consume_token (parser);
3859 paren_scope++;
3861 while (paren_scope > 0)
3863 c_token *token = c_parser_peek_token (parser);
3864 if (token->type == CPP_OPEN_PAREN)
3865 paren_scope++;
3866 else if (token->type == CPP_CLOSE_PAREN)
3867 paren_scope--;
3868 /* Do not push the last ')' since we are not pushing the '('. */
3869 if (!(token->type == CPP_CLOSE_PAREN && paren_scope == 0))
3870 vec_safe_push (parser->cilk_simd_fn_tokens, *token);
3871 c_parser_consume_token (parser);
3874 /* Since we are converting an attribute to a pragma, we need to end the
3875 attribute with PRAGMA_EOL. */
3876 c_token eol_token;
3877 memset (&eol_token, 0, sizeof (eol_token));
3878 eol_token.type = CPP_PRAGMA_EOL;
3879 vec_safe_push (parser->cilk_simd_fn_tokens, eol_token);
3882 /* Add 2 CPP_EOF at the end of PARSER->ELEM_FN_TOKENS vector. */
3884 static void
3885 c_finish_cilk_simd_fn_tokens (c_parser *parser)
3887 c_token last_token = parser->cilk_simd_fn_tokens->last ();
3889 /* c_parser_attributes is called in several places, so if these EOF
3890 tokens are already inserted, then don't do them again. */
3891 if (last_token.type == CPP_EOF)
3892 return;
3894 /* Two CPP_EOF token are added as a safety net since the normal C
3895 front-end has two token look-ahead. */
3896 c_token eof_token;
3897 eof_token.type = CPP_EOF;
3898 vec_safe_push (parser->cilk_simd_fn_tokens, eof_token);
3899 vec_safe_push (parser->cilk_simd_fn_tokens, eof_token);
3902 /* Parse (possibly empty) attributes. This is a GNU extension.
3904 attributes:
3905 empty
3906 attributes attribute
3908 attribute:
3909 __attribute__ ( ( attribute-list ) )
3911 attribute-list:
3912 attrib
3913 attribute_list , attrib
3915 attrib:
3916 empty
3917 any-word
3918 any-word ( identifier )
3919 any-word ( identifier , nonempty-expr-list )
3920 any-word ( expr-list )
3922 where the "identifier" must not be declared as a type, and
3923 "any-word" may be any identifier (including one declared as a
3924 type), a reserved word storage class specifier, type specifier or
3925 type qualifier. ??? This still leaves out most reserved keywords
3926 (following the old parser), shouldn't we include them, and why not
3927 allow identifiers declared as types to start the arguments? */
3929 static tree
3930 c_parser_attributes (c_parser *parser)
3932 tree attrs = NULL_TREE;
3933 while (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
3935 /* ??? Follow the C++ parser rather than using the
3936 lex_untranslated_string kludge. */
3937 parser->lex_untranslated_string = true;
3938 c_parser_consume_token (parser);
3939 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3941 parser->lex_untranslated_string = false;
3942 return attrs;
3944 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3946 parser->lex_untranslated_string = false;
3947 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
3948 return attrs;
3950 /* Parse the attribute list. */
3951 while (c_parser_next_token_is (parser, CPP_COMMA)
3952 || c_parser_next_token_is (parser, CPP_NAME)
3953 || c_parser_next_token_is (parser, CPP_KEYWORD))
3955 tree attr, attr_name, attr_args;
3956 vec<tree, va_gc> *expr_list;
3957 if (c_parser_next_token_is (parser, CPP_COMMA))
3959 c_parser_consume_token (parser);
3960 continue;
3963 attr_name = c_parser_attribute_any_word (parser);
3964 if (attr_name == NULL)
3965 break;
3966 if (is_cilkplus_vector_p (attr_name))
3968 c_token *v_token = c_parser_peek_token (parser);
3969 c_parser_cilk_simd_fn_vector_attrs (parser, *v_token);
3970 continue;
3972 c_parser_consume_token (parser);
3973 if (c_parser_next_token_is_not (parser, CPP_OPEN_PAREN))
3975 attr = build_tree_list (attr_name, NULL_TREE);
3976 attrs = chainon (attrs, attr);
3977 continue;
3979 c_parser_consume_token (parser);
3980 /* Parse the attribute contents. If they start with an
3981 identifier which is followed by a comma or close
3982 parenthesis, then the arguments start with that
3983 identifier; otherwise they are an expression list.
3984 In objective-c the identifier may be a classname. */
3985 if (c_parser_next_token_is (parser, CPP_NAME)
3986 && (c_parser_peek_token (parser)->id_kind == C_ID_ID
3987 || (c_dialect_objc ()
3988 && c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME))
3989 && ((c_parser_peek_2nd_token (parser)->type == CPP_COMMA)
3990 || (c_parser_peek_2nd_token (parser)->type
3991 == CPP_CLOSE_PAREN)))
3993 tree arg1 = c_parser_peek_token (parser)->value;
3994 c_parser_consume_token (parser);
3995 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3996 attr_args = build_tree_list (NULL_TREE, arg1);
3997 else
3999 tree tree_list;
4000 c_parser_consume_token (parser);
4001 expr_list = c_parser_expr_list (parser, false, true,
4002 NULL, NULL, NULL, NULL);
4003 tree_list = build_tree_list_vec (expr_list);
4004 attr_args = tree_cons (NULL_TREE, arg1, tree_list);
4005 release_tree_vector (expr_list);
4008 else
4010 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4011 attr_args = NULL_TREE;
4012 else
4014 expr_list = c_parser_expr_list (parser, false, true,
4015 NULL, NULL, NULL, NULL);
4016 attr_args = build_tree_list_vec (expr_list);
4017 release_tree_vector (expr_list);
4020 attr = build_tree_list (attr_name, attr_args);
4021 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4022 c_parser_consume_token (parser);
4023 else
4025 parser->lex_untranslated_string = false;
4026 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4027 "expected %<)%>");
4028 return attrs;
4030 attrs = chainon (attrs, attr);
4032 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4033 c_parser_consume_token (parser);
4034 else
4036 parser->lex_untranslated_string = false;
4037 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4038 "expected %<)%>");
4039 return attrs;
4041 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4042 c_parser_consume_token (parser);
4043 else
4045 parser->lex_untranslated_string = false;
4046 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4047 "expected %<)%>");
4048 return attrs;
4050 parser->lex_untranslated_string = false;
4053 if (flag_cilkplus && !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
4054 c_finish_cilk_simd_fn_tokens (parser);
4055 return attrs;
4058 /* Parse a type name (C90 6.5.5, C99 6.7.6).
4060 type-name:
4061 specifier-qualifier-list abstract-declarator[opt]
4064 static struct c_type_name *
4065 c_parser_type_name (c_parser *parser)
4067 struct c_declspecs *specs = build_null_declspecs ();
4068 struct c_declarator *declarator;
4069 struct c_type_name *ret;
4070 bool dummy = false;
4071 c_parser_declspecs (parser, specs, false, true, true, false, false,
4072 cla_prefer_type);
4073 if (!specs->declspecs_seen_p)
4075 c_parser_error (parser, "expected specifier-qualifier-list");
4076 return NULL;
4078 if (specs->type != error_mark_node)
4080 pending_xref_error ();
4081 finish_declspecs (specs);
4083 declarator = c_parser_declarator (parser,
4084 specs->typespec_kind != ctsk_none,
4085 C_DTR_ABSTRACT, &dummy);
4086 if (declarator == NULL)
4087 return NULL;
4088 ret = XOBNEW (&parser_obstack, struct c_type_name);
4089 ret->specs = specs;
4090 ret->declarator = declarator;
4091 return ret;
4094 /* Parse an initializer (C90 6.5.7, C99 6.7.8).
4096 initializer:
4097 assignment-expression
4098 { initializer-list }
4099 { initializer-list , }
4101 initializer-list:
4102 designation[opt] initializer
4103 initializer-list , designation[opt] initializer
4105 designation:
4106 designator-list =
4108 designator-list:
4109 designator
4110 designator-list designator
4112 designator:
4113 array-designator
4114 . identifier
4116 array-designator:
4117 [ constant-expression ]
4119 GNU extensions:
4121 initializer:
4124 designation:
4125 array-designator
4126 identifier :
4128 array-designator:
4129 [ constant-expression ... constant-expression ]
4131 Any expression without commas is accepted in the syntax for the
4132 constant-expressions, with non-constant expressions rejected later.
4134 This function is only used for top-level initializers; for nested
4135 ones, see c_parser_initval. */
4137 static struct c_expr
4138 c_parser_initializer (c_parser *parser)
4140 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
4141 return c_parser_braced_init (parser, NULL_TREE, false);
4142 else
4144 struct c_expr ret;
4145 location_t loc = c_parser_peek_token (parser)->location;
4146 ret = c_parser_expr_no_commas (parser, NULL);
4147 if (TREE_CODE (ret.value) != STRING_CST
4148 && TREE_CODE (ret.value) != COMPOUND_LITERAL_EXPR)
4149 ret = convert_lvalue_to_rvalue (loc, ret, true, true);
4150 return ret;
4154 /* Parse a braced initializer list. TYPE is the type specified for a
4155 compound literal, and NULL_TREE for other initializers and for
4156 nested braced lists. NESTED_P is true for nested braced lists,
4157 false for the list of a compound literal or the list that is the
4158 top-level initializer in a declaration. */
4160 static struct c_expr
4161 c_parser_braced_init (c_parser *parser, tree type, bool nested_p)
4163 struct c_expr ret;
4164 struct obstack braced_init_obstack;
4165 location_t brace_loc = c_parser_peek_token (parser)->location;
4166 gcc_obstack_init (&braced_init_obstack);
4167 gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
4168 c_parser_consume_token (parser);
4169 if (nested_p)
4170 push_init_level (0, &braced_init_obstack);
4171 else
4172 really_start_incremental_init (type);
4173 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
4175 pedwarn (brace_loc, OPT_Wpedantic, "ISO C forbids empty initializer braces");
4177 else
4179 /* Parse a non-empty initializer list, possibly with a trailing
4180 comma. */
4181 while (true)
4183 c_parser_initelt (parser, &braced_init_obstack);
4184 if (parser->error)
4185 break;
4186 if (c_parser_next_token_is (parser, CPP_COMMA))
4187 c_parser_consume_token (parser);
4188 else
4189 break;
4190 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
4191 break;
4194 if (c_parser_next_token_is_not (parser, CPP_CLOSE_BRACE))
4196 ret.value = error_mark_node;
4197 ret.original_code = ERROR_MARK;
4198 ret.original_type = NULL;
4199 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, "expected %<}%>");
4200 pop_init_level (0, &braced_init_obstack);
4201 obstack_free (&braced_init_obstack, NULL);
4202 return ret;
4204 c_parser_consume_token (parser);
4205 ret = pop_init_level (0, &braced_init_obstack);
4206 obstack_free (&braced_init_obstack, NULL);
4207 return ret;
4210 /* Parse a nested initializer, including designators. */
4212 static void
4213 c_parser_initelt (c_parser *parser, struct obstack * braced_init_obstack)
4215 /* Parse any designator or designator list. A single array
4216 designator may have the subsequent "=" omitted in GNU C, but a
4217 longer list or a structure member designator may not. */
4218 if (c_parser_next_token_is (parser, CPP_NAME)
4219 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
4221 /* Old-style structure member designator. */
4222 set_init_label (c_parser_peek_token (parser)->value,
4223 braced_init_obstack);
4224 /* Use the colon as the error location. */
4225 pedwarn (c_parser_peek_2nd_token (parser)->location, OPT_Wpedantic,
4226 "obsolete use of designated initializer with %<:%>");
4227 c_parser_consume_token (parser);
4228 c_parser_consume_token (parser);
4230 else
4232 /* des_seen is 0 if there have been no designators, 1 if there
4233 has been a single array designator and 2 otherwise. */
4234 int des_seen = 0;
4235 /* Location of a designator. */
4236 location_t des_loc = UNKNOWN_LOCATION; /* Quiet warning. */
4237 while (c_parser_next_token_is (parser, CPP_OPEN_SQUARE)
4238 || c_parser_next_token_is (parser, CPP_DOT))
4240 int des_prev = des_seen;
4241 if (!des_seen)
4242 des_loc = c_parser_peek_token (parser)->location;
4243 if (des_seen < 2)
4244 des_seen++;
4245 if (c_parser_next_token_is (parser, CPP_DOT))
4247 des_seen = 2;
4248 c_parser_consume_token (parser);
4249 if (c_parser_next_token_is (parser, CPP_NAME))
4251 set_init_label (c_parser_peek_token (parser)->value,
4252 braced_init_obstack);
4253 c_parser_consume_token (parser);
4255 else
4257 struct c_expr init;
4258 init.value = error_mark_node;
4259 init.original_code = ERROR_MARK;
4260 init.original_type = NULL;
4261 c_parser_error (parser, "expected identifier");
4262 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
4263 process_init_element (init, false, braced_init_obstack);
4264 return;
4267 else
4269 tree first, second;
4270 location_t ellipsis_loc = UNKNOWN_LOCATION; /* Quiet warning. */
4271 /* ??? Following the old parser, [ objc-receiver
4272 objc-message-args ] is accepted as an initializer,
4273 being distinguished from a designator by what follows
4274 the first assignment expression inside the square
4275 brackets, but after a first array designator a
4276 subsequent square bracket is for Objective-C taken to
4277 start an expression, using the obsolete form of
4278 designated initializer without '=', rather than
4279 possibly being a second level of designation: in LALR
4280 terms, the '[' is shifted rather than reducing
4281 designator to designator-list. */
4282 if (des_prev == 1 && c_dialect_objc ())
4284 des_seen = des_prev;
4285 break;
4287 if (des_prev == 0 && c_dialect_objc ())
4289 /* This might be an array designator or an
4290 Objective-C message expression. If the former,
4291 continue parsing here; if the latter, parse the
4292 remainder of the initializer given the starting
4293 primary-expression. ??? It might make sense to
4294 distinguish when des_prev == 1 as well; see
4295 previous comment. */
4296 tree rec, args;
4297 struct c_expr mexpr;
4298 c_parser_consume_token (parser);
4299 if (c_parser_peek_token (parser)->type == CPP_NAME
4300 && ((c_parser_peek_token (parser)->id_kind
4301 == C_ID_TYPENAME)
4302 || (c_parser_peek_token (parser)->id_kind
4303 == C_ID_CLASSNAME)))
4305 /* Type name receiver. */
4306 tree id = c_parser_peek_token (parser)->value;
4307 c_parser_consume_token (parser);
4308 rec = objc_get_class_reference (id);
4309 goto parse_message_args;
4311 first = c_parser_expr_no_commas (parser, NULL).value;
4312 mark_exp_read (first);
4313 if (c_parser_next_token_is (parser, CPP_ELLIPSIS)
4314 || c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
4315 goto array_desig_after_first;
4316 /* Expression receiver. So far only one part
4317 without commas has been parsed; there might be
4318 more of the expression. */
4319 rec = first;
4320 while (c_parser_next_token_is (parser, CPP_COMMA))
4322 struct c_expr next;
4323 location_t comma_loc, exp_loc;
4324 comma_loc = c_parser_peek_token (parser)->location;
4325 c_parser_consume_token (parser);
4326 exp_loc = c_parser_peek_token (parser)->location;
4327 next = c_parser_expr_no_commas (parser, NULL);
4328 next = convert_lvalue_to_rvalue (exp_loc, next,
4329 true, true);
4330 rec = build_compound_expr (comma_loc, rec, next.value);
4332 parse_message_args:
4333 /* Now parse the objc-message-args. */
4334 args = c_parser_objc_message_args (parser);
4335 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
4336 "expected %<]%>");
4337 mexpr.value
4338 = objc_build_message_expr (rec, args);
4339 mexpr.original_code = ERROR_MARK;
4340 mexpr.original_type = NULL;
4341 /* Now parse and process the remainder of the
4342 initializer, starting with this message
4343 expression as a primary-expression. */
4344 c_parser_initval (parser, &mexpr, braced_init_obstack);
4345 return;
4347 c_parser_consume_token (parser);
4348 first = c_parser_expr_no_commas (parser, NULL).value;
4349 mark_exp_read (first);
4350 array_desig_after_first:
4351 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
4353 ellipsis_loc = c_parser_peek_token (parser)->location;
4354 c_parser_consume_token (parser);
4355 second = c_parser_expr_no_commas (parser, NULL).value;
4356 mark_exp_read (second);
4358 else
4359 second = NULL_TREE;
4360 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
4362 c_parser_consume_token (parser);
4363 set_init_index (first, second, braced_init_obstack);
4364 if (second)
4365 pedwarn (ellipsis_loc, OPT_Wpedantic,
4366 "ISO C forbids specifying range of elements to initialize");
4368 else
4369 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
4370 "expected %<]%>");
4373 if (des_seen >= 1)
4375 if (c_parser_next_token_is (parser, CPP_EQ))
4377 if (!flag_isoc99)
4378 pedwarn (des_loc, OPT_Wpedantic,
4379 "ISO C90 forbids specifying subobject to initialize");
4380 c_parser_consume_token (parser);
4382 else
4384 if (des_seen == 1)
4385 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
4386 "obsolete use of designated initializer without %<=%>");
4387 else
4389 struct c_expr init;
4390 init.value = error_mark_node;
4391 init.original_code = ERROR_MARK;
4392 init.original_type = NULL;
4393 c_parser_error (parser, "expected %<=%>");
4394 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
4395 process_init_element (init, false, braced_init_obstack);
4396 return;
4401 c_parser_initval (parser, NULL, braced_init_obstack);
4404 /* Parse a nested initializer; as c_parser_initializer but parses
4405 initializers within braced lists, after any designators have been
4406 applied. If AFTER is not NULL then it is an Objective-C message
4407 expression which is the primary-expression starting the
4408 initializer. */
4410 static void
4411 c_parser_initval (c_parser *parser, struct c_expr *after,
4412 struct obstack * braced_init_obstack)
4414 struct c_expr init;
4415 gcc_assert (!after || c_dialect_objc ());
4416 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE) && !after)
4417 init = c_parser_braced_init (parser, NULL_TREE, true);
4418 else
4420 location_t loc = c_parser_peek_token (parser)->location;
4421 init = c_parser_expr_no_commas (parser, after);
4422 if (init.value != NULL_TREE
4423 && TREE_CODE (init.value) != STRING_CST
4424 && TREE_CODE (init.value) != COMPOUND_LITERAL_EXPR)
4425 init = convert_lvalue_to_rvalue (loc, init, true, true);
4427 process_init_element (init, false, braced_init_obstack);
4430 /* Parse a compound statement (possibly a function body) (C90 6.6.2,
4431 C99 6.8.2).
4433 compound-statement:
4434 { block-item-list[opt] }
4435 { label-declarations block-item-list }
4437 block-item-list:
4438 block-item
4439 block-item-list block-item
4441 block-item:
4442 nested-declaration
4443 statement
4445 nested-declaration:
4446 declaration
4448 GNU extensions:
4450 compound-statement:
4451 { label-declarations block-item-list }
4453 nested-declaration:
4454 __extension__ nested-declaration
4455 nested-function-definition
4457 label-declarations:
4458 label-declaration
4459 label-declarations label-declaration
4461 label-declaration:
4462 __label__ identifier-list ;
4464 Allowing the mixing of declarations and code is new in C99. The
4465 GNU syntax also permits (not shown above) labels at the end of
4466 compound statements, which yield an error. We don't allow labels
4467 on declarations; this might seem like a natural extension, but
4468 there would be a conflict between attributes on the label and
4469 prefix attributes on the declaration. ??? The syntax follows the
4470 old parser in requiring something after label declarations.
4471 Although they are erroneous if the labels declared aren't defined,
4472 is it useful for the syntax to be this way?
4474 OpenMP:
4476 block-item:
4477 openmp-directive
4479 openmp-directive:
4480 barrier-directive
4481 flush-directive
4482 taskwait-directive
4483 taskyield-directive
4484 cancel-directive
4485 cancellation-point-directive */
4487 static tree
4488 c_parser_compound_statement (c_parser *parser)
4490 tree stmt;
4491 location_t brace_loc;
4492 brace_loc = c_parser_peek_token (parser)->location;
4493 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
4495 /* Ensure a scope is entered and left anyway to avoid confusion
4496 if we have just prepared to enter a function body. */
4497 stmt = c_begin_compound_stmt (true);
4498 c_end_compound_stmt (brace_loc, stmt, true);
4499 return error_mark_node;
4501 stmt = c_begin_compound_stmt (true);
4502 c_parser_compound_statement_nostart (parser);
4504 /* If the compound stmt contains array notations, then we expand them. */
4505 if (flag_cilkplus && contains_array_notation_expr (stmt))
4506 stmt = expand_array_notation_exprs (stmt);
4507 return c_end_compound_stmt (brace_loc, stmt, true);
4510 /* Parse a compound statement except for the opening brace. This is
4511 used for parsing both compound statements and statement expressions
4512 (which follow different paths to handling the opening). */
4514 static void
4515 c_parser_compound_statement_nostart (c_parser *parser)
4517 bool last_stmt = false;
4518 bool last_label = false;
4519 bool save_valid_for_pragma = valid_location_for_stdc_pragma_p ();
4520 location_t label_loc = UNKNOWN_LOCATION; /* Quiet warning. */
4521 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
4523 c_parser_consume_token (parser);
4524 return;
4526 mark_valid_location_for_stdc_pragma (true);
4527 if (c_parser_next_token_is_keyword (parser, RID_LABEL))
4529 /* Read zero or more forward-declarations for labels that nested
4530 functions can jump to. */
4531 mark_valid_location_for_stdc_pragma (false);
4532 while (c_parser_next_token_is_keyword (parser, RID_LABEL))
4534 label_loc = c_parser_peek_token (parser)->location;
4535 c_parser_consume_token (parser);
4536 /* Any identifiers, including those declared as type names,
4537 are OK here. */
4538 while (true)
4540 tree label;
4541 if (c_parser_next_token_is_not (parser, CPP_NAME))
4543 c_parser_error (parser, "expected identifier");
4544 break;
4546 label
4547 = declare_label (c_parser_peek_token (parser)->value);
4548 C_DECLARED_LABEL_FLAG (label) = 1;
4549 add_stmt (build_stmt (label_loc, DECL_EXPR, label));
4550 c_parser_consume_token (parser);
4551 if (c_parser_next_token_is (parser, CPP_COMMA))
4552 c_parser_consume_token (parser);
4553 else
4554 break;
4556 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
4558 pedwarn (label_loc, OPT_Wpedantic, "ISO C forbids label declarations");
4560 /* We must now have at least one statement, label or declaration. */
4561 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
4563 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
4564 c_parser_error (parser, "expected declaration or statement");
4565 c_parser_consume_token (parser);
4566 return;
4568 /* Process all #pragma's just after the opening brace. This
4569 handles #pragma upc, which can only appear just after
4570 the opening brace, when it appears within a function body. */
4571 push_upc_consistency_mode ();
4572 permit_pragma_upc ();
4573 while (c_parser_next_token_is (parser, CPP_PRAGMA))
4575 location_t loc ATTRIBUTE_UNUSED = c_parser_peek_token (parser)->location;
4576 if (c_parser_pragma (parser, pragma_compound))
4577 last_label = false, last_stmt = true;
4578 parser->error = false;
4580 deny_pragma_upc ();
4581 while (c_parser_next_token_is_not (parser, CPP_CLOSE_BRACE))
4583 location_t loc = c_parser_peek_token (parser)->location;
4584 if (c_parser_next_token_is_keyword (parser, RID_CASE)
4585 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
4586 || (c_parser_next_token_is (parser, CPP_NAME)
4587 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
4589 if (c_parser_next_token_is_keyword (parser, RID_CASE))
4590 label_loc = c_parser_peek_2nd_token (parser)->location;
4591 else
4592 label_loc = c_parser_peek_token (parser)->location;
4593 last_label = true;
4594 last_stmt = false;
4595 mark_valid_location_for_stdc_pragma (false);
4596 c_parser_label (parser);
4598 else if (!last_label
4599 && c_parser_next_tokens_start_declaration (parser))
4601 last_label = false;
4602 mark_valid_location_for_stdc_pragma (false);
4603 c_parser_declaration_or_fndef (parser, true, true, true, true,
4604 true, NULL, vNULL);
4605 if (last_stmt)
4606 pedwarn_c90 (loc,
4607 (pedantic && !flag_isoc99)
4608 ? OPT_Wpedantic
4609 : OPT_Wdeclaration_after_statement,
4610 "ISO C90 forbids mixed declarations and code");
4611 last_stmt = false;
4613 else if (!last_label
4614 && c_parser_next_token_is_keyword (parser, RID_EXTENSION))
4616 /* __extension__ can start a declaration, but is also an
4617 unary operator that can start an expression. Consume all
4618 but the last of a possible series of __extension__ to
4619 determine which. */
4620 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
4621 && (c_parser_peek_2nd_token (parser)->keyword
4622 == RID_EXTENSION))
4623 c_parser_consume_token (parser);
4624 if (c_token_starts_declaration (c_parser_peek_2nd_token (parser)))
4626 int ext;
4627 ext = disable_extension_diagnostics ();
4628 c_parser_consume_token (parser);
4629 last_label = false;
4630 mark_valid_location_for_stdc_pragma (false);
4631 c_parser_declaration_or_fndef (parser, true, true, true, true,
4632 true, NULL, vNULL);
4633 /* Following the old parser, __extension__ does not
4634 disable this diagnostic. */
4635 restore_extension_diagnostics (ext);
4636 if (last_stmt)
4637 pedwarn_c90 (loc, (pedantic && !flag_isoc99)
4638 ? OPT_Wpedantic
4639 : OPT_Wdeclaration_after_statement,
4640 "ISO C90 forbids mixed declarations and code");
4641 last_stmt = false;
4643 else
4644 goto statement;
4646 else if (c_parser_next_token_is (parser, CPP_PRAGMA))
4648 /* External pragmas, and some omp pragmas, are not associated
4649 with regular c code, and so are not to be considered statements
4650 syntactically. This ensures that the user doesn't put them
4651 places that would turn into syntax errors if the directive
4652 were ignored. */
4653 if (c_parser_pragma (parser, pragma_compound))
4654 last_label = false, last_stmt = true;
4656 else if (c_parser_next_token_is (parser, CPP_EOF))
4658 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
4659 c_parser_error (parser, "expected declaration or statement");
4660 return;
4662 else if (c_parser_next_token_is_keyword (parser, RID_ELSE))
4664 if (parser->in_if_block)
4666 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
4667 error_at (loc, """expected %<}%> before %<else%>");
4668 return;
4670 else
4672 error_at (loc, "%<else%> without a previous %<if%>");
4673 c_parser_consume_token (parser);
4674 continue;
4677 else
4679 statement:
4680 last_label = false;
4681 last_stmt = true;
4682 mark_valid_location_for_stdc_pragma (false);
4683 c_parser_statement_after_labels (parser);
4686 parser->error = false;
4688 if (last_label)
4689 error_at (label_loc, "label at end of compound statement");
4690 c_parser_consume_token (parser);
4691 pop_upc_consistency_mode ();
4692 /* Restore the value we started with. */
4693 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
4696 /* Parse a label (C90 6.6.1, C99 6.8.1).
4698 label:
4699 identifier : attributes[opt]
4700 case constant-expression :
4701 default :
4703 GNU extensions:
4705 label:
4706 case constant-expression ... constant-expression :
4708 The use of attributes on labels is a GNU extension. The syntax in
4709 GNU C accepts any expressions without commas, non-constant
4710 expressions being rejected later. */
4712 static void
4713 c_parser_label (c_parser *parser)
4715 location_t loc1 = c_parser_peek_token (parser)->location;
4716 tree label = NULL_TREE;
4717 if (c_parser_next_token_is_keyword (parser, RID_CASE))
4719 tree exp1, exp2;
4720 c_parser_consume_token (parser);
4721 exp1 = c_parser_expr_no_commas (parser, NULL).value;
4722 if (c_parser_next_token_is (parser, CPP_COLON))
4724 c_parser_consume_token (parser);
4725 label = do_case (loc1, exp1, NULL_TREE);
4727 else if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
4729 c_parser_consume_token (parser);
4730 exp2 = c_parser_expr_no_commas (parser, NULL).value;
4731 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
4732 label = do_case (loc1, exp1, exp2);
4734 else
4735 c_parser_error (parser, "expected %<:%> or %<...%>");
4737 else if (c_parser_next_token_is_keyword (parser, RID_DEFAULT))
4739 c_parser_consume_token (parser);
4740 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
4741 label = do_case (loc1, NULL_TREE, NULL_TREE);
4743 else
4745 tree name = c_parser_peek_token (parser)->value;
4746 tree tlab;
4747 tree attrs;
4748 location_t loc2 = c_parser_peek_token (parser)->location;
4749 gcc_assert (c_parser_next_token_is (parser, CPP_NAME));
4750 c_parser_consume_token (parser);
4751 gcc_assert (c_parser_next_token_is (parser, CPP_COLON));
4752 c_parser_consume_token (parser);
4753 attrs = c_parser_attributes (parser);
4754 tlab = define_label (loc2, name);
4755 if (tlab)
4757 decl_attributes (&tlab, attrs, 0);
4758 label = add_stmt (build_stmt (loc1, LABEL_EXPR, tlab));
4761 if (label)
4763 if (c_parser_next_tokens_start_declaration (parser))
4765 error_at (c_parser_peek_token (parser)->location,
4766 "a label can only be part of a statement and "
4767 "a declaration is not a statement");
4768 c_parser_declaration_or_fndef (parser, /*fndef_ok*/ false,
4769 /*static_assert_ok*/ true,
4770 /*empty_ok*/ true, /*nested*/ true,
4771 /*start_attr_ok*/ true, NULL,
4772 vNULL);
4777 /* Parse a statement (C90 6.6, C99 6.8).
4779 statement:
4780 labeled-statement
4781 compound-statement
4782 expression-statement
4783 selection-statement
4784 iteration-statement
4785 jump-statement
4787 labeled-statement:
4788 label statement
4790 expression-statement:
4791 expression[opt] ;
4793 selection-statement:
4794 if-statement
4795 switch-statement
4797 iteration-statement:
4798 while-statement
4799 do-statement
4800 for-statement
4802 jump-statement:
4803 goto identifier ;
4804 continue ;
4805 break ;
4806 return expression[opt] ;
4808 GNU extensions:
4810 statement:
4811 asm-statement
4813 jump-statement:
4814 goto * expression ;
4816 Objective-C:
4818 statement:
4819 objc-throw-statement
4820 objc-try-catch-statement
4821 objc-synchronized-statement
4823 objc-throw-statement:
4824 @throw expression ;
4825 @throw ;
4827 OpenMP:
4829 statement:
4830 openmp-construct
4832 openmp-construct:
4833 parallel-construct
4834 for-construct
4835 simd-construct
4836 for-simd-construct
4837 sections-construct
4838 single-construct
4839 parallel-for-construct
4840 parallel-for-simd-construct
4841 parallel-sections-construct
4842 master-construct
4843 critical-construct
4844 atomic-construct
4845 ordered-construct
4847 parallel-construct:
4848 parallel-directive structured-block
4850 for-construct:
4851 for-directive iteration-statement
4853 simd-construct:
4854 simd-directive iteration-statements
4856 for-simd-construct:
4857 for-simd-directive iteration-statements
4859 sections-construct:
4860 sections-directive section-scope
4862 single-construct:
4863 single-directive structured-block
4865 parallel-for-construct:
4866 parallel-for-directive iteration-statement
4868 parallel-for-simd-construct:
4869 parallel-for-simd-directive iteration-statement
4871 parallel-sections-construct:
4872 parallel-sections-directive section-scope
4874 master-construct:
4875 master-directive structured-block
4877 critical-construct:
4878 critical-directive structured-block
4880 atomic-construct:
4881 atomic-directive expression-statement
4883 ordered-construct:
4884 ordered-directive structured-block
4886 Transactional Memory:
4888 statement:
4889 transaction-statement
4890 transaction-cancel-statement
4893 static void
4894 c_parser_statement (c_parser *parser)
4896 while (c_parser_next_token_is_keyword (parser, RID_CASE)
4897 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
4898 || (c_parser_next_token_is (parser, CPP_NAME)
4899 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
4900 c_parser_label (parser);
4901 c_parser_statement_after_labels (parser);
4904 /* Parse a statement, other than a labeled statement. */
4906 static void
4907 c_parser_statement_after_labels (c_parser *parser)
4909 location_t loc = c_parser_peek_token (parser)->location;
4910 tree stmt = NULL_TREE;
4911 bool in_if_block = parser->in_if_block;
4912 parser->in_if_block = false;
4913 switch (c_parser_peek_token (parser)->type)
4915 case CPP_OPEN_BRACE:
4916 add_stmt (c_parser_compound_statement (parser));
4917 break;
4918 case CPP_KEYWORD:
4919 switch (c_parser_peek_token (parser)->keyword)
4921 case RID_IF:
4922 c_parser_if_statement (parser);
4923 break;
4924 case RID_SWITCH:
4925 c_parser_switch_statement (parser);
4926 break;
4927 case RID_WHILE:
4928 c_parser_while_statement (parser, false);
4929 break;
4930 case RID_DO:
4931 c_parser_do_statement (parser, false);
4932 break;
4933 case RID_FOR:
4934 c_parser_for_statement (parser, false);
4935 break;
4936 case RID_CILK_SYNC:
4937 c_parser_consume_token (parser);
4938 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
4939 if (!flag_cilkplus)
4940 error_at (loc, "-fcilkplus must be enabled to use %<_Cilk_sync%>");
4941 else
4942 add_stmt (build_cilk_sync ());
4943 break;
4944 case RID_GOTO:
4945 c_parser_consume_token (parser);
4946 if (c_parser_next_token_is (parser, CPP_NAME))
4948 stmt = c_finish_goto_label (loc,
4949 c_parser_peek_token (parser)->value);
4950 c_parser_consume_token (parser);
4952 else if (c_parser_next_token_is (parser, CPP_MULT))
4954 struct c_expr val;
4956 c_parser_consume_token (parser);
4957 val = c_parser_expression (parser);
4958 val = convert_lvalue_to_rvalue (loc, val, false, true);
4959 stmt = c_finish_goto_ptr (loc, val.value);
4961 else
4962 c_parser_error (parser, "expected identifier or %<*%>");
4963 goto expect_semicolon;
4964 case RID_CONTINUE:
4965 c_parser_consume_token (parser);
4966 stmt = c_finish_bc_stmt (loc, &c_cont_label, false);
4967 goto expect_semicolon;
4968 case RID_BREAK:
4969 c_parser_consume_token (parser);
4970 stmt = c_finish_bc_stmt (loc, &c_break_label, true);
4971 goto expect_semicolon;
4972 case RID_RETURN:
4973 c_parser_consume_token (parser);
4974 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4976 stmt = c_finish_return (loc, NULL_TREE, NULL_TREE);
4977 c_parser_consume_token (parser);
4979 else
4981 struct c_expr expr = c_parser_expression_conv (parser);
4982 mark_exp_read (expr.value);
4983 stmt = c_finish_return (loc, expr.value, expr.original_type);
4984 goto expect_semicolon;
4986 break;
4987 case RID_ASM:
4988 stmt = c_parser_asm_statement (parser);
4989 break;
4990 case RID_TRANSACTION_ATOMIC:
4991 case RID_TRANSACTION_RELAXED:
4992 stmt = c_parser_transaction (parser,
4993 c_parser_peek_token (parser)->keyword);
4994 break;
4995 case RID_TRANSACTION_CANCEL:
4996 stmt = c_parser_transaction_cancel (parser);
4997 goto expect_semicolon;
4998 case RID_AT_THROW:
4999 gcc_assert (c_dialect_objc ());
5000 c_parser_consume_token (parser);
5001 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5003 stmt = objc_build_throw_stmt (loc, NULL_TREE);
5004 c_parser_consume_token (parser);
5006 else
5008 struct c_expr expr = c_parser_expression (parser);
5009 expr = convert_lvalue_to_rvalue (loc, expr, false, false);
5010 expr.value = c_fully_fold (expr.value, false, NULL);
5011 stmt = objc_build_throw_stmt (loc, expr.value);
5012 goto expect_semicolon;
5014 break;
5015 case RID_AT_TRY:
5016 gcc_assert (c_dialect_objc ());
5017 c_parser_objc_try_catch_finally_statement (parser);
5018 break;
5019 case RID_AT_SYNCHRONIZED:
5020 gcc_assert (c_dialect_objc ());
5021 c_parser_objc_synchronized_statement (parser);
5022 break;
5023 case RID_UPC_FORALL:
5024 gcc_assert (flag_upc);
5025 c_parser_upc_forall_statement (parser);
5026 break;
5027 case RID_UPC_NOTIFY:
5028 gcc_assert (flag_upc);
5029 c_parser_upc_sync_statement (parser, UPC_SYNC_NOTIFY_OP);
5030 goto expect_semicolon;
5031 case RID_UPC_WAIT:
5032 gcc_assert (flag_upc);
5033 c_parser_upc_sync_statement (parser, UPC_SYNC_WAIT_OP);
5034 goto expect_semicolon;
5035 case RID_UPC_BARRIER:
5036 gcc_assert (flag_upc);
5037 c_parser_upc_sync_statement (parser, UPC_SYNC_BARRIER_OP);
5038 goto expect_semicolon;
5039 default:
5040 goto expr_stmt;
5042 break;
5043 case CPP_SEMICOLON:
5044 c_parser_consume_token (parser);
5045 break;
5046 case CPP_CLOSE_PAREN:
5047 case CPP_CLOSE_SQUARE:
5048 /* Avoid infinite loop in error recovery:
5049 c_parser_skip_until_found stops at a closing nesting
5050 delimiter without consuming it, but here we need to consume
5051 it to proceed further. */
5052 c_parser_error (parser, "expected statement");
5053 c_parser_consume_token (parser);
5054 break;
5055 case CPP_PRAGMA:
5056 c_parser_pragma (parser, pragma_stmt);
5057 break;
5058 default:
5059 expr_stmt:
5060 stmt = c_finish_expr_stmt (loc, c_parser_expression_conv (parser).value);
5061 expect_semicolon:
5062 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
5063 break;
5065 /* Two cases cannot and do not have line numbers associated: If stmt
5066 is degenerate, such as "2;", then stmt is an INTEGER_CST, which
5067 cannot hold line numbers. But that's OK because the statement
5068 will either be changed to a MODIFY_EXPR during gimplification of
5069 the statement expr, or discarded. If stmt was compound, but
5070 without new variables, we will have skipped the creation of a
5071 BIND and will have a bare STATEMENT_LIST. But that's OK because
5072 (recursively) all of the component statements should already have
5073 line numbers assigned. ??? Can we discard no-op statements
5074 earlier? */
5075 if (CAN_HAVE_LOCATION_P (stmt)
5076 && EXPR_LOCATION (stmt) == UNKNOWN_LOCATION)
5077 SET_EXPR_LOCATION (stmt, loc);
5079 parser->in_if_block = in_if_block;
5082 /* Parse the condition from an if, do, while or for statements. */
5084 static tree
5085 c_parser_condition (c_parser *parser)
5087 location_t loc ATTRIBUTE_UNUSED = c_parser_peek_token (parser)->location;
5088 tree cond;
5089 cond = c_parser_expression_conv (parser).value;
5090 cond = c_objc_common_truthvalue_conversion (loc, cond);
5091 cond = c_fully_fold (cond, false, NULL);
5092 if (warn_sequence_point)
5093 verify_sequence_points (cond);
5094 return cond;
5097 /* Parse a parenthesized condition from an if, do or while statement.
5099 condition:
5100 ( expression )
5102 static tree
5103 c_parser_paren_condition (c_parser *parser)
5105 tree cond;
5106 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5107 return error_mark_node;
5108 cond = c_parser_condition (parser);
5109 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5110 return cond;
5113 /* Parse a statement which is a block in C99. */
5115 static tree
5116 c_parser_c99_block_statement (c_parser *parser)
5118 tree block = c_begin_compound_stmt (flag_isoc99);
5119 location_t loc = c_parser_peek_token (parser)->location;
5120 c_parser_statement (parser);
5121 return c_end_compound_stmt (loc, block, flag_isoc99);
5124 /* Parse the body of an if statement. This is just parsing a
5125 statement but (a) it is a block in C99, (b) we track whether the
5126 body is an if statement for the sake of -Wparentheses warnings, (c)
5127 we handle an empty body specially for the sake of -Wempty-body
5128 warnings, and (d) we call parser_compound_statement directly
5129 because c_parser_statement_after_labels resets
5130 parser->in_if_block. */
5132 static tree
5133 c_parser_if_body (c_parser *parser, bool *if_p)
5135 tree block = c_begin_compound_stmt (flag_isoc99);
5136 location_t body_loc = c_parser_peek_token (parser)->location;
5137 while (c_parser_next_token_is_keyword (parser, RID_CASE)
5138 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
5139 || (c_parser_next_token_is (parser, CPP_NAME)
5140 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
5141 c_parser_label (parser);
5142 *if_p = c_parser_next_token_is_keyword (parser, RID_IF);
5143 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5145 location_t loc = c_parser_peek_token (parser)->location;
5146 add_stmt (build_empty_stmt (loc));
5147 c_parser_consume_token (parser);
5148 if (!c_parser_next_token_is_keyword (parser, RID_ELSE))
5149 warning_at (loc, OPT_Wempty_body,
5150 "suggest braces around empty body in an %<if%> statement");
5152 else if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
5153 add_stmt (c_parser_compound_statement (parser));
5154 else
5155 c_parser_statement_after_labels (parser);
5156 return c_end_compound_stmt (body_loc, block, flag_isoc99);
5159 /* Parse the else body of an if statement. This is just parsing a
5160 statement but (a) it is a block in C99, (b) we handle an empty body
5161 specially for the sake of -Wempty-body warnings. */
5163 static tree
5164 c_parser_else_body (c_parser *parser)
5166 location_t else_loc = c_parser_peek_token (parser)->location;
5167 tree block = c_begin_compound_stmt (flag_isoc99);
5168 while (c_parser_next_token_is_keyword (parser, RID_CASE)
5169 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
5170 || (c_parser_next_token_is (parser, CPP_NAME)
5171 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
5172 c_parser_label (parser);
5173 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5175 location_t loc = c_parser_peek_token (parser)->location;
5176 warning_at (loc,
5177 OPT_Wempty_body,
5178 "suggest braces around empty body in an %<else%> statement");
5179 add_stmt (build_empty_stmt (loc));
5180 c_parser_consume_token (parser);
5182 else
5183 c_parser_statement_after_labels (parser);
5184 return c_end_compound_stmt (else_loc, block, flag_isoc99);
5187 /* Parse an if statement (C90 6.6.4, C99 6.8.4).
5189 if-statement:
5190 if ( expression ) statement
5191 if ( expression ) statement else statement
5194 static void
5195 c_parser_if_statement (c_parser *parser)
5197 tree block;
5198 location_t loc;
5199 tree cond;
5200 bool first_if = false;
5201 tree first_body, second_body;
5202 bool in_if_block;
5203 tree if_stmt;
5205 gcc_assert (c_parser_next_token_is_keyword (parser, RID_IF));
5206 c_parser_consume_token (parser);
5207 block = c_begin_compound_stmt (flag_isoc99);
5208 loc = c_parser_peek_token (parser)->location;
5209 cond = c_parser_paren_condition (parser);
5210 in_if_block = parser->in_if_block;
5211 parser->in_if_block = true;
5212 first_body = c_parser_if_body (parser, &first_if);
5213 parser->in_if_block = in_if_block;
5214 if (c_parser_next_token_is_keyword (parser, RID_ELSE))
5216 c_parser_consume_token (parser);
5217 second_body = c_parser_else_body (parser);
5219 else
5220 second_body = NULL_TREE;
5221 c_finish_if_stmt (loc, cond, first_body, second_body, first_if);
5222 if_stmt = c_end_compound_stmt (loc, block, flag_isoc99);
5224 /* If the if statement contains array notations, then we expand them. */
5225 if (flag_cilkplus && contains_array_notation_expr (if_stmt))
5226 if_stmt = fix_conditional_array_notations (if_stmt);
5227 add_stmt (if_stmt);
5230 /* Parse a switch statement (C90 6.6.4, C99 6.8.4).
5232 switch-statement:
5233 switch (expression) statement
5236 static void
5237 c_parser_switch_statement (c_parser *parser)
5239 struct c_expr ce;
5240 tree block, expr, body, save_break;
5241 location_t switch_loc = c_parser_peek_token (parser)->location;
5242 location_t switch_cond_loc;
5243 gcc_assert (c_parser_next_token_is_keyword (parser, RID_SWITCH));
5244 c_parser_consume_token (parser);
5245 block = c_begin_compound_stmt (flag_isoc99);
5246 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5248 switch_cond_loc = c_parser_peek_token (parser)->location;
5249 ce = c_parser_expression (parser);
5250 ce = convert_lvalue_to_rvalue (switch_cond_loc, ce, true, false);
5251 expr = ce.value;
5252 if (flag_cilkplus && contains_array_notation_expr (expr))
5254 error_at (switch_cond_loc,
5255 "array notations cannot be used as a condition for switch "
5256 "statement");
5257 expr = error_mark_node;
5259 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5261 else
5263 switch_cond_loc = UNKNOWN_LOCATION;
5264 expr = error_mark_node;
5266 c_start_case (switch_loc, switch_cond_loc, expr);
5267 save_break = c_break_label;
5268 c_break_label = NULL_TREE;
5269 body = c_parser_c99_block_statement (parser);
5270 c_finish_case (body);
5271 if (c_break_label)
5273 location_t here = c_parser_peek_token (parser)->location;
5274 tree t = build1 (LABEL_EXPR, void_type_node, c_break_label);
5275 SET_EXPR_LOCATION (t, here);
5276 add_stmt (t);
5278 c_break_label = save_break;
5279 add_stmt (c_end_compound_stmt (switch_loc, block, flag_isoc99));
5282 /* Parse a while statement (C90 6.6.5, C99 6.8.5).
5284 while-statement:
5285 while (expression) statement
5288 static void
5289 c_parser_while_statement (c_parser *parser, bool ivdep)
5291 tree block, cond, body, save_break, save_cont;
5292 location_t loc;
5293 gcc_assert (c_parser_next_token_is_keyword (parser, RID_WHILE));
5294 c_parser_consume_token (parser);
5295 block = c_begin_compound_stmt (flag_isoc99);
5296 loc = c_parser_peek_token (parser)->location;
5297 cond = c_parser_paren_condition (parser);
5298 if (flag_cilkplus && contains_array_notation_expr (cond))
5300 error_at (loc, "array notations cannot be used as a condition for while "
5301 "statement");
5302 cond = error_mark_node;
5305 if (ivdep && cond != error_mark_node)
5306 cond = build2 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
5307 build_int_cst (integer_type_node,
5308 annot_expr_ivdep_kind));
5309 save_break = c_break_label;
5310 c_break_label = NULL_TREE;
5311 save_cont = c_cont_label;
5312 c_cont_label = NULL_TREE;
5313 body = c_parser_c99_block_statement (parser);
5314 c_finish_loop (loc, cond, NULL, body, c_break_label, c_cont_label, true);
5315 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
5316 c_break_label = save_break;
5317 c_cont_label = save_cont;
5320 /* Parse a do statement (C90 6.6.5, C99 6.8.5).
5322 do-statement:
5323 do statement while ( expression ) ;
5326 static void
5327 c_parser_do_statement (c_parser *parser, bool ivdep)
5329 tree block, cond, body, save_break, save_cont, new_break, new_cont;
5330 location_t loc;
5331 gcc_assert (c_parser_next_token_is_keyword (parser, RID_DO));
5332 c_parser_consume_token (parser);
5333 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5334 warning_at (c_parser_peek_token (parser)->location,
5335 OPT_Wempty_body,
5336 "suggest braces around empty body in %<do%> statement");
5337 block = c_begin_compound_stmt (flag_isoc99);
5338 loc = c_parser_peek_token (parser)->location;
5339 save_break = c_break_label;
5340 c_break_label = NULL_TREE;
5341 save_cont = c_cont_label;
5342 c_cont_label = NULL_TREE;
5343 body = c_parser_c99_block_statement (parser);
5344 c_parser_require_keyword (parser, RID_WHILE, "expected %<while%>");
5345 new_break = c_break_label;
5346 c_break_label = save_break;
5347 new_cont = c_cont_label;
5348 c_cont_label = save_cont;
5349 cond = c_parser_paren_condition (parser);
5350 if (flag_cilkplus && contains_array_notation_expr (cond))
5352 error_at (loc, "array notations cannot be used as a condition for a "
5353 "do-while statement");
5354 cond = error_mark_node;
5356 if (ivdep && cond != error_mark_node)
5357 cond = build2 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
5358 build_int_cst (integer_type_node,
5359 annot_expr_ivdep_kind));
5360 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
5361 c_parser_skip_to_end_of_block_or_statement (parser);
5362 c_finish_loop (loc, cond, NULL, body, new_break, new_cont, false);
5363 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
5366 /* Parse a for statement (C90 6.6.5, C99 6.8.5).
5368 for-statement:
5369 for ( expression[opt] ; expression[opt] ; expression[opt] ) statement
5370 for ( nested-declaration expression[opt] ; expression[opt] ) statement
5372 The form with a declaration is new in C99.
5374 ??? In accordance with the old parser, the declaration may be a
5375 nested function, which is then rejected in check_for_loop_decls,
5376 but does it make any sense for this to be included in the grammar?
5377 Note in particular that the nested function does not include a
5378 trailing ';', whereas the "declaration" production includes one.
5379 Also, can we reject bad declarations earlier and cheaper than
5380 check_for_loop_decls?
5382 In Objective-C, there are two additional variants:
5384 foreach-statement:
5385 for ( expression in expresssion ) statement
5386 for ( declaration in expression ) statement
5388 This is inconsistent with C, because the second variant is allowed
5389 even if c99 is not enabled.
5391 The rest of the comment documents these Objective-C foreach-statement.
5393 Here is the canonical example of the first variant:
5394 for (object in array) { do something with object }
5395 we call the first expression ("object") the "object_expression" and
5396 the second expression ("array") the "collection_expression".
5397 object_expression must be an lvalue of type "id" (a generic Objective-C
5398 object) because the loop works by assigning to object_expression the
5399 various objects from the collection_expression. collection_expression
5400 must evaluate to something of type "id" which responds to the method
5401 countByEnumeratingWithState:objects:count:.
5403 The canonical example of the second variant is:
5404 for (id object in array) { do something with object }
5405 which is completely equivalent to
5407 id object;
5408 for (object in array) { do something with object }
5410 Note that initizializing 'object' in some way (eg, "for ((object =
5411 xxx) in array) { do something with object }") is possibly
5412 technically valid, but completely pointless as 'object' will be
5413 assigned to something else as soon as the loop starts. We should
5414 most likely reject it (TODO).
5416 The beginning of the Objective-C foreach-statement looks exactly
5417 like the beginning of the for-statement, and we can tell it is a
5418 foreach-statement only because the initial declaration or
5419 expression is terminated by 'in' instead of ';'.
5422 static void
5423 c_parser_for_statement (c_parser *parser, bool ivdep)
5425 tree block, cond, incr, save_break, save_cont, body;
5426 /* The following are only used when parsing an ObjC foreach statement. */
5427 tree object_expression;
5428 /* Silence the bogus uninitialized warning. */
5429 tree collection_expression = NULL;
5430 location_t loc = c_parser_peek_token (parser)->location;
5431 location_t for_loc = c_parser_peek_token (parser)->location;
5432 bool is_foreach_statement = false;
5433 gcc_assert (c_parser_next_token_is_keyword (parser, RID_FOR));
5434 c_parser_consume_token (parser);
5435 /* Open a compound statement in Objective-C as well, just in case this is
5436 as foreach expression. */
5437 block = c_begin_compound_stmt (flag_isoc99 || c_dialect_objc ());
5438 cond = error_mark_node;
5439 incr = error_mark_node;
5440 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5442 /* Parse the initialization declaration or expression. */
5443 object_expression = error_mark_node;
5444 parser->objc_could_be_foreach_context = c_dialect_objc ();
5445 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5447 parser->objc_could_be_foreach_context = false;
5448 c_parser_consume_token (parser);
5449 c_finish_expr_stmt (loc, NULL_TREE);
5451 else if (c_parser_next_tokens_start_declaration (parser))
5453 c_parser_declaration_or_fndef (parser, true, true, true, true, true,
5454 &object_expression, vNULL);
5455 parser->objc_could_be_foreach_context = false;
5457 if (c_parser_next_token_is_keyword (parser, RID_IN))
5459 c_parser_consume_token (parser);
5460 is_foreach_statement = true;
5461 if (check_for_loop_decls (for_loc, true) == NULL_TREE)
5462 c_parser_error (parser, "multiple iterating variables in fast enumeration");
5464 else
5465 check_for_loop_decls (for_loc, flag_isoc99);
5467 else if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
5469 /* __extension__ can start a declaration, but is also an
5470 unary operator that can start an expression. Consume all
5471 but the last of a possible series of __extension__ to
5472 determine which. */
5473 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
5474 && (c_parser_peek_2nd_token (parser)->keyword
5475 == RID_EXTENSION))
5476 c_parser_consume_token (parser);
5477 if (c_token_starts_declaration (c_parser_peek_2nd_token (parser)))
5479 int ext;
5480 ext = disable_extension_diagnostics ();
5481 c_parser_consume_token (parser);
5482 c_parser_declaration_or_fndef (parser, true, true, true, true,
5483 true, &object_expression, vNULL);
5484 parser->objc_could_be_foreach_context = false;
5486 restore_extension_diagnostics (ext);
5487 if (c_parser_next_token_is_keyword (parser, RID_IN))
5489 c_parser_consume_token (parser);
5490 is_foreach_statement = true;
5491 if (check_for_loop_decls (for_loc, true) == NULL_TREE)
5492 c_parser_error (parser, "multiple iterating variables in fast enumeration");
5494 else
5495 check_for_loop_decls (for_loc, flag_isoc99);
5497 else
5498 goto init_expr;
5500 else
5502 init_expr:
5504 struct c_expr ce;
5505 tree init_expression;
5506 ce = c_parser_expression (parser);
5507 init_expression = ce.value;
5508 parser->objc_could_be_foreach_context = false;
5509 if (c_parser_next_token_is_keyword (parser, RID_IN))
5511 c_parser_consume_token (parser);
5512 is_foreach_statement = true;
5513 if (! lvalue_p (init_expression))
5514 c_parser_error (parser, "invalid iterating variable in fast enumeration");
5515 object_expression = c_fully_fold (init_expression, false, NULL);
5517 else
5519 ce = convert_lvalue_to_rvalue (loc, ce, true, false);
5520 init_expression = ce.value;
5521 c_finish_expr_stmt (loc, init_expression);
5522 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
5526 /* Parse the loop condition. In the case of a foreach
5527 statement, there is no loop condition. */
5528 gcc_assert (!parser->objc_could_be_foreach_context);
5529 if (!is_foreach_statement)
5531 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5533 if (ivdep)
5535 c_parser_error (parser, "missing loop condition in loop with "
5536 "%<GCC ivdep%> pragma");
5537 cond = error_mark_node;
5539 else
5541 c_parser_consume_token (parser);
5542 cond = NULL_TREE;
5545 else
5547 cond = c_parser_condition (parser);
5548 if (flag_cilkplus && contains_array_notation_expr (cond))
5550 error_at (loc, "array notations cannot be used in a "
5551 "condition for a for-loop");
5552 cond = error_mark_node;
5554 c_parser_skip_until_found (parser, CPP_SEMICOLON,
5555 "expected %<;%>");
5557 if (ivdep && cond != error_mark_node)
5558 cond = build2 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
5559 build_int_cst (integer_type_node,
5560 annot_expr_ivdep_kind));
5562 /* Parse the increment expression (the third expression in a
5563 for-statement). In the case of a foreach-statement, this is
5564 the expression that follows the 'in'. */
5565 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
5567 if (is_foreach_statement)
5569 c_parser_error (parser, "missing collection in fast enumeration");
5570 collection_expression = error_mark_node;
5572 else
5573 incr = c_process_expr_stmt (loc, NULL_TREE);
5575 else
5577 if (is_foreach_statement)
5578 collection_expression = c_fully_fold (c_parser_expression (parser).value,
5579 false, NULL);
5580 else
5582 struct c_expr ce = c_parser_expression (parser);
5583 ce = convert_lvalue_to_rvalue (loc, ce, true, false);
5584 incr = c_process_expr_stmt (loc, ce.value);
5587 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5589 save_break = c_break_label;
5590 c_break_label = NULL_TREE;
5591 save_cont = c_cont_label;
5592 c_cont_label = NULL_TREE;
5593 body = c_parser_c99_block_statement (parser);
5594 if (is_foreach_statement)
5595 objc_finish_foreach_loop (loc, object_expression, collection_expression, body, c_break_label, c_cont_label);
5596 else
5597 c_finish_loop (loc, cond, incr, body, c_break_label, c_cont_label, true);
5598 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99 || c_dialect_objc ()));
5599 c_break_label = save_break;
5600 c_cont_label = save_cont;
5603 /* Parse an asm statement, a GNU extension. This is a full-blown asm
5604 statement with inputs, outputs, clobbers, and volatile tag
5605 allowed.
5607 asm-statement:
5608 asm type-qualifier[opt] ( asm-argument ) ;
5609 asm type-qualifier[opt] goto ( asm-goto-argument ) ;
5611 asm-argument:
5612 asm-string-literal
5613 asm-string-literal : asm-operands[opt]
5614 asm-string-literal : asm-operands[opt] : asm-operands[opt]
5615 asm-string-literal : asm-operands[opt] : asm-operands[opt] : asm-clobbers[opt]
5617 asm-goto-argument:
5618 asm-string-literal : : asm-operands[opt] : asm-clobbers[opt] \
5619 : asm-goto-operands
5621 Qualifiers other than volatile are accepted in the syntax but
5622 warned for. */
5624 static tree
5625 c_parser_asm_statement (c_parser *parser)
5627 tree quals, str, outputs, inputs, clobbers, labels, ret;
5628 bool simple, is_goto;
5629 location_t asm_loc = c_parser_peek_token (parser)->location;
5630 int section, nsections;
5632 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
5633 c_parser_consume_token (parser);
5634 if (c_parser_next_token_is_keyword (parser, RID_VOLATILE))
5636 quals = c_parser_peek_token (parser)->value;
5637 c_parser_consume_token (parser);
5639 else if (c_parser_next_token_is_keyword (parser, RID_CONST)
5640 || c_parser_next_token_is_keyword (parser, RID_RESTRICT))
5642 warning_at (c_parser_peek_token (parser)->location,
5644 "%E qualifier ignored on asm",
5645 c_parser_peek_token (parser)->value);
5646 quals = NULL_TREE;
5647 c_parser_consume_token (parser);
5649 else
5650 quals = NULL_TREE;
5652 is_goto = false;
5653 if (c_parser_next_token_is_keyword (parser, RID_GOTO))
5655 c_parser_consume_token (parser);
5656 is_goto = true;
5659 /* ??? Follow the C++ parser rather than using the
5660 lex_untranslated_string kludge. */
5661 parser->lex_untranslated_string = true;
5662 ret = NULL;
5664 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5665 goto error;
5667 str = c_parser_asm_string_literal (parser);
5668 if (str == NULL_TREE)
5669 goto error_close_paren;
5671 simple = true;
5672 outputs = NULL_TREE;
5673 inputs = NULL_TREE;
5674 clobbers = NULL_TREE;
5675 labels = NULL_TREE;
5677 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN) && !is_goto)
5678 goto done_asm;
5680 /* Parse each colon-delimited section of operands. */
5681 nsections = 3 + is_goto;
5682 for (section = 0; section < nsections; ++section)
5684 if (!c_parser_require (parser, CPP_COLON,
5685 is_goto
5686 ? "expected %<:%>"
5687 : "expected %<:%> or %<)%>"))
5688 goto error_close_paren;
5690 /* Once past any colon, we're no longer a simple asm. */
5691 simple = false;
5693 if ((!c_parser_next_token_is (parser, CPP_COLON)
5694 && !c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
5695 || section == 3)
5696 switch (section)
5698 case 0:
5699 /* For asm goto, we don't allow output operands, but reserve
5700 the slot for a future extension that does allow them. */
5701 if (!is_goto)
5702 outputs = c_parser_asm_operands (parser);
5703 break;
5704 case 1:
5705 inputs = c_parser_asm_operands (parser);
5706 break;
5707 case 2:
5708 clobbers = c_parser_asm_clobbers (parser);
5709 break;
5710 case 3:
5711 labels = c_parser_asm_goto_operands (parser);
5712 break;
5713 default:
5714 gcc_unreachable ();
5717 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN) && !is_goto)
5718 goto done_asm;
5721 done_asm:
5722 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
5724 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5725 goto error;
5728 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
5729 c_parser_skip_to_end_of_block_or_statement (parser);
5731 ret = build_asm_stmt (quals, build_asm_expr (asm_loc, str, outputs, inputs,
5732 clobbers, labels, simple));
5734 error:
5735 parser->lex_untranslated_string = false;
5736 return ret;
5738 error_close_paren:
5739 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5740 goto error;
5743 /* Parse asm operands, a GNU extension.
5745 asm-operands:
5746 asm-operand
5747 asm-operands , asm-operand
5749 asm-operand:
5750 asm-string-literal ( expression )
5751 [ identifier ] asm-string-literal ( expression )
5754 static tree
5755 c_parser_asm_operands (c_parser *parser)
5757 tree list = NULL_TREE;
5758 while (true)
5760 tree name, str;
5761 struct c_expr expr;
5762 if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
5764 c_parser_consume_token (parser);
5765 if (c_parser_next_token_is (parser, CPP_NAME))
5767 tree id = c_parser_peek_token (parser)->value;
5768 c_parser_consume_token (parser);
5769 name = build_string (IDENTIFIER_LENGTH (id),
5770 IDENTIFIER_POINTER (id));
5772 else
5774 c_parser_error (parser, "expected identifier");
5775 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
5776 return NULL_TREE;
5778 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
5779 "expected %<]%>");
5781 else
5782 name = NULL_TREE;
5783 str = c_parser_asm_string_literal (parser);
5784 if (str == NULL_TREE)
5785 return NULL_TREE;
5786 parser->lex_untranslated_string = false;
5787 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5789 parser->lex_untranslated_string = true;
5790 return NULL_TREE;
5792 expr = c_parser_expression (parser);
5793 mark_exp_read (expr.value);
5794 parser->lex_untranslated_string = true;
5795 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
5797 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5798 return NULL_TREE;
5800 list = chainon (list, build_tree_list (build_tree_list (name, str),
5801 expr.value));
5802 if (c_parser_next_token_is (parser, CPP_COMMA))
5803 c_parser_consume_token (parser);
5804 else
5805 break;
5807 return list;
5810 /* Parse asm clobbers, a GNU extension.
5812 asm-clobbers:
5813 asm-string-literal
5814 asm-clobbers , asm-string-literal
5817 static tree
5818 c_parser_asm_clobbers (c_parser *parser)
5820 tree list = NULL_TREE;
5821 while (true)
5823 tree str = c_parser_asm_string_literal (parser);
5824 if (str)
5825 list = tree_cons (NULL_TREE, str, list);
5826 else
5827 return NULL_TREE;
5828 if (c_parser_next_token_is (parser, CPP_COMMA))
5829 c_parser_consume_token (parser);
5830 else
5831 break;
5833 return list;
5836 /* Parse asm goto labels, a GNU extension.
5838 asm-goto-operands:
5839 identifier
5840 asm-goto-operands , identifier
5843 static tree
5844 c_parser_asm_goto_operands (c_parser *parser)
5846 tree list = NULL_TREE;
5847 while (true)
5849 tree name, label;
5851 if (c_parser_next_token_is (parser, CPP_NAME))
5853 c_token *tok = c_parser_peek_token (parser);
5854 name = tok->value;
5855 label = lookup_label_for_goto (tok->location, name);
5856 c_parser_consume_token (parser);
5857 TREE_USED (label) = 1;
5859 else
5861 c_parser_error (parser, "expected identifier");
5862 return NULL_TREE;
5865 name = build_string (IDENTIFIER_LENGTH (name),
5866 IDENTIFIER_POINTER (name));
5867 list = tree_cons (name, label, list);
5868 if (c_parser_next_token_is (parser, CPP_COMMA))
5869 c_parser_consume_token (parser);
5870 else
5871 return nreverse (list);
5875 /* Parse an expression other than a compound expression; that is, an
5876 assignment expression (C90 6.3.16, C99 6.5.16). If AFTER is not
5877 NULL then it is an Objective-C message expression which is the
5878 primary-expression starting the expression as an initializer.
5880 assignment-expression:
5881 conditional-expression
5882 unary-expression assignment-operator assignment-expression
5884 assignment-operator: one of
5885 = *= /= %= += -= <<= >>= &= ^= |=
5887 In GNU C we accept any conditional expression on the LHS and
5888 diagnose the invalid lvalue rather than producing a syntax
5889 error. */
5891 static struct c_expr
5892 c_parser_expr_no_commas (c_parser *parser, struct c_expr *after,
5893 tree omp_atomic_lhs)
5895 struct c_expr lhs, rhs, ret;
5896 enum tree_code code;
5897 location_t op_location, exp_location;
5898 gcc_assert (!after || c_dialect_objc ());
5899 lhs = c_parser_conditional_expression (parser, after, omp_atomic_lhs);
5900 op_location = c_parser_peek_token (parser)->location;
5901 switch (c_parser_peek_token (parser)->type)
5903 case CPP_EQ:
5904 code = NOP_EXPR;
5905 break;
5906 case CPP_MULT_EQ:
5907 code = MULT_EXPR;
5908 break;
5909 case CPP_DIV_EQ:
5910 code = TRUNC_DIV_EXPR;
5911 break;
5912 case CPP_MOD_EQ:
5913 code = TRUNC_MOD_EXPR;
5914 break;
5915 case CPP_PLUS_EQ:
5916 code = PLUS_EXPR;
5917 break;
5918 case CPP_MINUS_EQ:
5919 code = MINUS_EXPR;
5920 break;
5921 case CPP_LSHIFT_EQ:
5922 code = LSHIFT_EXPR;
5923 break;
5924 case CPP_RSHIFT_EQ:
5925 code = RSHIFT_EXPR;
5926 break;
5927 case CPP_AND_EQ:
5928 code = BIT_AND_EXPR;
5929 break;
5930 case CPP_XOR_EQ:
5931 code = BIT_XOR_EXPR;
5932 break;
5933 case CPP_OR_EQ:
5934 code = BIT_IOR_EXPR;
5935 break;
5936 default:
5937 return lhs;
5939 c_parser_consume_token (parser);
5940 exp_location = c_parser_peek_token (parser)->location;
5941 rhs = c_parser_expr_no_commas (parser, NULL);
5942 rhs = convert_lvalue_to_rvalue (exp_location, rhs, true, true);
5944 ret.value = build_modify_expr (op_location, lhs.value, lhs.original_type,
5945 code, exp_location, rhs.value,
5946 rhs.original_type);
5947 if (code == NOP_EXPR)
5948 ret.original_code = MODIFY_EXPR;
5949 else
5951 TREE_NO_WARNING (ret.value) = 1;
5952 ret.original_code = ERROR_MARK;
5954 ret.original_type = NULL;
5955 return ret;
5958 /* Parse a conditional expression (C90 6.3.15, C99 6.5.15). If AFTER
5959 is not NULL then it is an Objective-C message expression which is
5960 the primary-expression starting the expression as an initializer.
5962 conditional-expression:
5963 logical-OR-expression
5964 logical-OR-expression ? expression : conditional-expression
5966 GNU extensions:
5968 conditional-expression:
5969 logical-OR-expression ? : conditional-expression
5972 static struct c_expr
5973 c_parser_conditional_expression (c_parser *parser, struct c_expr *after,
5974 tree omp_atomic_lhs)
5976 struct c_expr cond, exp1, exp2, ret;
5977 location_t cond_loc, colon_loc, middle_loc;
5979 gcc_assert (!after || c_dialect_objc ());
5981 cond = c_parser_binary_expression (parser, after, omp_atomic_lhs);
5983 if (c_parser_next_token_is_not (parser, CPP_QUERY))
5984 return cond;
5985 cond_loc = c_parser_peek_token (parser)->location;
5986 cond = convert_lvalue_to_rvalue (cond_loc, cond, true, true);
5987 c_parser_consume_token (parser);
5988 if (c_parser_next_token_is (parser, CPP_COLON))
5990 tree eptype = NULL_TREE;
5992 middle_loc = c_parser_peek_token (parser)->location;
5993 pedwarn (middle_loc, OPT_Wpedantic,
5994 "ISO C forbids omitting the middle term of a ?: expression");
5995 warn_for_omitted_condop (middle_loc, cond.value);
5996 if (TREE_CODE (cond.value) == EXCESS_PRECISION_EXPR)
5998 eptype = TREE_TYPE (cond.value);
5999 cond.value = TREE_OPERAND (cond.value, 0);
6001 /* Make sure first operand is calculated only once. */
6002 exp1.value = c_save_expr (default_conversion (cond.value));
6003 if (eptype)
6004 exp1.value = build1 (EXCESS_PRECISION_EXPR, eptype, exp1.value);
6005 exp1.original_type = NULL;
6006 cond.value = c_objc_common_truthvalue_conversion (cond_loc, exp1.value);
6007 c_inhibit_evaluation_warnings += cond.value == truthvalue_true_node;
6009 else
6011 cond.value
6012 = c_objc_common_truthvalue_conversion
6013 (cond_loc, default_conversion (cond.value));
6014 c_inhibit_evaluation_warnings += cond.value == truthvalue_false_node;
6015 exp1 = c_parser_expression_conv (parser);
6016 mark_exp_read (exp1.value);
6017 c_inhibit_evaluation_warnings +=
6018 ((cond.value == truthvalue_true_node)
6019 - (cond.value == truthvalue_false_node));
6022 colon_loc = c_parser_peek_token (parser)->location;
6023 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
6025 c_inhibit_evaluation_warnings -= cond.value == truthvalue_true_node;
6026 ret.value = error_mark_node;
6027 ret.original_code = ERROR_MARK;
6028 ret.original_type = NULL;
6029 return ret;
6032 location_t exp2_loc = c_parser_peek_token (parser)->location;
6033 exp2 = c_parser_conditional_expression (parser, NULL, NULL_TREE);
6034 exp2 = convert_lvalue_to_rvalue (exp2_loc, exp2, true, true);
6036 c_inhibit_evaluation_warnings -= cond.value == truthvalue_true_node;
6037 ret.value = build_conditional_expr (colon_loc, cond.value,
6038 cond.original_code == C_MAYBE_CONST_EXPR,
6039 exp1.value, exp1.original_type,
6040 exp2.value, exp2.original_type);
6041 ret.original_code = ERROR_MARK;
6042 if (exp1.value == error_mark_node || exp2.value == error_mark_node)
6043 ret.original_type = NULL;
6044 else
6046 tree t1, t2;
6048 /* If both sides are enum type, the default conversion will have
6049 made the type of the result be an integer type. We want to
6050 remember the enum types we started with. */
6051 t1 = exp1.original_type ? exp1.original_type : TREE_TYPE (exp1.value);
6052 t2 = exp2.original_type ? exp2.original_type : TREE_TYPE (exp2.value);
6053 ret.original_type = ((t1 != error_mark_node
6054 && t2 != error_mark_node
6055 && (TYPE_MAIN_VARIANT (t1)
6056 == TYPE_MAIN_VARIANT (t2)))
6057 ? t1
6058 : NULL);
6060 return ret;
6063 /* Parse a binary expression; that is, a logical-OR-expression (C90
6064 6.3.5-6.3.14, C99 6.5.5-6.5.14). If AFTER is not NULL then it is
6065 an Objective-C message expression which is the primary-expression
6066 starting the expression as an initializer.
6068 OMP_ATOMIC_LHS is NULL, unless parsing OpenMP #pragma omp atomic,
6069 when it should be the unfolded lhs. In a valid OpenMP source,
6070 one of the operands of the toplevel binary expression must be equal
6071 to it. In that case, just return a build2 created binary operation
6072 rather than result of parser_build_binary_op.
6074 multiplicative-expression:
6075 cast-expression
6076 multiplicative-expression * cast-expression
6077 multiplicative-expression / cast-expression
6078 multiplicative-expression % cast-expression
6080 additive-expression:
6081 multiplicative-expression
6082 additive-expression + multiplicative-expression
6083 additive-expression - multiplicative-expression
6085 shift-expression:
6086 additive-expression
6087 shift-expression << additive-expression
6088 shift-expression >> additive-expression
6090 relational-expression:
6091 shift-expression
6092 relational-expression < shift-expression
6093 relational-expression > shift-expression
6094 relational-expression <= shift-expression
6095 relational-expression >= shift-expression
6097 equality-expression:
6098 relational-expression
6099 equality-expression == relational-expression
6100 equality-expression != relational-expression
6102 AND-expression:
6103 equality-expression
6104 AND-expression & equality-expression
6106 exclusive-OR-expression:
6107 AND-expression
6108 exclusive-OR-expression ^ AND-expression
6110 inclusive-OR-expression:
6111 exclusive-OR-expression
6112 inclusive-OR-expression | exclusive-OR-expression
6114 logical-AND-expression:
6115 inclusive-OR-expression
6116 logical-AND-expression && inclusive-OR-expression
6118 logical-OR-expression:
6119 logical-AND-expression
6120 logical-OR-expression || logical-AND-expression
6123 static struct c_expr
6124 c_parser_binary_expression (c_parser *parser, struct c_expr *after,
6125 tree omp_atomic_lhs)
6127 /* A binary expression is parsed using operator-precedence parsing,
6128 with the operands being cast expressions. All the binary
6129 operators are left-associative. Thus a binary expression is of
6130 form:
6132 E0 op1 E1 op2 E2 ...
6134 which we represent on a stack. On the stack, the precedence
6135 levels are strictly increasing. When a new operator is
6136 encountered of higher precedence than that at the top of the
6137 stack, it is pushed; its LHS is the top expression, and its RHS
6138 is everything parsed until it is popped. When a new operator is
6139 encountered with precedence less than or equal to that at the top
6140 of the stack, triples E[i-1] op[i] E[i] are popped and replaced
6141 by the result of the operation until the operator at the top of
6142 the stack has lower precedence than the new operator or there is
6143 only one element on the stack; then the top expression is the LHS
6144 of the new operator. In the case of logical AND and OR
6145 expressions, we also need to adjust c_inhibit_evaluation_warnings
6146 as appropriate when the operators are pushed and popped. */
6148 struct {
6149 /* The expression at this stack level. */
6150 struct c_expr expr;
6151 /* The precedence of the operator on its left, PREC_NONE at the
6152 bottom of the stack. */
6153 enum c_parser_prec prec;
6154 /* The operation on its left. */
6155 enum tree_code op;
6156 /* The source location of this operation. */
6157 location_t loc;
6158 } stack[NUM_PRECS];
6159 int sp;
6160 /* Location of the binary operator. */
6161 location_t binary_loc = UNKNOWN_LOCATION; /* Quiet warning. */
6162 #define POP \
6163 do { \
6164 switch (stack[sp].op) \
6166 case TRUTH_ANDIF_EXPR: \
6167 c_inhibit_evaluation_warnings -= (stack[sp - 1].expr.value \
6168 == truthvalue_false_node); \
6169 break; \
6170 case TRUTH_ORIF_EXPR: \
6171 c_inhibit_evaluation_warnings -= (stack[sp - 1].expr.value \
6172 == truthvalue_true_node); \
6173 break; \
6174 default: \
6175 break; \
6177 stack[sp - 1].expr \
6178 = convert_lvalue_to_rvalue (stack[sp - 1].loc, \
6179 stack[sp - 1].expr, true, true); \
6180 stack[sp].expr \
6181 = convert_lvalue_to_rvalue (stack[sp].loc, \
6182 stack[sp].expr, true, true); \
6183 if (__builtin_expect (omp_atomic_lhs != NULL_TREE, 0) && sp == 1 \
6184 && c_parser_peek_token (parser)->type == CPP_SEMICOLON \
6185 && ((1 << stack[sp].prec) \
6186 & (1 << (PREC_BITOR | PREC_BITXOR | PREC_BITAND | PREC_SHIFT \
6187 | PREC_ADD | PREC_MULT))) \
6188 && stack[sp].op != TRUNC_MOD_EXPR \
6189 && stack[0].expr.value != error_mark_node \
6190 && stack[1].expr.value != error_mark_node \
6191 && (c_tree_equal (stack[0].expr.value, omp_atomic_lhs) \
6192 || c_tree_equal (stack[1].expr.value, omp_atomic_lhs))) \
6193 stack[0].expr.value \
6194 = build2 (stack[1].op, TREE_TYPE (stack[0].expr.value), \
6195 stack[0].expr.value, stack[1].expr.value); \
6196 else \
6197 stack[sp - 1].expr = parser_build_binary_op (stack[sp].loc, \
6198 stack[sp].op, \
6199 stack[sp - 1].expr, \
6200 stack[sp].expr); \
6201 sp--; \
6202 } while (0)
6203 gcc_assert (!after || c_dialect_objc ());
6204 stack[0].loc = c_parser_peek_token (parser)->location;
6205 stack[0].expr = c_parser_cast_expression (parser, after);
6206 stack[0].prec = PREC_NONE;
6207 sp = 0;
6208 while (true)
6210 enum c_parser_prec oprec;
6211 enum tree_code ocode;
6212 if (parser->error)
6213 goto out;
6214 switch (c_parser_peek_token (parser)->type)
6216 case CPP_MULT:
6217 oprec = PREC_MULT;
6218 ocode = MULT_EXPR;
6219 break;
6220 case CPP_DIV:
6221 oprec = PREC_MULT;
6222 ocode = TRUNC_DIV_EXPR;
6223 break;
6224 case CPP_MOD:
6225 oprec = PREC_MULT;
6226 ocode = TRUNC_MOD_EXPR;
6227 break;
6228 case CPP_PLUS:
6229 oprec = PREC_ADD;
6230 ocode = PLUS_EXPR;
6231 break;
6232 case CPP_MINUS:
6233 oprec = PREC_ADD;
6234 ocode = MINUS_EXPR;
6235 break;
6236 case CPP_LSHIFT:
6237 oprec = PREC_SHIFT;
6238 ocode = LSHIFT_EXPR;
6239 break;
6240 case CPP_RSHIFT:
6241 oprec = PREC_SHIFT;
6242 ocode = RSHIFT_EXPR;
6243 break;
6244 case CPP_LESS:
6245 oprec = PREC_REL;
6246 ocode = LT_EXPR;
6247 break;
6248 case CPP_GREATER:
6249 oprec = PREC_REL;
6250 ocode = GT_EXPR;
6251 break;
6252 case CPP_LESS_EQ:
6253 oprec = PREC_REL;
6254 ocode = LE_EXPR;
6255 break;
6256 case CPP_GREATER_EQ:
6257 oprec = PREC_REL;
6258 ocode = GE_EXPR;
6259 break;
6260 case CPP_EQ_EQ:
6261 oprec = PREC_EQ;
6262 ocode = EQ_EXPR;
6263 break;
6264 case CPP_NOT_EQ:
6265 oprec = PREC_EQ;
6266 ocode = NE_EXPR;
6267 break;
6268 case CPP_AND:
6269 oprec = PREC_BITAND;
6270 ocode = BIT_AND_EXPR;
6271 break;
6272 case CPP_XOR:
6273 oprec = PREC_BITXOR;
6274 ocode = BIT_XOR_EXPR;
6275 break;
6276 case CPP_OR:
6277 oprec = PREC_BITOR;
6278 ocode = BIT_IOR_EXPR;
6279 break;
6280 case CPP_AND_AND:
6281 oprec = PREC_LOGAND;
6282 ocode = TRUTH_ANDIF_EXPR;
6283 break;
6284 case CPP_OR_OR:
6285 oprec = PREC_LOGOR;
6286 ocode = TRUTH_ORIF_EXPR;
6287 break;
6288 default:
6289 /* Not a binary operator, so end of the binary
6290 expression. */
6291 goto out;
6293 binary_loc = c_parser_peek_token (parser)->location;
6294 while (oprec <= stack[sp].prec)
6295 POP;
6296 c_parser_consume_token (parser);
6297 switch (ocode)
6299 case TRUTH_ANDIF_EXPR:
6300 stack[sp].expr
6301 = convert_lvalue_to_rvalue (stack[sp].loc,
6302 stack[sp].expr, true, true);
6303 stack[sp].expr.value = c_objc_common_truthvalue_conversion
6304 (stack[sp].loc, default_conversion (stack[sp].expr.value));
6305 c_inhibit_evaluation_warnings += (stack[sp].expr.value
6306 == truthvalue_false_node);
6307 break;
6308 case TRUTH_ORIF_EXPR:
6309 stack[sp].expr
6310 = convert_lvalue_to_rvalue (stack[sp].loc,
6311 stack[sp].expr, true, true);
6312 stack[sp].expr.value = c_objc_common_truthvalue_conversion
6313 (stack[sp].loc, default_conversion (stack[sp].expr.value));
6314 c_inhibit_evaluation_warnings += (stack[sp].expr.value
6315 == truthvalue_true_node);
6316 break;
6317 default:
6318 break;
6320 sp++;
6321 stack[sp].loc = binary_loc;
6322 stack[sp].expr = c_parser_cast_expression (parser, NULL);
6323 stack[sp].prec = oprec;
6324 stack[sp].op = ocode;
6325 stack[sp].loc = binary_loc;
6327 out:
6328 while (sp > 0)
6329 POP;
6330 return stack[0].expr;
6331 #undef POP
6334 /* Parse a cast expression (C90 6.3.4, C99 6.5.4). If AFTER is not
6335 NULL then it is an Objective-C message expression which is the
6336 primary-expression starting the expression as an initializer.
6338 cast-expression:
6339 unary-expression
6340 ( type-name ) unary-expression
6343 static struct c_expr
6344 c_parser_cast_expression (c_parser *parser, struct c_expr *after)
6346 location_t cast_loc = c_parser_peek_token (parser)->location;
6347 gcc_assert (!after || c_dialect_objc ());
6348 if (after)
6349 return c_parser_postfix_expression_after_primary (parser,
6350 cast_loc, *after);
6351 /* If the expression begins with a parenthesized type name, it may
6352 be either a cast or a compound literal; we need to see whether
6353 the next character is '{' to tell the difference. If not, it is
6354 an unary expression. Full detection of unknown typenames here
6355 would require a 3-token lookahead. */
6356 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
6357 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
6359 struct c_type_name *type_name;
6360 struct c_expr ret;
6361 struct c_expr expr;
6362 c_parser_consume_token (parser);
6363 type_name = c_parser_type_name (parser);
6364 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6365 if (type_name == NULL)
6367 ret.value = error_mark_node;
6368 ret.original_code = ERROR_MARK;
6369 ret.original_type = NULL;
6370 return ret;
6373 /* Save casted types in the function's used types hash table. */
6374 used_types_insert (type_name->specs->type);
6376 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
6377 return c_parser_postfix_expression_after_paren_type (parser, type_name,
6378 cast_loc);
6380 location_t expr_loc = c_parser_peek_token (parser)->location;
6381 expr = c_parser_cast_expression (parser, NULL);
6382 expr = convert_lvalue_to_rvalue (expr_loc, expr, true, true);
6384 ret.value = c_cast_expr (cast_loc, type_name, expr.value);
6385 ret.original_code = ERROR_MARK;
6386 ret.original_type = NULL;
6387 return ret;
6389 else
6390 return c_parser_unary_expression (parser);
6393 /* Parse an unary expression (C90 6.3.3, C99 6.5.3).
6395 unary-expression:
6396 postfix-expression
6397 ++ unary-expression
6398 -- unary-expression
6399 unary-operator cast-expression
6400 sizeof unary-expression
6401 sizeof ( type-name )
6403 unary-operator: one of
6404 & * + - ~ !
6406 GNU extensions:
6408 unary-expression:
6409 __alignof__ unary-expression
6410 __alignof__ ( type-name )
6411 && identifier
6413 (C11 permits _Alignof with type names only.)
6415 unary-operator: one of
6416 __extension__ __real__ __imag__
6418 Transactional Memory:
6420 unary-expression:
6421 transaction-expression
6423 In addition, the GNU syntax treats ++ and -- as unary operators, so
6424 they may be applied to cast expressions with errors for non-lvalues
6425 given later. */
6427 static struct c_expr
6428 c_parser_unary_expression (c_parser *parser)
6430 int ext;
6431 struct c_expr ret, op;
6432 location_t op_loc = c_parser_peek_token (parser)->location;
6433 location_t exp_loc;
6434 ret.original_code = ERROR_MARK;
6435 ret.original_type = NULL;
6436 switch (c_parser_peek_token (parser)->type)
6438 case CPP_PLUS_PLUS:
6439 c_parser_consume_token (parser);
6440 exp_loc = c_parser_peek_token (parser)->location;
6441 op = c_parser_cast_expression (parser, NULL);
6443 /* If there is array notations in op, we expand them. */
6444 if (flag_cilkplus && TREE_CODE (op.value) == ARRAY_NOTATION_REF)
6445 return fix_array_notation_expr (exp_loc, PREINCREMENT_EXPR, op);
6446 else
6448 op = default_function_array_read_conversion (exp_loc, op);
6449 return parser_build_unary_op (op_loc, PREINCREMENT_EXPR, op);
6451 case CPP_MINUS_MINUS:
6452 c_parser_consume_token (parser);
6453 exp_loc = c_parser_peek_token (parser)->location;
6454 op = c_parser_cast_expression (parser, NULL);
6456 /* If there is array notations in op, we expand them. */
6457 if (flag_cilkplus && TREE_CODE (op.value) == ARRAY_NOTATION_REF)
6458 return fix_array_notation_expr (exp_loc, PREDECREMENT_EXPR, op);
6459 else
6461 op = default_function_array_read_conversion (exp_loc, op);
6462 return parser_build_unary_op (op_loc, PREDECREMENT_EXPR, op);
6464 case CPP_AND:
6465 c_parser_consume_token (parser);
6466 op = c_parser_cast_expression (parser, NULL);
6467 mark_exp_read (op.value);
6468 return parser_build_unary_op (op_loc, ADDR_EXPR, op);
6469 case CPP_MULT:
6470 c_parser_consume_token (parser);
6471 exp_loc = c_parser_peek_token (parser)->location;
6472 op = c_parser_cast_expression (parser, NULL);
6473 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
6474 ret.value = build_indirect_ref (op_loc, op.value, RO_UNARY_STAR);
6475 return ret;
6476 case CPP_PLUS:
6477 if (!c_dialect_objc () && !in_system_header_at (input_location))
6478 warning_at (op_loc,
6479 OPT_Wtraditional,
6480 "traditional C rejects the unary plus operator");
6481 c_parser_consume_token (parser);
6482 exp_loc = c_parser_peek_token (parser)->location;
6483 op = c_parser_cast_expression (parser, NULL);
6484 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
6485 return parser_build_unary_op (op_loc, CONVERT_EXPR, op);
6486 case CPP_MINUS:
6487 c_parser_consume_token (parser);
6488 exp_loc = c_parser_peek_token (parser)->location;
6489 op = c_parser_cast_expression (parser, NULL);
6490 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
6491 return parser_build_unary_op (op_loc, NEGATE_EXPR, op);
6492 case CPP_COMPL:
6493 c_parser_consume_token (parser);
6494 exp_loc = c_parser_peek_token (parser)->location;
6495 op = c_parser_cast_expression (parser, NULL);
6496 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
6497 return parser_build_unary_op (op_loc, BIT_NOT_EXPR, op);
6498 case CPP_NOT:
6499 c_parser_consume_token (parser);
6500 exp_loc = c_parser_peek_token (parser)->location;
6501 op = c_parser_cast_expression (parser, NULL);
6502 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
6503 return parser_build_unary_op (op_loc, TRUTH_NOT_EXPR, op);
6504 case CPP_AND_AND:
6505 /* Refer to the address of a label as a pointer. */
6506 c_parser_consume_token (parser);
6507 if (c_parser_next_token_is (parser, CPP_NAME))
6509 ret.value = finish_label_address_expr
6510 (c_parser_peek_token (parser)->value, op_loc);
6511 c_parser_consume_token (parser);
6513 else
6515 c_parser_error (parser, "expected identifier");
6516 ret.value = error_mark_node;
6518 return ret;
6519 case CPP_KEYWORD:
6520 switch (c_parser_peek_token (parser)->keyword)
6522 case RID_SIZEOF:
6523 return c_parser_sizeof_expression (parser);
6524 case RID_UPC_BLOCKSIZEOF:
6525 case RID_UPC_ELEMSIZEOF:
6526 case RID_UPC_LOCALSIZEOF:
6527 gcc_assert (flag_upc);
6528 return c_parser_sizeof_expression (parser);
6529 case RID_ALIGNOF:
6530 return c_parser_alignof_expression (parser);
6531 case RID_EXTENSION:
6532 c_parser_consume_token (parser);
6533 ext = disable_extension_diagnostics ();
6534 ret = c_parser_cast_expression (parser, NULL);
6535 restore_extension_diagnostics (ext);
6536 return ret;
6537 case RID_REALPART:
6538 c_parser_consume_token (parser);
6539 exp_loc = c_parser_peek_token (parser)->location;
6540 op = c_parser_cast_expression (parser, NULL);
6541 op = default_function_array_conversion (exp_loc, op);
6542 return parser_build_unary_op (op_loc, REALPART_EXPR, op);
6543 case RID_IMAGPART:
6544 c_parser_consume_token (parser);
6545 exp_loc = c_parser_peek_token (parser)->location;
6546 op = c_parser_cast_expression (parser, NULL);
6547 op = default_function_array_conversion (exp_loc, op);
6548 return parser_build_unary_op (op_loc, IMAGPART_EXPR, op);
6549 case RID_TRANSACTION_ATOMIC:
6550 case RID_TRANSACTION_RELAXED:
6551 return c_parser_transaction_expression (parser,
6552 c_parser_peek_token (parser)->keyword);
6553 default:
6554 return c_parser_postfix_expression (parser);
6556 default:
6557 return c_parser_postfix_expression (parser);
6561 /* Return the result of upc_blocksizeof applied to EXPR. */
6563 static
6564 struct c_expr
6565 upc_blocksizeof_expr (location_t loc, struct c_expr expr)
6567 struct c_expr ret;
6568 ret.original_code = ERROR_MARK;
6569 ret.original_type = NULL_TREE;
6570 if (expr.value == error_mark_node)
6572 ret.value = error_mark_node;
6573 pop_maybe_used (false);
6575 else
6577 ret.value = upc_blocksizeof (loc, TREE_TYPE (expr.value));
6578 pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (expr.value)));
6580 return ret;
6583 /* Return the result of upc_blocksizeof applied to T, a structure
6584 for the type name passed to sizeof (rather than the type itself). */
6586 static
6587 struct c_expr
6588 upc_blocksizeof_type (location_t loc, struct c_type_name *t)
6590 tree type;
6591 struct c_expr ret;
6592 ret.original_code = ERROR_MARK;
6593 ret.original_type = NULL_TREE;
6594 type = groktypename (t, NULL, NULL);
6595 if (type == error_mark_node)
6597 ret.value = error_mark_node;
6598 pop_maybe_used (false);
6600 else
6602 ret.value = upc_blocksizeof (loc, type);
6603 pop_maybe_used (C_TYPE_VARIABLE_SIZE (type));
6605 return ret;
6608 /* Return the result of upc_elemsizeof applied to EXPR. */
6610 static
6611 struct c_expr
6612 upc_elemsizeof_expr (location_t loc, struct c_expr expr)
6614 struct c_expr ret;
6615 ret.original_code = ERROR_MARK;
6616 ret.original_type = NULL_TREE;
6617 if (expr.value == error_mark_node)
6619 ret.value = error_mark_node;
6620 pop_maybe_used (false);
6622 else
6624 ret.value = upc_elemsizeof (loc, TREE_TYPE (expr.value));
6625 pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (expr.value)));
6627 return ret;
6630 /* Return the result of upc_elemsizeof applied to T, a structure
6631 for the type name passed to sizeof (rather than the type itself). */
6633 static
6634 struct c_expr
6635 upc_elemsizeof_type (location_t loc, struct c_type_name *t)
6637 tree type;
6638 struct c_expr ret;
6639 ret.original_code = ERROR_MARK;
6640 ret.original_type = NULL_TREE;
6641 type = groktypename (t, NULL, NULL);
6642 if (type == error_mark_node)
6644 ret.value = error_mark_node;
6645 pop_maybe_used (false);
6647 else
6649 ret.value = upc_elemsizeof (loc, type);
6650 pop_maybe_used (C_TYPE_VARIABLE_SIZE (type));
6652 return ret;
6655 /* Return the result of upc_localsizeof applied to EXPR. */
6657 static
6658 struct c_expr
6659 upc_localsizeof_expr (location_t loc, struct c_expr expr)
6661 struct c_expr ret;
6662 ret.original_code = ERROR_MARK;
6663 ret.original_type = NULL_TREE;
6664 if (expr.value == error_mark_node)
6666 ret.value = error_mark_node;
6667 pop_maybe_used (false);
6669 else
6671 ret.value = upc_localsizeof (loc, TREE_TYPE (expr.value));
6672 pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (expr.value)));
6674 return ret;
6677 /* Return the result of upc_localsizeof applied to T, a structure
6678 for the type name passed to sizeof (rather than the type itself). */
6680 static
6681 struct c_expr
6682 upc_localsizeof_type (location_t loc, struct c_type_name *t)
6684 tree type;
6685 struct c_expr ret;
6686 ret.original_code = ERROR_MARK;
6687 ret.original_type = NULL_TREE;
6688 type = groktypename (t, NULL, NULL);
6689 if (type == error_mark_node)
6691 ret.value = error_mark_node;
6692 pop_maybe_used (false);
6694 else
6696 ret.value = upc_localsizeof (loc, type);
6697 pop_maybe_used (C_TYPE_VARIABLE_SIZE (type));
6699 return ret;
6702 /* Parse a sizeof expression. */
6704 static struct c_expr
6705 c_parser_sizeof_expression (c_parser *parser)
6707 struct c_expr expr;
6708 location_t expr_loc;
6709 enum rid keyword = c_parser_peek_token (parser)->keyword;
6710 c_parser_consume_token (parser);
6711 c_inhibit_evaluation_warnings++;
6712 in_sizeof++;
6713 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
6714 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
6716 /* Either sizeof ( type-name ) or sizeof unary-expression
6717 starting with a compound literal. */
6718 struct c_type_name *type_name;
6719 c_parser_consume_token (parser);
6720 expr_loc = c_parser_peek_token (parser)->location;
6721 type_name = c_parser_type_name (parser);
6722 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6723 if (type_name == NULL)
6725 struct c_expr ret;
6726 c_inhibit_evaluation_warnings--;
6727 in_sizeof--;
6728 ret.value = error_mark_node;
6729 ret.original_code = ERROR_MARK;
6730 ret.original_type = NULL;
6731 return ret;
6733 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
6735 expr = c_parser_postfix_expression_after_paren_type (parser,
6736 type_name,
6737 expr_loc);
6738 goto sizeof_expr;
6740 /* sizeof ( type-name ). */
6741 c_inhibit_evaluation_warnings--;
6742 in_sizeof--;
6743 /* Handle upc_*_sizeof (type) operations. */
6744 switch (keyword)
6746 case RID_UPC_BLOCKSIZEOF:
6747 return upc_blocksizeof_type (expr_loc, type_name);
6748 case RID_UPC_ELEMSIZEOF:
6749 return upc_elemsizeof_type (expr_loc, type_name);
6750 case RID_UPC_LOCALSIZEOF:
6751 return upc_localsizeof_type (expr_loc, type_name);
6752 default: break;
6754 return c_expr_sizeof_type (expr_loc, type_name);
6756 else
6758 expr_loc = c_parser_peek_token (parser)->location;
6759 expr = c_parser_unary_expression (parser);
6760 sizeof_expr:
6761 c_inhibit_evaluation_warnings--;
6762 in_sizeof--;
6763 mark_exp_read (expr.value);
6764 if (TREE_CODE (expr.value) == COMPONENT_REF
6765 && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
6766 error_at (expr_loc, "%<sizeof%> applied to a bit-field");
6767 /* Handle upc_*_sizeof (expr) operations. */
6768 switch (keyword)
6770 case RID_UPC_BLOCKSIZEOF:
6771 return upc_blocksizeof_expr (expr_loc, expr);
6772 case RID_UPC_ELEMSIZEOF:
6773 return upc_elemsizeof_expr (expr_loc, expr);
6774 case RID_UPC_LOCALSIZEOF:
6775 return upc_localsizeof_expr (expr_loc, expr);
6776 case RID_SIZEOF:
6777 return c_expr_sizeof_expr (expr_loc, expr);
6778 default: break;
6780 return c_expr_sizeof_expr (expr_loc, expr);
6784 /* Parse an alignof expression. */
6786 static struct c_expr
6787 c_parser_alignof_expression (c_parser *parser)
6789 struct c_expr expr;
6790 location_t loc = c_parser_peek_token (parser)->location;
6791 tree alignof_spelling = c_parser_peek_token (parser)->value;
6792 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ALIGNOF));
6793 bool is_c11_alignof = strcmp (IDENTIFIER_POINTER (alignof_spelling),
6794 "_Alignof") == 0;
6795 /* A diagnostic is not required for the use of this identifier in
6796 the implementation namespace; only diagnose it for the C11
6797 spelling because of existing code using the other spellings. */
6798 if (!flag_isoc11 && is_c11_alignof)
6800 if (flag_isoc99)
6801 pedwarn (loc, OPT_Wpedantic, "ISO C99 does not support %qE",
6802 alignof_spelling);
6803 else
6804 pedwarn (loc, OPT_Wpedantic, "ISO C90 does not support %qE",
6805 alignof_spelling);
6807 c_parser_consume_token (parser);
6808 c_inhibit_evaluation_warnings++;
6809 in_alignof++;
6810 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
6811 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
6813 /* Either __alignof__ ( type-name ) or __alignof__
6814 unary-expression starting with a compound literal. */
6815 location_t loc;
6816 struct c_type_name *type_name;
6817 struct c_expr ret;
6818 c_parser_consume_token (parser);
6819 loc = c_parser_peek_token (parser)->location;
6820 type_name = c_parser_type_name (parser);
6821 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6822 if (type_name == NULL)
6824 struct c_expr ret;
6825 c_inhibit_evaluation_warnings--;
6826 in_alignof--;
6827 ret.value = error_mark_node;
6828 ret.original_code = ERROR_MARK;
6829 ret.original_type = NULL;
6830 return ret;
6832 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
6834 expr = c_parser_postfix_expression_after_paren_type (parser,
6835 type_name,
6836 loc);
6837 goto alignof_expr;
6839 /* alignof ( type-name ). */
6840 c_inhibit_evaluation_warnings--;
6841 in_alignof--;
6842 ret.value = c_sizeof_or_alignof_type (loc, groktypename (type_name,
6843 NULL, NULL),
6844 false, is_c11_alignof, 1);
6845 ret.original_code = ERROR_MARK;
6846 ret.original_type = NULL;
6847 return ret;
6849 else
6851 struct c_expr ret;
6852 expr = c_parser_unary_expression (parser);
6853 alignof_expr:
6854 mark_exp_read (expr.value);
6855 c_inhibit_evaluation_warnings--;
6856 in_alignof--;
6857 pedwarn (loc, OPT_Wpedantic, "ISO C does not allow %<%E (expression)%>",
6858 alignof_spelling);
6859 ret.value = c_alignof_expr (loc, expr.value);
6860 ret.original_code = ERROR_MARK;
6861 ret.original_type = NULL;
6862 return ret;
6866 /* Helper function to read arguments of builtins which are interfaces
6867 for the middle-end nodes like COMPLEX_EXPR, VEC_PERM_EXPR and
6868 others. The name of the builtin is passed using BNAME parameter.
6869 Function returns true if there were no errors while parsing and
6870 stores the arguments in CEXPR_LIST. */
6871 static bool
6872 c_parser_get_builtin_args (c_parser *parser, const char *bname,
6873 vec<c_expr_t, va_gc> **ret_cexpr_list,
6874 bool choose_expr_p)
6876 location_t loc = c_parser_peek_token (parser)->location;
6877 vec<c_expr_t, va_gc> *cexpr_list;
6878 c_expr_t expr;
6879 bool saved_force_folding_builtin_constant_p;
6881 *ret_cexpr_list = NULL;
6882 if (c_parser_next_token_is_not (parser, CPP_OPEN_PAREN))
6884 error_at (loc, "cannot take address of %qs", bname);
6885 return false;
6888 c_parser_consume_token (parser);
6890 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
6892 c_parser_consume_token (parser);
6893 return true;
6896 saved_force_folding_builtin_constant_p
6897 = force_folding_builtin_constant_p;
6898 force_folding_builtin_constant_p |= choose_expr_p;
6899 expr = c_parser_expr_no_commas (parser, NULL);
6900 force_folding_builtin_constant_p
6901 = saved_force_folding_builtin_constant_p;
6902 vec_alloc (cexpr_list, 1);
6903 vec_safe_push (cexpr_list, expr);
6904 while (c_parser_next_token_is (parser, CPP_COMMA))
6906 c_parser_consume_token (parser);
6907 expr = c_parser_expr_no_commas (parser, NULL);
6908 vec_safe_push (cexpr_list, expr);
6911 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
6912 return false;
6914 *ret_cexpr_list = cexpr_list;
6915 return true;
6918 /* This represents a single generic-association. */
6920 struct c_generic_association
6922 /* The location of the starting token of the type. */
6923 location_t type_location;
6924 /* The association's type, or NULL_TREE for 'default'. */
6925 tree type;
6926 /* The association's expression. */
6927 struct c_expr expression;
6930 /* Parse a generic-selection. (C11 6.5.1.1).
6932 generic-selection:
6933 _Generic ( assignment-expression , generic-assoc-list )
6935 generic-assoc-list:
6936 generic-association
6937 generic-assoc-list , generic-association
6939 generic-association:
6940 type-name : assignment-expression
6941 default : assignment-expression
6944 static struct c_expr
6945 c_parser_generic_selection (c_parser *parser)
6947 vec<c_generic_association> associations = vNULL;
6948 struct c_expr selector, error_expr;
6949 tree selector_type;
6950 struct c_generic_association matched_assoc;
6951 bool match_found = false;
6952 location_t generic_loc, selector_loc;
6954 error_expr.original_code = ERROR_MARK;
6955 error_expr.original_type = NULL;
6956 error_expr.value = error_mark_node;
6957 matched_assoc.type_location = UNKNOWN_LOCATION;
6958 matched_assoc.type = NULL_TREE;
6959 matched_assoc.expression = error_expr;
6961 gcc_assert (c_parser_next_token_is_keyword (parser, RID_GENERIC));
6962 generic_loc = c_parser_peek_token (parser)->location;
6963 c_parser_consume_token (parser);
6964 if (!flag_isoc11)
6966 if (flag_isoc99)
6967 pedwarn (generic_loc, OPT_Wpedantic,
6968 "ISO C99 does not support %<_Generic%>");
6969 else
6970 pedwarn (generic_loc, OPT_Wpedantic,
6971 "ISO C90 does not support %<_Generic%>");
6974 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6975 return error_expr;
6977 c_inhibit_evaluation_warnings++;
6978 selector_loc = c_parser_peek_token (parser)->location;
6979 selector = c_parser_expr_no_commas (parser, NULL);
6980 selector = default_function_array_conversion (selector_loc, selector);
6981 c_inhibit_evaluation_warnings--;
6983 if (selector.value == error_mark_node)
6985 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6986 return selector;
6988 selector_type = TREE_TYPE (selector.value);
6989 /* In ISO C terms, rvalues (including the controlling expression of
6990 _Generic) do not have qualified types. */
6991 if (TREE_CODE (selector_type) != ARRAY_TYPE)
6992 selector_type = TYPE_MAIN_VARIANT (selector_type);
6993 /* In ISO C terms, _Noreturn is not part of the type of expressions
6994 such as &abort, but in GCC it is represented internally as a type
6995 qualifier. */
6996 if (FUNCTION_POINTER_TYPE_P (selector_type)
6997 && TYPE_QUALS (TREE_TYPE (selector_type)) != TYPE_UNQUALIFIED)
6998 selector_type
6999 = build_pointer_type (TYPE_MAIN_VARIANT (TREE_TYPE (selector_type)));
7001 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
7003 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7004 return error_expr;
7007 while (1)
7009 struct c_generic_association assoc, *iter;
7010 unsigned int ix;
7011 c_token *token = c_parser_peek_token (parser);
7013 assoc.type_location = token->location;
7014 if (token->type == CPP_KEYWORD && token->keyword == RID_DEFAULT)
7016 c_parser_consume_token (parser);
7017 assoc.type = NULL_TREE;
7019 else
7021 struct c_type_name *type_name;
7023 type_name = c_parser_type_name (parser);
7024 if (type_name == NULL)
7026 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7027 goto error_exit;
7029 assoc.type = groktypename (type_name, NULL, NULL);
7030 if (assoc.type == error_mark_node)
7032 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7033 goto error_exit;
7036 if (TREE_CODE (assoc.type) == FUNCTION_TYPE)
7037 error_at (assoc.type_location,
7038 "%<_Generic%> association has function type");
7039 else if (!COMPLETE_TYPE_P (assoc.type))
7040 error_at (assoc.type_location,
7041 "%<_Generic%> association has incomplete type");
7043 if (variably_modified_type_p (assoc.type, NULL_TREE))
7044 error_at (assoc.type_location,
7045 "%<_Generic%> association has "
7046 "variable length type");
7049 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
7051 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7052 goto error_exit;
7055 assoc.expression = c_parser_expr_no_commas (parser, NULL);
7056 if (assoc.expression.value == error_mark_node)
7058 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7059 goto error_exit;
7062 for (ix = 0; associations.iterate (ix, &iter); ++ix)
7064 if (assoc.type == NULL_TREE)
7066 if (iter->type == NULL_TREE)
7068 error_at (assoc.type_location,
7069 "duplicate %<default%> case in %<_Generic%>");
7070 inform (iter->type_location, "original %<default%> is here");
7073 else if (iter->type != NULL_TREE)
7075 if (comptypes (assoc.type, iter->type))
7077 error_at (assoc.type_location,
7078 "%<_Generic%> specifies two compatible types");
7079 inform (iter->type_location, "compatible type is here");
7084 if (assoc.type == NULL_TREE)
7086 if (!match_found)
7088 matched_assoc = assoc;
7089 match_found = true;
7092 else if (comptypes (assoc.type, selector_type))
7094 if (!match_found || matched_assoc.type == NULL_TREE)
7096 matched_assoc = assoc;
7097 match_found = true;
7099 else
7101 error_at (assoc.type_location,
7102 "%<_Generic> selector matches multiple associations");
7103 inform (matched_assoc.type_location,
7104 "other match is here");
7108 associations.safe_push (assoc);
7110 if (c_parser_peek_token (parser)->type != CPP_COMMA)
7111 break;
7112 c_parser_consume_token (parser);
7115 associations.release ();
7117 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
7119 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7120 return error_expr;
7123 if (!match_found)
7125 error_at (selector_loc, "%<_Generic%> selector of type %qT is not "
7126 "compatible with any association",
7127 selector_type);
7128 return error_expr;
7131 return matched_assoc.expression;
7133 error_exit:
7134 associations.release ();
7135 return error_expr;
7138 /* Parse a postfix expression (C90 6.3.1-6.3.2, C99 6.5.1-6.5.2).
7140 postfix-expression:
7141 primary-expression
7142 postfix-expression [ expression ]
7143 postfix-expression ( argument-expression-list[opt] )
7144 postfix-expression . identifier
7145 postfix-expression -> identifier
7146 postfix-expression ++
7147 postfix-expression --
7148 ( type-name ) { initializer-list }
7149 ( type-name ) { initializer-list , }
7151 argument-expression-list:
7152 argument-expression
7153 argument-expression-list , argument-expression
7155 primary-expression:
7156 identifier
7157 constant
7158 string-literal
7159 ( expression )
7160 generic-selection
7162 GNU extensions:
7164 primary-expression:
7165 __func__
7166 (treated as a keyword in GNU C)
7167 __FUNCTION__
7168 __PRETTY_FUNCTION__
7169 ( compound-statement )
7170 __builtin_va_arg ( assignment-expression , type-name )
7171 __builtin_offsetof ( type-name , offsetof-member-designator )
7172 __builtin_choose_expr ( assignment-expression ,
7173 assignment-expression ,
7174 assignment-expression )
7175 __builtin_types_compatible_p ( type-name , type-name )
7176 __builtin_complex ( assignment-expression , assignment-expression )
7177 __builtin_shuffle ( assignment-expression , assignment-expression )
7178 __builtin_shuffle ( assignment-expression ,
7179 assignment-expression ,
7180 assignment-expression, )
7182 offsetof-member-designator:
7183 identifier
7184 offsetof-member-designator . identifier
7185 offsetof-member-designator [ expression ]
7187 Objective-C:
7189 primary-expression:
7190 [ objc-receiver objc-message-args ]
7191 @selector ( objc-selector-arg )
7192 @protocol ( identifier )
7193 @encode ( type-name )
7194 objc-string-literal
7195 Classname . identifier
7198 static struct c_expr
7199 c_parser_postfix_expression (c_parser *parser)
7201 struct c_expr expr, e1;
7202 struct c_type_name *t1, *t2;
7203 location_t loc = c_parser_peek_token (parser)->location;;
7204 expr.original_code = ERROR_MARK;
7205 expr.original_type = NULL;
7206 switch (c_parser_peek_token (parser)->type)
7208 case CPP_NUMBER:
7209 expr.value = c_parser_peek_token (parser)->value;
7210 loc = c_parser_peek_token (parser)->location;
7211 c_parser_consume_token (parser);
7212 if (TREE_CODE (expr.value) == FIXED_CST
7213 && !targetm.fixed_point_supported_p ())
7215 error_at (loc, "fixed-point types not supported for this target");
7216 expr.value = error_mark_node;
7218 break;
7219 case CPP_CHAR:
7220 case CPP_CHAR16:
7221 case CPP_CHAR32:
7222 case CPP_WCHAR:
7223 expr.value = c_parser_peek_token (parser)->value;
7224 c_parser_consume_token (parser);
7225 break;
7226 case CPP_STRING:
7227 case CPP_STRING16:
7228 case CPP_STRING32:
7229 case CPP_WSTRING:
7230 case CPP_UTF8STRING:
7231 expr.value = c_parser_peek_token (parser)->value;
7232 expr.original_code = STRING_CST;
7233 c_parser_consume_token (parser);
7234 break;
7235 case CPP_OBJC_STRING:
7236 gcc_assert (c_dialect_objc ());
7237 expr.value
7238 = objc_build_string_object (c_parser_peek_token (parser)->value);
7239 c_parser_consume_token (parser);
7240 break;
7241 case CPP_NAME:
7242 switch (c_parser_peek_token (parser)->id_kind)
7244 case C_ID_ID:
7246 tree id = c_parser_peek_token (parser)->value;
7247 c_parser_consume_token (parser);
7248 expr.value = build_external_ref (loc, id,
7249 (c_parser_peek_token (parser)->type
7250 == CPP_OPEN_PAREN),
7251 &expr.original_type);
7252 break;
7254 case C_ID_CLASSNAME:
7256 /* Here we parse the Objective-C 2.0 Class.name dot
7257 syntax. */
7258 tree class_name = c_parser_peek_token (parser)->value;
7259 tree component;
7260 c_parser_consume_token (parser);
7261 gcc_assert (c_dialect_objc ());
7262 if (!c_parser_require (parser, CPP_DOT, "expected %<.%>"))
7264 expr.value = error_mark_node;
7265 break;
7267 if (c_parser_next_token_is_not (parser, CPP_NAME))
7269 c_parser_error (parser, "expected identifier");
7270 expr.value = error_mark_node;
7271 break;
7273 component = c_parser_peek_token (parser)->value;
7274 c_parser_consume_token (parser);
7275 expr.value = objc_build_class_component_ref (class_name,
7276 component);
7277 break;
7279 default:
7280 c_parser_error (parser, "expected expression");
7281 expr.value = error_mark_node;
7282 break;
7284 break;
7285 case CPP_OPEN_PAREN:
7286 /* A parenthesized expression, statement expression or compound
7287 literal. */
7288 if (c_parser_peek_2nd_token (parser)->type == CPP_OPEN_BRACE)
7290 /* A statement expression. */
7291 tree stmt;
7292 location_t brace_loc;
7293 c_parser_consume_token (parser);
7294 brace_loc = c_parser_peek_token (parser)->location;
7295 c_parser_consume_token (parser);
7296 if (!building_stmt_list_p ())
7298 error_at (loc, "braced-group within expression allowed "
7299 "only inside a function");
7300 parser->error = true;
7301 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
7302 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7303 expr.value = error_mark_node;
7304 break;
7306 stmt = c_begin_stmt_expr ();
7307 c_parser_compound_statement_nostart (parser);
7308 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7309 "expected %<)%>");
7310 pedwarn (loc, OPT_Wpedantic,
7311 "ISO C forbids braced-groups within expressions");
7312 expr.value = c_finish_stmt_expr (brace_loc, stmt);
7313 mark_exp_read (expr.value);
7315 else if (c_token_starts_typename (c_parser_peek_2nd_token (parser)))
7317 /* A compound literal. ??? Can we actually get here rather
7318 than going directly to
7319 c_parser_postfix_expression_after_paren_type from
7320 elsewhere? */
7321 location_t loc;
7322 struct c_type_name *type_name;
7323 c_parser_consume_token (parser);
7324 loc = c_parser_peek_token (parser)->location;
7325 type_name = c_parser_type_name (parser);
7326 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7327 "expected %<)%>");
7328 if (type_name == NULL)
7330 expr.value = error_mark_node;
7332 else
7333 expr = c_parser_postfix_expression_after_paren_type (parser,
7334 type_name,
7335 loc);
7337 else
7339 /* A parenthesized expression. */
7340 c_parser_consume_token (parser);
7341 expr = c_parser_expression (parser);
7342 if (TREE_CODE (expr.value) == MODIFY_EXPR)
7343 TREE_NO_WARNING (expr.value) = 1;
7344 if (expr.original_code != C_MAYBE_CONST_EXPR)
7345 expr.original_code = ERROR_MARK;
7346 /* Don't change EXPR.ORIGINAL_TYPE. */
7347 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7348 "expected %<)%>");
7350 break;
7351 case CPP_KEYWORD:
7352 switch (c_parser_peek_token (parser)->keyword)
7354 case RID_FUNCTION_NAME:
7355 case RID_PRETTY_FUNCTION_NAME:
7356 case RID_C99_FUNCTION_NAME:
7357 expr.value = fname_decl (loc,
7358 c_parser_peek_token (parser)->keyword,
7359 c_parser_peek_token (parser)->value);
7360 c_parser_consume_token (parser);
7361 break;
7362 case RID_VA_ARG:
7363 c_parser_consume_token (parser);
7364 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7366 expr.value = error_mark_node;
7367 break;
7369 e1 = c_parser_expr_no_commas (parser, NULL);
7370 mark_exp_read (e1.value);
7371 e1.value = c_fully_fold (e1.value, false, NULL);
7372 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
7374 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7375 expr.value = error_mark_node;
7376 break;
7378 loc = c_parser_peek_token (parser)->location;
7379 t1 = c_parser_type_name (parser);
7380 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7381 "expected %<)%>");
7382 if (t1 == NULL)
7384 expr.value = error_mark_node;
7386 else
7388 tree type_expr = NULL_TREE;
7389 expr.value = c_build_va_arg (loc, e1.value,
7390 groktypename (t1, &type_expr, NULL));
7391 if (type_expr)
7393 expr.value = build2 (C_MAYBE_CONST_EXPR,
7394 TREE_TYPE (expr.value), type_expr,
7395 expr.value);
7396 C_MAYBE_CONST_EXPR_NON_CONST (expr.value) = true;
7399 break;
7400 case RID_OFFSETOF:
7401 c_parser_consume_token (parser);
7402 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7404 expr.value = error_mark_node;
7405 break;
7407 t1 = c_parser_type_name (parser);
7408 if (t1 == NULL)
7409 parser->error = true;
7410 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
7411 gcc_assert (parser->error);
7412 if (parser->error)
7414 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7415 expr.value = error_mark_node;
7416 break;
7420 tree type = groktypename (t1, NULL, NULL);
7421 tree offsetof_ref;
7422 if (type == error_mark_node)
7423 offsetof_ref = error_mark_node;
7424 else
7426 offsetof_ref = build1 (INDIRECT_REF, type, null_pointer_node);
7427 SET_EXPR_LOCATION (offsetof_ref, loc);
7429 /* Parse the second argument to __builtin_offsetof. We
7430 must have one identifier, and beyond that we want to
7431 accept sub structure and sub array references. */
7432 if (c_parser_next_token_is (parser, CPP_NAME))
7434 offsetof_ref = build_component_ref
7435 (loc, offsetof_ref, c_parser_peek_token (parser)->value);
7436 c_parser_consume_token (parser);
7437 while (c_parser_next_token_is (parser, CPP_DOT)
7438 || c_parser_next_token_is (parser,
7439 CPP_OPEN_SQUARE)
7440 || c_parser_next_token_is (parser,
7441 CPP_DEREF))
7443 if (c_parser_next_token_is (parser, CPP_DEREF))
7445 loc = c_parser_peek_token (parser)->location;
7446 offsetof_ref = build_array_ref (loc,
7447 offsetof_ref,
7448 integer_zero_node);
7449 goto do_dot;
7451 else if (c_parser_next_token_is (parser, CPP_DOT))
7453 do_dot:
7454 c_parser_consume_token (parser);
7455 if (c_parser_next_token_is_not (parser,
7456 CPP_NAME))
7458 c_parser_error (parser, "expected identifier");
7459 break;
7461 offsetof_ref = build_component_ref
7462 (loc, offsetof_ref,
7463 c_parser_peek_token (parser)->value);
7464 c_parser_consume_token (parser);
7466 else
7468 struct c_expr ce;
7469 tree idx;
7470 loc = c_parser_peek_token (parser)->location;
7471 c_parser_consume_token (parser);
7472 ce = c_parser_expression (parser);
7473 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
7474 idx = ce.value;
7475 idx = c_fully_fold (idx, false, NULL);
7476 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
7477 "expected %<]%>");
7478 offsetof_ref = build_array_ref (loc, offsetof_ref, idx);
7482 else
7483 c_parser_error (parser, "expected identifier");
7484 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7485 "expected %<)%>");
7486 expr.value = fold_offsetof (offsetof_ref);
7488 break;
7489 case RID_CHOOSE_EXPR:
7491 vec<c_expr_t, va_gc> *cexpr_list;
7492 c_expr_t *e1_p, *e2_p, *e3_p;
7493 tree c;
7495 c_parser_consume_token (parser);
7496 if (!c_parser_get_builtin_args (parser,
7497 "__builtin_choose_expr",
7498 &cexpr_list, true))
7500 expr.value = error_mark_node;
7501 break;
7504 if (vec_safe_length (cexpr_list) != 3)
7506 error_at (loc, "wrong number of arguments to "
7507 "%<__builtin_choose_expr%>");
7508 expr.value = error_mark_node;
7509 break;
7512 e1_p = &(*cexpr_list)[0];
7513 e2_p = &(*cexpr_list)[1];
7514 e3_p = &(*cexpr_list)[2];
7516 c = e1_p->value;
7517 mark_exp_read (e2_p->value);
7518 mark_exp_read (e3_p->value);
7519 if (TREE_CODE (c) != INTEGER_CST
7520 || !INTEGRAL_TYPE_P (TREE_TYPE (c)))
7521 error_at (loc,
7522 "first argument to %<__builtin_choose_expr%> not"
7523 " a constant");
7524 constant_expression_warning (c);
7525 expr = integer_zerop (c) ? *e3_p : *e2_p;
7526 break;
7528 case RID_TYPES_COMPATIBLE_P:
7529 c_parser_consume_token (parser);
7530 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7532 expr.value = error_mark_node;
7533 break;
7535 t1 = c_parser_type_name (parser);
7536 if (t1 == NULL)
7538 expr.value = error_mark_node;
7539 break;
7541 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
7543 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7544 expr.value = error_mark_node;
7545 break;
7547 t2 = c_parser_type_name (parser);
7548 if (t2 == NULL)
7550 expr.value = error_mark_node;
7551 break;
7553 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7554 "expected %<)%>");
7556 tree e1, e2;
7557 e1 = groktypename (t1, NULL, NULL);
7558 e2 = groktypename (t2, NULL, NULL);
7559 if (e1 == error_mark_node || e2 == error_mark_node)
7561 expr.value = error_mark_node;
7562 break;
7565 e1 = TYPE_MAIN_VARIANT (e1);
7566 e2 = TYPE_MAIN_VARIANT (e2);
7568 expr.value
7569 = comptypes (e1, e2) ? integer_one_node : integer_zero_node;
7571 break;
7572 case RID_BUILTIN_COMPLEX:
7574 vec<c_expr_t, va_gc> *cexpr_list;
7575 c_expr_t *e1_p, *e2_p;
7577 c_parser_consume_token (parser);
7578 if (!c_parser_get_builtin_args (parser,
7579 "__builtin_complex",
7580 &cexpr_list, false))
7582 expr.value = error_mark_node;
7583 break;
7586 if (vec_safe_length (cexpr_list) != 2)
7588 error_at (loc, "wrong number of arguments to "
7589 "%<__builtin_complex%>");
7590 expr.value = error_mark_node;
7591 break;
7594 e1_p = &(*cexpr_list)[0];
7595 e2_p = &(*cexpr_list)[1];
7597 *e1_p = convert_lvalue_to_rvalue (loc, *e1_p, true, true);
7598 if (TREE_CODE (e1_p->value) == EXCESS_PRECISION_EXPR)
7599 e1_p->value = convert (TREE_TYPE (e1_p->value),
7600 TREE_OPERAND (e1_p->value, 0));
7601 *e2_p = convert_lvalue_to_rvalue (loc, *e2_p, true, true);
7602 if (TREE_CODE (e2_p->value) == EXCESS_PRECISION_EXPR)
7603 e2_p->value = convert (TREE_TYPE (e2_p->value),
7604 TREE_OPERAND (e2_p->value, 0));
7605 if (!SCALAR_FLOAT_TYPE_P (TREE_TYPE (e1_p->value))
7606 || DECIMAL_FLOAT_TYPE_P (TREE_TYPE (e1_p->value))
7607 || !SCALAR_FLOAT_TYPE_P (TREE_TYPE (e2_p->value))
7608 || DECIMAL_FLOAT_TYPE_P (TREE_TYPE (e2_p->value)))
7610 error_at (loc, "%<__builtin_complex%> operand "
7611 "not of real binary floating-point type");
7612 expr.value = error_mark_node;
7613 break;
7615 if (TYPE_MAIN_VARIANT (TREE_TYPE (e1_p->value))
7616 != TYPE_MAIN_VARIANT (TREE_TYPE (e2_p->value)))
7618 error_at (loc,
7619 "%<__builtin_complex%> operands of different types");
7620 expr.value = error_mark_node;
7621 break;
7623 if (!flag_isoc99)
7624 pedwarn (loc, OPT_Wpedantic,
7625 "ISO C90 does not support complex types");
7626 expr.value = build2 (COMPLEX_EXPR,
7627 build_complex_type
7628 (TYPE_MAIN_VARIANT
7629 (TREE_TYPE (e1_p->value))),
7630 e1_p->value, e2_p->value);
7631 break;
7633 case RID_BUILTIN_SHUFFLE:
7635 vec<c_expr_t, va_gc> *cexpr_list;
7636 unsigned int i;
7637 c_expr_t *p;
7639 c_parser_consume_token (parser);
7640 if (!c_parser_get_builtin_args (parser,
7641 "__builtin_shuffle",
7642 &cexpr_list, false))
7644 expr.value = error_mark_node;
7645 break;
7648 FOR_EACH_VEC_SAFE_ELT (cexpr_list, i, p)
7649 *p = convert_lvalue_to_rvalue (loc, *p, true, true);
7651 if (vec_safe_length (cexpr_list) == 2)
7652 expr.value =
7653 c_build_vec_perm_expr
7654 (loc, (*cexpr_list)[0].value,
7655 NULL_TREE, (*cexpr_list)[1].value);
7657 else if (vec_safe_length (cexpr_list) == 3)
7658 expr.value =
7659 c_build_vec_perm_expr
7660 (loc, (*cexpr_list)[0].value,
7661 (*cexpr_list)[1].value,
7662 (*cexpr_list)[2].value);
7663 else
7665 error_at (loc, "wrong number of arguments to "
7666 "%<__builtin_shuffle%>");
7667 expr.value = error_mark_node;
7669 break;
7671 case RID_AT_SELECTOR:
7672 gcc_assert (c_dialect_objc ());
7673 c_parser_consume_token (parser);
7674 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7676 expr.value = error_mark_node;
7677 break;
7680 tree sel = c_parser_objc_selector_arg (parser);
7681 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7682 "expected %<)%>");
7683 expr.value = objc_build_selector_expr (loc, sel);
7685 break;
7686 case RID_AT_PROTOCOL:
7687 gcc_assert (c_dialect_objc ());
7688 c_parser_consume_token (parser);
7689 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7691 expr.value = error_mark_node;
7692 break;
7694 if (c_parser_next_token_is_not (parser, CPP_NAME))
7696 c_parser_error (parser, "expected identifier");
7697 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7698 expr.value = error_mark_node;
7699 break;
7702 tree id = c_parser_peek_token (parser)->value;
7703 c_parser_consume_token (parser);
7704 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7705 "expected %<)%>");
7706 expr.value = objc_build_protocol_expr (id);
7708 break;
7709 case RID_AT_ENCODE:
7710 /* Extension to support C-structures in the archiver. */
7711 gcc_assert (c_dialect_objc ());
7712 c_parser_consume_token (parser);
7713 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7715 expr.value = error_mark_node;
7716 break;
7718 t1 = c_parser_type_name (parser);
7719 if (t1 == NULL)
7721 expr.value = error_mark_node;
7722 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7723 break;
7725 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7726 "expected %<)%>");
7728 tree type = groktypename (t1, NULL, NULL);
7729 expr.value = objc_build_encode_expr (type);
7731 break;
7732 case RID_GENERIC:
7733 expr = c_parser_generic_selection (parser);
7734 break;
7735 case RID_CILK_SPAWN:
7736 c_parser_consume_token (parser);
7737 if (!flag_cilkplus)
7739 error_at (loc, "-fcilkplus must be enabled to use "
7740 "%<_Cilk_spawn%>");
7741 expr = c_parser_postfix_expression (parser);
7742 expr.value = error_mark_node;
7744 else if (c_parser_peek_token (parser)->keyword == RID_CILK_SPAWN)
7746 error_at (loc, "consecutive %<_Cilk_spawn%> keywords "
7747 "are not permitted");
7748 /* Now flush out all the _Cilk_spawns. */
7749 while (c_parser_peek_token (parser)->keyword == RID_CILK_SPAWN)
7750 c_parser_consume_token (parser);
7751 expr = c_parser_postfix_expression (parser);
7753 else
7755 expr = c_parser_postfix_expression (parser);
7756 expr.value = build_cilk_spawn (loc, expr.value);
7758 break;
7759 default:
7760 c_parser_error (parser, "expected expression");
7761 expr.value = error_mark_node;
7762 break;
7764 break;
7765 case CPP_OPEN_SQUARE:
7766 if (c_dialect_objc ())
7768 tree receiver, args;
7769 c_parser_consume_token (parser);
7770 receiver = c_parser_objc_receiver (parser);
7771 args = c_parser_objc_message_args (parser);
7772 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
7773 "expected %<]%>");
7774 expr.value = objc_build_message_expr (receiver, args);
7775 break;
7777 /* Else fall through to report error. */
7778 default:
7779 c_parser_error (parser, "expected expression");
7780 expr.value = error_mark_node;
7781 break;
7783 return c_parser_postfix_expression_after_primary (parser, loc, expr);
7786 /* Parse a postfix expression after a parenthesized type name: the
7787 brace-enclosed initializer of a compound literal, possibly followed
7788 by some postfix operators. This is separate because it is not
7789 possible to tell until after the type name whether a cast
7790 expression has a cast or a compound literal, or whether the operand
7791 of sizeof is a parenthesized type name or starts with a compound
7792 literal. TYPE_LOC is the location where TYPE_NAME starts--the
7793 location of the first token after the parentheses around the type
7794 name. */
7796 static struct c_expr
7797 c_parser_postfix_expression_after_paren_type (c_parser *parser,
7798 struct c_type_name *type_name,
7799 location_t type_loc)
7801 tree type;
7802 struct c_expr init;
7803 bool non_const;
7804 struct c_expr expr;
7805 location_t start_loc;
7806 tree type_expr = NULL_TREE;
7807 bool type_expr_const = true;
7808 check_compound_literal_type (type_loc, type_name);
7809 start_init (NULL_TREE, NULL, 0);
7810 type = groktypename (type_name, &type_expr, &type_expr_const);
7811 start_loc = c_parser_peek_token (parser)->location;
7812 if (type != error_mark_node && C_TYPE_VARIABLE_SIZE (type))
7814 error_at (type_loc, "compound literal has variable size");
7815 type = error_mark_node;
7817 init = c_parser_braced_init (parser, type, false);
7818 finish_init ();
7819 maybe_warn_string_init (type, init);
7821 if (type != error_mark_node
7822 && !ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (type))
7823 && current_function_decl)
7825 error ("compound literal qualified by address-space qualifier");
7826 type = error_mark_node;
7829 if (!flag_isoc99)
7830 pedwarn (start_loc, OPT_Wpedantic, "ISO C90 forbids compound literals");
7831 non_const = ((init.value && TREE_CODE (init.value) == CONSTRUCTOR)
7832 ? CONSTRUCTOR_NON_CONST (init.value)
7833 : init.original_code == C_MAYBE_CONST_EXPR);
7834 non_const |= !type_expr_const;
7835 expr.value = build_compound_literal (start_loc, type, init.value, non_const);
7836 expr.original_code = ERROR_MARK;
7837 expr.original_type = NULL;
7838 if (type_expr)
7840 if (TREE_CODE (expr.value) == C_MAYBE_CONST_EXPR)
7842 gcc_assert (C_MAYBE_CONST_EXPR_PRE (expr.value) == NULL_TREE);
7843 C_MAYBE_CONST_EXPR_PRE (expr.value) = type_expr;
7845 else
7847 gcc_assert (!non_const);
7848 expr.value = build2 (C_MAYBE_CONST_EXPR, type,
7849 type_expr, expr.value);
7852 return c_parser_postfix_expression_after_primary (parser, start_loc, expr);
7855 /* Callback function for sizeof_pointer_memaccess_warning to compare
7856 types. */
7858 static bool
7859 sizeof_ptr_memacc_comptypes (tree type1, tree type2)
7861 return comptypes (type1, type2) == 1;
7864 /* Parse a postfix expression after the initial primary or compound
7865 literal; that is, parse a series of postfix operators.
7867 EXPR_LOC is the location of the primary expression. */
7869 static struct c_expr
7870 c_parser_postfix_expression_after_primary (c_parser *parser,
7871 location_t expr_loc,
7872 struct c_expr expr)
7874 struct c_expr orig_expr;
7875 tree ident, idx;
7876 location_t sizeof_arg_loc[3];
7877 tree sizeof_arg[3];
7878 unsigned int i;
7879 vec<tree, va_gc> *exprlist;
7880 vec<tree, va_gc> *origtypes = NULL;
7881 vec<location_t> arg_loc = vNULL;
7883 while (true)
7885 location_t op_loc = c_parser_peek_token (parser)->location;
7886 switch (c_parser_peek_token (parser)->type)
7888 case CPP_OPEN_SQUARE:
7889 /* Array reference. */
7890 c_parser_consume_token (parser);
7891 if (flag_cilkplus
7892 && c_parser_peek_token (parser)->type == CPP_COLON)
7893 /* If we are here, then we have something like this:
7894 Array [ : ]
7896 expr.value = c_parser_array_notation (expr_loc, parser, NULL_TREE,
7897 expr.value);
7898 else
7900 idx = c_parser_expression (parser).value;
7901 /* Here we have 3 options:
7902 1. Array [EXPR] -- Normal Array call.
7903 2. Array [EXPR : EXPR] -- Array notation without stride.
7904 3. Array [EXPR : EXPR : EXPR] -- Array notation with stride.
7906 For 1, we just handle it just like a normal array expression.
7907 For 2 and 3 we handle it like we handle array notations. The
7908 idx value we have above becomes the initial/start index.
7910 if (flag_cilkplus
7911 && c_parser_peek_token (parser)->type == CPP_COLON)
7912 expr.value = c_parser_array_notation (expr_loc, parser, idx,
7913 expr.value);
7914 else
7916 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
7917 "expected %<]%>");
7918 expr.value = build_array_ref (op_loc, expr.value, idx);
7921 expr.original_code = ERROR_MARK;
7922 expr.original_type = NULL;
7923 break;
7924 case CPP_OPEN_PAREN:
7925 /* Function call. */
7926 c_parser_consume_token (parser);
7927 for (i = 0; i < 3; i++)
7929 sizeof_arg[i] = NULL_TREE;
7930 sizeof_arg_loc[i] = UNKNOWN_LOCATION;
7932 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
7933 exprlist = NULL;
7934 else
7935 exprlist = c_parser_expr_list (parser, true, false, &origtypes,
7936 sizeof_arg_loc, sizeof_arg,
7937 &arg_loc);
7938 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7939 "expected %<)%>");
7940 orig_expr = expr;
7941 mark_exp_read (expr.value);
7942 if (warn_sizeof_pointer_memaccess)
7943 sizeof_pointer_memaccess_warning (sizeof_arg_loc,
7944 expr.value, exprlist,
7945 sizeof_arg,
7946 sizeof_ptr_memacc_comptypes);
7947 expr.value
7948 = c_build_function_call_vec (expr_loc, arg_loc, expr.value,
7949 exprlist, origtypes);
7950 expr.original_code = ERROR_MARK;
7951 if (TREE_CODE (expr.value) == INTEGER_CST
7952 && TREE_CODE (orig_expr.value) == FUNCTION_DECL
7953 && DECL_BUILT_IN_CLASS (orig_expr.value) == BUILT_IN_NORMAL
7954 && DECL_FUNCTION_CODE (orig_expr.value) == BUILT_IN_CONSTANT_P)
7955 expr.original_code = C_MAYBE_CONST_EXPR;
7956 expr.original_type = NULL;
7957 if (exprlist)
7959 release_tree_vector (exprlist);
7960 release_tree_vector (origtypes);
7962 arg_loc.release ();
7963 break;
7964 case CPP_DOT:
7965 /* Structure element reference. */
7966 c_parser_consume_token (parser);
7967 expr = default_function_array_conversion (expr_loc, expr);
7968 if (c_parser_next_token_is (parser, CPP_NAME))
7969 ident = c_parser_peek_token (parser)->value;
7970 else
7972 c_parser_error (parser, "expected identifier");
7973 expr.value = error_mark_node;
7974 expr.original_code = ERROR_MARK;
7975 expr.original_type = NULL;
7976 return expr;
7978 c_parser_consume_token (parser);
7979 expr.value = build_component_ref (op_loc, expr.value, ident);
7980 expr.original_code = ERROR_MARK;
7981 if (TREE_CODE (expr.value) != COMPONENT_REF)
7982 expr.original_type = NULL;
7983 else
7985 /* Remember the original type of a bitfield. */
7986 tree field = TREE_OPERAND (expr.value, 1);
7987 if (TREE_CODE (field) != FIELD_DECL)
7988 expr.original_type = NULL;
7989 else
7990 expr.original_type = DECL_BIT_FIELD_TYPE (field);
7992 break;
7993 case CPP_DEREF:
7994 /* Structure element reference. */
7995 c_parser_consume_token (parser);
7996 expr = convert_lvalue_to_rvalue (expr_loc, expr, true, false);
7997 if (c_parser_next_token_is (parser, CPP_NAME))
7998 ident = c_parser_peek_token (parser)->value;
7999 else
8001 c_parser_error (parser, "expected identifier");
8002 expr.value = error_mark_node;
8003 expr.original_code = ERROR_MARK;
8004 expr.original_type = NULL;
8005 return expr;
8007 c_parser_consume_token (parser);
8008 expr.value = build_component_ref (op_loc,
8009 build_indirect_ref (op_loc,
8010 expr.value,
8011 RO_ARROW),
8012 ident);
8013 expr.original_code = ERROR_MARK;
8014 if (TREE_CODE (expr.value) != COMPONENT_REF)
8015 expr.original_type = NULL;
8016 else
8018 /* Remember the original type of a bitfield. */
8019 tree field = TREE_OPERAND (expr.value, 1);
8020 if (TREE_CODE (field) != FIELD_DECL)
8021 expr.original_type = NULL;
8022 else
8023 expr.original_type = DECL_BIT_FIELD_TYPE (field);
8025 break;
8026 case CPP_PLUS_PLUS:
8027 /* Postincrement. */
8028 c_parser_consume_token (parser);
8029 /* If the expressions have array notations, we expand them. */
8030 if (flag_cilkplus
8031 && TREE_CODE (expr.value) == ARRAY_NOTATION_REF)
8032 expr = fix_array_notation_expr (expr_loc, POSTINCREMENT_EXPR, expr);
8033 else
8035 expr = default_function_array_read_conversion (expr_loc, expr);
8036 expr.value = build_unary_op (op_loc,
8037 POSTINCREMENT_EXPR, expr.value, 0);
8039 expr.original_code = ERROR_MARK;
8040 expr.original_type = NULL;
8041 break;
8042 case CPP_MINUS_MINUS:
8043 /* Postdecrement. */
8044 c_parser_consume_token (parser);
8045 /* If the expressions have array notations, we expand them. */
8046 if (flag_cilkplus
8047 && TREE_CODE (expr.value) == ARRAY_NOTATION_REF)
8048 expr = fix_array_notation_expr (expr_loc, POSTDECREMENT_EXPR, expr);
8049 else
8051 expr = default_function_array_read_conversion (expr_loc, expr);
8052 expr.value = build_unary_op (op_loc,
8053 POSTDECREMENT_EXPR, expr.value, 0);
8055 expr.original_code = ERROR_MARK;
8056 expr.original_type = NULL;
8057 break;
8058 default:
8059 return expr;
8064 /* Parse an expression (C90 6.3.17, C99 6.5.17).
8066 expression:
8067 assignment-expression
8068 expression , assignment-expression
8071 static struct c_expr
8072 c_parser_expression (c_parser *parser)
8074 location_t tloc = c_parser_peek_token (parser)->location;
8075 struct c_expr expr;
8076 expr = c_parser_expr_no_commas (parser, NULL);
8077 if (c_parser_next_token_is (parser, CPP_COMMA))
8078 expr = convert_lvalue_to_rvalue (tloc, expr, true, false);
8079 while (c_parser_next_token_is (parser, CPP_COMMA))
8081 struct c_expr next;
8082 tree lhsval;
8083 location_t loc = c_parser_peek_token (parser)->location;
8084 location_t expr_loc;
8085 c_parser_consume_token (parser);
8086 expr_loc = c_parser_peek_token (parser)->location;
8087 lhsval = expr.value;
8088 while (TREE_CODE (lhsval) == COMPOUND_EXPR)
8089 lhsval = TREE_OPERAND (lhsval, 1);
8090 if (DECL_P (lhsval) || handled_component_p (lhsval))
8091 mark_exp_read (lhsval);
8092 next = c_parser_expr_no_commas (parser, NULL);
8093 next = convert_lvalue_to_rvalue (expr_loc, next, true, false);
8094 expr.value = build_compound_expr (loc, expr.value, next.value);
8095 expr.original_code = COMPOUND_EXPR;
8096 expr.original_type = next.original_type;
8098 return expr;
8101 /* Parse an expression and convert functions or arrays to pointers and
8102 lvalues to rvalues. */
8104 static struct c_expr
8105 c_parser_expression_conv (c_parser *parser)
8107 struct c_expr expr;
8108 location_t loc = c_parser_peek_token (parser)->location;
8109 expr = c_parser_expression (parser);
8110 expr = convert_lvalue_to_rvalue (loc, expr, true, false);
8111 return expr;
8114 /* Parse a non-empty list of expressions. If CONVERT_P, convert
8115 functions and arrays to pointers and lvalues to rvalues. If
8116 FOLD_P, fold the expressions. If LOCATIONS is non-NULL, save the
8117 locations of function arguments into this vector.
8119 nonempty-expr-list:
8120 assignment-expression
8121 nonempty-expr-list , assignment-expression
8124 static vec<tree, va_gc> *
8125 c_parser_expr_list (c_parser *parser, bool convert_p, bool fold_p,
8126 vec<tree, va_gc> **p_orig_types,
8127 location_t *sizeof_arg_loc, tree *sizeof_arg,
8128 vec<location_t> *locations)
8130 vec<tree, va_gc> *ret;
8131 vec<tree, va_gc> *orig_types;
8132 struct c_expr expr;
8133 location_t loc = c_parser_peek_token (parser)->location;
8134 location_t cur_sizeof_arg_loc = UNKNOWN_LOCATION;
8135 unsigned int idx = 0;
8137 ret = make_tree_vector ();
8138 if (p_orig_types == NULL)
8139 orig_types = NULL;
8140 else
8141 orig_types = make_tree_vector ();
8143 if (sizeof_arg != NULL
8144 && c_parser_next_token_is_keyword (parser, RID_SIZEOF))
8145 cur_sizeof_arg_loc = c_parser_peek_2nd_token (parser)->location;
8146 expr = c_parser_expr_no_commas (parser, NULL);
8147 if (convert_p)
8148 expr = convert_lvalue_to_rvalue (loc, expr, true, true);
8149 if (fold_p)
8150 expr.value = c_fully_fold (expr.value, false, NULL);
8151 ret->quick_push (expr.value);
8152 if (orig_types)
8153 orig_types->quick_push (expr.original_type);
8154 if (locations)
8155 locations->safe_push (loc);
8156 if (sizeof_arg != NULL
8157 && cur_sizeof_arg_loc != UNKNOWN_LOCATION
8158 && expr.original_code == SIZEOF_EXPR)
8160 sizeof_arg[0] = c_last_sizeof_arg;
8161 sizeof_arg_loc[0] = cur_sizeof_arg_loc;
8163 while (c_parser_next_token_is (parser, CPP_COMMA))
8165 c_parser_consume_token (parser);
8166 loc = c_parser_peek_token (parser)->location;
8167 if (sizeof_arg != NULL
8168 && c_parser_next_token_is_keyword (parser, RID_SIZEOF))
8169 cur_sizeof_arg_loc = c_parser_peek_2nd_token (parser)->location;
8170 else
8171 cur_sizeof_arg_loc = UNKNOWN_LOCATION;
8172 expr = c_parser_expr_no_commas (parser, NULL);
8173 if (convert_p)
8174 expr = convert_lvalue_to_rvalue (loc, expr, true, true);
8175 if (fold_p)
8176 expr.value = c_fully_fold (expr.value, false, NULL);
8177 vec_safe_push (ret, expr.value);
8178 if (orig_types)
8179 vec_safe_push (orig_types, expr.original_type);
8180 if (locations)
8181 locations->safe_push (loc);
8182 if (++idx < 3
8183 && sizeof_arg != NULL
8184 && cur_sizeof_arg_loc != UNKNOWN_LOCATION
8185 && expr.original_code == SIZEOF_EXPR)
8187 sizeof_arg[idx] = c_last_sizeof_arg;
8188 sizeof_arg_loc[idx] = cur_sizeof_arg_loc;
8191 if (orig_types)
8192 *p_orig_types = orig_types;
8193 return ret;
8196 /* Parse Objective-C-specific constructs. */
8198 /* Parse an objc-class-definition.
8200 objc-class-definition:
8201 @interface identifier objc-superclass[opt] objc-protocol-refs[opt]
8202 objc-class-instance-variables[opt] objc-methodprotolist @end
8203 @implementation identifier objc-superclass[opt]
8204 objc-class-instance-variables[opt]
8205 @interface identifier ( identifier ) objc-protocol-refs[opt]
8206 objc-methodprotolist @end
8207 @interface identifier ( ) objc-protocol-refs[opt]
8208 objc-methodprotolist @end
8209 @implementation identifier ( identifier )
8211 objc-superclass:
8212 : identifier
8214 "@interface identifier (" must start "@interface identifier (
8215 identifier ) ...": objc-methodprotolist in the first production may
8216 not start with a parenthesized identifier as a declarator of a data
8217 definition with no declaration specifiers if the objc-superclass,
8218 objc-protocol-refs and objc-class-instance-variables are omitted. */
8220 static void
8221 c_parser_objc_class_definition (c_parser *parser, tree attributes)
8223 bool iface_p;
8224 tree id1;
8225 tree superclass;
8226 if (c_parser_next_token_is_keyword (parser, RID_AT_INTERFACE))
8227 iface_p = true;
8228 else if (c_parser_next_token_is_keyword (parser, RID_AT_IMPLEMENTATION))
8229 iface_p = false;
8230 else
8231 gcc_unreachable ();
8233 c_parser_consume_token (parser);
8234 if (c_parser_next_token_is_not (parser, CPP_NAME))
8236 c_parser_error (parser, "expected identifier");
8237 return;
8239 id1 = c_parser_peek_token (parser)->value;
8240 c_parser_consume_token (parser);
8241 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
8243 /* We have a category or class extension. */
8244 tree id2;
8245 tree proto = NULL_TREE;
8246 c_parser_consume_token (parser);
8247 if (c_parser_next_token_is_not (parser, CPP_NAME))
8249 if (iface_p && c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
8251 /* We have a class extension. */
8252 id2 = NULL_TREE;
8254 else
8256 c_parser_error (parser, "expected identifier or %<)%>");
8257 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
8258 return;
8261 else
8263 id2 = c_parser_peek_token (parser)->value;
8264 c_parser_consume_token (parser);
8266 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8267 if (!iface_p)
8269 objc_start_category_implementation (id1, id2);
8270 return;
8272 if (c_parser_next_token_is (parser, CPP_LESS))
8273 proto = c_parser_objc_protocol_refs (parser);
8274 objc_start_category_interface (id1, id2, proto, attributes);
8275 c_parser_objc_methodprotolist (parser);
8276 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
8277 objc_finish_interface ();
8278 return;
8280 if (c_parser_next_token_is (parser, CPP_COLON))
8282 c_parser_consume_token (parser);
8283 if (c_parser_next_token_is_not (parser, CPP_NAME))
8285 c_parser_error (parser, "expected identifier");
8286 return;
8288 superclass = c_parser_peek_token (parser)->value;
8289 c_parser_consume_token (parser);
8291 else
8292 superclass = NULL_TREE;
8293 if (iface_p)
8295 tree proto = NULL_TREE;
8296 if (c_parser_next_token_is (parser, CPP_LESS))
8297 proto = c_parser_objc_protocol_refs (parser);
8298 objc_start_class_interface (id1, superclass, proto, attributes);
8300 else
8301 objc_start_class_implementation (id1, superclass);
8302 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
8303 c_parser_objc_class_instance_variables (parser);
8304 if (iface_p)
8306 objc_continue_interface ();
8307 c_parser_objc_methodprotolist (parser);
8308 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
8309 objc_finish_interface ();
8311 else
8313 objc_continue_implementation ();
8314 return;
8318 /* Parse objc-class-instance-variables.
8320 objc-class-instance-variables:
8321 { objc-instance-variable-decl-list[opt] }
8323 objc-instance-variable-decl-list:
8324 objc-visibility-spec
8325 objc-instance-variable-decl ;
8327 objc-instance-variable-decl-list objc-visibility-spec
8328 objc-instance-variable-decl-list objc-instance-variable-decl ;
8329 objc-instance-variable-decl-list ;
8331 objc-visibility-spec:
8332 @private
8333 @protected
8334 @public
8336 objc-instance-variable-decl:
8337 struct-declaration
8340 static void
8341 c_parser_objc_class_instance_variables (c_parser *parser)
8343 gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
8344 c_parser_consume_token (parser);
8345 while (c_parser_next_token_is_not (parser, CPP_EOF))
8347 tree decls;
8348 /* Parse any stray semicolon. */
8349 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
8351 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
8352 "extra semicolon");
8353 c_parser_consume_token (parser);
8354 continue;
8356 /* Stop if at the end of the instance variables. */
8357 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
8359 c_parser_consume_token (parser);
8360 break;
8362 /* Parse any objc-visibility-spec. */
8363 if (c_parser_next_token_is_keyword (parser, RID_AT_PRIVATE))
8365 c_parser_consume_token (parser);
8366 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
8367 continue;
8369 else if (c_parser_next_token_is_keyword (parser, RID_AT_PROTECTED))
8371 c_parser_consume_token (parser);
8372 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
8373 continue;
8375 else if (c_parser_next_token_is_keyword (parser, RID_AT_PUBLIC))
8377 c_parser_consume_token (parser);
8378 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
8379 continue;
8381 else if (c_parser_next_token_is_keyword (parser, RID_AT_PACKAGE))
8383 c_parser_consume_token (parser);
8384 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
8385 continue;
8387 else if (c_parser_next_token_is (parser, CPP_PRAGMA))
8389 c_parser_pragma (parser, pragma_external);
8390 continue;
8393 /* Parse some comma-separated declarations. */
8394 decls = c_parser_struct_declaration (parser);
8395 if (decls == NULL)
8397 /* There is a syntax error. We want to skip the offending
8398 tokens up to the next ';' (included) or '}'
8399 (excluded). */
8401 /* First, skip manually a ')' or ']'. This is because they
8402 reduce the nesting level, so c_parser_skip_until_found()
8403 wouldn't be able to skip past them. */
8404 c_token *token = c_parser_peek_token (parser);
8405 if (token->type == CPP_CLOSE_PAREN || token->type == CPP_CLOSE_SQUARE)
8406 c_parser_consume_token (parser);
8408 /* Then, do the standard skipping. */
8409 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
8411 /* We hopefully recovered. Start normal parsing again. */
8412 parser->error = false;
8413 continue;
8415 else
8417 /* Comma-separated instance variables are chained together
8418 in reverse order; add them one by one. */
8419 tree ivar = nreverse (decls);
8420 for (; ivar; ivar = DECL_CHAIN (ivar))
8421 objc_add_instance_variable (copy_node (ivar));
8423 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8427 /* Parse an objc-class-declaration.
8429 objc-class-declaration:
8430 @class identifier-list ;
8433 static void
8434 c_parser_objc_class_declaration (c_parser *parser)
8436 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_CLASS));
8437 c_parser_consume_token (parser);
8438 /* Any identifiers, including those declared as type names, are OK
8439 here. */
8440 while (true)
8442 tree id;
8443 if (c_parser_next_token_is_not (parser, CPP_NAME))
8445 c_parser_error (parser, "expected identifier");
8446 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
8447 parser->error = false;
8448 return;
8450 id = c_parser_peek_token (parser)->value;
8451 objc_declare_class (id);
8452 c_parser_consume_token (parser);
8453 if (c_parser_next_token_is (parser, CPP_COMMA))
8454 c_parser_consume_token (parser);
8455 else
8456 break;
8458 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8461 /* Parse an objc-alias-declaration.
8463 objc-alias-declaration:
8464 @compatibility_alias identifier identifier ;
8467 static void
8468 c_parser_objc_alias_declaration (c_parser *parser)
8470 tree id1, id2;
8471 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_ALIAS));
8472 c_parser_consume_token (parser);
8473 if (c_parser_next_token_is_not (parser, CPP_NAME))
8475 c_parser_error (parser, "expected identifier");
8476 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
8477 return;
8479 id1 = c_parser_peek_token (parser)->value;
8480 c_parser_consume_token (parser);
8481 if (c_parser_next_token_is_not (parser, CPP_NAME))
8483 c_parser_error (parser, "expected identifier");
8484 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
8485 return;
8487 id2 = c_parser_peek_token (parser)->value;
8488 c_parser_consume_token (parser);
8489 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8490 objc_declare_alias (id1, id2);
8493 /* Parse an objc-protocol-definition.
8495 objc-protocol-definition:
8496 @protocol identifier objc-protocol-refs[opt] objc-methodprotolist @end
8497 @protocol identifier-list ;
8499 "@protocol identifier ;" should be resolved as "@protocol
8500 identifier-list ;": objc-methodprotolist may not start with a
8501 semicolon in the first alternative if objc-protocol-refs are
8502 omitted. */
8504 static void
8505 c_parser_objc_protocol_definition (c_parser *parser, tree attributes)
8507 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_PROTOCOL));
8509 c_parser_consume_token (parser);
8510 if (c_parser_next_token_is_not (parser, CPP_NAME))
8512 c_parser_error (parser, "expected identifier");
8513 return;
8515 if (c_parser_peek_2nd_token (parser)->type == CPP_COMMA
8516 || c_parser_peek_2nd_token (parser)->type == CPP_SEMICOLON)
8518 /* Any identifiers, including those declared as type names, are
8519 OK here. */
8520 while (true)
8522 tree id;
8523 if (c_parser_next_token_is_not (parser, CPP_NAME))
8525 c_parser_error (parser, "expected identifier");
8526 break;
8528 id = c_parser_peek_token (parser)->value;
8529 objc_declare_protocol (id, attributes);
8530 c_parser_consume_token (parser);
8531 if (c_parser_next_token_is (parser, CPP_COMMA))
8532 c_parser_consume_token (parser);
8533 else
8534 break;
8536 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8538 else
8540 tree id = c_parser_peek_token (parser)->value;
8541 tree proto = NULL_TREE;
8542 c_parser_consume_token (parser);
8543 if (c_parser_next_token_is (parser, CPP_LESS))
8544 proto = c_parser_objc_protocol_refs (parser);
8545 parser->objc_pq_context = true;
8546 objc_start_protocol (id, proto, attributes);
8547 c_parser_objc_methodprotolist (parser);
8548 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
8549 parser->objc_pq_context = false;
8550 objc_finish_interface ();
8554 /* Parse an objc-method-type.
8556 objc-method-type:
8560 Return true if it is a class method (+) and false if it is
8561 an instance method (-).
8563 static inline bool
8564 c_parser_objc_method_type (c_parser *parser)
8566 switch (c_parser_peek_token (parser)->type)
8568 case CPP_PLUS:
8569 c_parser_consume_token (parser);
8570 return true;
8571 case CPP_MINUS:
8572 c_parser_consume_token (parser);
8573 return false;
8574 default:
8575 gcc_unreachable ();
8579 /* Parse an objc-method-definition.
8581 objc-method-definition:
8582 objc-method-type objc-method-decl ;[opt] compound-statement
8585 static void
8586 c_parser_objc_method_definition (c_parser *parser)
8588 bool is_class_method = c_parser_objc_method_type (parser);
8589 tree decl, attributes = NULL_TREE, expr = NULL_TREE;
8590 parser->objc_pq_context = true;
8591 decl = c_parser_objc_method_decl (parser, is_class_method, &attributes,
8592 &expr);
8593 if (decl == error_mark_node)
8594 return; /* Bail here. */
8596 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
8598 c_parser_consume_token (parser);
8599 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
8600 "extra semicolon in method definition specified");
8603 if (!c_parser_next_token_is (parser, CPP_OPEN_BRACE))
8605 c_parser_error (parser, "expected %<{%>");
8606 return;
8609 parser->objc_pq_context = false;
8610 if (objc_start_method_definition (is_class_method, decl, attributes, expr))
8612 add_stmt (c_parser_compound_statement (parser));
8613 objc_finish_method_definition (current_function_decl);
8615 else
8617 /* This code is executed when we find a method definition
8618 outside of an @implementation context (or invalid for other
8619 reasons). Parse the method (to keep going) but do not emit
8620 any code.
8622 c_parser_compound_statement (parser);
8626 /* Parse an objc-methodprotolist.
8628 objc-methodprotolist:
8629 empty
8630 objc-methodprotolist objc-methodproto
8631 objc-methodprotolist declaration
8632 objc-methodprotolist ;
8633 @optional
8634 @required
8636 The declaration is a data definition, which may be missing
8637 declaration specifiers under the same rules and diagnostics as
8638 other data definitions outside functions, and the stray semicolon
8639 is diagnosed the same way as a stray semicolon outside a
8640 function. */
8642 static void
8643 c_parser_objc_methodprotolist (c_parser *parser)
8645 while (true)
8647 /* The list is terminated by @end. */
8648 switch (c_parser_peek_token (parser)->type)
8650 case CPP_SEMICOLON:
8651 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
8652 "ISO C does not allow extra %<;%> outside of a function");
8653 c_parser_consume_token (parser);
8654 break;
8655 case CPP_PLUS:
8656 case CPP_MINUS:
8657 c_parser_objc_methodproto (parser);
8658 break;
8659 case CPP_PRAGMA:
8660 c_parser_pragma (parser, pragma_external);
8661 break;
8662 case CPP_EOF:
8663 return;
8664 default:
8665 if (c_parser_next_token_is_keyword (parser, RID_AT_END))
8666 return;
8667 else if (c_parser_next_token_is_keyword (parser, RID_AT_PROPERTY))
8668 c_parser_objc_at_property_declaration (parser);
8669 else if (c_parser_next_token_is_keyword (parser, RID_AT_OPTIONAL))
8671 objc_set_method_opt (true);
8672 c_parser_consume_token (parser);
8674 else if (c_parser_next_token_is_keyword (parser, RID_AT_REQUIRED))
8676 objc_set_method_opt (false);
8677 c_parser_consume_token (parser);
8679 else
8680 c_parser_declaration_or_fndef (parser, false, false, true,
8681 false, true, NULL, vNULL);
8682 break;
8687 /* Parse an objc-methodproto.
8689 objc-methodproto:
8690 objc-method-type objc-method-decl ;
8693 static void
8694 c_parser_objc_methodproto (c_parser *parser)
8696 bool is_class_method = c_parser_objc_method_type (parser);
8697 tree decl, attributes = NULL_TREE;
8699 /* Remember protocol qualifiers in prototypes. */
8700 parser->objc_pq_context = true;
8701 decl = c_parser_objc_method_decl (parser, is_class_method, &attributes,
8702 NULL);
8703 /* Forget protocol qualifiers now. */
8704 parser->objc_pq_context = false;
8706 /* Do not allow the presence of attributes to hide an erroneous
8707 method implementation in the interface section. */
8708 if (!c_parser_next_token_is (parser, CPP_SEMICOLON))
8710 c_parser_error (parser, "expected %<;%>");
8711 return;
8714 if (decl != error_mark_node)
8715 objc_add_method_declaration (is_class_method, decl, attributes);
8717 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8720 /* If we are at a position that method attributes may be present, check that
8721 there are not any parsed already (a syntax error) and then collect any
8722 specified at the current location. Finally, if new attributes were present,
8723 check that the next token is legal ( ';' for decls and '{' for defs). */
8725 static bool
8726 c_parser_objc_maybe_method_attributes (c_parser* parser, tree* attributes)
8728 bool bad = false;
8729 if (*attributes)
8731 c_parser_error (parser,
8732 "method attributes must be specified at the end only");
8733 *attributes = NULL_TREE;
8734 bad = true;
8737 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
8738 *attributes = c_parser_attributes (parser);
8740 /* If there were no attributes here, just report any earlier error. */
8741 if (*attributes == NULL_TREE || bad)
8742 return bad;
8744 /* If the attributes are followed by a ; or {, then just report any earlier
8745 error. */
8746 if (c_parser_next_token_is (parser, CPP_SEMICOLON)
8747 || c_parser_next_token_is (parser, CPP_OPEN_BRACE))
8748 return bad;
8750 /* We've got attributes, but not at the end. */
8751 c_parser_error (parser,
8752 "expected %<;%> or %<{%> after method attribute definition");
8753 return true;
8756 /* Parse an objc-method-decl.
8758 objc-method-decl:
8759 ( objc-type-name ) objc-selector
8760 objc-selector
8761 ( objc-type-name ) objc-keyword-selector objc-optparmlist
8762 objc-keyword-selector objc-optparmlist
8763 attributes
8765 objc-keyword-selector:
8766 objc-keyword-decl
8767 objc-keyword-selector objc-keyword-decl
8769 objc-keyword-decl:
8770 objc-selector : ( objc-type-name ) identifier
8771 objc-selector : identifier
8772 : ( objc-type-name ) identifier
8773 : identifier
8775 objc-optparmlist:
8776 objc-optparms objc-optellipsis
8778 objc-optparms:
8779 empty
8780 objc-opt-parms , parameter-declaration
8782 objc-optellipsis:
8783 empty
8784 , ...
8787 static tree
8788 c_parser_objc_method_decl (c_parser *parser, bool is_class_method,
8789 tree *attributes, tree *expr)
8791 tree type = NULL_TREE;
8792 tree sel;
8793 tree parms = NULL_TREE;
8794 bool ellipsis = false;
8795 bool attr_err = false;
8797 *attributes = NULL_TREE;
8798 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
8800 c_parser_consume_token (parser);
8801 type = c_parser_objc_type_name (parser);
8802 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8804 sel = c_parser_objc_selector (parser);
8805 /* If there is no selector, or a colon follows, we have an
8806 objc-keyword-selector. If there is a selector, and a colon does
8807 not follow, that selector ends the objc-method-decl. */
8808 if (!sel || c_parser_next_token_is (parser, CPP_COLON))
8810 tree tsel = sel;
8811 tree list = NULL_TREE;
8812 while (true)
8814 tree atype = NULL_TREE, id, keyworddecl;
8815 tree param_attr = NULL_TREE;
8816 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
8817 break;
8818 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
8820 c_parser_consume_token (parser);
8821 atype = c_parser_objc_type_name (parser);
8822 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
8823 "expected %<)%>");
8825 /* New ObjC allows attributes on method parameters. */
8826 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
8827 param_attr = c_parser_attributes (parser);
8828 if (c_parser_next_token_is_not (parser, CPP_NAME))
8830 c_parser_error (parser, "expected identifier");
8831 return error_mark_node;
8833 id = c_parser_peek_token (parser)->value;
8834 c_parser_consume_token (parser);
8835 keyworddecl = objc_build_keyword_decl (tsel, atype, id, param_attr);
8836 list = chainon (list, keyworddecl);
8837 tsel = c_parser_objc_selector (parser);
8838 if (!tsel && c_parser_next_token_is_not (parser, CPP_COLON))
8839 break;
8842 attr_err |= c_parser_objc_maybe_method_attributes (parser, attributes) ;
8844 /* Parse the optional parameter list. Optional Objective-C
8845 method parameters follow the C syntax, and may include '...'
8846 to denote a variable number of arguments. */
8847 parms = make_node (TREE_LIST);
8848 while (c_parser_next_token_is (parser, CPP_COMMA))
8850 struct c_parm *parm;
8851 c_parser_consume_token (parser);
8852 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
8854 ellipsis = true;
8855 c_parser_consume_token (parser);
8856 attr_err |= c_parser_objc_maybe_method_attributes
8857 (parser, attributes) ;
8858 break;
8860 parm = c_parser_parameter_declaration (parser, NULL_TREE);
8861 if (parm == NULL)
8862 break;
8863 parms = chainon (parms,
8864 build_tree_list (NULL_TREE, grokparm (parm, expr)));
8866 sel = list;
8868 else
8869 attr_err |= c_parser_objc_maybe_method_attributes (parser, attributes) ;
8871 if (sel == NULL)
8873 c_parser_error (parser, "objective-c method declaration is expected");
8874 return error_mark_node;
8877 if (attr_err)
8878 return error_mark_node;
8880 return objc_build_method_signature (is_class_method, type, sel, parms, ellipsis);
8883 /* Parse an objc-type-name.
8885 objc-type-name:
8886 objc-type-qualifiers[opt] type-name
8887 objc-type-qualifiers[opt]
8889 objc-type-qualifiers:
8890 objc-type-qualifier
8891 objc-type-qualifiers objc-type-qualifier
8893 objc-type-qualifier: one of
8894 in out inout bycopy byref oneway
8897 static tree
8898 c_parser_objc_type_name (c_parser *parser)
8900 tree quals = NULL_TREE;
8901 struct c_type_name *type_name = NULL;
8902 tree type = NULL_TREE;
8903 while (true)
8905 c_token *token = c_parser_peek_token (parser);
8906 if (token->type == CPP_KEYWORD
8907 && (token->keyword == RID_IN
8908 || token->keyword == RID_OUT
8909 || token->keyword == RID_INOUT
8910 || token->keyword == RID_BYCOPY
8911 || token->keyword == RID_BYREF
8912 || token->keyword == RID_ONEWAY))
8914 quals = chainon (build_tree_list (NULL_TREE, token->value), quals);
8915 c_parser_consume_token (parser);
8917 else
8918 break;
8920 if (c_parser_next_tokens_start_typename (parser, cla_prefer_type))
8921 type_name = c_parser_type_name (parser);
8922 if (type_name)
8923 type = groktypename (type_name, NULL, NULL);
8925 /* If the type is unknown, and error has already been produced and
8926 we need to recover from the error. In that case, use NULL_TREE
8927 for the type, as if no type had been specified; this will use the
8928 default type ('id') which is good for error recovery. */
8929 if (type == error_mark_node)
8930 type = NULL_TREE;
8932 return build_tree_list (quals, type);
8935 /* Parse objc-protocol-refs.
8937 objc-protocol-refs:
8938 < identifier-list >
8941 static tree
8942 c_parser_objc_protocol_refs (c_parser *parser)
8944 tree list = NULL_TREE;
8945 gcc_assert (c_parser_next_token_is (parser, CPP_LESS));
8946 c_parser_consume_token (parser);
8947 /* Any identifiers, including those declared as type names, are OK
8948 here. */
8949 while (true)
8951 tree id;
8952 if (c_parser_next_token_is_not (parser, CPP_NAME))
8954 c_parser_error (parser, "expected identifier");
8955 break;
8957 id = c_parser_peek_token (parser)->value;
8958 list = chainon (list, build_tree_list (NULL_TREE, id));
8959 c_parser_consume_token (parser);
8960 if (c_parser_next_token_is (parser, CPP_COMMA))
8961 c_parser_consume_token (parser);
8962 else
8963 break;
8965 c_parser_require (parser, CPP_GREATER, "expected %<>%>");
8966 return list;
8969 /* Parse an objc-try-catch-finally-statement.
8971 objc-try-catch-finally-statement:
8972 @try compound-statement objc-catch-list[opt]
8973 @try compound-statement objc-catch-list[opt] @finally compound-statement
8975 objc-catch-list:
8976 @catch ( objc-catch-parameter-declaration ) compound-statement
8977 objc-catch-list @catch ( objc-catch-parameter-declaration ) compound-statement
8979 objc-catch-parameter-declaration:
8980 parameter-declaration
8981 '...'
8983 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
8985 PS: This function is identical to cp_parser_objc_try_catch_finally_statement
8986 for C++. Keep them in sync. */
8988 static void
8989 c_parser_objc_try_catch_finally_statement (c_parser *parser)
8991 location_t location;
8992 tree stmt;
8994 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_TRY));
8995 c_parser_consume_token (parser);
8996 location = c_parser_peek_token (parser)->location;
8997 objc_maybe_warn_exceptions (location);
8998 stmt = c_parser_compound_statement (parser);
8999 objc_begin_try_stmt (location, stmt);
9001 while (c_parser_next_token_is_keyword (parser, RID_AT_CATCH))
9003 struct c_parm *parm;
9004 tree parameter_declaration = error_mark_node;
9005 bool seen_open_paren = false;
9007 c_parser_consume_token (parser);
9008 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
9009 seen_open_paren = true;
9010 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
9012 /* We have "@catch (...)" (where the '...' are literally
9013 what is in the code). Skip the '...'.
9014 parameter_declaration is set to NULL_TREE, and
9015 objc_being_catch_clauses() knows that that means
9016 '...'. */
9017 c_parser_consume_token (parser);
9018 parameter_declaration = NULL_TREE;
9020 else
9022 /* We have "@catch (NSException *exception)" or something
9023 like that. Parse the parameter declaration. */
9024 parm = c_parser_parameter_declaration (parser, NULL_TREE);
9025 if (parm == NULL)
9026 parameter_declaration = error_mark_node;
9027 else
9028 parameter_declaration = grokparm (parm, NULL);
9030 if (seen_open_paren)
9031 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
9032 else
9034 /* If there was no open parenthesis, we are recovering from
9035 an error, and we are trying to figure out what mistake
9036 the user has made. */
9038 /* If there is an immediate closing parenthesis, the user
9039 probably forgot the opening one (ie, they typed "@catch
9040 NSException *e)". Parse the closing parenthesis and keep
9041 going. */
9042 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
9043 c_parser_consume_token (parser);
9045 /* If these is no immediate closing parenthesis, the user
9046 probably doesn't know that parenthesis are required at
9047 all (ie, they typed "@catch NSException *e"). So, just
9048 forget about the closing parenthesis and keep going. */
9050 objc_begin_catch_clause (parameter_declaration);
9051 if (c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
9052 c_parser_compound_statement_nostart (parser);
9053 objc_finish_catch_clause ();
9055 if (c_parser_next_token_is_keyword (parser, RID_AT_FINALLY))
9057 c_parser_consume_token (parser);
9058 location = c_parser_peek_token (parser)->location;
9059 stmt = c_parser_compound_statement (parser);
9060 objc_build_finally_clause (location, stmt);
9062 objc_finish_try_stmt ();
9065 /* Parse an objc-synchronized-statement.
9067 objc-synchronized-statement:
9068 @synchronized ( expression ) compound-statement
9071 static void
9072 c_parser_objc_synchronized_statement (c_parser *parser)
9074 location_t loc;
9075 tree expr, stmt;
9076 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_SYNCHRONIZED));
9077 c_parser_consume_token (parser);
9078 loc = c_parser_peek_token (parser)->location;
9079 objc_maybe_warn_exceptions (loc);
9080 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
9082 struct c_expr ce = c_parser_expression (parser);
9083 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
9084 expr = ce.value;
9085 expr = c_fully_fold (expr, false, NULL);
9086 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
9088 else
9089 expr = error_mark_node;
9090 stmt = c_parser_compound_statement (parser);
9091 objc_build_synchronized (loc, expr, stmt);
9094 /* Parse an objc-selector; return NULL_TREE without an error if the
9095 next token is not an objc-selector.
9097 objc-selector:
9098 identifier
9099 one of
9100 enum struct union if else while do for switch case default
9101 break continue return goto asm sizeof typeof __alignof
9102 unsigned long const short volatile signed restrict _Complex
9103 in out inout bycopy byref oneway int char float double void _Bool
9104 _Atomic
9106 ??? Why this selection of keywords but not, for example, storage
9107 class specifiers? */
9109 static tree
9110 c_parser_objc_selector (c_parser *parser)
9112 c_token *token = c_parser_peek_token (parser);
9113 tree value = token->value;
9114 if (token->type == CPP_NAME)
9116 c_parser_consume_token (parser);
9117 return value;
9119 if (token->type != CPP_KEYWORD)
9120 return NULL_TREE;
9121 switch (token->keyword)
9123 case RID_ENUM:
9124 case RID_STRUCT:
9125 case RID_UNION:
9126 case RID_IF:
9127 case RID_ELSE:
9128 case RID_WHILE:
9129 case RID_DO:
9130 case RID_FOR:
9131 case RID_SWITCH:
9132 case RID_CASE:
9133 case RID_DEFAULT:
9134 case RID_BREAK:
9135 case RID_CONTINUE:
9136 case RID_RETURN:
9137 case RID_GOTO:
9138 case RID_ASM:
9139 case RID_SIZEOF:
9140 case RID_TYPEOF:
9141 case RID_ALIGNOF:
9142 case RID_UNSIGNED:
9143 case RID_LONG:
9144 case RID_INT128:
9145 case RID_CONST:
9146 case RID_SHORT:
9147 case RID_VOLATILE:
9148 case RID_SIGNED:
9149 case RID_RESTRICT:
9150 case RID_COMPLEX:
9151 case RID_IN:
9152 case RID_OUT:
9153 case RID_INOUT:
9154 case RID_BYCOPY:
9155 case RID_BYREF:
9156 case RID_ONEWAY:
9157 case RID_INT:
9158 case RID_CHAR:
9159 case RID_FLOAT:
9160 case RID_DOUBLE:
9161 case RID_VOID:
9162 case RID_BOOL:
9163 case RID_ATOMIC:
9164 case RID_AUTO_TYPE:
9165 c_parser_consume_token (parser);
9166 return value;
9167 default:
9168 return NULL_TREE;
9172 /* Parse an objc-selector-arg.
9174 objc-selector-arg:
9175 objc-selector
9176 objc-keywordname-list
9178 objc-keywordname-list:
9179 objc-keywordname
9180 objc-keywordname-list objc-keywordname
9182 objc-keywordname:
9183 objc-selector :
9187 static tree
9188 c_parser_objc_selector_arg (c_parser *parser)
9190 tree sel = c_parser_objc_selector (parser);
9191 tree list = NULL_TREE;
9192 if (sel && c_parser_next_token_is_not (parser, CPP_COLON))
9193 return sel;
9194 while (true)
9196 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
9197 return list;
9198 list = chainon (list, build_tree_list (sel, NULL_TREE));
9199 sel = c_parser_objc_selector (parser);
9200 if (!sel && c_parser_next_token_is_not (parser, CPP_COLON))
9201 break;
9203 return list;
9206 /* Parse an objc-receiver.
9208 objc-receiver:
9209 expression
9210 class-name
9211 type-name
9214 static tree
9215 c_parser_objc_receiver (c_parser *parser)
9217 location_t loc = c_parser_peek_token (parser)->location;
9219 if (c_parser_peek_token (parser)->type == CPP_NAME
9220 && (c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME
9221 || c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME))
9223 tree id = c_parser_peek_token (parser)->value;
9224 c_parser_consume_token (parser);
9225 return objc_get_class_reference (id);
9227 struct c_expr ce = c_parser_expression (parser);
9228 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
9229 return c_fully_fold (ce.value, false, NULL);
9232 /* Parse objc-message-args.
9234 objc-message-args:
9235 objc-selector
9236 objc-keywordarg-list
9238 objc-keywordarg-list:
9239 objc-keywordarg
9240 objc-keywordarg-list objc-keywordarg
9242 objc-keywordarg:
9243 objc-selector : objc-keywordexpr
9244 : objc-keywordexpr
9247 static tree
9248 c_parser_objc_message_args (c_parser *parser)
9250 tree sel = c_parser_objc_selector (parser);
9251 tree list = NULL_TREE;
9252 if (sel && c_parser_next_token_is_not (parser, CPP_COLON))
9253 return sel;
9254 while (true)
9256 tree keywordexpr;
9257 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
9258 return error_mark_node;
9259 keywordexpr = c_parser_objc_keywordexpr (parser);
9260 list = chainon (list, build_tree_list (sel, keywordexpr));
9261 sel = c_parser_objc_selector (parser);
9262 if (!sel && c_parser_next_token_is_not (parser, CPP_COLON))
9263 break;
9265 return list;
9268 /* Parse an objc-keywordexpr.
9270 objc-keywordexpr:
9271 nonempty-expr-list
9274 static tree
9275 c_parser_objc_keywordexpr (c_parser *parser)
9277 tree ret;
9278 vec<tree, va_gc> *expr_list = c_parser_expr_list (parser, true, true,
9279 NULL, NULL, NULL, NULL);
9280 if (vec_safe_length (expr_list) == 1)
9282 /* Just return the expression, remove a level of
9283 indirection. */
9284 ret = (*expr_list)[0];
9286 else
9288 /* We have a comma expression, we will collapse later. */
9289 ret = build_tree_list_vec (expr_list);
9291 release_tree_vector (expr_list);
9292 return ret;
9295 /* A check, needed in several places, that ObjC interface, implementation or
9296 method definitions are not prefixed by incorrect items. */
9297 static bool
9298 c_parser_objc_diagnose_bad_element_prefix (c_parser *parser,
9299 struct c_declspecs *specs)
9301 if (!specs->declspecs_seen_p || specs->non_sc_seen_p
9302 || specs->typespec_kind != ctsk_none)
9304 c_parser_error (parser,
9305 "no type or storage class may be specified here,");
9306 c_parser_skip_to_end_of_block_or_statement (parser);
9307 return true;
9309 return false;
9312 /* Parse an Objective-C @property declaration. The syntax is:
9314 objc-property-declaration:
9315 '@property' objc-property-attributes[opt] struct-declaration ;
9317 objc-property-attributes:
9318 '(' objc-property-attribute-list ')'
9320 objc-property-attribute-list:
9321 objc-property-attribute
9322 objc-property-attribute-list, objc-property-attribute
9324 objc-property-attribute
9325 'getter' = identifier
9326 'setter' = identifier
9327 'readonly'
9328 'readwrite'
9329 'assign'
9330 'retain'
9331 'copy'
9332 'nonatomic'
9334 For example:
9335 @property NSString *name;
9336 @property (readonly) id object;
9337 @property (retain, nonatomic, getter=getTheName) id name;
9338 @property int a, b, c;
9340 PS: This function is identical to cp_parser_objc_at_propery_declaration
9341 for C++. Keep them in sync. */
9342 static void
9343 c_parser_objc_at_property_declaration (c_parser *parser)
9345 /* The following variables hold the attributes of the properties as
9346 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
9347 seen. When we see an attribute, we set them to 'true' (if they
9348 are boolean properties) or to the identifier (if they have an
9349 argument, ie, for getter and setter). Note that here we only
9350 parse the list of attributes, check the syntax and accumulate the
9351 attributes that we find. objc_add_property_declaration() will
9352 then process the information. */
9353 bool property_assign = false;
9354 bool property_copy = false;
9355 tree property_getter_ident = NULL_TREE;
9356 bool property_nonatomic = false;
9357 bool property_readonly = false;
9358 bool property_readwrite = false;
9359 bool property_retain = false;
9360 tree property_setter_ident = NULL_TREE;
9362 /* 'properties' is the list of properties that we read. Usually a
9363 single one, but maybe more (eg, in "@property int a, b, c;" there
9364 are three). */
9365 tree properties;
9366 location_t loc;
9368 loc = c_parser_peek_token (parser)->location;
9369 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_PROPERTY));
9371 c_parser_consume_token (parser); /* Eat '@property'. */
9373 /* Parse the optional attribute list... */
9374 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
9376 /* Eat the '(' */
9377 c_parser_consume_token (parser);
9379 /* Property attribute keywords are valid now. */
9380 parser->objc_property_attr_context = true;
9382 while (true)
9384 bool syntax_error = false;
9385 c_token *token = c_parser_peek_token (parser);
9386 enum rid keyword;
9388 if (token->type != CPP_KEYWORD)
9390 if (token->type == CPP_CLOSE_PAREN)
9391 c_parser_error (parser, "expected identifier");
9392 else
9394 c_parser_consume_token (parser);
9395 c_parser_error (parser, "unknown property attribute");
9397 break;
9399 keyword = token->keyword;
9400 c_parser_consume_token (parser);
9401 switch (keyword)
9403 case RID_ASSIGN: property_assign = true; break;
9404 case RID_COPY: property_copy = true; break;
9405 case RID_NONATOMIC: property_nonatomic = true; break;
9406 case RID_READONLY: property_readonly = true; break;
9407 case RID_READWRITE: property_readwrite = true; break;
9408 case RID_RETAIN: property_retain = true; break;
9410 case RID_GETTER:
9411 case RID_SETTER:
9412 if (c_parser_next_token_is_not (parser, CPP_EQ))
9414 if (keyword == RID_GETTER)
9415 c_parser_error (parser,
9416 "missing %<=%> (after %<getter%> attribute)");
9417 else
9418 c_parser_error (parser,
9419 "missing %<=%> (after %<setter%> attribute)");
9420 syntax_error = true;
9421 break;
9423 c_parser_consume_token (parser); /* eat the = */
9424 if (c_parser_next_token_is_not (parser, CPP_NAME))
9426 c_parser_error (parser, "expected identifier");
9427 syntax_error = true;
9428 break;
9430 if (keyword == RID_SETTER)
9432 if (property_setter_ident != NULL_TREE)
9433 c_parser_error (parser, "the %<setter%> attribute may only be specified once");
9434 else
9435 property_setter_ident = c_parser_peek_token (parser)->value;
9436 c_parser_consume_token (parser);
9437 if (c_parser_next_token_is_not (parser, CPP_COLON))
9438 c_parser_error (parser, "setter name must terminate with %<:%>");
9439 else
9440 c_parser_consume_token (parser);
9442 else
9444 if (property_getter_ident != NULL_TREE)
9445 c_parser_error (parser, "the %<getter%> attribute may only be specified once");
9446 else
9447 property_getter_ident = c_parser_peek_token (parser)->value;
9448 c_parser_consume_token (parser);
9450 break;
9451 default:
9452 c_parser_error (parser, "unknown property attribute");
9453 syntax_error = true;
9454 break;
9457 if (syntax_error)
9458 break;
9460 if (c_parser_next_token_is (parser, CPP_COMMA))
9461 c_parser_consume_token (parser);
9462 else
9463 break;
9465 parser->objc_property_attr_context = false;
9466 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
9468 /* ... and the property declaration(s). */
9469 properties = c_parser_struct_declaration (parser);
9471 if (properties == error_mark_node)
9473 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
9474 parser->error = false;
9475 return;
9478 if (properties == NULL_TREE)
9479 c_parser_error (parser, "expected identifier");
9480 else
9482 /* Comma-separated properties are chained together in
9483 reverse order; add them one by one. */
9484 properties = nreverse (properties);
9486 for (; properties; properties = TREE_CHAIN (properties))
9487 objc_add_property_declaration (loc, copy_node (properties),
9488 property_readonly, property_readwrite,
9489 property_assign, property_retain,
9490 property_copy, property_nonatomic,
9491 property_getter_ident, property_setter_ident);
9494 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9495 parser->error = false;
9498 /* Parse an Objective-C @synthesize declaration. The syntax is:
9500 objc-synthesize-declaration:
9501 @synthesize objc-synthesize-identifier-list ;
9503 objc-synthesize-identifier-list:
9504 objc-synthesize-identifier
9505 objc-synthesize-identifier-list, objc-synthesize-identifier
9507 objc-synthesize-identifier
9508 identifier
9509 identifier = identifier
9511 For example:
9512 @synthesize MyProperty;
9513 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
9515 PS: This function is identical to cp_parser_objc_at_synthesize_declaration
9516 for C++. Keep them in sync.
9518 static void
9519 c_parser_objc_at_synthesize_declaration (c_parser *parser)
9521 tree list = NULL_TREE;
9522 location_t loc;
9523 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_SYNTHESIZE));
9524 loc = c_parser_peek_token (parser)->location;
9526 c_parser_consume_token (parser);
9527 while (true)
9529 tree property, ivar;
9530 if (c_parser_next_token_is_not (parser, CPP_NAME))
9532 c_parser_error (parser, "expected identifier");
9533 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
9534 /* Once we find the semicolon, we can resume normal parsing.
9535 We have to reset parser->error manually because
9536 c_parser_skip_until_found() won't reset it for us if the
9537 next token is precisely a semicolon. */
9538 parser->error = false;
9539 return;
9541 property = c_parser_peek_token (parser)->value;
9542 c_parser_consume_token (parser);
9543 if (c_parser_next_token_is (parser, CPP_EQ))
9545 c_parser_consume_token (parser);
9546 if (c_parser_next_token_is_not (parser, CPP_NAME))
9548 c_parser_error (parser, "expected identifier");
9549 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
9550 parser->error = false;
9551 return;
9553 ivar = c_parser_peek_token (parser)->value;
9554 c_parser_consume_token (parser);
9556 else
9557 ivar = NULL_TREE;
9558 list = chainon (list, build_tree_list (ivar, property));
9559 if (c_parser_next_token_is (parser, CPP_COMMA))
9560 c_parser_consume_token (parser);
9561 else
9562 break;
9564 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9565 objc_add_synthesize_declaration (loc, list);
9568 /* Parse an Objective-C @dynamic declaration. The syntax is:
9570 objc-dynamic-declaration:
9571 @dynamic identifier-list ;
9573 For example:
9574 @dynamic MyProperty;
9575 @dynamic MyProperty, AnotherProperty;
9577 PS: This function is identical to cp_parser_objc_at_dynamic_declaration
9578 for C++. Keep them in sync.
9580 static void
9581 c_parser_objc_at_dynamic_declaration (c_parser *parser)
9583 tree list = NULL_TREE;
9584 location_t loc;
9585 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_DYNAMIC));
9586 loc = c_parser_peek_token (parser)->location;
9588 c_parser_consume_token (parser);
9589 while (true)
9591 tree property;
9592 if (c_parser_next_token_is_not (parser, CPP_NAME))
9594 c_parser_error (parser, "expected identifier");
9595 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
9596 parser->error = false;
9597 return;
9599 property = c_parser_peek_token (parser)->value;
9600 list = chainon (list, build_tree_list (NULL_TREE, property));
9601 c_parser_consume_token (parser);
9602 if (c_parser_next_token_is (parser, CPP_COMMA))
9603 c_parser_consume_token (parser);
9604 else
9605 break;
9607 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9608 objc_add_dynamic_declaration (loc, list);
9611 /* Parse UPC shared qualifier
9613 shared-type-qualifier: shared layout-qualifier-opt
9614 layout-qualifier: [ constant-expression-opt ] | [ * ]
9617 static void
9618 c_parser_upc_shared_qual (source_location loc,
9619 c_parser *parser,
9620 struct c_declspecs *specs)
9622 tree array_qual, arg1;
9624 /* consume "shared" part */
9625 c_parser_consume_token (parser);
9627 /* check for shared array layout specifier */
9628 if (!c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
9630 declspecs_add_qual (loc, specs, ridpointers[RID_SHARED]);
9631 return;
9633 c_parser_consume_token (parser);
9634 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
9636 /* [] layout specifier */
9637 arg1 = size_zero_node;
9639 else if (c_parser_next_token_is (parser, CPP_MULT))
9641 /* [*] layout specifier */
9642 arg1 = build1 (INDIRECT_REF, NULL_TREE, NULL_TREE);
9643 c_parser_consume_token (parser);
9645 else
9647 /* [ expression ] layout specifier */
9648 arg1 = c_parser_expression (parser).value;
9650 array_qual = build4 (ARRAY_REF, NULL_TREE, NULL_TREE,
9651 arg1, NULL_TREE, NULL_TREE);
9652 declspecs_add_qual (loc, specs, array_qual);
9654 if (!c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
9656 c_parser_error (parser, "expected ]");
9658 c_parser_consume_token (parser);
9661 /* Implement UPC's upc_forall 'affinity' test.
9662 If the type of AFFINITY is a UPC pointer-to-shared type,
9663 rewrite it into:
9664 upc_threadof (AFFINITY) == MYTHREAD
9665 If AFFINITY is an integer expression, then
9666 rewrite it into:
9667 (AFFINITY % THREADS) == MYTHREAD */
9669 static tree
9670 upc_affinity_test (location_t loc, tree affinity)
9672 tree mythread;
9673 tree affinity_test;
9675 gcc_assert (affinity != NULL_TREE);
9677 if (TREE_CODE (TREE_TYPE (affinity)) == POINTER_TYPE
9678 && upc_shared_type_p (TREE_TYPE (TREE_TYPE (affinity))))
9680 /* We have a pointer to a UPC shared object and the affinity is
9681 determined by the thread component of the address. */
9682 const tree pts_rep = build1 (VIEW_CONVERT_EXPR, upc_pts_rep_type_node,
9683 save_expr (affinity));
9684 affinity = (*upc_pts.threadof) (loc, pts_rep);
9686 else if (TREE_CODE (TREE_TYPE (affinity)) == INTEGER_TYPE)
9688 tree n_threads = upc_num_threads ();
9689 affinity =
9690 build_binary_op (loc, FLOOR_MOD_EXPR, affinity, n_threads, 0);
9692 else
9694 error
9695 ("UPC affinity expression is neither an integer nor the address of "
9696 "a shared object");
9697 return error_mark_node;
9700 /* Generate an external reference to the "MYTHREAD" identifier. */
9702 mythread = lookup_name (get_identifier ("MYTHREAD"));
9703 gcc_assert (mythread != NULL_TREE);
9704 assemble_external (mythread);
9705 TREE_USED (mythread) = 1;
9707 /* AFFINITY now contains an integer value that can be compared to MY_THREAD.
9708 Create an expression that tests if AFFINITY is equal to MYTHREAD. */
9710 if (!c_types_compatible_p (TREE_TYPE (affinity), TREE_TYPE (mythread)))
9711 affinity = convert (TREE_TYPE (mythread), affinity);
9712 affinity_test = c_objc_common_truthvalue_conversion (loc,
9713 build_binary_op (loc, EQ_EXPR,
9714 affinity, mythread, 1));
9715 /* Remove any MAYBE_CONST_EXPR's. */
9717 affinity_test = c_fully_fold (affinity_test, false, NULL);
9719 return affinity_test;
9722 /* Parse a UPC upc_forall statement
9724 upc_forall-statement:
9725 upc_forall ( expression[opt] ; expression[opt] ;
9726 expression[opt] ; affinity[opt] ) statement
9727 affinity: experssion | continue */
9729 static void
9730 c_parser_upc_forall_statement (c_parser *parser)
9732 tree block, cond, incr, save_break, save_cont, body;
9733 tree affinity;
9734 location_t loc = c_parser_peek_token (parser)->location;
9735 location_t affinity_loc = UNKNOWN_LOCATION;
9736 const int profile_upc_forall = flag_upc_instrument && get_upc_pupc_mode();
9737 gcc_assert (c_parser_next_token_is_keyword (parser, RID_UPC_FORALL));
9738 c_parser_consume_token (parser);
9739 block = c_begin_compound_stmt (flag_isoc99);
9740 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
9742 /* Parse the initialization declaration or expression. */
9743 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
9745 c_parser_consume_token (parser);
9746 c_finish_expr_stmt (loc, NULL_TREE);
9748 else if (c_parser_next_token_starts_declspecs (parser))
9750 c_parser_declaration_or_fndef (parser, true, true, true,
9751 true, true, NULL, vNULL);
9752 check_for_loop_decls (loc, true);
9754 else if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
9756 /* __extension__ can start a declaration, but is also an
9757 unary operator that can start an expression. Consume all
9758 but the last of a possible series of __extension__ to
9759 determine which. */
9760 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
9761 && (c_parser_peek_2nd_token (parser)->keyword
9762 == RID_EXTENSION))
9763 c_parser_consume_token (parser);
9764 if (c_token_starts_declspecs (c_parser_peek_2nd_token (parser)))
9766 int ext;
9767 ext = disable_extension_diagnostics ();
9768 c_parser_consume_token (parser);
9769 c_parser_declaration_or_fndef (parser, true, true, true, true, true,
9770 NULL, vNULL);
9771 restore_extension_diagnostics (ext);
9772 check_for_loop_decls (loc, true);
9774 else
9775 goto init_expr;
9777 else
9779 init_expr:
9780 c_finish_expr_stmt (loc, c_parser_expression (parser).value);
9781 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9783 /* Parse the loop condition. */
9784 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
9786 c_parser_consume_token (parser);
9787 cond = NULL_TREE;
9789 else
9791 cond = c_parser_condition (parser);
9792 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9794 /* Parse the increment expression. */
9795 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
9796 incr = c_process_expr_stmt (loc, NULL_TREE);
9797 else
9798 incr = c_process_expr_stmt (loc, c_parser_expression (parser).value);
9799 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9800 /* Parse the UPC affinity expression. */
9801 affinity_loc = c_parser_peek_token (parser)->location;
9802 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
9804 affinity = NULL_TREE;
9806 else if (c_parser_peek_token (parser)->type == CPP_KEYWORD
9807 && c_parser_peek_token (parser)->keyword == RID_CONTINUE)
9809 affinity = NULL_TREE;
9810 c_parser_consume_token (parser);
9812 else
9814 affinity = c_parser_expression_conv (parser).value;
9815 affinity = c_fully_fold (affinity, false, NULL);
9817 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
9818 if (affinity)
9819 affinity = upc_affinity_test (affinity_loc, affinity);
9821 else
9823 cond = error_mark_node;
9824 incr = error_mark_node;
9825 affinity = error_mark_node;
9827 save_break = c_break_label;
9828 c_break_label = NULL_TREE;
9829 save_cont = c_cont_label;
9830 c_cont_label = NULL_TREE;
9831 body = c_parser_c99_block_statement (parser);
9832 if (profile_upc_forall)
9834 const tree gasp_start = upc_instrument_forall (loc, 1 /* start */);
9835 add_stmt (gasp_start);
9837 loc = c_parser_peek_token (parser)->location;
9838 if (affinity != NULL_TREE && affinity != error_mark_node)
9840 tree upc_forall_depth = upc_rts_forall_depth_var ();
9841 tree inc_depth, depth_gt_one;
9842 inc_depth = build_unary_op (loc, PREINCREMENT_EXPR, upc_forall_depth, 0);
9843 c_finish_expr_stmt (loc, inc_depth);
9844 depth_gt_one = build_binary_op (affinity_loc,
9845 GT_EXPR, upc_forall_depth, integer_one_node, 0);
9846 depth_gt_one = c_objc_common_truthvalue_conversion (affinity_loc, depth_gt_one);
9847 depth_gt_one = c_fully_fold (depth_gt_one, false, NULL);
9848 affinity = build_binary_op (affinity_loc, TRUTH_OR_EXPR,
9849 depth_gt_one, affinity, 0);
9850 body = build3 (COND_EXPR, void_type_node, affinity,
9851 body, NULL_TREE);
9852 c_finish_loop (loc, cond, incr, body, c_break_label, c_cont_label, true);
9853 c_finish_expr_stmt (loc,
9854 build_unary_op (loc, PREDECREMENT_EXPR, upc_forall_depth, 0));
9856 else
9857 c_finish_loop (loc, cond, incr, body, c_break_label, c_cont_label, true);
9858 if (profile_upc_forall)
9860 const tree gasp_end = upc_instrument_forall (loc, 0 /* start */);
9861 add_stmt (gasp_end);
9863 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
9864 c_break_label = save_break;
9865 c_cont_label = save_cont;
9868 /* For the given kind of UPC synchronization statement given
9869 by SYNC_KIND (UPC_SYNC_NOTIFY_OP, UPC_SYNC_WAIT_OP,
9870 or UPC_SYNC_BARRIER_OP), build a UPC_SYNC_STMT tree node,
9871 and add it to the current statement list. The value of
9872 SYNC_EXPR will be non-null if an expression is present
9873 in the UPC statement being compiled.
9875 If SYNC_EXPR is supplied, it must be assignment compatible
9876 with type 'int'. */
9878 static tree
9879 upc_build_sync_stmt (location_t loc, tree sync_kind, tree sync_expr)
9881 if (sync_expr != NULL_TREE)
9883 mark_exp_read (sync_expr);
9884 sync_expr = c_cvt_expr_for_assign (loc, integer_type_node, sync_expr);
9885 if (sync_expr == error_mark_node)
9887 inform (loc, "UPC synchronization statement expressions "
9888 "must be assignment compatible with type `int'");
9889 sync_expr = NULL_TREE;
9892 return add_stmt (build_stmt (loc, UPC_SYNC_STMT, sync_kind, sync_expr));
9895 /* Parse an upc-sync-statement.
9897 upc_barrier, upc_wait, upc_notify
9900 static void
9901 c_parser_upc_sync_statement (c_parser *parser, int sync_kind)
9903 location_t loc;
9904 tree expr = NULL_TREE;
9905 tree stmt;
9906 gcc_assert (c_parser_next_token_is_keyword (parser, RID_UPC_BARRIER) ||
9907 c_parser_next_token_is_keyword (parser, RID_UPC_NOTIFY) ||
9908 c_parser_next_token_is_keyword (parser, RID_UPC_WAIT));
9909 loc = c_parser_peek_token (parser)->location;
9910 c_parser_consume_token (parser);
9911 if (c_parser_peek_token (parser)->type != CPP_SEMICOLON)
9913 loc = c_parser_peek_token (parser)->location;
9914 expr = c_parser_expression (parser).value;
9915 if (expr == error_mark_node)
9916 expr = NULL;
9918 stmt = size_int (sync_kind);
9919 (void) upc_build_sync_stmt (loc, stmt, expr);
9923 /* Handle pragmas. Some OpenMP pragmas are associated with, and therefore
9924 should be considered, statements. ALLOW_STMT is true if we're within
9925 the context of a function and such pragmas are to be allowed. Returns
9926 true if we actually parsed such a pragma. */
9928 static bool
9929 c_parser_pragma (c_parser *parser, enum pragma_context context)
9931 unsigned int id;
9933 id = c_parser_peek_token (parser)->pragma_kind;
9934 gcc_assert (id != PRAGMA_NONE);
9936 switch (id)
9938 case PRAGMA_OMP_BARRIER:
9939 if (context != pragma_compound)
9941 if (context == pragma_stmt)
9942 c_parser_error (parser, "%<#pragma omp barrier%> may only be "
9943 "used in compound statements");
9944 goto bad_stmt;
9946 c_parser_omp_barrier (parser);
9947 return false;
9949 case PRAGMA_OMP_FLUSH:
9950 if (context != pragma_compound)
9952 if (context == pragma_stmt)
9953 c_parser_error (parser, "%<#pragma omp flush%> may only be "
9954 "used in compound statements");
9955 goto bad_stmt;
9957 c_parser_omp_flush (parser);
9958 return false;
9960 case PRAGMA_OMP_TASKWAIT:
9961 if (context != pragma_compound)
9963 if (context == pragma_stmt)
9964 c_parser_error (parser, "%<#pragma omp taskwait%> may only be "
9965 "used in compound statements");
9966 goto bad_stmt;
9968 c_parser_omp_taskwait (parser);
9969 return false;
9971 case PRAGMA_OMP_TASKYIELD:
9972 if (context != pragma_compound)
9974 if (context == pragma_stmt)
9975 c_parser_error (parser, "%<#pragma omp taskyield%> may only be "
9976 "used in compound statements");
9977 goto bad_stmt;
9979 c_parser_omp_taskyield (parser);
9980 return false;
9982 case PRAGMA_OMP_CANCEL:
9983 if (context != pragma_compound)
9985 if (context == pragma_stmt)
9986 c_parser_error (parser, "%<#pragma omp cancel%> may only be "
9987 "used in compound statements");
9988 goto bad_stmt;
9990 c_parser_omp_cancel (parser);
9991 return false;
9993 case PRAGMA_OMP_CANCELLATION_POINT:
9994 if (context != pragma_compound)
9996 if (context == pragma_stmt)
9997 c_parser_error (parser, "%<#pragma omp cancellation point%> may "
9998 "only be used in compound statements");
9999 goto bad_stmt;
10001 c_parser_omp_cancellation_point (parser);
10002 return false;
10004 case PRAGMA_OMP_THREADPRIVATE:
10005 c_parser_omp_threadprivate (parser);
10006 return false;
10008 case PRAGMA_OMP_TARGET:
10009 return c_parser_omp_target (parser, context);
10011 case PRAGMA_OMP_END_DECLARE_TARGET:
10012 c_parser_omp_end_declare_target (parser);
10013 return false;
10015 case PRAGMA_OMP_SECTION:
10016 error_at (c_parser_peek_token (parser)->location,
10017 "%<#pragma omp section%> may only be used in "
10018 "%<#pragma omp sections%> construct");
10019 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
10020 return false;
10022 case PRAGMA_OMP_DECLARE_REDUCTION:
10023 c_parser_omp_declare (parser, context);
10024 return false;
10025 case PRAGMA_IVDEP:
10026 c_parser_consume_pragma (parser);
10027 c_parser_skip_to_pragma_eol (parser);
10028 if (!c_parser_next_token_is_keyword (parser, RID_FOR)
10029 && !c_parser_next_token_is_keyword (parser, RID_WHILE)
10030 && !c_parser_next_token_is_keyword (parser, RID_DO))
10032 c_parser_error (parser, "for, while or do statement expected");
10033 return false;
10035 if (c_parser_next_token_is_keyword (parser, RID_FOR))
10036 c_parser_for_statement (parser, true);
10037 else if (c_parser_next_token_is_keyword (parser, RID_WHILE))
10038 c_parser_while_statement (parser, true);
10039 else
10040 c_parser_do_statement (parser, true);
10041 return false;
10043 case PRAGMA_GCC_PCH_PREPROCESS:
10044 c_parser_error (parser, "%<#pragma GCC pch_preprocess%> must be first");
10045 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
10046 return false;
10048 case PRAGMA_CILK_SIMD:
10049 if (!c_parser_cilk_verify_simd (parser, context))
10050 return false;
10051 c_parser_consume_pragma (parser);
10052 c_parser_cilk_simd (parser);
10053 return false;
10055 default:
10056 if (id < PRAGMA_FIRST_EXTERNAL)
10058 if (context != pragma_stmt && context != pragma_compound)
10060 bad_stmt:
10061 c_parser_error (parser, "expected declaration specifiers");
10062 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
10063 return false;
10065 c_parser_omp_construct (parser);
10066 return true;
10068 break;
10071 c_parser_consume_pragma (parser);
10072 c_invoke_pragma_handler (id);
10074 /* Skip to EOL, but suppress any error message. Those will have been
10075 generated by the handler routine through calling error, as opposed
10076 to calling c_parser_error. */
10077 parser->error = true;
10078 c_parser_skip_to_pragma_eol (parser);
10080 return false;
10083 /* The interface the pragma parsers have to the lexer. */
10085 enum cpp_ttype
10086 pragma_lex (tree *value)
10088 c_token *tok = c_parser_peek_token (the_parser);
10089 enum cpp_ttype ret = tok->type;
10091 *value = tok->value;
10092 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
10093 ret = CPP_EOF;
10094 else
10096 if (ret == CPP_KEYWORD)
10097 ret = CPP_NAME;
10098 c_parser_consume_token (the_parser);
10101 return ret;
10104 static void
10105 c_parser_pragma_pch_preprocess (c_parser *parser)
10107 tree name = NULL;
10109 c_parser_consume_pragma (parser);
10110 if (c_parser_next_token_is (parser, CPP_STRING))
10112 name = c_parser_peek_token (parser)->value;
10113 c_parser_consume_token (parser);
10115 else
10116 c_parser_error (parser, "expected string literal");
10117 c_parser_skip_to_pragma_eol (parser);
10119 if (name)
10120 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
10123 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 parsing routines. */
10125 /* Returns name of the next clause.
10126 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
10127 the token is not consumed. Otherwise appropriate pragma_omp_clause is
10128 returned and the token is consumed. */
10130 static pragma_omp_clause
10131 c_parser_omp_clause_name (c_parser *parser)
10133 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
10135 if (c_parser_next_token_is_keyword (parser, RID_IF))
10136 result = PRAGMA_OMP_CLAUSE_IF;
10137 else if (c_parser_next_token_is_keyword (parser, RID_DEFAULT))
10138 result = PRAGMA_OMP_CLAUSE_DEFAULT;
10139 else if (c_parser_next_token_is_keyword (parser, RID_FOR))
10140 result = PRAGMA_OMP_CLAUSE_FOR;
10141 else if (c_parser_next_token_is (parser, CPP_NAME))
10143 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
10145 switch (p[0])
10147 case 'a':
10148 if (!strcmp ("aligned", p))
10149 result = PRAGMA_OMP_CLAUSE_ALIGNED;
10150 break;
10151 case 'c':
10152 if (!strcmp ("collapse", p))
10153 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
10154 else if (!strcmp ("copyin", p))
10155 result = PRAGMA_OMP_CLAUSE_COPYIN;
10156 else if (!strcmp ("copyprivate", p))
10157 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
10158 break;
10159 case 'd':
10160 if (!strcmp ("depend", p))
10161 result = PRAGMA_OMP_CLAUSE_DEPEND;
10162 else if (!strcmp ("device", p))
10163 result = PRAGMA_OMP_CLAUSE_DEVICE;
10164 else if (!strcmp ("dist_schedule", p))
10165 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
10166 break;
10167 case 'f':
10168 if (!strcmp ("final", p))
10169 result = PRAGMA_OMP_CLAUSE_FINAL;
10170 else if (!strcmp ("firstprivate", p))
10171 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
10172 else if (!strcmp ("from", p))
10173 result = PRAGMA_OMP_CLAUSE_FROM;
10174 break;
10175 case 'i':
10176 if (!strcmp ("inbranch", p))
10177 result = PRAGMA_OMP_CLAUSE_INBRANCH;
10178 break;
10179 case 'l':
10180 if (!strcmp ("lastprivate", p))
10181 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
10182 else if (!strcmp ("linear", p))
10183 result = PRAGMA_OMP_CLAUSE_LINEAR;
10184 break;
10185 case 'm':
10186 if (!strcmp ("map", p))
10187 result = PRAGMA_OMP_CLAUSE_MAP;
10188 else if (!strcmp ("mergeable", p))
10189 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
10190 else if (flag_cilkplus && !strcmp ("mask", p))
10191 result = PRAGMA_CILK_CLAUSE_MASK;
10192 break;
10193 case 'n':
10194 if (!strcmp ("notinbranch", p))
10195 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
10196 else if (!strcmp ("nowait", p))
10197 result = PRAGMA_OMP_CLAUSE_NOWAIT;
10198 else if (!strcmp ("num_teams", p))
10199 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
10200 else if (!strcmp ("num_threads", p))
10201 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
10202 else if (flag_cilkplus && !strcmp ("nomask", p))
10203 result = PRAGMA_CILK_CLAUSE_NOMASK;
10204 break;
10205 case 'o':
10206 if (!strcmp ("ordered", p))
10207 result = PRAGMA_OMP_CLAUSE_ORDERED;
10208 break;
10209 case 'p':
10210 if (!strcmp ("parallel", p))
10211 result = PRAGMA_OMP_CLAUSE_PARALLEL;
10212 else if (!strcmp ("private", p))
10213 result = PRAGMA_OMP_CLAUSE_PRIVATE;
10214 else if (!strcmp ("proc_bind", p))
10215 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
10216 break;
10217 case 'r':
10218 if (!strcmp ("reduction", p))
10219 result = PRAGMA_OMP_CLAUSE_REDUCTION;
10220 break;
10221 case 's':
10222 if (!strcmp ("safelen", p))
10223 result = PRAGMA_OMP_CLAUSE_SAFELEN;
10224 else if (!strcmp ("schedule", p))
10225 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
10226 else if (!strcmp ("sections", p))
10227 result = PRAGMA_OMP_CLAUSE_SECTIONS;
10228 else if (!strcmp ("shared", p))
10229 result = PRAGMA_OMP_CLAUSE_SHARED;
10230 else if (!strcmp ("simdlen", p))
10231 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
10232 break;
10233 case 't':
10234 if (!strcmp ("taskgroup", p))
10235 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
10236 else if (!strcmp ("thread_limit", p))
10237 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
10238 else if (!strcmp ("to", p))
10239 result = PRAGMA_OMP_CLAUSE_TO;
10240 break;
10241 case 'u':
10242 if (!strcmp ("uniform", p))
10243 result = PRAGMA_OMP_CLAUSE_UNIFORM;
10244 else if (!strcmp ("untied", p))
10245 result = PRAGMA_OMP_CLAUSE_UNTIED;
10246 break;
10247 case 'v':
10248 if (flag_cilkplus && !strcmp ("vectorlength", p))
10249 result = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
10250 break;
10254 if (result != PRAGMA_OMP_CLAUSE_NONE)
10255 c_parser_consume_token (parser);
10257 return result;
10260 /* Validate that a clause of the given type does not already exist. */
10262 static void
10263 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
10264 const char *name)
10266 tree c;
10268 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
10269 if (OMP_CLAUSE_CODE (c) == code)
10271 location_t loc = OMP_CLAUSE_LOCATION (c);
10272 error_at (loc, "too many %qs clauses", name);
10273 break;
10277 /* OpenMP 2.5:
10278 variable-list:
10279 identifier
10280 variable-list , identifier
10282 If KIND is nonzero, create the appropriate node and install the
10283 decl in OMP_CLAUSE_DECL and add the node to the head of the list.
10284 If KIND is nonzero, CLAUSE_LOC is the location of the clause.
10286 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
10287 return the list created. */
10289 static tree
10290 c_parser_omp_variable_list (c_parser *parser,
10291 location_t clause_loc,
10292 enum omp_clause_code kind, tree list)
10294 if (c_parser_next_token_is_not (parser, CPP_NAME)
10295 || c_parser_peek_token (parser)->id_kind != C_ID_ID)
10296 c_parser_error (parser, "expected identifier");
10298 while (c_parser_next_token_is (parser, CPP_NAME)
10299 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
10301 tree t = lookup_name (c_parser_peek_token (parser)->value);
10303 if (t == NULL_TREE)
10305 undeclared_variable (c_parser_peek_token (parser)->location,
10306 c_parser_peek_token (parser)->value);
10307 t = error_mark_node;
10310 c_parser_consume_token (parser);
10312 if (t == error_mark_node)
10314 else if (kind != 0)
10316 switch (kind)
10318 case OMP_CLAUSE_MAP:
10319 case OMP_CLAUSE_FROM:
10320 case OMP_CLAUSE_TO:
10321 case OMP_CLAUSE_DEPEND:
10322 while (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
10324 tree low_bound = NULL_TREE, length = NULL_TREE;
10326 c_parser_consume_token (parser);
10327 if (!c_parser_next_token_is (parser, CPP_COLON))
10328 low_bound = c_parser_expression (parser).value;
10329 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
10330 length = integer_one_node;
10331 else
10333 /* Look for `:'. */
10334 if (!c_parser_require (parser, CPP_COLON,
10335 "expected %<:%>"))
10337 t = error_mark_node;
10338 break;
10340 if (!c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
10341 length = c_parser_expression (parser).value;
10343 /* Look for the closing `]'. */
10344 if (!c_parser_require (parser, CPP_CLOSE_SQUARE,
10345 "expected %<]%>"))
10347 t = error_mark_node;
10348 break;
10350 t = tree_cons (low_bound, length, t);
10352 break;
10353 default:
10354 break;
10357 if (t != error_mark_node)
10359 tree u = build_omp_clause (clause_loc, kind);
10360 OMP_CLAUSE_DECL (u) = t;
10361 OMP_CLAUSE_CHAIN (u) = list;
10362 list = u;
10365 else
10366 list = tree_cons (t, NULL_TREE, list);
10368 if (c_parser_next_token_is_not (parser, CPP_COMMA))
10369 break;
10371 c_parser_consume_token (parser);
10374 return list;
10377 /* Similarly, but expect leading and trailing parenthesis. This is a very
10378 common case for omp clauses. */
10380 static tree
10381 c_parser_omp_var_list_parens (c_parser *parser, enum omp_clause_code kind,
10382 tree list)
10384 /* The clauses location. */
10385 location_t loc = c_parser_peek_token (parser)->location;
10387 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10389 list = c_parser_omp_variable_list (parser, loc, kind, list);
10390 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10392 return list;
10395 /* OpenMP 3.0:
10396 collapse ( constant-expression ) */
10398 static tree
10399 c_parser_omp_clause_collapse (c_parser *parser, tree list)
10401 tree c, num = error_mark_node;
10402 HOST_WIDE_INT n;
10403 location_t loc;
10405 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse");
10407 loc = c_parser_peek_token (parser)->location;
10408 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10410 num = c_parser_expr_no_commas (parser, NULL).value;
10411 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10413 if (num == error_mark_node)
10414 return list;
10415 mark_exp_read (num);
10416 num = c_fully_fold (num, false, NULL);
10417 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
10418 || !tree_fits_shwi_p (num)
10419 || (n = tree_to_shwi (num)) <= 0
10420 || (int) n != n)
10422 error_at (loc,
10423 "collapse argument needs positive constant integer expression");
10424 return list;
10426 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
10427 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
10428 OMP_CLAUSE_CHAIN (c) = list;
10429 return c;
10432 /* OpenMP 2.5:
10433 copyin ( variable-list ) */
10435 static tree
10436 c_parser_omp_clause_copyin (c_parser *parser, tree list)
10438 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_COPYIN, list);
10441 /* OpenMP 2.5:
10442 copyprivate ( variable-list ) */
10444 static tree
10445 c_parser_omp_clause_copyprivate (c_parser *parser, tree list)
10447 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_COPYPRIVATE, list);
10450 /* OpenMP 2.5:
10451 default ( shared | none ) */
10453 static tree
10454 c_parser_omp_clause_default (c_parser *parser, tree list)
10456 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
10457 location_t loc = c_parser_peek_token (parser)->location;
10458 tree c;
10460 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10461 return list;
10462 if (c_parser_next_token_is (parser, CPP_NAME))
10464 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
10466 switch (p[0])
10468 case 'n':
10469 if (strcmp ("none", p) != 0)
10470 goto invalid_kind;
10471 kind = OMP_CLAUSE_DEFAULT_NONE;
10472 break;
10474 case 's':
10475 if (strcmp ("shared", p) != 0)
10476 goto invalid_kind;
10477 kind = OMP_CLAUSE_DEFAULT_SHARED;
10478 break;
10480 default:
10481 goto invalid_kind;
10484 c_parser_consume_token (parser);
10486 else
10488 invalid_kind:
10489 c_parser_error (parser, "expected %<none%> or %<shared%>");
10491 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10493 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
10494 return list;
10496 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default");
10497 c = build_omp_clause (loc, OMP_CLAUSE_DEFAULT);
10498 OMP_CLAUSE_CHAIN (c) = list;
10499 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
10501 return c;
10504 /* OpenMP 2.5:
10505 firstprivate ( variable-list ) */
10507 static tree
10508 c_parser_omp_clause_firstprivate (c_parser *parser, tree list)
10510 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_FIRSTPRIVATE, list);
10513 /* OpenMP 3.1:
10514 final ( expression ) */
10516 static tree
10517 c_parser_omp_clause_final (c_parser *parser, tree list)
10519 location_t loc = c_parser_peek_token (parser)->location;
10520 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
10522 tree t = c_parser_paren_condition (parser);
10523 tree c;
10525 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final");
10527 c = build_omp_clause (loc, OMP_CLAUSE_FINAL);
10528 OMP_CLAUSE_FINAL_EXPR (c) = t;
10529 OMP_CLAUSE_CHAIN (c) = list;
10530 list = c;
10532 else
10533 c_parser_error (parser, "expected %<(%>");
10535 return list;
10538 /* OpenMP 2.5:
10539 if ( expression ) */
10541 static tree
10542 c_parser_omp_clause_if (c_parser *parser, tree list)
10544 location_t loc = c_parser_peek_token (parser)->location;
10545 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
10547 tree t = c_parser_paren_condition (parser);
10548 tree c;
10550 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if");
10552 c = build_omp_clause (loc, OMP_CLAUSE_IF);
10553 OMP_CLAUSE_IF_EXPR (c) = t;
10554 OMP_CLAUSE_CHAIN (c) = list;
10555 list = c;
10557 else
10558 c_parser_error (parser, "expected %<(%>");
10560 return list;
10563 /* OpenMP 2.5:
10564 lastprivate ( variable-list ) */
10566 static tree
10567 c_parser_omp_clause_lastprivate (c_parser *parser, tree list)
10569 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_LASTPRIVATE, list);
10572 /* OpenMP 3.1:
10573 mergeable */
10575 static tree
10576 c_parser_omp_clause_mergeable (c_parser *parser ATTRIBUTE_UNUSED, tree list)
10578 tree c;
10580 /* FIXME: Should we allow duplicates? */
10581 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable");
10583 c = build_omp_clause (c_parser_peek_token (parser)->location,
10584 OMP_CLAUSE_MERGEABLE);
10585 OMP_CLAUSE_CHAIN (c) = list;
10587 return c;
10590 /* OpenMP 2.5:
10591 nowait */
10593 static tree
10594 c_parser_omp_clause_nowait (c_parser *parser ATTRIBUTE_UNUSED, tree list)
10596 tree c;
10597 location_t loc = c_parser_peek_token (parser)->location;
10599 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait");
10601 c = build_omp_clause (loc, OMP_CLAUSE_NOWAIT);
10602 OMP_CLAUSE_CHAIN (c) = list;
10603 return c;
10606 /* OpenMP 2.5:
10607 num_threads ( expression ) */
10609 static tree
10610 c_parser_omp_clause_num_threads (c_parser *parser, tree list)
10612 location_t num_threads_loc = c_parser_peek_token (parser)->location;
10613 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10615 location_t expr_loc = c_parser_peek_token (parser)->location;
10616 tree c, t = c_parser_expression (parser).value;
10617 mark_exp_read (t);
10618 t = c_fully_fold (t, false, NULL);
10620 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10622 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
10624 c_parser_error (parser, "expected integer expression");
10625 return list;
10628 /* Attempt to statically determine when the number isn't positive. */
10629 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
10630 build_int_cst (TREE_TYPE (t), 0));
10631 if (CAN_HAVE_LOCATION_P (c))
10632 SET_EXPR_LOCATION (c, expr_loc);
10633 if (c == boolean_true_node)
10635 warning_at (expr_loc, 0,
10636 "%<num_threads%> value must be positive");
10637 t = integer_one_node;
10640 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS, "num_threads");
10642 c = build_omp_clause (num_threads_loc, OMP_CLAUSE_NUM_THREADS);
10643 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
10644 OMP_CLAUSE_CHAIN (c) = list;
10645 list = c;
10648 return list;
10651 /* OpenMP 2.5:
10652 ordered */
10654 static tree
10655 c_parser_omp_clause_ordered (c_parser *parser, tree list)
10657 tree c;
10659 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED, "ordered");
10661 c = build_omp_clause (c_parser_peek_token (parser)->location,
10662 OMP_CLAUSE_ORDERED);
10663 OMP_CLAUSE_CHAIN (c) = list;
10665 return c;
10668 /* OpenMP 2.5:
10669 private ( variable-list ) */
10671 static tree
10672 c_parser_omp_clause_private (c_parser *parser, tree list)
10674 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_PRIVATE, list);
10677 /* OpenMP 2.5:
10678 reduction ( reduction-operator : variable-list )
10680 reduction-operator:
10681 One of: + * - & ^ | && ||
10683 OpenMP 3.1:
10685 reduction-operator:
10686 One of: + * - & ^ | && || max min
10688 OpenMP 4.0:
10690 reduction-operator:
10691 One of: + * - & ^ | && ||
10692 identifier */
10694 static tree
10695 c_parser_omp_clause_reduction (c_parser *parser, tree list)
10697 location_t clause_loc = c_parser_peek_token (parser)->location;
10698 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10700 enum tree_code code = ERROR_MARK;
10701 tree reduc_id = NULL_TREE;
10703 switch (c_parser_peek_token (parser)->type)
10705 case CPP_PLUS:
10706 code = PLUS_EXPR;
10707 break;
10708 case CPP_MULT:
10709 code = MULT_EXPR;
10710 break;
10711 case CPP_MINUS:
10712 code = MINUS_EXPR;
10713 break;
10714 case CPP_AND:
10715 code = BIT_AND_EXPR;
10716 break;
10717 case CPP_XOR:
10718 code = BIT_XOR_EXPR;
10719 break;
10720 case CPP_OR:
10721 code = BIT_IOR_EXPR;
10722 break;
10723 case CPP_AND_AND:
10724 code = TRUTH_ANDIF_EXPR;
10725 break;
10726 case CPP_OR_OR:
10727 code = TRUTH_ORIF_EXPR;
10728 break;
10729 case CPP_NAME:
10731 const char *p
10732 = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
10733 if (strcmp (p, "min") == 0)
10735 code = MIN_EXPR;
10736 break;
10738 if (strcmp (p, "max") == 0)
10740 code = MAX_EXPR;
10741 break;
10743 reduc_id = c_parser_peek_token (parser)->value;
10744 break;
10746 default:
10747 c_parser_error (parser,
10748 "expected %<+%>, %<*%>, %<-%>, %<&%>, "
10749 "%<^%>, %<|%>, %<&&%>, %<||%>, %<min%> or %<max%>");
10750 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
10751 return list;
10753 c_parser_consume_token (parser);
10754 reduc_id = c_omp_reduction_id (code, reduc_id);
10755 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
10757 tree nl, c;
10759 nl = c_parser_omp_variable_list (parser, clause_loc,
10760 OMP_CLAUSE_REDUCTION, list);
10761 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
10763 tree type = TREE_TYPE (OMP_CLAUSE_DECL (c));
10764 OMP_CLAUSE_REDUCTION_CODE (c) = code;
10765 if (code == ERROR_MARK
10766 || !(INTEGRAL_TYPE_P (type)
10767 || TREE_CODE (type) == REAL_TYPE
10768 || TREE_CODE (type) == COMPLEX_TYPE))
10769 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c)
10770 = c_omp_reduction_lookup (reduc_id,
10771 TYPE_MAIN_VARIANT (type));
10774 list = nl;
10776 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10778 return list;
10781 /* OpenMP 2.5:
10782 schedule ( schedule-kind )
10783 schedule ( schedule-kind , expression )
10785 schedule-kind:
10786 static | dynamic | guided | runtime | auto
10789 static tree
10790 c_parser_omp_clause_schedule (c_parser *parser, tree list)
10792 tree c, t;
10793 location_t loc = c_parser_peek_token (parser)->location;
10795 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10796 return list;
10798 c = build_omp_clause (loc, OMP_CLAUSE_SCHEDULE);
10800 if (c_parser_next_token_is (parser, CPP_NAME))
10802 tree kind = c_parser_peek_token (parser)->value;
10803 const char *p = IDENTIFIER_POINTER (kind);
10805 switch (p[0])
10807 case 'd':
10808 if (strcmp ("dynamic", p) != 0)
10809 goto invalid_kind;
10810 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
10811 break;
10813 case 'g':
10814 if (strcmp ("guided", p) != 0)
10815 goto invalid_kind;
10816 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
10817 break;
10819 case 'r':
10820 if (strcmp ("runtime", p) != 0)
10821 goto invalid_kind;
10822 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
10823 break;
10825 default:
10826 goto invalid_kind;
10829 else if (c_parser_next_token_is_keyword (parser, RID_STATIC))
10830 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
10831 else if (c_parser_next_token_is_keyword (parser, RID_AUTO))
10832 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
10833 else
10834 goto invalid_kind;
10836 c_parser_consume_token (parser);
10837 if (c_parser_next_token_is (parser, CPP_COMMA))
10839 location_t here;
10840 c_parser_consume_token (parser);
10842 here = c_parser_peek_token (parser)->location;
10843 t = c_parser_expr_no_commas (parser, NULL).value;
10844 mark_exp_read (t);
10845 t = c_fully_fold (t, false, NULL);
10847 if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
10848 error_at (here, "schedule %<runtime%> does not take "
10849 "a %<chunk_size%> parameter");
10850 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
10851 error_at (here,
10852 "schedule %<auto%> does not take "
10853 "a %<chunk_size%> parameter");
10854 else if (TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE)
10855 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
10856 else
10857 c_parser_error (parser, "expected integer expression");
10859 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10861 else
10862 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
10863 "expected %<,%> or %<)%>");
10865 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
10866 OMP_CLAUSE_CHAIN (c) = list;
10867 return c;
10869 invalid_kind:
10870 c_parser_error (parser, "invalid schedule kind");
10871 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
10872 return list;
10875 /* OpenMP 2.5:
10876 shared ( variable-list ) */
10878 static tree
10879 c_parser_omp_clause_shared (c_parser *parser, tree list)
10881 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_SHARED, list);
10884 /* OpenMP 3.0:
10885 untied */
10887 static tree
10888 c_parser_omp_clause_untied (c_parser *parser ATTRIBUTE_UNUSED, tree list)
10890 tree c;
10892 /* FIXME: Should we allow duplicates? */
10893 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied");
10895 c = build_omp_clause (c_parser_peek_token (parser)->location,
10896 OMP_CLAUSE_UNTIED);
10897 OMP_CLAUSE_CHAIN (c) = list;
10899 return c;
10902 /* OpenMP 4.0:
10903 inbranch
10904 notinbranch */
10906 static tree
10907 c_parser_omp_clause_branch (c_parser *parser ATTRIBUTE_UNUSED,
10908 enum omp_clause_code code, tree list)
10910 check_no_duplicate_clause (list, code, omp_clause_code_name[code]);
10912 tree c = build_omp_clause (c_parser_peek_token (parser)->location, code);
10913 OMP_CLAUSE_CHAIN (c) = list;
10915 return c;
10918 /* OpenMP 4.0:
10919 parallel
10921 sections
10922 taskgroup */
10924 static tree
10925 c_parser_omp_clause_cancelkind (c_parser *parser ATTRIBUTE_UNUSED,
10926 enum omp_clause_code code, tree list)
10928 tree c = build_omp_clause (c_parser_peek_token (parser)->location, code);
10929 OMP_CLAUSE_CHAIN (c) = list;
10931 return c;
10934 /* OpenMP 4.0:
10935 num_teams ( expression ) */
10937 static tree
10938 c_parser_omp_clause_num_teams (c_parser *parser, tree list)
10940 location_t num_teams_loc = c_parser_peek_token (parser)->location;
10941 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10943 location_t expr_loc = c_parser_peek_token (parser)->location;
10944 tree c, t = c_parser_expression (parser).value;
10945 mark_exp_read (t);
10946 t = c_fully_fold (t, false, NULL);
10948 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10950 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
10952 c_parser_error (parser, "expected integer expression");
10953 return list;
10956 /* Attempt to statically determine when the number isn't positive. */
10957 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
10958 build_int_cst (TREE_TYPE (t), 0));
10959 if (CAN_HAVE_LOCATION_P (c))
10960 SET_EXPR_LOCATION (c, expr_loc);
10961 if (c == boolean_true_node)
10963 warning_at (expr_loc, 0, "%<num_teams%> value must be positive");
10964 t = integer_one_node;
10967 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS, "num_teams");
10969 c = build_omp_clause (num_teams_loc, OMP_CLAUSE_NUM_TEAMS);
10970 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
10971 OMP_CLAUSE_CHAIN (c) = list;
10972 list = c;
10975 return list;
10978 /* OpenMP 4.0:
10979 thread_limit ( expression ) */
10981 static tree
10982 c_parser_omp_clause_thread_limit (c_parser *parser, tree list)
10984 location_t num_teams_loc = c_parser_peek_token (parser)->location;
10985 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10987 location_t expr_loc = c_parser_peek_token (parser)->location;
10988 tree c, t = c_parser_expression (parser).value;
10989 mark_exp_read (t);
10990 t = c_fully_fold (t, false, NULL);
10992 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10994 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
10996 c_parser_error (parser, "expected integer expression");
10997 return list;
11000 /* Attempt to statically determine when the number isn't positive. */
11001 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
11002 build_int_cst (TREE_TYPE (t), 0));
11003 if (CAN_HAVE_LOCATION_P (c))
11004 SET_EXPR_LOCATION (c, expr_loc);
11005 if (c == boolean_true_node)
11007 warning_at (expr_loc, 0, "%<thread_limit%> value must be positive");
11008 t = integer_one_node;
11011 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
11012 "thread_limit");
11014 c = build_omp_clause (num_teams_loc, OMP_CLAUSE_THREAD_LIMIT);
11015 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
11016 OMP_CLAUSE_CHAIN (c) = list;
11017 list = c;
11020 return list;
11023 /* OpenMP 4.0:
11024 aligned ( variable-list )
11025 aligned ( variable-list : constant-expression ) */
11027 static tree
11028 c_parser_omp_clause_aligned (c_parser *parser, tree list)
11030 location_t clause_loc = c_parser_peek_token (parser)->location;
11031 tree nl, c;
11033 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11034 return list;
11036 nl = c_parser_omp_variable_list (parser, clause_loc,
11037 OMP_CLAUSE_ALIGNED, list);
11039 if (c_parser_next_token_is (parser, CPP_COLON))
11041 c_parser_consume_token (parser);
11042 tree alignment = c_parser_expr_no_commas (parser, NULL).value;
11043 mark_exp_read (alignment);
11044 alignment = c_fully_fold (alignment, false, NULL);
11045 if (!INTEGRAL_TYPE_P (TREE_TYPE (alignment))
11046 && TREE_CODE (alignment) != INTEGER_CST
11047 && tree_int_cst_sgn (alignment) != 1)
11049 error_at (clause_loc, "%<aligned%> clause alignment expression must "
11050 "be positive constant integer expression");
11051 alignment = NULL_TREE;
11054 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
11055 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
11058 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11059 return nl;
11062 /* OpenMP 4.0:
11063 linear ( variable-list )
11064 linear ( variable-list : expression ) */
11066 static tree
11067 c_parser_omp_clause_linear (c_parser *parser, tree list, bool is_cilk_simd_fn)
11069 location_t clause_loc = c_parser_peek_token (parser)->location;
11070 tree nl, c, step;
11072 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11073 return list;
11075 nl = c_parser_omp_variable_list (parser, clause_loc,
11076 OMP_CLAUSE_LINEAR, list);
11078 if (c_parser_next_token_is (parser, CPP_COLON))
11080 c_parser_consume_token (parser);
11081 step = c_parser_expression (parser).value;
11082 mark_exp_read (step);
11083 step = c_fully_fold (step, false, NULL);
11084 if (is_cilk_simd_fn && TREE_CODE (step) == PARM_DECL)
11086 sorry ("using parameters for %<linear%> step is not supported yet");
11087 step = integer_one_node;
11089 if (!INTEGRAL_TYPE_P (TREE_TYPE (step)))
11091 error_at (clause_loc, "%<linear%> clause step expression must "
11092 "be integral");
11093 step = integer_one_node;
11097 else
11098 step = integer_one_node;
11100 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
11102 OMP_CLAUSE_LINEAR_STEP (c) = step;
11105 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11106 return nl;
11109 /* OpenMP 4.0:
11110 safelen ( constant-expression ) */
11112 static tree
11113 c_parser_omp_clause_safelen (c_parser *parser, tree list)
11115 location_t clause_loc = c_parser_peek_token (parser)->location;
11116 tree c, t;
11118 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11119 return list;
11121 t = c_parser_expr_no_commas (parser, NULL).value;
11122 mark_exp_read (t);
11123 t = c_fully_fold (t, false, NULL);
11124 if (!INTEGRAL_TYPE_P (TREE_TYPE (t))
11125 && TREE_CODE (t) != INTEGER_CST
11126 && tree_int_cst_sgn (t) != 1)
11128 error_at (clause_loc, "%<safelen%> clause expression must "
11129 "be positive constant integer expression");
11130 t = NULL_TREE;
11133 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11134 if (t == NULL_TREE || t == error_mark_node)
11135 return list;
11137 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen");
11139 c = build_omp_clause (clause_loc, OMP_CLAUSE_SAFELEN);
11140 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
11141 OMP_CLAUSE_CHAIN (c) = list;
11142 return c;
11145 /* OpenMP 4.0:
11146 simdlen ( constant-expression ) */
11148 static tree
11149 c_parser_omp_clause_simdlen (c_parser *parser, tree list)
11151 location_t clause_loc = c_parser_peek_token (parser)->location;
11152 tree c, t;
11154 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11155 return list;
11157 t = c_parser_expr_no_commas (parser, NULL).value;
11158 mark_exp_read (t);
11159 t = c_fully_fold (t, false, NULL);
11160 if (!INTEGRAL_TYPE_P (TREE_TYPE (t))
11161 && TREE_CODE (t) != INTEGER_CST
11162 && tree_int_cst_sgn (t) != 1)
11164 error_at (clause_loc, "%<simdlen%> clause expression must "
11165 "be positive constant integer expression");
11166 t = NULL_TREE;
11169 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11170 if (t == NULL_TREE || t == error_mark_node)
11171 return list;
11173 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen");
11175 c = build_omp_clause (clause_loc, OMP_CLAUSE_SIMDLEN);
11176 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
11177 OMP_CLAUSE_CHAIN (c) = list;
11178 return c;
11181 /* OpenMP 4.0:
11182 depend ( depend-kind: variable-list )
11184 depend-kind:
11185 in | out | inout */
11187 static tree
11188 c_parser_omp_clause_depend (c_parser *parser, tree list)
11190 location_t clause_loc = c_parser_peek_token (parser)->location;
11191 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_INOUT;
11192 tree nl, c;
11194 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11195 return list;
11197 if (c_parser_next_token_is (parser, CPP_NAME))
11199 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
11200 if (strcmp ("in", p) == 0)
11201 kind = OMP_CLAUSE_DEPEND_IN;
11202 else if (strcmp ("inout", p) == 0)
11203 kind = OMP_CLAUSE_DEPEND_INOUT;
11204 else if (strcmp ("out", p) == 0)
11205 kind = OMP_CLAUSE_DEPEND_OUT;
11206 else
11207 goto invalid_kind;
11209 else
11210 goto invalid_kind;
11212 c_parser_consume_token (parser);
11213 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
11214 goto resync_fail;
11216 nl = c_parser_omp_variable_list (parser, clause_loc,
11217 OMP_CLAUSE_DEPEND, list);
11219 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
11220 OMP_CLAUSE_DEPEND_KIND (c) = kind;
11222 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11223 return nl;
11225 invalid_kind:
11226 c_parser_error (parser, "invalid depend kind");
11227 resync_fail:
11228 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11229 return list;
11232 /* OpenMP 4.0:
11233 map ( map-kind: variable-list )
11234 map ( variable-list )
11236 map-kind:
11237 alloc | to | from | tofrom */
11239 static tree
11240 c_parser_omp_clause_map (c_parser *parser, tree list)
11242 location_t clause_loc = c_parser_peek_token (parser)->location;
11243 enum omp_clause_map_kind kind = OMP_CLAUSE_MAP_TOFROM;
11244 tree nl, c;
11246 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11247 return list;
11249 if (c_parser_next_token_is (parser, CPP_NAME)
11250 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
11252 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
11253 if (strcmp ("alloc", p) == 0)
11254 kind = OMP_CLAUSE_MAP_ALLOC;
11255 else if (strcmp ("to", p) == 0)
11256 kind = OMP_CLAUSE_MAP_TO;
11257 else if (strcmp ("from", p) == 0)
11258 kind = OMP_CLAUSE_MAP_FROM;
11259 else if (strcmp ("tofrom", p) == 0)
11260 kind = OMP_CLAUSE_MAP_TOFROM;
11261 else
11263 c_parser_error (parser, "invalid map kind");
11264 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
11265 "expected %<)%>");
11266 return list;
11268 c_parser_consume_token (parser);
11269 c_parser_consume_token (parser);
11272 nl = c_parser_omp_variable_list (parser, clause_loc, OMP_CLAUSE_MAP, list);
11274 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
11275 OMP_CLAUSE_MAP_KIND (c) = kind;
11277 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11278 return nl;
11281 /* OpenMP 4.0:
11282 device ( expression ) */
11284 static tree
11285 c_parser_omp_clause_device (c_parser *parser, tree list)
11287 location_t clause_loc = c_parser_peek_token (parser)->location;
11288 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11290 tree c, t = c_parser_expr_no_commas (parser, NULL).value;
11291 mark_exp_read (t);
11292 t = c_fully_fold (t, false, NULL);
11294 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11296 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
11298 c_parser_error (parser, "expected integer expression");
11299 return list;
11302 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE, "device");
11304 c = build_omp_clause (clause_loc, OMP_CLAUSE_DEVICE);
11305 OMP_CLAUSE_DEVICE_ID (c) = t;
11306 OMP_CLAUSE_CHAIN (c) = list;
11307 list = c;
11310 return list;
11313 /* OpenMP 4.0:
11314 dist_schedule ( static )
11315 dist_schedule ( static , expression ) */
11317 static tree
11318 c_parser_omp_clause_dist_schedule (c_parser *parser, tree list)
11320 tree c, t = NULL_TREE;
11321 location_t loc = c_parser_peek_token (parser)->location;
11323 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11324 return list;
11326 if (!c_parser_next_token_is_keyword (parser, RID_STATIC))
11328 c_parser_error (parser, "invalid dist_schedule kind");
11329 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
11330 "expected %<)%>");
11331 return list;
11334 c_parser_consume_token (parser);
11335 if (c_parser_next_token_is (parser, CPP_COMMA))
11337 c_parser_consume_token (parser);
11339 t = c_parser_expr_no_commas (parser, NULL).value;
11340 mark_exp_read (t);
11341 t = c_fully_fold (t, false, NULL);
11342 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11344 else
11345 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
11346 "expected %<,%> or %<)%>");
11348 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
11349 if (t == error_mark_node)
11350 return list;
11352 c = build_omp_clause (loc, OMP_CLAUSE_DIST_SCHEDULE);
11353 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
11354 OMP_CLAUSE_CHAIN (c) = list;
11355 return c;
11358 /* OpenMP 4.0:
11359 proc_bind ( proc-bind-kind )
11361 proc-bind-kind:
11362 master | close | spread */
11364 static tree
11365 c_parser_omp_clause_proc_bind (c_parser *parser, tree list)
11367 location_t clause_loc = c_parser_peek_token (parser)->location;
11368 enum omp_clause_proc_bind_kind kind;
11369 tree c;
11371 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11372 return list;
11374 if (c_parser_next_token_is (parser, CPP_NAME))
11376 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
11377 if (strcmp ("master", p) == 0)
11378 kind = OMP_CLAUSE_PROC_BIND_MASTER;
11379 else if (strcmp ("close", p) == 0)
11380 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
11381 else if (strcmp ("spread", p) == 0)
11382 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
11383 else
11384 goto invalid_kind;
11386 else
11387 goto invalid_kind;
11389 c_parser_consume_token (parser);
11390 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11391 c = build_omp_clause (clause_loc, OMP_CLAUSE_PROC_BIND);
11392 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
11393 OMP_CLAUSE_CHAIN (c) = list;
11394 return c;
11396 invalid_kind:
11397 c_parser_error (parser, "invalid proc_bind kind");
11398 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11399 return list;
11402 /* OpenMP 4.0:
11403 to ( variable-list ) */
11405 static tree
11406 c_parser_omp_clause_to (c_parser *parser, tree list)
11408 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_TO, list);
11411 /* OpenMP 4.0:
11412 from ( variable-list ) */
11414 static tree
11415 c_parser_omp_clause_from (c_parser *parser, tree list)
11417 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_FROM, list);
11420 /* OpenMP 4.0:
11421 uniform ( variable-list ) */
11423 static tree
11424 c_parser_omp_clause_uniform (c_parser *parser, tree list)
11426 /* The clauses location. */
11427 location_t loc = c_parser_peek_token (parser)->location;
11429 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11431 list = c_parser_omp_variable_list (parser, loc, OMP_CLAUSE_UNIFORM,
11432 list);
11433 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11435 return list;
11438 /* Parse all OpenMP clauses. The set clauses allowed by the directive
11439 is a bitmask in MASK. Return the list of clauses found; the result
11440 of clause default goes in *pdefault. */
11442 static tree
11443 c_parser_omp_all_clauses (c_parser *parser, omp_clause_mask mask,
11444 const char *where, bool finish_p = true)
11446 tree clauses = NULL;
11447 bool first = true, cilk_simd_fn = false;
11449 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
11451 location_t here;
11452 pragma_omp_clause c_kind;
11453 const char *c_name;
11454 tree prev = clauses;
11456 if (!first && c_parser_next_token_is (parser, CPP_COMMA))
11457 c_parser_consume_token (parser);
11459 here = c_parser_peek_token (parser)->location;
11460 c_kind = c_parser_omp_clause_name (parser);
11462 switch (c_kind)
11464 case PRAGMA_OMP_CLAUSE_COLLAPSE:
11465 clauses = c_parser_omp_clause_collapse (parser, clauses);
11466 c_name = "collapse";
11467 break;
11468 case PRAGMA_OMP_CLAUSE_COPYIN:
11469 clauses = c_parser_omp_clause_copyin (parser, clauses);
11470 c_name = "copyin";
11471 break;
11472 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
11473 clauses = c_parser_omp_clause_copyprivate (parser, clauses);
11474 c_name = "copyprivate";
11475 break;
11476 case PRAGMA_OMP_CLAUSE_DEFAULT:
11477 clauses = c_parser_omp_clause_default (parser, clauses);
11478 c_name = "default";
11479 break;
11480 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
11481 clauses = c_parser_omp_clause_firstprivate (parser, clauses);
11482 c_name = "firstprivate";
11483 break;
11484 case PRAGMA_OMP_CLAUSE_FINAL:
11485 clauses = c_parser_omp_clause_final (parser, clauses);
11486 c_name = "final";
11487 break;
11488 case PRAGMA_OMP_CLAUSE_IF:
11489 clauses = c_parser_omp_clause_if (parser, clauses);
11490 c_name = "if";
11491 break;
11492 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
11493 clauses = c_parser_omp_clause_lastprivate (parser, clauses);
11494 c_name = "lastprivate";
11495 break;
11496 case PRAGMA_OMP_CLAUSE_MERGEABLE:
11497 clauses = c_parser_omp_clause_mergeable (parser, clauses);
11498 c_name = "mergeable";
11499 break;
11500 case PRAGMA_OMP_CLAUSE_NOWAIT:
11501 clauses = c_parser_omp_clause_nowait (parser, clauses);
11502 c_name = "nowait";
11503 break;
11504 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
11505 clauses = c_parser_omp_clause_num_threads (parser, clauses);
11506 c_name = "num_threads";
11507 break;
11508 case PRAGMA_OMP_CLAUSE_ORDERED:
11509 clauses = c_parser_omp_clause_ordered (parser, clauses);
11510 c_name = "ordered";
11511 break;
11512 case PRAGMA_OMP_CLAUSE_PRIVATE:
11513 clauses = c_parser_omp_clause_private (parser, clauses);
11514 c_name = "private";
11515 break;
11516 case PRAGMA_OMP_CLAUSE_REDUCTION:
11517 clauses = c_parser_omp_clause_reduction (parser, clauses);
11518 c_name = "reduction";
11519 break;
11520 case PRAGMA_OMP_CLAUSE_SCHEDULE:
11521 clauses = c_parser_omp_clause_schedule (parser, clauses);
11522 c_name = "schedule";
11523 break;
11524 case PRAGMA_OMP_CLAUSE_SHARED:
11525 clauses = c_parser_omp_clause_shared (parser, clauses);
11526 c_name = "shared";
11527 break;
11528 case PRAGMA_OMP_CLAUSE_UNTIED:
11529 clauses = c_parser_omp_clause_untied (parser, clauses);
11530 c_name = "untied";
11531 break;
11532 case PRAGMA_OMP_CLAUSE_INBRANCH:
11533 case PRAGMA_CILK_CLAUSE_MASK:
11534 clauses = c_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
11535 clauses);
11536 c_name = "inbranch";
11537 break;
11538 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
11539 case PRAGMA_CILK_CLAUSE_NOMASK:
11540 clauses = c_parser_omp_clause_branch (parser, OMP_CLAUSE_NOTINBRANCH,
11541 clauses);
11542 c_name = "notinbranch";
11543 break;
11544 case PRAGMA_OMP_CLAUSE_PARALLEL:
11545 clauses
11546 = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
11547 clauses);
11548 c_name = "parallel";
11549 if (!first)
11551 clause_not_first:
11552 error_at (here, "%qs must be the first clause of %qs",
11553 c_name, where);
11554 clauses = prev;
11556 break;
11557 case PRAGMA_OMP_CLAUSE_FOR:
11558 clauses
11559 = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
11560 clauses);
11561 c_name = "for";
11562 if (!first)
11563 goto clause_not_first;
11564 break;
11565 case PRAGMA_OMP_CLAUSE_SECTIONS:
11566 clauses
11567 = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
11568 clauses);
11569 c_name = "sections";
11570 if (!first)
11571 goto clause_not_first;
11572 break;
11573 case PRAGMA_OMP_CLAUSE_TASKGROUP:
11574 clauses
11575 = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
11576 clauses);
11577 c_name = "taskgroup";
11578 if (!first)
11579 goto clause_not_first;
11580 break;
11581 case PRAGMA_OMP_CLAUSE_TO:
11582 clauses = c_parser_omp_clause_to (parser, clauses);
11583 c_name = "to";
11584 break;
11585 case PRAGMA_OMP_CLAUSE_FROM:
11586 clauses = c_parser_omp_clause_from (parser, clauses);
11587 c_name = "from";
11588 break;
11589 case PRAGMA_OMP_CLAUSE_UNIFORM:
11590 clauses = c_parser_omp_clause_uniform (parser, clauses);
11591 c_name = "uniform";
11592 break;
11593 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
11594 clauses = c_parser_omp_clause_num_teams (parser, clauses);
11595 c_name = "num_teams";
11596 break;
11597 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
11598 clauses = c_parser_omp_clause_thread_limit (parser, clauses);
11599 c_name = "thread_limit";
11600 break;
11601 case PRAGMA_OMP_CLAUSE_ALIGNED:
11602 clauses = c_parser_omp_clause_aligned (parser, clauses);
11603 c_name = "aligned";
11604 break;
11605 case PRAGMA_OMP_CLAUSE_LINEAR:
11606 if (((mask >> PRAGMA_CILK_CLAUSE_VECTORLENGTH) & 1) != 0)
11607 cilk_simd_fn = true;
11608 clauses = c_parser_omp_clause_linear (parser, clauses, cilk_simd_fn);
11609 c_name = "linear";
11610 break;
11611 case PRAGMA_OMP_CLAUSE_DEPEND:
11612 clauses = c_parser_omp_clause_depend (parser, clauses);
11613 c_name = "depend";
11614 break;
11615 case PRAGMA_OMP_CLAUSE_MAP:
11616 clauses = c_parser_omp_clause_map (parser, clauses);
11617 c_name = "map";
11618 break;
11619 case PRAGMA_OMP_CLAUSE_DEVICE:
11620 clauses = c_parser_omp_clause_device (parser, clauses);
11621 c_name = "device";
11622 break;
11623 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
11624 clauses = c_parser_omp_clause_dist_schedule (parser, clauses);
11625 c_name = "dist_schedule";
11626 break;
11627 case PRAGMA_OMP_CLAUSE_PROC_BIND:
11628 clauses = c_parser_omp_clause_proc_bind (parser, clauses);
11629 c_name = "proc_bind";
11630 break;
11631 case PRAGMA_OMP_CLAUSE_SAFELEN:
11632 clauses = c_parser_omp_clause_safelen (parser, clauses);
11633 c_name = "safelen";
11634 break;
11635 case PRAGMA_CILK_CLAUSE_VECTORLENGTH:
11636 clauses = c_parser_cilk_clause_vectorlength (parser, clauses, true);
11637 c_name = "simdlen";
11638 break;
11639 case PRAGMA_OMP_CLAUSE_SIMDLEN:
11640 clauses = c_parser_omp_clause_simdlen (parser, clauses);
11641 c_name = "simdlen";
11642 break;
11643 default:
11644 c_parser_error (parser, "expected %<#pragma omp%> clause");
11645 goto saw_error;
11648 first = false;
11650 if (((mask >> c_kind) & 1) == 0 && !parser->error)
11652 /* Remove the invalid clause(s) from the list to avoid
11653 confusing the rest of the compiler. */
11654 clauses = prev;
11655 error_at (here, "%qs is not valid for %qs", c_name, where);
11659 saw_error:
11660 c_parser_skip_to_pragma_eol (parser);
11662 if (finish_p)
11663 return c_finish_omp_clauses (clauses);
11665 return clauses;
11668 /* OpenMP 2.5:
11669 structured-block:
11670 statement
11672 In practice, we're also interested in adding the statement to an
11673 outer node. So it is convenient if we work around the fact that
11674 c_parser_statement calls add_stmt. */
11676 static tree
11677 c_parser_omp_structured_block (c_parser *parser)
11679 tree stmt = push_stmt_list ();
11680 c_parser_statement (parser);
11681 return pop_stmt_list (stmt);
11684 /* OpenMP 2.5:
11685 # pragma omp atomic new-line
11686 expression-stmt
11688 expression-stmt:
11689 x binop= expr | x++ | ++x | x-- | --x
11690 binop:
11691 +, *, -, /, &, ^, |, <<, >>
11693 where x is an lvalue expression with scalar type.
11695 OpenMP 3.1:
11696 # pragma omp atomic new-line
11697 update-stmt
11699 # pragma omp atomic read new-line
11700 read-stmt
11702 # pragma omp atomic write new-line
11703 write-stmt
11705 # pragma omp atomic update new-line
11706 update-stmt
11708 # pragma omp atomic capture new-line
11709 capture-stmt
11711 # pragma omp atomic capture new-line
11712 capture-block
11714 read-stmt:
11715 v = x
11716 write-stmt:
11717 x = expr
11718 update-stmt:
11719 expression-stmt | x = x binop expr
11720 capture-stmt:
11721 v = expression-stmt
11722 capture-block:
11723 { v = x; update-stmt; } | { update-stmt; v = x; }
11725 OpenMP 4.0:
11726 update-stmt:
11727 expression-stmt | x = x binop expr | x = expr binop x
11728 capture-stmt:
11729 v = update-stmt
11730 capture-block:
11731 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
11733 where x and v are lvalue expressions with scalar type.
11735 LOC is the location of the #pragma token. */
11737 static void
11738 c_parser_omp_atomic (location_t loc, c_parser *parser)
11740 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE;
11741 tree lhs1 = NULL_TREE, rhs1 = NULL_TREE;
11742 tree stmt, orig_lhs, unfolded_lhs = NULL_TREE, unfolded_lhs1 = NULL_TREE;
11743 enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
11744 struct c_expr expr;
11745 location_t eloc;
11746 bool structured_block = false;
11747 bool swapped = false;
11748 bool seq_cst = false;
11750 if (c_parser_next_token_is (parser, CPP_NAME))
11752 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
11754 if (!strcmp (p, "read"))
11755 code = OMP_ATOMIC_READ;
11756 else if (!strcmp (p, "write"))
11757 code = NOP_EXPR;
11758 else if (!strcmp (p, "update"))
11759 code = OMP_ATOMIC;
11760 else if (!strcmp (p, "capture"))
11761 code = OMP_ATOMIC_CAPTURE_NEW;
11762 else
11763 p = NULL;
11764 if (p)
11765 c_parser_consume_token (parser);
11767 if (c_parser_next_token_is (parser, CPP_NAME))
11769 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
11770 if (!strcmp (p, "seq_cst"))
11772 seq_cst = true;
11773 c_parser_consume_token (parser);
11776 c_parser_skip_to_pragma_eol (parser);
11778 switch (code)
11780 case OMP_ATOMIC_READ:
11781 case NOP_EXPR: /* atomic write */
11782 v = c_parser_unary_expression (parser).value;
11783 v = c_fully_fold (v, false, NULL);
11784 if (v == error_mark_node)
11785 goto saw_error;
11786 loc = c_parser_peek_token (parser)->location;
11787 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
11788 goto saw_error;
11789 if (code == NOP_EXPR)
11790 lhs = c_parser_expression (parser).value;
11791 else
11792 lhs = c_parser_unary_expression (parser).value;
11793 lhs = c_fully_fold (lhs, false, NULL);
11794 if (lhs == error_mark_node)
11795 goto saw_error;
11796 if (code == NOP_EXPR)
11798 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
11799 opcode. */
11800 code = OMP_ATOMIC;
11801 rhs = lhs;
11802 lhs = v;
11803 v = NULL_TREE;
11805 goto done;
11806 case OMP_ATOMIC_CAPTURE_NEW:
11807 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
11809 c_parser_consume_token (parser);
11810 structured_block = true;
11812 else
11814 v = c_parser_unary_expression (parser).value;
11815 v = c_fully_fold (v, false, NULL);
11816 if (v == error_mark_node)
11817 goto saw_error;
11818 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
11819 goto saw_error;
11821 break;
11822 default:
11823 break;
11826 /* For structured_block case we don't know yet whether
11827 old or new x should be captured. */
11828 restart:
11829 eloc = c_parser_peek_token (parser)->location;
11830 expr = c_parser_unary_expression (parser);
11831 lhs = expr.value;
11832 expr = default_function_array_conversion (eloc, expr);
11833 unfolded_lhs = expr.value;
11834 lhs = c_fully_fold (lhs, false, NULL);
11835 orig_lhs = lhs;
11836 switch (TREE_CODE (lhs))
11838 case ERROR_MARK:
11839 saw_error:
11840 c_parser_skip_to_end_of_block_or_statement (parser);
11841 if (structured_block)
11843 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
11844 c_parser_consume_token (parser);
11845 else if (code == OMP_ATOMIC_CAPTURE_NEW)
11847 c_parser_skip_to_end_of_block_or_statement (parser);
11848 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
11849 c_parser_consume_token (parser);
11852 return;
11854 case POSTINCREMENT_EXPR:
11855 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
11856 code = OMP_ATOMIC_CAPTURE_OLD;
11857 /* FALLTHROUGH */
11858 case PREINCREMENT_EXPR:
11859 lhs = TREE_OPERAND (lhs, 0);
11860 unfolded_lhs = NULL_TREE;
11861 opcode = PLUS_EXPR;
11862 rhs = integer_one_node;
11863 break;
11865 case POSTDECREMENT_EXPR:
11866 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
11867 code = OMP_ATOMIC_CAPTURE_OLD;
11868 /* FALLTHROUGH */
11869 case PREDECREMENT_EXPR:
11870 lhs = TREE_OPERAND (lhs, 0);
11871 unfolded_lhs = NULL_TREE;
11872 opcode = MINUS_EXPR;
11873 rhs = integer_one_node;
11874 break;
11876 case COMPOUND_EXPR:
11877 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
11878 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
11879 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
11880 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
11881 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
11882 (TREE_OPERAND (lhs, 1), 0), 0)))
11883 == BOOLEAN_TYPE)
11884 /* Undo effects of boolean_increment for post {in,de}crement. */
11885 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
11886 /* FALLTHRU */
11887 case MODIFY_EXPR:
11888 if (TREE_CODE (lhs) == MODIFY_EXPR
11889 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
11891 /* Undo effects of boolean_increment. */
11892 if (integer_onep (TREE_OPERAND (lhs, 1)))
11894 /* This is pre or post increment. */
11895 rhs = TREE_OPERAND (lhs, 1);
11896 lhs = TREE_OPERAND (lhs, 0);
11897 unfolded_lhs = NULL_TREE;
11898 opcode = NOP_EXPR;
11899 if (code == OMP_ATOMIC_CAPTURE_NEW
11900 && !structured_block
11901 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
11902 code = OMP_ATOMIC_CAPTURE_OLD;
11903 break;
11905 if (TREE_CODE (TREE_OPERAND (lhs, 1)) == TRUTH_NOT_EXPR
11906 && TREE_OPERAND (lhs, 0)
11907 == TREE_OPERAND (TREE_OPERAND (lhs, 1), 0))
11909 /* This is pre or post decrement. */
11910 rhs = TREE_OPERAND (lhs, 1);
11911 lhs = TREE_OPERAND (lhs, 0);
11912 unfolded_lhs = NULL_TREE;
11913 opcode = NOP_EXPR;
11914 if (code == OMP_ATOMIC_CAPTURE_NEW
11915 && !structured_block
11916 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
11917 code = OMP_ATOMIC_CAPTURE_OLD;
11918 break;
11921 /* FALLTHRU */
11922 default:
11923 switch (c_parser_peek_token (parser)->type)
11925 case CPP_MULT_EQ:
11926 opcode = MULT_EXPR;
11927 break;
11928 case CPP_DIV_EQ:
11929 opcode = TRUNC_DIV_EXPR;
11930 break;
11931 case CPP_PLUS_EQ:
11932 opcode = PLUS_EXPR;
11933 break;
11934 case CPP_MINUS_EQ:
11935 opcode = MINUS_EXPR;
11936 break;
11937 case CPP_LSHIFT_EQ:
11938 opcode = LSHIFT_EXPR;
11939 break;
11940 case CPP_RSHIFT_EQ:
11941 opcode = RSHIFT_EXPR;
11942 break;
11943 case CPP_AND_EQ:
11944 opcode = BIT_AND_EXPR;
11945 break;
11946 case CPP_OR_EQ:
11947 opcode = BIT_IOR_EXPR;
11948 break;
11949 case CPP_XOR_EQ:
11950 opcode = BIT_XOR_EXPR;
11951 break;
11952 case CPP_EQ:
11953 c_parser_consume_token (parser);
11954 eloc = c_parser_peek_token (parser)->location;
11955 expr = c_parser_expr_no_commas (parser, NULL, unfolded_lhs);
11956 rhs1 = expr.value;
11957 switch (TREE_CODE (rhs1))
11959 case MULT_EXPR:
11960 case TRUNC_DIV_EXPR:
11961 case PLUS_EXPR:
11962 case MINUS_EXPR:
11963 case LSHIFT_EXPR:
11964 case RSHIFT_EXPR:
11965 case BIT_AND_EXPR:
11966 case BIT_IOR_EXPR:
11967 case BIT_XOR_EXPR:
11968 if (c_tree_equal (TREE_OPERAND (rhs1, 0), unfolded_lhs))
11970 opcode = TREE_CODE (rhs1);
11971 rhs = c_fully_fold (TREE_OPERAND (rhs1, 1), false, NULL);
11972 rhs1 = c_fully_fold (TREE_OPERAND (rhs1, 0), false, NULL);
11973 goto stmt_done;
11975 if (c_tree_equal (TREE_OPERAND (rhs1, 1), unfolded_lhs))
11977 opcode = TREE_CODE (rhs1);
11978 rhs = c_fully_fold (TREE_OPERAND (rhs1, 0), false, NULL);
11979 rhs1 = c_fully_fold (TREE_OPERAND (rhs1, 1), false, NULL);
11980 swapped = !commutative_tree_code (opcode);
11981 goto stmt_done;
11983 break;
11984 case ERROR_MARK:
11985 goto saw_error;
11986 default:
11987 break;
11989 if (c_parser_peek_token (parser)->type == CPP_SEMICOLON)
11991 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
11993 code = OMP_ATOMIC_CAPTURE_OLD;
11994 v = lhs;
11995 lhs = NULL_TREE;
11996 expr = default_function_array_read_conversion (eloc, expr);
11997 unfolded_lhs1 = expr.value;
11998 lhs1 = c_fully_fold (unfolded_lhs1, false, NULL);
11999 rhs1 = NULL_TREE;
12000 c_parser_consume_token (parser);
12001 goto restart;
12003 if (structured_block)
12005 opcode = NOP_EXPR;
12006 expr = default_function_array_read_conversion (eloc, expr);
12007 rhs = c_fully_fold (expr.value, false, NULL);
12008 rhs1 = NULL_TREE;
12009 goto stmt_done;
12012 c_parser_error (parser, "invalid form of %<#pragma omp atomic%>");
12013 goto saw_error;
12014 default:
12015 c_parser_error (parser,
12016 "invalid operator for %<#pragma omp atomic%>");
12017 goto saw_error;
12020 /* Arrange to pass the location of the assignment operator to
12021 c_finish_omp_atomic. */
12022 loc = c_parser_peek_token (parser)->location;
12023 c_parser_consume_token (parser);
12024 eloc = c_parser_peek_token (parser)->location;
12025 expr = c_parser_expression (parser);
12026 expr = default_function_array_read_conversion (eloc, expr);
12027 rhs = expr.value;
12028 rhs = c_fully_fold (rhs, false, NULL);
12029 break;
12031 stmt_done:
12032 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
12034 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
12035 goto saw_error;
12036 v = c_parser_unary_expression (parser).value;
12037 v = c_fully_fold (v, false, NULL);
12038 if (v == error_mark_node)
12039 goto saw_error;
12040 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
12041 goto saw_error;
12042 eloc = c_parser_peek_token (parser)->location;
12043 expr = c_parser_unary_expression (parser);
12044 lhs1 = expr.value;
12045 expr = default_function_array_read_conversion (eloc, expr);
12046 unfolded_lhs1 = expr.value;
12047 lhs1 = c_fully_fold (lhs1, false, NULL);
12048 if (lhs1 == error_mark_node)
12049 goto saw_error;
12051 if (structured_block)
12053 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
12054 c_parser_require (parser, CPP_CLOSE_BRACE, "expected %<}%>");
12056 done:
12057 if (unfolded_lhs && unfolded_lhs1
12058 && !c_tree_equal (unfolded_lhs, unfolded_lhs1))
12060 error ("%<#pragma omp atomic capture%> uses two different "
12061 "expressions for memory");
12062 stmt = error_mark_node;
12064 else
12065 stmt = c_finish_omp_atomic (loc, code, opcode, lhs, rhs, v, lhs1, rhs1,
12066 swapped, seq_cst);
12067 if (stmt != error_mark_node)
12068 add_stmt (stmt);
12070 if (!structured_block)
12071 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
12075 /* OpenMP 2.5:
12076 # pragma omp barrier new-line
12079 static void
12080 c_parser_omp_barrier (c_parser *parser)
12082 location_t loc = c_parser_peek_token (parser)->location;
12083 c_parser_consume_pragma (parser);
12084 c_parser_skip_to_pragma_eol (parser);
12086 c_finish_omp_barrier (loc);
12089 /* OpenMP 2.5:
12090 # pragma omp critical [(name)] new-line
12091 structured-block
12093 LOC is the location of the #pragma itself. */
12095 static tree
12096 c_parser_omp_critical (location_t loc, c_parser *parser)
12098 tree stmt, name = NULL;
12100 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
12102 c_parser_consume_token (parser);
12103 if (c_parser_next_token_is (parser, CPP_NAME))
12105 name = c_parser_peek_token (parser)->value;
12106 c_parser_consume_token (parser);
12107 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12109 else
12110 c_parser_error (parser, "expected identifier");
12112 else if (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
12113 c_parser_error (parser, "expected %<(%> or end of line");
12114 c_parser_skip_to_pragma_eol (parser);
12116 stmt = c_parser_omp_structured_block (parser);
12117 return c_finish_omp_critical (loc, stmt, name);
12120 /* OpenMP 2.5:
12121 # pragma omp flush flush-vars[opt] new-line
12123 flush-vars:
12124 ( variable-list ) */
12126 static void
12127 c_parser_omp_flush (c_parser *parser)
12129 location_t loc = c_parser_peek_token (parser)->location;
12130 c_parser_consume_pragma (parser);
12131 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
12132 c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
12133 else if (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
12134 c_parser_error (parser, "expected %<(%> or end of line");
12135 c_parser_skip_to_pragma_eol (parser);
12137 c_finish_omp_flush (loc);
12140 /* Parse the restricted form of the for statement allowed by OpenMP.
12141 The real trick here is to determine the loop control variable early
12142 so that we can push a new decl if necessary to make it private.
12143 LOC is the location of the OMP in "#pragma omp". */
12145 static tree
12146 c_parser_omp_for_loop (location_t loc, c_parser *parser, enum tree_code code,
12147 tree clauses, tree *cclauses)
12149 tree decl, cond, incr, save_break, save_cont, body, init, stmt, cl;
12150 tree declv, condv, incrv, initv, ret = NULL;
12151 bool fail = false, open_brace_parsed = false;
12152 int i, collapse = 1, nbraces = 0;
12153 location_t for_loc;
12154 vec<tree, va_gc> *for_block = make_tree_vector ();
12156 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
12157 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
12158 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
12160 gcc_assert (collapse >= 1);
12162 declv = make_tree_vec (collapse);
12163 initv = make_tree_vec (collapse);
12164 condv = make_tree_vec (collapse);
12165 incrv = make_tree_vec (collapse);
12167 if (!c_parser_next_token_is_keyword (parser, RID_FOR))
12169 c_parser_error (parser, "for statement expected");
12170 return NULL;
12172 for_loc = c_parser_peek_token (parser)->location;
12173 c_parser_consume_token (parser);
12175 for (i = 0; i < collapse; i++)
12177 int bracecount = 0;
12179 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
12180 goto pop_scopes;
12182 /* Parse the initialization declaration or expression. */
12183 if (c_parser_next_tokens_start_declaration (parser))
12185 if (i > 0)
12186 vec_safe_push (for_block, c_begin_compound_stmt (true));
12187 c_parser_declaration_or_fndef (parser, true, true, true, true, true,
12188 NULL, vNULL);
12189 decl = check_for_loop_decls (for_loc, flag_isoc99);
12190 if (decl == NULL)
12191 goto error_init;
12192 if (DECL_INITIAL (decl) == error_mark_node)
12193 decl = error_mark_node;
12194 init = decl;
12196 else if (c_parser_next_token_is (parser, CPP_NAME)
12197 && c_parser_peek_2nd_token (parser)->type == CPP_EQ)
12199 struct c_expr decl_exp;
12200 struct c_expr init_exp;
12201 location_t init_loc;
12203 decl_exp = c_parser_postfix_expression (parser);
12204 decl = decl_exp.value;
12206 c_parser_require (parser, CPP_EQ, "expected %<=%>");
12208 init_loc = c_parser_peek_token (parser)->location;
12209 init_exp = c_parser_expr_no_commas (parser, NULL);
12210 init_exp = default_function_array_read_conversion (init_loc,
12211 init_exp);
12212 init = build_modify_expr (init_loc, decl, decl_exp.original_type,
12213 NOP_EXPR, init_loc, init_exp.value,
12214 init_exp.original_type);
12215 init = c_process_expr_stmt (init_loc, init);
12217 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
12219 else
12221 error_init:
12222 c_parser_error (parser,
12223 "expected iteration declaration or initialization");
12224 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
12225 "expected %<)%>");
12226 fail = true;
12227 goto parse_next;
12230 /* Parse the loop condition. */
12231 cond = NULL_TREE;
12232 if (c_parser_next_token_is_not (parser, CPP_SEMICOLON))
12234 location_t cond_loc = c_parser_peek_token (parser)->location;
12235 struct c_expr cond_expr
12236 = c_parser_binary_expression (parser, NULL, NULL_TREE);
12238 cond = cond_expr.value;
12239 cond = c_objc_common_truthvalue_conversion (cond_loc, cond);
12240 cond = c_fully_fold (cond, false, NULL);
12241 switch (cond_expr.original_code)
12243 case GT_EXPR:
12244 case GE_EXPR:
12245 case LT_EXPR:
12246 case LE_EXPR:
12247 break;
12248 case NE_EXPR:
12249 if (code == CILK_SIMD)
12250 break;
12251 /* FALLTHRU. */
12252 default:
12253 /* Can't be cond = error_mark_node, because we want to preserve
12254 the location until c_finish_omp_for. */
12255 cond = build1 (NOP_EXPR, boolean_type_node, error_mark_node);
12256 break;
12258 protected_set_expr_location (cond, cond_loc);
12260 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
12262 /* Parse the increment expression. */
12263 incr = NULL_TREE;
12264 if (c_parser_next_token_is_not (parser, CPP_CLOSE_PAREN))
12266 location_t incr_loc = c_parser_peek_token (parser)->location;
12268 incr = c_process_expr_stmt (incr_loc,
12269 c_parser_expression (parser).value);
12271 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12273 if (decl == NULL || decl == error_mark_node || init == error_mark_node)
12274 fail = true;
12275 else
12277 TREE_VEC_ELT (declv, i) = decl;
12278 TREE_VEC_ELT (initv, i) = init;
12279 TREE_VEC_ELT (condv, i) = cond;
12280 TREE_VEC_ELT (incrv, i) = incr;
12283 parse_next:
12284 if (i == collapse - 1)
12285 break;
12287 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
12288 in between the collapsed for loops to be still considered perfectly
12289 nested. Hopefully the final version clarifies this.
12290 For now handle (multiple) {'s and empty statements. */
12293 if (c_parser_next_token_is_keyword (parser, RID_FOR))
12295 c_parser_consume_token (parser);
12296 break;
12298 else if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
12300 c_parser_consume_token (parser);
12301 bracecount++;
12303 else if (bracecount
12304 && c_parser_next_token_is (parser, CPP_SEMICOLON))
12305 c_parser_consume_token (parser);
12306 else
12308 c_parser_error (parser, "not enough perfectly nested loops");
12309 if (bracecount)
12311 open_brace_parsed = true;
12312 bracecount--;
12314 fail = true;
12315 collapse = 0;
12316 break;
12319 while (1);
12321 nbraces += bracecount;
12324 save_break = c_break_label;
12325 if (code == CILK_SIMD)
12326 c_break_label = build_int_cst (size_type_node, 2);
12327 else
12328 c_break_label = size_one_node;
12329 save_cont = c_cont_label;
12330 c_cont_label = NULL_TREE;
12331 body = push_stmt_list ();
12333 if (open_brace_parsed)
12335 location_t here = c_parser_peek_token (parser)->location;
12336 stmt = c_begin_compound_stmt (true);
12337 c_parser_compound_statement_nostart (parser);
12338 add_stmt (c_end_compound_stmt (here, stmt, true));
12340 else
12341 add_stmt (c_parser_c99_block_statement (parser));
12342 if (c_cont_label)
12344 tree t = build1 (LABEL_EXPR, void_type_node, c_cont_label);
12345 SET_EXPR_LOCATION (t, loc);
12346 add_stmt (t);
12349 body = pop_stmt_list (body);
12350 c_break_label = save_break;
12351 c_cont_label = save_cont;
12353 while (nbraces)
12355 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
12357 c_parser_consume_token (parser);
12358 nbraces--;
12360 else if (c_parser_next_token_is (parser, CPP_SEMICOLON))
12361 c_parser_consume_token (parser);
12362 else
12364 c_parser_error (parser, "collapsed loops not perfectly nested");
12365 while (nbraces)
12367 location_t here = c_parser_peek_token (parser)->location;
12368 stmt = c_begin_compound_stmt (true);
12369 add_stmt (body);
12370 c_parser_compound_statement_nostart (parser);
12371 body = c_end_compound_stmt (here, stmt, true);
12372 nbraces--;
12374 goto pop_scopes;
12378 /* Only bother calling c_finish_omp_for if we haven't already generated
12379 an error from the initialization parsing. */
12380 if (!fail)
12382 stmt = c_finish_omp_for (loc, code, declv, initv, condv,
12383 incrv, body, NULL);
12384 if (stmt)
12386 if (cclauses != NULL
12387 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL)
12389 tree *c;
12390 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
12391 if (OMP_CLAUSE_CODE (*c) != OMP_CLAUSE_FIRSTPRIVATE
12392 && OMP_CLAUSE_CODE (*c) != OMP_CLAUSE_LASTPRIVATE)
12393 c = &OMP_CLAUSE_CHAIN (*c);
12394 else
12396 for (i = 0; i < collapse; i++)
12397 if (TREE_VEC_ELT (declv, i) == OMP_CLAUSE_DECL (*c))
12398 break;
12399 if (i == collapse)
12400 c = &OMP_CLAUSE_CHAIN (*c);
12401 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE)
12403 error_at (loc,
12404 "iteration variable %qD should not be firstprivate",
12405 OMP_CLAUSE_DECL (*c));
12406 *c = OMP_CLAUSE_CHAIN (*c);
12408 else
12410 /* Copy lastprivate (decl) clause to OMP_FOR_CLAUSES,
12411 change it to shared (decl) in
12412 OMP_PARALLEL_CLAUSES. */
12413 tree l = build_omp_clause (OMP_CLAUSE_LOCATION (*c),
12414 OMP_CLAUSE_LASTPRIVATE);
12415 OMP_CLAUSE_DECL (l) = OMP_CLAUSE_DECL (*c);
12416 OMP_CLAUSE_CHAIN (l) = clauses;
12417 clauses = l;
12418 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
12422 OMP_FOR_CLAUSES (stmt) = clauses;
12424 ret = stmt;
12426 pop_scopes:
12427 while (!for_block->is_empty ())
12429 /* FIXME diagnostics: LOC below should be the actual location of
12430 this particular for block. We need to build a list of
12431 locations to go along with FOR_BLOCK. */
12432 stmt = c_end_compound_stmt (loc, for_block->pop (), true);
12433 add_stmt (stmt);
12435 release_tree_vector (for_block);
12436 return ret;
12439 /* Helper function for OpenMP parsing, split clauses and call
12440 finish_omp_clauses on each of the set of clauses afterwards. */
12442 static void
12443 omp_split_clauses (location_t loc, enum tree_code code,
12444 omp_clause_mask mask, tree clauses, tree *cclauses)
12446 int i;
12447 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
12448 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
12449 if (cclauses[i])
12450 cclauses[i] = c_finish_omp_clauses (cclauses[i]);
12453 /* OpenMP 4.0:
12454 #pragma omp simd simd-clause[optseq] new-line
12455 for-loop
12457 LOC is the location of the #pragma token.
12460 #define OMP_SIMD_CLAUSE_MASK \
12461 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
12462 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
12463 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
12464 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
12465 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
12466 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
12467 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
12469 static tree
12470 c_parser_omp_simd (location_t loc, c_parser *parser,
12471 char *p_name, omp_clause_mask mask, tree *cclauses)
12473 tree block, clauses, ret;
12475 strcat (p_name, " simd");
12476 mask |= OMP_SIMD_CLAUSE_MASK;
12477 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
12479 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
12480 if (cclauses)
12482 omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
12483 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
12486 block = c_begin_compound_stmt (true);
12487 ret = c_parser_omp_for_loop (loc, parser, OMP_SIMD, clauses, cclauses);
12488 block = c_end_compound_stmt (loc, block, true);
12489 add_stmt (block);
12491 return ret;
12494 /* OpenMP 2.5:
12495 #pragma omp for for-clause[optseq] new-line
12496 for-loop
12498 OpenMP 4.0:
12499 #pragma omp for simd for-simd-clause[optseq] new-line
12500 for-loop
12502 LOC is the location of the #pragma token.
12505 #define OMP_FOR_CLAUSE_MASK \
12506 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
12507 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
12508 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
12509 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
12510 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
12511 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
12512 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
12513 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
12515 static tree
12516 c_parser_omp_for (location_t loc, c_parser *parser,
12517 char *p_name, omp_clause_mask mask, tree *cclauses)
12519 tree block, clauses, ret;
12521 strcat (p_name, " for");
12522 mask |= OMP_FOR_CLAUSE_MASK;
12523 if (cclauses)
12524 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
12526 if (c_parser_next_token_is (parser, CPP_NAME))
12528 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
12530 if (strcmp (p, "simd") == 0)
12532 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
12533 if (cclauses == NULL)
12534 cclauses = cclauses_buf;
12536 c_parser_consume_token (parser);
12537 if (!flag_openmp) /* flag_openmp_simd */
12538 return c_parser_omp_simd (loc, parser, p_name, mask, cclauses);
12539 block = c_begin_compound_stmt (true);
12540 ret = c_parser_omp_simd (loc, parser, p_name, mask, cclauses);
12541 block = c_end_compound_stmt (loc, block, true);
12542 if (ret == NULL_TREE)
12543 return ret;
12544 ret = make_node (OMP_FOR);
12545 TREE_TYPE (ret) = void_type_node;
12546 OMP_FOR_BODY (ret) = block;
12547 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
12548 SET_EXPR_LOCATION (ret, loc);
12549 add_stmt (ret);
12550 return ret;
12553 if (!flag_openmp) /* flag_openmp_simd */
12555 c_parser_skip_to_pragma_eol (parser);
12556 return NULL_TREE;
12559 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
12560 if (cclauses)
12562 omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
12563 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
12566 block = c_begin_compound_stmt (true);
12567 ret = c_parser_omp_for_loop (loc, parser, OMP_FOR, clauses, cclauses);
12568 block = c_end_compound_stmt (loc, block, true);
12569 add_stmt (block);
12571 return ret;
12574 /* OpenMP 2.5:
12575 # pragma omp master new-line
12576 structured-block
12578 LOC is the location of the #pragma token.
12581 static tree
12582 c_parser_omp_master (location_t loc, c_parser *parser)
12584 c_parser_skip_to_pragma_eol (parser);
12585 return c_finish_omp_master (loc, c_parser_omp_structured_block (parser));
12588 /* OpenMP 2.5:
12589 # pragma omp ordered new-line
12590 structured-block
12592 LOC is the location of the #pragma itself.
12595 static tree
12596 c_parser_omp_ordered (location_t loc, c_parser *parser)
12598 c_parser_skip_to_pragma_eol (parser);
12599 return c_finish_omp_ordered (loc, c_parser_omp_structured_block (parser));
12602 /* OpenMP 2.5:
12604 section-scope:
12605 { section-sequence }
12607 section-sequence:
12608 section-directive[opt] structured-block
12609 section-sequence section-directive structured-block
12611 SECTIONS_LOC is the location of the #pragma omp sections. */
12613 static tree
12614 c_parser_omp_sections_scope (location_t sections_loc, c_parser *parser)
12616 tree stmt, substmt;
12617 bool error_suppress = false;
12618 location_t loc;
12620 loc = c_parser_peek_token (parser)->location;
12621 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
12623 /* Avoid skipping until the end of the block. */
12624 parser->error = false;
12625 return NULL_TREE;
12628 stmt = push_stmt_list ();
12630 if (c_parser_peek_token (parser)->pragma_kind != PRAGMA_OMP_SECTION)
12632 substmt = c_parser_omp_structured_block (parser);
12633 substmt = build1 (OMP_SECTION, void_type_node, substmt);
12634 SET_EXPR_LOCATION (substmt, loc);
12635 add_stmt (substmt);
12638 while (1)
12640 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
12641 break;
12642 if (c_parser_next_token_is (parser, CPP_EOF))
12643 break;
12645 loc = c_parser_peek_token (parser)->location;
12646 if (c_parser_peek_token (parser)->pragma_kind == PRAGMA_OMP_SECTION)
12648 c_parser_consume_pragma (parser);
12649 c_parser_skip_to_pragma_eol (parser);
12650 error_suppress = false;
12652 else if (!error_suppress)
12654 error_at (loc, "expected %<#pragma omp section%> or %<}%>");
12655 error_suppress = true;
12658 substmt = c_parser_omp_structured_block (parser);
12659 substmt = build1 (OMP_SECTION, void_type_node, substmt);
12660 SET_EXPR_LOCATION (substmt, loc);
12661 add_stmt (substmt);
12663 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE,
12664 "expected %<#pragma omp section%> or %<}%>");
12666 substmt = pop_stmt_list (stmt);
12668 stmt = make_node (OMP_SECTIONS);
12669 SET_EXPR_LOCATION (stmt, sections_loc);
12670 TREE_TYPE (stmt) = void_type_node;
12671 OMP_SECTIONS_BODY (stmt) = substmt;
12673 return add_stmt (stmt);
12676 /* OpenMP 2.5:
12677 # pragma omp sections sections-clause[optseq] newline
12678 sections-scope
12680 LOC is the location of the #pragma token.
12683 #define OMP_SECTIONS_CLAUSE_MASK \
12684 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
12685 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
12686 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
12687 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
12688 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
12690 static tree
12691 c_parser_omp_sections (location_t loc, c_parser *parser,
12692 char *p_name, omp_clause_mask mask, tree *cclauses)
12694 tree block, clauses, ret;
12696 strcat (p_name, " sections");
12697 mask |= OMP_SECTIONS_CLAUSE_MASK;
12698 if (cclauses)
12699 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
12701 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
12702 if (cclauses)
12704 omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
12705 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
12708 block = c_begin_compound_stmt (true);
12709 ret = c_parser_omp_sections_scope (loc, parser);
12710 if (ret)
12711 OMP_SECTIONS_CLAUSES (ret) = clauses;
12712 block = c_end_compound_stmt (loc, block, true);
12713 add_stmt (block);
12715 return ret;
12718 /* OpenMP 2.5:
12719 # pragma omp parallel parallel-clause[optseq] new-line
12720 structured-block
12721 # pragma omp parallel for parallel-for-clause[optseq] new-line
12722 structured-block
12723 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
12724 structured-block
12726 OpenMP 4.0:
12727 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
12728 structured-block
12730 LOC is the location of the #pragma token.
12733 #define OMP_PARALLEL_CLAUSE_MASK \
12734 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
12735 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
12736 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
12737 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
12738 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
12739 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
12740 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
12741 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
12742 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
12744 static tree
12745 c_parser_omp_parallel (location_t loc, c_parser *parser,
12746 char *p_name, omp_clause_mask mask, tree *cclauses)
12748 tree stmt, clauses, block;
12750 strcat (p_name, " parallel");
12751 mask |= OMP_PARALLEL_CLAUSE_MASK;
12753 if (c_parser_next_token_is_keyword (parser, RID_FOR))
12755 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
12756 if (cclauses == NULL)
12757 cclauses = cclauses_buf;
12759 c_parser_consume_token (parser);
12760 if (!flag_openmp) /* flag_openmp_simd */
12761 return c_parser_omp_for (loc, parser, p_name, mask, cclauses);
12762 block = c_begin_omp_parallel ();
12763 c_parser_omp_for (loc, parser, p_name, mask, cclauses);
12764 stmt
12765 = c_finish_omp_parallel (loc, cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
12766 block);
12767 OMP_PARALLEL_COMBINED (stmt) = 1;
12768 return stmt;
12770 else if (cclauses)
12772 error_at (loc, "expected %<for%> after %qs", p_name);
12773 c_parser_skip_to_pragma_eol (parser);
12774 return NULL_TREE;
12776 else if (!flag_openmp) /* flag_openmp_simd */
12778 c_parser_skip_to_pragma_eol (parser);
12779 return NULL_TREE;
12781 else if (c_parser_next_token_is (parser, CPP_NAME))
12783 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
12784 if (strcmp (p, "sections") == 0)
12786 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
12787 if (cclauses == NULL)
12788 cclauses = cclauses_buf;
12790 c_parser_consume_token (parser);
12791 block = c_begin_omp_parallel ();
12792 c_parser_omp_sections (loc, parser, p_name, mask, cclauses);
12793 stmt = c_finish_omp_parallel (loc,
12794 cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
12795 block);
12796 OMP_PARALLEL_COMBINED (stmt) = 1;
12797 return stmt;
12801 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
12803 block = c_begin_omp_parallel ();
12804 c_parser_statement (parser);
12805 stmt = c_finish_omp_parallel (loc, clauses, block);
12807 return stmt;
12810 /* OpenMP 2.5:
12811 # pragma omp single single-clause[optseq] new-line
12812 structured-block
12814 LOC is the location of the #pragma.
12817 #define OMP_SINGLE_CLAUSE_MASK \
12818 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
12819 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
12820 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
12821 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
12823 static tree
12824 c_parser_omp_single (location_t loc, c_parser *parser)
12826 tree stmt = make_node (OMP_SINGLE);
12827 SET_EXPR_LOCATION (stmt, loc);
12828 TREE_TYPE (stmt) = void_type_node;
12830 OMP_SINGLE_CLAUSES (stmt)
12831 = c_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
12832 "#pragma omp single");
12833 OMP_SINGLE_BODY (stmt) = c_parser_omp_structured_block (parser);
12835 return add_stmt (stmt);
12838 /* OpenMP 3.0:
12839 # pragma omp task task-clause[optseq] new-line
12841 LOC is the location of the #pragma.
12844 #define OMP_TASK_CLAUSE_MASK \
12845 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
12846 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
12847 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
12848 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
12849 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
12850 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
12851 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
12852 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
12853 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND))
12855 static tree
12856 c_parser_omp_task (location_t loc, c_parser *parser)
12858 tree clauses, block;
12860 clauses = c_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
12861 "#pragma omp task");
12863 block = c_begin_omp_task ();
12864 c_parser_statement (parser);
12865 return c_finish_omp_task (loc, clauses, block);
12868 /* OpenMP 3.0:
12869 # pragma omp taskwait new-line
12872 static void
12873 c_parser_omp_taskwait (c_parser *parser)
12875 location_t loc = c_parser_peek_token (parser)->location;
12876 c_parser_consume_pragma (parser);
12877 c_parser_skip_to_pragma_eol (parser);
12879 c_finish_omp_taskwait (loc);
12882 /* OpenMP 3.1:
12883 # pragma omp taskyield new-line
12886 static void
12887 c_parser_omp_taskyield (c_parser *parser)
12889 location_t loc = c_parser_peek_token (parser)->location;
12890 c_parser_consume_pragma (parser);
12891 c_parser_skip_to_pragma_eol (parser);
12893 c_finish_omp_taskyield (loc);
12896 /* OpenMP 4.0:
12897 # pragma omp taskgroup new-line
12900 static tree
12901 c_parser_omp_taskgroup (c_parser *parser)
12903 location_t loc = c_parser_peek_token (parser)->location;
12904 c_parser_skip_to_pragma_eol (parser);
12905 return c_finish_omp_taskgroup (loc, c_parser_omp_structured_block (parser));
12908 /* OpenMP 4.0:
12909 # pragma omp cancel cancel-clause[optseq] new-line
12911 LOC is the location of the #pragma.
12914 #define OMP_CANCEL_CLAUSE_MASK \
12915 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
12916 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
12917 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
12918 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
12919 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
12921 static void
12922 c_parser_omp_cancel (c_parser *parser)
12924 location_t loc = c_parser_peek_token (parser)->location;
12926 c_parser_consume_pragma (parser);
12927 tree clauses = c_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
12928 "#pragma omp cancel");
12930 c_finish_omp_cancel (loc, clauses);
12933 /* OpenMP 4.0:
12934 # pragma omp cancellation point cancelpt-clause[optseq] new-line
12936 LOC is the location of the #pragma.
12939 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
12940 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
12941 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
12942 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
12943 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
12945 static void
12946 c_parser_omp_cancellation_point (c_parser *parser)
12948 location_t loc = c_parser_peek_token (parser)->location;
12949 tree clauses;
12950 bool point_seen = false;
12952 c_parser_consume_pragma (parser);
12953 if (c_parser_next_token_is (parser, CPP_NAME))
12955 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
12956 if (strcmp (p, "point") == 0)
12958 c_parser_consume_token (parser);
12959 point_seen = true;
12962 if (!point_seen)
12964 c_parser_error (parser, "expected %<point%>");
12965 c_parser_skip_to_pragma_eol (parser);
12966 return;
12969 clauses
12970 = c_parser_omp_all_clauses (parser, OMP_CANCELLATION_POINT_CLAUSE_MASK,
12971 "#pragma omp cancellation point");
12973 c_finish_omp_cancellation_point (loc, clauses);
12976 /* OpenMP 4.0:
12977 #pragma omp distribute distribute-clause[optseq] new-line
12978 for-loop */
12980 #define OMP_DISTRIBUTE_CLAUSE_MASK \
12981 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
12982 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
12983 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
12984 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
12986 static tree
12987 c_parser_omp_distribute (location_t loc, c_parser *parser,
12988 char *p_name, omp_clause_mask mask, tree *cclauses)
12990 tree clauses, block, ret;
12992 strcat (p_name, " distribute");
12993 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
12995 if (c_parser_next_token_is (parser, CPP_NAME))
12997 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
12998 bool simd = false;
12999 bool parallel = false;
13001 if (strcmp (p, "simd") == 0)
13002 simd = true;
13003 else
13004 parallel = strcmp (p, "parallel") == 0;
13005 if (parallel || simd)
13007 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
13008 if (cclauses == NULL)
13009 cclauses = cclauses_buf;
13010 c_parser_consume_token (parser);
13011 if (!flag_openmp) /* flag_openmp_simd */
13013 if (simd)
13014 return c_parser_omp_simd (loc, parser, p_name, mask, cclauses);
13015 else
13016 return c_parser_omp_parallel (loc, parser, p_name, mask,
13017 cclauses);
13019 block = c_begin_compound_stmt (true);
13020 if (simd)
13021 ret = c_parser_omp_simd (loc, parser, p_name, mask, cclauses);
13022 else
13023 ret = c_parser_omp_parallel (loc, parser, p_name, mask, cclauses);
13024 block = c_end_compound_stmt (loc, block, true);
13025 if (ret == NULL)
13026 return ret;
13027 ret = make_node (OMP_DISTRIBUTE);
13028 TREE_TYPE (ret) = void_type_node;
13029 OMP_FOR_BODY (ret) = block;
13030 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
13031 SET_EXPR_LOCATION (ret, loc);
13032 add_stmt (ret);
13033 return ret;
13036 if (!flag_openmp) /* flag_openmp_simd */
13038 c_parser_skip_to_pragma_eol (parser);
13039 return NULL_TREE;
13042 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
13043 if (cclauses)
13045 omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
13046 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
13049 block = c_begin_compound_stmt (true);
13050 ret = c_parser_omp_for_loop (loc, parser, OMP_DISTRIBUTE, clauses, NULL);
13051 block = c_end_compound_stmt (loc, block, true);
13052 add_stmt (block);
13054 return ret;
13057 /* OpenMP 4.0:
13058 # pragma omp teams teams-clause[optseq] new-line
13059 structured-block */
13061 #define OMP_TEAMS_CLAUSE_MASK \
13062 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
13063 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
13064 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
13065 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
13066 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
13067 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
13068 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
13070 static tree
13071 c_parser_omp_teams (location_t loc, c_parser *parser,
13072 char *p_name, omp_clause_mask mask, tree *cclauses)
13074 tree clauses, block, ret;
13076 strcat (p_name, " teams");
13077 mask |= OMP_TEAMS_CLAUSE_MASK;
13079 if (c_parser_next_token_is (parser, CPP_NAME))
13081 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
13082 if (strcmp (p, "distribute") == 0)
13084 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
13085 if (cclauses == NULL)
13086 cclauses = cclauses_buf;
13088 c_parser_consume_token (parser);
13089 if (!flag_openmp) /* flag_openmp_simd */
13090 return c_parser_omp_distribute (loc, parser, p_name, mask, cclauses);
13091 block = c_begin_compound_stmt (true);
13092 ret = c_parser_omp_distribute (loc, parser, p_name, mask, cclauses);
13093 block = c_end_compound_stmt (loc, block, true);
13094 if (ret == NULL)
13095 return ret;
13096 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
13097 ret = make_node (OMP_TEAMS);
13098 TREE_TYPE (ret) = void_type_node;
13099 OMP_TEAMS_CLAUSES (ret) = clauses;
13100 OMP_TEAMS_BODY (ret) = block;
13101 return add_stmt (ret);
13104 if (!flag_openmp) /* flag_openmp_simd */
13106 c_parser_skip_to_pragma_eol (parser);
13107 return NULL_TREE;
13110 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
13111 if (cclauses)
13113 omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
13114 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
13117 tree stmt = make_node (OMP_TEAMS);
13118 TREE_TYPE (stmt) = void_type_node;
13119 OMP_TEAMS_CLAUSES (stmt) = clauses;
13120 OMP_TEAMS_BODY (stmt) = c_parser_omp_structured_block (parser);
13122 return add_stmt (stmt);
13125 /* OpenMP 4.0:
13126 # pragma omp target data target-data-clause[optseq] new-line
13127 structured-block */
13129 #define OMP_TARGET_DATA_CLAUSE_MASK \
13130 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
13131 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
13132 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
13134 static tree
13135 c_parser_omp_target_data (location_t loc, c_parser *parser)
13137 tree stmt = make_node (OMP_TARGET_DATA);
13138 TREE_TYPE (stmt) = void_type_node;
13140 OMP_TARGET_DATA_CLAUSES (stmt)
13141 = c_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
13142 "#pragma omp target data");
13143 keep_next_level ();
13144 tree block = c_begin_compound_stmt (true);
13145 add_stmt (c_parser_omp_structured_block (parser));
13146 OMP_TARGET_DATA_BODY (stmt) = c_end_compound_stmt (loc, block, true);
13148 SET_EXPR_LOCATION (stmt, loc);
13149 return add_stmt (stmt);
13152 /* OpenMP 4.0:
13153 # pragma omp target update target-update-clause[optseq] new-line */
13155 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
13156 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
13157 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
13158 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
13159 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
13161 static bool
13162 c_parser_omp_target_update (location_t loc, c_parser *parser,
13163 enum pragma_context context)
13165 if (context == pragma_stmt)
13167 error_at (loc,
13168 "%<#pragma omp target update%> may only be "
13169 "used in compound statements");
13170 c_parser_skip_to_pragma_eol (parser);
13171 return false;
13174 tree clauses
13175 = c_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
13176 "#pragma omp target update");
13177 if (find_omp_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
13178 && find_omp_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
13180 error_at (loc,
13181 "%<#pragma omp target update must contain at least one "
13182 "%<from%> or %<to%> clauses");
13183 return false;
13186 tree stmt = make_node (OMP_TARGET_UPDATE);
13187 TREE_TYPE (stmt) = void_type_node;
13188 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
13189 SET_EXPR_LOCATION (stmt, loc);
13190 add_stmt (stmt);
13191 return false;
13194 /* OpenMP 4.0:
13195 # pragma omp target target-clause[optseq] new-line
13196 structured-block */
13198 #define OMP_TARGET_CLAUSE_MASK \
13199 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
13200 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
13201 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
13203 static bool
13204 c_parser_omp_target (c_parser *parser, enum pragma_context context)
13206 location_t loc = c_parser_peek_token (parser)->location;
13207 c_parser_consume_pragma (parser);
13209 if (context != pragma_stmt && context != pragma_compound)
13211 c_parser_error (parser, "expected declaration specifiers");
13212 c_parser_skip_to_pragma_eol (parser);
13213 return false;
13216 if (c_parser_next_token_is (parser, CPP_NAME))
13218 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
13220 if (strcmp (p, "teams") == 0)
13222 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
13223 char p_name[sizeof ("#pragma omp target teams distribute "
13224 "parallel for simd")];
13226 c_parser_consume_token (parser);
13227 strcpy (p_name, "#pragma omp target");
13228 if (!flag_openmp) /* flag_openmp_simd */
13229 return c_parser_omp_teams (loc, parser, p_name,
13230 OMP_TARGET_CLAUSE_MASK, cclauses);
13231 keep_next_level ();
13232 tree block = c_begin_compound_stmt (true);
13233 tree ret = c_parser_omp_teams (loc, parser, p_name,
13234 OMP_TARGET_CLAUSE_MASK, cclauses);
13235 block = c_end_compound_stmt (loc, block, true);
13236 if (ret == NULL)
13237 return ret;
13238 tree stmt = make_node (OMP_TARGET);
13239 TREE_TYPE (stmt) = void_type_node;
13240 OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
13241 OMP_TARGET_BODY (stmt) = block;
13242 add_stmt (stmt);
13243 return true;
13245 else if (!flag_openmp) /* flag_openmp_simd */
13247 c_parser_skip_to_pragma_eol (parser);
13248 return NULL_TREE;
13250 else if (strcmp (p, "data") == 0)
13252 c_parser_consume_token (parser);
13253 c_parser_omp_target_data (loc, parser);
13254 return true;
13256 else if (strcmp (p, "update") == 0)
13258 c_parser_consume_token (parser);
13259 return c_parser_omp_target_update (loc, parser, context);
13263 tree stmt = make_node (OMP_TARGET);
13264 TREE_TYPE (stmt) = void_type_node;
13266 OMP_TARGET_CLAUSES (stmt)
13267 = c_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
13268 "#pragma omp target");
13269 keep_next_level ();
13270 tree block = c_begin_compound_stmt (true);
13271 add_stmt (c_parser_omp_structured_block (parser));
13272 OMP_TARGET_BODY (stmt) = c_end_compound_stmt (loc, block, true);
13274 SET_EXPR_LOCATION (stmt, loc);
13275 add_stmt (stmt);
13276 return true;
13279 /* OpenMP 4.0:
13280 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
13282 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
13283 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
13284 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
13285 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
13286 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
13287 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
13288 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
13290 static void
13291 c_parser_omp_declare_simd (c_parser *parser, enum pragma_context context)
13293 vec<c_token> clauses = vNULL;
13294 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
13296 c_token *token = c_parser_peek_token (parser);
13297 if (token->type == CPP_EOF)
13299 c_parser_skip_to_pragma_eol (parser);
13300 clauses.release ();
13301 return;
13303 clauses.safe_push (*token);
13304 c_parser_consume_token (parser);
13306 clauses.safe_push (*c_parser_peek_token (parser));
13307 c_parser_skip_to_pragma_eol (parser);
13309 while (c_parser_next_token_is (parser, CPP_PRAGMA))
13311 if (c_parser_peek_token (parser)->pragma_kind
13312 != PRAGMA_OMP_DECLARE_REDUCTION
13313 || c_parser_peek_2nd_token (parser)->type != CPP_NAME
13314 || strcmp (IDENTIFIER_POINTER
13315 (c_parser_peek_2nd_token (parser)->value),
13316 "simd") != 0)
13318 c_parser_error (parser,
13319 "%<#pragma omp declare simd%> must be followed by "
13320 "function declaration or definition or another "
13321 "%<#pragma omp declare simd%>");
13322 clauses.release ();
13323 return;
13325 c_parser_consume_pragma (parser);
13326 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
13328 c_token *token = c_parser_peek_token (parser);
13329 if (token->type == CPP_EOF)
13331 c_parser_skip_to_pragma_eol (parser);
13332 clauses.release ();
13333 return;
13335 clauses.safe_push (*token);
13336 c_parser_consume_token (parser);
13338 clauses.safe_push (*c_parser_peek_token (parser));
13339 c_parser_skip_to_pragma_eol (parser);
13342 /* Make sure nothing tries to read past the end of the tokens. */
13343 c_token eof_token;
13344 memset (&eof_token, 0, sizeof (eof_token));
13345 eof_token.type = CPP_EOF;
13346 clauses.safe_push (eof_token);
13347 clauses.safe_push (eof_token);
13349 switch (context)
13351 case pragma_external:
13352 if (c_parser_next_token_is (parser, CPP_KEYWORD)
13353 && c_parser_peek_token (parser)->keyword == RID_EXTENSION)
13355 int ext = disable_extension_diagnostics ();
13357 c_parser_consume_token (parser);
13358 while (c_parser_next_token_is (parser, CPP_KEYWORD)
13359 && c_parser_peek_token (parser)->keyword == RID_EXTENSION);
13360 c_parser_declaration_or_fndef (parser, true, true, true, false, true,
13361 NULL, clauses);
13362 restore_extension_diagnostics (ext);
13364 else
13365 c_parser_declaration_or_fndef (parser, true, true, true, false, true,
13366 NULL, clauses);
13367 break;
13368 case pragma_struct:
13369 case pragma_param:
13370 c_parser_error (parser, "%<#pragma omp declare simd%> must be followed by "
13371 "function declaration or definition");
13372 break;
13373 case pragma_compound:
13374 case pragma_stmt:
13375 if (c_parser_next_token_is (parser, CPP_KEYWORD)
13376 && c_parser_peek_token (parser)->keyword == RID_EXTENSION)
13378 int ext = disable_extension_diagnostics ();
13380 c_parser_consume_token (parser);
13381 while (c_parser_next_token_is (parser, CPP_KEYWORD)
13382 && c_parser_peek_token (parser)->keyword == RID_EXTENSION);
13383 if (c_parser_next_tokens_start_declaration (parser))
13385 c_parser_declaration_or_fndef (parser, true, true, true, true,
13386 true, NULL, clauses);
13387 restore_extension_diagnostics (ext);
13388 break;
13390 restore_extension_diagnostics (ext);
13392 else if (c_parser_next_tokens_start_declaration (parser))
13394 c_parser_declaration_or_fndef (parser, true, true, true, true, true,
13395 NULL, clauses);
13396 break;
13398 c_parser_error (parser, "%<#pragma omp declare simd%> must be followed by "
13399 "function declaration or definition");
13400 break;
13401 default:
13402 gcc_unreachable ();
13404 clauses.release ();
13407 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
13408 and put that into "omp declare simd" attribute. */
13410 static void
13411 c_finish_omp_declare_simd (c_parser *parser, tree fndecl, tree parms,
13412 vec<c_token> clauses)
13414 if (flag_cilkplus
13415 && clauses.exists () && !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
13417 error ("%<#pragma omp declare simd%> cannot be used in the same "
13418 "function marked as a Cilk Plus SIMD-enabled function");
13419 vec_free (parser->cilk_simd_fn_tokens);
13420 return;
13423 /* Normally first token is CPP_NAME "simd". CPP_EOF there indicates
13424 error has been reported and CPP_PRAGMA that c_finish_omp_declare_simd
13425 has already processed the tokens. */
13426 if (clauses.exists () && clauses[0].type == CPP_EOF)
13427 return;
13428 if (fndecl == NULL_TREE || TREE_CODE (fndecl) != FUNCTION_DECL)
13430 error ("%<#pragma omp declare simd%> not immediately followed by "
13431 "a function declaration or definition");
13432 clauses[0].type = CPP_EOF;
13433 return;
13435 if (clauses.exists () && clauses[0].type != CPP_NAME)
13437 error_at (DECL_SOURCE_LOCATION (fndecl),
13438 "%<#pragma omp declare simd%> not immediately followed by "
13439 "a single function declaration or definition");
13440 clauses[0].type = CPP_EOF;
13441 return;
13444 if (parms == NULL_TREE)
13445 parms = DECL_ARGUMENTS (fndecl);
13447 unsigned int tokens_avail = parser->tokens_avail;
13448 gcc_assert (parser->tokens == &parser->tokens_buf[0]);
13449 bool is_cilkplus_cilk_simd_fn = false;
13451 if (flag_cilkplus && !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
13453 parser->tokens = parser->cilk_simd_fn_tokens->address ();
13454 parser->tokens_avail = vec_safe_length (parser->cilk_simd_fn_tokens);
13455 is_cilkplus_cilk_simd_fn = true;
13457 else
13459 parser->tokens = clauses.address ();
13460 parser->tokens_avail = clauses.length ();
13463 /* c_parser_omp_declare_simd pushed 2 extra CPP_EOF tokens at the end. */
13464 while (parser->tokens_avail > 3)
13466 c_token *token = c_parser_peek_token (parser);
13467 if (!is_cilkplus_cilk_simd_fn)
13468 gcc_assert (token->type == CPP_NAME
13469 && strcmp (IDENTIFIER_POINTER (token->value), "simd") == 0);
13470 else
13471 gcc_assert (token->type == CPP_NAME
13472 && is_cilkplus_vector_p (token->value));
13473 c_parser_consume_token (parser);
13474 parser->in_pragma = true;
13476 tree c = NULL_TREE;
13477 if (is_cilkplus_cilk_simd_fn)
13478 c = c_parser_omp_all_clauses (parser, CILK_SIMD_FN_CLAUSE_MASK,
13479 "SIMD-enabled functions attribute");
13480 else
13481 c = c_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
13482 "#pragma omp declare simd");
13483 c = c_omp_declare_simd_clauses_to_numbers (parms, c);
13484 if (c != NULL_TREE)
13485 c = tree_cons (NULL_TREE, c, NULL_TREE);
13486 if (is_cilkplus_cilk_simd_fn)
13488 tree k = build_tree_list (get_identifier ("cilk simd function"),
13489 NULL_TREE);
13490 TREE_CHAIN (k) = DECL_ATTRIBUTES (fndecl);
13491 DECL_ATTRIBUTES (fndecl) = k;
13493 c = build_tree_list (get_identifier ("omp declare simd"), c);
13494 TREE_CHAIN (c) = DECL_ATTRIBUTES (fndecl);
13495 DECL_ATTRIBUTES (fndecl) = c;
13498 parser->tokens = &parser->tokens_buf[0];
13499 parser->tokens_avail = tokens_avail;
13500 if (clauses.exists ())
13501 clauses[0].type = CPP_PRAGMA;
13503 if (!vec_safe_is_empty (parser->cilk_simd_fn_tokens))
13504 vec_free (parser->cilk_simd_fn_tokens);
13508 /* OpenMP 4.0:
13509 # pragma omp declare target new-line
13510 declarations and definitions
13511 # pragma omp end declare target new-line */
13513 static void
13514 c_parser_omp_declare_target (c_parser *parser)
13516 c_parser_skip_to_pragma_eol (parser);
13517 current_omp_declare_target_attribute++;
13520 static void
13521 c_parser_omp_end_declare_target (c_parser *parser)
13523 location_t loc = c_parser_peek_token (parser)->location;
13524 c_parser_consume_pragma (parser);
13525 if (c_parser_next_token_is (parser, CPP_NAME)
13526 && strcmp (IDENTIFIER_POINTER (c_parser_peek_token (parser)->value),
13527 "declare") == 0)
13529 c_parser_consume_token (parser);
13530 if (c_parser_next_token_is (parser, CPP_NAME)
13531 && strcmp (IDENTIFIER_POINTER (c_parser_peek_token (parser)->value),
13532 "target") == 0)
13533 c_parser_consume_token (parser);
13534 else
13536 c_parser_error (parser, "expected %<target%>");
13537 c_parser_skip_to_pragma_eol (parser);
13538 return;
13541 else
13543 c_parser_error (parser, "expected %<declare%>");
13544 c_parser_skip_to_pragma_eol (parser);
13545 return;
13547 c_parser_skip_to_pragma_eol (parser);
13548 if (!current_omp_declare_target_attribute)
13549 error_at (loc, "%<#pragma omp end declare target%> without corresponding "
13550 "%<#pragma omp declare target%>");
13551 else
13552 current_omp_declare_target_attribute--;
13556 /* OpenMP 4.0
13557 #pragma omp declare reduction (reduction-id : typename-list : expression) \
13558 initializer-clause[opt] new-line
13560 initializer-clause:
13561 initializer (omp_priv = initializer)
13562 initializer (function-name (argument-list)) */
13564 static void
13565 c_parser_omp_declare_reduction (c_parser *parser, enum pragma_context context)
13567 unsigned int tokens_avail = 0, i;
13568 vec<tree> types = vNULL;
13569 vec<c_token> clauses = vNULL;
13570 enum tree_code reduc_code = ERROR_MARK;
13571 tree reduc_id = NULL_TREE;
13572 tree type;
13573 location_t rloc = c_parser_peek_token (parser)->location;
13575 if (context == pragma_struct || context == pragma_param)
13577 error ("%<#pragma omp declare reduction%> not at file or block scope");
13578 goto fail;
13581 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
13582 goto fail;
13584 switch (c_parser_peek_token (parser)->type)
13586 case CPP_PLUS:
13587 reduc_code = PLUS_EXPR;
13588 break;
13589 case CPP_MULT:
13590 reduc_code = MULT_EXPR;
13591 break;
13592 case CPP_MINUS:
13593 reduc_code = MINUS_EXPR;
13594 break;
13595 case CPP_AND:
13596 reduc_code = BIT_AND_EXPR;
13597 break;
13598 case CPP_XOR:
13599 reduc_code = BIT_XOR_EXPR;
13600 break;
13601 case CPP_OR:
13602 reduc_code = BIT_IOR_EXPR;
13603 break;
13604 case CPP_AND_AND:
13605 reduc_code = TRUTH_ANDIF_EXPR;
13606 break;
13607 case CPP_OR_OR:
13608 reduc_code = TRUTH_ORIF_EXPR;
13609 break;
13610 case CPP_NAME:
13611 const char *p;
13612 p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
13613 if (strcmp (p, "min") == 0)
13615 reduc_code = MIN_EXPR;
13616 break;
13618 if (strcmp (p, "max") == 0)
13620 reduc_code = MAX_EXPR;
13621 break;
13623 reduc_id = c_parser_peek_token (parser)->value;
13624 break;
13625 default:
13626 c_parser_error (parser,
13627 "expected %<+%>, %<*%>, %<-%>, %<&%>, "
13628 "%<^%>, %<|%>, %<&&%>, %<||%>, %<min%> or identifier");
13629 goto fail;
13632 tree orig_reduc_id, reduc_decl;
13633 orig_reduc_id = reduc_id;
13634 reduc_id = c_omp_reduction_id (reduc_code, reduc_id);
13635 reduc_decl = c_omp_reduction_decl (reduc_id);
13636 c_parser_consume_token (parser);
13638 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
13639 goto fail;
13641 while (true)
13643 location_t loc = c_parser_peek_token (parser)->location;
13644 struct c_type_name *ctype = c_parser_type_name (parser);
13645 if (ctype != NULL)
13647 type = groktypename (ctype, NULL, NULL);
13648 if (type == error_mark_node)
13650 else if ((INTEGRAL_TYPE_P (type)
13651 || TREE_CODE (type) == REAL_TYPE
13652 || TREE_CODE (type) == COMPLEX_TYPE)
13653 && orig_reduc_id == NULL_TREE)
13654 error_at (loc, "predeclared arithmetic type in "
13655 "%<#pragma omp declare reduction%>");
13656 else if (TREE_CODE (type) == FUNCTION_TYPE
13657 || TREE_CODE (type) == ARRAY_TYPE)
13658 error_at (loc, "function or array type in "
13659 "%<#pragma omp declare reduction%>");
13660 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
13661 error_at (loc, "const, volatile or restrict qualified type in "
13662 "%<#pragma omp declare reduction%>");
13663 else
13665 tree t;
13666 for (t = DECL_INITIAL (reduc_decl); t; t = TREE_CHAIN (t))
13667 if (comptypes (TREE_PURPOSE (t), type))
13669 error_at (loc, "redeclaration of %qs "
13670 "%<#pragma omp declare reduction%> for "
13671 "type %qT",
13672 IDENTIFIER_POINTER (reduc_id)
13673 + sizeof ("omp declare reduction ") - 1,
13674 type);
13675 location_t ploc
13676 = DECL_SOURCE_LOCATION (TREE_VEC_ELT (TREE_VALUE (t),
13677 0));
13678 error_at (ploc, "previous %<#pragma omp declare "
13679 "reduction%>");
13680 break;
13682 if (t == NULL_TREE)
13683 types.safe_push (type);
13685 if (c_parser_next_token_is (parser, CPP_COMMA))
13686 c_parser_consume_token (parser);
13687 else
13688 break;
13690 else
13691 break;
13694 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>")
13695 || types.is_empty ())
13697 fail:
13698 clauses.release ();
13699 types.release ();
13700 while (true)
13702 c_token *token = c_parser_peek_token (parser);
13703 if (token->type == CPP_EOF || token->type == CPP_PRAGMA_EOL)
13704 break;
13705 c_parser_consume_token (parser);
13707 c_parser_skip_to_pragma_eol (parser);
13708 return;
13711 if (types.length () > 1)
13713 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
13715 c_token *token = c_parser_peek_token (parser);
13716 if (token->type == CPP_EOF)
13717 goto fail;
13718 clauses.safe_push (*token);
13719 c_parser_consume_token (parser);
13721 clauses.safe_push (*c_parser_peek_token (parser));
13722 c_parser_skip_to_pragma_eol (parser);
13724 /* Make sure nothing tries to read past the end of the tokens. */
13725 c_token eof_token;
13726 memset (&eof_token, 0, sizeof (eof_token));
13727 eof_token.type = CPP_EOF;
13728 clauses.safe_push (eof_token);
13729 clauses.safe_push (eof_token);
13732 int errs = errorcount;
13733 FOR_EACH_VEC_ELT (types, i, type)
13735 tokens_avail = parser->tokens_avail;
13736 gcc_assert (parser->tokens == &parser->tokens_buf[0]);
13737 if (!clauses.is_empty ())
13739 parser->tokens = clauses.address ();
13740 parser->tokens_avail = clauses.length ();
13741 parser->in_pragma = true;
13744 bool nested = current_function_decl != NULL_TREE;
13745 if (nested)
13746 c_push_function_context ();
13747 tree fndecl = build_decl (BUILTINS_LOCATION, FUNCTION_DECL,
13748 reduc_id, default_function_type);
13749 current_function_decl = fndecl;
13750 allocate_struct_function (fndecl, true);
13751 push_scope ();
13752 tree stmt = push_stmt_list ();
13753 /* Intentionally BUILTINS_LOCATION, so that -Wshadow doesn't
13754 warn about these. */
13755 tree omp_out = build_decl (BUILTINS_LOCATION, VAR_DECL,
13756 get_identifier ("omp_out"), type);
13757 DECL_ARTIFICIAL (omp_out) = 1;
13758 DECL_CONTEXT (omp_out) = fndecl;
13759 pushdecl (omp_out);
13760 tree omp_in = build_decl (BUILTINS_LOCATION, VAR_DECL,
13761 get_identifier ("omp_in"), type);
13762 DECL_ARTIFICIAL (omp_in) = 1;
13763 DECL_CONTEXT (omp_in) = fndecl;
13764 pushdecl (omp_in);
13765 struct c_expr combiner = c_parser_expression (parser);
13766 struct c_expr initializer;
13767 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE;
13768 bool bad = false;
13769 initializer.value = error_mark_node;
13770 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
13771 bad = true;
13772 else if (c_parser_next_token_is (parser, CPP_NAME)
13773 && strcmp (IDENTIFIER_POINTER
13774 (c_parser_peek_token (parser)->value),
13775 "initializer") == 0)
13777 c_parser_consume_token (parser);
13778 pop_scope ();
13779 push_scope ();
13780 omp_priv = build_decl (BUILTINS_LOCATION, VAR_DECL,
13781 get_identifier ("omp_priv"), type);
13782 DECL_ARTIFICIAL (omp_priv) = 1;
13783 DECL_INITIAL (omp_priv) = error_mark_node;
13784 DECL_CONTEXT (omp_priv) = fndecl;
13785 pushdecl (omp_priv);
13786 omp_orig = build_decl (BUILTINS_LOCATION, VAR_DECL,
13787 get_identifier ("omp_orig"), type);
13788 DECL_ARTIFICIAL (omp_orig) = 1;
13789 DECL_CONTEXT (omp_orig) = fndecl;
13790 pushdecl (omp_orig);
13791 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
13792 bad = true;
13793 else if (!c_parser_next_token_is (parser, CPP_NAME))
13795 c_parser_error (parser, "expected %<omp_priv%> or "
13796 "function-name");
13797 bad = true;
13799 else if (strcmp (IDENTIFIER_POINTER
13800 (c_parser_peek_token (parser)->value),
13801 "omp_priv") != 0)
13803 if (c_parser_peek_2nd_token (parser)->type != CPP_OPEN_PAREN
13804 || c_parser_peek_token (parser)->id_kind != C_ID_ID)
13806 c_parser_error (parser, "expected function-name %<(%>");
13807 bad = true;
13809 else
13810 initializer = c_parser_postfix_expression (parser);
13811 if (initializer.value
13812 && TREE_CODE (initializer.value) == CALL_EXPR)
13814 int j;
13815 tree c = initializer.value;
13816 for (j = 0; j < call_expr_nargs (c); j++)
13817 if (TREE_CODE (CALL_EXPR_ARG (c, j)) == ADDR_EXPR
13818 && TREE_OPERAND (CALL_EXPR_ARG (c, j), 0) == omp_priv)
13819 break;
13820 if (j == call_expr_nargs (c))
13821 error ("one of the initializer call arguments should be "
13822 "%<&omp_priv%>");
13825 else
13827 c_parser_consume_token (parser);
13828 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
13829 bad = true;
13830 else
13832 tree st = push_stmt_list ();
13833 start_init (omp_priv, NULL_TREE, 0);
13834 location_t loc = c_parser_peek_token (parser)->location;
13835 struct c_expr init = c_parser_initializer (parser);
13836 finish_init ();
13837 finish_decl (omp_priv, loc, init.value,
13838 init.original_type, NULL_TREE);
13839 pop_stmt_list (st);
13842 if (!bad
13843 && !c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
13844 bad = true;
13847 if (!bad)
13849 c_parser_skip_to_pragma_eol (parser);
13851 tree t = tree_cons (type, make_tree_vec (omp_priv ? 6 : 3),
13852 DECL_INITIAL (reduc_decl));
13853 DECL_INITIAL (reduc_decl) = t;
13854 DECL_SOURCE_LOCATION (omp_out) = rloc;
13855 TREE_VEC_ELT (TREE_VALUE (t), 0) = omp_out;
13856 TREE_VEC_ELT (TREE_VALUE (t), 1) = omp_in;
13857 TREE_VEC_ELT (TREE_VALUE (t), 2) = combiner.value;
13858 walk_tree (&combiner.value, c_check_omp_declare_reduction_r,
13859 &TREE_VEC_ELT (TREE_VALUE (t), 0), NULL);
13860 if (omp_priv)
13862 DECL_SOURCE_LOCATION (omp_priv) = rloc;
13863 TREE_VEC_ELT (TREE_VALUE (t), 3) = omp_priv;
13864 TREE_VEC_ELT (TREE_VALUE (t), 4) = omp_orig;
13865 TREE_VEC_ELT (TREE_VALUE (t), 5) = initializer.value;
13866 walk_tree (&initializer.value, c_check_omp_declare_reduction_r,
13867 &TREE_VEC_ELT (TREE_VALUE (t), 3), NULL);
13868 walk_tree (&DECL_INITIAL (omp_priv),
13869 c_check_omp_declare_reduction_r,
13870 &TREE_VEC_ELT (TREE_VALUE (t), 3), NULL);
13874 pop_stmt_list (stmt);
13875 pop_scope ();
13876 if (cfun->language != NULL)
13878 ggc_free (cfun->language);
13879 cfun->language = NULL;
13881 set_cfun (NULL);
13882 current_function_decl = NULL_TREE;
13883 if (nested)
13884 c_pop_function_context ();
13886 if (!clauses.is_empty ())
13888 parser->tokens = &parser->tokens_buf[0];
13889 parser->tokens_avail = tokens_avail;
13891 if (bad)
13892 goto fail;
13893 if (errs != errorcount)
13894 break;
13897 clauses.release ();
13898 types.release ();
13902 /* OpenMP 4.0
13903 #pragma omp declare simd declare-simd-clauses[optseq] new-line
13904 #pragma omp declare reduction (reduction-id : typename-list : expression) \
13905 initializer-clause[opt] new-line
13906 #pragma omp declare target new-line */
13908 static void
13909 c_parser_omp_declare (c_parser *parser, enum pragma_context context)
13911 c_parser_consume_pragma (parser);
13912 if (c_parser_next_token_is (parser, CPP_NAME))
13914 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
13915 if (strcmp (p, "simd") == 0)
13917 /* c_parser_consume_token (parser); done in
13918 c_parser_omp_declare_simd. */
13919 c_parser_omp_declare_simd (parser, context);
13920 return;
13922 if (strcmp (p, "reduction") == 0)
13924 c_parser_consume_token (parser);
13925 c_parser_omp_declare_reduction (parser, context);
13926 return;
13928 if (!flag_openmp) /* flag_openmp_simd */
13930 c_parser_skip_to_pragma_eol (parser);
13931 return;
13933 if (strcmp (p, "target") == 0)
13935 c_parser_consume_token (parser);
13936 c_parser_omp_declare_target (parser);
13937 return;
13941 c_parser_error (parser, "expected %<simd%> or %<reduction%> "
13942 "or %<target%>");
13943 c_parser_skip_to_pragma_eol (parser);
13946 /* Main entry point to parsing most OpenMP pragmas. */
13948 static void
13949 c_parser_omp_construct (c_parser *parser)
13951 enum pragma_kind p_kind;
13952 location_t loc;
13953 tree stmt;
13954 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
13955 omp_clause_mask mask (0);
13957 loc = c_parser_peek_token (parser)->location;
13958 p_kind = c_parser_peek_token (parser)->pragma_kind;
13959 c_parser_consume_pragma (parser);
13961 switch (p_kind)
13963 case PRAGMA_OMP_ATOMIC:
13964 c_parser_omp_atomic (loc, parser);
13965 return;
13966 case PRAGMA_OMP_CRITICAL:
13967 stmt = c_parser_omp_critical (loc, parser);
13968 break;
13969 case PRAGMA_OMP_DISTRIBUTE:
13970 strcpy (p_name, "#pragma omp");
13971 stmt = c_parser_omp_distribute (loc, parser, p_name, mask, NULL);
13972 break;
13973 case PRAGMA_OMP_FOR:
13974 strcpy (p_name, "#pragma omp");
13975 stmt = c_parser_omp_for (loc, parser, p_name, mask, NULL);
13976 break;
13977 case PRAGMA_OMP_MASTER:
13978 stmt = c_parser_omp_master (loc, parser);
13979 break;
13980 case PRAGMA_OMP_ORDERED:
13981 stmt = c_parser_omp_ordered (loc, parser);
13982 break;
13983 case PRAGMA_OMP_PARALLEL:
13984 strcpy (p_name, "#pragma omp");
13985 stmt = c_parser_omp_parallel (loc, parser, p_name, mask, NULL);
13986 break;
13987 case PRAGMA_OMP_SECTIONS:
13988 strcpy (p_name, "#pragma omp");
13989 stmt = c_parser_omp_sections (loc, parser, p_name, mask, NULL);
13990 break;
13991 case PRAGMA_OMP_SIMD:
13992 strcpy (p_name, "#pragma omp");
13993 stmt = c_parser_omp_simd (loc, parser, p_name, mask, NULL);
13994 break;
13995 case PRAGMA_OMP_SINGLE:
13996 stmt = c_parser_omp_single (loc, parser);
13997 break;
13998 case PRAGMA_OMP_TASK:
13999 stmt = c_parser_omp_task (loc, parser);
14000 break;
14001 case PRAGMA_OMP_TASKGROUP:
14002 stmt = c_parser_omp_taskgroup (parser);
14003 break;
14004 case PRAGMA_OMP_TEAMS:
14005 strcpy (p_name, "#pragma omp");
14006 stmt = c_parser_omp_teams (loc, parser, p_name, mask, NULL);
14007 break;
14008 default:
14009 gcc_unreachable ();
14012 if (stmt)
14013 gcc_assert (EXPR_LOCATION (stmt) != UNKNOWN_LOCATION);
14017 /* OpenMP 2.5:
14018 # pragma omp threadprivate (variable-list) */
14020 static void
14021 c_parser_omp_threadprivate (c_parser *parser)
14023 tree vars, t;
14024 location_t loc;
14026 c_parser_consume_pragma (parser);
14027 loc = c_parser_peek_token (parser)->location;
14028 vars = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
14030 /* Mark every variable in VARS to be assigned thread local storage. */
14031 for (t = vars; t; t = TREE_CHAIN (t))
14033 tree v = TREE_PURPOSE (t);
14035 /* FIXME diagnostics: Ideally we should keep individual
14036 locations for all the variables in the var list to make the
14037 following errors more precise. Perhaps
14038 c_parser_omp_var_list_parens() should construct a list of
14039 locations to go along with the var list. */
14041 /* If V had already been marked threadprivate, it doesn't matter
14042 whether it had been used prior to this point. */
14043 if (TREE_CODE (v) != VAR_DECL)
14044 error_at (loc, "%qD is not a variable", v);
14045 else if (TREE_USED (v) && !C_DECL_THREADPRIVATE_P (v))
14046 error_at (loc, "%qE declared %<threadprivate%> after first use", v);
14047 else if (! TREE_STATIC (v) && ! DECL_EXTERNAL (v))
14048 error_at (loc, "automatic variable %qE cannot be %<threadprivate%>", v);
14049 else if (TREE_TYPE (v) == error_mark_node)
14051 else if (! COMPLETE_TYPE_P (TREE_TYPE (v)))
14052 error_at (loc, "%<threadprivate%> %qE has incomplete type", v);
14053 else
14055 if (! DECL_THREAD_LOCAL_P (v))
14057 DECL_TLS_MODEL (v) = decl_default_tls_model (v);
14058 /* If rtl has been already set for this var, call
14059 make_decl_rtl once again, so that encode_section_info
14060 has a chance to look at the new decl flags. */
14061 if (DECL_RTL_SET_P (v))
14062 make_decl_rtl (v);
14064 C_DECL_THREADPRIVATE_P (v) = 1;
14068 c_parser_skip_to_pragma_eol (parser);
14071 /* Cilk Plus <#pragma simd> parsing routines. */
14073 /* Helper function for c_parser_pragma. Perform some sanity checking
14074 for <#pragma simd> constructs. Returns FALSE if there was a
14075 problem. */
14077 static bool
14078 c_parser_cilk_verify_simd (c_parser *parser,
14079 enum pragma_context context)
14081 if (!flag_cilkplus)
14083 warning (0, "pragma simd ignored because -fcilkplus is not enabled");
14084 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
14085 return false;
14087 if (context == pragma_external)
14089 c_parser_error (parser,"pragma simd must be inside a function");
14090 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
14091 return false;
14093 return true;
14096 /* Cilk Plus:
14097 This function is shared by SIMD-enabled functions and #pragma simd.
14098 If IS_SIMD_FN is true then it is parsing a SIMD-enabled function and
14099 CLAUSES is unused. The main purpose of this function is to parse a
14100 vectorlength attribute or clause and check for parse errors.
14101 When IS_SIMD_FN is true then the function is merely caching the tokens
14102 in PARSER->CILK_SIMD_FN_TOKENS. If errors are found then the token
14103 cache is cleared since there is no reason to continue.
14104 Syntax:
14105 vectorlength ( constant-expression ) */
14107 static tree
14108 c_parser_cilk_clause_vectorlength (c_parser *parser, tree clauses,
14109 bool is_simd_fn)
14111 if (is_simd_fn)
14112 check_no_duplicate_clause (clauses, OMP_CLAUSE_SIMDLEN, "vectorlength");
14113 else
14114 /* The vectorlength clause behaves exactly like OpenMP's safelen
14115 clause. Represent it in OpenMP terms. */
14116 check_no_duplicate_clause (clauses, OMP_CLAUSE_SAFELEN, "vectorlength");
14118 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
14119 return clauses;
14121 location_t loc = c_parser_peek_token (parser)->location;
14122 tree expr = c_parser_expr_no_commas (parser, NULL).value;
14123 expr = c_fully_fold (expr, false, NULL);
14125 /* If expr is an error_mark_node then the above function would have
14126 emitted an error. No reason to do it twice. */
14127 if (expr == error_mark_node)
14129 else if (!TREE_TYPE (expr)
14130 || !TREE_CONSTANT (expr)
14131 || !INTEGRAL_TYPE_P (TREE_TYPE (expr)))
14133 error_at (loc, "vectorlength must be an integer constant");
14134 else if (exact_log2 (TREE_INT_CST_LOW (expr)) == -1)
14135 error_at (loc, "vectorlength must be a power of 2");
14136 else
14138 if (is_simd_fn)
14140 tree u = build_omp_clause (loc, OMP_CLAUSE_SIMDLEN);
14141 OMP_CLAUSE_SIMDLEN_EXPR (u) = expr;
14142 OMP_CLAUSE_CHAIN (u) = clauses;
14143 clauses = u;
14145 else
14147 tree u = build_omp_clause (loc, OMP_CLAUSE_SAFELEN);
14148 OMP_CLAUSE_SAFELEN_EXPR (u) = expr;
14149 OMP_CLAUSE_CHAIN (u) = clauses;
14150 clauses = u;
14154 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
14156 return clauses;
14159 /* Cilk Plus:
14160 linear ( simd-linear-variable-list )
14162 simd-linear-variable-list:
14163 simd-linear-variable
14164 simd-linear-variable-list , simd-linear-variable
14166 simd-linear-variable:
14167 id-expression
14168 id-expression : simd-linear-step
14170 simd-linear-step:
14171 conditional-expression */
14173 static tree
14174 c_parser_cilk_clause_linear (c_parser *parser, tree clauses)
14176 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
14177 return clauses;
14179 location_t loc = c_parser_peek_token (parser)->location;
14181 if (c_parser_next_token_is_not (parser, CPP_NAME)
14182 || c_parser_peek_token (parser)->id_kind != C_ID_ID)
14183 c_parser_error (parser, "expected identifier");
14185 while (c_parser_next_token_is (parser, CPP_NAME)
14186 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
14188 tree var = lookup_name (c_parser_peek_token (parser)->value);
14190 if (var == NULL)
14192 undeclared_variable (c_parser_peek_token (parser)->location,
14193 c_parser_peek_token (parser)->value);
14194 c_parser_consume_token (parser);
14196 else if (var == error_mark_node)
14197 c_parser_consume_token (parser);
14198 else
14200 tree step = integer_one_node;
14202 /* Parse the linear step if present. */
14203 if (c_parser_peek_2nd_token (parser)->type == CPP_COLON)
14205 c_parser_consume_token (parser);
14206 c_parser_consume_token (parser);
14208 tree expr = c_parser_expr_no_commas (parser, NULL).value;
14209 expr = c_fully_fold (expr, false, NULL);
14211 if (TREE_TYPE (expr)
14212 && INTEGRAL_TYPE_P (TREE_TYPE (expr))
14213 && (TREE_CONSTANT (expr)
14214 || DECL_P (expr)))
14215 step = expr;
14216 else
14217 c_parser_error (parser,
14218 "step size must be an integer constant "
14219 "expression or an integer variable");
14221 else
14222 c_parser_consume_token (parser);
14224 /* Use OMP_CLAUSE_LINEAR, which has the same semantics. */
14225 tree u = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
14226 OMP_CLAUSE_DECL (u) = var;
14227 OMP_CLAUSE_LINEAR_STEP (u) = step;
14228 OMP_CLAUSE_CHAIN (u) = clauses;
14229 clauses = u;
14232 if (c_parser_next_token_is_not (parser, CPP_COMMA))
14233 break;
14235 c_parser_consume_token (parser);
14238 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
14240 return clauses;
14243 /* Returns the name of the next clause. If the clause is not
14244 recognized SIMD_OMP_CLAUSE_NONE is returned and the next token is
14245 not consumed. Otherwise, the appropriate pragma_simd_clause is
14246 returned and the token is consumed. */
14248 static pragma_omp_clause
14249 c_parser_cilk_clause_name (c_parser *parser)
14251 pragma_omp_clause result;
14252 c_token *token = c_parser_peek_token (parser);
14254 if (!token->value || token->type != CPP_NAME)
14255 return PRAGMA_CILK_CLAUSE_NONE;
14257 const char *p = IDENTIFIER_POINTER (token->value);
14259 if (!strcmp (p, "vectorlength"))
14260 result = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
14261 else if (!strcmp (p, "linear"))
14262 result = PRAGMA_CILK_CLAUSE_LINEAR;
14263 else if (!strcmp (p, "private"))
14264 result = PRAGMA_CILK_CLAUSE_PRIVATE;
14265 else if (!strcmp (p, "firstprivate"))
14266 result = PRAGMA_CILK_CLAUSE_FIRSTPRIVATE;
14267 else if (!strcmp (p, "lastprivate"))
14268 result = PRAGMA_CILK_CLAUSE_LASTPRIVATE;
14269 else if (!strcmp (p, "reduction"))
14270 result = PRAGMA_CILK_CLAUSE_REDUCTION;
14271 else
14272 return PRAGMA_CILK_CLAUSE_NONE;
14274 c_parser_consume_token (parser);
14275 return result;
14278 /* Parse all #<pragma simd> clauses. Return the list of clauses
14279 found. */
14281 static tree
14282 c_parser_cilk_all_clauses (c_parser *parser)
14284 tree clauses = NULL;
14286 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
14288 pragma_omp_clause c_kind;
14290 c_kind = c_parser_cilk_clause_name (parser);
14292 switch (c_kind)
14294 case PRAGMA_CILK_CLAUSE_VECTORLENGTH:
14295 clauses = c_parser_cilk_clause_vectorlength (parser, clauses, false);
14296 break;
14297 case PRAGMA_CILK_CLAUSE_LINEAR:
14298 clauses = c_parser_cilk_clause_linear (parser, clauses);
14299 break;
14300 case PRAGMA_CILK_CLAUSE_PRIVATE:
14301 /* Use the OpenMP counterpart. */
14302 clauses = c_parser_omp_clause_private (parser, clauses);
14303 break;
14304 case PRAGMA_CILK_CLAUSE_FIRSTPRIVATE:
14305 /* Use the OpenMP counterpart. */
14306 clauses = c_parser_omp_clause_firstprivate (parser, clauses);
14307 break;
14308 case PRAGMA_CILK_CLAUSE_LASTPRIVATE:
14309 /* Use the OpenMP counterpart. */
14310 clauses = c_parser_omp_clause_lastprivate (parser, clauses);
14311 break;
14312 case PRAGMA_CILK_CLAUSE_REDUCTION:
14313 /* Use the OpenMP counterpart. */
14314 clauses = c_parser_omp_clause_reduction (parser, clauses);
14315 break;
14316 default:
14317 c_parser_error (parser, "expected %<#pragma simd%> clause");
14318 goto saw_error;
14322 saw_error:
14323 c_parser_skip_to_pragma_eol (parser);
14324 return c_finish_cilk_clauses (clauses);
14327 /* Main entry point for parsing Cilk Plus <#pragma simd> for
14328 loops. */
14330 static void
14331 c_parser_cilk_simd (c_parser *parser)
14333 tree clauses = c_parser_cilk_all_clauses (parser);
14334 tree block = c_begin_compound_stmt (true);
14335 location_t loc = c_parser_peek_token (parser)->location;
14336 c_parser_omp_for_loop (loc, parser, CILK_SIMD, clauses, NULL);
14337 block = c_end_compound_stmt (loc, block, true);
14338 add_stmt (block);
14341 /* Parse a transaction attribute (GCC Extension).
14343 transaction-attribute:
14344 attributes
14345 [ [ any-word ] ]
14347 The transactional memory language description is written for C++,
14348 and uses the C++0x attribute syntax. For compatibility, allow the
14349 bracket style for transactions in C as well. */
14351 static tree
14352 c_parser_transaction_attributes (c_parser *parser)
14354 tree attr_name, attr = NULL;
14356 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
14357 return c_parser_attributes (parser);
14359 if (!c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
14360 return NULL_TREE;
14361 c_parser_consume_token (parser);
14362 if (!c_parser_require (parser, CPP_OPEN_SQUARE, "expected %<[%>"))
14363 goto error1;
14365 attr_name = c_parser_attribute_any_word (parser);
14366 if (attr_name)
14368 c_parser_consume_token (parser);
14369 attr = build_tree_list (attr_name, NULL_TREE);
14371 else
14372 c_parser_error (parser, "expected identifier");
14374 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, "expected %<]%>");
14375 error1:
14376 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, "expected %<]%>");
14377 return attr;
14380 /* Parse a __transaction_atomic or __transaction_relaxed statement
14381 (GCC Extension).
14383 transaction-statement:
14384 __transaction_atomic transaction-attribute[opt] compound-statement
14385 __transaction_relaxed compound-statement
14387 Note that the only valid attribute is: "outer".
14390 static tree
14391 c_parser_transaction (c_parser *parser, enum rid keyword)
14393 unsigned int old_in = parser->in_transaction;
14394 unsigned int this_in = 1, new_in;
14395 location_t loc = c_parser_peek_token (parser)->location;
14396 tree stmt, attrs;
14398 gcc_assert ((keyword == RID_TRANSACTION_ATOMIC
14399 || keyword == RID_TRANSACTION_RELAXED)
14400 && c_parser_next_token_is_keyword (parser, keyword));
14401 c_parser_consume_token (parser);
14403 if (keyword == RID_TRANSACTION_RELAXED)
14404 this_in |= TM_STMT_ATTR_RELAXED;
14405 else
14407 attrs = c_parser_transaction_attributes (parser);
14408 if (attrs)
14409 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
14412 /* Keep track if we're in the lexical scope of an outer transaction. */
14413 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
14415 parser->in_transaction = new_in;
14416 stmt = c_parser_compound_statement (parser);
14417 parser->in_transaction = old_in;
14419 if (flag_tm)
14420 stmt = c_finish_transaction (loc, stmt, this_in);
14421 else
14422 error_at (loc, (keyword == RID_TRANSACTION_ATOMIC ?
14423 "%<__transaction_atomic%> without transactional memory support enabled"
14424 : "%<__transaction_relaxed %> "
14425 "without transactional memory support enabled"));
14427 return stmt;
14430 /* Parse a __transaction_atomic or __transaction_relaxed expression
14431 (GCC Extension).
14433 transaction-expression:
14434 __transaction_atomic ( expression )
14435 __transaction_relaxed ( expression )
14438 static struct c_expr
14439 c_parser_transaction_expression (c_parser *parser, enum rid keyword)
14441 struct c_expr ret;
14442 unsigned int old_in = parser->in_transaction;
14443 unsigned int this_in = 1;
14444 location_t loc = c_parser_peek_token (parser)->location;
14445 tree attrs;
14447 gcc_assert ((keyword == RID_TRANSACTION_ATOMIC
14448 || keyword == RID_TRANSACTION_RELAXED)
14449 && c_parser_next_token_is_keyword (parser, keyword));
14450 c_parser_consume_token (parser);
14452 if (keyword == RID_TRANSACTION_RELAXED)
14453 this_in |= TM_STMT_ATTR_RELAXED;
14454 else
14456 attrs = c_parser_transaction_attributes (parser);
14457 if (attrs)
14458 this_in |= parse_tm_stmt_attr (attrs, 0);
14461 parser->in_transaction = this_in;
14462 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
14464 tree expr = c_parser_expression (parser).value;
14465 ret.original_type = TREE_TYPE (expr);
14466 ret.value = build1 (TRANSACTION_EXPR, ret.original_type, expr);
14467 if (this_in & TM_STMT_ATTR_RELAXED)
14468 TRANSACTION_EXPR_RELAXED (ret.value) = 1;
14469 SET_EXPR_LOCATION (ret.value, loc);
14470 ret.original_code = TRANSACTION_EXPR;
14471 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
14473 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
14474 goto error;
14477 else
14479 error:
14480 ret.value = error_mark_node;
14481 ret.original_code = ERROR_MARK;
14482 ret.original_type = NULL;
14484 parser->in_transaction = old_in;
14486 if (!flag_tm)
14487 error_at (loc, (keyword == RID_TRANSACTION_ATOMIC ?
14488 "%<__transaction_atomic%> without transactional memory support enabled"
14489 : "%<__transaction_relaxed %> "
14490 "without transactional memory support enabled"));
14492 return ret;
14495 /* Parse a __transaction_cancel statement (GCC Extension).
14497 transaction-cancel-statement:
14498 __transaction_cancel transaction-attribute[opt] ;
14500 Note that the only valid attribute is "outer".
14503 static tree
14504 c_parser_transaction_cancel (c_parser *parser)
14506 location_t loc = c_parser_peek_token (parser)->location;
14507 tree attrs;
14508 bool is_outer = false;
14510 gcc_assert (c_parser_next_token_is_keyword (parser, RID_TRANSACTION_CANCEL));
14511 c_parser_consume_token (parser);
14513 attrs = c_parser_transaction_attributes (parser);
14514 if (attrs)
14515 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
14517 if (!flag_tm)
14519 error_at (loc, "%<__transaction_cancel%> without "
14520 "transactional memory support enabled");
14521 goto ret_error;
14523 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
14525 error_at (loc, "%<__transaction_cancel%> within a "
14526 "%<__transaction_relaxed%>");
14527 goto ret_error;
14529 else if (is_outer)
14531 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
14532 && !is_tm_may_cancel_outer (current_function_decl))
14534 error_at (loc, "outer %<__transaction_cancel%> not "
14535 "within outer %<__transaction_atomic%>");
14536 error_at (loc, " or a %<transaction_may_cancel_outer%> function");
14537 goto ret_error;
14540 else if (parser->in_transaction == 0)
14542 error_at (loc, "%<__transaction_cancel%> not within "
14543 "%<__transaction_atomic%>");
14544 goto ret_error;
14547 return add_stmt (build_tm_abort_call (loc, is_outer));
14549 ret_error:
14550 return build1 (NOP_EXPR, void_type_node, error_mark_node);
14553 /* Parse a single source file. */
14555 void
14556 c_parse_file (void)
14558 /* Use local storage to begin. If the first token is a pragma, parse it.
14559 If it is #pragma GCC pch_preprocess, then this will load a PCH file
14560 which will cause garbage collection. */
14561 c_parser tparser;
14563 memset (&tparser, 0, sizeof tparser);
14564 tparser.tokens = &tparser.tokens_buf[0];
14565 the_parser = &tparser;
14567 if (c_parser_peek_token (&tparser)->pragma_kind == PRAGMA_GCC_PCH_PREPROCESS)
14568 c_parser_pragma_pch_preprocess (&tparser);
14570 the_parser = ggc_alloc_c_parser ();
14571 *the_parser = tparser;
14572 if (tparser.tokens == &tparser.tokens_buf[0])
14573 the_parser->tokens = &the_parser->tokens_buf[0];
14575 /* Initialize EH, if we've been told to do so. */
14576 if (flag_exceptions)
14577 using_eh_for_cleanups ();
14579 c_parser_translation_unit (the_parser);
14580 the_parser = NULL;
14583 /* This function parses Cilk Plus array notation. The starting index is
14584 passed in INITIAL_INDEX and the array name is passes in ARRAY_VALUE. The
14585 return value of this function is a tree_node called VALUE_TREE of type
14586 ARRAY_NOTATION_REF. */
14588 static tree
14589 c_parser_array_notation (location_t loc, c_parser *parser, tree initial_index,
14590 tree array_value)
14592 c_token *token = NULL;
14593 tree start_index = NULL_TREE, end_index = NULL_TREE, stride = NULL_TREE;
14594 tree value_tree = NULL_TREE, type = NULL_TREE, array_type = NULL_TREE;
14595 tree array_type_domain = NULL_TREE;
14597 if (array_value == error_mark_node)
14599 /* No need to continue. If either of these 2 were true, then an error
14600 must be emitted already. Thus, no need to emit them twice. */
14601 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
14602 return error_mark_node;
14605 array_type = TREE_TYPE (array_value);
14606 gcc_assert (array_type);
14607 type = TREE_TYPE (array_type);
14608 token = c_parser_peek_token (parser);
14610 if (token->type == CPP_EOF)
14612 c_parser_error (parser, "expected %<:%> or numeral");
14613 return value_tree;
14615 else if (token->type == CPP_COLON)
14617 if (!initial_index)
14619 /* If we are here, then we have a case like this A[:]. */
14620 c_parser_consume_token (parser);
14621 if (TREE_CODE (array_type) == POINTER_TYPE)
14623 error_at (loc, "start-index and length fields necessary for "
14624 "using array notations in pointers");
14625 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
14626 return error_mark_node;
14628 if (TREE_CODE (array_type) == FUNCTION_TYPE)
14630 error_at (loc, "array notations cannot be used with function "
14631 "type");
14632 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
14633 return error_mark_node;
14635 array_type_domain = TYPE_DOMAIN (array_type);
14637 if (!array_type_domain)
14639 error_at (loc, "start-index and length fields necessary for "
14640 "using array notations in dimensionless arrays");
14641 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
14642 return error_mark_node;
14645 start_index = TYPE_MINVAL (array_type_domain);
14646 start_index = fold_build1 (CONVERT_EXPR, ptrdiff_type_node,
14647 start_index);
14648 if (!TYPE_MAXVAL (array_type_domain)
14649 || !TREE_CONSTANT (TYPE_MAXVAL (array_type_domain)))
14651 error_at (loc, "start-index and length fields necessary for "
14652 "using array notations in variable-length arrays");
14653 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
14654 return error_mark_node;
14656 end_index = TYPE_MAXVAL (array_type_domain);
14657 end_index = fold_build2 (PLUS_EXPR, TREE_TYPE (end_index),
14658 end_index, integer_one_node);
14659 end_index = fold_build1 (CONVERT_EXPR, ptrdiff_type_node, end_index);
14660 stride = build_int_cst (integer_type_node, 1);
14661 stride = fold_build1 (CONVERT_EXPR, ptrdiff_type_node, stride);
14663 else if (initial_index != error_mark_node)
14665 /* If we are here, then there should be 2 possibilities:
14666 1. Array [EXPR : EXPR]
14667 2. Array [EXPR : EXPR : EXPR]
14669 start_index = initial_index;
14671 if (TREE_CODE (array_type) == FUNCTION_TYPE)
14673 error_at (loc, "array notations cannot be used with function "
14674 "type");
14675 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
14676 return error_mark_node;
14678 c_parser_consume_token (parser); /* consume the ':' */
14679 struct c_expr ce = c_parser_expression (parser);
14680 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
14681 end_index = ce.value;
14682 if (!end_index || end_index == error_mark_node)
14684 c_parser_skip_to_end_of_block_or_statement (parser);
14685 return error_mark_node;
14687 if (c_parser_peek_token (parser)->type == CPP_COLON)
14689 c_parser_consume_token (parser);
14690 ce = c_parser_expression (parser);
14691 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
14692 stride = ce.value;
14693 if (!stride || stride == error_mark_node)
14695 c_parser_skip_to_end_of_block_or_statement (parser);
14696 return error_mark_node;
14700 else
14701 c_parser_error (parser, "expected array notation expression");
14703 else
14704 c_parser_error (parser, "expected array notation expression");
14706 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, "expected %<]%>");
14708 value_tree = build_array_notation_ref (loc, array_value, start_index,
14709 end_index, stride, type);
14710 if (value_tree != error_mark_node)
14711 SET_EXPR_LOCATION (value_tree, loc);
14712 return value_tree;
14715 #include "gt-c-c-parser.h"