Update gcc-50 to SVN version 222321 (gcc-5-branch)
[dragonfly.git] / contrib / gcc-5.0 / gcc / c / c-parser.c
CommitLineData
dda118e3
JM
1/* Parser for C and Objective-C.
2 Copyright (C) 1987-2015 Free Software Foundation, Inc.
3
4 Parser actions based on the old Bison parser; structure somewhat
5 influenced by and fragments based on the C++ parser.
6
7This file is part of GCC.
8
9GCC is free software; you can redistribute it and/or modify it under
10the terms of the GNU General Public License as published by the Free
11Software Foundation; either version 3, or (at your option) any later
12version.
13
14GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15WARRANTY; without even the implied warranty of MERCHANTABILITY or
16FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17for more details.
18
19You should have received a copy of the GNU General Public License
20along with GCC; see the file COPYING3. If not see
21<http://www.gnu.org/licenses/>. */
22
23/* TODO:
24
25 Make sure all relevant comments, and all relevant code from all
26 actions, brought over from old parser. Verify exact correspondence
27 of syntax accepted.
28
29 Add testcases covering every input symbol in every state in old and
30 new parsers.
31
32 Include full syntax for GNU C, including erroneous cases accepted
33 with error messages, in syntax productions in comments.
34
35 Make more diagnostics in the front end generally take an explicit
36 location rather than implicitly using input_location. */
37
38#include "config.h"
39#include "system.h"
40#include "coretypes.h"
41#include "tm.h" /* For rtl.h: needs enum reg_class. */
42#include "hash-set.h"
43#include "vec.h"
44#include "symtab.h"
45#include "input.h"
46#include "alias.h"
47#include "double-int.h"
48#include "machmode.h"
49#include "flags.h"
50#include "inchash.h"
51#include "tree.h"
52#include "fold-const.h"
53#include "stringpool.h"
54#include "attribs.h"
55#include "stor-layout.h"
56#include "varasm.h"
57#include "trans-mem.h"
58#include "langhooks.h"
59#include "input.h"
60#include "cpplib.h"
61#include "timevar.h"
62#include "c-family/c-pragma.h"
63#include "c-tree.h"
64#include "c-lang.h"
65#include "flags.h"
66#include "ggc.h"
67#include "c-family/c-common.h"
68#include "c-family/c-objc.h"
69#include "vec.h"
70#include "target.h"
71#include "hash-map.h"
72#include "is-a.h"
73#include "plugin-api.h"
74#include "hashtab.h"
75#include "hash-set.h"
76#include "machmode.h"
77#include "hard-reg-set.h"
78#include "function.h"
79#include "ipa-ref.h"
80#include "cgraph.h"
81#include "plugin.h"
82#include "omp-low.h"
83#include "builtins.h"
84#include "gomp-constants.h"
85
86\f
87/* Initialization routine for this file. */
88
89void
90c_parse_init (void)
91{
92 /* The only initialization required is of the reserved word
93 identifiers. */
94 unsigned int i;
95 tree id;
96 int mask = 0;
97
98 /* Make sure RID_MAX hasn't grown past the 8 bits used to hold the keyword in
99 the c_token structure. */
100 gcc_assert (RID_MAX <= 255);
101
102 mask |= D_CXXONLY;
103 if (!flag_isoc99)
104 mask |= D_C99;
105 if (flag_no_asm)
106 {
107 mask |= D_ASM | D_EXT;
108 if (!flag_isoc99)
109 mask |= D_EXT89;
110 }
111 if (!c_dialect_objc ())
112 mask |= D_OBJC | D_CXX_OBJC;
113
114 ridpointers = ggc_cleared_vec_alloc<tree> ((int) RID_MAX);
115 for (i = 0; i < num_c_common_reswords; i++)
116 {
117 /* If a keyword is disabled, do not enter it into the table
118 and so create a canonical spelling that isn't a keyword. */
119 if (c_common_reswords[i].disable & mask)
120 {
121 if (warn_cxx_compat
122 && (c_common_reswords[i].disable & D_CXXWARN))
123 {
124 id = get_identifier (c_common_reswords[i].word);
125 C_SET_RID_CODE (id, RID_CXX_COMPAT_WARN);
126 C_IS_RESERVED_WORD (id) = 1;
127 }
128 continue;
129 }
130
131 id = get_identifier (c_common_reswords[i].word);
132 C_SET_RID_CODE (id, c_common_reswords[i].rid);
133 C_IS_RESERVED_WORD (id) = 1;
134 ridpointers [(int) c_common_reswords[i].rid] = id;
135 }
136
137 for (i = 0; i < NUM_INT_N_ENTS; i++)
138 {
139 /* We always create the symbols but they aren't always supported. */
140 char name[50];
141 sprintf (name, "__int%d", int_n_data[i].bitsize);
f09a3553 142 id = get_identifier (name);
dda118e3
JM
143 C_SET_RID_CODE (id, RID_FIRST_INT_N + i);
144 C_IS_RESERVED_WORD (id) = 1;
145 }
146}
147\f
148/* The C lexer intermediates between the lexer in cpplib and c-lex.c
149 and the C parser. Unlike the C++ lexer, the parser structure
150 stores the lexer information instead of using a separate structure.
151 Identifiers are separated into ordinary identifiers, type names,
152 keywords and some other Objective-C types of identifiers, and some
153 look-ahead is maintained.
154
155 ??? It might be a good idea to lex the whole file up front (as for
156 C++). It would then be possible to share more of the C and C++
157 lexer code, if desired. */
158
159/* More information about the type of a CPP_NAME token. */
160typedef enum c_id_kind {
161 /* An ordinary identifier. */
162 C_ID_ID,
163 /* An identifier declared as a typedef name. */
164 C_ID_TYPENAME,
165 /* An identifier declared as an Objective-C class name. */
166 C_ID_CLASSNAME,
167 /* An address space identifier. */
168 C_ID_ADDRSPACE,
169 /* Not an identifier. */
170 C_ID_NONE
171} c_id_kind;
172
173/* A single C token after string literal concatenation and conversion
174 of preprocessing tokens to tokens. */
175typedef struct GTY (()) c_token {
176 /* The kind of token. */
177 ENUM_BITFIELD (cpp_ttype) type : 8;
178 /* If this token is a CPP_NAME, this value indicates whether also
179 declared as some kind of type. Otherwise, it is C_ID_NONE. */
180 ENUM_BITFIELD (c_id_kind) id_kind : 8;
181 /* If this token is a keyword, this value indicates which keyword.
182 Otherwise, this value is RID_MAX. */
183 ENUM_BITFIELD (rid) keyword : 8;
184 /* If this token is a CPP_PRAGMA, this indicates the pragma that
185 was seen. Otherwise it is PRAGMA_NONE. */
186 ENUM_BITFIELD (pragma_kind) pragma_kind : 8;
187 /* The location at which this token was found. */
188 location_t location;
189 /* The value associated with this token, if any. */
190 tree value;
191} c_token;
192
193/* A parser structure recording information about the state and
194 context of parsing. Includes lexer information with up to two
195 tokens of look-ahead; more are not needed for C. */
196typedef struct GTY(()) c_parser {
197 /* The look-ahead tokens. */
198 c_token * GTY((skip)) tokens;
199 /* Buffer for look-ahead tokens. */
200 c_token tokens_buf[2];
201 /* How many look-ahead tokens are available (0, 1 or 2, or
202 more if parsing from pre-lexed tokens). */
203 unsigned int tokens_avail;
204 /* True if a syntax error is being recovered from; false otherwise.
205 c_parser_error sets this flag. It should clear this flag when
206 enough tokens have been consumed to recover from the error. */
207 BOOL_BITFIELD error : 1;
208 /* True if we're processing a pragma, and shouldn't automatically
209 consume CPP_PRAGMA_EOL. */
210 BOOL_BITFIELD in_pragma : 1;
211 /* True if we're parsing the outermost block of an if statement. */
212 BOOL_BITFIELD in_if_block : 1;
213 /* True if we want to lex an untranslated string. */
214 BOOL_BITFIELD lex_untranslated_string : 1;
215
216 /* Objective-C specific parser/lexer information. */
217
218 /* True if we are in a context where the Objective-C "PQ" keywords
219 are considered keywords. */
220 BOOL_BITFIELD objc_pq_context : 1;
221 /* True if we are parsing a (potential) Objective-C foreach
222 statement. This is set to true after we parsed 'for (' and while
223 we wait for 'in' or ';' to decide if it's a standard C for loop or an
224 Objective-C foreach loop. */
225 BOOL_BITFIELD objc_could_be_foreach_context : 1;
226 /* The following flag is needed to contextualize Objective-C lexical
227 analysis. In some cases (e.g., 'int NSObject;'), it is
228 undesirable to bind an identifier to an Objective-C class, even
229 if a class with that name exists. */
230 BOOL_BITFIELD objc_need_raw_identifier : 1;
231 /* Nonzero if we're processing a __transaction statement. The value
232 is 1 | TM_STMT_ATTR_*. */
233 unsigned int in_transaction : 4;
234 /* True if we are in a context where the Objective-C "Property attribute"
235 keywords are valid. */
236 BOOL_BITFIELD objc_property_attr_context : 1;
237
238 /* Cilk Plus specific parser/lexer information. */
239
240 /* Buffer to hold all the tokens from parsing the vector attribute for the
241 SIMD-enabled functions (formerly known as elemental functions). */
242 vec <c_token, va_gc> *cilk_simd_fn_tokens;
243} c_parser;
244
245
246/* The actual parser and external interface. ??? Does this need to be
247 garbage-collected? */
248
249static GTY (()) c_parser *the_parser;
250
251/* Read in and lex a single token, storing it in *TOKEN. */
252
253static void
254c_lex_one_token (c_parser *parser, c_token *token)
255{
256 timevar_push (TV_LEX);
257
258 token->type = c_lex_with_flags (&token->value, &token->location, NULL,
259 (parser->lex_untranslated_string
260 ? C_LEX_STRING_NO_TRANSLATE : 0));
261 token->id_kind = C_ID_NONE;
262 token->keyword = RID_MAX;
263 token->pragma_kind = PRAGMA_NONE;
264
265 switch (token->type)
266 {
267 case CPP_NAME:
268 {
269 tree decl;
270
271 bool objc_force_identifier = parser->objc_need_raw_identifier;
272 if (c_dialect_objc ())
273 parser->objc_need_raw_identifier = false;
274
275 if (C_IS_RESERVED_WORD (token->value))
276 {
277 enum rid rid_code = C_RID_CODE (token->value);
278
279 if (rid_code == RID_CXX_COMPAT_WARN)
280 {
281 warning_at (token->location,
282 OPT_Wc___compat,
283 "identifier %qE conflicts with C++ keyword",
284 token->value);
285 }
286 else if (rid_code >= RID_FIRST_ADDR_SPACE
287 && rid_code <= RID_LAST_ADDR_SPACE)
288 {
289 token->id_kind = C_ID_ADDRSPACE;
290 token->keyword = rid_code;
291 break;
292 }
293 else if (c_dialect_objc () && OBJC_IS_PQ_KEYWORD (rid_code))
294 {
295 /* We found an Objective-C "pq" keyword (in, out,
296 inout, bycopy, byref, oneway). They need special
297 care because the interpretation depends on the
298 context. */
299 if (parser->objc_pq_context)
300 {
301 token->type = CPP_KEYWORD;
302 token->keyword = rid_code;
303 break;
304 }
305 else if (parser->objc_could_be_foreach_context
306 && rid_code == RID_IN)
307 {
308 /* We are in Objective-C, inside a (potential)
309 foreach context (which means after having
310 parsed 'for (', but before having parsed ';'),
311 and we found 'in'. We consider it the keyword
312 which terminates the declaration at the
313 beginning of a foreach-statement. Note that
314 this means you can't use 'in' for anything else
315 in that context; in particular, in Objective-C
316 you can't use 'in' as the name of the running
317 variable in a C for loop. We could potentially
318 try to add code here to disambiguate, but it
319 seems a reasonable limitation. */
320 token->type = CPP_KEYWORD;
321 token->keyword = rid_code;
322 break;
323 }
324 /* Else, "pq" keywords outside of the "pq" context are
325 not keywords, and we fall through to the code for
326 normal tokens. */
327 }
328 else if (c_dialect_objc () && OBJC_IS_PATTR_KEYWORD (rid_code))
329 {
330 /* We found an Objective-C "property attribute"
331 keyword (getter, setter, readonly, etc). These are
332 only valid in the property context. */
333 if (parser->objc_property_attr_context)
334 {
335 token->type = CPP_KEYWORD;
336 token->keyword = rid_code;
337 break;
338 }
339 /* Else they are not special keywords.
340 */
341 }
342 else if (c_dialect_objc ()
343 && (OBJC_IS_AT_KEYWORD (rid_code)
344 || OBJC_IS_CXX_KEYWORD (rid_code)))
345 {
346 /* We found one of the Objective-C "@" keywords (defs,
347 selector, synchronized, etc) or one of the
348 Objective-C "cxx" keywords (class, private,
349 protected, public, try, catch, throw) without a
350 preceding '@' sign. Do nothing and fall through to
351 the code for normal tokens (in C++ we would still
352 consider the CXX ones keywords, but not in C). */
353 ;
354 }
355 else
356 {
357 token->type = CPP_KEYWORD;
358 token->keyword = rid_code;
359 break;
360 }
361 }
362
363 decl = lookup_name (token->value);
364 if (decl)
365 {
366 if (TREE_CODE (decl) == TYPE_DECL)
367 {
368 token->id_kind = C_ID_TYPENAME;
369 break;
370 }
371 }
372 else if (c_dialect_objc ())
373 {
374 tree objc_interface_decl = objc_is_class_name (token->value);
375 /* Objective-C class names are in the same namespace as
376 variables and typedefs, and hence are shadowed by local
377 declarations. */
378 if (objc_interface_decl
379 && (!objc_force_identifier || global_bindings_p ()))
380 {
381 token->value = objc_interface_decl;
382 token->id_kind = C_ID_CLASSNAME;
383 break;
384 }
385 }
386 token->id_kind = C_ID_ID;
387 }
388 break;
389 case CPP_AT_NAME:
390 /* This only happens in Objective-C; it must be a keyword. */
391 token->type = CPP_KEYWORD;
392 switch (C_RID_CODE (token->value))
393 {
394 /* Replace 'class' with '@class', 'private' with '@private',
395 etc. This prevents confusion with the C++ keyword
396 'class', and makes the tokens consistent with other
397 Objective-C 'AT' keywords. For example '@class' is
398 reported as RID_AT_CLASS which is consistent with
399 '@synchronized', which is reported as
400 RID_AT_SYNCHRONIZED.
401 */
402 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
403 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
404 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
405 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
406 case RID_THROW: token->keyword = RID_AT_THROW; break;
407 case RID_TRY: token->keyword = RID_AT_TRY; break;
408 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
409 default: token->keyword = C_RID_CODE (token->value);
410 }
411 break;
412 case CPP_COLON:
413 case CPP_COMMA:
414 case CPP_CLOSE_PAREN:
415 case CPP_SEMICOLON:
416 /* These tokens may affect the interpretation of any identifiers
417 following, if doing Objective-C. */
418 if (c_dialect_objc ())
419 parser->objc_need_raw_identifier = false;
420 break;
421 case CPP_PRAGMA:
422 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
423 token->pragma_kind = (enum pragma_kind) TREE_INT_CST_LOW (token->value);
424 token->value = NULL;
425 break;
426 default:
427 break;
428 }
429 timevar_pop (TV_LEX);
430}
431
432/* Return a pointer to the next token from PARSER, reading it in if
433 necessary. */
434
435static inline c_token *
436c_parser_peek_token (c_parser *parser)
437{
438 if (parser->tokens_avail == 0)
439 {
440 c_lex_one_token (parser, &parser->tokens[0]);
441 parser->tokens_avail = 1;
442 }
443 return &parser->tokens[0];
444}
445
446/* Return true if the next token from PARSER has the indicated
447 TYPE. */
448
449static inline bool
450c_parser_next_token_is (c_parser *parser, enum cpp_ttype type)
451{
452 return c_parser_peek_token (parser)->type == type;
453}
454
455/* Return true if the next token from PARSER does not have the
456 indicated TYPE. */
457
458static inline bool
459c_parser_next_token_is_not (c_parser *parser, enum cpp_ttype type)
460{
461 return !c_parser_next_token_is (parser, type);
462}
463
464/* Return true if the next token from PARSER is the indicated
465 KEYWORD. */
466
467static inline bool
468c_parser_next_token_is_keyword (c_parser *parser, enum rid keyword)
469{
470 return c_parser_peek_token (parser)->keyword == keyword;
471}
472
473/* Return a pointer to the next-but-one token from PARSER, reading it
474 in if necessary. The next token is already read in. */
475
476static c_token *
477c_parser_peek_2nd_token (c_parser *parser)
478{
479 if (parser->tokens_avail >= 2)
480 return &parser->tokens[1];
481 gcc_assert (parser->tokens_avail == 1);
482 gcc_assert (parser->tokens[0].type != CPP_EOF);
483 gcc_assert (parser->tokens[0].type != CPP_PRAGMA_EOL);
484 c_lex_one_token (parser, &parser->tokens[1]);
485 parser->tokens_avail = 2;
486 return &parser->tokens[1];
487}
488
489/* Return true if TOKEN can start a type name,
490 false otherwise. */
491static bool
492c_token_starts_typename (c_token *token)
493{
494 switch (token->type)
495 {
496 case CPP_NAME:
497 switch (token->id_kind)
498 {
499 case C_ID_ID:
500 return false;
501 case C_ID_ADDRSPACE:
502 return true;
503 case C_ID_TYPENAME:
504 return true;
505 case C_ID_CLASSNAME:
506 gcc_assert (c_dialect_objc ());
507 return true;
508 default:
509 gcc_unreachable ();
510 }
511 case CPP_KEYWORD:
512 switch (token->keyword)
513 {
514 case RID_UNSIGNED:
515 case RID_LONG:
516 case RID_SHORT:
517 case RID_SIGNED:
518 case RID_COMPLEX:
519 case RID_INT:
520 case RID_CHAR:
521 case RID_FLOAT:
522 case RID_DOUBLE:
523 case RID_VOID:
524 case RID_DFLOAT32:
525 case RID_DFLOAT64:
526 case RID_DFLOAT128:
527 case RID_BOOL:
528 case RID_ENUM:
529 case RID_STRUCT:
530 case RID_UNION:
531 case RID_TYPEOF:
532 case RID_CONST:
533 case RID_ATOMIC:
534 case RID_VOLATILE:
535 case RID_RESTRICT:
536 case RID_ATTRIBUTE:
537 case RID_FRACT:
538 case RID_ACCUM:
539 case RID_SAT:
540 case RID_AUTO_TYPE:
541 return true;
542 default:
543 if (token->keyword >= RID_FIRST_INT_N
544 && token->keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
545 && int_n_enabled_p[token->keyword - RID_FIRST_INT_N])
546 return true;
547 return false;
548 }
549 case CPP_LESS:
550 if (c_dialect_objc ())
551 return true;
552 return false;
553 default:
554 return false;
555 }
556}
557
558enum c_lookahead_kind {
559 /* Always treat unknown identifiers as typenames. */
560 cla_prefer_type,
561
562 /* Could be parsing a nonabstract declarator. Only treat an identifier
563 as a typename if followed by another identifier or a star. */
564 cla_nonabstract_decl,
565
566 /* Never treat identifiers as typenames. */
567 cla_prefer_id
568};
569
570/* Return true if the next token from PARSER can start a type name,
571 false otherwise. LA specifies how to do lookahead in order to
572 detect unknown type names. If unsure, pick CLA_PREFER_ID. */
573
574static inline bool
575c_parser_next_tokens_start_typename (c_parser *parser, enum c_lookahead_kind la)
576{
577 c_token *token = c_parser_peek_token (parser);
578 if (c_token_starts_typename (token))
579 return true;
580
581 /* Try a bit harder to detect an unknown typename. */
582 if (la != cla_prefer_id
583 && token->type == CPP_NAME
584 && token->id_kind == C_ID_ID
585
586 /* Do not try too hard when we could have "object in array". */
587 && !parser->objc_could_be_foreach_context
588
589 && (la == cla_prefer_type
590 || c_parser_peek_2nd_token (parser)->type == CPP_NAME
591 || c_parser_peek_2nd_token (parser)->type == CPP_MULT)
592
593 /* Only unknown identifiers. */
594 && !lookup_name (token->value))
595 return true;
596
597 return false;
598}
599
600/* Return true if TOKEN is a type qualifier, false otherwise. */
601static bool
602c_token_is_qualifier (c_token *token)
603{
604 switch (token->type)
605 {
606 case CPP_NAME:
607 switch (token->id_kind)
608 {
609 case C_ID_ADDRSPACE:
610 return true;
611 default:
612 return false;
613 }
614 case CPP_KEYWORD:
615 switch (token->keyword)
616 {
617 case RID_CONST:
618 case RID_VOLATILE:
619 case RID_RESTRICT:
620 case RID_ATTRIBUTE:
621 case RID_ATOMIC:
622 return true;
623 default:
624 return false;
625 }
626 case CPP_LESS:
627 return false;
628 default:
629 gcc_unreachable ();
630 }
631}
632
633/* Return true if the next token from PARSER is a type qualifier,
634 false otherwise. */
635static inline bool
636c_parser_next_token_is_qualifier (c_parser *parser)
637{
638 c_token *token = c_parser_peek_token (parser);
639 return c_token_is_qualifier (token);
640}
641
642/* Return true if TOKEN can start declaration specifiers, false
643 otherwise. */
644static bool
645c_token_starts_declspecs (c_token *token)
646{
647 switch (token->type)
648 {
649 case CPP_NAME:
650 switch (token->id_kind)
651 {
652 case C_ID_ID:
653 return false;
654 case C_ID_ADDRSPACE:
655 return true;
656 case C_ID_TYPENAME:
657 return true;
658 case C_ID_CLASSNAME:
659 gcc_assert (c_dialect_objc ());
660 return true;
661 default:
662 gcc_unreachable ();
663 }
664 case CPP_KEYWORD:
665 switch (token->keyword)
666 {
667 case RID_STATIC:
668 case RID_EXTERN:
669 case RID_REGISTER:
670 case RID_TYPEDEF:
671 case RID_INLINE:
672 case RID_NORETURN:
673 case RID_AUTO:
674 case RID_THREAD:
675 case RID_UNSIGNED:
676 case RID_LONG:
677 case RID_SHORT:
678 case RID_SIGNED:
679 case RID_COMPLEX:
680 case RID_INT:
681 case RID_CHAR:
682 case RID_FLOAT:
683 case RID_DOUBLE:
684 case RID_VOID:
685 case RID_DFLOAT32:
686 case RID_DFLOAT64:
687 case RID_DFLOAT128:
688 case RID_BOOL:
689 case RID_ENUM:
690 case RID_STRUCT:
691 case RID_UNION:
692 case RID_TYPEOF:
693 case RID_CONST:
694 case RID_VOLATILE:
695 case RID_RESTRICT:
696 case RID_ATTRIBUTE:
697 case RID_FRACT:
698 case RID_ACCUM:
699 case RID_SAT:
700 case RID_ALIGNAS:
701 case RID_ATOMIC:
702 case RID_AUTO_TYPE:
703 return true;
704 default:
705 if (token->keyword >= RID_FIRST_INT_N
706 && token->keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
707 && int_n_enabled_p[token->keyword - RID_FIRST_INT_N])
708 return true;
709 return false;
710 }
711 case CPP_LESS:
712 if (c_dialect_objc ())
713 return true;
714 return false;
715 default:
716 return false;
717 }
718}
719
720
721/* Return true if TOKEN can start declaration specifiers or a static
722 assertion, false otherwise. */
723static bool
724c_token_starts_declaration (c_token *token)
725{
726 if (c_token_starts_declspecs (token)
727 || token->keyword == RID_STATIC_ASSERT)
728 return true;
729 else
730 return false;
731}
732
733/* Return true if the next token from PARSER can start declaration
734 specifiers, false otherwise. */
735static inline bool
736c_parser_next_token_starts_declspecs (c_parser *parser)
737{
738 c_token *token = c_parser_peek_token (parser);
739
740 /* In Objective-C, a classname normally starts a declspecs unless it
741 is immediately followed by a dot. In that case, it is the
742 Objective-C 2.0 "dot-syntax" for class objects, ie, calls the
743 setter/getter on the class. c_token_starts_declspecs() can't
744 differentiate between the two cases because it only checks the
745 current token, so we have a special check here. */
746 if (c_dialect_objc ()
747 && token->type == CPP_NAME
748 && token->id_kind == C_ID_CLASSNAME
749 && c_parser_peek_2nd_token (parser)->type == CPP_DOT)
750 return false;
751
752 return c_token_starts_declspecs (token);
753}
754
755/* Return true if the next tokens from PARSER can start declaration
756 specifiers or a static assertion, false otherwise. */
757static inline bool
758c_parser_next_tokens_start_declaration (c_parser *parser)
759{
760 c_token *token = c_parser_peek_token (parser);
761
762 /* Same as above. */
763 if (c_dialect_objc ()
764 && token->type == CPP_NAME
765 && token->id_kind == C_ID_CLASSNAME
766 && c_parser_peek_2nd_token (parser)->type == CPP_DOT)
767 return false;
768
769 /* Labels do not start declarations. */
770 if (token->type == CPP_NAME
771 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
772 return false;
773
774 if (c_token_starts_declaration (token))
775 return true;
776
777 if (c_parser_next_tokens_start_typename (parser, cla_nonabstract_decl))
778 return true;
779
780 return false;
781}
782
783/* Consume the next token from PARSER. */
784
785static void
786c_parser_consume_token (c_parser *parser)
787{
788 gcc_assert (parser->tokens_avail >= 1);
789 gcc_assert (parser->tokens[0].type != CPP_EOF);
790 gcc_assert (!parser->in_pragma || parser->tokens[0].type != CPP_PRAGMA_EOL);
791 gcc_assert (parser->error || parser->tokens[0].type != CPP_PRAGMA);
792 if (parser->tokens != &parser->tokens_buf[0])
793 parser->tokens++;
794 else if (parser->tokens_avail == 2)
795 parser->tokens[0] = parser->tokens[1];
796 parser->tokens_avail--;
797}
798
799/* Expect the current token to be a #pragma. Consume it and remember
800 that we've begun parsing a pragma. */
801
802static void
803c_parser_consume_pragma (c_parser *parser)
804{
805 gcc_assert (!parser->in_pragma);
806 gcc_assert (parser->tokens_avail >= 1);
807 gcc_assert (parser->tokens[0].type == CPP_PRAGMA);
808 if (parser->tokens != &parser->tokens_buf[0])
809 parser->tokens++;
810 else if (parser->tokens_avail == 2)
811 parser->tokens[0] = parser->tokens[1];
812 parser->tokens_avail--;
813 parser->in_pragma = true;
814}
815
816/* Update the global input_location from TOKEN. */
817static inline void
818c_parser_set_source_position_from_token (c_token *token)
819{
820 if (token->type != CPP_EOF)
821 {
822 input_location = token->location;
823 }
824}
825
826/* Issue a diagnostic of the form
827 FILE:LINE: MESSAGE before TOKEN
828 where TOKEN is the next token in the input stream of PARSER.
829 MESSAGE (specified by the caller) is usually of the form "expected
830 OTHER-TOKEN".
831
832 Do not issue a diagnostic if still recovering from an error.
833
834 ??? This is taken from the C++ parser, but building up messages in
835 this way is not i18n-friendly and some other approach should be
836 used. */
837
838static void
839c_parser_error (c_parser *parser, const char *gmsgid)
840{
841 c_token *token = c_parser_peek_token (parser);
842 if (parser->error)
843 return;
844 parser->error = true;
845 if (!gmsgid)
846 return;
847 /* This diagnostic makes more sense if it is tagged to the line of
848 the token we just peeked at. */
849 c_parser_set_source_position_from_token (token);
850 c_parse_error (gmsgid,
851 /* Because c_parse_error does not understand
852 CPP_KEYWORD, keywords are treated like
853 identifiers. */
854 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
855 /* ??? The C parser does not save the cpp flags of a
856 token, we need to pass 0 here and we will not get
857 the source spelling of some tokens but rather the
858 canonical spelling. */
859 token->value, /*flags=*/0);
860}
861
862/* If the next token is of the indicated TYPE, consume it. Otherwise,
863 issue the error MSGID. If MSGID is NULL then a message has already
864 been produced and no message will be produced this time. Returns
865 true if found, false otherwise. */
866
867static bool
868c_parser_require (c_parser *parser,
869 enum cpp_ttype type,
870 const char *msgid)
871{
872 if (c_parser_next_token_is (parser, type))
873 {
874 c_parser_consume_token (parser);
875 return true;
876 }
877 else
878 {
879 c_parser_error (parser, msgid);
880 return false;
881 }
882}
883
884/* If the next token is the indicated keyword, consume it. Otherwise,
885 issue the error MSGID. Returns true if found, false otherwise. */
886
887static bool
888c_parser_require_keyword (c_parser *parser,
889 enum rid keyword,
890 const char *msgid)
891{
892 if (c_parser_next_token_is_keyword (parser, keyword))
893 {
894 c_parser_consume_token (parser);
895 return true;
896 }
897 else
898 {
899 c_parser_error (parser, msgid);
900 return false;
901 }
902}
903
904/* Like c_parser_require, except that tokens will be skipped until the
905 desired token is found. An error message is still produced if the
906 next token is not as expected. If MSGID is NULL then a message has
907 already been produced and no message will be produced this
908 time. */
909
910static void
911c_parser_skip_until_found (c_parser *parser,
912 enum cpp_ttype type,
913 const char *msgid)
914{
915 unsigned nesting_depth = 0;
916
917 if (c_parser_require (parser, type, msgid))
918 return;
919
920 /* Skip tokens until the desired token is found. */
921 while (true)
922 {
923 /* Peek at the next token. */
924 c_token *token = c_parser_peek_token (parser);
925 /* If we've reached the token we want, consume it and stop. */
926 if (token->type == type && !nesting_depth)
927 {
928 c_parser_consume_token (parser);
929 break;
930 }
931
932 /* If we've run out of tokens, stop. */
933 if (token->type == CPP_EOF)
934 return;
935 if (token->type == CPP_PRAGMA_EOL && parser->in_pragma)
936 return;
937 if (token->type == CPP_OPEN_BRACE
938 || token->type == CPP_OPEN_PAREN
939 || token->type == CPP_OPEN_SQUARE)
940 ++nesting_depth;
941 else if (token->type == CPP_CLOSE_BRACE
942 || token->type == CPP_CLOSE_PAREN
943 || token->type == CPP_CLOSE_SQUARE)
944 {
945 if (nesting_depth-- == 0)
946 break;
947 }
948 /* Consume this token. */
949 c_parser_consume_token (parser);
950 }
951 parser->error = false;
952}
953
954/* Skip tokens until the end of a parameter is found, but do not
955 consume the comma, semicolon or closing delimiter. */
956
957static void
958c_parser_skip_to_end_of_parameter (c_parser *parser)
959{
960 unsigned nesting_depth = 0;
961
962 while (true)
963 {
964 c_token *token = c_parser_peek_token (parser);
965 if ((token->type == CPP_COMMA || token->type == CPP_SEMICOLON)
966 && !nesting_depth)
967 break;
968 /* If we've run out of tokens, stop. */
969 if (token->type == CPP_EOF)
970 return;
971 if (token->type == CPP_PRAGMA_EOL && parser->in_pragma)
972 return;
973 if (token->type == CPP_OPEN_BRACE
974 || token->type == CPP_OPEN_PAREN
975 || token->type == CPP_OPEN_SQUARE)
976 ++nesting_depth;
977 else if (token->type == CPP_CLOSE_BRACE
978 || token->type == CPP_CLOSE_PAREN
979 || token->type == CPP_CLOSE_SQUARE)
980 {
981 if (nesting_depth-- == 0)
982 break;
983 }
984 /* Consume this token. */
985 c_parser_consume_token (parser);
986 }
987 parser->error = false;
988}
989
990/* Expect to be at the end of the pragma directive and consume an
991 end of line marker. */
992
993static void
81e26c3d 994c_parser_skip_to_pragma_eol (c_parser *parser, bool error_if_not_eol = true)
dda118e3
JM
995{
996 gcc_assert (parser->in_pragma);
997 parser->in_pragma = false;
998
81e26c3d
JM
999 if (error_if_not_eol && c_parser_peek_token (parser)->type != CPP_PRAGMA_EOL)
1000 c_parser_error (parser, "expected end of line");
1001
1002 cpp_ttype token_type;
1003 do
1004 {
1005 c_token *token = c_parser_peek_token (parser);
1006 token_type = token->type;
1007 if (token_type == CPP_EOF)
1008 break;
1009 c_parser_consume_token (parser);
1010 }
1011 while (token_type != CPP_PRAGMA_EOL);
dda118e3
JM
1012
1013 parser->error = false;
1014}
1015
1016/* Skip tokens until we have consumed an entire block, or until we
1017 have consumed a non-nested ';'. */
1018
1019static void
1020c_parser_skip_to_end_of_block_or_statement (c_parser *parser)
1021{
1022 unsigned nesting_depth = 0;
1023 bool save_error = parser->error;
1024
1025 while (true)
1026 {
1027 c_token *token;
1028
1029 /* Peek at the next token. */
1030 token = c_parser_peek_token (parser);
1031
1032 switch (token->type)
1033 {
1034 case CPP_EOF:
1035 return;
1036
1037 case CPP_PRAGMA_EOL:
1038 if (parser->in_pragma)
1039 return;
1040 break;
1041
1042 case CPP_SEMICOLON:
1043 /* If the next token is a ';', we have reached the
1044 end of the statement. */
1045 if (!nesting_depth)
1046 {
1047 /* Consume the ';'. */
1048 c_parser_consume_token (parser);
1049 goto finished;
1050 }
1051 break;
1052
1053 case CPP_CLOSE_BRACE:
1054 /* If the next token is a non-nested '}', then we have
1055 reached the end of the current block. */
1056 if (nesting_depth == 0 || --nesting_depth == 0)
1057 {
1058 c_parser_consume_token (parser);
1059 goto finished;
1060 }
1061 break;
1062
1063 case CPP_OPEN_BRACE:
1064 /* If it the next token is a '{', then we are entering a new
1065 block. Consume the entire block. */
1066 ++nesting_depth;
1067 break;
1068
1069 case CPP_PRAGMA:
1070 /* If we see a pragma, consume the whole thing at once. We
1071 have some safeguards against consuming pragmas willy-nilly.
1072 Normally, we'd expect to be here with parser->error set,
1073 which disables these safeguards. But it's possible to get
1074 here for secondary error recovery, after parser->error has
1075 been cleared. */
1076 c_parser_consume_pragma (parser);
1077 c_parser_skip_to_pragma_eol (parser);
1078 parser->error = save_error;
1079 continue;
1080
1081 default:
1082 break;
1083 }
1084
1085 c_parser_consume_token (parser);
1086 }
1087
1088 finished:
1089 parser->error = false;
1090}
1091
1092/* CPP's options (initialized by c-opts.c). */
1093extern cpp_options *cpp_opts;
1094
1095/* Save the warning flags which are controlled by __extension__. */
1096
1097static inline int
1098disable_extension_diagnostics (void)
1099{
1100 int ret = (pedantic
1101 | (warn_pointer_arith << 1)
1102 | (warn_traditional << 2)
1103 | (flag_iso << 3)
1104 | (warn_long_long << 4)
1105 | (warn_cxx_compat << 5)
1106 | (warn_overlength_strings << 6)
1107 /* warn_c90_c99_compat has three states: -1/0/1, so we must
1108 play tricks to properly restore it. */
1109 | ((warn_c90_c99_compat == 1) << 7)
1110 | ((warn_c90_c99_compat == -1) << 8)
1111 /* Similarly for warn_c99_c11_compat. */
1112 | ((warn_c99_c11_compat == 1) << 9)
1113 | ((warn_c99_c11_compat == -1) << 10)
1114 );
1115 cpp_opts->cpp_pedantic = pedantic = 0;
1116 warn_pointer_arith = 0;
1117 cpp_opts->cpp_warn_traditional = warn_traditional = 0;
1118 flag_iso = 0;
1119 cpp_opts->cpp_warn_long_long = warn_long_long = 0;
1120 warn_cxx_compat = 0;
1121 warn_overlength_strings = 0;
1122 warn_c90_c99_compat = 0;
1123 warn_c99_c11_compat = 0;
1124 return ret;
1125}
1126
1127/* Restore the warning flags which are controlled by __extension__.
1128 FLAGS is the return value from disable_extension_diagnostics. */
1129
1130static inline void
1131restore_extension_diagnostics (int flags)
1132{
1133 cpp_opts->cpp_pedantic = pedantic = flags & 1;
1134 warn_pointer_arith = (flags >> 1) & 1;
1135 cpp_opts->cpp_warn_traditional = warn_traditional = (flags >> 2) & 1;
1136 flag_iso = (flags >> 3) & 1;
1137 cpp_opts->cpp_warn_long_long = warn_long_long = (flags >> 4) & 1;
1138 warn_cxx_compat = (flags >> 5) & 1;
1139 warn_overlength_strings = (flags >> 6) & 1;
1140 /* See above for why is this needed. */
1141 warn_c90_c99_compat = (flags >> 7) & 1 ? 1 : ((flags >> 8) & 1 ? -1 : 0);
1142 warn_c99_c11_compat = (flags >> 9) & 1 ? 1 : ((flags >> 10) & 1 ? -1 : 0);
1143}
1144
1145/* Possibly kinds of declarator to parse. */
1146typedef enum c_dtr_syn {
1147 /* A normal declarator with an identifier. */
1148 C_DTR_NORMAL,
1149 /* An abstract declarator (maybe empty). */
1150 C_DTR_ABSTRACT,
1151 /* A parameter declarator: may be either, but after a type name does
1152 not redeclare a typedef name as an identifier if it can
1153 alternatively be interpreted as a typedef name; see DR#009,
1154 applied in C90 TC1, omitted from C99 and reapplied in C99 TC2
1155 following DR#249. For example, given a typedef T, "int T" and
1156 "int *T" are valid parameter declarations redeclaring T, while
1157 "int (T)" and "int * (T)" and "int (T[])" and "int (T (int))" are
1158 abstract declarators rather than involving redundant parentheses;
1159 the same applies with attributes inside the parentheses before
1160 "T". */
1161 C_DTR_PARM
1162} c_dtr_syn;
1163
1164/* The binary operation precedence levels, where 0 is a dummy lowest level
1165 used for the bottom of the stack. */
1166enum c_parser_prec {
1167 PREC_NONE,
1168 PREC_LOGOR,
1169 PREC_LOGAND,
1170 PREC_BITOR,
1171 PREC_BITXOR,
1172 PREC_BITAND,
1173 PREC_EQ,
1174 PREC_REL,
1175 PREC_SHIFT,
1176 PREC_ADD,
1177 PREC_MULT,
1178 NUM_PRECS
1179};
1180
1181static void c_parser_external_declaration (c_parser *);
1182static void c_parser_asm_definition (c_parser *);
1183static void c_parser_declaration_or_fndef (c_parser *, bool, bool, bool,
1184 bool, bool, tree *, vec<c_token>);
1185static void c_parser_static_assert_declaration_no_semi (c_parser *);
1186static void c_parser_static_assert_declaration (c_parser *);
1187static void c_parser_declspecs (c_parser *, struct c_declspecs *, bool, bool,
1188 bool, bool, bool, enum c_lookahead_kind);
1189static struct c_typespec c_parser_enum_specifier (c_parser *);
1190static struct c_typespec c_parser_struct_or_union_specifier (c_parser *);
1191static tree c_parser_struct_declaration (c_parser *);
1192static struct c_typespec c_parser_typeof_specifier (c_parser *);
1193static tree c_parser_alignas_specifier (c_parser *);
1194static struct c_declarator *c_parser_declarator (c_parser *, bool, c_dtr_syn,
1195 bool *);
1196static struct c_declarator *c_parser_direct_declarator (c_parser *, bool,
1197 c_dtr_syn, bool *);
1198static struct c_declarator *c_parser_direct_declarator_inner (c_parser *,
1199 bool,
1200 struct c_declarator *);
1201static struct c_arg_info *c_parser_parms_declarator (c_parser *, bool, tree);
1202static struct c_arg_info *c_parser_parms_list_declarator (c_parser *, tree,
1203 tree);
1204static struct c_parm *c_parser_parameter_declaration (c_parser *, tree);
1205static tree c_parser_simple_asm_expr (c_parser *);
1206static tree c_parser_attributes (c_parser *);
1207static struct c_type_name *c_parser_type_name (c_parser *);
1208static struct c_expr c_parser_initializer (c_parser *);
1209static struct c_expr c_parser_braced_init (c_parser *, tree, bool);
1210static void c_parser_initelt (c_parser *, struct obstack *);
1211static void c_parser_initval (c_parser *, struct c_expr *,
1212 struct obstack *);
1213static tree c_parser_compound_statement (c_parser *);
1214static void c_parser_compound_statement_nostart (c_parser *);
1215static void c_parser_label (c_parser *);
1216static void c_parser_statement (c_parser *);
1217static void c_parser_statement_after_labels (c_parser *);
1218static void c_parser_if_statement (c_parser *);
1219static void c_parser_switch_statement (c_parser *);
1220static void c_parser_while_statement (c_parser *, bool);
1221static void c_parser_do_statement (c_parser *, bool);
1222static void c_parser_for_statement (c_parser *, bool);
1223static tree c_parser_asm_statement (c_parser *);
1224static tree c_parser_asm_operands (c_parser *);
1225static tree c_parser_asm_goto_operands (c_parser *);
1226static tree c_parser_asm_clobbers (c_parser *);
1227static struct c_expr c_parser_expr_no_commas (c_parser *, struct c_expr *,
1228 tree = NULL_TREE);
1229static struct c_expr c_parser_conditional_expression (c_parser *,
1230 struct c_expr *, tree);
1231static struct c_expr c_parser_binary_expression (c_parser *, struct c_expr *,
1232 tree);
1233static struct c_expr c_parser_cast_expression (c_parser *, struct c_expr *);
1234static struct c_expr c_parser_unary_expression (c_parser *);
1235static struct c_expr c_parser_sizeof_expression (c_parser *);
1236static struct c_expr c_parser_alignof_expression (c_parser *);
1237static struct c_expr c_parser_postfix_expression (c_parser *);
1238static struct c_expr c_parser_postfix_expression_after_paren_type (c_parser *,
1239 struct c_type_name *,
1240 location_t);
1241static struct c_expr c_parser_postfix_expression_after_primary (c_parser *,
1242 location_t loc,
1243 struct c_expr);
1244static tree c_parser_transaction (c_parser *, enum rid);
1245static struct c_expr c_parser_transaction_expression (c_parser *, enum rid);
1246static tree c_parser_transaction_cancel (c_parser *);
1247static struct c_expr c_parser_expression (c_parser *);
1248static struct c_expr c_parser_expression_conv (c_parser *);
1249static vec<tree, va_gc> *c_parser_expr_list (c_parser *, bool, bool,
1250 vec<tree, va_gc> **, location_t *,
1251 tree *, vec<location_t> *,
1252 unsigned int * = NULL);
1253static void c_parser_oacc_enter_exit_data (c_parser *, bool);
1254static void c_parser_oacc_update (c_parser *);
1255static tree c_parser_oacc_loop (location_t, c_parser *, char *);
1256static void c_parser_omp_construct (c_parser *);
1257static void c_parser_omp_threadprivate (c_parser *);
1258static void c_parser_omp_barrier (c_parser *);
1259static void c_parser_omp_flush (c_parser *);
1260static tree c_parser_omp_for_loop (location_t, c_parser *, enum tree_code,
1261 tree, tree *);
1262static void c_parser_omp_taskwait (c_parser *);
1263static void c_parser_omp_taskyield (c_parser *);
1264static void c_parser_omp_cancel (c_parser *);
1265static void c_parser_omp_cancellation_point (c_parser *);
1266
1267enum pragma_context { pragma_external, pragma_struct, pragma_param,
1268 pragma_stmt, pragma_compound };
1269static bool c_parser_pragma (c_parser *, enum pragma_context);
1270static bool c_parser_omp_target (c_parser *, enum pragma_context);
1271static void c_parser_omp_end_declare_target (c_parser *);
1272static void c_parser_omp_declare (c_parser *, enum pragma_context);
1273
1274/* These Objective-C parser functions are only ever called when
1275 compiling Objective-C. */
1276static void c_parser_objc_class_definition (c_parser *, tree);
1277static void c_parser_objc_class_instance_variables (c_parser *);
1278static void c_parser_objc_class_declaration (c_parser *);
1279static void c_parser_objc_alias_declaration (c_parser *);
1280static void c_parser_objc_protocol_definition (c_parser *, tree);
1281static bool c_parser_objc_method_type (c_parser *);
1282static void c_parser_objc_method_definition (c_parser *);
1283static void c_parser_objc_methodprotolist (c_parser *);
1284static void c_parser_objc_methodproto (c_parser *);
1285static tree c_parser_objc_method_decl (c_parser *, bool, tree *, tree *);
1286static tree c_parser_objc_type_name (c_parser *);
1287static tree c_parser_objc_protocol_refs (c_parser *);
1288static void c_parser_objc_try_catch_finally_statement (c_parser *);
1289static void c_parser_objc_synchronized_statement (c_parser *);
1290static tree c_parser_objc_selector (c_parser *);
1291static tree c_parser_objc_selector_arg (c_parser *);
1292static tree c_parser_objc_receiver (c_parser *);
1293static tree c_parser_objc_message_args (c_parser *);
1294static tree c_parser_objc_keywordexpr (c_parser *);
1295static void c_parser_objc_at_property_declaration (c_parser *);
1296static void c_parser_objc_at_synthesize_declaration (c_parser *);
1297static void c_parser_objc_at_dynamic_declaration (c_parser *);
1298static bool c_parser_objc_diagnose_bad_element_prefix
1299 (c_parser *, struct c_declspecs *);
1300
1301/* Cilk Plus supporting routines. */
1302static void c_parser_cilk_simd (c_parser *);
1303static void c_parser_cilk_for (c_parser *, tree);
1304static bool c_parser_cilk_verify_simd (c_parser *, enum pragma_context);
1305static tree c_parser_array_notation (location_t, c_parser *, tree, tree);
1306static tree c_parser_cilk_clause_vectorlength (c_parser *, tree, bool);
1307static void c_parser_cilk_grainsize (c_parser *);
1308
1309/* Parse a translation unit (C90 6.7, C99 6.9).
1310
1311 translation-unit:
1312 external-declarations
1313
1314 external-declarations:
1315 external-declaration
1316 external-declarations external-declaration
1317
1318 GNU extensions:
1319
1320 translation-unit:
1321 empty
1322*/
1323
1324static void
1325c_parser_translation_unit (c_parser *parser)
1326{
1327 if (c_parser_next_token_is (parser, CPP_EOF))
1328 {
1329 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
1330 "ISO C forbids an empty translation unit");
1331 }
1332 else
1333 {
1334 void *obstack_position = obstack_alloc (&parser_obstack, 0);
1335 mark_valid_location_for_stdc_pragma (false);
1336 do
1337 {
1338 ggc_collect ();
1339 c_parser_external_declaration (parser);
1340 obstack_free (&parser_obstack, obstack_position);
1341 }
1342 while (c_parser_next_token_is_not (parser, CPP_EOF));
1343 }
1344}
1345
1346/* Parse an external declaration (C90 6.7, C99 6.9).
1347
1348 external-declaration:
1349 function-definition
1350 declaration
1351
1352 GNU extensions:
1353
1354 external-declaration:
1355 asm-definition
1356 ;
1357 __extension__ external-declaration
1358
1359 Objective-C:
1360
1361 external-declaration:
1362 objc-class-definition
1363 objc-class-declaration
1364 objc-alias-declaration
1365 objc-protocol-definition
1366 objc-method-definition
1367 @end
1368*/
1369
1370static void
1371c_parser_external_declaration (c_parser *parser)
1372{
1373 int ext;
1374 switch (c_parser_peek_token (parser)->type)
1375 {
1376 case CPP_KEYWORD:
1377 switch (c_parser_peek_token (parser)->keyword)
1378 {
1379 case RID_EXTENSION:
1380 ext = disable_extension_diagnostics ();
1381 c_parser_consume_token (parser);
1382 c_parser_external_declaration (parser);
1383 restore_extension_diagnostics (ext);
1384 break;
1385 case RID_ASM:
1386 c_parser_asm_definition (parser);
1387 break;
1388 case RID_AT_INTERFACE:
1389 case RID_AT_IMPLEMENTATION:
1390 gcc_assert (c_dialect_objc ());
1391 c_parser_objc_class_definition (parser, NULL_TREE);
1392 break;
1393 case RID_AT_CLASS:
1394 gcc_assert (c_dialect_objc ());
1395 c_parser_objc_class_declaration (parser);
1396 break;
1397 case RID_AT_ALIAS:
1398 gcc_assert (c_dialect_objc ());
1399 c_parser_objc_alias_declaration (parser);
1400 break;
1401 case RID_AT_PROTOCOL:
1402 gcc_assert (c_dialect_objc ());
1403 c_parser_objc_protocol_definition (parser, NULL_TREE);
1404 break;
1405 case RID_AT_PROPERTY:
1406 gcc_assert (c_dialect_objc ());
1407 c_parser_objc_at_property_declaration (parser);
1408 break;
1409 case RID_AT_SYNTHESIZE:
1410 gcc_assert (c_dialect_objc ());
1411 c_parser_objc_at_synthesize_declaration (parser);
1412 break;
1413 case RID_AT_DYNAMIC:
1414 gcc_assert (c_dialect_objc ());
1415 c_parser_objc_at_dynamic_declaration (parser);
1416 break;
1417 case RID_AT_END:
1418 gcc_assert (c_dialect_objc ());
1419 c_parser_consume_token (parser);
1420 objc_finish_implementation ();
1421 break;
1422 default:
1423 goto decl_or_fndef;
1424 }
1425 break;
1426 case CPP_SEMICOLON:
1427 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
1428 "ISO C does not allow extra %<;%> outside of a function");
1429 c_parser_consume_token (parser);
1430 break;
1431 case CPP_PRAGMA:
1432 mark_valid_location_for_stdc_pragma (true);
1433 c_parser_pragma (parser, pragma_external);
1434 mark_valid_location_for_stdc_pragma (false);
1435 break;
1436 case CPP_PLUS:
1437 case CPP_MINUS:
1438 if (c_dialect_objc ())
1439 {
1440 c_parser_objc_method_definition (parser);
1441 break;
1442 }
1443 /* Else fall through, and yield a syntax error trying to parse
1444 as a declaration or function definition. */
1445 default:
1446 decl_or_fndef:
1447 /* A declaration or a function definition (or, in Objective-C,
1448 an @interface or @protocol with prefix attributes). We can
1449 only tell which after parsing the declaration specifiers, if
1450 any, and the first declarator. */
1451 c_parser_declaration_or_fndef (parser, true, true, true, false, true,
1452 NULL, vNULL);
1453 break;
1454 }
1455}
1456
1457static void c_finish_omp_declare_simd (c_parser *, tree, tree, vec<c_token>);
1458
1459/* Parse a declaration or function definition (C90 6.5, 6.7.1, C99
1460 6.7, 6.9.1). If FNDEF_OK is true, a function definition is
1461 accepted; otherwise (old-style parameter declarations) only other
1462 declarations are accepted. If STATIC_ASSERT_OK is true, a static
1463 assertion is accepted; otherwise (old-style parameter declarations)
1464 it is not. If NESTED is true, we are inside a function or parsing
1465 old-style parameter declarations; any functions encountered are
1466 nested functions and declaration specifiers are required; otherwise
1467 we are at top level and functions are normal functions and
1468 declaration specifiers may be optional. If EMPTY_OK is true, empty
1469 declarations are OK (subject to all other constraints); otherwise
1470 (old-style parameter declarations) they are diagnosed. If
1471 START_ATTR_OK is true, the declaration specifiers may start with
1472 attributes; otherwise they may not.
1473 OBJC_FOREACH_OBJECT_DECLARATION can be used to get back the parsed
1474 declaration when parsing an Objective-C foreach statement.
1475
1476 declaration:
1477 declaration-specifiers init-declarator-list[opt] ;
1478 static_assert-declaration
1479
1480 function-definition:
1481 declaration-specifiers[opt] declarator declaration-list[opt]
1482 compound-statement
1483
1484 declaration-list:
1485 declaration
1486 declaration-list declaration
1487
1488 init-declarator-list:
1489 init-declarator
1490 init-declarator-list , init-declarator
1491
1492 init-declarator:
1493 declarator simple-asm-expr[opt] attributes[opt]
1494 declarator simple-asm-expr[opt] attributes[opt] = initializer
1495
1496 GNU extensions:
1497
1498 nested-function-definition:
1499 declaration-specifiers declarator declaration-list[opt]
1500 compound-statement
1501
1502 Objective-C:
1503 attributes objc-class-definition
1504 attributes objc-category-definition
1505 attributes objc-protocol-definition
1506
1507 The simple-asm-expr and attributes are GNU extensions.
1508
1509 This function does not handle __extension__; that is handled in its
1510 callers. ??? Following the old parser, __extension__ may start
1511 external declarations, declarations in functions and declarations
1512 at the start of "for" loops, but not old-style parameter
1513 declarations.
1514
1515 C99 requires declaration specifiers in a function definition; the
1516 absence is diagnosed through the diagnosis of implicit int. In GNU
1517 C we also allow but diagnose declarations without declaration
1518 specifiers, but only at top level (elsewhere they conflict with
1519 other syntax).
1520
1521 In Objective-C, declarations of the looping variable in a foreach
1522 statement are exceptionally terminated by 'in' (for example, 'for
1523 (NSObject *object in array) { ... }').
1524
1525 OpenMP:
1526
1527 declaration:
1528 threadprivate-directive */
1529
1530static void
1531c_parser_declaration_or_fndef (c_parser *parser, bool fndef_ok,
1532 bool static_assert_ok, bool empty_ok,
1533 bool nested, bool start_attr_ok,
1534 tree *objc_foreach_object_declaration,
1535 vec<c_token> omp_declare_simd_clauses)
1536{
1537 struct c_declspecs *specs;
1538 tree prefix_attrs;
1539 tree all_prefix_attrs;
1540 bool diagnosed_no_specs = false;
1541 location_t here = c_parser_peek_token (parser)->location;
1542
1543 if (static_assert_ok
1544 && c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT))
1545 {
1546 c_parser_static_assert_declaration (parser);
1547 return;
1548 }
1549 specs = build_null_declspecs ();
1550
1551 /* Try to detect an unknown type name when we have "A B" or "A *B". */
1552 if (c_parser_peek_token (parser)->type == CPP_NAME
1553 && c_parser_peek_token (parser)->id_kind == C_ID_ID
1554 && (c_parser_peek_2nd_token (parser)->type == CPP_NAME
1555 || c_parser_peek_2nd_token (parser)->type == CPP_MULT)
1556 && (!nested || !lookup_name (c_parser_peek_token (parser)->value)))
1557 {
1558 error_at (here, "unknown type name %qE",
1559 c_parser_peek_token (parser)->value);
1560
1561 /* Parse declspecs normally to get a correct pointer type, but avoid
1562 a further "fails to be a type name" error. Refuse nested functions
1563 since it is not how the user likely wants us to recover. */
1564 c_parser_peek_token (parser)->type = CPP_KEYWORD;
1565 c_parser_peek_token (parser)->keyword = RID_VOID;
1566 c_parser_peek_token (parser)->value = error_mark_node;
1567 fndef_ok = !nested;
1568 }
1569
1570 c_parser_declspecs (parser, specs, true, true, start_attr_ok,
1571 true, true, cla_nonabstract_decl);
1572 if (parser->error)
1573 {
1574 c_parser_skip_to_end_of_block_or_statement (parser);
1575 return;
1576 }
1577 if (nested && !specs->declspecs_seen_p)
1578 {
1579 c_parser_error (parser, "expected declaration specifiers");
1580 c_parser_skip_to_end_of_block_or_statement (parser);
1581 return;
1582 }
1583 finish_declspecs (specs);
1584 bool auto_type_p = specs->typespec_word == cts_auto_type;
1585 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1586 {
1587 if (auto_type_p)
1588 error_at (here, "%<__auto_type%> in empty declaration");
1589 else if (empty_ok)
1590 shadow_tag (specs);
1591 else
1592 {
1593 shadow_tag_warned (specs, 1);
1594 pedwarn (here, 0, "empty declaration");
1595 }
1596 c_parser_consume_token (parser);
1597 return;
1598 }
1599
1600 /* Provide better error recovery. Note that a type name here is usually
1601 better diagnosed as a redeclaration. */
1602 if (empty_ok
1603 && specs->typespec_kind == ctsk_tagdef
1604 && c_parser_next_token_starts_declspecs (parser)
1605 && !c_parser_next_token_is (parser, CPP_NAME))
1606 {
1607 c_parser_error (parser, "expected %<;%>, identifier or %<(%>");
1608 parser->error = false;
1609 shadow_tag_warned (specs, 1);
1610 return;
1611 }
1612 else if (c_dialect_objc () && !auto_type_p)
1613 {
1614 /* Prefix attributes are an error on method decls. */
1615 switch (c_parser_peek_token (parser)->type)
1616 {
1617 case CPP_PLUS:
1618 case CPP_MINUS:
1619 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1620 return;
1621 if (specs->attrs)
1622 {
1623 warning_at (c_parser_peek_token (parser)->location,
1624 OPT_Wattributes,
1625 "prefix attributes are ignored for methods");
1626 specs->attrs = NULL_TREE;
1627 }
1628 if (fndef_ok)
1629 c_parser_objc_method_definition (parser);
1630 else
1631 c_parser_objc_methodproto (parser);
1632 return;
1633 break;
1634 default:
1635 break;
1636 }
1637 /* This is where we parse 'attributes @interface ...',
1638 'attributes @implementation ...', 'attributes @protocol ...'
1639 (where attributes could be, for example, __attribute__
1640 ((deprecated)).
1641 */
1642 switch (c_parser_peek_token (parser)->keyword)
1643 {
1644 case RID_AT_INTERFACE:
1645 {
1646 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1647 return;
1648 c_parser_objc_class_definition (parser, specs->attrs);
1649 return;
1650 }
1651 break;
1652 case RID_AT_IMPLEMENTATION:
1653 {
1654 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1655 return;
1656 if (specs->attrs)
1657 {
1658 warning_at (c_parser_peek_token (parser)->location,
1659 OPT_Wattributes,
1660 "prefix attributes are ignored for implementations");
1661 specs->attrs = NULL_TREE;
1662 }
1663 c_parser_objc_class_definition (parser, NULL_TREE);
1664 return;
1665 }
1666 break;
1667 case RID_AT_PROTOCOL:
1668 {
1669 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1670 return;
1671 c_parser_objc_protocol_definition (parser, specs->attrs);
1672 return;
1673 }
1674 break;
1675 case RID_AT_ALIAS:
1676 case RID_AT_CLASS:
1677 case RID_AT_END:
1678 case RID_AT_PROPERTY:
1679 if (specs->attrs)
1680 {
1681 c_parser_error (parser, "unexpected attribute");
1682 specs->attrs = NULL;
1683 }
1684 break;
1685 default:
1686 break;
1687 }
1688 }
1689
1690 pending_xref_error ();
1691 prefix_attrs = specs->attrs;
1692 all_prefix_attrs = prefix_attrs;
1693 specs->attrs = NULL_TREE;
1694 while (true)
1695 {
1696 struct c_declarator *declarator;
1697 bool dummy = false;
1698 timevar_id_t tv;
1699 tree fnbody;
1700 /* Declaring either one or more declarators (in which case we
1701 should diagnose if there were no declaration specifiers) or a
1702 function definition (in which case the diagnostic for
1703 implicit int suffices). */
1704 declarator = c_parser_declarator (parser,
1705 specs->typespec_kind != ctsk_none,
1706 C_DTR_NORMAL, &dummy);
1707 if (declarator == NULL)
1708 {
1709 if (omp_declare_simd_clauses.exists ()
1710 || !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
1711 c_finish_omp_declare_simd (parser, NULL_TREE, NULL_TREE,
1712 omp_declare_simd_clauses);
1713 c_parser_skip_to_end_of_block_or_statement (parser);
1714 return;
1715 }
1716 if (auto_type_p && declarator->kind != cdk_id)
1717 {
1718 error_at (here,
1719 "%<__auto_type%> requires a plain identifier"
1720 " as declarator");
1721 c_parser_skip_to_end_of_block_or_statement (parser);
1722 return;
1723 }
1724 if (c_parser_next_token_is (parser, CPP_EQ)
1725 || c_parser_next_token_is (parser, CPP_COMMA)
1726 || c_parser_next_token_is (parser, CPP_SEMICOLON)
1727 || c_parser_next_token_is_keyword (parser, RID_ASM)
1728 || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE)
1729 || c_parser_next_token_is_keyword (parser, RID_IN))
1730 {
1731 tree asm_name = NULL_TREE;
1732 tree postfix_attrs = NULL_TREE;
1733 if (!diagnosed_no_specs && !specs->declspecs_seen_p)
1734 {
1735 diagnosed_no_specs = true;
1736 pedwarn (here, 0, "data definition has no type or storage class");
1737 }
1738 /* Having seen a data definition, there cannot now be a
1739 function definition. */
1740 fndef_ok = false;
1741 if (c_parser_next_token_is_keyword (parser, RID_ASM))
1742 asm_name = c_parser_simple_asm_expr (parser);
1743 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1744 {
1745 postfix_attrs = c_parser_attributes (parser);
1746 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
1747 {
1748 /* This means there is an attribute specifier after
1749 the declarator in a function definition. Provide
1750 some more information for the user. */
1751 error_at (here, "attributes should be specified before the "
1752 "declarator in a function definition");
1753 c_parser_skip_to_end_of_block_or_statement (parser);
1754 return;
1755 }
1756 }
1757 if (c_parser_next_token_is (parser, CPP_EQ))
1758 {
1759 tree d;
1760 struct c_expr init;
1761 location_t init_loc;
1762 c_parser_consume_token (parser);
1763 if (auto_type_p)
1764 {
1765 start_init (NULL_TREE, asm_name, global_bindings_p ());
1766 init_loc = c_parser_peek_token (parser)->location;
1767 init = c_parser_expr_no_commas (parser, NULL);
1768 if (TREE_CODE (init.value) == COMPONENT_REF
1769 && DECL_C_BIT_FIELD (TREE_OPERAND (init.value, 1)))
1770 error_at (here,
1771 "%<__auto_type%> used with a bit-field"
1772 " initializer");
1773 init = convert_lvalue_to_rvalue (init_loc, init, true, true);
1774 tree init_type = TREE_TYPE (init.value);
1775 /* As with typeof, remove all qualifiers from atomic types. */
1776 if (init_type != error_mark_node && TYPE_ATOMIC (init_type))
1777 init_type
1778 = c_build_qualified_type (init_type, TYPE_UNQUALIFIED);
1779 bool vm_type = variably_modified_type_p (init_type,
1780 NULL_TREE);
1781 if (vm_type)
1782 init.value = c_save_expr (init.value);
1783 finish_init ();
1784 specs->typespec_kind = ctsk_typeof;
1785 specs->locations[cdw_typedef] = init_loc;
1786 specs->typedef_p = true;
1787 specs->type = init_type;
1788 if (vm_type)
1789 {
1790 bool maybe_const = true;
1791 tree type_expr = c_fully_fold (init.value, false,
1792 &maybe_const);
1793 specs->expr_const_operands &= maybe_const;
1794 if (specs->expr)
1795 specs->expr = build2 (COMPOUND_EXPR,
1796 TREE_TYPE (type_expr),
1797 specs->expr, type_expr);
1798 else
1799 specs->expr = type_expr;
1800 }
1801 d = start_decl (declarator, specs, true,
1802 chainon (postfix_attrs, all_prefix_attrs));
1803 if (!d)
1804 d = error_mark_node;
1805 if (omp_declare_simd_clauses.exists ()
1806 || !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
1807 c_finish_omp_declare_simd (parser, d, NULL_TREE,
1808 omp_declare_simd_clauses);
1809 }
1810 else
1811 {
1812 /* The declaration of the variable is in effect while
1813 its initializer is parsed. */
1814 d = start_decl (declarator, specs, true,
1815 chainon (postfix_attrs, all_prefix_attrs));
1816 if (!d)
1817 d = error_mark_node;
1818 if (omp_declare_simd_clauses.exists ()
1819 || !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
1820 c_finish_omp_declare_simd (parser, d, NULL_TREE,
1821 omp_declare_simd_clauses);
1822 start_init (d, asm_name, global_bindings_p ());
1823 init_loc = c_parser_peek_token (parser)->location;
1824 init = c_parser_initializer (parser);
1825 finish_init ();
1826 }
1827 if (d != error_mark_node)
1828 {
1829 maybe_warn_string_init (init_loc, TREE_TYPE (d), init);
1830 finish_decl (d, init_loc, init.value,
1831 init.original_type, asm_name);
1832 }
1833 }
1834 else
1835 {
1836 if (auto_type_p)
1837 {
1838 error_at (here,
1839 "%<__auto_type%> requires an initialized "
1840 "data declaration");
1841 c_parser_skip_to_end_of_block_or_statement (parser);
1842 return;
1843 }
1844 tree d = start_decl (declarator, specs, false,
1845 chainon (postfix_attrs,
1846 all_prefix_attrs));
1847 if (omp_declare_simd_clauses.exists ()
1848 || !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
1849 {
1850 tree parms = NULL_TREE;
1851 if (d && TREE_CODE (d) == FUNCTION_DECL)
1852 {
1853 struct c_declarator *ce = declarator;
1854 while (ce != NULL)
1855 if (ce->kind == cdk_function)
1856 {
1857 parms = ce->u.arg_info->parms;
1858 break;
1859 }
1860 else
1861 ce = ce->declarator;
1862 }
1863 if (parms)
1864 temp_store_parm_decls (d, parms);
1865 c_finish_omp_declare_simd (parser, d, parms,
1866 omp_declare_simd_clauses);
1867 if (parms)
1868 temp_pop_parm_decls ();
1869 }
1870 if (d)
1871 finish_decl (d, UNKNOWN_LOCATION, NULL_TREE,
1872 NULL_TREE, asm_name);
1873
1874 if (c_parser_next_token_is_keyword (parser, RID_IN))
1875 {
1876 if (d)
1877 *objc_foreach_object_declaration = d;
1878 else
1879 *objc_foreach_object_declaration = error_mark_node;
1880 }
1881 }
1882 if (c_parser_next_token_is (parser, CPP_COMMA))
1883 {
1884 if (auto_type_p)
1885 {
1886 error_at (here,
1887 "%<__auto_type%> may only be used with"
1888 " a single declarator");
1889 c_parser_skip_to_end_of_block_or_statement (parser);
1890 return;
1891 }
1892 c_parser_consume_token (parser);
1893 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1894 all_prefix_attrs = chainon (c_parser_attributes (parser),
1895 prefix_attrs);
1896 else
1897 all_prefix_attrs = prefix_attrs;
1898 continue;
1899 }
1900 else if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1901 {
1902 c_parser_consume_token (parser);
1903 return;
1904 }
1905 else if (c_parser_next_token_is_keyword (parser, RID_IN))
1906 {
1907 /* This can only happen in Objective-C: we found the
1908 'in' that terminates the declaration inside an
1909 Objective-C foreach statement. Do not consume the
1910 token, so that the caller can use it to determine
1911 that this indeed is a foreach context. */
1912 return;
1913 }
1914 else
1915 {
1916 c_parser_error (parser, "expected %<,%> or %<;%>");
1917 c_parser_skip_to_end_of_block_or_statement (parser);
1918 return;
1919 }
1920 }
1921 else if (auto_type_p)
1922 {
1923 error_at (here,
1924 "%<__auto_type%> requires an initialized data declaration");
1925 c_parser_skip_to_end_of_block_or_statement (parser);
1926 return;
1927 }
1928 else if (!fndef_ok)
1929 {
1930 c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, "
1931 "%<asm%> or %<__attribute__%>");
1932 c_parser_skip_to_end_of_block_or_statement (parser);
1933 return;
1934 }
1935 /* Function definition (nested or otherwise). */
1936 if (nested)
1937 {
1938 pedwarn (here, OPT_Wpedantic, "ISO C forbids nested functions");
1939 c_push_function_context ();
1940 }
1941 if (!start_function (specs, declarator, all_prefix_attrs))
1942 {
1943 /* This can appear in many cases looking nothing like a
1944 function definition, so we don't give a more specific
1945 error suggesting there was one. */
1946 c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, %<asm%> "
1947 "or %<__attribute__%>");
1948 if (nested)
1949 c_pop_function_context ();
1950 break;
1951 }
1952
1953 if (DECL_DECLARED_INLINE_P (current_function_decl))
1954 tv = TV_PARSE_INLINE;
1955 else
1956 tv = TV_PARSE_FUNC;
1957 timevar_push (tv);
1958
1959 /* Parse old-style parameter declarations. ??? Attributes are
1960 not allowed to start declaration specifiers here because of a
1961 syntax conflict between a function declaration with attribute
1962 suffix and a function definition with an attribute prefix on
1963 first old-style parameter declaration. Following the old
1964 parser, they are not accepted on subsequent old-style
1965 parameter declarations either. However, there is no
1966 ambiguity after the first declaration, nor indeed on the
1967 first as long as we don't allow postfix attributes after a
1968 declarator with a nonempty identifier list in a definition;
1969 and postfix attributes have never been accepted here in
1970 function definitions either. */
1971 while (c_parser_next_token_is_not (parser, CPP_EOF)
1972 && c_parser_next_token_is_not (parser, CPP_OPEN_BRACE))
1973 c_parser_declaration_or_fndef (parser, false, false, false,
1974 true, false, NULL, vNULL);
1975 store_parm_decls ();
1976 if (omp_declare_simd_clauses.exists ()
1977 || !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
1978 c_finish_omp_declare_simd (parser, current_function_decl, NULL_TREE,
1979 omp_declare_simd_clauses);
1980 DECL_STRUCT_FUNCTION (current_function_decl)->function_start_locus
1981 = c_parser_peek_token (parser)->location;
1982 fnbody = c_parser_compound_statement (parser);
1983 if (flag_cilkplus && contains_array_notation_expr (fnbody))
1984 fnbody = expand_array_notation_exprs (fnbody);
1985 if (nested)
1986 {
1987 tree decl = current_function_decl;
1988 /* Mark nested functions as needing static-chain initially.
1989 lower_nested_functions will recompute it but the
1990 DECL_STATIC_CHAIN flag is also used before that happens,
1991 by initializer_constant_valid_p. See gcc.dg/nested-fn-2.c. */
1992 DECL_STATIC_CHAIN (decl) = 1;
1993 add_stmt (fnbody);
1994 finish_function ();
1995 c_pop_function_context ();
1996 add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl));
1997 }
1998 else
1999 {
2000 add_stmt (fnbody);
2001 finish_function ();
2002 }
2003
2004 timevar_pop (tv);
2005 break;
2006 }
2007}
2008
2009/* Parse an asm-definition (asm() outside a function body). This is a
2010 GNU extension.
2011
2012 asm-definition:
2013 simple-asm-expr ;
2014*/
2015
2016static void
2017c_parser_asm_definition (c_parser *parser)
2018{
2019 tree asm_str = c_parser_simple_asm_expr (parser);
2020 if (asm_str)
2021 symtab->finalize_toplevel_asm (asm_str);
2022 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
2023}
2024
2025/* Parse a static assertion (C11 6.7.10).
2026
2027 static_assert-declaration:
2028 static_assert-declaration-no-semi ;
2029*/
2030
2031static void
2032c_parser_static_assert_declaration (c_parser *parser)
2033{
2034 c_parser_static_assert_declaration_no_semi (parser);
2035 if (parser->error
2036 || !c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
2037 c_parser_skip_to_end_of_block_or_statement (parser);
2038}
2039
2040/* Parse a static assertion (C11 6.7.10), without the trailing
2041 semicolon.
2042
2043 static_assert-declaration-no-semi:
2044 _Static_assert ( constant-expression , string-literal )
2045*/
2046
2047static void
2048c_parser_static_assert_declaration_no_semi (c_parser *parser)
2049{
2050 location_t assert_loc, value_loc;
2051 tree value;
2052 tree string;
2053
2054 gcc_assert (c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT));
2055 assert_loc = c_parser_peek_token (parser)->location;
2056 if (flag_isoc99)
2057 pedwarn_c99 (assert_loc, OPT_Wpedantic,
2058 "ISO C99 does not support %<_Static_assert%>");
2059 else
2060 pedwarn_c99 (assert_loc, OPT_Wpedantic,
2061 "ISO C90 does not support %<_Static_assert%>");
2062 c_parser_consume_token (parser);
2063 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2064 return;
2065 value_loc = c_parser_peek_token (parser)->location;
2066 value = c_parser_expr_no_commas (parser, NULL).value;
2067 parser->lex_untranslated_string = true;
2068 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
2069 {
2070 parser->lex_untranslated_string = false;
2071 return;
2072 }
2073 switch (c_parser_peek_token (parser)->type)
2074 {
2075 case CPP_STRING:
2076 case CPP_STRING16:
2077 case CPP_STRING32:
2078 case CPP_WSTRING:
2079 case CPP_UTF8STRING:
2080 string = c_parser_peek_token (parser)->value;
2081 c_parser_consume_token (parser);
2082 parser->lex_untranslated_string = false;
2083 break;
2084 default:
2085 c_parser_error (parser, "expected string literal");
2086 parser->lex_untranslated_string = false;
2087 return;
2088 }
2089 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
2090
2091 if (!INTEGRAL_TYPE_P (TREE_TYPE (value)))
2092 {
2093 error_at (value_loc, "expression in static assertion is not an integer");
2094 return;
2095 }
2096 if (TREE_CODE (value) != INTEGER_CST)
2097 {
2098 value = c_fully_fold (value, false, NULL);
2099 /* Strip no-op conversions. */
2100 STRIP_TYPE_NOPS (value);
2101 if (TREE_CODE (value) == INTEGER_CST)
2102 pedwarn (value_loc, OPT_Wpedantic, "expression in static assertion "
2103 "is not an integer constant expression");
2104 }
2105 if (TREE_CODE (value) != INTEGER_CST)
2106 {
2107 error_at (value_loc, "expression in static assertion is not constant");
2108 return;
2109 }
2110 constant_expression_warning (value);
2111 if (integer_zerop (value))
2112 error_at (assert_loc, "static assertion failed: %E", string);
2113}
2114
2115/* Parse some declaration specifiers (possibly none) (C90 6.5, C99
2116 6.7), adding them to SPECS (which may already include some).
2117 Storage class specifiers are accepted iff SCSPEC_OK; type
2118 specifiers are accepted iff TYPESPEC_OK; alignment specifiers are
2119 accepted iff ALIGNSPEC_OK; attributes are accepted at the start
2120 iff START_ATTR_OK; __auto_type is accepted iff AUTO_TYPE_OK.
2121
2122 declaration-specifiers:
2123 storage-class-specifier declaration-specifiers[opt]
2124 type-specifier declaration-specifiers[opt]
2125 type-qualifier declaration-specifiers[opt]
2126 function-specifier declaration-specifiers[opt]
2127 alignment-specifier declaration-specifiers[opt]
2128
2129 Function specifiers (inline) are from C99, and are currently
2130 handled as storage class specifiers, as is __thread. Alignment
2131 specifiers are from C11.
2132
2133 C90 6.5.1, C99 6.7.1:
2134 storage-class-specifier:
2135 typedef
2136 extern
2137 static
2138 auto
2139 register
2140 _Thread_local
2141
2142 (_Thread_local is new in C11.)
2143
2144 C99 6.7.4:
2145 function-specifier:
2146 inline
2147 _Noreturn
2148
2149 (_Noreturn is new in C11.)
2150
2151 C90 6.5.2, C99 6.7.2:
2152 type-specifier:
2153 void
2154 char
2155 short
2156 int
2157 long
2158 float
2159 double
2160 signed
2161 unsigned
2162 _Bool
2163 _Complex
2164 [_Imaginary removed in C99 TC2]
2165 struct-or-union-specifier
2166 enum-specifier
2167 typedef-name
2168 atomic-type-specifier
2169
2170 (_Bool and _Complex are new in C99.)
2171 (atomic-type-specifier is new in C11.)
2172
2173 C90 6.5.3, C99 6.7.3:
2174
2175 type-qualifier:
2176 const
2177 restrict
2178 volatile
2179 address-space-qualifier
2180 _Atomic
2181
2182 (restrict is new in C99.)
2183 (_Atomic is new in C11.)
2184
2185 GNU extensions:
2186
2187 declaration-specifiers:
2188 attributes declaration-specifiers[opt]
2189
2190 type-qualifier:
2191 address-space
2192
2193 address-space:
2194 identifier recognized by the target
2195
2196 storage-class-specifier:
2197 __thread
2198
2199 type-specifier:
2200 typeof-specifier
2201 __auto_type
2202 __intN
2203 _Decimal32
2204 _Decimal64
2205 _Decimal128
2206 _Fract
2207 _Accum
2208 _Sat
2209
2210 (_Fract, _Accum, and _Sat are new from ISO/IEC DTR 18037:
2211 http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1169.pdf)
2212
2213 atomic-type-specifier
2214 _Atomic ( type-name )
2215
2216 Objective-C:
2217
2218 type-specifier:
2219 class-name objc-protocol-refs[opt]
2220 typedef-name objc-protocol-refs
2221 objc-protocol-refs
2222*/
2223
2224static void
2225c_parser_declspecs (c_parser *parser, struct c_declspecs *specs,
2226 bool scspec_ok, bool typespec_ok, bool start_attr_ok,
2227 bool alignspec_ok, bool auto_type_ok,
2228 enum c_lookahead_kind la)
2229{
2230 bool attrs_ok = start_attr_ok;
2231 bool seen_type = specs->typespec_kind != ctsk_none;
2232
2233 if (!typespec_ok)
2234 gcc_assert (la == cla_prefer_id);
2235
2236 while (c_parser_next_token_is (parser, CPP_NAME)
2237 || c_parser_next_token_is (parser, CPP_KEYWORD)
2238 || (c_dialect_objc () && c_parser_next_token_is (parser, CPP_LESS)))
2239 {
2240 struct c_typespec t;
2241 tree attrs;
2242 tree align;
2243 location_t loc = c_parser_peek_token (parser)->location;
2244
2245 /* If we cannot accept a type, exit if the next token must start
2246 one. Also, if we already have seen a tagged definition,
2247 a typename would be an error anyway and likely the user
2248 has simply forgotten a semicolon, so we exit. */
2249 if ((!typespec_ok || specs->typespec_kind == ctsk_tagdef)
2250 && c_parser_next_tokens_start_typename (parser, la)
2251 && !c_parser_next_token_is_qualifier (parser))
2252 break;
2253
2254 if (c_parser_next_token_is (parser, CPP_NAME))
2255 {
2256 c_token *name_token = c_parser_peek_token (parser);
2257 tree value = name_token->value;
2258 c_id_kind kind = name_token->id_kind;
2259
2260 if (kind == C_ID_ADDRSPACE)
2261 {
2262 addr_space_t as
2263 = name_token->keyword - RID_FIRST_ADDR_SPACE;
2264 declspecs_add_addrspace (name_token->location, specs, as);
2265 c_parser_consume_token (parser);
2266 attrs_ok = true;
2267 continue;
2268 }
2269
2270 gcc_assert (!c_parser_next_token_is_qualifier (parser));
2271
2272 /* If we cannot accept a type, and the next token must start one,
2273 exit. Do the same if we already have seen a tagged definition,
2274 since it would be an error anyway and likely the user has simply
2275 forgotten a semicolon. */
2276 if (seen_type || !c_parser_next_tokens_start_typename (parser, la))
2277 break;
2278
2279 /* Now at an unknown typename (C_ID_ID), a C_ID_TYPENAME or
2280 a C_ID_CLASSNAME. */
2281 c_parser_consume_token (parser);
2282 seen_type = true;
2283 attrs_ok = true;
2284 if (kind == C_ID_ID)
2285 {
2286 error_at (loc, "unknown type name %qE", value);
2287 t.kind = ctsk_typedef;
2288 t.spec = error_mark_node;
2289 }
2290 else if (kind == C_ID_TYPENAME
2291 && (!c_dialect_objc ()
2292 || c_parser_next_token_is_not (parser, CPP_LESS)))
2293 {
2294 t.kind = ctsk_typedef;
2295 /* For a typedef name, record the meaning, not the name.
2296 In case of 'foo foo, bar;'. */
2297 t.spec = lookup_name (value);
2298 }
2299 else
2300 {
2301 tree proto = NULL_TREE;
2302 gcc_assert (c_dialect_objc ());
2303 t.kind = ctsk_objc;
2304 if (c_parser_next_token_is (parser, CPP_LESS))
2305 proto = c_parser_objc_protocol_refs (parser);
2306 t.spec = objc_get_protocol_qualified_type (value, proto);
2307 }
2308 t.expr = NULL_TREE;
2309 t.expr_const_operands = true;
2310 declspecs_add_type (name_token->location, specs, t);
2311 continue;
2312 }
2313 if (c_parser_next_token_is (parser, CPP_LESS))
2314 {
2315 /* Make "<SomeProtocol>" equivalent to "id <SomeProtocol>" -
2316 nisse@lysator.liu.se. */
2317 tree proto;
2318 gcc_assert (c_dialect_objc ());
2319 if (!typespec_ok || seen_type)
2320 break;
2321 proto = c_parser_objc_protocol_refs (parser);
2322 t.kind = ctsk_objc;
2323 t.spec = objc_get_protocol_qualified_type (NULL_TREE, proto);
2324 t.expr = NULL_TREE;
2325 t.expr_const_operands = true;
2326 declspecs_add_type (loc, specs, t);
2327 continue;
2328 }
2329 gcc_assert (c_parser_next_token_is (parser, CPP_KEYWORD));
2330 switch (c_parser_peek_token (parser)->keyword)
2331 {
2332 case RID_STATIC:
2333 case RID_EXTERN:
2334 case RID_REGISTER:
2335 case RID_TYPEDEF:
2336 case RID_INLINE:
2337 case RID_NORETURN:
2338 case RID_AUTO:
2339 case RID_THREAD:
2340 if (!scspec_ok)
2341 goto out;
2342 attrs_ok = true;
2343 /* TODO: Distinguish between function specifiers (inline, noreturn)
2344 and storage class specifiers, either here or in
2345 declspecs_add_scspec. */
2346 declspecs_add_scspec (loc, specs,
2347 c_parser_peek_token (parser)->value);
2348 c_parser_consume_token (parser);
2349 break;
2350 case RID_AUTO_TYPE:
2351 if (!auto_type_ok)
2352 goto out;
2353 /* Fall through. */
2354 case RID_UNSIGNED:
2355 case RID_LONG:
2356 case RID_SHORT:
2357 case RID_SIGNED:
2358 case RID_COMPLEX:
2359 case RID_INT:
2360 case RID_CHAR:
2361 case RID_FLOAT:
2362 case RID_DOUBLE:
2363 case RID_VOID:
2364 case RID_DFLOAT32:
2365 case RID_DFLOAT64:
2366 case RID_DFLOAT128:
2367 case RID_BOOL:
2368 case RID_FRACT:
2369 case RID_ACCUM:
2370 case RID_SAT:
2371 case RID_INT_N_0:
2372 case RID_INT_N_1:
2373 case RID_INT_N_2:
2374 case RID_INT_N_3:
2375 if (!typespec_ok)
2376 goto out;
2377 attrs_ok = true;
2378 seen_type = true;
2379 if (c_dialect_objc ())
2380 parser->objc_need_raw_identifier = true;
2381 t.kind = ctsk_resword;
2382 t.spec = c_parser_peek_token (parser)->value;
2383 t.expr = NULL_TREE;
2384 t.expr_const_operands = true;
2385 declspecs_add_type (loc, specs, t);
2386 c_parser_consume_token (parser);
2387 break;
2388 case RID_ENUM:
2389 if (!typespec_ok)
2390 goto out;
2391 attrs_ok = true;
2392 seen_type = true;
2393 t = c_parser_enum_specifier (parser);
9f50539d 2394 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, t.spec);
dda118e3
JM
2395 declspecs_add_type (loc, specs, t);
2396 break;
2397 case RID_STRUCT:
2398 case RID_UNION:
2399 if (!typespec_ok)
2400 goto out;
2401 attrs_ok = true;
2402 seen_type = true;
2403 t = c_parser_struct_or_union_specifier (parser);
2404 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, t.spec);
2405 declspecs_add_type (loc, specs, t);
2406 break;
2407 case RID_TYPEOF:
2408 /* ??? The old parser rejected typeof after other type
2409 specifiers, but is a syntax error the best way of
2410 handling this? */
2411 if (!typespec_ok || seen_type)
2412 goto out;
2413 attrs_ok = true;
2414 seen_type = true;
2415 t = c_parser_typeof_specifier (parser);
2416 declspecs_add_type (loc, specs, t);
2417 break;
2418 case RID_ATOMIC:
2419 /* C parser handling of Objective-C constructs needs
2420 checking for correct lvalue-to-rvalue conversions, and
2421 the code in build_modify_expr handling various
2422 Objective-C cases, and that in build_unary_op handling
2423 Objective-C cases for increment / decrement, also needs
2424 updating; uses of TYPE_MAIN_VARIANT in objc_compare_types
2425 and objc_types_are_equivalent may also need updates. */
2426 if (c_dialect_objc ())
2427 sorry ("%<_Atomic%> in Objective-C");
2428 /* C parser handling of OpenMP constructs needs checking for
2429 correct lvalue-to-rvalue conversions. */
2430 if (flag_openmp)
2431 sorry ("%<_Atomic%> with OpenMP");
2432 if (flag_isoc99)
2433 pedwarn_c99 (loc, OPT_Wpedantic,
2434 "ISO C99 does not support the %<_Atomic%> qualifier");
2435 else
2436 pedwarn_c99 (loc, OPT_Wpedantic,
2437 "ISO C90 does not support the %<_Atomic%> qualifier");
2438 attrs_ok = true;
2439 tree value;
2440 value = c_parser_peek_token (parser)->value;
2441 c_parser_consume_token (parser);
2442 if (typespec_ok && c_parser_next_token_is (parser, CPP_OPEN_PAREN))
2443 {
2444 /* _Atomic ( type-name ). */
2445 seen_type = true;
2446 c_parser_consume_token (parser);
2447 struct c_type_name *type = c_parser_type_name (parser);
2448 t.kind = ctsk_typeof;
2449 t.spec = error_mark_node;
2450 t.expr = NULL_TREE;
2451 t.expr_const_operands = true;
2452 if (type != NULL)
2453 t.spec = groktypename (type, &t.expr,
2454 &t.expr_const_operands);
2455 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2456 "expected %<)%>");
2457 if (t.spec != error_mark_node)
2458 {
2459 if (TREE_CODE (t.spec) == ARRAY_TYPE)
2460 error_at (loc, "%<_Atomic%>-qualified array type");
2461 else if (TREE_CODE (t.spec) == FUNCTION_TYPE)
2462 error_at (loc, "%<_Atomic%>-qualified function type");
2463 else if (TYPE_QUALS (t.spec) != TYPE_UNQUALIFIED)
2464 error_at (loc, "%<_Atomic%> applied to a qualified type");
2465 else
2466 t.spec = c_build_qualified_type (t.spec, TYPE_QUAL_ATOMIC);
2467 }
2468 declspecs_add_type (loc, specs, t);
2469 }
2470 else
2471 declspecs_add_qual (loc, specs, value);
2472 break;
2473 case RID_CONST:
2474 case RID_VOLATILE:
2475 case RID_RESTRICT:
2476 attrs_ok = true;
2477 declspecs_add_qual (loc, specs, c_parser_peek_token (parser)->value);
2478 c_parser_consume_token (parser);
2479 break;
2480 case RID_ATTRIBUTE:
2481 if (!attrs_ok)
2482 goto out;
2483 attrs = c_parser_attributes (parser);
2484 declspecs_add_attrs (loc, specs, attrs);
2485 break;
2486 case RID_ALIGNAS:
2487 if (!alignspec_ok)
2488 goto out;
2489 align = c_parser_alignas_specifier (parser);
2490 declspecs_add_alignas (loc, specs, align);
2491 break;
2492 default:
2493 goto out;
2494 }
2495 }
2496 out: ;
2497}
2498
2499/* Parse an enum specifier (C90 6.5.2.2, C99 6.7.2.2).
2500
2501 enum-specifier:
2502 enum attributes[opt] identifier[opt] { enumerator-list } attributes[opt]
2503 enum attributes[opt] identifier[opt] { enumerator-list , } attributes[opt]
2504 enum attributes[opt] identifier
2505
2506 The form with trailing comma is new in C99. The forms with
2507 attributes are GNU extensions. In GNU C, we accept any expression
2508 without commas in the syntax (assignment expressions, not just
2509 conditional expressions); assignment expressions will be diagnosed
2510 as non-constant.
2511
2512 enumerator-list:
2513 enumerator
2514 enumerator-list , enumerator
2515
2516 enumerator:
2517 enumeration-constant
2518 enumeration-constant = constant-expression
2519*/
2520
2521static struct c_typespec
2522c_parser_enum_specifier (c_parser *parser)
2523{
2524 struct c_typespec ret;
2525 tree attrs;
2526 tree ident = NULL_TREE;
2527 location_t enum_loc;
2528 location_t ident_loc = UNKNOWN_LOCATION; /* Quiet warning. */
2529 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ENUM));
2530 enum_loc = c_parser_peek_token (parser)->location;
2531 c_parser_consume_token (parser);
2532 attrs = c_parser_attributes (parser);
2533 enum_loc = c_parser_peek_token (parser)->location;
2534 /* Set the location in case we create a decl now. */
2535 c_parser_set_source_position_from_token (c_parser_peek_token (parser));
2536 if (c_parser_next_token_is (parser, CPP_NAME))
2537 {
2538 ident = c_parser_peek_token (parser)->value;
2539 ident_loc = c_parser_peek_token (parser)->location;
2540 enum_loc = ident_loc;
2541 c_parser_consume_token (parser);
2542 }
2543 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
2544 {
2545 /* Parse an enum definition. */
2546 struct c_enum_contents the_enum;
2547 tree type;
2548 tree postfix_attrs;
2549 /* We chain the enumerators in reverse order, then put them in
2550 forward order at the end. */
2551 tree values;
2552 timevar_push (TV_PARSE_ENUM);
2553 type = start_enum (enum_loc, &the_enum, ident);
2554 values = NULL_TREE;
2555 c_parser_consume_token (parser);
2556 while (true)
2557 {
2558 tree enum_id;
2559 tree enum_value;
2560 tree enum_decl;
2561 bool seen_comma;
2562 c_token *token;
2563 location_t comma_loc = UNKNOWN_LOCATION; /* Quiet warning. */
2564 location_t decl_loc, value_loc;
2565 if (c_parser_next_token_is_not (parser, CPP_NAME))
2566 {
2567 c_parser_error (parser, "expected identifier");
2568 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
2569 values = error_mark_node;
2570 break;
2571 }
2572 token = c_parser_peek_token (parser);
2573 enum_id = token->value;
2574 /* Set the location in case we create a decl now. */
2575 c_parser_set_source_position_from_token (token);
2576 decl_loc = value_loc = token->location;
2577 c_parser_consume_token (parser);
2578 if (c_parser_next_token_is (parser, CPP_EQ))
2579 {
2580 c_parser_consume_token (parser);
2581 value_loc = c_parser_peek_token (parser)->location;
2582 enum_value = c_parser_expr_no_commas (parser, NULL).value;
2583 }
2584 else
2585 enum_value = NULL_TREE;
2586 enum_decl = build_enumerator (decl_loc, value_loc,
2587 &the_enum, enum_id, enum_value);
2588 TREE_CHAIN (enum_decl) = values;
2589 values = enum_decl;
2590 seen_comma = false;
2591 if (c_parser_next_token_is (parser, CPP_COMMA))
2592 {
2593 comma_loc = c_parser_peek_token (parser)->location;
2594 seen_comma = true;
2595 c_parser_consume_token (parser);
2596 }
2597 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2598 {
2599 if (seen_comma)
2600 pedwarn_c90 (comma_loc, OPT_Wpedantic,
2601 "comma at end of enumerator list");
2602 c_parser_consume_token (parser);
2603 break;
2604 }
2605 if (!seen_comma)
2606 {
2607 c_parser_error (parser, "expected %<,%> or %<}%>");
2608 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
2609 values = error_mark_node;
2610 break;
2611 }
2612 }
2613 postfix_attrs = c_parser_attributes (parser);
2614 ret.spec = finish_enum (type, nreverse (values),
2615 chainon (attrs, postfix_attrs));
2616 ret.kind = ctsk_tagdef;
2617 ret.expr = NULL_TREE;
2618 ret.expr_const_operands = true;
2619 timevar_pop (TV_PARSE_ENUM);
2620 return ret;
2621 }
2622 else if (!ident)
2623 {
2624 c_parser_error (parser, "expected %<{%>");
2625 ret.spec = error_mark_node;
2626 ret.kind = ctsk_tagref;
2627 ret.expr = NULL_TREE;
2628 ret.expr_const_operands = true;
2629 return ret;
2630 }
2631 ret = parser_xref_tag (ident_loc, ENUMERAL_TYPE, ident);
2632 /* In ISO C, enumerated types can be referred to only if already
2633 defined. */
2634 if (pedantic && !COMPLETE_TYPE_P (ret.spec))
2635 {
2636 gcc_assert (ident);
2637 pedwarn (enum_loc, OPT_Wpedantic,
2638 "ISO C forbids forward references to %<enum%> types");
2639 }
2640 return ret;
2641}
2642
2643/* Parse a struct or union specifier (C90 6.5.2.1, C99 6.7.2.1).
2644
2645 struct-or-union-specifier:
2646 struct-or-union attributes[opt] identifier[opt]
2647 { struct-contents } attributes[opt]
2648 struct-or-union attributes[opt] identifier
2649
2650 struct-contents:
2651 struct-declaration-list
2652
2653 struct-declaration-list:
2654 struct-declaration ;
2655 struct-declaration-list struct-declaration ;
2656
2657 GNU extensions:
2658
2659 struct-contents:
2660 empty
2661 struct-declaration
2662 struct-declaration-list struct-declaration
2663
2664 struct-declaration-list:
2665 struct-declaration-list ;
2666 ;
2667
2668 (Note that in the syntax here, unlike that in ISO C, the semicolons
2669 are included here rather than in struct-declaration, in order to
2670 describe the syntax with extra semicolons and missing semicolon at
2671 end.)
2672
2673 Objective-C:
2674
2675 struct-declaration-list:
2676 @defs ( class-name )
2677
2678 (Note this does not include a trailing semicolon, but can be
2679 followed by further declarations, and gets a pedwarn-if-pedantic
2680 when followed by a semicolon.) */
2681
2682static struct c_typespec
2683c_parser_struct_or_union_specifier (c_parser *parser)
2684{
2685 struct c_typespec ret;
2686 tree attrs;
2687 tree ident = NULL_TREE;
2688 location_t struct_loc;
2689 location_t ident_loc = UNKNOWN_LOCATION;
2690 enum tree_code code;
2691 switch (c_parser_peek_token (parser)->keyword)
2692 {
2693 case RID_STRUCT:
2694 code = RECORD_TYPE;
2695 break;
2696 case RID_UNION:
2697 code = UNION_TYPE;
2698 break;
2699 default:
2700 gcc_unreachable ();
2701 }
2702 struct_loc = c_parser_peek_token (parser)->location;
2703 c_parser_consume_token (parser);
2704 attrs = c_parser_attributes (parser);
2705
2706 /* Set the location in case we create a decl now. */
2707 c_parser_set_source_position_from_token (c_parser_peek_token (parser));
2708
2709 if (c_parser_next_token_is (parser, CPP_NAME))
2710 {
2711 ident = c_parser_peek_token (parser)->value;
2712 ident_loc = c_parser_peek_token (parser)->location;
2713 struct_loc = ident_loc;
2714 c_parser_consume_token (parser);
2715 }
2716 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
2717 {
2718 /* Parse a struct or union definition. Start the scope of the
2719 tag before parsing components. */
2720 struct c_struct_parse_info *struct_info;
2721 tree type = start_struct (struct_loc, code, ident, &struct_info);
2722 tree postfix_attrs;
2723 /* We chain the components in reverse order, then put them in
2724 forward order at the end. Each struct-declaration may
2725 declare multiple components (comma-separated), so we must use
2726 chainon to join them, although when parsing each
2727 struct-declaration we can use TREE_CHAIN directly.
2728
2729 The theory behind all this is that there will be more
2730 semicolon separated fields than comma separated fields, and
2731 so we'll be minimizing the number of node traversals required
2732 by chainon. */
2733 tree contents;
2734 timevar_push (TV_PARSE_STRUCT);
2735 contents = NULL_TREE;
2736 c_parser_consume_token (parser);
2737 /* Handle the Objective-C @defs construct,
2738 e.g. foo(sizeof(struct{ @defs(ClassName) }));. */
2739 if (c_parser_next_token_is_keyword (parser, RID_AT_DEFS))
2740 {
2741 tree name;
2742 gcc_assert (c_dialect_objc ());
2743 c_parser_consume_token (parser);
2744 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2745 goto end_at_defs;
2746 if (c_parser_next_token_is (parser, CPP_NAME)
2747 && c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME)
2748 {
2749 name = c_parser_peek_token (parser)->value;
2750 c_parser_consume_token (parser);
2751 }
2752 else
2753 {
2754 c_parser_error (parser, "expected class name");
2755 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
2756 goto end_at_defs;
2757 }
2758 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2759 "expected %<)%>");
2760 contents = nreverse (objc_get_class_ivars (name));
2761 }
2762 end_at_defs:
2763 /* Parse the struct-declarations and semicolons. Problems with
2764 semicolons are diagnosed here; empty structures are diagnosed
2765 elsewhere. */
2766 while (true)
2767 {
2768 tree decls;
2769 /* Parse any stray semicolon. */
2770 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
2771 {
2772 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
2773 "extra semicolon in struct or union specified");
2774 c_parser_consume_token (parser);
2775 continue;
2776 }
2777 /* Stop if at the end of the struct or union contents. */
2778 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2779 {
2780 c_parser_consume_token (parser);
2781 break;
2782 }
2783 /* Accept #pragmas at struct scope. */
2784 if (c_parser_next_token_is (parser, CPP_PRAGMA))
2785 {
2786 c_parser_pragma (parser, pragma_struct);
2787 continue;
2788 }
2789 /* Parse some comma-separated declarations, but not the
2790 trailing semicolon if any. */
2791 decls = c_parser_struct_declaration (parser);
2792 contents = chainon (decls, contents);
2793 /* If no semicolon follows, either we have a parse error or
2794 are at the end of the struct or union and should
2795 pedwarn. */
2796 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
2797 c_parser_consume_token (parser);
2798 else
2799 {
2800 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2801 pedwarn (c_parser_peek_token (parser)->location, 0,
2802 "no semicolon at end of struct or union");
2803 else if (parser->error
2804 || !c_parser_next_token_starts_declspecs (parser))
2805 {
2806 c_parser_error (parser, "expected %<;%>");
2807 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
2808 break;
2809 }
2810
2811 /* If we come here, we have already emitted an error
2812 for an expected `;', identifier or `(', and we also
2813 recovered already. Go on with the next field. */
2814 }
2815 }
2816 postfix_attrs = c_parser_attributes (parser);
2817 ret.spec = finish_struct (struct_loc, type, nreverse (contents),
2818 chainon (attrs, postfix_attrs), struct_info);
2819 ret.kind = ctsk_tagdef;
2820 ret.expr = NULL_TREE;
2821 ret.expr_const_operands = true;
2822 timevar_pop (TV_PARSE_STRUCT);
2823 return ret;
2824 }
2825 else if (!ident)
2826 {
2827 c_parser_error (parser, "expected %<{%>");
2828 ret.spec = error_mark_node;
2829 ret.kind = ctsk_tagref;
2830 ret.expr = NULL_TREE;
2831 ret.expr_const_operands = true;
2832 return ret;
2833 }
2834 ret = parser_xref_tag (ident_loc, code, ident);
2835 return ret;
2836}
2837
2838/* Parse a struct-declaration (C90 6.5.2.1, C99 6.7.2.1), *without*
2839 the trailing semicolon.
2840
2841 struct-declaration:
2842 specifier-qualifier-list struct-declarator-list
2843 static_assert-declaration-no-semi
2844
2845 specifier-qualifier-list:
2846 type-specifier specifier-qualifier-list[opt]
2847 type-qualifier specifier-qualifier-list[opt]
2848 attributes specifier-qualifier-list[opt]
2849
2850 struct-declarator-list:
2851 struct-declarator
2852 struct-declarator-list , attributes[opt] struct-declarator
2853
2854 struct-declarator:
2855 declarator attributes[opt]
2856 declarator[opt] : constant-expression attributes[opt]
2857
2858 GNU extensions:
2859
2860 struct-declaration:
2861 __extension__ struct-declaration
2862 specifier-qualifier-list
2863
2864 Unlike the ISO C syntax, semicolons are handled elsewhere. The use
2865 of attributes where shown is a GNU extension. In GNU C, we accept
2866 any expression without commas in the syntax (assignment
2867 expressions, not just conditional expressions); assignment
2868 expressions will be diagnosed as non-constant. */
2869
2870static tree
2871c_parser_struct_declaration (c_parser *parser)
2872{
2873 struct c_declspecs *specs;
2874 tree prefix_attrs;
2875 tree all_prefix_attrs;
2876 tree decls;
2877 location_t decl_loc;
2878 if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
2879 {
2880 int ext;
2881 tree decl;
2882 ext = disable_extension_diagnostics ();
2883 c_parser_consume_token (parser);
2884 decl = c_parser_struct_declaration (parser);
2885 restore_extension_diagnostics (ext);
2886 return decl;
2887 }
2888 if (c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT))
2889 {
2890 c_parser_static_assert_declaration_no_semi (parser);
2891 return NULL_TREE;
2892 }
2893 specs = build_null_declspecs ();
2894 decl_loc = c_parser_peek_token (parser)->location;
2895 /* Strictly by the standard, we shouldn't allow _Alignas here,
2896 but it appears to have been intended to allow it there, so
2897 we're keeping it as it is until WG14 reaches a conclusion
2898 of N1731.
2899 <http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1731.pdf> */
2900 c_parser_declspecs (parser, specs, false, true, true,
2901 true, false, cla_nonabstract_decl);
2902 if (parser->error)
2903 return NULL_TREE;
2904 if (!specs->declspecs_seen_p)
2905 {
2906 c_parser_error (parser, "expected specifier-qualifier-list");
2907 return NULL_TREE;
2908 }
2909 finish_declspecs (specs);
2910 if (c_parser_next_token_is (parser, CPP_SEMICOLON)
2911 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2912 {
2913 tree ret;
2914 if (specs->typespec_kind == ctsk_none)
2915 {
2916 pedwarn (decl_loc, OPT_Wpedantic,
2917 "ISO C forbids member declarations with no members");
2918 shadow_tag_warned (specs, pedantic);
2919 ret = NULL_TREE;
2920 }
2921 else
2922 {
2923 /* Support for unnamed structs or unions as members of
2924 structs or unions (which is [a] useful and [b] supports
2925 MS P-SDK). */
2926 tree attrs = NULL;
2927
2928 ret = grokfield (c_parser_peek_token (parser)->location,
2929 build_id_declarator (NULL_TREE), specs,
2930 NULL_TREE, &attrs);
2931 if (ret)
2932 decl_attributes (&ret, attrs, 0);
2933 }
2934 return ret;
2935 }
2936
2937 /* Provide better error recovery. Note that a type name here is valid,
2938 and will be treated as a field name. */
2939 if (specs->typespec_kind == ctsk_tagdef
2940 && TREE_CODE (specs->type) != ENUMERAL_TYPE
2941 && c_parser_next_token_starts_declspecs (parser)
2942 && !c_parser_next_token_is (parser, CPP_NAME))
2943 {
2944 c_parser_error (parser, "expected %<;%>, identifier or %<(%>");
2945 parser->error = false;
2946 return NULL_TREE;
2947 }
2948
2949 pending_xref_error ();
2950 prefix_attrs = specs->attrs;
2951 all_prefix_attrs = prefix_attrs;
2952 specs->attrs = NULL_TREE;
2953 decls = NULL_TREE;
2954 while (true)
2955 {
2956 /* Declaring one or more declarators or un-named bit-fields. */
2957 struct c_declarator *declarator;
2958 bool dummy = false;
2959 if (c_parser_next_token_is (parser, CPP_COLON))
2960 declarator = build_id_declarator (NULL_TREE);
2961 else
2962 declarator = c_parser_declarator (parser,
2963 specs->typespec_kind != ctsk_none,
2964 C_DTR_NORMAL, &dummy);
2965 if (declarator == NULL)
2966 {
2967 c_parser_skip_to_end_of_block_or_statement (parser);
2968 break;
2969 }
2970 if (c_parser_next_token_is (parser, CPP_COLON)
2971 || c_parser_next_token_is (parser, CPP_COMMA)
2972 || c_parser_next_token_is (parser, CPP_SEMICOLON)
2973 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE)
2974 || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2975 {
2976 tree postfix_attrs = NULL_TREE;
2977 tree width = NULL_TREE;
2978 tree d;
2979 if (c_parser_next_token_is (parser, CPP_COLON))
2980 {
2981 c_parser_consume_token (parser);
2982 width = c_parser_expr_no_commas (parser, NULL).value;
2983 }
2984 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2985 postfix_attrs = c_parser_attributes (parser);
2986 d = grokfield (c_parser_peek_token (parser)->location,
2987 declarator, specs, width, &all_prefix_attrs);
2988 decl_attributes (&d, chainon (postfix_attrs,
2989 all_prefix_attrs), 0);
2990 DECL_CHAIN (d) = decls;
2991 decls = d;
2992 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2993 all_prefix_attrs = chainon (c_parser_attributes (parser),
2994 prefix_attrs);
2995 else
2996 all_prefix_attrs = prefix_attrs;
2997 if (c_parser_next_token_is (parser, CPP_COMMA))
2998 c_parser_consume_token (parser);
2999 else if (c_parser_next_token_is (parser, CPP_SEMICOLON)
3000 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3001 {
3002 /* Semicolon consumed in caller. */
3003 break;
3004 }
3005 else
3006 {
3007 c_parser_error (parser, "expected %<,%>, %<;%> or %<}%>");
3008 break;
3009 }
3010 }
3011 else
3012 {
3013 c_parser_error (parser,
3014 "expected %<:%>, %<,%>, %<;%>, %<}%> or "
3015 "%<__attribute__%>");
3016 break;
3017 }
3018 }
3019 return decls;
3020}
3021
3022/* Parse a typeof specifier (a GNU extension).
3023
3024 typeof-specifier:
3025 typeof ( expression )
3026 typeof ( type-name )
3027*/
3028
3029static struct c_typespec
3030c_parser_typeof_specifier (c_parser *parser)
3031{
3032 struct c_typespec ret;
3033 ret.kind = ctsk_typeof;
3034 ret.spec = error_mark_node;
3035 ret.expr = NULL_TREE;
3036 ret.expr_const_operands = true;
3037 gcc_assert (c_parser_next_token_is_keyword (parser, RID_TYPEOF));
3038 c_parser_consume_token (parser);
3039 c_inhibit_evaluation_warnings++;
3040 in_typeof++;
3041 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3042 {
3043 c_inhibit_evaluation_warnings--;
3044 in_typeof--;
3045 return ret;
3046 }
3047 if (c_parser_next_tokens_start_typename (parser, cla_prefer_id))
3048 {
3049 struct c_type_name *type = c_parser_type_name (parser);
3050 c_inhibit_evaluation_warnings--;
3051 in_typeof--;
3052 if (type != NULL)
3053 {
3054 ret.spec = groktypename (type, &ret.expr, &ret.expr_const_operands);
3055 pop_maybe_used (variably_modified_type_p (ret.spec, NULL_TREE));
3056 }
3057 }
3058 else
3059 {
3060 bool was_vm;
3061 location_t here = c_parser_peek_token (parser)->location;
3062 struct c_expr expr = c_parser_expression (parser);
3063 c_inhibit_evaluation_warnings--;
3064 in_typeof--;
3065 if (TREE_CODE (expr.value) == COMPONENT_REF
3066 && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
3067 error_at (here, "%<typeof%> applied to a bit-field");
3068 mark_exp_read (expr.value);
3069 ret.spec = TREE_TYPE (expr.value);
3070 was_vm = variably_modified_type_p (ret.spec, NULL_TREE);
3071 /* This is returned with the type so that when the type is
3072 evaluated, this can be evaluated. */
3073 if (was_vm)
3074 ret.expr = c_fully_fold (expr.value, false, &ret.expr_const_operands);
3075 pop_maybe_used (was_vm);
3076 /* For use in macros such as those in <stdatomic.h>, remove all
3077 qualifiers from atomic types. (const can be an issue for more macros
3078 using typeof than just the <stdatomic.h> ones.) */
3079 if (ret.spec != error_mark_node && TYPE_ATOMIC (ret.spec))
3080 ret.spec = c_build_qualified_type (ret.spec, TYPE_UNQUALIFIED);
3081 }
3082 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
3083 return ret;
3084}
3085
3086/* Parse an alignment-specifier.
3087
3088 C11 6.7.5:
3089
3090 alignment-specifier:
3091 _Alignas ( type-name )
3092 _Alignas ( constant-expression )
3093*/
3094
3095static tree
3096c_parser_alignas_specifier (c_parser * parser)
3097{
3098 tree ret = error_mark_node;
3099 location_t loc = c_parser_peek_token (parser)->location;
3100 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ALIGNAS));
3101 c_parser_consume_token (parser);
3102 if (flag_isoc99)
3103 pedwarn_c99 (loc, OPT_Wpedantic,
3104 "ISO C99 does not support %<_Alignas%>");
3105 else
3106 pedwarn_c99 (loc, OPT_Wpedantic,
3107 "ISO C90 does not support %<_Alignas%>");
3108 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3109 return ret;
3110 if (c_parser_next_tokens_start_typename (parser, cla_prefer_id))
3111 {
3112 struct c_type_name *type = c_parser_type_name (parser);
3113 if (type != NULL)
3114 ret = c_sizeof_or_alignof_type (loc, groktypename (type, NULL, NULL),
3115 false, true, 1);
3116 }
3117 else
3118 ret = c_parser_expr_no_commas (parser, NULL).value;
3119 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
3120 return ret;
3121}
3122
3123/* Parse a declarator, possibly an abstract declarator (C90 6.5.4,
3124 6.5.5, C99 6.7.5, 6.7.6). If TYPE_SEEN_P then a typedef name may
3125 be redeclared; otherwise it may not. KIND indicates which kind of
3126 declarator is wanted. Returns a valid declarator except in the
3127 case of a syntax error in which case NULL is returned. *SEEN_ID is
3128 set to true if an identifier being declared is seen; this is used
3129 to diagnose bad forms of abstract array declarators and to
3130 determine whether an identifier list is syntactically permitted.
3131
3132 declarator:
3133 pointer[opt] direct-declarator
3134
3135 direct-declarator:
3136 identifier
3137 ( attributes[opt] declarator )
3138 direct-declarator array-declarator
3139 direct-declarator ( parameter-type-list )
3140 direct-declarator ( identifier-list[opt] )
3141
3142 pointer:
3143 * type-qualifier-list[opt]
3144 * type-qualifier-list[opt] pointer
3145
3146 type-qualifier-list:
3147 type-qualifier
3148 attributes
3149 type-qualifier-list type-qualifier
3150 type-qualifier-list attributes
3151
3152 array-declarator:
3153 [ type-qualifier-list[opt] assignment-expression[opt] ]
3154 [ static type-qualifier-list[opt] assignment-expression ]
3155 [ type-qualifier-list static assignment-expression ]
3156 [ type-qualifier-list[opt] * ]
3157
3158 parameter-type-list:
3159 parameter-list
3160 parameter-list , ...
3161
3162 parameter-list:
3163 parameter-declaration
3164 parameter-list , parameter-declaration
3165
3166 parameter-declaration:
3167 declaration-specifiers declarator attributes[opt]
3168 declaration-specifiers abstract-declarator[opt] attributes[opt]
3169
3170 identifier-list:
3171 identifier
3172 identifier-list , identifier
3173
3174 abstract-declarator:
3175 pointer
3176 pointer[opt] direct-abstract-declarator
3177
3178 direct-abstract-declarator:
3179 ( attributes[opt] abstract-declarator )
3180 direct-abstract-declarator[opt] array-declarator
3181 direct-abstract-declarator[opt] ( parameter-type-list[opt] )
3182
3183 GNU extensions:
3184
3185 direct-declarator:
3186 direct-declarator ( parameter-forward-declarations
3187 parameter-type-list[opt] )
3188
3189 direct-abstract-declarator:
3190 direct-abstract-declarator[opt] ( parameter-forward-declarations
3191 parameter-type-list[opt] )
3192
3193 parameter-forward-declarations:
3194 parameter-list ;
3195 parameter-forward-declarations parameter-list ;
3196
3197 The uses of attributes shown above are GNU extensions.
3198
3199 Some forms of array declarator are not included in C99 in the
3200 syntax for abstract declarators; these are disallowed elsewhere.
3201 This may be a defect (DR#289).
3202
3203 This function also accepts an omitted abstract declarator as being
3204 an abstract declarator, although not part of the formal syntax. */
3205
3206static struct c_declarator *
3207c_parser_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
3208 bool *seen_id)
3209{
3210 /* Parse any initial pointer part. */
3211 if (c_parser_next_token_is (parser, CPP_MULT))
3212 {
3213 struct c_declspecs *quals_attrs = build_null_declspecs ();
3214 struct c_declarator *inner;
3215 c_parser_consume_token (parser);
3216 c_parser_declspecs (parser, quals_attrs, false, false, true,
3217 false, false, cla_prefer_id);
3218 inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
3219 if (inner == NULL)
3220 return NULL;
3221 else
3222 return make_pointer_declarator (quals_attrs, inner);
3223 }
3224 /* Now we have a direct declarator, direct abstract declarator or
3225 nothing (which counts as a direct abstract declarator here). */
3226 return c_parser_direct_declarator (parser, type_seen_p, kind, seen_id);
3227}
3228
3229/* Parse a direct declarator or direct abstract declarator; arguments
3230 as c_parser_declarator. */
3231
3232static struct c_declarator *
3233c_parser_direct_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
3234 bool *seen_id)
3235{
3236 /* The direct declarator must start with an identifier (possibly
3237 omitted) or a parenthesized declarator (possibly abstract). In
3238 an ordinary declarator, initial parentheses must start a
3239 parenthesized declarator. In an abstract declarator or parameter
3240 declarator, they could start a parenthesized declarator or a
3241 parameter list. To tell which, the open parenthesis and any
3242 following attributes must be read. If a declaration specifier
3243 follows, then it is a parameter list; if the specifier is a
3244 typedef name, there might be an ambiguity about redeclaring it,
3245 which is resolved in the direction of treating it as a typedef
3246 name. If a close parenthesis follows, it is also an empty
3247 parameter list, as the syntax does not permit empty abstract
3248 declarators. Otherwise, it is a parenthesized declarator (in
3249 which case the analysis may be repeated inside it, recursively).
3250
3251 ??? There is an ambiguity in a parameter declaration "int
3252 (__attribute__((foo)) x)", where x is not a typedef name: it
3253 could be an abstract declarator for a function, or declare x with
3254 parentheses. The proper resolution of this ambiguity needs
3255 documenting. At present we follow an accident of the old
3256 parser's implementation, whereby the first parameter must have
3257 some declaration specifiers other than just attributes. Thus as
3258 a parameter declaration it is treated as a parenthesized
3259 parameter named x, and as an abstract declarator it is
3260 rejected.
3261
3262 ??? Also following the old parser, attributes inside an empty
3263 parameter list are ignored, making it a list not yielding a
3264 prototype, rather than giving an error or making it have one
3265 parameter with implicit type int.
3266
3267 ??? Also following the old parser, typedef names may be
3268 redeclared in declarators, but not Objective-C class names. */
3269
3270 if (kind != C_DTR_ABSTRACT
3271 && c_parser_next_token_is (parser, CPP_NAME)
3272 && ((type_seen_p
3273 && (c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME
3274 || c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME))
3275 || c_parser_peek_token (parser)->id_kind == C_ID_ID))
3276 {
3277 struct c_declarator *inner
3278 = build_id_declarator (c_parser_peek_token (parser)->value);
3279 *seen_id = true;
3280 inner->id_loc = c_parser_peek_token (parser)->location;
3281 c_parser_consume_token (parser);
3282 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
3283 }
3284
3285 if (kind != C_DTR_NORMAL
3286 && c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
3287 {
3288 struct c_declarator *inner = build_id_declarator (NULL_TREE);
3289 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
3290 }
3291
3292 /* Either we are at the end of an abstract declarator, or we have
3293 parentheses. */
3294
3295 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
3296 {
3297 tree attrs;
3298 struct c_declarator *inner;
3299 c_parser_consume_token (parser);
3300 attrs = c_parser_attributes (parser);
3301 if (kind != C_DTR_NORMAL
3302 && (c_parser_next_token_starts_declspecs (parser)
3303 || c_parser_next_token_is (parser, CPP_CLOSE_PAREN)))
3304 {
3305 struct c_arg_info *args
3306 = c_parser_parms_declarator (parser, kind == C_DTR_NORMAL,
3307 attrs);
3308 if (args == NULL)
3309 return NULL;
3310 else
3311 {
3312 inner
3313 = build_function_declarator (args,
3314 build_id_declarator (NULL_TREE));
3315 return c_parser_direct_declarator_inner (parser, *seen_id,
3316 inner);
3317 }
3318 }
3319 /* A parenthesized declarator. */
3320 inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
3321 if (inner != NULL && attrs != NULL)
3322 inner = build_attrs_declarator (attrs, inner);
3323 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3324 {
3325 c_parser_consume_token (parser);
3326 if (inner == NULL)
3327 return NULL;
3328 else
3329 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
3330 }
3331 else
3332 {
3333 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3334 "expected %<)%>");
3335 return NULL;
3336 }
3337 }
3338 else
3339 {
3340 if (kind == C_DTR_NORMAL)
3341 {
3342 c_parser_error (parser, "expected identifier or %<(%>");
3343 return NULL;
3344 }
3345 else
3346 return build_id_declarator (NULL_TREE);
3347 }
3348}
3349
3350/* Parse part of a direct declarator or direct abstract declarator,
3351 given that some (in INNER) has already been parsed; ID_PRESENT is
3352 true if an identifier is present, false for an abstract
3353 declarator. */
3354
3355static struct c_declarator *
3356c_parser_direct_declarator_inner (c_parser *parser, bool id_present,
3357 struct c_declarator *inner)
3358{
3359 /* Parse a sequence of array declarators and parameter lists. */
3360 if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
3361 {
3362 location_t brace_loc = c_parser_peek_token (parser)->location;
3363 struct c_declarator *declarator;
3364 struct c_declspecs *quals_attrs = build_null_declspecs ();
3365 bool static_seen;
3366 bool star_seen;
3367 struct c_expr dimen;
3368 dimen.value = NULL_TREE;
3369 dimen.original_code = ERROR_MARK;
3370 dimen.original_type = NULL_TREE;
3371 c_parser_consume_token (parser);
3372 c_parser_declspecs (parser, quals_attrs, false, false, true,
3373 false, false, cla_prefer_id);
3374 static_seen = c_parser_next_token_is_keyword (parser, RID_STATIC);
3375 if (static_seen)
3376 c_parser_consume_token (parser);
3377 if (static_seen && !quals_attrs->declspecs_seen_p)
3378 c_parser_declspecs (parser, quals_attrs, false, false, true,
3379 false, false, cla_prefer_id);
3380 if (!quals_attrs->declspecs_seen_p)
3381 quals_attrs = NULL;
3382 /* If "static" is present, there must be an array dimension.
3383 Otherwise, there may be a dimension, "*", or no
3384 dimension. */
3385 if (static_seen)
3386 {
3387 star_seen = false;
3388 dimen = c_parser_expr_no_commas (parser, NULL);
3389 }
3390 else
3391 {
3392 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3393 {
3394 dimen.value = NULL_TREE;
3395 star_seen = false;
3396 }
3397 else if (flag_cilkplus
3398 && c_parser_next_token_is (parser, CPP_COLON))
3399 {
3400 dimen.value = error_mark_node;
3401 star_seen = false;
3402 error_at (c_parser_peek_token (parser)->location,
3403 "array notations cannot be used in declaration");
3404 c_parser_consume_token (parser);
3405 }
3406 else if (c_parser_next_token_is (parser, CPP_MULT))
3407 {
3408 if (c_parser_peek_2nd_token (parser)->type == CPP_CLOSE_SQUARE)
3409 {
3410 dimen.value = NULL_TREE;
3411 star_seen = true;
3412 c_parser_consume_token (parser);
3413 }
3414 else
3415 {
3416 star_seen = false;
3417 dimen = c_parser_expr_no_commas (parser, NULL);
3418 }
3419 }
3420 else
3421 {
3422 star_seen = false;
3423 dimen = c_parser_expr_no_commas (parser, NULL);
3424 }
3425 }
3426 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3427 c_parser_consume_token (parser);
3428 else if (flag_cilkplus
3429 && c_parser_next_token_is (parser, CPP_COLON))
3430 {
3431 error_at (c_parser_peek_token (parser)->location,
3432 "array notations cannot be used in declaration");
3433 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
3434 return NULL;
3435 }
3436 else
3437 {
3438 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
3439 "expected %<]%>");
3440 return NULL;
3441 }
3442 if (dimen.value)
3443 dimen = convert_lvalue_to_rvalue (brace_loc, dimen, true, true);
3444 declarator = build_array_declarator (brace_loc, dimen.value, quals_attrs,
3445 static_seen, star_seen);
3446 if (declarator == NULL)
3447 return NULL;
3448 inner = set_array_declarator_inner (declarator, inner);
3449 return c_parser_direct_declarator_inner (parser, id_present, inner);
3450 }
3451 else if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
3452 {
3453 tree attrs;
3454 struct c_arg_info *args;
3455 c_parser_consume_token (parser);
3456 attrs = c_parser_attributes (parser);
3457 args = c_parser_parms_declarator (parser, id_present, attrs);
3458 if (args == NULL)
3459 return NULL;
3460 else
3461 {
3462 inner = build_function_declarator (args, inner);
3463 return c_parser_direct_declarator_inner (parser, id_present, inner);
3464 }
3465 }
3466 return inner;
3467}
3468
3469/* Parse a parameter list or identifier list, including the closing
3470 parenthesis but not the opening one. ATTRS are the attributes at
3471 the start of the list. ID_LIST_OK is true if an identifier list is
3472 acceptable; such a list must not have attributes at the start. */
3473
3474static struct c_arg_info *
3475c_parser_parms_declarator (c_parser *parser, bool id_list_ok, tree attrs)
3476{
3477 push_scope ();
3478 declare_parm_level ();
3479 /* If the list starts with an identifier, it is an identifier list.
3480 Otherwise, it is either a prototype list or an empty list. */
3481 if (id_list_ok
3482 && !attrs
3483 && c_parser_next_token_is (parser, CPP_NAME)
3484 && c_parser_peek_token (parser)->id_kind == C_ID_ID
3485
3486 /* Look ahead to detect typos in type names. */
3487 && c_parser_peek_2nd_token (parser)->type != CPP_NAME
3488 && c_parser_peek_2nd_token (parser)->type != CPP_MULT
3489 && c_parser_peek_2nd_token (parser)->type != CPP_OPEN_PAREN
3490 && c_parser_peek_2nd_token (parser)->type != CPP_OPEN_SQUARE)
3491 {
3492 tree list = NULL_TREE, *nextp = &list;
3493 while (c_parser_next_token_is (parser, CPP_NAME)
3494 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
3495 {
3496 *nextp = build_tree_list (NULL_TREE,
3497 c_parser_peek_token (parser)->value);
3498 nextp = & TREE_CHAIN (*nextp);
3499 c_parser_consume_token (parser);
3500 if (c_parser_next_token_is_not (parser, CPP_COMMA))
3501 break;
3502 c_parser_consume_token (parser);
3503 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3504 {
3505 c_parser_error (parser, "expected identifier");
3506 break;
3507 }
3508 }
3509 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3510 {
3511 struct c_arg_info *ret = build_arg_info ();
3512 ret->types = list;
3513 c_parser_consume_token (parser);
3514 pop_scope ();
3515 return ret;
3516 }
3517 else
3518 {
3519 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3520 "expected %<)%>");
3521 pop_scope ();
3522 return NULL;
3523 }
3524 }
3525 else
3526 {
3527 struct c_arg_info *ret = c_parser_parms_list_declarator (parser, attrs,
3528 NULL);
3529 pop_scope ();
3530 return ret;
3531 }
3532}
3533
3534/* Parse a parameter list (possibly empty), including the closing
3535 parenthesis but not the opening one. ATTRS are the attributes at
3536 the start of the list. EXPR is NULL or an expression that needs to
3537 be evaluated for the side effects of array size expressions in the
3538 parameters. */
3539
3540static struct c_arg_info *
3541c_parser_parms_list_declarator (c_parser *parser, tree attrs, tree expr)
3542{
3543 bool bad_parm = false;
3544
3545 /* ??? Following the old parser, forward parameter declarations may
3546 use abstract declarators, and if no real parameter declarations
3547 follow the forward declarations then this is not diagnosed. Also
3548 note as above that attributes are ignored as the only contents of
3549 the parentheses, or as the only contents after forward
3550 declarations. */
3551 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3552 {
3553 struct c_arg_info *ret = build_arg_info ();
3554 c_parser_consume_token (parser);
3555 return ret;
3556 }
3557 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3558 {
3559 struct c_arg_info *ret = build_arg_info ();
3560
3561 if (flag_allow_parameterless_variadic_functions)
3562 {
3563 /* F (...) is allowed. */
3564 ret->types = NULL_TREE;
3565 }
3566 else
3567 {
3568 /* Suppress -Wold-style-definition for this case. */
3569 ret->types = error_mark_node;
3570 error_at (c_parser_peek_token (parser)->location,
3571 "ISO C requires a named argument before %<...%>");
3572 }
3573 c_parser_consume_token (parser);
3574 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3575 {
3576 c_parser_consume_token (parser);
3577 return ret;
3578 }
3579 else
3580 {
3581 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3582 "expected %<)%>");
3583 return NULL;
3584 }
3585 }
3586 /* Nonempty list of parameters, either terminated with semicolon
3587 (forward declarations; recurse) or with close parenthesis (normal
3588 function) or with ", ... )" (variadic function). */
3589 while (true)
3590 {
3591 /* Parse a parameter. */
3592 struct c_parm *parm = c_parser_parameter_declaration (parser, attrs);
3593 attrs = NULL_TREE;
3594 if (parm == NULL)
3595 bad_parm = true;
3596 else
3597 push_parm_decl (parm, &expr);
3598 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
3599 {
3600 tree new_attrs;
3601 c_parser_consume_token (parser);
3602 mark_forward_parm_decls ();
3603 new_attrs = c_parser_attributes (parser);
3604 return c_parser_parms_list_declarator (parser, new_attrs, expr);
3605 }
3606 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3607 {
3608 c_parser_consume_token (parser);
3609 if (bad_parm)
3610 return NULL;
3611 else
3612 return get_parm_info (false, expr);
3613 }
3614 if (!c_parser_require (parser, CPP_COMMA,
3615 "expected %<;%>, %<,%> or %<)%>"))
3616 {
3617 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
3618 return NULL;
3619 }
3620 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3621 {
3622 c_parser_consume_token (parser);
3623 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3624 {
3625 c_parser_consume_token (parser);
3626 if (bad_parm)
3627 return NULL;
3628 else
3629 return get_parm_info (true, expr);
3630 }
3631 else
3632 {
3633 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3634 "expected %<)%>");
3635 return NULL;
3636 }
3637 }
3638 }
3639}
3640
3641/* Parse a parameter declaration. ATTRS are the attributes at the
3642 start of the declaration if it is the first parameter. */
3643
3644static struct c_parm *
3645c_parser_parameter_declaration (c_parser *parser, tree attrs)
3646{
3647 struct c_declspecs *specs;
3648 struct c_declarator *declarator;
3649 tree prefix_attrs;
3650 tree postfix_attrs = NULL_TREE;
3651 bool dummy = false;
3652
3653 /* Accept #pragmas between parameter declarations. */
3654 while (c_parser_next_token_is (parser, CPP_PRAGMA))
3655 c_parser_pragma (parser, pragma_param);
3656
3657 if (!c_parser_next_token_starts_declspecs (parser))
3658 {
3659 c_token *token = c_parser_peek_token (parser);
3660 if (parser->error)
3661 return NULL;
3662 c_parser_set_source_position_from_token (token);
3663 if (c_parser_next_tokens_start_typename (parser, cla_prefer_type))
3664 {
3665 error_at (token->location, "unknown type name %qE", token->value);
3666 parser->error = true;
3667 }
3668 /* ??? In some Objective-C cases '...' isn't applicable so there
3669 should be a different message. */
3670 else
3671 c_parser_error (parser,
3672 "expected declaration specifiers or %<...%>");
3673 c_parser_skip_to_end_of_parameter (parser);
3674 return NULL;
3675 }
3676 specs = build_null_declspecs ();
3677 if (attrs)
3678 {
3679 declspecs_add_attrs (input_location, specs, attrs);
3680 attrs = NULL_TREE;
3681 }
3682 c_parser_declspecs (parser, specs, true, true, true, true, false,
3683 cla_nonabstract_decl);
3684 finish_declspecs (specs);
3685 pending_xref_error ();
3686 prefix_attrs = specs->attrs;
3687 specs->attrs = NULL_TREE;
3688 declarator = c_parser_declarator (parser,
3689 specs->typespec_kind != ctsk_none,
3690 C_DTR_PARM, &dummy);
3691 if (declarator == NULL)
3692 {
3693 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
3694 return NULL;
3695 }
3696 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
3697 postfix_attrs = c_parser_attributes (parser);
3698 return build_c_parm (specs, chainon (postfix_attrs, prefix_attrs),
3699 declarator);
3700}
3701
3702/* Parse a string literal in an asm expression. It should not be
3703 translated, and wide string literals are an error although
3704 permitted by the syntax. This is a GNU extension.
3705
3706 asm-string-literal:
3707 string-literal
3708
3709 ??? At present, following the old parser, the caller needs to have
3710 set lex_untranslated_string to 1. It would be better to follow the
3711 C++ parser rather than using this kludge. */
3712
3713static tree
3714c_parser_asm_string_literal (c_parser *parser)
3715{
3716 tree str;
3717 int save_flag = warn_overlength_strings;
3718 warn_overlength_strings = 0;
3719 if (c_parser_next_token_is (parser, CPP_STRING))
3720 {
3721 str = c_parser_peek_token (parser)->value;
3722 c_parser_consume_token (parser);
3723 }
3724 else if (c_parser_next_token_is (parser, CPP_WSTRING))
3725 {
3726 error_at (c_parser_peek_token (parser)->location,
3727 "wide string literal in %<asm%>");
3728 str = build_string (1, "");
3729 c_parser_consume_token (parser);
3730 }
3731 else
3732 {
3733 c_parser_error (parser, "expected string literal");
3734 str = NULL_TREE;
3735 }
3736 warn_overlength_strings = save_flag;
3737 return str;
3738}
3739
3740/* Parse a simple asm expression. This is used in restricted
3741 contexts, where a full expression with inputs and outputs does not
3742 make sense. This is a GNU extension.
3743
3744 simple-asm-expr:
3745 asm ( asm-string-literal )
3746*/
3747
3748static tree
3749c_parser_simple_asm_expr (c_parser *parser)
3750{
3751 tree str;
3752 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
3753 /* ??? Follow the C++ parser rather than using the
3754 lex_untranslated_string kludge. */
3755 parser->lex_untranslated_string = true;
3756 c_parser_consume_token (parser);
3757 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3758 {
3759 parser->lex_untranslated_string = false;
3760 return NULL_TREE;
3761 }
3762 str = c_parser_asm_string_literal (parser);
3763 parser->lex_untranslated_string = false;
3764 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
3765 {
3766 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
3767 return NULL_TREE;
3768 }
3769 return str;
3770}
3771
3772static tree
3773c_parser_attribute_any_word (c_parser *parser)
3774{
3775 tree attr_name = NULL_TREE;
3776
3777 if (c_parser_next_token_is (parser, CPP_KEYWORD))
3778 {
3779 /* ??? See comment above about what keywords are accepted here. */
3780 bool ok;
3781 switch (c_parser_peek_token (parser)->keyword)
3782 {
3783 case RID_STATIC:
3784 case RID_UNSIGNED:
3785 case RID_LONG:
3786 case RID_CONST:
3787 case RID_EXTERN:
3788 case RID_REGISTER:
3789 case RID_TYPEDEF:
3790 case RID_SHORT:
3791 case RID_INLINE:
3792 case RID_NORETURN:
3793 case RID_VOLATILE:
3794 case RID_SIGNED:
3795 case RID_AUTO:
3796 case RID_RESTRICT:
3797 case RID_COMPLEX:
3798 case RID_THREAD:
3799 case RID_INT:
3800 case RID_CHAR:
3801 case RID_FLOAT:
3802 case RID_DOUBLE:
3803 case RID_VOID:
3804 case RID_DFLOAT32:
3805 case RID_DFLOAT64:
3806 case RID_DFLOAT128:
3807 case RID_BOOL:
3808 case RID_FRACT:
3809 case RID_ACCUM:
3810 case RID_SAT:
3811 case RID_TRANSACTION_ATOMIC:
3812 case RID_TRANSACTION_CANCEL:
3813 case RID_ATOMIC:
3814 case RID_AUTO_TYPE:
3815 case RID_INT_N_0:
3816 case RID_INT_N_1:
3817 case RID_INT_N_2:
3818 case RID_INT_N_3:
3819 ok = true;
3820 break;
3821 default:
3822 ok = false;
3823 break;
3824 }
3825 if (!ok)
3826 return NULL_TREE;
3827
3828 /* Accept __attribute__((__const)) as __attribute__((const)) etc. */
3829 attr_name = ridpointers[(int) c_parser_peek_token (parser)->keyword];
3830 }
3831 else if (c_parser_next_token_is (parser, CPP_NAME))
3832 attr_name = c_parser_peek_token (parser)->value;
3833
3834 return attr_name;
3835}
3836
3837/* Returns true of NAME is an IDENTIFIER_NODE with identiifer "vector,"
3838 "__vector" or "__vector__." */
3839
3840static inline bool
3841is_cilkplus_vector_p (tree name)
3842{
3843 if (flag_cilkplus && is_attribute_p ("vector", name))
3844 return true;
3845 return false;
3846}
3847
3848#define CILK_SIMD_FN_CLAUSE_MASK \
3849 ((OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_VECTORLENGTH) \
3850 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_LINEAR) \
3851 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_UNIFORM) \
3852 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_MASK) \
3853 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_NOMASK))
3854
3855/* Parses the vector attribute of SIMD enabled functions in Cilk Plus.
3856 VEC_TOKEN is the "vector" token that is replaced with "simd" and
3857 pushed into the token list.
3858 Syntax:
3859 vector
3860 vector (<vector attributes>). */
3861
3862static void
3863c_parser_cilk_simd_fn_vector_attrs (c_parser *parser, c_token vec_token)
3864{
3865 gcc_assert (is_cilkplus_vector_p (vec_token.value));
3866
3867 int paren_scope = 0;
3868 vec_safe_push (parser->cilk_simd_fn_tokens, vec_token);
3869 /* Consume the "vector" token. */
3870 c_parser_consume_token (parser);
3871
3872 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
3873 {
3874 c_parser_consume_token (parser);
3875 paren_scope++;
3876 }
3877 while (paren_scope > 0)
3878 {
3879 c_token *token = c_parser_peek_token (parser);
3880 if (token->type == CPP_OPEN_PAREN)
3881 paren_scope++;
3882 else if (token->type == CPP_CLOSE_PAREN)
3883 paren_scope--;
3884 /* Do not push the last ')' since we are not pushing the '('. */
3885 if (!(token->type == CPP_CLOSE_PAREN && paren_scope == 0))
3886 vec_safe_push (parser->cilk_simd_fn_tokens, *token);
3887 c_parser_consume_token (parser);
3888 }
3889
3890 /* Since we are converting an attribute to a pragma, we need to end the
3891 attribute with PRAGMA_EOL. */
3892 c_token eol_token;
3893 memset (&eol_token, 0, sizeof (eol_token));
3894 eol_token.type = CPP_PRAGMA_EOL;
3895 vec_safe_push (parser->cilk_simd_fn_tokens, eol_token);
3896}
3897
3898/* Add 2 CPP_EOF at the end of PARSER->ELEM_FN_TOKENS vector. */