]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/c-parser.c
Merge from transactional-memory branch.
[thirdparty/gcc.git] / gcc / c-parser.c
1 /* Parser for C and Objective-C.
2 Copyright (C) 1987, 1988, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2007, 2008, 2009, 2010, 2011
4 Free Software Foundation, Inc.
5
6 Parser actions based on the old Bison parser; structure somewhat
7 influenced by and fragments based on the C++ parser.
8
9 This file is part of GCC.
10
11 GCC is free software; you can redistribute it and/or modify it under
12 the terms of the GNU General Public License as published by the Free
13 Software Foundation; either version 3, or (at your option) any later
14 version.
15
16 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
17 WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19 for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with GCC; see the file COPYING3. If not see
23 <http://www.gnu.org/licenses/>. */
24
25 /* TODO:
26
27 Make sure all relevant comments, and all relevant code from all
28 actions, brought over from old parser. Verify exact correspondence
29 of syntax accepted.
30
31 Add testcases covering every input symbol in every state in old and
32 new parsers.
33
34 Include full syntax for GNU C, including erroneous cases accepted
35 with error messages, in syntax productions in comments.
36
37 Make more diagnostics in the front end generally take an explicit
38 location rather than implicitly using input_location. */
39
40 #include "config.h"
41 #include "system.h"
42 #include "coretypes.h"
43 #include "tm.h" /* For rtl.h: needs enum reg_class. */
44 #include "tree.h"
45 #include "langhooks.h"
46 #include "input.h"
47 #include "cpplib.h"
48 #include "timevar.h"
49 #include "c-family/c-pragma.h"
50 #include "c-tree.h"
51 #include "flags.h"
52 #include "output.h"
53 #include "ggc.h"
54 #include "c-family/c-common.h"
55 #include "c-family/c-objc.h"
56 #include "vec.h"
57 #include "target.h"
58 #include "cgraph.h"
59 #include "plugin.h"
60
61 \f
62 /* Initialization routine for this file. */
63
64 void
65 c_parse_init (void)
66 {
67 /* The only initialization required is of the reserved word
68 identifiers. */
69 unsigned int i;
70 tree id;
71 int mask = 0;
72
73 /* Make sure RID_MAX hasn't grown past the 8 bits used to hold the keyword in
74 the c_token structure. */
75 gcc_assert (RID_MAX <= 255);
76
77 mask |= D_CXXONLY;
78 if (!flag_isoc99)
79 mask |= D_C99;
80 if (flag_no_asm)
81 {
82 mask |= D_ASM | D_EXT;
83 if (!flag_isoc99)
84 mask |= D_EXT89;
85 }
86 if (!c_dialect_objc ())
87 mask |= D_OBJC | D_CXX_OBJC;
88
89 ridpointers = ggc_alloc_cleared_vec_tree ((int) RID_MAX);
90 for (i = 0; i < num_c_common_reswords; i++)
91 {
92 /* If a keyword is disabled, do not enter it into the table
93 and so create a canonical spelling that isn't a keyword. */
94 if (c_common_reswords[i].disable & mask)
95 {
96 if (warn_cxx_compat
97 && (c_common_reswords[i].disable & D_CXXWARN))
98 {
99 id = get_identifier (c_common_reswords[i].word);
100 C_SET_RID_CODE (id, RID_CXX_COMPAT_WARN);
101 C_IS_RESERVED_WORD (id) = 1;
102 }
103 continue;
104 }
105
106 id = get_identifier (c_common_reswords[i].word);
107 C_SET_RID_CODE (id, c_common_reswords[i].rid);
108 C_IS_RESERVED_WORD (id) = 1;
109 ridpointers [(int) c_common_reswords[i].rid] = id;
110 }
111 }
112 \f
113 /* The C lexer intermediates between the lexer in cpplib and c-lex.c
114 and the C parser. Unlike the C++ lexer, the parser structure
115 stores the lexer information instead of using a separate structure.
116 Identifiers are separated into ordinary identifiers, type names,
117 keywords and some other Objective-C types of identifiers, and some
118 look-ahead is maintained.
119
120 ??? It might be a good idea to lex the whole file up front (as for
121 C++). It would then be possible to share more of the C and C++
122 lexer code, if desired. */
123
124 /* The following local token type is used. */
125
126 /* A keyword. */
127 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
128
129 /* More information about the type of a CPP_NAME token. */
130 typedef enum c_id_kind {
131 /* An ordinary identifier. */
132 C_ID_ID,
133 /* An identifier declared as a typedef name. */
134 C_ID_TYPENAME,
135 /* An identifier declared as an Objective-C class name. */
136 C_ID_CLASSNAME,
137 /* An address space identifier. */
138 C_ID_ADDRSPACE,
139 /* Not an identifier. */
140 C_ID_NONE
141 } c_id_kind;
142
143 /* A single C token after string literal concatenation and conversion
144 of preprocessing tokens to tokens. */
145 typedef struct GTY (()) c_token {
146 /* The kind of token. */
147 ENUM_BITFIELD (cpp_ttype) type : 8;
148 /* If this token is a CPP_NAME, this value indicates whether also
149 declared as some kind of type. Otherwise, it is C_ID_NONE. */
150 ENUM_BITFIELD (c_id_kind) id_kind : 8;
151 /* If this token is a keyword, this value indicates which keyword.
152 Otherwise, this value is RID_MAX. */
153 ENUM_BITFIELD (rid) keyword : 8;
154 /* If this token is a CPP_PRAGMA, this indicates the pragma that
155 was seen. Otherwise it is PRAGMA_NONE. */
156 ENUM_BITFIELD (pragma_kind) pragma_kind : 8;
157 /* The location at which this token was found. */
158 location_t location;
159 /* The value associated with this token, if any. */
160 tree value;
161 } c_token;
162
163 /* A parser structure recording information about the state and
164 context of parsing. Includes lexer information with up to two
165 tokens of look-ahead; more are not needed for C. */
166 typedef struct GTY(()) c_parser {
167 /* The look-ahead tokens. */
168 c_token tokens[2];
169 /* How many look-ahead tokens are available (0, 1 or 2). */
170 short tokens_avail;
171 /* True if a syntax error is being recovered from; false otherwise.
172 c_parser_error sets this flag. It should clear this flag when
173 enough tokens have been consumed to recover from the error. */
174 BOOL_BITFIELD error : 1;
175 /* True if we're processing a pragma, and shouldn't automatically
176 consume CPP_PRAGMA_EOL. */
177 BOOL_BITFIELD in_pragma : 1;
178 /* True if we're parsing the outermost block of an if statement. */
179 BOOL_BITFIELD in_if_block : 1;
180 /* True if we want to lex an untranslated string. */
181 BOOL_BITFIELD lex_untranslated_string : 1;
182
183 /* Objective-C specific parser/lexer information. */
184
185 /* True if we are in a context where the Objective-C "PQ" keywords
186 are considered keywords. */
187 BOOL_BITFIELD objc_pq_context : 1;
188 /* True if we are parsing a (potential) Objective-C foreach
189 statement. This is set to true after we parsed 'for (' and while
190 we wait for 'in' or ';' to decide if it's a standard C for loop or an
191 Objective-C foreach loop. */
192 BOOL_BITFIELD objc_could_be_foreach_context : 1;
193 /* The following flag is needed to contextualize Objective-C lexical
194 analysis. In some cases (e.g., 'int NSObject;'), it is
195 undesirable to bind an identifier to an Objective-C class, even
196 if a class with that name exists. */
197 BOOL_BITFIELD objc_need_raw_identifier : 1;
198 /* Nonzero if we're processing a __transaction statement. The value
199 is 1 | TM_STMT_ATTR_*. */
200 unsigned int in_transaction : 4;
201 /* True if we are in a context where the Objective-C "Property attribute"
202 keywords are valid. */
203 BOOL_BITFIELD objc_property_attr_context : 1;
204 } c_parser;
205
206
207 /* The actual parser and external interface. ??? Does this need to be
208 garbage-collected? */
209
210 static GTY (()) c_parser *the_parser;
211
212 /* Read in and lex a single token, storing it in *TOKEN. */
213
214 static void
215 c_lex_one_token (c_parser *parser, c_token *token)
216 {
217 timevar_push (TV_LEX);
218
219 token->type = c_lex_with_flags (&token->value, &token->location, NULL,
220 (parser->lex_untranslated_string
221 ? C_LEX_STRING_NO_TRANSLATE : 0));
222 token->id_kind = C_ID_NONE;
223 token->keyword = RID_MAX;
224 token->pragma_kind = PRAGMA_NONE;
225
226 switch (token->type)
227 {
228 case CPP_NAME:
229 {
230 tree decl;
231
232 bool objc_force_identifier = parser->objc_need_raw_identifier;
233 if (c_dialect_objc ())
234 parser->objc_need_raw_identifier = false;
235
236 if (C_IS_RESERVED_WORD (token->value))
237 {
238 enum rid rid_code = C_RID_CODE (token->value);
239
240 if (rid_code == RID_CXX_COMPAT_WARN)
241 {
242 warning_at (token->location,
243 OPT_Wc___compat,
244 "identifier %qE conflicts with C++ keyword",
245 token->value);
246 }
247 else if (rid_code >= RID_FIRST_ADDR_SPACE
248 && rid_code <= RID_LAST_ADDR_SPACE)
249 {
250 token->id_kind = C_ID_ADDRSPACE;
251 token->keyword = rid_code;
252 break;
253 }
254 else if (c_dialect_objc () && OBJC_IS_PQ_KEYWORD (rid_code))
255 {
256 /* We found an Objective-C "pq" keyword (in, out,
257 inout, bycopy, byref, oneway). They need special
258 care because the interpretation depends on the
259 context. */
260 if (parser->objc_pq_context)
261 {
262 token->type = CPP_KEYWORD;
263 token->keyword = rid_code;
264 break;
265 }
266 else if (parser->objc_could_be_foreach_context
267 && rid_code == RID_IN)
268 {
269 /* We are in Objective-C, inside a (potential)
270 foreach context (which means after having
271 parsed 'for (', but before having parsed ';'),
272 and we found 'in'. We consider it the keyword
273 which terminates the declaration at the
274 beginning of a foreach-statement. Note that
275 this means you can't use 'in' for anything else
276 in that context; in particular, in Objective-C
277 you can't use 'in' as the name of the running
278 variable in a C for loop. We could potentially
279 try to add code here to disambiguate, but it
280 seems a reasonable limitation. */
281 token->type = CPP_KEYWORD;
282 token->keyword = rid_code;
283 break;
284 }
285 /* Else, "pq" keywords outside of the "pq" context are
286 not keywords, and we fall through to the code for
287 normal tokens. */
288 }
289 else if (c_dialect_objc () && OBJC_IS_PATTR_KEYWORD (rid_code))
290 {
291 /* We found an Objective-C "property attribute"
292 keyword (getter, setter, readonly, etc). These are
293 only valid in the property context. */
294 if (parser->objc_property_attr_context)
295 {
296 token->type = CPP_KEYWORD;
297 token->keyword = rid_code;
298 break;
299 }
300 /* Else they are not special keywords.
301 */
302 }
303 else if (c_dialect_objc ()
304 && (OBJC_IS_AT_KEYWORD (rid_code)
305 || OBJC_IS_CXX_KEYWORD (rid_code)))
306 {
307 /* We found one of the Objective-C "@" keywords (defs,
308 selector, synchronized, etc) or one of the
309 Objective-C "cxx" keywords (class, private,
310 protected, public, try, catch, throw) without a
311 preceding '@' sign. Do nothing and fall through to
312 the code for normal tokens (in C++ we would still
313 consider the CXX ones keywords, but not in C). */
314 ;
315 }
316 else
317 {
318 token->type = CPP_KEYWORD;
319 token->keyword = rid_code;
320 break;
321 }
322 }
323
324 decl = lookup_name (token->value);
325 if (decl)
326 {
327 if (TREE_CODE (decl) == TYPE_DECL)
328 {
329 token->id_kind = C_ID_TYPENAME;
330 break;
331 }
332 }
333 else if (c_dialect_objc ())
334 {
335 tree objc_interface_decl = objc_is_class_name (token->value);
336 /* Objective-C class names are in the same namespace as
337 variables and typedefs, and hence are shadowed by local
338 declarations. */
339 if (objc_interface_decl
340 && (!objc_force_identifier || global_bindings_p ()))
341 {
342 token->value = objc_interface_decl;
343 token->id_kind = C_ID_CLASSNAME;
344 break;
345 }
346 }
347 token->id_kind = C_ID_ID;
348 }
349 break;
350 case CPP_AT_NAME:
351 /* This only happens in Objective-C; it must be a keyword. */
352 token->type = CPP_KEYWORD;
353 switch (C_RID_CODE (token->value))
354 {
355 /* Replace 'class' with '@class', 'private' with '@private',
356 etc. This prevents confusion with the C++ keyword
357 'class', and makes the tokens consistent with other
358 Objective-C 'AT' keywords. For example '@class' is
359 reported as RID_AT_CLASS which is consistent with
360 '@synchronized', which is reported as
361 RID_AT_SYNCHRONIZED.
362 */
363 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
364 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
365 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
366 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
367 case RID_THROW: token->keyword = RID_AT_THROW; break;
368 case RID_TRY: token->keyword = RID_AT_TRY; break;
369 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
370 default: token->keyword = C_RID_CODE (token->value);
371 }
372 break;
373 case CPP_COLON:
374 case CPP_COMMA:
375 case CPP_CLOSE_PAREN:
376 case CPP_SEMICOLON:
377 /* These tokens may affect the interpretation of any identifiers
378 following, if doing Objective-C. */
379 if (c_dialect_objc ())
380 parser->objc_need_raw_identifier = false;
381 break;
382 case CPP_PRAGMA:
383 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
384 token->pragma_kind = (enum pragma_kind) TREE_INT_CST_LOW (token->value);
385 token->value = NULL;
386 break;
387 default:
388 break;
389 }
390 timevar_pop (TV_LEX);
391 }
392
393 /* Return a pointer to the next token from PARSER, reading it in if
394 necessary. */
395
396 static inline c_token *
397 c_parser_peek_token (c_parser *parser)
398 {
399 if (parser->tokens_avail == 0)
400 {
401 c_lex_one_token (parser, &parser->tokens[0]);
402 parser->tokens_avail = 1;
403 }
404 return &parser->tokens[0];
405 }
406
407 /* Return true if the next token from PARSER has the indicated
408 TYPE. */
409
410 static inline bool
411 c_parser_next_token_is (c_parser *parser, enum cpp_ttype type)
412 {
413 return c_parser_peek_token (parser)->type == type;
414 }
415
416 /* Return true if the next token from PARSER does not have the
417 indicated TYPE. */
418
419 static inline bool
420 c_parser_next_token_is_not (c_parser *parser, enum cpp_ttype type)
421 {
422 return !c_parser_next_token_is (parser, type);
423 }
424
425 /* Return true if the next token from PARSER is the indicated
426 KEYWORD. */
427
428 static inline bool
429 c_parser_next_token_is_keyword (c_parser *parser, enum rid keyword)
430 {
431 return c_parser_peek_token (parser)->keyword == keyword;
432 }
433
434 /* Return a pointer to the next-but-one token from PARSER, reading it
435 in if necessary. The next token is already read in. */
436
437 static c_token *
438 c_parser_peek_2nd_token (c_parser *parser)
439 {
440 if (parser->tokens_avail >= 2)
441 return &parser->tokens[1];
442 gcc_assert (parser->tokens_avail == 1);
443 gcc_assert (parser->tokens[0].type != CPP_EOF);
444 gcc_assert (parser->tokens[0].type != CPP_PRAGMA_EOL);
445 c_lex_one_token (parser, &parser->tokens[1]);
446 parser->tokens_avail = 2;
447 return &parser->tokens[1];
448 }
449
450 /* Return true if TOKEN can start a type name,
451 false otherwise. */
452 static bool
453 c_token_starts_typename (c_token *token)
454 {
455 switch (token->type)
456 {
457 case CPP_NAME:
458 switch (token->id_kind)
459 {
460 case C_ID_ID:
461 return false;
462 case C_ID_ADDRSPACE:
463 return true;
464 case C_ID_TYPENAME:
465 return true;
466 case C_ID_CLASSNAME:
467 gcc_assert (c_dialect_objc ());
468 return true;
469 default:
470 gcc_unreachable ();
471 }
472 case CPP_KEYWORD:
473 switch (token->keyword)
474 {
475 case RID_UNSIGNED:
476 case RID_LONG:
477 case RID_INT128:
478 case RID_SHORT:
479 case RID_SIGNED:
480 case RID_COMPLEX:
481 case RID_INT:
482 case RID_CHAR:
483 case RID_FLOAT:
484 case RID_DOUBLE:
485 case RID_VOID:
486 case RID_DFLOAT32:
487 case RID_DFLOAT64:
488 case RID_DFLOAT128:
489 case RID_BOOL:
490 case RID_ENUM:
491 case RID_STRUCT:
492 case RID_UNION:
493 case RID_TYPEOF:
494 case RID_CONST:
495 case RID_VOLATILE:
496 case RID_RESTRICT:
497 case RID_ATTRIBUTE:
498 case RID_FRACT:
499 case RID_ACCUM:
500 case RID_SAT:
501 return true;
502 default:
503 return false;
504 }
505 case CPP_LESS:
506 if (c_dialect_objc ())
507 return true;
508 return false;
509 default:
510 return false;
511 }
512 }
513
514 enum c_lookahead_kind {
515 /* Always treat unknown identifiers as typenames. */
516 cla_prefer_type,
517
518 /* Could be parsing a nonabstract declarator. Only treat an identifier
519 as a typename if followed by another identifier or a star. */
520 cla_nonabstract_decl,
521
522 /* Never treat identifiers as typenames. */
523 cla_prefer_id
524 };
525
526 /* Return true if the next token from PARSER can start a type name,
527 false otherwise. LA specifies how to do lookahead in order to
528 detect unknown type names. If unsure, pick CLA_PREFER_ID. */
529
530 static inline bool
531 c_parser_next_tokens_start_typename (c_parser *parser, enum c_lookahead_kind la)
532 {
533 c_token *token = c_parser_peek_token (parser);
534 if (c_token_starts_typename (token))
535 return true;
536
537 /* Try a bit harder to detect an unknown typename. */
538 if (la != cla_prefer_id
539 && token->type == CPP_NAME
540 && token->id_kind == C_ID_ID
541
542 /* Do not try too hard when we could have "object in array". */
543 && !parser->objc_could_be_foreach_context
544
545 && (la == cla_prefer_type
546 || c_parser_peek_2nd_token (parser)->type == CPP_NAME
547 || c_parser_peek_2nd_token (parser)->type == CPP_MULT)
548
549 /* Only unknown identifiers. */
550 && !lookup_name (token->value))
551 return true;
552
553 return false;
554 }
555
556 /* Return true if TOKEN is a type qualifier, false otherwise. */
557 static bool
558 c_token_is_qualifier (c_token *token)
559 {
560 switch (token->type)
561 {
562 case CPP_NAME:
563 switch (token->id_kind)
564 {
565 case C_ID_ADDRSPACE:
566 return true;
567 default:
568 return false;
569 }
570 case CPP_KEYWORD:
571 switch (token->keyword)
572 {
573 case RID_CONST:
574 case RID_VOLATILE:
575 case RID_RESTRICT:
576 case RID_ATTRIBUTE:
577 return true;
578 default:
579 return false;
580 }
581 case CPP_LESS:
582 return false;
583 default:
584 gcc_unreachable ();
585 }
586 }
587
588 /* Return true if the next token from PARSER is a type qualifier,
589 false otherwise. */
590 static inline bool
591 c_parser_next_token_is_qualifier (c_parser *parser)
592 {
593 c_token *token = c_parser_peek_token (parser);
594 return c_token_is_qualifier (token);
595 }
596
597 /* Return true if TOKEN can start declaration specifiers, false
598 otherwise. */
599 static bool
600 c_token_starts_declspecs (c_token *token)
601 {
602 switch (token->type)
603 {
604 case CPP_NAME:
605 switch (token->id_kind)
606 {
607 case C_ID_ID:
608 return false;
609 case C_ID_ADDRSPACE:
610 return true;
611 case C_ID_TYPENAME:
612 return true;
613 case C_ID_CLASSNAME:
614 gcc_assert (c_dialect_objc ());
615 return true;
616 default:
617 gcc_unreachable ();
618 }
619 case CPP_KEYWORD:
620 switch (token->keyword)
621 {
622 case RID_STATIC:
623 case RID_EXTERN:
624 case RID_REGISTER:
625 case RID_TYPEDEF:
626 case RID_INLINE:
627 case RID_NORETURN:
628 case RID_AUTO:
629 case RID_THREAD:
630 case RID_UNSIGNED:
631 case RID_LONG:
632 case RID_INT128:
633 case RID_SHORT:
634 case RID_SIGNED:
635 case RID_COMPLEX:
636 case RID_INT:
637 case RID_CHAR:
638 case RID_FLOAT:
639 case RID_DOUBLE:
640 case RID_VOID:
641 case RID_DFLOAT32:
642 case RID_DFLOAT64:
643 case RID_DFLOAT128:
644 case RID_BOOL:
645 case RID_ENUM:
646 case RID_STRUCT:
647 case RID_UNION:
648 case RID_TYPEOF:
649 case RID_CONST:
650 case RID_VOLATILE:
651 case RID_RESTRICT:
652 case RID_ATTRIBUTE:
653 case RID_FRACT:
654 case RID_ACCUM:
655 case RID_SAT:
656 case RID_ALIGNAS:
657 return true;
658 default:
659 return false;
660 }
661 case CPP_LESS:
662 if (c_dialect_objc ())
663 return true;
664 return false;
665 default:
666 return false;
667 }
668 }
669
670
671 /* Return true if TOKEN can start declaration specifiers or a static
672 assertion, false otherwise. */
673 static bool
674 c_token_starts_declaration (c_token *token)
675 {
676 if (c_token_starts_declspecs (token)
677 || token->keyword == RID_STATIC_ASSERT)
678 return true;
679 else
680 return false;
681 }
682
683 /* Return true if the next token from PARSER can start declaration
684 specifiers, false otherwise. */
685 static inline bool
686 c_parser_next_token_starts_declspecs (c_parser *parser)
687 {
688 c_token *token = c_parser_peek_token (parser);
689
690 /* In Objective-C, a classname normally starts a declspecs unless it
691 is immediately followed by a dot. In that case, it is the
692 Objective-C 2.0 "dot-syntax" for class objects, ie, calls the
693 setter/getter on the class. c_token_starts_declspecs() can't
694 differentiate between the two cases because it only checks the
695 current token, so we have a special check here. */
696 if (c_dialect_objc ()
697 && token->type == CPP_NAME
698 && token->id_kind == C_ID_CLASSNAME
699 && c_parser_peek_2nd_token (parser)->type == CPP_DOT)
700 return false;
701
702 return c_token_starts_declspecs (token);
703 }
704
705 /* Return true if the next tokens from PARSER can start declaration
706 specifiers or a static assertion, false otherwise. */
707 static inline bool
708 c_parser_next_tokens_start_declaration (c_parser *parser)
709 {
710 c_token *token = c_parser_peek_token (parser);
711
712 /* Same as above. */
713 if (c_dialect_objc ()
714 && token->type == CPP_NAME
715 && token->id_kind == C_ID_CLASSNAME
716 && c_parser_peek_2nd_token (parser)->type == CPP_DOT)
717 return false;
718
719 /* Labels do not start declarations. */
720 if (token->type == CPP_NAME
721 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
722 return false;
723
724 if (c_token_starts_declaration (token))
725 return true;
726
727 if (c_parser_next_tokens_start_typename (parser, cla_nonabstract_decl))
728 return true;
729
730 return false;
731 }
732
733 /* Consume the next token from PARSER. */
734
735 static void
736 c_parser_consume_token (c_parser *parser)
737 {
738 gcc_assert (parser->tokens_avail >= 1);
739 gcc_assert (parser->tokens[0].type != CPP_EOF);
740 gcc_assert (!parser->in_pragma || parser->tokens[0].type != CPP_PRAGMA_EOL);
741 gcc_assert (parser->error || parser->tokens[0].type != CPP_PRAGMA);
742 if (parser->tokens_avail == 2)
743 parser->tokens[0] = parser->tokens[1];
744 parser->tokens_avail--;
745 }
746
747 /* Expect the current token to be a #pragma. Consume it and remember
748 that we've begun parsing a pragma. */
749
750 static void
751 c_parser_consume_pragma (c_parser *parser)
752 {
753 gcc_assert (!parser->in_pragma);
754 gcc_assert (parser->tokens_avail >= 1);
755 gcc_assert (parser->tokens[0].type == CPP_PRAGMA);
756 if (parser->tokens_avail == 2)
757 parser->tokens[0] = parser->tokens[1];
758 parser->tokens_avail--;
759 parser->in_pragma = true;
760 }
761
762 /* Update the globals input_location and in_system_header from
763 TOKEN. */
764 static inline void
765 c_parser_set_source_position_from_token (c_token *token)
766 {
767 if (token->type != CPP_EOF)
768 {
769 input_location = token->location;
770 }
771 }
772
773 /* Issue a diagnostic of the form
774 FILE:LINE: MESSAGE before TOKEN
775 where TOKEN is the next token in the input stream of PARSER.
776 MESSAGE (specified by the caller) is usually of the form "expected
777 OTHER-TOKEN".
778
779 Do not issue a diagnostic if still recovering from an error.
780
781 ??? This is taken from the C++ parser, but building up messages in
782 this way is not i18n-friendly and some other approach should be
783 used. */
784
785 static void
786 c_parser_error (c_parser *parser, const char *gmsgid)
787 {
788 c_token *token = c_parser_peek_token (parser);
789 if (parser->error)
790 return;
791 parser->error = true;
792 if (!gmsgid)
793 return;
794 /* This diagnostic makes more sense if it is tagged to the line of
795 the token we just peeked at. */
796 c_parser_set_source_position_from_token (token);
797 c_parse_error (gmsgid,
798 /* Because c_parse_error does not understand
799 CPP_KEYWORD, keywords are treated like
800 identifiers. */
801 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
802 /* ??? The C parser does not save the cpp flags of a
803 token, we need to pass 0 here and we will not get
804 the source spelling of some tokens but rather the
805 canonical spelling. */
806 token->value, /*flags=*/0);
807 }
808
809 /* If the next token is of the indicated TYPE, consume it. Otherwise,
810 issue the error MSGID. If MSGID is NULL then a message has already
811 been produced and no message will be produced this time. Returns
812 true if found, false otherwise. */
813
814 static bool
815 c_parser_require (c_parser *parser,
816 enum cpp_ttype type,
817 const char *msgid)
818 {
819 if (c_parser_next_token_is (parser, type))
820 {
821 c_parser_consume_token (parser);
822 return true;
823 }
824 else
825 {
826 c_parser_error (parser, msgid);
827 return false;
828 }
829 }
830
831 /* If the next token is the indicated keyword, consume it. Otherwise,
832 issue the error MSGID. Returns true if found, false otherwise. */
833
834 static bool
835 c_parser_require_keyword (c_parser *parser,
836 enum rid keyword,
837 const char *msgid)
838 {
839 if (c_parser_next_token_is_keyword (parser, keyword))
840 {
841 c_parser_consume_token (parser);
842 return true;
843 }
844 else
845 {
846 c_parser_error (parser, msgid);
847 return false;
848 }
849 }
850
851 /* Like c_parser_require, except that tokens will be skipped until the
852 desired token is found. An error message is still produced if the
853 next token is not as expected. If MSGID is NULL then a message has
854 already been produced and no message will be produced this
855 time. */
856
857 static void
858 c_parser_skip_until_found (c_parser *parser,
859 enum cpp_ttype type,
860 const char *msgid)
861 {
862 unsigned nesting_depth = 0;
863
864 if (c_parser_require (parser, type, msgid))
865 return;
866
867 /* Skip tokens until the desired token is found. */
868 while (true)
869 {
870 /* Peek at the next token. */
871 c_token *token = c_parser_peek_token (parser);
872 /* If we've reached the token we want, consume it and stop. */
873 if (token->type == type && !nesting_depth)
874 {
875 c_parser_consume_token (parser);
876 break;
877 }
878
879 /* If we've run out of tokens, stop. */
880 if (token->type == CPP_EOF)
881 return;
882 if (token->type == CPP_PRAGMA_EOL && parser->in_pragma)
883 return;
884 if (token->type == CPP_OPEN_BRACE
885 || token->type == CPP_OPEN_PAREN
886 || token->type == CPP_OPEN_SQUARE)
887 ++nesting_depth;
888 else if (token->type == CPP_CLOSE_BRACE
889 || token->type == CPP_CLOSE_PAREN
890 || token->type == CPP_CLOSE_SQUARE)
891 {
892 if (nesting_depth-- == 0)
893 break;
894 }
895 /* Consume this token. */
896 c_parser_consume_token (parser);
897 }
898 parser->error = false;
899 }
900
901 /* Skip tokens until the end of a parameter is found, but do not
902 consume the comma, semicolon or closing delimiter. */
903
904 static void
905 c_parser_skip_to_end_of_parameter (c_parser *parser)
906 {
907 unsigned nesting_depth = 0;
908
909 while (true)
910 {
911 c_token *token = c_parser_peek_token (parser);
912 if ((token->type == CPP_COMMA || token->type == CPP_SEMICOLON)
913 && !nesting_depth)
914 break;
915 /* If we've run out of tokens, stop. */
916 if (token->type == CPP_EOF)
917 return;
918 if (token->type == CPP_PRAGMA_EOL && parser->in_pragma)
919 return;
920 if (token->type == CPP_OPEN_BRACE
921 || token->type == CPP_OPEN_PAREN
922 || token->type == CPP_OPEN_SQUARE)
923 ++nesting_depth;
924 else if (token->type == CPP_CLOSE_BRACE
925 || token->type == CPP_CLOSE_PAREN
926 || token->type == CPP_CLOSE_SQUARE)
927 {
928 if (nesting_depth-- == 0)
929 break;
930 }
931 /* Consume this token. */
932 c_parser_consume_token (parser);
933 }
934 parser->error = false;
935 }
936
937 /* Expect to be at the end of the pragma directive and consume an
938 end of line marker. */
939
940 static void
941 c_parser_skip_to_pragma_eol (c_parser *parser)
942 {
943 gcc_assert (parser->in_pragma);
944 parser->in_pragma = false;
945
946 if (!c_parser_require (parser, CPP_PRAGMA_EOL, "expected end of line"))
947 while (true)
948 {
949 c_token *token = c_parser_peek_token (parser);
950 if (token->type == CPP_EOF)
951 break;
952 if (token->type == CPP_PRAGMA_EOL)
953 {
954 c_parser_consume_token (parser);
955 break;
956 }
957 c_parser_consume_token (parser);
958 }
959
960 parser->error = false;
961 }
962
963 /* Skip tokens until we have consumed an entire block, or until we
964 have consumed a non-nested ';'. */
965
966 static void
967 c_parser_skip_to_end_of_block_or_statement (c_parser *parser)
968 {
969 unsigned nesting_depth = 0;
970 bool save_error = parser->error;
971
972 while (true)
973 {
974 c_token *token;
975
976 /* Peek at the next token. */
977 token = c_parser_peek_token (parser);
978
979 switch (token->type)
980 {
981 case CPP_EOF:
982 return;
983
984 case CPP_PRAGMA_EOL:
985 if (parser->in_pragma)
986 return;
987 break;
988
989 case CPP_SEMICOLON:
990 /* If the next token is a ';', we have reached the
991 end of the statement. */
992 if (!nesting_depth)
993 {
994 /* Consume the ';'. */
995 c_parser_consume_token (parser);
996 goto finished;
997 }
998 break;
999
1000 case CPP_CLOSE_BRACE:
1001 /* If the next token is a non-nested '}', then we have
1002 reached the end of the current block. */
1003 if (nesting_depth == 0 || --nesting_depth == 0)
1004 {
1005 c_parser_consume_token (parser);
1006 goto finished;
1007 }
1008 break;
1009
1010 case CPP_OPEN_BRACE:
1011 /* If it the next token is a '{', then we are entering a new
1012 block. Consume the entire block. */
1013 ++nesting_depth;
1014 break;
1015
1016 case CPP_PRAGMA:
1017 /* If we see a pragma, consume the whole thing at once. We
1018 have some safeguards against consuming pragmas willy-nilly.
1019 Normally, we'd expect to be here with parser->error set,
1020 which disables these safeguards. But it's possible to get
1021 here for secondary error recovery, after parser->error has
1022 been cleared. */
1023 c_parser_consume_pragma (parser);
1024 c_parser_skip_to_pragma_eol (parser);
1025 parser->error = save_error;
1026 continue;
1027
1028 default:
1029 break;
1030 }
1031
1032 c_parser_consume_token (parser);
1033 }
1034
1035 finished:
1036 parser->error = false;
1037 }
1038
1039 /* CPP's options (initialized by c-opts.c). */
1040 extern cpp_options *cpp_opts;
1041
1042 /* Save the warning flags which are controlled by __extension__. */
1043
1044 static inline int
1045 disable_extension_diagnostics (void)
1046 {
1047 int ret = (pedantic
1048 | (warn_pointer_arith << 1)
1049 | (warn_traditional << 2)
1050 | (flag_iso << 3)
1051 | (warn_long_long << 4)
1052 | (warn_cxx_compat << 5)
1053 | (warn_overlength_strings << 6));
1054 cpp_opts->cpp_pedantic = pedantic = 0;
1055 warn_pointer_arith = 0;
1056 cpp_opts->cpp_warn_traditional = warn_traditional = 0;
1057 flag_iso = 0;
1058 cpp_opts->cpp_warn_long_long = warn_long_long = 0;
1059 warn_cxx_compat = 0;
1060 warn_overlength_strings = 0;
1061 return ret;
1062 }
1063
1064 /* Restore the warning flags which are controlled by __extension__.
1065 FLAGS is the return value from disable_extension_diagnostics. */
1066
1067 static inline void
1068 restore_extension_diagnostics (int flags)
1069 {
1070 cpp_opts->cpp_pedantic = pedantic = flags & 1;
1071 warn_pointer_arith = (flags >> 1) & 1;
1072 cpp_opts->cpp_warn_traditional = warn_traditional = (flags >> 2) & 1;
1073 flag_iso = (flags >> 3) & 1;
1074 cpp_opts->cpp_warn_long_long = warn_long_long = (flags >> 4) & 1;
1075 warn_cxx_compat = (flags >> 5) & 1;
1076 warn_overlength_strings = (flags >> 6) & 1;
1077 }
1078
1079 /* Possibly kinds of declarator to parse. */
1080 typedef enum c_dtr_syn {
1081 /* A normal declarator with an identifier. */
1082 C_DTR_NORMAL,
1083 /* An abstract declarator (maybe empty). */
1084 C_DTR_ABSTRACT,
1085 /* A parameter declarator: may be either, but after a type name does
1086 not redeclare a typedef name as an identifier if it can
1087 alternatively be interpreted as a typedef name; see DR#009,
1088 applied in C90 TC1, omitted from C99 and reapplied in C99 TC2
1089 following DR#249. For example, given a typedef T, "int T" and
1090 "int *T" are valid parameter declarations redeclaring T, while
1091 "int (T)" and "int * (T)" and "int (T[])" and "int (T (int))" are
1092 abstract declarators rather than involving redundant parentheses;
1093 the same applies with attributes inside the parentheses before
1094 "T". */
1095 C_DTR_PARM
1096 } c_dtr_syn;
1097
1098 /* The binary operation precedence levels, where 0 is a dummy lowest level
1099 used for the bottom of the stack. */
1100 enum c_parser_prec {
1101 PREC_NONE,
1102 PREC_LOGOR,
1103 PREC_LOGAND,
1104 PREC_BITOR,
1105 PREC_BITXOR,
1106 PREC_BITAND,
1107 PREC_EQ,
1108 PREC_REL,
1109 PREC_SHIFT,
1110 PREC_ADD,
1111 PREC_MULT,
1112 NUM_PRECS
1113 };
1114
1115 static void c_parser_external_declaration (c_parser *);
1116 static void c_parser_asm_definition (c_parser *);
1117 static void c_parser_declaration_or_fndef (c_parser *, bool, bool, bool,
1118 bool, bool, tree *);
1119 static void c_parser_static_assert_declaration_no_semi (c_parser *);
1120 static void c_parser_static_assert_declaration (c_parser *);
1121 static void c_parser_declspecs (c_parser *, struct c_declspecs *, bool, bool,
1122 bool, enum c_lookahead_kind);
1123 static struct c_typespec c_parser_enum_specifier (c_parser *);
1124 static struct c_typespec c_parser_struct_or_union_specifier (c_parser *);
1125 static tree c_parser_struct_declaration (c_parser *);
1126 static struct c_typespec c_parser_typeof_specifier (c_parser *);
1127 static tree c_parser_alignas_specifier (c_parser *);
1128 static struct c_declarator *c_parser_declarator (c_parser *, bool, c_dtr_syn,
1129 bool *);
1130 static struct c_declarator *c_parser_direct_declarator (c_parser *, bool,
1131 c_dtr_syn, bool *);
1132 static struct c_declarator *c_parser_direct_declarator_inner (c_parser *,
1133 bool,
1134 struct c_declarator *);
1135 static struct c_arg_info *c_parser_parms_declarator (c_parser *, bool, tree);
1136 static struct c_arg_info *c_parser_parms_list_declarator (c_parser *, tree,
1137 tree);
1138 static struct c_parm *c_parser_parameter_declaration (c_parser *, tree);
1139 static tree c_parser_simple_asm_expr (c_parser *);
1140 static tree c_parser_attributes (c_parser *);
1141 static struct c_type_name *c_parser_type_name (c_parser *);
1142 static struct c_expr c_parser_initializer (c_parser *);
1143 static struct c_expr c_parser_braced_init (c_parser *, tree, bool);
1144 static void c_parser_initelt (c_parser *, struct obstack *);
1145 static void c_parser_initval (c_parser *, struct c_expr *,
1146 struct obstack *);
1147 static tree c_parser_compound_statement (c_parser *);
1148 static void c_parser_compound_statement_nostart (c_parser *);
1149 static void c_parser_label (c_parser *);
1150 static void c_parser_statement (c_parser *);
1151 static void c_parser_statement_after_labels (c_parser *);
1152 static void c_parser_if_statement (c_parser *);
1153 static void c_parser_switch_statement (c_parser *);
1154 static void c_parser_while_statement (c_parser *);
1155 static void c_parser_do_statement (c_parser *);
1156 static void c_parser_for_statement (c_parser *);
1157 static tree c_parser_asm_statement (c_parser *);
1158 static tree c_parser_asm_operands (c_parser *, bool);
1159 static tree c_parser_asm_goto_operands (c_parser *);
1160 static tree c_parser_asm_clobbers (c_parser *);
1161 static struct c_expr c_parser_expr_no_commas (c_parser *, struct c_expr *);
1162 static struct c_expr c_parser_conditional_expression (c_parser *,
1163 struct c_expr *);
1164 static struct c_expr c_parser_binary_expression (c_parser *, struct c_expr *,
1165 enum c_parser_prec);
1166 static struct c_expr c_parser_cast_expression (c_parser *, struct c_expr *);
1167 static struct c_expr c_parser_unary_expression (c_parser *);
1168 static struct c_expr c_parser_sizeof_expression (c_parser *);
1169 static struct c_expr c_parser_alignof_expression (c_parser *);
1170 static struct c_expr c_parser_postfix_expression (c_parser *);
1171 static struct c_expr c_parser_postfix_expression_after_paren_type (c_parser *,
1172 struct c_type_name *,
1173 location_t);
1174 static struct c_expr c_parser_postfix_expression_after_primary (c_parser *,
1175 location_t loc,
1176 struct c_expr);
1177 static tree c_parser_transaction (c_parser *, enum rid);
1178 static struct c_expr c_parser_transaction_expression (c_parser *, enum rid);
1179 static tree c_parser_transaction_cancel (c_parser *);
1180 static struct c_expr c_parser_expression (c_parser *);
1181 static struct c_expr c_parser_expression_conv (c_parser *);
1182 static VEC(tree,gc) *c_parser_expr_list (c_parser *, bool, bool,
1183 VEC(tree,gc) **);
1184 static void c_parser_omp_construct (c_parser *);
1185 static void c_parser_omp_threadprivate (c_parser *);
1186 static void c_parser_omp_barrier (c_parser *);
1187 static void c_parser_omp_flush (c_parser *);
1188 static void c_parser_omp_taskwait (c_parser *);
1189 static void c_parser_omp_taskyield (c_parser *);
1190
1191 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
1192 static bool c_parser_pragma (c_parser *, enum pragma_context);
1193
1194 /* These Objective-C parser functions are only ever called when
1195 compiling Objective-C. */
1196 static void c_parser_objc_class_definition (c_parser *, tree);
1197 static void c_parser_objc_class_instance_variables (c_parser *);
1198 static void c_parser_objc_class_declaration (c_parser *);
1199 static void c_parser_objc_alias_declaration (c_parser *);
1200 static void c_parser_objc_protocol_definition (c_parser *, tree);
1201 static bool c_parser_objc_method_type (c_parser *);
1202 static void c_parser_objc_method_definition (c_parser *);
1203 static void c_parser_objc_methodprotolist (c_parser *);
1204 static void c_parser_objc_methodproto (c_parser *);
1205 static tree c_parser_objc_method_decl (c_parser *, bool, tree *, tree *);
1206 static tree c_parser_objc_type_name (c_parser *);
1207 static tree c_parser_objc_protocol_refs (c_parser *);
1208 static void c_parser_objc_try_catch_finally_statement (c_parser *);
1209 static void c_parser_objc_synchronized_statement (c_parser *);
1210 static tree c_parser_objc_selector (c_parser *);
1211 static tree c_parser_objc_selector_arg (c_parser *);
1212 static tree c_parser_objc_receiver (c_parser *);
1213 static tree c_parser_objc_message_args (c_parser *);
1214 static tree c_parser_objc_keywordexpr (c_parser *);
1215 static void c_parser_objc_at_property_declaration (c_parser *);
1216 static void c_parser_objc_at_synthesize_declaration (c_parser *);
1217 static void c_parser_objc_at_dynamic_declaration (c_parser *);
1218 static bool c_parser_objc_diagnose_bad_element_prefix
1219 (c_parser *, struct c_declspecs *);
1220
1221 /* Parse a translation unit (C90 6.7, C99 6.9).
1222
1223 translation-unit:
1224 external-declarations
1225
1226 external-declarations:
1227 external-declaration
1228 external-declarations external-declaration
1229
1230 GNU extensions:
1231
1232 translation-unit:
1233 empty
1234 */
1235
1236 static void
1237 c_parser_translation_unit (c_parser *parser)
1238 {
1239 if (c_parser_next_token_is (parser, CPP_EOF))
1240 {
1241 pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic,
1242 "ISO C forbids an empty translation unit");
1243 }
1244 else
1245 {
1246 void *obstack_position = obstack_alloc (&parser_obstack, 0);
1247 mark_valid_location_for_stdc_pragma (false);
1248 do
1249 {
1250 ggc_collect ();
1251 c_parser_external_declaration (parser);
1252 obstack_free (&parser_obstack, obstack_position);
1253 }
1254 while (c_parser_next_token_is_not (parser, CPP_EOF));
1255 }
1256 }
1257
1258 /* Parse an external declaration (C90 6.7, C99 6.9).
1259
1260 external-declaration:
1261 function-definition
1262 declaration
1263
1264 GNU extensions:
1265
1266 external-declaration:
1267 asm-definition
1268 ;
1269 __extension__ external-declaration
1270
1271 Objective-C:
1272
1273 external-declaration:
1274 objc-class-definition
1275 objc-class-declaration
1276 objc-alias-declaration
1277 objc-protocol-definition
1278 objc-method-definition
1279 @end
1280 */
1281
1282 static void
1283 c_parser_external_declaration (c_parser *parser)
1284 {
1285 int ext;
1286 switch (c_parser_peek_token (parser)->type)
1287 {
1288 case CPP_KEYWORD:
1289 switch (c_parser_peek_token (parser)->keyword)
1290 {
1291 case RID_EXTENSION:
1292 ext = disable_extension_diagnostics ();
1293 c_parser_consume_token (parser);
1294 c_parser_external_declaration (parser);
1295 restore_extension_diagnostics (ext);
1296 break;
1297 case RID_ASM:
1298 c_parser_asm_definition (parser);
1299 break;
1300 case RID_AT_INTERFACE:
1301 case RID_AT_IMPLEMENTATION:
1302 gcc_assert (c_dialect_objc ());
1303 c_parser_objc_class_definition (parser, NULL_TREE);
1304 break;
1305 case RID_AT_CLASS:
1306 gcc_assert (c_dialect_objc ());
1307 c_parser_objc_class_declaration (parser);
1308 break;
1309 case RID_AT_ALIAS:
1310 gcc_assert (c_dialect_objc ());
1311 c_parser_objc_alias_declaration (parser);
1312 break;
1313 case RID_AT_PROTOCOL:
1314 gcc_assert (c_dialect_objc ());
1315 c_parser_objc_protocol_definition (parser, NULL_TREE);
1316 break;
1317 case RID_AT_PROPERTY:
1318 gcc_assert (c_dialect_objc ());
1319 c_parser_objc_at_property_declaration (parser);
1320 break;
1321 case RID_AT_SYNTHESIZE:
1322 gcc_assert (c_dialect_objc ());
1323 c_parser_objc_at_synthesize_declaration (parser);
1324 break;
1325 case RID_AT_DYNAMIC:
1326 gcc_assert (c_dialect_objc ());
1327 c_parser_objc_at_dynamic_declaration (parser);
1328 break;
1329 case RID_AT_END:
1330 gcc_assert (c_dialect_objc ());
1331 c_parser_consume_token (parser);
1332 objc_finish_implementation ();
1333 break;
1334 default:
1335 goto decl_or_fndef;
1336 }
1337 break;
1338 case CPP_SEMICOLON:
1339 pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic,
1340 "ISO C does not allow extra %<;%> outside of a function");
1341 c_parser_consume_token (parser);
1342 break;
1343 case CPP_PRAGMA:
1344 mark_valid_location_for_stdc_pragma (true);
1345 c_parser_pragma (parser, pragma_external);
1346 mark_valid_location_for_stdc_pragma (false);
1347 break;
1348 case CPP_PLUS:
1349 case CPP_MINUS:
1350 if (c_dialect_objc ())
1351 {
1352 c_parser_objc_method_definition (parser);
1353 break;
1354 }
1355 /* Else fall through, and yield a syntax error trying to parse
1356 as a declaration or function definition. */
1357 default:
1358 decl_or_fndef:
1359 /* A declaration or a function definition (or, in Objective-C,
1360 an @interface or @protocol with prefix attributes). We can
1361 only tell which after parsing the declaration specifiers, if
1362 any, and the first declarator. */
1363 c_parser_declaration_or_fndef (parser, true, true, true, false, true, NULL);
1364 break;
1365 }
1366 }
1367
1368 /* Parse a declaration or function definition (C90 6.5, 6.7.1, C99
1369 6.7, 6.9.1). If FNDEF_OK is true, a function definition is
1370 accepted; otherwise (old-style parameter declarations) only other
1371 declarations are accepted. If STATIC_ASSERT_OK is true, a static
1372 assertion is accepted; otherwise (old-style parameter declarations)
1373 it is not. If NESTED is true, we are inside a function or parsing
1374 old-style parameter declarations; any functions encountered are
1375 nested functions and declaration specifiers are required; otherwise
1376 we are at top level and functions are normal functions and
1377 declaration specifiers may be optional. If EMPTY_OK is true, empty
1378 declarations are OK (subject to all other constraints); otherwise
1379 (old-style parameter declarations) they are diagnosed. If
1380 START_ATTR_OK is true, the declaration specifiers may start with
1381 attributes; otherwise they may not.
1382 OBJC_FOREACH_OBJECT_DECLARATION can be used to get back the parsed
1383 declaration when parsing an Objective-C foreach statement.
1384
1385 declaration:
1386 declaration-specifiers init-declarator-list[opt] ;
1387 static_assert-declaration
1388
1389 function-definition:
1390 declaration-specifiers[opt] declarator declaration-list[opt]
1391 compound-statement
1392
1393 declaration-list:
1394 declaration
1395 declaration-list declaration
1396
1397 init-declarator-list:
1398 init-declarator
1399 init-declarator-list , init-declarator
1400
1401 init-declarator:
1402 declarator simple-asm-expr[opt] attributes[opt]
1403 declarator simple-asm-expr[opt] attributes[opt] = initializer
1404
1405 GNU extensions:
1406
1407 nested-function-definition:
1408 declaration-specifiers declarator declaration-list[opt]
1409 compound-statement
1410
1411 Objective-C:
1412 attributes objc-class-definition
1413 attributes objc-category-definition
1414 attributes objc-protocol-definition
1415
1416 The simple-asm-expr and attributes are GNU extensions.
1417
1418 This function does not handle __extension__; that is handled in its
1419 callers. ??? Following the old parser, __extension__ may start
1420 external declarations, declarations in functions and declarations
1421 at the start of "for" loops, but not old-style parameter
1422 declarations.
1423
1424 C99 requires declaration specifiers in a function definition; the
1425 absence is diagnosed through the diagnosis of implicit int. In GNU
1426 C we also allow but diagnose declarations without declaration
1427 specifiers, but only at top level (elsewhere they conflict with
1428 other syntax).
1429
1430 In Objective-C, declarations of the looping variable in a foreach
1431 statement are exceptionally terminated by 'in' (for example, 'for
1432 (NSObject *object in array) { ... }').
1433
1434 OpenMP:
1435
1436 declaration:
1437 threadprivate-directive */
1438
1439 static void
1440 c_parser_declaration_or_fndef (c_parser *parser, bool fndef_ok,
1441 bool static_assert_ok, bool empty_ok,
1442 bool nested, bool start_attr_ok,
1443 tree *objc_foreach_object_declaration)
1444 {
1445 struct c_declspecs *specs;
1446 tree prefix_attrs;
1447 tree all_prefix_attrs;
1448 bool diagnosed_no_specs = false;
1449 location_t here = c_parser_peek_token (parser)->location;
1450
1451 if (static_assert_ok
1452 && c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT))
1453 {
1454 c_parser_static_assert_declaration (parser);
1455 return;
1456 }
1457 specs = build_null_declspecs ();
1458
1459 /* Try to detect an unknown type name when we have "A B" or "A *B". */
1460 if (c_parser_peek_token (parser)->type == CPP_NAME
1461 && c_parser_peek_token (parser)->id_kind == C_ID_ID
1462 && (c_parser_peek_2nd_token (parser)->type == CPP_NAME
1463 || c_parser_peek_2nd_token (parser)->type == CPP_MULT)
1464 && (!nested || !lookup_name (c_parser_peek_token (parser)->value)))
1465 {
1466 error_at (here, "unknown type name %qE",
1467 c_parser_peek_token (parser)->value);
1468
1469 /* Parse declspecs normally to get a correct pointer type, but avoid
1470 a further "fails to be a type name" error. Refuse nested functions
1471 since it is not how the user likely wants us to recover. */
1472 c_parser_peek_token (parser)->type = CPP_KEYWORD;
1473 c_parser_peek_token (parser)->keyword = RID_VOID;
1474 c_parser_peek_token (parser)->value = error_mark_node;
1475 fndef_ok = !nested;
1476 }
1477
1478 c_parser_declspecs (parser, specs, true, true, start_attr_ok, cla_nonabstract_decl);
1479 if (parser->error)
1480 {
1481 c_parser_skip_to_end_of_block_or_statement (parser);
1482 return;
1483 }
1484 if (nested && !specs->declspecs_seen_p)
1485 {
1486 c_parser_error (parser, "expected declaration specifiers");
1487 c_parser_skip_to_end_of_block_or_statement (parser);
1488 return;
1489 }
1490 finish_declspecs (specs);
1491 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1492 {
1493 if (empty_ok)
1494 shadow_tag (specs);
1495 else
1496 {
1497 shadow_tag_warned (specs, 1);
1498 pedwarn (here, 0, "empty declaration");
1499 }
1500 c_parser_consume_token (parser);
1501 return;
1502 }
1503
1504 /* Provide better error recovery. Note that a type name here is usually
1505 better diagnosed as a redeclaration. */
1506 if (empty_ok
1507 && specs->typespec_kind == ctsk_tagdef
1508 && c_parser_next_token_starts_declspecs (parser)
1509 && !c_parser_next_token_is (parser, CPP_NAME))
1510 {
1511 c_parser_error (parser, "expected %<;%>, identifier or %<(%>");
1512 parser->error = false;
1513 shadow_tag_warned (specs, 1);
1514 return;
1515 }
1516 else if (c_dialect_objc ())
1517 {
1518 /* Prefix attributes are an error on method decls. */
1519 switch (c_parser_peek_token (parser)->type)
1520 {
1521 case CPP_PLUS:
1522 case CPP_MINUS:
1523 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1524 return;
1525 if (specs->attrs)
1526 {
1527 warning_at (c_parser_peek_token (parser)->location,
1528 OPT_Wattributes,
1529 "prefix attributes are ignored for methods");
1530 specs->attrs = NULL_TREE;
1531 }
1532 if (fndef_ok)
1533 c_parser_objc_method_definition (parser);
1534 else
1535 c_parser_objc_methodproto (parser);
1536 return;
1537 break;
1538 default:
1539 break;
1540 }
1541 /* This is where we parse 'attributes @interface ...',
1542 'attributes @implementation ...', 'attributes @protocol ...'
1543 (where attributes could be, for example, __attribute__
1544 ((deprecated)).
1545 */
1546 switch (c_parser_peek_token (parser)->keyword)
1547 {
1548 case RID_AT_INTERFACE:
1549 {
1550 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1551 return;
1552 c_parser_objc_class_definition (parser, specs->attrs);
1553 return;
1554 }
1555 break;
1556 case RID_AT_IMPLEMENTATION:
1557 {
1558 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1559 return;
1560 if (specs->attrs)
1561 {
1562 warning_at (c_parser_peek_token (parser)->location,
1563 OPT_Wattributes,
1564 "prefix attributes are ignored for implementations");
1565 specs->attrs = NULL_TREE;
1566 }
1567 c_parser_objc_class_definition (parser, NULL_TREE);
1568 return;
1569 }
1570 break;
1571 case RID_AT_PROTOCOL:
1572 {
1573 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1574 return;
1575 c_parser_objc_protocol_definition (parser, specs->attrs);
1576 return;
1577 }
1578 break;
1579 case RID_AT_ALIAS:
1580 case RID_AT_CLASS:
1581 case RID_AT_END:
1582 case RID_AT_PROPERTY:
1583 if (specs->attrs)
1584 {
1585 c_parser_error (parser, "unexpected attribute");
1586 specs->attrs = NULL;
1587 }
1588 break;
1589 default:
1590 break;
1591 }
1592 }
1593
1594 pending_xref_error ();
1595 prefix_attrs = specs->attrs;
1596 all_prefix_attrs = prefix_attrs;
1597 specs->attrs = NULL_TREE;
1598 while (true)
1599 {
1600 struct c_declarator *declarator;
1601 bool dummy = false;
1602 timevar_id_t tv;
1603 tree fnbody;
1604 /* Declaring either one or more declarators (in which case we
1605 should diagnose if there were no declaration specifiers) or a
1606 function definition (in which case the diagnostic for
1607 implicit int suffices). */
1608 declarator = c_parser_declarator (parser,
1609 specs->typespec_kind != ctsk_none,
1610 C_DTR_NORMAL, &dummy);
1611 if (declarator == NULL)
1612 {
1613 c_parser_skip_to_end_of_block_or_statement (parser);
1614 return;
1615 }
1616 if (c_parser_next_token_is (parser, CPP_EQ)
1617 || c_parser_next_token_is (parser, CPP_COMMA)
1618 || c_parser_next_token_is (parser, CPP_SEMICOLON)
1619 || c_parser_next_token_is_keyword (parser, RID_ASM)
1620 || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE)
1621 || c_parser_next_token_is_keyword (parser, RID_IN))
1622 {
1623 tree asm_name = NULL_TREE;
1624 tree postfix_attrs = NULL_TREE;
1625 if (!diagnosed_no_specs && !specs->declspecs_seen_p)
1626 {
1627 diagnosed_no_specs = true;
1628 pedwarn (here, 0, "data definition has no type or storage class");
1629 }
1630 /* Having seen a data definition, there cannot now be a
1631 function definition. */
1632 fndef_ok = false;
1633 if (c_parser_next_token_is_keyword (parser, RID_ASM))
1634 asm_name = c_parser_simple_asm_expr (parser);
1635 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1636 postfix_attrs = c_parser_attributes (parser);
1637 if (c_parser_next_token_is (parser, CPP_EQ))
1638 {
1639 tree d;
1640 struct c_expr init;
1641 location_t init_loc;
1642 c_parser_consume_token (parser);
1643 /* The declaration of the variable is in effect while
1644 its initializer is parsed. */
1645 d = start_decl (declarator, specs, true,
1646 chainon (postfix_attrs, all_prefix_attrs));
1647 if (!d)
1648 d = error_mark_node;
1649 start_init (d, asm_name, global_bindings_p ());
1650 init_loc = c_parser_peek_token (parser)->location;
1651 init = c_parser_initializer (parser);
1652 finish_init ();
1653 if (d != error_mark_node)
1654 {
1655 maybe_warn_string_init (TREE_TYPE (d), init);
1656 finish_decl (d, init_loc, init.value,
1657 init.original_type, asm_name);
1658 }
1659 }
1660 else
1661 {
1662 tree d = start_decl (declarator, specs, false,
1663 chainon (postfix_attrs,
1664 all_prefix_attrs));
1665 if (d)
1666 finish_decl (d, UNKNOWN_LOCATION, NULL_TREE,
1667 NULL_TREE, asm_name);
1668
1669 if (c_parser_next_token_is_keyword (parser, RID_IN))
1670 {
1671 if (d)
1672 *objc_foreach_object_declaration = d;
1673 else
1674 *objc_foreach_object_declaration = error_mark_node;
1675 }
1676 }
1677 if (c_parser_next_token_is (parser, CPP_COMMA))
1678 {
1679 c_parser_consume_token (parser);
1680 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1681 all_prefix_attrs = chainon (c_parser_attributes (parser),
1682 prefix_attrs);
1683 else
1684 all_prefix_attrs = prefix_attrs;
1685 continue;
1686 }
1687 else if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1688 {
1689 c_parser_consume_token (parser);
1690 return;
1691 }
1692 else if (c_parser_next_token_is_keyword (parser, RID_IN))
1693 {
1694 /* This can only happen in Objective-C: we found the
1695 'in' that terminates the declaration inside an
1696 Objective-C foreach statement. Do not consume the
1697 token, so that the caller can use it to determine
1698 that this indeed is a foreach context. */
1699 return;
1700 }
1701 else
1702 {
1703 c_parser_error (parser, "expected %<,%> or %<;%>");
1704 c_parser_skip_to_end_of_block_or_statement (parser);
1705 return;
1706 }
1707 }
1708 else if (!fndef_ok)
1709 {
1710 c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, "
1711 "%<asm%> or %<__attribute__%>");
1712 c_parser_skip_to_end_of_block_or_statement (parser);
1713 return;
1714 }
1715 /* Function definition (nested or otherwise). */
1716 if (nested)
1717 {
1718 pedwarn (here, OPT_pedantic, "ISO C forbids nested functions");
1719 c_push_function_context ();
1720 }
1721 if (!start_function (specs, declarator, all_prefix_attrs))
1722 {
1723 /* This can appear in many cases looking nothing like a
1724 function definition, so we don't give a more specific
1725 error suggesting there was one. */
1726 c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, %<asm%> "
1727 "or %<__attribute__%>");
1728 if (nested)
1729 c_pop_function_context ();
1730 break;
1731 }
1732
1733 if (DECL_DECLARED_INLINE_P (current_function_decl))
1734 tv = TV_PARSE_INLINE;
1735 else
1736 tv = TV_PARSE_FUNC;
1737 timevar_push (tv);
1738
1739 /* Parse old-style parameter declarations. ??? Attributes are
1740 not allowed to start declaration specifiers here because of a
1741 syntax conflict between a function declaration with attribute
1742 suffix and a function definition with an attribute prefix on
1743 first old-style parameter declaration. Following the old
1744 parser, they are not accepted on subsequent old-style
1745 parameter declarations either. However, there is no
1746 ambiguity after the first declaration, nor indeed on the
1747 first as long as we don't allow postfix attributes after a
1748 declarator with a nonempty identifier list in a definition;
1749 and postfix attributes have never been accepted here in
1750 function definitions either. */
1751 while (c_parser_next_token_is_not (parser, CPP_EOF)
1752 && c_parser_next_token_is_not (parser, CPP_OPEN_BRACE))
1753 c_parser_declaration_or_fndef (parser, false, false, false,
1754 true, false, NULL);
1755 store_parm_decls ();
1756 DECL_STRUCT_FUNCTION (current_function_decl)->function_start_locus
1757 = c_parser_peek_token (parser)->location;
1758 fnbody = c_parser_compound_statement (parser);
1759 if (nested)
1760 {
1761 tree decl = current_function_decl;
1762 /* Mark nested functions as needing static-chain initially.
1763 lower_nested_functions will recompute it but the
1764 DECL_STATIC_CHAIN flag is also used before that happens,
1765 by initializer_constant_valid_p. See gcc.dg/nested-fn-2.c. */
1766 DECL_STATIC_CHAIN (decl) = 1;
1767 add_stmt (fnbody);
1768 finish_function ();
1769 c_pop_function_context ();
1770 add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl));
1771 }
1772 else
1773 {
1774 add_stmt (fnbody);
1775 finish_function ();
1776 }
1777
1778 timevar_pop (tv);
1779 break;
1780 }
1781 }
1782
1783 /* Parse an asm-definition (asm() outside a function body). This is a
1784 GNU extension.
1785
1786 asm-definition:
1787 simple-asm-expr ;
1788 */
1789
1790 static void
1791 c_parser_asm_definition (c_parser *parser)
1792 {
1793 tree asm_str = c_parser_simple_asm_expr (parser);
1794 if (asm_str)
1795 cgraph_add_asm_node (asm_str);
1796 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
1797 }
1798
1799 /* Parse a static assertion (C1X N1425 6.7.10).
1800
1801 static_assert-declaration:
1802 static_assert-declaration-no-semi ;
1803 */
1804
1805 static void
1806 c_parser_static_assert_declaration (c_parser *parser)
1807 {
1808 c_parser_static_assert_declaration_no_semi (parser);
1809 if (parser->error
1810 || !c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
1811 c_parser_skip_to_end_of_block_or_statement (parser);
1812 }
1813
1814 /* Parse a static assertion (C1X N1425 6.7.10), without the trailing
1815 semicolon.
1816
1817 static_assert-declaration-no-semi:
1818 _Static_assert ( constant-expression , string-literal )
1819 */
1820
1821 static void
1822 c_parser_static_assert_declaration_no_semi (c_parser *parser)
1823 {
1824 location_t assert_loc, value_loc;
1825 tree value;
1826 tree string;
1827
1828 gcc_assert (c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT));
1829 assert_loc = c_parser_peek_token (parser)->location;
1830 if (!flag_isoc1x)
1831 {
1832 if (flag_isoc99)
1833 pedwarn (assert_loc, OPT_pedantic,
1834 "ISO C99 does not support %<_Static_assert%>");
1835 else
1836 pedwarn (assert_loc, OPT_pedantic,
1837 "ISO C90 does not support %<_Static_assert%>");
1838 }
1839 c_parser_consume_token (parser);
1840 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
1841 return;
1842 value_loc = c_parser_peek_token (parser)->location;
1843 value = c_parser_expr_no_commas (parser, NULL).value;
1844 parser->lex_untranslated_string = true;
1845 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
1846 {
1847 parser->lex_untranslated_string = false;
1848 return;
1849 }
1850 switch (c_parser_peek_token (parser)->type)
1851 {
1852 case CPP_STRING:
1853 case CPP_STRING16:
1854 case CPP_STRING32:
1855 case CPP_WSTRING:
1856 case CPP_UTF8STRING:
1857 string = c_parser_peek_token (parser)->value;
1858 c_parser_consume_token (parser);
1859 parser->lex_untranslated_string = false;
1860 break;
1861 default:
1862 c_parser_error (parser, "expected string literal");
1863 parser->lex_untranslated_string = false;
1864 return;
1865 }
1866 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
1867
1868 if (!INTEGRAL_TYPE_P (TREE_TYPE (value)))
1869 {
1870 error_at (value_loc, "expression in static assertion is not an integer");
1871 return;
1872 }
1873 if (TREE_CODE (value) != INTEGER_CST)
1874 {
1875 value = c_fully_fold (value, false, NULL);
1876 if (TREE_CODE (value) == INTEGER_CST)
1877 pedwarn (value_loc, OPT_pedantic, "expression in static assertion "
1878 "is not an integer constant expression");
1879 }
1880 if (TREE_CODE (value) != INTEGER_CST)
1881 {
1882 error_at (value_loc, "expression in static assertion is not constant");
1883 return;
1884 }
1885 constant_expression_warning (value);
1886 if (integer_zerop (value))
1887 error_at (assert_loc, "static assertion failed: %E", string);
1888 }
1889
1890 /* Parse some declaration specifiers (possibly none) (C90 6.5, C99
1891 6.7), adding them to SPECS (which may already include some).
1892 Storage class specifiers are accepted iff SCSPEC_OK; type
1893 specifiers are accepted iff TYPESPEC_OK; attributes are accepted at
1894 the start iff START_ATTR_OK.
1895
1896 declaration-specifiers:
1897 storage-class-specifier declaration-specifiers[opt]
1898 type-specifier declaration-specifiers[opt]
1899 type-qualifier declaration-specifiers[opt]
1900 function-specifier declaration-specifiers[opt]
1901 alignment-specifier declaration-specifiers[opt]
1902
1903 Function specifiers (inline) are from C99, and are currently
1904 handled as storage class specifiers, as is __thread. Alignment
1905 specifiers are from C1X.
1906
1907 C90 6.5.1, C99 6.7.1:
1908 storage-class-specifier:
1909 typedef
1910 extern
1911 static
1912 auto
1913 register
1914
1915 C99 6.7.4:
1916 function-specifier:
1917 inline
1918 _Noreturn
1919
1920 (_Noreturn is new in C1X.)
1921
1922 C90 6.5.2, C99 6.7.2:
1923 type-specifier:
1924 void
1925 char
1926 short
1927 int
1928 long
1929 float
1930 double
1931 signed
1932 unsigned
1933 _Bool
1934 _Complex
1935 [_Imaginary removed in C99 TC2]
1936 struct-or-union-specifier
1937 enum-specifier
1938 typedef-name
1939
1940 (_Bool and _Complex are new in C99.)
1941
1942 C90 6.5.3, C99 6.7.3:
1943
1944 type-qualifier:
1945 const
1946 restrict
1947 volatile
1948 address-space-qualifier
1949
1950 (restrict is new in C99.)
1951
1952 GNU extensions:
1953
1954 declaration-specifiers:
1955 attributes declaration-specifiers[opt]
1956
1957 type-qualifier:
1958 address-space
1959
1960 address-space:
1961 identifier recognized by the target
1962
1963 storage-class-specifier:
1964 __thread
1965
1966 type-specifier:
1967 typeof-specifier
1968 __int128
1969 _Decimal32
1970 _Decimal64
1971 _Decimal128
1972 _Fract
1973 _Accum
1974 _Sat
1975
1976 (_Fract, _Accum, and _Sat are new from ISO/IEC DTR 18037:
1977 http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1169.pdf)
1978
1979 Objective-C:
1980
1981 type-specifier:
1982 class-name objc-protocol-refs[opt]
1983 typedef-name objc-protocol-refs
1984 objc-protocol-refs
1985 */
1986
1987 static void
1988 c_parser_declspecs (c_parser *parser, struct c_declspecs *specs,
1989 bool scspec_ok, bool typespec_ok, bool start_attr_ok,
1990 enum c_lookahead_kind la)
1991 {
1992 bool attrs_ok = start_attr_ok;
1993 bool seen_type = specs->typespec_kind != ctsk_none;
1994
1995 if (!typespec_ok)
1996 gcc_assert (la == cla_prefer_id);
1997
1998 while (c_parser_next_token_is (parser, CPP_NAME)
1999 || c_parser_next_token_is (parser, CPP_KEYWORD)
2000 || (c_dialect_objc () && c_parser_next_token_is (parser, CPP_LESS)))
2001 {
2002 struct c_typespec t;
2003 tree attrs;
2004 tree align;
2005 location_t loc = c_parser_peek_token (parser)->location;
2006
2007 /* If we cannot accept a type, exit if the next token must start
2008 one. Also, if we already have seen a tagged definition,
2009 a typename would be an error anyway and likely the user
2010 has simply forgotten a semicolon, so we exit. */
2011 if ((!typespec_ok || specs->typespec_kind == ctsk_tagdef)
2012 && c_parser_next_tokens_start_typename (parser, la)
2013 && !c_parser_next_token_is_qualifier (parser))
2014 break;
2015
2016 if (c_parser_next_token_is (parser, CPP_NAME))
2017 {
2018 tree value = c_parser_peek_token (parser)->value;
2019 c_id_kind kind = c_parser_peek_token (parser)->id_kind;
2020
2021 if (kind == C_ID_ADDRSPACE)
2022 {
2023 addr_space_t as
2024 = c_parser_peek_token (parser)->keyword - RID_FIRST_ADDR_SPACE;
2025 declspecs_add_addrspace (specs, as);
2026 c_parser_consume_token (parser);
2027 attrs_ok = true;
2028 continue;
2029 }
2030
2031 gcc_assert (!c_parser_next_token_is_qualifier (parser));
2032
2033 /* If we cannot accept a type, and the next token must start one,
2034 exit. Do the same if we already have seen a tagged definition,
2035 since it would be an error anyway and likely the user has simply
2036 forgotten a semicolon. */
2037 if (seen_type || !c_parser_next_tokens_start_typename (parser, la))
2038 break;
2039
2040 /* Now at an unknown typename (C_ID_ID), a C_ID_TYPENAME or
2041 a C_ID_CLASSNAME. */
2042 c_parser_consume_token (parser);
2043 seen_type = true;
2044 attrs_ok = true;
2045 if (kind == C_ID_ID)
2046 {
2047 error ("unknown type name %qE", value);
2048 t.kind = ctsk_typedef;
2049 t.spec = error_mark_node;
2050 }
2051 else if (kind == C_ID_TYPENAME
2052 && (!c_dialect_objc ()
2053 || c_parser_next_token_is_not (parser, CPP_LESS)))
2054 {
2055 t.kind = ctsk_typedef;
2056 /* For a typedef name, record the meaning, not the name.
2057 In case of 'foo foo, bar;'. */
2058 t.spec = lookup_name (value);
2059 }
2060 else
2061 {
2062 tree proto = NULL_TREE;
2063 gcc_assert (c_dialect_objc ());
2064 t.kind = ctsk_objc;
2065 if (c_parser_next_token_is (parser, CPP_LESS))
2066 proto = c_parser_objc_protocol_refs (parser);
2067 t.spec = objc_get_protocol_qualified_type (value, proto);
2068 }
2069 t.expr = NULL_TREE;
2070 t.expr_const_operands = true;
2071 declspecs_add_type (loc, specs, t);
2072 continue;
2073 }
2074 if (c_parser_next_token_is (parser, CPP_LESS))
2075 {
2076 /* Make "<SomeProtocol>" equivalent to "id <SomeProtocol>" -
2077 nisse@lysator.liu.se. */
2078 tree proto;
2079 gcc_assert (c_dialect_objc ());
2080 if (!typespec_ok || seen_type)
2081 break;
2082 proto = c_parser_objc_protocol_refs (parser);
2083 t.kind = ctsk_objc;
2084 t.spec = objc_get_protocol_qualified_type (NULL_TREE, proto);
2085 t.expr = NULL_TREE;
2086 t.expr_const_operands = true;
2087 declspecs_add_type (loc, specs, t);
2088 continue;
2089 }
2090 gcc_assert (c_parser_next_token_is (parser, CPP_KEYWORD));
2091 switch (c_parser_peek_token (parser)->keyword)
2092 {
2093 case RID_STATIC:
2094 case RID_EXTERN:
2095 case RID_REGISTER:
2096 case RID_TYPEDEF:
2097 case RID_INLINE:
2098 case RID_NORETURN:
2099 case RID_AUTO:
2100 case RID_THREAD:
2101 if (!scspec_ok)
2102 goto out;
2103 attrs_ok = true;
2104 /* TODO: Distinguish between function specifiers (inline, noreturn)
2105 and storage class specifiers, either here or in
2106 declspecs_add_scspec. */
2107 declspecs_add_scspec (specs, c_parser_peek_token (parser)->value);
2108 c_parser_consume_token (parser);
2109 break;
2110 case RID_UNSIGNED:
2111 case RID_LONG:
2112 case RID_INT128:
2113 case RID_SHORT:
2114 case RID_SIGNED:
2115 case RID_COMPLEX:
2116 case RID_INT:
2117 case RID_CHAR:
2118 case RID_FLOAT:
2119 case RID_DOUBLE:
2120 case RID_VOID:
2121 case RID_DFLOAT32:
2122 case RID_DFLOAT64:
2123 case RID_DFLOAT128:
2124 case RID_BOOL:
2125 case RID_FRACT:
2126 case RID_ACCUM:
2127 case RID_SAT:
2128 if (!typespec_ok)
2129 goto out;
2130 attrs_ok = true;
2131 seen_type = true;
2132 if (c_dialect_objc ())
2133 parser->objc_need_raw_identifier = true;
2134 t.kind = ctsk_resword;
2135 t.spec = c_parser_peek_token (parser)->value;
2136 t.expr = NULL_TREE;
2137 t.expr_const_operands = true;
2138 declspecs_add_type (loc, specs, t);
2139 c_parser_consume_token (parser);
2140 break;
2141 case RID_ENUM:
2142 if (!typespec_ok)
2143 goto out;
2144 attrs_ok = true;
2145 seen_type = true;
2146 t = c_parser_enum_specifier (parser);
2147 declspecs_add_type (loc, specs, t);
2148 break;
2149 case RID_STRUCT:
2150 case RID_UNION:
2151 if (!typespec_ok)
2152 goto out;
2153 attrs_ok = true;
2154 seen_type = true;
2155 t = c_parser_struct_or_union_specifier (parser);
2156 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, t.spec);
2157 declspecs_add_type (loc, specs, t);
2158 break;
2159 case RID_TYPEOF:
2160 /* ??? The old parser rejected typeof after other type
2161 specifiers, but is a syntax error the best way of
2162 handling this? */
2163 if (!typespec_ok || seen_type)
2164 goto out;
2165 attrs_ok = true;
2166 seen_type = true;
2167 t = c_parser_typeof_specifier (parser);
2168 declspecs_add_type (loc, specs, t);
2169 break;
2170 case RID_CONST:
2171 case RID_VOLATILE:
2172 case RID_RESTRICT:
2173 attrs_ok = true;
2174 declspecs_add_qual (specs, c_parser_peek_token (parser)->value);
2175 c_parser_consume_token (parser);
2176 break;
2177 case RID_ATTRIBUTE:
2178 if (!attrs_ok)
2179 goto out;
2180 attrs = c_parser_attributes (parser);
2181 declspecs_add_attrs (specs, attrs);
2182 break;
2183 case RID_ALIGNAS:
2184 align = c_parser_alignas_specifier (parser);
2185 declspecs_add_alignas (specs, align);
2186 break;
2187 default:
2188 goto out;
2189 }
2190 }
2191 out: ;
2192 }
2193
2194 /* Parse an enum specifier (C90 6.5.2.2, C99 6.7.2.2).
2195
2196 enum-specifier:
2197 enum attributes[opt] identifier[opt] { enumerator-list } attributes[opt]
2198 enum attributes[opt] identifier[opt] { enumerator-list , } attributes[opt]
2199 enum attributes[opt] identifier
2200
2201 The form with trailing comma is new in C99. The forms with
2202 attributes are GNU extensions. In GNU C, we accept any expression
2203 without commas in the syntax (assignment expressions, not just
2204 conditional expressions); assignment expressions will be diagnosed
2205 as non-constant.
2206
2207 enumerator-list:
2208 enumerator
2209 enumerator-list , enumerator
2210
2211 enumerator:
2212 enumeration-constant
2213 enumeration-constant = constant-expression
2214 */
2215
2216 static struct c_typespec
2217 c_parser_enum_specifier (c_parser *parser)
2218 {
2219 struct c_typespec ret;
2220 tree attrs;
2221 tree ident = NULL_TREE;
2222 location_t enum_loc;
2223 location_t ident_loc = UNKNOWN_LOCATION; /* Quiet warning. */
2224 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ENUM));
2225 enum_loc = c_parser_peek_token (parser)->location;
2226 c_parser_consume_token (parser);
2227 attrs = c_parser_attributes (parser);
2228 enum_loc = c_parser_peek_token (parser)->location;
2229 /* Set the location in case we create a decl now. */
2230 c_parser_set_source_position_from_token (c_parser_peek_token (parser));
2231 if (c_parser_next_token_is (parser, CPP_NAME))
2232 {
2233 ident = c_parser_peek_token (parser)->value;
2234 ident_loc = c_parser_peek_token (parser)->location;
2235 enum_loc = ident_loc;
2236 c_parser_consume_token (parser);
2237 }
2238 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
2239 {
2240 /* Parse an enum definition. */
2241 struct c_enum_contents the_enum;
2242 tree type;
2243 tree postfix_attrs;
2244 /* We chain the enumerators in reverse order, then put them in
2245 forward order at the end. */
2246 tree values;
2247 timevar_push (TV_PARSE_ENUM);
2248 type = start_enum (enum_loc, &the_enum, ident);
2249 values = NULL_TREE;
2250 c_parser_consume_token (parser);
2251 while (true)
2252 {
2253 tree enum_id;
2254 tree enum_value;
2255 tree enum_decl;
2256 bool seen_comma;
2257 c_token *token;
2258 location_t comma_loc = UNKNOWN_LOCATION; /* Quiet warning. */
2259 location_t decl_loc, value_loc;
2260 if (c_parser_next_token_is_not (parser, CPP_NAME))
2261 {
2262 c_parser_error (parser, "expected identifier");
2263 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
2264 values = error_mark_node;
2265 break;
2266 }
2267 token = c_parser_peek_token (parser);
2268 enum_id = token->value;
2269 /* Set the location in case we create a decl now. */
2270 c_parser_set_source_position_from_token (token);
2271 decl_loc = value_loc = token->location;
2272 c_parser_consume_token (parser);
2273 if (c_parser_next_token_is (parser, CPP_EQ))
2274 {
2275 c_parser_consume_token (parser);
2276 value_loc = c_parser_peek_token (parser)->location;
2277 enum_value = c_parser_expr_no_commas (parser, NULL).value;
2278 }
2279 else
2280 enum_value = NULL_TREE;
2281 enum_decl = build_enumerator (decl_loc, value_loc,
2282 &the_enum, enum_id, enum_value);
2283 TREE_CHAIN (enum_decl) = values;
2284 values = enum_decl;
2285 seen_comma = false;
2286 if (c_parser_next_token_is (parser, CPP_COMMA))
2287 {
2288 comma_loc = c_parser_peek_token (parser)->location;
2289 seen_comma = true;
2290 c_parser_consume_token (parser);
2291 }
2292 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2293 {
2294 if (seen_comma && !flag_isoc99)
2295 pedwarn (comma_loc, OPT_pedantic, "comma at end of enumerator list");
2296 c_parser_consume_token (parser);
2297 break;
2298 }
2299 if (!seen_comma)
2300 {
2301 c_parser_error (parser, "expected %<,%> or %<}%>");
2302 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
2303 values = error_mark_node;
2304 break;
2305 }
2306 }
2307 postfix_attrs = c_parser_attributes (parser);
2308 ret.spec = finish_enum (type, nreverse (values),
2309 chainon (attrs, postfix_attrs));
2310 ret.kind = ctsk_tagdef;
2311 ret.expr = NULL_TREE;
2312 ret.expr_const_operands = true;
2313 timevar_pop (TV_PARSE_ENUM);
2314 return ret;
2315 }
2316 else if (!ident)
2317 {
2318 c_parser_error (parser, "expected %<{%>");
2319 ret.spec = error_mark_node;
2320 ret.kind = ctsk_tagref;
2321 ret.expr = NULL_TREE;
2322 ret.expr_const_operands = true;
2323 return ret;
2324 }
2325 ret = parser_xref_tag (ident_loc, ENUMERAL_TYPE, ident);
2326 /* In ISO C, enumerated types can be referred to only if already
2327 defined. */
2328 if (pedantic && !COMPLETE_TYPE_P (ret.spec))
2329 {
2330 gcc_assert (ident);
2331 pedwarn (enum_loc, OPT_pedantic,
2332 "ISO C forbids forward references to %<enum%> types");
2333 }
2334 return ret;
2335 }
2336
2337 /* Parse a struct or union specifier (C90 6.5.2.1, C99 6.7.2.1).
2338
2339 struct-or-union-specifier:
2340 struct-or-union attributes[opt] identifier[opt]
2341 { struct-contents } attributes[opt]
2342 struct-or-union attributes[opt] identifier
2343
2344 struct-contents:
2345 struct-declaration-list
2346
2347 struct-declaration-list:
2348 struct-declaration ;
2349 struct-declaration-list struct-declaration ;
2350
2351 GNU extensions:
2352
2353 struct-contents:
2354 empty
2355 struct-declaration
2356 struct-declaration-list struct-declaration
2357
2358 struct-declaration-list:
2359 struct-declaration-list ;
2360 ;
2361
2362 (Note that in the syntax here, unlike that in ISO C, the semicolons
2363 are included here rather than in struct-declaration, in order to
2364 describe the syntax with extra semicolons and missing semicolon at
2365 end.)
2366
2367 Objective-C:
2368
2369 struct-declaration-list:
2370 @defs ( class-name )
2371
2372 (Note this does not include a trailing semicolon, but can be
2373 followed by further declarations, and gets a pedwarn-if-pedantic
2374 when followed by a semicolon.) */
2375
2376 static struct c_typespec
2377 c_parser_struct_or_union_specifier (c_parser *parser)
2378 {
2379 struct c_typespec ret;
2380 tree attrs;
2381 tree ident = NULL_TREE;
2382 location_t struct_loc;
2383 location_t ident_loc = UNKNOWN_LOCATION;
2384 enum tree_code code;
2385 switch (c_parser_peek_token (parser)->keyword)
2386 {
2387 case RID_STRUCT:
2388 code = RECORD_TYPE;
2389 break;
2390 case RID_UNION:
2391 code = UNION_TYPE;
2392 break;
2393 default:
2394 gcc_unreachable ();
2395 }
2396 struct_loc = c_parser_peek_token (parser)->location;
2397 c_parser_consume_token (parser);
2398 attrs = c_parser_attributes (parser);
2399
2400 /* Set the location in case we create a decl now. */
2401 c_parser_set_source_position_from_token (c_parser_peek_token (parser));
2402
2403 if (c_parser_next_token_is (parser, CPP_NAME))
2404 {
2405 ident = c_parser_peek_token (parser)->value;
2406 ident_loc = c_parser_peek_token (parser)->location;
2407 struct_loc = ident_loc;
2408 c_parser_consume_token (parser);
2409 }
2410 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
2411 {
2412 /* Parse a struct or union definition. Start the scope of the
2413 tag before parsing components. */
2414 struct c_struct_parse_info *struct_info;
2415 tree type = start_struct (struct_loc, code, ident, &struct_info);
2416 tree postfix_attrs;
2417 /* We chain the components in reverse order, then put them in
2418 forward order at the end. Each struct-declaration may
2419 declare multiple components (comma-separated), so we must use
2420 chainon to join them, although when parsing each
2421 struct-declaration we can use TREE_CHAIN directly.
2422
2423 The theory behind all this is that there will be more
2424 semicolon separated fields than comma separated fields, and
2425 so we'll be minimizing the number of node traversals required
2426 by chainon. */
2427 tree contents;
2428 timevar_push (TV_PARSE_STRUCT);
2429 contents = NULL_TREE;
2430 c_parser_consume_token (parser);
2431 /* Handle the Objective-C @defs construct,
2432 e.g. foo(sizeof(struct{ @defs(ClassName) }));. */
2433 if (c_parser_next_token_is_keyword (parser, RID_AT_DEFS))
2434 {
2435 tree name;
2436 gcc_assert (c_dialect_objc ());
2437 c_parser_consume_token (parser);
2438 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2439 goto end_at_defs;
2440 if (c_parser_next_token_is (parser, CPP_NAME)
2441 && c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME)
2442 {
2443 name = c_parser_peek_token (parser)->value;
2444 c_parser_consume_token (parser);
2445 }
2446 else
2447 {
2448 c_parser_error (parser, "expected class name");
2449 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
2450 goto end_at_defs;
2451 }
2452 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2453 "expected %<)%>");
2454 contents = nreverse (objc_get_class_ivars (name));
2455 }
2456 end_at_defs:
2457 /* Parse the struct-declarations and semicolons. Problems with
2458 semicolons are diagnosed here; empty structures are diagnosed
2459 elsewhere. */
2460 while (true)
2461 {
2462 tree decls;
2463 /* Parse any stray semicolon. */
2464 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
2465 {
2466 pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic,
2467 "extra semicolon in struct or union specified");
2468 c_parser_consume_token (parser);
2469 continue;
2470 }
2471 /* Stop if at the end of the struct or union contents. */
2472 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2473 {
2474 c_parser_consume_token (parser);
2475 break;
2476 }
2477 /* Accept #pragmas at struct scope. */
2478 if (c_parser_next_token_is (parser, CPP_PRAGMA))
2479 {
2480 c_parser_pragma (parser, pragma_external);
2481 continue;
2482 }
2483 /* Parse some comma-separated declarations, but not the
2484 trailing semicolon if any. */
2485 decls = c_parser_struct_declaration (parser);
2486 contents = chainon (decls, contents);
2487 /* If no semicolon follows, either we have a parse error or
2488 are at the end of the struct or union and should
2489 pedwarn. */
2490 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
2491 c_parser_consume_token (parser);
2492 else
2493 {
2494 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2495 pedwarn (c_parser_peek_token (parser)->location, 0,
2496 "no semicolon at end of struct or union");
2497 else if (parser->error
2498 || !c_parser_next_token_starts_declspecs (parser))
2499 {
2500 c_parser_error (parser, "expected %<;%>");
2501 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
2502 break;
2503 }
2504
2505 /* If we come here, we have already emitted an error
2506 for an expected `;', identifier or `(', and we also
2507 recovered already. Go on with the next field. */
2508 }
2509 }
2510 postfix_attrs = c_parser_attributes (parser);
2511 ret.spec = finish_struct (struct_loc, type, nreverse (contents),
2512 chainon (attrs, postfix_attrs), struct_info);
2513 ret.kind = ctsk_tagdef;
2514 ret.expr = NULL_TREE;
2515 ret.expr_const_operands = true;
2516 timevar_pop (TV_PARSE_STRUCT);
2517 return ret;
2518 }
2519 else if (!ident)
2520 {
2521 c_parser_error (parser, "expected %<{%>");
2522 ret.spec = error_mark_node;
2523 ret.kind = ctsk_tagref;
2524 ret.expr = NULL_TREE;
2525 ret.expr_const_operands = true;
2526 return ret;
2527 }
2528 ret = parser_xref_tag (ident_loc, code, ident);
2529 return ret;
2530 }
2531
2532 /* Parse a struct-declaration (C90 6.5.2.1, C99 6.7.2.1), *without*
2533 the trailing semicolon.
2534
2535 struct-declaration:
2536 specifier-qualifier-list struct-declarator-list
2537 static_assert-declaration-no-semi
2538
2539 specifier-qualifier-list:
2540 type-specifier specifier-qualifier-list[opt]
2541 type-qualifier specifier-qualifier-list[opt]
2542 attributes specifier-qualifier-list[opt]
2543
2544 struct-declarator-list:
2545 struct-declarator
2546 struct-declarator-list , attributes[opt] struct-declarator
2547
2548 struct-declarator:
2549 declarator attributes[opt]
2550 declarator[opt] : constant-expression attributes[opt]
2551
2552 GNU extensions:
2553
2554 struct-declaration:
2555 __extension__ struct-declaration
2556 specifier-qualifier-list
2557
2558 Unlike the ISO C syntax, semicolons are handled elsewhere. The use
2559 of attributes where shown is a GNU extension. In GNU C, we accept
2560 any expression without commas in the syntax (assignment
2561 expressions, not just conditional expressions); assignment
2562 expressions will be diagnosed as non-constant. */
2563
2564 static tree
2565 c_parser_struct_declaration (c_parser *parser)
2566 {
2567 struct c_declspecs *specs;
2568 tree prefix_attrs;
2569 tree all_prefix_attrs;
2570 tree decls;
2571 location_t decl_loc;
2572 if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
2573 {
2574 int ext;
2575 tree decl;
2576 ext = disable_extension_diagnostics ();
2577 c_parser_consume_token (parser);
2578 decl = c_parser_struct_declaration (parser);
2579 restore_extension_diagnostics (ext);
2580 return decl;
2581 }
2582 if (c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT))
2583 {
2584 c_parser_static_assert_declaration_no_semi (parser);
2585 return NULL_TREE;
2586 }
2587 specs = build_null_declspecs ();
2588 decl_loc = c_parser_peek_token (parser)->location;
2589 c_parser_declspecs (parser, specs, false, true, true, cla_nonabstract_decl);
2590 if (parser->error)
2591 return NULL_TREE;
2592 if (!specs->declspecs_seen_p)
2593 {
2594 c_parser_error (parser, "expected specifier-qualifier-list");
2595 return NULL_TREE;
2596 }
2597 finish_declspecs (specs);
2598 if (c_parser_next_token_is (parser, CPP_SEMICOLON)
2599 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2600 {
2601 tree ret;
2602 if (specs->typespec_kind == ctsk_none)
2603 {
2604 pedwarn (decl_loc, OPT_pedantic,
2605 "ISO C forbids member declarations with no members");
2606 shadow_tag_warned (specs, pedantic);
2607 ret = NULL_TREE;
2608 }
2609 else
2610 {
2611 /* Support for unnamed structs or unions as members of
2612 structs or unions (which is [a] useful and [b] supports
2613 MS P-SDK). */
2614 tree attrs = NULL;
2615
2616 ret = grokfield (c_parser_peek_token (parser)->location,
2617 build_id_declarator (NULL_TREE), specs,
2618 NULL_TREE, &attrs);
2619 if (ret)
2620 decl_attributes (&ret, attrs, 0);
2621 }
2622 return ret;
2623 }
2624
2625 /* Provide better error recovery. Note that a type name here is valid,
2626 and will be treated as a field name. */
2627 if (specs->typespec_kind == ctsk_tagdef
2628 && TREE_CODE (specs->type) != ENUMERAL_TYPE
2629 && c_parser_next_token_starts_declspecs (parser)
2630 && !c_parser_next_token_is (parser, CPP_NAME))
2631 {
2632 c_parser_error (parser, "expected %<;%>, identifier or %<(%>");
2633 parser->error = false;
2634 return NULL_TREE;
2635 }
2636
2637 pending_xref_error ();
2638 prefix_attrs = specs->attrs;
2639 all_prefix_attrs = prefix_attrs;
2640 specs->attrs = NULL_TREE;
2641 decls = NULL_TREE;
2642 while (true)
2643 {
2644 /* Declaring one or more declarators or un-named bit-fields. */
2645 struct c_declarator *declarator;
2646 bool dummy = false;
2647 if (c_parser_next_token_is (parser, CPP_COLON))
2648 declarator = build_id_declarator (NULL_TREE);
2649 else
2650 declarator = c_parser_declarator (parser,
2651 specs->typespec_kind != ctsk_none,
2652 C_DTR_NORMAL, &dummy);
2653 if (declarator == NULL)
2654 {
2655 c_parser_skip_to_end_of_block_or_statement (parser);
2656 break;
2657 }
2658 if (c_parser_next_token_is (parser, CPP_COLON)
2659 || c_parser_next_token_is (parser, CPP_COMMA)
2660 || c_parser_next_token_is (parser, CPP_SEMICOLON)
2661 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE)
2662 || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2663 {
2664 tree postfix_attrs = NULL_TREE;
2665 tree width = NULL_TREE;
2666 tree d;
2667 if (c_parser_next_token_is (parser, CPP_COLON))
2668 {
2669 c_parser_consume_token (parser);
2670 width = c_parser_expr_no_commas (parser, NULL).value;
2671 }
2672 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2673 postfix_attrs = c_parser_attributes (parser);
2674 d = grokfield (c_parser_peek_token (parser)->location,
2675 declarator, specs, width, &all_prefix_attrs);
2676 decl_attributes (&d, chainon (postfix_attrs,
2677 all_prefix_attrs), 0);
2678 DECL_CHAIN (d) = decls;
2679 decls = d;
2680 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2681 all_prefix_attrs = chainon (c_parser_attributes (parser),
2682 prefix_attrs);
2683 else
2684 all_prefix_attrs = prefix_attrs;
2685 if (c_parser_next_token_is (parser, CPP_COMMA))
2686 c_parser_consume_token (parser);
2687 else if (c_parser_next_token_is (parser, CPP_SEMICOLON)
2688 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2689 {
2690 /* Semicolon consumed in caller. */
2691 break;
2692 }
2693 else
2694 {
2695 c_parser_error (parser, "expected %<,%>, %<;%> or %<}%>");
2696 break;
2697 }
2698 }
2699 else
2700 {
2701 c_parser_error (parser,
2702 "expected %<:%>, %<,%>, %<;%>, %<}%> or "
2703 "%<__attribute__%>");
2704 break;
2705 }
2706 }
2707 return decls;
2708 }
2709
2710 /* Parse a typeof specifier (a GNU extension).
2711
2712 typeof-specifier:
2713 typeof ( expression )
2714 typeof ( type-name )
2715 */
2716
2717 static struct c_typespec
2718 c_parser_typeof_specifier (c_parser *parser)
2719 {
2720 struct c_typespec ret;
2721 ret.kind = ctsk_typeof;
2722 ret.spec = error_mark_node;
2723 ret.expr = NULL_TREE;
2724 ret.expr_const_operands = true;
2725 gcc_assert (c_parser_next_token_is_keyword (parser, RID_TYPEOF));
2726 c_parser_consume_token (parser);
2727 c_inhibit_evaluation_warnings++;
2728 in_typeof++;
2729 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2730 {
2731 c_inhibit_evaluation_warnings--;
2732 in_typeof--;
2733 return ret;
2734 }
2735 if (c_parser_next_tokens_start_typename (parser, cla_prefer_id))
2736 {
2737 struct c_type_name *type = c_parser_type_name (parser);
2738 c_inhibit_evaluation_warnings--;
2739 in_typeof--;
2740 if (type != NULL)
2741 {
2742 ret.spec = groktypename (type, &ret.expr, &ret.expr_const_operands);
2743 pop_maybe_used (variably_modified_type_p (ret.spec, NULL_TREE));
2744 }
2745 }
2746 else
2747 {
2748 bool was_vm;
2749 location_t here = c_parser_peek_token (parser)->location;
2750 struct c_expr expr = c_parser_expression (parser);
2751 c_inhibit_evaluation_warnings--;
2752 in_typeof--;
2753 if (TREE_CODE (expr.value) == COMPONENT_REF
2754 && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
2755 error_at (here, "%<typeof%> applied to a bit-field");
2756 mark_exp_read (expr.value);
2757 ret.spec = TREE_TYPE (expr.value);
2758 was_vm = variably_modified_type_p (ret.spec, NULL_TREE);
2759 /* This is returned with the type so that when the type is
2760 evaluated, this can be evaluated. */
2761 if (was_vm)
2762 ret.expr = c_fully_fold (expr.value, false, &ret.expr_const_operands);
2763 pop_maybe_used (was_vm);
2764 }
2765 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
2766 return ret;
2767 }
2768
2769 /* Parse an alignment-specifier.
2770
2771 C1X 6.7.5:
2772
2773 alignment-specifier:
2774 _Alignas ( type-name )
2775 _Alignas ( constant-expression )
2776 */
2777
2778 static tree
2779 c_parser_alignas_specifier (c_parser * parser)
2780 {
2781 tree ret = error_mark_node;
2782 location_t loc = c_parser_peek_token (parser)->location;
2783 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ALIGNAS));
2784 c_parser_consume_token (parser);
2785 if (!flag_isoc1x)
2786 {
2787 if (flag_isoc99)
2788 pedwarn (loc, OPT_pedantic,
2789 "ISO C99 does not support %<_Alignas%>");
2790 else
2791 pedwarn (loc, OPT_pedantic,
2792 "ISO C90 does not support %<_Alignas%>");
2793 }
2794 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2795 return ret;
2796 if (c_parser_next_tokens_start_typename (parser, cla_prefer_id))
2797 {
2798 struct c_type_name *type = c_parser_type_name (parser);
2799 if (type != NULL)
2800 ret = c_alignof (loc, groktypename (type, NULL, NULL));
2801 }
2802 else
2803 ret = c_parser_expr_no_commas (parser, NULL).value;
2804 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
2805 return ret;
2806 }
2807
2808 /* Parse a declarator, possibly an abstract declarator (C90 6.5.4,
2809 6.5.5, C99 6.7.5, 6.7.6). If TYPE_SEEN_P then a typedef name may
2810 be redeclared; otherwise it may not. KIND indicates which kind of
2811 declarator is wanted. Returns a valid declarator except in the
2812 case of a syntax error in which case NULL is returned. *SEEN_ID is
2813 set to true if an identifier being declared is seen; this is used
2814 to diagnose bad forms of abstract array declarators and to
2815 determine whether an identifier list is syntactically permitted.
2816
2817 declarator:
2818 pointer[opt] direct-declarator
2819
2820 direct-declarator:
2821 identifier
2822 ( attributes[opt] declarator )
2823 direct-declarator array-declarator
2824 direct-declarator ( parameter-type-list )
2825 direct-declarator ( identifier-list[opt] )
2826
2827 pointer:
2828 * type-qualifier-list[opt]
2829 * type-qualifier-list[opt] pointer
2830
2831 type-qualifier-list:
2832 type-qualifier
2833 attributes
2834 type-qualifier-list type-qualifier
2835 type-qualifier-list attributes
2836
2837 parameter-type-list:
2838 parameter-list
2839 parameter-list , ...
2840
2841 parameter-list:
2842 parameter-declaration
2843 parameter-list , parameter-declaration
2844
2845 parameter-declaration:
2846 declaration-specifiers declarator attributes[opt]
2847 declaration-specifiers abstract-declarator[opt] attributes[opt]
2848
2849 identifier-list:
2850 identifier
2851 identifier-list , identifier
2852
2853 abstract-declarator:
2854 pointer
2855 pointer[opt] direct-abstract-declarator
2856
2857 direct-abstract-declarator:
2858 ( attributes[opt] abstract-declarator )
2859 direct-abstract-declarator[opt] array-declarator
2860 direct-abstract-declarator[opt] ( parameter-type-list[opt] )
2861
2862 GNU extensions:
2863
2864 direct-declarator:
2865 direct-declarator ( parameter-forward-declarations
2866 parameter-type-list[opt] )
2867
2868 direct-abstract-declarator:
2869 direct-abstract-declarator[opt] ( parameter-forward-declarations
2870 parameter-type-list[opt] )
2871
2872 parameter-forward-declarations:
2873 parameter-list ;
2874 parameter-forward-declarations parameter-list ;
2875
2876 The uses of attributes shown above are GNU extensions.
2877
2878 Some forms of array declarator are not included in C99 in the
2879 syntax for abstract declarators; these are disallowed elsewhere.
2880 This may be a defect (DR#289).
2881
2882 This function also accepts an omitted abstract declarator as being
2883 an abstract declarator, although not part of the formal syntax. */
2884
2885 static struct c_declarator *
2886 c_parser_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
2887 bool *seen_id)
2888 {
2889 /* Parse any initial pointer part. */
2890 if (c_parser_next_token_is (parser, CPP_MULT))
2891 {
2892 struct c_declspecs *quals_attrs = build_null_declspecs ();
2893 struct c_declarator *inner;
2894 c_parser_consume_token (parser);
2895 c_parser_declspecs (parser, quals_attrs, false, false, true, cla_prefer_id);
2896 inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
2897 if (inner == NULL)
2898 return NULL;
2899 else
2900 return make_pointer_declarator (quals_attrs, inner);
2901 }
2902 /* Now we have a direct declarator, direct abstract declarator or
2903 nothing (which counts as a direct abstract declarator here). */
2904 return c_parser_direct_declarator (parser, type_seen_p, kind, seen_id);
2905 }
2906
2907 /* Parse a direct declarator or direct abstract declarator; arguments
2908 as c_parser_declarator. */
2909
2910 static struct c_declarator *
2911 c_parser_direct_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
2912 bool *seen_id)
2913 {
2914 /* The direct declarator must start with an identifier (possibly
2915 omitted) or a parenthesized declarator (possibly abstract). In
2916 an ordinary declarator, initial parentheses must start a
2917 parenthesized declarator. In an abstract declarator or parameter
2918 declarator, they could start a parenthesized declarator or a
2919 parameter list. To tell which, the open parenthesis and any
2920 following attributes must be read. If a declaration specifier
2921 follows, then it is a parameter list; if the specifier is a
2922 typedef name, there might be an ambiguity about redeclaring it,
2923 which is resolved in the direction of treating it as a typedef
2924 name. If a close parenthesis follows, it is also an empty
2925 parameter list, as the syntax does not permit empty abstract
2926 declarators. Otherwise, it is a parenthesized declarator (in
2927 which case the analysis may be repeated inside it, recursively).
2928
2929 ??? There is an ambiguity in a parameter declaration "int
2930 (__attribute__((foo)) x)", where x is not a typedef name: it
2931 could be an abstract declarator for a function, or declare x with
2932 parentheses. The proper resolution of this ambiguity needs
2933 documenting. At present we follow an accident of the old
2934 parser's implementation, whereby the first parameter must have
2935 some declaration specifiers other than just attributes. Thus as
2936 a parameter declaration it is treated as a parenthesized
2937 parameter named x, and as an abstract declarator it is
2938 rejected.
2939
2940 ??? Also following the old parser, attributes inside an empty
2941 parameter list are ignored, making it a list not yielding a
2942 prototype, rather than giving an error or making it have one
2943 parameter with implicit type int.
2944
2945 ??? Also following the old parser, typedef names may be
2946 redeclared in declarators, but not Objective-C class names. */
2947
2948 if (kind != C_DTR_ABSTRACT
2949 && c_parser_next_token_is (parser, CPP_NAME)
2950 && ((type_seen_p
2951 && (c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME
2952 || c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME))
2953 || c_parser_peek_token (parser)->id_kind == C_ID_ID))
2954 {
2955 struct c_declarator *inner
2956 = build_id_declarator (c_parser_peek_token (parser)->value);
2957 *seen_id = true;
2958 inner->id_loc = c_parser_peek_token (parser)->location;
2959 c_parser_consume_token (parser);
2960 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
2961 }
2962
2963 if (kind != C_DTR_NORMAL
2964 && c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
2965 {
2966 struct c_declarator *inner = build_id_declarator (NULL_TREE);
2967 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
2968 }
2969
2970 /* Either we are at the end of an abstract declarator, or we have
2971 parentheses. */
2972
2973 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
2974 {
2975 tree attrs;
2976 struct c_declarator *inner;
2977 c_parser_consume_token (parser);
2978 attrs = c_parser_attributes (parser);
2979 if (kind != C_DTR_NORMAL
2980 && (c_parser_next_token_starts_declspecs (parser)
2981 || c_parser_next_token_is (parser, CPP_CLOSE_PAREN)))
2982 {
2983 struct c_arg_info *args
2984 = c_parser_parms_declarator (parser, kind == C_DTR_NORMAL,
2985 attrs);
2986 if (args == NULL)
2987 return NULL;
2988 else
2989 {
2990 inner
2991 = build_function_declarator (args,
2992 build_id_declarator (NULL_TREE));
2993 return c_parser_direct_declarator_inner (parser, *seen_id,
2994 inner);
2995 }
2996 }
2997 /* A parenthesized declarator. */
2998 inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
2999 if (inner != NULL && attrs != NULL)
3000 inner = build_attrs_declarator (attrs, inner);
3001 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3002 {
3003 c_parser_consume_token (parser);
3004 if (inner == NULL)
3005 return NULL;
3006 else
3007 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
3008 }
3009 else
3010 {
3011 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3012 "expected %<)%>");
3013 return NULL;
3014 }
3015 }
3016 else
3017 {
3018 if (kind == C_DTR_NORMAL)
3019 {
3020 c_parser_error (parser, "expected identifier or %<(%>");
3021 return NULL;
3022 }
3023 else
3024 return build_id_declarator (NULL_TREE);
3025 }
3026 }
3027
3028 /* Parse part of a direct declarator or direct abstract declarator,
3029 given that some (in INNER) has already been parsed; ID_PRESENT is
3030 true if an identifier is present, false for an abstract
3031 declarator. */
3032
3033 static struct c_declarator *
3034 c_parser_direct_declarator_inner (c_parser *parser, bool id_present,
3035 struct c_declarator *inner)
3036 {
3037 /* Parse a sequence of array declarators and parameter lists. */
3038 if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
3039 {
3040 location_t brace_loc = c_parser_peek_token (parser)->location;
3041 struct c_declarator *declarator;
3042 struct c_declspecs *quals_attrs = build_null_declspecs ();
3043 bool static_seen;
3044 bool star_seen;
3045 tree dimen;
3046 c_parser_consume_token (parser);
3047 c_parser_declspecs (parser, quals_attrs, false, false, true, cla_prefer_id);
3048 static_seen = c_parser_next_token_is_keyword (parser, RID_STATIC);
3049 if (static_seen)
3050 c_parser_consume_token (parser);
3051 if (static_seen && !quals_attrs->declspecs_seen_p)
3052 c_parser_declspecs (parser, quals_attrs, false, false, true, cla_prefer_id);
3053 if (!quals_attrs->declspecs_seen_p)
3054 quals_attrs = NULL;
3055 /* If "static" is present, there must be an array dimension.
3056 Otherwise, there may be a dimension, "*", or no
3057 dimension. */
3058 if (static_seen)
3059 {
3060 star_seen = false;
3061 dimen = c_parser_expr_no_commas (parser, NULL).value;
3062 }
3063 else
3064 {
3065 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3066 {
3067 dimen = NULL_TREE;
3068 star_seen = false;
3069 }
3070 else if (c_parser_next_token_is (parser, CPP_MULT))
3071 {
3072 if (c_parser_peek_2nd_token (parser)->type == CPP_CLOSE_SQUARE)
3073 {
3074 dimen = NULL_TREE;
3075 star_seen = true;
3076 c_parser_consume_token (parser);
3077 }
3078 else
3079 {
3080 star_seen = false;
3081 dimen = c_parser_expr_no_commas (parser, NULL).value;
3082 }
3083 }
3084 else
3085 {
3086 star_seen = false;
3087 dimen = c_parser_expr_no_commas (parser, NULL).value;
3088 }
3089 }
3090 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3091 c_parser_consume_token (parser);
3092 else
3093 {
3094 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
3095 "expected %<]%>");
3096 return NULL;
3097 }
3098 if (dimen)
3099 mark_exp_read (dimen);
3100 declarator = build_array_declarator (brace_loc, dimen, quals_attrs,
3101 static_seen, star_seen);
3102 if (declarator == NULL)
3103 return NULL;
3104 inner = set_array_declarator_inner (declarator, inner);
3105 return c_parser_direct_declarator_inner (parser, id_present, inner);
3106 }
3107 else if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
3108 {
3109 tree attrs;
3110 struct c_arg_info *args;
3111 c_parser_consume_token (parser);
3112 attrs = c_parser_attributes (parser);
3113 args = c_parser_parms_declarator (parser, id_present, attrs);
3114 if (args == NULL)
3115 return NULL;
3116 else
3117 {
3118 inner = build_function_declarator (args, inner);
3119 return c_parser_direct_declarator_inner (parser, id_present, inner);
3120 }
3121 }
3122 return inner;
3123 }
3124
3125 /* Parse a parameter list or identifier list, including the closing
3126 parenthesis but not the opening one. ATTRS are the attributes at
3127 the start of the list. ID_LIST_OK is true if an identifier list is
3128 acceptable; such a list must not have attributes at the start. */
3129
3130 static struct c_arg_info *
3131 c_parser_parms_declarator (c_parser *parser, bool id_list_ok, tree attrs)
3132 {
3133 push_scope ();
3134 declare_parm_level ();
3135 /* If the list starts with an identifier, it is an identifier list.
3136 Otherwise, it is either a prototype list or an empty list. */
3137 if (id_list_ok
3138 && !attrs
3139 && c_parser_next_token_is (parser, CPP_NAME)
3140 && c_parser_peek_token (parser)->id_kind == C_ID_ID
3141
3142 /* Look ahead to detect typos in type names. */
3143 && c_parser_peek_2nd_token (parser)->type != CPP_NAME
3144 && c_parser_peek_2nd_token (parser)->type != CPP_MULT
3145 && c_parser_peek_2nd_token (parser)->type != CPP_OPEN_PAREN
3146 && c_parser_peek_2nd_token (parser)->type != CPP_OPEN_SQUARE)
3147 {
3148 tree list = NULL_TREE, *nextp = &list;
3149 while (c_parser_next_token_is (parser, CPP_NAME)
3150 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
3151 {
3152 *nextp = build_tree_list (NULL_TREE,
3153 c_parser_peek_token (parser)->value);
3154 nextp = & TREE_CHAIN (*nextp);
3155 c_parser_consume_token (parser);
3156 if (c_parser_next_token_is_not (parser, CPP_COMMA))
3157 break;
3158 c_parser_consume_token (parser);
3159 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3160 {
3161 c_parser_error (parser, "expected identifier");
3162 break;
3163 }
3164 }
3165 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3166 {
3167 struct c_arg_info *ret = build_arg_info ();
3168 ret->types = list;
3169 c_parser_consume_token (parser);
3170 pop_scope ();
3171 return ret;
3172 }
3173 else
3174 {
3175 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3176 "expected %<)%>");
3177 pop_scope ();
3178 return NULL;
3179 }
3180 }
3181 else
3182 {
3183 struct c_arg_info *ret = c_parser_parms_list_declarator (parser, attrs,
3184 NULL);
3185 pop_scope ();
3186 return ret;
3187 }
3188 }
3189
3190 /* Parse a parameter list (possibly empty), including the closing
3191 parenthesis but not the opening one. ATTRS are the attributes at
3192 the start of the list. EXPR is NULL or an expression that needs to
3193 be evaluated for the side effects of array size expressions in the
3194 parameters. */
3195
3196 static struct c_arg_info *
3197 c_parser_parms_list_declarator (c_parser *parser, tree attrs, tree expr)
3198 {
3199 bool bad_parm = false;
3200
3201 /* ??? Following the old parser, forward parameter declarations may
3202 use abstract declarators, and if no real parameter declarations
3203 follow the forward declarations then this is not diagnosed. Also
3204 note as above that attributes are ignored as the only contents of
3205 the parentheses, or as the only contents after forward
3206 declarations. */
3207 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3208 {
3209 struct c_arg_info *ret = build_arg_info ();
3210 c_parser_consume_token (parser);
3211 return ret;
3212 }
3213 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3214 {
3215 struct c_arg_info *ret = build_arg_info ();
3216
3217 if (flag_allow_parameterless_variadic_functions)
3218 {
3219 /* F (...) is allowed. */
3220 ret->types = NULL_TREE;
3221 }
3222 else
3223 {
3224 /* Suppress -Wold-style-definition for this case. */
3225 ret->types = error_mark_node;
3226 error_at (c_parser_peek_token (parser)->location,
3227 "ISO C requires a named argument before %<...%>");
3228 }
3229 c_parser_consume_token (parser);
3230 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3231 {
3232 c_parser_consume_token (parser);
3233 return ret;
3234 }
3235 else
3236 {
3237 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3238 "expected %<)%>");
3239 return NULL;
3240 }
3241 }
3242 /* Nonempty list of parameters, either terminated with semicolon
3243 (forward declarations; recurse) or with close parenthesis (normal
3244 function) or with ", ... )" (variadic function). */
3245 while (true)
3246 {
3247 /* Parse a parameter. */
3248 struct c_parm *parm = c_parser_parameter_declaration (parser, attrs);
3249 attrs = NULL_TREE;
3250 if (parm == NULL)
3251 bad_parm = true;
3252 else
3253 push_parm_decl (parm, &expr);
3254 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
3255 {
3256 tree new_attrs;
3257 c_parser_consume_token (parser);
3258 mark_forward_parm_decls ();
3259 new_attrs = c_parser_attributes (parser);
3260 return c_parser_parms_list_declarator (parser, new_attrs, expr);
3261 }
3262 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3263 {
3264 c_parser_consume_token (parser);
3265 if (bad_parm)
3266 return NULL;
3267 else
3268 return get_parm_info (false, expr);
3269 }
3270 if (!c_parser_require (parser, CPP_COMMA,
3271 "expected %<;%>, %<,%> or %<)%>"))
3272 {
3273 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
3274 return NULL;
3275 }
3276 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3277 {
3278 c_parser_consume_token (parser);
3279 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3280 {
3281 c_parser_consume_token (parser);
3282 if (bad_parm)
3283 return NULL;
3284 else
3285 return get_parm_info (true, expr);
3286 }
3287 else
3288 {
3289 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3290 "expected %<)%>");
3291 return NULL;
3292 }
3293 }
3294 }
3295 }
3296
3297 /* Parse a parameter declaration. ATTRS are the attributes at the
3298 start of the declaration if it is the first parameter. */
3299
3300 static struct c_parm *
3301 c_parser_parameter_declaration (c_parser *parser, tree attrs)
3302 {
3303 struct c_declspecs *specs;
3304 struct c_declarator *declarator;
3305 tree prefix_attrs;
3306 tree postfix_attrs = NULL_TREE;
3307 bool dummy = false;
3308 if (!c_parser_next_token_starts_declspecs (parser))
3309 {
3310 c_token *token = c_parser_peek_token (parser);
3311 if (parser->error)
3312 return NULL;
3313 c_parser_set_source_position_from_token (token);
3314 if (c_parser_next_tokens_start_typename (parser, cla_prefer_type))
3315 {
3316 error ("unknown type name %qE", token->value);
3317 parser->error = true;
3318 }
3319 /* ??? In some Objective-C cases '...' isn't applicable so there
3320 should be a different message. */
3321 else
3322 c_parser_error (parser,
3323 "expected declaration specifiers or %<...%>");
3324 c_parser_skip_to_end_of_parameter (parser);
3325 return NULL;
3326 }
3327 specs = build_null_declspecs ();
3328 if (attrs)
3329 {
3330 declspecs_add_attrs (specs, attrs);
3331 attrs = NULL_TREE;
3332 }
3333 c_parser_declspecs (parser, specs, true, true, true, cla_nonabstract_decl);
3334 finish_declspecs (specs);
3335 pending_xref_error ();
3336 prefix_attrs = specs->attrs;
3337 specs->attrs = NULL_TREE;
3338 declarator = c_parser_declarator (parser,
3339 specs->typespec_kind != ctsk_none,
3340 C_DTR_PARM, &dummy);
3341 if (declarator == NULL)
3342 {
3343 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
3344 return NULL;
3345 }
3346 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
3347 postfix_attrs = c_parser_attributes (parser);
3348 return build_c_parm (specs, chainon (postfix_attrs, prefix_attrs),
3349 declarator);
3350 }
3351
3352 /* Parse a string literal in an asm expression. It should not be
3353 translated, and wide string literals are an error although
3354 permitted by the syntax. This is a GNU extension.
3355
3356 asm-string-literal:
3357 string-literal
3358
3359 ??? At present, following the old parser, the caller needs to have
3360 set lex_untranslated_string to 1. It would be better to follow the
3361 C++ parser rather than using this kludge. */
3362
3363 static tree
3364 c_parser_asm_string_literal (c_parser *parser)
3365 {
3366 tree str;
3367 int save_flag = warn_overlength_strings;
3368 warn_overlength_strings = 0;
3369 if (c_parser_next_token_is (parser, CPP_STRING))
3370 {
3371 str = c_parser_peek_token (parser)->value;
3372 c_parser_consume_token (parser);
3373 }
3374 else if (c_parser_next_token_is (parser, CPP_WSTRING))
3375 {
3376 error_at (c_parser_peek_token (parser)->location,
3377 "wide string literal in %<asm%>");
3378 str = build_string (1, "");
3379 c_parser_consume_token (parser);
3380 }
3381 else
3382 {
3383 c_parser_error (parser, "expected string literal");
3384 str = NULL_TREE;
3385 }
3386 warn_overlength_strings = save_flag;
3387 return str;
3388 }
3389
3390 /* Parse a simple asm expression. This is used in restricted
3391 contexts, where a full expression with inputs and outputs does not
3392 make sense. This is a GNU extension.
3393
3394 simple-asm-expr:
3395 asm ( asm-string-literal )
3396 */
3397
3398 static tree
3399 c_parser_simple_asm_expr (c_parser *parser)
3400 {
3401 tree str;
3402 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
3403 /* ??? Follow the C++ parser rather than using the
3404 lex_untranslated_string kludge. */
3405 parser->lex_untranslated_string = true;
3406 c_parser_consume_token (parser);
3407 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3408 {
3409 parser->lex_untranslated_string = false;
3410 return NULL_TREE;
3411 }
3412 str = c_parser_asm_string_literal (parser);
3413 parser->lex_untranslated_string = false;
3414 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
3415 {
3416 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
3417 return NULL_TREE;
3418 }
3419 return str;
3420 }
3421
3422 static tree
3423 c_parser_attribute_any_word (c_parser *parser)
3424 {
3425 tree attr_name = NULL_TREE;
3426
3427 if (c_parser_next_token_is (parser, CPP_KEYWORD))
3428 {
3429 /* ??? See comment above about what keywords are accepted here. */
3430 bool ok;
3431 switch (c_parser_peek_token (parser)->keyword)
3432 {
3433 case RID_STATIC:
3434 case RID_UNSIGNED:
3435 case RID_LONG:
3436 case RID_INT128:
3437 case RID_CONST:
3438 case RID_EXTERN:
3439 case RID_REGISTER:
3440 case RID_TYPEDEF:
3441 case RID_SHORT:
3442 case RID_INLINE:
3443 case RID_NORETURN:
3444 case RID_VOLATILE:
3445 case RID_SIGNED:
3446 case RID_AUTO:
3447 case RID_RESTRICT:
3448 case RID_COMPLEX:
3449 case RID_THREAD:
3450 case RID_INT:
3451 case RID_CHAR:
3452 case RID_FLOAT:
3453 case RID_DOUBLE:
3454 case RID_VOID:
3455 case RID_DFLOAT32:
3456 case RID_DFLOAT64:
3457 case RID_DFLOAT128:
3458 case RID_BOOL:
3459 case RID_FRACT:
3460 case RID_ACCUM:
3461 case RID_SAT:
3462 case RID_TRANSACTION_ATOMIC:
3463 case RID_TRANSACTION_CANCEL:
3464 ok = true;
3465 break;
3466 default:
3467 ok = false;
3468 break;
3469 }
3470 if (!ok)
3471 return NULL_TREE;
3472
3473 /* Accept __attribute__((__const)) as __attribute__((const)) etc. */
3474 attr_name = ridpointers[(int) c_parser_peek_token (parser)->keyword];
3475 }
3476 else if (c_parser_next_token_is (parser, CPP_NAME))
3477 attr_name = c_parser_peek_token (parser)->value;
3478
3479 return attr_name;
3480 }
3481
3482 /* Parse (possibly empty) attributes. This is a GNU extension.
3483
3484 attributes:
3485 empty
3486 attributes attribute
3487
3488 attribute:
3489 __attribute__ ( ( attribute-list ) )
3490
3491 attribute-list:
3492 attrib
3493 attribute_list , attrib
3494
3495 attrib:
3496 empty
3497 any-word
3498 any-word ( identifier )
3499 any-word ( identifier , nonempty-expr-list )
3500 any-word ( expr-list )
3501
3502 where the "identifier" must not be declared as a type, and
3503 "any-word" may be any identifier (including one declared as a
3504 type), a reserved word storage class specifier, type specifier or
3505 type qualifier. ??? This still leaves out most reserved keywords
3506 (following the old parser), shouldn't we include them, and why not
3507 allow identifiers declared as types to start the arguments? */
3508
3509 static tree
3510 c_parser_attributes (c_parser *parser)
3511 {
3512 tree attrs = NULL_TREE;
3513 while (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
3514 {
3515 /* ??? Follow the C++ parser rather than using the
3516 lex_untranslated_string kludge. */
3517 parser->lex_untranslated_string = true;
3518 c_parser_consume_token (parser);
3519 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3520 {
3521 parser->lex_untranslated_string = false;
3522 return attrs;
3523 }
3524 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3525 {
3526 parser->lex_untranslated_string = false;
3527 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
3528 return attrs;
3529 }
3530 /* Parse the attribute list. */
3531 while (c_parser_next_token_is (parser, CPP_COMMA)
3532 || c_parser_next_token_is (parser, CPP_NAME)
3533 || c_parser_next_token_is (parser, CPP_KEYWORD))
3534 {
3535 tree attr, attr_name, attr_args;
3536 VEC(tree,gc) *expr_list;
3537 if (c_parser_next_token_is (parser, CPP_COMMA))
3538 {
3539 c_parser_consume_token (parser);
3540 continue;
3541 }
3542
3543 attr_name = c_parser_attribute_any_word (parser);
3544 if (attr_name == NULL)
3545 break;
3546 c_parser_consume_token (parser);
3547 if (c_parser_next_token_is_not (parser, CPP_OPEN_PAREN))
3548 {
3549 attr = build_tree_list (attr_name, NULL_TREE);
3550 attrs = chainon (attrs, attr);
3551 continue;
3552 }
3553 c_parser_consume_token (parser);
3554 /* Parse the attribute contents. If they start with an
3555 identifier which is followed by a comma or close
3556 parenthesis, then the arguments start with that
3557 identifier; otherwise they are an expression list.
3558 In objective-c the identifier may be a classname. */
3559 if (c_parser_next_token_is (parser, CPP_NAME)
3560 && (c_parser_peek_token (parser)->id_kind == C_ID_ID
3561 || (c_dialect_objc ()
3562 && c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME))
3563 && ((c_parser_peek_2nd_token (parser)->type == CPP_COMMA)
3564 || (c_parser_peek_2nd_token (parser)->type
3565 == CPP_CLOSE_PAREN)))
3566 {
3567 tree arg1 = c_parser_peek_token (parser)->value;
3568 c_parser_consume_token (parser);
3569 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3570 attr_args = build_tree_list (NULL_TREE, arg1);
3571 else
3572 {
3573 tree tree_list;
3574 c_parser_consume_token (parser);
3575 expr_list = c_parser_expr_list (parser, false, true, NULL);
3576 tree_list = build_tree_list_vec (expr_list);
3577 attr_args = tree_cons (NULL_TREE, arg1, tree_list);
3578 release_tree_vector (expr_list);
3579 }
3580 }
3581 else
3582 {
3583 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3584 attr_args = NULL_TREE;
3585 else
3586 {
3587 expr_list = c_parser_expr_list (parser, false, true, NULL);
3588 attr_args = build_tree_list_vec (expr_list);
3589 release_tree_vector (expr_list);
3590 }
3591 }
3592 attr = build_tree_list (attr_name, attr_args);
3593 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3594 c_parser_consume_token (parser);
3595 else
3596 {
3597 parser->lex_untranslated_string = false;
3598 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3599 "expected %<)%>");
3600 return attrs;
3601 }
3602 attrs = chainon (attrs, attr);
3603 }
3604 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3605 c_parser_consume_token (parser);
3606 else
3607 {
3608 parser->lex_untranslated_string = false;
3609 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3610 "expected %<)%>");
3611 return attrs;
3612 }
3613 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3614 c_parser_consume_token (parser);
3615 else
3616 {
3617 parser->lex_untranslated_string = false;
3618 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3619 "expected %<)%>");
3620 return attrs;
3621 }
3622 parser->lex_untranslated_string = false;
3623 }
3624 return attrs;
3625 }
3626
3627 /* Parse a type name (C90 6.5.5, C99 6.7.6).
3628
3629 type-name:
3630 specifier-qualifier-list abstract-declarator[opt]
3631 */
3632
3633 static struct c_type_name *
3634 c_parser_type_name (c_parser *parser)
3635 {
3636 struct c_declspecs *specs = build_null_declspecs ();
3637 struct c_declarator *declarator;
3638 struct c_type_name *ret;
3639 bool dummy = false;
3640 c_parser_declspecs (parser, specs, false, true, true, cla_prefer_type);
3641 if (!specs->declspecs_seen_p)
3642 {
3643 c_parser_error (parser, "expected specifier-qualifier-list");
3644 return NULL;
3645 }
3646 if (specs->type != error_mark_node)
3647 {
3648 pending_xref_error ();
3649 finish_declspecs (specs);
3650 }
3651 declarator = c_parser_declarator (parser,
3652 specs->typespec_kind != ctsk_none,
3653 C_DTR_ABSTRACT, &dummy);
3654 if (declarator == NULL)
3655 return NULL;
3656 ret = XOBNEW (&parser_obstack, struct c_type_name);
3657 ret->specs = specs;
3658 ret->declarator = declarator;
3659 return ret;
3660 }
3661
3662 /* Parse an initializer (C90 6.5.7, C99 6.7.8).
3663
3664 initializer:
3665 assignment-expression
3666 { initializer-list }
3667 { initializer-list , }
3668
3669 initializer-list:
3670 designation[opt] initializer
3671 initializer-list , designation[opt] initializer
3672
3673 designation:
3674 designator-list =
3675
3676 designator-list:
3677 designator
3678 designator-list designator
3679
3680 designator:
3681 array-designator
3682 . identifier
3683
3684 array-designator:
3685 [ constant-expression ]
3686
3687 GNU extensions:
3688
3689 initializer:
3690 { }
3691
3692 designation:
3693 array-designator
3694 identifier :
3695
3696 array-designator:
3697 [ constant-expression ... constant-expression ]
3698
3699 Any expression without commas is accepted in the syntax for the
3700 constant-expressions, with non-constant expressions rejected later.
3701
3702 This function is only used for top-level initializers; for nested
3703 ones, see c_parser_initval. */
3704
3705 static struct c_expr
3706 c_parser_initializer (c_parser *parser)
3707 {
3708 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
3709 return c_parser_braced_init (parser, NULL_TREE, false);
3710 else
3711 {
3712 struct c_expr ret;
3713 location_t loc = c_parser_peek_token (parser)->location;
3714 ret = c_parser_expr_no_commas (parser, NULL);
3715 if (TREE_CODE (ret.value) != STRING_CST
3716 && TREE_CODE (ret.value) != COMPOUND_LITERAL_EXPR)
3717 ret = default_function_array_read_conversion (loc, ret);
3718 return ret;
3719 }
3720 }
3721
3722 /* Parse a braced initializer list. TYPE is the type specified for a
3723 compound literal, and NULL_TREE for other initializers and for
3724 nested braced lists. NESTED_P is true for nested braced lists,
3725 false for the list of a compound literal or the list that is the
3726 top-level initializer in a declaration. */
3727
3728 static struct c_expr
3729 c_parser_braced_init (c_parser *parser, tree type, bool nested_p)
3730 {
3731 struct c_expr ret;
3732 struct obstack braced_init_obstack;
3733 location_t brace_loc = c_parser_peek_token (parser)->location;
3734 gcc_obstack_init (&braced_init_obstack);
3735 gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
3736 c_parser_consume_token (parser);
3737 if (nested_p)
3738 push_init_level (0, &braced_init_obstack);
3739 else
3740 really_start_incremental_init (type);
3741 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3742 {
3743 pedwarn (brace_loc, OPT_pedantic, "ISO C forbids empty initializer braces");
3744 }
3745 else
3746 {
3747 /* Parse a non-empty initializer list, possibly with a trailing
3748 comma. */
3749 while (true)
3750 {
3751 c_parser_initelt (parser, &braced_init_obstack);
3752 if (parser->error)
3753 break;
3754 if (c_parser_next_token_is (parser, CPP_COMMA))
3755 c_parser_consume_token (parser);
3756 else
3757 break;
3758 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3759 break;
3760 }
3761 }
3762 if (c_parser_next_token_is_not (parser, CPP_CLOSE_BRACE))
3763 {
3764 ret.value = error_mark_node;
3765 ret.original_code = ERROR_MARK;
3766 ret.original_type = NULL;
3767 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, "expected %<}%>");
3768 pop_init_level (0, &braced_init_obstack);
3769 obstack_free (&braced_init_obstack, NULL);
3770 return ret;
3771 }
3772 c_parser_consume_token (parser);
3773 ret = pop_init_level (0, &braced_init_obstack);
3774 obstack_free (&braced_init_obstack, NULL);
3775 return ret;
3776 }
3777
3778 /* Parse a nested initializer, including designators. */
3779
3780 static void
3781 c_parser_initelt (c_parser *parser, struct obstack * braced_init_obstack)
3782 {
3783 /* Parse any designator or designator list. A single array
3784 designator may have the subsequent "=" omitted in GNU C, but a
3785 longer list or a structure member designator may not. */
3786 if (c_parser_next_token_is (parser, CPP_NAME)
3787 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
3788 {
3789 /* Old-style structure member designator. */
3790 set_init_label (c_parser_peek_token (parser)->value,
3791 braced_init_obstack);
3792 /* Use the colon as the error location. */
3793 pedwarn (c_parser_peek_2nd_token (parser)->location, OPT_pedantic,
3794 "obsolete use of designated initializer with %<:%>");
3795 c_parser_consume_token (parser);
3796 c_parser_consume_token (parser);
3797 }
3798 else
3799 {
3800 /* des_seen is 0 if there have been no designators, 1 if there
3801 has been a single array designator and 2 otherwise. */
3802 int des_seen = 0;
3803 /* Location of a designator. */
3804 location_t des_loc = UNKNOWN_LOCATION; /* Quiet warning. */
3805 while (c_parser_next_token_is (parser, CPP_OPEN_SQUARE)
3806 || c_parser_next_token_is (parser, CPP_DOT))
3807 {
3808 int des_prev = des_seen;
3809 if (!des_seen)
3810 des_loc = c_parser_peek_token (parser)->location;
3811 if (des_seen < 2)
3812 des_seen++;
3813 if (c_parser_next_token_is (parser, CPP_DOT))
3814 {
3815 des_seen = 2;
3816 c_parser_consume_token (parser);
3817 if (c_parser_next_token_is (parser, CPP_NAME))
3818 {
3819 set_init_label (c_parser_peek_token (parser)->value,
3820 braced_init_obstack);
3821 c_parser_consume_token (parser);
3822 }
3823 else
3824 {
3825 struct c_expr init;
3826 init.value = error_mark_node;
3827 init.original_code = ERROR_MARK;
3828 init.original_type = NULL;
3829 c_parser_error (parser, "expected identifier");
3830 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
3831 process_init_element (init, false, braced_init_obstack);
3832 return;
3833 }
3834 }
3835 else
3836 {
3837 tree first, second;
3838 location_t ellipsis_loc = UNKNOWN_LOCATION; /* Quiet warning. */
3839 /* ??? Following the old parser, [ objc-receiver
3840 objc-message-args ] is accepted as an initializer,
3841 being distinguished from a designator by what follows
3842 the first assignment expression inside the square
3843 brackets, but after a first array designator a
3844 subsequent square bracket is for Objective-C taken to
3845 start an expression, using the obsolete form of
3846 designated initializer without '=', rather than
3847 possibly being a second level of designation: in LALR
3848 terms, the '[' is shifted rather than reducing
3849 designator to designator-list. */
3850 if (des_prev == 1 && c_dialect_objc ())
3851 {
3852 des_seen = des_prev;
3853 break;
3854 }
3855 if (des_prev == 0 && c_dialect_objc ())
3856 {
3857 /* This might be an array designator or an
3858 Objective-C message expression. If the former,
3859 continue parsing here; if the latter, parse the
3860 remainder of the initializer given the starting
3861 primary-expression. ??? It might make sense to
3862 distinguish when des_prev == 1 as well; see
3863 previous comment. */
3864 tree rec, args;
3865 struct c_expr mexpr;
3866 c_parser_consume_token (parser);
3867 if (c_parser_peek_token (parser)->type == CPP_NAME
3868 && ((c_parser_peek_token (parser)->id_kind
3869 == C_ID_TYPENAME)
3870 || (c_parser_peek_token (parser)->id_kind
3871 == C_ID_CLASSNAME)))
3872 {
3873 /* Type name receiver. */
3874 tree id = c_parser_peek_token (parser)->value;
3875 c_parser_consume_token (parser);
3876 rec = objc_get_class_reference (id);
3877 goto parse_message_args;
3878 }
3879 first = c_parser_expr_no_commas (parser, NULL).value;
3880 mark_exp_read (first);
3881 if (c_parser_next_token_is (parser, CPP_ELLIPSIS)
3882 || c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3883 goto array_desig_after_first;
3884 /* Expression receiver. So far only one part
3885 without commas has been parsed; there might be
3886 more of the expression. */
3887 rec = first;
3888 while (c_parser_next_token_is (parser, CPP_COMMA))
3889 {
3890 struct c_expr next;
3891 location_t comma_loc, exp_loc;
3892 comma_loc = c_parser_peek_token (parser)->location;
3893 c_parser_consume_token (parser);
3894 exp_loc = c_parser_peek_token (parser)->location;
3895 next = c_parser_expr_no_commas (parser, NULL);
3896 next = default_function_array_read_conversion (exp_loc,
3897 next);
3898 rec = build_compound_expr (comma_loc, rec, next.value);
3899 }
3900 parse_message_args:
3901 /* Now parse the objc-message-args. */
3902 args = c_parser_objc_message_args (parser);
3903 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
3904 "expected %<]%>");
3905 mexpr.value
3906 = objc_build_message_expr (rec, args);
3907 mexpr.original_code = ERROR_MARK;
3908 mexpr.original_type = NULL;
3909 /* Now parse and process the remainder of the
3910 initializer, starting with this message
3911 expression as a primary-expression. */
3912 c_parser_initval (parser, &mexpr, braced_init_obstack);
3913 return;
3914 }
3915 c_parser_consume_token (parser);
3916 first = c_parser_expr_no_commas (parser, NULL).value;
3917 mark_exp_read (first);
3918 array_desig_after_first:
3919 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3920 {
3921 ellipsis_loc = c_parser_peek_token (parser)->location;
3922 c_parser_consume_token (parser);
3923 second = c_parser_expr_no_commas (parser, NULL).value;
3924 mark_exp_read (second);
3925 }
3926 else
3927 second = NULL_TREE;
3928 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3929 {
3930 c_parser_consume_token (parser);
3931 set_init_index (first, second, braced_init_obstack);
3932 if (second)
3933 pedwarn (ellipsis_loc, OPT_pedantic,
3934 "ISO C forbids specifying range of elements to initialize");
3935 }
3936 else
3937 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
3938 "expected %<]%>");
3939 }
3940 }
3941 if (des_seen >= 1)
3942 {
3943 if (c_parser_next_token_is (parser, CPP_EQ))
3944 {
3945 if (!flag_isoc99)
3946 pedwarn (des_loc, OPT_pedantic,
3947 "ISO C90 forbids specifying subobject to initialize");
3948 c_parser_consume_token (parser);
3949 }
3950 else
3951 {
3952 if (des_seen == 1)
3953 pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic,
3954 "obsolete use of designated initializer without %<=%>");
3955 else
3956 {
3957 struct c_expr init;
3958 init.value = error_mark_node;
3959 init.original_code = ERROR_MARK;
3960 init.original_type = NULL;
3961 c_parser_error (parser, "expected %<=%>");
3962 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
3963 process_init_element (init, false, braced_init_obstack);
3964 return;
3965 }
3966 }
3967 }
3968 }
3969 c_parser_initval (parser, NULL, braced_init_obstack);
3970 }
3971
3972 /* Parse a nested initializer; as c_parser_initializer but parses
3973 initializers within braced lists, after any designators have been
3974 applied. If AFTER is not NULL then it is an Objective-C message
3975 expression which is the primary-expression starting the
3976 initializer. */
3977
3978 static void
3979 c_parser_initval (c_parser *parser, struct c_expr *after,
3980 struct obstack * braced_init_obstack)
3981 {
3982 struct c_expr init;
3983 gcc_assert (!after || c_dialect_objc ());
3984 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE) && !after)
3985 init = c_parser_braced_init (parser, NULL_TREE, true);
3986 else
3987 {
3988 location_t loc = c_parser_peek_token (parser)->location;
3989 init = c_parser_expr_no_commas (parser, after);
3990 if (init.value != NULL_TREE
3991 && TREE_CODE (init.value) != STRING_CST
3992 && TREE_CODE (init.value) != COMPOUND_LITERAL_EXPR)
3993 init = default_function_array_read_conversion (loc, init);
3994 }
3995 process_init_element (init, false, braced_init_obstack);
3996 }
3997
3998 /* Parse a compound statement (possibly a function body) (C90 6.6.2,
3999 C99 6.8.2).
4000
4001 compound-statement:
4002 { block-item-list[opt] }
4003 { label-declarations block-item-list }
4004
4005 block-item-list:
4006 block-item
4007 block-item-list block-item
4008
4009 block-item:
4010 nested-declaration
4011 statement
4012
4013 nested-declaration:
4014 declaration
4015
4016 GNU extensions:
4017
4018 compound-statement:
4019 { label-declarations block-item-list }
4020
4021 nested-declaration:
4022 __extension__ nested-declaration
4023 nested-function-definition
4024
4025 label-declarations:
4026 label-declaration
4027 label-declarations label-declaration
4028
4029 label-declaration:
4030 __label__ identifier-list ;
4031
4032 Allowing the mixing of declarations and code is new in C99. The
4033 GNU syntax also permits (not shown above) labels at the end of
4034 compound statements, which yield an error. We don't allow labels
4035 on declarations; this might seem like a natural extension, but
4036 there would be a conflict between attributes on the label and
4037 prefix attributes on the declaration. ??? The syntax follows the
4038 old parser in requiring something after label declarations.
4039 Although they are erroneous if the labels declared aren't defined,
4040 is it useful for the syntax to be this way?
4041
4042 OpenMP:
4043
4044 block-item:
4045 openmp-directive
4046
4047 openmp-directive:
4048 barrier-directive
4049 flush-directive */
4050
4051 static tree
4052 c_parser_compound_statement (c_parser *parser)
4053 {
4054 tree stmt;
4055 location_t brace_loc;
4056 brace_loc = c_parser_peek_token (parser)->location;
4057 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
4058 {
4059 /* Ensure a scope is entered and left anyway to avoid confusion
4060 if we have just prepared to enter a function body. */
4061 stmt = c_begin_compound_stmt (true);
4062 c_end_compound_stmt (brace_loc, stmt, true);
4063 return error_mark_node;
4064 }
4065 stmt = c_begin_compound_stmt (true);
4066 c_parser_compound_statement_nostart (parser);
4067 return c_end_compound_stmt (brace_loc, stmt, true);
4068 }
4069
4070 /* Parse a compound statement except for the opening brace. This is
4071 used for parsing both compound statements and statement expressions
4072 (which follow different paths to handling the opening). */
4073
4074 static void
4075 c_parser_compound_statement_nostart (c_parser *parser)
4076 {
4077 bool last_stmt = false;
4078 bool last_label = false;
4079 bool save_valid_for_pragma = valid_location_for_stdc_pragma_p ();
4080 location_t label_loc = UNKNOWN_LOCATION; /* Quiet warning. */
4081 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
4082 {
4083 c_parser_consume_token (parser);
4084 return;
4085 }
4086 mark_valid_location_for_stdc_pragma (true);
4087 if (c_parser_next_token_is_keyword (parser, RID_LABEL))
4088 {
4089 /* Read zero or more forward-declarations for labels that nested
4090 functions can jump to. */
4091 mark_valid_location_for_stdc_pragma (false);
4092 while (c_parser_next_token_is_keyword (parser, RID_LABEL))
4093 {
4094 label_loc = c_parser_peek_token (parser)->location;
4095 c_parser_consume_token (parser);
4096 /* Any identifiers, including those declared as type names,
4097 are OK here. */
4098 while (true)
4099 {
4100 tree label;
4101 if (c_parser_next_token_is_not (parser, CPP_NAME))
4102 {
4103 c_parser_error (parser, "expected identifier");
4104 break;
4105 }
4106 label
4107 = declare_label (c_parser_peek_token (parser)->value);
4108 C_DECLARED_LABEL_FLAG (label) = 1;
4109 add_stmt (build_stmt (label_loc, DECL_EXPR, label));
4110 c_parser_consume_token (parser);
4111 if (c_parser_next_token_is (parser, CPP_COMMA))
4112 c_parser_consume_token (parser);
4113 else
4114 break;
4115 }
4116 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
4117 }
4118 pedwarn (label_loc, OPT_pedantic, "ISO C forbids label declarations");
4119 }
4120 /* We must now have at least one statement, label or declaration. */
4121 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
4122 {
4123 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
4124 c_parser_error (parser, "expected declaration or statement");
4125 c_parser_consume_token (parser);
4126 return;
4127 }
4128 while (c_parser_next_token_is_not (parser, CPP_CLOSE_BRACE))
4129 {
4130 location_t loc = c_parser_peek_token (parser)->location;
4131 if (c_parser_next_token_is_keyword (parser, RID_CASE)
4132 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
4133 || (c_parser_next_token_is (parser, CPP_NAME)
4134 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
4135 {
4136 if (c_parser_next_token_is_keyword (parser, RID_CASE))
4137 label_loc = c_parser_peek_2nd_token (parser)->location;
4138 else
4139 label_loc = c_parser_peek_token (parser)->location;
4140 last_label = true;
4141 last_stmt = false;
4142 mark_valid_location_for_stdc_pragma (false);
4143 c_parser_label (parser);
4144 }
4145 else if (!last_label
4146 && c_parser_next_tokens_start_declaration (parser))
4147 {
4148 last_label = false;
4149 mark_valid_location_for_stdc_pragma (false);
4150 c_parser_declaration_or_fndef (parser, true, true, true, true, true, NULL);
4151 if (last_stmt)
4152 pedwarn_c90 (loc,
4153 (pedantic && !flag_isoc99)
4154 ? OPT_pedantic
4155 : OPT_Wdeclaration_after_statement,
4156 "ISO C90 forbids mixed declarations and code");
4157 last_stmt = false;
4158 }
4159 else if (!last_label
4160 && c_parser_next_token_is_keyword (parser, RID_EXTENSION))
4161 {
4162 /* __extension__ can start a declaration, but is also an
4163 unary operator that can start an expression. Consume all
4164 but the last of a possible series of __extension__ to
4165 determine which. */
4166 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
4167 && (c_parser_peek_2nd_token (parser)->keyword
4168 == RID_EXTENSION))
4169 c_parser_consume_token (parser);
4170 if (c_token_starts_declaration (c_parser_peek_2nd_token (parser)))
4171 {
4172 int ext;
4173 ext = disable_extension_diagnostics ();
4174 c_parser_consume_token (parser);
4175 last_label = false;
4176 mark_valid_location_for_stdc_pragma (false);
4177 c_parser_declaration_or_fndef (parser, true, true, true, true,
4178 true, NULL);
4179 /* Following the old parser, __extension__ does not
4180 disable this diagnostic. */
4181 restore_extension_diagnostics (ext);
4182 if (last_stmt)
4183 pedwarn_c90 (loc, (pedantic && !flag_isoc99)
4184 ? OPT_pedantic
4185 : OPT_Wdeclaration_after_statement,
4186 "ISO C90 forbids mixed declarations and code");
4187 last_stmt = false;
4188 }
4189 else
4190 goto statement;
4191 }
4192 else if (c_parser_next_token_is (parser, CPP_PRAGMA))
4193 {
4194 /* External pragmas, and some omp pragmas, are not associated
4195 with regular c code, and so are not to be considered statements
4196 syntactically. This ensures that the user doesn't put them
4197 places that would turn into syntax errors if the directive
4198 were ignored. */
4199 if (c_parser_pragma (parser, pragma_compound))
4200 last_label = false, last_stmt = true;
4201 }
4202 else if (c_parser_next_token_is (parser, CPP_EOF))
4203 {
4204 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
4205 c_parser_error (parser, "expected declaration or statement");
4206 return;
4207 }
4208 else if (c_parser_next_token_is_keyword (parser, RID_ELSE))
4209 {
4210 if (parser->in_if_block)
4211 {
4212 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
4213 error_at (loc, """expected %<}%> before %<else%>");
4214 return;
4215 }
4216 else
4217 {
4218 error_at (loc, "%<else%> without a previous %<if%>");
4219 c_parser_consume_token (parser);
4220 continue;
4221 }
4222 }
4223 else
4224 {
4225 statement:
4226 last_label = false;
4227 last_stmt = true;
4228 mark_valid_location_for_stdc_pragma (false);
4229 c_parser_statement_after_labels (parser);
4230 }
4231
4232 parser->error = false;
4233 }
4234 if (last_label)
4235 error_at (label_loc, "label at end of compound statement");
4236 c_parser_consume_token (parser);
4237 /* Restore the value we started with. */
4238 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
4239 }
4240
4241 /* Parse a label (C90 6.6.1, C99 6.8.1).
4242
4243 label:
4244 identifier : attributes[opt]
4245 case constant-expression :
4246 default :
4247
4248 GNU extensions:
4249
4250 label:
4251 case constant-expression ... constant-expression :
4252
4253 The use of attributes on labels is a GNU extension. The syntax in
4254 GNU C accepts any expressions without commas, non-constant
4255 expressions being rejected later. */
4256
4257 static void
4258 c_parser_label (c_parser *parser)
4259 {
4260 location_t loc1 = c_parser_peek_token (parser)->location;
4261 tree label = NULL_TREE;
4262 if (c_parser_next_token_is_keyword (parser, RID_CASE))
4263 {
4264 tree exp1, exp2;
4265 c_parser_consume_token (parser);
4266 exp1 = c_parser_expr_no_commas (parser, NULL).value;
4267 if (c_parser_next_token_is (parser, CPP_COLON))
4268 {
4269 c_parser_consume_token (parser);
4270 label = do_case (loc1, exp1, NULL_TREE);
4271 }
4272 else if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
4273 {
4274 c_parser_consume_token (parser);
4275 exp2 = c_parser_expr_no_commas (parser, NULL).value;
4276 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
4277 label = do_case (loc1, exp1, exp2);
4278 }
4279 else
4280 c_parser_error (parser, "expected %<:%> or %<...%>");
4281 }
4282 else if (c_parser_next_token_is_keyword (parser, RID_DEFAULT))
4283 {
4284 c_parser_consume_token (parser);
4285 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
4286 label = do_case (loc1, NULL_TREE, NULL_TREE);
4287 }
4288 else
4289 {
4290 tree name = c_parser_peek_token (parser)->value;
4291 tree tlab;
4292 tree attrs;
4293 location_t loc2 = c_parser_peek_token (parser)->location;
4294 gcc_assert (c_parser_next_token_is (parser, CPP_NAME));
4295 c_parser_consume_token (parser);
4296 gcc_assert (c_parser_next_token_is (parser, CPP_COLON));
4297 c_parser_consume_token (parser);
4298 attrs = c_parser_attributes (parser);
4299 tlab = define_label (loc2, name);
4300 if (tlab)
4301 {
4302 decl_attributes (&tlab, attrs, 0);
4303 label = add_stmt (build_stmt (loc1, LABEL_EXPR, tlab));
4304 }
4305 }
4306 if (label)
4307 {
4308 if (c_parser_next_tokens_start_declaration (parser))
4309 {
4310 error_at (c_parser_peek_token (parser)->location,
4311 "a label can only be part of a statement and "
4312 "a declaration is not a statement");
4313 c_parser_declaration_or_fndef (parser, /*fndef_ok*/ false,
4314 /*static_assert_ok*/ true,
4315 /*nested*/ true, /*empty_ok*/ false,
4316 /*start_attr_ok*/ true, NULL);
4317 }
4318 }
4319 }
4320
4321 /* Parse a statement (C90 6.6, C99 6.8).
4322
4323 statement:
4324 labeled-statement
4325 compound-statement
4326 expression-statement
4327 selection-statement
4328 iteration-statement
4329 jump-statement
4330
4331 labeled-statement:
4332 label statement
4333
4334 expression-statement:
4335 expression[opt] ;
4336
4337 selection-statement:
4338 if-statement
4339 switch-statement
4340
4341 iteration-statement:
4342 while-statement
4343 do-statement
4344 for-statement
4345
4346 jump-statement:
4347 goto identifier ;
4348 continue ;
4349 break ;
4350 return expression[opt] ;
4351
4352 GNU extensions:
4353
4354 statement:
4355 asm-statement
4356
4357 jump-statement:
4358 goto * expression ;
4359
4360 Objective-C:
4361
4362 statement:
4363 objc-throw-statement
4364 objc-try-catch-statement
4365 objc-synchronized-statement
4366
4367 objc-throw-statement:
4368 @throw expression ;
4369 @throw ;
4370
4371 OpenMP:
4372
4373 statement:
4374 openmp-construct
4375
4376 openmp-construct:
4377 parallel-construct
4378 for-construct
4379 sections-construct
4380 single-construct
4381 parallel-for-construct
4382 parallel-sections-construct
4383 master-construct
4384 critical-construct
4385 atomic-construct
4386 ordered-construct
4387
4388 parallel-construct:
4389 parallel-directive structured-block
4390
4391 for-construct:
4392 for-directive iteration-statement
4393
4394 sections-construct:
4395 sections-directive section-scope
4396
4397 single-construct:
4398 single-directive structured-block
4399
4400 parallel-for-construct:
4401 parallel-for-directive iteration-statement
4402
4403 parallel-sections-construct:
4404 parallel-sections-directive section-scope
4405
4406 master-construct:
4407 master-directive structured-block
4408
4409 critical-construct:
4410 critical-directive structured-block
4411
4412 atomic-construct:
4413 atomic-directive expression-statement
4414
4415 ordered-construct:
4416 ordered-directive structured-block
4417
4418 Transactional Memory:
4419
4420 statement:
4421 transaction-statement
4422 transaction-cancel-statement
4423 */
4424
4425 static void
4426 c_parser_statement (c_parser *parser)
4427 {
4428 while (c_parser_next_token_is_keyword (parser, RID_CASE)
4429 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
4430 || (c_parser_next_token_is (parser, CPP_NAME)
4431 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
4432 c_parser_label (parser);
4433 c_parser_statement_after_labels (parser);
4434 }
4435
4436 /* Parse a statement, other than a labeled statement. */
4437
4438 static void
4439 c_parser_statement_after_labels (c_parser *parser)
4440 {
4441 location_t loc = c_parser_peek_token (parser)->location;
4442 tree stmt = NULL_TREE;
4443 bool in_if_block = parser->in_if_block;
4444 parser->in_if_block = false;
4445 switch (c_parser_peek_token (parser)->type)
4446 {
4447 case CPP_OPEN_BRACE:
4448 add_stmt (c_parser_compound_statement (parser));
4449 break;
4450 case CPP_KEYWORD:
4451 switch (c_parser_peek_token (parser)->keyword)
4452 {
4453 case RID_IF:
4454 c_parser_if_statement (parser);
4455 break;
4456 case RID_SWITCH:
4457 c_parser_switch_statement (parser);
4458 break;
4459 case RID_WHILE:
4460 c_parser_while_statement (parser);
4461 break;
4462 case RID_DO:
4463 c_parser_do_statement (parser);
4464 break;
4465 case RID_FOR:
4466 c_parser_for_statement (parser);
4467 break;
4468 case RID_GOTO:
4469 c_parser_consume_token (parser);
4470 if (c_parser_next_token_is (parser, CPP_NAME))
4471 {
4472 stmt = c_finish_goto_label (loc,
4473 c_parser_peek_token (parser)->value);
4474 c_parser_consume_token (parser);
4475 }
4476 else if (c_parser_next_token_is (parser, CPP_MULT))
4477 {
4478 tree val;
4479
4480 c_parser_consume_token (parser);
4481 val = c_parser_expression (parser).value;
4482 mark_exp_read (val);
4483 stmt = c_finish_goto_ptr (loc, val);
4484 }
4485 else
4486 c_parser_error (parser, "expected identifier or %<*%>");
4487 goto expect_semicolon;
4488 case RID_CONTINUE:
4489 c_parser_consume_token (parser);
4490 stmt = c_finish_bc_stmt (loc, &c_cont_label, false);
4491 goto expect_semicolon;
4492 case RID_BREAK:
4493 c_parser_consume_token (parser);
4494 stmt = c_finish_bc_stmt (loc, &c_break_label, true);
4495 goto expect_semicolon;
4496 case RID_RETURN:
4497 c_parser_consume_token (parser);
4498 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4499 {
4500 stmt = c_finish_return (loc, NULL_TREE, NULL_TREE);
4501 c_parser_consume_token (parser);
4502 }
4503 else
4504 {
4505 struct c_expr expr = c_parser_expression_conv (parser);
4506 mark_exp_read (expr.value);
4507 stmt = c_finish_return (loc, expr.value, expr.original_type);
4508 goto expect_semicolon;
4509 }
4510 break;
4511 case RID_ASM:
4512 stmt = c_parser_asm_statement (parser);
4513 break;
4514 case RID_TRANSACTION_ATOMIC:
4515 case RID_TRANSACTION_RELAXED:
4516 stmt = c_parser_transaction (parser,
4517 c_parser_peek_token (parser)->keyword);
4518 break;
4519 case RID_TRANSACTION_CANCEL:
4520 stmt = c_parser_transaction_cancel (parser);
4521 goto expect_semicolon;
4522 case RID_AT_THROW:
4523 gcc_assert (c_dialect_objc ());
4524 c_parser_consume_token (parser);
4525 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4526 {
4527 stmt = objc_build_throw_stmt (loc, NULL_TREE);
4528 c_parser_consume_token (parser);
4529 }
4530 else
4531 {
4532 tree expr = c_parser_expression (parser).value;
4533 expr = c_fully_fold (expr, false, NULL);
4534 stmt = objc_build_throw_stmt (loc, expr);
4535 goto expect_semicolon;
4536 }
4537 break;
4538 case RID_AT_TRY:
4539 gcc_assert (c_dialect_objc ());
4540 c_parser_objc_try_catch_finally_statement (parser);
4541 break;
4542 case RID_AT_SYNCHRONIZED:
4543 gcc_assert (c_dialect_objc ());
4544 c_parser_objc_synchronized_statement (parser);
4545 break;
4546 default:
4547 goto expr_stmt;
4548 }
4549 break;
4550 case CPP_SEMICOLON:
4551 c_parser_consume_token (parser);
4552 break;
4553 case CPP_CLOSE_PAREN:
4554 case CPP_CLOSE_SQUARE:
4555 /* Avoid infinite loop in error recovery:
4556 c_parser_skip_until_found stops at a closing nesting
4557 delimiter without consuming it, but here we need to consume
4558 it to proceed further. */
4559 c_parser_error (parser, "expected statement");
4560 c_parser_consume_token (parser);
4561 break;
4562 case CPP_PRAGMA:
4563 c_parser_pragma (parser, pragma_stmt);
4564 break;
4565 default:
4566 expr_stmt:
4567 stmt = c_finish_expr_stmt (loc, c_parser_expression_conv (parser).value);
4568 expect_semicolon:
4569 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
4570 break;
4571 }
4572 /* Two cases cannot and do not have line numbers associated: If stmt
4573 is degenerate, such as "2;", then stmt is an INTEGER_CST, which
4574 cannot hold line numbers. But that's OK because the statement
4575 will either be changed to a MODIFY_EXPR during gimplification of
4576 the statement expr, or discarded. If stmt was compound, but
4577 without new variables, we will have skipped the creation of a
4578 BIND and will have a bare STATEMENT_LIST. But that's OK because
4579 (recursively) all of the component statements should already have
4580 line numbers assigned. ??? Can we discard no-op statements
4581 earlier? */
4582 if (CAN_HAVE_LOCATION_P (stmt)
4583 && EXPR_LOCATION (stmt) == UNKNOWN_LOCATION)
4584 SET_EXPR_LOCATION (stmt, loc);
4585
4586 parser->in_if_block = in_if_block;
4587 }
4588
4589 /* Parse the condition from an if, do, while or for statements. */
4590
4591 static tree
4592 c_parser_condition (c_parser *parser)
4593 {
4594 location_t loc = c_parser_peek_token (parser)->location;
4595 tree cond;
4596 cond = c_parser_expression_conv (parser).value;
4597 cond = c_objc_common_truthvalue_conversion (loc, cond);
4598 cond = c_fully_fold (cond, false, NULL);
4599 if (warn_sequence_point)
4600 verify_sequence_points (cond);
4601 return cond;
4602 }
4603
4604 /* Parse a parenthesized condition from an if, do or while statement.
4605
4606 condition:
4607 ( expression )
4608 */
4609 static tree
4610 c_parser_paren_condition (c_parser *parser)
4611 {
4612 tree cond;
4613 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4614 return error_mark_node;
4615 cond = c_parser_condition (parser);
4616 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
4617 return cond;
4618 }
4619
4620 /* Parse a statement which is a block in C99. */
4621
4622 static tree
4623 c_parser_c99_block_statement (c_parser *parser)
4624 {
4625 tree block = c_begin_compound_stmt (flag_isoc99);
4626 location_t loc = c_parser_peek_token (parser)->location;
4627 c_parser_statement (parser);
4628 return c_end_compound_stmt (loc, block, flag_isoc99);
4629 }
4630
4631 /* Parse the body of an if statement. This is just parsing a
4632 statement but (a) it is a block in C99, (b) we track whether the
4633 body is an if statement for the sake of -Wparentheses warnings, (c)
4634 we handle an empty body specially for the sake of -Wempty-body
4635 warnings, and (d) we call parser_compound_statement directly
4636 because c_parser_statement_after_labels resets
4637 parser->in_if_block. */
4638
4639 static tree
4640 c_parser_if_body (c_parser *parser, bool *if_p)
4641 {
4642 tree block = c_begin_compound_stmt (flag_isoc99);
4643 location_t body_loc = c_parser_peek_token (parser)->location;
4644 while (c_parser_next_token_is_keyword (parser, RID_CASE)
4645 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
4646 || (c_parser_next_token_is (parser, CPP_NAME)
4647 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
4648 c_parser_label (parser);
4649 *if_p = c_parser_next_token_is_keyword (parser, RID_IF);
4650 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4651 {
4652 location_t loc = c_parser_peek_token (parser)->location;
4653 add_stmt (build_empty_stmt (loc));
4654 c_parser_consume_token (parser);
4655 if (!c_parser_next_token_is_keyword (parser, RID_ELSE))
4656 warning_at (loc, OPT_Wempty_body,
4657 "suggest braces around empty body in an %<if%> statement");
4658 }
4659 else if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
4660 add_stmt (c_parser_compound_statement (parser));
4661 else
4662 c_parser_statement_after_labels (parser);
4663 return c_end_compound_stmt (body_loc, block, flag_isoc99);
4664 }
4665
4666 /* Parse the else body of an if statement. This is just parsing a
4667 statement but (a) it is a block in C99, (b) we handle an empty body
4668 specially for the sake of -Wempty-body warnings. */
4669
4670 static tree
4671 c_parser_else_body (c_parser *parser)
4672 {
4673 location_t else_loc = c_parser_peek_token (parser)->location;
4674 tree block = c_begin_compound_stmt (flag_isoc99);
4675 while (c_parser_next_token_is_keyword (parser, RID_CASE)
4676 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
4677 || (c_parser_next_token_is (parser, CPP_NAME)
4678 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
4679 c_parser_label (parser);
4680 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4681 {
4682 location_t loc = c_parser_peek_token (parser)->location;
4683 warning_at (loc,
4684 OPT_Wempty_body,
4685 "suggest braces around empty body in an %<else%> statement");
4686 add_stmt (build_empty_stmt (loc));
4687 c_parser_consume_token (parser);
4688 }
4689 else
4690 c_parser_statement_after_labels (parser);
4691 return c_end_compound_stmt (else_loc, block, flag_isoc99);
4692 }
4693
4694 /* Parse an if statement (C90 6.6.4, C99 6.8.4).
4695
4696 if-statement:
4697 if ( expression ) statement
4698 if ( expression ) statement else statement
4699 */
4700
4701 static void
4702 c_parser_if_statement (c_parser *parser)
4703 {
4704 tree block;
4705 location_t loc;
4706 tree cond;
4707 bool first_if = false;
4708 tree first_body, second_body;
4709 bool in_if_block;
4710
4711 gcc_assert (c_parser_next_token_is_keyword (parser, RID_IF));
4712 c_parser_consume_token (parser);
4713 block = c_begin_compound_stmt (flag_isoc99);
4714 loc = c_parser_peek_token (parser)->location;
4715 cond = c_parser_paren_condition (parser);
4716 in_if_block = parser->in_if_block;
4717 parser->in_if_block = true;
4718 first_body = c_parser_if_body (parser, &first_if);
4719 parser->in_if_block = in_if_block;
4720 if (c_parser_next_token_is_keyword (parser, RID_ELSE))
4721 {
4722 c_parser_consume_token (parser);
4723 second_body = c_parser_else_body (parser);
4724 }
4725 else
4726 second_body = NULL_TREE;
4727 c_finish_if_stmt (loc, cond, first_body, second_body, first_if);
4728 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
4729 }
4730
4731 /* Parse a switch statement (C90 6.6.4, C99 6.8.4).
4732
4733 switch-statement:
4734 switch (expression) statement
4735 */
4736
4737 static void
4738 c_parser_switch_statement (c_parser *parser)
4739 {
4740 tree block, expr, body, save_break;
4741 location_t switch_loc = c_parser_peek_token (parser)->location;
4742 location_t switch_cond_loc;
4743 gcc_assert (c_parser_next_token_is_keyword (parser, RID_SWITCH));
4744 c_parser_consume_token (parser);
4745 block = c_begin_compound_stmt (flag_isoc99);
4746 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4747 {
4748 switch_cond_loc = c_parser_peek_token (parser)->location;
4749 expr = c_parser_expression (parser).value;
4750 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
4751 }
4752 else
4753 {
4754 switch_cond_loc = UNKNOWN_LOCATION;
4755 expr = error_mark_node;
4756 }
4757 c_start_case (switch_loc, switch_cond_loc, expr);
4758 save_break = c_break_label;
4759 c_break_label = NULL_TREE;
4760 body = c_parser_c99_block_statement (parser);
4761 c_finish_case (body);
4762 if (c_break_label)
4763 {
4764 location_t here = c_parser_peek_token (parser)->location;
4765 tree t = build1 (LABEL_EXPR, void_type_node, c_break_label);
4766 SET_EXPR_LOCATION (t, here);
4767 add_stmt (t);
4768 }
4769 c_break_label = save_break;
4770 add_stmt (c_end_compound_stmt (switch_loc, block, flag_isoc99));
4771 }
4772
4773 /* Parse a while statement (C90 6.6.5, C99 6.8.5).
4774
4775 while-statement:
4776 while (expression) statement
4777 */
4778
4779 static void
4780 c_parser_while_statement (c_parser *parser)
4781 {
4782 tree block, cond, body, save_break, save_cont;
4783 location_t loc;
4784 gcc_assert (c_parser_next_token_is_keyword (parser, RID_WHILE));
4785 c_parser_consume_token (parser);
4786 block = c_begin_compound_stmt (flag_isoc99);
4787 loc = c_parser_peek_token (parser)->location;
4788 cond = c_parser_paren_condition (parser);
4789 save_break = c_break_label;
4790 c_break_label = NULL_TREE;
4791 save_cont = c_cont_label;
4792 c_cont_label = NULL_TREE;
4793 body = c_parser_c99_block_statement (parser);
4794 c_finish_loop (loc, cond, NULL, body, c_break_label, c_cont_label, true);
4795 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
4796 c_break_label = save_break;
4797 c_cont_label = save_cont;
4798 }
4799
4800 /* Parse a do statement (C90 6.6.5, C99 6.8.5).
4801
4802 do-statement:
4803 do statement while ( expression ) ;
4804 */
4805
4806 static void
4807 c_parser_do_statement (c_parser *parser)
4808 {
4809 tree block, cond, body, save_break, save_cont, new_break, new_cont;
4810 location_t loc;
4811 gcc_assert (c_parser_next_token_is_keyword (parser, RID_DO));
4812 c_parser_consume_token (parser);
4813 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4814 warning_at (c_parser_peek_token (parser)->location,
4815 OPT_Wempty_body,
4816 "suggest braces around empty body in %<do%> statement");
4817 block = c_begin_compound_stmt (flag_isoc99);
4818 loc = c_parser_peek_token (parser)->location;
4819 save_break = c_break_label;
4820 c_break_label = NULL_TREE;
4821 save_cont = c_cont_label;
4822 c_cont_label = NULL_TREE;
4823 body = c_parser_c99_block_statement (parser);
4824 c_parser_require_keyword (parser, RID_WHILE, "expected %<while%>");
4825 new_break = c_break_label;
4826 c_break_label = save_break;
4827 new_cont = c_cont_label;
4828 c_cont_label = save_cont;
4829 cond = c_parser_paren_condition (parser);
4830 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
4831 c_parser_skip_to_end_of_block_or_statement (parser);
4832 c_finish_loop (loc, cond, NULL, body, new_break, new_cont, false);
4833 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
4834 }
4835
4836 /* Parse a for statement (C90 6.6.5, C99 6.8.5).
4837
4838 for-statement:
4839 for ( expression[opt] ; expression[opt] ; expression[opt] ) statement
4840 for ( nested-declaration expression[opt] ; expression[opt] ) statement
4841
4842 The form with a declaration is new in C99.
4843
4844 ??? In accordance with the old parser, the declaration may be a
4845 nested function, which is then rejected in check_for_loop_decls,
4846 but does it make any sense for this to be included in the grammar?
4847 Note in particular that the nested function does not include a
4848 trailing ';', whereas the "declaration" production includes one.
4849 Also, can we reject bad declarations earlier and cheaper than
4850 check_for_loop_decls?
4851
4852 In Objective-C, there are two additional variants:
4853
4854 foreach-statement:
4855 for ( expression in expresssion ) statement
4856 for ( declaration in expression ) statement
4857
4858 This is inconsistent with C, because the second variant is allowed
4859 even if c99 is not enabled.
4860
4861 The rest of the comment documents these Objective-C foreach-statement.
4862
4863 Here is the canonical example of the first variant:
4864 for (object in array) { do something with object }
4865 we call the first expression ("object") the "object_expression" and
4866 the second expression ("array") the "collection_expression".
4867 object_expression must be an lvalue of type "id" (a generic Objective-C
4868 object) because the loop works by assigning to object_expression the
4869 various objects from the collection_expression. collection_expression
4870 must evaluate to something of type "id" which responds to the method
4871 countByEnumeratingWithState:objects:count:.
4872
4873 The canonical example of the second variant is:
4874 for (id object in array) { do something with object }
4875 which is completely equivalent to
4876 {
4877 id object;
4878 for (object in array) { do something with object }
4879 }
4880 Note that initizializing 'object' in some way (eg, "for ((object =
4881 xxx) in array) { do something with object }") is possibly
4882 technically valid, but completely pointless as 'object' will be
4883 assigned to something else as soon as the loop starts. We should
4884 most likely reject it (TODO).
4885
4886 The beginning of the Objective-C foreach-statement looks exactly
4887 like the beginning of the for-statement, and we can tell it is a
4888 foreach-statement only because the initial declaration or
4889 expression is terminated by 'in' instead of ';'.
4890 */
4891
4892 static void
4893 c_parser_for_statement (c_parser *parser)
4894 {
4895 tree block, cond, incr, save_break, save_cont, body;
4896 /* The following are only used when parsing an ObjC foreach statement. */
4897 tree object_expression;
4898 /* Silence the bogus uninitialized warning. */
4899 tree collection_expression = NULL;
4900 location_t loc = c_parser_peek_token (parser)->location;
4901 location_t for_loc = c_parser_peek_token (parser)->location;
4902 bool is_foreach_statement = false;
4903 gcc_assert (c_parser_next_token_is_keyword (parser, RID_FOR));
4904 c_parser_consume_token (parser);
4905 /* Open a compound statement in Objective-C as well, just in case this is
4906 as foreach expression. */
4907 block = c_begin_compound_stmt (flag_isoc99 || c_dialect_objc ());
4908 cond = error_mark_node;
4909 incr = error_mark_node;
4910 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4911 {
4912 /* Parse the initialization declaration or expression. */
4913 object_expression = error_mark_node;
4914 parser->objc_could_be_foreach_context = c_dialect_objc ();
4915 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4916 {
4917 parser->objc_could_be_foreach_context = false;
4918 c_parser_consume_token (parser);
4919 c_finish_expr_stmt (loc, NULL_TREE);
4920 }
4921 else if (c_parser_next_tokens_start_declaration (parser))
4922 {
4923 c_parser_declaration_or_fndef (parser, true, true, true, true, true,
4924 &object_expression);
4925 parser->objc_could_be_foreach_context = false;
4926
4927 if (c_parser_next_token_is_keyword (parser, RID_IN))
4928 {
4929 c_parser_consume_token (parser);
4930 is_foreach_statement = true;
4931 if (check_for_loop_decls (for_loc, true) == NULL_TREE)
4932 c_parser_error (parser, "multiple iterating variables in fast enumeration");
4933 }
4934 else
4935 check_for_loop_decls (for_loc, flag_isoc99);
4936 }
4937 else if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
4938 {
4939 /* __extension__ can start a declaration, but is also an
4940 unary operator that can start an expression. Consume all
4941 but the last of a possible series of __extension__ to
4942 determine which. */
4943 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
4944 && (c_parser_peek_2nd_token (parser)->keyword
4945 == RID_EXTENSION))
4946 c_parser_consume_token (parser);
4947 if (c_token_starts_declaration (c_parser_peek_2nd_token (parser)))
4948 {
4949 int ext;
4950 ext = disable_extension_diagnostics ();
4951 c_parser_consume_token (parser);
4952 c_parser_declaration_or_fndef (parser, true, true, true, true,
4953 true, &object_expression);
4954 parser->objc_could_be_foreach_context = false;
4955
4956 restore_extension_diagnostics (ext);
4957 if (c_parser_next_token_is_keyword (parser, RID_IN))
4958 {
4959 c_parser_consume_token (parser);
4960 is_foreach_statement = true;
4961 if (check_for_loop_decls (for_loc, true) == NULL_TREE)
4962 c_parser_error (parser, "multiple iterating variables in fast enumeration");
4963 }
4964 else
4965 check_for_loop_decls (for_loc, flag_isoc99);
4966 }
4967 else
4968 goto init_expr;
4969 }
4970 else
4971 {
4972 init_expr:
4973 {
4974 tree init_expression;
4975 init_expression = c_parser_expression (parser).value;
4976 parser->objc_could_be_foreach_context = false;
4977 if (c_parser_next_token_is_keyword (parser, RID_IN))
4978 {
4979 c_parser_consume_token (parser);
4980 is_foreach_statement = true;
4981 if (! lvalue_p (init_expression))
4982 c_parser_error (parser, "invalid iterating variable in fast enumeration");
4983 object_expression = c_fully_fold (init_expression, false, NULL);
4984 }
4985 else
4986 {
4987 c_finish_expr_stmt (loc, init_expression);
4988 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
4989 }
4990 }
4991 }
4992 /* Parse the loop condition. In the case of a foreach
4993 statement, there is no loop condition. */
4994 gcc_assert (!parser->objc_could_be_foreach_context);
4995 if (!is_foreach_statement)
4996 {
4997 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4998 {
4999 c_parser_consume_token (parser);
5000 cond = NULL_TREE;
5001 }
5002 else
5003 {
5004 cond = c_parser_condition (parser);
5005 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
5006 }
5007 }
5008 /* Parse the increment expression (the third expression in a
5009 for-statement). In the case of a foreach-statement, this is
5010 the expression that follows the 'in'. */
5011 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
5012 {
5013 if (is_foreach_statement)
5014 {
5015 c_parser_error (parser, "missing collection in fast enumeration");
5016 collection_expression = error_mark_node;
5017 }
5018 else
5019 incr = c_process_expr_stmt (loc, NULL_TREE);
5020 }
5021 else
5022 {
5023 if (is_foreach_statement)
5024 collection_expression = c_fully_fold (c_parser_expression (parser).value,
5025 false, NULL);
5026 else
5027 incr = c_process_expr_stmt (loc, c_parser_expression (parser).value);
5028 }
5029 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5030 }
5031 save_break = c_break_label;
5032 c_break_label = NULL_TREE;
5033 save_cont = c_cont_label;
5034 c_cont_label = NULL_TREE;
5035 body = c_parser_c99_block_statement (parser);
5036 if (is_foreach_statement)
5037 objc_finish_foreach_loop (loc, object_expression, collection_expression, body, c_break_label, c_cont_label);
5038 else
5039 c_finish_loop (loc, cond, incr, body, c_break_label, c_cont_label, true);
5040 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99 || c_dialect_objc ()));
5041 c_break_label = save_break;
5042 c_cont_label = save_cont;
5043 }
5044
5045 /* Parse an asm statement, a GNU extension. This is a full-blown asm
5046 statement with inputs, outputs, clobbers, and volatile tag
5047 allowed.
5048
5049 asm-statement:
5050 asm type-qualifier[opt] ( asm-argument ) ;
5051 asm type-qualifier[opt] goto ( asm-goto-argument ) ;
5052
5053 asm-argument:
5054 asm-string-literal
5055 asm-string-literal : asm-operands[opt]
5056 asm-string-literal : asm-operands[opt] : asm-operands[opt]
5057 asm-string-literal : asm-operands[opt] : asm-operands[opt] : asm-clobbers[opt]
5058
5059 asm-goto-argument:
5060 asm-string-literal : : asm-operands[opt] : asm-clobbers[opt] \
5061 : asm-goto-operands
5062
5063 Qualifiers other than volatile are accepted in the syntax but
5064 warned for. */
5065
5066 static tree
5067 c_parser_asm_statement (c_parser *parser)
5068 {
5069 tree quals, str, outputs, inputs, clobbers, labels, ret;
5070 bool simple, is_goto;
5071 location_t asm_loc = c_parser_peek_token (parser)->location;
5072 int section, nsections;
5073
5074 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
5075 c_parser_consume_token (parser);
5076 if (c_parser_next_token_is_keyword (parser, RID_VOLATILE))
5077 {
5078 quals = c_parser_peek_token (parser)->value;
5079 c_parser_consume_token (parser);
5080 }
5081 else if (c_parser_next_token_is_keyword (parser, RID_CONST)
5082 || c_parser_next_token_is_keyword (parser, RID_RESTRICT))
5083 {
5084 warning_at (c_parser_peek_token (parser)->location,
5085 0,
5086 "%E qualifier ignored on asm",
5087 c_parser_peek_token (parser)->value);
5088 quals = NULL_TREE;
5089 c_parser_consume_token (parser);
5090 }
5091 else
5092 quals = NULL_TREE;
5093
5094 is_goto = false;
5095 if (c_parser_next_token_is_keyword (parser, RID_GOTO))
5096 {
5097 c_parser_consume_token (parser);
5098 is_goto = true;
5099 }
5100
5101 /* ??? Follow the C++ parser rather than using the
5102 lex_untranslated_string kludge. */
5103 parser->lex_untranslated_string = true;
5104 ret = NULL;
5105
5106 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5107 goto error;
5108
5109 str = c_parser_asm_string_literal (parser);
5110 if (str == NULL_TREE)
5111 goto error_close_paren;
5112
5113 simple = true;
5114 outputs = NULL_TREE;
5115 inputs = NULL_TREE;
5116 clobbers = NULL_TREE;
5117 labels = NULL_TREE;
5118
5119 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN) && !is_goto)
5120 goto done_asm;
5121
5122 /* Parse each colon-delimited section of operands. */
5123 nsections = 3 + is_goto;
5124 for (section = 0; section < nsections; ++section)
5125 {
5126 if (!c_parser_require (parser, CPP_COLON,
5127 is_goto
5128 ? "expected %<:%>"
5129 : "expected %<:%> or %<)%>"))
5130 goto error_close_paren;
5131
5132 /* Once past any colon, we're no longer a simple asm. */
5133 simple = false;
5134
5135 if ((!c_parser_next_token_is (parser, CPP_COLON)
5136 && !c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
5137 || section == 3)
5138 switch (section)
5139 {
5140 case 0:
5141 /* For asm goto, we don't allow output operands, but reserve
5142 the slot for a future extension that does allow them. */
5143 if (!is_goto)
5144 outputs = c_parser_asm_operands (parser, false);
5145 break;
5146 case 1:
5147 inputs = c_parser_asm_operands (parser, true);
5148 break;
5149 case 2:
5150 clobbers = c_parser_asm_clobbers (parser);
5151 break;
5152 case 3:
5153 labels = c_parser_asm_goto_operands (parser);
5154 break;
5155 default:
5156 gcc_unreachable ();
5157 }
5158
5159 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN) && !is_goto)
5160 goto done_asm;
5161 }
5162
5163 done_asm:
5164 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
5165 {
5166 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5167 goto error;
5168 }
5169
5170 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
5171 c_parser_skip_to_end_of_block_or_statement (parser);
5172
5173 ret = build_asm_stmt (quals, build_asm_expr (asm_loc, str, outputs, inputs,
5174 clobbers, labels, simple));
5175
5176 error:
5177 parser->lex_untranslated_string = false;
5178 return ret;
5179
5180 error_close_paren:
5181 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5182 goto error;
5183 }
5184
5185 /* Parse asm operands, a GNU extension. If CONVERT_P (for inputs but
5186 not outputs), apply the default conversion of functions and arrays
5187 to pointers.
5188
5189 asm-operands:
5190 asm-operand
5191 asm-operands , asm-operand
5192
5193 asm-operand:
5194 asm-string-literal ( expression )
5195 [ identifier ] asm-string-literal ( expression )
5196 */
5197
5198 static tree
5199 c_parser_asm_operands (c_parser *parser, bool convert_p)
5200 {
5201 tree list = NULL_TREE;
5202 location_t loc;
5203 while (true)
5204 {
5205 tree name, str;
5206 struct c_expr expr;
5207 if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
5208 {
5209 c_parser_consume_token (parser);
5210 if (c_parser_next_token_is (parser, CPP_NAME))
5211 {
5212 tree id = c_parser_peek_token (parser)->value;
5213 c_parser_consume_token (parser);
5214 name = build_string (IDENTIFIER_LENGTH (id),
5215 IDENTIFIER_POINTER (id));
5216 }
5217 else
5218 {
5219 c_parser_error (parser, "expected identifier");
5220 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
5221 return NULL_TREE;
5222 }
5223 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
5224 "expected %<]%>");
5225 }
5226 else
5227 name = NULL_TREE;
5228 str = c_parser_asm_string_literal (parser);
5229 if (str == NULL_TREE)
5230 return NULL_TREE;
5231 parser->lex_untranslated_string = false;
5232 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5233 {
5234 parser->lex_untranslated_string = true;
5235 return NULL_TREE;
5236 }
5237 loc = c_parser_peek_token (parser)->location;
5238 expr = c_parser_expression (parser);
5239 mark_exp_read (expr.value);
5240 if (convert_p)
5241 expr = default_function_array_conversion (loc, expr);
5242 expr.value = c_fully_fold (expr.value, false, NULL);
5243 parser->lex_untranslated_string = true;
5244 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
5245 {
5246 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5247 return NULL_TREE;
5248 }
5249 list = chainon (list, build_tree_list (build_tree_list (name, str),
5250 expr.value));
5251 if (c_parser_next_token_is (parser, CPP_COMMA))
5252 c_parser_consume_token (parser);
5253 else
5254 break;
5255 }
5256 return list;
5257 }
5258
5259 /* Parse asm clobbers, a GNU extension.
5260
5261 asm-clobbers:
5262 asm-string-literal
5263 asm-clobbers , asm-string-literal
5264 */
5265
5266 static tree
5267 c_parser_asm_clobbers (c_parser *parser)
5268 {
5269 tree list = NULL_TREE;
5270 while (true)
5271 {
5272 tree str = c_parser_asm_string_literal (parser);
5273 if (str)
5274 list = tree_cons (NULL_TREE, str, list);
5275 else
5276 return NULL_TREE;
5277 if (c_parser_next_token_is (parser, CPP_COMMA))
5278 c_parser_consume_token (parser);
5279 else
5280 break;
5281 }
5282 return list;
5283 }
5284
5285 /* Parse asm goto labels, a GNU extension.
5286
5287 asm-goto-operands:
5288 identifier
5289 asm-goto-operands , identifier
5290 */
5291
5292 static tree
5293 c_parser_asm_goto_operands (c_parser *parser)
5294 {
5295 tree list = NULL_TREE;
5296 while (true)
5297 {
5298 tree name, label;
5299
5300 if (c_parser_next_token_is (parser, CPP_NAME))
5301 {
5302 c_token *tok = c_parser_peek_token (parser);
5303 name = tok->value;
5304 label = lookup_label_for_goto (tok->location, name);
5305 c_parser_consume_token (parser);
5306 TREE_USED (label) = 1;
5307 }
5308 else
5309 {
5310 c_parser_error (parser, "expected identifier");
5311 return NULL_TREE;
5312 }
5313
5314 name = build_string (IDENTIFIER_LENGTH (name),
5315 IDENTIFIER_POINTER (name));
5316 list = tree_cons (name, label, list);
5317 if (c_parser_next_token_is (parser, CPP_COMMA))
5318 c_parser_consume_token (parser);
5319 else
5320 return nreverse (list);
5321 }
5322 }
5323
5324 /* Parse an expression other than a compound expression; that is, an
5325 assignment expression (C90 6.3.16, C99 6.5.16). If AFTER is not
5326 NULL then it is an Objective-C message expression which is the
5327 primary-expression starting the expression as an initializer.
5328
5329 assignment-expression:
5330 conditional-expression
5331 unary-expression assignment-operator assignment-expression
5332
5333 assignment-operator: one of
5334 = *= /= %= += -= <<= >>= &= ^= |=
5335
5336 In GNU C we accept any conditional expression on the LHS and
5337 diagnose the invalid lvalue rather than producing a syntax
5338 error. */
5339
5340 static struct c_expr
5341 c_parser_expr_no_commas (c_parser *parser, struct c_expr *after)
5342 {
5343 struct c_expr lhs, rhs, ret;
5344 enum tree_code code;
5345 location_t op_location, exp_location;
5346 gcc_assert (!after || c_dialect_objc ());
5347 lhs = c_parser_conditional_expression (parser, after);
5348 op_location = c_parser_peek_token (parser)->location;
5349 switch (c_parser_peek_token (parser)->type)
5350 {
5351 case CPP_EQ:
5352 code = NOP_EXPR;
5353 break;
5354 case CPP_MULT_EQ:
5355 code = MULT_EXPR;
5356 break;
5357 case CPP_DIV_EQ:
5358 code = TRUNC_DIV_EXPR;
5359 break;
5360 case CPP_MOD_EQ:
5361 code = TRUNC_MOD_EXPR;
5362 break;
5363 case CPP_PLUS_EQ:
5364 code = PLUS_EXPR;
5365 break;
5366 case CPP_MINUS_EQ:
5367 code = MINUS_EXPR;
5368 break;
5369 case CPP_LSHIFT_EQ:
5370 code = LSHIFT_EXPR;
5371 break;
5372 case CPP_RSHIFT_EQ:
5373 code = RSHIFT_EXPR;
5374 break;
5375 case CPP_AND_EQ:
5376 code = BIT_AND_EXPR;
5377 break;
5378 case CPP_XOR_EQ:
5379 code = BIT_XOR_EXPR;
5380 break;
5381 case CPP_OR_EQ:
5382 code = BIT_IOR_EXPR;
5383 break;
5384 default:
5385 return lhs;
5386 }
5387 c_parser_consume_token (parser);
5388 exp_location = c_parser_peek_token (parser)->location;
5389 rhs = c_parser_expr_no_commas (parser, NULL);
5390 rhs = default_function_array_read_conversion (exp_location, rhs);
5391 ret.value = build_modify_expr (op_location, lhs.value, lhs.original_type,
5392 code, exp_location, rhs.value,
5393 rhs.original_type);
5394 if (code == NOP_EXPR)
5395 ret.original_code = MODIFY_EXPR;
5396 else
5397 {
5398 TREE_NO_WARNING (ret.value) = 1;
5399 ret.original_code = ERROR_MARK;
5400 }
5401 ret.original_type = NULL;
5402 return ret;
5403 }
5404
5405 /* Parse a conditional expression (C90 6.3.15, C99 6.5.15). If AFTER
5406 is not NULL then it is an Objective-C message expression which is
5407 the primary-expression starting the expression as an initializer.
5408
5409 conditional-expression:
5410 logical-OR-expression
5411 logical-OR-expression ? expression : conditional-expression
5412
5413 GNU extensions:
5414
5415 conditional-expression:
5416 logical-OR-expression ? : conditional-expression
5417 */
5418
5419 static struct c_expr
5420 c_parser_conditional_expression (c_parser *parser, struct c_expr *after)
5421 {
5422 struct c_expr cond, exp1, exp2, ret;
5423 location_t cond_loc, colon_loc, middle_loc;
5424
5425 gcc_assert (!after || c_dialect_objc ());
5426
5427 cond = c_parser_binary_expression (parser, after, PREC_NONE);
5428
5429 if (c_parser_next_token_is_not (parser, CPP_QUERY))
5430 return cond;
5431 cond_loc = c_parser_peek_token (parser)->location;
5432 cond = default_function_array_read_conversion (cond_loc, cond);
5433 c_parser_consume_token (parser);
5434 if (c_parser_next_token_is (parser, CPP_COLON))
5435 {
5436 tree eptype = NULL_TREE;
5437
5438 middle_loc = c_parser_peek_token (parser)->location;
5439 pedwarn (middle_loc, OPT_pedantic,
5440 "ISO C forbids omitting the middle term of a ?: expression");
5441 warn_for_omitted_condop (middle_loc, cond.value);
5442 if (TREE_CODE (cond.value) == EXCESS_PRECISION_EXPR)
5443 {
5444 eptype = TREE_TYPE (cond.value);
5445 cond.value = TREE_OPERAND (cond.value, 0);
5446 }
5447 /* Make sure first operand is calculated only once. */
5448 exp1.value = c_save_expr (default_conversion (cond.value));
5449 if (eptype)
5450 exp1.value = build1 (EXCESS_PRECISION_EXPR, eptype, exp1.value);
5451 exp1.original_type = NULL;
5452 cond.value = c_objc_common_truthvalue_conversion (cond_loc, exp1.value);
5453 c_inhibit_evaluation_warnings += cond.value == truthvalue_true_node;
5454 }
5455 else
5456 {
5457 cond.value
5458 = c_objc_common_truthvalue_conversion
5459 (cond_loc, default_conversion (cond.value));
5460 c_inhibit_evaluation_warnings += cond.value == truthvalue_false_node;
5461 exp1 = c_parser_expression_conv (parser);
5462 mark_exp_read (exp1.value);
5463 c_inhibit_evaluation_warnings +=
5464 ((cond.value == truthvalue_true_node)
5465 - (cond.value == truthvalue_false_node));
5466 }
5467
5468 colon_loc = c_parser_peek_token (parser)->location;
5469 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
5470 {
5471 c_inhibit_evaluation_warnings -= cond.value == truthvalue_true_node;
5472 ret.value = error_mark_node;
5473 ret.original_code = ERROR_MARK;
5474 ret.original_type = NULL;
5475 return ret;
5476 }
5477 {
5478 location_t exp2_loc = c_parser_peek_token (parser)->location;
5479 exp2 = c_parser_conditional_expression (parser, NULL);
5480 exp2 = default_function_array_read_conversion (exp2_loc, exp2);
5481 }
5482 c_inhibit_evaluation_warnings -= cond.value == truthvalue_true_node;
5483 ret.value = build_conditional_expr (colon_loc, cond.value,
5484 cond.original_code == C_MAYBE_CONST_EXPR,
5485 exp1.value, exp1.original_type,
5486 exp2.value, exp2.original_type);
5487 ret.original_code = ERROR_MARK;
5488 if (exp1.value == error_mark_node || exp2.value == error_mark_node)
5489 ret.original_type = NULL;
5490 else
5491 {
5492 tree t1, t2;
5493
5494 /* If both sides are enum type, the default conversion will have
5495 made the type of the result be an integer type. We want to
5496 remember the enum types we started with. */
5497 t1 = exp1.original_type ? exp1.original_type : TREE_TYPE (exp1.value);
5498 t2 = exp2.original_type ? exp2.original_type : TREE_TYPE (exp2.value);
5499 ret.original_type = ((t1 != error_mark_node
5500 && t2 != error_mark_node
5501 && (TYPE_MAIN_VARIANT (t1)
5502 == TYPE_MAIN_VARIANT (t2)))
5503 ? t1
5504 : NULL);
5505 }
5506 return ret;
5507 }
5508
5509 /* Parse a binary expression; that is, a logical-OR-expression (C90
5510 6.3.5-6.3.14, C99 6.5.5-6.5.14). If AFTER is not NULL then it is
5511 an Objective-C message expression which is the primary-expression
5512 starting the expression as an initializer. PREC is the starting
5513 precedence, usually PREC_NONE.
5514
5515 multiplicative-expression:
5516 cast-expression
5517 multiplicative-expression * cast-expression
5518 multiplicative-expression / cast-expression
5519 multiplicative-expression % cast-expression
5520
5521 additive-expression:
5522 multiplicative-expression
5523 additive-expression + multiplicative-expression
5524 additive-expression - multiplicative-expression
5525
5526 shift-expression:
5527 additive-expression
5528 shift-expression << additive-expression
5529 shift-expression >> additive-expression
5530
5531 relational-expression:
5532 shift-expression
5533 relational-expression < shift-expression
5534 relational-expression > shift-expression
5535 relational-expression <= shift-expression
5536 relational-expression >= shift-expression
5537
5538 equality-expression:
5539 relational-expression
5540 equality-expression == relational-expression
5541 equality-expression != relational-expression
5542
5543 AND-expression:
5544 equality-expression
5545 AND-expression & equality-expression
5546
5547 exclusive-OR-expression:
5548 AND-expression
5549 exclusive-OR-expression ^ AND-expression
5550
5551 inclusive-OR-expression:
5552 exclusive-OR-expression
5553 inclusive-OR-expression | exclusive-OR-expression
5554
5555 logical-AND-expression:
5556 inclusive-OR-expression
5557 logical-AND-expression && inclusive-OR-expression
5558
5559 logical-OR-expression:
5560 logical-AND-expression
5561 logical-OR-expression || logical-AND-expression
5562 */
5563
5564 static struct c_expr
5565 c_parser_binary_expression (c_parser *parser, struct c_expr *after,
5566 enum c_parser_prec prec)
5567 {
5568 /* A binary expression is parsed using operator-precedence parsing,
5569 with the operands being cast expressions. All the binary
5570 operators are left-associative. Thus a binary expression is of
5571 form:
5572
5573 E0 op1 E1 op2 E2 ...
5574
5575 which we represent on a stack. On the stack, the precedence
5576 levels are strictly increasing. When a new operator is
5577 encountered of higher precedence than that at the top of the
5578 stack, it is pushed; its LHS is the top expression, and its RHS
5579 is everything parsed until it is popped. When a new operator is
5580 encountered with precedence less than or equal to that at the top
5581 of the stack, triples E[i-1] op[i] E[i] are popped and replaced
5582 by the result of the operation until the operator at the top of
5583 the stack has lower precedence than the new operator or there is
5584 only one element on the stack; then the top expression is the LHS
5585 of the new operator. In the case of logical AND and OR
5586 expressions, we also need to adjust c_inhibit_evaluation_warnings
5587 as appropriate when the operators are pushed and popped. */
5588
5589 struct {
5590 /* The expression at this stack level. */
5591 struct c_expr expr;
5592 /* The precedence of the operator on its left, PREC_NONE at the
5593 bottom of the stack. */
5594 enum c_parser_prec prec;
5595 /* The operation on its left. */
5596 enum tree_code op;
5597 /* The source location of this operation. */
5598 location_t loc;
5599 } stack[NUM_PRECS];
5600 int sp;
5601 /* Location of the binary operator. */
5602 location_t binary_loc = UNKNOWN_LOCATION; /* Quiet warning. */
5603 #define POP \
5604 do { \
5605 switch (stack[sp].op) \
5606 { \
5607 case TRUTH_ANDIF_EXPR: \
5608 c_inhibit_evaluation_warnings -= (stack[sp - 1].expr.value \
5609 == truthvalue_false_node); \
5610 break; \
5611 case TRUTH_ORIF_EXPR: \
5612 c_inhibit_evaluation_warnings -= (stack[sp - 1].expr.value \
5613 == truthvalue_true_node); \
5614 break; \
5615 default: \
5616 break; \
5617 } \
5618 stack[sp - 1].expr \
5619 = default_function_array_read_conversion (stack[sp - 1].loc, \
5620 stack[sp - 1].expr); \
5621 stack[sp].expr \
5622 = default_function_array_read_conversion (stack[sp].loc, \
5623 stack[sp].expr); \
5624 stack[sp - 1].expr = parser_build_binary_op (stack[sp].loc, \
5625 stack[sp].op, \
5626 stack[sp - 1].expr, \
5627 stack[sp].expr); \
5628 sp--; \
5629 } while (0)
5630 gcc_assert (!after || c_dialect_objc ());
5631 stack[0].loc = c_parser_peek_token (parser)->location;
5632 stack[0].expr = c_parser_cast_expression (parser, after);
5633 stack[0].prec = prec;
5634 sp = 0;
5635 while (true)
5636 {
5637 enum c_parser_prec oprec;
5638 enum tree_code ocode;
5639 if (parser->error)
5640 goto out;
5641 switch (c_parser_peek_token (parser)->type)
5642 {
5643 case CPP_MULT:
5644 oprec = PREC_MULT;
5645 ocode = MULT_EXPR;
5646 break;
5647 case CPP_DIV:
5648 oprec = PREC_MULT;
5649 ocode = TRUNC_DIV_EXPR;
5650 break;
5651 case CPP_MOD:
5652 oprec = PREC_MULT;
5653 ocode = TRUNC_MOD_EXPR;
5654 break;
5655 case CPP_PLUS:
5656 oprec = PREC_ADD;
5657 ocode = PLUS_EXPR;
5658 break;
5659 case CPP_MINUS:
5660 oprec = PREC_ADD;
5661 ocode = MINUS_EXPR;
5662 break;
5663 case CPP_LSHIFT:
5664 oprec = PREC_SHIFT;
5665 ocode = LSHIFT_EXPR;
5666 break;
5667 case CPP_RSHIFT:
5668 oprec = PREC_SHIFT;
5669 ocode = RSHIFT_EXPR;
5670 break;
5671 case CPP_LESS:
5672 oprec = PREC_REL;
5673 ocode = LT_EXPR;
5674 break;
5675 case CPP_GREATER:
5676 oprec = PREC_REL;
5677 ocode = GT_EXPR;
5678 break;
5679 case CPP_LESS_EQ:
5680 oprec = PREC_REL;
5681 ocode = LE_EXPR;
5682 break;
5683 case CPP_GREATER_EQ:
5684 oprec = PREC_REL;
5685 ocode = GE_EXPR;
5686 break;
5687 case CPP_EQ_EQ:
5688 oprec = PREC_EQ;
5689 ocode = EQ_EXPR;
5690 break;
5691 case CPP_NOT_EQ:
5692 oprec = PREC_EQ;
5693 ocode = NE_EXPR;
5694 break;
5695 case CPP_AND:
5696 oprec = PREC_BITAND;
5697 ocode = BIT_AND_EXPR;
5698 break;
5699 case CPP_XOR:
5700 oprec = PREC_BITXOR;
5701 ocode = BIT_XOR_EXPR;
5702 break;
5703 case CPP_OR:
5704 oprec = PREC_BITOR;
5705 ocode = BIT_IOR_EXPR;
5706 break;
5707 case CPP_AND_AND:
5708 oprec = PREC_LOGAND;
5709 ocode = TRUTH_ANDIF_EXPR;
5710 break;
5711 case CPP_OR_OR:
5712 oprec = PREC_LOGOR;
5713 ocode = TRUTH_ORIF_EXPR;
5714 break;
5715 default:
5716 /* Not a binary operator, so end of the binary
5717 expression. */
5718 goto out;
5719 }
5720 binary_loc = c_parser_peek_token (parser)->location;
5721 while (oprec <= stack[sp].prec)
5722 {
5723 if (sp == 0)
5724 goto out;
5725 POP;
5726 }
5727 c_parser_consume_token (parser);
5728 switch (ocode)
5729 {
5730 case TRUTH_ANDIF_EXPR:
5731 stack[sp].expr
5732 = default_function_array_read_conversion (stack[sp].loc,
5733 stack[sp].expr);
5734 stack[sp].expr.value = c_objc_common_truthvalue_conversion
5735 (stack[sp].loc, default_conversion (stack[sp].expr.value));
5736 c_inhibit_evaluation_warnings += (stack[sp].expr.value
5737 == truthvalue_false_node);
5738 break;
5739 case TRUTH_ORIF_EXPR:
5740 stack[sp].expr
5741 = default_function_array_read_conversion (stack[sp].loc,
5742 stack[sp].expr);
5743 stack[sp].expr.value = c_objc_common_truthvalue_conversion
5744 (stack[sp].loc, default_conversion (stack[sp].expr.value));
5745 c_inhibit_evaluation_warnings += (stack[sp].expr.value
5746 == truthvalue_true_node);
5747 break;
5748 default:
5749 break;
5750 }
5751 sp++;
5752 stack[sp].loc = binary_loc;
5753 stack[sp].expr = c_parser_cast_expression (parser, NULL);
5754 stack[sp].prec = oprec;
5755 stack[sp].op = ocode;
5756 stack[sp].loc = binary_loc;
5757 }
5758 out:
5759 while (sp > 0)
5760 POP;
5761 return stack[0].expr;
5762 #undef POP
5763 }
5764
5765 /* Parse a cast expression (C90 6.3.4, C99 6.5.4). If AFTER is not
5766 NULL then it is an Objective-C message expression which is the
5767 primary-expression starting the expression as an initializer.
5768
5769 cast-expression:
5770 unary-expression
5771 ( type-name ) unary-expression
5772 */
5773
5774 static struct c_expr
5775 c_parser_cast_expression (c_parser *parser, struct c_expr *after)
5776 {
5777 location_t cast_loc = c_parser_peek_token (parser)->location;
5778 gcc_assert (!after || c_dialect_objc ());
5779 if (after)
5780 return c_parser_postfix_expression_after_primary (parser,
5781 cast_loc, *after);
5782 /* If the expression begins with a parenthesized type name, it may
5783 be either a cast or a compound literal; we need to see whether
5784 the next character is '{' to tell the difference. If not, it is
5785 an unary expression. Full detection of unknown typenames here
5786 would require a 3-token lookahead. */
5787 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
5788 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
5789 {
5790 struct c_type_name *type_name;
5791 struct c_expr ret;
5792 struct c_expr expr;
5793 c_parser_consume_token (parser);
5794 type_name = c_parser_type_name (parser);
5795 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5796 if (type_name == NULL)
5797 {
5798 ret.value = error_mark_node;
5799 ret.original_code = ERROR_MARK;
5800 ret.original_type = NULL;
5801 return ret;
5802 }
5803
5804 /* Save casted types in the function's used types hash table. */
5805 used_types_insert (type_name->specs->type);
5806
5807 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
5808 return c_parser_postfix_expression_after_paren_type (parser, type_name,
5809 cast_loc);
5810 {
5811 location_t expr_loc = c_parser_peek_token (parser)->location;
5812 expr = c_parser_cast_expression (parser, NULL);
5813 expr = default_function_array_read_conversion (expr_loc, expr);
5814 }
5815 ret.value = c_cast_expr (cast_loc, type_name, expr.value);
5816 ret.original_code = ERROR_MARK;
5817 ret.original_type = NULL;
5818 return ret;
5819 }
5820 else
5821 return c_parser_unary_expression (parser);
5822 }
5823
5824 /* Parse an unary expression (C90 6.3.3, C99 6.5.3).
5825
5826 unary-expression:
5827 postfix-expression
5828 ++ unary-expression
5829 -- unary-expression
5830 unary-operator cast-expression
5831 sizeof unary-expression
5832 sizeof ( type-name )
5833
5834 unary-operator: one of
5835 & * + - ~ !
5836
5837 GNU extensions:
5838
5839 unary-expression:
5840 __alignof__ unary-expression
5841 __alignof__ ( type-name )
5842 && identifier
5843
5844 (C1X permits _Alignof with type names only.)
5845
5846 unary-operator: one of
5847 __extension__ __real__ __imag__
5848
5849 Transactional Memory:
5850
5851 unary-expression:
5852 transaction-expression
5853
5854 In addition, the GNU syntax treats ++ and -- as unary operators, so
5855 they may be applied to cast expressions with errors for non-lvalues
5856 given later. */
5857
5858 static struct c_expr
5859 c_parser_unary_expression (c_parser *parser)
5860 {
5861 int ext;
5862 struct c_expr ret, op;
5863 location_t op_loc = c_parser_peek_token (parser)->location;
5864 location_t exp_loc;
5865 ret.original_code = ERROR_MARK;
5866 ret.original_type = NULL;
5867 switch (c_parser_peek_token (parser)->type)
5868 {
5869 case CPP_PLUS_PLUS:
5870 c_parser_consume_token (parser);
5871 exp_loc = c_parser_peek_token (parser)->location;
5872 op = c_parser_cast_expression (parser, NULL);
5873 op = default_function_array_read_conversion (exp_loc, op);
5874 return parser_build_unary_op (op_loc, PREINCREMENT_EXPR, op);
5875 case CPP_MINUS_MINUS:
5876 c_parser_consume_token (parser);
5877 exp_loc = c_parser_peek_token (parser)->location;
5878 op = c_parser_cast_expression (parser, NULL);
5879 op = default_function_array_read_conversion (exp_loc, op);
5880 return parser_build_unary_op (op_loc, PREDECREMENT_EXPR, op);
5881 case CPP_AND:
5882 c_parser_consume_token (parser);
5883 op = c_parser_cast_expression (parser, NULL);
5884 mark_exp_read (op.value);
5885 return parser_build_unary_op (op_loc, ADDR_EXPR, op);
5886 case CPP_MULT:
5887 c_parser_consume_token (parser);
5888 exp_loc = c_parser_peek_token (parser)->location;
5889 op = c_parser_cast_expression (parser, NULL);
5890 op = default_function_array_read_conversion (exp_loc, op);
5891 ret.value = build_indirect_ref (op_loc, op.value, RO_UNARY_STAR);
5892 return ret;
5893 case CPP_PLUS:
5894 if (!c_dialect_objc () && !in_system_header)
5895 warning_at (op_loc,
5896 OPT_Wtraditional,
5897 "traditional C rejects the unary plus operator");
5898 c_parser_consume_token (parser);
5899 exp_loc = c_parser_peek_token (parser)->location;
5900 op = c_parser_cast_expression (parser, NULL);
5901 op = default_function_array_read_conversion (exp_loc, op);
5902 return parser_build_unary_op (op_loc, CONVERT_EXPR, op);
5903 case CPP_MINUS:
5904 c_parser_consume_token (parser);
5905 exp_loc = c_parser_peek_token (parser)->location;
5906 op = c_parser_cast_expression (parser, NULL);
5907 op = default_function_array_read_conversion (exp_loc, op);
5908 return parser_build_unary_op (op_loc, NEGATE_EXPR, op);
5909 case CPP_COMPL:
5910 c_parser_consume_token (parser);
5911 exp_loc = c_parser_peek_token (parser)->location;
5912 op = c_parser_cast_expression (parser, NULL);
5913 op = default_function_array_read_conversion (exp_loc, op);
5914 return parser_build_unary_op (op_loc, BIT_NOT_EXPR, op);
5915 case CPP_NOT:
5916 c_parser_consume_token (parser);
5917 exp_loc = c_parser_peek_token (parser)->location;
5918 op = c_parser_cast_expression (parser, NULL);
5919 op = default_function_array_read_conversion (exp_loc, op);
5920 return parser_build_unary_op (op_loc, TRUTH_NOT_EXPR, op);
5921 case CPP_AND_AND:
5922 /* Refer to the address of a label as a pointer. */
5923 c_parser_consume_token (parser);
5924 if (c_parser_next_token_is (parser, CPP_NAME))
5925 {
5926 ret.value = finish_label_address_expr
5927 (c_parser_peek_token (parser)->value, op_loc);
5928 c_parser_consume_token (parser);
5929 }
5930 else
5931 {
5932 c_parser_error (parser, "expected identifier");
5933 ret.value = error_mark_node;
5934 }
5935 return ret;
5936 case CPP_KEYWORD:
5937 switch (c_parser_peek_token (parser)->keyword)
5938 {
5939 case RID_SIZEOF:
5940 return c_parser_sizeof_expression (parser);
5941 case RID_ALIGNOF:
5942 return c_parser_alignof_expression (parser);
5943 case RID_EXTENSION:
5944 c_parser_consume_token (parser);
5945 ext = disable_extension_diagnostics ();
5946 ret = c_parser_cast_expression (parser, NULL);
5947 restore_extension_diagnostics (ext);
5948 return ret;
5949 case RID_REALPART:
5950 c_parser_consume_token (parser);
5951 exp_loc = c_parser_peek_token (parser)->location;
5952 op = c_parser_cast_expression (parser, NULL);
5953 op = default_function_array_conversion (exp_loc, op);
5954 return parser_build_unary_op (op_loc, REALPART_EXPR, op);
5955 case RID_IMAGPART:
5956 c_parser_consume_token (parser);
5957 exp_loc = c_parser_peek_token (parser)->location;
5958 op = c_parser_cast_expression (parser, NULL);
5959 op = default_function_array_conversion (exp_loc, op);
5960 return parser_build_unary_op (op_loc, IMAGPART_EXPR, op);
5961 case RID_TRANSACTION_ATOMIC:
5962 case RID_TRANSACTION_RELAXED:
5963 return c_parser_transaction_expression (parser,
5964 c_parser_peek_token (parser)->keyword);
5965 default:
5966 return c_parser_postfix_expression (parser);
5967 }
5968 default:
5969 return c_parser_postfix_expression (parser);
5970 }
5971 }
5972
5973 /* Parse a sizeof expression. */
5974
5975 static struct c_expr
5976 c_parser_sizeof_expression (c_parser *parser)
5977 {
5978 struct c_expr expr;
5979 location_t expr_loc;
5980 gcc_assert (c_parser_next_token_is_keyword (parser, RID_SIZEOF));
5981 c_parser_consume_token (parser);
5982 c_inhibit_evaluation_warnings++;
5983 in_sizeof++;
5984 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
5985 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
5986 {
5987 /* Either sizeof ( type-name ) or sizeof unary-expression
5988 starting with a compound literal. */
5989 struct c_type_name *type_name;
5990 c_parser_consume_token (parser);
5991 expr_loc = c_parser_peek_token (parser)->location;
5992 type_name = c_parser_type_name (parser);
5993 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5994 if (type_name == NULL)
5995 {
5996 struct c_expr ret;
5997 c_inhibit_evaluation_warnings--;
5998 in_sizeof--;
5999 ret.value = error_mark_node;
6000 ret.original_code = ERROR_MARK;
6001 ret.original_type = NULL;
6002 return ret;
6003 }
6004 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
6005 {
6006 expr = c_parser_postfix_expression_after_paren_type (parser,
6007 type_name,
6008 expr_loc);
6009 goto sizeof_expr;
6010 }
6011 /* sizeof ( type-name ). */
6012 c_inhibit_evaluation_warnings--;
6013 in_sizeof--;
6014 return c_expr_sizeof_type (expr_loc, type_name);
6015 }
6016 else
6017 {
6018 expr_loc = c_parser_peek_token (parser)->location;
6019 expr = c_parser_unary_expression (parser);
6020 sizeof_expr:
6021 c_inhibit_evaluation_warnings--;
6022 in_sizeof--;
6023 mark_exp_read (expr.value);
6024 if (TREE_CODE (expr.value) == COMPONENT_REF
6025 && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
6026 error_at (expr_loc, "%<sizeof%> applied to a bit-field");
6027 return c_expr_sizeof_expr (expr_loc, expr);
6028 }
6029 }
6030
6031 /* Parse an alignof expression. */
6032
6033 static struct c_expr
6034 c_parser_alignof_expression (c_parser *parser)
6035 {
6036 struct c_expr expr;
6037 location_t loc = c_parser_peek_token (parser)->location;
6038 tree alignof_spelling = c_parser_peek_token (parser)->value;
6039 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ALIGNOF));
6040 /* A diagnostic is not required for the use of this identifier in
6041 the implementation namespace; only diagnose it for the C1X
6042 spelling because of existing code using the other spellings. */
6043 if (!flag_isoc1x
6044 && strcmp (IDENTIFIER_POINTER (alignof_spelling), "_Alignof") == 0)
6045 {
6046 if (flag_isoc99)
6047 pedwarn (loc, OPT_pedantic, "ISO C99 does not support %qE",
6048 alignof_spelling);
6049 else
6050 pedwarn (loc, OPT_pedantic, "ISO C90 does not support %qE",
6051 alignof_spelling);
6052 }
6053 c_parser_consume_token (parser);
6054 c_inhibit_evaluation_warnings++;
6055 in_alignof++;
6056 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
6057 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
6058 {
6059 /* Either __alignof__ ( type-name ) or __alignof__
6060 unary-expression starting with a compound literal. */
6061 location_t loc;
6062 struct c_type_name *type_name;
6063 struct c_expr ret;
6064 c_parser_consume_token (parser);
6065 loc = c_parser_peek_token (parser)->location;
6066 type_name = c_parser_type_name (parser);
6067 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6068 if (type_name == NULL)
6069 {
6070 struct c_expr ret;
6071 c_inhibit_evaluation_warnings--;
6072 in_alignof--;
6073 ret.value = error_mark_node;
6074 ret.original_code = ERROR_MARK;
6075 ret.original_type = NULL;
6076 return ret;
6077 }
6078 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
6079 {
6080 expr = c_parser_postfix_expression_after_paren_type (parser,
6081 type_name,
6082 loc);
6083 goto alignof_expr;
6084 }
6085 /* alignof ( type-name ). */
6086 c_inhibit_evaluation_warnings--;
6087 in_alignof--;
6088 ret.value = c_alignof (loc, groktypename (type_name, NULL, NULL));
6089 ret.original_code = ERROR_MARK;
6090 ret.original_type = NULL;
6091 return ret;
6092 }
6093 else
6094 {
6095 struct c_expr ret;
6096 expr = c_parser_unary_expression (parser);
6097 alignof_expr:
6098 mark_exp_read (expr.value);
6099 c_inhibit_evaluation_warnings--;
6100 in_alignof--;
6101 pedwarn (loc, OPT_pedantic, "ISO C does not allow %<%E (expression)%>",
6102 alignof_spelling);
6103 ret.value = c_alignof_expr (loc, expr.value);
6104 ret.original_code = ERROR_MARK;
6105 ret.original_type = NULL;
6106 return ret;
6107 }
6108 }
6109
6110 /* Helper function to read arguments of builtins which are interfaces
6111 for the middle-end nodes like COMPLEX_EXPR, VEC_PERM_EXPR and
6112 others. The name of the builtin is passed using BNAME parameter.
6113 Function returns true if there were no errors while parsing and
6114 stores the arguments in CEXPR_LIST. */
6115 static bool
6116 c_parser_get_builtin_args (c_parser *parser, const char *bname,
6117 VEC(c_expr_t,gc) **ret_cexpr_list)
6118 {
6119 location_t loc = c_parser_peek_token (parser)->location;
6120 VEC (c_expr_t,gc) *cexpr_list;
6121 c_expr_t expr;
6122
6123 *ret_cexpr_list = NULL;
6124 if (c_parser_next_token_is_not (parser, CPP_OPEN_PAREN))
6125 {
6126 error_at (loc, "cannot take address of %qs", bname);
6127 return false;
6128 }
6129
6130 c_parser_consume_token (parser);
6131
6132 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
6133 {
6134 c_parser_consume_token (parser);
6135 return true;
6136 }
6137
6138 expr = c_parser_expr_no_commas (parser, NULL);
6139 cexpr_list = VEC_alloc (c_expr_t, gc, 1);
6140 C_EXPR_APPEND (cexpr_list, expr);
6141 while (c_parser_next_token_is (parser, CPP_COMMA))
6142 {
6143 c_parser_consume_token (parser);
6144 expr = c_parser_expr_no_commas (parser, NULL);
6145 C_EXPR_APPEND (cexpr_list, expr);
6146 }
6147
6148 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
6149 return false;
6150
6151 *ret_cexpr_list = cexpr_list;
6152 return true;
6153 }
6154
6155
6156 /* Parse a postfix expression (C90 6.3.1-6.3.2, C99 6.5.1-6.5.2).
6157
6158 postfix-expression:
6159 primary-expression
6160 postfix-expression [ expression ]
6161 postfix-expression ( argument-expression-list[opt] )
6162 postfix-expression . identifier
6163 postfix-expression -> identifier
6164 postfix-expression ++
6165 postfix-expression --
6166 ( type-name ) { initializer-list }
6167 ( type-name ) { initializer-list , }
6168
6169 argument-expression-list:
6170 argument-expression
6171 argument-expression-list , argument-expression
6172
6173 primary-expression:
6174 identifier
6175 constant
6176 string-literal
6177 ( expression )
6178
6179 GNU extensions:
6180
6181 primary-expression:
6182 __func__
6183 (treated as a keyword in GNU C)
6184 __FUNCTION__
6185 __PRETTY_FUNCTION__
6186 ( compound-statement )
6187 __builtin_va_arg ( assignment-expression , type-name )
6188 __builtin_offsetof ( type-name , offsetof-member-designator )
6189 __builtin_choose_expr ( assignment-expression ,
6190 assignment-expression ,
6191 assignment-expression )
6192 __builtin_types_compatible_p ( type-name , type-name )
6193 __builtin_complex ( assignment-expression , assignment-expression )
6194 __builtin_shuffle ( assignment-expression , assignment-expression )
6195 __builtin_shuffle ( assignment-expression ,
6196 assignment-expression ,
6197 assignment-expression, )
6198
6199 offsetof-member-designator:
6200 identifier
6201 offsetof-member-designator . identifier
6202 offsetof-member-designator [ expression ]
6203
6204 Objective-C:
6205
6206 primary-expression:
6207 [ objc-receiver objc-message-args ]
6208 @selector ( objc-selector-arg )
6209 @protocol ( identifier )
6210 @encode ( type-name )
6211 objc-string-literal
6212 Classname . identifier
6213 */
6214
6215 static struct c_expr
6216 c_parser_postfix_expression (c_parser *parser)
6217 {
6218 struct c_expr expr, e1;
6219 struct c_type_name *t1, *t2;
6220 location_t loc = c_parser_peek_token (parser)->location;;
6221 expr.original_code = ERROR_MARK;
6222 expr.original_type = NULL;
6223 switch (c_parser_peek_token (parser)->type)
6224 {
6225 case CPP_NUMBER:
6226 expr.value = c_parser_peek_token (parser)->value;
6227 loc = c_parser_peek_token (parser)->location;
6228 c_parser_consume_token (parser);
6229 if (TREE_CODE (expr.value) == FIXED_CST
6230 && !targetm.fixed_point_supported_p ())
6231 {
6232 error_at (loc, "fixed-point types not supported for this target");
6233 expr.value = error_mark_node;
6234 }
6235 break;
6236 case CPP_CHAR:
6237 case CPP_CHAR16:
6238 case CPP_CHAR32:
6239 case CPP_WCHAR:
6240 expr.value = c_parser_peek_token (parser)->value;
6241 c_parser_consume_token (parser);
6242 break;
6243 case CPP_STRING:
6244 case CPP_STRING16:
6245 case CPP_STRING32:
6246 case CPP_WSTRING:
6247 case CPP_UTF8STRING:
6248 expr.value = c_parser_peek_token (parser)->value;
6249 expr.original_code = STRING_CST;
6250 c_parser_consume_token (parser);
6251 break;
6252 case CPP_OBJC_STRING:
6253 gcc_assert (c_dialect_objc ());
6254 expr.value
6255 = objc_build_string_object (c_parser_peek_token (parser)->value);
6256 c_parser_consume_token (parser);
6257 break;
6258 case CPP_NAME:
6259 switch (c_parser_peek_token (parser)->id_kind)
6260 {
6261 case C_ID_ID:
6262 {
6263 tree id = c_parser_peek_token (parser)->value;
6264 c_parser_consume_token (parser);
6265 expr.value = build_external_ref (loc, id,
6266 (c_parser_peek_token (parser)->type
6267 == CPP_OPEN_PAREN),
6268 &expr.original_type);
6269 break;
6270 }
6271 case C_ID_CLASSNAME:
6272 {
6273 /* Here we parse the Objective-C 2.0 Class.name dot
6274 syntax. */
6275 tree class_name = c_parser_peek_token (parser)->value;
6276 tree component;
6277 c_parser_consume_token (parser);
6278 gcc_assert (c_dialect_objc ());
6279 if (!c_parser_require (parser, CPP_DOT, "expected %<.%>"))
6280 {
6281 expr.value = error_mark_node;
6282 break;
6283 }
6284 if (c_parser_next_token_is_not (parser, CPP_NAME))
6285 {
6286 c_parser_error (parser, "expected identifier");
6287 expr.value = error_mark_node;
6288 break;
6289 }
6290 component = c_parser_peek_token (parser)->value;
6291 c_parser_consume_token (parser);
6292 expr.value = objc_build_class_component_ref (class_name,
6293 component);
6294 break;
6295 }
6296 default:
6297 c_parser_error (parser, "expected expression");
6298 expr.value = error_mark_node;
6299 break;
6300 }
6301 break;
6302 case CPP_OPEN_PAREN:
6303 /* A parenthesized expression, statement expression or compound
6304 literal. */
6305 if (c_parser_peek_2nd_token (parser)->type == CPP_OPEN_BRACE)
6306 {
6307 /* A statement expression. */
6308 tree stmt;
6309 location_t brace_loc;
6310 c_parser_consume_token (parser);
6311 brace_loc = c_parser_peek_token (parser)->location;
6312 c_parser_consume_token (parser);
6313 if (!building_stmt_list_p ())
6314 {
6315 error_at (loc, "braced-group within expression allowed "
6316 "only inside a function");
6317 parser->error = true;
6318 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
6319 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6320 expr.value = error_mark_node;
6321 break;
6322 }
6323 stmt = c_begin_stmt_expr ();
6324 c_parser_compound_statement_nostart (parser);
6325 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6326 "expected %<)%>");
6327 pedwarn (loc, OPT_pedantic,
6328 "ISO C forbids braced-groups within expressions");
6329 expr.value = c_finish_stmt_expr (brace_loc, stmt);
6330 mark_exp_read (expr.value);
6331 }
6332 else if (c_token_starts_typename (c_parser_peek_2nd_token (parser)))
6333 {
6334 /* A compound literal. ??? Can we actually get here rather
6335 than going directly to
6336 c_parser_postfix_expression_after_paren_type from
6337 elsewhere? */
6338 location_t loc;
6339 struct c_type_name *type_name;
6340 c_parser_consume_token (parser);
6341 loc = c_parser_peek_token (parser)->location;
6342 type_name = c_parser_type_name (parser);
6343 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6344 "expected %<)%>");
6345 if (type_name == NULL)
6346 {
6347 expr.value = error_mark_node;
6348 }
6349 else
6350 expr = c_parser_postfix_expression_after_paren_type (parser,
6351 type_name,
6352 loc);
6353 }
6354 else
6355 {
6356 /* A parenthesized expression. */
6357 c_parser_consume_token (parser);
6358 expr = c_parser_expression (parser);
6359 if (TREE_CODE (expr.value) == MODIFY_EXPR)
6360 TREE_NO_WARNING (expr.value) = 1;
6361 if (expr.original_code != C_MAYBE_CONST_EXPR)
6362 expr.original_code = ERROR_MARK;
6363 /* Don't change EXPR.ORIGINAL_TYPE. */
6364 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6365 "expected %<)%>");
6366 }
6367 break;
6368 case CPP_KEYWORD:
6369 switch (c_parser_peek_token (parser)->keyword)
6370 {
6371 case RID_FUNCTION_NAME:
6372 case RID_PRETTY_FUNCTION_NAME:
6373 case RID_C99_FUNCTION_NAME:
6374 expr.value = fname_decl (loc,
6375 c_parser_peek_token (parser)->keyword,
6376 c_parser_peek_token (parser)->value);
6377 c_parser_consume_token (parser);
6378 break;
6379 case RID_VA_ARG:
6380 c_parser_consume_token (parser);
6381 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6382 {
6383 expr.value = error_mark_node;
6384 break;
6385 }
6386 e1 = c_parser_expr_no_commas (parser, NULL);
6387 mark_exp_read (e1.value);
6388 e1.value = c_fully_fold (e1.value, false, NULL);
6389 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
6390 {
6391 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6392 expr.value = error_mark_node;
6393 break;
6394 }
6395 loc = c_parser_peek_token (parser)->location;
6396 t1 = c_parser_type_name (parser);
6397 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6398 "expected %<)%>");
6399 if (t1 == NULL)
6400 {
6401 expr.value = error_mark_node;
6402 }
6403 else
6404 {
6405 tree type_expr = NULL_TREE;
6406 expr.value = c_build_va_arg (loc, e1.value,
6407 groktypename (t1, &type_expr, NULL));
6408 if (type_expr)
6409 {
6410 expr.value = build2 (C_MAYBE_CONST_EXPR,
6411 TREE_TYPE (expr.value), type_expr,
6412 expr.value);
6413 C_MAYBE_CONST_EXPR_NON_CONST (expr.value) = true;
6414 }
6415 }
6416 break;
6417 case RID_OFFSETOF:
6418 c_parser_consume_token (parser);
6419 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6420 {
6421 expr.value = error_mark_node;
6422 break;
6423 }
6424 t1 = c_parser_type_name (parser);
6425 if (t1 == NULL)
6426 parser->error = true;
6427 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
6428 gcc_assert (parser->error);
6429 if (parser->error)
6430 {
6431 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6432 expr.value = error_mark_node;
6433 break;
6434 }
6435
6436 {
6437 tree type = groktypename (t1, NULL, NULL);
6438 tree offsetof_ref;
6439 if (type == error_mark_node)
6440 offsetof_ref = error_mark_node;
6441 else
6442 {
6443 offsetof_ref = build1 (INDIRECT_REF, type, null_pointer_node);
6444 SET_EXPR_LOCATION (offsetof_ref, loc);
6445 }
6446 /* Parse the second argument to __builtin_offsetof. We
6447 must have one identifier, and beyond that we want to
6448 accept sub structure and sub array references. */
6449 if (c_parser_next_token_is (parser, CPP_NAME))
6450 {
6451 offsetof_ref = build_component_ref
6452 (loc, offsetof_ref, c_parser_peek_token (parser)->value);
6453 c_parser_consume_token (parser);
6454 while (c_parser_next_token_is (parser, CPP_DOT)
6455 || c_parser_next_token_is (parser,
6456 CPP_OPEN_SQUARE)
6457 || c_parser_next_token_is (parser,
6458 CPP_DEREF))
6459 {
6460 if (c_parser_next_token_is (parser, CPP_DEREF))
6461 {
6462 loc = c_parser_peek_token (parser)->location;
6463 offsetof_ref = build_array_ref (loc,
6464 offsetof_ref,
6465 integer_zero_node);
6466 goto do_dot;
6467 }
6468 else if (c_parser_next_token_is (parser, CPP_DOT))
6469 {
6470 do_dot:
6471 c_parser_consume_token (parser);
6472 if (c_parser_next_token_is_not (parser,
6473 CPP_NAME))
6474 {
6475 c_parser_error (parser, "expected identifier");
6476 break;
6477 }
6478 offsetof_ref = build_component_ref
6479 (loc, offsetof_ref,
6480 c_parser_peek_token (parser)->value);
6481 c_parser_consume_token (parser);
6482 }
6483 else
6484 {
6485 tree idx;
6486 loc = c_parser_peek_token (parser)->location;
6487 c_parser_consume_token (parser);
6488 idx = c_parser_expression (parser).value;
6489 idx = c_fully_fold (idx, false, NULL);
6490 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
6491 "expected %<]%>");
6492 offsetof_ref = build_array_ref (loc, offsetof_ref, idx);
6493 }
6494 }
6495 }
6496 else
6497 c_parser_error (parser, "expected identifier");
6498 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6499 "expected %<)%>");
6500 expr.value = fold_offsetof (offsetof_ref);
6501 }
6502 break;
6503 case RID_CHOOSE_EXPR:
6504 {
6505 VEC (c_expr_t, gc) *cexpr_list;
6506 c_expr_t *e1_p, *e2_p, *e3_p;
6507 tree c;
6508
6509 c_parser_consume_token (parser);
6510 if (!c_parser_get_builtin_args (parser,
6511 "__builtin_choose_expr",
6512 &cexpr_list))
6513 {
6514 expr.value = error_mark_node;
6515 break;
6516 }
6517
6518 if (VEC_length (c_expr_t, cexpr_list) != 3)
6519 {
6520 error_at (loc, "wrong number of arguments to "
6521 "%<__builtin_choose_expr%>");
6522 expr.value = error_mark_node;
6523 break;
6524 }
6525
6526 e1_p = VEC_index (c_expr_t, cexpr_list, 0);
6527 e2_p = VEC_index (c_expr_t, cexpr_list, 1);
6528 e3_p = VEC_index (c_expr_t, cexpr_list, 2);
6529
6530 c = e1_p->value;
6531 mark_exp_read (e2_p->value);
6532 mark_exp_read (e3_p->value);
6533 if (TREE_CODE (c) != INTEGER_CST
6534 || !INTEGRAL_TYPE_P (TREE_TYPE (c)))
6535 error_at (loc,
6536 "first argument to %<__builtin_choose_expr%> not"
6537 " a constant");
6538 constant_expression_warning (c);
6539 expr = integer_zerop (c) ? *e3_p : *e2_p;
6540 break;
6541 }
6542 case RID_TYPES_COMPATIBLE_P:
6543 c_parser_consume_token (parser);
6544 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6545 {
6546 expr.value = error_mark_node;
6547 break;
6548 }
6549 t1 = c_parser_type_name (parser);
6550 if (t1 == NULL)
6551 {
6552 expr.value = error_mark_node;
6553 break;
6554 }
6555 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
6556 {
6557 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6558 expr.value = error_mark_node;
6559 break;
6560 }
6561 t2 = c_parser_type_name (parser);
6562 if (t2 == NULL)
6563 {
6564 expr.value = error_mark_node;
6565 break;
6566 }
6567 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6568 "expected %<)%>");
6569 {
6570 tree e1, e2;
6571
6572 e1 = TYPE_MAIN_VARIANT (groktypename (t1, NULL, NULL));
6573 e2 = TYPE_MAIN_VARIANT (groktypename (t2, NULL, NULL));
6574
6575 expr.value
6576 = comptypes (e1, e2) ? integer_one_node : integer_zero_node;
6577 }
6578 break;
6579 case RID_BUILTIN_COMPLEX:
6580 {
6581 VEC(c_expr_t, gc) *cexpr_list;
6582 c_expr_t *e1_p, *e2_p;
6583
6584 c_parser_consume_token (parser);
6585 if (!c_parser_get_builtin_args (parser,
6586 "__builtin_complex",
6587 &cexpr_list))
6588 {
6589 expr.value = error_mark_node;
6590 break;
6591 }
6592
6593 if (VEC_length (c_expr_t, cexpr_list) != 2)
6594 {
6595 error_at (loc, "wrong number of arguments to "
6596 "%<__builtin_complex%>");
6597 expr.value = error_mark_node;
6598 break;
6599 }
6600
6601 e1_p = VEC_index (c_expr_t, cexpr_list, 0);
6602 e2_p = VEC_index (c_expr_t, cexpr_list, 1);
6603
6604 mark_exp_read (e1_p->value);
6605 if (TREE_CODE (e1_p->value) == EXCESS_PRECISION_EXPR)
6606 e1_p->value = convert (TREE_TYPE (e1_p->value),
6607 TREE_OPERAND (e1_p->value, 0));
6608 mark_exp_read (e2_p->value);
6609 if (TREE_CODE (e2_p->value) == EXCESS_PRECISION_EXPR)
6610 e2_p->value = convert (TREE_TYPE (e2_p->value),
6611 TREE_OPERAND (e2_p->value, 0));
6612 if (!SCALAR_FLOAT_TYPE_P (TREE_TYPE (e1_p->value))
6613 || DECIMAL_FLOAT_TYPE_P (TREE_TYPE (e1_p->value))
6614 || !SCALAR_FLOAT_TYPE_P (TREE_TYPE (e2_p->value))
6615 || DECIMAL_FLOAT_TYPE_P (TREE_TYPE (e2_p->value)))
6616 {
6617 error_at (loc, "%<__builtin_complex%> operand "
6618 "not of real binary floating-point type");
6619 expr.value = error_mark_node;
6620 break;
6621 }
6622 if (TYPE_MAIN_VARIANT (TREE_TYPE (e1_p->value))
6623 != TYPE_MAIN_VARIANT (TREE_TYPE (e2_p->value)))
6624 {
6625 error_at (loc,
6626 "%<__builtin_complex%> operands of different types");
6627 expr.value = error_mark_node;
6628 break;
6629 }
6630 if (!flag_isoc99)
6631 pedwarn (loc, OPT_pedantic,
6632 "ISO C90 does not support complex types");
6633 expr.value = build2 (COMPLEX_EXPR,
6634 build_complex_type
6635 (TYPE_MAIN_VARIANT
6636 (TREE_TYPE (e1_p->value))),
6637 e1_p->value, e2_p->value);
6638 break;
6639 }
6640 case RID_BUILTIN_SHUFFLE:
6641 {
6642 VEC(c_expr_t,gc) *cexpr_list;
6643
6644 c_parser_consume_token (parser);
6645 if (!c_parser_get_builtin_args (parser,
6646 "__builtin_shuffle",
6647 &cexpr_list))
6648 {
6649 expr.value = error_mark_node;
6650 break;
6651 }
6652
6653 if (VEC_length (c_expr_t, cexpr_list) == 2)
6654 expr.value =
6655 c_build_vec_perm_expr
6656 (loc, VEC_index (c_expr_t, cexpr_list, 0)->value,
6657 NULL_TREE, VEC_index (c_expr_t, cexpr_list, 1)->value);
6658
6659 else if (VEC_length (c_expr_t, cexpr_list) == 3)
6660 expr.value =
6661 c_build_vec_perm_expr
6662 (loc, VEC_index (c_expr_t, cexpr_list, 0)->value,
6663 VEC_index (c_expr_t, cexpr_list, 1)->value,
6664 VEC_index (c_expr_t, cexpr_list, 2)->value);
6665 else
6666 {
6667 error_at (loc, "wrong number of arguments to "
6668 "%<__builtin_shuffle%>");
6669 expr.value = error_mark_node;
6670 }
6671 break;
6672 }
6673 case RID_AT_SELECTOR:
6674 gcc_assert (c_dialect_objc ());
6675 c_parser_consume_token (parser);
6676 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6677 {
6678 expr.value = error_mark_node;
6679 break;
6680 }
6681 {
6682 tree sel = c_parser_objc_selector_arg (parser);
6683 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6684 "expected %<)%>");
6685 expr.value = objc_build_selector_expr (loc, sel);
6686 }
6687 break;
6688 case RID_AT_PROTOCOL:
6689 gcc_assert (c_dialect_objc ());
6690 c_parser_consume_token (parser);
6691 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6692 {
6693 expr.value = error_mark_node;
6694 break;
6695 }
6696 if (c_parser_next_token_is_not (parser, CPP_NAME))
6697 {
6698 c_parser_error (parser, "expected identifier");
6699 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6700 expr.value = error_mark_node;
6701 break;
6702 }
6703 {
6704 tree id = c_parser_peek_token (parser)->value;
6705 c_parser_consume_token (parser);
6706 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6707 "expected %<)%>");
6708 expr.value = objc_build_protocol_expr (id);
6709 }
6710 break;
6711 case RID_AT_ENCODE:
6712 /* Extension to support C-structures in the archiver. */
6713 gcc_assert (c_dialect_objc ());
6714 c_parser_consume_token (parser);
6715 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6716 {
6717 expr.value = error_mark_node;
6718 break;
6719 }
6720 t1 = c_parser_type_name (parser);
6721 if (t1 == NULL)
6722 {
6723 expr.value = error_mark_node;
6724 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6725 break;
6726 }
6727 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6728 "expected %<)%>");
6729 {
6730 tree type = groktypename (t1, NULL, NULL);
6731 expr.value = objc_build_encode_expr (type);
6732 }
6733 break;
6734 default:
6735 c_parser_error (parser, "expected expression");
6736 expr.value = error_mark_node;
6737 break;
6738 }
6739 break;
6740 case CPP_OPEN_SQUARE:
6741 if (c_dialect_objc ())
6742 {
6743 tree receiver, args;
6744 c_parser_consume_token (parser);
6745 receiver = c_parser_objc_receiver (parser);
6746 args = c_parser_objc_message_args (parser);
6747 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
6748 "expected %<]%>");
6749 expr.value = objc_build_message_expr (receiver, args);
6750 break;
6751 }
6752 /* Else fall through to report error. */
6753 default:
6754 c_parser_error (parser, "expected expression");
6755 expr.value = error_mark_node;
6756 break;
6757 }
6758 return c_parser_postfix_expression_after_primary (parser, loc, expr);
6759 }
6760
6761 /* Parse a postfix expression after a parenthesized type name: the
6762 brace-enclosed initializer of a compound literal, possibly followed
6763 by some postfix operators. This is separate because it is not
6764 possible to tell until after the type name whether a cast
6765 expression has a cast or a compound literal, or whether the operand
6766 of sizeof is a parenthesized type name or starts with a compound
6767 literal. TYPE_LOC is the location where TYPE_NAME starts--the
6768 location of the first token after the parentheses around the type
6769 name. */
6770
6771 static struct c_expr
6772 c_parser_postfix_expression_after_paren_type (c_parser *parser,
6773 struct c_type_name *type_name,
6774 location_t type_loc)
6775 {
6776 tree type;
6777 struct c_expr init;
6778 bool non_const;
6779 struct c_expr expr;
6780 location_t start_loc;
6781 tree type_expr = NULL_TREE;
6782 bool type_expr_const = true;
6783 check_compound_literal_type (type_loc, type_name);
6784 start_init (NULL_TREE, NULL, 0);
6785 type = groktypename (type_name, &type_expr, &type_expr_const);
6786 start_loc = c_parser_peek_token (parser)->location;
6787 if (type != error_mark_node && C_TYPE_VARIABLE_SIZE (type))
6788 {
6789 error_at (type_loc, "compound literal has variable size");
6790 type = error_mark_node;
6791 }
6792 init = c_parser_braced_init (parser, type, false);
6793 finish_init ();
6794 maybe_warn_string_init (type, init);
6795
6796 if (type != error_mark_node
6797 && !ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (type))
6798 && current_function_decl)
6799 {
6800 error ("compound literal qualified by address-space qualifier");
6801 type = error_mark_node;
6802 }
6803
6804 if (!flag_isoc99)
6805 pedwarn (start_loc, OPT_pedantic, "ISO C90 forbids compound literals");
6806 non_const = ((init.value && TREE_CODE (init.value) == CONSTRUCTOR)
6807 ? CONSTRUCTOR_NON_CONST (init.value)
6808 : init.original_code == C_MAYBE_CONST_EXPR);
6809 non_const |= !type_expr_const;
6810 expr.value = build_compound_literal (start_loc, type, init.value, non_const);
6811 expr.original_code = ERROR_MARK;
6812 expr.original_type = NULL;
6813 if (type_expr)
6814 {
6815 if (TREE_CODE (expr.value) == C_MAYBE_CONST_EXPR)
6816 {
6817 gcc_assert (C_MAYBE_CONST_EXPR_PRE (expr.value) == NULL_TREE);
6818 C_MAYBE_CONST_EXPR_PRE (expr.value) = type_expr;
6819 }
6820 else
6821 {
6822 gcc_assert (!non_const);
6823 expr.value = build2 (C_MAYBE_CONST_EXPR, type,
6824 type_expr, expr.value);
6825 }
6826 }
6827 return c_parser_postfix_expression_after_primary (parser, start_loc, expr);
6828 }
6829
6830 /* Parse a postfix expression after the initial primary or compound
6831 literal; that is, parse a series of postfix operators.
6832
6833 EXPR_LOC is the location of the primary expression. */
6834
6835 static struct c_expr
6836 c_parser_postfix_expression_after_primary (c_parser *parser,
6837 location_t expr_loc,
6838 struct c_expr expr)
6839 {
6840 struct c_expr orig_expr;
6841 tree ident, idx;
6842 VEC(tree,gc) *exprlist;
6843 VEC(tree,gc) *origtypes;
6844 while (true)
6845 {
6846 location_t op_loc = c_parser_peek_token (parser)->location;
6847 switch (c_parser_peek_token (parser)->type)
6848 {
6849 case CPP_OPEN_SQUARE:
6850 /* Array reference. */
6851 c_parser_consume_token (parser);
6852 idx = c_parser_expression (parser).value;
6853 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
6854 "expected %<]%>");
6855 expr.value = build_array_ref (op_loc, expr.value, idx);
6856 expr.original_code = ERROR_MARK;
6857 expr.original_type = NULL;
6858 break;
6859 case CPP_OPEN_PAREN:
6860 /* Function call. */
6861 c_parser_consume_token (parser);
6862 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
6863 exprlist = NULL;
6864 else
6865 exprlist = c_parser_expr_list (parser, true, false, &origtypes);
6866 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6867 "expected %<)%>");
6868 orig_expr = expr;
6869 mark_exp_read (expr.value);
6870 /* FIXME diagnostics: Ideally we want the FUNCNAME, not the
6871 "(" after the FUNCNAME, which is what we have now. */
6872 expr.value = build_function_call_vec (op_loc, expr.value, exprlist,
6873 origtypes);
6874 expr.original_code = ERROR_MARK;
6875 if (TREE_CODE (expr.value) == INTEGER_CST
6876 && TREE_CODE (orig_expr.value) == FUNCTION_DECL
6877 && DECL_BUILT_IN_CLASS (orig_expr.value) == BUILT_IN_NORMAL
6878 && DECL_FUNCTION_CODE (orig_expr.value) == BUILT_IN_CONSTANT_P)
6879 expr.original_code = C_MAYBE_CONST_EXPR;
6880 expr.original_type = NULL;
6881 if (exprlist != NULL)
6882 {
6883 release_tree_vector (exprlist);
6884 release_tree_vector (origtypes);
6885 }
6886 break;
6887 case CPP_DOT:
6888 /* Structure element reference. */
6889 c_parser_consume_token (parser);
6890 expr = default_function_array_conversion (expr_loc, expr);
6891 if (c_parser_next_token_is (parser, CPP_NAME))
6892 ident = c_parser_peek_token (parser)->value;
6893 else
6894 {
6895 c_parser_error (parser, "expected identifier");
6896 expr.value = error_mark_node;
6897 expr.original_code = ERROR_MARK;
6898 expr.original_type = NULL;
6899 return expr;
6900 }
6901 c_parser_consume_token (parser);
6902 expr.value = build_component_ref (op_loc, expr.value, ident);
6903 expr.original_code = ERROR_MARK;
6904 if (TREE_CODE (expr.value) != COMPONENT_REF)
6905 expr.original_type = NULL;
6906 else
6907 {
6908 /* Remember the original type of a bitfield. */
6909 tree field = TREE_OPERAND (expr.value, 1);
6910 if (TREE_CODE (field) != FIELD_DECL)
6911 expr.original_type = NULL;
6912 else
6913 expr.original_type = DECL_BIT_FIELD_TYPE (field);
6914 }
6915 break;
6916 case CPP_DEREF:
6917 /* Structure element reference. */
6918 c_parser_consume_token (parser);
6919 expr = default_function_array_conversion (expr_loc, expr);
6920 if (c_parser_next_token_is (parser, CPP_NAME))
6921 ident = c_parser_peek_token (parser)->value;
6922 else
6923 {
6924 c_parser_error (parser, "expected identifier");
6925 expr.value = error_mark_node;
6926 expr.original_code = ERROR_MARK;
6927 expr.original_type = NULL;
6928 return expr;
6929 }
6930 c_parser_consume_token (parser);
6931 expr.value = build_component_ref (op_loc,
6932 build_indirect_ref (op_loc,
6933 expr.value,
6934 RO_ARROW),
6935 ident);
6936 expr.original_code = ERROR_MARK;
6937 if (TREE_CODE (expr.value) != COMPONENT_REF)
6938 expr.original_type = NULL;
6939 else
6940 {
6941 /* Remember the original type of a bitfield. */
6942 tree field = TREE_OPERAND (expr.value, 1);
6943 if (TREE_CODE (field) != FIELD_DECL)
6944 expr.original_type = NULL;
6945 else
6946 expr.original_type = DECL_BIT_FIELD_TYPE (field);
6947 }
6948 break;
6949 case CPP_PLUS_PLUS:
6950 /* Postincrement. */
6951 c_parser_consume_token (parser);
6952 expr = default_function_array_read_conversion (expr_loc, expr);
6953 expr.value = build_unary_op (op_loc,
6954 POSTINCREMENT_EXPR, expr.value, 0);
6955 expr.original_code = ERROR_MARK;
6956 expr.original_type = NULL;
6957 break;
6958 case CPP_MINUS_MINUS:
6959 /* Postdecrement. */
6960 c_parser_consume_token (parser);
6961 expr = default_function_array_read_conversion (expr_loc, expr);
6962 expr.value = build_unary_op (op_loc,
6963 POSTDECREMENT_EXPR, expr.value, 0);
6964 expr.original_code = ERROR_MARK;
6965 expr.original_type = NULL;
6966 break;
6967 default:
6968 return expr;
6969 }
6970 }
6971 }
6972
6973 /* Parse an expression (C90 6.3.17, C99 6.5.17).
6974
6975 expression:
6976 assignment-expression
6977 expression , assignment-expression
6978 */
6979
6980 static struct c_expr
6981 c_parser_expression (c_parser *parser)
6982 {
6983 struct c_expr expr;
6984 expr = c_parser_expr_no_commas (parser, NULL);
6985 while (c_parser_next_token_is (parser, CPP_COMMA))
6986 {
6987 struct c_expr next;
6988 tree lhsval;
6989 location_t loc = c_parser_peek_token (parser)->location;
6990 location_t expr_loc;
6991 c_parser_consume_token (parser);
6992 expr_loc = c_parser_peek_token (parser)->location;
6993 lhsval = expr.value;
6994 while (TREE_CODE (lhsval) == COMPOUND_EXPR)
6995 lhsval = TREE_OPERAND (lhsval, 1);
6996 if (DECL_P (lhsval) || handled_component_p (lhsval))
6997 mark_exp_read (lhsval);
6998 next = c_parser_expr_no_commas (parser, NULL);
6999 next = default_function_array_conversion (expr_loc, next);
7000 expr.value = build_compound_expr (loc, expr.value, next.value);
7001 expr.original_code = COMPOUND_EXPR;
7002 expr.original_type = next.original_type;
7003 }
7004 return expr;
7005 }
7006
7007 /* Parse an expression and convert functions or arrays to
7008 pointers. */
7009
7010 static struct c_expr
7011 c_parser_expression_conv (c_parser *parser)
7012 {
7013 struct c_expr expr;
7014 location_t loc = c_parser_peek_token (parser)->location;
7015 expr = c_parser_expression (parser);
7016 expr = default_function_array_conversion (loc, expr);
7017 return expr;
7018 }
7019
7020 /* Parse a non-empty list of expressions. If CONVERT_P, convert
7021 functions and arrays to pointers. If FOLD_P, fold the expressions.
7022
7023 nonempty-expr-list:
7024 assignment-expression
7025 nonempty-expr-list , assignment-expression
7026 */
7027
7028 static VEC(tree,gc) *
7029 c_parser_expr_list (c_parser *parser, bool convert_p, bool fold_p,
7030 VEC(tree,gc) **p_orig_types)
7031 {
7032 VEC(tree,gc) *ret;
7033 VEC(tree,gc) *orig_types;
7034 struct c_expr expr;
7035 location_t loc = c_parser_peek_token (parser)->location;
7036
7037 ret = make_tree_vector ();
7038 if (p_orig_types == NULL)
7039 orig_types = NULL;
7040 else
7041 orig_types = make_tree_vector ();
7042
7043 expr = c_parser_expr_no_commas (parser, NULL);
7044 if (convert_p)
7045 expr = default_function_array_read_conversion (loc, expr);
7046 if (fold_p)
7047 expr.value = c_fully_fold (expr.value, false, NULL);
7048 VEC_quick_push (tree, ret, expr.value);
7049 if (orig_types != NULL)
7050 VEC_quick_push (tree, orig_types, expr.original_type);
7051 while (c_parser_next_token_is (parser, CPP_COMMA))
7052 {
7053 c_parser_consume_token (parser);
7054 loc = c_parser_peek_token (parser)->location;
7055 expr = c_parser_expr_no_commas (parser, NULL);
7056 if (convert_p)
7057 expr = default_function_array_read_conversion (loc, expr);
7058 if (fold_p)
7059 expr.value = c_fully_fold (expr.value, false, NULL);
7060 VEC_safe_push (tree, gc, ret, expr.value);
7061 if (orig_types != NULL)
7062 VEC_safe_push (tree, gc, orig_types, expr.original_type);
7063 }
7064 if (orig_types != NULL)
7065 *p_orig_types = orig_types;
7066 return ret;
7067 }
7068 \f
7069 /* Parse Objective-C-specific constructs. */
7070
7071 /* Parse an objc-class-definition.
7072
7073 objc-class-definition:
7074 @interface identifier objc-superclass[opt] objc-protocol-refs[opt]
7075 objc-class-instance-variables[opt] objc-methodprotolist @end
7076 @implementation identifier objc-superclass[opt]
7077 objc-class-instance-variables[opt]
7078 @interface identifier ( identifier ) objc-protocol-refs[opt]
7079 objc-methodprotolist @end
7080 @interface identifier ( ) objc-protocol-refs[opt]
7081 objc-methodprotolist @end
7082 @implementation identifier ( identifier )
7083
7084 objc-superclass:
7085 : identifier
7086
7087 "@interface identifier (" must start "@interface identifier (
7088 identifier ) ...": objc-methodprotolist in the first production may
7089 not start with a parenthesized identifier as a declarator of a data
7090 definition with no declaration specifiers if the objc-superclass,
7091 objc-protocol-refs and objc-class-instance-variables are omitted. */
7092
7093 static void
7094 c_parser_objc_class_definition (c_parser *parser, tree attributes)
7095 {
7096 bool iface_p;
7097 tree id1;
7098 tree superclass;
7099 if (c_parser_next_token_is_keyword (parser, RID_AT_INTERFACE))
7100 iface_p = true;
7101 else if (c_parser_next_token_is_keyword (parser, RID_AT_IMPLEMENTATION))
7102 iface_p = false;
7103 else
7104 gcc_unreachable ();
7105
7106 c_parser_consume_token (parser);
7107 if (c_parser_next_token_is_not (parser, CPP_NAME))
7108 {
7109 c_parser_error (parser, "expected identifier");
7110 return;
7111 }
7112 id1 = c_parser_peek_token (parser)->value;
7113 c_parser_consume_token (parser);
7114 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
7115 {
7116 /* We have a category or class extension. */
7117 tree id2;
7118 tree proto = NULL_TREE;
7119 c_parser_consume_token (parser);
7120 if (c_parser_next_token_is_not (parser, CPP_NAME))
7121 {
7122 if (iface_p && c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
7123 {
7124 /* We have a class extension. */
7125 id2 = NULL_TREE;
7126 }
7127 else
7128 {
7129 c_parser_error (parser, "expected identifier or %<)%>");
7130 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7131 return;
7132 }
7133 }
7134 else
7135 {
7136 id2 = c_parser_peek_token (parser)->value;
7137 c_parser_consume_token (parser);
7138 }
7139 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
7140 if (!iface_p)
7141 {
7142 objc_start_category_implementation (id1, id2);
7143 return;
7144 }
7145 if (c_parser_next_token_is (parser, CPP_LESS))
7146 proto = c_parser_objc_protocol_refs (parser);
7147 objc_start_category_interface (id1, id2, proto, attributes);
7148 c_parser_objc_methodprotolist (parser);
7149 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
7150 objc_finish_interface ();
7151 return;
7152 }
7153 if (c_parser_next_token_is (parser, CPP_COLON))
7154 {
7155 c_parser_consume_token (parser);
7156 if (c_parser_next_token_is_not (parser, CPP_NAME))
7157 {
7158 c_parser_error (parser, "expected identifier");
7159 return;
7160 }
7161 superclass = c_parser_peek_token (parser)->value;
7162 c_parser_consume_token (parser);
7163 }
7164 else
7165 superclass = NULL_TREE;
7166 if (iface_p)
7167 {
7168 tree proto = NULL_TREE;
7169 if (c_parser_next_token_is (parser, CPP_LESS))
7170 proto = c_parser_objc_protocol_refs (parser);
7171 objc_start_class_interface (id1, superclass, proto, attributes);
7172 }
7173 else
7174 objc_start_class_implementation (id1, superclass);
7175 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
7176 c_parser_objc_class_instance_variables (parser);
7177 if (iface_p)
7178 {
7179 objc_continue_interface ();
7180 c_parser_objc_methodprotolist (parser);
7181 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
7182 objc_finish_interface ();
7183 }
7184 else
7185 {
7186 objc_continue_implementation ();
7187 return;
7188 }
7189 }
7190
7191 /* Parse objc-class-instance-variables.
7192
7193 objc-class-instance-variables:
7194 { objc-instance-variable-decl-list[opt] }
7195
7196 objc-instance-variable-decl-list:
7197 objc-visibility-spec
7198 objc-instance-variable-decl ;
7199 ;
7200 objc-instance-variable-decl-list objc-visibility-spec
7201 objc-instance-variable-decl-list objc-instance-variable-decl ;
7202 objc-instance-variable-decl-list ;
7203
7204 objc-visibility-spec:
7205 @private
7206 @protected
7207 @public
7208
7209 objc-instance-variable-decl:
7210 struct-declaration
7211 */
7212
7213 static void
7214 c_parser_objc_class_instance_variables (c_parser *parser)
7215 {
7216 gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
7217 c_parser_consume_token (parser);
7218 while (c_parser_next_token_is_not (parser, CPP_EOF))
7219 {
7220 tree decls;
7221 /* Parse any stray semicolon. */
7222 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
7223 {
7224 pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic,
7225 "extra semicolon");
7226 c_parser_consume_token (parser);
7227 continue;
7228 }
7229 /* Stop if at the end of the instance variables. */
7230 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
7231 {
7232 c_parser_consume_token (parser);
7233 break;
7234 }
7235 /* Parse any objc-visibility-spec. */
7236 if (c_parser_next_token_is_keyword (parser, RID_AT_PRIVATE))
7237 {
7238 c_parser_consume_token (parser);
7239 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
7240 continue;
7241 }
7242 else if (c_parser_next_token_is_keyword (parser, RID_AT_PROTECTED))
7243 {
7244 c_parser_consume_token (parser);
7245 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
7246 continue;
7247 }
7248 else if (c_parser_next_token_is_keyword (parser, RID_AT_PUBLIC))
7249 {
7250 c_parser_consume_token (parser);
7251 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
7252 continue;
7253 }
7254 else if (c_parser_next_token_is_keyword (parser, RID_AT_PACKAGE))
7255 {
7256 c_parser_consume_token (parser);
7257 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
7258 continue;
7259 }
7260 else if (c_parser_next_token_is (parser, CPP_PRAGMA))
7261 {
7262 c_parser_pragma (parser, pragma_external);
7263 continue;
7264 }
7265
7266 /* Parse some comma-separated declarations. */
7267 decls = c_parser_struct_declaration (parser);
7268 if (decls == NULL)
7269 {
7270 /* There is a syntax error. We want to skip the offending
7271 tokens up to the next ';' (included) or '}'
7272 (excluded). */
7273
7274 /* First, skip manually a ')' or ']'. This is because they
7275 reduce the nesting level, so c_parser_skip_until_found()
7276 wouldn't be able to skip past them. */
7277 c_token *token = c_parser_peek_token (parser);
7278 if (token->type == CPP_CLOSE_PAREN || token->type == CPP_CLOSE_SQUARE)
7279 c_parser_consume_token (parser);
7280
7281 /* Then, do the standard skipping. */
7282 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
7283
7284 /* We hopefully recovered. Start normal parsing again. */
7285 parser->error = false;
7286 continue;
7287 }
7288 else
7289 {
7290 /* Comma-separated instance variables are chained together
7291 in reverse order; add them one by one. */
7292 tree ivar = nreverse (decls);
7293 for (; ivar; ivar = DECL_CHAIN (ivar))
7294 objc_add_instance_variable (copy_node (ivar));
7295 }
7296 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
7297 }
7298 }
7299
7300 /* Parse an objc-class-declaration.
7301
7302 objc-class-declaration:
7303 @class identifier-list ;
7304 */
7305
7306 static void
7307 c_parser_objc_class_declaration (c_parser *parser)
7308 {
7309 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_CLASS));
7310 c_parser_consume_token (parser);
7311 /* Any identifiers, including those declared as type names, are OK
7312 here. */
7313 while (true)
7314 {
7315 tree id;
7316 if (c_parser_next_token_is_not (parser, CPP_NAME))
7317 {
7318 c_parser_error (parser, "expected identifier");
7319 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
7320 parser->error = false;
7321 return;
7322 }
7323 id = c_parser_peek_token (parser)->value;
7324 objc_declare_class (id);
7325 c_parser_consume_token (parser);
7326 if (c_parser_next_token_is (parser, CPP_COMMA))
7327 c_parser_consume_token (parser);
7328 else
7329 break;
7330 }
7331 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
7332 }
7333
7334 /* Parse an objc-alias-declaration.
7335
7336 objc-alias-declaration:
7337 @compatibility_alias identifier identifier ;
7338 */
7339
7340 static void
7341 c_parser_objc_alias_declaration (c_parser *parser)
7342 {
7343 tree id1, id2;
7344 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_ALIAS));
7345 c_parser_consume_token (parser);
7346 if (c_parser_next_token_is_not (parser, CPP_NAME))
7347 {
7348 c_parser_error (parser, "expected identifier");
7349 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
7350 return;
7351 }
7352 id1 = c_parser_peek_token (parser)->value;
7353 c_parser_consume_token (parser);
7354 if (c_parser_next_token_is_not (parser, CPP_NAME))
7355 {
7356 c_parser_error (parser, "expected identifier");
7357 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
7358 return;
7359 }
7360 id2 = c_parser_peek_token (parser)->value;
7361 c_parser_consume_token (parser);
7362 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
7363 objc_declare_alias (id1, id2);
7364 }
7365
7366 /* Parse an objc-protocol-definition.
7367
7368 objc-protocol-definition:
7369 @protocol identifier objc-protocol-refs[opt] objc-methodprotolist @end
7370 @protocol identifier-list ;
7371
7372 "@protocol identifier ;" should be resolved as "@protocol
7373 identifier-list ;": objc-methodprotolist may not start with a
7374 semicolon in the first alternative if objc-protocol-refs are
7375 omitted. */
7376
7377 static void
7378 c_parser_objc_protocol_definition (c_parser *parser, tree attributes)
7379 {
7380 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_PROTOCOL));
7381
7382 c_parser_consume_token (parser);
7383 if (c_parser_next_token_is_not (parser, CPP_NAME))
7384 {
7385 c_parser_error (parser, "expected identifier");
7386 return;
7387 }
7388 if (c_parser_peek_2nd_token (parser)->type == CPP_COMMA
7389 || c_parser_peek_2nd_token (parser)->type == CPP_SEMICOLON)
7390 {
7391 /* Any identifiers, including those declared as type names, are
7392 OK here. */
7393 while (true)
7394 {
7395 tree id;
7396 if (c_parser_next_token_is_not (parser, CPP_NAME))
7397 {
7398 c_parser_error (parser, "expected identifier");
7399 break;
7400 }
7401 id = c_parser_peek_token (parser)->value;
7402 objc_declare_protocol (id, attributes);
7403 c_parser_consume_token (parser);
7404 if (c_parser_next_token_is (parser, CPP_COMMA))
7405 c_parser_consume_token (parser);
7406 else
7407 break;
7408 }
7409 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
7410 }
7411 else
7412 {
7413 tree id = c_parser_peek_token (parser)->value;
7414 tree proto = NULL_TREE;
7415 c_parser_consume_token (parser);
7416 if (c_parser_next_token_is (parser, CPP_LESS))
7417 proto = c_parser_objc_protocol_refs (parser);
7418 parser->objc_pq_context = true;
7419 objc_start_protocol (id, proto, attributes);
7420 c_parser_objc_methodprotolist (parser);
7421 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
7422 parser->objc_pq_context = false;
7423 objc_finish_interface ();
7424 }
7425 }
7426
7427 /* Parse an objc-method-type.
7428
7429 objc-method-type:
7430 +
7431 -
7432
7433 Return true if it is a class method (+) and false if it is
7434 an instance method (-).
7435 */
7436 static inline bool
7437 c_parser_objc_method_type (c_parser *parser)
7438 {
7439 switch (c_parser_peek_token (parser)->type)
7440 {
7441 case CPP_PLUS:
7442 c_parser_consume_token (parser);
7443 return true;
7444 case CPP_MINUS:
7445 c_parser_consume_token (parser);
7446 return false;
7447 default:
7448 gcc_unreachable ();
7449 }
7450 }
7451
7452 /* Parse an objc-method-definition.
7453
7454 objc-method-definition:
7455 objc-method-type objc-method-decl ;[opt] compound-statement
7456 */
7457
7458 static void
7459 c_parser_objc_method_definition (c_parser *parser)
7460 {
7461 bool is_class_method = c_parser_objc_method_type (parser);
7462 tree decl, attributes = NULL_TREE, expr = NULL_TREE;
7463 parser->objc_pq_context = true;
7464 decl = c_parser_objc_method_decl (parser, is_class_method, &attributes,
7465 &expr);
7466 if (decl == error_mark_node)
7467 return; /* Bail here. */
7468
7469 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
7470 {
7471 c_parser_consume_token (parser);
7472 pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic,
7473 "extra semicolon in method definition specified");
7474 }
7475
7476 if (!c_parser_next_token_is (parser, CPP_OPEN_BRACE))
7477 {
7478 c_parser_error (parser, "expected %<{%>");
7479 return;
7480 }
7481
7482 parser->objc_pq_context = false;
7483 if (objc_start_method_definition (is_class_method, decl, attributes, expr))
7484 {
7485 add_stmt (c_parser_compound_statement (parser));
7486 objc_finish_method_definition (current_function_decl);
7487 }
7488 else
7489 {
7490 /* This code is executed when we find a method definition
7491 outside of an @implementation context (or invalid for other
7492 reasons). Parse the method (to keep going) but do not emit
7493 any code.
7494 */
7495 c_parser_compound_statement (parser);
7496 }
7497 }
7498
7499 /* Parse an objc-methodprotolist.
7500
7501 objc-methodprotolist:
7502 empty
7503 objc-methodprotolist objc-methodproto
7504 objc-methodprotolist declaration
7505 objc-methodprotolist ;
7506 @optional
7507 @required
7508
7509 The declaration is a data definition, which may be missing
7510 declaration specifiers under the same rules and diagnostics as
7511 other data definitions outside functions, and the stray semicolon
7512 is diagnosed the same way as a stray semicolon outside a
7513 function. */
7514
7515 static void
7516 c_parser_objc_methodprotolist (c_parser *parser)
7517 {
7518 while (true)
7519 {
7520 /* The list is terminated by @end. */
7521 switch (c_parser_peek_token (parser)->type)
7522 {
7523 case CPP_SEMICOLON:
7524 pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic,
7525 "ISO C does not allow extra %<;%> outside of a function");
7526 c_parser_consume_token (parser);
7527 break;
7528 case CPP_PLUS:
7529 case CPP_MINUS:
7530 c_parser_objc_methodproto (parser);
7531 break;
7532 case CPP_PRAGMA:
7533 c_parser_pragma (parser, pragma_external);
7534 break;
7535 case CPP_EOF:
7536 return;
7537 default:
7538 if (c_parser_next_token_is_keyword (parser, RID_AT_END))
7539 return;
7540 else if (c_parser_next_token_is_keyword (parser, RID_AT_PROPERTY))
7541 c_parser_objc_at_property_declaration (parser);
7542 else if (c_parser_next_token_is_keyword (parser, RID_AT_OPTIONAL))
7543 {
7544 objc_set_method_opt (true);
7545 c_parser_consume_token (parser);
7546 }
7547 else if (c_parser_next_token_is_keyword (parser, RID_AT_REQUIRED))
7548 {
7549 objc_set_method_opt (false);
7550 c_parser_consume_token (parser);
7551 }
7552 else
7553 c_parser_declaration_or_fndef (parser, false, false, true,
7554 false, true, NULL);
7555 break;
7556 }
7557 }
7558 }
7559
7560 /* Parse an objc-methodproto.
7561
7562 objc-methodproto:
7563 objc-method-type objc-method-decl ;
7564 */
7565
7566 static void
7567 c_parser_objc_methodproto (c_parser *parser)
7568 {
7569 bool is_class_method = c_parser_objc_method_type (parser);
7570 tree decl, attributes = NULL_TREE;
7571
7572 /* Remember protocol qualifiers in prototypes. */
7573 parser->objc_pq_context = true;
7574 decl = c_parser_objc_method_decl (parser, is_class_method, &attributes,
7575 NULL);
7576 /* Forget protocol qualifiers now. */
7577 parser->objc_pq_context = false;
7578
7579 /* Do not allow the presence of attributes to hide an erroneous
7580 method implementation in the interface section. */
7581 if (!c_parser_next_token_is (parser, CPP_SEMICOLON))
7582 {
7583 c_parser_error (parser, "expected %<;%>");
7584 return;
7585 }
7586
7587 if (decl != error_mark_node)
7588 objc_add_method_declaration (is_class_method, decl, attributes);
7589
7590 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
7591 }
7592
7593 /* If we are at a position that method attributes may be present, check that
7594 there are not any parsed already (a syntax error) and then collect any
7595 specified at the current location. Finally, if new attributes were present,
7596 check that the next token is legal ( ';' for decls and '{' for defs). */
7597
7598 static bool
7599 c_parser_objc_maybe_method_attributes (c_parser* parser, tree* attributes)
7600 {
7601 bool bad = false;
7602 if (*attributes)
7603 {
7604 c_parser_error (parser,
7605 "method attributes must be specified at the end only");
7606 *attributes = NULL_TREE;
7607 bad = true;
7608 }
7609
7610 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
7611 *attributes = c_parser_attributes (parser);
7612
7613 /* If there were no attributes here, just report any earlier error. */
7614 if (*attributes == NULL_TREE || bad)
7615 return bad;
7616
7617 /* If the attributes are followed by a ; or {, then just report any earlier
7618 error. */
7619 if (c_parser_next_token_is (parser, CPP_SEMICOLON)
7620 || c_parser_next_token_is (parser, CPP_OPEN_BRACE))
7621 return bad;
7622
7623 /* We've got attributes, but not at the end. */
7624 c_parser_error (parser,
7625 "expected %<;%> or %<{%> after method attribute definition");
7626 return true;
7627 }
7628
7629 /* Parse an objc-method-decl.
7630
7631 objc-method-decl:
7632 ( objc-type-name ) objc-selector
7633 objc-selector
7634 ( objc-type-name ) objc-keyword-selector objc-optparmlist
7635 objc-keyword-selector objc-optparmlist
7636 attributes
7637
7638 objc-keyword-selector:
7639 objc-keyword-decl
7640 objc-keyword-selector objc-keyword-decl
7641
7642 objc-keyword-decl:
7643 objc-selector : ( objc-type-name ) identifier
7644 objc-selector : identifier
7645 : ( objc-type-name ) identifier
7646 : identifier
7647
7648 objc-optparmlist:
7649 objc-optparms objc-optellipsis
7650
7651 objc-optparms:
7652 empty
7653 objc-opt-parms , parameter-declaration
7654
7655 objc-optellipsis:
7656 empty
7657 , ...
7658 */
7659
7660 static tree
7661 c_parser_objc_method_decl (c_parser *parser, bool is_class_method,
7662 tree *attributes, tree *expr)
7663 {
7664 tree type = NULL_TREE;
7665 tree sel;
7666 tree parms = NULL_TREE;
7667 bool ellipsis = false;
7668 bool attr_err = false;
7669
7670 *attributes = NULL_TREE;
7671 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
7672 {
7673 c_parser_consume_token (parser);
7674 type = c_parser_objc_type_name (parser);
7675 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
7676 }
7677 sel = c_parser_objc_selector (parser);
7678 /* If there is no selector, or a colon follows, we have an
7679 objc-keyword-selector. If there is a selector, and a colon does
7680 not follow, that selector ends the objc-method-decl. */
7681 if (!sel || c_parser_next_token_is (parser, CPP_COLON))
7682 {
7683 tree tsel = sel;
7684 tree list = NULL_TREE;
7685 while (true)
7686 {
7687 tree atype = NULL_TREE, id, keyworddecl;
7688 tree param_attr = NULL_TREE;
7689 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
7690 break;
7691 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
7692 {
7693 c_parser_consume_token (parser);
7694 atype = c_parser_objc_type_name (parser);
7695 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7696 "expected %<)%>");
7697 }
7698 /* New ObjC allows attributes on method parameters. */
7699 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
7700 param_attr = c_parser_attributes (parser);
7701 if (c_parser_next_token_is_not (parser, CPP_NAME))
7702 {
7703 c_parser_error (parser, "expected identifier");
7704 return error_mark_node;
7705 }
7706 id = c_parser_peek_token (parser)->value;
7707 c_parser_consume_token (parser);
7708 keyworddecl = objc_build_keyword_decl (tsel, atype, id, param_attr);
7709 list = chainon (list, keyworddecl);
7710 tsel = c_parser_objc_selector (parser);
7711 if (!tsel && c_parser_next_token_is_not (parser, CPP_COLON))
7712 break;
7713 }
7714
7715 attr_err |= c_parser_objc_maybe_method_attributes (parser, attributes) ;
7716
7717 /* Parse the optional parameter list. Optional Objective-C
7718 method parameters follow the C syntax, and may include '...'
7719 to denote a variable number of arguments. */
7720 parms = make_node (TREE_LIST);
7721 while (c_parser_next_token_is (parser, CPP_COMMA))
7722 {
7723 struct c_parm *parm;
7724 c_parser_consume_token (parser);
7725 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
7726 {
7727 ellipsis = true;
7728 c_parser_consume_token (parser);
7729 attr_err |= c_parser_objc_maybe_method_attributes
7730 (parser, attributes) ;
7731 break;
7732 }
7733 parm = c_parser_parameter_declaration (parser, NULL_TREE);
7734 if (parm == NULL)
7735 break;
7736 parms = chainon (parms,
7737 build_tree_list (NULL_TREE, grokparm (parm, expr)));
7738 }
7739 sel = list;
7740 }
7741 else
7742 attr_err |= c_parser_objc_maybe_method_attributes (parser, attributes) ;
7743
7744 if (sel == NULL)
7745 {
7746 c_parser_error (parser, "objective-c method declaration is expected");
7747 return error_mark_node;
7748 }
7749
7750 if (attr_err)
7751 return error_mark_node;
7752
7753 return objc_build_method_signature (is_class_method, type, sel, parms, ellipsis);
7754 }
7755
7756 /* Parse an objc-type-name.
7757
7758 objc-type-name:
7759 objc-type-qualifiers[opt] type-name
7760 objc-type-qualifiers[opt]
7761
7762 objc-type-qualifiers:
7763 objc-type-qualifier
7764 objc-type-qualifiers objc-type-qualifier
7765
7766 objc-type-qualifier: one of
7767 in out inout bycopy byref oneway
7768 */
7769
7770 static tree
7771 c_parser_objc_type_name (c_parser *parser)
7772 {
7773 tree quals = NULL_TREE;
7774 struct c_type_name *type_name = NULL;
7775 tree type = NULL_TREE;
7776 while (true)
7777 {
7778 c_token *token = c_parser_peek_token (parser);
7779 if (token->type == CPP_KEYWORD
7780 && (token->keyword == RID_IN
7781 || token->keyword == RID_OUT
7782 || token->keyword == RID_INOUT
7783 || token->keyword == RID_BYCOPY
7784 || token->keyword == RID_BYREF
7785 || token->keyword == RID_ONEWAY))
7786 {
7787 quals = chainon (build_tree_list (NULL_TREE, token->value), quals);
7788 c_parser_consume_token (parser);
7789 }
7790 else
7791 break;
7792 }
7793 if (c_parser_next_tokens_start_typename (parser, cla_prefer_type))
7794 type_name = c_parser_type_name (parser);
7795 if (type_name)
7796 type = groktypename (type_name, NULL, NULL);
7797
7798 /* If the type is unknown, and error has already been produced and
7799 we need to recover from the error. In that case, use NULL_TREE
7800 for the type, as if no type had been specified; this will use the
7801 default type ('id') which is good for error recovery. */
7802 if (type == error_mark_node)
7803 type = NULL_TREE;
7804
7805 return build_tree_list (quals, type);
7806 }
7807
7808 /* Parse objc-protocol-refs.
7809
7810 objc-protocol-refs:
7811 < identifier-list >
7812 */
7813
7814 static tree
7815 c_parser_objc_protocol_refs (c_parser *parser)
7816 {
7817 tree list = NULL_TREE;
7818 gcc_assert (c_parser_next_token_is (parser, CPP_LESS));
7819 c_parser_consume_token (parser);
7820 /* Any identifiers, including those declared as type names, are OK
7821 here. */
7822 while (true)
7823 {
7824 tree id;
7825 if (c_parser_next_token_is_not (parser, CPP_NAME))
7826 {
7827 c_parser_error (parser, "expected identifier");
7828 break;
7829 }
7830 id = c_parser_peek_token (parser)->value;
7831 list = chainon (list, build_tree_list (NULL_TREE, id));
7832 c_parser_consume_token (parser);
7833 if (c_parser_next_token_is (parser, CPP_COMMA))
7834 c_parser_consume_token (parser);
7835 else
7836 break;
7837 }
7838 c_parser_require (parser, CPP_GREATER, "expected %<>%>");
7839 return list;
7840 }
7841
7842 /* Parse an objc-try-catch-finally-statement.
7843
7844 objc-try-catch-finally-statement:
7845 @try compound-statement objc-catch-list[opt]
7846 @try compound-statement objc-catch-list[opt] @finally compound-statement
7847
7848 objc-catch-list:
7849 @catch ( objc-catch-parameter-declaration ) compound-statement
7850 objc-catch-list @catch ( objc-catch-parameter-declaration ) compound-statement
7851
7852 objc-catch-parameter-declaration:
7853 parameter-declaration
7854 '...'
7855
7856 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
7857
7858 PS: This function is identical to cp_parser_objc_try_catch_finally_statement
7859 for C++. Keep them in sync. */
7860
7861 static void
7862 c_parser_objc_try_catch_finally_statement (c_parser *parser)
7863 {
7864 location_t location;
7865 tree stmt;
7866
7867 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_TRY));
7868 c_parser_consume_token (parser);
7869 location = c_parser_peek_token (parser)->location;
7870 objc_maybe_warn_exceptions (location);
7871 stmt = c_parser_compound_statement (parser);
7872 objc_begin_try_stmt (location, stmt);
7873
7874 while (c_parser_next_token_is_keyword (parser, RID_AT_CATCH))
7875 {
7876 struct c_parm *parm;
7877 tree parameter_declaration = error_mark_node;
7878 bool seen_open_paren = false;
7879
7880 c_parser_consume_token (parser);
7881 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7882 seen_open_paren = true;
7883 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
7884 {
7885 /* We have "@catch (...)" (where the '...' are literally
7886 what is in the code). Skip the '...'.
7887 parameter_declaration is set to NULL_TREE, and
7888 objc_being_catch_clauses() knows that that means
7889 '...'. */
7890 c_parser_consume_token (parser);
7891 parameter_declaration = NULL_TREE;
7892 }
7893 else
7894 {
7895 /* We have "@catch (NSException *exception)" or something
7896 like that. Parse the parameter declaration. */
7897 parm = c_parser_parameter_declaration (parser, NULL_TREE);
7898 if (parm == NULL)
7899 parameter_declaration = error_mark_node;
7900 else
7901 parameter_declaration = grokparm (parm, NULL);
7902 }
7903 if (seen_open_paren)
7904 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
7905 else
7906 {
7907 /* If there was no open parenthesis, we are recovering from
7908 an error, and we are trying to figure out what mistake
7909 the user has made. */
7910
7911 /* If there is an immediate closing parenthesis, the user
7912 probably forgot the opening one (ie, they typed "@catch
7913 NSException *e)". Parse the closing parenthesis and keep
7914 going. */
7915 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
7916 c_parser_consume_token (parser);
7917
7918 /* If these is no immediate closing parenthesis, the user
7919 probably doesn't know that parenthesis are required at
7920 all (ie, they typed "@catch NSException *e"). So, just
7921 forget about the closing parenthesis and keep going. */
7922 }
7923 objc_begin_catch_clause (parameter_declaration);
7924 if (c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
7925 c_parser_compound_statement_nostart (parser);
7926 objc_finish_catch_clause ();
7927 }
7928 if (c_parser_next_token_is_keyword (parser, RID_AT_FINALLY))
7929 {
7930 c_parser_consume_token (parser);
7931 location = c_parser_peek_token (parser)->location;
7932 stmt = c_parser_compound_statement (parser);
7933 objc_build_finally_clause (location, stmt);
7934 }
7935 objc_finish_try_stmt ();
7936 }
7937
7938 /* Parse an objc-synchronized-statement.
7939
7940 objc-synchronized-statement:
7941 @synchronized ( expression ) compound-statement
7942 */
7943
7944 static void
7945 c_parser_objc_synchronized_statement (c_parser *parser)
7946 {
7947 location_t loc;
7948 tree expr, stmt;
7949 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_SYNCHRONIZED));
7950 c_parser_consume_token (parser);
7951 loc = c_parser_peek_token (parser)->location;
7952 objc_maybe_warn_exceptions (loc);
7953 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7954 {
7955 expr = c_parser_expression (parser).value;
7956 expr = c_fully_fold (expr, false, NULL);
7957 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
7958 }
7959 else
7960 expr = error_mark_node;
7961 stmt = c_parser_compound_statement (parser);
7962 objc_build_synchronized (loc, expr, stmt);
7963 }
7964
7965 /* Parse an objc-selector; return NULL_TREE without an error if the
7966 next token is not an objc-selector.
7967
7968 objc-selector:
7969 identifier
7970 one of
7971 enum struct union if else while do for switch case default
7972 break continue return goto asm sizeof typeof __alignof
7973 unsigned long const short volatile signed restrict _Complex
7974 in out inout bycopy byref oneway int char float double void _Bool
7975
7976 ??? Why this selection of keywords but not, for example, storage
7977 class specifiers? */
7978
7979 static tree
7980 c_parser_objc_selector (c_parser *parser)
7981 {
7982 c_token *token = c_parser_peek_token (parser);
7983 tree value = token->value;
7984 if (token->type == CPP_NAME)
7985 {
7986 c_parser_consume_token (parser);
7987 return value;
7988 }
7989 if (token->type != CPP_KEYWORD)
7990 return NULL_TREE;
7991 switch (token->keyword)
7992 {
7993 case RID_ENUM:
7994 case RID_STRUCT:
7995 case RID_UNION:
7996 case RID_IF:
7997 case RID_ELSE:
7998 case RID_WHILE:
7999 case RID_DO:
8000 case RID_FOR:
8001 case RID_SWITCH:
8002 case RID_CASE:
8003 case RID_DEFAULT:
8004 case RID_BREAK:
8005 case RID_CONTINUE:
8006 case RID_RETURN:
8007 case RID_GOTO:
8008 case RID_ASM:
8009 case RID_SIZEOF:
8010 case RID_TYPEOF:
8011 case RID_ALIGNOF:
8012 case RID_UNSIGNED:
8013 case RID_LONG:
8014 case RID_INT128:
8015 case RID_CONST:
8016 case RID_SHORT:
8017 case RID_VOLATILE:
8018 case RID_SIGNED:
8019 case RID_RESTRICT:
8020 case RID_COMPLEX:
8021 case RID_IN:
8022 case RID_OUT:
8023 case RID_INOUT:
8024 case RID_BYCOPY:
8025 case RID_BYREF:
8026 case RID_ONEWAY:
8027 case RID_INT:
8028 case RID_CHAR:
8029 case RID_FLOAT:
8030 case RID_DOUBLE:
8031 case RID_VOID:
8032 case RID_BOOL:
8033 c_parser_consume_token (parser);
8034 return value;
8035 default:
8036 return NULL_TREE;
8037 }
8038 }
8039
8040 /* Parse an objc-selector-arg.
8041
8042 objc-selector-arg:
8043 objc-selector
8044 objc-keywordname-list
8045
8046 objc-keywordname-list:
8047 objc-keywordname
8048 objc-keywordname-list objc-keywordname
8049
8050 objc-keywordname:
8051 objc-selector :
8052 :
8053 */
8054
8055 static tree
8056 c_parser_objc_selector_arg (c_parser *parser)
8057 {
8058 tree sel = c_parser_objc_selector (parser);
8059 tree list = NULL_TREE;
8060 if (sel && c_parser_next_token_is_not (parser, CPP_COLON))
8061 return sel;
8062 while (true)
8063 {
8064 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
8065 return list;
8066 list = chainon (list, build_tree_list (sel, NULL_TREE));
8067 sel = c_parser_objc_selector (parser);
8068 if (!sel && c_parser_next_token_is_not (parser, CPP_COLON))
8069 break;
8070 }
8071 return list;
8072 }
8073
8074 /* Parse an objc-receiver.
8075
8076 objc-receiver:
8077 expression
8078 class-name
8079 type-name
8080 */
8081
8082 static tree
8083 c_parser_objc_receiver (c_parser *parser)
8084 {
8085 if (c_parser_peek_token (parser)->type == CPP_NAME
8086 && (c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME
8087 || c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME))
8088 {
8089 tree id = c_parser_peek_token (parser)->value;
8090 c_parser_consume_token (parser);
8091 return objc_get_class_reference (id);
8092 }
8093 return c_fully_fold (c_parser_expression (parser).value, false, NULL);
8094 }
8095
8096 /* Parse objc-message-args.
8097
8098 objc-message-args:
8099 objc-selector
8100 objc-keywordarg-list
8101
8102 objc-keywordarg-list:
8103 objc-keywordarg
8104 objc-keywordarg-list objc-keywordarg
8105
8106 objc-keywordarg:
8107 objc-selector : objc-keywordexpr
8108 : objc-keywordexpr
8109 */
8110
8111 static tree
8112 c_parser_objc_message_args (c_parser *parser)
8113 {
8114 tree sel = c_parser_objc_selector (parser);
8115 tree list = NULL_TREE;
8116 if (sel && c_parser_next_token_is_not (parser, CPP_COLON))
8117 return sel;
8118 while (true)
8119 {
8120 tree keywordexpr;
8121 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
8122 return error_mark_node;
8123 keywordexpr = c_parser_objc_keywordexpr (parser);
8124 list = chainon (list, build_tree_list (sel, keywordexpr));
8125 sel = c_parser_objc_selector (parser);
8126 if (!sel && c_parser_next_token_is_not (parser, CPP_COLON))
8127 break;
8128 }
8129 return list;
8130 }
8131
8132 /* Parse an objc-keywordexpr.
8133
8134 objc-keywordexpr:
8135 nonempty-expr-list
8136 */
8137
8138 static tree
8139 c_parser_objc_keywordexpr (c_parser *parser)
8140 {
8141 tree ret;
8142 VEC(tree,gc) *expr_list = c_parser_expr_list (parser, true, true, NULL);
8143 if (VEC_length (tree, expr_list) == 1)
8144 {
8145 /* Just return the expression, remove a level of
8146 indirection. */
8147 ret = VEC_index (tree, expr_list, 0);
8148 }
8149 else
8150 {
8151 /* We have a comma expression, we will collapse later. */
8152 ret = build_tree_list_vec (expr_list);
8153 }
8154 release_tree_vector (expr_list);
8155 return ret;
8156 }
8157
8158 /* A check, needed in several places, that ObjC interface, implementation or
8159 method definitions are not prefixed by incorrect items. */
8160 static bool
8161 c_parser_objc_diagnose_bad_element_prefix (c_parser *parser,
8162 struct c_declspecs *specs)
8163 {
8164 if (!specs->declspecs_seen_p || specs->non_sc_seen_p
8165 || specs->typespec_kind != ctsk_none)
8166 {
8167 c_parser_error (parser,
8168 "no type or storage class may be specified here,");
8169 c_parser_skip_to_end_of_block_or_statement (parser);
8170 return true;
8171 }
8172 return false;
8173 }
8174
8175 /* Parse an Objective-C @property declaration. The syntax is:
8176
8177 objc-property-declaration:
8178 '@property' objc-property-attributes[opt] struct-declaration ;
8179
8180 objc-property-attributes:
8181 '(' objc-property-attribute-list ')'
8182
8183 objc-property-attribute-list:
8184 objc-property-attribute
8185 objc-property-attribute-list, objc-property-attribute
8186
8187 objc-property-attribute
8188 'getter' = identifier
8189 'setter' = identifier
8190 'readonly'
8191 'readwrite'
8192 'assign'
8193 'retain'
8194 'copy'
8195 'nonatomic'
8196
8197 For example:
8198 @property NSString *name;
8199 @property (readonly) id object;
8200 @property (retain, nonatomic, getter=getTheName) id name;
8201 @property int a, b, c;
8202
8203 PS: This function is identical to cp_parser_objc_at_propery_declaration
8204 for C++. Keep them in sync. */
8205 static void
8206 c_parser_objc_at_property_declaration (c_parser *parser)
8207 {
8208 /* The following variables hold the attributes of the properties as
8209 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
8210 seen. When we see an attribute, we set them to 'true' (if they
8211 are boolean properties) or to the identifier (if they have an
8212 argument, ie, for getter and setter). Note that here we only
8213 parse the list of attributes, check the syntax and accumulate the
8214 attributes that we find. objc_add_property_declaration() will
8215 then process the information. */
8216 bool property_assign = false;
8217 bool property_copy = false;
8218 tree property_getter_ident = NULL_TREE;
8219 bool property_nonatomic = false;
8220 bool property_readonly = false;
8221 bool property_readwrite = false;
8222 bool property_retain = false;
8223 tree property_setter_ident = NULL_TREE;
8224
8225 /* 'properties' is the list of properties that we read. Usually a
8226 single one, but maybe more (eg, in "@property int a, b, c;" there
8227 are three). */
8228 tree properties;
8229 location_t loc;
8230
8231 loc = c_parser_peek_token (parser)->location;
8232 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_PROPERTY));
8233
8234 c_parser_consume_token (parser); /* Eat '@property'. */
8235
8236 /* Parse the optional attribute list... */
8237 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
8238 {
8239 /* Eat the '(' */
8240 c_parser_consume_token (parser);
8241
8242 /* Property attribute keywords are valid now. */
8243 parser->objc_property_attr_context = true;
8244
8245 while (true)
8246 {
8247 bool syntax_error = false;
8248 c_token *token = c_parser_peek_token (parser);
8249 enum rid keyword;
8250
8251 if (token->type != CPP_KEYWORD)
8252 {
8253 if (token->type == CPP_CLOSE_PAREN)
8254 c_parser_error (parser, "expected identifier");
8255 else
8256 {
8257 c_parser_consume_token (parser);
8258 c_parser_error (parser, "unknown property attribute");
8259 }
8260 break;
8261 }
8262 keyword = token->keyword;
8263 c_parser_consume_token (parser);
8264 switch (keyword)
8265 {
8266 case RID_ASSIGN: property_assign = true; break;
8267 case RID_COPY: property_copy = true; break;
8268 case RID_NONATOMIC: property_nonatomic = true; break;
8269 case RID_READONLY: property_readonly = true; break;
8270 case RID_READWRITE: property_readwrite = true; break;
8271 case RID_RETAIN: property_retain = true; break;
8272
8273 case RID_GETTER:
8274 case RID_SETTER:
8275 if (c_parser_next_token_is_not (parser, CPP_EQ))
8276 {
8277 if (keyword == RID_GETTER)
8278 c_parser_error (parser,
8279 "missing %<=%> (after %<getter%> attribute)");
8280 else
8281 c_parser_error (parser,
8282 "missing %<=%> (after %<setter%> attribute)");
8283 syntax_error = true;
8284 break;
8285 }
8286 c_parser_consume_token (parser); /* eat the = */
8287 if (c_parser_next_token_is_not (parser, CPP_NAME))
8288 {
8289 c_parser_error (parser, "expected identifier");
8290 syntax_error = true;
8291 break;
8292 }
8293 if (keyword == RID_SETTER)
8294 {
8295 if (property_setter_ident != NULL_TREE)
8296 c_parser_error (parser, "the %<setter%> attribute may only be specified once");
8297 else
8298 property_setter_ident = c_parser_peek_token (parser)->value;
8299 c_parser_consume_token (parser);
8300 if (c_parser_next_token_is_not (parser, CPP_COLON))
8301 c_parser_error (parser, "setter name must terminate with %<:%>");
8302 else
8303 c_parser_consume_token (parser);
8304 }
8305 else
8306 {
8307 if (property_getter_ident != NULL_TREE)
8308 c_parser_error (parser, "the %<getter%> attribute may only be specified once");
8309 else
8310 property_getter_ident = c_parser_peek_token (parser)->value;
8311 c_parser_consume_token (parser);
8312 }
8313 break;
8314 default:
8315 c_parser_error (parser, "unknown property attribute");
8316 syntax_error = true;
8317 break;
8318 }
8319
8320 if (syntax_error)
8321 break;
8322
8323 if (c_parser_next_token_is (parser, CPP_COMMA))
8324 c_parser_consume_token (parser);
8325 else
8326 break;
8327 }
8328 parser->objc_property_attr_context = false;
8329 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8330 }
8331 /* ... and the property declaration(s). */
8332 properties = c_parser_struct_declaration (parser);
8333
8334 if (properties == error_mark_node)
8335 {
8336 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
8337 parser->error = false;
8338 return;
8339 }
8340
8341 if (properties == NULL_TREE)
8342 c_parser_error (parser, "expected identifier");
8343 else
8344 {
8345 /* Comma-separated properties are chained together in
8346 reverse order; add them one by one. */
8347 properties = nreverse (properties);
8348
8349 for (; properties; properties = TREE_CHAIN (properties))
8350 objc_add_property_declaration (loc, copy_node (properties),
8351 property_readonly, property_readwrite,
8352 property_assign, property_retain,
8353 property_copy, property_nonatomic,
8354 property_getter_ident, property_setter_ident);
8355 }
8356
8357 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8358 parser->error = false;
8359 }
8360
8361 /* Parse an Objective-C @synthesize declaration. The syntax is:
8362
8363 objc-synthesize-declaration:
8364 @synthesize objc-synthesize-identifier-list ;
8365
8366 objc-synthesize-identifier-list:
8367 objc-synthesize-identifier
8368 objc-synthesize-identifier-list, objc-synthesize-identifier
8369
8370 objc-synthesize-identifier
8371 identifier
8372 identifier = identifier
8373
8374 For example:
8375 @synthesize MyProperty;
8376 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
8377
8378 PS: This function is identical to cp_parser_objc_at_synthesize_declaration
8379 for C++. Keep them in sync.
8380 */
8381 static void
8382 c_parser_objc_at_synthesize_declaration (c_parser *parser)
8383 {
8384 tree list = NULL_TREE;
8385 location_t loc;
8386 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_SYNTHESIZE));
8387 loc = c_parser_peek_token (parser)->location;
8388
8389 c_parser_consume_token (parser);
8390 while (true)
8391 {
8392 tree property, ivar;
8393 if (c_parser_next_token_is_not (parser, CPP_NAME))
8394 {
8395 c_parser_error (parser, "expected identifier");
8396 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
8397 /* Once we find the semicolon, we can resume normal parsing.
8398 We have to reset parser->error manually because
8399 c_parser_skip_until_found() won't reset it for us if the
8400 next token is precisely a semicolon. */
8401 parser->error = false;
8402 return;
8403 }
8404 property = c_parser_peek_token (parser)->value;
8405 c_parser_consume_token (parser);
8406 if (c_parser_next_token_is (parser, CPP_EQ))
8407 {
8408 c_parser_consume_token (parser);
8409 if (c_parser_next_token_is_not (parser, CPP_NAME))
8410 {
8411 c_parser_error (parser, "expected identifier");
8412 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
8413 parser->error = false;
8414 return;
8415 }
8416 ivar = c_parser_peek_token (parser)->value;
8417 c_parser_consume_token (parser);
8418 }
8419 else
8420 ivar = NULL_TREE;
8421 list = chainon (list, build_tree_list (ivar, property));
8422 if (c_parser_next_token_is (parser, CPP_COMMA))
8423 c_parser_consume_token (parser);
8424 else
8425 break;
8426 }
8427 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8428 objc_add_synthesize_declaration (loc, list);
8429 }
8430
8431 /* Parse an Objective-C @dynamic declaration. The syntax is:
8432
8433 objc-dynamic-declaration:
8434 @dynamic identifier-list ;
8435
8436 For example:
8437 @dynamic MyProperty;
8438 @dynamic MyProperty, AnotherProperty;
8439
8440 PS: This function is identical to cp_parser_objc_at_dynamic_declaration
8441 for C++. Keep them in sync.
8442 */
8443 static void
8444 c_parser_objc_at_dynamic_declaration (c_parser *parser)
8445 {
8446 tree list = NULL_TREE;
8447 location_t loc;
8448 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_DYNAMIC));
8449 loc = c_parser_peek_token (parser)->location;
8450
8451 c_parser_consume_token (parser);
8452 while (true)
8453 {
8454 tree property;
8455 if (c_parser_next_token_is_not (parser, CPP_NAME))
8456 {
8457 c_parser_error (parser, "expected identifier");
8458 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
8459 parser->error = false;
8460 return;
8461 }
8462 property = c_parser_peek_token (parser)->value;
8463 list = chainon (list, build_tree_list (NULL_TREE, property));
8464 c_parser_consume_token (parser);
8465 if (c_parser_next_token_is (parser, CPP_COMMA))
8466 c_parser_consume_token (parser);
8467 else
8468 break;
8469 }
8470 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8471 objc_add_dynamic_declaration (loc, list);
8472 }
8473
8474 \f
8475 /* Handle pragmas. Some OpenMP pragmas are associated with, and therefore
8476 should be considered, statements. ALLOW_STMT is true if we're within
8477 the context of a function and such pragmas are to be allowed. Returns
8478 true if we actually parsed such a pragma. */
8479
8480 static bool
8481 c_parser_pragma (c_parser *parser, enum pragma_context context)
8482 {
8483 unsigned int id;
8484
8485 id = c_parser_peek_token (parser)->pragma_kind;
8486 gcc_assert (id != PRAGMA_NONE);
8487
8488 switch (id)
8489 {
8490 case PRAGMA_OMP_BARRIER:
8491 if (context != pragma_compound)
8492 {
8493 if (context == pragma_stmt)
8494 c_parser_error (parser, "%<#pragma omp barrier%> may only be "
8495 "used in compound statements");
8496 goto bad_stmt;
8497 }
8498 c_parser_omp_barrier (parser);
8499 return false;
8500
8501 case PRAGMA_OMP_FLUSH:
8502 if (context != pragma_compound)
8503 {
8504 if (context == pragma_stmt)
8505 c_parser_error (parser, "%<#pragma omp flush%> may only be "
8506 "used in compound statements");
8507 goto bad_stmt;
8508 }
8509 c_parser_omp_flush (parser);
8510 return false;
8511
8512 case PRAGMA_OMP_TASKWAIT:
8513 if (context != pragma_compound)
8514 {
8515 if (context == pragma_stmt)
8516 c_parser_error (parser, "%<#pragma omp taskwait%> may only be "
8517 "used in compound statements");
8518 goto bad_stmt;
8519 }
8520 c_parser_omp_taskwait (parser);
8521 return false;
8522
8523 case PRAGMA_OMP_TASKYIELD:
8524 if (context != pragma_compound)
8525 {
8526 if (context == pragma_stmt)
8527 c_parser_error (parser, "%<#pragma omp taskyield%> may only be "
8528 "used in compound statements");
8529 goto bad_stmt;
8530 }
8531 c_parser_omp_taskyield (parser);
8532 return false;
8533
8534 case PRAGMA_OMP_THREADPRIVATE:
8535 c_parser_omp_threadprivate (parser);
8536 return false;
8537
8538 case PRAGMA_OMP_SECTION:
8539 error_at (c_parser_peek_token (parser)->location,
8540 "%<#pragma omp section%> may only be used in "
8541 "%<#pragma omp sections%> construct");
8542 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
8543 return false;
8544
8545 case PRAGMA_GCC_PCH_PREPROCESS:
8546 c_parser_error (parser, "%<#pragma GCC pch_preprocess%> must be first");
8547 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
8548 return false;
8549
8550 default:
8551 if (id < PRAGMA_FIRST_EXTERNAL)
8552 {
8553 if (context == pragma_external)
8554 {
8555 bad_stmt:
8556 c_parser_error (parser, "expected declaration specifiers");
8557 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
8558 return false;
8559 }
8560 c_parser_omp_construct (parser);
8561 return true;
8562 }
8563 break;
8564 }
8565
8566 c_parser_consume_pragma (parser);
8567 c_invoke_pragma_handler (id);
8568
8569 /* Skip to EOL, but suppress any error message. Those will have been
8570 generated by the handler routine through calling error, as opposed
8571 to calling c_parser_error. */
8572 parser->error = true;
8573 c_parser_skip_to_pragma_eol (parser);
8574
8575 return false;
8576 }
8577
8578 /* The interface the pragma parsers have to the lexer. */
8579
8580 enum cpp_ttype
8581 pragma_lex (tree *value)
8582 {
8583 c_token *tok = c_parser_peek_token (the_parser);
8584 enum cpp_ttype ret = tok->type;
8585
8586 *value = tok->value;
8587 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
8588 ret = CPP_EOF;
8589 else
8590 {
8591 if (ret == CPP_KEYWORD)
8592 ret = CPP_NAME;
8593 c_parser_consume_token (the_parser);
8594 }
8595
8596 return ret;
8597 }
8598
8599 static void
8600 c_parser_pragma_pch_preprocess (c_parser *parser)
8601 {
8602 tree name = NULL;
8603
8604 c_parser_consume_pragma (parser);
8605 if (c_parser_next_token_is (parser, CPP_STRING))
8606 {
8607 name = c_parser_peek_token (parser)->value;
8608 c_parser_consume_token (parser);
8609 }
8610 else
8611 c_parser_error (parser, "expected string literal");
8612 c_parser_skip_to_pragma_eol (parser);
8613
8614 if (name)
8615 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
8616 }
8617 \f
8618 /* OpenMP 2.5 parsing routines. */
8619
8620 /* Returns name of the next clause.
8621 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
8622 the token is not consumed. Otherwise appropriate pragma_omp_clause is
8623 returned and the token is consumed. */
8624
8625 static pragma_omp_clause
8626 c_parser_omp_clause_name (c_parser *parser)
8627 {
8628 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
8629
8630 if (c_parser_next_token_is_keyword (parser, RID_IF))
8631 result = PRAGMA_OMP_CLAUSE_IF;
8632 else if (c_parser_next_token_is_keyword (parser, RID_DEFAULT))
8633 result = PRAGMA_OMP_CLAUSE_DEFAULT;
8634 else if (c_parser_next_token_is (parser, CPP_NAME))
8635 {
8636 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
8637
8638 switch (p[0])
8639 {
8640 case 'c':
8641 if (!strcmp ("collapse", p))
8642 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
8643 else if (!strcmp ("copyin", p))
8644 result = PRAGMA_OMP_CLAUSE_COPYIN;
8645 else if (!strcmp ("copyprivate", p))
8646 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
8647 break;
8648 case 'f':
8649 if (!strcmp ("final", p))
8650 result = PRAGMA_OMP_CLAUSE_FINAL;
8651 else if (!strcmp ("firstprivate", p))
8652 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
8653 break;
8654 case 'l':
8655 if (!strcmp ("lastprivate", p))
8656 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
8657 break;
8658 case 'm':
8659 if (!strcmp ("mergeable", p))
8660 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
8661 break;
8662 case 'n':
8663 if (!strcmp ("nowait", p))
8664 result = PRAGMA_OMP_CLAUSE_NOWAIT;
8665 else if (!strcmp ("num_threads", p))
8666 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
8667 break;
8668 case 'o':
8669 if (!strcmp ("ordered", p))
8670 result = PRAGMA_OMP_CLAUSE_ORDERED;
8671 break;
8672 case 'p':
8673 if (!strcmp ("private", p))
8674 result = PRAGMA_OMP_CLAUSE_PRIVATE;
8675 break;
8676 case 'r':
8677 if (!strcmp ("reduction", p))
8678 result = PRAGMA_OMP_CLAUSE_REDUCTION;
8679 break;
8680 case 's':
8681 if (!strcmp ("schedule", p))
8682 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
8683 else if (!strcmp ("shared", p))
8684 result = PRAGMA_OMP_CLAUSE_SHARED;
8685 break;
8686 case 'u':
8687 if (!strcmp ("untied", p))
8688 result = PRAGMA_OMP_CLAUSE_UNTIED;
8689 break;
8690 }
8691 }
8692
8693 if (result != PRAGMA_OMP_CLAUSE_NONE)
8694 c_parser_consume_token (parser);
8695
8696 return result;
8697 }
8698
8699 /* Validate that a clause of the given type does not already exist. */
8700
8701 static void
8702 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
8703 const char *name)
8704 {
8705 tree c;
8706
8707 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
8708 if (OMP_CLAUSE_CODE (c) == code)
8709 {
8710 location_t loc = OMP_CLAUSE_LOCATION (c);
8711 error_at (loc, "too many %qs clauses", name);
8712 break;
8713 }
8714 }
8715
8716 /* OpenMP 2.5:
8717 variable-list:
8718 identifier
8719 variable-list , identifier
8720
8721 If KIND is nonzero, create the appropriate node and install the
8722 decl in OMP_CLAUSE_DECL and add the node to the head of the list.
8723 If KIND is nonzero, CLAUSE_LOC is the location of the clause.
8724
8725 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
8726 return the list created. */
8727
8728 static tree
8729 c_parser_omp_variable_list (c_parser *parser,
8730 location_t clause_loc,
8731 enum omp_clause_code kind,
8732 tree list)
8733 {
8734 if (c_parser_next_token_is_not (parser, CPP_NAME)
8735 || c_parser_peek_token (parser)->id_kind != C_ID_ID)
8736 c_parser_error (parser, "expected identifier");
8737
8738 while (c_parser_next_token_is (parser, CPP_NAME)
8739 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
8740 {
8741 tree t = lookup_name (c_parser_peek_token (parser)->value);
8742
8743 if (t == NULL_TREE)
8744 undeclared_variable (c_parser_peek_token (parser)->location,
8745 c_parser_peek_token (parser)->value);
8746 else if (t == error_mark_node)
8747 ;
8748 else if (kind != 0)
8749 {
8750 tree u = build_omp_clause (clause_loc, kind);
8751 OMP_CLAUSE_DECL (u) = t;
8752 OMP_CLAUSE_CHAIN (u) = list;
8753 list = u;
8754 }
8755 else
8756 list = tree_cons (t, NULL_TREE, list);
8757
8758 c_parser_consume_token (parser);
8759
8760 if (c_parser_next_token_is_not (parser, CPP_COMMA))
8761 break;
8762
8763 c_parser_consume_token (parser);
8764 }
8765
8766 return list;
8767 }
8768
8769 /* Similarly, but expect leading and trailing parenthesis. This is a very
8770 common case for omp clauses. */
8771
8772 static tree
8773 c_parser_omp_var_list_parens (c_parser *parser, enum omp_clause_code kind,
8774 tree list)
8775 {
8776 /* The clauses location. */
8777 location_t loc = c_parser_peek_token (parser)->location;
8778
8779 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
8780 {
8781 list = c_parser_omp_variable_list (parser, loc, kind, list);
8782 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8783 }
8784 return list;
8785 }
8786
8787 /* OpenMP 3.0:
8788 collapse ( constant-expression ) */
8789
8790 static tree
8791 c_parser_omp_clause_collapse (c_parser *parser, tree list)
8792 {
8793 tree c, num = error_mark_node;
8794 HOST_WIDE_INT n;
8795 location_t loc;
8796
8797 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse");
8798
8799 loc = c_parser_peek_token (parser)->location;
8800 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
8801 {
8802 num = c_parser_expr_no_commas (parser, NULL).value;
8803 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8804 }
8805 if (num == error_mark_node)
8806 return list;
8807 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
8808 || !host_integerp (num, 0)
8809 || (n = tree_low_cst (num, 0)) <= 0
8810 || (int) n != n)
8811 {
8812 error_at (loc,
8813 "collapse argument needs positive constant integer expression");
8814 return list;
8815 }
8816 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
8817 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
8818 OMP_CLAUSE_CHAIN (c) = list;
8819 return c;
8820 }
8821
8822 /* OpenMP 2.5:
8823 copyin ( variable-list ) */
8824
8825 static tree
8826 c_parser_omp_clause_copyin (c_parser *parser, tree list)
8827 {
8828 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_COPYIN, list);
8829 }
8830
8831 /* OpenMP 2.5:
8832 copyprivate ( variable-list ) */
8833
8834 static tree
8835 c_parser_omp_clause_copyprivate (c_parser *parser, tree list)
8836 {
8837 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_COPYPRIVATE, list);
8838 }
8839
8840 /* OpenMP 2.5:
8841 default ( shared | none ) */
8842
8843 static tree
8844 c_parser_omp_clause_default (c_parser *parser, tree list)
8845 {
8846 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
8847 location_t loc = c_parser_peek_token (parser)->location;
8848 tree c;
8849
8850 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
8851 return list;
8852 if (c_parser_next_token_is (parser, CPP_NAME))
8853 {
8854 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
8855
8856 switch (p[0])
8857 {
8858 case 'n':
8859 if (strcmp ("none", p) != 0)
8860 goto invalid_kind;
8861 kind = OMP_CLAUSE_DEFAULT_NONE;
8862 break;
8863
8864 case 's':
8865 if (strcmp ("shared", p) != 0)
8866 goto invalid_kind;
8867 kind = OMP_CLAUSE_DEFAULT_SHARED;
8868 break;
8869
8870 default:
8871 goto invalid_kind;
8872 }
8873
8874 c_parser_consume_token (parser);
8875 }
8876 else
8877 {
8878 invalid_kind:
8879 c_parser_error (parser, "expected %<none%> or %<shared%>");
8880 }
8881 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8882
8883 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
8884 return list;
8885
8886 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default");
8887 c = build_omp_clause (loc, OMP_CLAUSE_DEFAULT);
8888 OMP_CLAUSE_CHAIN (c) = list;
8889 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
8890
8891 return c;
8892 }
8893
8894 /* OpenMP 2.5:
8895 firstprivate ( variable-list ) */
8896
8897 static tree
8898 c_parser_omp_clause_firstprivate (c_parser *parser, tree list)
8899 {
8900 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_FIRSTPRIVATE, list);
8901 }
8902
8903 /* OpenMP 3.1:
8904 final ( expression ) */
8905
8906 static tree
8907 c_parser_omp_clause_final (c_parser *parser, tree list)
8908 {
8909 location_t loc = c_parser_peek_token (parser)->location;
8910 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
8911 {
8912 tree t = c_parser_paren_condition (parser);
8913 tree c;
8914
8915 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final");
8916
8917 c = build_omp_clause (loc, OMP_CLAUSE_FINAL);
8918 OMP_CLAUSE_FINAL_EXPR (c) = t;
8919 OMP_CLAUSE_CHAIN (c) = list;
8920 list = c;
8921 }
8922 else
8923 c_parser_error (parser, "expected %<(%>");
8924
8925 return list;
8926 }
8927
8928 /* OpenMP 2.5:
8929 if ( expression ) */
8930
8931 static tree
8932 c_parser_omp_clause_if (c_parser *parser, tree list)
8933 {
8934 location_t loc = c_parser_peek_token (parser)->location;
8935 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
8936 {
8937 tree t = c_parser_paren_condition (parser);
8938 tree c;
8939
8940 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if");
8941
8942 c = build_omp_clause (loc, OMP_CLAUSE_IF);
8943 OMP_CLAUSE_IF_EXPR (c) = t;
8944 OMP_CLAUSE_CHAIN (c) = list;
8945 list = c;
8946 }
8947 else
8948 c_parser_error (parser, "expected %<(%>");
8949
8950 return list;
8951 }
8952
8953 /* OpenMP 2.5:
8954 lastprivate ( variable-list ) */
8955
8956 static tree
8957 c_parser_omp_clause_lastprivate (c_parser *parser, tree list)
8958 {
8959 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_LASTPRIVATE, list);
8960 }
8961
8962 /* OpenMP 3.1:
8963 mergeable */
8964
8965 static tree
8966 c_parser_omp_clause_mergeable (c_parser *parser ATTRIBUTE_UNUSED, tree list)
8967 {
8968 tree c;
8969
8970 /* FIXME: Should we allow duplicates? */
8971 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable");
8972
8973 c = build_omp_clause (c_parser_peek_token (parser)->location,
8974 OMP_CLAUSE_MERGEABLE);
8975 OMP_CLAUSE_CHAIN (c) = list;
8976
8977 return c;
8978 }
8979
8980 /* OpenMP 2.5:
8981 nowait */
8982
8983 static tree
8984 c_parser_omp_clause_nowait (c_parser *parser ATTRIBUTE_UNUSED, tree list)
8985 {
8986 tree c;
8987 location_t loc = c_parser_peek_token (parser)->location;
8988
8989 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait");
8990
8991 c = build_omp_clause (loc, OMP_CLAUSE_NOWAIT);
8992 OMP_CLAUSE_CHAIN (c) = list;
8993 return c;
8994 }
8995
8996 /* OpenMP 2.5:
8997 num_threads ( expression ) */
8998
8999 static tree
9000 c_parser_omp_clause_num_threads (c_parser *parser, tree list)
9001 {
9002 location_t num_threads_loc = c_parser_peek_token (parser)->location;
9003 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
9004 {
9005 location_t expr_loc = c_parser_peek_token (parser)->location;
9006 tree c, t = c_parser_expression (parser).value;
9007 t = c_fully_fold (t, false, NULL);
9008
9009 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
9010
9011 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
9012 {
9013 c_parser_error (parser, "expected integer expression");
9014 return list;
9015 }
9016
9017 /* Attempt to statically determine when the number isn't positive. */
9018 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
9019 build_int_cst (TREE_TYPE (t), 0));
9020 if (CAN_HAVE_LOCATION_P (c))
9021 SET_EXPR_LOCATION (c, expr_loc);
9022 if (c == boolean_true_node)
9023 {
9024 warning_at (expr_loc, 0,
9025 "%<num_threads%> value must be positive");
9026 t = integer_one_node;
9027 }
9028
9029 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS, "num_threads");
9030
9031 c = build_omp_clause (num_threads_loc, OMP_CLAUSE_NUM_THREADS);
9032 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
9033 OMP_CLAUSE_CHAIN (c) = list;
9034 list = c;
9035 }
9036
9037 return list;
9038 }
9039
9040 /* OpenMP 2.5:
9041 ordered */
9042
9043 static tree
9044 c_parser_omp_clause_ordered (c_parser *parser, tree list)
9045 {
9046 tree c;
9047
9048 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED, "ordered");
9049
9050 c = build_omp_clause (c_parser_peek_token (parser)->location,
9051 OMP_CLAUSE_ORDERED);
9052 OMP_CLAUSE_CHAIN (c) = list;
9053
9054 return c;
9055 }
9056
9057 /* OpenMP 2.5:
9058 private ( variable-list ) */
9059
9060 static tree
9061 c_parser_omp_clause_private (c_parser *parser, tree list)
9062 {
9063 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_PRIVATE, list);
9064 }
9065
9066 /* OpenMP 2.5:
9067 reduction ( reduction-operator : variable-list )
9068
9069 reduction-operator:
9070 One of: + * - & ^ | && ||
9071
9072 OpenMP 3.1:
9073
9074 reduction-operator:
9075 One of: + * - & ^ | && || max min */
9076
9077 static tree
9078 c_parser_omp_clause_reduction (c_parser *parser, tree list)
9079 {
9080 location_t clause_loc = c_parser_peek_token (parser)->location;
9081 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
9082 {
9083 enum tree_code code;
9084
9085 switch (c_parser_peek_token (parser)->type)
9086 {
9087 case CPP_PLUS:
9088 code = PLUS_EXPR;
9089 break;
9090 case CPP_MULT:
9091 code = MULT_EXPR;
9092 break;
9093 case CPP_MINUS:
9094 code = MINUS_EXPR;
9095 break;
9096 case CPP_AND:
9097 code = BIT_AND_EXPR;
9098 break;
9099 case CPP_XOR:
9100 code = BIT_XOR_EXPR;
9101 break;
9102 case CPP_OR:
9103 code = BIT_IOR_EXPR;
9104 break;
9105 case CPP_AND_AND:
9106 code = TRUTH_ANDIF_EXPR;
9107 break;
9108 case CPP_OR_OR:
9109 code = TRUTH_ORIF_EXPR;
9110 break;
9111 case CPP_NAME:
9112 {
9113 const char *p
9114 = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
9115 if (strcmp (p, "min") == 0)
9116 {
9117 code = MIN_EXPR;
9118 break;
9119 }
9120 if (strcmp (p, "max") == 0)
9121 {
9122 code = MAX_EXPR;
9123 break;
9124 }
9125 }
9126 /* FALLTHRU */
9127 default:
9128 c_parser_error (parser,
9129 "expected %<+%>, %<*%>, %<-%>, %<&%>, "
9130 "%<^%>, %<|%>, %<&&%>, %<||%>, %<min%> or %<max%>");
9131 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
9132 return list;
9133 }
9134 c_parser_consume_token (parser);
9135 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
9136 {
9137 tree nl, c;
9138
9139 nl = c_parser_omp_variable_list (parser, clause_loc,
9140 OMP_CLAUSE_REDUCTION, list);
9141 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
9142 OMP_CLAUSE_REDUCTION_CODE (c) = code;
9143
9144 list = nl;
9145 }
9146 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
9147 }
9148 return list;
9149 }
9150
9151 /* OpenMP 2.5:
9152 schedule ( schedule-kind )
9153 schedule ( schedule-kind , expression )
9154
9155 schedule-kind:
9156 static | dynamic | guided | runtime | auto
9157 */
9158
9159 static tree
9160 c_parser_omp_clause_schedule (c_parser *parser, tree list)
9161 {
9162 tree c, t;
9163 location_t loc = c_parser_peek_token (parser)->location;
9164
9165 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
9166 return list;
9167
9168 c = build_omp_clause (loc, OMP_CLAUSE_SCHEDULE);
9169
9170 if (c_parser_next_token_is (parser, CPP_NAME))
9171 {
9172 tree kind = c_parser_peek_token (parser)->value;
9173 const char *p = IDENTIFIER_POINTER (kind);
9174
9175 switch (p[0])
9176 {
9177 case 'd':
9178 if (strcmp ("dynamic", p) != 0)
9179 goto invalid_kind;
9180 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
9181 break;
9182
9183 case 'g':
9184 if (strcmp ("guided", p) != 0)
9185 goto invalid_kind;
9186 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
9187 break;
9188
9189 case 'r':
9190 if (strcmp ("runtime", p) != 0)
9191 goto invalid_kind;
9192 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
9193 break;
9194
9195 default:
9196 goto invalid_kind;
9197 }
9198 }
9199 else if (c_parser_next_token_is_keyword (parser, RID_STATIC))
9200 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
9201 else if (c_parser_next_token_is_keyword (parser, RID_AUTO))
9202 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
9203 else
9204 goto invalid_kind;
9205
9206 c_parser_consume_token (parser);
9207 if (c_parser_next_token_is (parser, CPP_COMMA))
9208 {
9209 location_t here;
9210 c_parser_consume_token (parser);
9211
9212 here = c_parser_peek_token (parser)->location;
9213 t = c_parser_expr_no_commas (parser, NULL).value;
9214 t = c_fully_fold (t, false, NULL);
9215
9216 if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
9217 error_at (here, "schedule %<runtime%> does not take "
9218 "a %<chunk_size%> parameter");
9219 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
9220 error_at (here,
9221 "schedule %<auto%> does not take "
9222 "a %<chunk_size%> parameter");
9223 else if (TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE)
9224 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
9225 else
9226 c_parser_error (parser, "expected integer expression");
9227
9228 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
9229 }
9230 else
9231 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
9232 "expected %<,%> or %<)%>");
9233
9234 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
9235 OMP_CLAUSE_CHAIN (c) = list;
9236 return c;
9237
9238 invalid_kind:
9239 c_parser_error (parser, "invalid schedule kind");
9240 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
9241 return list;
9242 }
9243
9244 /* OpenMP 2.5:
9245 shared ( variable-list ) */
9246
9247 static tree
9248 c_parser_omp_clause_shared (c_parser *parser, tree list)
9249 {
9250 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_SHARED, list);
9251 }
9252
9253 /* OpenMP 3.0:
9254 untied */
9255
9256 static tree
9257 c_parser_omp_clause_untied (c_parser *parser ATTRIBUTE_UNUSED, tree list)
9258 {
9259 tree c;
9260
9261 /* FIXME: Should we allow duplicates? */
9262 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied");
9263
9264 c = build_omp_clause (c_parser_peek_token (parser)->location,
9265 OMP_CLAUSE_UNTIED);
9266 OMP_CLAUSE_CHAIN (c) = list;
9267
9268 return c;
9269 }
9270
9271 /* Parse all OpenMP clauses. The set clauses allowed by the directive
9272 is a bitmask in MASK. Return the list of clauses found; the result
9273 of clause default goes in *pdefault. */
9274
9275 static tree
9276 c_parser_omp_all_clauses (c_parser *parser, unsigned int mask,
9277 const char *where)
9278 {
9279 tree clauses = NULL;
9280 bool first = true;
9281
9282 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
9283 {
9284 location_t here;
9285 pragma_omp_clause c_kind;
9286 const char *c_name;
9287 tree prev = clauses;
9288
9289 if (!first && c_parser_next_token_is (parser, CPP_COMMA))
9290 c_parser_consume_token (parser);
9291
9292 first = false;
9293 here = c_parser_peek_token (parser)->location;
9294 c_kind = c_parser_omp_clause_name (parser);
9295
9296 switch (c_kind)
9297 {
9298 case PRAGMA_OMP_CLAUSE_COLLAPSE:
9299 clauses = c_parser_omp_clause_collapse (parser, clauses);
9300 c_name = "collapse";
9301 break;
9302 case PRAGMA_OMP_CLAUSE_COPYIN:
9303 clauses = c_parser_omp_clause_copyin (parser, clauses);
9304 c_name = "copyin";
9305 break;
9306 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
9307 clauses = c_parser_omp_clause_copyprivate (parser, clauses);
9308 c_name = "copyprivate";
9309 break;
9310 case PRAGMA_OMP_CLAUSE_DEFAULT:
9311 clauses = c_parser_omp_clause_default (parser, clauses);
9312 c_name = "default";
9313 break;
9314 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
9315 clauses = c_parser_omp_clause_firstprivate (parser, clauses);
9316 c_name = "firstprivate";
9317 break;
9318 case PRAGMA_OMP_CLAUSE_FINAL:
9319 clauses = c_parser_omp_clause_final (parser, clauses);
9320 c_name = "final";
9321 break;
9322 case PRAGMA_OMP_CLAUSE_IF:
9323 clauses = c_parser_omp_clause_if (parser, clauses);
9324 c_name = "if";
9325 break;
9326 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
9327 clauses = c_parser_omp_clause_lastprivate (parser, clauses);
9328 c_name = "lastprivate";
9329 break;
9330 case PRAGMA_OMP_CLAUSE_MERGEABLE:
9331 clauses = c_parser_omp_clause_mergeable (parser, clauses);
9332 c_name = "mergeable";
9333 break;
9334 case PRAGMA_OMP_CLAUSE_NOWAIT:
9335 clauses = c_parser_omp_clause_nowait (parser, clauses);
9336 c_name = "nowait";
9337 break;
9338 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
9339 clauses = c_parser_omp_clause_num_threads (parser, clauses);
9340 c_name = "num_threads";
9341 break;
9342 case PRAGMA_OMP_CLAUSE_ORDERED:
9343 clauses = c_parser_omp_clause_ordered (parser, clauses);
9344 c_name = "ordered";
9345 break;
9346 case PRAGMA_OMP_CLAUSE_PRIVATE:
9347 clauses = c_parser_omp_clause_private (parser, clauses);
9348 c_name = "private";
9349 break;
9350 case PRAGMA_OMP_CLAUSE_REDUCTION:
9351 clauses = c_parser_omp_clause_reduction (parser, clauses);
9352 c_name = "reduction";
9353 break;
9354 case PRAGMA_OMP_CLAUSE_SCHEDULE:
9355 clauses = c_parser_omp_clause_schedule (parser, clauses);
9356 c_name = "schedule";
9357 break;
9358 case PRAGMA_OMP_CLAUSE_SHARED:
9359 clauses = c_parser_omp_clause_shared (parser, clauses);
9360 c_name = "shared";
9361 break;
9362 case PRAGMA_OMP_CLAUSE_UNTIED:
9363 clauses = c_parser_omp_clause_untied (parser, clauses);
9364 c_name = "untied";
9365 break;
9366 default:
9367 c_parser_error (parser, "expected %<#pragma omp%> clause");
9368 goto saw_error;
9369 }
9370
9371 if (((mask >> c_kind) & 1) == 0 && !parser->error)
9372 {
9373 /* Remove the invalid clause(s) from the list to avoid
9374 confusing the rest of the compiler. */
9375 clauses = prev;
9376 error_at (here, "%qs is not valid for %qs", c_name, where);
9377 }
9378 }
9379
9380 saw_error:
9381 c_parser_skip_to_pragma_eol (parser);
9382
9383 return c_finish_omp_clauses (clauses);
9384 }
9385
9386 /* OpenMP 2.5:
9387 structured-block:
9388 statement
9389
9390 In practice, we're also interested in adding the statement to an
9391 outer node. So it is convenient if we work around the fact that
9392 c_parser_statement calls add_stmt. */
9393
9394 static tree
9395 c_parser_omp_structured_block (c_parser *parser)
9396 {
9397 tree stmt = push_stmt_list ();
9398 c_parser_statement (parser);
9399 return pop_stmt_list (stmt);
9400 }
9401
9402 /* OpenMP 2.5:
9403 # pragma omp atomic new-line
9404 expression-stmt
9405
9406 expression-stmt:
9407 x binop= expr | x++ | ++x | x-- | --x
9408 binop:
9409 +, *, -, /, &, ^, |, <<, >>
9410
9411 where x is an lvalue expression with scalar type.
9412
9413 OpenMP 3.1:
9414 # pragma omp atomic new-line
9415 update-stmt
9416
9417 # pragma omp atomic read new-line
9418 read-stmt
9419
9420 # pragma omp atomic write new-line
9421 write-stmt
9422
9423 # pragma omp atomic update new-line
9424 update-stmt
9425
9426 # pragma omp atomic capture new-line
9427 capture-stmt
9428
9429 # pragma omp atomic capture new-line
9430 capture-block
9431
9432 read-stmt:
9433 v = x
9434 write-stmt:
9435 x = expr
9436 update-stmt:
9437 expression-stmt | x = x binop expr
9438 capture-stmt:
9439 v = x binop= expr | v = x++ | v = ++x | v = x-- | v = --x
9440 capture-block:
9441 { v = x; update-stmt; } | { update-stmt; v = x; }
9442
9443 where x and v are lvalue expressions with scalar type.
9444
9445 LOC is the location of the #pragma token. */
9446
9447 static void
9448 c_parser_omp_atomic (location_t loc, c_parser *parser)
9449 {
9450 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE;
9451 tree lhs1 = NULL_TREE, rhs1 = NULL_TREE;
9452 tree stmt, orig_lhs;
9453 enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
9454 struct c_expr rhs_expr;
9455 bool structured_block = false;
9456
9457 if (c_parser_next_token_is (parser, CPP_NAME))
9458 {
9459 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
9460
9461 if (!strcmp (p, "read"))
9462 code = OMP_ATOMIC_READ;
9463 else if (!strcmp (p, "write"))
9464 code = NOP_EXPR;
9465 else if (!strcmp (p, "update"))
9466 code = OMP_ATOMIC;
9467 else if (!strcmp (p, "capture"))
9468 code = OMP_ATOMIC_CAPTURE_NEW;
9469 else
9470 p = NULL;
9471 if (p)
9472 c_parser_consume_token (parser);
9473 }
9474 c_parser_skip_to_pragma_eol (parser);
9475
9476 switch (code)
9477 {
9478 case OMP_ATOMIC_READ:
9479 case NOP_EXPR: /* atomic write */
9480 v = c_parser_unary_expression (parser).value;
9481 v = c_fully_fold (v, false, NULL);
9482 if (v == error_mark_node)
9483 goto saw_error;
9484 loc = c_parser_peek_token (parser)->location;
9485 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
9486 goto saw_error;
9487 if (code == NOP_EXPR)
9488 lhs = c_parser_expression (parser).value;
9489 else
9490 lhs = c_parser_unary_expression (parser).value;
9491 lhs = c_fully_fold (lhs, false, NULL);
9492 if (lhs == error_mark_node)
9493 goto saw_error;
9494 if (code == NOP_EXPR)
9495 {
9496 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
9497 opcode. */
9498 code = OMP_ATOMIC;
9499 rhs = lhs;
9500 lhs = v;
9501 v = NULL_TREE;
9502 }
9503 goto done;
9504 case OMP_ATOMIC_CAPTURE_NEW:
9505 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
9506 {
9507 c_parser_consume_token (parser);
9508 structured_block = true;
9509 }
9510 else
9511 {
9512 v = c_parser_unary_expression (parser).value;
9513 v = c_fully_fold (v, false, NULL);
9514 if (v == error_mark_node)
9515 goto saw_error;
9516 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
9517 goto saw_error;
9518 }
9519 break;
9520 default:
9521 break;
9522 }
9523
9524 /* For structured_block case we don't know yet whether
9525 old or new x should be captured. */
9526 restart:
9527 lhs = c_parser_unary_expression (parser).value;
9528 lhs = c_fully_fold (lhs, false, NULL);
9529 orig_lhs = lhs;
9530 switch (TREE_CODE (lhs))
9531 {
9532 case ERROR_MARK:
9533 saw_error:
9534 c_parser_skip_to_end_of_block_or_statement (parser);
9535 if (structured_block)
9536 {
9537 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
9538 c_parser_consume_token (parser);
9539 else if (code == OMP_ATOMIC_CAPTURE_NEW)
9540 {
9541 c_parser_skip_to_end_of_block_or_statement (parser);
9542 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
9543 c_parser_consume_token (parser);
9544 }
9545 }
9546 return;
9547
9548 case POSTINCREMENT_EXPR:
9549 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
9550 code = OMP_ATOMIC_CAPTURE_OLD;
9551 /* FALLTHROUGH */
9552 case PREINCREMENT_EXPR:
9553 lhs = TREE_OPERAND (lhs, 0);
9554 opcode = PLUS_EXPR;
9555 rhs = integer_one_node;
9556 break;
9557
9558 case POSTDECREMENT_EXPR:
9559 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
9560 code = OMP_ATOMIC_CAPTURE_OLD;
9561 /* FALLTHROUGH */
9562 case PREDECREMENT_EXPR:
9563 lhs = TREE_OPERAND (lhs, 0);
9564 opcode = MINUS_EXPR;
9565 rhs = integer_one_node;
9566 break;
9567
9568 case COMPOUND_EXPR:
9569 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
9570 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
9571 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
9572 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
9573 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
9574 (TREE_OPERAND (lhs, 1), 0), 0)))
9575 == BOOLEAN_TYPE)
9576 /* Undo effects of boolean_increment for post {in,de}crement. */
9577 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
9578 /* FALLTHRU */
9579 case MODIFY_EXPR:
9580 if (TREE_CODE (lhs) == MODIFY_EXPR
9581 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
9582 {
9583 /* Undo effects of boolean_increment. */
9584 if (integer_onep (TREE_OPERAND (lhs, 1)))
9585 {
9586 /* This is pre or post increment. */
9587 rhs = TREE_OPERAND (lhs, 1);
9588 lhs = TREE_OPERAND (lhs, 0);
9589 opcode = NOP_EXPR;
9590 if (code == OMP_ATOMIC_CAPTURE_NEW
9591 && !structured_block
9592 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
9593 code = OMP_ATOMIC_CAPTURE_OLD;
9594 break;
9595 }
9596 if (TREE_CODE (TREE_OPERAND (lhs, 1)) == TRUTH_NOT_EXPR
9597 && TREE_OPERAND (lhs, 0)
9598 == TREE_OPERAND (TREE_OPERAND (lhs, 1), 0))
9599 {
9600 /* This is pre or post decrement. */
9601 rhs = TREE_OPERAND (lhs, 1);
9602 lhs = TREE_OPERAND (lhs, 0);
9603 opcode = NOP_EXPR;
9604 if (code == OMP_ATOMIC_CAPTURE_NEW
9605 && !structured_block
9606 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
9607 code = OMP_ATOMIC_CAPTURE_OLD;
9608 break;
9609 }
9610 }
9611 /* FALLTHRU */
9612 default:
9613 switch (c_parser_peek_token (parser)->type)
9614 {
9615 case CPP_MULT_EQ:
9616 opcode = MULT_EXPR;
9617 break;
9618 case CPP_DIV_EQ:
9619 opcode = TRUNC_DIV_EXPR;
9620 break;
9621 case CPP_PLUS_EQ:
9622 opcode = PLUS_EXPR;
9623 break;
9624 case CPP_MINUS_EQ:
9625 opcode = MINUS_EXPR;
9626 break;
9627 case CPP_LSHIFT_EQ:
9628 opcode = LSHIFT_EXPR;
9629 break;
9630 case CPP_RSHIFT_EQ:
9631 opcode = RSHIFT_EXPR;
9632 break;
9633 case CPP_AND_EQ:
9634 opcode = BIT_AND_EXPR;
9635 break;
9636 case CPP_OR_EQ:
9637 opcode = BIT_IOR_EXPR;
9638 break;
9639 case CPP_XOR_EQ:
9640 opcode = BIT_XOR_EXPR;
9641 break;
9642 case CPP_EQ:
9643 if (structured_block || code == OMP_ATOMIC)
9644 {
9645 location_t aloc = c_parser_peek_token (parser)->location;
9646 location_t rhs_loc;
9647 enum c_parser_prec oprec = PREC_NONE;
9648
9649 c_parser_consume_token (parser);
9650 rhs1 = c_parser_unary_expression (parser).value;
9651 rhs1 = c_fully_fold (rhs1, false, NULL);
9652 if (rhs1 == error_mark_node)
9653 goto saw_error;
9654 switch (c_parser_peek_token (parser)->type)
9655 {
9656 case CPP_SEMICOLON:
9657 if (code == OMP_ATOMIC_CAPTURE_NEW)
9658 {
9659 code = OMP_ATOMIC_CAPTURE_OLD;
9660 v = lhs;
9661 lhs = NULL_TREE;
9662 lhs1 = rhs1;
9663 rhs1 = NULL_TREE;
9664 c_parser_consume_token (parser);
9665 goto restart;
9666 }
9667 c_parser_error (parser,
9668 "invalid form of %<#pragma omp atomic%>");
9669 goto saw_error;
9670 case CPP_MULT:
9671 opcode = MULT_EXPR;
9672 oprec = PREC_MULT;
9673 break;
9674 case CPP_DIV:
9675 opcode = TRUNC_DIV_EXPR;
9676 oprec = PREC_MULT;
9677 break;
9678 case CPP_PLUS:
9679 opcode = PLUS_EXPR;
9680 oprec = PREC_ADD;
9681 break;
9682 case CPP_MINUS:
9683 opcode = MINUS_EXPR;
9684 oprec = PREC_ADD;
9685 break;
9686 case CPP_LSHIFT:
9687 opcode = LSHIFT_EXPR;
9688 oprec = PREC_SHIFT;
9689 break;
9690 case CPP_RSHIFT:
9691 opcode = RSHIFT_EXPR;
9692 oprec = PREC_SHIFT;
9693 break;
9694 case CPP_AND:
9695 opcode = BIT_AND_EXPR;
9696 oprec = PREC_BITAND;
9697 break;
9698 case CPP_OR:
9699 opcode = BIT_IOR_EXPR;
9700 oprec = PREC_BITOR;
9701 break;
9702 case CPP_XOR:
9703 opcode = BIT_XOR_EXPR;
9704 oprec = PREC_BITXOR;
9705 break;
9706 default:
9707 c_parser_error (parser,
9708 "invalid operator for %<#pragma omp atomic%>");
9709 goto saw_error;
9710 }
9711 loc = aloc;
9712 c_parser_consume_token (parser);
9713 rhs_loc = c_parser_peek_token (parser)->location;
9714 if (commutative_tree_code (opcode))
9715 oprec = (enum c_parser_prec) (oprec - 1);
9716 rhs_expr = c_parser_binary_expression (parser, NULL, oprec);
9717 rhs_expr = default_function_array_read_conversion (rhs_loc,
9718 rhs_expr);
9719 rhs = rhs_expr.value;
9720 rhs = c_fully_fold (rhs, false, NULL);
9721 goto stmt_done;
9722 }
9723 /* FALLTHROUGH */
9724 default:
9725 c_parser_error (parser,
9726 "invalid operator for %<#pragma omp atomic%>");
9727 goto saw_error;
9728 }
9729
9730 /* Arrange to pass the location of the assignment operator to
9731 c_finish_omp_atomic. */
9732 loc = c_parser_peek_token (parser)->location;
9733 c_parser_consume_token (parser);
9734 {
9735 location_t rhs_loc = c_parser_peek_token (parser)->location;
9736 rhs_expr = c_parser_expression (parser);
9737 rhs_expr = default_function_array_read_conversion (rhs_loc, rhs_expr);
9738 }
9739 rhs = rhs_expr.value;
9740 rhs = c_fully_fold (rhs, false, NULL);
9741 break;
9742 }
9743 stmt_done:
9744 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
9745 {
9746 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
9747 goto saw_error;
9748 v = c_parser_unary_expression (parser).value;
9749 v = c_fully_fold (v, false, NULL);
9750 if (v == error_mark_node)
9751 goto saw_error;
9752 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
9753 goto saw_error;
9754 lhs1 = c_parser_unary_expression (parser).value;
9755 lhs1 = c_fully_fold (lhs1, false, NULL);
9756 if (lhs1 == error_mark_node)
9757 goto saw_error;
9758 }
9759 if (structured_block)
9760 {
9761 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9762 c_parser_require (parser, CPP_CLOSE_BRACE, "expected %<}%>");
9763 }
9764 done:
9765 stmt = c_finish_omp_atomic (loc, code, opcode, lhs, rhs, v, lhs1, rhs1);
9766 if (stmt != error_mark_node)
9767 add_stmt (stmt);
9768
9769 if (!structured_block)
9770 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9771 }
9772
9773
9774 /* OpenMP 2.5:
9775 # pragma omp barrier new-line
9776 */
9777
9778 static void
9779 c_parser_omp_barrier (c_parser *parser)
9780 {
9781 location_t loc = c_parser_peek_token (parser)->location;
9782 c_parser_consume_pragma (parser);
9783 c_parser_skip_to_pragma_eol (parser);
9784
9785 c_finish_omp_barrier (loc);
9786 }
9787
9788 /* OpenMP 2.5:
9789 # pragma omp critical [(name)] new-line
9790 structured-block
9791
9792 LOC is the location of the #pragma itself. */
9793
9794 static tree
9795 c_parser_omp_critical (location_t loc, c_parser *parser)
9796 {
9797 tree stmt, name = NULL;
9798
9799 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
9800 {
9801 c_parser_consume_token (parser);
9802 if (c_parser_next_token_is (parser, CPP_NAME))
9803 {
9804 name = c_parser_peek_token (parser)->value;
9805 c_parser_consume_token (parser);
9806 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
9807 }
9808 else
9809 c_parser_error (parser, "expected identifier");
9810 }
9811 else if (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
9812 c_parser_error (parser, "expected %<(%> or end of line");
9813 c_parser_skip_to_pragma_eol (parser);
9814
9815 stmt = c_parser_omp_structured_block (parser);
9816 return c_finish_omp_critical (loc, stmt, name);
9817 }
9818
9819 /* OpenMP 2.5:
9820 # pragma omp flush flush-vars[opt] new-line
9821
9822 flush-vars:
9823 ( variable-list ) */
9824
9825 static void
9826 c_parser_omp_flush (c_parser *parser)
9827 {
9828 location_t loc = c_parser_peek_token (parser)->location;
9829 c_parser_consume_pragma (parser);
9830 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
9831 c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
9832 else if (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
9833 c_parser_error (parser, "expected %<(%> or end of line");
9834 c_parser_skip_to_pragma_eol (parser);
9835
9836 c_finish_omp_flush (loc);
9837 }
9838
9839 /* Parse the restricted form of the for statement allowed by OpenMP.
9840 The real trick here is to determine the loop control variable early
9841 so that we can push a new decl if necessary to make it private.
9842 LOC is the location of the OMP in "#pragma omp". */
9843
9844 static tree
9845 c_parser_omp_for_loop (location_t loc,
9846 c_parser *parser, tree clauses, tree *par_clauses)
9847 {
9848 tree decl, cond, incr, save_break, save_cont, body, init, stmt, cl;
9849 tree declv, condv, incrv, initv, ret = NULL;
9850 bool fail = false, open_brace_parsed = false;
9851 int i, collapse = 1, nbraces = 0;
9852 location_t for_loc;
9853 VEC(tree,gc) *for_block = make_tree_vector ();
9854
9855 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
9856 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
9857 collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
9858
9859 gcc_assert (collapse >= 1);
9860
9861 declv = make_tree_vec (collapse);
9862 initv = make_tree_vec (collapse);
9863 condv = make_tree_vec (collapse);
9864 incrv = make_tree_vec (collapse);
9865
9866 if (!c_parser_next_token_is_keyword (parser, RID_FOR))
9867 {
9868 c_parser_error (parser, "for statement expected");
9869 return NULL;
9870 }
9871 for_loc = c_parser_peek_token (parser)->location;
9872 c_parser_consume_token (parser);
9873
9874 for (i = 0; i < collapse; i++)
9875 {
9876 int bracecount = 0;
9877
9878 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
9879 goto pop_scopes;
9880
9881 /* Parse the initialization declaration or expression. */
9882 if (c_parser_next_tokens_start_declaration (parser))
9883 {
9884 if (i > 0)
9885 VEC_safe_push (tree, gc, for_block, c_begin_compound_stmt (true));
9886 c_parser_declaration_or_fndef (parser, true, true, true, true, true, NULL);
9887 decl = check_for_loop_decls (for_loc, flag_isoc99);
9888 if (decl == NULL)
9889 goto error_init;
9890 if (DECL_INITIAL (decl) == error_mark_node)
9891 decl = error_mark_node;
9892 init = decl;
9893 }
9894 else if (c_parser_next_token_is (parser, CPP_NAME)
9895 && c_parser_peek_2nd_token (parser)->type == CPP_EQ)
9896 {
9897 struct c_expr decl_exp;
9898 struct c_expr init_exp;
9899 location_t init_loc;
9900
9901 decl_exp = c_parser_postfix_expression (parser);
9902 decl = decl_exp.value;
9903
9904 c_parser_require (parser, CPP_EQ, "expected %<=%>");
9905
9906 init_loc = c_parser_peek_token (parser)->location;
9907 init_exp = c_parser_expr_no_commas (parser, NULL);
9908 init_exp = default_function_array_read_conversion (init_loc,
9909 init_exp);
9910 init = build_modify_expr (init_loc, decl, decl_exp.original_type,
9911 NOP_EXPR, init_loc, init_exp.value,
9912 init_exp.original_type);
9913 init = c_process_expr_stmt (init_loc, init);
9914
9915 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9916 }
9917 else
9918 {
9919 error_init:
9920 c_parser_error (parser,
9921 "expected iteration declaration or initialization");
9922 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
9923 "expected %<)%>");
9924 fail = true;
9925 goto parse_next;
9926 }
9927
9928 /* Parse the loop condition. */
9929 cond = NULL_TREE;
9930 if (c_parser_next_token_is_not (parser, CPP_SEMICOLON))
9931 {
9932 location_t cond_loc = c_parser_peek_token (parser)->location;
9933 struct c_expr cond_expr = c_parser_binary_expression (parser, NULL,
9934 PREC_NONE);
9935
9936 cond = cond_expr.value;
9937 cond = c_objc_common_truthvalue_conversion (cond_loc, cond);
9938 cond = c_fully_fold (cond, false, NULL);
9939 switch (cond_expr.original_code)
9940 {
9941 case GT_EXPR:
9942 case GE_EXPR:
9943 case LT_EXPR:
9944 case LE_EXPR:
9945 break;
9946 default:
9947 /* Can't be cond = error_mark_node, because we want to preserve
9948 the location until c_finish_omp_for. */
9949 cond = build1 (NOP_EXPR, boolean_type_node, error_mark_node);
9950 break;
9951 }
9952 protected_set_expr_location (cond, cond_loc);
9953 }
9954 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9955
9956 /* Parse the increment expression. */
9957 incr = NULL_TREE;
9958 if (c_parser_next_token_is_not (parser, CPP_CLOSE_PAREN))
9959 {
9960 location_t incr_loc = c_parser_peek_token (parser)->location;
9961
9962 incr = c_process_expr_stmt (incr_loc,
9963 c_parser_expression (parser).value);
9964 }
9965 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
9966
9967 if (decl == NULL || decl == error_mark_node || init == error_mark_node)
9968 fail = true;
9969 else
9970 {
9971 TREE_VEC_ELT (declv, i) = decl;
9972 TREE_VEC_ELT (initv, i) = init;
9973 TREE_VEC_ELT (condv, i) = cond;
9974 TREE_VEC_ELT (incrv, i) = incr;
9975 }
9976
9977 parse_next:
9978 if (i == collapse - 1)
9979 break;
9980
9981 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
9982 in between the collapsed for loops to be still considered perfectly
9983 nested. Hopefully the final version clarifies this.
9984 For now handle (multiple) {'s and empty statements. */
9985 do
9986 {
9987 if (c_parser_next_token_is_keyword (parser, RID_FOR))
9988 {
9989 c_parser_consume_token (parser);
9990 break;
9991 }
9992 else if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
9993 {
9994 c_parser_consume_token (parser);
9995 bracecount++;
9996 }
9997 else if (bracecount
9998 && c_parser_next_token_is (parser, CPP_SEMICOLON))
9999 c_parser_consume_token (parser);
10000 else
10001 {
10002 c_parser_error (parser, "not enough perfectly nested loops");
10003 if (bracecount)
10004 {
10005 open_brace_parsed = true;
10006 bracecount--;
10007 }
10008 fail = true;
10009 collapse = 0;
10010 break;
10011 }
10012 }
10013 while (1);
10014
10015 nbraces += bracecount;
10016 }
10017
10018 save_break = c_break_label;
10019 c_break_label = size_one_node;
10020 save_cont = c_cont_label;
10021 c_cont_label = NULL_TREE;
10022 body = push_stmt_list ();
10023
10024 if (open_brace_parsed)
10025 {
10026 location_t here = c_parser_peek_token (parser)->location;
10027 stmt = c_begin_compound_stmt (true);
10028 c_parser_compound_statement_nostart (parser);
10029 add_stmt (c_end_compound_stmt (here, stmt, true));
10030 }
10031 else
10032 add_stmt (c_parser_c99_block_statement (parser));
10033 if (c_cont_label)
10034 {
10035 tree t = build1 (LABEL_EXPR, void_type_node, c_cont_label);
10036 SET_EXPR_LOCATION (t, loc);
10037 add_stmt (t);
10038 }
10039
10040 body = pop_stmt_list (body);
10041 c_break_label = save_break;
10042 c_cont_label = save_cont;
10043
10044 while (nbraces)
10045 {
10046 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
10047 {
10048 c_parser_consume_token (parser);
10049 nbraces--;
10050 }
10051 else if (c_parser_next_token_is (parser, CPP_SEMICOLON))
10052 c_parser_consume_token (parser);
10053 else
10054 {
10055 c_parser_error (parser, "collapsed loops not perfectly nested");
10056 while (nbraces)
10057 {
10058 location_t here = c_parser_peek_token (parser)->location;
10059 stmt = c_begin_compound_stmt (true);
10060 add_stmt (body);
10061 c_parser_compound_statement_nostart (parser);
10062 body = c_end_compound_stmt (here, stmt, true);
10063 nbraces--;
10064 }
10065 goto pop_scopes;
10066 }
10067 }
10068
10069 /* Only bother calling c_finish_omp_for if we haven't already generated
10070 an error from the initialization parsing. */
10071 if (!fail)
10072 {
10073 stmt = c_finish_omp_for (loc, declv, initv, condv, incrv, body, NULL);
10074 if (stmt)
10075 {
10076 if (par_clauses != NULL)
10077 {
10078 tree *c;
10079 for (c = par_clauses; *c ; )
10080 if (OMP_CLAUSE_CODE (*c) != OMP_CLAUSE_FIRSTPRIVATE
10081 && OMP_CLAUSE_CODE (*c) != OMP_CLAUSE_LASTPRIVATE)
10082 c = &OMP_CLAUSE_CHAIN (*c);
10083 else
10084 {
10085 for (i = 0; i < collapse; i++)
10086 if (TREE_VEC_ELT (declv, i) == OMP_CLAUSE_DECL (*c))
10087 break;
10088 if (i == collapse)
10089 c = &OMP_CLAUSE_CHAIN (*c);
10090 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE)
10091 {
10092 error_at (loc,
10093 "iteration variable %qD should not be firstprivate",
10094 OMP_CLAUSE_DECL (*c));
10095 *c = OMP_CLAUSE_CHAIN (*c);
10096 }
10097 else
10098 {
10099 /* Copy lastprivate (decl) clause to OMP_FOR_CLAUSES,
10100 change it to shared (decl) in
10101 OMP_PARALLEL_CLAUSES. */
10102 tree l = build_omp_clause (OMP_CLAUSE_LOCATION (*c),
10103 OMP_CLAUSE_LASTPRIVATE);
10104 OMP_CLAUSE_DECL (l) = OMP_CLAUSE_DECL (*c);
10105 OMP_CLAUSE_CHAIN (l) = clauses;
10106 clauses = l;
10107 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
10108 }
10109 }
10110 }
10111 OMP_FOR_CLAUSES (stmt) = clauses;
10112 }
10113 ret = stmt;
10114 }
10115 pop_scopes:
10116 while (!VEC_empty (tree, for_block))
10117 {
10118 /* FIXME diagnostics: LOC below should be the actual location of
10119 this particular for block. We need to build a list of
10120 locations to go along with FOR_BLOCK. */
10121 stmt = c_end_compound_stmt (loc, VEC_pop (tree, for_block), true);
10122 add_stmt (stmt);
10123 }
10124 release_tree_vector (for_block);
10125 return ret;
10126 }
10127
10128 /* OpenMP 2.5:
10129 #pragma omp for for-clause[optseq] new-line
10130 for-loop
10131
10132 LOC is the location of the #pragma token.
10133 */
10134
10135 #define OMP_FOR_CLAUSE_MASK \
10136 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
10137 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
10138 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
10139 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
10140 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \
10141 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \
10142 | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE) \
10143 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
10144
10145 static tree
10146 c_parser_omp_for (location_t loc, c_parser *parser)
10147 {
10148 tree block, clauses, ret;
10149
10150 clauses = c_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
10151 "#pragma omp for");
10152
10153 block = c_begin_compound_stmt (true);
10154 ret = c_parser_omp_for_loop (loc, parser, clauses, NULL);
10155 block = c_end_compound_stmt (loc, block, true);
10156 add_stmt (block);
10157
10158 return ret;
10159 }
10160
10161 /* OpenMP 2.5:
10162 # pragma omp master new-line
10163 structured-block
10164
10165 LOC is the location of the #pragma token.
10166 */
10167
10168 static tree
10169 c_parser_omp_master (location_t loc, c_parser *parser)
10170 {
10171 c_parser_skip_to_pragma_eol (parser);
10172 return c_finish_omp_master (loc, c_parser_omp_structured_block (parser));
10173 }
10174
10175 /* OpenMP 2.5:
10176 # pragma omp ordered new-line
10177 structured-block
10178
10179 LOC is the location of the #pragma itself.
10180 */
10181
10182 static tree
10183 c_parser_omp_ordered (location_t loc, c_parser *parser)
10184 {
10185 c_parser_skip_to_pragma_eol (parser);
10186 return c_finish_omp_ordered (loc, c_parser_omp_structured_block (parser));
10187 }
10188
10189 /* OpenMP 2.5:
10190
10191 section-scope:
10192 { section-sequence }
10193
10194 section-sequence:
10195 section-directive[opt] structured-block
10196 section-sequence section-directive structured-block
10197
10198 SECTIONS_LOC is the location of the #pragma omp sections. */
10199
10200 static tree
10201 c_parser_omp_sections_scope (location_t sections_loc, c_parser *parser)
10202 {
10203 tree stmt, substmt;
10204 bool error_suppress = false;
10205 location_t loc;
10206
10207 loc = c_parser_peek_token (parser)->location;
10208 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
10209 {
10210 /* Avoid skipping until the end of the block. */
10211 parser->error = false;
10212 return NULL_TREE;
10213 }
10214
10215 stmt = push_stmt_list ();
10216
10217 if (c_parser_peek_token (parser)->pragma_kind != PRAGMA_OMP_SECTION)
10218 {
10219 substmt = push_stmt_list ();
10220
10221 while (1)
10222 {
10223 c_parser_statement (parser);
10224
10225 if (c_parser_peek_token (parser)->pragma_kind == PRAGMA_OMP_SECTION)
10226 break;
10227 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
10228 break;
10229 if (c_parser_next_token_is (parser, CPP_EOF))
10230 break;
10231 }
10232
10233 substmt = pop_stmt_list (substmt);
10234 substmt = build1 (OMP_SECTION, void_type_node, substmt);
10235 SET_EXPR_LOCATION (substmt, loc);
10236 add_stmt (substmt);
10237 }
10238
10239 while (1)
10240 {
10241 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
10242 break;
10243 if (c_parser_next_token_is (parser, CPP_EOF))
10244 break;
10245
10246 loc = c_parser_peek_token (parser)->location;
10247 if (c_parser_peek_token (parser)->pragma_kind == PRAGMA_OMP_SECTION)
10248 {
10249 c_parser_consume_pragma (parser);
10250 c_parser_skip_to_pragma_eol (parser);
10251 error_suppress = false;
10252 }
10253 else if (!error_suppress)
10254 {
10255 error_at (loc, "expected %<#pragma omp section%> or %<}%>");
10256 error_suppress = true;
10257 }
10258
10259 substmt = c_parser_omp_structured_block (parser);
10260 substmt = build1 (OMP_SECTION, void_type_node, substmt);
10261 SET_EXPR_LOCATION (substmt, loc);
10262 add_stmt (substmt);
10263 }
10264 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE,
10265 "expected %<#pragma omp section%> or %<}%>");
10266
10267 substmt = pop_stmt_list (stmt);
10268
10269 stmt = make_node (OMP_SECTIONS);
10270 SET_EXPR_LOCATION (stmt, sections_loc);
10271 TREE_TYPE (stmt) = void_type_node;
10272 OMP_SECTIONS_BODY (stmt) = substmt;
10273
10274 return add_stmt (stmt);
10275 }
10276
10277 /* OpenMP 2.5:
10278 # pragma omp sections sections-clause[optseq] newline
10279 sections-scope
10280
10281 LOC is the location of the #pragma token.
10282 */
10283
10284 #define OMP_SECTIONS_CLAUSE_MASK \
10285 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
10286 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
10287 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
10288 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
10289 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
10290
10291 static tree
10292 c_parser_omp_sections (location_t loc, c_parser *parser)
10293 {
10294 tree block, clauses, ret;
10295
10296 clauses = c_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
10297 "#pragma omp sections");
10298
10299 block = c_begin_compound_stmt (true);
10300 ret = c_parser_omp_sections_scope (loc, parser);
10301 if (ret)
10302 OMP_SECTIONS_CLAUSES (ret) = clauses;
10303 block = c_end_compound_stmt (loc, block, true);
10304 add_stmt (block);
10305
10306 return ret;
10307 }
10308
10309 /* OpenMP 2.5:
10310 # pragma parallel parallel-clause new-line
10311 # pragma parallel for parallel-for-clause new-line
10312 # pragma parallel sections parallel-sections-clause new-line
10313
10314 LOC is the location of the #pragma token.
10315 */
10316
10317 #define OMP_PARALLEL_CLAUSE_MASK \
10318 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
10319 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
10320 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
10321 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
10322 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
10323 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \
10324 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
10325 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
10326
10327 static tree
10328 c_parser_omp_parallel (location_t loc, c_parser *parser)
10329 {
10330 enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
10331 const char *p_name = "#pragma omp parallel";
10332 tree stmt, clauses, par_clause, ws_clause, block;
10333 unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
10334
10335 if (c_parser_next_token_is_keyword (parser, RID_FOR))
10336 {
10337 c_parser_consume_token (parser);
10338 p_kind = PRAGMA_OMP_PARALLEL_FOR;
10339 p_name = "#pragma omp parallel for";
10340 mask |= OMP_FOR_CLAUSE_MASK;
10341 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
10342 }
10343 else if (c_parser_next_token_is (parser, CPP_NAME))
10344 {
10345 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
10346 if (strcmp (p, "sections") == 0)
10347 {
10348 c_parser_consume_token (parser);
10349 p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
10350 p_name = "#pragma omp parallel sections";
10351 mask |= OMP_SECTIONS_CLAUSE_MASK;
10352 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
10353 }
10354 }
10355
10356 clauses = c_parser_omp_all_clauses (parser, mask, p_name);
10357
10358 switch (p_kind)
10359 {
10360 case PRAGMA_OMP_PARALLEL:
10361 block = c_begin_omp_parallel ();
10362 c_parser_statement (parser);
10363 stmt = c_finish_omp_parallel (loc, clauses, block);
10364 break;
10365
10366 case PRAGMA_OMP_PARALLEL_FOR:
10367 block = c_begin_omp_parallel ();
10368 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
10369 c_parser_omp_for_loop (loc, parser, ws_clause, &par_clause);
10370 stmt = c_finish_omp_parallel (loc, par_clause, block);
10371 OMP_PARALLEL_COMBINED (stmt) = 1;
10372 break;
10373
10374 case PRAGMA_OMP_PARALLEL_SECTIONS:
10375 block = c_begin_omp_parallel ();
10376 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
10377 stmt = c_parser_omp_sections_scope (loc, parser);
10378 if (stmt)
10379 OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
10380 stmt = c_finish_omp_parallel (loc, par_clause, block);
10381 OMP_PARALLEL_COMBINED (stmt) = 1;
10382 break;
10383
10384 default:
10385 gcc_unreachable ();
10386 }
10387
10388 return stmt;
10389 }
10390
10391 /* OpenMP 2.5:
10392 # pragma omp single single-clause[optseq] new-line
10393 structured-block
10394
10395 LOC is the location of the #pragma.
10396 */
10397
10398 #define OMP_SINGLE_CLAUSE_MASK \
10399 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
10400 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
10401 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
10402 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
10403
10404 static tree
10405 c_parser_omp_single (location_t loc, c_parser *parser)
10406 {
10407 tree stmt = make_node (OMP_SINGLE);
10408 SET_EXPR_LOCATION (stmt, loc);
10409 TREE_TYPE (stmt) = void_type_node;
10410
10411 OMP_SINGLE_CLAUSES (stmt)
10412 = c_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
10413 "#pragma omp single");
10414 OMP_SINGLE_BODY (stmt) = c_parser_omp_structured_block (parser);
10415
10416 return add_stmt (stmt);
10417 }
10418
10419 /* OpenMP 3.0:
10420 # pragma omp task task-clause[optseq] new-line
10421
10422 LOC is the location of the #pragma.
10423 */
10424
10425 #define OMP_TASK_CLAUSE_MASK \
10426 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
10427 | (1u << PRAGMA_OMP_CLAUSE_UNTIED) \
10428 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
10429 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
10430 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
10431 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
10432 | (1u << PRAGMA_OMP_CLAUSE_FINAL) \
10433 | (1u << PRAGMA_OMP_CLAUSE_MERGEABLE))
10434
10435 static tree
10436 c_parser_omp_task (location_t loc, c_parser *parser)
10437 {
10438 tree clauses, block;
10439
10440 clauses = c_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
10441 "#pragma omp task");
10442
10443 block = c_begin_omp_task ();
10444 c_parser_statement (parser);
10445 return c_finish_omp_task (loc, clauses, block);
10446 }
10447
10448 /* OpenMP 3.0:
10449 # pragma omp taskwait new-line
10450 */
10451
10452 static void
10453 c_parser_omp_taskwait (c_parser *parser)
10454 {
10455 location_t loc = c_parser_peek_token (parser)->location;
10456 c_parser_consume_pragma (parser);
10457 c_parser_skip_to_pragma_eol (parser);
10458
10459 c_finish_omp_taskwait (loc);
10460 }
10461
10462 /* OpenMP 3.1:
10463 # pragma omp taskyield new-line
10464 */
10465
10466 static void
10467 c_parser_omp_taskyield (c_parser *parser)
10468 {
10469 location_t loc = c_parser_peek_token (parser)->location;
10470 c_parser_consume_pragma (parser);
10471 c_parser_skip_to_pragma_eol (parser);
10472
10473 c_finish_omp_taskyield (loc);
10474 }
10475
10476 /* Main entry point to parsing most OpenMP pragmas. */
10477
10478 static void
10479 c_parser_omp_construct (c_parser *parser)
10480 {
10481 enum pragma_kind p_kind;
10482 location_t loc;
10483 tree stmt;
10484
10485 loc = c_parser_peek_token (parser)->location;
10486 p_kind = c_parser_peek_token (parser)->pragma_kind;
10487 c_parser_consume_pragma (parser);
10488
10489 switch (p_kind)
10490 {
10491 case PRAGMA_OMP_ATOMIC:
10492 c_parser_omp_atomic (loc, parser);
10493 return;
10494 case PRAGMA_OMP_CRITICAL:
10495 stmt = c_parser_omp_critical (loc, parser);
10496 break;
10497 case PRAGMA_OMP_FOR:
10498 stmt = c_parser_omp_for (loc, parser);
10499 break;
10500 case PRAGMA_OMP_MASTER:
10501 stmt = c_parser_omp_master (loc, parser);
10502 break;
10503 case PRAGMA_OMP_ORDERED:
10504 stmt = c_parser_omp_ordered (loc, parser);
10505 break;
10506 case PRAGMA_OMP_PARALLEL:
10507 stmt = c_parser_omp_parallel (loc, parser);
10508 break;
10509 case PRAGMA_OMP_SECTIONS:
10510 stmt = c_parser_omp_sections (loc, parser);
10511 break;
10512 case PRAGMA_OMP_SINGLE:
10513 stmt = c_parser_omp_single (loc, parser);
10514 break;
10515 case PRAGMA_OMP_TASK:
10516 stmt = c_parser_omp_task (loc, parser);
10517 break;
10518 default:
10519 gcc_unreachable ();
10520 }
10521
10522 if (stmt)
10523 gcc_assert (EXPR_LOCATION (stmt) != UNKNOWN_LOCATION);
10524 }
10525
10526
10527 /* OpenMP 2.5:
10528 # pragma omp threadprivate (variable-list) */
10529
10530 static void
10531 c_parser_omp_threadprivate (c_parser *parser)
10532 {
10533 tree vars, t;
10534 location_t loc;
10535
10536 c_parser_consume_pragma (parser);
10537 loc = c_parser_peek_token (parser)->location;
10538 vars = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
10539
10540 /* Mark every variable in VARS to be assigned thread local storage. */
10541 for (t = vars; t; t = TREE_CHAIN (t))
10542 {
10543 tree v = TREE_PURPOSE (t);
10544
10545 /* FIXME diagnostics: Ideally we should keep individual
10546 locations for all the variables in the var list to make the
10547 following errors more precise. Perhaps
10548 c_parser_omp_var_list_parens() should construct a list of
10549 locations to go along with the var list. */
10550
10551 /* If V had already been marked threadprivate, it doesn't matter
10552 whether it had been used prior to this point. */
10553 if (TREE_CODE (v) != VAR_DECL)
10554 error_at (loc, "%qD is not a variable", v);
10555 else if (TREE_USED (v) && !C_DECL_THREADPRIVATE_P (v))
10556 error_at (loc, "%qE declared %<threadprivate%> after first use", v);
10557 else if (! TREE_STATIC (v) && ! DECL_EXTERNAL (v))
10558 error_at (loc, "automatic variable %qE cannot be %<threadprivate%>", v);
10559 else if (TREE_TYPE (v) == error_mark_node)
10560 ;
10561 else if (! COMPLETE_TYPE_P (TREE_TYPE (v)))
10562 error_at (loc, "%<threadprivate%> %qE has incomplete type", v);
10563 else
10564 {
10565 if (! DECL_THREAD_LOCAL_P (v))
10566 {
10567 DECL_TLS_MODEL (v) = decl_default_tls_model (v);
10568 /* If rtl has been already set for this var, call
10569 make_decl_rtl once again, so that encode_section_info
10570 has a chance to look at the new decl flags. */
10571 if (DECL_RTL_SET_P (v))
10572 make_decl_rtl (v);
10573 }
10574 C_DECL_THREADPRIVATE_P (v) = 1;
10575 }
10576 }
10577
10578 c_parser_skip_to_pragma_eol (parser);
10579 }
10580
10581 /* Parse a transaction attribute (GCC Extension).
10582
10583 transaction-attribute:
10584 attributes
10585 [ [ any-word ] ]
10586
10587 The transactional memory language description is written for C++,
10588 and uses the C++0x attribute syntax. For compatibility, allow the
10589 bracket style for transactions in C as well. */
10590
10591 static tree
10592 c_parser_transaction_attributes (c_parser *parser)
10593 {
10594 tree attr_name, attr = NULL;
10595
10596 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
10597 return c_parser_attributes (parser);
10598
10599 if (!c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
10600 return NULL_TREE;
10601 c_parser_consume_token (parser);
10602 if (!c_parser_require (parser, CPP_OPEN_SQUARE, "expected %<[%>"))
10603 goto error1;
10604
10605 attr_name = c_parser_attribute_any_word (parser);
10606 if (attr_name)
10607 {
10608 c_parser_consume_token (parser);
10609 attr = build_tree_list (attr_name, NULL_TREE);
10610 }
10611 else
10612 c_parser_error (parser, "expected identifier");
10613
10614 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, "expected %<]%>");
10615 error1:
10616 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, "expected %<]%>");
10617 return attr;
10618 }
10619
10620 /* Parse a __transaction_atomic or __transaction_relaxed statement
10621 (GCC Extension).
10622
10623 transaction-statement:
10624 __transaction_atomic transaction-attribute[opt] compound-statement
10625 __transaction_relaxed compound-statement
10626
10627 Note that the only valid attribute is: "outer".
10628 */
10629
10630 static tree
10631 c_parser_transaction (c_parser *parser, enum rid keyword)
10632 {
10633 unsigned int old_in = parser->in_transaction;
10634 unsigned int this_in = 1, new_in;
10635 location_t loc = c_parser_peek_token (parser)->location;
10636 tree stmt, attrs;
10637
10638 gcc_assert ((keyword == RID_TRANSACTION_ATOMIC
10639 || keyword == RID_TRANSACTION_RELAXED)
10640 && c_parser_next_token_is_keyword (parser, keyword));
10641 c_parser_consume_token (parser);
10642
10643 if (keyword == RID_TRANSACTION_RELAXED)
10644 this_in |= TM_STMT_ATTR_RELAXED;
10645 else
10646 {
10647 attrs = c_parser_transaction_attributes (parser);
10648 if (attrs)
10649 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
10650 }
10651
10652 /* Keep track if we're in the lexical scope of an outer transaction. */
10653 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
10654
10655 parser->in_transaction = new_in;
10656 stmt = c_parser_compound_statement (parser);
10657 parser->in_transaction = old_in;
10658
10659 if (flag_tm)
10660 stmt = c_finish_transaction (loc, stmt, this_in);
10661 else
10662 error_at (loc, (keyword == RID_TRANSACTION_ATOMIC ?
10663 "%<__transaction_atomic%> without transactional memory support enabled"
10664 : "%<__transaction_relaxed %> "
10665 "without transactional memory support enabled"));
10666
10667 return stmt;
10668 }
10669
10670 /* Parse a __transaction_atomic or __transaction_relaxed expression
10671 (GCC Extension).
10672
10673 transaction-expression:
10674 __transaction_atomic ( expression )
10675 __transaction_relaxed ( expression )
10676 */
10677
10678 static struct c_expr
10679 c_parser_transaction_expression (c_parser *parser, enum rid keyword)
10680 {
10681 struct c_expr ret;
10682 unsigned int old_in = parser->in_transaction;
10683 unsigned int this_in = 1;
10684 location_t loc = c_parser_peek_token (parser)->location;
10685 tree attrs;
10686
10687 gcc_assert ((keyword == RID_TRANSACTION_ATOMIC
10688 || keyword == RID_TRANSACTION_RELAXED)
10689 && c_parser_next_token_is_keyword (parser, keyword));
10690 c_parser_consume_token (parser);
10691
10692 if (keyword == RID_TRANSACTION_RELAXED)
10693 this_in |= TM_STMT_ATTR_RELAXED;
10694 else
10695 {
10696 attrs = c_parser_transaction_attributes (parser);
10697 if (attrs)
10698 this_in |= parse_tm_stmt_attr (attrs, 0);
10699 }
10700
10701 parser->in_transaction = this_in;
10702 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
10703 {
10704 tree expr = c_parser_expression (parser).value;
10705 ret.original_type = TREE_TYPE (expr);
10706 ret.value = build1 (TRANSACTION_EXPR, ret.original_type, expr);
10707 if (this_in & TM_STMT_ATTR_RELAXED)
10708 TRANSACTION_EXPR_RELAXED (ret.value) = 1;
10709 SET_EXPR_LOCATION (ret.value, loc);
10710 ret.original_code = TRANSACTION_EXPR;
10711 }
10712 else
10713 {
10714 c_parser_error (parser, "expected %<(%>");
10715 ret.value = error_mark_node;
10716 ret.original_code = ERROR_MARK;
10717 ret.original_type = NULL;
10718 }
10719 parser->in_transaction = old_in;
10720
10721 if (!flag_tm)
10722 error_at (loc, (keyword == RID_TRANSACTION_ATOMIC ?
10723 "%<__transaction_atomic%> without transactional memory support enabled"
10724 : "%<__transaction_relaxed %> "
10725 "without transactional memory support enabled"));
10726
10727 return ret;
10728 }
10729
10730 /* Parse a __transaction_cancel statement (GCC Extension).
10731
10732 transaction-cancel-statement:
10733 __transaction_cancel transaction-attribute[opt] ;
10734
10735 Note that the only valid attribute is "outer".
10736 */
10737
10738 static tree
10739 c_parser_transaction_cancel(c_parser *parser)
10740 {
10741 location_t loc = c_parser_peek_token (parser)->location;
10742 tree attrs;
10743 bool is_outer = false;
10744
10745 gcc_assert (c_parser_next_token_is_keyword (parser, RID_TRANSACTION_CANCEL));
10746 c_parser_consume_token (parser);
10747
10748 attrs = c_parser_transaction_attributes (parser);
10749 if (attrs)
10750 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
10751
10752 if (!flag_tm)
10753 {
10754 error_at (loc, "%<__transaction_cancel%> without "
10755 "transactional memory support enabled");
10756 goto ret_error;
10757 }
10758 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
10759 {
10760 error_at (loc, "%<__transaction_cancel%> within a "
10761 "%<__transaction_relaxed%>");
10762 goto ret_error;
10763 }
10764 else if (is_outer)
10765 {
10766 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
10767 && !is_tm_may_cancel_outer (current_function_decl))
10768 {
10769 error_at (loc, "outer %<__transaction_cancel%> not "
10770 "within outer %<__transaction_atomic%>");
10771 error_at (loc, " or a %<transaction_may_cancel_outer%> function");
10772 goto ret_error;
10773 }
10774 }
10775 else if (parser->in_transaction == 0)
10776 {
10777 error_at (loc, "%<__transaction_cancel%> not within "
10778 "%<__transaction_atomic%>");
10779 goto ret_error;
10780 }
10781
10782 return add_stmt (build_tm_abort_call (loc, is_outer));
10783
10784 ret_error:
10785 return build1 (NOP_EXPR, void_type_node, error_mark_node);
10786 }
10787 \f
10788 /* Parse a single source file. */
10789
10790 void
10791 c_parse_file (void)
10792 {
10793 /* Use local storage to begin. If the first token is a pragma, parse it.
10794 If it is #pragma GCC pch_preprocess, then this will load a PCH file
10795 which will cause garbage collection. */
10796 c_parser tparser;
10797
10798 memset (&tparser, 0, sizeof tparser);
10799 the_parser = &tparser;
10800
10801 if (c_parser_peek_token (&tparser)->pragma_kind == PRAGMA_GCC_PCH_PREPROCESS)
10802 c_parser_pragma_pch_preprocess (&tparser);
10803
10804 the_parser = ggc_alloc_c_parser ();
10805 *the_parser = tparser;
10806
10807 /* Initialize EH, if we've been told to do so. */
10808 if (flag_exceptions)
10809 using_eh_for_cleanups ();
10810
10811 c_parser_translation_unit (the_parser);
10812 the_parser = NULL;
10813 }
10814
10815 #include "gt-c-parser.h"