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