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