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