]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/c/c-parser.c
Factor unrelated declarations out of tree.h.
[thirdparty/gcc.git] / gcc / c / c-parser.c
1 /* Parser for C and Objective-C.
2 Copyright (C) 1987-2013 Free Software Foundation, Inc.
3
4 Parser actions based on the old Bison parser; structure somewhat
5 influenced by and fragments based on the C++ parser.
6
7 This file is part of GCC.
8
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.
13
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.
18
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/>. */
22
23 /* TODO:
24
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.
28
29 Add testcases covering every input symbol in every state in old and
30 new parsers.
31
32 Include full syntax for GNU C, including erroneous cases accepted
33 with error messages, in syntax productions in comments.
34
35 Make more diagnostics in the front end generally take an explicit
36 location rather than implicitly using input_location. */
37
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 "vec.h"
60 #include "target.h"
61 #include "cgraph.h"
62 #include "plugin.h"
63 #include "omp-low.h"
64
65 \f
66 /* Initialization routine for this file. */
67
68 void
69 c_parse_init (void)
70 {
71 /* The only initialization required is of the reserved word
72 identifiers. */
73 unsigned int i;
74 tree id;
75 int mask = 0;
76
77 /* Make sure RID_MAX hasn't grown past the 8 bits used to hold the keyword in
78 the c_token structure. */
79 gcc_assert (RID_MAX <= 255);
80
81 mask |= D_CXXONLY;
82 if (!flag_isoc99)
83 mask |= D_C99;
84 if (flag_no_asm)
85 {
86 mask |= D_ASM | D_EXT;
87 if (!flag_isoc99)
88 mask |= D_EXT89;
89 }
90 if (!c_dialect_objc ())
91 mask |= D_OBJC | D_CXX_OBJC;
92
93 ridpointers = ggc_alloc_cleared_vec_tree ((int) RID_MAX);
94 for (i = 0; i < num_c_common_reswords; i++)
95 {
96 /* If a keyword is disabled, do not enter it into the table
97 and so create a canonical spelling that isn't a keyword. */
98 if (c_common_reswords[i].disable & mask)
99 {
100 if (warn_cxx_compat
101 && (c_common_reswords[i].disable & D_CXXWARN))
102 {
103 id = get_identifier (c_common_reswords[i].word);
104 C_SET_RID_CODE (id, RID_CXX_COMPAT_WARN);
105 C_IS_RESERVED_WORD (id) = 1;
106 }
107 continue;
108 }
109
110 id = get_identifier (c_common_reswords[i].word);
111 C_SET_RID_CODE (id, c_common_reswords[i].rid);
112 C_IS_RESERVED_WORD (id) = 1;
113 ridpointers [(int) c_common_reswords[i].rid] = id;
114 }
115 }
116 \f
117 /* The C lexer intermediates between the lexer in cpplib and c-lex.c
118 and the C parser. Unlike the C++ lexer, the parser structure
119 stores the lexer information instead of using a separate structure.
120 Identifiers are separated into ordinary identifiers, type names,
121 keywords and some other Objective-C types of identifiers, and some
122 look-ahead is maintained.
123
124 ??? It might be a good idea to lex the whole file up front (as for
125 C++). It would then be possible to share more of the C and C++
126 lexer code, if desired. */
127
128 /* The following local token type is used. */
129
130 /* A keyword. */
131 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
132
133 /* More information about the type of a CPP_NAME token. */
134 typedef enum c_id_kind {
135 /* An ordinary identifier. */
136 C_ID_ID,
137 /* An identifier declared as a typedef name. */
138 C_ID_TYPENAME,
139 /* An identifier declared as an Objective-C class name. */
140 C_ID_CLASSNAME,
141 /* An address space identifier. */
142 C_ID_ADDRSPACE,
143 /* Not an identifier. */
144 C_ID_NONE
145 } c_id_kind;
146
147 /* A single C token after string literal concatenation and conversion
148 of preprocessing tokens to tokens. */
149 typedef struct GTY (()) c_token {
150 /* The kind of token. */
151 ENUM_BITFIELD (cpp_ttype) type : 8;
152 /* If this token is a CPP_NAME, this value indicates whether also
153 declared as some kind of type. Otherwise, it is C_ID_NONE. */
154 ENUM_BITFIELD (c_id_kind) id_kind : 8;
155 /* If this token is a keyword, this value indicates which keyword.
156 Otherwise, this value is RID_MAX. */
157 ENUM_BITFIELD (rid) keyword : 8;
158 /* If this token is a CPP_PRAGMA, this indicates the pragma that
159 was seen. Otherwise it is PRAGMA_NONE. */
160 ENUM_BITFIELD (pragma_kind) pragma_kind : 8;
161 /* The location at which this token was found. */
162 location_t location;
163 /* The value associated with this token, if any. */
164 tree value;
165 } c_token;
166
167 /* A parser structure recording information about the state and
168 context of parsing. Includes lexer information with up to two
169 tokens of look-ahead; more are not needed for C. */
170 typedef struct GTY(()) c_parser {
171 /* The look-ahead tokens. */
172 c_token * GTY((skip)) tokens;
173 /* Buffer for look-ahead tokens. */
174 c_token tokens_buf[2];
175 /* How many look-ahead tokens are available (0, 1 or 2, or
176 more if parsing from pre-lexed tokens). */
177 unsigned int tokens_avail;
178 /* True if a syntax error is being recovered from; false otherwise.
179 c_parser_error sets this flag. It should clear this flag when
180 enough tokens have been consumed to recover from the error. */
181 BOOL_BITFIELD error : 1;
182 /* True if we're processing a pragma, and shouldn't automatically
183 consume CPP_PRAGMA_EOL. */
184 BOOL_BITFIELD in_pragma : 1;
185 /* True if we're parsing the outermost block of an if statement. */
186 BOOL_BITFIELD in_if_block : 1;
187 /* True if we want to lex an untranslated string. */
188 BOOL_BITFIELD lex_untranslated_string : 1;
189
190 /* Objective-C specific parser/lexer information. */
191
192 /* True if we are in a context where the Objective-C "PQ" keywords
193 are considered keywords. */
194 BOOL_BITFIELD objc_pq_context : 1;
195 /* True if we are parsing a (potential) Objective-C foreach
196 statement. This is set to true after we parsed 'for (' and while
197 we wait for 'in' or ';' to decide if it's a standard C for loop or an
198 Objective-C foreach loop. */
199 BOOL_BITFIELD objc_could_be_foreach_context : 1;
200 /* The following flag is needed to contextualize Objective-C lexical
201 analysis. In some cases (e.g., 'int NSObject;'), it is
202 undesirable to bind an identifier to an Objective-C class, even
203 if a class with that name exists. */
204 BOOL_BITFIELD objc_need_raw_identifier : 1;
205 /* Nonzero if we're processing a __transaction statement. The value
206 is 1 | TM_STMT_ATTR_*. */
207 unsigned int in_transaction : 4;
208 /* True if we are in a context where the Objective-C "Property attribute"
209 keywords are valid. */
210 BOOL_BITFIELD objc_property_attr_context : 1;
211 } c_parser;
212
213
214 /* The actual parser and external interface. ??? Does this need to be
215 garbage-collected? */
216
217 static GTY (()) c_parser *the_parser;
218
219 /* Read in and lex a single token, storing it in *TOKEN. */
220
221 static void
222 c_lex_one_token (c_parser *parser, c_token *token)
223 {
224 timevar_push (TV_LEX);
225
226 token->type = c_lex_with_flags (&token->value, &token->location, NULL,
227 (parser->lex_untranslated_string
228 ? C_LEX_STRING_NO_TRANSLATE : 0));
229 token->id_kind = C_ID_NONE;
230 token->keyword = RID_MAX;
231 token->pragma_kind = PRAGMA_NONE;
232
233 switch (token->type)
234 {
235 case CPP_NAME:
236 {
237 tree decl;
238
239 bool objc_force_identifier = parser->objc_need_raw_identifier;
240 if (c_dialect_objc ())
241 parser->objc_need_raw_identifier = false;
242
243 if (C_IS_RESERVED_WORD (token->value))
244 {
245 enum rid rid_code = C_RID_CODE (token->value);
246
247 if (rid_code == RID_CXX_COMPAT_WARN)
248 {
249 warning_at (token->location,
250 OPT_Wc___compat,
251 "identifier %qE conflicts with C++ keyword",
252 token->value);
253 }
254 else if (rid_code >= RID_FIRST_ADDR_SPACE
255 && rid_code <= RID_LAST_ADDR_SPACE)
256 {
257 token->id_kind = C_ID_ADDRSPACE;
258 token->keyword = rid_code;
259 break;
260 }
261 else if (c_dialect_objc () && OBJC_IS_PQ_KEYWORD (rid_code))
262 {
263 /* We found an Objective-C "pq" keyword (in, out,
264 inout, bycopy, byref, oneway). They need special
265 care because the interpretation depends on the
266 context. */
267 if (parser->objc_pq_context)
268 {
269 token->type = CPP_KEYWORD;
270 token->keyword = rid_code;
271 break;
272 }
273 else if (parser->objc_could_be_foreach_context
274 && rid_code == RID_IN)
275 {
276 /* We are in Objective-C, inside a (potential)
277 foreach context (which means after having
278 parsed 'for (', but before having parsed ';'),
279 and we found 'in'. We consider it the keyword
280 which terminates the declaration at the
281 beginning of a foreach-statement. Note that
282 this means you can't use 'in' for anything else
283 in that context; in particular, in Objective-C
284 you can't use 'in' as the name of the running
285 variable in a C for loop. We could potentially
286 try to add code here to disambiguate, but it
287 seems a reasonable limitation. */
288 token->type = CPP_KEYWORD;
289 token->keyword = rid_code;
290 break;
291 }
292 /* Else, "pq" keywords outside of the "pq" context are
293 not keywords, and we fall through to the code for
294 normal tokens. */
295 }
296 else if (c_dialect_objc () && OBJC_IS_PATTR_KEYWORD (rid_code))
297 {
298 /* We found an Objective-C "property attribute"
299 keyword (getter, setter, readonly, etc). These are
300 only valid in the property context. */
301 if (parser->objc_property_attr_context)
302 {
303 token->type = CPP_KEYWORD;
304 token->keyword = rid_code;
305 break;
306 }
307 /* Else they are not special keywords.
308 */
309 }
310 else if (c_dialect_objc ()
311 && (OBJC_IS_AT_KEYWORD (rid_code)
312 || OBJC_IS_CXX_KEYWORD (rid_code)))
313 {
314 /* We found one of the Objective-C "@" keywords (defs,
315 selector, synchronized, etc) or one of the
316 Objective-C "cxx" keywords (class, private,
317 protected, public, try, catch, throw) without a
318 preceding '@' sign. Do nothing and fall through to
319 the code for normal tokens (in C++ we would still
320 consider the CXX ones keywords, but not in C). */
321 ;
322 }
323 else
324 {
325 token->type = CPP_KEYWORD;
326 token->keyword = rid_code;
327 break;
328 }
329 }
330
331 decl = lookup_name (token->value);
332 if (decl)
333 {
334 if (TREE_CODE (decl) == TYPE_DECL)
335 {
336 token->id_kind = C_ID_TYPENAME;
337 break;
338 }
339 }
340 else if (c_dialect_objc ())
341 {
342 tree objc_interface_decl = objc_is_class_name (token->value);
343 /* Objective-C class names are in the same namespace as
344 variables and typedefs, and hence are shadowed by local
345 declarations. */
346 if (objc_interface_decl
347 && (!objc_force_identifier || global_bindings_p ()))
348 {
349 token->value = objc_interface_decl;
350 token->id_kind = C_ID_CLASSNAME;
351 break;
352 }
353 }
354 token->id_kind = C_ID_ID;
355 }
356 break;
357 case CPP_AT_NAME:
358 /* This only happens in Objective-C; it must be a keyword. */
359 token->type = CPP_KEYWORD;
360 switch (C_RID_CODE (token->value))
361 {
362 /* Replace 'class' with '@class', 'private' with '@private',
363 etc. This prevents confusion with the C++ keyword
364 'class', and makes the tokens consistent with other
365 Objective-C 'AT' keywords. For example '@class' is
366 reported as RID_AT_CLASS which is consistent with
367 '@synchronized', which is reported as
368 RID_AT_SYNCHRONIZED.
369 */
370 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
371 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
372 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
373 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
374 case RID_THROW: token->keyword = RID_AT_THROW; break;
375 case RID_TRY: token->keyword = RID_AT_TRY; break;
376 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
377 default: token->keyword = C_RID_CODE (token->value);
378 }
379 break;
380 case CPP_COLON:
381 case CPP_COMMA:
382 case CPP_CLOSE_PAREN:
383 case CPP_SEMICOLON:
384 /* These tokens may affect the interpretation of any identifiers
385 following, if doing Objective-C. */
386 if (c_dialect_objc ())
387 parser->objc_need_raw_identifier = false;
388 break;
389 case CPP_PRAGMA:
390 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
391 token->pragma_kind = (enum pragma_kind) TREE_INT_CST_LOW (token->value);
392 token->value = NULL;
393 break;
394 default:
395 break;
396 }
397 timevar_pop (TV_LEX);
398 }
399
400 /* Return a pointer to the next token from PARSER, reading it in if
401 necessary. */
402
403 static inline c_token *
404 c_parser_peek_token (c_parser *parser)
405 {
406 if (parser->tokens_avail == 0)
407 {
408 c_lex_one_token (parser, &parser->tokens[0]);
409 parser->tokens_avail = 1;
410 }
411 return &parser->tokens[0];
412 }
413
414 /* Return true if the next token from PARSER has the indicated
415 TYPE. */
416
417 static inline bool
418 c_parser_next_token_is (c_parser *parser, enum cpp_ttype type)
419 {
420 return c_parser_peek_token (parser)->type == type;
421 }
422
423 /* Return true if the next token from PARSER does not have the
424 indicated TYPE. */
425
426 static inline bool
427 c_parser_next_token_is_not (c_parser *parser, enum cpp_ttype type)
428 {
429 return !c_parser_next_token_is (parser, type);
430 }
431
432 /* Return true if the next token from PARSER is the indicated
433 KEYWORD. */
434
435 static inline bool
436 c_parser_next_token_is_keyword (c_parser *parser, enum rid keyword)
437 {
438 return c_parser_peek_token (parser)->keyword == keyword;
439 }
440
441 /* Return a pointer to the next-but-one token from PARSER, reading it
442 in if necessary. The next token is already read in. */
443
444 static c_token *
445 c_parser_peek_2nd_token (c_parser *parser)
446 {
447 if (parser->tokens_avail >= 2)
448 return &parser->tokens[1];
449 gcc_assert (parser->tokens_avail == 1);
450 gcc_assert (parser->tokens[0].type != CPP_EOF);
451 gcc_assert (parser->tokens[0].type != CPP_PRAGMA_EOL);
452 c_lex_one_token (parser, &parser->tokens[1]);
453 parser->tokens_avail = 2;
454 return &parser->tokens[1];
455 }
456
457 /* Return true if TOKEN can start a type name,
458 false otherwise. */
459 static bool
460 c_token_starts_typename (c_token *token)
461 {
462 switch (token->type)
463 {
464 case CPP_NAME:
465 switch (token->id_kind)
466 {
467 case C_ID_ID:
468 return false;
469 case C_ID_ADDRSPACE:
470 return true;
471 case C_ID_TYPENAME:
472 return true;
473 case C_ID_CLASSNAME:
474 gcc_assert (c_dialect_objc ());
475 return true;
476 default:
477 gcc_unreachable ();
478 }
479 case CPP_KEYWORD:
480 switch (token->keyword)
481 {
482 case RID_UNSIGNED:
483 case RID_LONG:
484 case RID_INT128:
485 case RID_SHORT:
486 case RID_SIGNED:
487 case RID_COMPLEX:
488 case RID_INT:
489 case RID_CHAR:
490 case RID_FLOAT:
491 case RID_DOUBLE:
492 case RID_VOID:
493 case RID_DFLOAT32:
494 case RID_DFLOAT64:
495 case RID_DFLOAT128:
496 case RID_BOOL:
497 case RID_ENUM:
498 case RID_STRUCT:
499 case RID_UNION:
500 case RID_TYPEOF:
501 case RID_CONST:
502 case RID_ATOMIC:
503 case RID_VOLATILE:
504 case RID_RESTRICT:
505 case RID_ATTRIBUTE:
506 case RID_FRACT:
507 case RID_ACCUM:
508 case RID_SAT:
509 case RID_AUTO_TYPE:
510 return true;
511 default:
512 return false;
513 }
514 case CPP_LESS:
515 if (c_dialect_objc ())
516 return true;
517 return false;
518 default:
519 return false;
520 }
521 }
522
523 enum c_lookahead_kind {
524 /* Always treat unknown identifiers as typenames. */
525 cla_prefer_type,
526
527 /* Could be parsing a nonabstract declarator. Only treat an identifier
528 as a typename if followed by another identifier or a star. */
529 cla_nonabstract_decl,
530
531 /* Never treat identifiers as typenames. */
532 cla_prefer_id
533 };
534
535 /* Return true if the next token from PARSER can start a type name,
536 false otherwise. LA specifies how to do lookahead in order to
537 detect unknown type names. If unsure, pick CLA_PREFER_ID. */
538
539 static inline bool
540 c_parser_next_tokens_start_typename (c_parser *parser, enum c_lookahead_kind la)
541 {
542 c_token *token = c_parser_peek_token (parser);
543 if (c_token_starts_typename (token))
544 return true;
545
546 /* Try a bit harder to detect an unknown typename. */
547 if (la != cla_prefer_id
548 && token->type == CPP_NAME
549 && token->id_kind == C_ID_ID
550
551 /* Do not try too hard when we could have "object in array". */
552 && !parser->objc_could_be_foreach_context
553
554 && (la == cla_prefer_type
555 || c_parser_peek_2nd_token (parser)->type == CPP_NAME
556 || c_parser_peek_2nd_token (parser)->type == CPP_MULT)
557
558 /* Only unknown identifiers. */
559 && !lookup_name (token->value))
560 return true;
561
562 return false;
563 }
564
565 /* Return true if TOKEN is a type qualifier, false otherwise. */
566 static bool
567 c_token_is_qualifier (c_token *token)
568 {
569 switch (token->type)
570 {
571 case CPP_NAME:
572 switch (token->id_kind)
573 {
574 case C_ID_ADDRSPACE:
575 return true;
576 default:
577 return false;
578 }
579 case CPP_KEYWORD:
580 switch (token->keyword)
581 {
582 case RID_CONST:
583 case RID_VOLATILE:
584 case RID_RESTRICT:
585 case RID_ATTRIBUTE:
586 case RID_ATOMIC:
587 return true;
588 default:
589 return false;
590 }
591 case CPP_LESS:
592 return false;
593 default:
594 gcc_unreachable ();
595 }
596 }
597
598 /* Return true if the next token from PARSER is a type qualifier,
599 false otherwise. */
600 static inline bool
601 c_parser_next_token_is_qualifier (c_parser *parser)
602 {
603 c_token *token = c_parser_peek_token (parser);
604 return c_token_is_qualifier (token);
605 }
606
607 /* Return true if TOKEN can start declaration specifiers, false
608 otherwise. */
609 static bool
610 c_token_starts_declspecs (c_token *token)
611 {
612 switch (token->type)
613 {
614 case CPP_NAME:
615 switch (token->id_kind)
616 {
617 case C_ID_ID:
618 return false;
619 case C_ID_ADDRSPACE:
620 return true;
621 case C_ID_TYPENAME:
622 return true;
623 case C_ID_CLASSNAME:
624 gcc_assert (c_dialect_objc ());
625 return true;
626 default:
627 gcc_unreachable ();
628 }
629 case CPP_KEYWORD:
630 switch (token->keyword)
631 {
632 case RID_STATIC:
633 case RID_EXTERN:
634 case RID_REGISTER:
635 case RID_TYPEDEF:
636 case RID_INLINE:
637 case RID_NORETURN:
638 case RID_AUTO:
639 case RID_THREAD:
640 case RID_UNSIGNED:
641 case RID_LONG:
642 case RID_INT128:
643 case RID_SHORT:
644 case RID_SIGNED:
645 case RID_COMPLEX:
646 case RID_INT:
647 case RID_CHAR:
648 case RID_FLOAT:
649 case RID_DOUBLE:
650 case RID_VOID:
651 case RID_DFLOAT32:
652 case RID_DFLOAT64:
653 case RID_DFLOAT128:
654 case RID_BOOL:
655 case RID_ENUM:
656 case RID_STRUCT:
657 case RID_UNION:
658 case RID_TYPEOF:
659 case RID_CONST:
660 case RID_VOLATILE:
661 case RID_RESTRICT:
662 case RID_ATTRIBUTE:
663 case RID_FRACT:
664 case RID_ACCUM:
665 case RID_SAT:
666 case RID_ALIGNAS:
667 case RID_ATOMIC:
668 case RID_AUTO_TYPE:
669 return true;
670 default:
671 return false;
672 }
673 case CPP_LESS:
674 if (c_dialect_objc ())
675 return true;
676 return false;
677 default:
678 return false;
679 }
680 }
681
682
683 /* Return true if TOKEN can start declaration specifiers or a static
684 assertion, false otherwise. */
685 static bool
686 c_token_starts_declaration (c_token *token)
687 {
688 if (c_token_starts_declspecs (token)
689 || token->keyword == RID_STATIC_ASSERT)
690 return true;
691 else
692 return false;
693 }
694
695 /* Return true if the next token from PARSER can start declaration
696 specifiers, false otherwise. */
697 static inline bool
698 c_parser_next_token_starts_declspecs (c_parser *parser)
699 {
700 c_token *token = c_parser_peek_token (parser);
701
702 /* In Objective-C, a classname normally starts a declspecs unless it
703 is immediately followed by a dot. In that case, it is the
704 Objective-C 2.0 "dot-syntax" for class objects, ie, calls the
705 setter/getter on the class. c_token_starts_declspecs() can't
706 differentiate between the two cases because it only checks the
707 current token, so we have a special check here. */
708 if (c_dialect_objc ()
709 && token->type == CPP_NAME
710 && token->id_kind == C_ID_CLASSNAME
711 && c_parser_peek_2nd_token (parser)->type == CPP_DOT)
712 return false;
713
714 return c_token_starts_declspecs (token);
715 }
716
717 /* Return true if the next tokens from PARSER can start declaration
718 specifiers or a static assertion, false otherwise. */
719 static inline bool
720 c_parser_next_tokens_start_declaration (c_parser *parser)
721 {
722 c_token *token = c_parser_peek_token (parser);
723
724 /* Same as above. */
725 if (c_dialect_objc ()
726 && token->type == CPP_NAME
727 && token->id_kind == C_ID_CLASSNAME
728 && c_parser_peek_2nd_token (parser)->type == CPP_DOT)
729 return false;
730
731 /* Labels do not start declarations. */
732 if (token->type == CPP_NAME
733 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
734 return false;
735
736 if (c_token_starts_declaration (token))
737 return true;
738
739 if (c_parser_next_tokens_start_typename (parser, cla_nonabstract_decl))
740 return true;
741
742 return false;
743 }
744
745 /* Consume the next token from PARSER. */
746
747 static void
748 c_parser_consume_token (c_parser *parser)
749 {
750 gcc_assert (parser->tokens_avail >= 1);
751 gcc_assert (parser->tokens[0].type != CPP_EOF);
752 gcc_assert (!parser->in_pragma || parser->tokens[0].type != CPP_PRAGMA_EOL);
753 gcc_assert (parser->error || parser->tokens[0].type != CPP_PRAGMA);
754 if (parser->tokens != &parser->tokens_buf[0])
755 parser->tokens++;
756 else if (parser->tokens_avail == 2)
757 parser->tokens[0] = parser->tokens[1];
758 parser->tokens_avail--;
759 }
760
761 /* Expect the current token to be a #pragma. Consume it and remember
762 that we've begun parsing a pragma. */
763
764 static void
765 c_parser_consume_pragma (c_parser *parser)
766 {
767 gcc_assert (!parser->in_pragma);
768 gcc_assert (parser->tokens_avail >= 1);
769 gcc_assert (parser->tokens[0].type == CPP_PRAGMA);
770 if (parser->tokens != &parser->tokens_buf[0])
771 parser->tokens++;
772 else if (parser->tokens_avail == 2)
773 parser->tokens[0] = parser->tokens[1];
774 parser->tokens_avail--;
775 parser->in_pragma = true;
776 }
777
778 /* Update the globals input_location and in_system_header from
779 TOKEN. */
780 static inline void
781 c_parser_set_source_position_from_token (c_token *token)
782 {
783 if (token->type != CPP_EOF)
784 {
785 input_location = token->location;
786 }
787 }
788
789 /* Issue a diagnostic of the form
790 FILE:LINE: MESSAGE before TOKEN
791 where TOKEN is the next token in the input stream of PARSER.
792 MESSAGE (specified by the caller) is usually of the form "expected
793 OTHER-TOKEN".
794
795 Do not issue a diagnostic if still recovering from an error.
796
797 ??? This is taken from the C++ parser, but building up messages in
798 this way is not i18n-friendly and some other approach should be
799 used. */
800
801 static void
802 c_parser_error (c_parser *parser, const char *gmsgid)
803 {
804 c_token *token = c_parser_peek_token (parser);
805 if (parser->error)
806 return;
807 parser->error = true;
808 if (!gmsgid)
809 return;
810 /* This diagnostic makes more sense if it is tagged to the line of
811 the token we just peeked at. */
812 c_parser_set_source_position_from_token (token);
813 c_parse_error (gmsgid,
814 /* Because c_parse_error does not understand
815 CPP_KEYWORD, keywords are treated like
816 identifiers. */
817 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
818 /* ??? The C parser does not save the cpp flags of a
819 token, we need to pass 0 here and we will not get
820 the source spelling of some tokens but rather the
821 canonical spelling. */
822 token->value, /*flags=*/0);
823 }
824
825 /* If the next token is of the indicated TYPE, consume it. Otherwise,
826 issue the error MSGID. If MSGID is NULL then a message has already
827 been produced and no message will be produced this time. Returns
828 true if found, false otherwise. */
829
830 static bool
831 c_parser_require (c_parser *parser,
832 enum cpp_ttype type,
833 const char *msgid)
834 {
835 if (c_parser_next_token_is (parser, type))
836 {
837 c_parser_consume_token (parser);
838 return true;
839 }
840 else
841 {
842 c_parser_error (parser, msgid);
843 return false;
844 }
845 }
846
847 /* If the next token is the indicated keyword, consume it. Otherwise,
848 issue the error MSGID. Returns true if found, false otherwise. */
849
850 static bool
851 c_parser_require_keyword (c_parser *parser,
852 enum rid keyword,
853 const char *msgid)
854 {
855 if (c_parser_next_token_is_keyword (parser, keyword))
856 {
857 c_parser_consume_token (parser);
858 return true;
859 }
860 else
861 {
862 c_parser_error (parser, msgid);
863 return false;
864 }
865 }
866
867 /* Like c_parser_require, except that tokens will be skipped until the
868 desired token is found. An error message is still produced if the
869 next token is not as expected. If MSGID is NULL then a message has
870 already been produced and no message will be produced this
871 time. */
872
873 static void
874 c_parser_skip_until_found (c_parser *parser,
875 enum cpp_ttype type,
876 const char *msgid)
877 {
878 unsigned nesting_depth = 0;
879
880 if (c_parser_require (parser, type, msgid))
881 return;
882
883 /* Skip tokens until the desired token is found. */
884 while (true)
885 {
886 /* Peek at the next token. */
887 c_token *token = c_parser_peek_token (parser);
888 /* If we've reached the token we want, consume it and stop. */
889 if (token->type == type && !nesting_depth)
890 {
891 c_parser_consume_token (parser);
892 break;
893 }
894
895 /* If we've run out of tokens, stop. */
896 if (token->type == CPP_EOF)
897 return;
898 if (token->type == CPP_PRAGMA_EOL && parser->in_pragma)
899 return;
900 if (token->type == CPP_OPEN_BRACE
901 || token->type == CPP_OPEN_PAREN
902 || token->type == CPP_OPEN_SQUARE)
903 ++nesting_depth;
904 else if (token->type == CPP_CLOSE_BRACE
905 || token->type == CPP_CLOSE_PAREN
906 || token->type == CPP_CLOSE_SQUARE)
907 {
908 if (nesting_depth-- == 0)
909 break;
910 }
911 /* Consume this token. */
912 c_parser_consume_token (parser);
913 }
914 parser->error = false;
915 }
916
917 /* Skip tokens until the end of a parameter is found, but do not
918 consume the comma, semicolon or closing delimiter. */
919
920 static void
921 c_parser_skip_to_end_of_parameter (c_parser *parser)
922 {
923 unsigned nesting_depth = 0;
924
925 while (true)
926 {
927 c_token *token = c_parser_peek_token (parser);
928 if ((token->type == CPP_COMMA || token->type == CPP_SEMICOLON)
929 && !nesting_depth)
930 break;
931 /* If we've run out of tokens, stop. */
932 if (token->type == CPP_EOF)
933 return;
934 if (token->type == CPP_PRAGMA_EOL && parser->in_pragma)
935 return;
936 if (token->type == CPP_OPEN_BRACE
937 || token->type == CPP_OPEN_PAREN
938 || token->type == CPP_OPEN_SQUARE)
939 ++nesting_depth;
940 else if (token->type == CPP_CLOSE_BRACE
941 || token->type == CPP_CLOSE_PAREN
942 || token->type == CPP_CLOSE_SQUARE)
943 {
944 if (nesting_depth-- == 0)
945 break;
946 }
947 /* Consume this token. */
948 c_parser_consume_token (parser);
949 }
950 parser->error = false;
951 }
952
953 /* Expect to be at the end of the pragma directive and consume an
954 end of line marker. */
955
956 static void
957 c_parser_skip_to_pragma_eol (c_parser *parser)
958 {
959 gcc_assert (parser->in_pragma);
960 parser->in_pragma = false;
961
962 if (!c_parser_require (parser, CPP_PRAGMA_EOL, "expected end of line"))
963 while (true)
964 {
965 c_token *token = c_parser_peek_token (parser);
966 if (token->type == CPP_EOF)
967 break;
968 if (token->type == CPP_PRAGMA_EOL)
969 {
970 c_parser_consume_token (parser);
971 break;
972 }
973 c_parser_consume_token (parser);
974 }
975
976 parser->error = false;
977 }
978
979 /* Skip tokens until we have consumed an entire block, or until we
980 have consumed a non-nested ';'. */
981
982 static void
983 c_parser_skip_to_end_of_block_or_statement (c_parser *parser)
984 {
985 unsigned nesting_depth = 0;
986 bool save_error = parser->error;
987
988 while (true)
989 {
990 c_token *token;
991
992 /* Peek at the next token. */
993 token = c_parser_peek_token (parser);
994
995 switch (token->type)
996 {
997 case CPP_EOF:
998 return;
999
1000 case CPP_PRAGMA_EOL:
1001 if (parser->in_pragma)
1002 return;
1003 break;
1004
1005 case CPP_SEMICOLON:
1006 /* If the next token is a ';', we have reached the
1007 end of the statement. */
1008 if (!nesting_depth)
1009 {
1010 /* Consume the ';'. */
1011 c_parser_consume_token (parser);
1012 goto finished;
1013 }
1014 break;
1015
1016 case CPP_CLOSE_BRACE:
1017 /* If the next token is a non-nested '}', then we have
1018 reached the end of the current block. */
1019 if (nesting_depth == 0 || --nesting_depth == 0)
1020 {
1021 c_parser_consume_token (parser);
1022 goto finished;
1023 }
1024 break;
1025
1026 case CPP_OPEN_BRACE:
1027 /* If it the next token is a '{', then we are entering a new
1028 block. Consume the entire block. */
1029 ++nesting_depth;
1030 break;
1031
1032 case CPP_PRAGMA:
1033 /* If we see a pragma, consume the whole thing at once. We
1034 have some safeguards against consuming pragmas willy-nilly.
1035 Normally, we'd expect to be here with parser->error set,
1036 which disables these safeguards. But it's possible to get
1037 here for secondary error recovery, after parser->error has
1038 been cleared. */
1039 c_parser_consume_pragma (parser);
1040 c_parser_skip_to_pragma_eol (parser);
1041 parser->error = save_error;
1042 continue;
1043
1044 default:
1045 break;
1046 }
1047
1048 c_parser_consume_token (parser);
1049 }
1050
1051 finished:
1052 parser->error = false;
1053 }
1054
1055 /* CPP's options (initialized by c-opts.c). */
1056 extern cpp_options *cpp_opts;
1057
1058 /* Save the warning flags which are controlled by __extension__. */
1059
1060 static inline int
1061 disable_extension_diagnostics (void)
1062 {
1063 int ret = (pedantic
1064 | (warn_pointer_arith << 1)
1065 | (warn_traditional << 2)
1066 | (flag_iso << 3)
1067 | (warn_long_long << 4)
1068 | (warn_cxx_compat << 5)
1069 | (warn_overlength_strings << 6));
1070 cpp_opts->cpp_pedantic = pedantic = 0;
1071 warn_pointer_arith = 0;
1072 cpp_opts->cpp_warn_traditional = warn_traditional = 0;
1073 flag_iso = 0;
1074 cpp_opts->cpp_warn_long_long = warn_long_long = 0;
1075 warn_cxx_compat = 0;
1076 warn_overlength_strings = 0;
1077 return ret;
1078 }
1079
1080 /* Restore the warning flags which are controlled by __extension__.
1081 FLAGS is the return value from disable_extension_diagnostics. */
1082
1083 static inline void
1084 restore_extension_diagnostics (int flags)
1085 {
1086 cpp_opts->cpp_pedantic = pedantic = flags & 1;
1087 warn_pointer_arith = (flags >> 1) & 1;
1088 cpp_opts->cpp_warn_traditional = warn_traditional = (flags >> 2) & 1;
1089 flag_iso = (flags >> 3) & 1;
1090 cpp_opts->cpp_warn_long_long = warn_long_long = (flags >> 4) & 1;
1091 warn_cxx_compat = (flags >> 5) & 1;
1092 warn_overlength_strings = (flags >> 6) & 1;
1093 }
1094
1095 /* Possibly kinds of declarator to parse. */
1096 typedef enum c_dtr_syn {
1097 /* A normal declarator with an identifier. */
1098 C_DTR_NORMAL,
1099 /* An abstract declarator (maybe empty). */
1100 C_DTR_ABSTRACT,
1101 /* A parameter declarator: may be either, but after a type name does
1102 not redeclare a typedef name as an identifier if it can
1103 alternatively be interpreted as a typedef name; see DR#009,
1104 applied in C90 TC1, omitted from C99 and reapplied in C99 TC2
1105 following DR#249. For example, given a typedef T, "int T" and
1106 "int *T" are valid parameter declarations redeclaring T, while
1107 "int (T)" and "int * (T)" and "int (T[])" and "int (T (int))" are
1108 abstract declarators rather than involving redundant parentheses;
1109 the same applies with attributes inside the parentheses before
1110 "T". */
1111 C_DTR_PARM
1112 } c_dtr_syn;
1113
1114 /* The binary operation precedence levels, where 0 is a dummy lowest level
1115 used for the bottom of the stack. */
1116 enum c_parser_prec {
1117 PREC_NONE,
1118 PREC_LOGOR,
1119 PREC_LOGAND,
1120 PREC_BITOR,
1121 PREC_BITXOR,
1122 PREC_BITAND,
1123 PREC_EQ,
1124 PREC_REL,
1125 PREC_SHIFT,
1126 PREC_ADD,
1127 PREC_MULT,
1128 NUM_PRECS
1129 };
1130
1131 static void c_parser_external_declaration (c_parser *);
1132 static void c_parser_asm_definition (c_parser *);
1133 static void c_parser_declaration_or_fndef (c_parser *, bool, bool, bool,
1134 bool, bool, tree *, vec<c_token>);
1135 static void c_parser_static_assert_declaration_no_semi (c_parser *);
1136 static void c_parser_static_assert_declaration (c_parser *);
1137 static void c_parser_declspecs (c_parser *, struct c_declspecs *, bool, bool,
1138 bool, bool, bool, enum c_lookahead_kind);
1139 static struct c_typespec c_parser_enum_specifier (c_parser *);
1140 static struct c_typespec c_parser_struct_or_union_specifier (c_parser *);
1141 static tree c_parser_struct_declaration (c_parser *);
1142 static struct c_typespec c_parser_typeof_specifier (c_parser *);
1143 static tree c_parser_alignas_specifier (c_parser *);
1144 static struct c_declarator *c_parser_declarator (c_parser *, bool, c_dtr_syn,
1145 bool *);
1146 static struct c_declarator *c_parser_direct_declarator (c_parser *, bool,
1147 c_dtr_syn, bool *);
1148 static struct c_declarator *c_parser_direct_declarator_inner (c_parser *,
1149 bool,
1150 struct c_declarator *);
1151 static struct c_arg_info *c_parser_parms_declarator (c_parser *, bool, tree);
1152 static struct c_arg_info *c_parser_parms_list_declarator (c_parser *, tree,
1153 tree);
1154 static struct c_parm *c_parser_parameter_declaration (c_parser *, tree);
1155 static tree c_parser_simple_asm_expr (c_parser *);
1156 static tree c_parser_attributes (c_parser *);
1157 static struct c_type_name *c_parser_type_name (c_parser *);
1158 static struct c_expr c_parser_initializer (c_parser *);
1159 static struct c_expr c_parser_braced_init (c_parser *, tree, bool);
1160 static void c_parser_initelt (c_parser *, struct obstack *);
1161 static void c_parser_initval (c_parser *, struct c_expr *,
1162 struct obstack *);
1163 static tree c_parser_compound_statement (c_parser *);
1164 static void c_parser_compound_statement_nostart (c_parser *);
1165 static void c_parser_label (c_parser *);
1166 static void c_parser_statement (c_parser *);
1167 static void c_parser_statement_after_labels (c_parser *);
1168 static void c_parser_if_statement (c_parser *);
1169 static void c_parser_switch_statement (c_parser *);
1170 static void c_parser_while_statement (c_parser *, bool);
1171 static void c_parser_do_statement (c_parser *, bool);
1172 static void c_parser_for_statement (c_parser *, bool);
1173 static tree c_parser_asm_statement (c_parser *);
1174 static tree c_parser_asm_operands (c_parser *);
1175 static tree c_parser_asm_goto_operands (c_parser *);
1176 static tree c_parser_asm_clobbers (c_parser *);
1177 static struct c_expr c_parser_expr_no_commas (c_parser *, struct c_expr *,
1178 tree = NULL_TREE);
1179 static struct c_expr c_parser_conditional_expression (c_parser *,
1180 struct c_expr *, tree);
1181 static struct c_expr c_parser_binary_expression (c_parser *, struct c_expr *,
1182 tree);
1183 static struct c_expr c_parser_cast_expression (c_parser *, struct c_expr *);
1184 static struct c_expr c_parser_unary_expression (c_parser *);
1185 static struct c_expr c_parser_sizeof_expression (c_parser *);
1186 static struct c_expr c_parser_alignof_expression (c_parser *);
1187 static struct c_expr c_parser_postfix_expression (c_parser *);
1188 static struct c_expr c_parser_postfix_expression_after_paren_type (c_parser *,
1189 struct c_type_name *,
1190 location_t);
1191 static struct c_expr c_parser_postfix_expression_after_primary (c_parser *,
1192 location_t loc,
1193 struct c_expr);
1194 static tree c_parser_transaction (c_parser *, enum rid);
1195 static struct c_expr c_parser_transaction_expression (c_parser *, enum rid);
1196 static tree c_parser_transaction_cancel (c_parser *);
1197 static struct c_expr c_parser_expression (c_parser *);
1198 static struct c_expr c_parser_expression_conv (c_parser *);
1199 static vec<tree, va_gc> *c_parser_expr_list (c_parser *, bool, bool,
1200 vec<tree, va_gc> **, location_t *,
1201 tree *);
1202 static void c_parser_omp_construct (c_parser *);
1203 static void c_parser_omp_threadprivate (c_parser *);
1204 static void c_parser_omp_barrier (c_parser *);
1205 static void c_parser_omp_flush (c_parser *);
1206 static void c_parser_omp_taskwait (c_parser *);
1207 static void c_parser_omp_taskyield (c_parser *);
1208 static void c_parser_omp_cancel (c_parser *);
1209 static void c_parser_omp_cancellation_point (c_parser *);
1210
1211 enum pragma_context { pragma_external, pragma_struct, pragma_param,
1212 pragma_stmt, pragma_compound };
1213 static bool c_parser_pragma (c_parser *, enum pragma_context);
1214 static bool c_parser_omp_target (c_parser *, enum pragma_context);
1215 static void c_parser_omp_end_declare_target (c_parser *);
1216 static void c_parser_omp_declare (c_parser *, enum pragma_context);
1217
1218 /* These Objective-C parser functions are only ever called when
1219 compiling Objective-C. */
1220 static void c_parser_objc_class_definition (c_parser *, tree);
1221 static void c_parser_objc_class_instance_variables (c_parser *);
1222 static void c_parser_objc_class_declaration (c_parser *);
1223 static void c_parser_objc_alias_declaration (c_parser *);
1224 static void c_parser_objc_protocol_definition (c_parser *, tree);
1225 static bool c_parser_objc_method_type (c_parser *);
1226 static void c_parser_objc_method_definition (c_parser *);
1227 static void c_parser_objc_methodprotolist (c_parser *);
1228 static void c_parser_objc_methodproto (c_parser *);
1229 static tree c_parser_objc_method_decl (c_parser *, bool, tree *, tree *);
1230 static tree c_parser_objc_type_name (c_parser *);
1231 static tree c_parser_objc_protocol_refs (c_parser *);
1232 static void c_parser_objc_try_catch_finally_statement (c_parser *);
1233 static void c_parser_objc_synchronized_statement (c_parser *);
1234 static tree c_parser_objc_selector (c_parser *);
1235 static tree c_parser_objc_selector_arg (c_parser *);
1236 static tree c_parser_objc_receiver (c_parser *);
1237 static tree c_parser_objc_message_args (c_parser *);
1238 static tree c_parser_objc_keywordexpr (c_parser *);
1239 static void c_parser_objc_at_property_declaration (c_parser *);
1240 static void c_parser_objc_at_synthesize_declaration (c_parser *);
1241 static void c_parser_objc_at_dynamic_declaration (c_parser *);
1242 static bool c_parser_objc_diagnose_bad_element_prefix
1243 (c_parser *, struct c_declspecs *);
1244
1245 /* Cilk Plus supporting routines. */
1246 static void c_parser_cilk_simd (c_parser *);
1247 static bool c_parser_cilk_verify_simd (c_parser *, enum pragma_context);
1248 static tree c_parser_array_notation (location_t, c_parser *, tree, tree);
1249
1250 /* Parse a translation unit (C90 6.7, C99 6.9).
1251
1252 translation-unit:
1253 external-declarations
1254
1255 external-declarations:
1256 external-declaration
1257 external-declarations external-declaration
1258
1259 GNU extensions:
1260
1261 translation-unit:
1262 empty
1263 */
1264
1265 static void
1266 c_parser_translation_unit (c_parser *parser)
1267 {
1268 if (c_parser_next_token_is (parser, CPP_EOF))
1269 {
1270 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
1271 "ISO C forbids an empty translation unit");
1272 }
1273 else
1274 {
1275 void *obstack_position = obstack_alloc (&parser_obstack, 0);
1276 mark_valid_location_for_stdc_pragma (false);
1277 do
1278 {
1279 ggc_collect ();
1280 c_parser_external_declaration (parser);
1281 obstack_free (&parser_obstack, obstack_position);
1282 }
1283 while (c_parser_next_token_is_not (parser, CPP_EOF));
1284 }
1285 }
1286
1287 /* Parse an external declaration (C90 6.7, C99 6.9).
1288
1289 external-declaration:
1290 function-definition
1291 declaration
1292
1293 GNU extensions:
1294
1295 external-declaration:
1296 asm-definition
1297 ;
1298 __extension__ external-declaration
1299
1300 Objective-C:
1301
1302 external-declaration:
1303 objc-class-definition
1304 objc-class-declaration
1305 objc-alias-declaration
1306 objc-protocol-definition
1307 objc-method-definition
1308 @end
1309 */
1310
1311 static void
1312 c_parser_external_declaration (c_parser *parser)
1313 {
1314 int ext;
1315 switch (c_parser_peek_token (parser)->type)
1316 {
1317 case CPP_KEYWORD:
1318 switch (c_parser_peek_token (parser)->keyword)
1319 {
1320 case RID_EXTENSION:
1321 ext = disable_extension_diagnostics ();
1322 c_parser_consume_token (parser);
1323 c_parser_external_declaration (parser);
1324 restore_extension_diagnostics (ext);
1325 break;
1326 case RID_ASM:
1327 c_parser_asm_definition (parser);
1328 break;
1329 case RID_AT_INTERFACE:
1330 case RID_AT_IMPLEMENTATION:
1331 gcc_assert (c_dialect_objc ());
1332 c_parser_objc_class_definition (parser, NULL_TREE);
1333 break;
1334 case RID_AT_CLASS:
1335 gcc_assert (c_dialect_objc ());
1336 c_parser_objc_class_declaration (parser);
1337 break;
1338 case RID_AT_ALIAS:
1339 gcc_assert (c_dialect_objc ());
1340 c_parser_objc_alias_declaration (parser);
1341 break;
1342 case RID_AT_PROTOCOL:
1343 gcc_assert (c_dialect_objc ());
1344 c_parser_objc_protocol_definition (parser, NULL_TREE);
1345 break;
1346 case RID_AT_PROPERTY:
1347 gcc_assert (c_dialect_objc ());
1348 c_parser_objc_at_property_declaration (parser);
1349 break;
1350 case RID_AT_SYNTHESIZE:
1351 gcc_assert (c_dialect_objc ());
1352 c_parser_objc_at_synthesize_declaration (parser);
1353 break;
1354 case RID_AT_DYNAMIC:
1355 gcc_assert (c_dialect_objc ());
1356 c_parser_objc_at_dynamic_declaration (parser);
1357 break;
1358 case RID_AT_END:
1359 gcc_assert (c_dialect_objc ());
1360 c_parser_consume_token (parser);
1361 objc_finish_implementation ();
1362 break;
1363 default:
1364 goto decl_or_fndef;
1365 }
1366 break;
1367 case CPP_SEMICOLON:
1368 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
1369 "ISO C does not allow extra %<;%> outside of a function");
1370 c_parser_consume_token (parser);
1371 break;
1372 case CPP_PRAGMA:
1373 mark_valid_location_for_stdc_pragma (true);
1374 c_parser_pragma (parser, pragma_external);
1375 mark_valid_location_for_stdc_pragma (false);
1376 break;
1377 case CPP_PLUS:
1378 case CPP_MINUS:
1379 if (c_dialect_objc ())
1380 {
1381 c_parser_objc_method_definition (parser);
1382 break;
1383 }
1384 /* Else fall through, and yield a syntax error trying to parse
1385 as a declaration or function definition. */
1386 default:
1387 decl_or_fndef:
1388 /* A declaration or a function definition (or, in Objective-C,
1389 an @interface or @protocol with prefix attributes). We can
1390 only tell which after parsing the declaration specifiers, if
1391 any, and the first declarator. */
1392 c_parser_declaration_or_fndef (parser, true, true, true, false, true,
1393 NULL, vNULL);
1394 break;
1395 }
1396 }
1397
1398 static void c_finish_omp_declare_simd (c_parser *, tree, tree, vec<c_token>);
1399
1400 /* Parse a declaration or function definition (C90 6.5, 6.7.1, C99
1401 6.7, 6.9.1). If FNDEF_OK is true, a function definition is
1402 accepted; otherwise (old-style parameter declarations) only other
1403 declarations are accepted. If STATIC_ASSERT_OK is true, a static
1404 assertion is accepted; otherwise (old-style parameter declarations)
1405 it is not. If NESTED is true, we are inside a function or parsing
1406 old-style parameter declarations; any functions encountered are
1407 nested functions and declaration specifiers are required; otherwise
1408 we are at top level and functions are normal functions and
1409 declaration specifiers may be optional. If EMPTY_OK is true, empty
1410 declarations are OK (subject to all other constraints); otherwise
1411 (old-style parameter declarations) they are diagnosed. If
1412 START_ATTR_OK is true, the declaration specifiers may start with
1413 attributes; otherwise they may not.
1414 OBJC_FOREACH_OBJECT_DECLARATION can be used to get back the parsed
1415 declaration when parsing an Objective-C foreach statement.
1416
1417 declaration:
1418 declaration-specifiers init-declarator-list[opt] ;
1419 static_assert-declaration
1420
1421 function-definition:
1422 declaration-specifiers[opt] declarator declaration-list[opt]
1423 compound-statement
1424
1425 declaration-list:
1426 declaration
1427 declaration-list declaration
1428
1429 init-declarator-list:
1430 init-declarator
1431 init-declarator-list , init-declarator
1432
1433 init-declarator:
1434 declarator simple-asm-expr[opt] attributes[opt]
1435 declarator simple-asm-expr[opt] attributes[opt] = initializer
1436
1437 GNU extensions:
1438
1439 nested-function-definition:
1440 declaration-specifiers declarator declaration-list[opt]
1441 compound-statement
1442
1443 Objective-C:
1444 attributes objc-class-definition
1445 attributes objc-category-definition
1446 attributes objc-protocol-definition
1447
1448 The simple-asm-expr and attributes are GNU extensions.
1449
1450 This function does not handle __extension__; that is handled in its
1451 callers. ??? Following the old parser, __extension__ may start
1452 external declarations, declarations in functions and declarations
1453 at the start of "for" loops, but not old-style parameter
1454 declarations.
1455
1456 C99 requires declaration specifiers in a function definition; the
1457 absence is diagnosed through the diagnosis of implicit int. In GNU
1458 C we also allow but diagnose declarations without declaration
1459 specifiers, but only at top level (elsewhere they conflict with
1460 other syntax).
1461
1462 In Objective-C, declarations of the looping variable in a foreach
1463 statement are exceptionally terminated by 'in' (for example, 'for
1464 (NSObject *object in array) { ... }').
1465
1466 OpenMP:
1467
1468 declaration:
1469 threadprivate-directive */
1470
1471 static void
1472 c_parser_declaration_or_fndef (c_parser *parser, bool fndef_ok,
1473 bool static_assert_ok, bool empty_ok,
1474 bool nested, bool start_attr_ok,
1475 tree *objc_foreach_object_declaration,
1476 vec<c_token> omp_declare_simd_clauses)
1477 {
1478 struct c_declspecs *specs;
1479 tree prefix_attrs;
1480 tree all_prefix_attrs;
1481 bool diagnosed_no_specs = false;
1482 location_t here = c_parser_peek_token (parser)->location;
1483
1484 if (static_assert_ok
1485 && c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT))
1486 {
1487 c_parser_static_assert_declaration (parser);
1488 return;
1489 }
1490 specs = build_null_declspecs ();
1491
1492 /* Try to detect an unknown type name when we have "A B" or "A *B". */
1493 if (c_parser_peek_token (parser)->type == CPP_NAME
1494 && c_parser_peek_token (parser)->id_kind == C_ID_ID
1495 && (c_parser_peek_2nd_token (parser)->type == CPP_NAME
1496 || c_parser_peek_2nd_token (parser)->type == CPP_MULT)
1497 && (!nested || !lookup_name (c_parser_peek_token (parser)->value)))
1498 {
1499 error_at (here, "unknown type name %qE",
1500 c_parser_peek_token (parser)->value);
1501
1502 /* Parse declspecs normally to get a correct pointer type, but avoid
1503 a further "fails to be a type name" error. Refuse nested functions
1504 since it is not how the user likely wants us to recover. */
1505 c_parser_peek_token (parser)->type = CPP_KEYWORD;
1506 c_parser_peek_token (parser)->keyword = RID_VOID;
1507 c_parser_peek_token (parser)->value = error_mark_node;
1508 fndef_ok = !nested;
1509 }
1510
1511 c_parser_declspecs (parser, specs, true, true, start_attr_ok,
1512 true, true, cla_nonabstract_decl);
1513 if (parser->error)
1514 {
1515 c_parser_skip_to_end_of_block_or_statement (parser);
1516 return;
1517 }
1518 if (nested && !specs->declspecs_seen_p)
1519 {
1520 c_parser_error (parser, "expected declaration specifiers");
1521 c_parser_skip_to_end_of_block_or_statement (parser);
1522 return;
1523 }
1524 finish_declspecs (specs);
1525 bool auto_type_p = specs->typespec_word == cts_auto_type;
1526 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1527 {
1528 if (auto_type_p)
1529 error_at (here, "%<__auto_type%> in empty declaration");
1530 else if (empty_ok)
1531 shadow_tag (specs);
1532 else
1533 {
1534 shadow_tag_warned (specs, 1);
1535 pedwarn (here, 0, "empty declaration");
1536 }
1537 c_parser_consume_token (parser);
1538 return;
1539 }
1540
1541 /* Provide better error recovery. Note that a type name here is usually
1542 better diagnosed as a redeclaration. */
1543 if (empty_ok
1544 && specs->typespec_kind == ctsk_tagdef
1545 && c_parser_next_token_starts_declspecs (parser)
1546 && !c_parser_next_token_is (parser, CPP_NAME))
1547 {
1548 c_parser_error (parser, "expected %<;%>, identifier or %<(%>");
1549 parser->error = false;
1550 shadow_tag_warned (specs, 1);
1551 return;
1552 }
1553 else if (c_dialect_objc () && !auto_type_p)
1554 {
1555 /* Prefix attributes are an error on method decls. */
1556 switch (c_parser_peek_token (parser)->type)
1557 {
1558 case CPP_PLUS:
1559 case CPP_MINUS:
1560 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1561 return;
1562 if (specs->attrs)
1563 {
1564 warning_at (c_parser_peek_token (parser)->location,
1565 OPT_Wattributes,
1566 "prefix attributes are ignored for methods");
1567 specs->attrs = NULL_TREE;
1568 }
1569 if (fndef_ok)
1570 c_parser_objc_method_definition (parser);
1571 else
1572 c_parser_objc_methodproto (parser);
1573 return;
1574 break;
1575 default:
1576 break;
1577 }
1578 /* This is where we parse 'attributes @interface ...',
1579 'attributes @implementation ...', 'attributes @protocol ...'
1580 (where attributes could be, for example, __attribute__
1581 ((deprecated)).
1582 */
1583 switch (c_parser_peek_token (parser)->keyword)
1584 {
1585 case RID_AT_INTERFACE:
1586 {
1587 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1588 return;
1589 c_parser_objc_class_definition (parser, specs->attrs);
1590 return;
1591 }
1592 break;
1593 case RID_AT_IMPLEMENTATION:
1594 {
1595 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1596 return;
1597 if (specs->attrs)
1598 {
1599 warning_at (c_parser_peek_token (parser)->location,
1600 OPT_Wattributes,
1601 "prefix attributes are ignored for implementations");
1602 specs->attrs = NULL_TREE;
1603 }
1604 c_parser_objc_class_definition (parser, NULL_TREE);
1605 return;
1606 }
1607 break;
1608 case RID_AT_PROTOCOL:
1609 {
1610 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1611 return;
1612 c_parser_objc_protocol_definition (parser, specs->attrs);
1613 return;
1614 }
1615 break;
1616 case RID_AT_ALIAS:
1617 case RID_AT_CLASS:
1618 case RID_AT_END:
1619 case RID_AT_PROPERTY:
1620 if (specs->attrs)
1621 {
1622 c_parser_error (parser, "unexpected attribute");
1623 specs->attrs = NULL;
1624 }
1625 break;
1626 default:
1627 break;
1628 }
1629 }
1630
1631 pending_xref_error ();
1632 prefix_attrs = specs->attrs;
1633 all_prefix_attrs = prefix_attrs;
1634 specs->attrs = NULL_TREE;
1635 while (true)
1636 {
1637 struct c_declarator *declarator;
1638 bool dummy = false;
1639 timevar_id_t tv;
1640 tree fnbody;
1641 /* Declaring either one or more declarators (in which case we
1642 should diagnose if there were no declaration specifiers) or a
1643 function definition (in which case the diagnostic for
1644 implicit int suffices). */
1645 declarator = c_parser_declarator (parser,
1646 specs->typespec_kind != ctsk_none,
1647 C_DTR_NORMAL, &dummy);
1648 if (declarator == NULL)
1649 {
1650 if (omp_declare_simd_clauses.exists ())
1651 c_finish_omp_declare_simd (parser, NULL_TREE, NULL_TREE,
1652 omp_declare_simd_clauses);
1653 c_parser_skip_to_end_of_block_or_statement (parser);
1654 return;
1655 }
1656 if (auto_type_p && declarator->kind != cdk_id)
1657 {
1658 error_at (here,
1659 "%<__auto_type%> requires a plain identifier"
1660 " as declarator");
1661 c_parser_skip_to_end_of_block_or_statement (parser);
1662 return;
1663 }
1664 if (c_parser_next_token_is (parser, CPP_EQ)
1665 || c_parser_next_token_is (parser, CPP_COMMA)
1666 || c_parser_next_token_is (parser, CPP_SEMICOLON)
1667 || c_parser_next_token_is_keyword (parser, RID_ASM)
1668 || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE)
1669 || c_parser_next_token_is_keyword (parser, RID_IN))
1670 {
1671 tree asm_name = NULL_TREE;
1672 tree postfix_attrs = NULL_TREE;
1673 if (!diagnosed_no_specs && !specs->declspecs_seen_p)
1674 {
1675 diagnosed_no_specs = true;
1676 pedwarn (here, 0, "data definition has no type or storage class");
1677 }
1678 /* Having seen a data definition, there cannot now be a
1679 function definition. */
1680 fndef_ok = false;
1681 if (c_parser_next_token_is_keyword (parser, RID_ASM))
1682 asm_name = c_parser_simple_asm_expr (parser);
1683 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1684 postfix_attrs = c_parser_attributes (parser);
1685 if (c_parser_next_token_is (parser, CPP_EQ))
1686 {
1687 tree d;
1688 struct c_expr init;
1689 location_t init_loc;
1690 c_parser_consume_token (parser);
1691 if (auto_type_p)
1692 {
1693 start_init (NULL_TREE, asm_name, global_bindings_p ());
1694 init_loc = c_parser_peek_token (parser)->location;
1695 init = c_parser_expr_no_commas (parser, NULL);
1696 if (TREE_CODE (init.value) == COMPONENT_REF
1697 && DECL_C_BIT_FIELD (TREE_OPERAND (init.value, 1)))
1698 error_at (here,
1699 "%<__auto_type%> used with a bit-field"
1700 " initializer");
1701 init = convert_lvalue_to_rvalue (init_loc, init, true, true);
1702 tree init_type = TREE_TYPE (init.value);
1703 /* As with typeof, remove _Atomic and const
1704 qualifiers from atomic types. */
1705 if (init_type != error_mark_node && TYPE_ATOMIC (init_type))
1706 init_type
1707 = c_build_qualified_type (init_type,
1708 (TYPE_QUALS (init_type)
1709 & ~(TYPE_QUAL_ATOMIC
1710 | TYPE_QUAL_CONST)));
1711 bool vm_type = variably_modified_type_p (init_type,
1712 NULL_TREE);
1713 if (vm_type)
1714 init.value = c_save_expr (init.value);
1715 finish_init ();
1716 specs->typespec_kind = ctsk_typeof;
1717 specs->locations[cdw_typedef] = init_loc;
1718 specs->typedef_p = true;
1719 specs->type = init_type;
1720 if (vm_type)
1721 {
1722 bool maybe_const = true;
1723 tree type_expr = c_fully_fold (init.value, false,
1724 &maybe_const);
1725 specs->expr_const_operands &= maybe_const;
1726 if (specs->expr)
1727 specs->expr = build2 (COMPOUND_EXPR,
1728 TREE_TYPE (type_expr),
1729 specs->expr, type_expr);
1730 else
1731 specs->expr = type_expr;
1732 }
1733 d = start_decl (declarator, specs, true,
1734 chainon (postfix_attrs, all_prefix_attrs));
1735 if (!d)
1736 d = error_mark_node;
1737 if (omp_declare_simd_clauses.exists ())
1738 c_finish_omp_declare_simd (parser, d, NULL_TREE,
1739 omp_declare_simd_clauses);
1740 }
1741 else
1742 {
1743 /* The declaration of the variable is in effect while
1744 its initializer is parsed. */
1745 d = start_decl (declarator, specs, true,
1746 chainon (postfix_attrs, all_prefix_attrs));
1747 if (!d)
1748 d = error_mark_node;
1749 if (omp_declare_simd_clauses.exists ())
1750 c_finish_omp_declare_simd (parser, d, NULL_TREE,
1751 omp_declare_simd_clauses);
1752 start_init (d, asm_name, global_bindings_p ());
1753 init_loc = c_parser_peek_token (parser)->location;
1754 init = c_parser_initializer (parser);
1755 finish_init ();
1756 }
1757 if (d != error_mark_node)
1758 {
1759 maybe_warn_string_init (TREE_TYPE (d), init);
1760 finish_decl (d, init_loc, init.value,
1761 init.original_type, asm_name);
1762 }
1763 }
1764 else
1765 {
1766 if (auto_type_p)
1767 {
1768 error_at (here,
1769 "%<__auto_type%> requires an initialized "
1770 "data declaration");
1771 c_parser_skip_to_end_of_block_or_statement (parser);
1772 return;
1773 }
1774 tree d = start_decl (declarator, specs, false,
1775 chainon (postfix_attrs,
1776 all_prefix_attrs));
1777 if (omp_declare_simd_clauses.exists ())
1778 {
1779 tree parms = NULL_TREE;
1780 if (d && TREE_CODE (d) == FUNCTION_DECL)
1781 {
1782 struct c_declarator *ce = declarator;
1783 while (ce != NULL)
1784 if (ce->kind == cdk_function)
1785 {
1786 parms = ce->u.arg_info->parms;
1787 break;
1788 }
1789 else
1790 ce = ce->declarator;
1791 }
1792 if (parms)
1793 temp_store_parm_decls (d, parms);
1794 c_finish_omp_declare_simd (parser, d, parms,
1795 omp_declare_simd_clauses);
1796 if (parms)
1797 temp_pop_parm_decls ();
1798 }
1799 if (d)
1800 finish_decl (d, UNKNOWN_LOCATION, NULL_TREE,
1801 NULL_TREE, asm_name);
1802
1803 if (c_parser_next_token_is_keyword (parser, RID_IN))
1804 {
1805 if (d)
1806 *objc_foreach_object_declaration = d;
1807 else
1808 *objc_foreach_object_declaration = error_mark_node;
1809 }
1810 }
1811 if (c_parser_next_token_is (parser, CPP_COMMA))
1812 {
1813 if (auto_type_p)
1814 {
1815 error_at (here,
1816 "%<__auto_type%> may only be used with"
1817 " a single declarator");
1818 c_parser_skip_to_end_of_block_or_statement (parser);
1819 return;
1820 }
1821 c_parser_consume_token (parser);
1822 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1823 all_prefix_attrs = chainon (c_parser_attributes (parser),
1824 prefix_attrs);
1825 else
1826 all_prefix_attrs = prefix_attrs;
1827 continue;
1828 }
1829 else if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1830 {
1831 c_parser_consume_token (parser);
1832 return;
1833 }
1834 else if (c_parser_next_token_is_keyword (parser, RID_IN))
1835 {
1836 /* This can only happen in Objective-C: we found the
1837 'in' that terminates the declaration inside an
1838 Objective-C foreach statement. Do not consume the
1839 token, so that the caller can use it to determine
1840 that this indeed is a foreach context. */
1841 return;
1842 }
1843 else
1844 {
1845 c_parser_error (parser, "expected %<,%> or %<;%>");
1846 c_parser_skip_to_end_of_block_or_statement (parser);
1847 return;
1848 }
1849 }
1850 else if (auto_type_p)
1851 {
1852 error_at (here,
1853 "%<__auto_type%> requires an initialized data declaration");
1854 c_parser_skip_to_end_of_block_or_statement (parser);
1855 return;
1856 }
1857 else if (!fndef_ok)
1858 {
1859 c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, "
1860 "%<asm%> or %<__attribute__%>");
1861 c_parser_skip_to_end_of_block_or_statement (parser);
1862 return;
1863 }
1864 /* Function definition (nested or otherwise). */
1865 if (nested)
1866 {
1867 pedwarn (here, OPT_Wpedantic, "ISO C forbids nested functions");
1868 c_push_function_context ();
1869 }
1870 if (!start_function (specs, declarator, all_prefix_attrs))
1871 {
1872 /* This can appear in many cases looking nothing like a
1873 function definition, so we don't give a more specific
1874 error suggesting there was one. */
1875 c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, %<asm%> "
1876 "or %<__attribute__%>");
1877 if (nested)
1878 c_pop_function_context ();
1879 break;
1880 }
1881
1882 if (DECL_DECLARED_INLINE_P (current_function_decl))
1883 tv = TV_PARSE_INLINE;
1884 else
1885 tv = TV_PARSE_FUNC;
1886 timevar_push (tv);
1887
1888 /* Parse old-style parameter declarations. ??? Attributes are
1889 not allowed to start declaration specifiers here because of a
1890 syntax conflict between a function declaration with attribute
1891 suffix and a function definition with an attribute prefix on
1892 first old-style parameter declaration. Following the old
1893 parser, they are not accepted on subsequent old-style
1894 parameter declarations either. However, there is no
1895 ambiguity after the first declaration, nor indeed on the
1896 first as long as we don't allow postfix attributes after a
1897 declarator with a nonempty identifier list in a definition;
1898 and postfix attributes have never been accepted here in
1899 function definitions either. */
1900 while (c_parser_next_token_is_not (parser, CPP_EOF)
1901 && c_parser_next_token_is_not (parser, CPP_OPEN_BRACE))
1902 c_parser_declaration_or_fndef (parser, false, false, false,
1903 true, false, NULL, vNULL);
1904 store_parm_decls ();
1905 if (omp_declare_simd_clauses.exists ())
1906 c_finish_omp_declare_simd (parser, current_function_decl, NULL_TREE,
1907 omp_declare_simd_clauses);
1908 DECL_STRUCT_FUNCTION (current_function_decl)->function_start_locus
1909 = c_parser_peek_token (parser)->location;
1910 fnbody = c_parser_compound_statement (parser);
1911 if (flag_enable_cilkplus && contains_array_notation_expr (fnbody))
1912 fnbody = expand_array_notation_exprs (fnbody);
1913 if (nested)
1914 {
1915 tree decl = current_function_decl;
1916 /* Mark nested functions as needing static-chain initially.
1917 lower_nested_functions will recompute it but the
1918 DECL_STATIC_CHAIN flag is also used before that happens,
1919 by initializer_constant_valid_p. See gcc.dg/nested-fn-2.c. */
1920 DECL_STATIC_CHAIN (decl) = 1;
1921 add_stmt (fnbody);
1922 finish_function ();
1923 c_pop_function_context ();
1924 add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl));
1925 }
1926 else
1927 {
1928 add_stmt (fnbody);
1929 finish_function ();
1930 }
1931
1932 timevar_pop (tv);
1933 break;
1934 }
1935 }
1936
1937 /* Parse an asm-definition (asm() outside a function body). This is a
1938 GNU extension.
1939
1940 asm-definition:
1941 simple-asm-expr ;
1942 */
1943
1944 static void
1945 c_parser_asm_definition (c_parser *parser)
1946 {
1947 tree asm_str = c_parser_simple_asm_expr (parser);
1948 if (asm_str)
1949 add_asm_node (asm_str);
1950 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
1951 }
1952
1953 /* Parse a static assertion (C11 6.7.10).
1954
1955 static_assert-declaration:
1956 static_assert-declaration-no-semi ;
1957 */
1958
1959 static void
1960 c_parser_static_assert_declaration (c_parser *parser)
1961 {
1962 c_parser_static_assert_declaration_no_semi (parser);
1963 if (parser->error
1964 || !c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
1965 c_parser_skip_to_end_of_block_or_statement (parser);
1966 }
1967
1968 /* Parse a static assertion (C11 6.7.10), without the trailing
1969 semicolon.
1970
1971 static_assert-declaration-no-semi:
1972 _Static_assert ( constant-expression , string-literal )
1973 */
1974
1975 static void
1976 c_parser_static_assert_declaration_no_semi (c_parser *parser)
1977 {
1978 location_t assert_loc, value_loc;
1979 tree value;
1980 tree string;
1981
1982 gcc_assert (c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT));
1983 assert_loc = c_parser_peek_token (parser)->location;
1984 if (!flag_isoc11)
1985 {
1986 if (flag_isoc99)
1987 pedwarn (assert_loc, OPT_Wpedantic,
1988 "ISO C99 does not support %<_Static_assert%>");
1989 else
1990 pedwarn (assert_loc, OPT_Wpedantic,
1991 "ISO C90 does not support %<_Static_assert%>");
1992 }
1993 c_parser_consume_token (parser);
1994 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
1995 return;
1996 value_loc = c_parser_peek_token (parser)->location;
1997 value = c_parser_expr_no_commas (parser, NULL).value;
1998 parser->lex_untranslated_string = true;
1999 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
2000 {
2001 parser->lex_untranslated_string = false;
2002 return;
2003 }
2004 switch (c_parser_peek_token (parser)->type)
2005 {
2006 case CPP_STRING:
2007 case CPP_STRING16:
2008 case CPP_STRING32:
2009 case CPP_WSTRING:
2010 case CPP_UTF8STRING:
2011 string = c_parser_peek_token (parser)->value;
2012 c_parser_consume_token (parser);
2013 parser->lex_untranslated_string = false;
2014 break;
2015 default:
2016 c_parser_error (parser, "expected string literal");
2017 parser->lex_untranslated_string = false;
2018 return;
2019 }
2020 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
2021
2022 if (!INTEGRAL_TYPE_P (TREE_TYPE (value)))
2023 {
2024 error_at (value_loc, "expression in static assertion is not an integer");
2025 return;
2026 }
2027 if (TREE_CODE (value) != INTEGER_CST)
2028 {
2029 value = c_fully_fold (value, false, NULL);
2030 if (TREE_CODE (value) == INTEGER_CST)
2031 pedwarn (value_loc, OPT_Wpedantic, "expression in static assertion "
2032 "is not an integer constant expression");
2033 }
2034 if (TREE_CODE (value) != INTEGER_CST)
2035 {
2036 error_at (value_loc, "expression in static assertion is not constant");
2037 return;
2038 }
2039 constant_expression_warning (value);
2040 if (integer_zerop (value))
2041 error_at (assert_loc, "static assertion failed: %E", string);
2042 }
2043
2044 /* Parse some declaration specifiers (possibly none) (C90 6.5, C99
2045 6.7), adding them to SPECS (which may already include some).
2046 Storage class specifiers are accepted iff SCSPEC_OK; type
2047 specifiers are accepted iff TYPESPEC_OK; alignment specifiers are
2048 accepted iff ALIGNSPEC_OK; attributes are accepted at the start
2049 iff START_ATTR_OK; __auto_type is accepted iff AUTO_TYPE_OK.
2050
2051 declaration-specifiers:
2052 storage-class-specifier declaration-specifiers[opt]
2053 type-specifier declaration-specifiers[opt]
2054 type-qualifier declaration-specifiers[opt]
2055 function-specifier declaration-specifiers[opt]
2056 alignment-specifier declaration-specifiers[opt]
2057
2058 Function specifiers (inline) are from C99, and are currently
2059 handled as storage class specifiers, as is __thread. Alignment
2060 specifiers are from C11.
2061
2062 C90 6.5.1, C99 6.7.1:
2063 storage-class-specifier:
2064 typedef
2065 extern
2066 static
2067 auto
2068 register
2069 _Thread_local
2070
2071 (_Thread_local is new in C11.)
2072
2073 C99 6.7.4:
2074 function-specifier:
2075 inline
2076 _Noreturn
2077
2078 (_Noreturn is new in C11.)
2079
2080 C90 6.5.2, C99 6.7.2:
2081 type-specifier:
2082 void
2083 char
2084 short
2085 int
2086 long
2087 float
2088 double
2089 signed
2090 unsigned
2091 _Bool
2092 _Complex
2093 [_Imaginary removed in C99 TC2]
2094 struct-or-union-specifier
2095 enum-specifier
2096 typedef-name
2097 atomic-type-specifier
2098
2099 (_Bool and _Complex are new in C99.)
2100 (atomic-type-specifier is new in C11.)
2101
2102 C90 6.5.3, C99 6.7.3:
2103
2104 type-qualifier:
2105 const
2106 restrict
2107 volatile
2108 address-space-qualifier
2109 _Atomic
2110
2111 (restrict is new in C99.)
2112 (_Atomic is new in C11.)
2113
2114 GNU extensions:
2115
2116 declaration-specifiers:
2117 attributes declaration-specifiers[opt]
2118
2119 type-qualifier:
2120 address-space
2121
2122 address-space:
2123 identifier recognized by the target
2124
2125 storage-class-specifier:
2126 __thread
2127
2128 type-specifier:
2129 typeof-specifier
2130 __auto_type
2131 __int128
2132 _Decimal32
2133 _Decimal64
2134 _Decimal128
2135 _Fract
2136 _Accum
2137 _Sat
2138
2139 (_Fract, _Accum, and _Sat are new from ISO/IEC DTR 18037:
2140 http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1169.pdf)
2141
2142 atomic-type-specifier
2143 _Atomic ( type-name )
2144
2145 Objective-C:
2146
2147 type-specifier:
2148 class-name objc-protocol-refs[opt]
2149 typedef-name objc-protocol-refs
2150 objc-protocol-refs
2151 */
2152
2153 static void
2154 c_parser_declspecs (c_parser *parser, struct c_declspecs *specs,
2155 bool scspec_ok, bool typespec_ok, bool start_attr_ok,
2156 bool alignspec_ok, bool auto_type_ok,
2157 enum c_lookahead_kind la)
2158 {
2159 bool attrs_ok = start_attr_ok;
2160 bool seen_type = specs->typespec_kind != ctsk_none;
2161
2162 if (!typespec_ok)
2163 gcc_assert (la == cla_prefer_id);
2164
2165 while (c_parser_next_token_is (parser, CPP_NAME)
2166 || c_parser_next_token_is (parser, CPP_KEYWORD)
2167 || (c_dialect_objc () && c_parser_next_token_is (parser, CPP_LESS)))
2168 {
2169 struct c_typespec t;
2170 tree attrs;
2171 tree align;
2172 location_t loc = c_parser_peek_token (parser)->location;
2173
2174 /* If we cannot accept a type, exit if the next token must start
2175 one. Also, if we already have seen a tagged definition,
2176 a typename would be an error anyway and likely the user
2177 has simply forgotten a semicolon, so we exit. */
2178 if ((!typespec_ok || specs->typespec_kind == ctsk_tagdef)
2179 && c_parser_next_tokens_start_typename (parser, la)
2180 && !c_parser_next_token_is_qualifier (parser))
2181 break;
2182
2183 if (c_parser_next_token_is (parser, CPP_NAME))
2184 {
2185 c_token *name_token = c_parser_peek_token (parser);
2186 tree value = name_token->value;
2187 c_id_kind kind = name_token->id_kind;
2188
2189 if (kind == C_ID_ADDRSPACE)
2190 {
2191 addr_space_t as
2192 = name_token->keyword - RID_FIRST_ADDR_SPACE;
2193 declspecs_add_addrspace (name_token->location, specs, as);
2194 c_parser_consume_token (parser);
2195 attrs_ok = true;
2196 continue;
2197 }
2198
2199 gcc_assert (!c_parser_next_token_is_qualifier (parser));
2200
2201 /* If we cannot accept a type, and the next token must start one,
2202 exit. Do the same if we already have seen a tagged definition,
2203 since it would be an error anyway and likely the user has simply
2204 forgotten a semicolon. */
2205 if (seen_type || !c_parser_next_tokens_start_typename (parser, la))
2206 break;
2207
2208 /* Now at an unknown typename (C_ID_ID), a C_ID_TYPENAME or
2209 a C_ID_CLASSNAME. */
2210 c_parser_consume_token (parser);
2211 seen_type = true;
2212 attrs_ok = true;
2213 if (kind == C_ID_ID)
2214 {
2215 error ("unknown type name %qE", value);
2216 t.kind = ctsk_typedef;
2217 t.spec = error_mark_node;
2218 }
2219 else if (kind == C_ID_TYPENAME
2220 && (!c_dialect_objc ()
2221 || c_parser_next_token_is_not (parser, CPP_LESS)))
2222 {
2223 t.kind = ctsk_typedef;
2224 /* For a typedef name, record the meaning, not the name.
2225 In case of 'foo foo, bar;'. */
2226 t.spec = lookup_name (value);
2227 }
2228 else
2229 {
2230 tree proto = NULL_TREE;
2231 gcc_assert (c_dialect_objc ());
2232 t.kind = ctsk_objc;
2233 if (c_parser_next_token_is (parser, CPP_LESS))
2234 proto = c_parser_objc_protocol_refs (parser);
2235 t.spec = objc_get_protocol_qualified_type (value, proto);
2236 }
2237 t.expr = NULL_TREE;
2238 t.expr_const_operands = true;
2239 declspecs_add_type (name_token->location, specs, t);
2240 continue;
2241 }
2242 if (c_parser_next_token_is (parser, CPP_LESS))
2243 {
2244 /* Make "<SomeProtocol>" equivalent to "id <SomeProtocol>" -
2245 nisse@lysator.liu.se. */
2246 tree proto;
2247 gcc_assert (c_dialect_objc ());
2248 if (!typespec_ok || seen_type)
2249 break;
2250 proto = c_parser_objc_protocol_refs (parser);
2251 t.kind = ctsk_objc;
2252 t.spec = objc_get_protocol_qualified_type (NULL_TREE, proto);
2253 t.expr = NULL_TREE;
2254 t.expr_const_operands = true;
2255 declspecs_add_type (loc, specs, t);
2256 continue;
2257 }
2258 gcc_assert (c_parser_next_token_is (parser, CPP_KEYWORD));
2259 switch (c_parser_peek_token (parser)->keyword)
2260 {
2261 case RID_STATIC:
2262 case RID_EXTERN:
2263 case RID_REGISTER:
2264 case RID_TYPEDEF:
2265 case RID_INLINE:
2266 case RID_NORETURN:
2267 case RID_AUTO:
2268 case RID_THREAD:
2269 if (!scspec_ok)
2270 goto out;
2271 attrs_ok = true;
2272 /* TODO: Distinguish between function specifiers (inline, noreturn)
2273 and storage class specifiers, either here or in
2274 declspecs_add_scspec. */
2275 declspecs_add_scspec (loc, specs,
2276 c_parser_peek_token (parser)->value);
2277 c_parser_consume_token (parser);
2278 break;
2279 case RID_AUTO_TYPE:
2280 if (!auto_type_ok)
2281 goto out;
2282 /* Fall through. */
2283 case RID_UNSIGNED:
2284 case RID_LONG:
2285 case RID_INT128:
2286 case RID_SHORT:
2287 case RID_SIGNED:
2288 case RID_COMPLEX:
2289 case RID_INT:
2290 case RID_CHAR:
2291 case RID_FLOAT:
2292 case RID_DOUBLE:
2293 case RID_VOID:
2294 case RID_DFLOAT32:
2295 case RID_DFLOAT64:
2296 case RID_DFLOAT128:
2297 case RID_BOOL:
2298 case RID_FRACT:
2299 case RID_ACCUM:
2300 case RID_SAT:
2301 if (!typespec_ok)
2302 goto out;
2303 attrs_ok = true;
2304 seen_type = true;
2305 if (c_dialect_objc ())
2306 parser->objc_need_raw_identifier = true;
2307 t.kind = ctsk_resword;
2308 t.spec = c_parser_peek_token (parser)->value;
2309 t.expr = NULL_TREE;
2310 t.expr_const_operands = true;
2311 declspecs_add_type (loc, specs, t);
2312 c_parser_consume_token (parser);
2313 break;
2314 case RID_ENUM:
2315 if (!typespec_ok)
2316 goto out;
2317 attrs_ok = true;
2318 seen_type = true;
2319 t = c_parser_enum_specifier (parser);
2320 declspecs_add_type (loc, specs, t);
2321 break;
2322 case RID_STRUCT:
2323 case RID_UNION:
2324 if (!typespec_ok)
2325 goto out;
2326 attrs_ok = true;
2327 seen_type = true;
2328 t = c_parser_struct_or_union_specifier (parser);
2329 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, t.spec);
2330 declspecs_add_type (loc, specs, t);
2331 break;
2332 case RID_TYPEOF:
2333 /* ??? The old parser rejected typeof after other type
2334 specifiers, but is a syntax error the best way of
2335 handling this? */
2336 if (!typespec_ok || seen_type)
2337 goto out;
2338 attrs_ok = true;
2339 seen_type = true;
2340 t = c_parser_typeof_specifier (parser);
2341 declspecs_add_type (loc, specs, t);
2342 break;
2343 case RID_ATOMIC:
2344 /* C parser handling of Objective-C constructs needs
2345 checking for correct lvalue-to-rvalue conversions, and
2346 the code in build_modify_expr handling various
2347 Objective-C cases, and that in build_unary_op handling
2348 Objective-C cases for increment / decrement, also needs
2349 updating; uses of TYPE_MAIN_VARIANT in objc_compare_types
2350 and objc_types_are_equivalent may also need updates. */
2351 if (c_dialect_objc ())
2352 sorry ("%<_Atomic%> in Objective-C");
2353 /* C parser handling of OpenMP constructs needs checking for
2354 correct lvalue-to-rvalue conversions. */
2355 if (flag_openmp)
2356 sorry ("%<_Atomic%> with OpenMP");
2357 if (!flag_isoc11)
2358 {
2359 if (flag_isoc99)
2360 pedwarn (loc, OPT_Wpedantic,
2361 "ISO C99 does not support the %<_Atomic%> qualifier");
2362 else
2363 pedwarn (loc, OPT_Wpedantic,
2364 "ISO C90 does not support the %<_Atomic%> qualifier");
2365 }
2366 attrs_ok = true;
2367 tree value;
2368 value = c_parser_peek_token (parser)->value;
2369 c_parser_consume_token (parser);
2370 if (typespec_ok && c_parser_next_token_is (parser, CPP_OPEN_PAREN))
2371 {
2372 /* _Atomic ( type-name ). */
2373 seen_type = true;
2374 c_parser_consume_token (parser);
2375 struct c_type_name *type = c_parser_type_name (parser);
2376 t.kind = ctsk_typeof;
2377 t.spec = error_mark_node;
2378 t.expr = NULL_TREE;
2379 t.expr_const_operands = true;
2380 if (type != NULL)
2381 t.spec = groktypename (type, &t.expr,
2382 &t.expr_const_operands);
2383 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2384 "expected %<)%>");
2385 if (t.spec != error_mark_node)
2386 {
2387 if (TREE_CODE (t.spec) == ARRAY_TYPE)
2388 error_at (loc, "%<_Atomic%>-qualified array type");
2389 else if (TREE_CODE (t.spec) == FUNCTION_TYPE)
2390 error_at (loc, "%<_Atomic%>-qualified function type");
2391 else if (TYPE_QUALS (t.spec) != TYPE_UNQUALIFIED)
2392 error_at (loc, "%<_Atomic%> applied to a qualified type");
2393 else
2394 t.spec = c_build_qualified_type (t.spec, TYPE_QUAL_ATOMIC);
2395 }
2396 declspecs_add_type (loc, specs, t);
2397 }
2398 else
2399 declspecs_add_qual (loc, specs, value);
2400 break;
2401 case RID_CONST:
2402 case RID_VOLATILE:
2403 case RID_RESTRICT:
2404 attrs_ok = true;
2405 declspecs_add_qual (loc, specs, c_parser_peek_token (parser)->value);
2406 c_parser_consume_token (parser);
2407 break;
2408 case RID_ATTRIBUTE:
2409 if (!attrs_ok)
2410 goto out;
2411 attrs = c_parser_attributes (parser);
2412 declspecs_add_attrs (loc, specs, attrs);
2413 break;
2414 case RID_ALIGNAS:
2415 if (!alignspec_ok)
2416 goto out;
2417 align = c_parser_alignas_specifier (parser);
2418 declspecs_add_alignas (loc, specs, align);
2419 break;
2420 default:
2421 goto out;
2422 }
2423 }
2424 out: ;
2425 }
2426
2427 /* Parse an enum specifier (C90 6.5.2.2, C99 6.7.2.2).
2428
2429 enum-specifier:
2430 enum attributes[opt] identifier[opt] { enumerator-list } attributes[opt]
2431 enum attributes[opt] identifier[opt] { enumerator-list , } attributes[opt]
2432 enum attributes[opt] identifier
2433
2434 The form with trailing comma is new in C99. The forms with
2435 attributes are GNU extensions. In GNU C, we accept any expression
2436 without commas in the syntax (assignment expressions, not just
2437 conditional expressions); assignment expressions will be diagnosed
2438 as non-constant.
2439
2440 enumerator-list:
2441 enumerator
2442 enumerator-list , enumerator
2443
2444 enumerator:
2445 enumeration-constant
2446 enumeration-constant = constant-expression
2447 */
2448
2449 static struct c_typespec
2450 c_parser_enum_specifier (c_parser *parser)
2451 {
2452 struct c_typespec ret;
2453 tree attrs;
2454 tree ident = NULL_TREE;
2455 location_t enum_loc;
2456 location_t ident_loc = UNKNOWN_LOCATION; /* Quiet warning. */
2457 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ENUM));
2458 enum_loc = c_parser_peek_token (parser)->location;
2459 c_parser_consume_token (parser);
2460 attrs = c_parser_attributes (parser);
2461 enum_loc = c_parser_peek_token (parser)->location;
2462 /* Set the location in case we create a decl now. */
2463 c_parser_set_source_position_from_token (c_parser_peek_token (parser));
2464 if (c_parser_next_token_is (parser, CPP_NAME))
2465 {
2466 ident = c_parser_peek_token (parser)->value;
2467 ident_loc = c_parser_peek_token (parser)->location;
2468 enum_loc = ident_loc;
2469 c_parser_consume_token (parser);
2470 }
2471 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
2472 {
2473 /* Parse an enum definition. */
2474 struct c_enum_contents the_enum;
2475 tree type;
2476 tree postfix_attrs;
2477 /* We chain the enumerators in reverse order, then put them in
2478 forward order at the end. */
2479 tree values;
2480 timevar_push (TV_PARSE_ENUM);
2481 type = start_enum (enum_loc, &the_enum, ident);
2482 values = NULL_TREE;
2483 c_parser_consume_token (parser);
2484 while (true)
2485 {
2486 tree enum_id;
2487 tree enum_value;
2488 tree enum_decl;
2489 bool seen_comma;
2490 c_token *token;
2491 location_t comma_loc = UNKNOWN_LOCATION; /* Quiet warning. */
2492 location_t decl_loc, value_loc;
2493 if (c_parser_next_token_is_not (parser, CPP_NAME))
2494 {
2495 c_parser_error (parser, "expected identifier");
2496 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
2497 values = error_mark_node;
2498 break;
2499 }
2500 token = c_parser_peek_token (parser);
2501 enum_id = token->value;
2502 /* Set the location in case we create a decl now. */
2503 c_parser_set_source_position_from_token (token);
2504 decl_loc = value_loc = token->location;
2505 c_parser_consume_token (parser);
2506 if (c_parser_next_token_is (parser, CPP_EQ))
2507 {
2508 c_parser_consume_token (parser);
2509 value_loc = c_parser_peek_token (parser)->location;
2510 enum_value = c_parser_expr_no_commas (parser, NULL).value;
2511 }
2512 else
2513 enum_value = NULL_TREE;
2514 enum_decl = build_enumerator (decl_loc, value_loc,
2515 &the_enum, enum_id, enum_value);
2516 TREE_CHAIN (enum_decl) = values;
2517 values = enum_decl;
2518 seen_comma = false;
2519 if (c_parser_next_token_is (parser, CPP_COMMA))
2520 {
2521 comma_loc = c_parser_peek_token (parser)->location;
2522 seen_comma = true;
2523 c_parser_consume_token (parser);
2524 }
2525 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2526 {
2527 if (seen_comma && !flag_isoc99)
2528 pedwarn (comma_loc, OPT_Wpedantic, "comma at end of enumerator list");
2529 c_parser_consume_token (parser);
2530 break;
2531 }
2532 if (!seen_comma)
2533 {
2534 c_parser_error (parser, "expected %<,%> or %<}%>");
2535 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
2536 values = error_mark_node;
2537 break;
2538 }
2539 }
2540 postfix_attrs = c_parser_attributes (parser);
2541 ret.spec = finish_enum (type, nreverse (values),
2542 chainon (attrs, postfix_attrs));
2543 ret.kind = ctsk_tagdef;
2544 ret.expr = NULL_TREE;
2545 ret.expr_const_operands = true;
2546 timevar_pop (TV_PARSE_ENUM);
2547 return ret;
2548 }
2549 else if (!ident)
2550 {
2551 c_parser_error (parser, "expected %<{%>");
2552 ret.spec = error_mark_node;
2553 ret.kind = ctsk_tagref;
2554 ret.expr = NULL_TREE;
2555 ret.expr_const_operands = true;
2556 return ret;
2557 }
2558 ret = parser_xref_tag (ident_loc, ENUMERAL_TYPE, ident);
2559 /* In ISO C, enumerated types can be referred to only if already
2560 defined. */
2561 if (pedantic && !COMPLETE_TYPE_P (ret.spec))
2562 {
2563 gcc_assert (ident);
2564 pedwarn (enum_loc, OPT_Wpedantic,
2565 "ISO C forbids forward references to %<enum%> types");
2566 }
2567 return ret;
2568 }
2569
2570 /* Parse a struct or union specifier (C90 6.5.2.1, C99 6.7.2.1).
2571
2572 struct-or-union-specifier:
2573 struct-or-union attributes[opt] identifier[opt]
2574 { struct-contents } attributes[opt]
2575 struct-or-union attributes[opt] identifier
2576
2577 struct-contents:
2578 struct-declaration-list
2579
2580 struct-declaration-list:
2581 struct-declaration ;
2582 struct-declaration-list struct-declaration ;
2583
2584 GNU extensions:
2585
2586 struct-contents:
2587 empty
2588 struct-declaration
2589 struct-declaration-list struct-declaration
2590
2591 struct-declaration-list:
2592 struct-declaration-list ;
2593 ;
2594
2595 (Note that in the syntax here, unlike that in ISO C, the semicolons
2596 are included here rather than in struct-declaration, in order to
2597 describe the syntax with extra semicolons and missing semicolon at
2598 end.)
2599
2600 Objective-C:
2601
2602 struct-declaration-list:
2603 @defs ( class-name )
2604
2605 (Note this does not include a trailing semicolon, but can be
2606 followed by further declarations, and gets a pedwarn-if-pedantic
2607 when followed by a semicolon.) */
2608
2609 static struct c_typespec
2610 c_parser_struct_or_union_specifier (c_parser *parser)
2611 {
2612 struct c_typespec ret;
2613 tree attrs;
2614 tree ident = NULL_TREE;
2615 location_t struct_loc;
2616 location_t ident_loc = UNKNOWN_LOCATION;
2617 enum tree_code code;
2618 switch (c_parser_peek_token (parser)->keyword)
2619 {
2620 case RID_STRUCT:
2621 code = RECORD_TYPE;
2622 break;
2623 case RID_UNION:
2624 code = UNION_TYPE;
2625 break;
2626 default:
2627 gcc_unreachable ();
2628 }
2629 struct_loc = c_parser_peek_token (parser)->location;
2630 c_parser_consume_token (parser);
2631 attrs = c_parser_attributes (parser);
2632
2633 /* Set the location in case we create a decl now. */
2634 c_parser_set_source_position_from_token (c_parser_peek_token (parser));
2635
2636 if (c_parser_next_token_is (parser, CPP_NAME))
2637 {
2638 ident = c_parser_peek_token (parser)->value;
2639 ident_loc = c_parser_peek_token (parser)->location;
2640 struct_loc = ident_loc;
2641 c_parser_consume_token (parser);
2642 }
2643 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
2644 {
2645 /* Parse a struct or union definition. Start the scope of the
2646 tag before parsing components. */
2647 struct c_struct_parse_info *struct_info;
2648 tree type = start_struct (struct_loc, code, ident, &struct_info);
2649 tree postfix_attrs;
2650 /* We chain the components in reverse order, then put them in
2651 forward order at the end. Each struct-declaration may
2652 declare multiple components (comma-separated), so we must use
2653 chainon to join them, although when parsing each
2654 struct-declaration we can use TREE_CHAIN directly.
2655
2656 The theory behind all this is that there will be more
2657 semicolon separated fields than comma separated fields, and
2658 so we'll be minimizing the number of node traversals required
2659 by chainon. */
2660 tree contents;
2661 timevar_push (TV_PARSE_STRUCT);
2662 contents = NULL_TREE;
2663 c_parser_consume_token (parser);
2664 /* Handle the Objective-C @defs construct,
2665 e.g. foo(sizeof(struct{ @defs(ClassName) }));. */
2666 if (c_parser_next_token_is_keyword (parser, RID_AT_DEFS))
2667 {
2668 tree name;
2669 gcc_assert (c_dialect_objc ());
2670 c_parser_consume_token (parser);
2671 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2672 goto end_at_defs;
2673 if (c_parser_next_token_is (parser, CPP_NAME)
2674 && c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME)
2675 {
2676 name = c_parser_peek_token (parser)->value;
2677 c_parser_consume_token (parser);
2678 }
2679 else
2680 {
2681 c_parser_error (parser, "expected class name");
2682 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
2683 goto end_at_defs;
2684 }
2685 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2686 "expected %<)%>");
2687 contents = nreverse (objc_get_class_ivars (name));
2688 }
2689 end_at_defs:
2690 /* Parse the struct-declarations and semicolons. Problems with
2691 semicolons are diagnosed here; empty structures are diagnosed
2692 elsewhere. */
2693 while (true)
2694 {
2695 tree decls;
2696 /* Parse any stray semicolon. */
2697 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
2698 {
2699 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
2700 "extra semicolon in struct or union specified");
2701 c_parser_consume_token (parser);
2702 continue;
2703 }
2704 /* Stop if at the end of the struct or union contents. */
2705 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2706 {
2707 c_parser_consume_token (parser);
2708 break;
2709 }
2710 /* Accept #pragmas at struct scope. */
2711 if (c_parser_next_token_is (parser, CPP_PRAGMA))
2712 {
2713 c_parser_pragma (parser, pragma_struct);
2714 continue;
2715 }
2716 /* Parse some comma-separated declarations, but not the
2717 trailing semicolon if any. */
2718 decls = c_parser_struct_declaration (parser);
2719 contents = chainon (decls, contents);
2720 /* If no semicolon follows, either we have a parse error or
2721 are at the end of the struct or union and should
2722 pedwarn. */
2723 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
2724 c_parser_consume_token (parser);
2725 else
2726 {
2727 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2728 pedwarn (c_parser_peek_token (parser)->location, 0,
2729 "no semicolon at end of struct or union");
2730 else if (parser->error
2731 || !c_parser_next_token_starts_declspecs (parser))
2732 {
2733 c_parser_error (parser, "expected %<;%>");
2734 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
2735 break;
2736 }
2737
2738 /* If we come here, we have already emitted an error
2739 for an expected `;', identifier or `(', and we also
2740 recovered already. Go on with the next field. */
2741 }
2742 }
2743 postfix_attrs = c_parser_attributes (parser);
2744 ret.spec = finish_struct (struct_loc, type, nreverse (contents),
2745 chainon (attrs, postfix_attrs), struct_info);
2746 ret.kind = ctsk_tagdef;
2747 ret.expr = NULL_TREE;
2748 ret.expr_const_operands = true;
2749 timevar_pop (TV_PARSE_STRUCT);
2750 return ret;
2751 }
2752 else if (!ident)
2753 {
2754 c_parser_error (parser, "expected %<{%>");
2755 ret.spec = error_mark_node;
2756 ret.kind = ctsk_tagref;
2757 ret.expr = NULL_TREE;
2758 ret.expr_const_operands = true;
2759 return ret;
2760 }
2761 ret = parser_xref_tag (ident_loc, code, ident);
2762 return ret;
2763 }
2764
2765 /* Parse a struct-declaration (C90 6.5.2.1, C99 6.7.2.1), *without*
2766 the trailing semicolon.
2767
2768 struct-declaration:
2769 specifier-qualifier-list struct-declarator-list
2770 static_assert-declaration-no-semi
2771
2772 specifier-qualifier-list:
2773 type-specifier specifier-qualifier-list[opt]
2774 type-qualifier specifier-qualifier-list[opt]
2775 attributes specifier-qualifier-list[opt]
2776
2777 struct-declarator-list:
2778 struct-declarator
2779 struct-declarator-list , attributes[opt] struct-declarator
2780
2781 struct-declarator:
2782 declarator attributes[opt]
2783 declarator[opt] : constant-expression attributes[opt]
2784
2785 GNU extensions:
2786
2787 struct-declaration:
2788 __extension__ struct-declaration
2789 specifier-qualifier-list
2790
2791 Unlike the ISO C syntax, semicolons are handled elsewhere. The use
2792 of attributes where shown is a GNU extension. In GNU C, we accept
2793 any expression without commas in the syntax (assignment
2794 expressions, not just conditional expressions); assignment
2795 expressions will be diagnosed as non-constant. */
2796
2797 static tree
2798 c_parser_struct_declaration (c_parser *parser)
2799 {
2800 struct c_declspecs *specs;
2801 tree prefix_attrs;
2802 tree all_prefix_attrs;
2803 tree decls;
2804 location_t decl_loc;
2805 if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
2806 {
2807 int ext;
2808 tree decl;
2809 ext = disable_extension_diagnostics ();
2810 c_parser_consume_token (parser);
2811 decl = c_parser_struct_declaration (parser);
2812 restore_extension_diagnostics (ext);
2813 return decl;
2814 }
2815 if (c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT))
2816 {
2817 c_parser_static_assert_declaration_no_semi (parser);
2818 return NULL_TREE;
2819 }
2820 specs = build_null_declspecs ();
2821 decl_loc = c_parser_peek_token (parser)->location;
2822 /* Strictly by the standard, we shouldn't allow _Alignas here,
2823 but it appears to have been intended to allow it there, so
2824 we're keeping it as it is until WG14 reaches a conclusion
2825 of N1731.
2826 <http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1731.pdf> */
2827 c_parser_declspecs (parser, specs, false, true, true,
2828 true, false, cla_nonabstract_decl);
2829 if (parser->error)
2830 return NULL_TREE;
2831 if (!specs->declspecs_seen_p)
2832 {
2833 c_parser_error (parser, "expected specifier-qualifier-list");
2834 return NULL_TREE;
2835 }
2836 finish_declspecs (specs);
2837 if (c_parser_next_token_is (parser, CPP_SEMICOLON)
2838 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2839 {
2840 tree ret;
2841 if (specs->typespec_kind == ctsk_none)
2842 {
2843 pedwarn (decl_loc, OPT_Wpedantic,
2844 "ISO C forbids member declarations with no members");
2845 shadow_tag_warned (specs, pedantic);
2846 ret = NULL_TREE;
2847 }
2848 else
2849 {
2850 /* Support for unnamed structs or unions as members of
2851 structs or unions (which is [a] useful and [b] supports
2852 MS P-SDK). */
2853 tree attrs = NULL;
2854
2855 ret = grokfield (c_parser_peek_token (parser)->location,
2856 build_id_declarator (NULL_TREE), specs,
2857 NULL_TREE, &attrs);
2858 if (ret)
2859 decl_attributes (&ret, attrs, 0);
2860 }
2861 return ret;
2862 }
2863
2864 /* Provide better error recovery. Note that a type name here is valid,
2865 and will be treated as a field name. */
2866 if (specs->typespec_kind == ctsk_tagdef
2867 && TREE_CODE (specs->type) != ENUMERAL_TYPE
2868 && c_parser_next_token_starts_declspecs (parser)
2869 && !c_parser_next_token_is (parser, CPP_NAME))
2870 {
2871 c_parser_error (parser, "expected %<;%>, identifier or %<(%>");
2872 parser->error = false;
2873 return NULL_TREE;
2874 }
2875
2876 pending_xref_error ();
2877 prefix_attrs = specs->attrs;
2878 all_prefix_attrs = prefix_attrs;
2879 specs->attrs = NULL_TREE;
2880 decls = NULL_TREE;
2881 while (true)
2882 {
2883 /* Declaring one or more declarators or un-named bit-fields. */
2884 struct c_declarator *declarator;
2885 bool dummy = false;
2886 if (c_parser_next_token_is (parser, CPP_COLON))
2887 declarator = build_id_declarator (NULL_TREE);
2888 else
2889 declarator = c_parser_declarator (parser,
2890 specs->typespec_kind != ctsk_none,
2891 C_DTR_NORMAL, &dummy);
2892 if (declarator == NULL)
2893 {
2894 c_parser_skip_to_end_of_block_or_statement (parser);
2895 break;
2896 }
2897 if (c_parser_next_token_is (parser, CPP_COLON)
2898 || c_parser_next_token_is (parser, CPP_COMMA)
2899 || c_parser_next_token_is (parser, CPP_SEMICOLON)
2900 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE)
2901 || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2902 {
2903 tree postfix_attrs = NULL_TREE;
2904 tree width = NULL_TREE;
2905 tree d;
2906 if (c_parser_next_token_is (parser, CPP_COLON))
2907 {
2908 c_parser_consume_token (parser);
2909 width = c_parser_expr_no_commas (parser, NULL).value;
2910 }
2911 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2912 postfix_attrs = c_parser_attributes (parser);
2913 d = grokfield (c_parser_peek_token (parser)->location,
2914 declarator, specs, width, &all_prefix_attrs);
2915 decl_attributes (&d, chainon (postfix_attrs,
2916 all_prefix_attrs), 0);
2917 DECL_CHAIN (d) = decls;
2918 decls = d;
2919 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2920 all_prefix_attrs = chainon (c_parser_attributes (parser),
2921 prefix_attrs);
2922 else
2923 all_prefix_attrs = prefix_attrs;
2924 if (c_parser_next_token_is (parser, CPP_COMMA))
2925 c_parser_consume_token (parser);
2926 else if (c_parser_next_token_is (parser, CPP_SEMICOLON)
2927 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2928 {
2929 /* Semicolon consumed in caller. */
2930 break;
2931 }
2932 else
2933 {
2934 c_parser_error (parser, "expected %<,%>, %<;%> or %<}%>");
2935 break;
2936 }
2937 }
2938 else
2939 {
2940 c_parser_error (parser,
2941 "expected %<:%>, %<,%>, %<;%>, %<}%> or "
2942 "%<__attribute__%>");
2943 break;
2944 }
2945 }
2946 return decls;
2947 }
2948
2949 /* Parse a typeof specifier (a GNU extension).
2950
2951 typeof-specifier:
2952 typeof ( expression )
2953 typeof ( type-name )
2954 */
2955
2956 static struct c_typespec
2957 c_parser_typeof_specifier (c_parser *parser)
2958 {
2959 struct c_typespec ret;
2960 ret.kind = ctsk_typeof;
2961 ret.spec = error_mark_node;
2962 ret.expr = NULL_TREE;
2963 ret.expr_const_operands = true;
2964 gcc_assert (c_parser_next_token_is_keyword (parser, RID_TYPEOF));
2965 c_parser_consume_token (parser);
2966 c_inhibit_evaluation_warnings++;
2967 in_typeof++;
2968 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2969 {
2970 c_inhibit_evaluation_warnings--;
2971 in_typeof--;
2972 return ret;
2973 }
2974 if (c_parser_next_tokens_start_typename (parser, cla_prefer_id))
2975 {
2976 struct c_type_name *type = c_parser_type_name (parser);
2977 c_inhibit_evaluation_warnings--;
2978 in_typeof--;
2979 if (type != NULL)
2980 {
2981 ret.spec = groktypename (type, &ret.expr, &ret.expr_const_operands);
2982 pop_maybe_used (variably_modified_type_p (ret.spec, NULL_TREE));
2983 }
2984 }
2985 else
2986 {
2987 bool was_vm;
2988 location_t here = c_parser_peek_token (parser)->location;
2989 struct c_expr expr = c_parser_expression (parser);
2990 c_inhibit_evaluation_warnings--;
2991 in_typeof--;
2992 if (TREE_CODE (expr.value) == COMPONENT_REF
2993 && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
2994 error_at (here, "%<typeof%> applied to a bit-field");
2995 mark_exp_read (expr.value);
2996 ret.spec = TREE_TYPE (expr.value);
2997 was_vm = variably_modified_type_p (ret.spec, NULL_TREE);
2998 /* This is returned with the type so that when the type is
2999 evaluated, this can be evaluated. */
3000 if (was_vm)
3001 ret.expr = c_fully_fold (expr.value, false, &ret.expr_const_operands);
3002 pop_maybe_used (was_vm);
3003 /* For use in macros such as those in <stdatomic.h>, remove
3004 _Atomic and const qualifiers from atomic types. (Possibly
3005 all qualifiers should be removed; const can be an issue for
3006 more macros using typeof than just the <stdatomic.h>
3007 ones.) */
3008 if (ret.spec != error_mark_node && TYPE_ATOMIC (ret.spec))
3009 ret.spec = c_build_qualified_type (ret.spec,
3010 (TYPE_QUALS (ret.spec)
3011 & ~(TYPE_QUAL_ATOMIC
3012 | TYPE_QUAL_CONST)));
3013 }
3014 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
3015 return ret;
3016 }
3017
3018 /* Parse an alignment-specifier.
3019
3020 C11 6.7.5:
3021
3022 alignment-specifier:
3023 _Alignas ( type-name )
3024 _Alignas ( constant-expression )
3025 */
3026
3027 static tree
3028 c_parser_alignas_specifier (c_parser * parser)
3029 {
3030 tree ret = error_mark_node;
3031 location_t loc = c_parser_peek_token (parser)->location;
3032 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ALIGNAS));
3033 c_parser_consume_token (parser);
3034 if (!flag_isoc11)
3035 {
3036 if (flag_isoc99)
3037 pedwarn (loc, OPT_Wpedantic,
3038 "ISO C99 does not support %<_Alignas%>");
3039 else
3040 pedwarn (loc, OPT_Wpedantic,
3041 "ISO C90 does not support %<_Alignas%>");
3042 }
3043 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3044 return ret;
3045 if (c_parser_next_tokens_start_typename (parser, cla_prefer_id))
3046 {
3047 struct c_type_name *type = c_parser_type_name (parser);
3048 if (type != NULL)
3049 ret = c_alignof (loc, groktypename (type, NULL, NULL));
3050 }
3051 else
3052 ret = c_parser_expr_no_commas (parser, NULL).value;
3053 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
3054 return ret;
3055 }
3056
3057 /* Parse a declarator, possibly an abstract declarator (C90 6.5.4,
3058 6.5.5, C99 6.7.5, 6.7.6). If TYPE_SEEN_P then a typedef name may
3059 be redeclared; otherwise it may not. KIND indicates which kind of
3060 declarator is wanted. Returns a valid declarator except in the
3061 case of a syntax error in which case NULL is returned. *SEEN_ID is
3062 set to true if an identifier being declared is seen; this is used
3063 to diagnose bad forms of abstract array declarators and to
3064 determine whether an identifier list is syntactically permitted.
3065
3066 declarator:
3067 pointer[opt] direct-declarator
3068
3069 direct-declarator:
3070 identifier
3071 ( attributes[opt] declarator )
3072 direct-declarator array-declarator
3073 direct-declarator ( parameter-type-list )
3074 direct-declarator ( identifier-list[opt] )
3075
3076 pointer:
3077 * type-qualifier-list[opt]
3078 * type-qualifier-list[opt] pointer
3079
3080 type-qualifier-list:
3081 type-qualifier
3082 attributes
3083 type-qualifier-list type-qualifier
3084 type-qualifier-list attributes
3085
3086 array-declarator:
3087 [ type-qualifier-list[opt] assignment-expression[opt] ]
3088 [ static type-qualifier-list[opt] assignment-expression ]
3089 [ type-qualifier-list static assignment-expression ]
3090 [ type-qualifier-list[opt] * ]
3091
3092 parameter-type-list:
3093 parameter-list
3094 parameter-list , ...
3095
3096 parameter-list:
3097 parameter-declaration
3098 parameter-list , parameter-declaration
3099
3100 parameter-declaration:
3101 declaration-specifiers declarator attributes[opt]
3102 declaration-specifiers abstract-declarator[opt] attributes[opt]
3103
3104 identifier-list:
3105 identifier
3106 identifier-list , identifier
3107
3108 abstract-declarator:
3109 pointer
3110 pointer[opt] direct-abstract-declarator
3111
3112 direct-abstract-declarator:
3113 ( attributes[opt] abstract-declarator )
3114 direct-abstract-declarator[opt] array-declarator
3115 direct-abstract-declarator[opt] ( parameter-type-list[opt] )
3116
3117 GNU extensions:
3118
3119 direct-declarator:
3120 direct-declarator ( parameter-forward-declarations
3121 parameter-type-list[opt] )
3122
3123 direct-abstract-declarator:
3124 direct-abstract-declarator[opt] ( parameter-forward-declarations
3125 parameter-type-list[opt] )
3126
3127 parameter-forward-declarations:
3128 parameter-list ;
3129 parameter-forward-declarations parameter-list ;
3130
3131 The uses of attributes shown above are GNU extensions.
3132
3133 Some forms of array declarator are not included in C99 in the
3134 syntax for abstract declarators; these are disallowed elsewhere.
3135 This may be a defect (DR#289).
3136
3137 This function also accepts an omitted abstract declarator as being
3138 an abstract declarator, although not part of the formal syntax. */
3139
3140 static struct c_declarator *
3141 c_parser_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
3142 bool *seen_id)
3143 {
3144 /* Parse any initial pointer part. */
3145 if (c_parser_next_token_is (parser, CPP_MULT))
3146 {
3147 struct c_declspecs *quals_attrs = build_null_declspecs ();
3148 struct c_declarator *inner;
3149 c_parser_consume_token (parser);
3150 c_parser_declspecs (parser, quals_attrs, false, false, true,
3151 false, false, cla_prefer_id);
3152 inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
3153 if (inner == NULL)
3154 return NULL;
3155 else
3156 return make_pointer_declarator (quals_attrs, inner);
3157 }
3158 /* Now we have a direct declarator, direct abstract declarator or
3159 nothing (which counts as a direct abstract declarator here). */
3160 return c_parser_direct_declarator (parser, type_seen_p, kind, seen_id);
3161 }
3162
3163 /* Parse a direct declarator or direct abstract declarator; arguments
3164 as c_parser_declarator. */
3165
3166 static struct c_declarator *
3167 c_parser_direct_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
3168 bool *seen_id)
3169 {
3170 /* The direct declarator must start with an identifier (possibly
3171 omitted) or a parenthesized declarator (possibly abstract). In
3172 an ordinary declarator, initial parentheses must start a
3173 parenthesized declarator. In an abstract declarator or parameter
3174 declarator, they could start a parenthesized declarator or a
3175 parameter list. To tell which, the open parenthesis and any
3176 following attributes must be read. If a declaration specifier
3177 follows, then it is a parameter list; if the specifier is a
3178 typedef name, there might be an ambiguity about redeclaring it,
3179 which is resolved in the direction of treating it as a typedef
3180 name. If a close parenthesis follows, it is also an empty
3181 parameter list, as the syntax does not permit empty abstract
3182 declarators. Otherwise, it is a parenthesized declarator (in
3183 which case the analysis may be repeated inside it, recursively).
3184
3185 ??? There is an ambiguity in a parameter declaration "int
3186 (__attribute__((foo)) x)", where x is not a typedef name: it
3187 could be an abstract declarator for a function, or declare x with
3188 parentheses. The proper resolution of this ambiguity needs
3189 documenting. At present we follow an accident of the old
3190 parser's implementation, whereby the first parameter must have
3191 some declaration specifiers other than just attributes. Thus as
3192 a parameter declaration it is treated as a parenthesized
3193 parameter named x, and as an abstract declarator it is
3194 rejected.
3195
3196 ??? Also following the old parser, attributes inside an empty
3197 parameter list are ignored, making it a list not yielding a
3198 prototype, rather than giving an error or making it have one
3199 parameter with implicit type int.
3200
3201 ??? Also following the old parser, typedef names may be
3202 redeclared in declarators, but not Objective-C class names. */
3203
3204 if (kind != C_DTR_ABSTRACT
3205 && c_parser_next_token_is (parser, CPP_NAME)
3206 && ((type_seen_p
3207 && (c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME
3208 || c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME))
3209 || c_parser_peek_token (parser)->id_kind == C_ID_ID))
3210 {
3211 struct c_declarator *inner
3212 = build_id_declarator (c_parser_peek_token (parser)->value);
3213 *seen_id = true;
3214 inner->id_loc = c_parser_peek_token (parser)->location;
3215 c_parser_consume_token (parser);
3216 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
3217 }
3218
3219 if (kind != C_DTR_NORMAL
3220 && c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
3221 {
3222 struct c_declarator *inner = build_id_declarator (NULL_TREE);
3223 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
3224 }
3225
3226 /* Either we are at the end of an abstract declarator, or we have
3227 parentheses. */
3228
3229 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
3230 {
3231 tree attrs;
3232 struct c_declarator *inner;
3233 c_parser_consume_token (parser);
3234 attrs = c_parser_attributes (parser);
3235 if (kind != C_DTR_NORMAL
3236 && (c_parser_next_token_starts_declspecs (parser)
3237 || c_parser_next_token_is (parser, CPP_CLOSE_PAREN)))
3238 {
3239 struct c_arg_info *args
3240 = c_parser_parms_declarator (parser, kind == C_DTR_NORMAL,
3241 attrs);
3242 if (args == NULL)
3243 return NULL;
3244 else
3245 {
3246 inner
3247 = build_function_declarator (args,
3248 build_id_declarator (NULL_TREE));
3249 return c_parser_direct_declarator_inner (parser, *seen_id,
3250 inner);
3251 }
3252 }
3253 /* A parenthesized declarator. */
3254 inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
3255 if (inner != NULL && attrs != NULL)
3256 inner = build_attrs_declarator (attrs, inner);
3257 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3258 {
3259 c_parser_consume_token (parser);
3260 if (inner == NULL)
3261 return NULL;
3262 else
3263 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
3264 }
3265 else
3266 {
3267 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3268 "expected %<)%>");
3269 return NULL;
3270 }
3271 }
3272 else
3273 {
3274 if (kind == C_DTR_NORMAL)
3275 {
3276 c_parser_error (parser, "expected identifier or %<(%>");
3277 return NULL;
3278 }
3279 else
3280 return build_id_declarator (NULL_TREE);
3281 }
3282 }
3283
3284 /* Parse part of a direct declarator or direct abstract declarator,
3285 given that some (in INNER) has already been parsed; ID_PRESENT is
3286 true if an identifier is present, false for an abstract
3287 declarator. */
3288
3289 static struct c_declarator *
3290 c_parser_direct_declarator_inner (c_parser *parser, bool id_present,
3291 struct c_declarator *inner)
3292 {
3293 /* Parse a sequence of array declarators and parameter lists. */
3294 if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
3295 {
3296 location_t brace_loc = c_parser_peek_token (parser)->location;
3297 struct c_declarator *declarator;
3298 struct c_declspecs *quals_attrs = build_null_declspecs ();
3299 bool static_seen;
3300 bool star_seen;
3301 struct c_expr dimen;
3302 dimen.value = NULL_TREE;
3303 dimen.original_code = ERROR_MARK;
3304 dimen.original_type = NULL_TREE;
3305 c_parser_consume_token (parser);
3306 c_parser_declspecs (parser, quals_attrs, false, false, true,
3307 false, false, cla_prefer_id);
3308 static_seen = c_parser_next_token_is_keyword (parser, RID_STATIC);
3309 if (static_seen)
3310 c_parser_consume_token (parser);
3311 if (static_seen && !quals_attrs->declspecs_seen_p)
3312 c_parser_declspecs (parser, quals_attrs, false, false, true,
3313 false, false, cla_prefer_id);
3314 if (!quals_attrs->declspecs_seen_p)
3315 quals_attrs = NULL;
3316 /* If "static" is present, there must be an array dimension.
3317 Otherwise, there may be a dimension, "*", or no
3318 dimension. */
3319 if (static_seen)
3320 {
3321 star_seen = false;
3322 dimen = c_parser_expr_no_commas (parser, NULL);
3323 }
3324 else
3325 {
3326 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3327 {
3328 dimen.value = NULL_TREE;
3329 star_seen = false;
3330 }
3331 else if (flag_enable_cilkplus
3332 && c_parser_next_token_is (parser, CPP_COLON))
3333 {
3334 dimen.value = error_mark_node;
3335 star_seen = false;
3336 error_at (c_parser_peek_token (parser)->location,
3337 "array notations cannot be used in declaration");
3338 c_parser_consume_token (parser);
3339 }
3340 else if (c_parser_next_token_is (parser, CPP_MULT))
3341 {
3342 if (c_parser_peek_2nd_token (parser)->type == CPP_CLOSE_SQUARE)
3343 {
3344 dimen.value = NULL_TREE;
3345 star_seen = true;
3346 c_parser_consume_token (parser);
3347 }
3348 else
3349 {
3350 star_seen = false;
3351 dimen = c_parser_expr_no_commas (parser, NULL);
3352 }
3353 }
3354 else
3355 {
3356 star_seen = false;
3357 dimen = c_parser_expr_no_commas (parser, NULL);
3358 }
3359 }
3360 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3361 c_parser_consume_token (parser);
3362 else if (flag_enable_cilkplus
3363 && c_parser_next_token_is (parser, CPP_COLON))
3364 {
3365 error_at (c_parser_peek_token (parser)->location,
3366 "array notations cannot be used in declaration");
3367 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
3368 return NULL;
3369 }
3370 else
3371 {
3372 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
3373 "expected %<]%>");
3374 return NULL;
3375 }
3376 if (dimen.value)
3377 dimen = convert_lvalue_to_rvalue (brace_loc, dimen, true, true);
3378 declarator = build_array_declarator (brace_loc, dimen.value, quals_attrs,
3379 static_seen, star_seen);
3380 if (declarator == NULL)
3381 return NULL;
3382 inner = set_array_declarator_inner (declarator, inner);
3383 return c_parser_direct_declarator_inner (parser, id_present, inner);
3384 }
3385 else if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
3386 {
3387 tree attrs;
3388 struct c_arg_info *args;
3389 c_parser_consume_token (parser);
3390 attrs = c_parser_attributes (parser);
3391 args = c_parser_parms_declarator (parser, id_present, attrs);
3392 if (args == NULL)
3393 return NULL;
3394 else
3395 {
3396 inner = build_function_declarator (args, inner);
3397 return c_parser_direct_declarator_inner (parser, id_present, inner);
3398 }
3399 }
3400 return inner;
3401 }
3402
3403 /* Parse a parameter list or identifier list, including the closing
3404 parenthesis but not the opening one. ATTRS are the attributes at
3405 the start of the list. ID_LIST_OK is true if an identifier list is
3406 acceptable; such a list must not have attributes at the start. */
3407
3408 static struct c_arg_info *
3409 c_parser_parms_declarator (c_parser *parser, bool id_list_ok, tree attrs)
3410 {
3411 push_scope ();
3412 declare_parm_level ();
3413 /* If the list starts with an identifier, it is an identifier list.
3414 Otherwise, it is either a prototype list or an empty list. */
3415 if (id_list_ok
3416 && !attrs
3417 && c_parser_next_token_is (parser, CPP_NAME)
3418 && c_parser_peek_token (parser)->id_kind == C_ID_ID
3419
3420 /* Look ahead to detect typos in type names. */
3421 && c_parser_peek_2nd_token (parser)->type != CPP_NAME
3422 && c_parser_peek_2nd_token (parser)->type != CPP_MULT
3423 && c_parser_peek_2nd_token (parser)->type != CPP_OPEN_PAREN
3424 && c_parser_peek_2nd_token (parser)->type != CPP_OPEN_SQUARE)
3425 {
3426 tree list = NULL_TREE, *nextp = &list;
3427 while (c_parser_next_token_is (parser, CPP_NAME)
3428 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
3429 {
3430 *nextp = build_tree_list (NULL_TREE,
3431 c_parser_peek_token (parser)->value);
3432 nextp = & TREE_CHAIN (*nextp);
3433 c_parser_consume_token (parser);
3434 if (c_parser_next_token_is_not (parser, CPP_COMMA))
3435 break;
3436 c_parser_consume_token (parser);
3437 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3438 {
3439 c_parser_error (parser, "expected identifier");
3440 break;
3441 }
3442 }
3443 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3444 {
3445 struct c_arg_info *ret = build_arg_info ();
3446 ret->types = list;
3447 c_parser_consume_token (parser);
3448 pop_scope ();
3449 return ret;
3450 }
3451 else
3452 {
3453 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3454 "expected %<)%>");
3455 pop_scope ();
3456 return NULL;
3457 }
3458 }
3459 else
3460 {
3461 struct c_arg_info *ret = c_parser_parms_list_declarator (parser, attrs,
3462 NULL);
3463 pop_scope ();
3464 return ret;
3465 }
3466 }
3467
3468 /* Parse a parameter list (possibly empty), including the closing
3469 parenthesis but not the opening one. ATTRS are the attributes at
3470 the start of the list. EXPR is NULL or an expression that needs to
3471 be evaluated for the side effects of array size expressions in the
3472 parameters. */
3473
3474 static struct c_arg_info *
3475 c_parser_parms_list_declarator (c_parser *parser, tree attrs, tree expr)
3476 {
3477 bool bad_parm = false;
3478
3479 /* ??? Following the old parser, forward parameter declarations may
3480 use abstract declarators, and if no real parameter declarations
3481 follow the forward declarations then this is not diagnosed. Also
3482 note as above that attributes are ignored as the only contents of
3483 the parentheses, or as the only contents after forward
3484 declarations. */
3485 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3486 {
3487 struct c_arg_info *ret = build_arg_info ();
3488 c_parser_consume_token (parser);
3489 return ret;
3490 }
3491 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3492 {
3493 struct c_arg_info *ret = build_arg_info ();
3494
3495 if (flag_allow_parameterless_variadic_functions)
3496 {
3497 /* F (...) is allowed. */
3498 ret->types = NULL_TREE;
3499 }
3500 else
3501 {
3502 /* Suppress -Wold-style-definition for this case. */
3503 ret->types = error_mark_node;
3504 error_at (c_parser_peek_token (parser)->location,
3505 "ISO C requires a named argument before %<...%>");
3506 }
3507 c_parser_consume_token (parser);
3508 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3509 {
3510 c_parser_consume_token (parser);
3511 return ret;
3512 }
3513 else
3514 {
3515 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3516 "expected %<)%>");
3517 return NULL;
3518 }
3519 }
3520 /* Nonempty list of parameters, either terminated with semicolon
3521 (forward declarations; recurse) or with close parenthesis (normal
3522 function) or with ", ... )" (variadic function). */
3523 while (true)
3524 {
3525 /* Parse a parameter. */
3526 struct c_parm *parm = c_parser_parameter_declaration (parser, attrs);
3527 attrs = NULL_TREE;
3528 if (parm == NULL)
3529 bad_parm = true;
3530 else
3531 push_parm_decl (parm, &expr);
3532 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
3533 {
3534 tree new_attrs;
3535 c_parser_consume_token (parser);
3536 mark_forward_parm_decls ();
3537 new_attrs = c_parser_attributes (parser);
3538 return c_parser_parms_list_declarator (parser, new_attrs, expr);
3539 }
3540 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3541 {
3542 c_parser_consume_token (parser);
3543 if (bad_parm)
3544 return NULL;
3545 else
3546 return get_parm_info (false, expr);
3547 }
3548 if (!c_parser_require (parser, CPP_COMMA,
3549 "expected %<;%>, %<,%> or %<)%>"))
3550 {
3551 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
3552 return NULL;
3553 }
3554 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3555 {
3556 c_parser_consume_token (parser);
3557 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3558 {
3559 c_parser_consume_token (parser);
3560 if (bad_parm)
3561 return NULL;
3562 else
3563 return get_parm_info (true, expr);
3564 }
3565 else
3566 {
3567 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3568 "expected %<)%>");
3569 return NULL;
3570 }
3571 }
3572 }
3573 }
3574
3575 /* Parse a parameter declaration. ATTRS are the attributes at the
3576 start of the declaration if it is the first parameter. */
3577
3578 static struct c_parm *
3579 c_parser_parameter_declaration (c_parser *parser, tree attrs)
3580 {
3581 struct c_declspecs *specs;
3582 struct c_declarator *declarator;
3583 tree prefix_attrs;
3584 tree postfix_attrs = NULL_TREE;
3585 bool dummy = false;
3586
3587 /* Accept #pragmas between parameter declarations. */
3588 while (c_parser_next_token_is (parser, CPP_PRAGMA))
3589 c_parser_pragma (parser, pragma_param);
3590
3591 if (!c_parser_next_token_starts_declspecs (parser))
3592 {
3593 c_token *token = c_parser_peek_token (parser);
3594 if (parser->error)
3595 return NULL;
3596 c_parser_set_source_position_from_token (token);
3597 if (c_parser_next_tokens_start_typename (parser, cla_prefer_type))
3598 {
3599 error ("unknown type name %qE", token->value);
3600 parser->error = true;
3601 }
3602 /* ??? In some Objective-C cases '...' isn't applicable so there
3603 should be a different message. */
3604 else
3605 c_parser_error (parser,
3606 "expected declaration specifiers or %<...%>");
3607 c_parser_skip_to_end_of_parameter (parser);
3608 return NULL;
3609 }
3610 specs = build_null_declspecs ();
3611 if (attrs)
3612 {
3613 declspecs_add_attrs (input_location, specs, attrs);
3614 attrs = NULL_TREE;
3615 }
3616 c_parser_declspecs (parser, specs, true, true, true, true, false,
3617 cla_nonabstract_decl);
3618 finish_declspecs (specs);
3619 pending_xref_error ();
3620 prefix_attrs = specs->attrs;
3621 specs->attrs = NULL_TREE;
3622 declarator = c_parser_declarator (parser,
3623 specs->typespec_kind != ctsk_none,
3624 C_DTR_PARM, &dummy);
3625 if (declarator == NULL)
3626 {
3627 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
3628 return NULL;
3629 }
3630 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
3631 postfix_attrs = c_parser_attributes (parser);
3632 return build_c_parm (specs, chainon (postfix_attrs, prefix_attrs),
3633 declarator);
3634 }
3635
3636 /* Parse a string literal in an asm expression. It should not be
3637 translated, and wide string literals are an error although
3638 permitted by the syntax. This is a GNU extension.
3639
3640 asm-string-literal:
3641 string-literal
3642
3643 ??? At present, following the old parser, the caller needs to have
3644 set lex_untranslated_string to 1. It would be better to follow the
3645 C++ parser rather than using this kludge. */
3646
3647 static tree
3648 c_parser_asm_string_literal (c_parser *parser)
3649 {
3650 tree str;
3651 int save_flag = warn_overlength_strings;
3652 warn_overlength_strings = 0;
3653 if (c_parser_next_token_is (parser, CPP_STRING))
3654 {
3655 str = c_parser_peek_token (parser)->value;
3656 c_parser_consume_token (parser);
3657 }
3658 else if (c_parser_next_token_is (parser, CPP_WSTRING))
3659 {
3660 error_at (c_parser_peek_token (parser)->location,
3661 "wide string literal in %<asm%>");
3662 str = build_string (1, "");
3663 c_parser_consume_token (parser);
3664 }
3665 else
3666 {
3667 c_parser_error (parser, "expected string literal");
3668 str = NULL_TREE;
3669 }
3670 warn_overlength_strings = save_flag;
3671 return str;
3672 }
3673
3674 /* Parse a simple asm expression. This is used in restricted
3675 contexts, where a full expression with inputs and outputs does not
3676 make sense. This is a GNU extension.
3677
3678 simple-asm-expr:
3679 asm ( asm-string-literal )
3680 */
3681
3682 static tree
3683 c_parser_simple_asm_expr (c_parser *parser)
3684 {
3685 tree str;
3686 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
3687 /* ??? Follow the C++ parser rather than using the
3688 lex_untranslated_string kludge. */
3689 parser->lex_untranslated_string = true;
3690 c_parser_consume_token (parser);
3691 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3692 {
3693 parser->lex_untranslated_string = false;
3694 return NULL_TREE;
3695 }
3696 str = c_parser_asm_string_literal (parser);
3697 parser->lex_untranslated_string = false;
3698 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
3699 {
3700 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
3701 return NULL_TREE;
3702 }
3703 return str;
3704 }
3705
3706 static tree
3707 c_parser_attribute_any_word (c_parser *parser)
3708 {
3709 tree attr_name = NULL_TREE;
3710
3711 if (c_parser_next_token_is (parser, CPP_KEYWORD))
3712 {
3713 /* ??? See comment above about what keywords are accepted here. */
3714 bool ok;
3715 switch (c_parser_peek_token (parser)->keyword)
3716 {
3717 case RID_STATIC:
3718 case RID_UNSIGNED:
3719 case RID_LONG:
3720 case RID_INT128:
3721 case RID_CONST:
3722 case RID_EXTERN:
3723 case RID_REGISTER:
3724 case RID_TYPEDEF:
3725 case RID_SHORT:
3726 case RID_INLINE:
3727 case RID_NORETURN:
3728 case RID_VOLATILE:
3729 case RID_SIGNED:
3730 case RID_AUTO:
3731 case RID_RESTRICT:
3732 case RID_COMPLEX:
3733 case RID_THREAD:
3734 case RID_INT:
3735 case RID_CHAR:
3736 case RID_FLOAT:
3737 case RID_DOUBLE:
3738 case RID_VOID:
3739 case RID_DFLOAT32:
3740 case RID_DFLOAT64:
3741 case RID_DFLOAT128:
3742 case RID_BOOL:
3743 case RID_FRACT:
3744 case RID_ACCUM:
3745 case RID_SAT:
3746 case RID_TRANSACTION_ATOMIC:
3747 case RID_TRANSACTION_CANCEL:
3748 case RID_ATOMIC:
3749 case RID_AUTO_TYPE:
3750 ok = true;
3751 break;
3752 default:
3753 ok = false;
3754 break;
3755 }
3756 if (!ok)
3757 return NULL_TREE;
3758
3759 /* Accept __attribute__((__const)) as __attribute__((const)) etc. */
3760 attr_name = ridpointers[(int) c_parser_peek_token (parser)->keyword];
3761 }
3762 else if (c_parser_next_token_is (parser, CPP_NAME))
3763 attr_name = c_parser_peek_token (parser)->value;
3764
3765 return attr_name;
3766 }
3767
3768 /* Parse (possibly empty) attributes. This is a GNU extension.
3769
3770 attributes:
3771 empty
3772 attributes attribute
3773
3774 attribute:
3775 __attribute__ ( ( attribute-list ) )
3776
3777 attribute-list:
3778 attrib
3779 attribute_list , attrib
3780
3781 attrib:
3782 empty
3783 any-word
3784 any-word ( identifier )
3785 any-word ( identifier , nonempty-expr-list )
3786 any-word ( expr-list )
3787
3788 where the "identifier" must not be declared as a type, and
3789 "any-word" may be any identifier (including one declared as a
3790 type), a reserved word storage class specifier, type specifier or
3791 type qualifier. ??? This still leaves out most reserved keywords
3792 (following the old parser), shouldn't we include them, and why not
3793 allow identifiers declared as types to start the arguments? */
3794
3795 static tree
3796 c_parser_attributes (c_parser *parser)
3797 {
3798 tree attrs = NULL_TREE;
3799 while (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
3800 {
3801 /* ??? Follow the C++ parser rather than using the
3802 lex_untranslated_string kludge. */
3803 parser->lex_untranslated_string = true;
3804 c_parser_consume_token (parser);
3805 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3806 {
3807 parser->lex_untranslated_string = false;
3808 return attrs;
3809 }
3810 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3811 {
3812 parser->lex_untranslated_string = false;
3813 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
3814 return attrs;
3815 }
3816 /* Parse the attribute list. */
3817 while (c_parser_next_token_is (parser, CPP_COMMA)
3818 || c_parser_next_token_is (parser, CPP_NAME)
3819 || c_parser_next_token_is (parser, CPP_KEYWORD))
3820 {
3821 tree attr, attr_name, attr_args;
3822 vec<tree, va_gc> *expr_list;
3823 if (c_parser_next_token_is (parser, CPP_COMMA))
3824 {
3825 c_parser_consume_token (parser);
3826 continue;
3827 }
3828
3829 attr_name = c_parser_attribute_any_word (parser);
3830 if (attr_name == NULL)
3831 break;
3832 c_parser_consume_token (parser);
3833 if (c_parser_next_token_is_not (parser, CPP_OPEN_PAREN))
3834 {
3835 attr = build_tree_list (attr_name, NULL_TREE);
3836 attrs = chainon (attrs, attr);
3837 continue;
3838 }
3839 c_parser_consume_token (parser);
3840 /* Parse the attribute contents. If they start with an
3841 identifier which is followed by a comma or close
3842 parenthesis, then the arguments start with that
3843 identifier; otherwise they are an expression list.
3844 In objective-c the identifier may be a classname. */
3845 if (c_parser_next_token_is (parser, CPP_NAME)
3846 && (c_parser_peek_token (parser)->id_kind == C_ID_ID
3847 || (c_dialect_objc ()
3848 && c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME))
3849 && ((c_parser_peek_2nd_token (parser)->type == CPP_COMMA)
3850 || (c_parser_peek_2nd_token (parser)->type
3851 == CPP_CLOSE_PAREN)))
3852 {
3853 tree arg1 = c_parser_peek_token (parser)->value;
3854 c_parser_consume_token (parser);
3855 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3856 attr_args = build_tree_list (NULL_TREE, arg1);
3857 else
3858 {
3859 tree tree_list;
3860 c_parser_consume_token (parser);
3861 expr_list = c_parser_expr_list (parser, false, true,
3862 NULL, NULL, NULL);
3863 tree_list = build_tree_list_vec (expr_list);
3864 attr_args = tree_cons (NULL_TREE, arg1, tree_list);
3865 release_tree_vector (expr_list);
3866 }
3867 }
3868 else
3869 {
3870 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3871 attr_args = NULL_TREE;
3872 else
3873 {
3874 expr_list = c_parser_expr_list (parser, false, true,
3875 NULL, NULL, NULL);
3876 attr_args = build_tree_list_vec (expr_list);
3877 release_tree_vector (expr_list);
3878 }
3879 }
3880 attr = build_tree_list (attr_name, attr_args);
3881 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3882 c_parser_consume_token (parser);
3883 else
3884 {
3885 parser->lex_untranslated_string = false;
3886 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3887 "expected %<)%>");
3888 return attrs;
3889 }
3890 attrs = chainon (attrs, attr);
3891 }
3892 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3893 c_parser_consume_token (parser);
3894 else
3895 {
3896 parser->lex_untranslated_string = false;
3897 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3898 "expected %<)%>");
3899 return attrs;
3900 }
3901 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3902 c_parser_consume_token (parser);
3903 else
3904 {
3905 parser->lex_untranslated_string = false;
3906 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3907 "expected %<)%>");
3908 return attrs;
3909 }
3910 parser->lex_untranslated_string = false;
3911 }
3912 return attrs;
3913 }
3914
3915 /* Parse a type name (C90 6.5.5, C99 6.7.6).
3916
3917 type-name:
3918 specifier-qualifier-list abstract-declarator[opt]
3919 */
3920
3921 static struct c_type_name *
3922 c_parser_type_name (c_parser *parser)
3923 {
3924 struct c_declspecs *specs = build_null_declspecs ();
3925 struct c_declarator *declarator;
3926 struct c_type_name *ret;
3927 bool dummy = false;
3928 c_parser_declspecs (parser, specs, false, true, true, false, false,
3929 cla_prefer_type);
3930 if (!specs->declspecs_seen_p)
3931 {
3932 c_parser_error (parser, "expected specifier-qualifier-list");
3933 return NULL;
3934 }
3935 if (specs->type != error_mark_node)
3936 {
3937 pending_xref_error ();
3938 finish_declspecs (specs);
3939 }
3940 declarator = c_parser_declarator (parser,
3941 specs->typespec_kind != ctsk_none,
3942 C_DTR_ABSTRACT, &dummy);
3943 if (declarator == NULL)
3944 return NULL;
3945 ret = XOBNEW (&parser_obstack, struct c_type_name);
3946 ret->specs = specs;
3947 ret->declarator = declarator;
3948 return ret;
3949 }
3950
3951 /* Parse an initializer (C90 6.5.7, C99 6.7.8).
3952
3953 initializer:
3954 assignment-expression
3955 { initializer-list }
3956 { initializer-list , }
3957
3958 initializer-list:
3959 designation[opt] initializer
3960 initializer-list , designation[opt] initializer
3961
3962 designation:
3963 designator-list =
3964
3965 designator-list:
3966 designator
3967 designator-list designator
3968
3969 designator:
3970 array-designator
3971 . identifier
3972
3973 array-designator:
3974 [ constant-expression ]
3975
3976 GNU extensions:
3977
3978 initializer:
3979 { }
3980
3981 designation:
3982 array-designator
3983 identifier :
3984
3985 array-designator:
3986 [ constant-expression ... constant-expression ]
3987
3988 Any expression without commas is accepted in the syntax for the
3989 constant-expressions, with non-constant expressions rejected later.
3990
3991 This function is only used for top-level initializers; for nested
3992 ones, see c_parser_initval. */
3993
3994 static struct c_expr
3995 c_parser_initializer (c_parser *parser)
3996 {
3997 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
3998 return c_parser_braced_init (parser, NULL_TREE, false);
3999 else
4000 {
4001 struct c_expr ret;
4002 location_t loc = c_parser_peek_token (parser)->location;
4003 ret = c_parser_expr_no_commas (parser, NULL);
4004 if (TREE_CODE (ret.value) != STRING_CST
4005 && TREE_CODE (ret.value) != COMPOUND_LITERAL_EXPR)
4006 ret = convert_lvalue_to_rvalue (loc, ret, true, true);
4007 return ret;
4008 }
4009 }
4010
4011 /* Parse a braced initializer list. TYPE is the type specified for a
4012 compound literal, and NULL_TREE for other initializers and for
4013 nested braced lists. NESTED_P is true for nested braced lists,
4014 false for the list of a compound literal or the list that is the
4015 top-level initializer in a declaration. */
4016
4017 static struct c_expr
4018 c_parser_braced_init (c_parser *parser, tree type, bool nested_p)
4019 {
4020 struct c_expr ret;
4021 struct obstack braced_init_obstack;
4022 location_t brace_loc = c_parser_peek_token (parser)->location;
4023 gcc_obstack_init (&braced_init_obstack);
4024 gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
4025 c_parser_consume_token (parser);
4026 if (nested_p)
4027 push_init_level (0, &braced_init_obstack);
4028 else
4029 really_start_incremental_init (type);
4030 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
4031 {
4032 pedwarn (brace_loc, OPT_Wpedantic, "ISO C forbids empty initializer braces");
4033 }
4034 else
4035 {
4036 /* Parse a non-empty initializer list, possibly with a trailing
4037 comma. */
4038 while (true)
4039 {
4040 c_parser_initelt (parser, &braced_init_obstack);
4041 if (parser->error)
4042 break;
4043 if (c_parser_next_token_is (parser, CPP_COMMA))
4044 c_parser_consume_token (parser);
4045 else
4046 break;
4047 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
4048 break;
4049 }
4050 }
4051 if (c_parser_next_token_is_not (parser, CPP_CLOSE_BRACE))
4052 {
4053 ret.value = error_mark_node;
4054 ret.original_code = ERROR_MARK;
4055 ret.original_type = NULL;
4056 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, "expected %<}%>");
4057 pop_init_level (0, &braced_init_obstack);
4058 obstack_free (&braced_init_obstack, NULL);
4059 return ret;
4060 }
4061 c_parser_consume_token (parser);
4062 ret = pop_init_level (0, &braced_init_obstack);
4063 obstack_free (&braced_init_obstack, NULL);
4064 return ret;
4065 }
4066
4067 /* Parse a nested initializer, including designators. */
4068
4069 static void
4070 c_parser_initelt (c_parser *parser, struct obstack * braced_init_obstack)
4071 {
4072 /* Parse any designator or designator list. A single array
4073 designator may have the subsequent "=" omitted in GNU C, but a
4074 longer list or a structure member designator may not. */
4075 if (c_parser_next_token_is (parser, CPP_NAME)
4076 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
4077 {
4078 /* Old-style structure member designator. */
4079 set_init_label (c_parser_peek_token (parser)->value,
4080 braced_init_obstack);
4081 /* Use the colon as the error location. */
4082 pedwarn (c_parser_peek_2nd_token (parser)->location, OPT_Wpedantic,
4083 "obsolete use of designated initializer with %<:%>");
4084 c_parser_consume_token (parser);
4085 c_parser_consume_token (parser);
4086 }
4087 else
4088 {
4089 /* des_seen is 0 if there have been no designators, 1 if there
4090 has been a single array designator and 2 otherwise. */
4091 int des_seen = 0;
4092 /* Location of a designator. */
4093 location_t des_loc = UNKNOWN_LOCATION; /* Quiet warning. */
4094 while (c_parser_next_token_is (parser, CPP_OPEN_SQUARE)
4095 || c_parser_next_token_is (parser, CPP_DOT))
4096 {
4097 int des_prev = des_seen;
4098 if (!des_seen)
4099 des_loc = c_parser_peek_token (parser)->location;
4100 if (des_seen < 2)
4101 des_seen++;
4102 if (c_parser_next_token_is (parser, CPP_DOT))
4103 {
4104 des_seen = 2;
4105 c_parser_consume_token (parser);
4106 if (c_parser_next_token_is (parser, CPP_NAME))
4107 {
4108 set_init_label (c_parser_peek_token (parser)->value,
4109 braced_init_obstack);
4110 c_parser_consume_token (parser);
4111 }
4112 else
4113 {
4114 struct c_expr init;
4115 init.value = error_mark_node;
4116 init.original_code = ERROR_MARK;
4117 init.original_type = NULL;
4118 c_parser_error (parser, "expected identifier");
4119 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
4120 process_init_element (init, false, braced_init_obstack);
4121 return;
4122 }
4123 }
4124 else
4125 {
4126 tree first, second;
4127 location_t ellipsis_loc = UNKNOWN_LOCATION; /* Quiet warning. */
4128 /* ??? Following the old parser, [ objc-receiver
4129 objc-message-args ] is accepted as an initializer,
4130 being distinguished from a designator by what follows
4131 the first assignment expression inside the square
4132 brackets, but after a first array designator a
4133 subsequent square bracket is for Objective-C taken to
4134 start an expression, using the obsolete form of
4135 designated initializer without '=', rather than
4136 possibly being a second level of designation: in LALR
4137 terms, the '[' is shifted rather than reducing
4138 designator to designator-list. */
4139 if (des_prev == 1 && c_dialect_objc ())
4140 {
4141 des_seen = des_prev;
4142 break;
4143 }
4144 if (des_prev == 0 && c_dialect_objc ())
4145 {
4146 /* This might be an array designator or an
4147 Objective-C message expression. If the former,
4148 continue parsing here; if the latter, parse the
4149 remainder of the initializer given the starting
4150 primary-expression. ??? It might make sense to
4151 distinguish when des_prev == 1 as well; see
4152 previous comment. */
4153 tree rec, args;
4154 struct c_expr mexpr;
4155 c_parser_consume_token (parser);
4156 if (c_parser_peek_token (parser)->type == CPP_NAME
4157 && ((c_parser_peek_token (parser)->id_kind
4158 == C_ID_TYPENAME)
4159 || (c_parser_peek_token (parser)->id_kind
4160 == C_ID_CLASSNAME)))
4161 {
4162 /* Type name receiver. */
4163 tree id = c_parser_peek_token (parser)->value;
4164 c_parser_consume_token (parser);
4165 rec = objc_get_class_reference (id);
4166 goto parse_message_args;
4167 }
4168 first = c_parser_expr_no_commas (parser, NULL).value;
4169 mark_exp_read (first);
4170 if (c_parser_next_token_is (parser, CPP_ELLIPSIS)
4171 || c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
4172 goto array_desig_after_first;
4173 /* Expression receiver. So far only one part
4174 without commas has been parsed; there might be
4175 more of the expression. */
4176 rec = first;
4177 while (c_parser_next_token_is (parser, CPP_COMMA))
4178 {
4179 struct c_expr next;
4180 location_t comma_loc, exp_loc;
4181 comma_loc = c_parser_peek_token (parser)->location;
4182 c_parser_consume_token (parser);
4183 exp_loc = c_parser_peek_token (parser)->location;
4184 next = c_parser_expr_no_commas (parser, NULL);
4185 next = convert_lvalue_to_rvalue (exp_loc, next,
4186 true, true);
4187 rec = build_compound_expr (comma_loc, rec, next.value);
4188 }
4189 parse_message_args:
4190 /* Now parse the objc-message-args. */
4191 args = c_parser_objc_message_args (parser);
4192 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
4193 "expected %<]%>");
4194 mexpr.value
4195 = objc_build_message_expr (rec, args);
4196 mexpr.original_code = ERROR_MARK;
4197 mexpr.original_type = NULL;
4198 /* Now parse and process the remainder of the
4199 initializer, starting with this message
4200 expression as a primary-expression. */
4201 c_parser_initval (parser, &mexpr, braced_init_obstack);
4202 return;
4203 }
4204 c_parser_consume_token (parser);
4205 first = c_parser_expr_no_commas (parser, NULL).value;
4206 mark_exp_read (first);
4207 array_desig_after_first:
4208 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
4209 {
4210 ellipsis_loc = c_parser_peek_token (parser)->location;
4211 c_parser_consume_token (parser);
4212 second = c_parser_expr_no_commas (parser, NULL).value;
4213 mark_exp_read (second);
4214 }
4215 else
4216 second = NULL_TREE;
4217 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
4218 {
4219 c_parser_consume_token (parser);
4220 set_init_index (first, second, braced_init_obstack);
4221 if (second)
4222 pedwarn (ellipsis_loc, OPT_Wpedantic,
4223 "ISO C forbids specifying range of elements to initialize");
4224 }
4225 else
4226 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
4227 "expected %<]%>");
4228 }
4229 }
4230 if (des_seen >= 1)
4231 {
4232 if (c_parser_next_token_is (parser, CPP_EQ))
4233 {
4234 if (!flag_isoc99)
4235 pedwarn (des_loc, OPT_Wpedantic,
4236 "ISO C90 forbids specifying subobject to initialize");
4237 c_parser_consume_token (parser);
4238 }
4239 else
4240 {
4241 if (des_seen == 1)
4242 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
4243 "obsolete use of designated initializer without %<=%>");
4244 else
4245 {
4246 struct c_expr init;
4247 init.value = error_mark_node;
4248 init.original_code = ERROR_MARK;
4249 init.original_type = NULL;
4250 c_parser_error (parser, "expected %<=%>");
4251 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
4252 process_init_element (init, false, braced_init_obstack);
4253 return;
4254 }
4255 }
4256 }
4257 }
4258 c_parser_initval (parser, NULL, braced_init_obstack);
4259 }
4260
4261 /* Parse a nested initializer; as c_parser_initializer but parses
4262 initializers within braced lists, after any designators have been
4263 applied. If AFTER is not NULL then it is an Objective-C message
4264 expression which is the primary-expression starting the
4265 initializer. */
4266
4267 static void
4268 c_parser_initval (c_parser *parser, struct c_expr *after,
4269 struct obstack * braced_init_obstack)
4270 {
4271 struct c_expr init;
4272 gcc_assert (!after || c_dialect_objc ());
4273 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE) && !after)
4274 init = c_parser_braced_init (parser, NULL_TREE, true);
4275 else
4276 {
4277 location_t loc = c_parser_peek_token (parser)->location;
4278 init = c_parser_expr_no_commas (parser, after);
4279 if (init.value != NULL_TREE
4280 && TREE_CODE (init.value) != STRING_CST
4281 && TREE_CODE (init.value) != COMPOUND_LITERAL_EXPR)
4282 init = convert_lvalue_to_rvalue (loc, init, true, true);
4283 }
4284 process_init_element (init, false, braced_init_obstack);
4285 }
4286
4287 /* Parse a compound statement (possibly a function body) (C90 6.6.2,
4288 C99 6.8.2).
4289
4290 compound-statement:
4291 { block-item-list[opt] }
4292 { label-declarations block-item-list }
4293
4294 block-item-list:
4295 block-item
4296 block-item-list block-item
4297
4298 block-item:
4299 nested-declaration
4300 statement
4301
4302 nested-declaration:
4303 declaration
4304
4305 GNU extensions:
4306
4307 compound-statement:
4308 { label-declarations block-item-list }
4309
4310 nested-declaration:
4311 __extension__ nested-declaration
4312 nested-function-definition
4313
4314 label-declarations:
4315 label-declaration
4316 label-declarations label-declaration
4317
4318 label-declaration:
4319 __label__ identifier-list ;
4320
4321 Allowing the mixing of declarations and code is new in C99. The
4322 GNU syntax also permits (not shown above) labels at the end of
4323 compound statements, which yield an error. We don't allow labels
4324 on declarations; this might seem like a natural extension, but
4325 there would be a conflict between attributes on the label and
4326 prefix attributes on the declaration. ??? The syntax follows the
4327 old parser in requiring something after label declarations.
4328 Although they are erroneous if the labels declared aren't defined,
4329 is it useful for the syntax to be this way?
4330
4331 OpenMP:
4332
4333 block-item:
4334 openmp-directive
4335
4336 openmp-directive:
4337 barrier-directive
4338 flush-directive
4339 taskwait-directive
4340 taskyield-directive
4341 cancel-directive
4342 cancellation-point-directive */
4343
4344 static tree
4345 c_parser_compound_statement (c_parser *parser)
4346 {
4347 tree stmt;
4348 location_t brace_loc;
4349 brace_loc = c_parser_peek_token (parser)->location;
4350 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
4351 {
4352 /* Ensure a scope is entered and left anyway to avoid confusion
4353 if we have just prepared to enter a function body. */
4354 stmt = c_begin_compound_stmt (true);
4355 c_end_compound_stmt (brace_loc, stmt, true);
4356 return error_mark_node;
4357 }
4358 stmt = c_begin_compound_stmt (true);
4359 c_parser_compound_statement_nostart (parser);
4360
4361 /* If the compound stmt contains array notations, then we expand them. */
4362 if (flag_enable_cilkplus && contains_array_notation_expr (stmt))
4363 stmt = expand_array_notation_exprs (stmt);
4364 return c_end_compound_stmt (brace_loc, stmt, true);
4365 }
4366
4367 /* Parse a compound statement except for the opening brace. This is
4368 used for parsing both compound statements and statement expressions
4369 (which follow different paths to handling the opening). */
4370
4371 static void
4372 c_parser_compound_statement_nostart (c_parser *parser)
4373 {
4374 bool last_stmt = false;
4375 bool last_label = false;
4376 bool save_valid_for_pragma = valid_location_for_stdc_pragma_p ();
4377 location_t label_loc = UNKNOWN_LOCATION; /* Quiet warning. */
4378 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
4379 {
4380 c_parser_consume_token (parser);
4381 return;
4382 }
4383 mark_valid_location_for_stdc_pragma (true);
4384 if (c_parser_next_token_is_keyword (parser, RID_LABEL))
4385 {
4386 /* Read zero or more forward-declarations for labels that nested
4387 functions can jump to. */
4388 mark_valid_location_for_stdc_pragma (false);
4389 while (c_parser_next_token_is_keyword (parser, RID_LABEL))
4390 {
4391 label_loc = c_parser_peek_token (parser)->location;
4392 c_parser_consume_token (parser);
4393 /* Any identifiers, including those declared as type names,
4394 are OK here. */
4395 while (true)
4396 {
4397 tree label;
4398 if (c_parser_next_token_is_not (parser, CPP_NAME))
4399 {
4400 c_parser_error (parser, "expected identifier");
4401 break;
4402 }
4403 label
4404 = declare_label (c_parser_peek_token (parser)->value);
4405 C_DECLARED_LABEL_FLAG (label) = 1;
4406 add_stmt (build_stmt (label_loc, DECL_EXPR, label));
4407 c_parser_consume_token (parser);
4408 if (c_parser_next_token_is (parser, CPP_COMMA))
4409 c_parser_consume_token (parser);
4410 else
4411 break;
4412 }
4413 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
4414 }
4415 pedwarn (label_loc, OPT_Wpedantic, "ISO C forbids label declarations");
4416 }
4417 /* We must now have at least one statement, label or declaration. */
4418 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
4419 {
4420 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
4421 c_parser_error (parser, "expected declaration or statement");
4422 c_parser_consume_token (parser);
4423 return;
4424 }
4425 while (c_parser_next_token_is_not (parser, CPP_CLOSE_BRACE))
4426 {
4427 location_t loc = c_parser_peek_token (parser)->location;
4428 if (c_parser_next_token_is_keyword (parser, RID_CASE)
4429 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
4430 || (c_parser_next_token_is (parser, CPP_NAME)
4431 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
4432 {
4433 if (c_parser_next_token_is_keyword (parser, RID_CASE))
4434 label_loc = c_parser_peek_2nd_token (parser)->location;
4435 else
4436 label_loc = c_parser_peek_token (parser)->location;
4437 last_label = true;
4438 last_stmt = false;
4439 mark_valid_location_for_stdc_pragma (false);
4440 c_parser_label (parser);
4441 }
4442 else if (!last_label
4443 && c_parser_next_tokens_start_declaration (parser))
4444 {
4445 last_label = false;
4446 mark_valid_location_for_stdc_pragma (false);
4447 c_parser_declaration_or_fndef (parser, true, true, true, true,
4448 true, NULL, vNULL);
4449 if (last_stmt)
4450 pedwarn_c90 (loc,
4451 (pedantic && !flag_isoc99)
4452 ? OPT_Wpedantic
4453 : OPT_Wdeclaration_after_statement,
4454 "ISO C90 forbids mixed declarations and code");
4455 last_stmt = false;
4456 }
4457 else if (!last_label
4458 && c_parser_next_token_is_keyword (parser, RID_EXTENSION))
4459 {
4460 /* __extension__ can start a declaration, but is also an
4461 unary operator that can start an expression. Consume all
4462 but the last of a possible series of __extension__ to
4463 determine which. */
4464 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
4465 && (c_parser_peek_2nd_token (parser)->keyword
4466 == RID_EXTENSION))
4467 c_parser_consume_token (parser);
4468 if (c_token_starts_declaration (c_parser_peek_2nd_token (parser)))
4469 {
4470 int ext;
4471 ext = disable_extension_diagnostics ();
4472 c_parser_consume_token (parser);
4473 last_label = false;
4474 mark_valid_location_for_stdc_pragma (false);
4475 c_parser_declaration_or_fndef (parser, true, true, true, true,
4476 true, NULL, vNULL);
4477 /* Following the old parser, __extension__ does not
4478 disable this diagnostic. */
4479 restore_extension_diagnostics (ext);
4480 if (last_stmt)
4481 pedwarn_c90 (loc, (pedantic && !flag_isoc99)
4482 ? OPT_Wpedantic
4483 : OPT_Wdeclaration_after_statement,
4484 "ISO C90 forbids mixed declarations and code");
4485 last_stmt = false;
4486 }
4487 else
4488 goto statement;
4489 }
4490 else if (c_parser_next_token_is (parser, CPP_PRAGMA))
4491 {
4492 /* External pragmas, and some omp pragmas, are not associated
4493 with regular c code, and so are not to be considered statements
4494 syntactically. This ensures that the user doesn't put them
4495 places that would turn into syntax errors if the directive
4496 were ignored. */
4497 if (c_parser_pragma (parser, pragma_compound))
4498 last_label = false, last_stmt = true;
4499 }
4500 else if (c_parser_next_token_is (parser, CPP_EOF))
4501 {
4502 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
4503 c_parser_error (parser, "expected declaration or statement");
4504 return;
4505 }
4506 else if (c_parser_next_token_is_keyword (parser, RID_ELSE))
4507 {
4508 if (parser->in_if_block)
4509 {
4510 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
4511 error_at (loc, """expected %<}%> before %<else%>");
4512 return;
4513 }
4514 else
4515 {
4516 error_at (loc, "%<else%> without a previous %<if%>");
4517 c_parser_consume_token (parser);
4518 continue;
4519 }
4520 }
4521 else
4522 {
4523 statement:
4524 last_label = false;
4525 last_stmt = true;
4526 mark_valid_location_for_stdc_pragma (false);
4527 c_parser_statement_after_labels (parser);
4528 }
4529
4530 parser->error = false;
4531 }
4532 if (last_label)
4533 error_at (label_loc, "label at end of compound statement");
4534 c_parser_consume_token (parser);
4535 /* Restore the value we started with. */
4536 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
4537 }
4538
4539 /* Parse a label (C90 6.6.1, C99 6.8.1).
4540
4541 label:
4542 identifier : attributes[opt]
4543 case constant-expression :
4544 default :
4545
4546 GNU extensions:
4547
4548 label:
4549 case constant-expression ... constant-expression :
4550
4551 The use of attributes on labels is a GNU extension. The syntax in
4552 GNU C accepts any expressions without commas, non-constant
4553 expressions being rejected later. */
4554
4555 static void
4556 c_parser_label (c_parser *parser)
4557 {
4558 location_t loc1 = c_parser_peek_token (parser)->location;
4559 tree label = NULL_TREE;
4560 if (c_parser_next_token_is_keyword (parser, RID_CASE))
4561 {
4562 tree exp1, exp2;
4563 c_parser_consume_token (parser);
4564 exp1 = c_parser_expr_no_commas (parser, NULL).value;
4565 if (c_parser_next_token_is (parser, CPP_COLON))
4566 {
4567 c_parser_consume_token (parser);
4568 label = do_case (loc1, exp1, NULL_TREE);
4569 }
4570 else if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
4571 {
4572 c_parser_consume_token (parser);
4573 exp2 = c_parser_expr_no_commas (parser, NULL).value;
4574 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
4575 label = do_case (loc1, exp1, exp2);
4576 }
4577 else
4578 c_parser_error (parser, "expected %<:%> or %<...%>");
4579 }
4580 else if (c_parser_next_token_is_keyword (parser, RID_DEFAULT))
4581 {
4582 c_parser_consume_token (parser);
4583 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
4584 label = do_case (loc1, NULL_TREE, NULL_TREE);
4585 }
4586 else
4587 {
4588 tree name = c_parser_peek_token (parser)->value;
4589 tree tlab;
4590 tree attrs;
4591 location_t loc2 = c_parser_peek_token (parser)->location;
4592 gcc_assert (c_parser_next_token_is (parser, CPP_NAME));
4593 c_parser_consume_token (parser);
4594 gcc_assert (c_parser_next_token_is (parser, CPP_COLON));
4595 c_parser_consume_token (parser);
4596 attrs = c_parser_attributes (parser);
4597 tlab = define_label (loc2, name);
4598 if (tlab)
4599 {
4600 decl_attributes (&tlab, attrs, 0);
4601 label = add_stmt (build_stmt (loc1, LABEL_EXPR, tlab));
4602 }
4603 }
4604 if (label)
4605 {
4606 if (c_parser_next_tokens_start_declaration (parser))
4607 {
4608 error_at (c_parser_peek_token (parser)->location,
4609 "a label can only be part of a statement and "
4610 "a declaration is not a statement");
4611 c_parser_declaration_or_fndef (parser, /*fndef_ok*/ false,
4612 /*static_assert_ok*/ true,
4613 /*empty_ok*/ true, /*nested*/ true,
4614 /*start_attr_ok*/ true, NULL,
4615 vNULL);
4616 }
4617 }
4618 }
4619
4620 /* Parse a statement (C90 6.6, C99 6.8).
4621
4622 statement:
4623 labeled-statement
4624 compound-statement
4625 expression-statement
4626 selection-statement
4627 iteration-statement
4628 jump-statement
4629
4630 labeled-statement:
4631 label statement
4632
4633 expression-statement:
4634 expression[opt] ;
4635
4636 selection-statement:
4637 if-statement
4638 switch-statement
4639
4640 iteration-statement:
4641 while-statement
4642 do-statement
4643 for-statement
4644
4645 jump-statement:
4646 goto identifier ;
4647 continue ;
4648 break ;
4649 return expression[opt] ;
4650
4651 GNU extensions:
4652
4653 statement:
4654 asm-statement
4655
4656 jump-statement:
4657 goto * expression ;
4658
4659 Objective-C:
4660
4661 statement:
4662 objc-throw-statement
4663 objc-try-catch-statement
4664 objc-synchronized-statement
4665
4666 objc-throw-statement:
4667 @throw expression ;
4668 @throw ;
4669
4670 OpenMP:
4671
4672 statement:
4673 openmp-construct
4674
4675 openmp-construct:
4676 parallel-construct
4677 for-construct
4678 simd-construct
4679 for-simd-construct
4680 sections-construct
4681 single-construct
4682 parallel-for-construct
4683 parallel-for-simd-construct
4684 parallel-sections-construct
4685 master-construct
4686 critical-construct
4687 atomic-construct
4688 ordered-construct
4689
4690 parallel-construct:
4691 parallel-directive structured-block
4692
4693 for-construct:
4694 for-directive iteration-statement
4695
4696 simd-construct:
4697 simd-directive iteration-statements
4698
4699 for-simd-construct:
4700 for-simd-directive iteration-statements
4701
4702 sections-construct:
4703 sections-directive section-scope
4704
4705 single-construct:
4706 single-directive structured-block
4707
4708 parallel-for-construct:
4709 parallel-for-directive iteration-statement
4710
4711 parallel-for-simd-construct:
4712 parallel-for-simd-directive iteration-statement
4713
4714 parallel-sections-construct:
4715 parallel-sections-directive section-scope
4716
4717 master-construct:
4718 master-directive structured-block
4719
4720 critical-construct:
4721 critical-directive structured-block
4722
4723 atomic-construct:
4724 atomic-directive expression-statement
4725
4726 ordered-construct:
4727 ordered-directive structured-block
4728
4729 Transactional Memory:
4730
4731 statement:
4732 transaction-statement
4733 transaction-cancel-statement
4734 */
4735
4736 static void
4737 c_parser_statement (c_parser *parser)
4738 {
4739 while (c_parser_next_token_is_keyword (parser, RID_CASE)
4740 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
4741 || (c_parser_next_token_is (parser, CPP_NAME)
4742 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
4743 c_parser_label (parser);
4744 c_parser_statement_after_labels (parser);
4745 }
4746
4747 /* Parse a statement, other than a labeled statement. */
4748
4749 static void
4750 c_parser_statement_after_labels (c_parser *parser)
4751 {
4752 location_t loc = c_parser_peek_token (parser)->location;
4753 tree stmt = NULL_TREE;
4754 bool in_if_block = parser->in_if_block;
4755 parser->in_if_block = false;
4756 switch (c_parser_peek_token (parser)->type)
4757 {
4758 case CPP_OPEN_BRACE:
4759 add_stmt (c_parser_compound_statement (parser));
4760 break;
4761 case CPP_KEYWORD:
4762 switch (c_parser_peek_token (parser)->keyword)
4763 {
4764 case RID_IF:
4765 c_parser_if_statement (parser);
4766 break;
4767 case RID_SWITCH:
4768 c_parser_switch_statement (parser);
4769 break;
4770 case RID_WHILE:
4771 c_parser_while_statement (parser, false);
4772 break;
4773 case RID_DO:
4774 c_parser_do_statement (parser, false);
4775 break;
4776 case RID_FOR:
4777 c_parser_for_statement (parser, false);
4778 break;
4779 case RID_CILK_SYNC:
4780 c_parser_consume_token (parser);
4781 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
4782 if (!flag_enable_cilkplus)
4783 error_at (loc, "-fcilkplus must be enabled to use %<_Cilk_sync%>");
4784 else
4785 add_stmt (build_cilk_sync ());
4786 break;
4787 case RID_GOTO:
4788 c_parser_consume_token (parser);
4789 if (c_parser_next_token_is (parser, CPP_NAME))
4790 {
4791 stmt = c_finish_goto_label (loc,
4792 c_parser_peek_token (parser)->value);
4793 c_parser_consume_token (parser);
4794 }
4795 else if (c_parser_next_token_is (parser, CPP_MULT))
4796 {
4797 struct c_expr val;
4798
4799 c_parser_consume_token (parser);
4800 val = c_parser_expression (parser);
4801 val = convert_lvalue_to_rvalue (loc, val, false, true);
4802 stmt = c_finish_goto_ptr (loc, val.value);
4803 }
4804 else
4805 c_parser_error (parser, "expected identifier or %<*%>");
4806 goto expect_semicolon;
4807 case RID_CONTINUE:
4808 c_parser_consume_token (parser);
4809 stmt = c_finish_bc_stmt (loc, &c_cont_label, false);
4810 goto expect_semicolon;
4811 case RID_BREAK:
4812 c_parser_consume_token (parser);
4813 stmt = c_finish_bc_stmt (loc, &c_break_label, true);
4814 goto expect_semicolon;
4815 case RID_RETURN:
4816 c_parser_consume_token (parser);
4817 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4818 {
4819 stmt = c_finish_return (loc, NULL_TREE, NULL_TREE);
4820 c_parser_consume_token (parser);
4821 }
4822 else
4823 {
4824 struct c_expr expr = c_parser_expression_conv (parser);
4825 mark_exp_read (expr.value);
4826 stmt = c_finish_return (loc, expr.value, expr.original_type);
4827 goto expect_semicolon;
4828 }
4829 break;
4830 case RID_ASM:
4831 stmt = c_parser_asm_statement (parser);
4832 break;
4833 case RID_TRANSACTION_ATOMIC:
4834 case RID_TRANSACTION_RELAXED:
4835 stmt = c_parser_transaction (parser,
4836 c_parser_peek_token (parser)->keyword);
4837 break;
4838 case RID_TRANSACTION_CANCEL:
4839 stmt = c_parser_transaction_cancel (parser);
4840 goto expect_semicolon;
4841 case RID_AT_THROW:
4842 gcc_assert (c_dialect_objc ());
4843 c_parser_consume_token (parser);
4844 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4845 {
4846 stmt = objc_build_throw_stmt (loc, NULL_TREE);
4847 c_parser_consume_token (parser);
4848 }
4849 else
4850 {
4851 struct c_expr expr = c_parser_expression (parser);
4852 expr = convert_lvalue_to_rvalue (loc, expr, false, false);
4853 expr.value = c_fully_fold (expr.value, false, NULL);
4854 stmt = objc_build_throw_stmt (loc, expr.value);
4855 goto expect_semicolon;
4856 }
4857 break;
4858 case RID_AT_TRY:
4859 gcc_assert (c_dialect_objc ());
4860 c_parser_objc_try_catch_finally_statement (parser);
4861 break;
4862 case RID_AT_SYNCHRONIZED:
4863 gcc_assert (c_dialect_objc ());
4864 c_parser_objc_synchronized_statement (parser);
4865 break;
4866 default:
4867 goto expr_stmt;
4868 }
4869 break;
4870 case CPP_SEMICOLON:
4871 c_parser_consume_token (parser);
4872 break;
4873 case CPP_CLOSE_PAREN:
4874 case CPP_CLOSE_SQUARE:
4875 /* Avoid infinite loop in error recovery:
4876 c_parser_skip_until_found stops at a closing nesting
4877 delimiter without consuming it, but here we need to consume
4878 it to proceed further. */
4879 c_parser_error (parser, "expected statement");
4880 c_parser_consume_token (parser);
4881 break;
4882 case CPP_PRAGMA:
4883 c_parser_pragma (parser, pragma_stmt);
4884 break;
4885 default:
4886 expr_stmt:
4887 stmt = c_finish_expr_stmt (loc, c_parser_expression_conv (parser).value);
4888 expect_semicolon:
4889 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
4890 break;
4891 }
4892 /* Two cases cannot and do not have line numbers associated: If stmt
4893 is degenerate, such as "2;", then stmt is an INTEGER_CST, which
4894 cannot hold line numbers. But that's OK because the statement
4895 will either be changed to a MODIFY_EXPR during gimplification of
4896 the statement expr, or discarded. If stmt was compound, but
4897 without new variables, we will have skipped the creation of a
4898 BIND and will have a bare STATEMENT_LIST. But that's OK because
4899 (recursively) all of the component statements should already have
4900 line numbers assigned. ??? Can we discard no-op statements
4901 earlier? */
4902 if (CAN_HAVE_LOCATION_P (stmt)
4903 && EXPR_LOCATION (stmt) == UNKNOWN_LOCATION)
4904 SET_EXPR_LOCATION (stmt, loc);
4905
4906 parser->in_if_block = in_if_block;
4907 }
4908
4909 /* Parse the condition from an if, do, while or for statements. */
4910
4911 static tree
4912 c_parser_condition (c_parser *parser)
4913 {
4914 location_t loc = c_parser_peek_token (parser)->location;
4915 tree cond;
4916 cond = c_parser_expression_conv (parser).value;
4917 cond = c_objc_common_truthvalue_conversion (loc, cond);
4918 cond = c_fully_fold (cond, false, NULL);
4919 if (warn_sequence_point)
4920 verify_sequence_points (cond);
4921 return cond;
4922 }
4923
4924 /* Parse a parenthesized condition from an if, do or while statement.
4925
4926 condition:
4927 ( expression )
4928 */
4929 static tree
4930 c_parser_paren_condition (c_parser *parser)
4931 {
4932 tree cond;
4933 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4934 return error_mark_node;
4935 cond = c_parser_condition (parser);
4936 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
4937 return cond;
4938 }
4939
4940 /* Parse a statement which is a block in C99. */
4941
4942 static tree
4943 c_parser_c99_block_statement (c_parser *parser)
4944 {
4945 tree block = c_begin_compound_stmt (flag_isoc99);
4946 location_t loc = c_parser_peek_token (parser)->location;
4947 c_parser_statement (parser);
4948 return c_end_compound_stmt (loc, block, flag_isoc99);
4949 }
4950
4951 /* Parse the body of an if statement. This is just parsing a
4952 statement but (a) it is a block in C99, (b) we track whether the
4953 body is an if statement for the sake of -Wparentheses warnings, (c)
4954 we handle an empty body specially for the sake of -Wempty-body
4955 warnings, and (d) we call parser_compound_statement directly
4956 because c_parser_statement_after_labels resets
4957 parser->in_if_block. */
4958
4959 static tree
4960 c_parser_if_body (c_parser *parser, bool *if_p)
4961 {
4962 tree block = c_begin_compound_stmt (flag_isoc99);
4963 location_t body_loc = c_parser_peek_token (parser)->location;
4964 while (c_parser_next_token_is_keyword (parser, RID_CASE)
4965 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
4966 || (c_parser_next_token_is (parser, CPP_NAME)
4967 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
4968 c_parser_label (parser);
4969 *if_p = c_parser_next_token_is_keyword (parser, RID_IF);
4970 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4971 {
4972 location_t loc = c_parser_peek_token (parser)->location;
4973 add_stmt (build_empty_stmt (loc));
4974 c_parser_consume_token (parser);
4975 if (!c_parser_next_token_is_keyword (parser, RID_ELSE))
4976 warning_at (loc, OPT_Wempty_body,
4977 "suggest braces around empty body in an %<if%> statement");
4978 }
4979 else if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
4980 add_stmt (c_parser_compound_statement (parser));
4981 else
4982 c_parser_statement_after_labels (parser);
4983 return c_end_compound_stmt (body_loc, block, flag_isoc99);
4984 }
4985
4986 /* Parse the else body of an if statement. This is just parsing a
4987 statement but (a) it is a block in C99, (b) we handle an empty body
4988 specially for the sake of -Wempty-body warnings. */
4989
4990 static tree
4991 c_parser_else_body (c_parser *parser)
4992 {
4993 location_t else_loc = c_parser_peek_token (parser)->location;
4994 tree block = c_begin_compound_stmt (flag_isoc99);
4995 while (c_parser_next_token_is_keyword (parser, RID_CASE)
4996 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
4997 || (c_parser_next_token_is (parser, CPP_NAME)
4998 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
4999 c_parser_label (parser);
5000 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5001 {
5002 location_t loc = c_parser_peek_token (parser)->location;
5003 warning_at (loc,
5004 OPT_Wempty_body,
5005 "suggest braces around empty body in an %<else%> statement");
5006 add_stmt (build_empty_stmt (loc));
5007 c_parser_consume_token (parser);
5008 }
5009 else
5010 c_parser_statement_after_labels (parser);
5011 return c_end_compound_stmt (else_loc, block, flag_isoc99);
5012 }
5013
5014 /* Parse an if statement (C90 6.6.4, C99 6.8.4).
5015
5016 if-statement:
5017 if ( expression ) statement
5018 if ( expression ) statement else statement
5019 */
5020
5021 static void
5022 c_parser_if_statement (c_parser *parser)
5023 {
5024 tree block;
5025 location_t loc;
5026 tree cond;
5027 bool first_if = false;
5028 tree first_body, second_body;
5029 bool in_if_block;
5030 tree if_stmt;
5031
5032 gcc_assert (c_parser_next_token_is_keyword (parser, RID_IF));
5033 c_parser_consume_token (parser);
5034 block = c_begin_compound_stmt (flag_isoc99);
5035 loc = c_parser_peek_token (parser)->location;
5036 cond = c_parser_paren_condition (parser);
5037 in_if_block = parser->in_if_block;
5038 parser->in_if_block = true;
5039 first_body = c_parser_if_body (parser, &first_if);
5040 parser->in_if_block = in_if_block;
5041 if (c_parser_next_token_is_keyword (parser, RID_ELSE))
5042 {
5043 c_parser_consume_token (parser);
5044 second_body = c_parser_else_body (parser);
5045 }
5046 else
5047 second_body = NULL_TREE;
5048 c_finish_if_stmt (loc, cond, first_body, second_body, first_if);
5049 if_stmt = c_end_compound_stmt (loc, block, flag_isoc99);
5050
5051 /* If the if statement contains array notations, then we expand them. */
5052 if (flag_enable_cilkplus && contains_array_notation_expr (if_stmt))
5053 if_stmt = fix_conditional_array_notations (if_stmt);
5054 add_stmt (if_stmt);
5055 }
5056
5057 /* Parse a switch statement (C90 6.6.4, C99 6.8.4).
5058
5059 switch-statement:
5060 switch (expression) statement
5061 */
5062
5063 static void
5064 c_parser_switch_statement (c_parser *parser)
5065 {
5066 struct c_expr ce;
5067 tree block, expr, body, save_break;
5068 location_t switch_loc = c_parser_peek_token (parser)->location;
5069 location_t switch_cond_loc;
5070 gcc_assert (c_parser_next_token_is_keyword (parser, RID_SWITCH));
5071 c_parser_consume_token (parser);
5072 block = c_begin_compound_stmt (flag_isoc99);
5073 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5074 {
5075 switch_cond_loc = c_parser_peek_token (parser)->location;
5076 ce = c_parser_expression (parser);
5077 ce = convert_lvalue_to_rvalue (switch_cond_loc, ce, true, false);
5078 expr = ce.value;
5079 if (flag_enable_cilkplus && contains_array_notation_expr (expr))
5080 {
5081 error_at (switch_cond_loc,
5082 "array notations cannot be used as a condition for switch "
5083 "statement");
5084 expr = error_mark_node;
5085 }
5086 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5087 }
5088 else
5089 {
5090 switch_cond_loc = UNKNOWN_LOCATION;
5091 expr = error_mark_node;
5092 }
5093 c_start_case (switch_loc, switch_cond_loc, expr);
5094 save_break = c_break_label;
5095 c_break_label = NULL_TREE;
5096 body = c_parser_c99_block_statement (parser);
5097 c_finish_case (body);
5098 if (c_break_label)
5099 {
5100 location_t here = c_parser_peek_token (parser)->location;
5101 tree t = build1 (LABEL_EXPR, void_type_node, c_break_label);
5102 SET_EXPR_LOCATION (t, here);
5103 add_stmt (t);
5104 }
5105 c_break_label = save_break;
5106 add_stmt (c_end_compound_stmt (switch_loc, block, flag_isoc99));
5107 }
5108
5109 /* Parse a while statement (C90 6.6.5, C99 6.8.5).
5110
5111 while-statement:
5112 while (expression) statement
5113 */
5114
5115 static void
5116 c_parser_while_statement (c_parser *parser, bool ivdep)
5117 {
5118 tree block, cond, body, save_break, save_cont;
5119 location_t loc;
5120 gcc_assert (c_parser_next_token_is_keyword (parser, RID_WHILE));
5121 c_parser_consume_token (parser);
5122 block = c_begin_compound_stmt (flag_isoc99);
5123 loc = c_parser_peek_token (parser)->location;
5124 cond = c_parser_paren_condition (parser);
5125 if (flag_enable_cilkplus && contains_array_notation_expr (cond))
5126 {
5127 error_at (loc, "array notations cannot be used as a condition for while "
5128 "statement");
5129 cond = error_mark_node;
5130 }
5131
5132 if (ivdep && cond != error_mark_node)
5133 cond = build2 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
5134 build_int_cst (integer_type_node,
5135 annot_expr_ivdep_kind));
5136 save_break = c_break_label;
5137 c_break_label = NULL_TREE;
5138 save_cont = c_cont_label;
5139 c_cont_label = NULL_TREE;
5140 body = c_parser_c99_block_statement (parser);
5141 c_finish_loop (loc, cond, NULL, body, c_break_label, c_cont_label, true);
5142 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
5143 c_break_label = save_break;
5144 c_cont_label = save_cont;
5145 }
5146
5147 /* Parse a do statement (C90 6.6.5, C99 6.8.5).
5148
5149 do-statement:
5150 do statement while ( expression ) ;
5151 */
5152
5153 static void
5154 c_parser_do_statement (c_parser *parser, bool ivdep)
5155 {
5156 tree block, cond, body, save_break, save_cont, new_break, new_cont;
5157 location_t loc;
5158 gcc_assert (c_parser_next_token_is_keyword (parser, RID_DO));
5159 c_parser_consume_token (parser);
5160 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5161 warning_at (c_parser_peek_token (parser)->location,
5162 OPT_Wempty_body,
5163 "suggest braces around empty body in %<do%> statement");
5164 block = c_begin_compound_stmt (flag_isoc99);
5165 loc = c_parser_peek_token (parser)->location;
5166 save_break = c_break_label;
5167 c_break_label = NULL_TREE;
5168 save_cont = c_cont_label;
5169 c_cont_label = NULL_TREE;
5170 body = c_parser_c99_block_statement (parser);
5171 c_parser_require_keyword (parser, RID_WHILE, "expected %<while%>");
5172 new_break = c_break_label;
5173 c_break_label = save_break;
5174 new_cont = c_cont_label;
5175 c_cont_label = save_cont;
5176 cond = c_parser_paren_condition (parser);
5177 if (flag_enable_cilkplus && contains_array_notation_expr (cond))
5178 {
5179 error_at (loc, "array notations cannot be used as a condition for a "
5180 "do-while statement");
5181 cond = error_mark_node;
5182 }
5183 if (ivdep && cond != error_mark_node)
5184 cond = build2 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
5185 build_int_cst (integer_type_node,
5186 annot_expr_ivdep_kind));
5187 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
5188 c_parser_skip_to_end_of_block_or_statement (parser);
5189 c_finish_loop (loc, cond, NULL, body, new_break, new_cont, false);
5190 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
5191 }
5192
5193 /* Parse a for statement (C90 6.6.5, C99 6.8.5).
5194
5195 for-statement:
5196 for ( expression[opt] ; expression[opt] ; expression[opt] ) statement
5197 for ( nested-declaration expression[opt] ; expression[opt] ) statement
5198
5199 The form with a declaration is new in C99.
5200
5201 ??? In accordance with the old parser, the declaration may be a
5202 nested function, which is then rejected in check_for_loop_decls,
5203 but does it make any sense for this to be included in the grammar?
5204 Note in particular that the nested function does not include a
5205 trailing ';', whereas the "declaration" production includes one.
5206 Also, can we reject bad declarations earlier and cheaper than
5207 check_for_loop_decls?
5208
5209 In Objective-C, there are two additional variants:
5210
5211 foreach-statement:
5212 for ( expression in expresssion ) statement
5213 for ( declaration in expression ) statement
5214
5215 This is inconsistent with C, because the second variant is allowed
5216 even if c99 is not enabled.
5217
5218 The rest of the comment documents these Objective-C foreach-statement.
5219
5220 Here is the canonical example of the first variant:
5221 for (object in array) { do something with object }
5222 we call the first expression ("object") the "object_expression" and
5223 the second expression ("array") the "collection_expression".
5224 object_expression must be an lvalue of type "id" (a generic Objective-C
5225 object) because the loop works by assigning to object_expression the
5226 various objects from the collection_expression. collection_expression
5227 must evaluate to something of type "id" which responds to the method
5228 countByEnumeratingWithState:objects:count:.
5229
5230 The canonical example of the second variant is:
5231 for (id object in array) { do something with object }
5232 which is completely equivalent to
5233 {
5234 id object;
5235 for (object in array) { do something with object }
5236 }
5237 Note that initizializing 'object' in some way (eg, "for ((object =
5238 xxx) in array) { do something with object }") is possibly
5239 technically valid, but completely pointless as 'object' will be
5240 assigned to something else as soon as the loop starts. We should
5241 most likely reject it (TODO).
5242
5243 The beginning of the Objective-C foreach-statement looks exactly
5244 like the beginning of the for-statement, and we can tell it is a
5245 foreach-statement only because the initial declaration or
5246 expression is terminated by 'in' instead of ';'.
5247 */
5248
5249 static void
5250 c_parser_for_statement (c_parser *parser, bool ivdep)
5251 {
5252 tree block, cond, incr, save_break, save_cont, body;
5253 /* The following are only used when parsing an ObjC foreach statement. */
5254 tree object_expression;
5255 /* Silence the bogus uninitialized warning. */
5256 tree collection_expression = NULL;
5257 location_t loc = c_parser_peek_token (parser)->location;
5258 location_t for_loc = c_parser_peek_token (parser)->location;
5259 bool is_foreach_statement = false;
5260 gcc_assert (c_parser_next_token_is_keyword (parser, RID_FOR));
5261 c_parser_consume_token (parser);
5262 /* Open a compound statement in Objective-C as well, just in case this is
5263 as foreach expression. */
5264 block = c_begin_compound_stmt (flag_isoc99 || c_dialect_objc ());
5265 cond = error_mark_node;
5266 incr = error_mark_node;
5267 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5268 {
5269 /* Parse the initialization declaration or expression. */
5270 object_expression = error_mark_node;
5271 parser->objc_could_be_foreach_context = c_dialect_objc ();
5272 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5273 {
5274 parser->objc_could_be_foreach_context = false;
5275 c_parser_consume_token (parser);
5276 c_finish_expr_stmt (loc, NULL_TREE);
5277 }
5278 else if (c_parser_next_tokens_start_declaration (parser))
5279 {
5280 c_parser_declaration_or_fndef (parser, true, true, true, true, true,
5281 &object_expression, vNULL);
5282 parser->objc_could_be_foreach_context = false;
5283
5284 if (c_parser_next_token_is_keyword (parser, RID_IN))
5285 {
5286 c_parser_consume_token (parser);
5287 is_foreach_statement = true;
5288 if (check_for_loop_decls (for_loc, true) == NULL_TREE)
5289 c_parser_error (parser, "multiple iterating variables in fast enumeration");
5290 }
5291 else
5292 check_for_loop_decls (for_loc, flag_isoc99);
5293 }
5294 else if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
5295 {
5296 /* __extension__ can start a declaration, but is also an
5297 unary operator that can start an expression. Consume all
5298 but the last of a possible series of __extension__ to
5299 determine which. */
5300 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
5301 && (c_parser_peek_2nd_token (parser)->keyword
5302 == RID_EXTENSION))
5303 c_parser_consume_token (parser);
5304 if (c_token_starts_declaration (c_parser_peek_2nd_token (parser)))
5305 {
5306 int ext;
5307 ext = disable_extension_diagnostics ();
5308 c_parser_consume_token (parser);
5309 c_parser_declaration_or_fndef (parser, true, true, true, true,
5310 true, &object_expression, vNULL);
5311 parser->objc_could_be_foreach_context = false;
5312
5313 restore_extension_diagnostics (ext);
5314 if (c_parser_next_token_is_keyword (parser, RID_IN))
5315 {
5316 c_parser_consume_token (parser);
5317 is_foreach_statement = true;
5318 if (check_for_loop_decls (for_loc, true) == NULL_TREE)
5319 c_parser_error (parser, "multiple iterating variables in fast enumeration");
5320 }
5321 else
5322 check_for_loop_decls (for_loc, flag_isoc99);
5323 }
5324 else
5325 goto init_expr;
5326 }
5327 else
5328 {
5329 init_expr:
5330 {
5331 struct c_expr ce;
5332 tree init_expression;
5333 ce = c_parser_expression (parser);
5334 init_expression = ce.value;
5335 parser->objc_could_be_foreach_context = false;
5336 if (c_parser_next_token_is_keyword (parser, RID_IN))
5337 {
5338 c_parser_consume_token (parser);
5339 is_foreach_statement = true;
5340 if (! lvalue_p (init_expression))
5341 c_parser_error (parser, "invalid iterating variable in fast enumeration");
5342 object_expression = c_fully_fold (init_expression, false, NULL);
5343 }
5344 else
5345 {
5346 ce = convert_lvalue_to_rvalue (loc, ce, true, false);
5347 init_expression = ce.value;
5348 c_finish_expr_stmt (loc, init_expression);
5349 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
5350 }
5351 }
5352 }
5353 /* Parse the loop condition. In the case of a foreach
5354 statement, there is no loop condition. */
5355 gcc_assert (!parser->objc_could_be_foreach_context);
5356 if (!is_foreach_statement)
5357 {
5358 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5359 {
5360 if (ivdep)
5361 {
5362 c_parser_error (parser, "missing loop condition in loop with "
5363 "%<GCC ivdep%> pragma");
5364 cond = error_mark_node;
5365 }
5366 else
5367 {
5368 c_parser_consume_token (parser);
5369 cond = NULL_TREE;
5370 }
5371 }
5372 else
5373 {
5374 cond = c_parser_condition (parser);
5375 if (flag_enable_cilkplus && contains_array_notation_expr (cond))
5376 {
5377 error_at (loc, "array notations cannot be used in a "
5378 "condition for a for-loop");
5379 cond = error_mark_node;
5380 }
5381 c_parser_skip_until_found (parser, CPP_SEMICOLON,
5382 "expected %<;%>");
5383 }
5384 if (ivdep && cond != error_mark_node)
5385 cond = build2 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
5386 build_int_cst (integer_type_node,
5387 annot_expr_ivdep_kind));
5388 }
5389 /* Parse the increment expression (the third expression in a
5390 for-statement). In the case of a foreach-statement, this is
5391 the expression that follows the 'in'. */
5392 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
5393 {
5394 if (is_foreach_statement)
5395 {
5396 c_parser_error (parser, "missing collection in fast enumeration");
5397 collection_expression = error_mark_node;
5398 }
5399 else
5400 incr = c_process_expr_stmt (loc, NULL_TREE);
5401 }
5402 else
5403 {
5404 if (is_foreach_statement)
5405 collection_expression = c_fully_fold (c_parser_expression (parser).value,
5406 false, NULL);
5407 else
5408 {
5409 struct c_expr ce = c_parser_expression (parser);
5410 ce = convert_lvalue_to_rvalue (loc, ce, true, false);
5411 incr = c_process_expr_stmt (loc, ce.value);
5412 }
5413 }
5414 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5415 }
5416 save_break = c_break_label;
5417 c_break_label = NULL_TREE;
5418 save_cont = c_cont_label;
5419 c_cont_label = NULL_TREE;
5420 body = c_parser_c99_block_statement (parser);
5421 if (is_foreach_statement)
5422 objc_finish_foreach_loop (loc, object_expression, collection_expression, body, c_break_label, c_cont_label);
5423 else
5424 c_finish_loop (loc, cond, incr, body, c_break_label, c_cont_label, true);
5425 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99 || c_dialect_objc ()));
5426 c_break_label = save_break;
5427 c_cont_label = save_cont;
5428 }
5429
5430 /* Parse an asm statement, a GNU extension. This is a full-blown asm
5431 statement with inputs, outputs, clobbers, and volatile tag
5432 allowed.
5433
5434 asm-statement:
5435 asm type-qualifier[opt] ( asm-argument ) ;
5436 asm type-qualifier[opt] goto ( asm-goto-argument ) ;
5437
5438 asm-argument:
5439 asm-string-literal
5440 asm-string-literal : asm-operands[opt]
5441 asm-string-literal : asm-operands[opt] : asm-operands[opt]
5442 asm-string-literal : asm-operands[opt] : asm-operands[opt] : asm-clobbers[opt]
5443
5444 asm-goto-argument:
5445 asm-string-literal : : asm-operands[opt] : asm-clobbers[opt] \
5446 : asm-goto-operands
5447
5448 Qualifiers other than volatile are accepted in the syntax but
5449 warned for. */
5450
5451 static tree
5452 c_parser_asm_statement (c_parser *parser)
5453 {
5454 tree quals, str, outputs, inputs, clobbers, labels, ret;
5455 bool simple, is_goto;
5456 location_t asm_loc = c_parser_peek_token (parser)->location;
5457 int section, nsections;
5458
5459 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
5460 c_parser_consume_token (parser);
5461 if (c_parser_next_token_is_keyword (parser, RID_VOLATILE))
5462 {
5463 quals = c_parser_peek_token (parser)->value;
5464 c_parser_consume_token (parser);
5465 }
5466 else if (c_parser_next_token_is_keyword (parser, RID_CONST)
5467 || c_parser_next_token_is_keyword (parser, RID_RESTRICT))
5468 {
5469 warning_at (c_parser_peek_token (parser)->location,
5470 0,
5471 "%E qualifier ignored on asm",
5472 c_parser_peek_token (parser)->value);
5473 quals = NULL_TREE;
5474 c_parser_consume_token (parser);
5475 }
5476 else
5477 quals = NULL_TREE;
5478
5479 is_goto = false;
5480 if (c_parser_next_token_is_keyword (parser, RID_GOTO))
5481 {
5482 c_parser_consume_token (parser);
5483 is_goto = true;
5484 }
5485
5486 /* ??? Follow the C++ parser rather than using the
5487 lex_untranslated_string kludge. */
5488 parser->lex_untranslated_string = true;
5489 ret = NULL;
5490
5491 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5492 goto error;
5493
5494 str = c_parser_asm_string_literal (parser);
5495 if (str == NULL_TREE)
5496 goto error_close_paren;
5497
5498 simple = true;
5499 outputs = NULL_TREE;
5500 inputs = NULL_TREE;
5501 clobbers = NULL_TREE;
5502 labels = NULL_TREE;
5503
5504 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN) && !is_goto)
5505 goto done_asm;
5506
5507 /* Parse each colon-delimited section of operands. */
5508 nsections = 3 + is_goto;
5509 for (section = 0; section < nsections; ++section)
5510 {
5511 if (!c_parser_require (parser, CPP_COLON,
5512 is_goto
5513 ? "expected %<:%>"
5514 : "expected %<:%> or %<)%>"))
5515 goto error_close_paren;
5516
5517 /* Once past any colon, we're no longer a simple asm. */
5518 simple = false;
5519
5520 if ((!c_parser_next_token_is (parser, CPP_COLON)
5521 && !c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
5522 || section == 3)
5523 switch (section)
5524 {
5525 case 0:
5526 /* For asm goto, we don't allow output operands, but reserve
5527 the slot for a future extension that does allow them. */
5528 if (!is_goto)
5529 outputs = c_parser_asm_operands (parser);
5530 break;
5531 case 1:
5532 inputs = c_parser_asm_operands (parser);
5533 break;
5534 case 2:
5535 clobbers = c_parser_asm_clobbers (parser);
5536 break;
5537 case 3:
5538 labels = c_parser_asm_goto_operands (parser);
5539 break;
5540 default:
5541 gcc_unreachable ();
5542 }
5543
5544 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN) && !is_goto)
5545 goto done_asm;
5546 }
5547
5548 done_asm:
5549 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
5550 {
5551 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5552 goto error;
5553 }
5554
5555 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
5556 c_parser_skip_to_end_of_block_or_statement (parser);
5557
5558 ret = build_asm_stmt (quals, build_asm_expr (asm_loc, str, outputs, inputs,
5559 clobbers, labels, simple));
5560
5561 error:
5562 parser->lex_untranslated_string = false;
5563 return ret;
5564
5565 error_close_paren:
5566 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5567 goto error;
5568 }
5569
5570 /* Parse asm operands, a GNU extension.
5571
5572 asm-operands:
5573 asm-operand
5574 asm-operands , asm-operand
5575
5576 asm-operand:
5577 asm-string-literal ( expression )
5578 [ identifier ] asm-string-literal ( expression )
5579 */
5580
5581 static tree
5582 c_parser_asm_operands (c_parser *parser)
5583 {
5584 tree list = NULL_TREE;
5585 while (true)
5586 {
5587 tree name, str;
5588 struct c_expr expr;
5589 if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
5590 {
5591 c_parser_consume_token (parser);
5592 if (c_parser_next_token_is (parser, CPP_NAME))
5593 {
5594 tree id = c_parser_peek_token (parser)->value;
5595 c_parser_consume_token (parser);
5596 name = build_string (IDENTIFIER_LENGTH (id),
5597 IDENTIFIER_POINTER (id));
5598 }
5599 else
5600 {
5601 c_parser_error (parser, "expected identifier");
5602 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
5603 return NULL_TREE;
5604 }
5605 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
5606 "expected %<]%>");
5607 }
5608 else
5609 name = NULL_TREE;
5610 str = c_parser_asm_string_literal (parser);
5611 if (str == NULL_TREE)
5612 return NULL_TREE;
5613 parser->lex_untranslated_string = false;
5614 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5615 {
5616 parser->lex_untranslated_string = true;
5617 return NULL_TREE;
5618 }
5619 expr = c_parser_expression (parser);
5620 mark_exp_read (expr.value);
5621 parser->lex_untranslated_string = true;
5622 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
5623 {
5624 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5625 return NULL_TREE;
5626 }
5627 list = chainon (list, build_tree_list (build_tree_list (name, str),
5628 expr.value));
5629 if (c_parser_next_token_is (parser, CPP_COMMA))
5630 c_parser_consume_token (parser);
5631 else
5632 break;
5633 }
5634 return list;
5635 }
5636
5637 /* Parse asm clobbers, a GNU extension.
5638
5639 asm-clobbers:
5640 asm-string-literal
5641 asm-clobbers , asm-string-literal
5642 */
5643
5644 static tree
5645 c_parser_asm_clobbers (c_parser *parser)
5646 {
5647 tree list = NULL_TREE;
5648 while (true)
5649 {
5650 tree str = c_parser_asm_string_literal (parser);
5651 if (str)
5652 list = tree_cons (NULL_TREE, str, list);
5653 else
5654 return NULL_TREE;
5655 if (c_parser_next_token_is (parser, CPP_COMMA))
5656 c_parser_consume_token (parser);
5657 else
5658 break;
5659 }
5660 return list;
5661 }
5662
5663 /* Parse asm goto labels, a GNU extension.
5664
5665 asm-goto-operands:
5666 identifier
5667 asm-goto-operands , identifier
5668 */
5669
5670 static tree
5671 c_parser_asm_goto_operands (c_parser *parser)
5672 {
5673 tree list = NULL_TREE;
5674 while (true)
5675 {
5676 tree name, label;
5677
5678 if (c_parser_next_token_is (parser, CPP_NAME))
5679 {
5680 c_token *tok = c_parser_peek_token (parser);
5681 name = tok->value;
5682 label = lookup_label_for_goto (tok->location, name);
5683 c_parser_consume_token (parser);
5684 TREE_USED (label) = 1;
5685 }
5686 else
5687 {
5688 c_parser_error (parser, "expected identifier");
5689 return NULL_TREE;
5690 }
5691
5692 name = build_string (IDENTIFIER_LENGTH (name),
5693 IDENTIFIER_POINTER (name));
5694 list = tree_cons (name, label, list);
5695 if (c_parser_next_token_is (parser, CPP_COMMA))
5696 c_parser_consume_token (parser);
5697 else
5698 return nreverse (list);
5699 }
5700 }
5701
5702 /* Parse an expression other than a compound expression; that is, an
5703 assignment expression (C90 6.3.16, C99 6.5.16). If AFTER is not
5704 NULL then it is an Objective-C message expression which is the
5705 primary-expression starting the expression as an initializer.
5706
5707 assignment-expression:
5708 conditional-expression
5709 unary-expression assignment-operator assignment-expression
5710
5711 assignment-operator: one of
5712 = *= /= %= += -= <<= >>= &= ^= |=
5713
5714 In GNU C we accept any conditional expression on the LHS and
5715 diagnose the invalid lvalue rather than producing a syntax
5716 error. */
5717
5718 static struct c_expr
5719 c_parser_expr_no_commas (c_parser *parser, struct c_expr *after,
5720 tree omp_atomic_lhs)
5721 {
5722 struct c_expr lhs, rhs, ret;
5723 enum tree_code code;
5724 location_t op_location, exp_location;
5725 gcc_assert (!after || c_dialect_objc ());
5726 lhs = c_parser_conditional_expression (parser, after, omp_atomic_lhs);
5727 op_location = c_parser_peek_token (parser)->location;
5728 switch (c_parser_peek_token (parser)->type)
5729 {
5730 case CPP_EQ:
5731 code = NOP_EXPR;
5732 break;
5733 case CPP_MULT_EQ:
5734 code = MULT_EXPR;
5735 break;
5736 case CPP_DIV_EQ:
5737 code = TRUNC_DIV_EXPR;
5738 break;
5739 case CPP_MOD_EQ:
5740 code = TRUNC_MOD_EXPR;
5741 break;
5742 case CPP_PLUS_EQ:
5743 code = PLUS_EXPR;
5744 break;
5745 case CPP_MINUS_EQ:
5746 code = MINUS_EXPR;
5747 break;
5748 case CPP_LSHIFT_EQ:
5749 code = LSHIFT_EXPR;
5750 break;
5751 case CPP_RSHIFT_EQ:
5752 code = RSHIFT_EXPR;
5753 break;
5754 case CPP_AND_EQ:
5755 code = BIT_AND_EXPR;
5756 break;
5757 case CPP_XOR_EQ:
5758 code = BIT_XOR_EXPR;
5759 break;
5760 case CPP_OR_EQ:
5761 code = BIT_IOR_EXPR;
5762 break;
5763 default:
5764 return lhs;
5765 }
5766 c_parser_consume_token (parser);
5767 exp_location = c_parser_peek_token (parser)->location;
5768 rhs = c_parser_expr_no_commas (parser, NULL);
5769 rhs = convert_lvalue_to_rvalue (exp_location, rhs, true, true);
5770
5771 ret.value = build_modify_expr (op_location, lhs.value, lhs.original_type,
5772 code, exp_location, rhs.value,
5773 rhs.original_type);
5774 if (code == NOP_EXPR)
5775 ret.original_code = MODIFY_EXPR;
5776 else
5777 {
5778 TREE_NO_WARNING (ret.value) = 1;
5779 ret.original_code = ERROR_MARK;
5780 }
5781 ret.original_type = NULL;
5782 return ret;
5783 }
5784
5785 /* Parse a conditional expression (C90 6.3.15, C99 6.5.15). If AFTER
5786 is not NULL then it is an Objective-C message expression which is
5787 the primary-expression starting the expression as an initializer.
5788
5789 conditional-expression:
5790 logical-OR-expression
5791 logical-OR-expression ? expression : conditional-expression
5792
5793 GNU extensions:
5794
5795 conditional-expression:
5796 logical-OR-expression ? : conditional-expression
5797 */
5798
5799 static struct c_expr
5800 c_parser_conditional_expression (c_parser *parser, struct c_expr *after,
5801 tree omp_atomic_lhs)
5802 {
5803 struct c_expr cond, exp1, exp2, ret;
5804 location_t cond_loc, colon_loc, middle_loc;
5805
5806 gcc_assert (!after || c_dialect_objc ());
5807
5808 cond = c_parser_binary_expression (parser, after, omp_atomic_lhs);
5809
5810 if (c_parser_next_token_is_not (parser, CPP_QUERY))
5811 return cond;
5812 cond_loc = c_parser_peek_token (parser)->location;
5813 cond = convert_lvalue_to_rvalue (cond_loc, cond, true, true);
5814 c_parser_consume_token (parser);
5815 if (c_parser_next_token_is (parser, CPP_COLON))
5816 {
5817 tree eptype = NULL_TREE;
5818
5819 middle_loc = c_parser_peek_token (parser)->location;
5820 pedwarn (middle_loc, OPT_Wpedantic,
5821 "ISO C forbids omitting the middle term of a ?: expression");
5822 warn_for_omitted_condop (middle_loc, cond.value);
5823 if (TREE_CODE (cond.value) == EXCESS_PRECISION_EXPR)
5824 {
5825 eptype = TREE_TYPE (cond.value);
5826 cond.value = TREE_OPERAND (cond.value, 0);
5827 }
5828 /* Make sure first operand is calculated only once. */
5829 exp1.value = c_save_expr (default_conversion (cond.value));
5830 if (eptype)
5831 exp1.value = build1 (EXCESS_PRECISION_EXPR, eptype, exp1.value);
5832 exp1.original_type = NULL;
5833 cond.value = c_objc_common_truthvalue_conversion (cond_loc, exp1.value);
5834 c_inhibit_evaluation_warnings += cond.value == truthvalue_true_node;
5835 }
5836 else
5837 {
5838 cond.value
5839 = c_objc_common_truthvalue_conversion
5840 (cond_loc, default_conversion (cond.value));
5841 c_inhibit_evaluation_warnings += cond.value == truthvalue_false_node;
5842 exp1 = c_parser_expression_conv (parser);
5843 mark_exp_read (exp1.value);
5844 c_inhibit_evaluation_warnings +=
5845 ((cond.value == truthvalue_true_node)
5846 - (cond.value == truthvalue_false_node));
5847 }
5848
5849 colon_loc = c_parser_peek_token (parser)->location;
5850 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
5851 {
5852 c_inhibit_evaluation_warnings -= cond.value == truthvalue_true_node;
5853 ret.value = error_mark_node;
5854 ret.original_code = ERROR_MARK;
5855 ret.original_type = NULL;
5856 return ret;
5857 }
5858 {
5859 location_t exp2_loc = c_parser_peek_token (parser)->location;
5860 exp2 = c_parser_conditional_expression (parser, NULL, NULL_TREE);
5861 exp2 = convert_lvalue_to_rvalue (exp2_loc, exp2, true, true);
5862 }
5863 c_inhibit_evaluation_warnings -= cond.value == truthvalue_true_node;
5864 ret.value = build_conditional_expr (colon_loc, cond.value,
5865 cond.original_code == C_MAYBE_CONST_EXPR,
5866 exp1.value, exp1.original_type,
5867 exp2.value, exp2.original_type);
5868 ret.original_code = ERROR_MARK;
5869 if (exp1.value == error_mark_node || exp2.value == error_mark_node)
5870 ret.original_type = NULL;
5871 else
5872 {
5873 tree t1, t2;
5874
5875 /* If both sides are enum type, the default conversion will have
5876 made the type of the result be an integer type. We want to
5877 remember the enum types we started with. */
5878 t1 = exp1.original_type ? exp1.original_type : TREE_TYPE (exp1.value);
5879 t2 = exp2.original_type ? exp2.original_type : TREE_TYPE (exp2.value);
5880 ret.original_type = ((t1 != error_mark_node
5881 && t2 != error_mark_node
5882 && (TYPE_MAIN_VARIANT (t1)
5883 == TYPE_MAIN_VARIANT (t2)))
5884 ? t1
5885 : NULL);
5886 }
5887 return ret;
5888 }
5889
5890 /* Parse a binary expression; that is, a logical-OR-expression (C90
5891 6.3.5-6.3.14, C99 6.5.5-6.5.14). If AFTER is not NULL then it is
5892 an Objective-C message expression which is the primary-expression
5893 starting the expression as an initializer.
5894
5895 OMP_ATOMIC_LHS is NULL, unless parsing OpenMP #pragma omp atomic,
5896 when it should be the unfolded lhs. In a valid OpenMP source,
5897 one of the operands of the toplevel binary expression must be equal
5898 to it. In that case, just return a build2 created binary operation
5899 rather than result of parser_build_binary_op.
5900
5901 multiplicative-expression:
5902 cast-expression
5903 multiplicative-expression * cast-expression
5904 multiplicative-expression / cast-expression
5905 multiplicative-expression % cast-expression
5906
5907 additive-expression:
5908 multiplicative-expression
5909 additive-expression + multiplicative-expression
5910 additive-expression - multiplicative-expression
5911
5912 shift-expression:
5913 additive-expression
5914 shift-expression << additive-expression
5915 shift-expression >> additive-expression
5916
5917 relational-expression:
5918 shift-expression
5919 relational-expression < shift-expression
5920 relational-expression > shift-expression
5921 relational-expression <= shift-expression
5922 relational-expression >= shift-expression
5923
5924 equality-expression:
5925 relational-expression
5926 equality-expression == relational-expression
5927 equality-expression != relational-expression
5928
5929 AND-expression:
5930 equality-expression
5931 AND-expression & equality-expression
5932
5933 exclusive-OR-expression:
5934 AND-expression
5935 exclusive-OR-expression ^ AND-expression
5936
5937 inclusive-OR-expression:
5938 exclusive-OR-expression
5939 inclusive-OR-expression | exclusive-OR-expression
5940
5941 logical-AND-expression:
5942 inclusive-OR-expression
5943 logical-AND-expression && inclusive-OR-expression
5944
5945 logical-OR-expression:
5946 logical-AND-expression
5947 logical-OR-expression || logical-AND-expression
5948 */
5949
5950 static struct c_expr
5951 c_parser_binary_expression (c_parser *parser, struct c_expr *after,
5952 tree omp_atomic_lhs)
5953 {
5954 /* A binary expression is parsed using operator-precedence parsing,
5955 with the operands being cast expressions. All the binary
5956 operators are left-associative. Thus a binary expression is of
5957 form:
5958
5959 E0 op1 E1 op2 E2 ...
5960
5961 which we represent on a stack. On the stack, the precedence
5962 levels are strictly increasing. When a new operator is
5963 encountered of higher precedence than that at the top of the
5964 stack, it is pushed; its LHS is the top expression, and its RHS
5965 is everything parsed until it is popped. When a new operator is
5966 encountered with precedence less than or equal to that at the top
5967 of the stack, triples E[i-1] op[i] E[i] are popped and replaced
5968 by the result of the operation until the operator at the top of
5969 the stack has lower precedence than the new operator or there is
5970 only one element on the stack; then the top expression is the LHS
5971 of the new operator. In the case of logical AND and OR
5972 expressions, we also need to adjust c_inhibit_evaluation_warnings
5973 as appropriate when the operators are pushed and popped. */
5974
5975 struct {
5976 /* The expression at this stack level. */
5977 struct c_expr expr;
5978 /* The precedence of the operator on its left, PREC_NONE at the
5979 bottom of the stack. */
5980 enum c_parser_prec prec;
5981 /* The operation on its left. */
5982 enum tree_code op;
5983 /* The source location of this operation. */
5984 location_t loc;
5985 } stack[NUM_PRECS];
5986 int sp;
5987 /* Location of the binary operator. */
5988 location_t binary_loc = UNKNOWN_LOCATION; /* Quiet warning. */
5989 #define POP \
5990 do { \
5991 switch (stack[sp].op) \
5992 { \
5993 case TRUTH_ANDIF_EXPR: \
5994 c_inhibit_evaluation_warnings -= (stack[sp - 1].expr.value \
5995 == truthvalue_false_node); \
5996 break; \
5997 case TRUTH_ORIF_EXPR: \
5998 c_inhibit_evaluation_warnings -= (stack[sp - 1].expr.value \
5999 == truthvalue_true_node); \
6000 break; \
6001 default: \
6002 break; \
6003 } \
6004 stack[sp - 1].expr \
6005 = convert_lvalue_to_rvalue (stack[sp - 1].loc, \
6006 stack[sp - 1].expr, true, true); \
6007 stack[sp].expr \
6008 = convert_lvalue_to_rvalue (stack[sp].loc, \
6009 stack[sp].expr, true, true); \
6010 if (__builtin_expect (omp_atomic_lhs != NULL_TREE, 0) && sp == 1 \
6011 && c_parser_peek_token (parser)->type == CPP_SEMICOLON \
6012 && ((1 << stack[sp].prec) \
6013 & (1 << (PREC_BITOR | PREC_BITXOR | PREC_BITAND | PREC_SHIFT \
6014 | PREC_ADD | PREC_MULT))) \
6015 && stack[sp].op != TRUNC_MOD_EXPR \
6016 && stack[0].expr.value != error_mark_node \
6017 && stack[1].expr.value != error_mark_node \
6018 && (c_tree_equal (stack[0].expr.value, omp_atomic_lhs) \
6019 || c_tree_equal (stack[1].expr.value, omp_atomic_lhs))) \
6020 stack[0].expr.value \
6021 = build2 (stack[1].op, TREE_TYPE (stack[0].expr.value), \
6022 stack[0].expr.value, stack[1].expr.value); \
6023 else \
6024 stack[sp - 1].expr = parser_build_binary_op (stack[sp].loc, \
6025 stack[sp].op, \
6026 stack[sp - 1].expr, \
6027 stack[sp].expr); \
6028 sp--; \
6029 } while (0)
6030 gcc_assert (!after || c_dialect_objc ());
6031 stack[0].loc = c_parser_peek_token (parser)->location;
6032 stack[0].expr = c_parser_cast_expression (parser, after);
6033 stack[0].prec = PREC_NONE;
6034 sp = 0;
6035 while (true)
6036 {
6037 enum c_parser_prec oprec;
6038 enum tree_code ocode;
6039 if (parser->error)
6040 goto out;
6041 switch (c_parser_peek_token (parser)->type)
6042 {
6043 case CPP_MULT:
6044 oprec = PREC_MULT;
6045 ocode = MULT_EXPR;
6046 break;
6047 case CPP_DIV:
6048 oprec = PREC_MULT;
6049 ocode = TRUNC_DIV_EXPR;
6050 break;
6051 case CPP_MOD:
6052 oprec = PREC_MULT;
6053 ocode = TRUNC_MOD_EXPR;
6054 break;
6055 case CPP_PLUS:
6056 oprec = PREC_ADD;
6057 ocode = PLUS_EXPR;
6058 break;
6059 case CPP_MINUS:
6060 oprec = PREC_ADD;
6061 ocode = MINUS_EXPR;
6062 break;
6063 case CPP_LSHIFT:
6064 oprec = PREC_SHIFT;
6065 ocode = LSHIFT_EXPR;
6066 break;
6067 case CPP_RSHIFT:
6068 oprec = PREC_SHIFT;
6069 ocode = RSHIFT_EXPR;
6070 break;
6071 case CPP_LESS:
6072 oprec = PREC_REL;
6073 ocode = LT_EXPR;
6074 break;
6075 case CPP_GREATER:
6076 oprec = PREC_REL;
6077 ocode = GT_EXPR;
6078 break;
6079 case CPP_LESS_EQ:
6080 oprec = PREC_REL;
6081 ocode = LE_EXPR;
6082 break;
6083 case CPP_GREATER_EQ:
6084 oprec = PREC_REL;
6085 ocode = GE_EXPR;
6086 break;
6087 case CPP_EQ_EQ:
6088 oprec = PREC_EQ;
6089 ocode = EQ_EXPR;
6090 break;
6091 case CPP_NOT_EQ:
6092 oprec = PREC_EQ;
6093 ocode = NE_EXPR;
6094 break;
6095 case CPP_AND:
6096 oprec = PREC_BITAND;
6097 ocode = BIT_AND_EXPR;
6098 break;
6099 case CPP_XOR:
6100 oprec = PREC_BITXOR;
6101 ocode = BIT_XOR_EXPR;
6102 break;
6103 case CPP_OR:
6104 oprec = PREC_BITOR;
6105 ocode = BIT_IOR_EXPR;
6106 break;
6107 case CPP_AND_AND:
6108 oprec = PREC_LOGAND;
6109 ocode = TRUTH_ANDIF_EXPR;
6110 break;
6111 case CPP_OR_OR:
6112 oprec = PREC_LOGOR;
6113 ocode = TRUTH_ORIF_EXPR;
6114 break;
6115 default:
6116 /* Not a binary operator, so end of the binary
6117 expression. */
6118 goto out;
6119 }
6120 binary_loc = c_parser_peek_token (parser)->location;
6121 while (oprec <= stack[sp].prec)
6122 POP;
6123 c_parser_consume_token (parser);
6124 switch (ocode)
6125 {
6126 case TRUTH_ANDIF_EXPR:
6127 stack[sp].expr
6128 = convert_lvalue_to_rvalue (stack[sp].loc,
6129 stack[sp].expr, true, true);
6130 stack[sp].expr.value = c_objc_common_truthvalue_conversion
6131 (stack[sp].loc, default_conversion (stack[sp].expr.value));
6132 c_inhibit_evaluation_warnings += (stack[sp].expr.value
6133 == truthvalue_false_node);
6134 break;
6135 case TRUTH_ORIF_EXPR:
6136 stack[sp].expr
6137 = convert_lvalue_to_rvalue (stack[sp].loc,
6138 stack[sp].expr, true, true);
6139 stack[sp].expr.value = c_objc_common_truthvalue_conversion
6140 (stack[sp].loc, default_conversion (stack[sp].expr.value));
6141 c_inhibit_evaluation_warnings += (stack[sp].expr.value
6142 == truthvalue_true_node);
6143 break;
6144 default:
6145 break;
6146 }
6147 sp++;
6148 stack[sp].loc = binary_loc;
6149 stack[sp].expr = c_parser_cast_expression (parser, NULL);
6150 stack[sp].prec = oprec;
6151 stack[sp].op = ocode;
6152 stack[sp].loc = binary_loc;
6153 }
6154 out:
6155 while (sp > 0)
6156 POP;
6157 return stack[0].expr;
6158 #undef POP
6159 }
6160
6161 /* Parse a cast expression (C90 6.3.4, C99 6.5.4). If AFTER is not
6162 NULL then it is an Objective-C message expression which is the
6163 primary-expression starting the expression as an initializer.
6164
6165 cast-expression:
6166 unary-expression
6167 ( type-name ) unary-expression
6168 */
6169
6170 static struct c_expr
6171 c_parser_cast_expression (c_parser *parser, struct c_expr *after)
6172 {
6173 location_t cast_loc = c_parser_peek_token (parser)->location;
6174 gcc_assert (!after || c_dialect_objc ());
6175 if (after)
6176 return c_parser_postfix_expression_after_primary (parser,
6177 cast_loc, *after);
6178 /* If the expression begins with a parenthesized type name, it may
6179 be either a cast or a compound literal; we need to see whether
6180 the next character is '{' to tell the difference. If not, it is
6181 an unary expression. Full detection of unknown typenames here
6182 would require a 3-token lookahead. */
6183 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
6184 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
6185 {
6186 struct c_type_name *type_name;
6187 struct c_expr ret;
6188 struct c_expr expr;
6189 c_parser_consume_token (parser);
6190 type_name = c_parser_type_name (parser);
6191 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6192 if (type_name == NULL)
6193 {
6194 ret.value = error_mark_node;
6195 ret.original_code = ERROR_MARK;
6196 ret.original_type = NULL;
6197 return ret;
6198 }
6199
6200 /* Save casted types in the function's used types hash table. */
6201 used_types_insert (type_name->specs->type);
6202
6203 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
6204 return c_parser_postfix_expression_after_paren_type (parser, type_name,
6205 cast_loc);
6206 {
6207 location_t expr_loc = c_parser_peek_token (parser)->location;
6208 expr = c_parser_cast_expression (parser, NULL);
6209 expr = convert_lvalue_to_rvalue (expr_loc, expr, true, true);
6210 }
6211 ret.value = c_cast_expr (cast_loc, type_name, expr.value);
6212 ret.original_code = ERROR_MARK;
6213 ret.original_type = NULL;
6214 return ret;
6215 }
6216 else
6217 return c_parser_unary_expression (parser);
6218 }
6219
6220 /* Parse an unary expression (C90 6.3.3, C99 6.5.3).
6221
6222 unary-expression:
6223 postfix-expression
6224 ++ unary-expression
6225 -- unary-expression
6226 unary-operator cast-expression
6227 sizeof unary-expression
6228 sizeof ( type-name )
6229
6230 unary-operator: one of
6231 & * + - ~ !
6232
6233 GNU extensions:
6234
6235 unary-expression:
6236 __alignof__ unary-expression
6237 __alignof__ ( type-name )
6238 && identifier
6239
6240 (C11 permits _Alignof with type names only.)
6241
6242 unary-operator: one of
6243 __extension__ __real__ __imag__
6244
6245 Transactional Memory:
6246
6247 unary-expression:
6248 transaction-expression
6249
6250 In addition, the GNU syntax treats ++ and -- as unary operators, so
6251 they may be applied to cast expressions with errors for non-lvalues
6252 given later. */
6253
6254 static struct c_expr
6255 c_parser_unary_expression (c_parser *parser)
6256 {
6257 int ext;
6258 struct c_expr ret, op;
6259 location_t op_loc = c_parser_peek_token (parser)->location;
6260 location_t exp_loc;
6261 ret.original_code = ERROR_MARK;
6262 ret.original_type = NULL;
6263 switch (c_parser_peek_token (parser)->type)
6264 {
6265 case CPP_PLUS_PLUS:
6266 c_parser_consume_token (parser);
6267 exp_loc = c_parser_peek_token (parser)->location;
6268 op = c_parser_cast_expression (parser, NULL);
6269
6270 /* If there is array notations in op, we expand them. */
6271 if (flag_enable_cilkplus && TREE_CODE (op.value) == ARRAY_NOTATION_REF)
6272 return fix_array_notation_expr (exp_loc, PREINCREMENT_EXPR, op);
6273 else
6274 {
6275 op = default_function_array_read_conversion (exp_loc, op);
6276 return parser_build_unary_op (op_loc, PREINCREMENT_EXPR, op);
6277 }
6278 case CPP_MINUS_MINUS:
6279 c_parser_consume_token (parser);
6280 exp_loc = c_parser_peek_token (parser)->location;
6281 op = c_parser_cast_expression (parser, NULL);
6282
6283 /* If there is array notations in op, we expand them. */
6284 if (flag_enable_cilkplus && TREE_CODE (op.value) == ARRAY_NOTATION_REF)
6285 return fix_array_notation_expr (exp_loc, PREDECREMENT_EXPR, op);
6286 else
6287 {
6288 op = default_function_array_read_conversion (exp_loc, op);
6289 return parser_build_unary_op (op_loc, PREDECREMENT_EXPR, op);
6290 }
6291 case CPP_AND:
6292 c_parser_consume_token (parser);
6293 op = c_parser_cast_expression (parser, NULL);
6294 mark_exp_read (op.value);
6295 return parser_build_unary_op (op_loc, ADDR_EXPR, op);
6296 case CPP_MULT:
6297 c_parser_consume_token (parser);
6298 exp_loc = c_parser_peek_token (parser)->location;
6299 op = c_parser_cast_expression (parser, NULL);
6300 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
6301 ret.value = build_indirect_ref (op_loc, op.value, RO_UNARY_STAR);
6302 return ret;
6303 case CPP_PLUS:
6304 if (!c_dialect_objc () && !in_system_header)
6305 warning_at (op_loc,
6306 OPT_Wtraditional,
6307 "traditional C rejects the unary plus operator");
6308 c_parser_consume_token (parser);
6309 exp_loc = c_parser_peek_token (parser)->location;
6310 op = c_parser_cast_expression (parser, NULL);
6311 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
6312 return parser_build_unary_op (op_loc, CONVERT_EXPR, op);
6313 case CPP_MINUS:
6314 c_parser_consume_token (parser);
6315 exp_loc = c_parser_peek_token (parser)->location;
6316 op = c_parser_cast_expression (parser, NULL);
6317 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
6318 return parser_build_unary_op (op_loc, NEGATE_EXPR, op);
6319 case CPP_COMPL:
6320 c_parser_consume_token (parser);
6321 exp_loc = c_parser_peek_token (parser)->location;
6322 op = c_parser_cast_expression (parser, NULL);
6323 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
6324 return parser_build_unary_op (op_loc, BIT_NOT_EXPR, op);
6325 case CPP_NOT:
6326 c_parser_consume_token (parser);
6327 exp_loc = c_parser_peek_token (parser)->location;
6328 op = c_parser_cast_expression (parser, NULL);
6329 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
6330 return parser_build_unary_op (op_loc, TRUTH_NOT_EXPR, op);
6331 case CPP_AND_AND:
6332 /* Refer to the address of a label as a pointer. */
6333 c_parser_consume_token (parser);
6334 if (c_parser_next_token_is (parser, CPP_NAME))
6335 {
6336 ret.value = finish_label_address_expr
6337 (c_parser_peek_token (parser)->value, op_loc);
6338 c_parser_consume_token (parser);
6339 }
6340 else
6341 {
6342 c_parser_error (parser, "expected identifier");
6343 ret.value = error_mark_node;
6344 }
6345 return ret;
6346 case CPP_KEYWORD:
6347 switch (c_parser_peek_token (parser)->keyword)
6348 {
6349 case RID_SIZEOF:
6350 return c_parser_sizeof_expression (parser);
6351 case RID_ALIGNOF:
6352 return c_parser_alignof_expression (parser);
6353 case RID_EXTENSION:
6354 c_parser_consume_token (parser);
6355 ext = disable_extension_diagnostics ();
6356 ret = c_parser_cast_expression (parser, NULL);
6357 restore_extension_diagnostics (ext);
6358 return ret;
6359 case RID_REALPART:
6360 c_parser_consume_token (parser);
6361 exp_loc = c_parser_peek_token (parser)->location;
6362 op = c_parser_cast_expression (parser, NULL);
6363 op = default_function_array_conversion (exp_loc, op);
6364 return parser_build_unary_op (op_loc, REALPART_EXPR, op);
6365 case RID_IMAGPART:
6366 c_parser_consume_token (parser);
6367 exp_loc = c_parser_peek_token (parser)->location;
6368 op = c_parser_cast_expression (parser, NULL);
6369 op = default_function_array_conversion (exp_loc, op);
6370 return parser_build_unary_op (op_loc, IMAGPART_EXPR, op);
6371 case RID_TRANSACTION_ATOMIC:
6372 case RID_TRANSACTION_RELAXED:
6373 return c_parser_transaction_expression (parser,
6374 c_parser_peek_token (parser)->keyword);
6375 default:
6376 return c_parser_postfix_expression (parser);
6377 }
6378 default:
6379 return c_parser_postfix_expression (parser);
6380 }
6381 }
6382
6383 /* Parse a sizeof expression. */
6384
6385 static struct c_expr
6386 c_parser_sizeof_expression (c_parser *parser)
6387 {
6388 struct c_expr expr;
6389 location_t expr_loc;
6390 gcc_assert (c_parser_next_token_is_keyword (parser, RID_SIZEOF));
6391 c_parser_consume_token (parser);
6392 c_inhibit_evaluation_warnings++;
6393 in_sizeof++;
6394 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
6395 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
6396 {
6397 /* Either sizeof ( type-name ) or sizeof unary-expression
6398 starting with a compound literal. */
6399 struct c_type_name *type_name;
6400 c_parser_consume_token (parser);
6401 expr_loc = c_parser_peek_token (parser)->location;
6402 type_name = c_parser_type_name (parser);
6403 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6404 if (type_name == NULL)
6405 {
6406 struct c_expr ret;
6407 c_inhibit_evaluation_warnings--;
6408 in_sizeof--;
6409 ret.value = error_mark_node;
6410 ret.original_code = ERROR_MARK;
6411 ret.original_type = NULL;
6412 return ret;
6413 }
6414 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
6415 {
6416 expr = c_parser_postfix_expression_after_paren_type (parser,
6417 type_name,
6418 expr_loc);
6419 goto sizeof_expr;
6420 }
6421 /* sizeof ( type-name ). */
6422 c_inhibit_evaluation_warnings--;
6423 in_sizeof--;
6424 return c_expr_sizeof_type (expr_loc, type_name);
6425 }
6426 else
6427 {
6428 expr_loc = c_parser_peek_token (parser)->location;
6429 expr = c_parser_unary_expression (parser);
6430 sizeof_expr:
6431 c_inhibit_evaluation_warnings--;
6432 in_sizeof--;
6433 mark_exp_read (expr.value);
6434 if (TREE_CODE (expr.value) == COMPONENT_REF
6435 && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
6436 error_at (expr_loc, "%<sizeof%> applied to a bit-field");
6437 return c_expr_sizeof_expr (expr_loc, expr);
6438 }
6439 }
6440
6441 /* Parse an alignof expression. */
6442
6443 static struct c_expr
6444 c_parser_alignof_expression (c_parser *parser)
6445 {
6446 struct c_expr expr;
6447 location_t loc = c_parser_peek_token (parser)->location;
6448 tree alignof_spelling = c_parser_peek_token (parser)->value;
6449 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ALIGNOF));
6450 /* A diagnostic is not required for the use of this identifier in
6451 the implementation namespace; only diagnose it for the C11
6452 spelling because of existing code using the other spellings. */
6453 if (!flag_isoc11
6454 && strcmp (IDENTIFIER_POINTER (alignof_spelling), "_Alignof") == 0)
6455 {
6456 if (flag_isoc99)
6457 pedwarn (loc, OPT_Wpedantic, "ISO C99 does not support %qE",
6458 alignof_spelling);
6459 else
6460 pedwarn (loc, OPT_Wpedantic, "ISO C90 does not support %qE",
6461 alignof_spelling);
6462 }
6463 c_parser_consume_token (parser);
6464 c_inhibit_evaluation_warnings++;
6465 in_alignof++;
6466 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
6467 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
6468 {
6469 /* Either __alignof__ ( type-name ) or __alignof__
6470 unary-expression starting with a compound literal. */
6471 location_t loc;
6472 struct c_type_name *type_name;
6473 struct c_expr ret;
6474 c_parser_consume_token (parser);
6475 loc = c_parser_peek_token (parser)->location;
6476 type_name = c_parser_type_name (parser);
6477 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6478 if (type_name == NULL)
6479 {
6480 struct c_expr ret;
6481 c_inhibit_evaluation_warnings--;
6482 in_alignof--;
6483 ret.value = error_mark_node;
6484 ret.original_code = ERROR_MARK;
6485 ret.original_type = NULL;
6486 return ret;
6487 }
6488 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
6489 {
6490 expr = c_parser_postfix_expression_after_paren_type (parser,
6491 type_name,
6492 loc);
6493 goto alignof_expr;
6494 }
6495 /* alignof ( type-name ). */
6496 c_inhibit_evaluation_warnings--;
6497 in_alignof--;
6498 ret.value = c_alignof (loc, groktypename (type_name, NULL, NULL));
6499 ret.original_code = ERROR_MARK;
6500 ret.original_type = NULL;
6501 return ret;
6502 }
6503 else
6504 {
6505 struct c_expr ret;
6506 expr = c_parser_unary_expression (parser);
6507 alignof_expr:
6508 mark_exp_read (expr.value);
6509 c_inhibit_evaluation_warnings--;
6510 in_alignof--;
6511 pedwarn (loc, OPT_Wpedantic, "ISO C does not allow %<%E (expression)%>",
6512 alignof_spelling);
6513 ret.value = c_alignof_expr (loc, expr.value);
6514 ret.original_code = ERROR_MARK;
6515 ret.original_type = NULL;
6516 return ret;
6517 }
6518 }
6519
6520 /* Helper function to read arguments of builtins which are interfaces
6521 for the middle-end nodes like COMPLEX_EXPR, VEC_PERM_EXPR and
6522 others. The name of the builtin is passed using BNAME parameter.
6523 Function returns true if there were no errors while parsing and
6524 stores the arguments in CEXPR_LIST. */
6525 static bool
6526 c_parser_get_builtin_args (c_parser *parser, const char *bname,
6527 vec<c_expr_t, va_gc> **ret_cexpr_list,
6528 bool choose_expr_p)
6529 {
6530 location_t loc = c_parser_peek_token (parser)->location;
6531 vec<c_expr_t, va_gc> *cexpr_list;
6532 c_expr_t expr;
6533 bool saved_force_folding_builtin_constant_p;
6534
6535 *ret_cexpr_list = NULL;
6536 if (c_parser_next_token_is_not (parser, CPP_OPEN_PAREN))
6537 {
6538 error_at (loc, "cannot take address of %qs", bname);
6539 return false;
6540 }
6541
6542 c_parser_consume_token (parser);
6543
6544 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
6545 {
6546 c_parser_consume_token (parser);
6547 return true;
6548 }
6549
6550 saved_force_folding_builtin_constant_p
6551 = force_folding_builtin_constant_p;
6552 force_folding_builtin_constant_p |= choose_expr_p;
6553 expr = c_parser_expr_no_commas (parser, NULL);
6554 force_folding_builtin_constant_p
6555 = saved_force_folding_builtin_constant_p;
6556 vec_alloc (cexpr_list, 1);
6557 C_EXPR_APPEND (cexpr_list, expr);
6558 while (c_parser_next_token_is (parser, CPP_COMMA))
6559 {
6560 c_parser_consume_token (parser);
6561 expr = c_parser_expr_no_commas (parser, NULL);
6562 C_EXPR_APPEND (cexpr_list, expr);
6563 }
6564
6565 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
6566 return false;
6567
6568 *ret_cexpr_list = cexpr_list;
6569 return true;
6570 }
6571
6572 /* This represents a single generic-association. */
6573
6574 struct c_generic_association
6575 {
6576 /* The location of the starting token of the type. */
6577 location_t type_location;
6578 /* The association's type, or NULL_TREE for 'default'. */
6579 tree type;
6580 /* The association's expression. */
6581 struct c_expr expression;
6582 };
6583
6584 /* Parse a generic-selection. (C11 6.5.1.1).
6585
6586 generic-selection:
6587 _Generic ( assignment-expression , generic-assoc-list )
6588
6589 generic-assoc-list:
6590 generic-association
6591 generic-assoc-list , generic-association
6592
6593 generic-association:
6594 type-name : assignment-expression
6595 default : assignment-expression
6596 */
6597
6598 static struct c_expr
6599 c_parser_generic_selection (c_parser *parser)
6600 {
6601 vec<c_generic_association> associations = vNULL;
6602 struct c_expr selector, error_expr;
6603 tree selector_type;
6604 struct c_generic_association matched_assoc;
6605 bool match_found = false;
6606 location_t generic_loc, selector_loc;
6607
6608 error_expr.original_code = ERROR_MARK;
6609 error_expr.original_type = NULL;
6610 error_expr.value = error_mark_node;
6611 matched_assoc.type_location = UNKNOWN_LOCATION;
6612 matched_assoc.type = NULL_TREE;
6613 matched_assoc.expression = error_expr;
6614
6615 gcc_assert (c_parser_next_token_is_keyword (parser, RID_GENERIC));
6616 generic_loc = c_parser_peek_token (parser)->location;
6617 c_parser_consume_token (parser);
6618 if (!flag_isoc11)
6619 {
6620 if (flag_isoc99)
6621 pedwarn (generic_loc, OPT_Wpedantic,
6622 "ISO C99 does not support %<_Generic%>");
6623 else
6624 pedwarn (generic_loc, OPT_Wpedantic,
6625 "ISO C90 does not support %<_Generic%>");
6626 }
6627
6628 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6629 return error_expr;
6630
6631 c_inhibit_evaluation_warnings++;
6632 selector_loc = c_parser_peek_token (parser)->location;
6633 selector = c_parser_expr_no_commas (parser, NULL);
6634 selector = default_function_array_conversion (selector_loc, selector);
6635 c_inhibit_evaluation_warnings--;
6636
6637 if (selector.value == error_mark_node)
6638 {
6639 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6640 return selector;
6641 }
6642 selector_type = TREE_TYPE (selector.value);
6643 /* In ISO C terms, rvalues (including the controlling expression of
6644 _Generic) do not have qualified types. */
6645 if (TREE_CODE (selector_type) != ARRAY_TYPE)
6646 selector_type = TYPE_MAIN_VARIANT (selector_type);
6647 /* In ISO C terms, _Noreturn is not part of the type of expressions
6648 such as &abort, but in GCC it is represented internally as a type
6649 qualifier. */
6650 if (FUNCTION_POINTER_TYPE_P (selector_type)
6651 && TYPE_QUALS (TREE_TYPE (selector_type)) != TYPE_UNQUALIFIED)
6652 selector_type
6653 = build_pointer_type (TYPE_MAIN_VARIANT (TREE_TYPE (selector_type)));
6654
6655 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
6656 {
6657 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6658 return error_expr;
6659 }
6660
6661 while (1)
6662 {
6663 struct c_generic_association assoc, *iter;
6664 unsigned int ix;
6665 c_token *token = c_parser_peek_token (parser);
6666
6667 assoc.type_location = token->location;
6668 if (token->type == CPP_KEYWORD && token->keyword == RID_DEFAULT)
6669 {
6670 c_parser_consume_token (parser);
6671 assoc.type = NULL_TREE;
6672 }
6673 else
6674 {
6675 struct c_type_name *type_name;
6676
6677 type_name = c_parser_type_name (parser);
6678 if (type_name == NULL)
6679 {
6680 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6681 goto error_exit;
6682 }
6683 assoc.type = groktypename (type_name, NULL, NULL);
6684 if (assoc.type == error_mark_node)
6685 {
6686 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6687 goto error_exit;
6688 }
6689
6690 if (TREE_CODE (assoc.type) == FUNCTION_TYPE)
6691 error_at (assoc.type_location,
6692 "%<_Generic%> association has function type");
6693 else if (!COMPLETE_TYPE_P (assoc.type))
6694 error_at (assoc.type_location,
6695 "%<_Generic%> association has incomplete type");
6696
6697 if (variably_modified_type_p (assoc.type, NULL_TREE))
6698 error_at (assoc.type_location,
6699 "%<_Generic%> association has "
6700 "variable length type");
6701 }
6702
6703 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
6704 {
6705 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6706 goto error_exit;
6707 }
6708
6709 assoc.expression = c_parser_expr_no_commas (parser, NULL);
6710 if (assoc.expression.value == error_mark_node)
6711 {
6712 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6713 goto error_exit;
6714 }
6715
6716 for (ix = 0; associations.iterate (ix, &iter); ++ix)
6717 {
6718 if (assoc.type == NULL_TREE)
6719 {
6720 if (iter->type == NULL_TREE)
6721 {
6722 error_at (assoc.type_location,
6723 "duplicate %<default%> case in %<_Generic%>");
6724 inform (iter->type_location, "original %<default%> is here");
6725 }
6726 }
6727 else if (iter->type != NULL_TREE)
6728 {
6729 if (comptypes (assoc.type, iter->type))
6730 {
6731 error_at (assoc.type_location,
6732 "%<_Generic%> specifies two compatible types");
6733 inform (iter->type_location, "compatible type is here");
6734 }
6735 }
6736 }
6737
6738 if (assoc.type == NULL_TREE)
6739 {
6740 if (!match_found)
6741 {
6742 matched_assoc = assoc;
6743 match_found = true;
6744 }
6745 }
6746 else if (comptypes (assoc.type, selector_type))
6747 {
6748 if (!match_found || matched_assoc.type == NULL_TREE)
6749 {
6750 matched_assoc = assoc;
6751 match_found = true;
6752 }
6753 else
6754 {
6755 error_at (assoc.type_location,
6756 "%<_Generic> selector matches multiple associations");
6757 inform (matched_assoc.type_location,
6758 "other match is here");
6759 }
6760 }
6761
6762 associations.safe_push (assoc);
6763
6764 if (c_parser_peek_token (parser)->type != CPP_COMMA)
6765 break;
6766 c_parser_consume_token (parser);
6767 }
6768
6769 associations.release ();
6770
6771 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
6772 {
6773 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6774 return error_expr;
6775 }
6776
6777 if (!match_found)
6778 {
6779 error_at (selector_loc, "%<_Generic%> selector of type %qT is not "
6780 "compatible with any association",
6781 selector_type);
6782 return error_expr;
6783 }
6784
6785 return matched_assoc.expression;
6786
6787 error_exit:
6788 associations.release ();
6789 return error_expr;
6790 }
6791
6792 /* Parse a postfix expression (C90 6.3.1-6.3.2, C99 6.5.1-6.5.2).
6793
6794 postfix-expression:
6795 primary-expression
6796 postfix-expression [ expression ]
6797 postfix-expression ( argument-expression-list[opt] )
6798 postfix-expression . identifier
6799 postfix-expression -> identifier
6800 postfix-expression ++
6801 postfix-expression --
6802 ( type-name ) { initializer-list }
6803 ( type-name ) { initializer-list , }
6804
6805 argument-expression-list:
6806 argument-expression
6807 argument-expression-list , argument-expression
6808
6809 primary-expression:
6810 identifier
6811 constant
6812 string-literal
6813 ( expression )
6814 generic-selection
6815
6816 GNU extensions:
6817
6818 primary-expression:
6819 __func__
6820 (treated as a keyword in GNU C)
6821 __FUNCTION__
6822 __PRETTY_FUNCTION__
6823 ( compound-statement )
6824 __builtin_va_arg ( assignment-expression , type-name )
6825 __builtin_offsetof ( type-name , offsetof-member-designator )
6826 __builtin_choose_expr ( assignment-expression ,
6827 assignment-expression ,
6828 assignment-expression )
6829 __builtin_types_compatible_p ( type-name , type-name )
6830 __builtin_complex ( assignment-expression , assignment-expression )
6831 __builtin_shuffle ( assignment-expression , assignment-expression )
6832 __builtin_shuffle ( assignment-expression ,
6833 assignment-expression ,
6834 assignment-expression, )
6835
6836 offsetof-member-designator:
6837 identifier
6838 offsetof-member-designator . identifier
6839 offsetof-member-designator [ expression ]
6840
6841 Objective-C:
6842
6843 primary-expression:
6844 [ objc-receiver objc-message-args ]
6845 @selector ( objc-selector-arg )
6846 @protocol ( identifier )
6847 @encode ( type-name )
6848 objc-string-literal
6849 Classname . identifier
6850 */
6851
6852 static struct c_expr
6853 c_parser_postfix_expression (c_parser *parser)
6854 {
6855 struct c_expr expr, e1;
6856 struct c_type_name *t1, *t2;
6857 location_t loc = c_parser_peek_token (parser)->location;;
6858 expr.original_code = ERROR_MARK;
6859 expr.original_type = NULL;
6860 switch (c_parser_peek_token (parser)->type)
6861 {
6862 case CPP_NUMBER:
6863 expr.value = c_parser_peek_token (parser)->value;
6864 loc = c_parser_peek_token (parser)->location;
6865 c_parser_consume_token (parser);
6866 if (TREE_CODE (expr.value) == FIXED_CST
6867 && !targetm.fixed_point_supported_p ())
6868 {
6869 error_at (loc, "fixed-point types not supported for this target");
6870 expr.value = error_mark_node;
6871 }
6872 break;
6873 case CPP_CHAR:
6874 case CPP_CHAR16:
6875 case CPP_CHAR32:
6876 case CPP_WCHAR:
6877 expr.value = c_parser_peek_token (parser)->value;
6878 c_parser_consume_token (parser);
6879 break;
6880 case CPP_STRING:
6881 case CPP_STRING16:
6882 case CPP_STRING32:
6883 case CPP_WSTRING:
6884 case CPP_UTF8STRING:
6885 expr.value = c_parser_peek_token (parser)->value;
6886 expr.original_code = STRING_CST;
6887 c_parser_consume_token (parser);
6888 break;
6889 case CPP_OBJC_STRING:
6890 gcc_assert (c_dialect_objc ());
6891 expr.value
6892 = objc_build_string_object (c_parser_peek_token (parser)->value);
6893 c_parser_consume_token (parser);
6894 break;
6895 case CPP_NAME:
6896 switch (c_parser_peek_token (parser)->id_kind)
6897 {
6898 case C_ID_ID:
6899 {
6900 tree id = c_parser_peek_token (parser)->value;
6901 c_parser_consume_token (parser);
6902 expr.value = build_external_ref (loc, id,
6903 (c_parser_peek_token (parser)->type
6904 == CPP_OPEN_PAREN),
6905 &expr.original_type);
6906 break;
6907 }
6908 case C_ID_CLASSNAME:
6909 {
6910 /* Here we parse the Objective-C 2.0 Class.name dot
6911 syntax. */
6912 tree class_name = c_parser_peek_token (parser)->value;
6913 tree component;
6914 c_parser_consume_token (parser);
6915 gcc_assert (c_dialect_objc ());
6916 if (!c_parser_require (parser, CPP_DOT, "expected %<.%>"))
6917 {
6918 expr.value = error_mark_node;
6919 break;
6920 }
6921 if (c_parser_next_token_is_not (parser, CPP_NAME))
6922 {
6923 c_parser_error (parser, "expected identifier");
6924 expr.value = error_mark_node;
6925 break;
6926 }
6927 component = c_parser_peek_token (parser)->value;
6928 c_parser_consume_token (parser);
6929 expr.value = objc_build_class_component_ref (class_name,
6930 component);
6931 break;
6932 }
6933 default:
6934 c_parser_error (parser, "expected expression");
6935 expr.value = error_mark_node;
6936 break;
6937 }
6938 break;
6939 case CPP_OPEN_PAREN:
6940 /* A parenthesized expression, statement expression or compound
6941 literal. */
6942 if (c_parser_peek_2nd_token (parser)->type == CPP_OPEN_BRACE)
6943 {
6944 /* A statement expression. */
6945 tree stmt;
6946 location_t brace_loc;
6947 c_parser_consume_token (parser);
6948 brace_loc = c_parser_peek_token (parser)->location;
6949 c_parser_consume_token (parser);
6950 if (!building_stmt_list_p ())
6951 {
6952 error_at (loc, "braced-group within expression allowed "
6953 "only inside a function");
6954 parser->error = true;
6955 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
6956 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6957 expr.value = error_mark_node;
6958 break;
6959 }
6960 stmt = c_begin_stmt_expr ();
6961 c_parser_compound_statement_nostart (parser);
6962 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6963 "expected %<)%>");
6964 pedwarn (loc, OPT_Wpedantic,
6965 "ISO C forbids braced-groups within expressions");
6966 expr.value = c_finish_stmt_expr (brace_loc, stmt);
6967 mark_exp_read (expr.value);
6968 }
6969 else if (c_token_starts_typename (c_parser_peek_2nd_token (parser)))
6970 {
6971 /* A compound literal. ??? Can we actually get here rather
6972 than going directly to
6973 c_parser_postfix_expression_after_paren_type from
6974 elsewhere? */
6975 location_t loc;
6976 struct c_type_name *type_name;
6977 c_parser_consume_token (parser);
6978 loc = c_parser_peek_token (parser)->location;
6979 type_name = c_parser_type_name (parser);
6980 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6981 "expected %<)%>");
6982 if (type_name == NULL)
6983 {
6984 expr.value = error_mark_node;
6985 }
6986 else
6987 expr = c_parser_postfix_expression_after_paren_type (parser,
6988 type_name,
6989 loc);
6990 }
6991 else
6992 {
6993 /* A parenthesized expression. */
6994 c_parser_consume_token (parser);
6995 expr = c_parser_expression (parser);
6996 if (TREE_CODE (expr.value) == MODIFY_EXPR)
6997 TREE_NO_WARNING (expr.value) = 1;
6998 if (expr.original_code != C_MAYBE_CONST_EXPR)
6999 expr.original_code = ERROR_MARK;
7000 /* Don't change EXPR.ORIGINAL_TYPE. */
7001 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7002 "expected %<)%>");
7003 }
7004 break;
7005 case CPP_KEYWORD:
7006 switch (c_parser_peek_token (parser)->keyword)
7007 {
7008 case RID_FUNCTION_NAME:
7009 case RID_PRETTY_FUNCTION_NAME:
7010 case RID_C99_FUNCTION_NAME:
7011 expr.value = fname_decl (loc,
7012 c_parser_peek_token (parser)->keyword,
7013 c_parser_peek_token (parser)->value);
7014 c_parser_consume_token (parser);
7015 break;
7016 case RID_VA_ARG:
7017 c_parser_consume_token (parser);
7018 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7019 {
7020 expr.value = error_mark_node;
7021 break;
7022 }
7023 e1 = c_parser_expr_no_commas (parser, NULL);
7024 mark_exp_read (e1.value);
7025 e1.value = c_fully_fold (e1.value, false, NULL);
7026 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
7027 {
7028 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7029 expr.value = error_mark_node;
7030 break;
7031 }
7032 loc = c_parser_peek_token (parser)->location;
7033 t1 = c_parser_type_name (parser);
7034 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7035 "expected %<)%>");
7036 if (t1 == NULL)
7037 {
7038 expr.value = error_mark_node;
7039 }
7040 else
7041 {
7042 tree type_expr = NULL_TREE;
7043 expr.value = c_build_va_arg (loc, e1.value,
7044 groktypename (t1, &type_expr, NULL));
7045 if (type_expr)
7046 {
7047 expr.value = build2 (C_MAYBE_CONST_EXPR,
7048 TREE_TYPE (expr.value), type_expr,
7049 expr.value);
7050 C_MAYBE_CONST_EXPR_NON_CONST (expr.value) = true;
7051 }
7052 }
7053 break;
7054 case RID_OFFSETOF:
7055 c_parser_consume_token (parser);
7056 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7057 {
7058 expr.value = error_mark_node;
7059 break;
7060 }
7061 t1 = c_parser_type_name (parser);
7062 if (t1 == NULL)
7063 parser->error = true;
7064 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
7065 gcc_assert (parser->error);
7066 if (parser->error)
7067 {
7068 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7069 expr.value = error_mark_node;
7070 break;
7071 }
7072
7073 {
7074 tree type = groktypename (t1, NULL, NULL);
7075 tree offsetof_ref;
7076 if (type == error_mark_node)
7077 offsetof_ref = error_mark_node;
7078 else
7079 {
7080 offsetof_ref = build1 (INDIRECT_REF, type, null_pointer_node);
7081 SET_EXPR_LOCATION (offsetof_ref, loc);
7082 }
7083 /* Parse the second argument to __builtin_offsetof. We
7084 must have one identifier, and beyond that we want to
7085 accept sub structure and sub array references. */
7086 if (c_parser_next_token_is (parser, CPP_NAME))
7087 {
7088 offsetof_ref = build_component_ref
7089 (loc, offsetof_ref, c_parser_peek_token (parser)->value);
7090 c_parser_consume_token (parser);
7091 while (c_parser_next_token_is (parser, CPP_DOT)
7092 || c_parser_next_token_is (parser,
7093 CPP_OPEN_SQUARE)
7094 || c_parser_next_token_is (parser,
7095 CPP_DEREF))
7096 {
7097 if (c_parser_next_token_is (parser, CPP_DEREF))
7098 {
7099 loc = c_parser_peek_token (parser)->location;
7100 offsetof_ref = build_array_ref (loc,
7101 offsetof_ref,
7102 integer_zero_node);
7103 goto do_dot;
7104 }
7105 else if (c_parser_next_token_is (parser, CPP_DOT))
7106 {
7107 do_dot:
7108 c_parser_consume_token (parser);
7109 if (c_parser_next_token_is_not (parser,
7110 CPP_NAME))
7111 {
7112 c_parser_error (parser, "expected identifier");
7113 break;
7114 }
7115 offsetof_ref = build_component_ref
7116 (loc, offsetof_ref,
7117 c_parser_peek_token (parser)->value);
7118 c_parser_consume_token (parser);
7119 }
7120 else
7121 {
7122 struct c_expr ce;
7123 tree idx;
7124 loc = c_parser_peek_token (parser)->location;
7125 c_parser_consume_token (parser);
7126 ce = c_parser_expression (parser);
7127 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
7128 idx = ce.value;
7129 idx = c_fully_fold (idx, false, NULL);
7130 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
7131 "expected %<]%>");
7132 offsetof_ref = build_array_ref (loc, offsetof_ref, idx);
7133 }
7134 }
7135 }
7136 else
7137 c_parser_error (parser, "expected identifier");
7138 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7139 "expected %<)%>");
7140 expr.value = fold_offsetof (offsetof_ref);
7141 }
7142 break;
7143 case RID_CHOOSE_EXPR:
7144 {
7145 vec<c_expr_t, va_gc> *cexpr_list;
7146 c_expr_t *e1_p, *e2_p, *e3_p;
7147 tree c;
7148
7149 c_parser_consume_token (parser);
7150 if (!c_parser_get_builtin_args (parser,
7151 "__builtin_choose_expr",
7152 &cexpr_list, true))
7153 {
7154 expr.value = error_mark_node;
7155 break;
7156 }
7157
7158 if (vec_safe_length (cexpr_list) != 3)
7159 {
7160 error_at (loc, "wrong number of arguments to "
7161 "%<__builtin_choose_expr%>");
7162 expr.value = error_mark_node;
7163 break;
7164 }
7165
7166 e1_p = &(*cexpr_list)[0];
7167 e2_p = &(*cexpr_list)[1];
7168 e3_p = &(*cexpr_list)[2];
7169
7170 c = e1_p->value;
7171 mark_exp_read (e2_p->value);
7172 mark_exp_read (e3_p->value);
7173 if (TREE_CODE (c) != INTEGER_CST
7174 || !INTEGRAL_TYPE_P (TREE_TYPE (c)))
7175 error_at (loc,
7176 "first argument to %<__builtin_choose_expr%> not"
7177 " a constant");
7178 constant_expression_warning (c);
7179 expr = integer_zerop (c) ? *e3_p : *e2_p;
7180 break;
7181 }
7182 case RID_TYPES_COMPATIBLE_P:
7183 c_parser_consume_token (parser);
7184 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7185 {
7186 expr.value = error_mark_node;
7187 break;
7188 }
7189 t1 = c_parser_type_name (parser);
7190 if (t1 == NULL)
7191 {
7192 expr.value = error_mark_node;
7193 break;
7194 }
7195 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
7196 {
7197 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7198 expr.value = error_mark_node;
7199 break;
7200 }
7201 t2 = c_parser_type_name (parser);
7202 if (t2 == NULL)
7203 {
7204 expr.value = error_mark_node;
7205 break;
7206 }
7207 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7208 "expected %<)%>");
7209 {
7210 tree e1, e2;
7211 e1 = groktypename (t1, NULL, NULL);
7212 e2 = groktypename (t2, NULL, NULL);
7213 if (e1 == error_mark_node || e2 == error_mark_node)
7214 {
7215 expr.value = error_mark_node;
7216 break;
7217 }
7218
7219 e1 = TYPE_MAIN_VARIANT (e1);
7220 e2 = TYPE_MAIN_VARIANT (e2);
7221
7222 expr.value
7223 = comptypes (e1, e2) ? integer_one_node : integer_zero_node;
7224 }
7225 break;
7226 case RID_BUILTIN_COMPLEX:
7227 {
7228 vec<c_expr_t, va_gc> *cexpr_list;
7229 c_expr_t *e1_p, *e2_p;
7230
7231 c_parser_consume_token (parser);
7232 if (!c_parser_get_builtin_args (parser,
7233 "__builtin_complex",
7234 &cexpr_list, false))
7235 {
7236 expr.value = error_mark_node;
7237 break;
7238 }
7239
7240 if (vec_safe_length (cexpr_list) != 2)
7241 {
7242 error_at (loc, "wrong number of arguments to "
7243 "%<__builtin_complex%>");
7244 expr.value = error_mark_node;
7245 break;
7246 }
7247
7248 e1_p = &(*cexpr_list)[0];
7249 e2_p = &(*cexpr_list)[1];
7250
7251 *e1_p = convert_lvalue_to_rvalue (loc, *e1_p, true, true);
7252 if (TREE_CODE (e1_p->value) == EXCESS_PRECISION_EXPR)
7253 e1_p->value = convert (TREE_TYPE (e1_p->value),
7254 TREE_OPERAND (e1_p->value, 0));
7255 *e2_p = convert_lvalue_to_rvalue (loc, *e2_p, true, true);
7256 if (TREE_CODE (e2_p->value) == EXCESS_PRECISION_EXPR)
7257 e2_p->value = convert (TREE_TYPE (e2_p->value),
7258 TREE_OPERAND (e2_p->value, 0));
7259 if (!SCALAR_FLOAT_TYPE_P (TREE_TYPE (e1_p->value))
7260 || DECIMAL_FLOAT_TYPE_P (TREE_TYPE (e1_p->value))
7261 || !SCALAR_FLOAT_TYPE_P (TREE_TYPE (e2_p->value))
7262 || DECIMAL_FLOAT_TYPE_P (TREE_TYPE (e2_p->value)))
7263 {
7264 error_at (loc, "%<__builtin_complex%> operand "
7265 "not of real binary floating-point type");
7266 expr.value = error_mark_node;
7267 break;
7268 }
7269 if (TYPE_MAIN_VARIANT (TREE_TYPE (e1_p->value))
7270 != TYPE_MAIN_VARIANT (TREE_TYPE (e2_p->value)))
7271 {
7272 error_at (loc,
7273 "%<__builtin_complex%> operands of different types");
7274 expr.value = error_mark_node;
7275 break;
7276 }
7277 if (!flag_isoc99)
7278 pedwarn (loc, OPT_Wpedantic,
7279 "ISO C90 does not support complex types");
7280 expr.value = build2 (COMPLEX_EXPR,
7281 build_complex_type
7282 (TYPE_MAIN_VARIANT
7283 (TREE_TYPE (e1_p->value))),
7284 e1_p->value, e2_p->value);
7285 break;
7286 }
7287 case RID_BUILTIN_SHUFFLE:
7288 {
7289 vec<c_expr_t, va_gc> *cexpr_list;
7290 unsigned int i;
7291 c_expr_t *p;
7292
7293 c_parser_consume_token (parser);
7294 if (!c_parser_get_builtin_args (parser,
7295 "__builtin_shuffle",
7296 &cexpr_list, false))
7297 {
7298 expr.value = error_mark_node;
7299 break;
7300 }
7301
7302 FOR_EACH_VEC_SAFE_ELT (cexpr_list, i, p)
7303 *p = convert_lvalue_to_rvalue (loc, *p, true, true);
7304
7305 if (vec_safe_length (cexpr_list) == 2)
7306 expr.value =
7307 c_build_vec_perm_expr
7308 (loc, (*cexpr_list)[0].value,
7309 NULL_TREE, (*cexpr_list)[1].value);
7310
7311 else if (vec_safe_length (cexpr_list) == 3)
7312 expr.value =
7313 c_build_vec_perm_expr
7314 (loc, (*cexpr_list)[0].value,
7315 (*cexpr_list)[1].value,
7316 (*cexpr_list)[2].value);
7317 else
7318 {
7319 error_at (loc, "wrong number of arguments to "
7320 "%<__builtin_shuffle%>");
7321 expr.value = error_mark_node;
7322 }
7323 break;
7324 }
7325 case RID_AT_SELECTOR:
7326 gcc_assert (c_dialect_objc ());
7327 c_parser_consume_token (parser);
7328 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7329 {
7330 expr.value = error_mark_node;
7331 break;
7332 }
7333 {
7334 tree sel = c_parser_objc_selector_arg (parser);
7335 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7336 "expected %<)%>");
7337 expr.value = objc_build_selector_expr (loc, sel);
7338 }
7339 break;
7340 case RID_AT_PROTOCOL:
7341 gcc_assert (c_dialect_objc ());
7342 c_parser_consume_token (parser);
7343 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7344 {
7345 expr.value = error_mark_node;
7346 break;
7347 }
7348 if (c_parser_next_token_is_not (parser, CPP_NAME))
7349 {
7350 c_parser_error (parser, "expected identifier");
7351 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7352 expr.value = error_mark_node;
7353 break;
7354 }
7355 {
7356 tree id = c_parser_peek_token (parser)->value;
7357 c_parser_consume_token (parser);
7358 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7359 "expected %<)%>");
7360 expr.value = objc_build_protocol_expr (id);
7361 }
7362 break;
7363 case RID_AT_ENCODE:
7364 /* Extension to support C-structures in the archiver. */
7365 gcc_assert (c_dialect_objc ());
7366 c_parser_consume_token (parser);
7367 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7368 {
7369 expr.value = error_mark_node;
7370 break;
7371 }
7372 t1 = c_parser_type_name (parser);
7373 if (t1 == NULL)
7374 {
7375 expr.value = error_mark_node;
7376 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7377 break;
7378 }
7379 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7380 "expected %<)%>");
7381 {
7382 tree type = groktypename (t1, NULL, NULL);
7383 expr.value = objc_build_encode_expr (type);
7384 }
7385 break;
7386 case RID_GENERIC:
7387 expr = c_parser_generic_selection (parser);
7388 break;
7389 case RID_CILK_SPAWN:
7390 c_parser_consume_token (parser);
7391 if (!flag_enable_cilkplus)
7392 {
7393 error_at (loc, "-fcilkplus must be enabled to use "
7394 "%<_Cilk_spawn%>");
7395 expr = c_parser_postfix_expression (parser);
7396 expr.value = error_mark_node;
7397 }
7398 if (c_parser_peek_token (parser)->keyword == RID_CILK_SPAWN)
7399 {
7400 error_at (loc, "consecutive %<_Cilk_spawn%> keywords "
7401 "are not permitted");
7402 /* Now flush out all the _Cilk_spawns. */
7403 while (c_parser_peek_token (parser)->keyword == RID_CILK_SPAWN)
7404 c_parser_consume_token (parser);
7405 expr = c_parser_postfix_expression (parser);
7406 }
7407 else
7408 {
7409 expr = c_parser_postfix_expression (parser);
7410 expr.value = build_cilk_spawn (loc, expr.value);
7411 }
7412 break;
7413 default:
7414 c_parser_error (parser, "expected expression");
7415 expr.value = error_mark_node;
7416 break;
7417 }
7418 break;
7419 case CPP_OPEN_SQUARE:
7420 if (c_dialect_objc ())
7421 {
7422 tree receiver, args;
7423 c_parser_consume_token (parser);
7424 receiver = c_parser_objc_receiver (parser);
7425 args = c_parser_objc_message_args (parser);
7426 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
7427 "expected %<]%>");
7428 expr.value = objc_build_message_expr (receiver, args);
7429 break;
7430 }
7431 /* Else fall through to report error. */
7432 default:
7433 c_parser_error (parser, "expected expression");
7434 expr.value = error_mark_node;
7435 break;
7436 }
7437 return c_parser_postfix_expression_after_primary (parser, loc, expr);
7438 }
7439
7440 /* Parse a postfix expression after a parenthesized type name: the
7441 brace-enclosed initializer of a compound literal, possibly followed
7442 by some postfix operators. This is separate because it is not
7443 possible to tell until after the type name whether a cast
7444 expression has a cast or a compound literal, or whether the operand
7445 of sizeof is a parenthesized type name or starts with a compound
7446 literal. TYPE_LOC is the location where TYPE_NAME starts--the
7447 location of the first token after the parentheses around the type
7448 name. */
7449
7450 static struct c_expr
7451 c_parser_postfix_expression_after_paren_type (c_parser *parser,
7452 struct c_type_name *type_name,
7453 location_t type_loc)
7454 {
7455 tree type;
7456 struct c_expr init;
7457 bool non_const;
7458 struct c_expr expr;
7459 location_t start_loc;
7460 tree type_expr = NULL_TREE;
7461 bool type_expr_const = true;
7462 check_compound_literal_type (type_loc, type_name);
7463 start_init (NULL_TREE, NULL, 0);
7464 type = groktypename (type_name, &type_expr, &type_expr_const);
7465 start_loc = c_parser_peek_token (parser)->location;
7466 if (type != error_mark_node && C_TYPE_VARIABLE_SIZE (type))
7467 {
7468 error_at (type_loc, "compound literal has variable size");
7469 type = error_mark_node;
7470 }
7471 init = c_parser_braced_init (parser, type, false);
7472 finish_init ();
7473 maybe_warn_string_init (type, init);
7474
7475 if (type != error_mark_node
7476 && !ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (type))
7477 && current_function_decl)
7478 {
7479 error ("compound literal qualified by address-space qualifier");
7480 type = error_mark_node;
7481 }
7482
7483 if (!flag_isoc99)
7484 pedwarn (start_loc, OPT_Wpedantic, "ISO C90 forbids compound literals");
7485 non_const = ((init.value && TREE_CODE (init.value) == CONSTRUCTOR)
7486 ? CONSTRUCTOR_NON_CONST (init.value)
7487 : init.original_code == C_MAYBE_CONST_EXPR);
7488 non_const |= !type_expr_const;
7489 expr.value = build_compound_literal (start_loc, type, init.value, non_const);
7490 expr.original_code = ERROR_MARK;
7491 expr.original_type = NULL;
7492 if (type_expr)
7493 {
7494 if (TREE_CODE (expr.value) == C_MAYBE_CONST_EXPR)
7495 {
7496 gcc_assert (C_MAYBE_CONST_EXPR_PRE (expr.value) == NULL_TREE);
7497 C_MAYBE_CONST_EXPR_PRE (expr.value) = type_expr;
7498 }
7499 else
7500 {
7501 gcc_assert (!non_const);
7502 expr.value = build2 (C_MAYBE_CONST_EXPR, type,
7503 type_expr, expr.value);
7504 }
7505 }
7506 return c_parser_postfix_expression_after_primary (parser, start_loc, expr);
7507 }
7508
7509 /* Callback function for sizeof_pointer_memaccess_warning to compare
7510 types. */
7511
7512 static bool
7513 sizeof_ptr_memacc_comptypes (tree type1, tree type2)
7514 {
7515 return comptypes (type1, type2) == 1;
7516 }
7517
7518 /* Parse a postfix expression after the initial primary or compound
7519 literal; that is, parse a series of postfix operators.
7520
7521 EXPR_LOC is the location of the primary expression. */
7522
7523 static struct c_expr
7524 c_parser_postfix_expression_after_primary (c_parser *parser,
7525 location_t expr_loc,
7526 struct c_expr expr)
7527 {
7528 struct c_expr orig_expr;
7529 tree ident, idx;
7530 location_t sizeof_arg_loc[3];
7531 tree sizeof_arg[3];
7532 unsigned int i;
7533 vec<tree, va_gc> *exprlist;
7534 vec<tree, va_gc> *origtypes = NULL;
7535 while (true)
7536 {
7537 location_t op_loc = c_parser_peek_token (parser)->location;
7538 switch (c_parser_peek_token (parser)->type)
7539 {
7540 case CPP_OPEN_SQUARE:
7541 /* Array reference. */
7542 c_parser_consume_token (parser);
7543 if (flag_enable_cilkplus
7544 && c_parser_peek_token (parser)->type == CPP_COLON)
7545 /* If we are here, then we have something like this:
7546 Array [ : ]
7547 */
7548 expr.value = c_parser_array_notation (expr_loc, parser, NULL_TREE,
7549 expr.value);
7550 else
7551 {
7552 idx = c_parser_expression (parser).value;
7553 /* Here we have 3 options:
7554 1. Array [EXPR] -- Normal Array call.
7555 2. Array [EXPR : EXPR] -- Array notation without stride.
7556 3. Array [EXPR : EXPR : EXPR] -- Array notation with stride.
7557
7558 For 1, we just handle it just like a normal array expression.
7559 For 2 and 3 we handle it like we handle array notations. The
7560 idx value we have above becomes the initial/start index.
7561 */
7562 if (flag_enable_cilkplus
7563 && c_parser_peek_token (parser)->type == CPP_COLON)
7564 expr.value = c_parser_array_notation (expr_loc, parser, idx,
7565 expr.value);
7566 else
7567 {
7568 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
7569 "expected %<]%>");
7570 expr.value = build_array_ref (op_loc, expr.value, idx);
7571 }
7572 }
7573 expr.original_code = ERROR_MARK;
7574 expr.original_type = NULL;
7575 break;
7576 case CPP_OPEN_PAREN:
7577 /* Function call. */
7578 c_parser_consume_token (parser);
7579 for (i = 0; i < 3; i++)
7580 {
7581 sizeof_arg[i] = NULL_TREE;
7582 sizeof_arg_loc[i] = UNKNOWN_LOCATION;
7583 }
7584 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
7585 exprlist = NULL;
7586 else
7587 exprlist = c_parser_expr_list (parser, true, false, &origtypes,
7588 sizeof_arg_loc, sizeof_arg);
7589 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7590 "expected %<)%>");
7591 orig_expr = expr;
7592 mark_exp_read (expr.value);
7593 if (warn_sizeof_pointer_memaccess)
7594 sizeof_pointer_memaccess_warning (sizeof_arg_loc,
7595 expr.value, exprlist,
7596 sizeof_arg,
7597 sizeof_ptr_memacc_comptypes);
7598 /* FIXME diagnostics: Ideally we want the FUNCNAME, not the
7599 "(" after the FUNCNAME, which is what we have now. */
7600 expr.value = build_function_call_vec (op_loc, expr.value, exprlist,
7601 origtypes);
7602 expr.original_code = ERROR_MARK;
7603 if (TREE_CODE (expr.value) == INTEGER_CST
7604 && TREE_CODE (orig_expr.value) == FUNCTION_DECL
7605 && DECL_BUILT_IN_CLASS (orig_expr.value) == BUILT_IN_NORMAL
7606 && DECL_FUNCTION_CODE (orig_expr.value) == BUILT_IN_CONSTANT_P)
7607 expr.original_code = C_MAYBE_CONST_EXPR;
7608 expr.original_type = NULL;
7609 if (exprlist)
7610 {
7611 release_tree_vector (exprlist);
7612 release_tree_vector (origtypes);
7613 }
7614 break;
7615 case CPP_DOT:
7616 /* Structure element reference. */
7617 c_parser_consume_token (parser);
7618 expr = default_function_array_conversion (expr_loc, expr);
7619 if (c_parser_next_token_is (parser, CPP_NAME))
7620 ident = c_parser_peek_token (parser)->value;
7621 else
7622 {
7623 c_parser_error (parser, "expected identifier");
7624 expr.value = error_mark_node;
7625 expr.original_code = ERROR_MARK;
7626 expr.original_type = NULL;
7627 return expr;
7628 }
7629 c_parser_consume_token (parser);
7630 expr.value = build_component_ref (op_loc, expr.value, ident);
7631 expr.original_code = ERROR_MARK;
7632 if (TREE_CODE (expr.value) != COMPONENT_REF)
7633 expr.original_type = NULL;
7634 else
7635 {
7636 /* Remember the original type of a bitfield. */
7637 tree field = TREE_OPERAND (expr.value, 1);
7638 if (TREE_CODE (field) != FIELD_DECL)
7639 expr.original_type = NULL;
7640 else
7641 expr.original_type = DECL_BIT_FIELD_TYPE (field);
7642 }
7643 break;
7644 case CPP_DEREF:
7645 /* Structure element reference. */
7646 c_parser_consume_token (parser);
7647 expr = convert_lvalue_to_rvalue (expr_loc, expr, true, false);
7648 if (c_parser_next_token_is (parser, CPP_NAME))
7649 ident = c_parser_peek_token (parser)->value;
7650 else
7651 {
7652 c_parser_error (parser, "expected identifier");
7653 expr.value = error_mark_node;
7654 expr.original_code = ERROR_MARK;
7655 expr.original_type = NULL;
7656 return expr;
7657 }
7658 c_parser_consume_token (parser);
7659 expr.value = build_component_ref (op_loc,
7660 build_indirect_ref (op_loc,
7661 expr.value,
7662 RO_ARROW),
7663 ident);
7664 expr.original_code = ERROR_MARK;
7665 if (TREE_CODE (expr.value) != COMPONENT_REF)
7666 expr.original_type = NULL;
7667 else
7668 {
7669 /* Remember the original type of a bitfield. */
7670 tree field = TREE_OPERAND (expr.value, 1);
7671 if (TREE_CODE (field) != FIELD_DECL)
7672 expr.original_type = NULL;
7673 else
7674 expr.original_type = DECL_BIT_FIELD_TYPE (field);
7675 }
7676 break;
7677 case CPP_PLUS_PLUS:
7678 /* Postincrement. */
7679 c_parser_consume_token (parser);
7680 /* If the expressions have array notations, we expand them. */
7681 if (flag_enable_cilkplus
7682 && TREE_CODE (expr.value) == ARRAY_NOTATION_REF)
7683 expr = fix_array_notation_expr (expr_loc, POSTINCREMENT_EXPR, expr);
7684 else
7685 {
7686 expr = default_function_array_read_conversion (expr_loc, expr);
7687 expr.value = build_unary_op (op_loc,
7688 POSTINCREMENT_EXPR, expr.value, 0);
7689 }
7690 expr.original_code = ERROR_MARK;
7691 expr.original_type = NULL;
7692 break;
7693 case CPP_MINUS_MINUS:
7694 /* Postdecrement. */
7695 c_parser_consume_token (parser);
7696 /* If the expressions have array notations, we expand them. */
7697 if (flag_enable_cilkplus
7698 && TREE_CODE (expr.value) == ARRAY_NOTATION_REF)
7699 expr = fix_array_notation_expr (expr_loc, POSTDECREMENT_EXPR, expr);
7700 else
7701 {
7702 expr = default_function_array_read_conversion (expr_loc, expr);
7703 expr.value = build_unary_op (op_loc,
7704 POSTDECREMENT_EXPR, expr.value, 0);
7705 }
7706 expr.original_code = ERROR_MARK;
7707 expr.original_type = NULL;
7708 break;
7709 default:
7710 return expr;
7711 }
7712 }
7713 }
7714
7715 /* Parse an expression (C90 6.3.17, C99 6.5.17).
7716
7717 expression:
7718 assignment-expression
7719 expression , assignment-expression
7720 */
7721
7722 static struct c_expr
7723 c_parser_expression (c_parser *parser)
7724 {
7725 location_t tloc = c_parser_peek_token (parser)->location;
7726 struct c_expr expr;
7727 expr = c_parser_expr_no_commas (parser, NULL);
7728 if (c_parser_next_token_is (parser, CPP_COMMA))
7729 expr = convert_lvalue_to_rvalue (tloc, expr, true, false);
7730 while (c_parser_next_token_is (parser, CPP_COMMA))
7731 {
7732 struct c_expr next;
7733 tree lhsval;
7734 location_t loc = c_parser_peek_token (parser)->location;
7735 location_t expr_loc;
7736 c_parser_consume_token (parser);
7737 expr_loc = c_parser_peek_token (parser)->location;
7738 lhsval = expr.value;
7739 while (TREE_CODE (lhsval) == COMPOUND_EXPR)
7740 lhsval = TREE_OPERAND (lhsval, 1);
7741 if (DECL_P (lhsval) || handled_component_p (lhsval))
7742 mark_exp_read (lhsval);
7743 next = c_parser_expr_no_commas (parser, NULL);
7744 next = convert_lvalue_to_rvalue (expr_loc, next, true, false);
7745 expr.value = build_compound_expr (loc, expr.value, next.value);
7746 expr.original_code = COMPOUND_EXPR;
7747 expr.original_type = next.original_type;
7748 }
7749 return expr;
7750 }
7751
7752 /* Parse an expression and convert functions or arrays to pointers and
7753 lvalues to rvalues. */
7754
7755 static struct c_expr
7756 c_parser_expression_conv (c_parser *parser)
7757 {
7758 struct c_expr expr;
7759 location_t loc = c_parser_peek_token (parser)->location;
7760 expr = c_parser_expression (parser);
7761 expr = convert_lvalue_to_rvalue (loc, expr, true, false);
7762 return expr;
7763 }
7764
7765 /* Parse a non-empty list of expressions. If CONVERT_P, convert
7766 functions and arrays to pointers and lvalues to rvalues. If
7767 FOLD_P, fold the expressions.
7768
7769 nonempty-expr-list:
7770 assignment-expression
7771 nonempty-expr-list , assignment-expression
7772 */
7773
7774 static vec<tree, va_gc> *
7775 c_parser_expr_list (c_parser *parser, bool convert_p, bool fold_p,
7776 vec<tree, va_gc> **p_orig_types,
7777 location_t *sizeof_arg_loc, tree *sizeof_arg)
7778 {
7779 vec<tree, va_gc> *ret;
7780 vec<tree, va_gc> *orig_types;
7781 struct c_expr expr;
7782 location_t loc = c_parser_peek_token (parser)->location;
7783 location_t cur_sizeof_arg_loc = UNKNOWN_LOCATION;
7784 unsigned int idx = 0;
7785
7786 ret = make_tree_vector ();
7787 if (p_orig_types == NULL)
7788 orig_types = NULL;
7789 else
7790 orig_types = make_tree_vector ();
7791
7792 if (sizeof_arg != NULL
7793 && c_parser_next_token_is_keyword (parser, RID_SIZEOF))
7794 cur_sizeof_arg_loc = c_parser_peek_2nd_token (parser)->location;
7795 expr = c_parser_expr_no_commas (parser, NULL);
7796 if (convert_p)
7797 expr = convert_lvalue_to_rvalue (loc, expr, true, true);
7798 if (fold_p)
7799 expr.value = c_fully_fold (expr.value, false, NULL);
7800 ret->quick_push (expr.value);
7801 if (orig_types)
7802 orig_types->quick_push (expr.original_type);
7803 if (sizeof_arg != NULL
7804 && cur_sizeof_arg_loc != UNKNOWN_LOCATION
7805 && expr.original_code == SIZEOF_EXPR)
7806 {
7807 sizeof_arg[0] = c_last_sizeof_arg;
7808 sizeof_arg_loc[0] = cur_sizeof_arg_loc;
7809 }
7810 while (c_parser_next_token_is (parser, CPP_COMMA))
7811 {
7812 c_parser_consume_token (parser);
7813 loc = c_parser_peek_token (parser)->location;
7814 if (sizeof_arg != NULL
7815 && c_parser_next_token_is_keyword (parser, RID_SIZEOF))
7816 cur_sizeof_arg_loc = c_parser_peek_2nd_token (parser)->location;
7817 else
7818 cur_sizeof_arg_loc = UNKNOWN_LOCATION;
7819 expr = c_parser_expr_no_commas (parser, NULL);
7820 if (convert_p)
7821 expr = convert_lvalue_to_rvalue (loc, expr, true, true);
7822 if (fold_p)
7823 expr.value = c_fully_fold (expr.value, false, NULL);
7824 vec_safe_push (ret, expr.value);
7825 if (orig_types)
7826 vec_safe_push (orig_types, expr.original_type);
7827 if (++idx < 3
7828 && sizeof_arg != NULL
7829 && cur_sizeof_arg_loc != UNKNOWN_LOCATION
7830 && expr.original_code == SIZEOF_EXPR)
7831 {
7832 sizeof_arg[idx] = c_last_sizeof_arg;
7833 sizeof_arg_loc[idx] = cur_sizeof_arg_loc;
7834 }
7835 }
7836 if (orig_types)
7837 *p_orig_types = orig_types;
7838 return ret;
7839 }
7840 \f
7841 /* Parse Objective-C-specific constructs. */
7842
7843 /* Parse an objc-class-definition.
7844
7845 objc-class-definition:
7846 @interface identifier objc-superclass[opt] objc-protocol-refs[opt]
7847 objc-class-instance-variables[opt] objc-methodprotolist @end
7848 @implementation identifier objc-superclass[opt]
7849 objc-class-instance-variables[opt]
7850 @interface identifier ( identifier ) objc-protocol-refs[opt]
7851 objc-methodprotolist @end
7852 @interface identifier ( ) objc-protocol-refs[opt]
7853 objc-methodprotolist @end
7854 @implementation identifier ( identifier )
7855
7856 objc-superclass:
7857 : identifier
7858
7859 "@interface identifier (" must start "@interface identifier (
7860 identifier ) ...": objc-methodprotolist in the first production may
7861 not start with a parenthesized identifier as a declarator of a data
7862 definition with no declaration specifiers if the objc-superclass,
7863 objc-protocol-refs and objc-class-instance-variables are omitted. */
7864
7865 static void
7866 c_parser_objc_class_definition (c_parser *parser, tree attributes)
7867 {
7868 bool iface_p;
7869 tree id1;
7870 tree superclass;
7871 if (c_parser_next_token_is_keyword (parser, RID_AT_INTERFACE))
7872 iface_p = true;
7873 else if (c_parser_next_token_is_keyword (parser, RID_AT_IMPLEMENTATION))
7874 iface_p = false;
7875 else
7876 gcc_unreachable ();
7877
7878 c_parser_consume_token (parser);
7879 if (c_parser_next_token_is_not (parser, CPP_NAME))
7880 {
7881 c_parser_error (parser, "expected identifier");
7882 return;
7883 }
7884 id1 = c_parser_peek_token (parser)->value;
7885 c_parser_consume_token (parser);
7886 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
7887 {
7888 /* We have a category or class extension. */
7889 tree id2;
7890 tree proto = NULL_TREE;
7891 c_parser_consume_token (parser);
7892 if (c_parser_next_token_is_not (parser, CPP_NAME))
7893 {
7894 if (iface_p && c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
7895 {
7896 /* We have a class extension. */
7897 id2 = NULL_TREE;
7898 }
7899 else
7900 {
7901 c_parser_error (parser, "expected identifier or %<)%>");
7902 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7903 return;
7904 }
7905 }
7906 else
7907 {
7908 id2 = c_parser_peek_token (parser)->value;
7909 c_parser_consume_token (parser);
7910 }
7911 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
7912 if (!iface_p)
7913 {
7914 objc_start_category_implementation (id1, id2);
7915 return;
7916 }
7917 if (c_parser_next_token_is (parser, CPP_LESS))
7918 proto = c_parser_objc_protocol_refs (parser);
7919 objc_start_category_interface (id1, id2, proto, attributes);
7920 c_parser_objc_methodprotolist (parser);
7921 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
7922 objc_finish_interface ();
7923 return;
7924 }
7925 if (c_parser_next_token_is (parser, CPP_COLON))
7926 {
7927 c_parser_consume_token (parser);
7928 if (c_parser_next_token_is_not (parser, CPP_NAME))
7929 {
7930 c_parser_error (parser, "expected identifier");
7931 return;
7932 }
7933 superclass = c_parser_peek_token (parser)->value;
7934 c_parser_consume_token (parser);
7935 }
7936 else
7937 superclass = NULL_TREE;
7938 if (iface_p)
7939 {
7940 tree proto = NULL_TREE;
7941 if (c_parser_next_token_is (parser, CPP_LESS))
7942 proto = c_parser_objc_protocol_refs (parser);
7943 objc_start_class_interface (id1, superclass, proto, attributes);
7944 }
7945 else
7946 objc_start_class_implementation (id1, superclass);
7947 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
7948 c_parser_objc_class_instance_variables (parser);
7949 if (iface_p)
7950 {
7951 objc_continue_interface ();
7952 c_parser_objc_methodprotolist (parser);
7953 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
7954 objc_finish_interface ();
7955 }
7956 else
7957 {
7958 objc_continue_implementation ();
7959 return;
7960 }
7961 }
7962
7963 /* Parse objc-class-instance-variables.
7964
7965 objc-class-instance-variables:
7966 { objc-instance-variable-decl-list[opt] }
7967
7968 objc-instance-variable-decl-list:
7969 objc-visibility-spec
7970 objc-instance-variable-decl ;
7971 ;
7972 objc-instance-variable-decl-list objc-visibility-spec
7973 objc-instance-variable-decl-list objc-instance-variable-decl ;
7974 objc-instance-variable-decl-list ;
7975
7976 objc-visibility-spec:
7977 @private
7978 @protected
7979 @public
7980
7981 objc-instance-variable-decl:
7982 struct-declaration
7983 */
7984
7985 static void
7986 c_parser_objc_class_instance_variables (c_parser *parser)
7987 {
7988 gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
7989 c_parser_consume_token (parser);
7990 while (c_parser_next_token_is_not (parser, CPP_EOF))
7991 {
7992 tree decls;
7993 /* Parse any stray semicolon. */
7994 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
7995 {
7996 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
7997 "extra semicolon");
7998 c_parser_consume_token (parser);
7999 continue;
8000 }
8001 /* Stop if at the end of the instance variables. */
8002 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
8003 {
8004 c_parser_consume_token (parser);
8005 break;
8006 }
8007 /* Parse any objc-visibility-spec. */
8008 if (c_parser_next_token_is_keyword (parser, RID_AT_PRIVATE))
8009 {
8010 c_parser_consume_token (parser);
8011 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
8012 continue;
8013 }
8014 else if (c_parser_next_token_is_keyword (parser, RID_AT_PROTECTED))
8015 {
8016 c_parser_consume_token (parser);
8017 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
8018 continue;
8019 }
8020 else if (c_parser_next_token_is_keyword (parser, RID_AT_PUBLIC))
8021 {
8022 c_parser_consume_token (parser);
8023 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
8024 continue;
8025 }
8026 else if (c_parser_next_token_is_keyword (parser, RID_AT_PACKAGE))
8027 {
8028 c_parser_consume_token (parser);
8029 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
8030 continue;
8031 }
8032 else if (c_parser_next_token_is (parser, CPP_PRAGMA))
8033 {
8034 c_parser_pragma (parser, pragma_external);
8035 continue;
8036 }
8037
8038 /* Parse some comma-separated declarations. */
8039 decls = c_parser_struct_declaration (parser);
8040 if (decls == NULL)
8041 {
8042 /* There is a syntax error. We want to skip the offending
8043 tokens up to the next ';' (included) or '}'
8044 (excluded). */
8045
8046 /* First, skip manually a ')' or ']'. This is because they
8047 reduce the nesting level, so c_parser_skip_until_found()
8048 wouldn't be able to skip past them. */
8049 c_token *token = c_parser_peek_token (parser);
8050 if (token->type == CPP_CLOSE_PAREN || token->type == CPP_CLOSE_SQUARE)
8051 c_parser_consume_token (parser);
8052
8053 /* Then, do the standard skipping. */
8054 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
8055
8056 /* We hopefully recovered. Start normal parsing again. */
8057 parser->error = false;
8058 continue;
8059 }
8060 else
8061 {
8062 /* Comma-separated instance variables are chained together
8063 in reverse order; add them one by one. */
8064 tree ivar = nreverse (decls);
8065 for (; ivar; ivar = DECL_CHAIN (ivar))
8066 objc_add_instance_variable (copy_node (ivar));
8067 }
8068 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8069 }
8070 }
8071
8072 /* Parse an objc-class-declaration.
8073
8074 objc-class-declaration:
8075 @class identifier-list ;
8076 */
8077
8078 static void
8079 c_parser_objc_class_declaration (c_parser *parser)
8080 {
8081 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_CLASS));
8082 c_parser_consume_token (parser);
8083 /* Any identifiers, including those declared as type names, are OK
8084 here. */
8085 while (true)
8086 {
8087 tree id;
8088 if (c_parser_next_token_is_not (parser, CPP_NAME))
8089 {
8090 c_parser_error (parser, "expected identifier");
8091 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
8092 parser->error = false;
8093 return;
8094 }
8095 id = c_parser_peek_token (parser)->value;
8096 objc_declare_class (id);
8097 c_parser_consume_token (parser);
8098 if (c_parser_next_token_is (parser, CPP_COMMA))
8099 c_parser_consume_token (parser);
8100 else
8101 break;
8102 }
8103 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8104 }
8105
8106 /* Parse an objc-alias-declaration.
8107
8108 objc-alias-declaration:
8109 @compatibility_alias identifier identifier ;
8110 */
8111
8112 static void
8113 c_parser_objc_alias_declaration (c_parser *parser)
8114 {
8115 tree id1, id2;
8116 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_ALIAS));
8117 c_parser_consume_token (parser);
8118 if (c_parser_next_token_is_not (parser, CPP_NAME))
8119 {
8120 c_parser_error (parser, "expected identifier");
8121 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
8122 return;
8123 }
8124 id1 = c_parser_peek_token (parser)->value;
8125 c_parser_consume_token (parser);
8126 if (c_parser_next_token_is_not (parser, CPP_NAME))
8127 {
8128 c_parser_error (parser, "expected identifier");
8129 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
8130 return;
8131 }
8132 id2 = c_parser_peek_token (parser)->value;
8133 c_parser_consume_token (parser);
8134 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8135 objc_declare_alias (id1, id2);
8136 }
8137
8138 /* Parse an objc-protocol-definition.
8139
8140 objc-protocol-definition:
8141 @protocol identifier objc-protocol-refs[opt] objc-methodprotolist @end
8142 @protocol identifier-list ;
8143
8144 "@protocol identifier ;" should be resolved as "@protocol
8145 identifier-list ;": objc-methodprotolist may not start with a
8146 semicolon in the first alternative if objc-protocol-refs are
8147 omitted. */
8148
8149 static void
8150 c_parser_objc_protocol_definition (c_parser *parser, tree attributes)
8151 {
8152 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_PROTOCOL));
8153
8154 c_parser_consume_token (parser);
8155 if (c_parser_next_token_is_not (parser, CPP_NAME))
8156 {
8157 c_parser_error (parser, "expected identifier");
8158 return;
8159 }
8160 if (c_parser_peek_2nd_token (parser)->type == CPP_COMMA
8161 || c_parser_peek_2nd_token (parser)->type == CPP_SEMICOLON)
8162 {
8163 /* Any identifiers, including those declared as type names, are
8164 OK here. */
8165 while (true)
8166 {
8167 tree id;
8168 if (c_parser_next_token_is_not (parser, CPP_NAME))
8169 {
8170 c_parser_error (parser, "expected identifier");
8171 break;
8172 }
8173 id = c_parser_peek_token (parser)->value;
8174 objc_declare_protocol (id, attributes);
8175 c_parser_consume_token (parser);
8176 if (c_parser_next_token_is (parser, CPP_COMMA))
8177 c_parser_consume_token (parser);
8178 else
8179 break;
8180 }
8181 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8182 }
8183 else
8184 {
8185 tree id = c_parser_peek_token (parser)->value;
8186 tree proto = NULL_TREE;
8187 c_parser_consume_token (parser);
8188 if (c_parser_next_token_is (parser, CPP_LESS))
8189 proto = c_parser_objc_protocol_refs (parser);
8190 parser->objc_pq_context = true;
8191 objc_start_protocol (id, proto, attributes);
8192 c_parser_objc_methodprotolist (parser);
8193 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
8194 parser->objc_pq_context = false;
8195 objc_finish_interface ();
8196 }
8197 }
8198
8199 /* Parse an objc-method-type.
8200
8201 objc-method-type:
8202 +
8203 -
8204
8205 Return true if it is a class method (+) and false if it is
8206 an instance method (-).
8207 */
8208 static inline bool
8209 c_parser_objc_method_type (c_parser *parser)
8210 {
8211 switch (c_parser_peek_token (parser)->type)
8212 {
8213 case CPP_PLUS:
8214 c_parser_consume_token (parser);
8215 return true;
8216 case CPP_MINUS:
8217 c_parser_consume_token (parser);
8218 return false;
8219 default:
8220 gcc_unreachable ();
8221 }
8222 }
8223
8224 /* Parse an objc-method-definition.
8225
8226 objc-method-definition:
8227 objc-method-type objc-method-decl ;[opt] compound-statement
8228 */
8229
8230 static void
8231 c_parser_objc_method_definition (c_parser *parser)
8232 {
8233 bool is_class_method = c_parser_objc_method_type (parser);
8234 tree decl, attributes = NULL_TREE, expr = NULL_TREE;
8235 parser->objc_pq_context = true;
8236 decl = c_parser_objc_method_decl (parser, is_class_method, &attributes,
8237 &expr);
8238 if (decl == error_mark_node)
8239 return; /* Bail here. */
8240
8241 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
8242 {
8243 c_parser_consume_token (parser);
8244 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
8245 "extra semicolon in method definition specified");
8246 }
8247
8248 if (!c_parser_next_token_is (parser, CPP_OPEN_BRACE))
8249 {
8250 c_parser_error (parser, "expected %<{%>");
8251 return;
8252 }
8253
8254 parser->objc_pq_context = false;
8255 if (objc_start_method_definition (is_class_method, decl, attributes, expr))
8256 {
8257 add_stmt (c_parser_compound_statement (parser));
8258 objc_finish_method_definition (current_function_decl);
8259 }
8260 else
8261 {
8262 /* This code is executed when we find a method definition
8263 outside of an @implementation context (or invalid for other
8264 reasons). Parse the method (to keep going) but do not emit
8265 any code.
8266 */
8267 c_parser_compound_statement (parser);
8268 }
8269 }
8270
8271 /* Parse an objc-methodprotolist.
8272
8273 objc-methodprotolist:
8274 empty
8275 objc-methodprotolist objc-methodproto
8276 objc-methodprotolist declaration
8277 objc-methodprotolist ;
8278 @optional
8279 @required
8280
8281 The declaration is a data definition, which may be missing
8282 declaration specifiers under the same rules and diagnostics as
8283 other data definitions outside functions, and the stray semicolon
8284 is diagnosed the same way as a stray semicolon outside a
8285 function. */
8286
8287 static void
8288 c_parser_objc_methodprotolist (c_parser *parser)
8289 {
8290 while (true)
8291 {
8292 /* The list is terminated by @end. */
8293 switch (c_parser_peek_token (parser)->type)
8294 {
8295 case CPP_SEMICOLON:
8296 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
8297 "ISO C does not allow extra %<;%> outside of a function");
8298 c_parser_consume_token (parser);
8299 break;
8300 case CPP_PLUS:
8301 case CPP_MINUS:
8302 c_parser_objc_methodproto (parser);
8303 break;
8304 case CPP_PRAGMA:
8305 c_parser_pragma (parser, pragma_external);
8306 break;
8307 case CPP_EOF:
8308 return;
8309 default:
8310 if (c_parser_next_token_is_keyword (parser, RID_AT_END))
8311 return;
8312 else if (c_parser_next_token_is_keyword (parser, RID_AT_PROPERTY))
8313 c_parser_objc_at_property_declaration (parser);
8314 else if (c_parser_next_token_is_keyword (parser, RID_AT_OPTIONAL))
8315 {
8316 objc_set_method_opt (true);
8317 c_parser_consume_token (parser);
8318 }
8319 else if (c_parser_next_token_is_keyword (parser, RID_AT_REQUIRED))
8320 {
8321 objc_set_method_opt (false);
8322 c_parser_consume_token (parser);
8323 }
8324 else
8325 c_parser_declaration_or_fndef (parser, false, false, true,
8326 false, true, NULL, vNULL);
8327 break;
8328 }
8329 }
8330 }
8331
8332 /* Parse an objc-methodproto.
8333
8334 objc-methodproto:
8335 objc-method-type objc-method-decl ;
8336 */
8337
8338 static void
8339 c_parser_objc_methodproto (c_parser *parser)
8340 {
8341 bool is_class_method = c_parser_objc_method_type (parser);
8342 tree decl, attributes = NULL_TREE;
8343
8344 /* Remember protocol qualifiers in prototypes. */
8345 parser->objc_pq_context = true;
8346 decl = c_parser_objc_method_decl (parser, is_class_method, &attributes,
8347 NULL);
8348 /* Forget protocol qualifiers now. */
8349 parser->objc_pq_context = false;
8350
8351 /* Do not allow the presence of attributes to hide an erroneous
8352 method implementation in the interface section. */
8353 if (!c_parser_next_token_is (parser, CPP_SEMICOLON))
8354 {
8355 c_parser_error (parser, "expected %<;%>");
8356 return;
8357 }
8358
8359 if (decl != error_mark_node)
8360 objc_add_method_declaration (is_class_method, decl, attributes);
8361
8362 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8363 }
8364
8365 /* If we are at a position that method attributes may be present, check that
8366 there are not any parsed already (a syntax error) and then collect any
8367 specified at the current location. Finally, if new attributes were present,
8368 check that the next token is legal ( ';' for decls and '{' for defs). */
8369
8370 static bool
8371 c_parser_objc_maybe_method_attributes (c_parser* parser, tree* attributes)
8372 {
8373 bool bad = false;
8374 if (*attributes)
8375 {
8376 c_parser_error (parser,
8377 "method attributes must be specified at the end only");
8378 *attributes = NULL_TREE;
8379 bad = true;
8380 }
8381
8382 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
8383 *attributes = c_parser_attributes (parser);
8384
8385 /* If there were no attributes here, just report any earlier error. */
8386 if (*attributes == NULL_TREE || bad)
8387 return bad;
8388
8389 /* If the attributes are followed by a ; or {, then just report any earlier
8390 error. */
8391 if (c_parser_next_token_is (parser, CPP_SEMICOLON)
8392 || c_parser_next_token_is (parser, CPP_OPEN_BRACE))
8393 return bad;
8394
8395 /* We've got attributes, but not at the end. */
8396 c_parser_error (parser,
8397 "expected %<;%> or %<{%> after method attribute definition");
8398 return true;
8399 }
8400
8401 /* Parse an objc-method-decl.
8402
8403 objc-method-decl:
8404 ( objc-type-name ) objc-selector
8405 objc-selector
8406 ( objc-type-name ) objc-keyword-selector objc-optparmlist
8407 objc-keyword-selector objc-optparmlist
8408 attributes
8409
8410 objc-keyword-selector:
8411 objc-keyword-decl
8412 objc-keyword-selector objc-keyword-decl
8413
8414 objc-keyword-decl:
8415 objc-selector : ( objc-type-name ) identifier
8416 objc-selector : identifier
8417 : ( objc-type-name ) identifier
8418 : identifier
8419
8420 objc-optparmlist:
8421 objc-optparms objc-optellipsis
8422
8423 objc-optparms:
8424 empty
8425 objc-opt-parms , parameter-declaration
8426
8427 objc-optellipsis:
8428 empty
8429 , ...
8430 */
8431
8432 static tree
8433 c_parser_objc_method_decl (c_parser *parser, bool is_class_method,
8434 tree *attributes, tree *expr)
8435 {
8436 tree type = NULL_TREE;
8437 tree sel;
8438 tree parms = NULL_TREE;
8439 bool ellipsis = false;
8440 bool attr_err = false;
8441
8442 *attributes = NULL_TREE;
8443 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
8444 {
8445 c_parser_consume_token (parser);
8446 type = c_parser_objc_type_name (parser);
8447 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8448 }
8449 sel = c_parser_objc_selector (parser);
8450 /* If there is no selector, or a colon follows, we have an
8451 objc-keyword-selector. If there is a selector, and a colon does
8452 not follow, that selector ends the objc-method-decl. */
8453 if (!sel || c_parser_next_token_is (parser, CPP_COLON))
8454 {
8455 tree tsel = sel;
8456 tree list = NULL_TREE;
8457 while (true)
8458 {
8459 tree atype = NULL_TREE, id, keyworddecl;
8460 tree param_attr = NULL_TREE;
8461 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
8462 break;
8463 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
8464 {
8465 c_parser_consume_token (parser);
8466 atype = c_parser_objc_type_name (parser);
8467 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
8468 "expected %<)%>");
8469 }
8470 /* New ObjC allows attributes on method parameters. */
8471 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
8472 param_attr = c_parser_attributes (parser);
8473 if (c_parser_next_token_is_not (parser, CPP_NAME))
8474 {
8475 c_parser_error (parser, "expected identifier");
8476 return error_mark_node;
8477 }
8478 id = c_parser_peek_token (parser)->value;
8479 c_parser_consume_token (parser);
8480 keyworddecl = objc_build_keyword_decl (tsel, atype, id, param_attr);
8481 list = chainon (list, keyworddecl);
8482 tsel = c_parser_objc_selector (parser);
8483 if (!tsel && c_parser_next_token_is_not (parser, CPP_COLON))
8484 break;
8485 }
8486
8487 attr_err |= c_parser_objc_maybe_method_attributes (parser, attributes) ;
8488
8489 /* Parse the optional parameter list. Optional Objective-C
8490 method parameters follow the C syntax, and may include '...'
8491 to denote a variable number of arguments. */
8492 parms = make_node (TREE_LIST);
8493 while (c_parser_next_token_is (parser, CPP_COMMA))
8494 {
8495 struct c_parm *parm;
8496 c_parser_consume_token (parser);
8497 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
8498 {
8499 ellipsis = true;
8500 c_parser_consume_token (parser);
8501 attr_err |= c_parser_objc_maybe_method_attributes
8502 (parser, attributes) ;
8503 break;
8504 }
8505 parm = c_parser_parameter_declaration (parser, NULL_TREE);
8506 if (parm == NULL)
8507 break;
8508 parms = chainon (parms,
8509 build_tree_list (NULL_TREE, grokparm (parm, expr)));
8510 }
8511 sel = list;
8512 }
8513 else
8514 attr_err |= c_parser_objc_maybe_method_attributes (parser, attributes) ;
8515
8516 if (sel == NULL)
8517 {
8518 c_parser_error (parser, "objective-c method declaration is expected");
8519 return error_mark_node;
8520 }
8521
8522 if (attr_err)
8523 return error_mark_node;
8524
8525 return objc_build_method_signature (is_class_method, type, sel, parms, ellipsis);
8526 }
8527
8528 /* Parse an objc-type-name.
8529
8530 objc-type-name:
8531 objc-type-qualifiers[opt] type-name
8532 objc-type-qualifiers[opt]
8533
8534 objc-type-qualifiers:
8535 objc-type-qualifier
8536 objc-type-qualifiers objc-type-qualifier
8537
8538 objc-type-qualifier: one of
8539 in out inout bycopy byref oneway
8540 */
8541
8542 static tree
8543 c_parser_objc_type_name (c_parser *parser)
8544 {
8545 tree quals = NULL_TREE;
8546 struct c_type_name *type_name = NULL;
8547 tree type = NULL_TREE;
8548 while (true)
8549 {
8550 c_token *token = c_parser_peek_token (parser);
8551 if (token->type == CPP_KEYWORD
8552 && (token->keyword == RID_IN
8553 || token->keyword == RID_OUT
8554 || token->keyword == RID_INOUT
8555 || token->keyword == RID_BYCOPY
8556 || token->keyword == RID_BYREF
8557 || token->keyword == RID_ONEWAY))
8558 {
8559 quals = chainon (build_tree_list (NULL_TREE, token->value), quals);
8560 c_parser_consume_token (parser);
8561 }
8562 else
8563 break;
8564 }
8565 if (c_parser_next_tokens_start_typename (parser, cla_prefer_type))
8566 type_name = c_parser_type_name (parser);
8567 if (type_name)
8568 type = groktypename (type_name, NULL, NULL);
8569
8570 /* If the type is unknown, and error has already been produced and
8571 we need to recover from the error. In that case, use NULL_TREE
8572 for the type, as if no type had been specified; this will use the
8573 default type ('id') which is good for error recovery. */
8574 if (type == error_mark_node)
8575 type = NULL_TREE;
8576
8577 return build_tree_list (quals, type);
8578 }
8579
8580 /* Parse objc-protocol-refs.
8581
8582 objc-protocol-refs:
8583 < identifier-list >
8584 */
8585
8586 static tree
8587 c_parser_objc_protocol_refs (c_parser *parser)
8588 {
8589 tree list = NULL_TREE;
8590 gcc_assert (c_parser_next_token_is (parser, CPP_LESS));
8591 c_parser_consume_token (parser);
8592 /* Any identifiers, including those declared as type names, are OK
8593 here. */
8594 while (true)
8595 {
8596 tree id;
8597 if (c_parser_next_token_is_not (parser, CPP_NAME))
8598 {
8599 c_parser_error (parser, "expected identifier");
8600 break;
8601 }
8602 id = c_parser_peek_token (parser)->value;
8603 list = chainon (list, build_tree_list (NULL_TREE, id));
8604 c_parser_consume_token (parser);
8605 if (c_parser_next_token_is (parser, CPP_COMMA))
8606 c_parser_consume_token (parser);
8607 else
8608 break;
8609 }
8610 c_parser_require (parser, CPP_GREATER, "expected %<>%>");
8611 return list;
8612 }
8613
8614 /* Parse an objc-try-catch-finally-statement.
8615
8616 objc-try-catch-finally-statement:
8617 @try compound-statement objc-catch-list[opt]
8618 @try compound-statement objc-catch-list[opt] @finally compound-statement
8619
8620 objc-catch-list:
8621 @catch ( objc-catch-parameter-declaration ) compound-statement
8622 objc-catch-list @catch ( objc-catch-parameter-declaration ) compound-statement
8623
8624 objc-catch-parameter-declaration:
8625 parameter-declaration
8626 '...'
8627
8628 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
8629
8630 PS: This function is identical to cp_parser_objc_try_catch_finally_statement
8631 for C++. Keep them in sync. */
8632
8633 static void
8634 c_parser_objc_try_catch_finally_statement (c_parser *parser)
8635 {
8636 location_t location;
8637 tree stmt;
8638
8639 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_TRY));
8640 c_parser_consume_token (parser);
8641 location = c_parser_peek_token (parser)->location;
8642 objc_maybe_warn_exceptions (location);
8643 stmt = c_parser_compound_statement (parser);
8644 objc_begin_try_stmt (location, stmt);
8645
8646 while (c_parser_next_token_is_keyword (parser, RID_AT_CATCH))
8647 {
8648 struct c_parm *parm;
8649 tree parameter_declaration = error_mark_node;
8650 bool seen_open_paren = false;
8651
8652 c_parser_consume_token (parser);
8653 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
8654 seen_open_paren = true;
8655 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
8656 {
8657 /* We have "@catch (...)" (where the '...' are literally
8658 what is in the code). Skip the '...'.
8659 parameter_declaration is set to NULL_TREE, and
8660 objc_being_catch_clauses() knows that that means
8661 '...'. */
8662 c_parser_consume_token (parser);
8663 parameter_declaration = NULL_TREE;
8664 }
8665 else
8666 {
8667 /* We have "@catch (NSException *exception)" or something
8668 like that. Parse the parameter declaration. */
8669 parm = c_parser_parameter_declaration (parser, NULL_TREE);
8670 if (parm == NULL)
8671 parameter_declaration = error_mark_node;
8672 else
8673 parameter_declaration = grokparm (parm, NULL);
8674 }
8675 if (seen_open_paren)
8676 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8677 else
8678 {
8679 /* If there was no open parenthesis, we are recovering from
8680 an error, and we are trying to figure out what mistake
8681 the user has made. */
8682
8683 /* If there is an immediate closing parenthesis, the user
8684 probably forgot the opening one (ie, they typed "@catch
8685 NSException *e)". Parse the closing parenthesis and keep
8686 going. */
8687 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
8688 c_parser_consume_token (parser);
8689
8690 /* If these is no immediate closing parenthesis, the user
8691 probably doesn't know that parenthesis are required at
8692 all (ie, they typed "@catch NSException *e"). So, just
8693 forget about the closing parenthesis and keep going. */
8694 }
8695 objc_begin_catch_clause (parameter_declaration);
8696 if (c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
8697 c_parser_compound_statement_nostart (parser);
8698 objc_finish_catch_clause ();
8699 }
8700 if (c_parser_next_token_is_keyword (parser, RID_AT_FINALLY))
8701 {
8702 c_parser_consume_token (parser);
8703 location = c_parser_peek_token (parser)->location;
8704 stmt = c_parser_compound_statement (parser);
8705 objc_build_finally_clause (location, stmt);
8706 }
8707 objc_finish_try_stmt ();
8708 }
8709
8710 /* Parse an objc-synchronized-statement.
8711
8712 objc-synchronized-statement:
8713 @synchronized ( expression ) compound-statement
8714 */
8715
8716 static void
8717 c_parser_objc_synchronized_statement (c_parser *parser)
8718 {
8719 location_t loc;
8720 tree expr, stmt;
8721 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_SYNCHRONIZED));
8722 c_parser_consume_token (parser);
8723 loc = c_parser_peek_token (parser)->location;
8724 objc_maybe_warn_exceptions (loc);
8725 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
8726 {
8727 struct c_expr ce = c_parser_expression (parser);
8728 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
8729 expr = ce.value;
8730 expr = c_fully_fold (expr, false, NULL);
8731 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8732 }
8733 else
8734 expr = error_mark_node;
8735 stmt = c_parser_compound_statement (parser);
8736 objc_build_synchronized (loc, expr, stmt);
8737 }
8738
8739 /* Parse an objc-selector; return NULL_TREE without an error if the
8740 next token is not an objc-selector.
8741
8742 objc-selector:
8743 identifier
8744 one of
8745 enum struct union if else while do for switch case default
8746 break continue return goto asm sizeof typeof __alignof
8747 unsigned long const short volatile signed restrict _Complex
8748 in out inout bycopy byref oneway int char float double void _Bool
8749 _Atomic
8750
8751 ??? Why this selection of keywords but not, for example, storage
8752 class specifiers? */
8753
8754 static tree
8755 c_parser_objc_selector (c_parser *parser)
8756 {
8757 c_token *token = c_parser_peek_token (parser);
8758 tree value = token->value;
8759 if (token->type == CPP_NAME)
8760 {
8761 c_parser_consume_token (parser);
8762 return value;
8763 }
8764 if (token->type != CPP_KEYWORD)
8765 return NULL_TREE;
8766 switch (token->keyword)
8767 {
8768 case RID_ENUM:
8769 case RID_STRUCT:
8770 case RID_UNION:
8771 case RID_IF:
8772 case RID_ELSE:
8773 case RID_WHILE:
8774 case RID_DO:
8775 case RID_FOR:
8776 case RID_SWITCH:
8777 case RID_CASE:
8778 case RID_DEFAULT:
8779 case RID_BREAK:
8780 case RID_CONTINUE:
8781 case RID_RETURN:
8782 case RID_GOTO:
8783 case RID_ASM:
8784 case RID_SIZEOF:
8785 case RID_TYPEOF:
8786 case RID_ALIGNOF:
8787 case RID_UNSIGNED:
8788 case RID_LONG:
8789 case RID_INT128:
8790 case RID_CONST:
8791 case RID_SHORT:
8792 case RID_VOLATILE:
8793 case RID_SIGNED:
8794 case RID_RESTRICT:
8795 case RID_COMPLEX:
8796 case RID_IN:
8797 case RID_OUT:
8798 case RID_INOUT:
8799 case RID_BYCOPY:
8800 case RID_BYREF:
8801 case RID_ONEWAY:
8802 case RID_INT:
8803 case RID_CHAR:
8804 case RID_FLOAT:
8805 case RID_DOUBLE:
8806 case RID_VOID:
8807 case RID_BOOL:
8808 case RID_ATOMIC:
8809 case RID_AUTO_TYPE:
8810 c_parser_consume_token (parser);
8811 return value;
8812 default:
8813 return NULL_TREE;
8814 }
8815 }
8816
8817 /* Parse an objc-selector-arg.
8818
8819 objc-selector-arg:
8820 objc-selector
8821 objc-keywordname-list
8822
8823 objc-keywordname-list:
8824 objc-keywordname
8825 objc-keywordname-list objc-keywordname
8826
8827 objc-keywordname:
8828 objc-selector :
8829 :
8830 */
8831
8832 static tree
8833 c_parser_objc_selector_arg (c_parser *parser)
8834 {
8835 tree sel = c_parser_objc_selector (parser);
8836 tree list = NULL_TREE;
8837 if (sel && c_parser_next_token_is_not (parser, CPP_COLON))
8838 return sel;
8839 while (true)
8840 {
8841 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
8842 return list;
8843 list = chainon (list, build_tree_list (sel, NULL_TREE));
8844 sel = c_parser_objc_selector (parser);
8845 if (!sel && c_parser_next_token_is_not (parser, CPP_COLON))
8846 break;
8847 }
8848 return list;
8849 }
8850
8851 /* Parse an objc-receiver.
8852
8853 objc-receiver:
8854 expression
8855 class-name
8856 type-name
8857 */
8858
8859 static tree
8860 c_parser_objc_receiver (c_parser *parser)
8861 {
8862 location_t loc = c_parser_peek_token (parser)->location;
8863
8864 if (c_parser_peek_token (parser)->type == CPP_NAME
8865 && (c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME
8866 || c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME))
8867 {
8868 tree id = c_parser_peek_token (parser)->value;
8869 c_parser_consume_token (parser);
8870 return objc_get_class_reference (id);
8871 }
8872 struct c_expr ce = c_parser_expression (parser);
8873 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
8874 return c_fully_fold (ce.value, false, NULL);
8875 }
8876
8877 /* Parse objc-message-args.
8878
8879 objc-message-args:
8880 objc-selector
8881 objc-keywordarg-list
8882
8883 objc-keywordarg-list:
8884 objc-keywordarg
8885 objc-keywordarg-list objc-keywordarg
8886
8887 objc-keywordarg:
8888 objc-selector : objc-keywordexpr
8889 : objc-keywordexpr
8890 */
8891
8892 static tree
8893 c_parser_objc_message_args (c_parser *parser)
8894 {
8895 tree sel = c_parser_objc_selector (parser);
8896 tree list = NULL_TREE;
8897 if (sel && c_parser_next_token_is_not (parser, CPP_COLON))
8898 return sel;
8899 while (true)
8900 {
8901 tree keywordexpr;
8902 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
8903 return error_mark_node;
8904 keywordexpr = c_parser_objc_keywordexpr (parser);
8905 list = chainon (list, build_tree_list (sel, keywordexpr));
8906 sel = c_parser_objc_selector (parser);
8907 if (!sel && c_parser_next_token_is_not (parser, CPP_COLON))
8908 break;
8909 }
8910 return list;
8911 }
8912
8913 /* Parse an objc-keywordexpr.
8914
8915 objc-keywordexpr:
8916 nonempty-expr-list
8917 */
8918
8919 static tree
8920 c_parser_objc_keywordexpr (c_parser *parser)
8921 {
8922 tree ret;
8923 vec<tree, va_gc> *expr_list = c_parser_expr_list (parser, true, true,
8924 NULL, NULL, NULL);
8925 if (vec_safe_length (expr_list) == 1)
8926 {
8927 /* Just return the expression, remove a level of
8928 indirection. */
8929 ret = (*expr_list)[0];
8930 }
8931 else
8932 {
8933 /* We have a comma expression, we will collapse later. */
8934 ret = build_tree_list_vec (expr_list);
8935 }
8936 release_tree_vector (expr_list);
8937 return ret;
8938 }
8939
8940 /* A check, needed in several places, that ObjC interface, implementation or
8941 method definitions are not prefixed by incorrect items. */
8942 static bool
8943 c_parser_objc_diagnose_bad_element_prefix (c_parser *parser,
8944 struct c_declspecs *specs)
8945 {
8946 if (!specs->declspecs_seen_p || specs->non_sc_seen_p
8947 || specs->typespec_kind != ctsk_none)
8948 {
8949 c_parser_error (parser,
8950 "no type or storage class may be specified here,");
8951 c_parser_skip_to_end_of_block_or_statement (parser);
8952 return true;
8953 }
8954 return false;
8955 }
8956
8957 /* Parse an Objective-C @property declaration. The syntax is:
8958
8959 objc-property-declaration:
8960 '@property' objc-property-attributes[opt] struct-declaration ;
8961
8962 objc-property-attributes:
8963 '(' objc-property-attribute-list ')'
8964
8965 objc-property-attribute-list:
8966 objc-property-attribute
8967 objc-property-attribute-list, objc-property-attribute
8968
8969 objc-property-attribute
8970 'getter' = identifier
8971 'setter' = identifier
8972 'readonly'
8973 'readwrite'
8974 'assign'
8975 'retain'
8976 'copy'
8977 'nonatomic'
8978
8979 For example:
8980 @property NSString *name;
8981 @property (readonly) id object;
8982 @property (retain, nonatomic, getter=getTheName) id name;
8983 @property int a, b, c;
8984
8985 PS: This function is identical to cp_parser_objc_at_propery_declaration
8986 for C++. Keep them in sync. */
8987 static void
8988 c_parser_objc_at_property_declaration (c_parser *parser)
8989 {
8990 /* The following variables hold the attributes of the properties as
8991 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
8992 seen. When we see an attribute, we set them to 'true' (if they
8993 are boolean properties) or to the identifier (if they have an
8994 argument, ie, for getter and setter). Note that here we only
8995 parse the list of attributes, check the syntax and accumulate the
8996 attributes that we find. objc_add_property_declaration() will
8997 then process the information. */
8998 bool property_assign = false;
8999 bool property_copy = false;
9000 tree property_getter_ident = NULL_TREE;
9001 bool property_nonatomic = false;
9002 bool property_readonly = false;
9003 bool property_readwrite = false;
9004 bool property_retain = false;
9005 tree property_setter_ident = NULL_TREE;
9006
9007 /* 'properties' is the list of properties that we read. Usually a
9008 single one, but maybe more (eg, in "@property int a, b, c;" there
9009 are three). */
9010 tree properties;
9011 location_t loc;
9012
9013 loc = c_parser_peek_token (parser)->location;
9014 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_PROPERTY));
9015
9016 c_parser_consume_token (parser); /* Eat '@property'. */
9017
9018 /* Parse the optional attribute list... */
9019 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
9020 {
9021 /* Eat the '(' */
9022 c_parser_consume_token (parser);
9023
9024 /* Property attribute keywords are valid now. */
9025 parser->objc_property_attr_context = true;
9026
9027 while (true)
9028 {
9029 bool syntax_error = false;
9030 c_token *token = c_parser_peek_token (parser);
9031 enum rid keyword;
9032
9033 if (token->type != CPP_KEYWORD)
9034 {
9035 if (token->type == CPP_CLOSE_PAREN)
9036 c_parser_error (parser, "expected identifier");
9037 else
9038 {
9039 c_parser_consume_token (parser);
9040 c_parser_error (parser, "unknown property attribute");
9041 }
9042 break;
9043 }
9044 keyword = token->keyword;
9045 c_parser_consume_token (parser);
9046 switch (keyword)
9047 {
9048 case RID_ASSIGN: property_assign = true; break;
9049 case RID_COPY: property_copy = true; break;
9050 case RID_NONATOMIC: property_nonatomic = true; break;
9051 case RID_READONLY: property_readonly = true; break;
9052 case RID_READWRITE: property_readwrite = true; break;
9053 case RID_RETAIN: property_retain = true; break;
9054
9055 case RID_GETTER:
9056 case RID_SETTER:
9057 if (c_parser_next_token_is_not (parser, CPP_EQ))
9058 {
9059 if (keyword == RID_GETTER)
9060 c_parser_error (parser,
9061 "missing %<=%> (after %<getter%> attribute)");
9062 else
9063 c_parser_error (parser,
9064 "missing %<=%> (after %<setter%> attribute)");
9065 syntax_error = true;
9066 break;
9067 }
9068 c_parser_consume_token (parser); /* eat the = */
9069 if (c_parser_next_token_is_not (parser, CPP_NAME))
9070 {
9071 c_parser_error (parser, "expected identifier");
9072 syntax_error = true;
9073 break;
9074 }
9075 if (keyword == RID_SETTER)
9076 {
9077 if (property_setter_ident != NULL_TREE)
9078 c_parser_error (parser, "the %<setter%> attribute may only be specified once");
9079 else
9080 property_setter_ident = c_parser_peek_token (parser)->value;
9081 c_parser_consume_token (parser);
9082 if (c_parser_next_token_is_not (parser, CPP_COLON))
9083 c_parser_error (parser, "setter name must terminate with %<:%>");
9084 else
9085 c_parser_consume_token (parser);
9086 }
9087 else
9088 {
9089 if (property_getter_ident != NULL_TREE)
9090 c_parser_error (parser, "the %<getter%> attribute may only be specified once");
9091 else
9092 property_getter_ident = c_parser_peek_token (parser)->value;
9093 c_parser_consume_token (parser);
9094 }
9095 break;
9096 default:
9097 c_parser_error (parser, "unknown property attribute");
9098 syntax_error = true;
9099 break;
9100 }
9101
9102 if (syntax_error)
9103 break;
9104
9105 if (c_parser_next_token_is (parser, CPP_COMMA))
9106 c_parser_consume_token (parser);
9107 else
9108 break;
9109 }
9110 parser->objc_property_attr_context = false;
9111 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
9112 }
9113 /* ... and the property declaration(s). */
9114 properties = c_parser_struct_declaration (parser);
9115
9116 if (properties == error_mark_node)
9117 {
9118 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
9119 parser->error = false;
9120 return;
9121 }
9122
9123 if (properties == NULL_TREE)
9124 c_parser_error (parser, "expected identifier");
9125 else
9126 {
9127 /* Comma-separated properties are chained together in
9128 reverse order; add them one by one. */
9129 properties = nreverse (properties);
9130
9131 for (; properties; properties = TREE_CHAIN (properties))
9132 objc_add_property_declaration (loc, copy_node (properties),
9133 property_readonly, property_readwrite,
9134 property_assign, property_retain,
9135 property_copy, property_nonatomic,
9136 property_getter_ident, property_setter_ident);
9137 }
9138
9139 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9140 parser->error = false;
9141 }
9142
9143 /* Parse an Objective-C @synthesize declaration. The syntax is:
9144
9145 objc-synthesize-declaration:
9146 @synthesize objc-synthesize-identifier-list ;
9147
9148 objc-synthesize-identifier-list:
9149 objc-synthesize-identifier
9150 objc-synthesize-identifier-list, objc-synthesize-identifier
9151
9152 objc-synthesize-identifier
9153 identifier
9154 identifier = identifier
9155
9156 For example:
9157 @synthesize MyProperty;
9158 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
9159
9160 PS: This function is identical to cp_parser_objc_at_synthesize_declaration
9161 for C++. Keep them in sync.
9162 */
9163 static void
9164 c_parser_objc_at_synthesize_declaration (c_parser *parser)
9165 {
9166 tree list = NULL_TREE;
9167 location_t loc;
9168 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_SYNTHESIZE));
9169 loc = c_parser_peek_token (parser)->location;
9170
9171 c_parser_consume_token (parser);
9172 while (true)
9173 {
9174 tree property, ivar;
9175 if (c_parser_next_token_is_not (parser, CPP_NAME))
9176 {
9177 c_parser_error (parser, "expected identifier");
9178 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
9179 /* Once we find the semicolon, we can resume normal parsing.
9180 We have to reset parser->error manually because
9181 c_parser_skip_until_found() won't reset it for us if the
9182 next token is precisely a semicolon. */
9183 parser->error = false;
9184 return;
9185 }
9186 property = c_parser_peek_token (parser)->value;
9187 c_parser_consume_token (parser);
9188 if (c_parser_next_token_is (parser, CPP_EQ))
9189 {
9190 c_parser_consume_token (parser);
9191 if (c_parser_next_token_is_not (parser, CPP_NAME))
9192 {
9193 c_parser_error (parser, "expected identifier");
9194 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
9195 parser->error = false;
9196 return;
9197 }
9198 ivar = c_parser_peek_token (parser)->value;
9199 c_parser_consume_token (parser);
9200 }
9201 else
9202 ivar = NULL_TREE;
9203 list = chainon (list, build_tree_list (ivar, property));
9204 if (c_parser_next_token_is (parser, CPP_COMMA))
9205 c_parser_consume_token (parser);
9206 else
9207 break;
9208 }
9209 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9210 objc_add_synthesize_declaration (loc, list);
9211 }
9212
9213 /* Parse an Objective-C @dynamic declaration. The syntax is:
9214
9215 objc-dynamic-declaration:
9216 @dynamic identifier-list ;
9217
9218 For example:
9219 @dynamic MyProperty;
9220 @dynamic MyProperty, AnotherProperty;
9221
9222 PS: This function is identical to cp_parser_objc_at_dynamic_declaration
9223 for C++. Keep them in sync.
9224 */
9225 static void
9226 c_parser_objc_at_dynamic_declaration (c_parser *parser)
9227 {
9228 tree list = NULL_TREE;
9229 location_t loc;
9230 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_DYNAMIC));
9231 loc = c_parser_peek_token (parser)->location;
9232
9233 c_parser_consume_token (parser);
9234 while (true)
9235 {
9236 tree property;
9237 if (c_parser_next_token_is_not (parser, CPP_NAME))
9238 {
9239 c_parser_error (parser, "expected identifier");
9240 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
9241 parser->error = false;
9242 return;
9243 }
9244 property = c_parser_peek_token (parser)->value;
9245 list = chainon (list, build_tree_list (NULL_TREE, property));
9246 c_parser_consume_token (parser);
9247 if (c_parser_next_token_is (parser, CPP_COMMA))
9248 c_parser_consume_token (parser);
9249 else
9250 break;
9251 }
9252 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9253 objc_add_dynamic_declaration (loc, list);
9254 }
9255
9256 \f
9257 /* Handle pragmas. Some OpenMP pragmas are associated with, and therefore
9258 should be considered, statements. ALLOW_STMT is true if we're within
9259 the context of a function and such pragmas are to be allowed. Returns
9260 true if we actually parsed such a pragma. */
9261
9262 static bool
9263 c_parser_pragma (c_parser *parser, enum pragma_context context)
9264 {
9265 unsigned int id;
9266
9267 id = c_parser_peek_token (parser)->pragma_kind;
9268 gcc_assert (id != PRAGMA_NONE);
9269
9270 switch (id)
9271 {
9272 case PRAGMA_OMP_BARRIER:
9273 if (context != pragma_compound)
9274 {
9275 if (context == pragma_stmt)
9276 c_parser_error (parser, "%<#pragma omp barrier%> may only be "
9277 "used in compound statements");
9278 goto bad_stmt;
9279 }
9280 c_parser_omp_barrier (parser);
9281 return false;
9282
9283 case PRAGMA_OMP_FLUSH:
9284 if (context != pragma_compound)
9285 {
9286 if (context == pragma_stmt)
9287 c_parser_error (parser, "%<#pragma omp flush%> may only be "
9288 "used in compound statements");
9289 goto bad_stmt;
9290 }
9291 c_parser_omp_flush (parser);
9292 return false;
9293
9294 case PRAGMA_OMP_TASKWAIT:
9295 if (context != pragma_compound)
9296 {
9297 if (context == pragma_stmt)
9298 c_parser_error (parser, "%<#pragma omp taskwait%> may only be "
9299 "used in compound statements");
9300 goto bad_stmt;
9301 }
9302 c_parser_omp_taskwait (parser);
9303 return false;
9304
9305 case PRAGMA_OMP_TASKYIELD:
9306 if (context != pragma_compound)
9307 {
9308 if (context == pragma_stmt)
9309 c_parser_error (parser, "%<#pragma omp taskyield%> may only be "
9310 "used in compound statements");
9311 goto bad_stmt;
9312 }
9313 c_parser_omp_taskyield (parser);
9314 return false;
9315
9316 case PRAGMA_OMP_CANCEL:
9317 if (context != pragma_compound)
9318 {
9319 if (context == pragma_stmt)
9320 c_parser_error (parser, "%<#pragma omp cancel%> may only be "
9321 "used in compound statements");
9322 goto bad_stmt;
9323 }
9324 c_parser_omp_cancel (parser);
9325 return false;
9326
9327 case PRAGMA_OMP_CANCELLATION_POINT:
9328 if (context != pragma_compound)
9329 {
9330 if (context == pragma_stmt)
9331 c_parser_error (parser, "%<#pragma omp cancellation point%> may "
9332 "only be used in compound statements");
9333 goto bad_stmt;
9334 }
9335 c_parser_omp_cancellation_point (parser);
9336 return false;
9337
9338 case PRAGMA_OMP_THREADPRIVATE:
9339 c_parser_omp_threadprivate (parser);
9340 return false;
9341
9342 case PRAGMA_OMP_TARGET:
9343 return c_parser_omp_target (parser, context);
9344
9345 case PRAGMA_OMP_END_DECLARE_TARGET:
9346 c_parser_omp_end_declare_target (parser);
9347 return false;
9348
9349 case PRAGMA_OMP_SECTION:
9350 error_at (c_parser_peek_token (parser)->location,
9351 "%<#pragma omp section%> may only be used in "
9352 "%<#pragma omp sections%> construct");
9353 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
9354 return false;
9355
9356 case PRAGMA_OMP_DECLARE_REDUCTION:
9357 c_parser_omp_declare (parser, context);
9358 return false;
9359 case PRAGMA_IVDEP:
9360 c_parser_consume_pragma (parser);
9361 c_parser_skip_to_pragma_eol (parser);
9362 if (!c_parser_next_token_is_keyword (parser, RID_FOR)
9363 && !c_parser_next_token_is_keyword (parser, RID_WHILE)
9364 && !c_parser_next_token_is_keyword (parser, RID_DO))
9365 {
9366 c_parser_error (parser, "for, while or do statement expected");
9367 return false;
9368 }
9369 if (c_parser_next_token_is_keyword (parser, RID_FOR))
9370 c_parser_for_statement (parser, true);
9371 else if (c_parser_next_token_is_keyword (parser, RID_WHILE))
9372 c_parser_while_statement (parser, true);
9373 else
9374 c_parser_do_statement (parser, true);
9375 return false;
9376
9377 case PRAGMA_GCC_PCH_PREPROCESS:
9378 c_parser_error (parser, "%<#pragma GCC pch_preprocess%> must be first");
9379 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
9380 return false;
9381
9382 case PRAGMA_CILK_SIMD:
9383 if (!c_parser_cilk_verify_simd (parser, context))
9384 return false;
9385 c_parser_consume_pragma (parser);
9386 c_parser_cilk_simd (parser);
9387 return false;
9388
9389 default:
9390 if (id < PRAGMA_FIRST_EXTERNAL)
9391 {
9392 if (context != pragma_stmt && context != pragma_compound)
9393 {
9394 bad_stmt:
9395 c_parser_error (parser, "expected declaration specifiers");
9396 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
9397 return false;
9398 }
9399 c_parser_omp_construct (parser);
9400 return true;
9401 }
9402 break;
9403 }
9404
9405 c_parser_consume_pragma (parser);
9406 c_invoke_pragma_handler (id);
9407
9408 /* Skip to EOL, but suppress any error message. Those will have been
9409 generated by the handler routine through calling error, as opposed
9410 to calling c_parser_error. */
9411 parser->error = true;
9412 c_parser_skip_to_pragma_eol (parser);
9413
9414 return false;
9415 }
9416
9417 /* The interface the pragma parsers have to the lexer. */
9418
9419 enum cpp_ttype
9420 pragma_lex (tree *value)
9421 {
9422 c_token *tok = c_parser_peek_token (the_parser);
9423 enum cpp_ttype ret = tok->type;
9424
9425 *value = tok->value;
9426 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
9427 ret = CPP_EOF;
9428 else
9429 {
9430 if (ret == CPP_KEYWORD)
9431 ret = CPP_NAME;
9432 c_parser_consume_token (the_parser);
9433 }
9434
9435 return ret;
9436 }
9437
9438 static void
9439 c_parser_pragma_pch_preprocess (c_parser *parser)
9440 {
9441 tree name = NULL;
9442
9443 c_parser_consume_pragma (parser);
9444 if (c_parser_next_token_is (parser, CPP_STRING))
9445 {
9446 name = c_parser_peek_token (parser)->value;
9447 c_parser_consume_token (parser);
9448 }
9449 else
9450 c_parser_error (parser, "expected string literal");
9451 c_parser_skip_to_pragma_eol (parser);
9452
9453 if (name)
9454 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
9455 }
9456 \f
9457 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 parsing routines. */
9458
9459 /* Returns name of the next clause.
9460 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
9461 the token is not consumed. Otherwise appropriate pragma_omp_clause is
9462 returned and the token is consumed. */
9463
9464 static pragma_omp_clause
9465 c_parser_omp_clause_name (c_parser *parser)
9466 {
9467 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
9468
9469 if (c_parser_next_token_is_keyword (parser, RID_IF))
9470 result = PRAGMA_OMP_CLAUSE_IF;
9471 else if (c_parser_next_token_is_keyword (parser, RID_DEFAULT))
9472 result = PRAGMA_OMP_CLAUSE_DEFAULT;
9473 else if (c_parser_next_token_is_keyword (parser, RID_FOR))
9474 result = PRAGMA_OMP_CLAUSE_FOR;
9475 else if (c_parser_next_token_is (parser, CPP_NAME))
9476 {
9477 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
9478
9479 switch (p[0])
9480 {
9481 case 'a':
9482 if (!strcmp ("aligned", p))
9483 result = PRAGMA_OMP_CLAUSE_ALIGNED;
9484 break;
9485 case 'c':
9486 if (!strcmp ("collapse", p))
9487 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
9488 else if (!strcmp ("copyin", p))
9489 result = PRAGMA_OMP_CLAUSE_COPYIN;
9490 else if (!strcmp ("copyprivate", p))
9491 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
9492 break;
9493 case 'd':
9494 if (!strcmp ("depend", p))
9495 result = PRAGMA_OMP_CLAUSE_DEPEND;
9496 else if (!strcmp ("device", p))
9497 result = PRAGMA_OMP_CLAUSE_DEVICE;
9498 else if (!strcmp ("dist_schedule", p))
9499 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
9500 break;
9501 case 'f':
9502 if (!strcmp ("final", p))
9503 result = PRAGMA_OMP_CLAUSE_FINAL;
9504 else if (!strcmp ("firstprivate", p))
9505 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
9506 else if (!strcmp ("from", p))
9507 result = PRAGMA_OMP_CLAUSE_FROM;
9508 break;
9509 case 'i':
9510 if (!strcmp ("inbranch", p))
9511 result = PRAGMA_OMP_CLAUSE_INBRANCH;
9512 break;
9513 case 'l':
9514 if (!strcmp ("lastprivate", p))
9515 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
9516 else if (!strcmp ("linear", p))
9517 result = PRAGMA_OMP_CLAUSE_LINEAR;
9518 break;
9519 case 'm':
9520 if (!strcmp ("map", p))
9521 result = PRAGMA_OMP_CLAUSE_MAP;
9522 else if (!strcmp ("mergeable", p))
9523 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
9524 break;
9525 case 'n':
9526 if (!strcmp ("notinbranch", p))
9527 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
9528 else if (!strcmp ("nowait", p))
9529 result = PRAGMA_OMP_CLAUSE_NOWAIT;
9530 else if (!strcmp ("num_teams", p))
9531 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
9532 else if (!strcmp ("num_threads", p))
9533 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
9534 break;
9535 case 'o':
9536 if (!strcmp ("ordered", p))
9537 result = PRAGMA_OMP_CLAUSE_ORDERED;
9538 break;
9539 case 'p':
9540 if (!strcmp ("parallel", p))
9541 result = PRAGMA_OMP_CLAUSE_PARALLEL;
9542 else if (!strcmp ("private", p))
9543 result = PRAGMA_OMP_CLAUSE_PRIVATE;
9544 else if (!strcmp ("proc_bind", p))
9545 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
9546 break;
9547 case 'r':
9548 if (!strcmp ("reduction", p))
9549 result = PRAGMA_OMP_CLAUSE_REDUCTION;
9550 break;
9551 case 's':
9552 if (!strcmp ("safelen", p))
9553 result = PRAGMA_OMP_CLAUSE_SAFELEN;
9554 else if (!strcmp ("schedule", p))
9555 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
9556 else if (!strcmp ("sections", p))
9557 result = PRAGMA_OMP_CLAUSE_SECTIONS;
9558 else if (!strcmp ("shared", p))
9559 result = PRAGMA_OMP_CLAUSE_SHARED;
9560 else if (!strcmp ("simdlen", p))
9561 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
9562 break;
9563 case 't':
9564 if (!strcmp ("taskgroup", p))
9565 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
9566 else if (!strcmp ("thread_limit", p))
9567 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
9568 else if (!strcmp ("to", p))
9569 result = PRAGMA_OMP_CLAUSE_TO;
9570 break;
9571 case 'u':
9572 if (!strcmp ("uniform", p))
9573 result = PRAGMA_OMP_CLAUSE_UNIFORM;
9574 else if (!strcmp ("untied", p))
9575 result = PRAGMA_OMP_CLAUSE_UNTIED;
9576 break;
9577 }
9578 }
9579
9580 if (result != PRAGMA_OMP_CLAUSE_NONE)
9581 c_parser_consume_token (parser);
9582
9583 return result;
9584 }
9585
9586 /* Validate that a clause of the given type does not already exist. */
9587
9588 static void
9589 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
9590 const char *name)
9591 {
9592 tree c;
9593
9594 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
9595 if (OMP_CLAUSE_CODE (c) == code)
9596 {
9597 location_t loc = OMP_CLAUSE_LOCATION (c);
9598 error_at (loc, "too many %qs clauses", name);
9599 break;
9600 }
9601 }
9602
9603 /* OpenMP 2.5:
9604 variable-list:
9605 identifier
9606 variable-list , identifier
9607
9608 If KIND is nonzero, create the appropriate node and install the
9609 decl in OMP_CLAUSE_DECL and add the node to the head of the list.
9610 If KIND is nonzero, CLAUSE_LOC is the location of the clause.
9611
9612 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
9613 return the list created. */
9614
9615 static tree
9616 c_parser_omp_variable_list (c_parser *parser,
9617 location_t clause_loc,
9618 enum omp_clause_code kind, tree list)
9619 {
9620 if (c_parser_next_token_is_not (parser, CPP_NAME)
9621 || c_parser_peek_token (parser)->id_kind != C_ID_ID)
9622 c_parser_error (parser, "expected identifier");
9623
9624 while (c_parser_next_token_is (parser, CPP_NAME)
9625 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
9626 {
9627 tree t = lookup_name (c_parser_peek_token (parser)->value);
9628
9629 if (t == NULL_TREE)
9630 {
9631 undeclared_variable (c_parser_peek_token (parser)->location,
9632 c_parser_peek_token (parser)->value);
9633 t = error_mark_node;
9634 }
9635
9636 c_parser_consume_token (parser);
9637
9638 if (t == error_mark_node)
9639 ;
9640 else if (kind != 0)
9641 {
9642 switch (kind)
9643 {
9644 case OMP_CLAUSE_MAP:
9645 case OMP_CLAUSE_FROM:
9646 case OMP_CLAUSE_TO:
9647 case OMP_CLAUSE_DEPEND:
9648 while (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
9649 {
9650 tree low_bound = NULL_TREE, length = NULL_TREE;
9651
9652 c_parser_consume_token (parser);
9653 if (!c_parser_next_token_is (parser, CPP_COLON))
9654 low_bound = c_parser_expression (parser).value;
9655 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
9656 length = integer_one_node;
9657 else
9658 {
9659 /* Look for `:'. */
9660 if (!c_parser_require (parser, CPP_COLON,
9661 "expected %<:%>"))
9662 {
9663 t = error_mark_node;
9664 break;
9665 }
9666 if (!c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
9667 length = c_parser_expression (parser).value;
9668 }
9669 /* Look for the closing `]'. */
9670 if (!c_parser_require (parser, CPP_CLOSE_SQUARE,
9671 "expected %<]%>"))
9672 {
9673 t = error_mark_node;
9674 break;
9675 }
9676 t = tree_cons (low_bound, length, t);
9677 }
9678 break;
9679 default:
9680 break;
9681 }
9682
9683 if (t != error_mark_node)
9684 {
9685 tree u = build_omp_clause (clause_loc, kind);
9686 OMP_CLAUSE_DECL (u) = t;
9687 OMP_CLAUSE_CHAIN (u) = list;
9688 list = u;
9689 }
9690 }
9691 else
9692 list = tree_cons (t, NULL_TREE, list);
9693
9694 if (c_parser_next_token_is_not (parser, CPP_COMMA))
9695 break;
9696
9697 c_parser_consume_token (parser);
9698 }
9699
9700 return list;
9701 }
9702
9703 /* Similarly, but expect leading and trailing parenthesis. This is a very
9704 common case for omp clauses. */
9705
9706 static tree
9707 c_parser_omp_var_list_parens (c_parser *parser, enum omp_clause_code kind,
9708 tree list)
9709 {
9710 /* The clauses location. */
9711 location_t loc = c_parser_peek_token (parser)->location;
9712
9713 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
9714 {
9715 list = c_parser_omp_variable_list (parser, loc, kind, list);
9716 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
9717 }
9718 return list;
9719 }
9720
9721 /* OpenMP 3.0:
9722 collapse ( constant-expression ) */
9723
9724 static tree
9725 c_parser_omp_clause_collapse (c_parser *parser, tree list)
9726 {
9727 tree c, num = error_mark_node;
9728 HOST_WIDE_INT n;
9729 location_t loc;
9730
9731 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse");
9732
9733 loc = c_parser_peek_token (parser)->location;
9734 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
9735 {
9736 num = c_parser_expr_no_commas (parser, NULL).value;
9737 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
9738 }
9739 if (num == error_mark_node)
9740 return list;
9741 mark_exp_read (num);
9742 num = c_fully_fold (num, false, NULL);
9743 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
9744 || !tree_fits_shwi_p (num)
9745 || (n = tree_to_shwi (num)) <= 0
9746 || (int) n != n)
9747 {
9748 error_at (loc,
9749 "collapse argument needs positive constant integer expression");
9750 return list;
9751 }
9752 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
9753 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
9754 OMP_CLAUSE_CHAIN (c) = list;
9755 return c;
9756 }
9757
9758 /* OpenMP 2.5:
9759 copyin ( variable-list ) */
9760
9761 static tree
9762 c_parser_omp_clause_copyin (c_parser *parser, tree list)
9763 {
9764 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_COPYIN, list);
9765 }
9766
9767 /* OpenMP 2.5:
9768 copyprivate ( variable-list ) */
9769
9770 static tree
9771 c_parser_omp_clause_copyprivate (c_parser *parser, tree list)
9772 {
9773 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_COPYPRIVATE, list);
9774 }
9775
9776 /* OpenMP 2.5:
9777 default ( shared | none ) */
9778
9779 static tree
9780 c_parser_omp_clause_default (c_parser *parser, tree list)
9781 {
9782 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
9783 location_t loc = c_parser_peek_token (parser)->location;
9784 tree c;
9785
9786 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
9787 return list;
9788 if (c_parser_next_token_is (parser, CPP_NAME))
9789 {
9790 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
9791
9792 switch (p[0])
9793 {
9794 case 'n':
9795 if (strcmp ("none", p) != 0)
9796 goto invalid_kind;
9797 kind = OMP_CLAUSE_DEFAULT_NONE;
9798 break;
9799
9800 case 's':
9801 if (strcmp ("shared", p) != 0)
9802 goto invalid_kind;
9803 kind = OMP_CLAUSE_DEFAULT_SHARED;
9804 break;
9805
9806 default:
9807 goto invalid_kind;
9808 }
9809
9810 c_parser_consume_token (parser);
9811 }
9812 else
9813 {
9814 invalid_kind:
9815 c_parser_error (parser, "expected %<none%> or %<shared%>");
9816 }
9817 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
9818
9819 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
9820 return list;
9821
9822 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default");
9823 c = build_omp_clause (loc, OMP_CLAUSE_DEFAULT);
9824 OMP_CLAUSE_CHAIN (c) = list;
9825 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
9826
9827 return c;
9828 }
9829
9830 /* OpenMP 2.5:
9831 firstprivate ( variable-list ) */
9832
9833 static tree
9834 c_parser_omp_clause_firstprivate (c_parser *parser, tree list)
9835 {
9836 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_FIRSTPRIVATE, list);
9837 }
9838
9839 /* OpenMP 3.1:
9840 final ( expression ) */
9841
9842 static tree
9843 c_parser_omp_clause_final (c_parser *parser, tree list)
9844 {
9845 location_t loc = c_parser_peek_token (parser)->location;
9846 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
9847 {
9848 tree t = c_parser_paren_condition (parser);
9849 tree c;
9850
9851 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final");
9852
9853 c = build_omp_clause (loc, OMP_CLAUSE_FINAL);
9854 OMP_CLAUSE_FINAL_EXPR (c) = t;
9855 OMP_CLAUSE_CHAIN (c) = list;
9856 list = c;
9857 }
9858 else
9859 c_parser_error (parser, "expected %<(%>");
9860
9861 return list;
9862 }
9863
9864 /* OpenMP 2.5:
9865 if ( expression ) */
9866
9867 static tree
9868 c_parser_omp_clause_if (c_parser *parser, tree list)
9869 {
9870 location_t loc = c_parser_peek_token (parser)->location;
9871 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
9872 {
9873 tree t = c_parser_paren_condition (parser);
9874 tree c;
9875
9876 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if");
9877
9878 c = build_omp_clause (loc, OMP_CLAUSE_IF);
9879 OMP_CLAUSE_IF_EXPR (c) = t;
9880 OMP_CLAUSE_CHAIN (c) = list;
9881 list = c;
9882 }
9883 else
9884 c_parser_error (parser, "expected %<(%>");
9885
9886 return list;
9887 }
9888
9889 /* OpenMP 2.5:
9890 lastprivate ( variable-list ) */
9891
9892 static tree
9893 c_parser_omp_clause_lastprivate (c_parser *parser, tree list)
9894 {
9895 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_LASTPRIVATE, list);
9896 }
9897
9898 /* OpenMP 3.1:
9899 mergeable */
9900
9901 static tree
9902 c_parser_omp_clause_mergeable (c_parser *parser ATTRIBUTE_UNUSED, tree list)
9903 {
9904 tree c;
9905
9906 /* FIXME: Should we allow duplicates? */
9907 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable");
9908
9909 c = build_omp_clause (c_parser_peek_token (parser)->location,
9910 OMP_CLAUSE_MERGEABLE);
9911 OMP_CLAUSE_CHAIN (c) = list;
9912
9913 return c;
9914 }
9915
9916 /* OpenMP 2.5:
9917 nowait */
9918
9919 static tree
9920 c_parser_omp_clause_nowait (c_parser *parser ATTRIBUTE_UNUSED, tree list)
9921 {
9922 tree c;
9923 location_t loc = c_parser_peek_token (parser)->location;
9924
9925 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait");
9926
9927 c = build_omp_clause (loc, OMP_CLAUSE_NOWAIT);
9928 OMP_CLAUSE_CHAIN (c) = list;
9929 return c;
9930 }
9931
9932 /* OpenMP 2.5:
9933 num_threads ( expression ) */
9934
9935 static tree
9936 c_parser_omp_clause_num_threads (c_parser *parser, tree list)
9937 {
9938 location_t num_threads_loc = c_parser_peek_token (parser)->location;
9939 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
9940 {
9941 location_t expr_loc = c_parser_peek_token (parser)->location;
9942 tree c, t = c_parser_expression (parser).value;
9943 mark_exp_read (t);
9944 t = c_fully_fold (t, false, NULL);
9945
9946 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
9947
9948 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
9949 {
9950 c_parser_error (parser, "expected integer expression");
9951 return list;
9952 }
9953
9954 /* Attempt to statically determine when the number isn't positive. */
9955 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
9956 build_int_cst (TREE_TYPE (t), 0));
9957 if (CAN_HAVE_LOCATION_P (c))
9958 SET_EXPR_LOCATION (c, expr_loc);
9959 if (c == boolean_true_node)
9960 {
9961 warning_at (expr_loc, 0,
9962 "%<num_threads%> value must be positive");
9963 t = integer_one_node;
9964 }
9965
9966 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS, "num_threads");
9967
9968 c = build_omp_clause (num_threads_loc, OMP_CLAUSE_NUM_THREADS);
9969 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
9970 OMP_CLAUSE_CHAIN (c) = list;
9971 list = c;
9972 }
9973
9974 return list;
9975 }
9976
9977 /* OpenMP 2.5:
9978 ordered */
9979
9980 static tree
9981 c_parser_omp_clause_ordered (c_parser *parser, tree list)
9982 {
9983 tree c;
9984
9985 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED, "ordered");
9986
9987 c = build_omp_clause (c_parser_peek_token (parser)->location,
9988 OMP_CLAUSE_ORDERED);
9989 OMP_CLAUSE_CHAIN (c) = list;
9990
9991 return c;
9992 }
9993
9994 /* OpenMP 2.5:
9995 private ( variable-list ) */
9996
9997 static tree
9998 c_parser_omp_clause_private (c_parser *parser, tree list)
9999 {
10000 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_PRIVATE, list);
10001 }
10002
10003 /* OpenMP 2.5:
10004 reduction ( reduction-operator : variable-list )
10005
10006 reduction-operator:
10007 One of: + * - & ^ | && ||
10008
10009 OpenMP 3.1:
10010
10011 reduction-operator:
10012 One of: + * - & ^ | && || max min
10013
10014 OpenMP 4.0:
10015
10016 reduction-operator:
10017 One of: + * - & ^ | && ||
10018 identifier */
10019
10020 static tree
10021 c_parser_omp_clause_reduction (c_parser *parser, tree list)
10022 {
10023 location_t clause_loc = c_parser_peek_token (parser)->location;
10024 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10025 {
10026 enum tree_code code = ERROR_MARK;
10027 tree reduc_id = NULL_TREE;
10028
10029 switch (c_parser_peek_token (parser)->type)
10030 {
10031 case CPP_PLUS:
10032 code = PLUS_EXPR;
10033 break;
10034 case CPP_MULT:
10035 code = MULT_EXPR;
10036 break;
10037 case CPP_MINUS:
10038 code = MINUS_EXPR;
10039 break;
10040 case CPP_AND:
10041 code = BIT_AND_EXPR;
10042 break;
10043 case CPP_XOR:
10044 code = BIT_XOR_EXPR;
10045 break;
10046 case CPP_OR:
10047 code = BIT_IOR_EXPR;
10048 break;
10049 case CPP_AND_AND:
10050 code = TRUTH_ANDIF_EXPR;
10051 break;
10052 case CPP_OR_OR:
10053 code = TRUTH_ORIF_EXPR;
10054 break;
10055 case CPP_NAME:
10056 {
10057 const char *p
10058 = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
10059 if (strcmp (p, "min") == 0)
10060 {
10061 code = MIN_EXPR;
10062 break;
10063 }
10064 if (strcmp (p, "max") == 0)
10065 {
10066 code = MAX_EXPR;
10067 break;
10068 }
10069 reduc_id = c_parser_peek_token (parser)->value;
10070 break;
10071 }
10072 default:
10073 c_parser_error (parser,
10074 "expected %<+%>, %<*%>, %<-%>, %<&%>, "
10075 "%<^%>, %<|%>, %<&&%>, %<||%>, %<min%> or %<max%>");
10076 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
10077 return list;
10078 }
10079 c_parser_consume_token (parser);
10080 reduc_id = c_omp_reduction_id (code, reduc_id);
10081 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
10082 {
10083 tree nl, c;
10084
10085 nl = c_parser_omp_variable_list (parser, clause_loc,
10086 OMP_CLAUSE_REDUCTION, list);
10087 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
10088 {
10089 tree type = TREE_TYPE (OMP_CLAUSE_DECL (c));
10090 OMP_CLAUSE_REDUCTION_CODE (c) = code;
10091 if (code == ERROR_MARK
10092 || !(INTEGRAL_TYPE_P (type)
10093 || TREE_CODE (type) == REAL_TYPE
10094 || TREE_CODE (type) == COMPLEX_TYPE))
10095 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c)
10096 = c_omp_reduction_lookup (reduc_id,
10097 TYPE_MAIN_VARIANT (type));
10098 }
10099
10100 list = nl;
10101 }
10102 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10103 }
10104 return list;
10105 }
10106
10107 /* OpenMP 2.5:
10108 schedule ( schedule-kind )
10109 schedule ( schedule-kind , expression )
10110
10111 schedule-kind:
10112 static | dynamic | guided | runtime | auto
10113 */
10114
10115 static tree
10116 c_parser_omp_clause_schedule (c_parser *parser, tree list)
10117 {
10118 tree c, t;
10119 location_t loc = c_parser_peek_token (parser)->location;
10120
10121 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10122 return list;
10123
10124 c = build_omp_clause (loc, OMP_CLAUSE_SCHEDULE);
10125
10126 if (c_parser_next_token_is (parser, CPP_NAME))
10127 {
10128 tree kind = c_parser_peek_token (parser)->value;
10129 const char *p = IDENTIFIER_POINTER (kind);
10130
10131 switch (p[0])
10132 {
10133 case 'd':
10134 if (strcmp ("dynamic", p) != 0)
10135 goto invalid_kind;
10136 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
10137 break;
10138
10139 case 'g':
10140 if (strcmp ("guided", p) != 0)
10141 goto invalid_kind;
10142 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
10143 break;
10144
10145 case 'r':
10146 if (strcmp ("runtime", p) != 0)
10147 goto invalid_kind;
10148 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
10149 break;
10150
10151 default:
10152 goto invalid_kind;
10153 }
10154 }
10155 else if (c_parser_next_token_is_keyword (parser, RID_STATIC))
10156 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
10157 else if (c_parser_next_token_is_keyword (parser, RID_AUTO))
10158 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
10159 else
10160 goto invalid_kind;
10161
10162 c_parser_consume_token (parser);
10163 if (c_parser_next_token_is (parser, CPP_COMMA))
10164 {
10165 location_t here;
10166 c_parser_consume_token (parser);
10167
10168 here = c_parser_peek_token (parser)->location;
10169 t = c_parser_expr_no_commas (parser, NULL).value;
10170 mark_exp_read (t);
10171 t = c_fully_fold (t, false, NULL);
10172
10173 if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
10174 error_at (here, "schedule %<runtime%> does not take "
10175 "a %<chunk_size%> parameter");
10176 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
10177 error_at (here,
10178 "schedule %<auto%> does not take "
10179 "a %<chunk_size%> parameter");
10180 else if (TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE)
10181 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
10182 else
10183 c_parser_error (parser, "expected integer expression");
10184
10185 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10186 }
10187 else
10188 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
10189 "expected %<,%> or %<)%>");
10190
10191 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
10192 OMP_CLAUSE_CHAIN (c) = list;
10193 return c;
10194
10195 invalid_kind:
10196 c_parser_error (parser, "invalid schedule kind");
10197 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
10198 return list;
10199 }
10200
10201 /* OpenMP 2.5:
10202 shared ( variable-list ) */
10203
10204 static tree
10205 c_parser_omp_clause_shared (c_parser *parser, tree list)
10206 {
10207 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_SHARED, list);
10208 }
10209
10210 /* OpenMP 3.0:
10211 untied */
10212
10213 static tree
10214 c_parser_omp_clause_untied (c_parser *parser ATTRIBUTE_UNUSED, tree list)
10215 {
10216 tree c;
10217
10218 /* FIXME: Should we allow duplicates? */
10219 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied");
10220
10221 c = build_omp_clause (c_parser_peek_token (parser)->location,
10222 OMP_CLAUSE_UNTIED);
10223 OMP_CLAUSE_CHAIN (c) = list;
10224
10225 return c;
10226 }
10227
10228 /* OpenMP 4.0:
10229 inbranch
10230 notinbranch */
10231
10232 static tree
10233 c_parser_omp_clause_branch (c_parser *parser ATTRIBUTE_UNUSED,
10234 enum omp_clause_code code, tree list)
10235 {
10236 check_no_duplicate_clause (list, code, omp_clause_code_name[code]);
10237
10238 tree c = build_omp_clause (c_parser_peek_token (parser)->location, code);
10239 OMP_CLAUSE_CHAIN (c) = list;
10240
10241 return c;
10242 }
10243
10244 /* OpenMP 4.0:
10245 parallel
10246 for
10247 sections
10248 taskgroup */
10249
10250 static tree
10251 c_parser_omp_clause_cancelkind (c_parser *parser ATTRIBUTE_UNUSED,
10252 enum omp_clause_code code, tree list)
10253 {
10254 tree c = build_omp_clause (c_parser_peek_token (parser)->location, code);
10255 OMP_CLAUSE_CHAIN (c) = list;
10256
10257 return c;
10258 }
10259
10260 /* OpenMP 4.0:
10261 num_teams ( expression ) */
10262
10263 static tree
10264 c_parser_omp_clause_num_teams (c_parser *parser, tree list)
10265 {
10266 location_t num_teams_loc = c_parser_peek_token (parser)->location;
10267 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10268 {
10269 location_t expr_loc = c_parser_peek_token (parser)->location;
10270 tree c, t = c_parser_expression (parser).value;
10271 mark_exp_read (t);
10272 t = c_fully_fold (t, false, NULL);
10273
10274 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10275
10276 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
10277 {
10278 c_parser_error (parser, "expected integer expression");
10279 return list;
10280 }
10281
10282 /* Attempt to statically determine when the number isn't positive. */
10283 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
10284 build_int_cst (TREE_TYPE (t), 0));
10285 if (CAN_HAVE_LOCATION_P (c))
10286 SET_EXPR_LOCATION (c, expr_loc);
10287 if (c == boolean_true_node)
10288 {
10289 warning_at (expr_loc, 0, "%<num_teams%> value must be positive");
10290 t = integer_one_node;
10291 }
10292
10293 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS, "num_teams");
10294
10295 c = build_omp_clause (num_teams_loc, OMP_CLAUSE_NUM_TEAMS);
10296 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
10297 OMP_CLAUSE_CHAIN (c) = list;
10298 list = c;
10299 }
10300
10301 return list;
10302 }
10303
10304 /* OpenMP 4.0:
10305 thread_limit ( expression ) */
10306
10307 static tree
10308 c_parser_omp_clause_thread_limit (c_parser *parser, tree list)
10309 {
10310 location_t num_teams_loc = c_parser_peek_token (parser)->location;
10311 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10312 {
10313 location_t expr_loc = c_parser_peek_token (parser)->location;
10314 tree c, t = c_parser_expression (parser).value;
10315 mark_exp_read (t);
10316 t = c_fully_fold (t, false, NULL);
10317
10318 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10319
10320 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
10321 {
10322 c_parser_error (parser, "expected integer expression");
10323 return list;
10324 }
10325
10326 /* Attempt to statically determine when the number isn't positive. */
10327 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
10328 build_int_cst (TREE_TYPE (t), 0));
10329 if (CAN_HAVE_LOCATION_P (c))
10330 SET_EXPR_LOCATION (c, expr_loc);
10331 if (c == boolean_true_node)
10332 {
10333 warning_at (expr_loc, 0, "%<thread_limit%> value must be positive");
10334 t = integer_one_node;
10335 }
10336
10337 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
10338 "thread_limit");
10339
10340 c = build_omp_clause (num_teams_loc, OMP_CLAUSE_THREAD_LIMIT);
10341 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
10342 OMP_CLAUSE_CHAIN (c) = list;
10343 list = c;
10344 }
10345
10346 return list;
10347 }
10348
10349 /* OpenMP 4.0:
10350 aligned ( variable-list )
10351 aligned ( variable-list : constant-expression ) */
10352
10353 static tree
10354 c_parser_omp_clause_aligned (c_parser *parser, tree list)
10355 {
10356 location_t clause_loc = c_parser_peek_token (parser)->location;
10357 tree nl, c;
10358
10359 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10360 return list;
10361
10362 nl = c_parser_omp_variable_list (parser, clause_loc,
10363 OMP_CLAUSE_ALIGNED, list);
10364
10365 if (c_parser_next_token_is (parser, CPP_COLON))
10366 {
10367 c_parser_consume_token (parser);
10368 tree alignment = c_parser_expr_no_commas (parser, NULL).value;
10369 mark_exp_read (alignment);
10370 alignment = c_fully_fold (alignment, false, NULL);
10371 if (!INTEGRAL_TYPE_P (TREE_TYPE (alignment))
10372 && TREE_CODE (alignment) != INTEGER_CST
10373 && tree_int_cst_sgn (alignment) != 1)
10374 {
10375 error_at (clause_loc, "%<aligned%> clause alignment expression must "
10376 "be positive constant integer expression");
10377 alignment = NULL_TREE;
10378 }
10379
10380 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
10381 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
10382 }
10383
10384 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10385 return nl;
10386 }
10387
10388 /* OpenMP 4.0:
10389 linear ( variable-list )
10390 linear ( variable-list : expression ) */
10391
10392 static tree
10393 c_parser_omp_clause_linear (c_parser *parser, tree list)
10394 {
10395 location_t clause_loc = c_parser_peek_token (parser)->location;
10396 tree nl, c, step;
10397
10398 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10399 return list;
10400
10401 nl = c_parser_omp_variable_list (parser, clause_loc,
10402 OMP_CLAUSE_LINEAR, list);
10403
10404 if (c_parser_next_token_is (parser, CPP_COLON))
10405 {
10406 c_parser_consume_token (parser);
10407 step = c_parser_expression (parser).value;
10408 mark_exp_read (step);
10409 step = c_fully_fold (step, false, NULL);
10410 if (!INTEGRAL_TYPE_P (TREE_TYPE (step)))
10411 {
10412 error_at (clause_loc, "%<linear%> clause step expression must "
10413 "be integral");
10414 step = integer_one_node;
10415 }
10416
10417 }
10418 else
10419 step = integer_one_node;
10420
10421 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
10422 {
10423 OMP_CLAUSE_LINEAR_STEP (c) = step;
10424 }
10425
10426 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10427 return nl;
10428 }
10429
10430 /* OpenMP 4.0:
10431 safelen ( constant-expression ) */
10432
10433 static tree
10434 c_parser_omp_clause_safelen (c_parser *parser, tree list)
10435 {
10436 location_t clause_loc = c_parser_peek_token (parser)->location;
10437 tree c, t;
10438
10439 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10440 return list;
10441
10442 t = c_parser_expr_no_commas (parser, NULL).value;
10443 mark_exp_read (t);
10444 t = c_fully_fold (t, false, NULL);
10445 if (!INTEGRAL_TYPE_P (TREE_TYPE (t))
10446 && TREE_CODE (t) != INTEGER_CST
10447 && tree_int_cst_sgn (t) != 1)
10448 {
10449 error_at (clause_loc, "%<safelen%> clause expression must "
10450 "be positive constant integer expression");
10451 t = NULL_TREE;
10452 }
10453
10454 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10455 if (t == NULL_TREE || t == error_mark_node)
10456 return list;
10457
10458 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen");
10459
10460 c = build_omp_clause (clause_loc, OMP_CLAUSE_SAFELEN);
10461 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
10462 OMP_CLAUSE_CHAIN (c) = list;
10463 return c;
10464 }
10465
10466 /* OpenMP 4.0:
10467 simdlen ( constant-expression ) */
10468
10469 static tree
10470 c_parser_omp_clause_simdlen (c_parser *parser, tree list)
10471 {
10472 location_t clause_loc = c_parser_peek_token (parser)->location;
10473 tree c, t;
10474
10475 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10476 return list;
10477
10478 t = c_parser_expr_no_commas (parser, NULL).value;
10479 mark_exp_read (t);
10480 t = c_fully_fold (t, false, NULL);
10481 if (!INTEGRAL_TYPE_P (TREE_TYPE (t))
10482 && TREE_CODE (t) != INTEGER_CST
10483 && tree_int_cst_sgn (t) != 1)
10484 {
10485 error_at (clause_loc, "%<simdlen%> clause expression must "
10486 "be positive constant integer expression");
10487 t = NULL_TREE;
10488 }
10489
10490 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10491 if (t == NULL_TREE || t == error_mark_node)
10492 return list;
10493
10494 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen");
10495
10496 c = build_omp_clause (clause_loc, OMP_CLAUSE_SIMDLEN);
10497 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
10498 OMP_CLAUSE_CHAIN (c) = list;
10499 return c;
10500 }
10501
10502 /* OpenMP 4.0:
10503 depend ( depend-kind: variable-list )
10504
10505 depend-kind:
10506 in | out | inout */
10507
10508 static tree
10509 c_parser_omp_clause_depend (c_parser *parser, tree list)
10510 {
10511 location_t clause_loc = c_parser_peek_token (parser)->location;
10512 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_INOUT;
10513 tree nl, c;
10514
10515 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10516 return list;
10517
10518 if (c_parser_next_token_is (parser, CPP_NAME))
10519 {
10520 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
10521 if (strcmp ("in", p) == 0)
10522 kind = OMP_CLAUSE_DEPEND_IN;
10523 else if (strcmp ("inout", p) == 0)
10524 kind = OMP_CLAUSE_DEPEND_INOUT;
10525 else if (strcmp ("out", p) == 0)
10526 kind = OMP_CLAUSE_DEPEND_OUT;
10527 else
10528 goto invalid_kind;
10529 }
10530 else
10531 goto invalid_kind;
10532
10533 c_parser_consume_token (parser);
10534 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
10535 goto resync_fail;
10536
10537 nl = c_parser_omp_variable_list (parser, clause_loc,
10538 OMP_CLAUSE_DEPEND, list);
10539
10540 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
10541 OMP_CLAUSE_DEPEND_KIND (c) = kind;
10542
10543 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10544 return nl;
10545
10546 invalid_kind:
10547 c_parser_error (parser, "invalid depend kind");
10548 resync_fail:
10549 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10550 return list;
10551 }
10552
10553 /* OpenMP 4.0:
10554 map ( map-kind: variable-list )
10555 map ( variable-list )
10556
10557 map-kind:
10558 alloc | to | from | tofrom */
10559
10560 static tree
10561 c_parser_omp_clause_map (c_parser *parser, tree list)
10562 {
10563 location_t clause_loc = c_parser_peek_token (parser)->location;
10564 enum omp_clause_map_kind kind = OMP_CLAUSE_MAP_TOFROM;
10565 tree nl, c;
10566
10567 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10568 return list;
10569
10570 if (c_parser_next_token_is (parser, CPP_NAME)
10571 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
10572 {
10573 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
10574 if (strcmp ("alloc", p) == 0)
10575 kind = OMP_CLAUSE_MAP_ALLOC;
10576 else if (strcmp ("to", p) == 0)
10577 kind = OMP_CLAUSE_MAP_TO;
10578 else if (strcmp ("from", p) == 0)
10579 kind = OMP_CLAUSE_MAP_FROM;
10580 else if (strcmp ("tofrom", p) == 0)
10581 kind = OMP_CLAUSE_MAP_TOFROM;
10582 else
10583 {
10584 c_parser_error (parser, "invalid map kind");
10585 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
10586 "expected %<)%>");
10587 return list;
10588 }
10589 c_parser_consume_token (parser);
10590 c_parser_consume_token (parser);
10591 }
10592
10593 nl = c_parser_omp_variable_list (parser, clause_loc, OMP_CLAUSE_MAP, list);
10594
10595 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
10596 OMP_CLAUSE_MAP_KIND (c) = kind;
10597
10598 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10599 return nl;
10600 }
10601
10602 /* OpenMP 4.0:
10603 device ( expression ) */
10604
10605 static tree
10606 c_parser_omp_clause_device (c_parser *parser, tree list)
10607 {
10608 location_t clause_loc = c_parser_peek_token (parser)->location;
10609 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10610 {
10611 tree c, t = c_parser_expr_no_commas (parser, NULL).value;
10612 mark_exp_read (t);
10613 t = c_fully_fold (t, false, NULL);
10614
10615 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10616
10617 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
10618 {
10619 c_parser_error (parser, "expected integer expression");
10620 return list;
10621 }
10622
10623 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE, "device");
10624
10625 c = build_omp_clause (clause_loc, OMP_CLAUSE_DEVICE);
10626 OMP_CLAUSE_DEVICE_ID (c) = t;
10627 OMP_CLAUSE_CHAIN (c) = list;
10628 list = c;
10629 }
10630
10631 return list;
10632 }
10633
10634 /* OpenMP 4.0:
10635 dist_schedule ( static )
10636 dist_schedule ( static , expression ) */
10637
10638 static tree
10639 c_parser_omp_clause_dist_schedule (c_parser *parser, tree list)
10640 {
10641 tree c, t = NULL_TREE;
10642 location_t loc = c_parser_peek_token (parser)->location;
10643
10644 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10645 return list;
10646
10647 if (!c_parser_next_token_is_keyword (parser, RID_STATIC))
10648 {
10649 c_parser_error (parser, "invalid dist_schedule kind");
10650 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
10651 "expected %<)%>");
10652 return list;
10653 }
10654
10655 c_parser_consume_token (parser);
10656 if (c_parser_next_token_is (parser, CPP_COMMA))
10657 {
10658 c_parser_consume_token (parser);
10659
10660 t = c_parser_expr_no_commas (parser, NULL).value;
10661 mark_exp_read (t);
10662 t = c_fully_fold (t, false, NULL);
10663 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10664 }
10665 else
10666 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
10667 "expected %<,%> or %<)%>");
10668
10669 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
10670 if (t == error_mark_node)
10671 return list;
10672
10673 c = build_omp_clause (loc, OMP_CLAUSE_DIST_SCHEDULE);
10674 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
10675 OMP_CLAUSE_CHAIN (c) = list;
10676 return c;
10677 }
10678
10679 /* OpenMP 4.0:
10680 proc_bind ( proc-bind-kind )
10681
10682 proc-bind-kind:
10683 master | close | spread */
10684
10685 static tree
10686 c_parser_omp_clause_proc_bind (c_parser *parser, tree list)
10687 {
10688 location_t clause_loc = c_parser_peek_token (parser)->location;
10689 enum omp_clause_proc_bind_kind kind;
10690 tree c;
10691
10692 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10693 return list;
10694
10695 if (c_parser_next_token_is (parser, CPP_NAME))
10696 {
10697 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
10698 if (strcmp ("master", p) == 0)
10699 kind = OMP_CLAUSE_PROC_BIND_MASTER;
10700 else if (strcmp ("close", p) == 0)
10701 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
10702 else if (strcmp ("spread", p) == 0)
10703 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
10704 else
10705 goto invalid_kind;
10706 }
10707 else
10708 goto invalid_kind;
10709
10710 c_parser_consume_token (parser);
10711 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10712 c = build_omp_clause (clause_loc, OMP_CLAUSE_PROC_BIND);
10713 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
10714 OMP_CLAUSE_CHAIN (c) = list;
10715 return c;
10716
10717 invalid_kind:
10718 c_parser_error (parser, "invalid proc_bind kind");
10719 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10720 return list;
10721 }
10722
10723 /* OpenMP 4.0:
10724 to ( variable-list ) */
10725
10726 static tree
10727 c_parser_omp_clause_to (c_parser *parser, tree list)
10728 {
10729 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_TO, list);
10730 }
10731
10732 /* OpenMP 4.0:
10733 from ( variable-list ) */
10734
10735 static tree
10736 c_parser_omp_clause_from (c_parser *parser, tree list)
10737 {
10738 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_FROM, list);
10739 }
10740
10741 /* OpenMP 4.0:
10742 uniform ( variable-list ) */
10743
10744 static tree
10745 c_parser_omp_clause_uniform (c_parser *parser, tree list)
10746 {
10747 /* The clauses location. */
10748 location_t loc = c_parser_peek_token (parser)->location;
10749
10750 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10751 {
10752 list = c_parser_omp_variable_list (parser, loc, OMP_CLAUSE_UNIFORM,
10753 list);
10754 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10755 }
10756 return list;
10757 }
10758
10759 /* Parse all OpenMP clauses. The set clauses allowed by the directive
10760 is a bitmask in MASK. Return the list of clauses found; the result
10761 of clause default goes in *pdefault. */
10762
10763 static tree
10764 c_parser_omp_all_clauses (c_parser *parser, omp_clause_mask mask,
10765 const char *where, bool finish_p = true)
10766 {
10767 tree clauses = NULL;
10768 bool first = true;
10769
10770 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
10771 {
10772 location_t here;
10773 pragma_omp_clause c_kind;
10774 const char *c_name;
10775 tree prev = clauses;
10776
10777 if (!first && c_parser_next_token_is (parser, CPP_COMMA))
10778 c_parser_consume_token (parser);
10779
10780 here = c_parser_peek_token (parser)->location;
10781 c_kind = c_parser_omp_clause_name (parser);
10782
10783 switch (c_kind)
10784 {
10785 case PRAGMA_OMP_CLAUSE_COLLAPSE:
10786 clauses = c_parser_omp_clause_collapse (parser, clauses);
10787 c_name = "collapse";
10788 break;
10789 case PRAGMA_OMP_CLAUSE_COPYIN:
10790 clauses = c_parser_omp_clause_copyin (parser, clauses);
10791 c_name = "copyin";
10792 break;
10793 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
10794 clauses = c_parser_omp_clause_copyprivate (parser, clauses);
10795 c_name = "copyprivate";
10796 break;
10797 case PRAGMA_OMP_CLAUSE_DEFAULT:
10798 clauses = c_parser_omp_clause_default (parser, clauses);
10799 c_name = "default";
10800 break;
10801 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
10802 clauses = c_parser_omp_clause_firstprivate (parser, clauses);
10803 c_name = "firstprivate";
10804 break;
10805 case PRAGMA_OMP_CLAUSE_FINAL:
10806 clauses = c_parser_omp_clause_final (parser, clauses);
10807 c_name = "final";
10808 break;
10809 case PRAGMA_OMP_CLAUSE_IF:
10810 clauses = c_parser_omp_clause_if (parser, clauses);
10811 c_name = "if";
10812 break;
10813 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
10814 clauses = c_parser_omp_clause_lastprivate (parser, clauses);
10815 c_name = "lastprivate";
10816 break;
10817 case PRAGMA_OMP_CLAUSE_MERGEABLE:
10818 clauses = c_parser_omp_clause_mergeable (parser, clauses);
10819 c_name = "mergeable";
10820 break;
10821 case PRAGMA_OMP_CLAUSE_NOWAIT:
10822 clauses = c_parser_omp_clause_nowait (parser, clauses);
10823 c_name = "nowait";
10824 break;
10825 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
10826 clauses = c_parser_omp_clause_num_threads (parser, clauses);
10827 c_name = "num_threads";
10828 break;
10829 case PRAGMA_OMP_CLAUSE_ORDERED:
10830 clauses = c_parser_omp_clause_ordered (parser, clauses);
10831 c_name = "ordered";
10832 break;
10833 case PRAGMA_OMP_CLAUSE_PRIVATE:
10834 clauses = c_parser_omp_clause_private (parser, clauses);
10835 c_name = "private";
10836 break;
10837 case PRAGMA_OMP_CLAUSE_REDUCTION:
10838 clauses = c_parser_omp_clause_reduction (parser, clauses);
10839 c_name = "reduction";
10840 break;
10841 case PRAGMA_OMP_CLAUSE_SCHEDULE:
10842 clauses = c_parser_omp_clause_schedule (parser, clauses);
10843 c_name = "schedule";
10844 break;
10845 case PRAGMA_OMP_CLAUSE_SHARED:
10846 clauses = c_parser_omp_clause_shared (parser, clauses);
10847 c_name = "shared";
10848 break;
10849 case PRAGMA_OMP_CLAUSE_UNTIED:
10850 clauses = c_parser_omp_clause_untied (parser, clauses);
10851 c_name = "untied";
10852 break;
10853 case PRAGMA_OMP_CLAUSE_INBRANCH:
10854 clauses = c_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
10855 clauses);
10856 c_name = "inbranch";
10857 break;
10858 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
10859 clauses = c_parser_omp_clause_branch (parser, OMP_CLAUSE_NOTINBRANCH,
10860 clauses);
10861 c_name = "notinbranch";
10862 break;
10863 case PRAGMA_OMP_CLAUSE_PARALLEL:
10864 clauses
10865 = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
10866 clauses);
10867 c_name = "parallel";
10868 if (!first)
10869 {
10870 clause_not_first:
10871 error_at (here, "%qs must be the first clause of %qs",
10872 c_name, where);
10873 clauses = prev;
10874 }
10875 break;
10876 case PRAGMA_OMP_CLAUSE_FOR:
10877 clauses
10878 = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
10879 clauses);
10880 c_name = "for";
10881 if (!first)
10882 goto clause_not_first;
10883 break;
10884 case PRAGMA_OMP_CLAUSE_SECTIONS:
10885 clauses
10886 = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
10887 clauses);
10888 c_name = "sections";
10889 if (!first)
10890 goto clause_not_first;
10891 break;
10892 case PRAGMA_OMP_CLAUSE_TASKGROUP:
10893 clauses
10894 = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
10895 clauses);
10896 c_name = "taskgroup";
10897 if (!first)
10898 goto clause_not_first;
10899 break;
10900 case PRAGMA_OMP_CLAUSE_TO:
10901 clauses = c_parser_omp_clause_to (parser, clauses);
10902 c_name = "to";
10903 break;
10904 case PRAGMA_OMP_CLAUSE_FROM:
10905 clauses = c_parser_omp_clause_from (parser, clauses);
10906 c_name = "from";
10907 break;
10908 case PRAGMA_OMP_CLAUSE_UNIFORM:
10909 clauses = c_parser_omp_clause_uniform (parser, clauses);
10910 c_name = "uniform";
10911 break;
10912 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
10913 clauses = c_parser_omp_clause_num_teams (parser, clauses);
10914 c_name = "num_teams";
10915 break;
10916 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
10917 clauses = c_parser_omp_clause_thread_limit (parser, clauses);
10918 c_name = "thread_limit";
10919 break;
10920 case PRAGMA_OMP_CLAUSE_ALIGNED:
10921 clauses = c_parser_omp_clause_aligned (parser, clauses);
10922 c_name = "aligned";
10923 break;
10924 case PRAGMA_OMP_CLAUSE_LINEAR:
10925 clauses = c_parser_omp_clause_linear (parser, clauses);
10926 c_name = "linear";
10927 break;
10928 case PRAGMA_OMP_CLAUSE_DEPEND:
10929 clauses = c_parser_omp_clause_depend (parser, clauses);
10930 c_name = "depend";
10931 break;
10932 case PRAGMA_OMP_CLAUSE_MAP:
10933 clauses = c_parser_omp_clause_map (parser, clauses);
10934 c_name = "map";
10935 break;
10936 case PRAGMA_OMP_CLAUSE_DEVICE:
10937 clauses = c_parser_omp_clause_device (parser, clauses);
10938 c_name = "device";
10939 break;
10940 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
10941 clauses = c_parser_omp_clause_dist_schedule (parser, clauses);
10942 c_name = "dist_schedule";
10943 break;
10944 case PRAGMA_OMP_CLAUSE_PROC_BIND:
10945 clauses = c_parser_omp_clause_proc_bind (parser, clauses);
10946 c_name = "proc_bind";
10947 break;
10948 case PRAGMA_OMP_CLAUSE_SAFELEN:
10949 clauses = c_parser_omp_clause_safelen (parser, clauses);
10950 c_name = "safelen";
10951 break;
10952 case PRAGMA_OMP_CLAUSE_SIMDLEN:
10953 clauses = c_parser_omp_clause_simdlen (parser, clauses);
10954 c_name = "simdlen";
10955 break;
10956 default:
10957 c_parser_error (parser, "expected %<#pragma omp%> clause");
10958 goto saw_error;
10959 }
10960
10961 first = false;
10962
10963 if (((mask >> c_kind) & 1) == 0 && !parser->error)
10964 {
10965 /* Remove the invalid clause(s) from the list to avoid
10966 confusing the rest of the compiler. */
10967 clauses = prev;
10968 error_at (here, "%qs is not valid for %qs", c_name, where);
10969 }
10970 }
10971
10972 saw_error:
10973 c_parser_skip_to_pragma_eol (parser);
10974
10975 if (finish_p)
10976 return c_finish_omp_clauses (clauses);
10977
10978 return clauses;
10979 }
10980
10981 /* OpenMP 2.5:
10982 structured-block:
10983 statement
10984
10985 In practice, we're also interested in adding the statement to an
10986 outer node. So it is convenient if we work around the fact that
10987 c_parser_statement calls add_stmt. */
10988
10989 static tree
10990 c_parser_omp_structured_block (c_parser *parser)
10991 {
10992 tree stmt = push_stmt_list ();
10993 c_parser_statement (parser);
10994 return pop_stmt_list (stmt);
10995 }
10996
10997 /* OpenMP 2.5:
10998 # pragma omp atomic new-line
10999 expression-stmt
11000
11001 expression-stmt:
11002 x binop= expr | x++ | ++x | x-- | --x
11003 binop:
11004 +, *, -, /, &, ^, |, <<, >>
11005
11006 where x is an lvalue expression with scalar type.
11007
11008 OpenMP 3.1:
11009 # pragma omp atomic new-line
11010 update-stmt
11011
11012 # pragma omp atomic read new-line
11013 read-stmt
11014
11015 # pragma omp atomic write new-line
11016 write-stmt
11017
11018 # pragma omp atomic update new-line
11019 update-stmt
11020
11021 # pragma omp atomic capture new-line
11022 capture-stmt
11023
11024 # pragma omp atomic capture new-line
11025 capture-block
11026
11027 read-stmt:
11028 v = x
11029 write-stmt:
11030 x = expr
11031 update-stmt:
11032 expression-stmt | x = x binop expr
11033 capture-stmt:
11034 v = expression-stmt
11035 capture-block:
11036 { v = x; update-stmt; } | { update-stmt; v = x; }
11037
11038 OpenMP 4.0:
11039 update-stmt:
11040 expression-stmt | x = x binop expr | x = expr binop x
11041 capture-stmt:
11042 v = update-stmt
11043 capture-block:
11044 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
11045
11046 where x and v are lvalue expressions with scalar type.
11047
11048 LOC is the location of the #pragma token. */
11049
11050 static void
11051 c_parser_omp_atomic (location_t loc, c_parser *parser)
11052 {
11053 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE;
11054 tree lhs1 = NULL_TREE, rhs1 = NULL_TREE;
11055 tree stmt, orig_lhs, unfolded_lhs = NULL_TREE, unfolded_lhs1 = NULL_TREE;
11056 enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
11057 struct c_expr expr;
11058 location_t eloc;
11059 bool structured_block = false;
11060 bool swapped = false;
11061 bool seq_cst = false;
11062
11063 if (c_parser_next_token_is (parser, CPP_NAME))
11064 {
11065 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
11066
11067 if (!strcmp (p, "read"))
11068 code = OMP_ATOMIC_READ;
11069 else if (!strcmp (p, "write"))
11070 code = NOP_EXPR;
11071 else if (!strcmp (p, "update"))
11072 code = OMP_ATOMIC;
11073 else if (!strcmp (p, "capture"))
11074 code = OMP_ATOMIC_CAPTURE_NEW;
11075 else
11076 p = NULL;
11077 if (p)
11078 c_parser_consume_token (parser);
11079 }
11080 if (c_parser_next_token_is (parser, CPP_NAME))
11081 {
11082 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
11083 if (!strcmp (p, "seq_cst"))
11084 {
11085 seq_cst = true;
11086 c_parser_consume_token (parser);
11087 }
11088 }
11089 c_parser_skip_to_pragma_eol (parser);
11090
11091 switch (code)
11092 {
11093 case OMP_ATOMIC_READ:
11094 case NOP_EXPR: /* atomic write */
11095 v = c_parser_unary_expression (parser).value;
11096 v = c_fully_fold (v, false, NULL);
11097 if (v == error_mark_node)
11098 goto saw_error;
11099 loc = c_parser_peek_token (parser)->location;
11100 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
11101 goto saw_error;
11102 if (code == NOP_EXPR)
11103 lhs = c_parser_expression (parser).value;
11104 else
11105 lhs = c_parser_unary_expression (parser).value;
11106 lhs = c_fully_fold (lhs, false, NULL);
11107 if (lhs == error_mark_node)
11108 goto saw_error;
11109 if (code == NOP_EXPR)
11110 {
11111 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
11112 opcode. */
11113 code = OMP_ATOMIC;
11114 rhs = lhs;
11115 lhs = v;
11116 v = NULL_TREE;
11117 }
11118 goto done;
11119 case OMP_ATOMIC_CAPTURE_NEW:
11120 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
11121 {
11122 c_parser_consume_token (parser);
11123 structured_block = true;
11124 }
11125 else
11126 {
11127 v = c_parser_unary_expression (parser).value;
11128 v = c_fully_fold (v, false, NULL);
11129 if (v == error_mark_node)
11130 goto saw_error;
11131 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
11132 goto saw_error;
11133 }
11134 break;
11135 default:
11136 break;
11137 }
11138
11139 /* For structured_block case we don't know yet whether
11140 old or new x should be captured. */
11141 restart:
11142 eloc = c_parser_peek_token (parser)->location;
11143 expr = c_parser_unary_expression (parser);
11144 lhs = expr.value;
11145 expr = default_function_array_conversion (eloc, expr);
11146 unfolded_lhs = expr.value;
11147 lhs = c_fully_fold (lhs, false, NULL);
11148 orig_lhs = lhs;
11149 switch (TREE_CODE (lhs))
11150 {
11151 case ERROR_MARK:
11152 saw_error:
11153 c_parser_skip_to_end_of_block_or_statement (parser);
11154 if (structured_block)
11155 {
11156 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
11157 c_parser_consume_token (parser);
11158 else if (code == OMP_ATOMIC_CAPTURE_NEW)
11159 {
11160 c_parser_skip_to_end_of_block_or_statement (parser);
11161 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
11162 c_parser_consume_token (parser);
11163 }
11164 }
11165 return;
11166
11167 case POSTINCREMENT_EXPR:
11168 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
11169 code = OMP_ATOMIC_CAPTURE_OLD;
11170 /* FALLTHROUGH */
11171 case PREINCREMENT_EXPR:
11172 lhs = TREE_OPERAND (lhs, 0);
11173 unfolded_lhs = NULL_TREE;
11174 opcode = PLUS_EXPR;
11175 rhs = integer_one_node;
11176 break;
11177
11178 case POSTDECREMENT_EXPR:
11179 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
11180 code = OMP_ATOMIC_CAPTURE_OLD;
11181 /* FALLTHROUGH */
11182 case PREDECREMENT_EXPR:
11183 lhs = TREE_OPERAND (lhs, 0);
11184 unfolded_lhs = NULL_TREE;
11185 opcode = MINUS_EXPR;
11186 rhs = integer_one_node;
11187 break;
11188
11189 case COMPOUND_EXPR:
11190 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
11191 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
11192 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
11193 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
11194 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
11195 (TREE_OPERAND (lhs, 1), 0), 0)))
11196 == BOOLEAN_TYPE)
11197 /* Undo effects of boolean_increment for post {in,de}crement. */
11198 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
11199 /* FALLTHRU */
11200 case MODIFY_EXPR:
11201 if (TREE_CODE (lhs) == MODIFY_EXPR
11202 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
11203 {
11204 /* Undo effects of boolean_increment. */
11205 if (integer_onep (TREE_OPERAND (lhs, 1)))
11206 {
11207 /* This is pre or post increment. */
11208 rhs = TREE_OPERAND (lhs, 1);
11209 lhs = TREE_OPERAND (lhs, 0);
11210 unfolded_lhs = NULL_TREE;
11211 opcode = NOP_EXPR;
11212 if (code == OMP_ATOMIC_CAPTURE_NEW
11213 && !structured_block
11214 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
11215 code = OMP_ATOMIC_CAPTURE_OLD;
11216 break;
11217 }
11218 if (TREE_CODE (TREE_OPERAND (lhs, 1)) == TRUTH_NOT_EXPR
11219 && TREE_OPERAND (lhs, 0)
11220 == TREE_OPERAND (TREE_OPERAND (lhs, 1), 0))
11221 {
11222 /* This is pre or post decrement. */
11223 rhs = TREE_OPERAND (lhs, 1);
11224 lhs = TREE_OPERAND (lhs, 0);
11225 unfolded_lhs = NULL_TREE;
11226 opcode = NOP_EXPR;
11227 if (code == OMP_ATOMIC_CAPTURE_NEW
11228 && !structured_block
11229 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
11230 code = OMP_ATOMIC_CAPTURE_OLD;
11231 break;
11232 }
11233 }
11234 /* FALLTHRU */
11235 default:
11236 switch (c_parser_peek_token (parser)->type)
11237 {
11238 case CPP_MULT_EQ:
11239 opcode = MULT_EXPR;
11240 break;
11241 case CPP_DIV_EQ:
11242 opcode = TRUNC_DIV_EXPR;
11243 break;
11244 case CPP_PLUS_EQ:
11245 opcode = PLUS_EXPR;
11246 break;
11247 case CPP_MINUS_EQ:
11248 opcode = MINUS_EXPR;
11249 break;
11250 case CPP_LSHIFT_EQ:
11251 opcode = LSHIFT_EXPR;
11252 break;
11253 case CPP_RSHIFT_EQ:
11254 opcode = RSHIFT_EXPR;
11255 break;
11256 case CPP_AND_EQ:
11257 opcode = BIT_AND_EXPR;
11258 break;
11259 case CPP_OR_EQ:
11260 opcode = BIT_IOR_EXPR;
11261 break;
11262 case CPP_XOR_EQ:
11263 opcode = BIT_XOR_EXPR;
11264 break;
11265 case CPP_EQ:
11266 c_parser_consume_token (parser);
11267 eloc = c_parser_peek_token (parser)->location;
11268 expr = c_parser_expr_no_commas (parser, NULL, unfolded_lhs);
11269 rhs1 = expr.value;
11270 switch (TREE_CODE (rhs1))
11271 {
11272 case MULT_EXPR:
11273 case TRUNC_DIV_EXPR:
11274 case PLUS_EXPR:
11275 case MINUS_EXPR:
11276 case LSHIFT_EXPR:
11277 case RSHIFT_EXPR:
11278 case BIT_AND_EXPR:
11279 case BIT_IOR_EXPR:
11280 case BIT_XOR_EXPR:
11281 if (c_tree_equal (TREE_OPERAND (rhs1, 0), unfolded_lhs))
11282 {
11283 opcode = TREE_CODE (rhs1);
11284 rhs = c_fully_fold (TREE_OPERAND (rhs1, 1), false, NULL);
11285 rhs1 = c_fully_fold (TREE_OPERAND (rhs1, 0), false, NULL);
11286 goto stmt_done;
11287 }
11288 if (c_tree_equal (TREE_OPERAND (rhs1, 1), unfolded_lhs))
11289 {
11290 opcode = TREE_CODE (rhs1);
11291 rhs = c_fully_fold (TREE_OPERAND (rhs1, 0), false, NULL);
11292 rhs1 = c_fully_fold (TREE_OPERAND (rhs1, 1), false, NULL);
11293 swapped = !commutative_tree_code (opcode);
11294 goto stmt_done;
11295 }
11296 break;
11297 case ERROR_MARK:
11298 goto saw_error;
11299 default:
11300 break;
11301 }
11302 if (c_parser_peek_token (parser)->type == CPP_SEMICOLON)
11303 {
11304 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
11305 {
11306 code = OMP_ATOMIC_CAPTURE_OLD;
11307 v = lhs;
11308 lhs = NULL_TREE;
11309 expr = default_function_array_read_conversion (eloc, expr);
11310 unfolded_lhs1 = expr.value;
11311 lhs1 = c_fully_fold (unfolded_lhs1, false, NULL);
11312 rhs1 = NULL_TREE;
11313 c_parser_consume_token (parser);
11314 goto restart;
11315 }
11316 if (structured_block)
11317 {
11318 opcode = NOP_EXPR;
11319 expr = default_function_array_read_conversion (eloc, expr);
11320 rhs = c_fully_fold (expr.value, false, NULL);
11321 rhs1 = NULL_TREE;
11322 goto stmt_done;
11323 }
11324 }
11325 c_parser_error (parser, "invalid form of %<#pragma omp atomic%>");
11326 goto saw_error;
11327 default:
11328 c_parser_error (parser,
11329 "invalid operator for %<#pragma omp atomic%>");
11330 goto saw_error;
11331 }
11332
11333 /* Arrange to pass the location of the assignment operator to
11334 c_finish_omp_atomic. */
11335 loc = c_parser_peek_token (parser)->location;
11336 c_parser_consume_token (parser);
11337 eloc = c_parser_peek_token (parser)->location;
11338 expr = c_parser_expression (parser);
11339 expr = default_function_array_read_conversion (eloc, expr);
11340 rhs = expr.value;
11341 rhs = c_fully_fold (rhs, false, NULL);
11342 break;
11343 }
11344 stmt_done:
11345 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
11346 {
11347 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
11348 goto saw_error;
11349 v = c_parser_unary_expression (parser).value;
11350 v = c_fully_fold (v, false, NULL);
11351 if (v == error_mark_node)
11352 goto saw_error;
11353 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
11354 goto saw_error;
11355 eloc = c_parser_peek_token (parser)->location;
11356 expr = c_parser_unary_expression (parser);
11357 lhs1 = expr.value;
11358 expr = default_function_array_read_conversion (eloc, expr);
11359 unfolded_lhs1 = expr.value;
11360 lhs1 = c_fully_fold (lhs1, false, NULL);
11361 if (lhs1 == error_mark_node)
11362 goto saw_error;
11363 }
11364 if (structured_block)
11365 {
11366 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
11367 c_parser_require (parser, CPP_CLOSE_BRACE, "expected %<}%>");
11368 }
11369 done:
11370 if (unfolded_lhs && unfolded_lhs1
11371 && !c_tree_equal (unfolded_lhs, unfolded_lhs1))
11372 {
11373 error ("%<#pragma omp atomic capture%> uses two different "
11374 "expressions for memory");
11375 stmt = error_mark_node;
11376 }
11377 else
11378 stmt = c_finish_omp_atomic (loc, code, opcode, lhs, rhs, v, lhs1, rhs1,
11379 swapped, seq_cst);
11380 if (stmt != error_mark_node)
11381 add_stmt (stmt);
11382
11383 if (!structured_block)
11384 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
11385 }
11386
11387
11388 /* OpenMP 2.5:
11389 # pragma omp barrier new-line
11390 */
11391
11392 static void
11393 c_parser_omp_barrier (c_parser *parser)
11394 {
11395 location_t loc = c_parser_peek_token (parser)->location;
11396 c_parser_consume_pragma (parser);
11397 c_parser_skip_to_pragma_eol (parser);
11398
11399 c_finish_omp_barrier (loc);
11400 }
11401
11402 /* OpenMP 2.5:
11403 # pragma omp critical [(name)] new-line
11404 structured-block
11405
11406 LOC is the location of the #pragma itself. */
11407
11408 static tree
11409 c_parser_omp_critical (location_t loc, c_parser *parser)
11410 {
11411 tree stmt, name = NULL;
11412
11413 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
11414 {
11415 c_parser_consume_token (parser);
11416 if (c_parser_next_token_is (parser, CPP_NAME))
11417 {
11418 name = c_parser_peek_token (parser)->value;
11419 c_parser_consume_token (parser);
11420 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11421 }
11422 else
11423 c_parser_error (parser, "expected identifier");
11424 }
11425 else if (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
11426 c_parser_error (parser, "expected %<(%> or end of line");
11427 c_parser_skip_to_pragma_eol (parser);
11428
11429 stmt = c_parser_omp_structured_block (parser);
11430 return c_finish_omp_critical (loc, stmt, name);
11431 }
11432
11433 /* OpenMP 2.5:
11434 # pragma omp flush flush-vars[opt] new-line
11435
11436 flush-vars:
11437 ( variable-list ) */
11438
11439 static void
11440 c_parser_omp_flush (c_parser *parser)
11441 {
11442 location_t loc = c_parser_peek_token (parser)->location;
11443 c_parser_consume_pragma (parser);
11444 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
11445 c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
11446 else if (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
11447 c_parser_error (parser, "expected %<(%> or end of line");
11448 c_parser_skip_to_pragma_eol (parser);
11449
11450 c_finish_omp_flush (loc);
11451 }
11452
11453 /* Parse the restricted form of the for statement allowed by OpenMP.
11454 The real trick here is to determine the loop control variable early
11455 so that we can push a new decl if necessary to make it private.
11456 LOC is the location of the OMP in "#pragma omp". */
11457
11458 static tree
11459 c_parser_omp_for_loop (location_t loc, c_parser *parser, enum tree_code code,
11460 tree clauses, tree *cclauses)
11461 {
11462 tree decl, cond, incr, save_break, save_cont, body, init, stmt, cl;
11463 tree declv, condv, incrv, initv, ret = NULL;
11464 bool fail = false, open_brace_parsed = false;
11465 int i, collapse = 1, nbraces = 0;
11466 location_t for_loc;
11467 vec<tree, va_gc> *for_block = make_tree_vector ();
11468
11469 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
11470 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
11471 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
11472
11473 gcc_assert (collapse >= 1);
11474
11475 declv = make_tree_vec (collapse);
11476 initv = make_tree_vec (collapse);
11477 condv = make_tree_vec (collapse);
11478 incrv = make_tree_vec (collapse);
11479
11480 if (!c_parser_next_token_is_keyword (parser, RID_FOR))
11481 {
11482 c_parser_error (parser, "for statement expected");
11483 return NULL;
11484 }
11485 for_loc = c_parser_peek_token (parser)->location;
11486 c_parser_consume_token (parser);
11487
11488 for (i = 0; i < collapse; i++)
11489 {
11490 int bracecount = 0;
11491
11492 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11493 goto pop_scopes;
11494
11495 /* Parse the initialization declaration or expression. */
11496 if (c_parser_next_tokens_start_declaration (parser))
11497 {
11498 if (i > 0)
11499 vec_safe_push (for_block, c_begin_compound_stmt (true));
11500 c_parser_declaration_or_fndef (parser, true, true, true, true, true,
11501 NULL, vNULL);
11502 decl = check_for_loop_decls (for_loc, flag_isoc99);
11503 if (decl == NULL)
11504 goto error_init;
11505 if (DECL_INITIAL (decl) == error_mark_node)
11506 decl = error_mark_node;
11507 init = decl;
11508 }
11509 else if (c_parser_next_token_is (parser, CPP_NAME)
11510 && c_parser_peek_2nd_token (parser)->type == CPP_EQ)
11511 {
11512 struct c_expr decl_exp;
11513 struct c_expr init_exp;
11514 location_t init_loc;
11515
11516 decl_exp = c_parser_postfix_expression (parser);
11517 decl = decl_exp.value;
11518
11519 c_parser_require (parser, CPP_EQ, "expected %<=%>");
11520
11521 init_loc = c_parser_peek_token (parser)->location;
11522 init_exp = c_parser_expr_no_commas (parser, NULL);
11523 init_exp = default_function_array_read_conversion (init_loc,
11524 init_exp);
11525 init = build_modify_expr (init_loc, decl, decl_exp.original_type,
11526 NOP_EXPR, init_loc, init_exp.value,
11527 init_exp.original_type);
11528 init = c_process_expr_stmt (init_loc, init);
11529
11530 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
11531 }
11532 else
11533 {
11534 error_init:
11535 c_parser_error (parser,
11536 "expected iteration declaration or initialization");
11537 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
11538 "expected %<)%>");
11539 fail = true;
11540 goto parse_next;
11541 }
11542
11543 /* Parse the loop condition. */
11544 cond = NULL_TREE;
11545 if (c_parser_next_token_is_not (parser, CPP_SEMICOLON))
11546 {
11547 location_t cond_loc = c_parser_peek_token (parser)->location;
11548 struct c_expr cond_expr
11549 = c_parser_binary_expression (parser, NULL, NULL_TREE);
11550
11551 cond = cond_expr.value;
11552 cond = c_objc_common_truthvalue_conversion (cond_loc, cond);
11553 cond = c_fully_fold (cond, false, NULL);
11554 switch (cond_expr.original_code)
11555 {
11556 case GT_EXPR:
11557 case GE_EXPR:
11558 case LT_EXPR:
11559 case LE_EXPR:
11560 break;
11561 case NE_EXPR:
11562 if (code == CILK_SIMD)
11563 break;
11564 /* FALLTHRU. */
11565 default:
11566 /* Can't be cond = error_mark_node, because we want to preserve
11567 the location until c_finish_omp_for. */
11568 cond = build1 (NOP_EXPR, boolean_type_node, error_mark_node);
11569 break;
11570 }
11571 protected_set_expr_location (cond, cond_loc);
11572 }
11573 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
11574
11575 /* Parse the increment expression. */
11576 incr = NULL_TREE;
11577 if (c_parser_next_token_is_not (parser, CPP_CLOSE_PAREN))
11578 {
11579 location_t incr_loc = c_parser_peek_token (parser)->location;
11580
11581 incr = c_process_expr_stmt (incr_loc,
11582 c_parser_expression (parser).value);
11583 }
11584 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11585
11586 if (decl == NULL || decl == error_mark_node || init == error_mark_node)
11587 fail = true;
11588 else
11589 {
11590 TREE_VEC_ELT (declv, i) = decl;
11591 TREE_VEC_ELT (initv, i) = init;
11592 TREE_VEC_ELT (condv, i) = cond;
11593 TREE_VEC_ELT (incrv, i) = incr;
11594 }
11595
11596 parse_next:
11597 if (i == collapse - 1)
11598 break;
11599
11600 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
11601 in between the collapsed for loops to be still considered perfectly
11602 nested. Hopefully the final version clarifies this.
11603 For now handle (multiple) {'s and empty statements. */
11604 do
11605 {
11606 if (c_parser_next_token_is_keyword (parser, RID_FOR))
11607 {
11608 c_parser_consume_token (parser);
11609 break;
11610 }
11611 else if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
11612 {
11613 c_parser_consume_token (parser);
11614 bracecount++;
11615 }
11616 else if (bracecount
11617 && c_parser_next_token_is (parser, CPP_SEMICOLON))
11618 c_parser_consume_token (parser);
11619 else
11620 {
11621 c_parser_error (parser, "not enough perfectly nested loops");
11622 if (bracecount)
11623 {
11624 open_brace_parsed = true;
11625 bracecount--;
11626 }
11627 fail = true;
11628 collapse = 0;
11629 break;
11630 }
11631 }
11632 while (1);
11633
11634 nbraces += bracecount;
11635 }
11636
11637 save_break = c_break_label;
11638 if (code == CILK_SIMD)
11639 c_break_label = build_int_cst (size_type_node, 2);
11640 else
11641 c_break_label = size_one_node;
11642 save_cont = c_cont_label;
11643 c_cont_label = NULL_TREE;
11644 body = push_stmt_list ();
11645
11646 if (open_brace_parsed)
11647 {
11648 location_t here = c_parser_peek_token (parser)->location;
11649 stmt = c_begin_compound_stmt (true);
11650 c_parser_compound_statement_nostart (parser);
11651 add_stmt (c_end_compound_stmt (here, stmt, true));
11652 }
11653 else
11654 add_stmt (c_parser_c99_block_statement (parser));
11655 if (c_cont_label)
11656 {
11657 tree t = build1 (LABEL_EXPR, void_type_node, c_cont_label);
11658 SET_EXPR_LOCATION (t, loc);
11659 add_stmt (t);
11660 }
11661
11662 body = pop_stmt_list (body);
11663 c_break_label = save_break;
11664 c_cont_label = save_cont;
11665
11666 while (nbraces)
11667 {
11668 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
11669 {
11670 c_parser_consume_token (parser);
11671 nbraces--;
11672 }
11673 else if (c_parser_next_token_is (parser, CPP_SEMICOLON))
11674 c_parser_consume_token (parser);
11675 else
11676 {
11677 c_parser_error (parser, "collapsed loops not perfectly nested");
11678 while (nbraces)
11679 {
11680 location_t here = c_parser_peek_token (parser)->location;
11681 stmt = c_begin_compound_stmt (true);
11682 add_stmt (body);
11683 c_parser_compound_statement_nostart (parser);
11684 body = c_end_compound_stmt (here, stmt, true);
11685 nbraces--;
11686 }
11687 goto pop_scopes;
11688 }
11689 }
11690
11691 /* Only bother calling c_finish_omp_for if we haven't already generated
11692 an error from the initialization parsing. */
11693 if (!fail)
11694 {
11695 stmt = c_finish_omp_for (loc, code, declv, initv, condv,
11696 incrv, body, NULL);
11697 if (stmt)
11698 {
11699 if (cclauses != NULL
11700 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL)
11701 {
11702 tree *c;
11703 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
11704 if (OMP_CLAUSE_CODE (*c) != OMP_CLAUSE_FIRSTPRIVATE
11705 && OMP_CLAUSE_CODE (*c) != OMP_CLAUSE_LASTPRIVATE)
11706 c = &OMP_CLAUSE_CHAIN (*c);
11707 else
11708 {
11709 for (i = 0; i < collapse; i++)
11710 if (TREE_VEC_ELT (declv, i) == OMP_CLAUSE_DECL (*c))
11711 break;
11712 if (i == collapse)
11713 c = &OMP_CLAUSE_CHAIN (*c);
11714 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE)
11715 {
11716 error_at (loc,
11717 "iteration variable %qD should not be firstprivate",
11718 OMP_CLAUSE_DECL (*c));
11719 *c = OMP_CLAUSE_CHAIN (*c);
11720 }
11721 else
11722 {
11723 /* Copy lastprivate (decl) clause to OMP_FOR_CLAUSES,
11724 change it to shared (decl) in
11725 OMP_PARALLEL_CLAUSES. */
11726 tree l = build_omp_clause (OMP_CLAUSE_LOCATION (*c),
11727 OMP_CLAUSE_LASTPRIVATE);
11728 OMP_CLAUSE_DECL (l) = OMP_CLAUSE_DECL (*c);
11729 OMP_CLAUSE_CHAIN (l) = clauses;
11730 clauses = l;
11731 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
11732 }
11733 }
11734 }
11735 OMP_FOR_CLAUSES (stmt) = clauses;
11736 }
11737 ret = stmt;
11738 }
11739 pop_scopes:
11740 while (!for_block->is_empty ())
11741 {
11742 /* FIXME diagnostics: LOC below should be the actual location of
11743 this particular for block. We need to build a list of
11744 locations to go along with FOR_BLOCK. */
11745 stmt = c_end_compound_stmt (loc, for_block->pop (), true);
11746 add_stmt (stmt);
11747 }
11748 release_tree_vector (for_block);
11749 return ret;
11750 }
11751
11752 /* Helper function for OpenMP parsing, split clauses and call
11753 finish_omp_clauses on each of the set of clauses afterwards. */
11754
11755 static void
11756 omp_split_clauses (location_t loc, enum tree_code code,
11757 omp_clause_mask mask, tree clauses, tree *cclauses)
11758 {
11759 int i;
11760 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
11761 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
11762 if (cclauses[i])
11763 cclauses[i] = c_finish_omp_clauses (cclauses[i]);
11764 }
11765
11766 /* OpenMP 4.0:
11767 #pragma omp simd simd-clause[optseq] new-line
11768 for-loop
11769
11770 LOC is the location of the #pragma token.
11771 */
11772
11773 #define OMP_SIMD_CLAUSE_MASK \
11774 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
11775 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
11776 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
11777 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
11778 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
11779 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
11780 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
11781
11782 static tree
11783 c_parser_omp_simd (location_t loc, c_parser *parser,
11784 char *p_name, omp_clause_mask mask, tree *cclauses)
11785 {
11786 tree block, clauses, ret;
11787
11788 strcat (p_name, " simd");
11789 mask |= OMP_SIMD_CLAUSE_MASK;
11790 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
11791
11792 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
11793 if (cclauses)
11794 {
11795 omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
11796 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
11797 }
11798
11799 block = c_begin_compound_stmt (true);
11800 ret = c_parser_omp_for_loop (loc, parser, OMP_SIMD, clauses, cclauses);
11801 block = c_end_compound_stmt (loc, block, true);
11802 add_stmt (block);
11803
11804 return ret;
11805 }
11806
11807 /* OpenMP 2.5:
11808 #pragma omp for for-clause[optseq] new-line
11809 for-loop
11810
11811 OpenMP 4.0:
11812 #pragma omp for simd for-simd-clause[optseq] new-line
11813 for-loop
11814
11815 LOC is the location of the #pragma token.
11816 */
11817
11818 #define OMP_FOR_CLAUSE_MASK \
11819 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
11820 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
11821 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
11822 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
11823 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
11824 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
11825 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
11826 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
11827
11828 static tree
11829 c_parser_omp_for (location_t loc, c_parser *parser,
11830 char *p_name, omp_clause_mask mask, tree *cclauses)
11831 {
11832 tree block, clauses, ret;
11833
11834 strcat (p_name, " for");
11835 mask |= OMP_FOR_CLAUSE_MASK;
11836 if (cclauses)
11837 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
11838
11839 if (c_parser_next_token_is (parser, CPP_NAME))
11840 {
11841 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
11842
11843 if (strcmp (p, "simd") == 0)
11844 {
11845 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
11846 if (cclauses == NULL)
11847 cclauses = cclauses_buf;
11848
11849 c_parser_consume_token (parser);
11850 if (!flag_openmp) /* flag_openmp_simd */
11851 return c_parser_omp_simd (loc, parser, p_name, mask, cclauses);
11852 block = c_begin_compound_stmt (true);
11853 ret = c_parser_omp_simd (loc, parser, p_name, mask, cclauses);
11854 block = c_end_compound_stmt (loc, block, true);
11855 if (ret == NULL_TREE)
11856 return ret;
11857 ret = make_node (OMP_FOR);
11858 TREE_TYPE (ret) = void_type_node;
11859 OMP_FOR_BODY (ret) = block;
11860 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
11861 SET_EXPR_LOCATION (ret, loc);
11862 add_stmt (ret);
11863 return ret;
11864 }
11865 }
11866 if (!flag_openmp) /* flag_openmp_simd */
11867 {
11868 c_parser_skip_to_pragma_eol (parser);
11869 return NULL_TREE;
11870 }
11871
11872 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
11873 if (cclauses)
11874 {
11875 omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
11876 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
11877 }
11878
11879 block = c_begin_compound_stmt (true);
11880 ret = c_parser_omp_for_loop (loc, parser, OMP_FOR, clauses, cclauses);
11881 block = c_end_compound_stmt (loc, block, true);
11882 add_stmt (block);
11883
11884 return ret;
11885 }
11886
11887 /* OpenMP 2.5:
11888 # pragma omp master new-line
11889 structured-block
11890
11891 LOC is the location of the #pragma token.
11892 */
11893
11894 static tree
11895 c_parser_omp_master (location_t loc, c_parser *parser)
11896 {
11897 c_parser_skip_to_pragma_eol (parser);
11898 return c_finish_omp_master (loc, c_parser_omp_structured_block (parser));
11899 }
11900
11901 /* OpenMP 2.5:
11902 # pragma omp ordered new-line
11903 structured-block
11904
11905 LOC is the location of the #pragma itself.
11906 */
11907
11908 static tree
11909 c_parser_omp_ordered (location_t loc, c_parser *parser)
11910 {
11911 c_parser_skip_to_pragma_eol (parser);
11912 return c_finish_omp_ordered (loc, c_parser_omp_structured_block (parser));
11913 }
11914
11915 /* OpenMP 2.5:
11916
11917 section-scope:
11918 { section-sequence }
11919
11920 section-sequence:
11921 section-directive[opt] structured-block
11922 section-sequence section-directive structured-block
11923
11924 SECTIONS_LOC is the location of the #pragma omp sections. */
11925
11926 static tree
11927 c_parser_omp_sections_scope (location_t sections_loc, c_parser *parser)
11928 {
11929 tree stmt, substmt;
11930 bool error_suppress = false;
11931 location_t loc;
11932
11933 loc = c_parser_peek_token (parser)->location;
11934 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
11935 {
11936 /* Avoid skipping until the end of the block. */
11937 parser->error = false;
11938 return NULL_TREE;
11939 }
11940
11941 stmt = push_stmt_list ();
11942
11943 if (c_parser_peek_token (parser)->pragma_kind != PRAGMA_OMP_SECTION)
11944 {
11945 substmt = c_parser_omp_structured_block (parser);
11946 substmt = build1 (OMP_SECTION, void_type_node, substmt);
11947 SET_EXPR_LOCATION (substmt, loc);
11948 add_stmt (substmt);
11949 }
11950
11951 while (1)
11952 {
11953 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
11954 break;
11955 if (c_parser_next_token_is (parser, CPP_EOF))
11956 break;
11957
11958 loc = c_parser_peek_token (parser)->location;
11959 if (c_parser_peek_token (parser)->pragma_kind == PRAGMA_OMP_SECTION)
11960 {
11961 c_parser_consume_pragma (parser);
11962 c_parser_skip_to_pragma_eol (parser);
11963 error_suppress = false;
11964 }
11965 else if (!error_suppress)
11966 {
11967 error_at (loc, "expected %<#pragma omp section%> or %<}%>");
11968 error_suppress = true;
11969 }
11970
11971 substmt = c_parser_omp_structured_block (parser);
11972 substmt = build1 (OMP_SECTION, void_type_node, substmt);
11973 SET_EXPR_LOCATION (substmt, loc);
11974 add_stmt (substmt);
11975 }
11976 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE,
11977 "expected %<#pragma omp section%> or %<}%>");
11978
11979 substmt = pop_stmt_list (stmt);
11980
11981 stmt = make_node (OMP_SECTIONS);
11982 SET_EXPR_LOCATION (stmt, sections_loc);
11983 TREE_TYPE (stmt) = void_type_node;
11984 OMP_SECTIONS_BODY (stmt) = substmt;
11985
11986 return add_stmt (stmt);
11987 }
11988
11989 /* OpenMP 2.5:
11990 # pragma omp sections sections-clause[optseq] newline
11991 sections-scope
11992
11993 LOC is the location of the #pragma token.
11994 */
11995
11996 #define OMP_SECTIONS_CLAUSE_MASK \
11997 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
11998 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
11999 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
12000 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
12001 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
12002
12003 static tree
12004 c_parser_omp_sections (location_t loc, c_parser *parser,
12005 char *p_name, omp_clause_mask mask, tree *cclauses)
12006 {
12007 tree block, clauses, ret;
12008
12009 strcat (p_name, " sections");
12010 mask |= OMP_SECTIONS_CLAUSE_MASK;
12011 if (cclauses)
12012 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
12013
12014 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
12015 if (cclauses)
12016 {
12017 omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
12018 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
12019 }
12020
12021 block = c_begin_compound_stmt (true);
12022 ret = c_parser_omp_sections_scope (loc, parser);
12023 if (ret)
12024 OMP_SECTIONS_CLAUSES (ret) = clauses;
12025 block = c_end_compound_stmt (loc, block, true);
12026 add_stmt (block);
12027
12028 return ret;
12029 }
12030
12031 /* OpenMP 2.5:
12032 # pragma parallel parallel-clause new-line
12033 # pragma parallel for parallel-for-clause new-line
12034 # pragma parallel sections parallel-sections-clause new-line
12035
12036 LOC is the location of the #pragma token.
12037 */
12038
12039 #define OMP_PARALLEL_CLAUSE_MASK \
12040 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
12041 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
12042 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
12043 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
12044 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
12045 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
12046 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
12047 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
12048 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
12049
12050 static tree
12051 c_parser_omp_parallel (location_t loc, c_parser *parser,
12052 char *p_name, omp_clause_mask mask, tree *cclauses)
12053 {
12054 tree stmt, clauses, block;
12055
12056 strcat (p_name, " parallel");
12057 mask |= OMP_PARALLEL_CLAUSE_MASK;
12058
12059 if (c_parser_next_token_is_keyword (parser, RID_FOR))
12060 {
12061 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
12062 if (cclauses == NULL)
12063 cclauses = cclauses_buf;
12064
12065 c_parser_consume_token (parser);
12066 if (!flag_openmp) /* flag_openmp_simd */
12067 return c_parser_omp_for (loc, parser, p_name, mask, cclauses);
12068 block = c_begin_omp_parallel ();
12069 c_parser_omp_for (loc, parser, p_name, mask, cclauses);
12070 stmt
12071 = c_finish_omp_parallel (loc, cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
12072 block);
12073 OMP_PARALLEL_COMBINED (stmt) = 1;
12074 return stmt;
12075 }
12076 else if (cclauses)
12077 {
12078 error_at (loc, "expected %<for%> after %qs", p_name);
12079 c_parser_skip_to_pragma_eol (parser);
12080 return NULL_TREE;
12081 }
12082 else if (!flag_openmp) /* flag_openmp_simd */
12083 {
12084 c_parser_skip_to_pragma_eol (parser);
12085 return NULL_TREE;
12086 }
12087 else if (c_parser_next_token_is (parser, CPP_NAME))
12088 {
12089 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
12090 if (strcmp (p, "sections") == 0)
12091 {
12092 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
12093 if (cclauses == NULL)
12094 cclauses = cclauses_buf;
12095
12096 c_parser_consume_token (parser);
12097 block = c_begin_omp_parallel ();
12098 c_parser_omp_sections (loc, parser, p_name, mask, cclauses);
12099 stmt = c_finish_omp_parallel (loc,
12100 cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
12101 block);
12102 OMP_PARALLEL_COMBINED (stmt) = 1;
12103 return stmt;
12104 }
12105 }
12106
12107 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
12108
12109 block = c_begin_omp_parallel ();
12110 c_parser_statement (parser);
12111 stmt = c_finish_omp_parallel (loc, clauses, block);
12112
12113 return stmt;
12114 }
12115
12116 /* OpenMP 2.5:
12117 # pragma omp single single-clause[optseq] new-line
12118 structured-block
12119
12120 LOC is the location of the #pragma.
12121 */
12122
12123 #define OMP_SINGLE_CLAUSE_MASK \
12124 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
12125 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
12126 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
12127 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
12128
12129 static tree
12130 c_parser_omp_single (location_t loc, c_parser *parser)
12131 {
12132 tree stmt = make_node (OMP_SINGLE);
12133 SET_EXPR_LOCATION (stmt, loc);
12134 TREE_TYPE (stmt) = void_type_node;
12135
12136 OMP_SINGLE_CLAUSES (stmt)
12137 = c_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
12138 "#pragma omp single");
12139 OMP_SINGLE_BODY (stmt) = c_parser_omp_structured_block (parser);
12140
12141 return add_stmt (stmt);
12142 }
12143
12144 /* OpenMP 3.0:
12145 # pragma omp task task-clause[optseq] new-line
12146
12147 LOC is the location of the #pragma.
12148 */
12149
12150 #define OMP_TASK_CLAUSE_MASK \
12151 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
12152 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
12153 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
12154 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
12155 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
12156 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
12157 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
12158 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
12159 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND))
12160
12161 static tree
12162 c_parser_omp_task (location_t loc, c_parser *parser)
12163 {
12164 tree clauses, block;
12165
12166 clauses = c_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
12167 "#pragma omp task");
12168
12169 block = c_begin_omp_task ();
12170 c_parser_statement (parser);
12171 return c_finish_omp_task (loc, clauses, block);
12172 }
12173
12174 /* OpenMP 3.0:
12175 # pragma omp taskwait new-line
12176 */
12177
12178 static void
12179 c_parser_omp_taskwait (c_parser *parser)
12180 {
12181 location_t loc = c_parser_peek_token (parser)->location;
12182 c_parser_consume_pragma (parser);
12183 c_parser_skip_to_pragma_eol (parser);
12184
12185 c_finish_omp_taskwait (loc);
12186 }
12187
12188 /* OpenMP 3.1:
12189 # pragma omp taskyield new-line
12190 */
12191
12192 static void
12193 c_parser_omp_taskyield (c_parser *parser)
12194 {
12195 location_t loc = c_parser_peek_token (parser)->location;
12196 c_parser_consume_pragma (parser);
12197 c_parser_skip_to_pragma_eol (parser);
12198
12199 c_finish_omp_taskyield (loc);
12200 }
12201
12202 /* OpenMP 4.0:
12203 # pragma omp taskgroup new-line
12204 */
12205
12206 static tree
12207 c_parser_omp_taskgroup (c_parser *parser)
12208 {
12209 location_t loc = c_parser_peek_token (parser)->location;
12210 c_parser_skip_to_pragma_eol (parser);
12211 return c_finish_omp_taskgroup (loc, c_parser_omp_structured_block (parser));
12212 }
12213
12214 /* OpenMP 4.0:
12215 # pragma omp cancel cancel-clause[optseq] new-line
12216
12217 LOC is the location of the #pragma.
12218 */
12219
12220 #define OMP_CANCEL_CLAUSE_MASK \
12221 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
12222 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
12223 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
12224 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
12225 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
12226
12227 static void
12228 c_parser_omp_cancel (c_parser *parser)
12229 {
12230 location_t loc = c_parser_peek_token (parser)->location;
12231
12232 c_parser_consume_pragma (parser);
12233 tree clauses = c_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
12234 "#pragma omp cancel");
12235
12236 c_finish_omp_cancel (loc, clauses);
12237 }
12238
12239 /* OpenMP 4.0:
12240 # pragma omp cancellation point cancelpt-clause[optseq] new-line
12241
12242 LOC is the location of the #pragma.
12243 */
12244
12245 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
12246 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
12247 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
12248 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
12249 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
12250
12251 static void
12252 c_parser_omp_cancellation_point (c_parser *parser)
12253 {
12254 location_t loc = c_parser_peek_token (parser)->location;
12255 tree clauses;
12256 bool point_seen = false;
12257
12258 c_parser_consume_pragma (parser);
12259 if (c_parser_next_token_is (parser, CPP_NAME))
12260 {
12261 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
12262 if (strcmp (p, "point") == 0)
12263 {
12264 c_parser_consume_token (parser);
12265 point_seen = true;
12266 }
12267 }
12268 if (!point_seen)
12269 {
12270 c_parser_error (parser, "expected %<point%>");
12271 c_parser_skip_to_pragma_eol (parser);
12272 return;
12273 }
12274
12275 clauses
12276 = c_parser_omp_all_clauses (parser, OMP_CANCELLATION_POINT_CLAUSE_MASK,
12277 "#pragma omp cancellation point");
12278
12279 c_finish_omp_cancellation_point (loc, clauses);
12280 }
12281
12282 /* OpenMP 4.0:
12283 #pragma omp distribute distribute-clause[optseq] new-line
12284 for-loop */
12285
12286 #define OMP_DISTRIBUTE_CLAUSE_MASK \
12287 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
12288 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
12289 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
12290 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
12291
12292 static tree
12293 c_parser_omp_distribute (location_t loc, c_parser *parser,
12294 char *p_name, omp_clause_mask mask, tree *cclauses)
12295 {
12296 tree clauses, block, ret;
12297
12298 strcat (p_name, " distribute");
12299 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
12300
12301 if (c_parser_next_token_is (parser, CPP_NAME))
12302 {
12303 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
12304 bool simd = false;
12305 bool parallel = false;
12306
12307 if (strcmp (p, "simd") == 0)
12308 simd = true;
12309 else
12310 parallel = strcmp (p, "parallel") == 0;
12311 if (parallel || simd)
12312 {
12313 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
12314 if (cclauses == NULL)
12315 cclauses = cclauses_buf;
12316 c_parser_consume_token (parser);
12317 if (!flag_openmp) /* flag_openmp_simd */
12318 {
12319 if (simd)
12320 return c_parser_omp_simd (loc, parser, p_name, mask, cclauses);
12321 else
12322 return c_parser_omp_parallel (loc, parser, p_name, mask,
12323 cclauses);
12324 }
12325 block = c_begin_compound_stmt (true);
12326 if (simd)
12327 ret = c_parser_omp_simd (loc, parser, p_name, mask, cclauses);
12328 else
12329 ret = c_parser_omp_parallel (loc, parser, p_name, mask, cclauses);
12330 block = c_end_compound_stmt (loc, block, true);
12331 if (ret == NULL)
12332 return ret;
12333 ret = make_node (OMP_DISTRIBUTE);
12334 TREE_TYPE (ret) = void_type_node;
12335 OMP_FOR_BODY (ret) = block;
12336 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
12337 SET_EXPR_LOCATION (ret, loc);
12338 add_stmt (ret);
12339 return ret;
12340 }
12341 }
12342 if (!flag_openmp) /* flag_openmp_simd */
12343 {
12344 c_parser_skip_to_pragma_eol (parser);
12345 return NULL_TREE;
12346 }
12347
12348 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
12349 if (cclauses)
12350 {
12351 omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
12352 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
12353 }
12354
12355 block = c_begin_compound_stmt (true);
12356 ret = c_parser_omp_for_loop (loc, parser, OMP_DISTRIBUTE, clauses, NULL);
12357 block = c_end_compound_stmt (loc, block, true);
12358 add_stmt (block);
12359
12360 return ret;
12361 }
12362
12363 /* OpenMP 4.0:
12364 # pragma omp teams teams-clause[optseq] new-line
12365 structured-block */
12366
12367 #define OMP_TEAMS_CLAUSE_MASK \
12368 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
12369 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
12370 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
12371 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
12372 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
12373 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
12374 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
12375
12376 static tree
12377 c_parser_omp_teams (location_t loc, c_parser *parser,
12378 char *p_name, omp_clause_mask mask, tree *cclauses)
12379 {
12380 tree clauses, block, ret;
12381
12382 strcat (p_name, " teams");
12383 mask |= OMP_TEAMS_CLAUSE_MASK;
12384
12385 if (c_parser_next_token_is (parser, CPP_NAME))
12386 {
12387 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
12388 if (strcmp (p, "distribute") == 0)
12389 {
12390 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
12391 if (cclauses == NULL)
12392 cclauses = cclauses_buf;
12393
12394 c_parser_consume_token (parser);
12395 if (!flag_openmp) /* flag_openmp_simd */
12396 return c_parser_omp_distribute (loc, parser, p_name, mask, cclauses);
12397 block = c_begin_compound_stmt (true);
12398 ret = c_parser_omp_distribute (loc, parser, p_name, mask, cclauses);
12399 block = c_end_compound_stmt (loc, block, true);
12400 if (ret == NULL)
12401 return ret;
12402 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
12403 ret = make_node (OMP_TEAMS);
12404 TREE_TYPE (ret) = void_type_node;
12405 OMP_TEAMS_CLAUSES (ret) = clauses;
12406 OMP_TEAMS_BODY (ret) = block;
12407 return add_stmt (ret);
12408 }
12409 }
12410 if (!flag_openmp) /* flag_openmp_simd */
12411 {
12412 c_parser_skip_to_pragma_eol (parser);
12413 return NULL_TREE;
12414 }
12415
12416 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
12417 if (cclauses)
12418 {
12419 omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
12420 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
12421 }
12422
12423 tree stmt = make_node (OMP_TEAMS);
12424 TREE_TYPE (stmt) = void_type_node;
12425 OMP_TEAMS_CLAUSES (stmt) = clauses;
12426 OMP_TEAMS_BODY (stmt) = c_parser_omp_structured_block (parser);
12427
12428 return add_stmt (stmt);
12429 }
12430
12431 /* OpenMP 4.0:
12432 # pragma omp target data target-data-clause[optseq] new-line
12433 structured-block */
12434
12435 #define OMP_TARGET_DATA_CLAUSE_MASK \
12436 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
12437 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
12438 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
12439
12440 static tree
12441 c_parser_omp_target_data (location_t loc, c_parser *parser)
12442 {
12443 tree stmt = make_node (OMP_TARGET_DATA);
12444 TREE_TYPE (stmt) = void_type_node;
12445
12446 OMP_TARGET_DATA_CLAUSES (stmt)
12447 = c_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
12448 "#pragma omp target data");
12449 keep_next_level ();
12450 tree block = c_begin_compound_stmt (true);
12451 add_stmt (c_parser_omp_structured_block (parser));
12452 OMP_TARGET_DATA_BODY (stmt) = c_end_compound_stmt (loc, block, true);
12453
12454 SET_EXPR_LOCATION (stmt, loc);
12455 return add_stmt (stmt);
12456 }
12457
12458 /* OpenMP 4.0:
12459 # pragma omp target update target-update-clause[optseq] new-line */
12460
12461 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
12462 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
12463 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
12464 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
12465 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
12466
12467 static bool
12468 c_parser_omp_target_update (location_t loc, c_parser *parser,
12469 enum pragma_context context)
12470 {
12471 if (context == pragma_stmt)
12472 {
12473 error_at (loc,
12474 "%<#pragma omp target update%> may only be "
12475 "used in compound statements");
12476 c_parser_skip_to_pragma_eol (parser);
12477 return false;
12478 }
12479
12480 tree clauses
12481 = c_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
12482 "#pragma omp target update");
12483 if (find_omp_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
12484 && find_omp_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
12485 {
12486 error_at (loc,
12487 "%<#pragma omp target update must contain at least one "
12488 "%<from%> or %<to%> clauses");
12489 return false;
12490 }
12491
12492 tree stmt = make_node (OMP_TARGET_UPDATE);
12493 TREE_TYPE (stmt) = void_type_node;
12494 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
12495 SET_EXPR_LOCATION (stmt, loc);
12496 add_stmt (stmt);
12497 return false;
12498 }
12499
12500 /* OpenMP 4.0:
12501 # pragma omp target target-clause[optseq] new-line
12502 structured-block */
12503
12504 #define OMP_TARGET_CLAUSE_MASK \
12505 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
12506 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
12507 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
12508
12509 static bool
12510 c_parser_omp_target (c_parser *parser, enum pragma_context context)
12511 {
12512 location_t loc = c_parser_peek_token (parser)->location;
12513 c_parser_consume_pragma (parser);
12514
12515 if (context != pragma_stmt && context != pragma_compound)
12516 {
12517 c_parser_error (parser, "expected declaration specifiers");
12518 c_parser_skip_to_pragma_eol (parser);
12519 return false;
12520 }
12521
12522 if (c_parser_next_token_is (parser, CPP_NAME))
12523 {
12524 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
12525
12526 if (strcmp (p, "teams") == 0)
12527 {
12528 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
12529 char p_name[sizeof ("#pragma omp target teams distribute "
12530 "parallel for simd")];
12531
12532 c_parser_consume_token (parser);
12533 if (!flag_openmp) /* flag_openmp_simd */
12534 return c_parser_omp_teams (loc, parser, p_name,
12535 OMP_TARGET_CLAUSE_MASK, cclauses);
12536 strcpy (p_name, "#pragma omp target");
12537 keep_next_level ();
12538 tree block = c_begin_compound_stmt (true);
12539 tree ret = c_parser_omp_teams (loc, parser, p_name,
12540 OMP_TARGET_CLAUSE_MASK, cclauses);
12541 block = c_end_compound_stmt (loc, block, true);
12542 if (ret == NULL)
12543 return ret;
12544 tree stmt = make_node (OMP_TARGET);
12545 TREE_TYPE (stmt) = void_type_node;
12546 OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
12547 OMP_TARGET_BODY (stmt) = block;
12548 add_stmt (stmt);
12549 return true;
12550 }
12551 else if (!flag_openmp) /* flag_openmp_simd */
12552 {
12553 c_parser_skip_to_pragma_eol (parser);
12554 return NULL_TREE;
12555 }
12556 else if (strcmp (p, "data") == 0)
12557 {
12558 c_parser_consume_token (parser);
12559 c_parser_omp_target_data (loc, parser);
12560 return true;
12561 }
12562 else if (strcmp (p, "update") == 0)
12563 {
12564 c_parser_consume_token (parser);
12565 return c_parser_omp_target_update (loc, parser, context);
12566 }
12567 }
12568
12569 tree stmt = make_node (OMP_TARGET);
12570 TREE_TYPE (stmt) = void_type_node;
12571
12572 OMP_TARGET_CLAUSES (stmt)
12573 = c_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
12574 "#pragma omp target");
12575 keep_next_level ();
12576 tree block = c_begin_compound_stmt (true);
12577 add_stmt (c_parser_omp_structured_block (parser));
12578 OMP_TARGET_BODY (stmt) = c_end_compound_stmt (loc, block, true);
12579
12580 SET_EXPR_LOCATION (stmt, loc);
12581 add_stmt (stmt);
12582 return true;
12583 }
12584
12585 /* OpenMP 4.0:
12586 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
12587
12588 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
12589 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
12590 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
12591 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
12592 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
12593 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
12594 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
12595
12596 static void
12597 c_parser_omp_declare_simd (c_parser *parser, enum pragma_context context)
12598 {
12599 vec<c_token> clauses = vNULL;
12600 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
12601 {
12602 c_token *token = c_parser_peek_token (parser);
12603 if (token->type == CPP_EOF)
12604 {
12605 c_parser_skip_to_pragma_eol (parser);
12606 clauses.release ();
12607 return;
12608 }
12609 clauses.safe_push (*token);
12610 c_parser_consume_token (parser);
12611 }
12612 clauses.safe_push (*c_parser_peek_token (parser));
12613 c_parser_skip_to_pragma_eol (parser);
12614
12615 while (c_parser_next_token_is (parser, CPP_PRAGMA))
12616 {
12617 if (c_parser_peek_token (parser)->pragma_kind
12618 != PRAGMA_OMP_DECLARE_REDUCTION
12619 || c_parser_peek_2nd_token (parser)->type != CPP_NAME
12620 || strcmp (IDENTIFIER_POINTER
12621 (c_parser_peek_2nd_token (parser)->value),
12622 "simd") != 0)
12623 {
12624 c_parser_error (parser,
12625 "%<#pragma omp declare simd%> must be followed by "
12626 "function declaration or definition or another "
12627 "%<#pragma omp declare simd%>");
12628 clauses.release ();
12629 return;
12630 }
12631 c_parser_consume_pragma (parser);
12632 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
12633 {
12634 c_token *token = c_parser_peek_token (parser);
12635 if (token->type == CPP_EOF)
12636 {
12637 c_parser_skip_to_pragma_eol (parser);
12638 clauses.release ();
12639 return;
12640 }
12641 clauses.safe_push (*token);
12642 c_parser_consume_token (parser);
12643 }
12644 clauses.safe_push (*c_parser_peek_token (parser));
12645 c_parser_skip_to_pragma_eol (parser);
12646 }
12647
12648 /* Make sure nothing tries to read past the end of the tokens. */
12649 c_token eof_token;
12650 memset (&eof_token, 0, sizeof (eof_token));
12651 eof_token.type = CPP_EOF;
12652 clauses.safe_push (eof_token);
12653 clauses.safe_push (eof_token);
12654
12655 switch (context)
12656 {
12657 case pragma_external:
12658 if (c_parser_next_token_is (parser, CPP_KEYWORD)
12659 && c_parser_peek_token (parser)->keyword == RID_EXTENSION)
12660 {
12661 int ext = disable_extension_diagnostics ();
12662 do
12663 c_parser_consume_token (parser);
12664 while (c_parser_next_token_is (parser, CPP_KEYWORD)
12665 && c_parser_peek_token (parser)->keyword == RID_EXTENSION);
12666 c_parser_declaration_or_fndef (parser, true, true, true, false, true,
12667 NULL, clauses);
12668 restore_extension_diagnostics (ext);
12669 }
12670 else
12671 c_parser_declaration_or_fndef (parser, true, true, true, false, true,
12672 NULL, clauses);
12673 break;
12674 case pragma_struct:
12675 case pragma_param:
12676 c_parser_error (parser, "%<#pragma omp declare simd%> must be followed by "
12677 "function declaration or definition");
12678 break;
12679 case pragma_compound:
12680 case pragma_stmt:
12681 if (c_parser_next_token_is (parser, CPP_KEYWORD)
12682 && c_parser_peek_token (parser)->keyword == RID_EXTENSION)
12683 {
12684 int ext = disable_extension_diagnostics ();
12685 do
12686 c_parser_consume_token (parser);
12687 while (c_parser_next_token_is (parser, CPP_KEYWORD)
12688 && c_parser_peek_token (parser)->keyword == RID_EXTENSION);
12689 if (c_parser_next_tokens_start_declaration (parser))
12690 {
12691 c_parser_declaration_or_fndef (parser, true, true, true, true,
12692 true, NULL, clauses);
12693 restore_extension_diagnostics (ext);
12694 break;
12695 }
12696 restore_extension_diagnostics (ext);
12697 }
12698 else if (c_parser_next_tokens_start_declaration (parser))
12699 {
12700 c_parser_declaration_or_fndef (parser, true, true, true, true, true,
12701 NULL, clauses);
12702 break;
12703 }
12704 c_parser_error (parser, "%<#pragma omp declare simd%> must be followed by "
12705 "function declaration or definition");
12706 break;
12707 default:
12708 gcc_unreachable ();
12709 }
12710 clauses.release ();
12711 }
12712
12713 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
12714 and put that into "omp declare simd" attribute. */
12715
12716 static void
12717 c_finish_omp_declare_simd (c_parser *parser, tree fndecl, tree parms,
12718 vec<c_token> clauses)
12719 {
12720 /* Normally first token is CPP_NAME "simd". CPP_EOF there indicates
12721 error has been reported and CPP_PRAGMA that c_finish_omp_declare_simd
12722 has already processed the tokens. */
12723 if (clauses[0].type == CPP_EOF)
12724 return;
12725 if (fndecl == NULL_TREE || TREE_CODE (fndecl) != FUNCTION_DECL)
12726 {
12727 error ("%<#pragma omp declare simd%> not immediately followed by "
12728 "a function declaration or definition");
12729 clauses[0].type = CPP_EOF;
12730 return;
12731 }
12732 if (clauses[0].type != CPP_NAME)
12733 {
12734 error_at (DECL_SOURCE_LOCATION (fndecl),
12735 "%<#pragma omp declare simd%> not immediately followed by "
12736 "a single function declaration or definition");
12737 clauses[0].type = CPP_EOF;
12738 return;
12739 }
12740
12741 if (parms == NULL_TREE)
12742 parms = DECL_ARGUMENTS (fndecl);
12743
12744 unsigned int tokens_avail = parser->tokens_avail;
12745 gcc_assert (parser->tokens == &parser->tokens_buf[0]);
12746 parser->tokens = clauses.address ();
12747 parser->tokens_avail = clauses.length ();
12748
12749 /* c_parser_omp_declare_simd pushed 2 extra CPP_EOF tokens at the end. */
12750 while (parser->tokens_avail > 3)
12751 {
12752 c_token *token = c_parser_peek_token (parser);
12753 gcc_assert (token->type == CPP_NAME
12754 && strcmp (IDENTIFIER_POINTER (token->value), "simd") == 0);
12755 c_parser_consume_token (parser);
12756 parser->in_pragma = true;
12757
12758 tree c = c_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
12759 "#pragma omp declare simd");
12760 c = c_omp_declare_simd_clauses_to_numbers (parms, c);
12761 if (c != NULL_TREE)
12762 c = tree_cons (NULL_TREE, c, NULL_TREE);
12763 c = build_tree_list (get_identifier ("omp declare simd"), c);
12764 TREE_CHAIN (c) = DECL_ATTRIBUTES (fndecl);
12765 DECL_ATTRIBUTES (fndecl) = c;
12766 }
12767
12768 parser->tokens = &parser->tokens_buf[0];
12769 parser->tokens_avail = tokens_avail;
12770 clauses[0].type = CPP_PRAGMA;
12771 }
12772
12773
12774 /* OpenMP 4.0:
12775 # pragma omp declare target new-line
12776 declarations and definitions
12777 # pragma omp end declare target new-line */
12778
12779 static void
12780 c_parser_omp_declare_target (c_parser *parser)
12781 {
12782 c_parser_skip_to_pragma_eol (parser);
12783 current_omp_declare_target_attribute++;
12784 }
12785
12786 static void
12787 c_parser_omp_end_declare_target (c_parser *parser)
12788 {
12789 location_t loc = c_parser_peek_token (parser)->location;
12790 c_parser_consume_pragma (parser);
12791 if (c_parser_next_token_is (parser, CPP_NAME)
12792 && strcmp (IDENTIFIER_POINTER (c_parser_peek_token (parser)->value),
12793 "declare") == 0)
12794 {
12795 c_parser_consume_token (parser);
12796 if (c_parser_next_token_is (parser, CPP_NAME)
12797 && strcmp (IDENTIFIER_POINTER (c_parser_peek_token (parser)->value),
12798 "target") == 0)
12799 c_parser_consume_token (parser);
12800 else
12801 {
12802 c_parser_error (parser, "expected %<target%>");
12803 c_parser_skip_to_pragma_eol (parser);
12804 return;
12805 }
12806 }
12807 else
12808 {
12809 c_parser_error (parser, "expected %<declare%>");
12810 c_parser_skip_to_pragma_eol (parser);
12811 return;
12812 }
12813 c_parser_skip_to_pragma_eol (parser);
12814 if (!current_omp_declare_target_attribute)
12815 error_at (loc, "%<#pragma omp end declare target%> without corresponding "
12816 "%<#pragma omp declare target%>");
12817 else
12818 current_omp_declare_target_attribute--;
12819 }
12820
12821
12822 /* OpenMP 4.0
12823 #pragma omp declare reduction (reduction-id : typename-list : expression) \
12824 initializer-clause[opt] new-line
12825
12826 initializer-clause:
12827 initializer (omp_priv = initializer)
12828 initializer (function-name (argument-list)) */
12829
12830 static void
12831 c_parser_omp_declare_reduction (c_parser *parser, enum pragma_context context)
12832 {
12833 unsigned int tokens_avail = 0, i;
12834 vec<tree> types = vNULL;
12835 vec<c_token> clauses = vNULL;
12836 enum tree_code reduc_code = ERROR_MARK;
12837 tree reduc_id = NULL_TREE;
12838 tree type;
12839 location_t rloc = c_parser_peek_token (parser)->location;
12840
12841 if (context == pragma_struct || context == pragma_param)
12842 {
12843 error ("%<#pragma omp declare reduction%> not at file or block scope");
12844 goto fail;
12845 }
12846
12847 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
12848 goto fail;
12849
12850 switch (c_parser_peek_token (parser)->type)
12851 {
12852 case CPP_PLUS:
12853 reduc_code = PLUS_EXPR;
12854 break;
12855 case CPP_MULT:
12856 reduc_code = MULT_EXPR;
12857 break;
12858 case CPP_MINUS:
12859 reduc_code = MINUS_EXPR;
12860 break;
12861 case CPP_AND:
12862 reduc_code = BIT_AND_EXPR;
12863 break;
12864 case CPP_XOR:
12865 reduc_code = BIT_XOR_EXPR;
12866 break;
12867 case CPP_OR:
12868 reduc_code = BIT_IOR_EXPR;
12869 break;
12870 case CPP_AND_AND:
12871 reduc_code = TRUTH_ANDIF_EXPR;
12872 break;
12873 case CPP_OR_OR:
12874 reduc_code = TRUTH_ORIF_EXPR;
12875 break;
12876 case CPP_NAME:
12877 const char *p;
12878 p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
12879 if (strcmp (p, "min") == 0)
12880 {
12881 reduc_code = MIN_EXPR;
12882 break;
12883 }
12884 if (strcmp (p, "max") == 0)
12885 {
12886 reduc_code = MAX_EXPR;
12887 break;
12888 }
12889 reduc_id = c_parser_peek_token (parser)->value;
12890 break;
12891 default:
12892 c_parser_error (parser,
12893 "expected %<+%>, %<*%>, %<-%>, %<&%>, "
12894 "%<^%>, %<|%>, %<&&%>, %<||%>, %<min%> or identifier");
12895 goto fail;
12896 }
12897
12898 tree orig_reduc_id, reduc_decl;
12899 orig_reduc_id = reduc_id;
12900 reduc_id = c_omp_reduction_id (reduc_code, reduc_id);
12901 reduc_decl = c_omp_reduction_decl (reduc_id);
12902 c_parser_consume_token (parser);
12903
12904 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
12905 goto fail;
12906
12907 while (true)
12908 {
12909 location_t loc = c_parser_peek_token (parser)->location;
12910 struct c_type_name *ctype = c_parser_type_name (parser);
12911 if (ctype != NULL)
12912 {
12913 type = groktypename (ctype, NULL, NULL);
12914 if (type == error_mark_node)
12915 ;
12916 else if ((INTEGRAL_TYPE_P (type)
12917 || TREE_CODE (type) == REAL_TYPE
12918 || TREE_CODE (type) == COMPLEX_TYPE)
12919 && orig_reduc_id == NULL_TREE)
12920 error_at (loc, "predeclared arithmetic type in "
12921 "%<#pragma omp declare reduction%>");
12922 else if (TREE_CODE (type) == FUNCTION_TYPE
12923 || TREE_CODE (type) == ARRAY_TYPE)
12924 error_at (loc, "function or array type in "
12925 "%<#pragma omp declare reduction%>");
12926 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
12927 error_at (loc, "const, volatile or restrict qualified type in "
12928 "%<#pragma omp declare reduction%>");
12929 else
12930 {
12931 tree t;
12932 for (t = DECL_INITIAL (reduc_decl); t; t = TREE_CHAIN (t))
12933 if (comptypes (TREE_PURPOSE (t), type))
12934 {
12935 error_at (loc, "redeclaration of %qs "
12936 "%<#pragma omp declare reduction%> for "
12937 "type %qT",
12938 IDENTIFIER_POINTER (reduc_id)
12939 + sizeof ("omp declare reduction ") - 1,
12940 type);
12941 location_t ploc
12942 = DECL_SOURCE_LOCATION (TREE_VEC_ELT (TREE_VALUE (t),
12943 0));
12944 error_at (ploc, "previous %<#pragma omp declare "
12945 "reduction%>");
12946 break;
12947 }
12948 if (t == NULL_TREE)
12949 types.safe_push (type);
12950 }
12951 if (c_parser_next_token_is (parser, CPP_COMMA))
12952 c_parser_consume_token (parser);
12953 else
12954 break;
12955 }
12956 else
12957 break;
12958 }
12959
12960 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>")
12961 || types.is_empty ())
12962 {
12963 fail:
12964 clauses.release ();
12965 types.release ();
12966 while (true)
12967 {
12968 c_token *token = c_parser_peek_token (parser);
12969 if (token->type == CPP_EOF || token->type == CPP_PRAGMA_EOL)
12970 break;
12971 c_parser_consume_token (parser);
12972 }
12973 c_parser_skip_to_pragma_eol (parser);
12974 return;
12975 }
12976
12977 if (types.length () > 1)
12978 {
12979 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
12980 {
12981 c_token *token = c_parser_peek_token (parser);
12982 if (token->type == CPP_EOF)
12983 goto fail;
12984 clauses.safe_push (*token);
12985 c_parser_consume_token (parser);
12986 }
12987 clauses.safe_push (*c_parser_peek_token (parser));
12988 c_parser_skip_to_pragma_eol (parser);
12989
12990 /* Make sure nothing tries to read past the end of the tokens. */
12991 c_token eof_token;
12992 memset (&eof_token, 0, sizeof (eof_token));
12993 eof_token.type = CPP_EOF;
12994 clauses.safe_push (eof_token);
12995 clauses.safe_push (eof_token);
12996 }
12997
12998 int errs = errorcount;
12999 FOR_EACH_VEC_ELT (types, i, type)
13000 {
13001 tokens_avail = parser->tokens_avail;
13002 gcc_assert (parser->tokens == &parser->tokens_buf[0]);
13003 if (!clauses.is_empty ())
13004 {
13005 parser->tokens = clauses.address ();
13006 parser->tokens_avail = clauses.length ();
13007 parser->in_pragma = true;
13008 }
13009
13010 bool nested = current_function_decl != NULL_TREE;
13011 if (nested)
13012 c_push_function_context ();
13013 tree fndecl = build_decl (BUILTINS_LOCATION, FUNCTION_DECL,
13014 reduc_id, default_function_type);
13015 current_function_decl = fndecl;
13016 allocate_struct_function (fndecl, true);
13017 push_scope ();
13018 tree stmt = push_stmt_list ();
13019 /* Intentionally BUILTINS_LOCATION, so that -Wshadow doesn't
13020 warn about these. */
13021 tree omp_out = build_decl (BUILTINS_LOCATION, VAR_DECL,
13022 get_identifier ("omp_out"), type);
13023 DECL_ARTIFICIAL (omp_out) = 1;
13024 DECL_CONTEXT (omp_out) = fndecl;
13025 pushdecl (omp_out);
13026 tree omp_in = build_decl (BUILTINS_LOCATION, VAR_DECL,
13027 get_identifier ("omp_in"), type);
13028 DECL_ARTIFICIAL (omp_in) = 1;
13029 DECL_CONTEXT (omp_in) = fndecl;
13030 pushdecl (omp_in);
13031 struct c_expr combiner = c_parser_expression (parser);
13032 struct c_expr initializer;
13033 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE;
13034 bool bad = false;
13035 initializer.value = error_mark_node;
13036 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
13037 bad = true;
13038 else if (c_parser_next_token_is (parser, CPP_NAME)
13039 && strcmp (IDENTIFIER_POINTER
13040 (c_parser_peek_token (parser)->value),
13041 "initializer") == 0)
13042 {
13043 c_parser_consume_token (parser);
13044 pop_scope ();
13045 push_scope ();
13046 omp_priv = build_decl (BUILTINS_LOCATION, VAR_DECL,
13047 get_identifier ("omp_priv"), type);
13048 DECL_ARTIFICIAL (omp_priv) = 1;
13049 DECL_INITIAL (omp_priv) = error_mark_node;
13050 DECL_CONTEXT (omp_priv) = fndecl;
13051 pushdecl (omp_priv);
13052 omp_orig = build_decl (BUILTINS_LOCATION, VAR_DECL,
13053 get_identifier ("omp_orig"), type);
13054 DECL_ARTIFICIAL (omp_orig) = 1;
13055 DECL_CONTEXT (omp_orig) = fndecl;
13056 pushdecl (omp_orig);
13057 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
13058 bad = true;
13059 else if (!c_parser_next_token_is (parser, CPP_NAME))
13060 {
13061 c_parser_error (parser, "expected %<omp_priv%> or "
13062 "function-name");
13063 bad = true;
13064 }
13065 else if (strcmp (IDENTIFIER_POINTER
13066 (c_parser_peek_token (parser)->value),
13067 "omp_priv") != 0)
13068 {
13069 if (c_parser_peek_2nd_token (parser)->type != CPP_OPEN_PAREN
13070 || c_parser_peek_token (parser)->id_kind != C_ID_ID)
13071 {
13072 c_parser_error (parser, "expected function-name %<(%>");
13073 bad = true;
13074 }
13075 else
13076 initializer = c_parser_postfix_expression (parser);
13077 if (initializer.value
13078 && TREE_CODE (initializer.value) == CALL_EXPR)
13079 {
13080 int j;
13081 tree c = initializer.value;
13082 for (j = 0; j < call_expr_nargs (c); j++)
13083 if (TREE_CODE (CALL_EXPR_ARG (c, j)) == ADDR_EXPR
13084 && TREE_OPERAND (CALL_EXPR_ARG (c, j), 0) == omp_priv)
13085 break;
13086 if (j == call_expr_nargs (c))
13087 error ("one of the initializer call arguments should be "
13088 "%<&omp_priv%>");
13089 }
13090 }
13091 else
13092 {
13093 c_parser_consume_token (parser);
13094 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
13095 bad = true;
13096 else
13097 {
13098 tree st = push_stmt_list ();
13099 start_init (omp_priv, NULL_TREE, 0);
13100 location_t loc = c_parser_peek_token (parser)->location;
13101 struct c_expr init = c_parser_initializer (parser);
13102 finish_init ();
13103 finish_decl (omp_priv, loc, init.value,
13104 init.original_type, NULL_TREE);
13105 pop_stmt_list (st);
13106 }
13107 }
13108 if (!bad
13109 && !c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
13110 bad = true;
13111 }
13112
13113 if (!bad)
13114 {
13115 c_parser_skip_to_pragma_eol (parser);
13116
13117 tree t = tree_cons (type, make_tree_vec (omp_priv ? 6 : 3),
13118 DECL_INITIAL (reduc_decl));
13119 DECL_INITIAL (reduc_decl) = t;
13120 DECL_SOURCE_LOCATION (omp_out) = rloc;
13121 TREE_VEC_ELT (TREE_VALUE (t), 0) = omp_out;
13122 TREE_VEC_ELT (TREE_VALUE (t), 1) = omp_in;
13123 TREE_VEC_ELT (TREE_VALUE (t), 2) = combiner.value;
13124 walk_tree (&combiner.value, c_check_omp_declare_reduction_r,
13125 &TREE_VEC_ELT (TREE_VALUE (t), 0), NULL);
13126 if (omp_priv)
13127 {
13128 DECL_SOURCE_LOCATION (omp_priv) = rloc;
13129 TREE_VEC_ELT (TREE_VALUE (t), 3) = omp_priv;
13130 TREE_VEC_ELT (TREE_VALUE (t), 4) = omp_orig;
13131 TREE_VEC_ELT (TREE_VALUE (t), 5) = initializer.value;
13132 walk_tree (&initializer.value, c_check_omp_declare_reduction_r,
13133 &TREE_VEC_ELT (TREE_VALUE (t), 3), NULL);
13134 walk_tree (&DECL_INITIAL (omp_priv),
13135 c_check_omp_declare_reduction_r,
13136 &TREE_VEC_ELT (TREE_VALUE (t), 3), NULL);
13137 }
13138 }
13139
13140 pop_stmt_list (stmt);
13141 pop_scope ();
13142 if (cfun->language != NULL)
13143 {
13144 ggc_free (cfun->language);
13145 cfun->language = NULL;
13146 }
13147 set_cfun (NULL);
13148 current_function_decl = NULL_TREE;
13149 if (nested)
13150 c_pop_function_context ();
13151
13152 if (!clauses.is_empty ())
13153 {
13154 parser->tokens = &parser->tokens_buf[0];
13155 parser->tokens_avail = tokens_avail;
13156 }
13157 if (bad)
13158 goto fail;
13159 if (errs != errorcount)
13160 break;
13161 }
13162
13163 clauses.release ();
13164 types.release ();
13165 }
13166
13167
13168 /* OpenMP 4.0
13169 #pragma omp declare simd declare-simd-clauses[optseq] new-line
13170 #pragma omp declare reduction (reduction-id : typename-list : expression) \
13171 initializer-clause[opt] new-line
13172 #pragma omp declare target new-line */
13173
13174 static void
13175 c_parser_omp_declare (c_parser *parser, enum pragma_context context)
13176 {
13177 c_parser_consume_pragma (parser);
13178 if (c_parser_next_token_is (parser, CPP_NAME))
13179 {
13180 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
13181 if (strcmp (p, "simd") == 0)
13182 {
13183 /* c_parser_consume_token (parser); done in
13184 c_parser_omp_declare_simd. */
13185 c_parser_omp_declare_simd (parser, context);
13186 return;
13187 }
13188 if (strcmp (p, "reduction") == 0)
13189 {
13190 c_parser_consume_token (parser);
13191 c_parser_omp_declare_reduction (parser, context);
13192 return;
13193 }
13194 if (!flag_openmp) /* flag_openmp_simd */
13195 {
13196 c_parser_skip_to_pragma_eol (parser);
13197 return;
13198 }
13199 if (strcmp (p, "target") == 0)
13200 {
13201 c_parser_consume_token (parser);
13202 c_parser_omp_declare_target (parser);
13203 return;
13204 }
13205 }
13206
13207 c_parser_error (parser, "expected %<simd%> or %<reduction%> "
13208 "or %<target%>");
13209 c_parser_skip_to_pragma_eol (parser);
13210 }
13211
13212 /* Main entry point to parsing most OpenMP pragmas. */
13213
13214 static void
13215 c_parser_omp_construct (c_parser *parser)
13216 {
13217 enum pragma_kind p_kind;
13218 location_t loc;
13219 tree stmt;
13220 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
13221 omp_clause_mask mask (0);
13222
13223 loc = c_parser_peek_token (parser)->location;
13224 p_kind = c_parser_peek_token (parser)->pragma_kind;
13225 c_parser_consume_pragma (parser);
13226
13227 switch (p_kind)
13228 {
13229 case PRAGMA_OMP_ATOMIC:
13230 c_parser_omp_atomic (loc, parser);
13231 return;
13232 case PRAGMA_OMP_CRITICAL:
13233 stmt = c_parser_omp_critical (loc, parser);
13234 break;
13235 case PRAGMA_OMP_DISTRIBUTE:
13236 strcpy (p_name, "#pragma omp");
13237 stmt = c_parser_omp_distribute (loc, parser, p_name, mask, NULL);
13238 break;
13239 case PRAGMA_OMP_FOR:
13240 strcpy (p_name, "#pragma omp");
13241 stmt = c_parser_omp_for (loc, parser, p_name, mask, NULL);
13242 break;
13243 case PRAGMA_OMP_MASTER:
13244 stmt = c_parser_omp_master (loc, parser);
13245 break;
13246 case PRAGMA_OMP_ORDERED:
13247 stmt = c_parser_omp_ordered (loc, parser);
13248 break;
13249 case PRAGMA_OMP_PARALLEL:
13250 strcpy (p_name, "#pragma omp");
13251 stmt = c_parser_omp_parallel (loc, parser, p_name, mask, NULL);
13252 break;
13253 case PRAGMA_OMP_SECTIONS:
13254 strcpy (p_name, "#pragma omp");
13255 stmt = c_parser_omp_sections (loc, parser, p_name, mask, NULL);
13256 break;
13257 case PRAGMA_OMP_SIMD:
13258 strcpy (p_name, "#pragma omp");
13259 stmt = c_parser_omp_simd (loc, parser, p_name, mask, NULL);
13260 break;
13261 case PRAGMA_OMP_SINGLE:
13262 stmt = c_parser_omp_single (loc, parser);
13263 break;
13264 case PRAGMA_OMP_TASK:
13265 stmt = c_parser_omp_task (loc, parser);
13266 break;
13267 case PRAGMA_OMP_TASKGROUP:
13268 stmt = c_parser_omp_taskgroup (parser);
13269 break;
13270 case PRAGMA_OMP_TEAMS:
13271 strcpy (p_name, "#pragma omp");
13272 stmt = c_parser_omp_teams (loc, parser, p_name, mask, NULL);
13273 break;
13274 default:
13275 gcc_unreachable ();
13276 }
13277
13278 if (stmt)
13279 gcc_assert (EXPR_LOCATION (stmt) != UNKNOWN_LOCATION);
13280 }
13281
13282
13283 /* OpenMP 2.5:
13284 # pragma omp threadprivate (variable-list) */
13285
13286 static void
13287 c_parser_omp_threadprivate (c_parser *parser)
13288 {
13289 tree vars, t;
13290 location_t loc;
13291
13292 c_parser_consume_pragma (parser);
13293 loc = c_parser_peek_token (parser)->location;
13294 vars = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
13295
13296 /* Mark every variable in VARS to be assigned thread local storage. */
13297 for (t = vars; t; t = TREE_CHAIN (t))
13298 {
13299 tree v = TREE_PURPOSE (t);
13300
13301 /* FIXME diagnostics: Ideally we should keep individual
13302 locations for all the variables in the var list to make the
13303 following errors more precise. Perhaps
13304 c_parser_omp_var_list_parens() should construct a list of
13305 locations to go along with the var list. */
13306
13307 /* If V had already been marked threadprivate, it doesn't matter
13308 whether it had been used prior to this point. */
13309 if (TREE_CODE (v) != VAR_DECL)
13310 error_at (loc, "%qD is not a variable", v);
13311 else if (TREE_USED (v) && !C_DECL_THREADPRIVATE_P (v))
13312 error_at (loc, "%qE declared %<threadprivate%> after first use", v);
13313 else if (! TREE_STATIC (v) && ! DECL_EXTERNAL (v))
13314 error_at (loc, "automatic variable %qE cannot be %<threadprivate%>", v);
13315 else if (TREE_TYPE (v) == error_mark_node)
13316 ;
13317 else if (! COMPLETE_TYPE_P (TREE_TYPE (v)))
13318 error_at (loc, "%<threadprivate%> %qE has incomplete type", v);
13319 else
13320 {
13321 if (! DECL_THREAD_LOCAL_P (v))
13322 {
13323 DECL_TLS_MODEL (v) = decl_default_tls_model (v);
13324 /* If rtl has been already set for this var, call
13325 make_decl_rtl once again, so that encode_section_info
13326 has a chance to look at the new decl flags. */
13327 if (DECL_RTL_SET_P (v))
13328 make_decl_rtl (v);
13329 }
13330 C_DECL_THREADPRIVATE_P (v) = 1;
13331 }
13332 }
13333
13334 c_parser_skip_to_pragma_eol (parser);
13335 }
13336 \f
13337 /* Cilk Plus <#pragma simd> parsing routines. */
13338
13339 /* Helper function for c_parser_pragma. Perform some sanity checking
13340 for <#pragma simd> constructs. Returns FALSE if there was a
13341 problem. */
13342
13343 static bool
13344 c_parser_cilk_verify_simd (c_parser *parser,
13345 enum pragma_context context)
13346 {
13347 if (!flag_enable_cilkplus)
13348 {
13349 warning (0, "pragma simd ignored because -fcilkplus is not enabled");
13350 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
13351 return false;
13352 }
13353 if (context == pragma_external)
13354 {
13355 c_parser_error (parser,"pragma simd must be inside a function");
13356 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
13357 return false;
13358 }
13359 return true;
13360 }
13361
13362 /* Cilk Plus:
13363 vectorlength ( constant-expression ) */
13364
13365 static tree
13366 c_parser_cilk_clause_vectorlength (c_parser *parser, tree clauses)
13367 {
13368 /* The vectorlength clause behaves exactly like OpenMP's safelen
13369 clause. Represent it in OpenMP terms. */
13370 check_no_duplicate_clause (clauses, OMP_CLAUSE_SAFELEN, "vectorlength");
13371
13372 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
13373 return clauses;
13374
13375 location_t loc = c_parser_peek_token (parser)->location;
13376 tree expr = c_parser_expr_no_commas (parser, NULL).value;
13377 expr = c_fully_fold (expr, false, NULL);
13378
13379 if (!TREE_TYPE (expr)
13380 || !TREE_CONSTANT (expr)
13381 || !INTEGRAL_TYPE_P (TREE_TYPE (expr)))
13382 error_at (loc, "vectorlength must be an integer constant");
13383 else if (exact_log2 (TREE_INT_CST_LOW (expr)) == -1)
13384 error_at (loc, "vectorlength must be a power of 2");
13385 else
13386 {
13387 tree u = build_omp_clause (loc, OMP_CLAUSE_SAFELEN);
13388 OMP_CLAUSE_SAFELEN_EXPR (u) = expr;
13389 OMP_CLAUSE_CHAIN (u) = clauses;
13390 clauses = u;
13391 }
13392
13393 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
13394
13395 return clauses;
13396 }
13397
13398 /* Cilk Plus:
13399 linear ( simd-linear-variable-list )
13400
13401 simd-linear-variable-list:
13402 simd-linear-variable
13403 simd-linear-variable-list , simd-linear-variable
13404
13405 simd-linear-variable:
13406 id-expression
13407 id-expression : simd-linear-step
13408
13409 simd-linear-step:
13410 conditional-expression */
13411
13412 static tree
13413 c_parser_cilk_clause_linear (c_parser *parser, tree clauses)
13414 {
13415 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
13416 return clauses;
13417
13418 location_t loc = c_parser_peek_token (parser)->location;
13419
13420 if (c_parser_next_token_is_not (parser, CPP_NAME)
13421 || c_parser_peek_token (parser)->id_kind != C_ID_ID)
13422 c_parser_error (parser, "expected identifier");
13423
13424 while (c_parser_next_token_is (parser, CPP_NAME)
13425 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
13426 {
13427 tree var = lookup_name (c_parser_peek_token (parser)->value);
13428
13429 if (var == NULL)
13430 {
13431 undeclared_variable (c_parser_peek_token (parser)->location,
13432 c_parser_peek_token (parser)->value);
13433 c_parser_consume_token (parser);
13434 }
13435 else if (var == error_mark_node)
13436 c_parser_consume_token (parser);
13437 else
13438 {
13439 tree step = integer_one_node;
13440
13441 /* Parse the linear step if present. */
13442 if (c_parser_peek_2nd_token (parser)->type == CPP_COLON)
13443 {
13444 c_parser_consume_token (parser);
13445 c_parser_consume_token (parser);
13446
13447 tree expr = c_parser_expr_no_commas (parser, NULL).value;
13448 expr = c_fully_fold (expr, false, NULL);
13449
13450 if (TREE_TYPE (expr)
13451 && INTEGRAL_TYPE_P (TREE_TYPE (expr))
13452 && (TREE_CONSTANT (expr)
13453 || DECL_P (expr)))
13454 step = expr;
13455 else
13456 c_parser_error (parser,
13457 "step size must be an integer constant "
13458 "expression or an integer variable");
13459 }
13460 else
13461 c_parser_consume_token (parser);
13462
13463 /* Use OMP_CLAUSE_LINEAR, which has the same semantics. */
13464 tree u = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
13465 OMP_CLAUSE_DECL (u) = var;
13466 OMP_CLAUSE_LINEAR_STEP (u) = step;
13467 OMP_CLAUSE_CHAIN (u) = clauses;
13468 clauses = u;
13469 }
13470
13471 if (c_parser_next_token_is_not (parser, CPP_COMMA))
13472 break;
13473
13474 c_parser_consume_token (parser);
13475 }
13476
13477 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
13478
13479 return clauses;
13480 }
13481
13482 /* Returns the name of the next clause. If the clause is not
13483 recognized SIMD_OMP_CLAUSE_NONE is returned and the next token is
13484 not consumed. Otherwise, the appropriate pragma_simd_clause is
13485 returned and the token is consumed. */
13486
13487 static pragma_cilk_clause
13488 c_parser_cilk_clause_name (c_parser *parser)
13489 {
13490 pragma_cilk_clause result;
13491 c_token *token = c_parser_peek_token (parser);
13492
13493 if (!token->value || token->type != CPP_NAME)
13494 return PRAGMA_CILK_CLAUSE_NONE;
13495
13496 const char *p = IDENTIFIER_POINTER (token->value);
13497
13498 if (!strcmp (p, "vectorlength"))
13499 result = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
13500 else if (!strcmp (p, "linear"))
13501 result = PRAGMA_CILK_CLAUSE_LINEAR;
13502 else if (!strcmp (p, "private"))
13503 result = PRAGMA_CILK_CLAUSE_PRIVATE;
13504 else if (!strcmp (p, "firstprivate"))
13505 result = PRAGMA_CILK_CLAUSE_FIRSTPRIVATE;
13506 else if (!strcmp (p, "lastprivate"))
13507 result = PRAGMA_CILK_CLAUSE_LASTPRIVATE;
13508 else if (!strcmp (p, "reduction"))
13509 result = PRAGMA_CILK_CLAUSE_REDUCTION;
13510 else
13511 return PRAGMA_CILK_CLAUSE_NONE;
13512
13513 c_parser_consume_token (parser);
13514 return result;
13515 }
13516
13517 /* Parse all #<pragma simd> clauses. Return the list of clauses
13518 found. */
13519
13520 static tree
13521 c_parser_cilk_all_clauses (c_parser *parser)
13522 {
13523 tree clauses = NULL;
13524
13525 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
13526 {
13527 pragma_cilk_clause c_kind;
13528
13529 c_kind = c_parser_cilk_clause_name (parser);
13530
13531 switch (c_kind)
13532 {
13533 case PRAGMA_CILK_CLAUSE_VECTORLENGTH:
13534 clauses = c_parser_cilk_clause_vectorlength (parser, clauses);
13535 break;
13536 case PRAGMA_CILK_CLAUSE_LINEAR:
13537 clauses = c_parser_cilk_clause_linear (parser, clauses);
13538 break;
13539 case PRAGMA_CILK_CLAUSE_PRIVATE:
13540 /* Use the OpenMP counterpart. */
13541 clauses = c_parser_omp_clause_private (parser, clauses);
13542 break;
13543 case PRAGMA_CILK_CLAUSE_FIRSTPRIVATE:
13544 /* Use the OpenMP counterpart. */
13545 clauses = c_parser_omp_clause_firstprivate (parser, clauses);
13546 break;
13547 case PRAGMA_CILK_CLAUSE_LASTPRIVATE:
13548 /* Use the OpenMP counterpart. */
13549 clauses = c_parser_omp_clause_lastprivate (parser, clauses);
13550 break;
13551 case PRAGMA_CILK_CLAUSE_REDUCTION:
13552 /* Use the OpenMP counterpart. */
13553 clauses = c_parser_omp_clause_reduction (parser, clauses);
13554 break;
13555 default:
13556 c_parser_error (parser, "expected %<#pragma simd%> clause");
13557 goto saw_error;
13558 }
13559 }
13560
13561 saw_error:
13562 c_parser_skip_to_pragma_eol (parser);
13563 return c_finish_cilk_clauses (clauses);
13564 }
13565
13566 /* Main entry point for parsing Cilk Plus <#pragma simd> for
13567 loops. */
13568
13569 static void
13570 c_parser_cilk_simd (c_parser *parser ATTRIBUTE_UNUSED)
13571 {
13572 char p_name[100];
13573 strcpy (p_name, "#pragma omp");
13574 tree clauses = c_parser_cilk_all_clauses (parser);
13575 tree block = c_begin_compound_stmt (true);
13576 location_t loc = c_parser_peek_token (parser)->location;
13577 c_parser_omp_for_loop (loc, parser, CILK_SIMD, clauses, NULL);
13578 block = c_end_compound_stmt (loc, block, true);
13579 add_stmt (block);
13580 }
13581 \f
13582 /* Parse a transaction attribute (GCC Extension).
13583
13584 transaction-attribute:
13585 attributes
13586 [ [ any-word ] ]
13587
13588 The transactional memory language description is written for C++,
13589 and uses the C++0x attribute syntax. For compatibility, allow the
13590 bracket style for transactions in C as well. */
13591
13592 static tree
13593 c_parser_transaction_attributes (c_parser *parser)
13594 {
13595 tree attr_name, attr = NULL;
13596
13597 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
13598 return c_parser_attributes (parser);
13599
13600 if (!c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
13601 return NULL_TREE;
13602 c_parser_consume_token (parser);
13603 if (!c_parser_require (parser, CPP_OPEN_SQUARE, "expected %<[%>"))
13604 goto error1;
13605
13606 attr_name = c_parser_attribute_any_word (parser);
13607 if (attr_name)
13608 {
13609 c_parser_consume_token (parser);
13610 attr = build_tree_list (attr_name, NULL_TREE);
13611 }
13612 else
13613 c_parser_error (parser, "expected identifier");
13614
13615 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, "expected %<]%>");
13616 error1:
13617 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, "expected %<]%>");
13618 return attr;
13619 }
13620
13621 /* Parse a __transaction_atomic or __transaction_relaxed statement
13622 (GCC Extension).
13623
13624 transaction-statement:
13625 __transaction_atomic transaction-attribute[opt] compound-statement
13626 __transaction_relaxed compound-statement
13627
13628 Note that the only valid attribute is: "outer".
13629 */
13630
13631 static tree
13632 c_parser_transaction (c_parser *parser, enum rid keyword)
13633 {
13634 unsigned int old_in = parser->in_transaction;
13635 unsigned int this_in = 1, new_in;
13636 location_t loc = c_parser_peek_token (parser)->location;
13637 tree stmt, attrs;
13638
13639 gcc_assert ((keyword == RID_TRANSACTION_ATOMIC
13640 || keyword == RID_TRANSACTION_RELAXED)
13641 && c_parser_next_token_is_keyword (parser, keyword));
13642 c_parser_consume_token (parser);
13643
13644 if (keyword == RID_TRANSACTION_RELAXED)
13645 this_in |= TM_STMT_ATTR_RELAXED;
13646 else
13647 {
13648 attrs = c_parser_transaction_attributes (parser);
13649 if (attrs)
13650 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
13651 }
13652
13653 /* Keep track if we're in the lexical scope of an outer transaction. */
13654 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
13655
13656 parser->in_transaction = new_in;
13657 stmt = c_parser_compound_statement (parser);
13658 parser->in_transaction = old_in;
13659
13660 if (flag_tm)
13661 stmt = c_finish_transaction (loc, stmt, this_in);
13662 else
13663 error_at (loc, (keyword == RID_TRANSACTION_ATOMIC ?
13664 "%<__transaction_atomic%> without transactional memory support enabled"
13665 : "%<__transaction_relaxed %> "
13666 "without transactional memory support enabled"));
13667
13668 return stmt;
13669 }
13670
13671 /* Parse a __transaction_atomic or __transaction_relaxed expression
13672 (GCC Extension).
13673
13674 transaction-expression:
13675 __transaction_atomic ( expression )
13676 __transaction_relaxed ( expression )
13677 */
13678
13679 static struct c_expr
13680 c_parser_transaction_expression (c_parser *parser, enum rid keyword)
13681 {
13682 struct c_expr ret;
13683 unsigned int old_in = parser->in_transaction;
13684 unsigned int this_in = 1;
13685 location_t loc = c_parser_peek_token (parser)->location;
13686 tree attrs;
13687
13688 gcc_assert ((keyword == RID_TRANSACTION_ATOMIC
13689 || keyword == RID_TRANSACTION_RELAXED)
13690 && c_parser_next_token_is_keyword (parser, keyword));
13691 c_parser_consume_token (parser);
13692
13693 if (keyword == RID_TRANSACTION_RELAXED)
13694 this_in |= TM_STMT_ATTR_RELAXED;
13695 else
13696 {
13697 attrs = c_parser_transaction_attributes (parser);
13698 if (attrs)
13699 this_in |= parse_tm_stmt_attr (attrs, 0);
13700 }
13701
13702 parser->in_transaction = this_in;
13703 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
13704 {
13705 tree expr = c_parser_expression (parser).value;
13706 ret.original_type = TREE_TYPE (expr);
13707 ret.value = build1 (TRANSACTION_EXPR, ret.original_type, expr);
13708 if (this_in & TM_STMT_ATTR_RELAXED)
13709 TRANSACTION_EXPR_RELAXED (ret.value) = 1;
13710 SET_EXPR_LOCATION (ret.value, loc);
13711 ret.original_code = TRANSACTION_EXPR;
13712 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
13713 {
13714 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
13715 goto error;
13716 }
13717 }
13718 else
13719 {
13720 error:
13721 ret.value = error_mark_node;
13722 ret.original_code = ERROR_MARK;
13723 ret.original_type = NULL;
13724 }
13725 parser->in_transaction = old_in;
13726
13727 if (!flag_tm)
13728 error_at (loc, (keyword == RID_TRANSACTION_ATOMIC ?
13729 "%<__transaction_atomic%> without transactional memory support enabled"
13730 : "%<__transaction_relaxed %> "
13731 "without transactional memory support enabled"));
13732
13733 return ret;
13734 }
13735
13736 /* Parse a __transaction_cancel statement (GCC Extension).
13737
13738 transaction-cancel-statement:
13739 __transaction_cancel transaction-attribute[opt] ;
13740
13741 Note that the only valid attribute is "outer".
13742 */
13743
13744 static tree
13745 c_parser_transaction_cancel (c_parser *parser)
13746 {
13747 location_t loc = c_parser_peek_token (parser)->location;
13748 tree attrs;
13749 bool is_outer = false;
13750
13751 gcc_assert (c_parser_next_token_is_keyword (parser, RID_TRANSACTION_CANCEL));
13752 c_parser_consume_token (parser);
13753
13754 attrs = c_parser_transaction_attributes (parser);
13755 if (attrs)
13756 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
13757
13758 if (!flag_tm)
13759 {
13760 error_at (loc, "%<__transaction_cancel%> without "
13761 "transactional memory support enabled");
13762 goto ret_error;
13763 }
13764 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
13765 {
13766 error_at (loc, "%<__transaction_cancel%> within a "
13767 "%<__transaction_relaxed%>");
13768 goto ret_error;
13769 }
13770 else if (is_outer)
13771 {
13772 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
13773 && !is_tm_may_cancel_outer (current_function_decl))
13774 {
13775 error_at (loc, "outer %<__transaction_cancel%> not "
13776 "within outer %<__transaction_atomic%>");
13777 error_at (loc, " or a %<transaction_may_cancel_outer%> function");
13778 goto ret_error;
13779 }
13780 }
13781 else if (parser->in_transaction == 0)
13782 {
13783 error_at (loc, "%<__transaction_cancel%> not within "
13784 "%<__transaction_atomic%>");
13785 goto ret_error;
13786 }
13787
13788 return add_stmt (build_tm_abort_call (loc, is_outer));
13789
13790 ret_error:
13791 return build1 (NOP_EXPR, void_type_node, error_mark_node);
13792 }
13793 \f
13794 /* Parse a single source file. */
13795
13796 void
13797 c_parse_file (void)
13798 {
13799 /* Use local storage to begin. If the first token is a pragma, parse it.
13800 If it is #pragma GCC pch_preprocess, then this will load a PCH file
13801 which will cause garbage collection. */
13802 c_parser tparser;
13803
13804 memset (&tparser, 0, sizeof tparser);
13805 tparser.tokens = &tparser.tokens_buf[0];
13806 the_parser = &tparser;
13807
13808 if (c_parser_peek_token (&tparser)->pragma_kind == PRAGMA_GCC_PCH_PREPROCESS)
13809 c_parser_pragma_pch_preprocess (&tparser);
13810
13811 the_parser = ggc_alloc_c_parser ();
13812 *the_parser = tparser;
13813 if (tparser.tokens == &tparser.tokens_buf[0])
13814 the_parser->tokens = &the_parser->tokens_buf[0];
13815
13816 /* Initialize EH, if we've been told to do so. */
13817 if (flag_exceptions)
13818 using_eh_for_cleanups ();
13819
13820 c_parser_translation_unit (the_parser);
13821 the_parser = NULL;
13822 }
13823
13824 /* This function parses Cilk Plus array notation. The starting index is
13825 passed in INITIAL_INDEX and the array name is passes in ARRAY_VALUE. The
13826 return value of this function is a tree_node called VALUE_TREE of type
13827 ARRAY_NOTATION_REF. */
13828
13829 static tree
13830 c_parser_array_notation (location_t loc, c_parser *parser, tree initial_index,
13831 tree array_value)
13832 {
13833 c_token *token = NULL;
13834 tree start_index = NULL_TREE, end_index = NULL_TREE, stride = NULL_TREE;
13835 tree value_tree = NULL_TREE, type = NULL_TREE, array_type = NULL_TREE;
13836 tree array_type_domain = NULL_TREE;
13837
13838 if (array_value == error_mark_node)
13839 {
13840 /* No need to continue. If either of these 2 were true, then an error
13841 must be emitted already. Thus, no need to emit them twice. */
13842 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
13843 return error_mark_node;
13844 }
13845
13846 array_type = TREE_TYPE (array_value);
13847 gcc_assert (array_type);
13848 type = TREE_TYPE (array_type);
13849 token = c_parser_peek_token (parser);
13850
13851 if (token->type == CPP_EOF)
13852 {
13853 c_parser_error (parser, "expected %<:%> or numeral");
13854 return value_tree;
13855 }
13856 else if (token->type == CPP_COLON)
13857 {
13858 if (!initial_index)
13859 {
13860 /* If we are here, then we have a case like this A[:]. */
13861 c_parser_consume_token (parser);
13862 if (TREE_CODE (array_type) == POINTER_TYPE)
13863 {
13864 error_at (loc, "start-index and length fields necessary for "
13865 "using array notations in pointers");
13866 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
13867 return error_mark_node;
13868 }
13869 if (TREE_CODE (array_type) == FUNCTION_TYPE)
13870 {
13871 error_at (loc, "array notations cannot be used with function "
13872 "type");
13873 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
13874 return error_mark_node;
13875 }
13876 array_type_domain = TYPE_DOMAIN (array_type);
13877
13878 if (!array_type_domain)
13879 {
13880 error_at (loc, "start-index and length fields necessary for "
13881 "using array notations in dimensionless arrays");
13882 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
13883 return error_mark_node;
13884 }
13885
13886 start_index = TYPE_MINVAL (array_type_domain);
13887 start_index = fold_build1 (CONVERT_EXPR, ptrdiff_type_node,
13888 start_index);
13889 if (!TYPE_MAXVAL (array_type_domain)
13890 || !TREE_CONSTANT (TYPE_MAXVAL (array_type_domain)))
13891 {
13892 error_at (loc, "start-index and length fields necessary for "
13893 "using array notations in variable-length arrays");
13894 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
13895 return error_mark_node;
13896 }
13897 end_index = TYPE_MAXVAL (array_type_domain);
13898 end_index = fold_build2 (PLUS_EXPR, TREE_TYPE (end_index),
13899 end_index, integer_one_node);
13900 end_index = fold_build1 (CONVERT_EXPR, ptrdiff_type_node, end_index);
13901 stride = build_int_cst (integer_type_node, 1);
13902 stride = fold_build1 (CONVERT_EXPR, ptrdiff_type_node, stride);
13903 }
13904 else if (initial_index != error_mark_node)
13905 {
13906 /* If we are here, then there should be 2 possibilities:
13907 1. Array [EXPR : EXPR]
13908 2. Array [EXPR : EXPR : EXPR]
13909 */
13910 start_index = initial_index;
13911
13912 if (TREE_CODE (array_type) == FUNCTION_TYPE)
13913 {
13914 error_at (loc, "array notations cannot be used with function "
13915 "type");
13916 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
13917 return error_mark_node;
13918 }
13919 c_parser_consume_token (parser); /* consume the ':' */
13920 struct c_expr ce = c_parser_expression (parser);
13921 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
13922 end_index = ce.value;
13923 if (!end_index || end_index == error_mark_node)
13924 {
13925 c_parser_skip_to_end_of_block_or_statement (parser);
13926 return error_mark_node;
13927 }
13928 if (c_parser_peek_token (parser)->type == CPP_COLON)
13929 {
13930 c_parser_consume_token (parser);
13931 ce = c_parser_expression (parser);
13932 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
13933 stride = ce.value;
13934 if (!stride || stride == error_mark_node)
13935 {
13936 c_parser_skip_to_end_of_block_or_statement (parser);
13937 return error_mark_node;
13938 }
13939 }
13940 }
13941 else
13942 c_parser_error (parser, "expected array notation expression");
13943 }
13944 else
13945 c_parser_error (parser, "expected array notation expression");
13946
13947 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, "expected %<]%>");
13948
13949 value_tree = build_array_notation_ref (loc, array_value, start_index,
13950 end_index, stride, type);
13951 if (value_tree != error_mark_node)
13952 SET_EXPR_LOCATION (value_tree, loc);
13953 return value_tree;
13954 }
13955
13956 #include "gt-c-c-parser.h"