]>
Commit | Line | Data |
---|---|---|
27bf414c JM |
1 | /* Parser for C and Objective-C. |
2 | Copyright (C) 1987, 1988, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998, | |
b295aee2 | 3 | 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2007, 2008, 2009 |
5df27e4a | 4 | Free Software Foundation, Inc. |
27bf414c JM |
5 | |
6 | Parser actions based on the old Bison parser; structure somewhat | |
7 | influenced by and fragments based on the C++ parser. | |
8 | ||
9 | This file is part of GCC. | |
10 | ||
11 | GCC is free software; you can redistribute it and/or modify it under | |
12 | the terms of the GNU General Public License as published by the Free | |
9dcd6f09 | 13 | Software Foundation; either version 3, or (at your option) any later |
27bf414c JM |
14 | version. |
15 | ||
16 | GCC is distributed in the hope that it will be useful, but WITHOUT ANY | |
17 | WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
18 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
19 | for more details. | |
20 | ||
21 | You should have received a copy of the GNU General Public License | |
9dcd6f09 NC |
22 | along with GCC; see the file COPYING3. If not see |
23 | <http://www.gnu.org/licenses/>. */ | |
27bf414c JM |
24 | |
25 | /* TODO: | |
26 | ||
27 | Make sure all relevant comments, and all relevant code from all | |
28 | actions, brought over from old parser. Verify exact correspondence | |
29 | of syntax accepted. | |
30 | ||
31 | Add testcases covering every input symbol in every state in old and | |
32 | new parsers. | |
33 | ||
34 | Include full syntax for GNU C, including erroneous cases accepted | |
35 | with error messages, in syntax productions in comments. | |
36 | ||
37 | Make more diagnostics in the front end generally take an explicit | |
38 | location rather than implicitly using input_location. */ | |
39 | ||
40 | #include "config.h" | |
41 | #include "system.h" | |
42 | #include "coretypes.h" | |
43 | #include "tm.h" | |
44 | #include "tree.h" | |
bc4071dd | 45 | #include "rtl.h" |
27bf414c JM |
46 | #include "langhooks.h" |
47 | #include "input.h" | |
48 | #include "cpplib.h" | |
49 | #include "timevar.h" | |
50 | #include "c-pragma.h" | |
51 | #include "c-tree.h" | |
52 | #include "flags.h" | |
53 | #include "output.h" | |
54 | #include "toplev.h" | |
55 | #include "ggc.h" | |
56 | #include "c-common.h" | |
bc4071dd RH |
57 | #include "vec.h" |
58 | #include "target.h" | |
474eccc6 | 59 | #include "cgraph.h" |
68a607d8 | 60 | #include "plugin.h" |
27bf414c JM |
61 | |
62 | \f | |
27bf414c JM |
63 | /* Initialization routine for this file. */ |
64 | ||
65 | void | |
66 | c_parse_init (void) | |
67 | { | |
68 | /* The only initialization required is of the reserved word | |
69 | identifiers. */ | |
70 | unsigned int i; | |
71 | tree id; | |
eea1139b | 72 | int mask = 0; |
27bf414c | 73 | |
eea1139b ILT |
74 | mask |= D_CXXONLY; |
75 | if (!flag_isoc99) | |
76 | mask |= D_C99; | |
77 | if (flag_no_asm) | |
78 | { | |
79 | mask |= D_ASM | D_EXT; | |
80 | if (!flag_isoc99) | |
81 | mask |= D_EXT89; | |
82 | } | |
27bf414c | 83 | if (!c_dialect_objc ()) |
eea1139b | 84 | mask |= D_OBJC | D_CXX_OBJC; |
27bf414c JM |
85 | |
86 | ridpointers = GGC_CNEWVEC (tree, (int) RID_MAX); | |
eea1139b | 87 | for (i = 0; i < num_c_common_reswords; i++) |
27bf414c JM |
88 | { |
89 | /* If a keyword is disabled, do not enter it into the table | |
90 | and so create a canonical spelling that isn't a keyword. */ | |
eea1139b ILT |
91 | if (c_common_reswords[i].disable & mask) |
92 | { | |
93 | if (warn_cxx_compat | |
94 | && (c_common_reswords[i].disable & D_CXXWARN)) | |
95 | { | |
96 | id = get_identifier (c_common_reswords[i].word); | |
97 | C_SET_RID_CODE (id, RID_CXX_COMPAT_WARN); | |
98 | C_IS_RESERVED_WORD (id) = 1; | |
99 | } | |
100 | continue; | |
101 | } | |
27bf414c | 102 | |
eea1139b ILT |
103 | id = get_identifier (c_common_reswords[i].word); |
104 | C_SET_RID_CODE (id, c_common_reswords[i].rid); | |
27bf414c | 105 | C_IS_RESERVED_WORD (id) = 1; |
eea1139b | 106 | ridpointers [(int) c_common_reswords[i].rid] = id; |
27bf414c JM |
107 | } |
108 | } | |
109 | \f | |
110 | /* The C lexer intermediates between the lexer in cpplib and c-lex.c | |
111 | and the C parser. Unlike the C++ lexer, the parser structure | |
112 | stores the lexer information instead of using a separate structure. | |
113 | Identifiers are separated into ordinary identifiers, type names, | |
114 | keywords and some other Objective-C types of identifiers, and some | |
115 | look-ahead is maintained. | |
116 | ||
117 | ??? It might be a good idea to lex the whole file up front (as for | |
118 | C++). It would then be possible to share more of the C and C++ | |
119 | lexer code, if desired. */ | |
120 | ||
121 | /* The following local token type is used. */ | |
122 | ||
123 | /* A keyword. */ | |
124 | #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1)) | |
125 | ||
27bf414c JM |
126 | /* More information about the type of a CPP_NAME token. */ |
127 | typedef enum c_id_kind { | |
128 | /* An ordinary identifier. */ | |
129 | C_ID_ID, | |
130 | /* An identifier declared as a typedef name. */ | |
131 | C_ID_TYPENAME, | |
132 | /* An identifier declared as an Objective-C class name. */ | |
133 | C_ID_CLASSNAME, | |
134 | /* Not an identifier. */ | |
135 | C_ID_NONE | |
136 | } c_id_kind; | |
137 | ||
138 | /* A single C token after string literal concatenation and conversion | |
139 | of preprocessing tokens to tokens. */ | |
d1b38208 | 140 | typedef struct GTY (()) c_token { |
27bf414c JM |
141 | /* The kind of token. */ |
142 | ENUM_BITFIELD (cpp_ttype) type : 8; | |
143 | /* If this token is a CPP_NAME, this value indicates whether also | |
144 | declared as some kind of type. Otherwise, it is C_ID_NONE. */ | |
145 | ENUM_BITFIELD (c_id_kind) id_kind : 8; | |
146 | /* If this token is a keyword, this value indicates which keyword. | |
147 | Otherwise, this value is RID_MAX. */ | |
148 | ENUM_BITFIELD (rid) keyword : 8; | |
bc4071dd RH |
149 | /* If this token is a CPP_PRAGMA, this indicates the pragma that |
150 | was seen. Otherwise it is PRAGMA_NONE. */ | |
13fa1171 | 151 | ENUM_BITFIELD (pragma_kind) pragma_kind : 8; |
27bf414c JM |
152 | /* The value associated with this token, if any. */ |
153 | tree value; | |
154 | /* The location at which this token was found. */ | |
155 | location_t location; | |
156 | } c_token; | |
157 | ||
158 | /* A parser structure recording information about the state and | |
159 | context of parsing. Includes lexer information with up to two | |
160 | tokens of look-ahead; more are not needed for C. */ | |
d1b38208 | 161 | typedef struct GTY(()) c_parser { |
27bf414c JM |
162 | /* The look-ahead tokens. */ |
163 | c_token tokens[2]; | |
164 | /* How many look-ahead tokens are available (0, 1 or 2). */ | |
165 | short tokens_avail; | |
166 | /* True if a syntax error is being recovered from; false otherwise. | |
167 | c_parser_error sets this flag. It should clear this flag when | |
168 | enough tokens have been consumed to recover from the error. */ | |
169 | BOOL_BITFIELD error : 1; | |
bc4071dd RH |
170 | /* True if we're processing a pragma, and shouldn't automatically |
171 | consume CPP_PRAGMA_EOL. */ | |
172 | BOOL_BITFIELD in_pragma : 1; | |
b4b56033 MLI |
173 | /* True if we're parsing the outermost block of an if statement. */ |
174 | BOOL_BITFIELD in_if_block : 1; | |
46c2514e TT |
175 | /* True if we want to lex an untranslated string. */ |
176 | BOOL_BITFIELD lex_untranslated_string : 1; | |
0bacb8c7 TT |
177 | /* Objective-C specific parser/lexer information. */ |
178 | BOOL_BITFIELD objc_pq_context : 1; | |
179 | /* The following flag is needed to contextualize Objective-C lexical | |
180 | analysis. In some cases (e.g., 'int NSObject;'), it is | |
181 | undesirable to bind an identifier to an Objective-C class, even | |
182 | if a class with that name exists. */ | |
183 | BOOL_BITFIELD objc_need_raw_identifier : 1; | |
27bf414c JM |
184 | } c_parser; |
185 | ||
bc4071dd RH |
186 | |
187 | /* The actual parser and external interface. ??? Does this need to be | |
188 | garbage-collected? */ | |
189 | ||
190 | static GTY (()) c_parser *the_parser; | |
191 | ||
192 | ||
27bf414c JM |
193 | /* Read in and lex a single token, storing it in *TOKEN. */ |
194 | ||
195 | static void | |
0bacb8c7 | 196 | c_lex_one_token (c_parser *parser, c_token *token) |
27bf414c JM |
197 | { |
198 | timevar_push (TV_LEX); | |
bc4071dd | 199 | |
46c2514e TT |
200 | token->type = c_lex_with_flags (&token->value, &token->location, NULL, |
201 | (parser->lex_untranslated_string | |
202 | ? C_LEX_STRING_NO_TRANSLATE : 0)); | |
bc4071dd RH |
203 | token->id_kind = C_ID_NONE; |
204 | token->keyword = RID_MAX; | |
205 | token->pragma_kind = PRAGMA_NONE; | |
bc4071dd | 206 | |
27bf414c JM |
207 | switch (token->type) |
208 | { | |
209 | case CPP_NAME: | |
27bf414c JM |
210 | { |
211 | tree decl; | |
212 | ||
0bacb8c7 TT |
213 | bool objc_force_identifier = parser->objc_need_raw_identifier; |
214 | if (c_dialect_objc ()) | |
215 | parser->objc_need_raw_identifier = false; | |
27bf414c JM |
216 | |
217 | if (C_IS_RESERVED_WORD (token->value)) | |
218 | { | |
219 | enum rid rid_code = C_RID_CODE (token->value); | |
220 | ||
eea1139b ILT |
221 | if (rid_code == RID_CXX_COMPAT_WARN) |
222 | { | |
3ba09659 AH |
223 | warning_at (token->location, |
224 | OPT_Wc___compat, | |
88388a52 JM |
225 | "identifier %qE conflicts with C++ keyword", |
226 | token->value); | |
eea1139b ILT |
227 | } |
228 | else if (c_dialect_objc ()) | |
27bf414c | 229 | { |
eea1139b | 230 | if (!objc_is_reserved_word (token->value) |
0bacb8c7 TT |
231 | && (!OBJC_IS_PQ_KEYWORD (rid_code) |
232 | || parser->objc_pq_context)) | |
27bf414c JM |
233 | { |
234 | /* Return the canonical spelling for this keyword. */ | |
235 | token->value = ridpointers[(int) rid_code]; | |
236 | token->type = CPP_KEYWORD; | |
237 | token->keyword = rid_code; | |
238 | break; | |
239 | } | |
240 | } | |
241 | else | |
242 | { | |
27bf414c JM |
243 | token->type = CPP_KEYWORD; |
244 | token->keyword = rid_code; | |
245 | break; | |
246 | } | |
247 | } | |
248 | ||
249 | decl = lookup_name (token->value); | |
250 | if (decl) | |
251 | { | |
252 | if (TREE_CODE (decl) == TYPE_DECL) | |
253 | { | |
254 | token->id_kind = C_ID_TYPENAME; | |
255 | break; | |
256 | } | |
257 | } | |
258 | else if (c_dialect_objc ()) | |
259 | { | |
260 | tree objc_interface_decl = objc_is_class_name (token->value); | |
261 | /* Objective-C class names are in the same namespace as | |
262 | variables and typedefs, and hence are shadowed by local | |
263 | declarations. */ | |
264 | if (objc_interface_decl | |
265 | && (global_bindings_p () | |
266 | || (!objc_force_identifier && !decl))) | |
267 | { | |
268 | token->value = objc_interface_decl; | |
269 | token->id_kind = C_ID_CLASSNAME; | |
270 | break; | |
271 | } | |
272 | } | |
bc4071dd | 273 | token->id_kind = C_ID_ID; |
27bf414c | 274 | } |
27bf414c JM |
275 | break; |
276 | case CPP_AT_NAME: | |
277 | /* This only happens in Objective-C; it must be a keyword. */ | |
278 | token->type = CPP_KEYWORD; | |
27bf414c JM |
279 | token->keyword = C_RID_CODE (token->value); |
280 | break; | |
281 | case CPP_COLON: | |
282 | case CPP_COMMA: | |
283 | case CPP_CLOSE_PAREN: | |
284 | case CPP_SEMICOLON: | |
285 | /* These tokens may affect the interpretation of any identifiers | |
286 | following, if doing Objective-C. */ | |
0bacb8c7 TT |
287 | if (c_dialect_objc ()) |
288 | parser->objc_need_raw_identifier = false; | |
bc4071dd RH |
289 | break; |
290 | case CPP_PRAGMA: | |
291 | /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */ | |
d75d71e0 | 292 | token->pragma_kind = (enum pragma_kind) TREE_INT_CST_LOW (token->value); |
bc4071dd | 293 | token->value = NULL; |
27bf414c JM |
294 | break; |
295 | default: | |
27bf414c JM |
296 | break; |
297 | } | |
298 | timevar_pop (TV_LEX); | |
299 | } | |
300 | ||
301 | /* Return a pointer to the next token from PARSER, reading it in if | |
302 | necessary. */ | |
303 | ||
304 | static inline c_token * | |
305 | c_parser_peek_token (c_parser *parser) | |
306 | { | |
307 | if (parser->tokens_avail == 0) | |
308 | { | |
0bacb8c7 | 309 | c_lex_one_token (parser, &parser->tokens[0]); |
27bf414c JM |
310 | parser->tokens_avail = 1; |
311 | } | |
312 | return &parser->tokens[0]; | |
313 | } | |
314 | ||
315 | /* Return true if the next token from PARSER has the indicated | |
316 | TYPE. */ | |
317 | ||
318 | static inline bool | |
319 | c_parser_next_token_is (c_parser *parser, enum cpp_ttype type) | |
320 | { | |
321 | return c_parser_peek_token (parser)->type == type; | |
322 | } | |
323 | ||
324 | /* Return true if the next token from PARSER does not have the | |
325 | indicated TYPE. */ | |
326 | ||
327 | static inline bool | |
328 | c_parser_next_token_is_not (c_parser *parser, enum cpp_ttype type) | |
329 | { | |
330 | return !c_parser_next_token_is (parser, type); | |
331 | } | |
332 | ||
333 | /* Return true if the next token from PARSER is the indicated | |
334 | KEYWORD. */ | |
335 | ||
336 | static inline bool | |
337 | c_parser_next_token_is_keyword (c_parser *parser, enum rid keyword) | |
338 | { | |
dbc518f0 | 339 | return c_parser_peek_token (parser)->keyword == keyword; |
27bf414c JM |
340 | } |
341 | ||
342 | /* Return true if TOKEN can start a type name, | |
343 | false otherwise. */ | |
344 | static bool | |
345 | c_token_starts_typename (c_token *token) | |
346 | { | |
347 | switch (token->type) | |
348 | { | |
349 | case CPP_NAME: | |
350 | switch (token->id_kind) | |
351 | { | |
352 | case C_ID_ID: | |
353 | return false; | |
354 | case C_ID_TYPENAME: | |
355 | return true; | |
356 | case C_ID_CLASSNAME: | |
357 | gcc_assert (c_dialect_objc ()); | |
358 | return true; | |
359 | default: | |
360 | gcc_unreachable (); | |
361 | } | |
362 | case CPP_KEYWORD: | |
363 | switch (token->keyword) | |
364 | { | |
365 | case RID_UNSIGNED: | |
366 | case RID_LONG: | |
367 | case RID_SHORT: | |
368 | case RID_SIGNED: | |
369 | case RID_COMPLEX: | |
370 | case RID_INT: | |
371 | case RID_CHAR: | |
372 | case RID_FLOAT: | |
373 | case RID_DOUBLE: | |
374 | case RID_VOID: | |
9a8ce21f JG |
375 | case RID_DFLOAT32: |
376 | case RID_DFLOAT64: | |
377 | case RID_DFLOAT128: | |
27bf414c JM |
378 | case RID_BOOL: |
379 | case RID_ENUM: | |
380 | case RID_STRUCT: | |
381 | case RID_UNION: | |
382 | case RID_TYPEOF: | |
383 | case RID_CONST: | |
384 | case RID_VOLATILE: | |
385 | case RID_RESTRICT: | |
386 | case RID_ATTRIBUTE: | |
ab22c1fa CF |
387 | case RID_FRACT: |
388 | case RID_ACCUM: | |
389 | case RID_SAT: | |
27bf414c JM |
390 | return true; |
391 | default: | |
392 | return false; | |
393 | } | |
394 | case CPP_LESS: | |
395 | if (c_dialect_objc ()) | |
396 | return true; | |
397 | return false; | |
398 | default: | |
399 | return false; | |
400 | } | |
401 | } | |
402 | ||
403 | /* Return true if the next token from PARSER can start a type name, | |
404 | false otherwise. */ | |
405 | static inline bool | |
406 | c_parser_next_token_starts_typename (c_parser *parser) | |
407 | { | |
408 | c_token *token = c_parser_peek_token (parser); | |
409 | return c_token_starts_typename (token); | |
410 | } | |
411 | ||
412 | /* Return true if TOKEN can start declaration specifiers, false | |
413 | otherwise. */ | |
414 | static bool | |
415 | c_token_starts_declspecs (c_token *token) | |
416 | { | |
417 | switch (token->type) | |
418 | { | |
419 | case CPP_NAME: | |
420 | switch (token->id_kind) | |
421 | { | |
422 | case C_ID_ID: | |
423 | return false; | |
424 | case C_ID_TYPENAME: | |
425 | return true; | |
426 | case C_ID_CLASSNAME: | |
427 | gcc_assert (c_dialect_objc ()); | |
428 | return true; | |
429 | default: | |
430 | gcc_unreachable (); | |
431 | } | |
432 | case CPP_KEYWORD: | |
433 | switch (token->keyword) | |
434 | { | |
435 | case RID_STATIC: | |
436 | case RID_EXTERN: | |
437 | case RID_REGISTER: | |
438 | case RID_TYPEDEF: | |
439 | case RID_INLINE: | |
440 | case RID_AUTO: | |
441 | case RID_THREAD: | |
442 | case RID_UNSIGNED: | |
443 | case RID_LONG: | |
444 | case RID_SHORT: | |
445 | case RID_SIGNED: | |
446 | case RID_COMPLEX: | |
447 | case RID_INT: | |
448 | case RID_CHAR: | |
449 | case RID_FLOAT: | |
450 | case RID_DOUBLE: | |
451 | case RID_VOID: | |
9a8ce21f JG |
452 | case RID_DFLOAT32: |
453 | case RID_DFLOAT64: | |
454 | case RID_DFLOAT128: | |
27bf414c JM |
455 | case RID_BOOL: |
456 | case RID_ENUM: | |
457 | case RID_STRUCT: | |
458 | case RID_UNION: | |
459 | case RID_TYPEOF: | |
460 | case RID_CONST: | |
461 | case RID_VOLATILE: | |
462 | case RID_RESTRICT: | |
463 | case RID_ATTRIBUTE: | |
ab22c1fa CF |
464 | case RID_FRACT: |
465 | case RID_ACCUM: | |
466 | case RID_SAT: | |
27bf414c JM |
467 | return true; |
468 | default: | |
469 | return false; | |
470 | } | |
471 | case CPP_LESS: | |
472 | if (c_dialect_objc ()) | |
473 | return true; | |
474 | return false; | |
475 | default: | |
476 | return false; | |
477 | } | |
478 | } | |
479 | ||
480 | /* Return true if the next token from PARSER can start declaration | |
481 | specifiers, false otherwise. */ | |
482 | static inline bool | |
483 | c_parser_next_token_starts_declspecs (c_parser *parser) | |
484 | { | |
485 | c_token *token = c_parser_peek_token (parser); | |
486 | return c_token_starts_declspecs (token); | |
487 | } | |
488 | ||
489 | /* Return a pointer to the next-but-one token from PARSER, reading it | |
490 | in if necessary. The next token is already read in. */ | |
491 | ||
492 | static c_token * | |
493 | c_parser_peek_2nd_token (c_parser *parser) | |
494 | { | |
495 | if (parser->tokens_avail >= 2) | |
496 | return &parser->tokens[1]; | |
497 | gcc_assert (parser->tokens_avail == 1); | |
498 | gcc_assert (parser->tokens[0].type != CPP_EOF); | |
bc4071dd | 499 | gcc_assert (parser->tokens[0].type != CPP_PRAGMA_EOL); |
0bacb8c7 | 500 | c_lex_one_token (parser, &parser->tokens[1]); |
27bf414c JM |
501 | parser->tokens_avail = 2; |
502 | return &parser->tokens[1]; | |
503 | } | |
504 | ||
505 | /* Consume the next token from PARSER. */ | |
506 | ||
507 | static void | |
508 | c_parser_consume_token (c_parser *parser) | |
509 | { | |
bc4071dd RH |
510 | gcc_assert (parser->tokens_avail >= 1); |
511 | gcc_assert (parser->tokens[0].type != CPP_EOF); | |
512 | gcc_assert (!parser->in_pragma || parser->tokens[0].type != CPP_PRAGMA_EOL); | |
513 | gcc_assert (parser->error || parser->tokens[0].type != CPP_PRAGMA); | |
27bf414c JM |
514 | if (parser->tokens_avail == 2) |
515 | parser->tokens[0] = parser->tokens[1]; | |
27bf414c JM |
516 | parser->tokens_avail--; |
517 | } | |
518 | ||
bc4071dd RH |
519 | /* Expect the current token to be a #pragma. Consume it and remember |
520 | that we've begun parsing a pragma. */ | |
521 | ||
522 | static void | |
523 | c_parser_consume_pragma (c_parser *parser) | |
524 | { | |
525 | gcc_assert (!parser->in_pragma); | |
526 | gcc_assert (parser->tokens_avail >= 1); | |
527 | gcc_assert (parser->tokens[0].type == CPP_PRAGMA); | |
528 | if (parser->tokens_avail == 2) | |
529 | parser->tokens[0] = parser->tokens[1]; | |
530 | parser->tokens_avail--; | |
531 | parser->in_pragma = true; | |
532 | } | |
533 | ||
27bf414c JM |
534 | /* Update the globals input_location and in_system_header from |
535 | TOKEN. */ | |
536 | static inline void | |
537 | c_parser_set_source_position_from_token (c_token *token) | |
538 | { | |
539 | if (token->type != CPP_EOF) | |
540 | { | |
541 | input_location = token->location; | |
27bf414c JM |
542 | } |
543 | } | |
544 | ||
27bf414c JM |
545 | /* Issue a diagnostic of the form |
546 | FILE:LINE: MESSAGE before TOKEN | |
547 | where TOKEN is the next token in the input stream of PARSER. | |
548 | MESSAGE (specified by the caller) is usually of the form "expected | |
549 | OTHER-TOKEN". | |
550 | ||
551 | Do not issue a diagnostic if still recovering from an error. | |
552 | ||
553 | ??? This is taken from the C++ parser, but building up messages in | |
554 | this way is not i18n-friendly and some other approach should be | |
555 | used. */ | |
556 | ||
557 | static void | |
4b794eaf | 558 | c_parser_error (c_parser *parser, const char *gmsgid) |
27bf414c JM |
559 | { |
560 | c_token *token = c_parser_peek_token (parser); | |
561 | if (parser->error) | |
562 | return; | |
563 | parser->error = true; | |
4b794eaf | 564 | if (!gmsgid) |
27bf414c JM |
565 | return; |
566 | /* This diagnostic makes more sense if it is tagged to the line of | |
567 | the token we just peeked at. */ | |
568 | c_parser_set_source_position_from_token (token); | |
4b794eaf | 569 | c_parse_error (gmsgid, |
27bf414c JM |
570 | /* Because c_parse_error does not understand |
571 | CPP_KEYWORD, keywords are treated like | |
572 | identifiers. */ | |
573 | (token->type == CPP_KEYWORD ? CPP_NAME : token->type), | |
cfc93532 MLI |
574 | /* ??? The C parser does not save the cpp flags of a |
575 | token, we need to pass 0 here and we will not get | |
576 | the source spelling of some tokens but rather the | |
577 | canonical spelling. */ | |
578 | token->value, /*flags=*/0); | |
27bf414c JM |
579 | } |
580 | ||
581 | /* If the next token is of the indicated TYPE, consume it. Otherwise, | |
582 | issue the error MSGID. If MSGID is NULL then a message has already | |
583 | been produced and no message will be produced this time. Returns | |
584 | true if found, false otherwise. */ | |
585 | ||
586 | static bool | |
587 | c_parser_require (c_parser *parser, | |
588 | enum cpp_ttype type, | |
589 | const char *msgid) | |
590 | { | |
591 | if (c_parser_next_token_is (parser, type)) | |
592 | { | |
593 | c_parser_consume_token (parser); | |
594 | return true; | |
595 | } | |
596 | else | |
597 | { | |
598 | c_parser_error (parser, msgid); | |
599 | return false; | |
600 | } | |
601 | } | |
602 | ||
603 | /* If the next token is the indicated keyword, consume it. Otherwise, | |
604 | issue the error MSGID. Returns true if found, false otherwise. */ | |
605 | ||
606 | static bool | |
607 | c_parser_require_keyword (c_parser *parser, | |
608 | enum rid keyword, | |
609 | const char *msgid) | |
610 | { | |
611 | if (c_parser_next_token_is_keyword (parser, keyword)) | |
612 | { | |
613 | c_parser_consume_token (parser); | |
614 | return true; | |
615 | } | |
616 | else | |
617 | { | |
618 | c_parser_error (parser, msgid); | |
619 | return false; | |
620 | } | |
621 | } | |
622 | ||
623 | /* Like c_parser_require, except that tokens will be skipped until the | |
624 | desired token is found. An error message is still produced if the | |
625 | next token is not as expected. If MSGID is NULL then a message has | |
626 | already been produced and no message will be produced this | |
627 | time. */ | |
628 | ||
629 | static void | |
630 | c_parser_skip_until_found (c_parser *parser, | |
631 | enum cpp_ttype type, | |
632 | const char *msgid) | |
633 | { | |
634 | unsigned nesting_depth = 0; | |
635 | ||
636 | if (c_parser_require (parser, type, msgid)) | |
637 | return; | |
638 | ||
639 | /* Skip tokens until the desired token is found. */ | |
640 | while (true) | |
641 | { | |
642 | /* Peek at the next token. */ | |
643 | c_token *token = c_parser_peek_token (parser); | |
644 | /* If we've reached the token we want, consume it and stop. */ | |
645 | if (token->type == type && !nesting_depth) | |
646 | { | |
647 | c_parser_consume_token (parser); | |
648 | break; | |
649 | } | |
bc4071dd | 650 | |
27bf414c JM |
651 | /* If we've run out of tokens, stop. */ |
652 | if (token->type == CPP_EOF) | |
653 | return; | |
bc4071dd RH |
654 | if (token->type == CPP_PRAGMA_EOL && parser->in_pragma) |
655 | return; | |
27bf414c JM |
656 | if (token->type == CPP_OPEN_BRACE |
657 | || token->type == CPP_OPEN_PAREN | |
658 | || token->type == CPP_OPEN_SQUARE) | |
659 | ++nesting_depth; | |
660 | else if (token->type == CPP_CLOSE_BRACE | |
661 | || token->type == CPP_CLOSE_PAREN | |
662 | || token->type == CPP_CLOSE_SQUARE) | |
663 | { | |
664 | if (nesting_depth-- == 0) | |
665 | break; | |
666 | } | |
667 | /* Consume this token. */ | |
668 | c_parser_consume_token (parser); | |
669 | } | |
670 | parser->error = false; | |
671 | } | |
672 | ||
673 | /* Skip tokens until the end of a parameter is found, but do not | |
674 | consume the comma, semicolon or closing delimiter. */ | |
675 | ||
676 | static void | |
677 | c_parser_skip_to_end_of_parameter (c_parser *parser) | |
678 | { | |
679 | unsigned nesting_depth = 0; | |
680 | ||
681 | while (true) | |
682 | { | |
683 | c_token *token = c_parser_peek_token (parser); | |
684 | if ((token->type == CPP_COMMA || token->type == CPP_SEMICOLON) | |
685 | && !nesting_depth) | |
686 | break; | |
687 | /* If we've run out of tokens, stop. */ | |
688 | if (token->type == CPP_EOF) | |
689 | return; | |
bc4071dd RH |
690 | if (token->type == CPP_PRAGMA_EOL && parser->in_pragma) |
691 | return; | |
27bf414c JM |
692 | if (token->type == CPP_OPEN_BRACE |
693 | || token->type == CPP_OPEN_PAREN | |
694 | || token->type == CPP_OPEN_SQUARE) | |
695 | ++nesting_depth; | |
696 | else if (token->type == CPP_CLOSE_BRACE | |
697 | || token->type == CPP_CLOSE_PAREN | |
698 | || token->type == CPP_CLOSE_SQUARE) | |
699 | { | |
700 | if (nesting_depth-- == 0) | |
701 | break; | |
702 | } | |
703 | /* Consume this token. */ | |
704 | c_parser_consume_token (parser); | |
705 | } | |
706 | parser->error = false; | |
707 | } | |
708 | ||
bc4071dd RH |
709 | /* Expect to be at the end of the pragma directive and consume an |
710 | end of line marker. */ | |
711 | ||
712 | static void | |
713 | c_parser_skip_to_pragma_eol (c_parser *parser) | |
714 | { | |
715 | gcc_assert (parser->in_pragma); | |
716 | parser->in_pragma = false; | |
717 | ||
718 | if (!c_parser_require (parser, CPP_PRAGMA_EOL, "expected end of line")) | |
719 | while (true) | |
720 | { | |
721 | c_token *token = c_parser_peek_token (parser); | |
722 | if (token->type == CPP_EOF) | |
723 | break; | |
724 | if (token->type == CPP_PRAGMA_EOL) | |
725 | { | |
726 | c_parser_consume_token (parser); | |
727 | break; | |
728 | } | |
729 | c_parser_consume_token (parser); | |
730 | } | |
731 | ||
732 | parser->error = false; | |
733 | } | |
27bf414c | 734 | |
2a83cc52 RH |
735 | /* Skip tokens until we have consumed an entire block, or until we |
736 | have consumed a non-nested ';'. */ | |
737 | ||
738 | static void | |
739 | c_parser_skip_to_end_of_block_or_statement (c_parser *parser) | |
740 | { | |
741 | unsigned nesting_depth = 0; | |
742 | bool save_error = parser->error; | |
743 | ||
744 | while (true) | |
745 | { | |
746 | c_token *token; | |
747 | ||
748 | /* Peek at the next token. */ | |
749 | token = c_parser_peek_token (parser); | |
750 | ||
751 | switch (token->type) | |
752 | { | |
753 | case CPP_EOF: | |
754 | return; | |
755 | ||
756 | case CPP_PRAGMA_EOL: | |
757 | if (parser->in_pragma) | |
758 | return; | |
759 | break; | |
760 | ||
761 | case CPP_SEMICOLON: | |
762 | /* If the next token is a ';', we have reached the | |
763 | end of the statement. */ | |
764 | if (!nesting_depth) | |
765 | { | |
766 | /* Consume the ';'. */ | |
767 | c_parser_consume_token (parser); | |
768 | goto finished; | |
769 | } | |
770 | break; | |
771 | ||
772 | case CPP_CLOSE_BRACE: | |
773 | /* If the next token is a non-nested '}', then we have | |
774 | reached the end of the current block. */ | |
775 | if (nesting_depth == 0 || --nesting_depth == 0) | |
776 | { | |
777 | c_parser_consume_token (parser); | |
778 | goto finished; | |
779 | } | |
780 | break; | |
781 | ||
782 | case CPP_OPEN_BRACE: | |
783 | /* If it the next token is a '{', then we are entering a new | |
784 | block. Consume the entire block. */ | |
785 | ++nesting_depth; | |
786 | break; | |
787 | ||
788 | case CPP_PRAGMA: | |
789 | /* If we see a pragma, consume the whole thing at once. We | |
790 | have some safeguards against consuming pragmas willy-nilly. | |
791 | Normally, we'd expect to be here with parser->error set, | |
792 | which disables these safeguards. But it's possible to get | |
793 | here for secondary error recovery, after parser->error has | |
794 | been cleared. */ | |
795 | c_parser_consume_pragma (parser); | |
796 | c_parser_skip_to_pragma_eol (parser); | |
797 | parser->error = save_error; | |
798 | continue; | |
9e33de05 RS |
799 | |
800 | default: | |
801 | break; | |
2a83cc52 RH |
802 | } |
803 | ||
804 | c_parser_consume_token (parser); | |
805 | } | |
806 | ||
807 | finished: | |
808 | parser->error = false; | |
809 | } | |
810 | ||
d2e796ad MLI |
811 | /* CPP's options (initialized by c-opts.c). */ |
812 | extern cpp_options *cpp_opts; | |
813 | ||
27bf414c JM |
814 | /* Save the warning flags which are controlled by __extension__. */ |
815 | ||
816 | static inline int | |
817 | disable_extension_diagnostics (void) | |
818 | { | |
819 | int ret = (pedantic | |
820 | | (warn_pointer_arith << 1) | |
821 | | (warn_traditional << 2) | |
d2e796ad | 822 | | (flag_iso << 3) |
24b97832 ILT |
823 | | (warn_long_long << 4) |
824 | | (warn_cxx_compat << 5)); | |
d2e796ad | 825 | cpp_opts->pedantic = pedantic = 0; |
27bf414c | 826 | warn_pointer_arith = 0; |
d2e796ad | 827 | cpp_opts->warn_traditional = warn_traditional = 0; |
27bf414c | 828 | flag_iso = 0; |
9c650d90 | 829 | cpp_opts->warn_long_long = warn_long_long = 0; |
24b97832 | 830 | warn_cxx_compat = 0; |
27bf414c JM |
831 | return ret; |
832 | } | |
833 | ||
834 | /* Restore the warning flags which are controlled by __extension__. | |
835 | FLAGS is the return value from disable_extension_diagnostics. */ | |
836 | ||
837 | static inline void | |
838 | restore_extension_diagnostics (int flags) | |
839 | { | |
d2e796ad | 840 | cpp_opts->pedantic = pedantic = flags & 1; |
27bf414c | 841 | warn_pointer_arith = (flags >> 1) & 1; |
d2e796ad | 842 | cpp_opts->warn_traditional = warn_traditional = (flags >> 2) & 1; |
27bf414c | 843 | flag_iso = (flags >> 3) & 1; |
9c650d90 | 844 | cpp_opts->warn_long_long = warn_long_long = (flags >> 4) & 1; |
24b97832 | 845 | warn_cxx_compat = (flags >> 5) & 1; |
27bf414c JM |
846 | } |
847 | ||
848 | /* Possibly kinds of declarator to parse. */ | |
849 | typedef enum c_dtr_syn { | |
850 | /* A normal declarator with an identifier. */ | |
851 | C_DTR_NORMAL, | |
852 | /* An abstract declarator (maybe empty). */ | |
853 | C_DTR_ABSTRACT, | |
854 | /* A parameter declarator: may be either, but after a type name does | |
855 | not redeclare a typedef name as an identifier if it can | |
856 | alternatively be interpreted as a typedef name; see DR#009, | |
857 | applied in C90 TC1, omitted from C99 and reapplied in C99 TC2 | |
858 | following DR#249. For example, given a typedef T, "int T" and | |
859 | "int *T" are valid parameter declarations redeclaring T, while | |
860 | "int (T)" and "int * (T)" and "int (T[])" and "int (T (int))" are | |
861 | abstract declarators rather than involving redundant parentheses; | |
862 | the same applies with attributes inside the parentheses before | |
863 | "T". */ | |
864 | C_DTR_PARM | |
865 | } c_dtr_syn; | |
866 | ||
867 | static void c_parser_external_declaration (c_parser *); | |
868 | static void c_parser_asm_definition (c_parser *); | |
869 | static void c_parser_declaration_or_fndef (c_parser *, bool, bool, bool, bool); | |
870 | static void c_parser_declspecs (c_parser *, struct c_declspecs *, bool, bool, | |
871 | bool); | |
872 | static struct c_typespec c_parser_enum_specifier (c_parser *); | |
873 | static struct c_typespec c_parser_struct_or_union_specifier (c_parser *); | |
874 | static tree c_parser_struct_declaration (c_parser *); | |
875 | static struct c_typespec c_parser_typeof_specifier (c_parser *); | |
876 | static struct c_declarator *c_parser_declarator (c_parser *, bool, c_dtr_syn, | |
877 | bool *); | |
878 | static struct c_declarator *c_parser_direct_declarator (c_parser *, bool, | |
879 | c_dtr_syn, bool *); | |
880 | static struct c_declarator *c_parser_direct_declarator_inner (c_parser *, | |
881 | bool, | |
882 | struct c_declarator *); | |
883 | static struct c_arg_info *c_parser_parms_declarator (c_parser *, bool, tree); | |
884 | static struct c_arg_info *c_parser_parms_list_declarator (c_parser *, tree); | |
885 | static struct c_parm *c_parser_parameter_declaration (c_parser *, tree); | |
886 | static tree c_parser_simple_asm_expr (c_parser *); | |
887 | static tree c_parser_attributes (c_parser *); | |
888 | static struct c_type_name *c_parser_type_name (c_parser *); | |
889 | static struct c_expr c_parser_initializer (c_parser *); | |
890 | static struct c_expr c_parser_braced_init (c_parser *, tree, bool); | |
891 | static void c_parser_initelt (c_parser *); | |
892 | static void c_parser_initval (c_parser *, struct c_expr *); | |
893 | static tree c_parser_compound_statement (c_parser *); | |
894 | static void c_parser_compound_statement_nostart (c_parser *); | |
895 | static void c_parser_label (c_parser *); | |
896 | static void c_parser_statement (c_parser *); | |
897 | static void c_parser_statement_after_labels (c_parser *); | |
898 | static void c_parser_if_statement (c_parser *); | |
899 | static void c_parser_switch_statement (c_parser *); | |
900 | static void c_parser_while_statement (c_parser *); | |
901 | static void c_parser_do_statement (c_parser *); | |
902 | static void c_parser_for_statement (c_parser *); | |
903 | static tree c_parser_asm_statement (c_parser *); | |
46bdb9cf | 904 | static tree c_parser_asm_operands (c_parser *, bool); |
27bf414c JM |
905 | static tree c_parser_asm_clobbers (c_parser *); |
906 | static struct c_expr c_parser_expr_no_commas (c_parser *, struct c_expr *); | |
907 | static struct c_expr c_parser_conditional_expression (c_parser *, | |
908 | struct c_expr *); | |
909 | static struct c_expr c_parser_binary_expression (c_parser *, struct c_expr *); | |
910 | static struct c_expr c_parser_cast_expression (c_parser *, struct c_expr *); | |
911 | static struct c_expr c_parser_unary_expression (c_parser *); | |
912 | static struct c_expr c_parser_sizeof_expression (c_parser *); | |
913 | static struct c_expr c_parser_alignof_expression (c_parser *); | |
914 | static struct c_expr c_parser_postfix_expression (c_parser *); | |
915 | static struct c_expr c_parser_postfix_expression_after_paren_type (c_parser *, | |
24b97832 ILT |
916 | struct c_type_name *, |
917 | location_t); | |
27bf414c | 918 | static struct c_expr c_parser_postfix_expression_after_primary (c_parser *, |
c2255bc4 | 919 | location_t loc, |
27bf414c JM |
920 | struct c_expr); |
921 | static struct c_expr c_parser_expression (c_parser *); | |
46bdb9cf | 922 | static struct c_expr c_parser_expression_conv (c_parser *); |
bbbbb16a ILT |
923 | static VEC(tree,gc) *c_parser_expr_list (c_parser *, bool, bool, |
924 | VEC(tree,gc) **); | |
953ff289 DN |
925 | static void c_parser_omp_construct (c_parser *); |
926 | static void c_parser_omp_threadprivate (c_parser *); | |
927 | static void c_parser_omp_barrier (c_parser *); | |
928 | static void c_parser_omp_flush (c_parser *); | |
a68ab351 | 929 | static void c_parser_omp_taskwait (c_parser *); |
27bf414c | 930 | |
bc4071dd RH |
931 | enum pragma_context { pragma_external, pragma_stmt, pragma_compound }; |
932 | static bool c_parser_pragma (c_parser *, enum pragma_context); | |
933 | ||
27bf414c JM |
934 | /* These Objective-C parser functions are only ever called when |
935 | compiling Objective-C. */ | |
936 | static void c_parser_objc_class_definition (c_parser *); | |
937 | static void c_parser_objc_class_instance_variables (c_parser *); | |
938 | static void c_parser_objc_class_declaration (c_parser *); | |
939 | static void c_parser_objc_alias_declaration (c_parser *); | |
940 | static void c_parser_objc_protocol_definition (c_parser *); | |
941 | static enum tree_code c_parser_objc_method_type (c_parser *); | |
942 | static void c_parser_objc_method_definition (c_parser *); | |
943 | static void c_parser_objc_methodprotolist (c_parser *); | |
944 | static void c_parser_objc_methodproto (c_parser *); | |
945 | static tree c_parser_objc_method_decl (c_parser *); | |
946 | static tree c_parser_objc_type_name (c_parser *); | |
947 | static tree c_parser_objc_protocol_refs (c_parser *); | |
948 | static void c_parser_objc_try_catch_statement (c_parser *); | |
949 | static void c_parser_objc_synchronized_statement (c_parser *); | |
950 | static tree c_parser_objc_selector (c_parser *); | |
951 | static tree c_parser_objc_selector_arg (c_parser *); | |
952 | static tree c_parser_objc_receiver (c_parser *); | |
953 | static tree c_parser_objc_message_args (c_parser *); | |
954 | static tree c_parser_objc_keywordexpr (c_parser *); | |
955 | ||
956 | /* Parse a translation unit (C90 6.7, C99 6.9). | |
957 | ||
958 | translation-unit: | |
959 | external-declarations | |
960 | ||
961 | external-declarations: | |
962 | external-declaration | |
963 | external-declarations external-declaration | |
964 | ||
965 | GNU extensions: | |
966 | ||
967 | translation-unit: | |
968 | empty | |
969 | */ | |
970 | ||
971 | static void | |
972 | c_parser_translation_unit (c_parser *parser) | |
973 | { | |
974 | if (c_parser_next_token_is (parser, CPP_EOF)) | |
975 | { | |
509c9d60 MLI |
976 | pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic, |
977 | "ISO C forbids an empty translation unit"); | |
27bf414c JM |
978 | } |
979 | else | |
980 | { | |
981 | void *obstack_position = obstack_alloc (&parser_obstack, 0); | |
6ec637a4 | 982 | mark_valid_location_for_stdc_pragma (false); |
27bf414c JM |
983 | do |
984 | { | |
985 | ggc_collect (); | |
986 | c_parser_external_declaration (parser); | |
987 | obstack_free (&parser_obstack, obstack_position); | |
988 | } | |
989 | while (c_parser_next_token_is_not (parser, CPP_EOF)); | |
990 | } | |
991 | } | |
992 | ||
993 | /* Parse an external declaration (C90 6.7, C99 6.9). | |
994 | ||
995 | external-declaration: | |
996 | function-definition | |
997 | declaration | |
998 | ||
999 | GNU extensions: | |
1000 | ||
1001 | external-declaration: | |
1002 | asm-definition | |
1003 | ; | |
1004 | __extension__ external-declaration | |
1005 | ||
1006 | Objective-C: | |
1007 | ||
1008 | external-declaration: | |
1009 | objc-class-definition | |
1010 | objc-class-declaration | |
1011 | objc-alias-declaration | |
1012 | objc-protocol-definition | |
1013 | objc-method-definition | |
1014 | @end | |
1015 | */ | |
1016 | ||
1017 | static void | |
1018 | c_parser_external_declaration (c_parser *parser) | |
1019 | { | |
1020 | int ext; | |
1021 | switch (c_parser_peek_token (parser)->type) | |
1022 | { | |
1023 | case CPP_KEYWORD: | |
1024 | switch (c_parser_peek_token (parser)->keyword) | |
1025 | { | |
1026 | case RID_EXTENSION: | |
1027 | ext = disable_extension_diagnostics (); | |
1028 | c_parser_consume_token (parser); | |
1029 | c_parser_external_declaration (parser); | |
1030 | restore_extension_diagnostics (ext); | |
1031 | break; | |
1032 | case RID_ASM: | |
1033 | c_parser_asm_definition (parser); | |
1034 | break; | |
1035 | case RID_AT_INTERFACE: | |
1036 | case RID_AT_IMPLEMENTATION: | |
1037 | gcc_assert (c_dialect_objc ()); | |
1038 | c_parser_objc_class_definition (parser); | |
1039 | break; | |
eea1139b | 1040 | case RID_CLASS: |
27bf414c JM |
1041 | gcc_assert (c_dialect_objc ()); |
1042 | c_parser_objc_class_declaration (parser); | |
1043 | break; | |
1044 | case RID_AT_ALIAS: | |
1045 | gcc_assert (c_dialect_objc ()); | |
1046 | c_parser_objc_alias_declaration (parser); | |
1047 | break; | |
1048 | case RID_AT_PROTOCOL: | |
1049 | gcc_assert (c_dialect_objc ()); | |
1050 | c_parser_objc_protocol_definition (parser); | |
1051 | break; | |
1052 | case RID_AT_END: | |
1053 | gcc_assert (c_dialect_objc ()); | |
1054 | c_parser_consume_token (parser); | |
1055 | objc_finish_implementation (); | |
1056 | break; | |
1057 | default: | |
1058 | goto decl_or_fndef; | |
1059 | } | |
1060 | break; | |
1061 | case CPP_SEMICOLON: | |
509c9d60 MLI |
1062 | pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic, |
1063 | "ISO C does not allow extra %<;%> outside of a function"); | |
27bf414c JM |
1064 | c_parser_consume_token (parser); |
1065 | break; | |
bc4071dd | 1066 | case CPP_PRAGMA: |
6ec637a4 | 1067 | mark_valid_location_for_stdc_pragma (true); |
bc4071dd | 1068 | c_parser_pragma (parser, pragma_external); |
6ec637a4 | 1069 | mark_valid_location_for_stdc_pragma (false); |
bc4071dd | 1070 | break; |
27bf414c JM |
1071 | case CPP_PLUS: |
1072 | case CPP_MINUS: | |
1073 | if (c_dialect_objc ()) | |
1074 | { | |
1075 | c_parser_objc_method_definition (parser); | |
1076 | break; | |
1077 | } | |
1078 | /* Else fall through, and yield a syntax error trying to parse | |
1079 | as a declaration or function definition. */ | |
1080 | default: | |
1081 | decl_or_fndef: | |
1082 | /* A declaration or a function definition. We can only tell | |
1083 | which after parsing the declaration specifiers, if any, and | |
1084 | the first declarator. */ | |
1085 | c_parser_declaration_or_fndef (parser, true, true, false, true); | |
1086 | break; | |
1087 | } | |
1088 | } | |
1089 | ||
bc4071dd | 1090 | |
27bf414c JM |
1091 | /* Parse a declaration or function definition (C90 6.5, 6.7.1, C99 |
1092 | 6.7, 6.9.1). If FNDEF_OK is true, a function definition is | |
1093 | accepted; otherwise (old-style parameter declarations) only other | |
1094 | declarations are accepted. If NESTED is true, we are inside a | |
1095 | function or parsing old-style parameter declarations; any functions | |
1096 | encountered are nested functions and declaration specifiers are | |
1097 | required; otherwise we are at top level and functions are normal | |
1098 | functions and declaration specifiers may be optional. If EMPTY_OK | |
1099 | is true, empty declarations are OK (subject to all other | |
1100 | constraints); otherwise (old-style parameter declarations) they are | |
1101 | diagnosed. If START_ATTR_OK is true, the declaration specifiers | |
1102 | may start with attributes; otherwise they may not. | |
1103 | ||
1104 | declaration: | |
1105 | declaration-specifiers init-declarator-list[opt] ; | |
1106 | ||
1107 | function-definition: | |
1108 | declaration-specifiers[opt] declarator declaration-list[opt] | |
1109 | compound-statement | |
1110 | ||
1111 | declaration-list: | |
1112 | declaration | |
1113 | declaration-list declaration | |
1114 | ||
1115 | init-declarator-list: | |
1116 | init-declarator | |
1117 | init-declarator-list , init-declarator | |
1118 | ||
1119 | init-declarator: | |
1120 | declarator simple-asm-expr[opt] attributes[opt] | |
1121 | declarator simple-asm-expr[opt] attributes[opt] = initializer | |
1122 | ||
1123 | GNU extensions: | |
1124 | ||
1125 | nested-function-definition: | |
1126 | declaration-specifiers declarator declaration-list[opt] | |
1127 | compound-statement | |
1128 | ||
1129 | The simple-asm-expr and attributes are GNU extensions. | |
1130 | ||
1131 | This function does not handle __extension__; that is handled in its | |
1132 | callers. ??? Following the old parser, __extension__ may start | |
1133 | external declarations, declarations in functions and declarations | |
1134 | at the start of "for" loops, but not old-style parameter | |
1135 | declarations. | |
1136 | ||
1137 | C99 requires declaration specifiers in a function definition; the | |
1138 | absence is diagnosed through the diagnosis of implicit int. In GNU | |
1139 | C we also allow but diagnose declarations without declaration | |
1140 | specifiers, but only at top level (elsewhere they conflict with | |
953ff289 DN |
1141 | other syntax). |
1142 | ||
1143 | OpenMP: | |
1144 | ||
1145 | declaration: | |
1146 | threadprivate-directive */ | |
27bf414c JM |
1147 | |
1148 | static void | |
1149 | c_parser_declaration_or_fndef (c_parser *parser, bool fndef_ok, bool empty_ok, | |
1150 | bool nested, bool start_attr_ok) | |
1151 | { | |
1152 | struct c_declspecs *specs; | |
1153 | tree prefix_attrs; | |
1154 | tree all_prefix_attrs; | |
1155 | bool diagnosed_no_specs = false; | |
c7412148 | 1156 | location_t here = c_parser_peek_token (parser)->location; |
bc4071dd | 1157 | |
27bf414c JM |
1158 | specs = build_null_declspecs (); |
1159 | c_parser_declspecs (parser, specs, true, true, start_attr_ok); | |
1160 | if (parser->error) | |
1161 | { | |
1162 | c_parser_skip_to_end_of_block_or_statement (parser); | |
1163 | return; | |
1164 | } | |
1165 | if (nested && !specs->declspecs_seen_p) | |
1166 | { | |
1167 | c_parser_error (parser, "expected declaration specifiers"); | |
1168 | c_parser_skip_to_end_of_block_or_statement (parser); | |
1169 | return; | |
1170 | } | |
1171 | finish_declspecs (specs); | |
1172 | if (c_parser_next_token_is (parser, CPP_SEMICOLON)) | |
1173 | { | |
1174 | if (empty_ok) | |
1175 | shadow_tag (specs); | |
1176 | else | |
1177 | { | |
1178 | shadow_tag_warned (specs, 1); | |
509c9d60 | 1179 | pedwarn (here, 0, "empty declaration"); |
27bf414c JM |
1180 | } |
1181 | c_parser_consume_token (parser); | |
1182 | return; | |
1183 | } | |
1184 | pending_xref_error (); | |
1185 | prefix_attrs = specs->attrs; | |
1186 | all_prefix_attrs = prefix_attrs; | |
1187 | specs->attrs = NULL_TREE; | |
1188 | while (true) | |
1189 | { | |
1190 | struct c_declarator *declarator; | |
1191 | bool dummy = false; | |
1192 | tree fnbody; | |
1193 | /* Declaring either one or more declarators (in which case we | |
1194 | should diagnose if there were no declaration specifiers) or a | |
1195 | function definition (in which case the diagnostic for | |
1196 | implicit int suffices). */ | |
1197 | declarator = c_parser_declarator (parser, specs->type_seen_p, | |
1198 | C_DTR_NORMAL, &dummy); | |
1199 | if (declarator == NULL) | |
1200 | { | |
1201 | c_parser_skip_to_end_of_block_or_statement (parser); | |
1202 | return; | |
1203 | } | |
1204 | if (c_parser_next_token_is (parser, CPP_EQ) | |
1205 | || c_parser_next_token_is (parser, CPP_COMMA) | |
1206 | || c_parser_next_token_is (parser, CPP_SEMICOLON) | |
1207 | || c_parser_next_token_is_keyword (parser, RID_ASM) | |
1208 | || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE)) | |
1209 | { | |
1210 | tree asm_name = NULL_TREE; | |
1211 | tree postfix_attrs = NULL_TREE; | |
1212 | if (!diagnosed_no_specs && !specs->declspecs_seen_p) | |
1213 | { | |
1214 | diagnosed_no_specs = true; | |
509c9d60 | 1215 | pedwarn (here, 0, "data definition has no type or storage class"); |
27bf414c JM |
1216 | } |
1217 | /* Having seen a data definition, there cannot now be a | |
1218 | function definition. */ | |
1219 | fndef_ok = false; | |
1220 | if (c_parser_next_token_is_keyword (parser, RID_ASM)) | |
1221 | asm_name = c_parser_simple_asm_expr (parser); | |
1222 | if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE)) | |
1223 | postfix_attrs = c_parser_attributes (parser); | |
1224 | if (c_parser_next_token_is (parser, CPP_EQ)) | |
1225 | { | |
1226 | tree d; | |
1227 | struct c_expr init; | |
c2255bc4 | 1228 | location_t init_loc; |
27bf414c JM |
1229 | c_parser_consume_token (parser); |
1230 | /* The declaration of the variable is in effect while | |
1231 | its initializer is parsed. */ | |
1232 | d = start_decl (declarator, specs, true, | |
1233 | chainon (postfix_attrs, all_prefix_attrs)); | |
1234 | if (!d) | |
1235 | d = error_mark_node; | |
1236 | start_init (d, asm_name, global_bindings_p ()); | |
c2255bc4 | 1237 | init_loc = c_parser_peek_token (parser)->location; |
27bf414c JM |
1238 | init = c_parser_initializer (parser); |
1239 | finish_init (); | |
1240 | if (d != error_mark_node) | |
1241 | { | |
1242 | maybe_warn_string_init (TREE_TYPE (d), init); | |
c2255bc4 AH |
1243 | finish_decl (d, init_loc, init.value, |
1244 | init.original_type, asm_name); | |
27bf414c JM |
1245 | } |
1246 | } | |
1247 | else | |
1248 | { | |
1249 | tree d = start_decl (declarator, specs, false, | |
1250 | chainon (postfix_attrs, | |
1251 | all_prefix_attrs)); | |
1252 | if (d) | |
c2255bc4 AH |
1253 | finish_decl (d, UNKNOWN_LOCATION, NULL_TREE, |
1254 | NULL_TREE, asm_name); | |
27bf414c JM |
1255 | } |
1256 | if (c_parser_next_token_is (parser, CPP_COMMA)) | |
1257 | { | |
1258 | c_parser_consume_token (parser); | |
1259 | if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE)) | |
1260 | all_prefix_attrs = chainon (c_parser_attributes (parser), | |
1261 | prefix_attrs); | |
1262 | else | |
1263 | all_prefix_attrs = prefix_attrs; | |
1264 | continue; | |
1265 | } | |
1266 | else if (c_parser_next_token_is (parser, CPP_SEMICOLON)) | |
1267 | { | |
1268 | c_parser_consume_token (parser); | |
1269 | return; | |
1270 | } | |
1271 | else | |
1272 | { | |
1273 | c_parser_error (parser, "expected %<,%> or %<;%>"); | |
1274 | c_parser_skip_to_end_of_block_or_statement (parser); | |
1275 | return; | |
1276 | } | |
1277 | } | |
1278 | else if (!fndef_ok) | |
1279 | { | |
1280 | c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, " | |
1281 | "%<asm%> or %<__attribute__%>"); | |
1282 | c_parser_skip_to_end_of_block_or_statement (parser); | |
1283 | return; | |
1284 | } | |
1285 | /* Function definition (nested or otherwise). */ | |
1286 | if (nested) | |
1287 | { | |
509c9d60 | 1288 | pedwarn (here, OPT_pedantic, "ISO C forbids nested functions"); |
d2784db4 | 1289 | c_push_function_context (); |
27bf414c JM |
1290 | } |
1291 | if (!start_function (specs, declarator, all_prefix_attrs)) | |
1292 | { | |
1293 | /* This can appear in many cases looking nothing like a | |
1294 | function definition, so we don't give a more specific | |
1295 | error suggesting there was one. */ | |
1296 | c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, %<asm%> " | |
1297 | "or %<__attribute__%>"); | |
1298 | if (nested) | |
d2784db4 | 1299 | c_pop_function_context (); |
27bf414c JM |
1300 | break; |
1301 | } | |
1302 | /* Parse old-style parameter declarations. ??? Attributes are | |
1303 | not allowed to start declaration specifiers here because of a | |
1304 | syntax conflict between a function declaration with attribute | |
1305 | suffix and a function definition with an attribute prefix on | |
1306 | first old-style parameter declaration. Following the old | |
1307 | parser, they are not accepted on subsequent old-style | |
1308 | parameter declarations either. However, there is no | |
1309 | ambiguity after the first declaration, nor indeed on the | |
1310 | first as long as we don't allow postfix attributes after a | |
1311 | declarator with a nonempty identifier list in a definition; | |
1312 | and postfix attributes have never been accepted here in | |
1313 | function definitions either. */ | |
1314 | while (c_parser_next_token_is_not (parser, CPP_EOF) | |
1315 | && c_parser_next_token_is_not (parser, CPP_OPEN_BRACE)) | |
1316 | c_parser_declaration_or_fndef (parser, false, false, true, false); | |
27bf414c | 1317 | store_parm_decls (); |
1751ecd6 AH |
1318 | DECL_STRUCT_FUNCTION (current_function_decl)->function_start_locus |
1319 | = c_parser_peek_token (parser)->location; | |
27bf414c JM |
1320 | fnbody = c_parser_compound_statement (parser); |
1321 | if (nested) | |
1322 | { | |
1323 | tree decl = current_function_decl; | |
1324 | add_stmt (fnbody); | |
1325 | finish_function (); | |
d2784db4 | 1326 | c_pop_function_context (); |
c2255bc4 | 1327 | add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl)); |
27bf414c JM |
1328 | } |
1329 | else | |
1330 | { | |
1331 | add_stmt (fnbody); | |
1332 | finish_function (); | |
1333 | } | |
1334 | break; | |
1335 | } | |
1336 | } | |
1337 | ||
1338 | /* Parse an asm-definition (asm() outside a function body). This is a | |
1339 | GNU extension. | |
1340 | ||
1341 | asm-definition: | |
1342 | simple-asm-expr ; | |
1343 | */ | |
1344 | ||
1345 | static void | |
1346 | c_parser_asm_definition (c_parser *parser) | |
1347 | { | |
1348 | tree asm_str = c_parser_simple_asm_expr (parser); | |
27bf414c | 1349 | if (asm_str) |
474eccc6 | 1350 | cgraph_add_asm_node (asm_str); |
27bf414c JM |
1351 | c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>"); |
1352 | } | |
1353 | ||
1354 | /* Parse some declaration specifiers (possibly none) (C90 6.5, C99 | |
1355 | 6.7), adding them to SPECS (which may already include some). | |
1356 | Storage class specifiers are accepted iff SCSPEC_OK; type | |
1357 | specifiers are accepted iff TYPESPEC_OK; attributes are accepted at | |
1358 | the start iff START_ATTR_OK. | |
1359 | ||
1360 | declaration-specifiers: | |
1361 | storage-class-specifier declaration-specifiers[opt] | |
1362 | type-specifier declaration-specifiers[opt] | |
1363 | type-qualifier declaration-specifiers[opt] | |
1364 | function-specifier declaration-specifiers[opt] | |
1365 | ||
1366 | Function specifiers (inline) are from C99, and are currently | |
1367 | handled as storage class specifiers, as is __thread. | |
1368 | ||
1369 | C90 6.5.1, C99 6.7.1: | |
1370 | storage-class-specifier: | |
1371 | typedef | |
1372 | extern | |
1373 | static | |
1374 | auto | |
1375 | register | |
1376 | ||
1377 | C99 6.7.4: | |
1378 | function-specifier: | |
1379 | inline | |
1380 | ||
1381 | C90 6.5.2, C99 6.7.2: | |
1382 | type-specifier: | |
1383 | void | |
1384 | char | |
1385 | short | |
1386 | int | |
1387 | long | |
1388 | float | |
1389 | double | |
1390 | signed | |
1391 | unsigned | |
1392 | _Bool | |
1393 | _Complex | |
1394 | [_Imaginary removed in C99 TC2] | |
1395 | struct-or-union-specifier | |
1396 | enum-specifier | |
1397 | typedef-name | |
1398 | ||
1399 | (_Bool and _Complex are new in C99.) | |
1400 | ||
1401 | C90 6.5.3, C99 6.7.3: | |
1402 | ||
1403 | type-qualifier: | |
1404 | const | |
1405 | restrict | |
1406 | volatile | |
1407 | ||
1408 | (restrict is new in C99.) | |
1409 | ||
1410 | GNU extensions: | |
1411 | ||
1412 | declaration-specifiers: | |
1413 | attributes declaration-specifiers[opt] | |
1414 | ||
1415 | storage-class-specifier: | |
1416 | __thread | |
1417 | ||
1418 | type-specifier: | |
1419 | typeof-specifier | |
9a8ce21f JG |
1420 | _Decimal32 |
1421 | _Decimal64 | |
1422 | _Decimal128 | |
ab22c1fa CF |
1423 | _Fract |
1424 | _Accum | |
1425 | _Sat | |
1426 | ||
1427 | (_Fract, _Accum, and _Sat are new from ISO/IEC DTR 18037: | |
1428 | http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1169.pdf) | |
27bf414c JM |
1429 | |
1430 | Objective-C: | |
1431 | ||
1432 | type-specifier: | |
1433 | class-name objc-protocol-refs[opt] | |
1434 | typedef-name objc-protocol-refs | |
1435 | objc-protocol-refs | |
1436 | */ | |
1437 | ||
1438 | static void | |
1439 | c_parser_declspecs (c_parser *parser, struct c_declspecs *specs, | |
1440 | bool scspec_ok, bool typespec_ok, bool start_attr_ok) | |
1441 | { | |
1442 | bool attrs_ok = start_attr_ok; | |
1443 | bool seen_type = specs->type_seen_p; | |
1444 | while (c_parser_next_token_is (parser, CPP_NAME) | |
1445 | || c_parser_next_token_is (parser, CPP_KEYWORD) | |
1446 | || (c_dialect_objc () && c_parser_next_token_is (parser, CPP_LESS))) | |
1447 | { | |
1448 | struct c_typespec t; | |
1449 | tree attrs; | |
dc491a25 | 1450 | location_t loc = c_parser_peek_token (parser)->location; |
27bf414c JM |
1451 | if (c_parser_next_token_is (parser, CPP_NAME)) |
1452 | { | |
1453 | tree value = c_parser_peek_token (parser)->value; | |
1454 | c_id_kind kind = c_parser_peek_token (parser)->id_kind; | |
1455 | /* This finishes the specifiers unless a type name is OK, it | |
1456 | is declared as a type name and a type name hasn't yet | |
1457 | been seen. */ | |
1458 | if (!typespec_ok || seen_type | |
1459 | || (kind != C_ID_TYPENAME && kind != C_ID_CLASSNAME)) | |
1460 | break; | |
1461 | c_parser_consume_token (parser); | |
1462 | seen_type = true; | |
1463 | attrs_ok = true; | |
1464 | if (kind == C_ID_TYPENAME | |
1465 | && (!c_dialect_objc () | |
1466 | || c_parser_next_token_is_not (parser, CPP_LESS))) | |
1467 | { | |
1468 | t.kind = ctsk_typedef; | |
1469 | /* For a typedef name, record the meaning, not the name. | |
1470 | In case of 'foo foo, bar;'. */ | |
1471 | t.spec = lookup_name (value); | |
928c19bb JM |
1472 | t.expr = NULL_TREE; |
1473 | t.expr_const_operands = true; | |
27bf414c JM |
1474 | } |
1475 | else | |
1476 | { | |
1477 | tree proto = NULL_TREE; | |
1478 | gcc_assert (c_dialect_objc ()); | |
1479 | t.kind = ctsk_objc; | |
1480 | if (c_parser_next_token_is (parser, CPP_LESS)) | |
1481 | proto = c_parser_objc_protocol_refs (parser); | |
1482 | t.spec = objc_get_protocol_qualified_type (value, proto); | |
928c19bb JM |
1483 | t.expr = NULL_TREE; |
1484 | t.expr_const_operands = true; | |
27bf414c | 1485 | } |
dc491a25 | 1486 | declspecs_add_type (loc, specs, t); |
27bf414c JM |
1487 | continue; |
1488 | } | |
1489 | if (c_parser_next_token_is (parser, CPP_LESS)) | |
1490 | { | |
1491 | /* Make "<SomeProtocol>" equivalent to "id <SomeProtocol>" - | |
1492 | nisse@lysator.liu.se. */ | |
1493 | tree proto; | |
1494 | gcc_assert (c_dialect_objc ()); | |
1495 | if (!typespec_ok || seen_type) | |
1496 | break; | |
1497 | proto = c_parser_objc_protocol_refs (parser); | |
1498 | t.kind = ctsk_objc; | |
1499 | t.spec = objc_get_protocol_qualified_type (NULL_TREE, proto); | |
928c19bb JM |
1500 | t.expr = NULL_TREE; |
1501 | t.expr_const_operands = true; | |
dc491a25 | 1502 | declspecs_add_type (loc, specs, t); |
27bf414c JM |
1503 | continue; |
1504 | } | |
1505 | gcc_assert (c_parser_next_token_is (parser, CPP_KEYWORD)); | |
1506 | switch (c_parser_peek_token (parser)->keyword) | |
1507 | { | |
1508 | case RID_STATIC: | |
1509 | case RID_EXTERN: | |
1510 | case RID_REGISTER: | |
1511 | case RID_TYPEDEF: | |
1512 | case RID_INLINE: | |
1513 | case RID_AUTO: | |
1514 | case RID_THREAD: | |
1515 | if (!scspec_ok) | |
1516 | goto out; | |
1517 | attrs_ok = true; | |
1518 | /* TODO: Distinguish between function specifiers (inline) | |
1519 | and storage class specifiers, either here or in | |
1520 | declspecs_add_scspec. */ | |
1521 | declspecs_add_scspec (specs, c_parser_peek_token (parser)->value); | |
1522 | c_parser_consume_token (parser); | |
1523 | break; | |
1524 | case RID_UNSIGNED: | |
1525 | case RID_LONG: | |
1526 | case RID_SHORT: | |
1527 | case RID_SIGNED: | |
1528 | case RID_COMPLEX: | |
1529 | case RID_INT: | |
1530 | case RID_CHAR: | |
1531 | case RID_FLOAT: | |
1532 | case RID_DOUBLE: | |
1533 | case RID_VOID: | |
9a8ce21f JG |
1534 | case RID_DFLOAT32: |
1535 | case RID_DFLOAT64: | |
1536 | case RID_DFLOAT128: | |
27bf414c | 1537 | case RID_BOOL: |
ab22c1fa CF |
1538 | case RID_FRACT: |
1539 | case RID_ACCUM: | |
1540 | case RID_SAT: | |
27bf414c JM |
1541 | if (!typespec_ok) |
1542 | goto out; | |
1543 | attrs_ok = true; | |
1544 | seen_type = true; | |
0bacb8c7 TT |
1545 | if (c_dialect_objc ()) |
1546 | parser->objc_need_raw_identifier = true; | |
27bf414c JM |
1547 | t.kind = ctsk_resword; |
1548 | t.spec = c_parser_peek_token (parser)->value; | |
928c19bb JM |
1549 | t.expr = NULL_TREE; |
1550 | t.expr_const_operands = true; | |
dc491a25 | 1551 | declspecs_add_type (loc, specs, t); |
27bf414c JM |
1552 | c_parser_consume_token (parser); |
1553 | break; | |
1554 | case RID_ENUM: | |
1555 | if (!typespec_ok) | |
1556 | goto out; | |
1557 | attrs_ok = true; | |
1558 | seen_type = true; | |
1559 | t = c_parser_enum_specifier (parser); | |
dc491a25 | 1560 | declspecs_add_type (loc, specs, t); |
27bf414c JM |
1561 | break; |
1562 | case RID_STRUCT: | |
1563 | case RID_UNION: | |
1564 | if (!typespec_ok) | |
1565 | goto out; | |
1566 | attrs_ok = true; | |
1567 | seen_type = true; | |
1568 | t = c_parser_struct_or_union_specifier (parser); | |
dab71827 | 1569 | invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, t.spec); |
dc491a25 | 1570 | declspecs_add_type (loc, specs, t); |
27bf414c JM |
1571 | break; |
1572 | case RID_TYPEOF: | |
1573 | /* ??? The old parser rejected typeof after other type | |
1574 | specifiers, but is a syntax error the best way of | |
1575 | handling this? */ | |
1576 | if (!typespec_ok || seen_type) | |
1577 | goto out; | |
1578 | attrs_ok = true; | |
1579 | seen_type = true; | |
1580 | t = c_parser_typeof_specifier (parser); | |
dc491a25 | 1581 | declspecs_add_type (loc, specs, t); |
27bf414c JM |
1582 | break; |
1583 | case RID_CONST: | |
1584 | case RID_VOLATILE: | |
1585 | case RID_RESTRICT: | |
1586 | attrs_ok = true; | |
1587 | declspecs_add_qual (specs, c_parser_peek_token (parser)->value); | |
1588 | c_parser_consume_token (parser); | |
1589 | break; | |
1590 | case RID_ATTRIBUTE: | |
1591 | if (!attrs_ok) | |
1592 | goto out; | |
1593 | attrs = c_parser_attributes (parser); | |
1594 | declspecs_add_attrs (specs, attrs); | |
1595 | break; | |
1596 | default: | |
1597 | goto out; | |
1598 | } | |
1599 | } | |
1600 | out: ; | |
1601 | } | |
1602 | ||
1603 | /* Parse an enum specifier (C90 6.5.2.2, C99 6.7.2.2). | |
1604 | ||
1605 | enum-specifier: | |
1606 | enum attributes[opt] identifier[opt] { enumerator-list } attributes[opt] | |
1607 | enum attributes[opt] identifier[opt] { enumerator-list , } attributes[opt] | |
1608 | enum attributes[opt] identifier | |
1609 | ||
1610 | The form with trailing comma is new in C99. The forms with | |
1611 | attributes are GNU extensions. In GNU C, we accept any expression | |
1612 | without commas in the syntax (assignment expressions, not just | |
1613 | conditional expressions); assignment expressions will be diagnosed | |
1614 | as non-constant. | |
1615 | ||
1616 | enumerator-list: | |
1617 | enumerator | |
1618 | enumerator-list , enumerator | |
1619 | ||
1620 | enumerator: | |
1621 | enumeration-constant | |
1622 | enumeration-constant = constant-expression | |
1623 | */ | |
1624 | ||
1625 | static struct c_typespec | |
1626 | c_parser_enum_specifier (c_parser *parser) | |
1627 | { | |
1628 | struct c_typespec ret; | |
1629 | tree attrs; | |
1630 | tree ident = NULL_TREE; | |
24b97832 | 1631 | location_t enum_loc; |
922f2908 | 1632 | location_t ident_loc = UNKNOWN_LOCATION; /* Quiet warning. */ |
27bf414c | 1633 | gcc_assert (c_parser_next_token_is_keyword (parser, RID_ENUM)); |
24b97832 | 1634 | enum_loc = c_parser_peek_token (parser)->location; |
27bf414c JM |
1635 | c_parser_consume_token (parser); |
1636 | attrs = c_parser_attributes (parser); | |
c2255bc4 | 1637 | enum_loc = c_parser_peek_token (parser)->location; |
5af28c74 TT |
1638 | /* Set the location in case we create a decl now. */ |
1639 | c_parser_set_source_position_from_token (c_parser_peek_token (parser)); | |
27bf414c JM |
1640 | if (c_parser_next_token_is (parser, CPP_NAME)) |
1641 | { | |
1642 | ident = c_parser_peek_token (parser)->value; | |
c7412148 | 1643 | ident_loc = c_parser_peek_token (parser)->location; |
24b97832 | 1644 | enum_loc = ident_loc; |
27bf414c JM |
1645 | c_parser_consume_token (parser); |
1646 | } | |
1647 | if (c_parser_next_token_is (parser, CPP_OPEN_BRACE)) | |
1648 | { | |
1649 | /* Parse an enum definition. */ | |
7114359f | 1650 | struct c_enum_contents the_enum; |
c2255bc4 | 1651 | tree type = start_enum (enum_loc, &the_enum, ident); |
27bf414c JM |
1652 | tree postfix_attrs; |
1653 | /* We chain the enumerators in reverse order, then put them in | |
1654 | forward order at the end. */ | |
1655 | tree values = NULL_TREE; | |
1656 | c_parser_consume_token (parser); | |
1657 | while (true) | |
1658 | { | |
1659 | tree enum_id; | |
1660 | tree enum_value; | |
1661 | tree enum_decl; | |
1662 | bool seen_comma; | |
5af28c74 | 1663 | c_token *token; |
922f2908 | 1664 | location_t comma_loc = UNKNOWN_LOCATION; /* Quiet warning. */ |
85790e66 | 1665 | location_t value_loc; |
27bf414c JM |
1666 | if (c_parser_next_token_is_not (parser, CPP_NAME)) |
1667 | { | |
1668 | c_parser_error (parser, "expected identifier"); | |
1669 | c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL); | |
1670 | values = error_mark_node; | |
1671 | break; | |
1672 | } | |
5af28c74 TT |
1673 | token = c_parser_peek_token (parser); |
1674 | enum_id = token->value; | |
1675 | /* Set the location in case we create a decl now. */ | |
1676 | c_parser_set_source_position_from_token (token); | |
85790e66 | 1677 | value_loc = token->location; |
27bf414c JM |
1678 | c_parser_consume_token (parser); |
1679 | if (c_parser_next_token_is (parser, CPP_EQ)) | |
1680 | { | |
1681 | c_parser_consume_token (parser); | |
85790e66 | 1682 | value_loc = c_parser_peek_token (parser)->location; |
27bf414c JM |
1683 | enum_value = c_parser_expr_no_commas (parser, NULL).value; |
1684 | } | |
1685 | else | |
1686 | enum_value = NULL_TREE; | |
c2255bc4 AH |
1687 | enum_decl = build_enumerator (value_loc, |
1688 | &the_enum, enum_id, enum_value); | |
27bf414c JM |
1689 | TREE_CHAIN (enum_decl) = values; |
1690 | values = enum_decl; | |
1691 | seen_comma = false; | |
1692 | if (c_parser_next_token_is (parser, CPP_COMMA)) | |
1693 | { | |
c7412148 | 1694 | comma_loc = c_parser_peek_token (parser)->location; |
27bf414c JM |
1695 | seen_comma = true; |
1696 | c_parser_consume_token (parser); | |
1697 | } | |
1698 | if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE)) | |
1699 | { | |
fcf73884 | 1700 | if (seen_comma && !flag_isoc99) |
509c9d60 | 1701 | pedwarn (comma_loc, OPT_pedantic, "comma at end of enumerator list"); |
27bf414c JM |
1702 | c_parser_consume_token (parser); |
1703 | break; | |
1704 | } | |
1705 | if (!seen_comma) | |
1706 | { | |
1707 | c_parser_error (parser, "expected %<,%> or %<}%>"); | |
1708 | c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL); | |
1709 | values = error_mark_node; | |
1710 | break; | |
1711 | } | |
1712 | } | |
1713 | postfix_attrs = c_parser_attributes (parser); | |
1714 | ret.spec = finish_enum (type, nreverse (values), | |
1715 | chainon (attrs, postfix_attrs)); | |
1716 | ret.kind = ctsk_tagdef; | |
928c19bb JM |
1717 | ret.expr = NULL_TREE; |
1718 | ret.expr_const_operands = true; | |
27bf414c JM |
1719 | return ret; |
1720 | } | |
1721 | else if (!ident) | |
1722 | { | |
1723 | c_parser_error (parser, "expected %<{%>"); | |
1724 | ret.spec = error_mark_node; | |
1725 | ret.kind = ctsk_tagref; | |
928c19bb JM |
1726 | ret.expr = NULL_TREE; |
1727 | ret.expr_const_operands = true; | |
27bf414c JM |
1728 | return ret; |
1729 | } | |
c2255bc4 | 1730 | ret = parser_xref_tag (ident_loc, ENUMERAL_TYPE, ident); |
27bf414c JM |
1731 | /* In ISO C, enumerated types can be referred to only if already |
1732 | defined. */ | |
1733 | if (pedantic && !COMPLETE_TYPE_P (ret.spec)) | |
c7412148 TT |
1734 | { |
1735 | gcc_assert (ident); | |
c2255bc4 | 1736 | pedwarn (enum_loc, OPT_pedantic, |
509c9d60 | 1737 | "ISO C forbids forward references to %<enum%> types"); |
c7412148 | 1738 | } |
27bf414c JM |
1739 | return ret; |
1740 | } | |
1741 | ||
1742 | /* Parse a struct or union specifier (C90 6.5.2.1, C99 6.7.2.1). | |
1743 | ||
1744 | struct-or-union-specifier: | |
1745 | struct-or-union attributes[opt] identifier[opt] | |
1746 | { struct-contents } attributes[opt] | |
1747 | struct-or-union attributes[opt] identifier | |
1748 | ||
1749 | struct-contents: | |
1750 | struct-declaration-list | |
1751 | ||
1752 | struct-declaration-list: | |
1753 | struct-declaration ; | |
1754 | struct-declaration-list struct-declaration ; | |
1755 | ||
1756 | GNU extensions: | |
1757 | ||
1758 | struct-contents: | |
1759 | empty | |
1760 | struct-declaration | |
1761 | struct-declaration-list struct-declaration | |
1762 | ||
1763 | struct-declaration-list: | |
1764 | struct-declaration-list ; | |
1765 | ; | |
1766 | ||
1767 | (Note that in the syntax here, unlike that in ISO C, the semicolons | |
1768 | are included here rather than in struct-declaration, in order to | |
1769 | describe the syntax with extra semicolons and missing semicolon at | |
1770 | end.) | |
1771 | ||
1772 | Objective-C: | |
1773 | ||
1774 | struct-declaration-list: | |
1775 | @defs ( class-name ) | |
1776 | ||
1777 | (Note this does not include a trailing semicolon, but can be | |
1778 | followed by further declarations, and gets a pedwarn-if-pedantic | |
1779 | when followed by a semicolon.) */ | |
1780 | ||
1781 | static struct c_typespec | |
1782 | c_parser_struct_or_union_specifier (c_parser *parser) | |
1783 | { | |
1784 | struct c_typespec ret; | |
1785 | tree attrs; | |
1786 | tree ident = NULL_TREE; | |
24b97832 ILT |
1787 | location_t struct_loc; |
1788 | location_t ident_loc = UNKNOWN_LOCATION; | |
27bf414c JM |
1789 | enum tree_code code; |
1790 | switch (c_parser_peek_token (parser)->keyword) | |
1791 | { | |
1792 | case RID_STRUCT: | |
1793 | code = RECORD_TYPE; | |
1794 | break; | |
1795 | case RID_UNION: | |
1796 | code = UNION_TYPE; | |
1797 | break; | |
1798 | default: | |
1799 | gcc_unreachable (); | |
1800 | } | |
24b97832 | 1801 | struct_loc = c_parser_peek_token (parser)->location; |
27bf414c JM |
1802 | c_parser_consume_token (parser); |
1803 | attrs = c_parser_attributes (parser); | |
c2255bc4 | 1804 | |
5af28c74 TT |
1805 | /* Set the location in case we create a decl now. */ |
1806 | c_parser_set_source_position_from_token (c_parser_peek_token (parser)); | |
c2255bc4 | 1807 | |
27bf414c JM |
1808 | if (c_parser_next_token_is (parser, CPP_NAME)) |
1809 | { | |
1810 | ident = c_parser_peek_token (parser)->value; | |
24b97832 ILT |
1811 | ident_loc = c_parser_peek_token (parser)->location; |
1812 | struct_loc = ident_loc; | |
27bf414c JM |
1813 | c_parser_consume_token (parser); |
1814 | } | |
1815 | if (c_parser_next_token_is (parser, CPP_OPEN_BRACE)) | |
1816 | { | |
1817 | /* Parse a struct or union definition. Start the scope of the | |
1818 | tag before parsing components. */ | |
dc491a25 ILT |
1819 | struct c_struct_parse_info *struct_info; |
1820 | tree type = start_struct (struct_loc, code, ident, &struct_info); | |
27bf414c JM |
1821 | tree postfix_attrs; |
1822 | /* We chain the components in reverse order, then put them in | |
1823 | forward order at the end. Each struct-declaration may | |
1824 | declare multiple components (comma-separated), so we must use | |
1825 | chainon to join them, although when parsing each | |
1826 | struct-declaration we can use TREE_CHAIN directly. | |
1827 | ||
1828 | The theory behind all this is that there will be more | |
1829 | semicolon separated fields than comma separated fields, and | |
1830 | so we'll be minimizing the number of node traversals required | |
1831 | by chainon. */ | |
1832 | tree contents = NULL_TREE; | |
1833 | c_parser_consume_token (parser); | |
1834 | /* Handle the Objective-C @defs construct, | |
1835 | e.g. foo(sizeof(struct{ @defs(ClassName) }));. */ | |
1836 | if (c_parser_next_token_is_keyword (parser, RID_AT_DEFS)) | |
1837 | { | |
1838 | tree name; | |
1839 | gcc_assert (c_dialect_objc ()); | |
1840 | c_parser_consume_token (parser); | |
1841 | if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>")) | |
1842 | goto end_at_defs; | |
1843 | if (c_parser_next_token_is (parser, CPP_NAME) | |
1844 | && c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME) | |
1845 | { | |
1846 | name = c_parser_peek_token (parser)->value; | |
1847 | c_parser_consume_token (parser); | |
1848 | } | |
1849 | else | |
1850 | { | |
1851 | c_parser_error (parser, "expected class name"); | |
1852 | c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL); | |
1853 | goto end_at_defs; | |
1854 | } | |
1855 | c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, | |
1856 | "expected %<)%>"); | |
1857 | contents = nreverse (objc_get_class_ivars (name)); | |
1858 | } | |
1859 | end_at_defs: | |
1860 | /* Parse the struct-declarations and semicolons. Problems with | |
1861 | semicolons are diagnosed here; empty structures are diagnosed | |
1862 | elsewhere. */ | |
1863 | while (true) | |
1864 | { | |
1865 | tree decls; | |
1866 | /* Parse any stray semicolon. */ | |
1867 | if (c_parser_next_token_is (parser, CPP_SEMICOLON)) | |
1868 | { | |
509c9d60 MLI |
1869 | pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic, |
1870 | "extra semicolon in struct or union specified"); | |
27bf414c JM |
1871 | c_parser_consume_token (parser); |
1872 | continue; | |
1873 | } | |
1874 | /* Stop if at the end of the struct or union contents. */ | |
1875 | if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE)) | |
1876 | { | |
1877 | c_parser_consume_token (parser); | |
1878 | break; | |
1879 | } | |
bc4071dd RH |
1880 | /* Accept #pragmas at struct scope. */ |
1881 | if (c_parser_next_token_is (parser, CPP_PRAGMA)) | |
1882 | { | |
1883 | c_parser_pragma (parser, pragma_external); | |
1884 | continue; | |
1885 | } | |
27bf414c JM |
1886 | /* Parse some comma-separated declarations, but not the |
1887 | trailing semicolon if any. */ | |
1888 | decls = c_parser_struct_declaration (parser); | |
1889 | contents = chainon (decls, contents); | |
1890 | /* If no semicolon follows, either we have a parse error or | |
1891 | are at the end of the struct or union and should | |
1892 | pedwarn. */ | |
1893 | if (c_parser_next_token_is (parser, CPP_SEMICOLON)) | |
1894 | c_parser_consume_token (parser); | |
1895 | else | |
1896 | { | |
1897 | if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE)) | |
509c9d60 MLI |
1898 | pedwarn (c_parser_peek_token (parser)->location, 0, |
1899 | "no semicolon at end of struct or union"); | |
27bf414c JM |
1900 | else |
1901 | { | |
1902 | c_parser_error (parser, "expected %<;%>"); | |
1903 | c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL); | |
1904 | break; | |
1905 | } | |
1906 | } | |
1907 | } | |
1908 | postfix_attrs = c_parser_attributes (parser); | |
c2255bc4 | 1909 | ret.spec = finish_struct (struct_loc, type, nreverse (contents), |
dc491a25 | 1910 | chainon (attrs, postfix_attrs), struct_info); |
27bf414c | 1911 | ret.kind = ctsk_tagdef; |
928c19bb JM |
1912 | ret.expr = NULL_TREE; |
1913 | ret.expr_const_operands = true; | |
27bf414c JM |
1914 | return ret; |
1915 | } | |
1916 | else if (!ident) | |
1917 | { | |
1918 | c_parser_error (parser, "expected %<{%>"); | |
1919 | ret.spec = error_mark_node; | |
1920 | ret.kind = ctsk_tagref; | |
928c19bb JM |
1921 | ret.expr = NULL_TREE; |
1922 | ret.expr_const_operands = true; | |
67c2939d | 1923 | return ret; |
27bf414c | 1924 | } |
c2255bc4 | 1925 | ret = parser_xref_tag (ident_loc, code, ident); |
27bf414c JM |
1926 | return ret; |
1927 | } | |
1928 | ||
1929 | /* Parse a struct-declaration (C90 6.5.2.1, C99 6.7.2.1), *without* | |
1930 | the trailing semicolon. | |
1931 | ||
1932 | struct-declaration: | |
1933 | specifier-qualifier-list struct-declarator-list | |
1934 | ||
1935 | specifier-qualifier-list: | |
1936 | type-specifier specifier-qualifier-list[opt] | |
1937 | type-qualifier specifier-qualifier-list[opt] | |
1938 | attributes specifier-qualifier-list[opt] | |
1939 | ||
1940 | struct-declarator-list: | |
1941 | struct-declarator | |
1942 | struct-declarator-list , attributes[opt] struct-declarator | |
1943 | ||
1944 | struct-declarator: | |
1945 | declarator attributes[opt] | |
1946 | declarator[opt] : constant-expression attributes[opt] | |
1947 | ||
1948 | GNU extensions: | |
1949 | ||
1950 | struct-declaration: | |
1951 | __extension__ struct-declaration | |
1952 | specifier-qualifier-list | |
1953 | ||
1954 | Unlike the ISO C syntax, semicolons are handled elsewhere. The use | |
1955 | of attributes where shown is a GNU extension. In GNU C, we accept | |
1956 | any expression without commas in the syntax (assignment | |
1957 | expressions, not just conditional expressions); assignment | |
1958 | expressions will be diagnosed as non-constant. */ | |
1959 | ||
1960 | static tree | |
1961 | c_parser_struct_declaration (c_parser *parser) | |
1962 | { | |
1963 | struct c_declspecs *specs; | |
1964 | tree prefix_attrs; | |
1965 | tree all_prefix_attrs; | |
1966 | tree decls; | |
c7412148 | 1967 | location_t decl_loc; |
27bf414c JM |
1968 | if (c_parser_next_token_is_keyword (parser, RID_EXTENSION)) |
1969 | { | |
1970 | int ext; | |
1971 | tree decl; | |
1972 | ext = disable_extension_diagnostics (); | |
1973 | c_parser_consume_token (parser); | |
1974 | decl = c_parser_struct_declaration (parser); | |
1975 | restore_extension_diagnostics (ext); | |
1976 | return decl; | |
1977 | } | |
1978 | specs = build_null_declspecs (); | |
c7412148 | 1979 | decl_loc = c_parser_peek_token (parser)->location; |
27bf414c JM |
1980 | c_parser_declspecs (parser, specs, false, true, true); |
1981 | if (parser->error) | |
67c2939d | 1982 | return NULL_TREE; |
27bf414c JM |
1983 | if (!specs->declspecs_seen_p) |
1984 | { | |
1985 | c_parser_error (parser, "expected specifier-qualifier-list"); | |
1986 | return NULL_TREE; | |
1987 | } | |
1988 | finish_declspecs (specs); | |
1989 | if (c_parser_next_token_is (parser, CPP_SEMICOLON)) | |
1990 | { | |
1991 | tree ret; | |
1992 | if (!specs->type_seen_p) | |
1993 | { | |
509c9d60 MLI |
1994 | pedwarn (decl_loc, OPT_pedantic, |
1995 | "ISO C forbids member declarations with no members"); | |
27bf414c JM |
1996 | shadow_tag_warned (specs, pedantic); |
1997 | ret = NULL_TREE; | |
1998 | } | |
1999 | else | |
2000 | { | |
2001 | /* Support for unnamed structs or unions as members of | |
2002 | structs or unions (which is [a] useful and [b] supports | |
2003 | MS P-SDK). */ | |
b9baeecd | 2004 | tree attrs = NULL; |
3d10ed6c AH |
2005 | |
2006 | ret = grokfield (c_parser_peek_token (parser)->location, | |
2007 | build_id_declarator (NULL_TREE), specs, | |
b9baeecd | 2008 | NULL_TREE, &attrs); |
0ad7e054 RS |
2009 | if (ret) |
2010 | decl_attributes (&ret, attrs, 0); | |
27bf414c JM |
2011 | } |
2012 | return ret; | |
2013 | } | |
2014 | pending_xref_error (); | |
2015 | prefix_attrs = specs->attrs; | |
2016 | all_prefix_attrs = prefix_attrs; | |
2017 | specs->attrs = NULL_TREE; | |
2018 | decls = NULL_TREE; | |
2019 | while (true) | |
2020 | { | |
2021 | /* Declaring one or more declarators or un-named bit-fields. */ | |
2022 | struct c_declarator *declarator; | |
2023 | bool dummy = false; | |
2024 | if (c_parser_next_token_is (parser, CPP_COLON)) | |
2025 | declarator = build_id_declarator (NULL_TREE); | |
2026 | else | |
2027 | declarator = c_parser_declarator (parser, specs->type_seen_p, | |
2028 | C_DTR_NORMAL, &dummy); | |
2029 | if (declarator == NULL) | |
2030 | { | |
2031 | c_parser_skip_to_end_of_block_or_statement (parser); | |
2032 | break; | |
2033 | } | |
2034 | if (c_parser_next_token_is (parser, CPP_COLON) | |
2035 | || c_parser_next_token_is (parser, CPP_COMMA) | |
2036 | || c_parser_next_token_is (parser, CPP_SEMICOLON) | |
2037 | || c_parser_next_token_is (parser, CPP_CLOSE_BRACE) | |
2038 | || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE)) | |
2039 | { | |
2040 | tree postfix_attrs = NULL_TREE; | |
2041 | tree width = NULL_TREE; | |
2042 | tree d; | |
2043 | if (c_parser_next_token_is (parser, CPP_COLON)) | |
2044 | { | |
2045 | c_parser_consume_token (parser); | |
2046 | width = c_parser_expr_no_commas (parser, NULL).value; | |
2047 | } | |
2048 | if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE)) | |
2049 | postfix_attrs = c_parser_attributes (parser); | |
3d10ed6c AH |
2050 | d = grokfield (c_parser_peek_token (parser)->location, |
2051 | declarator, specs, width, &all_prefix_attrs); | |
27bf414c JM |
2052 | decl_attributes (&d, chainon (postfix_attrs, |
2053 | all_prefix_attrs), 0); | |
2054 | TREE_CHAIN (d) = decls; | |
2055 | decls = d; | |
2056 | if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE)) | |
2057 | all_prefix_attrs = chainon (c_parser_attributes (parser), | |
2058 | prefix_attrs); | |
2059 | else | |
2060 | all_prefix_attrs = prefix_attrs; | |
2061 | if (c_parser_next_token_is (parser, CPP_COMMA)) | |
2062 | c_parser_consume_token (parser); | |
2063 | else if (c_parser_next_token_is (parser, CPP_SEMICOLON) | |
2064 | || c_parser_next_token_is (parser, CPP_CLOSE_BRACE)) | |
2065 | { | |
2066 | /* Semicolon consumed in caller. */ | |
2067 | break; | |
2068 | } | |
2069 | else | |
2070 | { | |
2071 | c_parser_error (parser, "expected %<,%>, %<;%> or %<}%>"); | |
2072 | break; | |
2073 | } | |
2074 | } | |
2075 | else | |
2076 | { | |
2077 | c_parser_error (parser, | |
2078 | "expected %<:%>, %<,%>, %<;%>, %<}%> or " | |
2079 | "%<__attribute__%>"); | |
2080 | break; | |
2081 | } | |
2082 | } | |
2083 | return decls; | |
2084 | } | |
2085 | ||
2086 | /* Parse a typeof specifier (a GNU extension). | |
2087 | ||
2088 | typeof-specifier: | |
2089 | typeof ( expression ) | |
2090 | typeof ( type-name ) | |
2091 | */ | |
2092 | ||
2093 | static struct c_typespec | |
2094 | c_parser_typeof_specifier (c_parser *parser) | |
2095 | { | |
2096 | struct c_typespec ret; | |
2097 | ret.kind = ctsk_typeof; | |
2098 | ret.spec = error_mark_node; | |
928c19bb JM |
2099 | ret.expr = NULL_TREE; |
2100 | ret.expr_const_operands = true; | |
27bf414c JM |
2101 | gcc_assert (c_parser_next_token_is_keyword (parser, RID_TYPEOF)); |
2102 | c_parser_consume_token (parser); | |
7d882b83 | 2103 | c_inhibit_evaluation_warnings++; |
27bf414c JM |
2104 | in_typeof++; |
2105 | if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>")) | |
2106 | { | |
7d882b83 | 2107 | c_inhibit_evaluation_warnings--; |
27bf414c JM |
2108 | in_typeof--; |
2109 | return ret; | |
2110 | } | |
2111 | if (c_parser_next_token_starts_typename (parser)) | |
2112 | { | |
2113 | struct c_type_name *type = c_parser_type_name (parser); | |
7d882b83 | 2114 | c_inhibit_evaluation_warnings--; |
27bf414c JM |
2115 | in_typeof--; |
2116 | if (type != NULL) | |
2117 | { | |
928c19bb | 2118 | ret.spec = groktypename (type, &ret.expr, &ret.expr_const_operands); |
27bf414c JM |
2119 | pop_maybe_used (variably_modified_type_p (ret.spec, NULL_TREE)); |
2120 | } | |
2121 | } | |
2122 | else | |
2123 | { | |
52ffd86e | 2124 | bool was_vm; |
c7412148 | 2125 | location_t here = c_parser_peek_token (parser)->location; |
27bf414c | 2126 | struct c_expr expr = c_parser_expression (parser); |
7d882b83 | 2127 | c_inhibit_evaluation_warnings--; |
27bf414c JM |
2128 | in_typeof--; |
2129 | if (TREE_CODE (expr.value) == COMPONENT_REF | |
2130 | && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1))) | |
3ba09659 | 2131 | error_at (here, "%<typeof%> applied to a bit-field"); |
27bf414c | 2132 | ret.spec = TREE_TYPE (expr.value); |
52ffd86e | 2133 | was_vm = variably_modified_type_p (ret.spec, NULL_TREE); |
928c19bb JM |
2134 | /* This is returned with the type so that when the type is |
2135 | evaluated, this can be evaluated. */ | |
2136 | if (was_vm) | |
2137 | ret.expr = c_fully_fold (expr.value, false, &ret.expr_const_operands); | |
52ffd86e | 2138 | pop_maybe_used (was_vm); |
27bf414c JM |
2139 | } |
2140 | c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>"); | |
2141 | return ret; | |
2142 | } | |
2143 | ||
2144 | /* Parse a declarator, possibly an abstract declarator (C90 6.5.4, | |
2145 | 6.5.5, C99 6.7.5, 6.7.6). If TYPE_SEEN_P then a typedef name may | |
2146 | be redeclared; otherwise it may not. KIND indicates which kind of | |
2147 | declarator is wanted. Returns a valid declarator except in the | |
2148 | case of a syntax error in which case NULL is returned. *SEEN_ID is | |
2149 | set to true if an identifier being declared is seen; this is used | |
2150 | to diagnose bad forms of abstract array declarators and to | |
2151 | determine whether an identifier list is syntactically permitted. | |
2152 | ||
2153 | declarator: | |
2154 | pointer[opt] direct-declarator | |
2155 | ||
2156 | direct-declarator: | |
2157 | identifier | |
2158 | ( attributes[opt] declarator ) | |
2159 | direct-declarator array-declarator | |
2160 | direct-declarator ( parameter-type-list ) | |
2161 | direct-declarator ( identifier-list[opt] ) | |
2162 | ||
2163 | pointer: | |
2164 | * type-qualifier-list[opt] | |
2165 | * type-qualifier-list[opt] pointer | |
2166 | ||
2167 | type-qualifier-list: | |
2168 | type-qualifier | |
2169 | attributes | |
2170 | type-qualifier-list type-qualifier | |
2171 | type-qualifier-list attributes | |
2172 | ||
2173 | parameter-type-list: | |
2174 | parameter-list | |
2175 | parameter-list , ... | |
2176 | ||
2177 | parameter-list: | |
2178 | parameter-declaration | |
2179 | parameter-list , parameter-declaration | |
2180 | ||
2181 | parameter-declaration: | |
2182 | declaration-specifiers declarator attributes[opt] | |
2183 | declaration-specifiers abstract-declarator[opt] attributes[opt] | |
2184 | ||
2185 | identifier-list: | |
2186 | identifier | |
2187 | identifier-list , identifier | |
2188 | ||
2189 | abstract-declarator: | |
2190 | pointer | |
2191 | pointer[opt] direct-abstract-declarator | |
2192 | ||
2193 | direct-abstract-declarator: | |
2194 | ( attributes[opt] abstract-declarator ) | |
2195 | direct-abstract-declarator[opt] array-declarator | |
2196 | direct-abstract-declarator[opt] ( parameter-type-list[opt] ) | |
2197 | ||
2198 | GNU extensions: | |
2199 | ||
2200 | direct-declarator: | |
2201 | direct-declarator ( parameter-forward-declarations | |
2202 | parameter-type-list[opt] ) | |
2203 | ||
2204 | direct-abstract-declarator: | |
c22cacf3 | 2205 | direct-abstract-declarator[opt] ( parameter-forward-declarations |
27bf414c JM |
2206 | parameter-type-list[opt] ) |
2207 | ||
2208 | parameter-forward-declarations: | |
2209 | parameter-list ; | |
2210 | parameter-forward-declarations parameter-list ; | |
2211 | ||
2212 | The uses of attributes shown above are GNU extensions. | |
2213 | ||
2214 | Some forms of array declarator are not included in C99 in the | |
2215 | syntax for abstract declarators; these are disallowed elsewhere. | |
2216 | This may be a defect (DR#289). | |
2217 | ||
2218 | This function also accepts an omitted abstract declarator as being | |
2219 | an abstract declarator, although not part of the formal syntax. */ | |
2220 | ||
2221 | static struct c_declarator * | |
2222 | c_parser_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind, | |
2223 | bool *seen_id) | |
2224 | { | |
2225 | /* Parse any initial pointer part. */ | |
2226 | if (c_parser_next_token_is (parser, CPP_MULT)) | |
2227 | { | |
2228 | struct c_declspecs *quals_attrs = build_null_declspecs (); | |
2229 | struct c_declarator *inner; | |
2230 | c_parser_consume_token (parser); | |
2231 | c_parser_declspecs (parser, quals_attrs, false, false, true); | |
2232 | inner = c_parser_declarator (parser, type_seen_p, kind, seen_id); | |
2233 | if (inner == NULL) | |
2234 | return NULL; | |
2235 | else | |
2236 | return make_pointer_declarator (quals_attrs, inner); | |
2237 | } | |
2238 | /* Now we have a direct declarator, direct abstract declarator or | |
2239 | nothing (which counts as a direct abstract declarator here). */ | |
2240 | return c_parser_direct_declarator (parser, type_seen_p, kind, seen_id); | |
2241 | } | |
2242 | ||
2243 | /* Parse a direct declarator or direct abstract declarator; arguments | |
2244 | as c_parser_declarator. */ | |
2245 | ||
2246 | static struct c_declarator * | |
2247 | c_parser_direct_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind, | |
2248 | bool *seen_id) | |
2249 | { | |
2250 | /* The direct declarator must start with an identifier (possibly | |
2251 | omitted) or a parenthesized declarator (possibly abstract). In | |
2252 | an ordinary declarator, initial parentheses must start a | |
2253 | parenthesized declarator. In an abstract declarator or parameter | |
2254 | declarator, they could start a parenthesized declarator or a | |
2255 | parameter list. To tell which, the open parenthesis and any | |
2256 | following attributes must be read. If a declaration specifier | |
2257 | follows, then it is a parameter list; if the specifier is a | |
2258 | typedef name, there might be an ambiguity about redeclaring it, | |
2259 | which is resolved in the direction of treating it as a typedef | |
2260 | name. If a close parenthesis follows, it is also an empty | |
2261 | parameter list, as the syntax does not permit empty abstract | |
0fa2e4df | 2262 | declarators. Otherwise, it is a parenthesized declarator (in |
27bf414c JM |
2263 | which case the analysis may be repeated inside it, recursively). |
2264 | ||
2265 | ??? There is an ambiguity in a parameter declaration "int | |
2266 | (__attribute__((foo)) x)", where x is not a typedef name: it | |
2267 | could be an abstract declarator for a function, or declare x with | |
2268 | parentheses. The proper resolution of this ambiguity needs | |
2269 | documenting. At present we follow an accident of the old | |
2270 | parser's implementation, whereby the first parameter must have | |
2271 | some declaration specifiers other than just attributes. Thus as | |
0fa2e4df | 2272 | a parameter declaration it is treated as a parenthesized |
27bf414c JM |
2273 | parameter named x, and as an abstract declarator it is |
2274 | rejected. | |
2275 | ||
2276 | ??? Also following the old parser, attributes inside an empty | |
2277 | parameter list are ignored, making it a list not yielding a | |
2278 | prototype, rather than giving an error or making it have one | |
2279 | parameter with implicit type int. | |
2280 | ||
2281 | ??? Also following the old parser, typedef names may be | |
2282 | redeclared in declarators, but not Objective-C class names. */ | |
2283 | ||
2284 | if (kind != C_DTR_ABSTRACT | |
2285 | && c_parser_next_token_is (parser, CPP_NAME) | |
2286 | && ((type_seen_p | |
2287 | && c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME) | |
2288 | || c_parser_peek_token (parser)->id_kind == C_ID_ID)) | |
2289 | { | |
2290 | struct c_declarator *inner | |
2291 | = build_id_declarator (c_parser_peek_token (parser)->value); | |
2292 | *seen_id = true; | |
6037d88d | 2293 | inner->id_loc = c_parser_peek_token (parser)->location; |
27bf414c JM |
2294 | c_parser_consume_token (parser); |
2295 | return c_parser_direct_declarator_inner (parser, *seen_id, inner); | |
2296 | } | |
2297 | ||
2298 | if (kind != C_DTR_NORMAL | |
2299 | && c_parser_next_token_is (parser, CPP_OPEN_SQUARE)) | |
2300 | { | |
2301 | struct c_declarator *inner = build_id_declarator (NULL_TREE); | |
2302 | return c_parser_direct_declarator_inner (parser, *seen_id, inner); | |
2303 | } | |
2304 | ||
2305 | /* Either we are at the end of an abstract declarator, or we have | |
2306 | parentheses. */ | |
2307 | ||
2308 | if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)) | |
2309 | { | |
2310 | tree attrs; | |
2311 | struct c_declarator *inner; | |
2312 | c_parser_consume_token (parser); | |
2313 | attrs = c_parser_attributes (parser); | |
2314 | if (kind != C_DTR_NORMAL | |
2315 | && (c_parser_next_token_starts_declspecs (parser) | |
2316 | || c_parser_next_token_is (parser, CPP_CLOSE_PAREN))) | |
2317 | { | |
2318 | struct c_arg_info *args | |
2319 | = c_parser_parms_declarator (parser, kind == C_DTR_NORMAL, | |
2320 | attrs); | |
2321 | if (args == NULL) | |
2322 | return NULL; | |
2323 | else | |
2324 | { | |
2325 | inner | |
2326 | = build_function_declarator (args, | |
2327 | build_id_declarator (NULL_TREE)); | |
2328 | return c_parser_direct_declarator_inner (parser, *seen_id, | |
2329 | inner); | |
2330 | } | |
2331 | } | |
2332 | /* A parenthesized declarator. */ | |
2333 | inner = c_parser_declarator (parser, type_seen_p, kind, seen_id); | |
2334 | if (inner != NULL && attrs != NULL) | |
2335 | inner = build_attrs_declarator (attrs, inner); | |
2336 | if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN)) | |
2337 | { | |
2338 | c_parser_consume_token (parser); | |
2339 | if (inner == NULL) | |
2340 | return NULL; | |
2341 | else | |
2342 | return c_parser_direct_declarator_inner (parser, *seen_id, inner); | |
2343 | } | |
2344 | else | |
2345 | { | |
2346 | c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, | |
2347 | "expected %<)%>"); | |
2348 | return NULL; | |
2349 | } | |
2350 | } | |
2351 | else | |
2352 | { | |
2353 | if (kind == C_DTR_NORMAL) | |
2354 | { | |
2355 | c_parser_error (parser, "expected identifier or %<(%>"); | |
2356 | return NULL; | |
2357 | } | |
2358 | else | |
2359 | return build_id_declarator (NULL_TREE); | |
2360 | } | |
2361 | } | |
2362 | ||
2363 | /* Parse part of a direct declarator or direct abstract declarator, | |
2364 | given that some (in INNER) has already been parsed; ID_PRESENT is | |
2365 | true if an identifier is present, false for an abstract | |
2366 | declarator. */ | |
2367 | ||
2368 | static struct c_declarator * | |
2369 | c_parser_direct_declarator_inner (c_parser *parser, bool id_present, | |
2370 | struct c_declarator *inner) | |
2371 | { | |
2372 | /* Parse a sequence of array declarators and parameter lists. */ | |
2373 | if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE)) | |
2374 | { | |
c2255bc4 | 2375 | location_t brace_loc = c_parser_peek_token (parser)->location; |
27bf414c JM |
2376 | struct c_declarator *declarator; |
2377 | struct c_declspecs *quals_attrs = build_null_declspecs (); | |
2378 | bool static_seen; | |
2379 | bool star_seen; | |
2380 | tree dimen; | |
2381 | c_parser_consume_token (parser); | |
2382 | c_parser_declspecs (parser, quals_attrs, false, false, true); | |
2383 | static_seen = c_parser_next_token_is_keyword (parser, RID_STATIC); | |
2384 | if (static_seen) | |
2385 | c_parser_consume_token (parser); | |
2386 | if (static_seen && !quals_attrs->declspecs_seen_p) | |
2387 | c_parser_declspecs (parser, quals_attrs, false, false, true); | |
2388 | if (!quals_attrs->declspecs_seen_p) | |
2389 | quals_attrs = NULL; | |
2390 | /* If "static" is present, there must be an array dimension. | |
2391 | Otherwise, there may be a dimension, "*", or no | |
2392 | dimension. */ | |
2393 | if (static_seen) | |
2394 | { | |
2395 | star_seen = false; | |
2396 | dimen = c_parser_expr_no_commas (parser, NULL).value; | |
2397 | } | |
2398 | else | |
2399 | { | |
2400 | if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE)) | |
2401 | { | |
2402 | dimen = NULL_TREE; | |
2403 | star_seen = false; | |
2404 | } | |
2405 | else if (c_parser_next_token_is (parser, CPP_MULT)) | |
2406 | { | |
2407 | if (c_parser_peek_2nd_token (parser)->type == CPP_CLOSE_SQUARE) | |
2408 | { | |
2409 | dimen = NULL_TREE; | |
2410 | star_seen = true; | |
2411 | c_parser_consume_token (parser); | |
2412 | } | |
2413 | else | |
2414 | { | |
2415 | star_seen = false; | |
2416 | dimen = c_parser_expr_no_commas (parser, NULL).value; | |
2417 | } | |
2418 | } | |
2419 | else | |
2420 | { | |
2421 | star_seen = false; | |
2422 | dimen = c_parser_expr_no_commas (parser, NULL).value; | |
2423 | } | |
2424 | } | |
2425 | if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE)) | |
2426 | c_parser_consume_token (parser); | |
2427 | else | |
2428 | { | |
2429 | c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, | |
2430 | "expected %<]%>"); | |
2431 | return NULL; | |
2432 | } | |
c2255bc4 AH |
2433 | declarator = build_array_declarator (brace_loc, dimen, quals_attrs, |
2434 | static_seen, star_seen); | |
52ffd86e MS |
2435 | if (declarator == NULL) |
2436 | return NULL; | |
6ac0194d | 2437 | inner = set_array_declarator_inner (declarator, inner); |
27bf414c JM |
2438 | return c_parser_direct_declarator_inner (parser, id_present, inner); |
2439 | } | |
2440 | else if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)) | |
2441 | { | |
2442 | tree attrs; | |
2443 | struct c_arg_info *args; | |
2444 | c_parser_consume_token (parser); | |
2445 | attrs = c_parser_attributes (parser); | |
2446 | args = c_parser_parms_declarator (parser, id_present, attrs); | |
2447 | if (args == NULL) | |
2448 | return NULL; | |
2449 | else | |
2450 | { | |
2451 | inner = build_function_declarator (args, inner); | |
2452 | return c_parser_direct_declarator_inner (parser, id_present, inner); | |
2453 | } | |
2454 | } | |
2455 | return inner; | |
2456 | } | |
2457 | ||
2458 | /* Parse a parameter list or identifier list, including the closing | |
2459 | parenthesis but not the opening one. ATTRS are the attributes at | |
2460 | the start of the list. ID_LIST_OK is true if an identifier list is | |
2461 | acceptable; such a list must not have attributes at the start. */ | |
2462 | ||
2463 | static struct c_arg_info * | |
2464 | c_parser_parms_declarator (c_parser *parser, bool id_list_ok, tree attrs) | |
2465 | { | |
2466 | push_scope (); | |
2467 | declare_parm_level (); | |
2468 | /* If the list starts with an identifier, it is an identifier list. | |
2469 | Otherwise, it is either a prototype list or an empty list. */ | |
2470 | if (id_list_ok | |
2471 | && !attrs | |
2472 | && c_parser_next_token_is (parser, CPP_NAME) | |
2473 | && c_parser_peek_token (parser)->id_kind == C_ID_ID) | |
2474 | { | |
7fa3585c | 2475 | tree list = NULL_TREE, *nextp = &list; |
27bf414c JM |
2476 | while (c_parser_next_token_is (parser, CPP_NAME) |
2477 | && c_parser_peek_token (parser)->id_kind == C_ID_ID) | |
2478 | { | |
7fa3585c GK |
2479 | *nextp = build_tree_list (NULL_TREE, |
2480 | c_parser_peek_token (parser)->value); | |
2481 | nextp = & TREE_CHAIN (*nextp); | |
27bf414c JM |
2482 | c_parser_consume_token (parser); |
2483 | if (c_parser_next_token_is_not (parser, CPP_COMMA)) | |
2484 | break; | |
2485 | c_parser_consume_token (parser); | |
2486 | if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN)) | |
2487 | { | |
2488 | c_parser_error (parser, "expected identifier"); | |
2489 | break; | |
2490 | } | |
2491 | } | |
2492 | if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN)) | |
2493 | { | |
2494 | struct c_arg_info *ret = XOBNEW (&parser_obstack, struct c_arg_info); | |
2495 | ret->parms = 0; | |
2496 | ret->tags = 0; | |
2497 | ret->types = list; | |
2498 | ret->others = 0; | |
3542a5c0 | 2499 | ret->pending_sizes = 0; |
6a1f8611 | 2500 | ret->had_vla_unspec = 0; |
27bf414c JM |
2501 | c_parser_consume_token (parser); |
2502 | pop_scope (); | |
2503 | return ret; | |
2504 | } | |
2505 | else | |
2506 | { | |
2507 | c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, | |
2508 | "expected %<)%>"); | |
2509 | pop_scope (); | |
2510 | return NULL; | |
2511 | } | |
2512 | } | |
2513 | else | |
2514 | { | |
2515 | struct c_arg_info *ret = c_parser_parms_list_declarator (parser, attrs); | |
2516 | pop_scope (); | |
2517 | return ret; | |
2518 | } | |
2519 | } | |
2520 | ||
2521 | /* Parse a parameter list (possibly empty), including the closing | |
2522 | parenthesis but not the opening one. ATTRS are the attributes at | |
2523 | the start of the list. */ | |
2524 | ||
2525 | static struct c_arg_info * | |
2526 | c_parser_parms_list_declarator (c_parser *parser, tree attrs) | |
2527 | { | |
2528 | bool good_parm = false; | |
2529 | /* ??? Following the old parser, forward parameter declarations may | |
2530 | use abstract declarators, and if no real parameter declarations | |
2531 | follow the forward declarations then this is not diagnosed. Also | |
2532 | note as above that attributes are ignored as the only contents of | |
2533 | the parentheses, or as the only contents after forward | |
2534 | declarations. */ | |
2535 | if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN)) | |
2536 | { | |
2537 | struct c_arg_info *ret = XOBNEW (&parser_obstack, struct c_arg_info); | |
2538 | ret->parms = 0; | |
2539 | ret->tags = 0; | |
2540 | ret->types = 0; | |
2541 | ret->others = 0; | |
3542a5c0 | 2542 | ret->pending_sizes = 0; |
6a1f8611 | 2543 | ret->had_vla_unspec = 0; |
27bf414c JM |
2544 | c_parser_consume_token (parser); |
2545 | return ret; | |
2546 | } | |
2547 | if (c_parser_next_token_is (parser, CPP_ELLIPSIS)) | |
2548 | { | |
2549 | struct c_arg_info *ret = XOBNEW (&parser_obstack, struct c_arg_info); | |
2550 | ret->parms = 0; | |
2551 | ret->tags = 0; | |
2552 | ret->others = 0; | |
3542a5c0 | 2553 | ret->pending_sizes = 0; |
6a1f8611 | 2554 | ret->had_vla_unspec = 0; |
27bf414c JM |
2555 | /* Suppress -Wold-style-definition for this case. */ |
2556 | ret->types = error_mark_node; | |
3ba09659 AH |
2557 | error_at (c_parser_peek_token (parser)->location, |
2558 | "ISO C requires a named argument before %<...%>"); | |
27bf414c JM |
2559 | c_parser_consume_token (parser); |
2560 | if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN)) | |
2561 | { | |
2562 | c_parser_consume_token (parser); | |
2563 | return ret; | |
2564 | } | |
2565 | else | |
2566 | { | |
2567 | c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, | |
2568 | "expected %<)%>"); | |
2569 | return NULL; | |
2570 | } | |
2571 | } | |
2572 | /* Nonempty list of parameters, either terminated with semicolon | |
2573 | (forward declarations; recurse) or with close parenthesis (normal | |
2574 | function) or with ", ... )" (variadic function). */ | |
2575 | while (true) | |
2576 | { | |
2577 | /* Parse a parameter. */ | |
2578 | struct c_parm *parm = c_parser_parameter_declaration (parser, attrs); | |
2579 | attrs = NULL_TREE; | |
2580 | if (parm != NULL) | |
2581 | { | |
2582 | good_parm = true; | |
2583 | push_parm_decl (parm); | |
2584 | } | |
2585 | if (c_parser_next_token_is (parser, CPP_SEMICOLON)) | |
2586 | { | |
2587 | tree new_attrs; | |
2588 | c_parser_consume_token (parser); | |
91d975b8 | 2589 | mark_forward_parm_decls (); |
27bf414c JM |
2590 | new_attrs = c_parser_attributes (parser); |
2591 | return c_parser_parms_list_declarator (parser, new_attrs); | |
2592 | } | |
2593 | if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN)) | |
2594 | { | |
2595 | c_parser_consume_token (parser); | |
2596 | if (good_parm) | |
2597 | return get_parm_info (false); | |
2598 | else | |
2599 | { | |
2600 | struct c_arg_info *ret | |
2601 | = XOBNEW (&parser_obstack, struct c_arg_info); | |
2602 | ret->parms = 0; | |
2603 | ret->tags = 0; | |
2604 | ret->types = 0; | |
2605 | ret->others = 0; | |
3542a5c0 | 2606 | ret->pending_sizes = 0; |
6a1f8611 | 2607 | ret->had_vla_unspec = 0; |
27bf414c JM |
2608 | return ret; |
2609 | } | |
2610 | } | |
2611 | if (!c_parser_require (parser, CPP_COMMA, | |
2612 | "expected %<;%>, %<,%> or %<)%>")) | |
2613 | { | |
2614 | c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL); | |
f007042e | 2615 | get_pending_sizes (); |
27bf414c JM |
2616 | return NULL; |
2617 | } | |
2618 | if (c_parser_next_token_is (parser, CPP_ELLIPSIS)) | |
2619 | { | |
2620 | c_parser_consume_token (parser); | |
2621 | if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN)) | |
2622 | { | |
2623 | c_parser_consume_token (parser); | |
2624 | if (good_parm) | |
2625 | return get_parm_info (true); | |
2626 | else | |
2627 | { | |
2628 | struct c_arg_info *ret | |
2629 | = XOBNEW (&parser_obstack, struct c_arg_info); | |
2630 | ret->parms = 0; | |
2631 | ret->tags = 0; | |
2632 | ret->types = 0; | |
2633 | ret->others = 0; | |
3542a5c0 | 2634 | ret->pending_sizes = 0; |
6a1f8611 | 2635 | ret->had_vla_unspec = 0; |
27bf414c JM |
2636 | return ret; |
2637 | } | |
2638 | } | |
2639 | else | |
2640 | { | |
2641 | c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, | |
2642 | "expected %<)%>"); | |
f007042e | 2643 | get_pending_sizes (); |
27bf414c JM |
2644 | return NULL; |
2645 | } | |
2646 | } | |
2647 | } | |
2648 | } | |
2649 | ||
2650 | /* Parse a parameter declaration. ATTRS are the attributes at the | |
2651 | start of the declaration if it is the first parameter. */ | |
2652 | ||
2653 | static struct c_parm * | |
2654 | c_parser_parameter_declaration (c_parser *parser, tree attrs) | |
2655 | { | |
2656 | struct c_declspecs *specs; | |
2657 | struct c_declarator *declarator; | |
2658 | tree prefix_attrs; | |
2659 | tree postfix_attrs = NULL_TREE; | |
2660 | bool dummy = false; | |
2661 | if (!c_parser_next_token_starts_declspecs (parser)) | |
2662 | { | |
2663 | /* ??? In some Objective-C cases '...' isn't applicable so there | |
2664 | should be a different message. */ | |
2665 | c_parser_error (parser, | |
2666 | "expected declaration specifiers or %<...%>"); | |
2667 | c_parser_skip_to_end_of_parameter (parser); | |
2668 | return NULL; | |
2669 | } | |
2670 | specs = build_null_declspecs (); | |
2671 | if (attrs) | |
2672 | { | |
2673 | declspecs_add_attrs (specs, attrs); | |
2674 | attrs = NULL_TREE; | |
2675 | } | |
2676 | c_parser_declspecs (parser, specs, true, true, true); | |
2677 | finish_declspecs (specs); | |
2678 | pending_xref_error (); | |
2679 | prefix_attrs = specs->attrs; | |
2680 | specs->attrs = NULL_TREE; | |
2681 | declarator = c_parser_declarator (parser, specs->type_seen_p, | |
2682 | C_DTR_PARM, &dummy); | |
2683 | if (declarator == NULL) | |
2684 | { | |
2685 | c_parser_skip_until_found (parser, CPP_COMMA, NULL); | |
2686 | return NULL; | |
2687 | } | |
2688 | if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE)) | |
2689 | postfix_attrs = c_parser_attributes (parser); | |
2690 | return build_c_parm (specs, chainon (postfix_attrs, prefix_attrs), | |
2691 | declarator); | |
2692 | } | |
2693 | ||
2694 | /* Parse a string literal in an asm expression. It should not be | |
2695 | translated, and wide string literals are an error although | |
2696 | permitted by the syntax. This is a GNU extension. | |
2697 | ||
2698 | asm-string-literal: | |
2699 | string-literal | |
2700 | ||
2701 | ??? At present, following the old parser, the caller needs to have | |
46c2514e TT |
2702 | set lex_untranslated_string to 1. It would be better to follow the |
2703 | C++ parser rather than using this kludge. */ | |
27bf414c JM |
2704 | |
2705 | static tree | |
2706 | c_parser_asm_string_literal (c_parser *parser) | |
2707 | { | |
2708 | tree str; | |
2709 | if (c_parser_next_token_is (parser, CPP_STRING)) | |
2710 | { | |
2711 | str = c_parser_peek_token (parser)->value; | |
2712 | c_parser_consume_token (parser); | |
2713 | } | |
2714 | else if (c_parser_next_token_is (parser, CPP_WSTRING)) | |
2715 | { | |
3ba09659 AH |
2716 | error_at (c_parser_peek_token (parser)->location, |
2717 | "wide string literal in %<asm%>"); | |
27bf414c JM |
2718 | str = build_string (1, ""); |
2719 | c_parser_consume_token (parser); | |
2720 | } | |
2721 | else | |
2722 | { | |
2723 | c_parser_error (parser, "expected string literal"); | |
2724 | str = NULL_TREE; | |
2725 | } | |
2726 | return str; | |
2727 | } | |
2728 | ||
2729 | /* Parse a simple asm expression. This is used in restricted | |
2730 | contexts, where a full expression with inputs and outputs does not | |
2731 | make sense. This is a GNU extension. | |
2732 | ||
2733 | simple-asm-expr: | |
2734 | asm ( asm-string-literal ) | |
2735 | */ | |
2736 | ||
2737 | static tree | |
2738 | c_parser_simple_asm_expr (c_parser *parser) | |
2739 | { | |
2740 | tree str; | |
2741 | gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM)); | |
2742 | /* ??? Follow the C++ parser rather than using the | |
46c2514e TT |
2743 | lex_untranslated_string kludge. */ |
2744 | parser->lex_untranslated_string = true; | |
27bf414c JM |
2745 | c_parser_consume_token (parser); |
2746 | if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>")) | |
2747 | { | |
46c2514e | 2748 | parser->lex_untranslated_string = false; |
27bf414c JM |
2749 | return NULL_TREE; |
2750 | } | |
2751 | str = c_parser_asm_string_literal (parser); | |
46c2514e | 2752 | parser->lex_untranslated_string = false; |
27bf414c JM |
2753 | if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>")) |
2754 | { | |
2755 | c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL); | |
2756 | return NULL_TREE; | |
2757 | } | |
2758 | return str; | |
2759 | } | |
2760 | ||
2761 | /* Parse (possibly empty) attributes. This is a GNU extension. | |
2762 | ||
2763 | attributes: | |
2764 | empty | |
2765 | attributes attribute | |
2766 | ||
2767 | attribute: | |
2768 | __attribute__ ( ( attribute-list ) ) | |
2769 | ||
2770 | attribute-list: | |
2771 | attrib | |
2772 | attribute_list , attrib | |
2773 | ||
2774 | attrib: | |
2775 | empty | |
2776 | any-word | |
2777 | any-word ( identifier ) | |
2778 | any-word ( identifier , nonempty-expr-list ) | |
2779 | any-word ( expr-list ) | |
2780 | ||
2781 | where the "identifier" must not be declared as a type, and | |
2782 | "any-word" may be any identifier (including one declared as a | |
2783 | type), a reserved word storage class specifier, type specifier or | |
2784 | type qualifier. ??? This still leaves out most reserved keywords | |
2785 | (following the old parser), shouldn't we include them, and why not | |
2786 | allow identifiers declared as types to start the arguments? */ | |
2787 | ||
2788 | static tree | |
2789 | c_parser_attributes (c_parser *parser) | |
2790 | { | |
2791 | tree attrs = NULL_TREE; | |
2792 | while (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE)) | |
2793 | { | |
2794 | /* ??? Follow the C++ parser rather than using the | |
46c2514e TT |
2795 | lex_untranslated_string kludge. */ |
2796 | parser->lex_untranslated_string = true; | |
27bf414c JM |
2797 | c_parser_consume_token (parser); |
2798 | if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>")) | |
2799 | { | |
46c2514e | 2800 | parser->lex_untranslated_string = false; |
27bf414c JM |
2801 | return attrs; |
2802 | } | |
2803 | if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>")) | |
2804 | { | |
46c2514e | 2805 | parser->lex_untranslated_string = false; |
27bf414c JM |
2806 | c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL); |
2807 | return attrs; | |
2808 | } | |
2809 | /* Parse the attribute list. */ | |
2810 | while (c_parser_next_token_is (parser, CPP_COMMA) | |
2811 | || c_parser_next_token_is (parser, CPP_NAME) | |
2812 | || c_parser_next_token_is (parser, CPP_KEYWORD)) | |
2813 | { | |
2814 | tree attr, attr_name, attr_args; | |
bbbbb16a | 2815 | VEC(tree,gc) *expr_list; |
27bf414c JM |
2816 | if (c_parser_next_token_is (parser, CPP_COMMA)) |
2817 | { | |
2818 | c_parser_consume_token (parser); | |
2819 | continue; | |
2820 | } | |
2821 | if (c_parser_next_token_is (parser, CPP_KEYWORD)) | |
2822 | { | |
2823 | /* ??? See comment above about what keywords are | |
2824 | accepted here. */ | |
2825 | bool ok; | |
2826 | switch (c_parser_peek_token (parser)->keyword) | |
2827 | { | |
2828 | case RID_STATIC: | |
2829 | case RID_UNSIGNED: | |
2830 | case RID_LONG: | |
2831 | case RID_CONST: | |
2832 | case RID_EXTERN: | |
2833 | case RID_REGISTER: | |
2834 | case RID_TYPEDEF: | |
2835 | case RID_SHORT: | |
2836 | case RID_INLINE: | |
2837 | case RID_VOLATILE: | |
2838 | case RID_SIGNED: | |
2839 | case RID_AUTO: | |
2840 | case RID_RESTRICT: | |
2841 | case RID_COMPLEX: | |
2842 | case RID_THREAD: | |
2843 | case RID_INT: | |
2844 | case RID_CHAR: | |
2845 | case RID_FLOAT: | |
2846 | case RID_DOUBLE: | |
2847 | case RID_VOID: | |
9a8ce21f JG |
2848 | case RID_DFLOAT32: |
2849 | case RID_DFLOAT64: | |
2850 | case RID_DFLOAT128: | |
27bf414c | 2851 | case RID_BOOL: |
ab22c1fa CF |
2852 | case RID_FRACT: |
2853 | case RID_ACCUM: | |
2854 | case RID_SAT: | |
27bf414c JM |
2855 | ok = true; |
2856 | break; | |
2857 | default: | |
2858 | ok = false; | |
2859 | break; | |
2860 | } | |
2861 | if (!ok) | |
2862 | break; | |
5dc4a7f4 JJ |
2863 | /* Accept __attribute__((__const)) as __attribute__((const)) |
2864 | etc. */ | |
2865 | attr_name | |
2866 | = ridpointers[(int) c_parser_peek_token (parser)->keyword]; | |
27bf414c | 2867 | } |
5dc4a7f4 JJ |
2868 | else |
2869 | attr_name = c_parser_peek_token (parser)->value; | |
27bf414c JM |
2870 | c_parser_consume_token (parser); |
2871 | if (c_parser_next_token_is_not (parser, CPP_OPEN_PAREN)) | |
2872 | { | |
2873 | attr = build_tree_list (attr_name, NULL_TREE); | |
2874 | attrs = chainon (attrs, attr); | |
2875 | continue; | |
2876 | } | |
2877 | c_parser_consume_token (parser); | |
2878 | /* Parse the attribute contents. If they start with an | |
2879 | identifier which is followed by a comma or close | |
2880 | parenthesis, then the arguments start with that | |
2881 | identifier; otherwise they are an expression list. */ | |
2882 | if (c_parser_next_token_is (parser, CPP_NAME) | |
2883 | && c_parser_peek_token (parser)->id_kind == C_ID_ID | |
2884 | && ((c_parser_peek_2nd_token (parser)->type == CPP_COMMA) | |
2885 | || (c_parser_peek_2nd_token (parser)->type | |
2886 | == CPP_CLOSE_PAREN))) | |
2887 | { | |
2888 | tree arg1 = c_parser_peek_token (parser)->value; | |
2889 | c_parser_consume_token (parser); | |
2890 | if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN)) | |
2891 | attr_args = build_tree_list (NULL_TREE, arg1); | |
2892 | else | |
2893 | { | |
bbbbb16a | 2894 | tree tree_list; |
27bf414c | 2895 | c_parser_consume_token (parser); |
bbbbb16a | 2896 | expr_list = c_parser_expr_list (parser, false, true, NULL); |
c166b898 | 2897 | tree_list = build_tree_list_vec (expr_list); |
bbbbb16a | 2898 | attr_args = tree_cons (NULL_TREE, arg1, tree_list); |
c166b898 | 2899 | release_tree_vector (expr_list); |
27bf414c JM |
2900 | } |
2901 | } | |
2902 | else | |
2903 | { | |
2904 | if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN)) | |
2905 | attr_args = NULL_TREE; | |
2906 | else | |
bbbbb16a ILT |
2907 | { |
2908 | expr_list = c_parser_expr_list (parser, false, true, NULL); | |
c166b898 ILT |
2909 | attr_args = build_tree_list_vec (expr_list); |
2910 | release_tree_vector (expr_list); | |
bbbbb16a | 2911 | } |
27bf414c JM |
2912 | } |
2913 | attr = build_tree_list (attr_name, attr_args); | |
2914 | if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN)) | |
2915 | c_parser_consume_token (parser); | |
2916 | else | |
2917 | { | |
46c2514e | 2918 | parser->lex_untranslated_string = false; |
27bf414c JM |
2919 | c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, |
2920 | "expected %<)%>"); | |
2921 | return attrs; | |
2922 | } | |
2923 | attrs = chainon (attrs, attr); | |
2924 | } | |
2925 | if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN)) | |
2926 | c_parser_consume_token (parser); | |
2927 | else | |
2928 | { | |
46c2514e | 2929 | parser->lex_untranslated_string = false; |
27bf414c JM |
2930 | c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, |
2931 | "expected %<)%>"); | |
2932 | return attrs; | |
2933 | } | |
2934 | if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN)) | |
2935 | c_parser_consume_token (parser); | |
2936 | else | |
2937 | { | |
46c2514e | 2938 | parser->lex_untranslated_string = false; |
27bf414c JM |
2939 | c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, |
2940 | "expected %<)%>"); | |
2941 | return attrs; | |
2942 | } | |
46c2514e | 2943 | parser->lex_untranslated_string = false; |
27bf414c JM |
2944 | } |
2945 | return attrs; | |
2946 | } | |
2947 | ||
2948 | /* Parse a type name (C90 6.5.5, C99 6.7.6). | |
2949 | ||
2950 | type-name: | |
2951 | specifier-qualifier-list abstract-declarator[opt] | |
2952 | */ | |
2953 | ||
2954 | static struct c_type_name * | |
2955 | c_parser_type_name (c_parser *parser) | |
2956 | { | |
2957 | struct c_declspecs *specs = build_null_declspecs (); | |
2958 | struct c_declarator *declarator; | |
2959 | struct c_type_name *ret; | |
2960 | bool dummy = false; | |
2961 | c_parser_declspecs (parser, specs, false, true, true); | |
2962 | if (!specs->declspecs_seen_p) | |
2963 | { | |
2964 | c_parser_error (parser, "expected specifier-qualifier-list"); | |
2965 | return NULL; | |
2966 | } | |
2967 | pending_xref_error (); | |
2968 | finish_declspecs (specs); | |
2969 | declarator = c_parser_declarator (parser, specs->type_seen_p, | |
2970 | C_DTR_ABSTRACT, &dummy); | |
2971 | if (declarator == NULL) | |
2972 | return NULL; | |
2973 | ret = XOBNEW (&parser_obstack, struct c_type_name); | |
2974 | ret->specs = specs; | |
2975 | ret->declarator = declarator; | |
2976 | return ret; | |
2977 | } | |
2978 | ||
2979 | /* Parse an initializer (C90 6.5.7, C99 6.7.8). | |
2980 | ||
2981 | initializer: | |
2982 | assignment-expression | |
2983 | { initializer-list } | |
2984 | { initializer-list , } | |
2985 | ||
2986 | initializer-list: | |
2987 | designation[opt] initializer | |
2988 | initializer-list , designation[opt] initializer | |
2989 | ||
2990 | designation: | |
2991 | designator-list = | |
2992 | ||
2993 | designator-list: | |
2994 | designator | |
2995 | designator-list designator | |
2996 | ||
2997 | designator: | |
2998 | array-designator | |
2999 | . identifier | |
3000 | ||
3001 | array-designator: | |
3002 | [ constant-expression ] | |
3003 | ||
3004 | GNU extensions: | |
3005 | ||
3006 | initializer: | |
3007 | { } | |
3008 | ||
3009 | designation: | |
3010 | array-designator | |
3011 | identifier : | |
3012 | ||
3013 | array-designator: | |
3014 | [ constant-expression ... constant-expression ] | |
3015 | ||
3016 | Any expression without commas is accepted in the syntax for the | |
3017 | constant-expressions, with non-constant expressions rejected later. | |
3018 | ||
3019 | This function is only used for top-level initializers; for nested | |
3020 | ones, see c_parser_initval. */ | |
3021 | ||
3022 | static struct c_expr | |
3023 | c_parser_initializer (c_parser *parser) | |
3024 | { | |
3025 | if (c_parser_next_token_is (parser, CPP_OPEN_BRACE)) | |
3026 | return c_parser_braced_init (parser, NULL_TREE, false); | |
3027 | else | |
46bdb9cf JM |
3028 | { |
3029 | struct c_expr ret; | |
c2255bc4 | 3030 | location_t loc = c_parser_peek_token (parser)->location; |
46bdb9cf JM |
3031 | ret = c_parser_expr_no_commas (parser, NULL); |
3032 | if (TREE_CODE (ret.value) != STRING_CST | |
3033 | && TREE_CODE (ret.value) != COMPOUND_LITERAL_EXPR) | |
c2255bc4 | 3034 | ret = default_function_array_conversion (loc, ret); |
46bdb9cf JM |
3035 | return ret; |
3036 | } | |
27bf414c JM |
3037 | } |
3038 | ||
3039 | /* Parse a braced initializer list. TYPE is the type specified for a | |
3040 | compound literal, and NULL_TREE for other initializers and for | |
3041 | nested braced lists. NESTED_P is true for nested braced lists, | |
3042 | false for the list of a compound literal or the list that is the | |
3043 | top-level initializer in a declaration. */ | |
3044 | ||
3045 | static struct c_expr | |
3046 | c_parser_braced_init (c_parser *parser, tree type, bool nested_p) | |
3047 | { | |
c7412148 | 3048 | location_t brace_loc = c_parser_peek_token (parser)->location; |
27bf414c JM |
3049 | gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE)); |
3050 | c_parser_consume_token (parser); | |
3051 | if (nested_p) | |
3052 | push_init_level (0); | |
3053 | else | |
3054 | really_start_incremental_init (type); | |
3055 | if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE)) | |
3056 | { | |
509c9d60 | 3057 | pedwarn (brace_loc, OPT_pedantic, "ISO C forbids empty initializer braces"); |
27bf414c JM |
3058 | } |
3059 | else | |
3060 | { | |
3061 | /* Parse a non-empty initializer list, possibly with a trailing | |
3062 | comma. */ | |
3063 | while (true) | |
3064 | { | |
3065 | c_parser_initelt (parser); | |
3066 | if (parser->error) | |
3067 | break; | |
3068 | if (c_parser_next_token_is (parser, CPP_COMMA)) | |
3069 | c_parser_consume_token (parser); | |
3070 | else | |
3071 | break; | |
3072 | if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE)) | |
3073 | break; | |
3074 | } | |
3075 | } | |
3076 | if (c_parser_next_token_is_not (parser, CPP_CLOSE_BRACE)) | |
3077 | { | |
3078 | struct c_expr ret; | |
3079 | ret.value = error_mark_node; | |
3080 | ret.original_code = ERROR_MARK; | |
6866c6e8 | 3081 | ret.original_type = NULL; |
27bf414c | 3082 | c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, "expected %<}%>"); |
a47e20fd | 3083 | pop_init_level (0); |
27bf414c JM |
3084 | return ret; |
3085 | } | |
3086 | c_parser_consume_token (parser); | |
3087 | return pop_init_level (0); | |
3088 | } | |
3089 | ||
3090 | /* Parse a nested initializer, including designators. */ | |
3091 | ||
3092 | static void | |
3093 | c_parser_initelt (c_parser *parser) | |
3094 | { | |
3095 | /* Parse any designator or designator list. A single array | |
3096 | designator may have the subsequent "=" omitted in GNU C, but a | |
3097 | longer list or a structure member designator may not. */ | |
3098 | if (c_parser_next_token_is (parser, CPP_NAME) | |
3099 | && c_parser_peek_2nd_token (parser)->type == CPP_COLON) | |
3100 | { | |
3101 | /* Old-style structure member designator. */ | |
3102 | set_init_label (c_parser_peek_token (parser)->value); | |
fcf73884 | 3103 | /* Use the colon as the error location. */ |
509c9d60 MLI |
3104 | pedwarn (c_parser_peek_2nd_token (parser)->location, OPT_pedantic, |
3105 | "obsolete use of designated initializer with %<:%>"); | |
27bf414c JM |
3106 | c_parser_consume_token (parser); |
3107 | c_parser_consume_token (parser); | |
3108 | } | |
3109 | else | |
3110 | { | |
3111 | /* des_seen is 0 if there have been no designators, 1 if there | |
3112 | has been a single array designator and 2 otherwise. */ | |
3113 | int des_seen = 0; | |
c7412148 | 3114 | /* Location of a designator. */ |
922f2908 | 3115 | location_t des_loc = UNKNOWN_LOCATION; /* Quiet warning. */ |
27bf414c JM |
3116 | while (c_parser_next_token_is (parser, CPP_OPEN_SQUARE) |
3117 | || c_parser_next_token_is (parser, CPP_DOT)) | |
3118 | { | |
3119 | int des_prev = des_seen; | |
c7412148 TT |
3120 | if (!des_seen) |
3121 | des_loc = c_parser_peek_token (parser)->location; | |
27bf414c JM |
3122 | if (des_seen < 2) |
3123 | des_seen++; | |
3124 | if (c_parser_next_token_is (parser, CPP_DOT)) | |
3125 | { | |
3126 | des_seen = 2; | |
3127 | c_parser_consume_token (parser); | |
3128 | if (c_parser_next_token_is (parser, CPP_NAME)) | |
3129 | { | |
3130 | set_init_label (c_parser_peek_token (parser)->value); | |
3131 | c_parser_consume_token (parser); | |
3132 | } | |
3133 | else | |
3134 | { | |
3135 | struct c_expr init; | |
3136 | init.value = error_mark_node; | |
3137 | init.original_code = ERROR_MARK; | |
6866c6e8 | 3138 | init.original_type = NULL; |
27bf414c JM |
3139 | c_parser_error (parser, "expected identifier"); |
3140 | c_parser_skip_until_found (parser, CPP_COMMA, NULL); | |
b295aee2 | 3141 | process_init_element (init, false); |
27bf414c JM |
3142 | return; |
3143 | } | |
3144 | } | |
3145 | else | |
3146 | { | |
3147 | tree first, second; | |
922f2908 | 3148 | location_t ellipsis_loc = UNKNOWN_LOCATION; /* Quiet warning. */ |
27bf414c JM |
3149 | /* ??? Following the old parser, [ objc-receiver |
3150 | objc-message-args ] is accepted as an initializer, | |
3151 | being distinguished from a designator by what follows | |
3152 | the first assignment expression inside the square | |
3153 | brackets, but after a first array designator a | |
3154 | subsequent square bracket is for Objective-C taken to | |
3155 | start an expression, using the obsolete form of | |
3156 | designated initializer without '=', rather than | |
3157 | possibly being a second level of designation: in LALR | |
3158 | terms, the '[' is shifted rather than reducing | |
3159 | designator to designator-list. */ | |
3160 | if (des_prev == 1 && c_dialect_objc ()) | |
3161 | { | |
3162 | des_seen = des_prev; | |
3163 | break; | |
3164 | } | |
3165 | if (des_prev == 0 && c_dialect_objc ()) | |
3166 | { | |
3167 | /* This might be an array designator or an | |
3168 | Objective-C message expression. If the former, | |
3169 | continue parsing here; if the latter, parse the | |
3170 | remainder of the initializer given the starting | |
3171 | primary-expression. ??? It might make sense to | |
3172 | distinguish when des_prev == 1 as well; see | |
3173 | previous comment. */ | |
3174 | tree rec, args; | |
3175 | struct c_expr mexpr; | |
3176 | c_parser_consume_token (parser); | |
3177 | if (c_parser_peek_token (parser)->type == CPP_NAME | |
3178 | && ((c_parser_peek_token (parser)->id_kind | |
3179 | == C_ID_TYPENAME) | |
3180 | || (c_parser_peek_token (parser)->id_kind | |
3181 | == C_ID_CLASSNAME))) | |
3182 | { | |
3183 | /* Type name receiver. */ | |
3184 | tree id = c_parser_peek_token (parser)->value; | |
3185 | c_parser_consume_token (parser); | |
3186 | rec = objc_get_class_reference (id); | |
3187 | goto parse_message_args; | |
3188 | } | |
3189 | first = c_parser_expr_no_commas (parser, NULL).value; | |
3190 | if (c_parser_next_token_is (parser, CPP_ELLIPSIS) | |
3191 | || c_parser_next_token_is (parser, CPP_CLOSE_SQUARE)) | |
3192 | goto array_desig_after_first; | |
3193 | /* Expression receiver. So far only one part | |
3194 | without commas has been parsed; there might be | |
3195 | more of the expression. */ | |
3196 | rec = first; | |
3197 | while (c_parser_next_token_is (parser, CPP_COMMA)) | |
3198 | { | |
f2a71bbc | 3199 | struct c_expr next; |
c2255bc4 AH |
3200 | location_t comma_loc, exp_loc; |
3201 | comma_loc = c_parser_peek_token (parser)->location; | |
27bf414c | 3202 | c_parser_consume_token (parser); |
c2255bc4 | 3203 | exp_loc = c_parser_peek_token (parser)->location; |
f2a71bbc | 3204 | next = c_parser_expr_no_commas (parser, NULL); |
c2255bc4 AH |
3205 | next = default_function_array_conversion (exp_loc, next); |
3206 | rec = build_compound_expr (comma_loc, rec, next.value); | |
27bf414c JM |
3207 | } |
3208 | parse_message_args: | |
3209 | /* Now parse the objc-message-args. */ | |
3210 | args = c_parser_objc_message_args (parser); | |
3211 | c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, | |
3212 | "expected %<]%>"); | |
3213 | mexpr.value | |
3214 | = objc_build_message_expr (build_tree_list (rec, args)); | |
3215 | mexpr.original_code = ERROR_MARK; | |
6866c6e8 | 3216 | mexpr.original_type = NULL; |
27bf414c JM |
3217 | /* Now parse and process the remainder of the |
3218 | initializer, starting with this message | |
3219 | expression as a primary-expression. */ | |
3220 | c_parser_initval (parser, &mexpr); | |
3221 | return; | |
3222 | } | |
3223 | c_parser_consume_token (parser); | |
3224 | first = c_parser_expr_no_commas (parser, NULL).value; | |
3225 | array_desig_after_first: | |
3226 | if (c_parser_next_token_is (parser, CPP_ELLIPSIS)) | |
3227 | { | |
c7412148 | 3228 | ellipsis_loc = c_parser_peek_token (parser)->location; |
27bf414c JM |
3229 | c_parser_consume_token (parser); |
3230 | second = c_parser_expr_no_commas (parser, NULL).value; | |
3231 | } | |
3232 | else | |
3233 | second = NULL_TREE; | |
3234 | if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE)) | |
3235 | { | |
3236 | c_parser_consume_token (parser); | |
3237 | set_init_index (first, second); | |
fcf73884 | 3238 | if (second) |
509c9d60 MLI |
3239 | pedwarn (ellipsis_loc, OPT_pedantic, |
3240 | "ISO C forbids specifying range of elements to initialize"); | |
27bf414c JM |
3241 | } |
3242 | else | |
3243 | c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, | |
3244 | "expected %<]%>"); | |
3245 | } | |
3246 | } | |
3247 | if (des_seen >= 1) | |
3248 | { | |
3249 | if (c_parser_next_token_is (parser, CPP_EQ)) | |
3250 | { | |
fcf73884 | 3251 | if (!flag_isoc99) |
509c9d60 MLI |
3252 | pedwarn (des_loc, OPT_pedantic, |
3253 | "ISO C90 forbids specifying subobject to initialize"); | |
27bf414c JM |
3254 | c_parser_consume_token (parser); |
3255 | } | |
3256 | else | |
3257 | { | |
3258 | if (des_seen == 1) | |
509c9d60 MLI |
3259 | pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic, |
3260 | "obsolete use of designated initializer without %<=%>"); | |
27bf414c JM |
3261 | else |
3262 | { | |
3263 | struct c_expr init; | |
3264 | init.value = error_mark_node; | |
3265 | init.original_code = ERROR_MARK; | |
6866c6e8 | 3266 | init.original_type = NULL; |
27bf414c JM |
3267 | c_parser_error (parser, "expected %<=%>"); |
3268 | c_parser_skip_until_found (parser, CPP_COMMA, NULL); | |
b295aee2 | 3269 | process_init_element (init, false); |
27bf414c JM |
3270 | return; |
3271 | } | |
3272 | } | |
3273 | } | |
3274 | } | |
3275 | c_parser_initval (parser, NULL); | |
3276 | } | |
3277 | ||
3278 | /* Parse a nested initializer; as c_parser_initializer but parses | |
3279 | initializers within braced lists, after any designators have been | |
3280 | applied. If AFTER is not NULL then it is an Objective-C message | |
3281 | expression which is the primary-expression starting the | |
3282 | initializer. */ | |
3283 | ||
3284 | static void | |
3285 | c_parser_initval (c_parser *parser, struct c_expr *after) | |
3286 | { | |
3287 | struct c_expr init; | |
3288 | gcc_assert (!after || c_dialect_objc ()); | |
3289 | if (c_parser_next_token_is (parser, CPP_OPEN_BRACE) && !after) | |
3290 | init = c_parser_braced_init (parser, NULL_TREE, true); | |
3291 | else | |
46bdb9cf | 3292 | { |
c2255bc4 | 3293 | location_t loc = c_parser_peek_token (parser)->location; |
46bdb9cf JM |
3294 | init = c_parser_expr_no_commas (parser, after); |
3295 | if (init.value != NULL_TREE | |
3296 | && TREE_CODE (init.value) != STRING_CST | |
3297 | && TREE_CODE (init.value) != COMPOUND_LITERAL_EXPR) | |
c2255bc4 | 3298 | init = default_function_array_conversion (loc, init); |
46bdb9cf | 3299 | } |
b295aee2 | 3300 | process_init_element (init, false); |
27bf414c JM |
3301 | } |
3302 | ||
3303 | /* Parse a compound statement (possibly a function body) (C90 6.6.2, | |
3304 | C99 6.8.2). | |
3305 | ||
3306 | compound-statement: | |
3307 | { block-item-list[opt] } | |
3308 | { label-declarations block-item-list } | |
3309 | ||
3310 | block-item-list: | |
3311 | block-item | |
3312 | block-item-list block-item | |
3313 | ||
3314 | block-item: | |
3315 | nested-declaration | |
3316 | statement | |
3317 | ||
3318 | nested-declaration: | |
3319 | declaration | |
3320 | ||
3321 | GNU extensions: | |
3322 | ||
3323 | compound-statement: | |
3324 | { label-declarations block-item-list } | |
3325 | ||
3326 | nested-declaration: | |
3327 | __extension__ nested-declaration | |
3328 | nested-function-definition | |
3329 | ||
3330 | label-declarations: | |
3331 | label-declaration | |
3332 | label-declarations label-declaration | |
3333 | ||
3334 | label-declaration: | |
3335 | __label__ identifier-list ; | |
3336 | ||
3337 | Allowing the mixing of declarations and code is new in C99. The | |
3338 | GNU syntax also permits (not shown above) labels at the end of | |
3339 | compound statements, which yield an error. We don't allow labels | |
3340 | on declarations; this might seem like a natural extension, but | |
3341 | there would be a conflict between attributes on the label and | |
3342 | prefix attributes on the declaration. ??? The syntax follows the | |
3343 | old parser in requiring something after label declarations. | |
3344 | Although they are erroneous if the labels declared aren't defined, | |
953ff289 DN |
3345 | is it useful for the syntax to be this way? |
3346 | ||
3347 | OpenMP: | |
3348 | ||
3349 | block-item: | |
3350 | openmp-directive | |
3351 | ||
3352 | openmp-directive: | |
3353 | barrier-directive | |
3354 | flush-directive */ | |
27bf414c JM |
3355 | |
3356 | static tree | |
3357 | c_parser_compound_statement (c_parser *parser) | |
3358 | { | |
3359 | tree stmt; | |
c2255bc4 AH |
3360 | location_t brace_loc; |
3361 | brace_loc = c_parser_peek_token (parser)->location; | |
27bf414c | 3362 | if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>")) |
5600f233 JM |
3363 | { |
3364 | /* Ensure a scope is entered and left anyway to avoid confusion | |
3365 | if we have just prepared to enter a function body. */ | |
3366 | stmt = c_begin_compound_stmt (true); | |
c2255bc4 | 3367 | c_end_compound_stmt (brace_loc, stmt, true); |
5600f233 JM |
3368 | return error_mark_node; |
3369 | } | |
27bf414c JM |
3370 | stmt = c_begin_compound_stmt (true); |
3371 | c_parser_compound_statement_nostart (parser); | |
c2255bc4 | 3372 | return c_end_compound_stmt (brace_loc, stmt, true); |
27bf414c JM |
3373 | } |
3374 | ||
3375 | /* Parse a compound statement except for the opening brace. This is | |
3376 | used for parsing both compound statements and statement expressions | |
3377 | (which follow different paths to handling the opening). */ | |
3378 | ||
3379 | static void | |
3380 | c_parser_compound_statement_nostart (c_parser *parser) | |
3381 | { | |
3382 | bool last_stmt = false; | |
3383 | bool last_label = false; | |
6ec637a4 | 3384 | bool save_valid_for_pragma = valid_location_for_stdc_pragma_p (); |
3ba09659 | 3385 | location_t label_loc = UNKNOWN_LOCATION; /* Quiet warning. */ |
27bf414c JM |
3386 | if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE)) |
3387 | { | |
3388 | c_parser_consume_token (parser); | |
3389 | return; | |
3390 | } | |
6ec637a4 | 3391 | mark_valid_location_for_stdc_pragma (true); |
27bf414c JM |
3392 | if (c_parser_next_token_is_keyword (parser, RID_LABEL)) |
3393 | { | |
3394 | /* Read zero or more forward-declarations for labels that nested | |
3395 | functions can jump to. */ | |
6ec637a4 | 3396 | mark_valid_location_for_stdc_pragma (false); |
27bf414c JM |
3397 | while (c_parser_next_token_is_keyword (parser, RID_LABEL)) |
3398 | { | |
c2255bc4 | 3399 | label_loc = c_parser_peek_token (parser)->location; |
27bf414c JM |
3400 | c_parser_consume_token (parser); |
3401 | /* Any identifiers, including those declared as type names, | |
3402 | are OK here. */ | |
3403 | while (true) | |
3404 | { | |
3405 | tree label; | |
3406 | if (c_parser_next_token_is_not (parser, CPP_NAME)) | |
3407 | { | |
3408 | c_parser_error (parser, "expected identifier"); | |
3409 | break; | |
3410 | } | |
3411 | label | |
3412 | = declare_label (c_parser_peek_token (parser)->value); | |
3413 | C_DECLARED_LABEL_FLAG (label) = 1; | |
c2255bc4 | 3414 | add_stmt (build_stmt (label_loc, DECL_EXPR, label)); |
27bf414c JM |
3415 | c_parser_consume_token (parser); |
3416 | if (c_parser_next_token_is (parser, CPP_COMMA)) | |
3417 | c_parser_consume_token (parser); | |
3418 | else | |
3419 | break; | |
3420 | } | |
3421 | c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>"); | |
3422 | } | |
c2255bc4 | 3423 | pedwarn (label_loc, OPT_pedantic, "ISO C forbids label declarations"); |
27bf414c JM |
3424 | } |
3425 | /* We must now have at least one statement, label or declaration. */ | |
3426 | if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE)) | |
3427 | { | |
6ec637a4 | 3428 | mark_valid_location_for_stdc_pragma (save_valid_for_pragma); |
27bf414c JM |
3429 | c_parser_error (parser, "expected declaration or statement"); |
3430 | c_parser_consume_token (parser); | |
3431 | return; | |
3432 | } | |
3433 | while (c_parser_next_token_is_not (parser, CPP_CLOSE_BRACE)) | |
3434 | { | |
3435 | location_t loc = c_parser_peek_token (parser)->location; | |
27bf414c JM |
3436 | if (c_parser_next_token_is_keyword (parser, RID_CASE) |
3437 | || c_parser_next_token_is_keyword (parser, RID_DEFAULT) | |
3438 | || (c_parser_next_token_is (parser, CPP_NAME) | |
3439 | && c_parser_peek_2nd_token (parser)->type == CPP_COLON)) | |
3440 | { | |
c7412148 TT |
3441 | if (c_parser_next_token_is_keyword (parser, RID_CASE)) |
3442 | label_loc = c_parser_peek_2nd_token (parser)->location; | |
3443 | else | |
3444 | label_loc = c_parser_peek_token (parser)->location; | |
27bf414c JM |
3445 | last_label = true; |
3446 | last_stmt = false; | |
6ec637a4 | 3447 | mark_valid_location_for_stdc_pragma (false); |
27bf414c JM |
3448 | c_parser_label (parser); |
3449 | } | |
3450 | else if (!last_label | |
3451 | && c_parser_next_token_starts_declspecs (parser)) | |
3452 | { | |
3453 | last_label = false; | |
6ec637a4 | 3454 | mark_valid_location_for_stdc_pragma (false); |
27bf414c | 3455 | c_parser_declaration_or_fndef (parser, true, true, true, true); |
fcf73884 | 3456 | if (last_stmt) |
509c9d60 MLI |
3457 | pedwarn_c90 (loc, |
3458 | (pedantic && !flag_isoc99) | |
fcf73884 MLI |
3459 | ? OPT_pedantic |
3460 | : OPT_Wdeclaration_after_statement, | |
509c9d60 | 3461 | "ISO C90 forbids mixed declarations and code"); |
27bf414c JM |
3462 | last_stmt = false; |
3463 | } | |
3464 | else if (!last_label | |
3465 | && c_parser_next_token_is_keyword (parser, RID_EXTENSION)) | |
3466 | { | |
3467 | /* __extension__ can start a declaration, but is also an | |
3468 | unary operator that can start an expression. Consume all | |
3469 | but the last of a possible series of __extension__ to | |
3470 | determine which. */ | |
3471 | while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD | |
3472 | && (c_parser_peek_2nd_token (parser)->keyword | |
3473 | == RID_EXTENSION)) | |
3474 | c_parser_consume_token (parser); | |
3475 | if (c_token_starts_declspecs (c_parser_peek_2nd_token (parser))) | |
3476 | { | |
3477 | int ext; | |
3478 | ext = disable_extension_diagnostics (); | |
3479 | c_parser_consume_token (parser); | |
3480 | last_label = false; | |
6ec637a4 | 3481 | mark_valid_location_for_stdc_pragma (false); |
27bf414c JM |
3482 | c_parser_declaration_or_fndef (parser, true, true, true, true); |
3483 | /* Following the old parser, __extension__ does not | |
3484 | disable this diagnostic. */ | |
3485 | restore_extension_diagnostics (ext); | |
fcf73884 | 3486 | if (last_stmt) |
509c9d60 | 3487 | pedwarn_c90 (loc, (pedantic && !flag_isoc99) |
fcf73884 MLI |
3488 | ? OPT_pedantic |
3489 | : OPT_Wdeclaration_after_statement, | |
509c9d60 | 3490 | "ISO C90 forbids mixed declarations and code"); |
27bf414c JM |
3491 | last_stmt = false; |
3492 | } | |
3493 | else | |
3494 | goto statement; | |
3495 | } | |
bc4071dd RH |
3496 | else if (c_parser_next_token_is (parser, CPP_PRAGMA)) |
3497 | { | |
3498 | /* External pragmas, and some omp pragmas, are not associated | |
3499 | with regular c code, and so are not to be considered statements | |
3500 | syntactically. This ensures that the user doesn't put them | |
3501 | places that would turn into syntax errors if the directive | |
3502 | were ignored. */ | |
3503 | if (c_parser_pragma (parser, pragma_compound)) | |
3504 | last_label = false, last_stmt = true; | |
3505 | } | |
3506 | else if (c_parser_next_token_is (parser, CPP_EOF)) | |
3507 | { | |
6ec637a4 | 3508 | mark_valid_location_for_stdc_pragma (save_valid_for_pragma); |
bc4071dd RH |
3509 | c_parser_error (parser, "expected declaration or statement"); |
3510 | return; | |
3511 | } | |
b4b56033 MLI |
3512 | else if (c_parser_next_token_is_keyword (parser, RID_ELSE)) |
3513 | { | |
3514 | if (parser->in_if_block) | |
3515 | { | |
6ec637a4 | 3516 | mark_valid_location_for_stdc_pragma (save_valid_for_pragma); |
3ba09659 | 3517 | error_at (loc, """expected %<}%> before %<else%>"); |
b4b56033 MLI |
3518 | return; |
3519 | } | |
3520 | else | |
3521 | { | |
3ba09659 | 3522 | error_at (loc, "%<else%> without a previous %<if%>"); |
b4b56033 MLI |
3523 | c_parser_consume_token (parser); |
3524 | continue; | |
3525 | } | |
3526 | } | |
27bf414c JM |
3527 | else |
3528 | { | |
3529 | statement: | |
3530 | last_label = false; | |
3531 | last_stmt = true; | |
6ec637a4 | 3532 | mark_valid_location_for_stdc_pragma (false); |
27bf414c JM |
3533 | c_parser_statement_after_labels (parser); |
3534 | } | |
2c14ae9a VR |
3535 | |
3536 | parser->error = false; | |
27bf414c JM |
3537 | } |
3538 | if (last_label) | |
3ba09659 | 3539 | error_at (label_loc, "label at end of compound statement"); |
27bf414c | 3540 | c_parser_consume_token (parser); |
6ec637a4 JJ |
3541 | /* Restore the value we started with. */ |
3542 | mark_valid_location_for_stdc_pragma (save_valid_for_pragma); | |
27bf414c JM |
3543 | } |
3544 | ||
3545 | /* Parse a label (C90 6.6.1, C99 6.8.1). | |
3546 | ||
3547 | label: | |
3548 | identifier : attributes[opt] | |
3549 | case constant-expression : | |
3550 | default : | |
3551 | ||
3552 | GNU extensions: | |
3553 | ||
3554 | label: | |
3555 | case constant-expression ... constant-expression : | |
3556 | ||
3557 | The use of attributes on labels is a GNU extension. The syntax in | |
3558 | GNU C accepts any expressions without commas, non-constant | |
3559 | expressions being rejected later. */ | |
3560 | ||
3561 | static void | |
3562 | c_parser_label (c_parser *parser) | |
3563 | { | |
3564 | location_t loc1 = c_parser_peek_token (parser)->location; | |
3565 | tree label = NULL_TREE; | |
3566 | if (c_parser_next_token_is_keyword (parser, RID_CASE)) | |
3567 | { | |
3568 | tree exp1, exp2; | |
3569 | c_parser_consume_token (parser); | |
3570 | exp1 = c_parser_expr_no_commas (parser, NULL).value; | |
3571 | if (c_parser_next_token_is (parser, CPP_COLON)) | |
3572 | { | |
3573 | c_parser_consume_token (parser); | |
c2255bc4 | 3574 | label = do_case (loc1, exp1, NULL_TREE); |
27bf414c JM |
3575 | } |
3576 | else if (c_parser_next_token_is (parser, CPP_ELLIPSIS)) | |
3577 | { | |
3578 | c_parser_consume_token (parser); | |
3579 | exp2 = c_parser_expr_no_commas (parser, NULL).value; | |
3580 | if (c_parser_require (parser, CPP_COLON, "expected %<:%>")) | |
c2255bc4 | 3581 | label = do_case (loc1, exp1, exp2); |
27bf414c JM |
3582 | } |
3583 | else | |
3584 | c_parser_error (parser, "expected %<:%> or %<...%>"); | |
3585 | } | |
3586 | else if (c_parser_next_token_is_keyword (parser, RID_DEFAULT)) | |
3587 | { | |
3588 | c_parser_consume_token (parser); | |
3589 | if (c_parser_require (parser, CPP_COLON, "expected %<:%>")) | |
c2255bc4 | 3590 | label = do_case (loc1, NULL_TREE, NULL_TREE); |
27bf414c JM |
3591 | } |
3592 | else | |
3593 | { | |
3594 | tree name = c_parser_peek_token (parser)->value; | |
3595 | tree tlab; | |
27bf414c | 3596 | tree attrs; |
c7412148 | 3597 | location_t loc2 = c_parser_peek_token (parser)->location; |
27bf414c JM |
3598 | gcc_assert (c_parser_next_token_is (parser, CPP_NAME)); |
3599 | c_parser_consume_token (parser); | |
3600 | gcc_assert (c_parser_next_token_is (parser, CPP_COLON)); | |
27bf414c JM |
3601 | c_parser_consume_token (parser); |
3602 | attrs = c_parser_attributes (parser); | |
3603 | tlab = define_label (loc2, name); | |
3604 | if (tlab) | |
3605 | { | |
3606 | decl_attributes (&tlab, attrs, 0); | |
c2255bc4 | 3607 | label = add_stmt (build_stmt (loc1, LABEL_EXPR, tlab)); |
27bf414c JM |
3608 | } |
3609 | } | |
3610 | if (label) | |
3d57f0f0 | 3611 | { |
3d57f0f0 MLI |
3612 | if (c_parser_next_token_starts_declspecs (parser) |
3613 | && !(c_parser_next_token_is (parser, CPP_NAME) | |
3614 | && c_parser_peek_2nd_token (parser)->type == CPP_COLON)) | |
3615 | { | |
3ba09659 AH |
3616 | error_at (c_parser_peek_token (parser)->location, |
3617 | "a label can only be part of a statement and " | |
3618 | "a declaration is not a statement"); | |
3d57f0f0 MLI |
3619 | c_parser_declaration_or_fndef (parser, /*fndef_ok*/ false, |
3620 | /*nested*/ true, /*empty_ok*/ false, | |
3621 | /*start_attr_ok*/ true); | |
3622 | } | |
3623 | } | |
27bf414c JM |
3624 | } |
3625 | ||
3626 | /* Parse a statement (C90 6.6, C99 6.8). | |
3627 | ||
3628 | statement: | |
3629 | labeled-statement | |
3630 | compound-statement | |
3631 | expression-statement | |
3632 | selection-statement | |
3633 | iteration-statement | |
3634 | jump-statement | |
3635 | ||
3636 | labeled-statement: | |
3637 | label statement | |
3638 | ||
3639 | expression-statement: | |
3640 | expression[opt] ; | |
3641 | ||
3642 | selection-statement: | |
3643 | if-statement | |
3644 | switch-statement | |
3645 | ||
3646 | iteration-statement: | |
3647 | while-statement | |
3648 | do-statement | |
3649 | for-statement | |
3650 | ||
3651 | jump-statement: | |
3652 | goto identifier ; | |
3653 | continue ; | |
3654 | break ; | |
3655 | return expression[opt] ; | |
3656 | ||
3657 | GNU extensions: | |
3658 | ||
3659 | statement: | |
3660 | asm-statement | |
3661 | ||
3662 | jump-statement: | |
3663 | goto * expression ; | |
3664 | ||
3665 | Objective-C: | |
3666 | ||
3667 | statement: | |
3668 | objc-throw-statement | |
3669 | objc-try-catch-statement | |
3670 | objc-synchronized-statement | |
3671 | ||
3672 | objc-throw-statement: | |
3673 | @throw expression ; | |
3674 | @throw ; | |
953ff289 DN |
3675 | |
3676 | OpenMP: | |
3677 | ||
3678 | statement: | |
3679 | openmp-construct | |
3680 | ||
3681 | openmp-construct: | |
3682 | parallel-construct | |
3683 | for-construct | |
3684 | sections-construct | |
3685 | single-construct | |
3686 | parallel-for-construct | |
3687 | parallel-sections-construct | |
3688 | master-construct | |
3689 | critical-construct | |
3690 | atomic-construct | |
3691 | ordered-construct | |
3692 | ||
3693 | parallel-construct: | |
3694 | parallel-directive structured-block | |
3695 | ||
3696 | for-construct: | |
3697 | for-directive iteration-statement | |
3698 | ||
3699 | sections-construct: | |
3700 | sections-directive section-scope | |
3701 | ||
3702 | single-construct: | |
3703 | single-directive structured-block | |
3704 | ||
3705 | parallel-for-construct: | |
3706 | parallel-for-directive iteration-statement | |
3707 | ||
3708 | parallel-sections-construct: | |
3709 | parallel-sections-directive section-scope | |
3710 | ||
3711 | master-construct: | |
3712 | master-directive structured-block | |
3713 | ||
3714 | critical-construct: | |
3715 | critical-directive structured-block | |
3716 | ||
3717 | atomic-construct: | |
3718 | atomic-directive expression-statement | |
3719 | ||
3720 | ordered-construct: | |
3721 | ordered-directive structured-block */ | |
27bf414c JM |
3722 | |
3723 | static void | |
3724 | c_parser_statement (c_parser *parser) | |
3725 | { | |
3726 | while (c_parser_next_token_is_keyword (parser, RID_CASE) | |
3727 | || c_parser_next_token_is_keyword (parser, RID_DEFAULT) | |
3728 | || (c_parser_next_token_is (parser, CPP_NAME) | |
3729 | && c_parser_peek_2nd_token (parser)->type == CPP_COLON)) | |
3730 | c_parser_label (parser); | |
3731 | c_parser_statement_after_labels (parser); | |
3732 | } | |
3733 | ||
3734 | /* Parse a statement, other than a labeled statement. */ | |
3735 | ||
3736 | static void | |
3737 | c_parser_statement_after_labels (c_parser *parser) | |
3738 | { | |
3739 | location_t loc = c_parser_peek_token (parser)->location; | |
3740 | tree stmt = NULL_TREE; | |
b4b56033 MLI |
3741 | bool in_if_block = parser->in_if_block; |
3742 | parser->in_if_block = false; | |
27bf414c JM |
3743 | switch (c_parser_peek_token (parser)->type) |
3744 | { | |
3745 | case CPP_OPEN_BRACE: | |
3746 | add_stmt (c_parser_compound_statement (parser)); | |
3747 | break; | |
3748 | case CPP_KEYWORD: | |
3749 | switch (c_parser_peek_token (parser)->keyword) | |
3750 | { | |
3751 | case RID_IF: | |
3752 | c_parser_if_statement (parser); | |
3753 | break; | |
3754 | case RID_SWITCH: | |
3755 | c_parser_switch_statement (parser); | |
3756 | break; | |
3757 | case RID_WHILE: | |
3758 | c_parser_while_statement (parser); | |
3759 | break; | |
3760 | case RID_DO: | |
3761 | c_parser_do_statement (parser); | |
3762 | break; | |
3763 | case RID_FOR: | |
3764 | c_parser_for_statement (parser); | |
3765 | break; | |
3766 | case RID_GOTO: | |
3767 | c_parser_consume_token (parser); | |
3768 | if (c_parser_next_token_is (parser, CPP_NAME)) | |
3769 | { | |
c2255bc4 AH |
3770 | stmt = c_finish_goto_label (loc, |
3771 | c_parser_peek_token (parser)->value); | |
27bf414c JM |
3772 | c_parser_consume_token (parser); |
3773 | } | |
3774 | else if (c_parser_next_token_is (parser, CPP_MULT)) | |
3775 | { | |
3776 | c_parser_consume_token (parser); | |
c2255bc4 AH |
3777 | stmt = c_finish_goto_ptr (loc, |
3778 | c_parser_expression (parser).value); | |
27bf414c JM |
3779 | } |
3780 | else | |
3781 | c_parser_error (parser, "expected identifier or %<*%>"); | |
3782 | goto expect_semicolon; | |
3783 | case RID_CONTINUE: | |
3784 | c_parser_consume_token (parser); | |
c2255bc4 | 3785 | stmt = c_finish_bc_stmt (loc, &c_cont_label, false); |
27bf414c JM |
3786 | goto expect_semicolon; |
3787 | case RID_BREAK: | |
3788 | c_parser_consume_token (parser); | |
c2255bc4 | 3789 | stmt = c_finish_bc_stmt (loc, &c_break_label, true); |
27bf414c JM |
3790 | goto expect_semicolon; |
3791 | case RID_RETURN: | |
3792 | c_parser_consume_token (parser); | |
3793 | if (c_parser_next_token_is (parser, CPP_SEMICOLON)) | |
3794 | { | |
c2255bc4 | 3795 | stmt = c_finish_return (loc, NULL_TREE, NULL_TREE); |
27bf414c JM |
3796 | c_parser_consume_token (parser); |
3797 | } | |
3798 | else | |
3799 | { | |
bbbbb16a | 3800 | struct c_expr expr = c_parser_expression_conv (parser); |
c2255bc4 | 3801 | stmt = c_finish_return (loc, expr.value, expr.original_type); |
27bf414c JM |
3802 | goto expect_semicolon; |
3803 | } | |
3804 | break; | |
3805 | case RID_ASM: | |
3806 | stmt = c_parser_asm_statement (parser); | |
3807 | break; | |
eea1139b | 3808 | case RID_THROW: |
27bf414c JM |
3809 | gcc_assert (c_dialect_objc ()); |
3810 | c_parser_consume_token (parser); | |
3811 | if (c_parser_next_token_is (parser, CPP_SEMICOLON)) | |
3812 | { | |
c2255bc4 | 3813 | stmt = objc_build_throw_stmt (loc, NULL_TREE); |
27bf414c JM |
3814 | c_parser_consume_token (parser); |
3815 | } | |
3816 | else | |
3817 | { | |
928c19bb JM |
3818 | tree expr = c_parser_expression (parser).value; |
3819 | expr = c_fully_fold (expr, false, NULL); | |
c2255bc4 | 3820 | stmt = objc_build_throw_stmt (loc, expr); |
27bf414c JM |
3821 | goto expect_semicolon; |
3822 | } | |
3823 | break; | |
eea1139b | 3824 | case RID_TRY: |
27bf414c JM |
3825 | gcc_assert (c_dialect_objc ()); |
3826 | c_parser_objc_try_catch_statement (parser); | |
3827 | break; | |
3828 | case RID_AT_SYNCHRONIZED: | |
3829 | gcc_assert (c_dialect_objc ()); | |
3830 | c_parser_objc_synchronized_statement (parser); | |
3831 | break; | |
3832 | default: | |
3833 | goto expr_stmt; | |
3834 | } | |
3835 | break; | |
3836 | case CPP_SEMICOLON: | |
3837 | c_parser_consume_token (parser); | |
3838 | break; | |
3839 | case CPP_CLOSE_PAREN: | |
3840 | case CPP_CLOSE_SQUARE: | |
3841 | /* Avoid infinite loop in error recovery: | |
3842 | c_parser_skip_until_found stops at a closing nesting | |
3843 | delimiter without consuming it, but here we need to consume | |
3844 | it to proceed further. */ | |
3845 | c_parser_error (parser, "expected statement"); | |
3846 | c_parser_consume_token (parser); | |
3847 | break; | |
bc4071dd RH |
3848 | case CPP_PRAGMA: |
3849 | c_parser_pragma (parser, pragma_stmt); | |
3850 | break; | |
27bf414c JM |
3851 | default: |
3852 | expr_stmt: | |
c2255bc4 | 3853 | stmt = c_finish_expr_stmt (loc, c_parser_expression_conv (parser).value); |
27bf414c JM |
3854 | expect_semicolon: |
3855 | c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>"); | |
3856 | break; | |
3857 | } | |
3858 | /* Two cases cannot and do not have line numbers associated: If stmt | |
3859 | is degenerate, such as "2;", then stmt is an INTEGER_CST, which | |
3860 | cannot hold line numbers. But that's OK because the statement | |
3861 | will either be changed to a MODIFY_EXPR during gimplification of | |
3862 | the statement expr, or discarded. If stmt was compound, but | |
3863 | without new variables, we will have skipped the creation of a | |
3864 | BIND and will have a bare STATEMENT_LIST. But that's OK because | |
3865 | (recursively) all of the component statements should already have | |
3866 | line numbers assigned. ??? Can we discard no-op statements | |
3867 | earlier? */ | |
c2255bc4 AH |
3868 | if (CAN_HAVE_LOCATION_P (stmt) |
3869 | && EXPR_LOCATION (stmt) == UNKNOWN_LOCATION) | |
3870 | SET_EXPR_LOCATION (stmt, loc); | |
b4b56033 MLI |
3871 | |
3872 | parser->in_if_block = in_if_block; | |
27bf414c JM |
3873 | } |
3874 | ||
ca085fd7 MLI |
3875 | /* Parse the condition from an if, do, while or for statements. */ |
3876 | ||
3877 | static tree | |
3878 | c_parser_condition (c_parser *parser) | |
3879 | { | |
c2255bc4 | 3880 | location_t loc = c_parser_peek_token (parser)->location; |
ca085fd7 | 3881 | tree cond; |
928c19bb JM |
3882 | cond = c_parser_expression_conv (parser).value; |
3883 | cond = c_objc_common_truthvalue_conversion (loc, cond); | |
3884 | cond = c_fully_fold (cond, false, NULL); | |
ca085fd7 MLI |
3885 | if (warn_sequence_point) |
3886 | verify_sequence_points (cond); | |
3887 | return cond; | |
3888 | } | |
3889 | ||
27bf414c JM |
3890 | /* Parse a parenthesized condition from an if, do or while statement. |
3891 | ||
3892 | condition: | |
3893 | ( expression ) | |
3894 | */ | |
3895 | static tree | |
3896 | c_parser_paren_condition (c_parser *parser) | |
3897 | { | |
27bf414c JM |
3898 | tree cond; |
3899 | if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>")) | |
3900 | return error_mark_node; | |
ca085fd7 | 3901 | cond = c_parser_condition (parser); |
27bf414c JM |
3902 | c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>"); |
3903 | return cond; | |
3904 | } | |
3905 | ||
3906 | /* Parse a statement which is a block in C99. */ | |
3907 | ||
3908 | static tree | |
3909 | c_parser_c99_block_statement (c_parser *parser) | |
3910 | { | |
3911 | tree block = c_begin_compound_stmt (flag_isoc99); | |
c2255bc4 | 3912 | location_t loc = c_parser_peek_token (parser)->location; |
27bf414c | 3913 | c_parser_statement (parser); |
c2255bc4 | 3914 | return c_end_compound_stmt (loc, block, flag_isoc99); |
27bf414c JM |
3915 | } |
3916 | ||
b4b56033 MLI |
3917 | /* Parse the body of an if statement. This is just parsing a |
3918 | statement but (a) it is a block in C99, (b) we track whether the | |
3919 | body is an if statement for the sake of -Wparentheses warnings, (c) | |
3920 | we handle an empty body specially for the sake of -Wempty-body | |
3921 | warnings, and (d) we call parser_compound_statement directly | |
3922 | because c_parser_statement_after_labels resets | |
3923 | parser->in_if_block. */ | |
27bf414c JM |
3924 | |
3925 | static tree | |
3926 | c_parser_if_body (c_parser *parser, bool *if_p) | |
3927 | { | |
3928 | tree block = c_begin_compound_stmt (flag_isoc99); | |
c2255bc4 | 3929 | location_t body_loc = c_parser_peek_token (parser)->location; |
27bf414c JM |
3930 | while (c_parser_next_token_is_keyword (parser, RID_CASE) |
3931 | || c_parser_next_token_is_keyword (parser, RID_DEFAULT) | |
3932 | || (c_parser_next_token_is (parser, CPP_NAME) | |
3933 | && c_parser_peek_2nd_token (parser)->type == CPP_COLON)) | |
3934 | c_parser_label (parser); | |
3935 | *if_p = c_parser_next_token_is_keyword (parser, RID_IF); | |
62e00e94 | 3936 | if (c_parser_next_token_is (parser, CPP_SEMICOLON)) |
b4b56033 | 3937 | { |
626c34b5 | 3938 | location_t loc = c_parser_peek_token (parser)->location; |
c2255bc4 | 3939 | add_stmt (build_empty_stmt (loc)); |
b4b56033 | 3940 | c_parser_consume_token (parser); |
626c34b5 PB |
3941 | if (!c_parser_next_token_is_keyword (parser, RID_ELSE)) |
3942 | warning_at (loc, OPT_Wempty_body, | |
3943 | "suggest braces around empty body in an %<if%> statement"); | |
b4b56033 MLI |
3944 | } |
3945 | else if (c_parser_next_token_is (parser, CPP_OPEN_BRACE)) | |
3946 | add_stmt (c_parser_compound_statement (parser)); | |
3947 | else | |
3948 | c_parser_statement_after_labels (parser); | |
c2255bc4 | 3949 | return c_end_compound_stmt (body_loc, block, flag_isoc99); |
b4b56033 MLI |
3950 | } |
3951 | ||
3952 | /* Parse the else body of an if statement. This is just parsing a | |
3953 | statement but (a) it is a block in C99, (b) we handle an empty body | |
3954 | specially for the sake of -Wempty-body warnings. */ | |
3955 | ||
3956 | static tree | |
3957 | c_parser_else_body (c_parser *parser) | |
3958 | { | |
c2255bc4 | 3959 | location_t else_loc = c_parser_peek_token (parser)->location; |
b4b56033 MLI |
3960 | tree block = c_begin_compound_stmt (flag_isoc99); |
3961 | while (c_parser_next_token_is_keyword (parser, RID_CASE) | |
3962 | || c_parser_next_token_is_keyword (parser, RID_DEFAULT) | |
3963 | || (c_parser_next_token_is (parser, CPP_NAME) | |
3964 | && c_parser_peek_2nd_token (parser)->type == CPP_COLON)) | |
3965 | c_parser_label (parser); | |
3966 | if (c_parser_next_token_is (parser, CPP_SEMICOLON)) | |
3967 | { | |
c2255bc4 AH |
3968 | location_t loc = c_parser_peek_token (parser)->location; |
3969 | warning_at (loc, | |
626c34b5 PB |
3970 | OPT_Wempty_body, |
3971 | "suggest braces around empty body in an %<else%> statement"); | |
c2255bc4 | 3972 | add_stmt (build_empty_stmt (loc)); |
b4b56033 MLI |
3973 | c_parser_consume_token (parser); |
3974 | } | |
3975 | else | |
3976 | c_parser_statement_after_labels (parser); | |
c2255bc4 | 3977 | return c_end_compound_stmt (else_loc, block, flag_isoc99); |
27bf414c JM |
3978 | } |
3979 | ||
3980 | /* Parse an if statement (C90 6.6.4, C99 6.8.4). | |
3981 | ||
3982 | if-statement: | |
3983 | if ( expression ) statement | |
3984 | if ( expression ) statement else statement | |
3985 | */ | |
3986 | ||
3987 | static void | |
3988 | c_parser_if_statement (c_parser *parser) | |
3989 | { | |
3990 | tree block; | |
3991 | location_t loc; | |
3992 | tree cond; | |
b4b56033 | 3993 | bool first_if = false; |
27bf414c | 3994 | tree first_body, second_body; |
b4b56033 MLI |
3995 | bool in_if_block; |
3996 | ||
27bf414c JM |
3997 | gcc_assert (c_parser_next_token_is_keyword (parser, RID_IF)); |
3998 | c_parser_consume_token (parser); | |
3999 | block = c_begin_compound_stmt (flag_isoc99); | |
4000 | loc = c_parser_peek_token (parser)->location; | |
4001 | cond = c_parser_paren_condition (parser); | |
b4b56033 MLI |
4002 | in_if_block = parser->in_if_block; |
4003 | parser->in_if_block = true; | |
27bf414c | 4004 | first_body = c_parser_if_body (parser, &first_if); |
b4b56033 | 4005 | parser->in_if_block = in_if_block; |
27bf414c JM |
4006 | if (c_parser_next_token_is_keyword (parser, RID_ELSE)) |
4007 | { | |
4008 | c_parser_consume_token (parser); | |
b4b56033 | 4009 | second_body = c_parser_else_body (parser); |
27bf414c JM |
4010 | } |
4011 | else | |
4012 | second_body = NULL_TREE; | |
4013 | c_finish_if_stmt (loc, cond, first_body, second_body, first_if); | |
c2255bc4 | 4014 | add_stmt (c_end_compound_stmt (loc, block, flag_isoc99)); |
27bf414c JM |
4015 | } |
4016 | ||
4017 | /* Parse a switch statement (C90 6.6.4, C99 6.8.4). | |
4018 | ||
4019 | switch-statement: | |
4020 | switch (expression) statement | |
4021 | */ | |
4022 | ||
4023 | static void | |
4024 | c_parser_switch_statement (c_parser *parser) | |
4025 | { | |
4026 | tree block, expr, body, save_break; | |
c2255bc4 AH |
4027 | location_t switch_loc = c_parser_peek_token (parser)->location; |
4028 | location_t switch_cond_loc; | |
27bf414c JM |
4029 | gcc_assert (c_parser_next_token_is_keyword (parser, RID_SWITCH)); |
4030 | c_parser_consume_token (parser); | |
4031 | block = c_begin_compound_stmt (flag_isoc99); | |
4032 | if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>")) | |
4033 | { | |
c2255bc4 | 4034 | switch_cond_loc = c_parser_peek_token (parser)->location; |
27bf414c JM |
4035 | expr = c_parser_expression (parser).value; |
4036 | c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>"); | |
4037 | } | |
4038 | else | |
c2255bc4 AH |
4039 | { |
4040 | switch_cond_loc = UNKNOWN_LOCATION; | |
4041 | expr = error_mark_node; | |
4042 | } | |
4043 | c_start_case (switch_loc, switch_cond_loc, expr); | |
27bf414c JM |
4044 | save_break = c_break_label; |
4045 | c_break_label = NULL_TREE; | |
4046 | body = c_parser_c99_block_statement (parser); | |
4047 | c_finish_case (body); | |
4048 | if (c_break_label) | |
c2255bc4 AH |
4049 | { |
4050 | location_t here = c_parser_peek_token (parser)->location; | |
4051 | tree t = build1 (LABEL_EXPR, void_type_node, c_break_label); | |
4052 | SET_EXPR_LOCATION (t, here); | |
4053 | add_stmt (t); | |
4054 | } | |
27bf414c | 4055 | c_break_label = save_break; |
c2255bc4 | 4056 | add_stmt (c_end_compound_stmt (switch_loc, block, flag_isoc99)); |
27bf414c JM |
4057 | } |
4058 | ||
4059 | /* Parse a while statement (C90 6.6.5, C99 6.8.5). | |
4060 | ||
4061 | while-statement: | |
4062 | while (expression) statement | |
4063 | */ | |
4064 | ||
4065 | static void | |
4066 | c_parser_while_statement (c_parser *parser) | |
4067 | { | |
4068 | tree block, cond, body, save_break, save_cont; | |
4069 | location_t loc; | |
4070 | gcc_assert (c_parser_next_token_is_keyword (parser, RID_WHILE)); | |
4071 | c_parser_consume_token (parser); | |
4072 | block = c_begin_compound_stmt (flag_isoc99); | |
4073 | loc = c_parser_peek_token (parser)->location; | |
4074 | cond = c_parser_paren_condition (parser); | |
4075 | save_break = c_break_label; | |
4076 | c_break_label = NULL_TREE; | |
4077 | save_cont = c_cont_label; | |
4078 | c_cont_label = NULL_TREE; | |
4079 | body = c_parser_c99_block_statement (parser); | |
4080 | c_finish_loop (loc, cond, NULL, body, c_break_label, c_cont_label, true); | |
c2255bc4 | 4081 | add_stmt (c_end_compound_stmt (loc, block, flag_isoc99)); |
27bf414c JM |
4082 | c_break_label = save_break; |
4083 | c_cont_label = save_cont; | |
4084 | } | |
4085 | ||
4086 | /* Parse a do statement (C90 6.6.5, C99 6.8.5). | |
4087 | ||
4088 | do-statement: | |
4089 | do statement while ( expression ) ; | |
4090 | */ | |
4091 | ||
4092 | static void | |
4093 | c_parser_do_statement (c_parser *parser) | |
4094 | { | |
4095 | tree block, cond, body, save_break, save_cont, new_break, new_cont; | |
4096 | location_t loc; | |
4097 | gcc_assert (c_parser_next_token_is_keyword (parser, RID_DO)); | |
4098 | c_parser_consume_token (parser); | |
62e00e94 | 4099 | if (c_parser_next_token_is (parser, CPP_SEMICOLON)) |
3ba09659 AH |
4100 | warning_at (c_parser_peek_token (parser)->location, |
4101 | OPT_Wempty_body, | |
4102 | "suggest braces around empty body in %<do%> statement"); | |
27bf414c JM |
4103 | block = c_begin_compound_stmt (flag_isoc99); |
4104 | loc = c_parser_peek_token (parser)->location; | |
4105 | save_break = c_break_label; | |
4106 | c_break_label = NULL_TREE; | |
4107 | save_cont = c_cont_label; | |
4108 | c_cont_label = NULL_TREE; | |
4109 | body = c_parser_c99_block_statement (parser); | |
4110 | c_parser_require_keyword (parser, RID_WHILE, "expected %<while%>"); | |
4111 | new_break = c_break_label; | |
4112 | c_break_label = save_break; | |
4113 | new_cont = c_cont_label; | |
4114 | c_cont_label = save_cont; | |
4115 | cond = c_parser_paren_condition (parser); | |
4116 | if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>")) | |
4117 | c_parser_skip_to_end_of_block_or_statement (parser); | |
4118 | c_finish_loop (loc, cond, NULL, body, new_break, new_cont, false); | |
c2255bc4 | 4119 | add_stmt (c_end_compound_stmt (loc, block, flag_isoc99)); |
27bf414c JM |
4120 | } |
4121 | ||
4122 | /* Parse a for statement (C90 6.6.5, C99 6.8.5). | |
4123 | ||
4124 | for-statement: | |
4125 | for ( expression[opt] ; expression[opt] ; expression[opt] ) statement | |
4126 | for ( nested-declaration expression[opt] ; expression[opt] ) statement | |
4127 | ||
4128 | The form with a declaration is new in C99. | |
4129 | ||
4130 | ??? In accordance with the old parser, the declaration may be a | |
4131 | nested function, which is then rejected in check_for_loop_decls, | |
4132 | but does it make any sense for this to be included in the grammar? | |
4133 | Note in particular that the nested function does not include a | |
4134 | trailing ';', whereas the "declaration" production includes one. | |
4135 | Also, can we reject bad declarations earlier and cheaper than | |
4136 | check_for_loop_decls? */ | |
4137 | ||
4138 | static void | |
4139 | c_parser_for_statement (c_parser *parser) | |
4140 | { | |
4141 | tree block, cond, incr, save_break, save_cont, body; | |
c2255bc4 AH |
4142 | location_t loc = c_parser_peek_token (parser)->location; |
4143 | location_t for_loc = c_parser_peek_token (parser)->location; | |
27bf414c JM |
4144 | gcc_assert (c_parser_next_token_is_keyword (parser, RID_FOR)); |
4145 | c_parser_consume_token (parser); | |
4146 | block = c_begin_compound_stmt (flag_isoc99); | |
4147 | if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>")) | |
4148 | { | |
4149 | /* Parse the initialization declaration or expression. */ | |
4150 | if (c_parser_next_token_is (parser, CPP_SEMICOLON)) | |
4151 | { | |
4152 | c_parser_consume_token (parser); | |
c2255bc4 | 4153 | c_finish_expr_stmt (loc, NULL_TREE); |
27bf414c JM |
4154 | } |
4155 | else if (c_parser_next_token_starts_declspecs (parser)) | |
4156 | { | |
4157 | c_parser_declaration_or_fndef (parser, true, true, true, true); | |
c2255bc4 | 4158 | check_for_loop_decls (for_loc); |
27bf414c JM |
4159 | } |
4160 | else if (c_parser_next_token_is_keyword (parser, RID_EXTENSION)) | |
4161 | { | |
4162 | /* __extension__ can start a declaration, but is also an | |
4163 | unary operator that can start an expression. Consume all | |
4164 | but the last of a possible series of __extension__ to | |
4165 | determine which. */ | |
4166 | while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD | |
4167 | && (c_parser_peek_2nd_token (parser)->keyword | |
4168 | == RID_EXTENSION)) | |
4169 | c_parser_consume_token (parser); | |
4170 | if (c_token_starts_declspecs (c_parser_peek_2nd_token (parser))) | |
4171 | { | |
4172 | int ext; | |
4173 | ext = disable_extension_diagnostics (); | |
4174 | c_parser_consume_token (parser); | |
4175 | c_parser_declaration_or_fndef (parser, true, true, true, true); | |
4176 | restore_extension_diagnostics (ext); | |
c2255bc4 | 4177 | check_for_loop_decls (for_loc); |
27bf414c JM |
4178 | } |
4179 | else | |
4180 | goto init_expr; | |
4181 | } | |
4182 | else | |
4183 | { | |
4184 | init_expr: | |
c2255bc4 | 4185 | c_finish_expr_stmt (loc, c_parser_expression (parser).value); |
27bf414c JM |
4186 | c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>"); |
4187 | } | |
4188 | /* Parse the loop condition. */ | |
27bf414c JM |
4189 | if (c_parser_next_token_is (parser, CPP_SEMICOLON)) |
4190 | { | |
4191 | c_parser_consume_token (parser); | |
4192 | cond = NULL_TREE; | |
4193 | } | |
4194 | else | |
4195 | { | |
ca085fd7 | 4196 | cond = c_parser_condition (parser); |
27bf414c JM |
4197 | c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>"); |
4198 | } | |
4199 | /* Parse the increment expression. */ | |
4200 | if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN)) | |
c2255bc4 | 4201 | incr = c_process_expr_stmt (loc, NULL_TREE); |
27bf414c | 4202 | else |
c2255bc4 | 4203 | incr = c_process_expr_stmt (loc, c_parser_expression (parser).value); |
27bf414c JM |
4204 | c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>"); |
4205 | } | |
4206 | else | |
4207 | { | |
4208 | cond = error_mark_node; | |
4209 | incr = error_mark_node; | |
4210 | } | |
4211 | save_break = c_break_label; | |
4212 | c_break_label = NULL_TREE; | |
4213 | save_cont = c_cont_label; | |
4214 | c_cont_label = NULL_TREE; | |
4215 | body = c_parser_c99_block_statement (parser); | |
4216 | c_finish_loop (loc, cond, incr, body, c_break_label, c_cont_label, true); | |
c2255bc4 | 4217 | add_stmt (c_end_compound_stmt (loc, block, flag_isoc99)); |
27bf414c JM |
4218 | c_break_label = save_break; |
4219 | c_cont_label = save_cont; | |
4220 | } | |
4221 | ||
4222 | /* Parse an asm statement, a GNU extension. This is a full-blown asm | |
4223 | statement with inputs, outputs, clobbers, and volatile tag | |
4224 | allowed. | |
4225 | ||
4226 | asm-statement: | |
4227 | asm type-qualifier[opt] ( asm-argument ) ; | |
4228 | ||
4229 | asm-argument: | |
4230 | asm-string-literal | |
4231 | asm-string-literal : asm-operands[opt] | |
4232 | asm-string-literal : asm-operands[opt] : asm-operands[opt] | |
4233 | asm-string-literal : asm-operands[opt] : asm-operands[opt] : asm-clobbers | |
4234 | ||
4235 | Qualifiers other than volatile are accepted in the syntax but | |
4236 | warned for. */ | |
4237 | ||
4238 | static tree | |
4239 | c_parser_asm_statement (c_parser *parser) | |
4240 | { | |
4241 | tree quals, str, outputs, inputs, clobbers, ret; | |
4242 | bool simple; | |
c2255bc4 | 4243 | location_t asm_loc = c_parser_peek_token (parser)->location; |
27bf414c JM |
4244 | gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM)); |
4245 | c_parser_consume_token (parser); | |
4246 | if (c_parser_next_token_is_keyword (parser, RID_VOLATILE)) | |
4247 | { | |
4248 | quals = c_parser_peek_token (parser)->value; | |
4249 | c_parser_consume_token (parser); | |
4250 | } | |
4251 | else if (c_parser_next_token_is_keyword (parser, RID_CONST) | |
4252 | || c_parser_next_token_is_keyword (parser, RID_RESTRICT)) | |
4253 | { | |
3ba09659 AH |
4254 | warning_at (c_parser_peek_token (parser)->location, |
4255 | 0, | |
4256 | "%E qualifier ignored on asm", | |
4257 | c_parser_peek_token (parser)->value); | |
27bf414c JM |
4258 | quals = NULL_TREE; |
4259 | c_parser_consume_token (parser); | |
4260 | } | |
4261 | else | |
4262 | quals = NULL_TREE; | |
4263 | /* ??? Follow the C++ parser rather than using the | |
46c2514e TT |
4264 | lex_untranslated_string kludge. */ |
4265 | parser->lex_untranslated_string = true; | |
27bf414c JM |
4266 | if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>")) |
4267 | { | |
46c2514e | 4268 | parser->lex_untranslated_string = false; |
27bf414c JM |
4269 | return NULL_TREE; |
4270 | } | |
4271 | str = c_parser_asm_string_literal (parser); | |
b85eb797 JJ |
4272 | if (str == NULL_TREE) |
4273 | { | |
4274 | parser->lex_untranslated_string = false; | |
4275 | c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL); | |
4276 | return NULL_TREE; | |
4277 | } | |
27bf414c JM |
4278 | if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN)) |
4279 | { | |
4280 | simple = true; | |
4281 | outputs = NULL_TREE; | |
4282 | inputs = NULL_TREE; | |
4283 | clobbers = NULL_TREE; | |
4284 | goto done_asm; | |
4285 | } | |
4286 | if (!c_parser_require (parser, CPP_COLON, "expected %<:%> or %<)%>")) | |
4287 | { | |
46c2514e | 4288 | parser->lex_untranslated_string = false; |
27bf414c JM |
4289 | c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL); |
4290 | return NULL_TREE; | |
4291 | } | |
4292 | simple = false; | |
4293 | /* Parse outputs. */ | |
4294 | if (c_parser_next_token_is (parser, CPP_COLON) | |
4295 | || c_parser_next_token_is (parser, CPP_CLOSE_PAREN)) | |
4296 | outputs = NULL_TREE; | |
4297 | else | |
46bdb9cf | 4298 | outputs = c_parser_asm_operands (parser, false); |
27bf414c JM |
4299 | if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN)) |
4300 | { | |
4301 | inputs = NULL_TREE; | |
4302 | clobbers = NULL_TREE; | |
4303 | goto done_asm; | |
4304 | } | |
4305 | if (!c_parser_require (parser, CPP_COLON, "expected %<:%> or %<)%>")) | |
4306 | { | |
46c2514e | 4307 | parser->lex_untranslated_string = false; |
27bf414c JM |
4308 | c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL); |
4309 | return NULL_TREE; | |
4310 | } | |
4311 | /* Parse inputs. */ | |
4312 | if (c_parser_next_token_is (parser, CPP_COLON) | |
4313 | || c_parser_next_token_is (parser, CPP_CLOSE_PAREN)) | |
4314 | inputs = NULL_TREE; | |
4315 | else | |
46bdb9cf | 4316 | inputs = c_parser_asm_operands (parser, true); |
27bf414c JM |
4317 | if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN)) |
4318 | { | |
4319 | clobbers = NULL_TREE; | |
4320 | goto done_asm; | |
4321 | } | |
4322 | if (!c_parser_require (parser, CPP_COLON, "expected %<:%> or %<)%>")) | |
4323 | { | |
46c2514e | 4324 | parser->lex_untranslated_string = false; |
27bf414c JM |
4325 | c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL); |
4326 | return NULL_TREE; | |
4327 | } | |
4328 | /* Parse clobbers. */ | |
4329 | clobbers = c_parser_asm_clobbers (parser); | |
4330 | done_asm: | |
46c2514e | 4331 | parser->lex_untranslated_string = false; |
27bf414c JM |
4332 | if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>")) |
4333 | { | |
4334 | c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL); | |
4335 | return NULL_TREE; | |
4336 | } | |
4337 | if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>")) | |
4338 | c_parser_skip_to_end_of_block_or_statement (parser); | |
c2255bc4 | 4339 | ret = build_asm_stmt (quals, build_asm_expr (asm_loc, str, outputs, inputs, |
27bf414c JM |
4340 | clobbers, simple)); |
4341 | return ret; | |
4342 | } | |
4343 | ||
46bdb9cf JM |
4344 | /* Parse asm operands, a GNU extension. If CONVERT_P (for inputs but |
4345 | not outputs), apply the default conversion of functions and arrays | |
4346 | to pointers. | |
27bf414c JM |
4347 | |
4348 | asm-operands: | |
4349 | asm-operand | |
4350 | asm-operands , asm-operand | |
4351 | ||
4352 | asm-operand: | |
4353 | asm-string-literal ( expression ) | |
4354 | [ identifier ] asm-string-literal ( expression ) | |
4355 | */ | |
4356 | ||
4357 | static tree | |
46bdb9cf | 4358 | c_parser_asm_operands (c_parser *parser, bool convert_p) |
27bf414c JM |
4359 | { |
4360 | tree list = NULL_TREE; | |
c2255bc4 | 4361 | location_t loc; |
27bf414c JM |
4362 | while (true) |
4363 | { | |
f2a71bbc JM |
4364 | tree name, str; |
4365 | struct c_expr expr; | |
27bf414c JM |
4366 | if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE)) |
4367 | { | |
4368 | c_parser_consume_token (parser); | |
4369 | if (c_parser_next_token_is (parser, CPP_NAME)) | |
4370 | { | |
4371 | tree id = c_parser_peek_token (parser)->value; | |
4372 | c_parser_consume_token (parser); | |
4373 | name = build_string (IDENTIFIER_LENGTH (id), | |
4374 | IDENTIFIER_POINTER (id)); | |
4375 | } | |
4376 | else | |
4377 | { | |
4378 | c_parser_error (parser, "expected identifier"); | |
4379 | c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL); | |
4380 | return NULL_TREE; | |
4381 | } | |
4382 | c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, | |
4383 | "expected %<]%>"); | |
4384 | } | |
4385 | else | |
4386 | name = NULL_TREE; | |
4387 | str = c_parser_asm_string_literal (parser); | |
4388 | if (str == NULL_TREE) | |
4389 | return NULL_TREE; | |
46c2514e | 4390 | parser->lex_untranslated_string = false; |
27bf414c JM |
4391 | if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>")) |
4392 | { | |
46c2514e | 4393 | parser->lex_untranslated_string = true; |
27bf414c JM |
4394 | return NULL_TREE; |
4395 | } | |
c2255bc4 | 4396 | loc = c_parser_peek_token (parser)->location; |
f2a71bbc | 4397 | expr = c_parser_expression (parser); |
46bdb9cf | 4398 | if (convert_p) |
c2255bc4 | 4399 | expr = default_function_array_conversion (loc, expr); |
928c19bb | 4400 | expr.value = c_fully_fold (expr.value, false, NULL); |
46c2514e | 4401 | parser->lex_untranslated_string = true; |
27bf414c JM |
4402 | if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>")) |
4403 | { | |
4404 | c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL); | |
4405 | return NULL_TREE; | |
4406 | } | |
4407 | list = chainon (list, build_tree_list (build_tree_list (name, str), | |
f2a71bbc | 4408 | expr.value)); |
27bf414c JM |
4409 | if (c_parser_next_token_is (parser, CPP_COMMA)) |
4410 | c_parser_consume_token (parser); | |
4411 | else | |
4412 | break; | |
4413 | } | |
4414 | return list; | |
4415 | } | |
4416 | ||
4417 | /* Parse asm clobbers, a GNU extension. | |
4418 | ||
4419 | asm-clobbers: | |
4420 | asm-string-literal | |
4421 | asm-clobbers , asm-string-literal | |
4422 | */ | |
4423 | ||
4424 | static tree | |
4425 | c_parser_asm_clobbers (c_parser *parser) | |
4426 | { | |
4427 | tree list = NULL_TREE; | |
4428 | while (true) | |
4429 | { | |
4430 | tree str = c_parser_asm_string_literal (parser); | |
4431 | if (str) | |
4432 | list = tree_cons (NULL_TREE, str, list); | |
4433 | else | |
4434 | return NULL_TREE; | |
4435 | if (c_parser_next_token_is (parser, CPP_COMMA)) | |
4436 | c_parser_consume_token (parser); | |
4437 | else | |
4438 | break; | |
4439 | } | |
4440 | return list; | |
4441 | } | |
4442 | ||
4443 | /* Parse an expression other than a compound expression; that is, an | |
4444 | assignment expression (C90 6.3.16, C99 6.5.16). If AFTER is not | |
4445 | NULL then it is an Objective-C message expression which is the | |
4446 | primary-expression starting the expression as an initializer. | |
4447 | ||
4448 | assignment-expression: | |
4449 | conditional-expression | |
4450 | unary-expression assignment-operator assignment-expression | |
4451 | ||
4452 | assignment-operator: one of | |
4453 | = *= /= %= += -= <<= >>= &= ^= |= | |
4454 | ||
4455 | In GNU C we accept any conditional expression on the LHS and | |
4456 | diagnose the invalid lvalue rather than producing a syntax | |
4457 | error. */ | |
4458 | ||
4459 | static struct c_expr | |
4460 | c_parser_expr_no_commas (c_parser *parser, struct c_expr *after) | |
4461 | { | |
4462 | struct c_expr lhs, rhs, ret; | |
4463 | enum tree_code code; | |
c2255bc4 | 4464 | location_t op_location, exp_location; |
27bf414c JM |
4465 | gcc_assert (!after || c_dialect_objc ()); |
4466 | lhs = c_parser_conditional_expression (parser, after); | |
c9f9eb5d | 4467 | op_location = c_parser_peek_token (parser)->location; |
27bf414c JM |
4468 | switch (c_parser_peek_token (parser)->type) |
4469 | { | |
4470 | case CPP_EQ: | |
4471 | code = NOP_EXPR; | |
4472 | break; | |
4473 | case CPP_MULT_EQ: | |
4474 | code = MULT_EXPR; | |
4475 | break; | |
4476 | case CPP_DIV_EQ: | |
4477 | code = TRUNC_DIV_EXPR; | |
4478 | break; | |
4479 | case CPP_MOD_EQ: | |
4480 | code = TRUNC_MOD_EXPR; | |
4481 | break; | |
4482 | case CPP_PLUS_EQ: | |
4483 | code = PLUS_EXPR; | |
4484 | break; | |
4485 | case CPP_MINUS_EQ: | |
4486 | code = MINUS_EXPR; | |
4487 | break; | |
4488 | case CPP_LSHIFT_EQ: | |
4489 | code = LSHIFT_EXPR; | |
4490 | break; | |
4491 | case CPP_RSHIFT_EQ: | |
4492 | code = RSHIFT_EXPR; | |
4493 | break; | |
4494 | case CPP_AND_EQ: | |
4495 | code = BIT_AND_EXPR; | |
4496 | break; | |
4497 | case CPP_XOR_EQ: | |
4498 | code = BIT_XOR_EXPR; | |
4499 | break; | |
4500 | case CPP_OR_EQ: | |
4501 | code = BIT_IOR_EXPR; | |
4502 | break; | |
4503 | default: | |
4504 | return lhs; | |
4505 | } | |
4506 | c_parser_consume_token (parser); | |
c2255bc4 | 4507 | exp_location = c_parser_peek_token (parser)->location; |
27bf414c | 4508 | rhs = c_parser_expr_no_commas (parser, NULL); |
c2255bc4 | 4509 | rhs = default_function_array_conversion (exp_location, rhs); |
32e8bb8e | 4510 | ret.value = build_modify_expr (op_location, lhs.value, lhs.original_type, |
c2255bc4 AH |
4511 | code, exp_location, rhs.value, |
4512 | rhs.original_type); | |
27bf414c JM |
4513 | if (code == NOP_EXPR) |
4514 | ret.original_code = MODIFY_EXPR; | |
4515 | else | |
4516 | { | |
4517 | TREE_NO_WARNING (ret.value) = 1; | |
4518 | ret.original_code = ERROR_MARK; | |
4519 | } | |
6866c6e8 | 4520 | ret.original_type = NULL; |
27bf414c JM |
4521 | return ret; |
4522 | } | |
4523 | ||
4524 | /* Parse a conditional expression (C90 6.3.15, C99 6.5.15). If AFTER | |
4525 | is not NULL then it is an Objective-C message expression which is | |
4526 | the primary-expression starting the expression as an initializer. | |
4527 | ||
4528 | conditional-expression: | |
4529 | logical-OR-expression | |
4530 | logical-OR-expression ? expression : conditional-expression | |
4531 | ||
4532 | GNU extensions: | |
4533 | ||
4534 | conditional-expression: | |
4535 | logical-OR-expression ? : conditional-expression | |
4536 | */ | |
4537 | ||
4538 | static struct c_expr | |
4539 | c_parser_conditional_expression (c_parser *parser, struct c_expr *after) | |
4540 | { | |
4541 | struct c_expr cond, exp1, exp2, ret; | |
744aa42f | 4542 | location_t cond_loc, colon_loc; |
ba47d38d | 4543 | |
27bf414c | 4544 | gcc_assert (!after || c_dialect_objc ()); |
ba47d38d | 4545 | |
27bf414c | 4546 | cond = c_parser_binary_expression (parser, after); |
ba47d38d | 4547 | |
27bf414c JM |
4548 | if (c_parser_next_token_is_not (parser, CPP_QUERY)) |
4549 | return cond; | |
c2255bc4 AH |
4550 | cond_loc = c_parser_peek_token (parser)->location; |
4551 | cond = default_function_array_conversion (cond_loc, cond); | |
27bf414c JM |
4552 | c_parser_consume_token (parser); |
4553 | if (c_parser_next_token_is (parser, CPP_COLON)) | |
4554 | { | |
8ce94e44 | 4555 | tree eptype = NULL_TREE; |
509c9d60 MLI |
4556 | pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic, |
4557 | "ISO C forbids omitting the middle term of a ?: expression"); | |
8ce94e44 JM |
4558 | if (TREE_CODE (cond.value) == EXCESS_PRECISION_EXPR) |
4559 | { | |
4560 | eptype = TREE_TYPE (cond.value); | |
4561 | cond.value = TREE_OPERAND (cond.value, 0); | |
4562 | } | |
27bf414c | 4563 | /* Make sure first operand is calculated only once. */ |
928c19bb | 4564 | exp1.value = c_save_expr (default_conversion (cond.value)); |
8ce94e44 JM |
4565 | if (eptype) |
4566 | exp1.value = build1 (EXCESS_PRECISION_EXPR, eptype, exp1.value); | |
6866c6e8 | 4567 | exp1.original_type = NULL; |
ba47d38d | 4568 | cond.value = c_objc_common_truthvalue_conversion (cond_loc, exp1.value); |
7d882b83 | 4569 | c_inhibit_evaluation_warnings += cond.value == truthvalue_true_node; |
27bf414c JM |
4570 | } |
4571 | else | |
4572 | { | |
4573 | cond.value | |
85498824 | 4574 | = c_objc_common_truthvalue_conversion |
ba47d38d | 4575 | (cond_loc, default_conversion (cond.value)); |
7d882b83 | 4576 | c_inhibit_evaluation_warnings += cond.value == truthvalue_false_node; |
46bdb9cf | 4577 | exp1 = c_parser_expression_conv (parser); |
7d882b83 ILT |
4578 | c_inhibit_evaluation_warnings += |
4579 | ((cond.value == truthvalue_true_node) | |
4580 | - (cond.value == truthvalue_false_node)); | |
27bf414c | 4581 | } |
744aa42f ILT |
4582 | |
4583 | colon_loc = c_parser_peek_token (parser)->location; | |
27bf414c JM |
4584 | if (!c_parser_require (parser, CPP_COLON, "expected %<:%>")) |
4585 | { | |
7d882b83 | 4586 | c_inhibit_evaluation_warnings -= cond.value == truthvalue_true_node; |
27bf414c JM |
4587 | ret.value = error_mark_node; |
4588 | ret.original_code = ERROR_MARK; | |
6866c6e8 | 4589 | ret.original_type = NULL; |
27bf414c JM |
4590 | return ret; |
4591 | } | |
c2255bc4 AH |
4592 | { |
4593 | location_t exp2_loc = c_parser_peek_token (parser)->location; | |
4594 | exp2 = c_parser_conditional_expression (parser, NULL); | |
4595 | exp2 = default_function_array_conversion (exp2_loc, exp2); | |
4596 | } | |
7d882b83 | 4597 | c_inhibit_evaluation_warnings -= cond.value == truthvalue_true_node; |
744aa42f | 4598 | ret.value = build_conditional_expr (colon_loc, cond.value, |
928c19bb | 4599 | cond.original_code == C_MAYBE_CONST_EXPR, |
d130ae11 ILT |
4600 | exp1.value, exp1.original_type, |
4601 | exp2.value, exp2.original_type); | |
27bf414c | 4602 | ret.original_code = ERROR_MARK; |
6866c6e8 ILT |
4603 | if (exp1.value == error_mark_node || exp2.value == error_mark_node) |
4604 | ret.original_type = NULL; | |
4605 | else | |
4606 | { | |
4607 | tree t1, t2; | |
4608 | ||
4609 | /* If both sides are enum type, the default conversion will have | |
4610 | made the type of the result be an integer type. We want to | |
4611 | remember the enum types we started with. */ | |
4612 | t1 = exp1.original_type ? exp1.original_type : TREE_TYPE (exp1.value); | |
4613 | t2 = exp2.original_type ? exp2.original_type : TREE_TYPE (exp2.value); | |
4614 | ret.original_type = ((t1 != error_mark_node | |
4615 | && t2 != error_mark_node | |
4616 | && (TYPE_MAIN_VARIANT (t1) | |
4617 | == TYPE_MAIN_VARIANT (t2))) | |
4618 | ? t1 | |
4619 | : NULL); | |
4620 | } | |
27bf414c JM |
4621 | return ret; |
4622 | } | |
4623 | ||
4624 | /* Parse a binary expression; that is, a logical-OR-expression (C90 | |
4625 | 6.3.5-6.3.14, C99 6.5.5-6.5.14). If AFTER is not NULL then it is | |
4626 | an Objective-C message expression which is the primary-expression | |
4627 | starting the expression as an initializer. | |
4628 | ||
4629 | multiplicative-expression: | |
4630 | cast-expression | |
4631 | multiplicative-expression * cast-expression | |
4632 | multiplicative-expression / cast-expression | |
4633 | multiplicative-expression % cast-expression | |
4634 | ||
4635 | additive-expression: | |
4636 | multiplicative-expression | |
4637 | additive-expression + multiplicative-expression | |
4638 | additive-expression - multiplicative-expression | |
4639 | ||
4640 | shift-expression: | |
4641 | additive-expression | |
4642 | shift-expression << additive-expression | |
4643 | shift-expression >> additive-expression | |
4644 | ||
4645 | relational-expression: | |
4646 | shift-expression | |
4647 | relational-expression < shift-expression | |
4648 | relational-expression > shift-expression | |
4649 | relational-expression <= shift-expression | |
4650 | relational-expression >= shift-expression | |
4651 | ||
4652 | equality-expression: | |
4653 | relational-expression | |
4654 | equality-expression == relational-expression | |
4655 | equality-expression != relational-expression | |
4656 | ||
4657 | AND-expression: | |
4658 | equality-expression | |
4659 | AND-expression & equality-expression | |
4660 | ||
4661 | exclusive-OR-expression: | |
4662 | AND-expression | |
4663 | exclusive-OR-expression ^ AND-expression | |
4664 | ||
4665 | inclusive-OR-expression: | |
4666 | exclusive-OR-expression | |
4667 | inclusive-OR-expression | exclusive-OR-expression | |
4668 | ||
4669 | logical-AND-expression: | |
4670 | inclusive-OR-expression | |
4671 | logical-AND-expression && inclusive-OR-expression | |
4672 | ||
4673 | logical-OR-expression: | |
4674 | logical-AND-expression | |
4675 | logical-OR-expression || logical-AND-expression | |
4676 | */ | |
4677 | ||
4678 | static struct c_expr | |
4679 | c_parser_binary_expression (c_parser *parser, struct c_expr *after) | |
4680 | { | |
4681 | /* A binary expression is parsed using operator-precedence parsing, | |
4682 | with the operands being cast expressions. All the binary | |
4683 | operators are left-associative. Thus a binary expression is of | |
4684 | form: | |
4685 | ||
4686 | E0 op1 E1 op2 E2 ... | |
4687 | ||
4688 | which we represent on a stack. On the stack, the precedence | |
4689 | levels are strictly increasing. When a new operator is | |
4690 | encountered of higher precedence than that at the top of the | |
4691 | stack, it is pushed; its LHS is the top expression, and its RHS | |
4692 | is everything parsed until it is popped. When a new operator is | |
4693 | encountered with precedence less than or equal to that at the top | |
4694 | of the stack, triples E[i-1] op[i] E[i] are popped and replaced | |
4695 | by the result of the operation until the operator at the top of | |
4696 | the stack has lower precedence than the new operator or there is | |
4697 | only one element on the stack; then the top expression is the LHS | |
4698 | of the new operator. In the case of logical AND and OR | |
7d882b83 ILT |
4699 | expressions, we also need to adjust c_inhibit_evaluation_warnings |
4700 | as appropriate when the operators are pushed and popped. */ | |
27bf414c JM |
4701 | |
4702 | /* The precedence levels, where 0 is a dummy lowest level used for | |
4703 | the bottom of the stack. */ | |
4704 | enum prec { | |
4705 | PREC_NONE, | |
4706 | PREC_LOGOR, | |
4707 | PREC_LOGAND, | |
4708 | PREC_BITOR, | |
4709 | PREC_BITXOR, | |
4710 | PREC_BITAND, | |
4711 | PREC_EQ, | |
4712 | PREC_REL, | |
4713 | PREC_SHIFT, | |
4714 | PREC_ADD, | |
4715 | PREC_MULT, | |
4716 | NUM_PRECS | |
4717 | }; | |
4718 | struct { | |
4719 | /* The expression at this stack level. */ | |
4720 | struct c_expr expr; | |
4721 | /* The precedence of the operator on its left, PREC_NONE at the | |
4722 | bottom of the stack. */ | |
4723 | enum prec prec; | |
4724 | /* The operation on its left. */ | |
4725 | enum tree_code op; | |
ca80e52b EB |
4726 | /* The source location of this operation. */ |
4727 | location_t loc; | |
27bf414c JM |
4728 | } stack[NUM_PRECS]; |
4729 | int sp; | |
ba47d38d | 4730 | /* Location of the binary operator. */ |
1f6d0c60 | 4731 | location_t binary_loc = UNKNOWN_LOCATION; /* Quiet warning. */ |
27bf414c JM |
4732 | #define POP \ |
4733 | do { \ | |
4734 | switch (stack[sp].op) \ | |
4735 | { \ | |
4736 | case TRUTH_ANDIF_EXPR: \ | |
7d882b83 ILT |
4737 | c_inhibit_evaluation_warnings -= (stack[sp - 1].expr.value \ |
4738 | == truthvalue_false_node); \ | |
27bf414c JM |
4739 | break; \ |
4740 | case TRUTH_ORIF_EXPR: \ | |
7d882b83 ILT |
4741 | c_inhibit_evaluation_warnings -= (stack[sp - 1].expr.value \ |
4742 | == truthvalue_true_node); \ | |
27bf414c JM |
4743 | break; \ |
4744 | default: \ | |
4745 | break; \ | |
4746 | } \ | |
f2a71bbc | 4747 | stack[sp - 1].expr \ |
c2255bc4 AH |
4748 | = default_function_array_conversion (stack[sp - 1].loc, \ |
4749 | stack[sp - 1].expr); \ | |
f2a71bbc | 4750 | stack[sp].expr \ |
c2255bc4 | 4751 | = default_function_array_conversion (stack[sp].loc, stack[sp].expr); \ |
ca80e52b | 4752 | stack[sp - 1].expr = parser_build_binary_op (stack[sp].loc, \ |
ba47d38d | 4753 | stack[sp].op, \ |
27bf414c JM |
4754 | stack[sp - 1].expr, \ |
4755 | stack[sp].expr); \ | |
4756 | sp--; \ | |
4757 | } while (0) | |
4758 | gcc_assert (!after || c_dialect_objc ()); | |
ca80e52b | 4759 | stack[0].loc = c_parser_peek_token (parser)->location; |
27bf414c JM |
4760 | stack[0].expr = c_parser_cast_expression (parser, after); |
4761 | stack[0].prec = PREC_NONE; | |
4762 | sp = 0; | |
4763 | while (true) | |
4764 | { | |
4765 | enum prec oprec; | |
4766 | enum tree_code ocode; | |
4767 | if (parser->error) | |
4768 | goto out; | |
4769 | switch (c_parser_peek_token (parser)->type) | |
4770 | { | |
4771 | case CPP_MULT: | |
4772 | oprec = PREC_MULT; | |
4773 | ocode = MULT_EXPR; | |
4774 | break; | |
4775 | case CPP_DIV: | |
4776 | oprec = PREC_MULT; | |
4777 | ocode = TRUNC_DIV_EXPR; | |
4778 | break; | |
4779 | case CPP_MOD: | |
4780 | oprec = PREC_MULT; | |
4781 | ocode = TRUNC_MOD_EXPR; | |
4782 | break; | |
4783 | case CPP_PLUS: | |
4784 | oprec = PREC_ADD; | |
4785 | ocode = PLUS_EXPR; | |
4786 | break; | |
4787 | case CPP_MINUS: | |
4788 | oprec = PREC_ADD; | |
4789 | ocode = MINUS_EXPR; | |
4790 | break; | |
4791 | case CPP_LSHIFT: | |
4792 | oprec = PREC_SHIFT; | |
4793 | ocode = LSHIFT_EXPR; | |
4794 | break; | |
4795 | case CPP_RSHIFT: | |
4796 | oprec = PREC_SHIFT; | |
4797 | ocode = RSHIFT_EXPR; | |
4798 | break; | |
4799 | case CPP_LESS: | |
4800 | oprec = PREC_REL; | |
4801 | ocode = LT_EXPR; | |
4802 | break; | |
4803 | case CPP_GREATER: | |
4804 | oprec = PREC_REL; | |
4805 | ocode = GT_EXPR; | |
4806 | break; | |
4807 | case CPP_LESS_EQ: | |
4808 | oprec = PREC_REL; | |
4809 | ocode = LE_EXPR; | |
4810 | break; | |
4811 | case CPP_GREATER_EQ: | |
4812 | oprec = PREC_REL; | |
4813 | ocode = GE_EXPR; | |
4814 | break; | |
4815 | case CPP_EQ_EQ: | |
4816 | oprec = PREC_EQ; | |
4817 | ocode = EQ_EXPR; | |
4818 | break; | |
4819 | case CPP_NOT_EQ: | |
4820 | oprec = PREC_EQ; | |
4821 | ocode = NE_EXPR; | |
4822 | break; | |
4823 | case CPP_AND: | |
4824 | oprec = PREC_BITAND; | |
4825 | ocode = BIT_AND_EXPR; | |
4826 | break; | |
4827 | case CPP_XOR: | |
4828 | oprec = PREC_BITXOR; | |
4829 | ocode = BIT_XOR_EXPR; | |
4830 | break; | |
4831 | case CPP_OR: | |
4832 | oprec = PREC_BITOR; | |
4833 | ocode = BIT_IOR_EXPR; | |
4834 | break; | |
4835 | case CPP_AND_AND: | |
4836 | oprec = PREC_LOGAND; | |
4837 | ocode = TRUTH_ANDIF_EXPR; | |
4838 | break; | |
4839 | case CPP_OR_OR: | |
4840 | oprec = PREC_LOGOR; | |
4841 | ocode = TRUTH_ORIF_EXPR; | |
4842 | break; | |
4843 | default: | |
4844 | /* Not a binary operator, so end of the binary | |
4845 | expression. */ | |
4846 | goto out; | |
4847 | } | |
ba47d38d | 4848 | binary_loc = c_parser_peek_token (parser)->location; |
27bf414c JM |
4849 | c_parser_consume_token (parser); |
4850 | while (oprec <= stack[sp].prec) | |
4851 | POP; | |
4852 | switch (ocode) | |
4853 | { | |
4854 | case TRUTH_ANDIF_EXPR: | |
f2a71bbc | 4855 | stack[sp].expr |
c2255bc4 AH |
4856 | = default_function_array_conversion (stack[sp].loc, |
4857 | stack[sp].expr); | |
85498824 | 4858 | stack[sp].expr.value = c_objc_common_truthvalue_conversion |
ca80e52b | 4859 | (stack[sp].loc, default_conversion (stack[sp].expr.value)); |
7d882b83 ILT |
4860 | c_inhibit_evaluation_warnings += (stack[sp].expr.value |
4861 | == truthvalue_false_node); | |
27bf414c JM |
4862 | break; |
4863 | case TRUTH_ORIF_EXPR: | |
f2a71bbc | 4864 | stack[sp].expr |
c2255bc4 AH |
4865 | = default_function_array_conversion (stack[sp].loc, |
4866 | stack[sp].expr); | |
85498824 | 4867 | stack[sp].expr.value = c_objc_common_truthvalue_conversion |
ca80e52b | 4868 | (stack[sp].loc, default_conversion (stack[sp].expr.value)); |
7d882b83 ILT |
4869 | c_inhibit_evaluation_warnings += (stack[sp].expr.value |
4870 | == truthvalue_true_node); | |
27bf414c JM |
4871 | break; |
4872 | default: | |
4873 | break; | |
4874 | } | |
4875 | sp++; | |
ca80e52b | 4876 | stack[sp].loc = binary_loc; |
27bf414c JM |
4877 | stack[sp].expr = c_parser_cast_expression (parser, NULL); |
4878 | stack[sp].prec = oprec; | |
4879 | stack[sp].op = ocode; | |
c2255bc4 | 4880 | stack[sp].loc = binary_loc; |
27bf414c JM |
4881 | } |
4882 | out: | |
4883 | while (sp > 0) | |
4884 | POP; | |
4885 | return stack[0].expr; | |
4886 | #undef POP | |
4887 | } | |
4888 | ||
4889 | /* Parse a cast expression (C90 6.3.4, C99 6.5.4). If AFTER is not | |
4890 | NULL then it is an Objective-C message expression which is the | |
4891 | primary-expression starting the expression as an initializer. | |
4892 | ||
4893 | cast-expression: | |
4894 | unary-expression | |
4895 | ( type-name ) unary-expression | |
4896 | */ | |
4897 | ||
4898 | static struct c_expr | |
4899 | c_parser_cast_expression (c_parser *parser, struct c_expr *after) | |
4900 | { | |
c2255bc4 | 4901 | location_t cast_loc = c_parser_peek_token (parser)->location; |
27bf414c JM |
4902 | gcc_assert (!after || c_dialect_objc ()); |
4903 | if (after) | |
c2255bc4 AH |
4904 | return c_parser_postfix_expression_after_primary (parser, |
4905 | cast_loc, *after); | |
27bf414c JM |
4906 | /* If the expression begins with a parenthesized type name, it may |
4907 | be either a cast or a compound literal; we need to see whether | |
4908 | the next character is '{' to tell the difference. If not, it is | |
4909 | an unary expression. */ | |
4910 | if (c_parser_next_token_is (parser, CPP_OPEN_PAREN) | |
4911 | && c_token_starts_typename (c_parser_peek_2nd_token (parser))) | |
4912 | { | |
4913 | struct c_type_name *type_name; | |
4914 | struct c_expr ret; | |
f2a71bbc | 4915 | struct c_expr expr; |
27bf414c JM |
4916 | c_parser_consume_token (parser); |
4917 | type_name = c_parser_type_name (parser); | |
4918 | c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>"); | |
4919 | if (type_name == NULL) | |
4920 | { | |
4921 | ret.value = error_mark_node; | |
4922 | ret.original_code = ERROR_MARK; | |
6866c6e8 | 4923 | ret.original_type = NULL; |
27bf414c JM |
4924 | return ret; |
4925 | } | |
33c9159e AH |
4926 | |
4927 | /* Save casted types in the function's used types hash table. */ | |
8d8d1a28 | 4928 | used_types_insert (type_name->specs->type); |
33c9159e | 4929 | |
27bf414c | 4930 | if (c_parser_next_token_is (parser, CPP_OPEN_BRACE)) |
24b97832 | 4931 | return c_parser_postfix_expression_after_paren_type (parser, type_name, |
c2255bc4 AH |
4932 | cast_loc); |
4933 | { | |
4934 | location_t expr_loc = c_parser_peek_token (parser)->location; | |
4935 | expr = c_parser_cast_expression (parser, NULL); | |
4936 | expr = default_function_array_conversion (expr_loc, expr); | |
4937 | } | |
4938 | ret.value = c_cast_expr (cast_loc, type_name, expr.value); | |
27bf414c | 4939 | ret.original_code = ERROR_MARK; |
6866c6e8 | 4940 | ret.original_type = NULL; |
27bf414c JM |
4941 | return ret; |
4942 | } | |
4943 | else | |
4944 | return c_parser_unary_expression (parser); | |
4945 | } | |
4946 | ||
4947 | /* Parse an unary expression (C90 6.3.3, C99 6.5.3). | |
4948 | ||
4949 | unary-expression: | |
4950 | postfix-expression | |
4951 | ++ unary-expression | |
4952 | -- unary-expression | |
4953 | unary-operator cast-expression | |
4954 | sizeof unary-expression | |
4955 | sizeof ( type-name ) | |
4956 | ||
4957 | unary-operator: one of | |
4958 | & * + - ~ ! | |
4959 | ||
4960 | GNU extensions: | |
4961 | ||
4962 | unary-expression: | |
4963 | __alignof__ unary-expression | |
4964 | __alignof__ ( type-name ) | |
4965 | && identifier | |
4966 | ||
4967 | unary-operator: one of | |
4968 | __extension__ __real__ __imag__ | |
4969 | ||
4970 | In addition, the GNU syntax treats ++ and -- as unary operators, so | |
4971 | they may be applied to cast expressions with errors for non-lvalues | |
4972 | given later. */ | |
4973 | ||
4974 | static struct c_expr | |
4975 | c_parser_unary_expression (c_parser *parser) | |
4976 | { | |
4977 | int ext; | |
46bdb9cf | 4978 | struct c_expr ret, op; |
c2255bc4 AH |
4979 | location_t op_loc = c_parser_peek_token (parser)->location; |
4980 | location_t exp_loc; | |
6866c6e8 ILT |
4981 | ret.original_code = ERROR_MARK; |
4982 | ret.original_type = NULL; | |
27bf414c JM |
4983 | switch (c_parser_peek_token (parser)->type) |
4984 | { | |
4985 | case CPP_PLUS_PLUS: | |
4986 | c_parser_consume_token (parser); | |
c2255bc4 | 4987 | exp_loc = c_parser_peek_token (parser)->location; |
46bdb9cf | 4988 | op = c_parser_cast_expression (parser, NULL); |
c2255bc4 AH |
4989 | op = default_function_array_conversion (exp_loc, op); |
4990 | return parser_build_unary_op (op_loc, PREINCREMENT_EXPR, op); | |
27bf414c JM |
4991 | case CPP_MINUS_MINUS: |
4992 | c_parser_consume_token (parser); | |
c2255bc4 | 4993 | exp_loc = c_parser_peek_token (parser)->location; |
46bdb9cf | 4994 | op = c_parser_cast_expression (parser, NULL); |
c2255bc4 AH |
4995 | op = default_function_array_conversion (exp_loc, op); |
4996 | return parser_build_unary_op (op_loc, PREDECREMENT_EXPR, op); | |
27bf414c JM |
4997 | case CPP_AND: |
4998 | c_parser_consume_token (parser); | |
c2255bc4 AH |
4999 | return parser_build_unary_op (op_loc, ADDR_EXPR, |
5000 | c_parser_cast_expression (parser, NULL)); | |
27bf414c JM |
5001 | case CPP_MULT: |
5002 | c_parser_consume_token (parser); | |
c2255bc4 | 5003 | exp_loc = c_parser_peek_token (parser)->location; |
46bdb9cf | 5004 | op = c_parser_cast_expression (parser, NULL); |
c2255bc4 AH |
5005 | op = default_function_array_conversion (exp_loc, op); |
5006 | ret.value = build_indirect_ref (op_loc, op.value, "unary *"); | |
27bf414c JM |
5007 | return ret; |
5008 | case CPP_PLUS: | |
44c21c7f | 5009 | if (!c_dialect_objc () && !in_system_header) |
c2255bc4 | 5010 | warning_at (op_loc, |
3ba09659 AH |
5011 | OPT_Wtraditional, |
5012 | "traditional C rejects the unary plus operator"); | |
c7412148 | 5013 | c_parser_consume_token (parser); |
c2255bc4 | 5014 | exp_loc = c_parser_peek_token (parser)->location; |
46bdb9cf | 5015 | op = c_parser_cast_expression (parser, NULL); |
c2255bc4 AH |
5016 | op = default_function_array_conversion (exp_loc, op); |
5017 | return parser_build_unary_op (op_loc, CONVERT_EXPR, op); | |
27bf414c JM |
5018 | case CPP_MINUS: |
5019 | c_parser_consume_token (parser); | |
c2255bc4 | 5020 | exp_loc = c_parser_peek_token (parser)->location; |
46bdb9cf | 5021 | op = c_parser_cast_expression (parser, NULL); |
c2255bc4 AH |
5022 | op = default_function_array_conversion (exp_loc, op); |
5023 | return parser_build_unary_op (op_loc, NEGATE_EXPR, op); | |
27bf414c JM |
5024 | case CPP_COMPL: |
5025 | c_parser_consume_token (parser); | |
c2255bc4 | 5026 | exp_loc = c_parser_peek_token (parser)->location; |
46bdb9cf | 5027 | op = c_parser_cast_expression (parser, NULL); |
c2255bc4 AH |
5028 | op = default_function_array_conversion (exp_loc, op); |
5029 | return parser_build_unary_op (op_loc, BIT_NOT_EXPR, op); | |
27bf414c JM |
5030 | case CPP_NOT: |
5031 | c_parser_consume_token (parser); | |
c2255bc4 | 5032 | exp_loc = c_parser_peek_token (parser)->location; |
46bdb9cf | 5033 | op = c_parser_cast_expression (parser, NULL); |
c2255bc4 AH |
5034 | op = default_function_array_conversion (exp_loc, op); |
5035 | return parser_build_unary_op (op_loc, TRUTH_NOT_EXPR, op); | |
27bf414c JM |
5036 | case CPP_AND_AND: |
5037 | /* Refer to the address of a label as a pointer. */ | |
5038 | c_parser_consume_token (parser); | |
5039 | if (c_parser_next_token_is (parser, CPP_NAME)) | |
5040 | { | |
5041 | ret.value = finish_label_address_expr | |
c2255bc4 | 5042 | (c_parser_peek_token (parser)->value, op_loc); |
27bf414c | 5043 | c_parser_consume_token (parser); |
27bf414c JM |
5044 | } |
5045 | else | |
5046 | { | |
5047 | c_parser_error (parser, "expected identifier"); | |
5048 | ret.value = error_mark_node; | |
27bf414c | 5049 | } |
43f6dfd3 | 5050 | return ret; |
27bf414c JM |
5051 | case CPP_KEYWORD: |
5052 | switch (c_parser_peek_token (parser)->keyword) | |
5053 | { | |
5054 | case RID_SIZEOF: | |
5055 | return c_parser_sizeof_expression (parser); | |
5056 | case RID_ALIGNOF: | |
5057 | return c_parser_alignof_expression (parser); | |
5058 | case RID_EXTENSION: | |
5059 | c_parser_consume_token (parser); | |
5060 | ext = disable_extension_diagnostics (); | |
5061 | ret = c_parser_cast_expression (parser, NULL); | |
5062 | restore_extension_diagnostics (ext); | |
5063 | return ret; | |
5064 | case RID_REALPART: | |
5065 | c_parser_consume_token (parser); | |
c2255bc4 | 5066 | exp_loc = c_parser_peek_token (parser)->location; |
46bdb9cf | 5067 | op = c_parser_cast_expression (parser, NULL); |
c2255bc4 AH |
5068 | op = default_function_array_conversion (exp_loc, op); |
5069 | return parser_build_unary_op (op_loc, REALPART_EXPR, op); | |
27bf414c JM |
5070 | case RID_IMAGPART: |
5071 | c_parser_consume_token (parser); | |
c2255bc4 | 5072 | exp_loc = c_parser_peek_token (parser)->location; |
46bdb9cf | 5073 | op = c_parser_cast_expression (parser, NULL); |
c2255bc4 AH |
5074 | op = default_function_array_conversion (exp_loc, op); |
5075 | return parser_build_unary_op (op_loc, IMAGPART_EXPR, op); | |
27bf414c JM |
5076 | default: |
5077 | return c_parser_postfix_expression (parser); | |
5078 | } | |
5079 | default: | |
5080 | return c_parser_postfix_expression (parser); | |
5081 | } | |
5082 | } | |
5083 | ||
5084 | /* Parse a sizeof expression. */ | |
5085 | ||
5086 | static struct c_expr | |
5087 | c_parser_sizeof_expression (c_parser *parser) | |
5088 | { | |
5089 | struct c_expr expr; | |
c7412148 | 5090 | location_t expr_loc; |
27bf414c JM |
5091 | gcc_assert (c_parser_next_token_is_keyword (parser, RID_SIZEOF)); |
5092 | c_parser_consume_token (parser); | |
7d882b83 | 5093 | c_inhibit_evaluation_warnings++; |
27bf414c JM |
5094 | in_sizeof++; |
5095 | if (c_parser_next_token_is (parser, CPP_OPEN_PAREN) | |
5096 | && c_token_starts_typename (c_parser_peek_2nd_token (parser))) | |
5097 | { | |
5098 | /* Either sizeof ( type-name ) or sizeof unary-expression | |
5099 | starting with a compound literal. */ | |
5100 | struct c_type_name *type_name; | |
5101 | c_parser_consume_token (parser); | |
c7412148 | 5102 | expr_loc = c_parser_peek_token (parser)->location; |
27bf414c JM |
5103 | type_name = c_parser_type_name (parser); |
5104 | c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>"); | |
5105 | if (type_name == NULL) | |
5106 | { | |
5107 | struct c_expr ret; | |
7d882b83 | 5108 | c_inhibit_evaluation_warnings--; |
27bf414c JM |
5109 | in_sizeof--; |
5110 | ret.value = error_mark_node; | |
5111 | ret.original_code = ERROR_MARK; | |
6866c6e8 | 5112 | ret.original_type = NULL; |
27bf414c JM |
5113 | return ret; |
5114 | } | |
5115 | if (c_parser_next_token_is (parser, CPP_OPEN_BRACE)) | |
5116 | { | |
5117 | expr = c_parser_postfix_expression_after_paren_type (parser, | |
24b97832 ILT |
5118 | type_name, |
5119 | expr_loc); | |
27bf414c JM |
5120 | goto sizeof_expr; |
5121 | } | |
5122 | /* sizeof ( type-name ). */ | |
7d882b83 | 5123 | c_inhibit_evaluation_warnings--; |
27bf414c | 5124 | in_sizeof--; |
c2255bc4 | 5125 | return c_expr_sizeof_type (expr_loc, type_name); |
27bf414c JM |
5126 | } |
5127 | else | |
5128 | { | |
c7412148 | 5129 | expr_loc = c_parser_peek_token (parser)->location; |
27bf414c JM |
5130 | expr = c_parser_unary_expression (parser); |
5131 | sizeof_expr: | |
7d882b83 | 5132 | c_inhibit_evaluation_warnings--; |
27bf414c JM |
5133 | in_sizeof--; |
5134 | if (TREE_CODE (expr.value) == COMPONENT_REF | |
5135 | && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1))) | |
3ba09659 | 5136 | error_at (expr_loc, "%<sizeof%> applied to a bit-field"); |
c2255bc4 | 5137 | return c_expr_sizeof_expr (expr_loc, expr); |
27bf414c JM |
5138 | } |
5139 | } | |
5140 | ||
5141 | /* Parse an alignof expression. */ | |
5142 | ||
5143 | static struct c_expr | |
5144 | c_parser_alignof_expression (c_parser *parser) | |
5145 | { | |
5146 | struct c_expr expr; | |
c2255bc4 | 5147 | location_t loc = c_parser_peek_token (parser)->location; |
27bf414c JM |
5148 | gcc_assert (c_parser_next_token_is_keyword (parser, RID_ALIGNOF)); |
5149 | c_parser_consume_token (parser); | |
7d882b83 | 5150 | c_inhibit_evaluation_warnings++; |
27bf414c JM |
5151 | in_alignof++; |
5152 | if (c_parser_next_token_is (parser, CPP_OPEN_PAREN) | |
5153 | && c_token_starts_typename (c_parser_peek_2nd_token (parser))) | |
5154 | { | |
5155 | /* Either __alignof__ ( type-name ) or __alignof__ | |
5156 | unary-expression starting with a compound literal. */ | |
24b97832 | 5157 | location_t loc; |
27bf414c JM |
5158 | struct c_type_name *type_name; |
5159 | struct c_expr ret; | |
5160 | c_parser_consume_token (parser); | |
24b97832 | 5161 | loc = c_parser_peek_token (parser)->location; |
27bf414c JM |
5162 | type_name = c_parser_type_name (parser); |
5163 | c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>"); | |
5164 | if (type_name == NULL) | |
5165 | { | |
5166 | struct c_expr ret; | |
7d882b83 | 5167 | c_inhibit_evaluation_warnings--; |
27bf414c JM |
5168 | in_alignof--; |
5169 | ret.value = error_mark_node; | |
5170 | ret.original_code = ERROR_MARK; | |
6866c6e8 | 5171 | ret.original_type = NULL; |
27bf414c JM |
5172 | return ret; |
5173 | } | |
5174 | if (c_parser_next_token_is (parser, CPP_OPEN_BRACE)) | |
5175 | { | |
5176 | expr = c_parser_postfix_expression_after_paren_type (parser, | |
24b97832 ILT |
5177 | type_name, |
5178 | loc); | |
27bf414c JM |
5179 | goto alignof_expr; |
5180 | } | |
5181 | /* alignof ( type-name ). */ | |
7d882b83 | 5182 | c_inhibit_evaluation_warnings--; |
27bf414c | 5183 | in_alignof--; |
c2255bc4 | 5184 | ret.value = c_alignof (loc, groktypename (type_name, NULL, NULL)); |
27bf414c | 5185 | ret.original_code = ERROR_MARK; |
6866c6e8 | 5186 | ret.original_type = NULL; |
27bf414c JM |
5187 | return ret; |
5188 | } | |
5189 | else | |
5190 | { | |
5191 | struct c_expr ret; | |
5192 | expr = c_parser_unary_expression (parser); | |
5193 | alignof_expr: | |
7d882b83 | 5194 | c_inhibit_evaluation_warnings--; |
27bf414c | 5195 | in_alignof--; |
c2255bc4 | 5196 | ret.value = c_alignof_expr (loc, expr.value); |
27bf414c | 5197 | ret.original_code = ERROR_MARK; |
6866c6e8 | 5198 | ret.original_type = NULL; |
27bf414c JM |
5199 | return ret; |
5200 | } | |
5201 | } | |
5202 | ||
5203 | /* Parse a postfix expression (C90 6.3.1-6.3.2, C99 6.5.1-6.5.2). | |
5204 | ||
5205 | postfix-expression: | |
5206 | primary-expression | |
5207 | postfix-expression [ expression ] | |
5208 | postfix-expression ( argument-expression-list[opt] ) | |
5209 | postfix-expression . identifier | |
5210 | postfix-expression -> identifier | |
5211 | postfix-expression ++ | |
5212 | postfix-expression -- | |
5213 | ( type-name ) { initializer-list } | |
5214 | ( type-name ) { initializer-list , } | |
5215 | ||
5216 | argument-expression-list: | |
5217 | argument-expression | |
5218 | argument-expression-list , argument-expression | |
5219 | ||
5220 | primary-expression: | |
5221 | identifier | |
5222 | constant | |
5223 | string-literal | |
5224 | ( expression ) | |
5225 | ||
5226 | GNU extensions: | |
5227 | ||
5228 | primary-expression: | |
5229 | __func__ | |
5230 | (treated as a keyword in GNU C) | |
5231 | __FUNCTION__ | |
5232 | __PRETTY_FUNCTION__ | |
5233 | ( compound-statement ) | |
5234 | __builtin_va_arg ( assignment-expression , type-name ) | |
5235 | __builtin_offsetof ( type-name , offsetof-member-designator ) | |
5236 | __builtin_choose_expr ( assignment-expression , | |
5237 | assignment-expression , | |
5238 | assignment-expression ) | |
5239 | __builtin_types_compatible_p ( type-name , type-name ) | |
5240 | ||
5241 | offsetof-member-designator: | |
5242 | identifier | |
5243 | offsetof-member-designator . identifier | |
5244 | offsetof-member-designator [ expression ] | |
5245 | ||
5246 | Objective-C: | |
5247 | ||
5248 | primary-expression: | |
5249 | [ objc-receiver objc-message-args ] | |
5250 | @selector ( objc-selector-arg ) | |
5251 | @protocol ( identifier ) | |
5252 | @encode ( type-name ) | |
5253 | objc-string-literal | |
5254 | */ | |
5255 | ||
5256 | static struct c_expr | |
5257 | c_parser_postfix_expression (c_parser *parser) | |
5258 | { | |
5259 | struct c_expr expr, e1, e2, e3; | |
5260 | struct c_type_name *t1, *t2; | |
c2255bc4 | 5261 | location_t loc = c_parser_peek_token (parser)->location;; |
6866c6e8 ILT |
5262 | expr.original_code = ERROR_MARK; |
5263 | expr.original_type = NULL; | |
27bf414c JM |
5264 | switch (c_parser_peek_token (parser)->type) |
5265 | { | |
5266 | case CPP_NUMBER: | |
754ccf7c | 5267 | expr.value = c_parser_peek_token (parser)->value; |
754ccf7c JJ |
5268 | loc = c_parser_peek_token (parser)->location; |
5269 | c_parser_consume_token (parser); | |
5270 | if (TREE_CODE (expr.value) == FIXED_CST | |
5271 | && !targetm.fixed_point_supported_p ()) | |
5272 | { | |
5273 | error_at (loc, "fixed-point types not supported for this target"); | |
5274 | expr.value = error_mark_node; | |
5275 | } | |
5276 | break; | |
27bf414c | 5277 | case CPP_CHAR: |
b6baa67d KVH |
5278 | case CPP_CHAR16: |
5279 | case CPP_CHAR32: | |
27bf414c JM |
5280 | case CPP_WCHAR: |
5281 | expr.value = c_parser_peek_token (parser)->value; | |
27bf414c JM |
5282 | c_parser_consume_token (parser); |
5283 | break; | |
5284 | case CPP_STRING: | |
b6baa67d KVH |
5285 | case CPP_STRING16: |
5286 | case CPP_STRING32: | |
27bf414c JM |
5287 | case CPP_WSTRING: |
5288 | expr.value = c_parser_peek_token (parser)->value; | |
5289 | expr.original_code = STRING_CST; | |
5290 | c_parser_consume_token (parser); | |
5291 | break; | |
5292 | case CPP_OBJC_STRING: | |
5293 | gcc_assert (c_dialect_objc ()); | |
5294 | expr.value | |
5295 | = objc_build_string_object (c_parser_peek_token (parser)->value); | |
27bf414c JM |
5296 | c_parser_consume_token (parser); |
5297 | break; | |
5298 | case CPP_NAME: | |
5299 | if (c_parser_peek_token (parser)->id_kind != C_ID_ID) | |
5300 | { | |
5301 | c_parser_error (parser, "expected expression"); | |
5302 | expr.value = error_mark_node; | |
27bf414c JM |
5303 | break; |
5304 | } | |
5305 | { | |
5306 | tree id = c_parser_peek_token (parser)->value; | |
5307 | c_parser_consume_token (parser); | |
c2255bc4 | 5308 | expr.value = build_external_ref (loc, id, |
27bf414c | 5309 | (c_parser_peek_token (parser)->type |
c2255bc4 | 5310 | == CPP_OPEN_PAREN), |
6866c6e8 | 5311 | &expr.original_type); |
27bf414c JM |
5312 | } |
5313 | break; | |
5314 | case CPP_OPEN_PAREN: | |
5315 | /* A parenthesized expression, statement expression or compound | |
5316 | literal. */ | |
5317 | if (c_parser_peek_2nd_token (parser)->type == CPP_OPEN_BRACE) | |
5318 | { | |
5319 | /* A statement expression. */ | |
5320 | tree stmt; | |
c2255bc4 | 5321 | location_t brace_loc; |
27bf414c | 5322 | c_parser_consume_token (parser); |
c2255bc4 | 5323 | brace_loc = c_parser_peek_token (parser)->location; |
27bf414c JM |
5324 | c_parser_consume_token (parser); |
5325 | if (cur_stmt_list == NULL) | |
5326 | { | |
c2255bc4 | 5327 | error_at (loc, "braced-group within expression allowed " |
3ba09659 | 5328 | "only inside a function"); |
27bf414c JM |
5329 | parser->error = true; |
5330 | c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL); | |
5331 | c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL); | |
5332 | expr.value = error_mark_node; | |
27bf414c JM |
5333 | break; |
5334 | } | |
5335 | stmt = c_begin_stmt_expr (); | |
5336 | c_parser_compound_statement_nostart (parser); | |
5337 | c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, | |
5338 | "expected %<)%>"); | |
c2255bc4 | 5339 | pedwarn (loc, OPT_pedantic, |
509c9d60 | 5340 | "ISO C forbids braced-groups within expressions"); |
c2255bc4 | 5341 | expr.value = c_finish_stmt_expr (brace_loc, stmt); |
27bf414c JM |
5342 | } |
5343 | else if (c_token_starts_typename (c_parser_peek_2nd_token (parser))) | |
5344 | { | |
5345 | /* A compound literal. ??? Can we actually get here rather | |
5346 | than going directly to | |
5347 | c_parser_postfix_expression_after_paren_type from | |
5348 | elsewhere? */ | |
24b97832 | 5349 | location_t loc; |
27bf414c JM |
5350 | struct c_type_name *type_name; |
5351 | c_parser_consume_token (parser); | |
24b97832 | 5352 | loc = c_parser_peek_token (parser)->location; |
27bf414c JM |
5353 | type_name = c_parser_type_name (parser); |
5354 | c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, | |
5355 | "expected %<)%>"); | |
5356 | if (type_name == NULL) | |
5357 | { | |
5358 | expr.value = error_mark_node; | |
27bf414c JM |
5359 | } |
5360 | else | |
5361 | expr = c_parser_postfix_expression_after_paren_type (parser, | |
24b97832 ILT |
5362 | type_name, |
5363 | loc); | |
27bf414c JM |
5364 | } |
5365 | else | |
5366 | { | |
5367 | /* A parenthesized expression. */ | |
5368 | c_parser_consume_token (parser); | |
5369 | expr = c_parser_expression (parser); | |
5370 | if (TREE_CODE (expr.value) == MODIFY_EXPR) | |
5371 | TREE_NO_WARNING (expr.value) = 1; | |
928c19bb JM |
5372 | if (expr.original_code != C_MAYBE_CONST_EXPR) |
5373 | expr.original_code = ERROR_MARK; | |
6866c6e8 | 5374 | /* Don't change EXPR.ORIGINAL_TYPE. */ |
27bf414c JM |
5375 | c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, |
5376 | "expected %<)%>"); | |
5377 | } | |
5378 | break; | |
5379 | case CPP_KEYWORD: | |
5380 | switch (c_parser_peek_token (parser)->keyword) | |
5381 | { | |
5382 | case RID_FUNCTION_NAME: | |
5383 | case RID_PRETTY_FUNCTION_NAME: | |
5384 | case RID_C99_FUNCTION_NAME: | |
c2255bc4 | 5385 | expr.value = fname_decl (loc, |
3ba09659 | 5386 | c_parser_peek_token (parser)->keyword, |
27bf414c | 5387 | c_parser_peek_token (parser)->value); |
27bf414c JM |
5388 | c_parser_consume_token (parser); |
5389 | break; | |
5390 | case RID_VA_ARG: | |
5391 | c_parser_consume_token (parser); | |
5392 | if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>")) | |
5393 | { | |
5394 | expr.value = error_mark_node; | |
27bf414c JM |
5395 | break; |
5396 | } | |
5397 | e1 = c_parser_expr_no_commas (parser, NULL); | |
928c19bb | 5398 | e1.value = c_fully_fold (e1.value, false, NULL); |
27bf414c JM |
5399 | if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>")) |
5400 | { | |
5401 | c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL); | |
5402 | expr.value = error_mark_node; | |
27bf414c JM |
5403 | break; |
5404 | } | |
72b5577d | 5405 | loc = c_parser_peek_token (parser)->location; |
27bf414c JM |
5406 | t1 = c_parser_type_name (parser); |
5407 | c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, | |
5408 | "expected %<)%>"); | |
5409 | if (t1 == NULL) | |
5410 | { | |
5411 | expr.value = error_mark_node; | |
27bf414c JM |
5412 | } |
5413 | else | |
5414 | { | |
928c19bb | 5415 | tree type_expr = NULL_TREE; |
c2255bc4 AH |
5416 | expr.value = c_build_va_arg (loc, e1.value, |
5417 | groktypename (t1, &type_expr, NULL)); | |
928c19bb JM |
5418 | if (type_expr) |
5419 | { | |
5420 | expr.value = build2 (C_MAYBE_CONST_EXPR, | |
5421 | TREE_TYPE (expr.value), type_expr, | |
5422 | expr.value); | |
5423 | C_MAYBE_CONST_EXPR_NON_CONST (expr.value) = true; | |
5424 | } | |
27bf414c JM |
5425 | } |
5426 | break; | |
5427 | case RID_OFFSETOF: | |
5428 | c_parser_consume_token (parser); | |
5429 | if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>")) | |
5430 | { | |
5431 | expr.value = error_mark_node; | |
27bf414c JM |
5432 | break; |
5433 | } | |
5434 | t1 = c_parser_type_name (parser); | |
5435 | if (t1 == NULL) | |
5436 | { | |
5437 | expr.value = error_mark_node; | |
27bf414c JM |
5438 | break; |
5439 | } | |
5440 | if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>")) | |
5441 | { | |
5442 | c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL); | |
5443 | expr.value = error_mark_node; | |
27bf414c JM |
5444 | break; |
5445 | } | |
5446 | { | |
928c19bb | 5447 | tree type = groktypename (t1, NULL, NULL); |
27bf414c JM |
5448 | tree offsetof_ref; |
5449 | if (type == error_mark_node) | |
5450 | offsetof_ref = error_mark_node; | |
5451 | else | |
c2255bc4 AH |
5452 | { |
5453 | offsetof_ref = build1 (INDIRECT_REF, type, null_pointer_node); | |
5454 | SET_EXPR_LOCATION (offsetof_ref, loc); | |
5455 | } | |
27bf414c JM |
5456 | /* Parse the second argument to __builtin_offsetof. We |
5457 | must have one identifier, and beyond that we want to | |
5458 | accept sub structure and sub array references. */ | |
5459 | if (c_parser_next_token_is (parser, CPP_NAME)) | |
5460 | { | |
5461 | offsetof_ref = build_component_ref | |
c2255bc4 | 5462 | (loc, offsetof_ref, c_parser_peek_token (parser)->value); |
27bf414c JM |
5463 | c_parser_consume_token (parser); |
5464 | while (c_parser_next_token_is (parser, CPP_DOT) | |
5465 | || c_parser_next_token_is (parser, | |
634b5df5 JJ |
5466 | CPP_OPEN_SQUARE) |
5467 | || c_parser_next_token_is (parser, | |
5468 | CPP_DEREF)) | |
27bf414c | 5469 | { |
634b5df5 JJ |
5470 | if (c_parser_next_token_is (parser, CPP_DEREF)) |
5471 | { | |
5472 | loc = c_parser_peek_token (parser)->location; | |
c2255bc4 AH |
5473 | offsetof_ref = build_array_ref (loc, |
5474 | offsetof_ref, | |
5475 | integer_zero_node); | |
634b5df5 JJ |
5476 | goto do_dot; |
5477 | } | |
5478 | else if (c_parser_next_token_is (parser, CPP_DOT)) | |
27bf414c | 5479 | { |
634b5df5 | 5480 | do_dot: |
27bf414c JM |
5481 | c_parser_consume_token (parser); |
5482 | if (c_parser_next_token_is_not (parser, | |
5483 | CPP_NAME)) | |
5484 | { | |
5485 | c_parser_error (parser, "expected identifier"); | |
5486 | break; | |
5487 | } | |
5488 | offsetof_ref = build_component_ref | |
c2255bc4 | 5489 | (loc, offsetof_ref, |
27bf414c JM |
5490 | c_parser_peek_token (parser)->value); |
5491 | c_parser_consume_token (parser); | |
5492 | } | |
5493 | else | |
5494 | { | |
5495 | tree idx; | |
6a3799eb | 5496 | loc = c_parser_peek_token (parser)->location; |
27bf414c JM |
5497 | c_parser_consume_token (parser); |
5498 | idx = c_parser_expression (parser).value; | |
928c19bb | 5499 | idx = c_fully_fold (idx, false, NULL); |
27bf414c JM |
5500 | c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, |
5501 | "expected %<]%>"); | |
c2255bc4 | 5502 | offsetof_ref = build_array_ref (loc, offsetof_ref, idx); |
27bf414c JM |
5503 | } |
5504 | } | |
5505 | } | |
5506 | else | |
5507 | c_parser_error (parser, "expected identifier"); | |
5508 | c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, | |
5509 | "expected %<)%>"); | |
6d4d7b0e | 5510 | expr.value = fold_offsetof (offsetof_ref, NULL_TREE); |
27bf414c JM |
5511 | } |
5512 | break; | |
5513 | case RID_CHOOSE_EXPR: | |
5514 | c_parser_consume_token (parser); | |
5515 | if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>")) | |
5516 | { | |
5517 | expr.value = error_mark_node; | |
27bf414c JM |
5518 | break; |
5519 | } | |
c7412148 | 5520 | loc = c_parser_peek_token (parser)->location; |
27bf414c JM |
5521 | e1 = c_parser_expr_no_commas (parser, NULL); |
5522 | if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>")) | |
5523 | { | |
5524 | c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL); | |
5525 | expr.value = error_mark_node; | |
27bf414c JM |
5526 | break; |
5527 | } | |
5528 | e2 = c_parser_expr_no_commas (parser, NULL); | |
5529 | if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>")) | |
5530 | { | |
5531 | c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL); | |
5532 | expr.value = error_mark_node; | |
27bf414c JM |
5533 | break; |
5534 | } | |
5535 | e3 = c_parser_expr_no_commas (parser, NULL); | |
5536 | c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, | |
5537 | "expected %<)%>"); | |
5538 | { | |
5539 | tree c; | |
5540 | ||
4d84fe7c | 5541 | c = e1.value; |
928c19bb JM |
5542 | if (TREE_CODE (c) != INTEGER_CST |
5543 | || !INTEGRAL_TYPE_P (TREE_TYPE (c))) | |
3ba09659 AH |
5544 | error_at (loc, |
5545 | "first argument to %<__builtin_choose_expr%> not" | |
5546 | " a constant"); | |
928c19bb | 5547 | constant_expression_warning (c); |
27bf414c JM |
5548 | expr = integer_zerop (c) ? e3 : e2; |
5549 | } | |
5550 | break; | |
5551 | case RID_TYPES_COMPATIBLE_P: | |
5552 | c_parser_consume_token (parser); | |
5553 | if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>")) | |
5554 | { | |
5555 | expr.value = error_mark_node; | |
27bf414c JM |
5556 | break; |
5557 | } | |
5558 | t1 = c_parser_type_name (parser); | |
5559 | if (t1 == NULL) | |
5560 | { | |
5561 | expr.value = error_mark_node; | |
27bf414c JM |
5562 | break; |
5563 | } | |
5564 | if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>")) | |
5565 | { | |
5566 | c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL); | |
5567 | expr.value = error_mark_node; | |
27bf414c JM |
5568 | break; |
5569 | } | |
5570 | t2 = c_parser_type_name (parser); | |
5571 | if (t2 == NULL) | |
5572 | { | |
5573 | expr.value = error_mark_node; | |
27bf414c JM |
5574 | break; |
5575 | } | |
5576 | c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, | |
5577 | "expected %<)%>"); | |
5578 | { | |
5579 | tree e1, e2; | |
5580 | ||
928c19bb JM |
5581 | e1 = TYPE_MAIN_VARIANT (groktypename (t1, NULL, NULL)); |
5582 | e2 = TYPE_MAIN_VARIANT (groktypename (t2, NULL, NULL)); | |
27bf414c JM |
5583 | |
5584 | expr.value = comptypes (e1, e2) | |
5585 | ? build_int_cst (NULL_TREE, 1) | |
5586 | : build_int_cst (NULL_TREE, 0); | |
27bf414c JM |
5587 | } |
5588 | break; | |
5589 | case RID_AT_SELECTOR: | |
5590 | gcc_assert (c_dialect_objc ()); | |
5591 | c_parser_consume_token (parser); | |
5592 | if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>")) | |
5593 | { | |
5594 | expr.value = error_mark_node; | |
27bf414c JM |
5595 | break; |
5596 | } | |
5597 | { | |
5598 | tree sel = c_parser_objc_selector_arg (parser); | |
5599 | c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, | |
5600 | "expected %<)%>"); | |
c2255bc4 | 5601 | expr.value = objc_build_selector_expr (loc, sel); |
27bf414c JM |
5602 | } |
5603 | break; | |
5604 | case RID_AT_PROTOCOL: | |
5605 | gcc_assert (c_dialect_objc ()); | |
5606 | c_parser_consume_token (parser); | |
5607 | if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>")) | |
5608 | { | |
5609 | expr.value = error_mark_node; | |
27bf414c JM |
5610 | break; |
5611 | } | |
5612 | if (c_parser_next_token_is_not (parser, CPP_NAME)) | |
5613 | { | |
5614 | c_parser_error (parser, "expected identifier"); | |
5615 | c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL); | |
5616 | expr.value = error_mark_node; | |
27bf414c JM |
5617 | break; |
5618 | } | |
5619 | { | |
5620 | tree id = c_parser_peek_token (parser)->value; | |
5621 | c_parser_consume_token (parser); | |
5622 | c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, | |
5623 | "expected %<)%>"); | |
5624 | expr.value = objc_build_protocol_expr (id); | |
27bf414c JM |
5625 | } |
5626 | break; | |
5627 | case RID_AT_ENCODE: | |
5628 | /* Extension to support C-structures in the archiver. */ | |
5629 | gcc_assert (c_dialect_objc ()); | |
5630 | c_parser_consume_token (parser); | |
5631 | if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>")) | |
5632 | { | |
5633 | expr.value = error_mark_node; | |
27bf414c JM |
5634 | break; |
5635 | } | |
5636 | t1 = c_parser_type_name (parser); | |
5637 | if (t1 == NULL) | |
5638 | { | |
5639 | expr.value = error_mark_node; | |
27bf414c JM |
5640 | c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL); |
5641 | break; | |
5642 | } | |
5643 | c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, | |
5644 | "expected %<)%>"); | |
5645 | { | |
928c19bb | 5646 | tree type = groktypename (t1, NULL, NULL); |
27bf414c | 5647 | expr.value = objc_build_encode_expr (type); |
27bf414c JM |
5648 | } |
5649 | break; | |
5650 | default: | |
5651 | c_parser_error (parser, "expected expression"); | |
5652 | expr.value = error_mark_node; | |
27bf414c JM |
5653 | break; |
5654 | } | |
5655 | break; | |
5656 | case CPP_OPEN_SQUARE: | |
5657 | if (c_dialect_objc ()) | |
5658 | { | |
5659 | tree receiver, args; | |
5660 | c_parser_consume_token (parser); | |
5661 | receiver = c_parser_objc_receiver (parser); | |
5662 | args = c_parser_objc_message_args (parser); | |
5663 | c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, | |
5664 | "expected %<]%>"); | |
5665 | expr.value = objc_build_message_expr (build_tree_list (receiver, | |
5666 | args)); | |
27bf414c JM |
5667 | break; |
5668 | } | |
5669 | /* Else fall through to report error. */ | |
5670 | default: | |
5671 | c_parser_error (parser, "expected expression"); | |
5672 | expr.value = error_mark_node; | |
27bf414c JM |
5673 | break; |
5674 | } | |
c2255bc4 | 5675 | return c_parser_postfix_expression_after_primary (parser, loc, expr); |
27bf414c JM |
5676 | } |
5677 | ||
5678 | /* Parse a postfix expression after a parenthesized type name: the | |
5679 | brace-enclosed initializer of a compound literal, possibly followed | |
5680 | by some postfix operators. This is separate because it is not | |
5681 | possible to tell until after the type name whether a cast | |
5682 | expression has a cast or a compound literal, or whether the operand | |
5683 | of sizeof is a parenthesized type name or starts with a compound | |
24b97832 ILT |
5684 | literal. TYPE_LOC is the location where TYPE_NAME starts--the |
5685 | location of the first token after the parentheses around the type | |
5686 | name. */ | |
27bf414c JM |
5687 | |
5688 | static struct c_expr | |
5689 | c_parser_postfix_expression_after_paren_type (c_parser *parser, | |
24b97832 ILT |
5690 | struct c_type_name *type_name, |
5691 | location_t type_loc) | |
27bf414c JM |
5692 | { |
5693 | tree type; | |
5694 | struct c_expr init; | |
928c19bb | 5695 | bool non_const; |
27bf414c | 5696 | struct c_expr expr; |
c7412148 | 5697 | location_t start_loc; |
928c19bb JM |
5698 | tree type_expr = NULL_TREE; |
5699 | bool type_expr_const = true; | |
c2255bc4 | 5700 | check_compound_literal_type (type_loc, type_name); |
27bf414c | 5701 | start_init (NULL_TREE, NULL, 0); |
928c19bb | 5702 | type = groktypename (type_name, &type_expr, &type_expr_const); |
c7412148 | 5703 | start_loc = c_parser_peek_token (parser)->location; |
85cad37c | 5704 | if (type != error_mark_node && C_TYPE_VARIABLE_SIZE (type)) |
27bf414c | 5705 | { |
24b97832 | 5706 | error_at (type_loc, "compound literal has variable size"); |
27bf414c JM |
5707 | type = error_mark_node; |
5708 | } | |
5709 | init = c_parser_braced_init (parser, type, false); | |
5710 | finish_init (); | |
5711 | maybe_warn_string_init (type, init); | |
5712 | ||
fcf73884 | 5713 | if (!flag_isoc99) |
509c9d60 | 5714 | pedwarn (start_loc, OPT_pedantic, "ISO C90 forbids compound literals"); |
928c19bb JM |
5715 | non_const = ((init.value && TREE_CODE (init.value) == CONSTRUCTOR) |
5716 | ? CONSTRUCTOR_NON_CONST (init.value) | |
5717 | : init.original_code == C_MAYBE_CONST_EXPR); | |
5718 | non_const |= !type_expr_const; | |
c2255bc4 | 5719 | expr.value = build_compound_literal (start_loc, type, init.value, non_const); |
27bf414c | 5720 | expr.original_code = ERROR_MARK; |
6866c6e8 | 5721 | expr.original_type = NULL; |
928c19bb JM |
5722 | if (type_expr) |
5723 | { | |
5724 | if (TREE_CODE (expr.value) == C_MAYBE_CONST_EXPR) | |
5725 | { | |
5726 | gcc_assert (C_MAYBE_CONST_EXPR_PRE (expr.value) == NULL_TREE); | |
5727 | C_MAYBE_CONST_EXPR_PRE (expr.value) = type_expr; | |
5728 | } | |
5729 | else | |
5730 | { | |
5731 | gcc_assert (!non_const); | |
5732 | expr.value = build2 (C_MAYBE_CONST_EXPR, type, | |
5733 | type_expr, expr.value); | |
5734 | } | |
5735 | } | |
c2255bc4 | 5736 | return c_parser_postfix_expression_after_primary (parser, start_loc, expr); |
27bf414c JM |
5737 | } |
5738 | ||
5739 | /* Parse a postfix expression after the initial primary or compound | |
c2255bc4 AH |
5740 | literal; that is, parse a series of postfix operators. |
5741 | ||
5742 | EXPR_LOC is the location of the primary expression. */ | |
27bf414c JM |
5743 | |
5744 | static struct c_expr | |
5745 | c_parser_postfix_expression_after_primary (c_parser *parser, | |
c2255bc4 | 5746 | location_t expr_loc, |
27bf414c JM |
5747 | struct c_expr expr) |
5748 | { | |
928c19bb | 5749 | struct c_expr orig_expr; |
bbbbb16a ILT |
5750 | tree ident, idx; |
5751 | VEC(tree,gc) *exprlist; | |
5752 | VEC(tree,gc) *origtypes; | |
27bf414c JM |
5753 | while (true) |
5754 | { | |
c2255bc4 | 5755 | location_t op_loc = c_parser_peek_token (parser)->location; |
27bf414c JM |
5756 | switch (c_parser_peek_token (parser)->type) |
5757 | { | |
5758 | case CPP_OPEN_SQUARE: | |
5759 | /* Array reference. */ | |
5760 | c_parser_consume_token (parser); | |
5761 | idx = c_parser_expression (parser).value; | |
5762 | c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, | |
5763 | "expected %<]%>"); | |
c2255bc4 | 5764 | expr.value = build_array_ref (op_loc, expr.value, idx); |
27bf414c | 5765 | expr.original_code = ERROR_MARK; |
6866c6e8 | 5766 | expr.original_type = NULL; |
27bf414c JM |
5767 | break; |
5768 | case CPP_OPEN_PAREN: | |
5769 | /* Function call. */ | |
5770 | c_parser_consume_token (parser); | |
5771 | if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN)) | |
bbbbb16a | 5772 | exprlist = NULL; |
27bf414c | 5773 | else |
bbbbb16a | 5774 | exprlist = c_parser_expr_list (parser, true, false, &origtypes); |
27bf414c JM |
5775 | c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, |
5776 | "expected %<)%>"); | |
928c19bb | 5777 | orig_expr = expr; |
c2255bc4 AH |
5778 | /* FIXME diagnostics: Ideally we want the FUNCNAME, not the |
5779 | "(" after the FUNCNAME, which is what we have now. */ | |
5780 | expr.value = build_function_call_vec (op_loc, expr.value, exprlist, | |
bbbbb16a | 5781 | origtypes); |
27bf414c | 5782 | expr.original_code = ERROR_MARK; |
928c19bb JM |
5783 | if (TREE_CODE (expr.value) == INTEGER_CST |
5784 | && TREE_CODE (orig_expr.value) == FUNCTION_DECL | |
5785 | && DECL_BUILT_IN_CLASS (orig_expr.value) == BUILT_IN_NORMAL | |
5786 | && DECL_FUNCTION_CODE (orig_expr.value) == BUILT_IN_CONSTANT_P) | |
5787 | expr.original_code = C_MAYBE_CONST_EXPR; | |
6866c6e8 | 5788 | expr.original_type = NULL; |
bbbbb16a ILT |
5789 | if (exprlist != NULL) |
5790 | { | |
c166b898 ILT |
5791 | release_tree_vector (exprlist); |
5792 | release_tree_vector (origtypes); | |
bbbbb16a | 5793 | } |
27bf414c JM |
5794 | break; |
5795 | case CPP_DOT: | |
5796 | /* Structure element reference. */ | |
5797 | c_parser_consume_token (parser); | |
c2255bc4 | 5798 | expr = default_function_array_conversion (expr_loc, expr); |
27bf414c JM |
5799 | if (c_parser_next_token_is (parser, CPP_NAME)) |
5800 | ident = c_parser_peek_token (parser)->value; | |
5801 | else | |
5802 | { | |
5803 | c_parser_error (parser, "expected identifier"); | |
5804 | expr.value = error_mark_node; | |
5805 | expr.original_code = ERROR_MARK; | |
6866c6e8 | 5806 | expr.original_type = NULL; |
27bf414c JM |
5807 | return expr; |
5808 | } | |
5809 | c_parser_consume_token (parser); | |
c2255bc4 | 5810 | expr.value = build_component_ref (op_loc, expr.value, ident); |
27bf414c | 5811 | expr.original_code = ERROR_MARK; |
6866c6e8 ILT |
5812 | if (TREE_CODE (expr.value) != COMPONENT_REF) |
5813 | expr.original_type = NULL; | |
5814 | else | |
5815 | { | |
5816 | /* Remember the original type of a bitfield. */ | |
5817 | tree field = TREE_OPERAND (expr.value, 1); | |
5818 | if (TREE_CODE (field) != FIELD_DECL) | |
5819 | expr.original_type = NULL; | |
5820 | else | |
5821 | expr.original_type = DECL_BIT_FIELD_TYPE (field); | |
5822 | } | |
27bf414c JM |
5823 | break; |
5824 | case CPP_DEREF: | |
5825 | /* Structure element reference. */ | |
5826 | c_parser_consume_token (parser); | |
c2255bc4 | 5827 | expr = default_function_array_conversion (expr_loc, expr); |
27bf414c JM |
5828 | if (c_parser_next_token_is (parser, CPP_NAME)) |
5829 | ident = c_parser_peek_token (parser)->value; | |
5830 | else | |
5831 | { | |
5832 | c_parser_error (parser, "expected identifier"); | |
5833 | expr.value = error_mark_node; | |
5834 | expr.original_code = ERROR_MARK; | |
6866c6e8 | 5835 | expr.original_type = NULL; |
27bf414c JM |
5836 | return expr; |
5837 | } | |
5838 | c_parser_consume_token (parser); | |
c2255bc4 AH |
5839 | expr.value = build_component_ref (op_loc, |
5840 | build_indirect_ref (op_loc, | |
c9f9eb5d AH |
5841 | expr.value, |
5842 | "->"), | |
6a3799eb | 5843 | ident); |
27bf414c | 5844 | expr.original_code = ERROR_MARK; |
6866c6e8 ILT |
5845 | if (TREE_CODE (expr.value) != COMPONENT_REF) |
5846 | expr.original_type = NULL; | |
5847 | else | |
5848 | { | |
5849 | /* Remember the original type of a bitfield. */ | |
5850 | tree field = TREE_OPERAND (expr.value, 1); | |
5851 | if (TREE_CODE (field) != FIELD_DECL) | |
5852 | expr.original_type = NULL; | |
5853 | else | |
5854 | expr.original_type = DECL_BIT_FIELD_TYPE (field); | |
5855 | } | |
27bf414c JM |
5856 | break; |
5857 | case CPP_PLUS_PLUS: | |
5858 | /* Postincrement. */ | |
5859 | c_parser_consume_token (parser); | |
c2255bc4 AH |
5860 | expr = default_function_array_conversion (expr_loc, expr); |
5861 | expr.value = build_unary_op (op_loc, | |
c9f9eb5d | 5862 | POSTINCREMENT_EXPR, expr.value, 0); |
27bf414c | 5863 | expr.original_code = ERROR_MARK; |
6866c6e8 | 5864 | expr.original_type = NULL; |
27bf414c JM |
5865 | break; |
5866 | case CPP_MINUS_MINUS: | |
5867 | /* Postdecrement. */ | |
5868 | c_parser_consume_token (parser); | |
c2255bc4 AH |
5869 | expr = default_function_array_conversion (expr_loc, expr); |
5870 | expr.value = build_unary_op (op_loc, | |
c9f9eb5d | 5871 | POSTDECREMENT_EXPR, expr.value, 0); |
27bf414c | 5872 | expr.original_code = ERROR_MARK; |
6866c6e8 | 5873 | expr.original_type = NULL; |
27bf414c JM |
5874 | break; |
5875 | default: | |
5876 | return expr; | |
5877 | } | |
5878 | } | |
5879 | } | |
5880 | ||
5881 | /* Parse an expression (C90 6.3.17, C99 6.5.17). | |
5882 | ||
5883 | expression: | |
5884 | assignment-expression | |
5885 | expression , assignment-expression | |
5886 | */ | |
5887 | ||
5888 | static struct c_expr | |
5889 | c_parser_expression (c_parser *parser) | |
5890 | { | |
5891 | struct c_expr expr; | |
5892 | expr = c_parser_expr_no_commas (parser, NULL); | |
5893 | while (c_parser_next_token_is (parser, CPP_COMMA)) | |
5894 | { | |
5895 | struct c_expr next; | |
c2255bc4 AH |
5896 | location_t loc = c_parser_peek_token (parser)->location; |
5897 | location_t expr_loc; | |
27bf414c | 5898 | c_parser_consume_token (parser); |
c2255bc4 | 5899 | expr_loc = c_parser_peek_token (parser)->location; |
27bf414c | 5900 | next = c_parser_expr_no_commas (parser, NULL); |
c2255bc4 AH |
5901 | next = default_function_array_conversion (expr_loc, next); |
5902 | expr.value = build_compound_expr (loc, expr.value, next.value); | |
27bf414c | 5903 | expr.original_code = COMPOUND_EXPR; |
81f40b79 | 5904 | expr.original_type = next.original_type; |
27bf414c JM |
5905 | } |
5906 | return expr; | |
5907 | } | |
5908 | ||
46bdb9cf JM |
5909 | /* Parse an expression and convert functions or arrays to |
5910 | pointers. */ | |
5911 | ||
5912 | static struct c_expr | |
5913 | c_parser_expression_conv (c_parser *parser) | |
5914 | { | |
5915 | struct c_expr expr; | |
c2255bc4 | 5916 | location_t loc = c_parser_peek_token (parser)->location; |
46bdb9cf | 5917 | expr = c_parser_expression (parser); |
c2255bc4 | 5918 | expr = default_function_array_conversion (loc, expr); |
46bdb9cf JM |
5919 | return expr; |
5920 | } | |
5921 | ||
5922 | /* Parse a non-empty list of expressions. If CONVERT_P, convert | |
928c19bb | 5923 | functions and arrays to pointers. If FOLD_P, fold the expressions. |
27bf414c JM |
5924 | |
5925 | nonempty-expr-list: | |
5926 | assignment-expression | |
5927 | nonempty-expr-list , assignment-expression | |
5928 | */ | |
5929 | ||
bbbbb16a ILT |
5930 | static VEC(tree,gc) * |
5931 | c_parser_expr_list (c_parser *parser, bool convert_p, bool fold_p, | |
5932 | VEC(tree,gc) **p_orig_types) | |
27bf414c | 5933 | { |
bbbbb16a ILT |
5934 | VEC(tree,gc) *ret; |
5935 | VEC(tree,gc) *orig_types; | |
27bf414c | 5936 | struct c_expr expr; |
c2255bc4 | 5937 | location_t loc = c_parser_peek_token (parser)->location; |
bbbbb16a | 5938 | |
c166b898 | 5939 | ret = make_tree_vector (); |
bbbbb16a ILT |
5940 | if (p_orig_types == NULL) |
5941 | orig_types = NULL; | |
5942 | else | |
c166b898 | 5943 | orig_types = make_tree_vector (); |
bbbbb16a | 5944 | |
27bf414c | 5945 | expr = c_parser_expr_no_commas (parser, NULL); |
46bdb9cf | 5946 | if (convert_p) |
c2255bc4 | 5947 | expr = default_function_array_conversion (loc, expr); |
928c19bb JM |
5948 | if (fold_p) |
5949 | expr.value = c_fully_fold (expr.value, false, NULL); | |
bbbbb16a ILT |
5950 | VEC_quick_push (tree, ret, expr.value); |
5951 | if (orig_types != NULL) | |
5952 | VEC_quick_push (tree, orig_types, expr.original_type); | |
27bf414c JM |
5953 | while (c_parser_next_token_is (parser, CPP_COMMA)) |
5954 | { | |
5955 | c_parser_consume_token (parser); | |
c2255bc4 | 5956 | loc = c_parser_peek_token (parser)->location; |
27bf414c | 5957 | expr = c_parser_expr_no_commas (parser, NULL); |
46bdb9cf | 5958 | if (convert_p) |
c2255bc4 | 5959 | expr = default_function_array_conversion (loc, expr); |
928c19bb JM |
5960 | if (fold_p) |
5961 | expr.value = c_fully_fold (expr.value, false, NULL); | |
bbbbb16a ILT |
5962 | VEC_safe_push (tree, gc, ret, expr.value); |
5963 | if (orig_types != NULL) | |
5964 | VEC_safe_push (tree, gc, orig_types, expr.original_type); | |
27bf414c | 5965 | } |
bbbbb16a ILT |
5966 | if (orig_types != NULL) |
5967 | *p_orig_types = orig_types; | |
27bf414c JM |
5968 | return ret; |
5969 | } | |
27bf414c JM |
5970 | \f |
5971 | /* Parse Objective-C-specific constructs. */ | |
5972 | ||
5973 | /* Parse an objc-class-definition. | |
5974 | ||
5975 | objc-class-definition: | |
5976 | @interface identifier objc-superclass[opt] objc-protocol-refs[opt] | |
5977 | objc-class-instance-variables[opt] objc-methodprotolist @end | |
5978 | @implementation identifier objc-superclass[opt] | |
5979 | objc-class-instance-variables[opt] | |
5980 | @interface identifier ( identifier ) objc-protocol-refs[opt] | |
5981 | objc-methodprotolist @end | |
5982 | @implementation identifier ( identifier ) | |
5983 | ||
5984 | objc-superclass: | |
5985 | : identifier | |
5986 | ||
5987 | "@interface identifier (" must start "@interface identifier ( | |
5988 | identifier ) ...": objc-methodprotolist in the first production may | |
0fa2e4df | 5989 | not start with a parenthesized identifier as a declarator of a data |
27bf414c JM |
5990 | definition with no declaration specifiers if the objc-superclass, |
5991 | objc-protocol-refs and objc-class-instance-variables are omitted. */ | |
5992 | ||
5993 | static void | |
5994 | c_parser_objc_class_definition (c_parser *parser) | |
5995 | { | |
5996 | bool iface_p; | |
5997 | tree id1; | |
5998 | tree superclass; | |
5999 | if (c_parser_next_token_is_keyword (parser, RID_AT_INTERFACE)) | |
6000 | iface_p = true; | |
6001 | else if (c_parser_next_token_is_keyword (parser, RID_AT_IMPLEMENTATION)) | |
6002 | iface_p = false; | |
6003 | else | |
6004 | gcc_unreachable (); | |
6005 | c_parser_consume_token (parser); | |
6006 | if (c_parser_next_token_is_not (parser, CPP_NAME)) | |
6007 | { | |
6008 | c_parser_error (parser, "expected identifier"); | |
6009 | return; | |
6010 | } | |
6011 | id1 = c_parser_peek_token (parser)->value; | |
6012 | c_parser_consume_token (parser); | |
6013 | if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)) | |
6014 | { | |
6015 | tree id2; | |
6016 | tree proto = NULL_TREE; | |
6017 | c_parser_consume_token (parser); | |
6018 | if (c_parser_next_token_is_not (parser, CPP_NAME)) | |
6019 | { | |
6020 | c_parser_error (parser, "expected identifier"); | |
6021 | c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL); | |
6022 | return; | |
6023 | } | |
6024 | id2 = c_parser_peek_token (parser)->value; | |
6025 | c_parser_consume_token (parser); | |
6026 | c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>"); | |
6027 | if (!iface_p) | |
6028 | { | |
6029 | objc_start_category_implementation (id1, id2); | |
6030 | return; | |
6031 | } | |
6032 | if (c_parser_next_token_is (parser, CPP_LESS)) | |
6033 | proto = c_parser_objc_protocol_refs (parser); | |
6034 | objc_start_category_interface (id1, id2, proto); | |
6035 | c_parser_objc_methodprotolist (parser); | |
6036 | c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>"); | |
6037 | objc_finish_interface (); | |
6038 | return; | |
6039 | } | |
6040 | if (c_parser_next_token_is (parser, CPP_COLON)) | |
6041 | { | |
6042 | c_parser_consume_token (parser); | |
6043 | if (c_parser_next_token_is_not (parser, CPP_NAME)) | |
6044 | { | |
6045 | c_parser_error (parser, "expected identifier"); | |
6046 | return; | |
6047 | } | |
6048 | superclass = c_parser_peek_token (parser)->value; | |
6049 | c_parser_consume_token (parser); | |
6050 | } | |
6051 | else | |
6052 | superclass = NULL_TREE; | |
6053 | if (iface_p) | |
6054 | { | |
6055 | tree proto = NULL_TREE; | |
6056 | if (c_parser_next_token_is (parser, CPP_LESS)) | |
6057 | proto = c_parser_objc_protocol_refs (parser); | |
6058 | objc_start_class_interface (id1, superclass, proto); | |
6059 | } | |
6060 | else | |
6061 | objc_start_class_implementation (id1, superclass); | |
6062 | if (c_parser_next_token_is (parser, CPP_OPEN_BRACE)) | |
6063 | c_parser_objc_class_instance_variables (parser); | |
6064 | if (iface_p) | |
6065 | { | |
6066 | objc_continue_interface (); | |
6067 | c_parser_objc_methodprotolist (parser); | |
6068 | c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>"); | |
6069 | objc_finish_interface (); | |
6070 | } | |
6071 | else | |
6072 | { | |
6073 | objc_continue_implementation (); | |
6074 | return; | |
6075 | } | |
6076 | } | |
6077 | ||
6078 | /* Parse objc-class-instance-variables. | |
6079 | ||
6080 | objc-class-instance-variables: | |
6081 | { objc-instance-variable-decl-list[opt] } | |
6082 | ||
6083 | objc-instance-variable-decl-list: | |
6084 | objc-visibility-spec | |
6085 | objc-instance-variable-decl ; | |
6086 | ; | |
6087 | objc-instance-variable-decl-list objc-visibility-spec | |
6088 | objc-instance-variable-decl-list objc-instance-variable-decl ; | |
6089 | objc-instance-variable-decl-list ; | |
6090 | ||
6091 | objc-visibility-spec: | |
6092 | @private | |
6093 | @protected | |
6094 | @public | |
6095 | ||
6096 | objc-instance-variable-decl: | |
6097 | struct-declaration | |
6098 | */ | |
6099 | ||
6100 | static void | |
6101 | c_parser_objc_class_instance_variables (c_parser *parser) | |
6102 | { | |
6103 | gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE)); | |
6104 | c_parser_consume_token (parser); | |
6105 | while (c_parser_next_token_is_not (parser, CPP_EOF)) | |
6106 | { | |
6107 | tree decls; | |
6108 | /* Parse any stray semicolon. */ | |
6109 | if (c_parser_next_token_is (parser, CPP_SEMICOLON)) | |
6110 | { | |
509c9d60 MLI |
6111 | pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic, |
6112 | "extra semicolon in struct or union specified"); | |
27bf414c JM |
6113 | c_parser_consume_token (parser); |
6114 | continue; | |
6115 | } | |
6116 | /* Stop if at the end of the instance variables. */ | |
6117 | if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE)) | |
6118 | { | |
6119 | c_parser_consume_token (parser); | |
6120 | break; | |
6121 | } | |
6122 | /* Parse any objc-visibility-spec. */ | |
eea1139b | 6123 | if (c_parser_next_token_is_keyword (parser, RID_PRIVATE)) |
27bf414c JM |
6124 | { |
6125 | c_parser_consume_token (parser); | |
6126 | objc_set_visibility (2); | |
6127 | continue; | |
6128 | } | |
eea1139b | 6129 | else if (c_parser_next_token_is_keyword (parser, RID_PROTECTED)) |
27bf414c JM |
6130 | { |
6131 | c_parser_consume_token (parser); | |
6132 | objc_set_visibility (0); | |
6133 | continue; | |
6134 | } | |
eea1139b | 6135 | else if (c_parser_next_token_is_keyword (parser, RID_PUBLIC)) |
27bf414c JM |
6136 | { |
6137 | c_parser_consume_token (parser); | |
6138 | objc_set_visibility (1); | |
6139 | continue; | |
6140 | } | |
bc4071dd RH |
6141 | else if (c_parser_next_token_is (parser, CPP_PRAGMA)) |
6142 | { | |
6143 | c_parser_pragma (parser, pragma_external); | |
6144 | continue; | |
6145 | } | |
6146 | ||
27bf414c JM |
6147 | /* Parse some comma-separated declarations. */ |
6148 | decls = c_parser_struct_declaration (parser); | |
6149 | { | |
6150 | /* Comma-separated instance variables are chained together in | |
6151 | reverse order; add them one by one. */ | |
6152 | tree ivar = nreverse (decls); | |
6153 | for (; ivar; ivar = TREE_CHAIN (ivar)) | |
6154 | objc_add_instance_variable (copy_node (ivar)); | |
6155 | } | |
6156 | c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>"); | |
6157 | } | |
6158 | } | |
6159 | ||
6160 | /* Parse an objc-class-declaration. | |
6161 | ||
6162 | objc-class-declaration: | |
6163 | @class identifier-list ; | |
6164 | */ | |
6165 | ||
6166 | static void | |
6167 | c_parser_objc_class_declaration (c_parser *parser) | |
6168 | { | |
6169 | tree list = NULL_TREE; | |
eea1139b | 6170 | gcc_assert (c_parser_next_token_is_keyword (parser, RID_CLASS)); |
27bf414c JM |
6171 | c_parser_consume_token (parser); |
6172 | /* Any identifiers, including those declared as type names, are OK | |
6173 | here. */ | |
6174 | while (true) | |
6175 | { | |
6176 | tree id; | |
6177 | if (c_parser_next_token_is_not (parser, CPP_NAME)) | |
6178 | { | |
6179 | c_parser_error (parser, "expected identifier"); | |
6180 | break; | |
6181 | } | |
6182 | id = c_parser_peek_token (parser)->value; | |
6183 | list = chainon (list, build_tree_list (NULL_TREE, id)); | |
6184 | c_parser_consume_token (parser); | |
6185 | if (c_parser_next_token_is (parser, CPP_COMMA)) | |
6186 | c_parser_consume_token (parser); | |
6187 | else | |
6188 | break; | |
6189 | } | |
6190 | c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>"); | |
6191 | objc_declare_class (list); | |
6192 | } | |
6193 | ||
6194 | /* Parse an objc-alias-declaration. | |
6195 | ||
6196 | objc-alias-declaration: | |
6197 | @compatibility_alias identifier identifier ; | |
6198 | */ | |
6199 | ||
6200 | static void | |
6201 | c_parser_objc_alias_declaration (c_parser *parser) | |
6202 | { | |
6203 | tree id1, id2; | |
6204 | gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_ALIAS)); | |
6205 | c_parser_consume_token (parser); | |
6206 | if (c_parser_next_token_is_not (parser, CPP_NAME)) | |
6207 | { | |
6208 | c_parser_error (parser, "expected identifier"); | |
6209 | c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL); | |
6210 | return; | |
6211 | } | |
6212 | id1 = c_parser_peek_token (parser)->value; | |
6213 | c_parser_consume_token (parser); | |
6214 | if (c_parser_next_token_is_not (parser, CPP_NAME)) | |
6215 | { | |
6216 | c_parser_error (parser, "expected identifier"); | |
6217 | c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL); | |
6218 | return; | |
6219 | } | |
6220 | id2 = c_parser_peek_token (parser)->value; | |
6221 | c_parser_consume_token (parser); | |
6222 | c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>"); | |
6223 | objc_declare_alias (id1, id2); | |
6224 | } | |
6225 | ||
6226 | /* Parse an objc-protocol-definition. | |
6227 | ||
6228 | objc-protocol-definition: | |
6229 | @protocol identifier objc-protocol-refs[opt] objc-methodprotolist @end | |
6230 | @protocol identifier-list ; | |
6231 | ||
6232 | "@protocol identifier ;" should be resolved as "@protocol | |
6233 | identifier-list ;": objc-methodprotolist may not start with a | |
6234 | semicolon in the first alternative if objc-protocol-refs are | |
6235 | omitted. */ | |
6236 | ||
6237 | static void | |
6238 | c_parser_objc_protocol_definition (c_parser *parser) | |
6239 | { | |
6240 | gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_PROTOCOL)); | |
6241 | c_parser_consume_token (parser); | |
6242 | if (c_parser_next_token_is_not (parser, CPP_NAME)) | |
6243 | { | |
6244 | c_parser_error (parser, "expected identifier"); | |
6245 | return; | |
6246 | } | |
6247 | if (c_parser_peek_2nd_token (parser)->type == CPP_COMMA | |
6248 | || c_parser_peek_2nd_token (parser)->type == CPP_SEMICOLON) | |
6249 | { | |
6250 | tree list = NULL_TREE; | |
6251 | /* Any identifiers, including those declared as type names, are | |
6252 | OK here. */ | |
6253 | while (true) | |
6254 | { | |
6255 | tree id; | |
6256 | if (c_parser_next_token_is_not (parser, CPP_NAME)) | |
6257 | { | |
6258 | c_parser_error (parser, "expected identifier"); | |
6259 | break; | |
6260 | } | |
6261 | id = c_parser_peek_token (parser)->value; | |
6262 | list = chainon (list, build_tree_list (NULL_TREE, id)); | |
6263 | c_parser_consume_token (parser); | |
6264 | if (c_parser_next_token_is (parser, CPP_COMMA)) | |
6265 | c_parser_consume_token (parser); | |
6266 | else | |
6267 | break; | |
6268 | } | |
6269 | c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>"); | |
6270 | objc_declare_protocols (list); | |
6271 | } | |
6272 | else | |
6273 | { | |
6274 | tree id = c_parser_peek_token (parser)->value; | |
6275 | tree proto = NULL_TREE; | |
6276 | c_parser_consume_token (parser); | |
6277 | if (c_parser_next_token_is (parser, CPP_LESS)) | |
6278 | proto = c_parser_objc_protocol_refs (parser); | |
0bacb8c7 | 6279 | parser->objc_pq_context = true; |
27bf414c JM |
6280 | objc_start_protocol (id, proto); |
6281 | c_parser_objc_methodprotolist (parser); | |
6282 | c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>"); | |
0bacb8c7 | 6283 | parser->objc_pq_context = false; |
27bf414c JM |
6284 | objc_finish_interface (); |
6285 | } | |
6286 | } | |
6287 | ||
6288 | /* Parse an objc-method-type. | |
6289 | ||
6290 | objc-method-type: | |
6291 | + | |
6292 | - | |
6293 | */ | |
6294 | ||
6295 | static enum tree_code | |
6296 | c_parser_objc_method_type (c_parser *parser) | |
6297 | { | |
6298 | switch (c_parser_peek_token (parser)->type) | |
6299 | { | |
6300 | case CPP_PLUS: | |
6301 | c_parser_consume_token (parser); | |
6302 | return PLUS_EXPR; | |
6303 | case CPP_MINUS: | |
6304 | c_parser_consume_token (parser); | |
6305 | return MINUS_EXPR; | |
6306 | default: | |
6307 | gcc_unreachable (); | |
6308 | } | |
6309 | } | |
6310 | ||
6311 | /* Parse an objc-method-definition. | |
6312 | ||
6313 | objc-method-definition: | |
6314 | objc-method-type objc-method-decl ;[opt] compound-statement | |
6315 | */ | |
6316 | ||
6317 | static void | |
6318 | c_parser_objc_method_definition (c_parser *parser) | |
6319 | { | |
6320 | enum tree_code type = c_parser_objc_method_type (parser); | |
6321 | tree decl; | |
6322 | objc_set_method_type (type); | |
0bacb8c7 | 6323 | parser->objc_pq_context = true; |
27bf414c JM |
6324 | decl = c_parser_objc_method_decl (parser); |
6325 | if (c_parser_next_token_is (parser, CPP_SEMICOLON)) | |
6326 | { | |
6327 | c_parser_consume_token (parser); | |
509c9d60 MLI |
6328 | pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic, |
6329 | "extra semicolon in method definition specified"); | |
27bf414c | 6330 | } |
8f078c08 AP |
6331 | if (!c_parser_next_token_is (parser, CPP_OPEN_BRACE)) |
6332 | { | |
6333 | c_parser_error (parser, "expected %<{%>"); | |
6334 | return; | |
6335 | } | |
0bacb8c7 | 6336 | parser->objc_pq_context = false; |
27bf414c JM |
6337 | objc_start_method_definition (decl); |
6338 | add_stmt (c_parser_compound_statement (parser)); | |
6339 | objc_finish_method_definition (current_function_decl); | |
6340 | } | |
6341 | ||
6342 | /* Parse an objc-methodprotolist. | |
6343 | ||
6344 | objc-methodprotolist: | |
6345 | empty | |
6346 | objc-methodprotolist objc-methodproto | |
6347 | objc-methodprotolist declaration | |
6348 | objc-methodprotolist ; | |
6349 | ||
6350 | The declaration is a data definition, which may be missing | |
6351 | declaration specifiers under the same rules and diagnostics as | |
6352 | other data definitions outside functions, and the stray semicolon | |
6353 | is diagnosed the same way as a stray semicolon outside a | |
6354 | function. */ | |
6355 | ||
6356 | static void | |
6357 | c_parser_objc_methodprotolist (c_parser *parser) | |
6358 | { | |
6359 | while (true) | |
6360 | { | |
6361 | /* The list is terminated by @end. */ | |
6362 | switch (c_parser_peek_token (parser)->type) | |
6363 | { | |
6364 | case CPP_SEMICOLON: | |
509c9d60 MLI |
6365 | pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic, |
6366 | "ISO C does not allow extra %<;%> outside of a function"); | |
27bf414c JM |
6367 | c_parser_consume_token (parser); |
6368 | break; | |
6369 | case CPP_PLUS: | |
6370 | case CPP_MINUS: | |
6371 | c_parser_objc_methodproto (parser); | |
6372 | break; | |
b9b58168 RH |
6373 | case CPP_PRAGMA: |
6374 | c_parser_pragma (parser, pragma_external); | |
6375 | break; | |
27bf414c JM |
6376 | case CPP_EOF: |
6377 | return; | |
6378 | default: | |
6379 | if (c_parser_next_token_is_keyword (parser, RID_AT_END)) | |
6380 | return; | |
6381 | c_parser_declaration_or_fndef (parser, false, true, false, true); | |
6382 | break; | |
6383 | } | |
6384 | } | |
6385 | } | |
6386 | ||
6387 | /* Parse an objc-methodproto. | |
6388 | ||
6389 | objc-methodproto: | |
6390 | objc-method-type objc-method-decl ; | |
6391 | */ | |
6392 | ||
6393 | static void | |
6394 | c_parser_objc_methodproto (c_parser *parser) | |
6395 | { | |
6396 | enum tree_code type = c_parser_objc_method_type (parser); | |
6397 | tree decl; | |
6398 | objc_set_method_type (type); | |
6399 | /* Remember protocol qualifiers in prototypes. */ | |
0bacb8c7 | 6400 | parser->objc_pq_context = true; |
27bf414c JM |
6401 | decl = c_parser_objc_method_decl (parser); |
6402 | /* Forget protocol qualifiers here. */ | |
0bacb8c7 | 6403 | parser->objc_pq_context = false; |
27bf414c JM |
6404 | objc_add_method_declaration (decl); |
6405 | c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>"); | |
6406 | } | |
6407 | ||
6408 | /* Parse an objc-method-decl. | |
6409 | ||
6410 | objc-method-decl: | |
6411 | ( objc-type-name ) objc-selector | |
6412 | objc-selector | |
6413 | ( objc-type-name ) objc-keyword-selector objc-optparmlist | |
6414 | objc-keyword-selector objc-optparmlist | |
6415 | ||
6416 | objc-keyword-selector: | |
6417 | objc-keyword-decl | |
6418 | objc-keyword-selector objc-keyword-decl | |
6419 | ||
6420 | objc-keyword-decl: | |
6421 | objc-selector : ( objc-type-name ) identifier | |
6422 | objc-selector : identifier | |
6423 | : ( objc-type-name ) identifier | |
6424 | : identifier | |
6425 | ||
6426 | objc-optparmlist: | |
6427 | objc-optparms objc-optellipsis | |
6428 | ||
6429 | objc-optparms: | |
6430 | empty | |
6431 | objc-opt-parms , parameter-declaration | |
6432 | ||
6433 | objc-optellipsis: | |
6434 | empty | |
6435 | , ... | |
6436 | */ | |
6437 | ||
6438 | static tree | |
6439 | c_parser_objc_method_decl (c_parser *parser) | |
6440 | { | |
6441 | tree type = NULL_TREE; | |
6442 | tree sel; | |
6443 | tree parms = NULL_TREE; | |
dbb74365 RS |
6444 | bool ellipsis = false; |
6445 | ||
27bf414c JM |
6446 | if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)) |
6447 | { | |
6448 | c_parser_consume_token (parser); | |
6449 | type = c_parser_objc_type_name (parser); | |
6450 | c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>"); | |
6451 | } | |
6452 | sel = c_parser_objc_selector (parser); | |
6453 | /* If there is no selector, or a colon follows, we have an | |
6454 | objc-keyword-selector. If there is a selector, and a colon does | |
6455 | not follow, that selector ends the objc-method-decl. */ | |
6456 | if (!sel || c_parser_next_token_is (parser, CPP_COLON)) | |
6457 | { | |
6458 | tree tsel = sel; | |
6459 | tree list = NULL_TREE; | |
27bf414c JM |
6460 | while (true) |
6461 | { | |
6462 | tree atype = NULL_TREE, id, keyworddecl; | |
6463 | if (!c_parser_require (parser, CPP_COLON, "expected %<:%>")) | |
6464 | break; | |
6465 | if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)) | |
6466 | { | |
6467 | c_parser_consume_token (parser); | |
6468 | atype = c_parser_objc_type_name (parser); | |
6469 | c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, | |
6470 | "expected %<)%>"); | |
6471 | } | |
6472 | if (c_parser_next_token_is_not (parser, CPP_NAME)) | |
6473 | { | |
6474 | c_parser_error (parser, "expected identifier"); | |
6475 | return error_mark_node; | |
6476 | } | |
6477 | id = c_parser_peek_token (parser)->value; | |
6478 | c_parser_consume_token (parser); | |
6479 | keyworddecl = objc_build_keyword_decl (tsel, atype, id); | |
6480 | list = chainon (list, keyworddecl); | |
6481 | tsel = c_parser_objc_selector (parser); | |
6482 | if (!tsel && c_parser_next_token_is_not (parser, CPP_COLON)) | |
6483 | break; | |
6484 | } | |
6485 | /* Parse the optional parameter list. Optional Objective-C | |
6486 | method parameters follow the C syntax, and may include '...' | |
6487 | to denote a variable number of arguments. */ | |
6488 | parms = make_node (TREE_LIST); | |
27bf414c JM |
6489 | while (c_parser_next_token_is (parser, CPP_COMMA)) |
6490 | { | |
6491 | struct c_parm *parm; | |
6492 | c_parser_consume_token (parser); | |
6493 | if (c_parser_next_token_is (parser, CPP_ELLIPSIS)) | |
6494 | { | |
6495 | ellipsis = true; | |
6496 | c_parser_consume_token (parser); | |
6497 | break; | |
6498 | } | |
6499 | parm = c_parser_parameter_declaration (parser, NULL_TREE); | |
6500 | if (parm == NULL) | |
6501 | break; | |
6502 | parms = chainon (parms, | |
6503 | build_tree_list (NULL_TREE, grokparm (parm))); | |
6504 | } | |
27bf414c JM |
6505 | sel = list; |
6506 | } | |
dbb74365 | 6507 | return objc_build_method_signature (type, sel, parms, ellipsis); |
27bf414c JM |
6508 | } |
6509 | ||
6510 | /* Parse an objc-type-name. | |
6511 | ||
6512 | objc-type-name: | |
6513 | objc-type-qualifiers[opt] type-name | |
6514 | objc-type-qualifiers[opt] | |
6515 | ||
6516 | objc-type-qualifiers: | |
6517 | objc-type-qualifier | |
6518 | objc-type-qualifiers objc-type-qualifier | |
6519 | ||
6520 | objc-type-qualifier: one of | |
6521 | in out inout bycopy byref oneway | |
6522 | */ | |
6523 | ||
6524 | static tree | |
6525 | c_parser_objc_type_name (c_parser *parser) | |
6526 | { | |
6527 | tree quals = NULL_TREE; | |
d75d71e0 | 6528 | struct c_type_name *type_name = NULL; |
27bf414c JM |
6529 | tree type = NULL_TREE; |
6530 | while (true) | |
6531 | { | |
6532 | c_token *token = c_parser_peek_token (parser); | |
6533 | if (token->type == CPP_KEYWORD | |
6534 | && (token->keyword == RID_IN | |
6535 | || token->keyword == RID_OUT | |
6536 | || token->keyword == RID_INOUT | |
6537 | || token->keyword == RID_BYCOPY | |
6538 | || token->keyword == RID_BYREF | |
6539 | || token->keyword == RID_ONEWAY)) | |
6540 | { | |
6541 | quals = chainon (quals, build_tree_list (NULL_TREE, token->value)); | |
6542 | c_parser_consume_token (parser); | |
6543 | } | |
6544 | else | |
6545 | break; | |
6546 | } | |
6547 | if (c_parser_next_token_starts_typename (parser)) | |
d75d71e0 ILT |
6548 | type_name = c_parser_type_name (parser); |
6549 | if (type_name) | |
928c19bb | 6550 | type = groktypename (type_name, NULL, NULL); |
27bf414c JM |
6551 | return build_tree_list (quals, type); |
6552 | } | |
6553 | ||
6554 | /* Parse objc-protocol-refs. | |
6555 | ||
6556 | objc-protocol-refs: | |
6557 | < identifier-list > | |
6558 | */ | |
6559 | ||
6560 | static tree | |
6561 | c_parser_objc_protocol_refs (c_parser *parser) | |
6562 | { | |
6563 | tree list = NULL_TREE; | |
6564 | gcc_assert (c_parser_next_token_is (parser, CPP_LESS)); | |
6565 | c_parser_consume_token (parser); | |
6566 | /* Any identifiers, including those declared as type names, are OK | |
6567 | here. */ | |
6568 | while (true) | |
6569 | { | |
6570 | tree id; | |
6571 | if (c_parser_next_token_is_not (parser, CPP_NAME)) | |
6572 | { | |
6573 | c_parser_error (parser, "expected identifier"); | |
6574 | break; | |
6575 | } | |
6576 | id = c_parser_peek_token (parser)->value; | |
6577 | list = chainon (list, build_tree_list (NULL_TREE, id)); | |
6578 | c_parser_consume_token (parser); | |
6579 | if (c_parser_next_token_is (parser, CPP_COMMA)) | |
6580 | c_parser_consume_token (parser); | |
6581 | else | |
6582 | break; | |
6583 | } | |
6584 | c_parser_require (parser, CPP_GREATER, "expected %<>%>"); | |
6585 | return list; | |
6586 | } | |
6587 | ||
6588 | /* Parse an objc-try-catch-statement. | |
6589 | ||
6590 | objc-try-catch-statement: | |
6591 | @try compound-statement objc-catch-list[opt] | |
6592 | @try compound-statement objc-catch-list[opt] @finally compound-statement | |
6593 | ||
6594 | objc-catch-list: | |
6595 | @catch ( parameter-declaration ) compound-statement | |
6596 | objc-catch-list @catch ( parameter-declaration ) compound-statement | |
6597 | */ | |
6598 | ||
6599 | static void | |
6600 | c_parser_objc_try_catch_statement (c_parser *parser) | |
6601 | { | |
6602 | location_t loc; | |
6603 | tree stmt; | |
eea1139b | 6604 | gcc_assert (c_parser_next_token_is_keyword (parser, RID_TRY)); |
27bf414c JM |
6605 | c_parser_consume_token (parser); |
6606 | loc = c_parser_peek_token (parser)->location; | |
6607 | stmt = c_parser_compound_statement (parser); | |
6608 | objc_begin_try_stmt (loc, stmt); | |
eea1139b | 6609 | while (c_parser_next_token_is_keyword (parser, RID_CATCH)) |
27bf414c JM |
6610 | { |
6611 | struct c_parm *parm; | |
6612 | c_parser_consume_token (parser); | |
6613 | if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>")) | |
6614 | break; | |
6615 | parm = c_parser_parameter_declaration (parser, NULL_TREE); | |
6616 | if (parm == NULL) | |
6617 | { | |
6618 | c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL); | |
6619 | break; | |
6620 | } | |
6621 | c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>"); | |
6622 | objc_begin_catch_clause (grokparm (parm)); | |
6623 | if (c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>")) | |
6624 | c_parser_compound_statement_nostart (parser); | |
6625 | objc_finish_catch_clause (); | |
6626 | } | |
6627 | if (c_parser_next_token_is_keyword (parser, RID_AT_FINALLY)) | |
6628 | { | |
6629 | location_t finloc; | |
6630 | tree finstmt; | |
6631 | c_parser_consume_token (parser); | |
6632 | finloc = c_parser_peek_token (parser)->location; | |
6633 | finstmt = c_parser_compound_statement (parser); | |
6634 | objc_build_finally_clause (finloc, finstmt); | |
6635 | } | |
6636 | objc_finish_try_stmt (); | |
6637 | } | |
6638 | ||
6639 | /* Parse an objc-synchronized-statement. | |
6640 | ||
6641 | objc-synchronized-statement: | |
6642 | @synchronized ( expression ) compound-statement | |
6643 | */ | |
6644 | ||
6645 | static void | |
6646 | c_parser_objc_synchronized_statement (c_parser *parser) | |
6647 | { | |
6648 | location_t loc; | |
6649 | tree expr, stmt; | |
6650 | gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_SYNCHRONIZED)); | |
6651 | c_parser_consume_token (parser); | |
6652 | loc = c_parser_peek_token (parser)->location; | |
6653 | if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>")) | |
6654 | { | |
6655 | expr = c_parser_expression (parser).value; | |
928c19bb | 6656 | expr = c_fully_fold (expr, false, NULL); |
27bf414c JM |
6657 | c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>"); |
6658 | } | |
6659 | else | |
6660 | expr = error_mark_node; | |
6661 | stmt = c_parser_compound_statement (parser); | |
6662 | objc_build_synchronized (loc, expr, stmt); | |
6663 | } | |
6664 | ||
6665 | /* Parse an objc-selector; return NULL_TREE without an error if the | |
6666 | next token is not an objc-selector. | |
6667 | ||
6668 | objc-selector: | |
6669 | identifier | |
6670 | one of | |
6671 | enum struct union if else while do for switch case default | |
6672 | break continue return goto asm sizeof typeof __alignof | |
6673 | unsigned long const short volatile signed restrict _Complex | |
6674 | in out inout bycopy byref oneway int char float double void _Bool | |
6675 | ||
6676 | ??? Why this selection of keywords but not, for example, storage | |
6677 | class specifiers? */ | |
6678 | ||
6679 | static tree | |
6680 | c_parser_objc_selector (c_parser *parser) | |
6681 | { | |
6682 | c_token *token = c_parser_peek_token (parser); | |
6683 | tree value = token->value; | |
6684 | if (token->type == CPP_NAME) | |
6685 | { | |
6686 | c_parser_consume_token (parser); | |
6687 | return value; | |
6688 | } | |
6689 | if (token->type != CPP_KEYWORD) | |
6690 | return NULL_TREE; | |
6691 | switch (token->keyword) | |
6692 | { | |
6693 | case RID_ENUM: | |
6694 | case RID_STRUCT: | |
6695 | case RID_UNION: | |
6696 | case RID_IF: | |
6697 | case RID_ELSE: | |
6698 | case RID_WHILE: | |
6699 | case RID_DO: | |
6700 | case RID_FOR: | |
6701 | case RID_SWITCH: | |
6702 | case RID_CASE: | |
6703 | case RID_DEFAULT: | |
6704 | case RID_BREAK: | |
6705 | case RID_CONTINUE: | |
6706 | case RID_RETURN: | |
6707 | case RID_GOTO: | |
6708 | case RID_ASM: | |
6709 | case RID_SIZEOF: | |
6710 | case RID_TYPEOF: | |
6711 | case RID_ALIGNOF: | |
6712 | case RID_UNSIGNED: | |
6713 | case RID_LONG: | |
6714 | case RID_CONST: | |
6715 | case RID_SHORT: | |
6716 | case RID_VOLATILE: | |
6717 | case RID_SIGNED: | |
6718 | case RID_RESTRICT: | |
6719 | case RID_COMPLEX: | |
6720 | case RID_IN: | |
6721 | case RID_OUT: | |
6722 | case RID_INOUT: | |
6723 | case RID_BYCOPY: | |
6724 | case RID_BYREF: | |
6725 | case RID_ONEWAY: | |
6726 | case RID_INT: | |
6727 | case RID_CHAR: | |
6728 | case RID_FLOAT: | |
6729 | case RID_DOUBLE: | |
6730 | case RID_VOID: | |
6731 | case RID_BOOL: | |
6732 | c_parser_consume_token (parser); | |
6733 | return value; | |
6734 | default: | |
6735 | return NULL_TREE; | |
6736 | } | |
6737 | } | |
6738 | ||
6739 | /* Parse an objc-selector-arg. | |
6740 | ||
6741 | objc-selector-arg: | |
6742 | objc-selector | |
6743 | objc-keywordname-list | |
6744 | ||
6745 | objc-keywordname-list: | |
6746 | objc-keywordname | |
6747 | objc-keywordname-list objc-keywordname | |
6748 | ||
6749 | objc-keywordname: | |
6750 | objc-selector : | |
6751 | : | |
6752 | */ | |
6753 | ||
6754 | static tree | |
6755 | c_parser_objc_selector_arg (c_parser *parser) | |
6756 | { | |
6757 | tree sel = c_parser_objc_selector (parser); | |
6758 | tree list = NULL_TREE; | |
6759 | if (sel && c_parser_next_token_is_not (parser, CPP_COLON)) | |
6760 | return sel; | |
6761 | while (true) | |
6762 | { | |
6763 | if (!c_parser_require (parser, CPP_COLON, "expected %<:%>")) | |
6764 | return list; | |
6765 | list = chainon (list, build_tree_list (sel, NULL_TREE)); | |
6766 | sel = c_parser_objc_selector (parser); | |
6767 | if (!sel && c_parser_next_token_is_not (parser, CPP_COLON)) | |
6768 | break; | |
6769 | } | |
6770 | return list; | |
6771 | } | |
6772 | ||
6773 | /* Parse an objc-receiver. | |
6774 | ||
6775 | objc-receiver: | |
6776 | expression | |
6777 | class-name | |
6778 | type-name | |
6779 | */ | |
6780 | ||
6781 | static tree | |
6782 | c_parser_objc_receiver (c_parser *parser) | |
6783 | { | |
6784 | if (c_parser_peek_token (parser)->type == CPP_NAME | |
6785 | && (c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME | |
6786 | || c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME)) | |
6787 | { | |
6788 | tree id = c_parser_peek_token (parser)->value; | |
6789 | c_parser_consume_token (parser); | |
6790 | return objc_get_class_reference (id); | |
6791 | } | |
928c19bb | 6792 | return c_fully_fold (c_parser_expression (parser).value, false, NULL); |
27bf414c JM |
6793 | } |
6794 | ||
6795 | /* Parse objc-message-args. | |
6796 | ||
6797 | objc-message-args: | |
6798 | objc-selector | |
6799 | objc-keywordarg-list | |
6800 | ||
6801 | objc-keywordarg-list: | |
6802 | objc-keywordarg | |
6803 | objc-keywordarg-list objc-keywordarg | |
6804 | ||
6805 | objc-keywordarg: | |
6806 | objc-selector : objc-keywordexpr | |
6807 | : objc-keywordexpr | |
6808 | */ | |
6809 | ||
6810 | static tree | |
6811 | c_parser_objc_message_args (c_parser *parser) | |
6812 | { | |
6813 | tree sel = c_parser_objc_selector (parser); | |
6814 | tree list = NULL_TREE; | |
6815 | if (sel && c_parser_next_token_is_not (parser, CPP_COLON)) | |
6816 | return sel; | |
6817 | while (true) | |
6818 | { | |
6819 | tree keywordexpr; | |
6820 | if (!c_parser_require (parser, CPP_COLON, "expected %<:%>")) | |
0a7d7dea | 6821 | return error_mark_node; |
27bf414c JM |
6822 | keywordexpr = c_parser_objc_keywordexpr (parser); |
6823 | list = chainon (list, build_tree_list (sel, keywordexpr)); | |
6824 | sel = c_parser_objc_selector (parser); | |
6825 | if (!sel && c_parser_next_token_is_not (parser, CPP_COLON)) | |
6826 | break; | |
6827 | } | |
6828 | return list; | |
6829 | } | |
6830 | ||
6831 | /* Parse an objc-keywordexpr. | |
6832 | ||
6833 | objc-keywordexpr: | |
6834 | nonempty-expr-list | |
6835 | */ | |
6836 | ||
6837 | static tree | |
6838 | c_parser_objc_keywordexpr (c_parser *parser) | |
6839 | { | |
bbbbb16a ILT |
6840 | tree ret; |
6841 | VEC(tree,gc) *expr_list = c_parser_expr_list (parser, true, true, NULL); | |
6842 | if (VEC_length (tree, expr_list) == 1) | |
27bf414c JM |
6843 | { |
6844 | /* Just return the expression, remove a level of | |
6845 | indirection. */ | |
bbbbb16a | 6846 | ret = VEC_index (tree, expr_list, 0); |
27bf414c JM |
6847 | } |
6848 | else | |
6849 | { | |
6850 | /* We have a comma expression, we will collapse later. */ | |
c166b898 | 6851 | ret = build_tree_list_vec (expr_list); |
27bf414c | 6852 | } |
c166b898 | 6853 | release_tree_vector (expr_list); |
bbbbb16a | 6854 | return ret; |
27bf414c JM |
6855 | } |
6856 | ||
6857 | \f | |
953ff289 DN |
6858 | /* Handle pragmas. Some OpenMP pragmas are associated with, and therefore |
6859 | should be considered, statements. ALLOW_STMT is true if we're within | |
6860 | the context of a function and such pragmas are to be allowed. Returns | |
6861 | true if we actually parsed such a pragma. */ | |
27bf414c | 6862 | |
bc4071dd | 6863 | static bool |
953ff289 | 6864 | c_parser_pragma (c_parser *parser, enum pragma_context context) |
bc4071dd RH |
6865 | { |
6866 | unsigned int id; | |
6867 | ||
6868 | id = c_parser_peek_token (parser)->pragma_kind; | |
6869 | gcc_assert (id != PRAGMA_NONE); | |
6870 | ||
6871 | switch (id) | |
6872 | { | |
953ff289 DN |
6873 | case PRAGMA_OMP_BARRIER: |
6874 | if (context != pragma_compound) | |
6875 | { | |
6876 | if (context == pragma_stmt) | |
6877 | c_parser_error (parser, "%<#pragma omp barrier%> may only be " | |
6878 | "used in compound statements"); | |
6879 | goto bad_stmt; | |
6880 | } | |
6881 | c_parser_omp_barrier (parser); | |
6882 | return false; | |
6883 | ||
6884 | case PRAGMA_OMP_FLUSH: | |
6885 | if (context != pragma_compound) | |
6886 | { | |
6887 | if (context == pragma_stmt) | |
6888 | c_parser_error (parser, "%<#pragma omp flush%> may only be " | |
6889 | "used in compound statements"); | |
6890 | goto bad_stmt; | |
6891 | } | |
6892 | c_parser_omp_flush (parser); | |
6893 | return false; | |
6894 | ||
a68ab351 JJ |
6895 | case PRAGMA_OMP_TASKWAIT: |
6896 | if (context != pragma_compound) | |
6897 | { | |
6898 | if (context == pragma_stmt) | |
6899 | c_parser_error (parser, "%<#pragma omp taskwait%> may only be " | |
6900 | "used in compound statements"); | |
6901 | goto bad_stmt; | |
6902 | } | |
6903 | c_parser_omp_taskwait (parser); | |
6904 | return false; | |
6905 | ||
953ff289 DN |
6906 | case PRAGMA_OMP_THREADPRIVATE: |
6907 | c_parser_omp_threadprivate (parser); | |
6908 | return false; | |
6909 | ||
6910 | case PRAGMA_OMP_SECTION: | |
3ba09659 AH |
6911 | error_at (c_parser_peek_token (parser)->location, |
6912 | "%<#pragma omp section%> may only be used in " | |
6913 | "%<#pragma omp sections%> construct"); | |
953ff289 DN |
6914 | c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL); |
6915 | return false; | |
6916 | ||
bc4071dd RH |
6917 | case PRAGMA_GCC_PCH_PREPROCESS: |
6918 | c_parser_error (parser, "%<#pragma GCC pch_preprocess%> must be first"); | |
6919 | c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL); | |
6920 | return false; | |
6921 | ||
6922 | default: | |
953ff289 DN |
6923 | if (id < PRAGMA_FIRST_EXTERNAL) |
6924 | { | |
6925 | if (context == pragma_external) | |
6926 | { | |
6927 | bad_stmt: | |
6928 | c_parser_error (parser, "expected declaration specifiers"); | |
6929 | c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL); | |
6930 | return false; | |
6931 | } | |
6932 | c_parser_omp_construct (parser); | |
6933 | return true; | |
6934 | } | |
bc4071dd RH |
6935 | break; |
6936 | } | |
6937 | ||
6938 | c_parser_consume_pragma (parser); | |
6939 | c_invoke_pragma_handler (id); | |
27bf414c | 6940 | |
bc4071dd RH |
6941 | /* Skip to EOL, but suppress any error message. Those will have been |
6942 | generated by the handler routine through calling error, as opposed | |
6943 | to calling c_parser_error. */ | |
6944 | parser->error = true; | |
6945 | c_parser_skip_to_pragma_eol (parser); | |
6946 | ||
6947 | return false; | |
6948 | } | |
6949 | ||
6950 | /* The interface the pragma parsers have to the lexer. */ | |
6951 | ||
6952 | enum cpp_ttype | |
6953 | pragma_lex (tree *value) | |
6954 | { | |
6955 | c_token *tok = c_parser_peek_token (the_parser); | |
6956 | enum cpp_ttype ret = tok->type; | |
6957 | ||
6958 | *value = tok->value; | |
6959 | if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF) | |
6960 | ret = CPP_EOF; | |
6961 | else | |
6962 | { | |
6963 | if (ret == CPP_KEYWORD) | |
6964 | ret = CPP_NAME; | |
6965 | c_parser_consume_token (the_parser); | |
6966 | } | |
6967 | ||
6968 | return ret; | |
6969 | } | |
6970 | ||
6971 | static void | |
6972 | c_parser_pragma_pch_preprocess (c_parser *parser) | |
6973 | { | |
6974 | tree name = NULL; | |
6975 | ||
6976 | c_parser_consume_pragma (parser); | |
6977 | if (c_parser_next_token_is (parser, CPP_STRING)) | |
6978 | { | |
6979 | name = c_parser_peek_token (parser)->value; | |
6980 | c_parser_consume_token (parser); | |
6981 | } | |
6982 | else | |
6983 | c_parser_error (parser, "expected string literal"); | |
6984 | c_parser_skip_to_pragma_eol (parser); | |
6985 | ||
6986 | if (name) | |
6987 | c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name)); | |
6988 | } | |
953ff289 DN |
6989 | \f |
6990 | /* OpenMP 2.5 parsing routines. */ | |
6991 | ||
6992 | /* Returns name of the next clause. | |
6993 | If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and | |
6994 | the token is not consumed. Otherwise appropriate pragma_omp_clause is | |
6995 | returned and the token is consumed. */ | |
6996 | ||
6997 | static pragma_omp_clause | |
6998 | c_parser_omp_clause_name (c_parser *parser) | |
6999 | { | |
7000 | pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE; | |
7001 | ||
7002 | if (c_parser_next_token_is_keyword (parser, RID_IF)) | |
7003 | result = PRAGMA_OMP_CLAUSE_IF; | |
7004 | else if (c_parser_next_token_is_keyword (parser, RID_DEFAULT)) | |
7005 | result = PRAGMA_OMP_CLAUSE_DEFAULT; | |
7006 | else if (c_parser_next_token_is (parser, CPP_NAME)) | |
7007 | { | |
7008 | const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value); | |
7009 | ||
7010 | switch (p[0]) | |
7011 | { | |
7012 | case 'c': | |
a68ab351 JJ |
7013 | if (!strcmp ("collapse", p)) |
7014 | result = PRAGMA_OMP_CLAUSE_COLLAPSE; | |
7015 | else if (!strcmp ("copyin", p)) | |
953ff289 DN |
7016 | result = PRAGMA_OMP_CLAUSE_COPYIN; |
7017 | else if (!strcmp ("copyprivate", p)) | |
7018 | result = PRAGMA_OMP_CLAUSE_COPYPRIVATE; | |
7019 | break; | |
7020 | case 'f': | |
7021 | if (!strcmp ("firstprivate", p)) | |
7022 | result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE; | |
7023 | break; | |
7024 | case 'l': | |
7025 | if (!strcmp ("lastprivate", p)) | |
7026 | result = PRAGMA_OMP_CLAUSE_LASTPRIVATE; | |
7027 | break; | |
7028 | case 'n': | |
7029 | if (!strcmp ("nowait", p)) | |
7030 | result = PRAGMA_OMP_CLAUSE_NOWAIT; | |
7031 | else if (!strcmp ("num_threads", p)) | |
7032 | result = PRAGMA_OMP_CLAUSE_NUM_THREADS; | |
7033 | break; | |
7034 | case 'o': | |
7035 | if (!strcmp ("ordered", p)) | |
7036 | result = PRAGMA_OMP_CLAUSE_ORDERED; | |
7037 | break; | |
7038 | case 'p': | |
7039 | if (!strcmp ("private", p)) | |
7040 | result = PRAGMA_OMP_CLAUSE_PRIVATE; | |
7041 | break; | |
7042 | case 'r': | |
7043 | if (!strcmp ("reduction", p)) | |
7044 | result = PRAGMA_OMP_CLAUSE_REDUCTION; | |
7045 | break; | |
7046 | case 's': | |
7047 | if (!strcmp ("schedule", p)) | |
7048 | result = PRAGMA_OMP_CLAUSE_SCHEDULE; | |
7049 | else if (!strcmp ("shared", p)) | |
7050 | result = PRAGMA_OMP_CLAUSE_SHARED; | |
7051 | break; | |
a68ab351 JJ |
7052 | case 'u': |
7053 | if (!strcmp ("untied", p)) | |
7054 | result = PRAGMA_OMP_CLAUSE_UNTIED; | |
7055 | break; | |
953ff289 DN |
7056 | } |
7057 | } | |
7058 | ||
7059 | if (result != PRAGMA_OMP_CLAUSE_NONE) | |
7060 | c_parser_consume_token (parser); | |
7061 | ||
7062 | return result; | |
7063 | } | |
7064 | ||
7065 | /* Validate that a clause of the given type does not already exist. */ | |
7066 | ||
7067 | static void | |
d75d71e0 ILT |
7068 | check_no_duplicate_clause (tree clauses, enum omp_clause_code code, |
7069 | const char *name) | |
953ff289 DN |
7070 | { |
7071 | tree c; | |
7072 | ||
7073 | for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c)) | |
aaf46ef9 | 7074 | if (OMP_CLAUSE_CODE (c) == code) |
953ff289 | 7075 | { |
c2255bc4 AH |
7076 | location_t loc = OMP_CLAUSE_LOCATION (c); |
7077 | error_at (loc, "too many %qs clauses", name); | |
953ff289 DN |
7078 | break; |
7079 | } | |
7080 | } | |
7081 | ||
7082 | /* OpenMP 2.5: | |
7083 | variable-list: | |
7084 | identifier | |
7085 | variable-list , identifier | |
7086 | ||
c2255bc4 AH |
7087 | If KIND is nonzero, create the appropriate node and install the |
7088 | decl in OMP_CLAUSE_DECL and add the node to the head of the list. | |
7089 | If KIND is nonzero, CLAUSE_LOC is the location of the clause. | |
953ff289 DN |
7090 | |
7091 | If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE; | |
7092 | return the list created. */ | |
7093 | ||
7094 | static tree | |
c2255bc4 AH |
7095 | c_parser_omp_variable_list (c_parser *parser, |
7096 | location_t clause_loc, | |
7097 | enum omp_clause_code kind, | |
aaf46ef9 | 7098 | tree list) |
953ff289 DN |
7099 | { |
7100 | if (c_parser_next_token_is_not (parser, CPP_NAME) | |
7101 | || c_parser_peek_token (parser)->id_kind != C_ID_ID) | |
7102 | c_parser_error (parser, "expected identifier"); | |
7103 | ||
7104 | while (c_parser_next_token_is (parser, CPP_NAME) | |
7105 | && c_parser_peek_token (parser)->id_kind == C_ID_ID) | |
7106 | { | |
7107 | tree t = lookup_name (c_parser_peek_token (parser)->value); | |
7108 | ||
7109 | if (t == NULL_TREE) | |
c2255bc4 AH |
7110 | undeclared_variable (c_parser_peek_token (parser)->location, |
7111 | c_parser_peek_token (parser)->value); | |
953ff289 DN |
7112 | else if (t == error_mark_node) |
7113 | ; | |
7114 | else if (kind != 0) | |
7115 | { | |
c2255bc4 | 7116 | tree u = build_omp_clause (clause_loc, kind); |
953ff289 DN |
7117 | OMP_CLAUSE_DECL (u) = t; |
7118 | OMP_CLAUSE_CHAIN (u) = list; | |
7119 | list = u; | |
7120 | } | |
7121 | else | |
7122 | list = tree_cons (t, NULL_TREE, list); | |
7123 | ||
7124 | c_parser_consume_token (parser); | |
7125 | ||
7126 | if (c_parser_next_token_is_not (parser, CPP_COMMA)) | |
7127 | break; | |
7128 | ||
7129 | c_parser_consume_token (parser); | |
7130 | } | |
7131 | ||
7132 | return list; | |
7133 | } | |
7134 | ||
7135 | /* Similarly, but expect leading and trailing parenthesis. This is a very | |
7136 | common case for omp clauses. */ | |
7137 | ||
7138 | static tree | |
d75d71e0 ILT |
7139 | c_parser_omp_var_list_parens (c_parser *parser, enum omp_clause_code kind, |
7140 | tree list) | |
953ff289 | 7141 | { |
c2255bc4 AH |
7142 | /* The clauses location. */ |
7143 | location_t loc = c_parser_peek_token (parser)->location; | |
7144 | ||
953ff289 DN |
7145 | if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>")) |
7146 | { | |
c2255bc4 | 7147 | list = c_parser_omp_variable_list (parser, loc, kind, list); |
953ff289 DN |
7148 | c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>"); |
7149 | } | |
7150 | return list; | |
7151 | } | |
7152 | ||
a68ab351 JJ |
7153 | /* OpenMP 3.0: |
7154 | collapse ( constant-expression ) */ | |
7155 | ||
7156 | static tree | |
7157 | c_parser_omp_clause_collapse (c_parser *parser, tree list) | |
7158 | { | |
7159 | tree c, num = error_mark_node; | |
7160 | HOST_WIDE_INT n; | |
7161 | location_t loc; | |
7162 | ||
7163 | check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse"); | |
7164 | ||
7165 | loc = c_parser_peek_token (parser)->location; | |
7166 | if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>")) | |
7167 | { | |
7168 | num = c_parser_expr_no_commas (parser, NULL).value; | |
7169 | c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>"); | |
7170 | } | |
7171 | if (num == error_mark_node) | |
7172 | return list; | |
7173 | if (!INTEGRAL_TYPE_P (TREE_TYPE (num)) | |
7174 | || !host_integerp (num, 0) | |
7175 | || (n = tree_low_cst (num, 0)) <= 0 | |
7176 | || (int) n != n) | |
7177 | { | |
3ba09659 AH |
7178 | error_at (loc, |
7179 | "collapse argument needs positive constant integer expression"); | |
a68ab351 JJ |
7180 | return list; |
7181 | } | |
c2255bc4 | 7182 | c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE); |
a68ab351 JJ |
7183 | OMP_CLAUSE_COLLAPSE_EXPR (c) = num; |
7184 | OMP_CLAUSE_CHAIN (c) = list; | |
7185 | return c; | |
7186 | } | |
7187 | ||
953ff289 DN |
7188 | /* OpenMP 2.5: |
7189 | copyin ( variable-list ) */ | |
7190 | ||
7191 | static tree | |
7192 | c_parser_omp_clause_copyin (c_parser *parser, tree list) | |
7193 | { | |
7194 | return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_COPYIN, list); | |
7195 | } | |
7196 | ||
7197 | /* OpenMP 2.5: | |
7198 | copyprivate ( variable-list ) */ | |
7199 | ||
7200 | static tree | |
7201 | c_parser_omp_clause_copyprivate (c_parser *parser, tree list) | |
7202 | { | |
7203 | return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_COPYPRIVATE, list); | |
7204 | } | |
7205 | ||
7206 | /* OpenMP 2.5: | |
7207 | default ( shared | none ) */ | |
7208 | ||
7209 | static tree | |
7210 | c_parser_omp_clause_default (c_parser *parser, tree list) | |
7211 | { | |
7212 | enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED; | |
c2255bc4 | 7213 | location_t loc = c_parser_peek_token (parser)->location; |
953ff289 DN |
7214 | tree c; |
7215 | ||
7216 | if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>")) | |
7217 | return list; | |
7218 | if (c_parser_next_token_is (parser, CPP_NAME)) | |
7219 | { | |
7220 | const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value); | |
7221 | ||
7222 | switch (p[0]) | |
7223 | { | |
7224 | case 'n': | |
7225 | if (strcmp ("none", p) != 0) | |
7226 | goto invalid_kind; | |
7227 | kind = OMP_CLAUSE_DEFAULT_NONE; | |
7228 | break; | |
7229 | ||
7230 | case 's': | |
7231 | if (strcmp ("shared", p) != 0) | |
7232 | goto invalid_kind; | |
7233 | kind = OMP_CLAUSE_DEFAULT_SHARED; | |
7234 | break; | |
7235 | ||
7236 | default: | |
7237 | goto invalid_kind; | |
7238 | } | |
7239 | ||
7240 | c_parser_consume_token (parser); | |
7241 | } | |
7242 | else | |
7243 | { | |
7244 | invalid_kind: | |
7245 | c_parser_error (parser, "expected %<none%> or %<shared%>"); | |
7246 | } | |
7247 | c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>"); | |
7248 | ||
7249 | if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED) | |
7250 | return list; | |
7251 | ||
7252 | check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default"); | |
c2255bc4 | 7253 | c = build_omp_clause (loc, OMP_CLAUSE_DEFAULT); |
953ff289 DN |
7254 | OMP_CLAUSE_CHAIN (c) = list; |
7255 | OMP_CLAUSE_DEFAULT_KIND (c) = kind; | |
7256 | ||
7257 | return c; | |
7258 | } | |
7259 | ||
7260 | /* OpenMP 2.5: | |
7261 | firstprivate ( variable-list ) */ | |
7262 | ||
7263 | static tree | |
7264 | c_parser_omp_clause_firstprivate (c_parser *parser, tree list) | |
7265 | { | |
7266 | return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_FIRSTPRIVATE, list); | |
7267 | } | |
7268 | ||
7269 | /* OpenMP 2.5: | |
7270 | if ( expression ) */ | |
7271 | ||
7272 | static tree | |
7273 | c_parser_omp_clause_if (c_parser *parser, tree list) | |
7274 | { | |
c2255bc4 | 7275 | location_t loc = c_parser_peek_token (parser)->location; |
953ff289 DN |
7276 | if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)) |
7277 | { | |
7278 | tree t = c_parser_paren_condition (parser); | |
7279 | tree c; | |
7280 | ||
7281 | check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if"); | |
7282 | ||
c2255bc4 | 7283 | c = build_omp_clause (loc, OMP_CLAUSE_IF); |
953ff289 DN |
7284 | OMP_CLAUSE_IF_EXPR (c) = t; |
7285 | OMP_CLAUSE_CHAIN (c) = list; | |
7286 | list = c; | |
7287 | } | |
7288 | else | |
7289 | c_parser_error (parser, "expected %<(%>"); | |
7290 | ||
7291 | return list; | |
7292 | } | |
7293 | ||
7294 | /* OpenMP 2.5: | |
7295 | lastprivate ( variable-list ) */ | |
7296 | ||
7297 | static tree | |
7298 | c_parser_omp_clause_lastprivate (c_parser *parser, tree list) | |
7299 | { | |
7300 | return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_LASTPRIVATE, list); | |
7301 | } | |
7302 | ||
7303 | /* OpenMP 2.5: | |
7304 | nowait */ | |
7305 | ||
7306 | static tree | |
7307 | c_parser_omp_clause_nowait (c_parser *parser ATTRIBUTE_UNUSED, tree list) | |
7308 | { | |
7309 | tree c; | |
c2255bc4 | 7310 | location_t loc = c_parser_peek_token (parser)->location; |
953ff289 DN |
7311 | |
7312 | check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait"); | |
7313 | ||
c2255bc4 | 7314 | c = build_omp_clause (loc, OMP_CLAUSE_NOWAIT); |
953ff289 DN |
7315 | OMP_CLAUSE_CHAIN (c) = list; |
7316 | return c; | |
7317 | } | |
7318 | ||
7319 | /* OpenMP 2.5: | |
7320 | num_threads ( expression ) */ | |
7321 | ||
7322 | static tree | |
7323 | c_parser_omp_clause_num_threads (c_parser *parser, tree list) | |
7324 | { | |
c2255bc4 | 7325 | location_t num_threads_loc = c_parser_peek_token (parser)->location; |
953ff289 DN |
7326 | if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>")) |
7327 | { | |
c7412148 | 7328 | location_t expr_loc = c_parser_peek_token (parser)->location; |
953ff289 | 7329 | tree c, t = c_parser_expression (parser).value; |
928c19bb | 7330 | t = c_fully_fold (t, false, NULL); |
953ff289 DN |
7331 | |
7332 | c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>"); | |
7333 | ||
7334 | if (!INTEGRAL_TYPE_P (TREE_TYPE (t))) | |
7335 | { | |
7336 | c_parser_error (parser, "expected integer expression"); | |
7337 | return list; | |
7338 | } | |
7339 | ||
7340 | /* Attempt to statically determine when the number isn't positive. */ | |
7341 | c = fold_build2 (LE_EXPR, boolean_type_node, t, | |
7342 | build_int_cst (TREE_TYPE (t), 0)); | |
c2255bc4 AH |
7343 | if (CAN_HAVE_LOCATION_P (c)) |
7344 | SET_EXPR_LOCATION (c, expr_loc); | |
953ff289 DN |
7345 | if (c == boolean_true_node) |
7346 | { | |
3ba09659 AH |
7347 | warning_at (expr_loc, 0, |
7348 | "%<num_threads%> value must be positive"); | |
953ff289 DN |
7349 | t = integer_one_node; |
7350 | } | |
7351 | ||
7352 | check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS, "num_threads"); | |
7353 | ||
c2255bc4 | 7354 | c = build_omp_clause (num_threads_loc, OMP_CLAUSE_NUM_THREADS); |
953ff289 DN |
7355 | OMP_CLAUSE_NUM_THREADS_EXPR (c) = t; |
7356 | OMP_CLAUSE_CHAIN (c) = list; | |
7357 | list = c; | |
7358 | } | |
7359 | ||
7360 | return list; | |
7361 | } | |
7362 | ||
7363 | /* OpenMP 2.5: | |
7364 | ordered */ | |
7365 | ||
7366 | static tree | |
c2255bc4 | 7367 | c_parser_omp_clause_ordered (c_parser *parser, tree list) |
953ff289 DN |
7368 | { |
7369 | tree c; | |
7370 | ||
7371 | check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED, "ordered"); | |
7372 | ||
c2255bc4 AH |
7373 | c = build_omp_clause (c_parser_peek_token (parser)->location, |
7374 | OMP_CLAUSE_ORDERED); | |
953ff289 | 7375 | OMP_CLAUSE_CHAIN (c) = list; |
c2255bc4 | 7376 | |
953ff289 DN |
7377 | return c; |
7378 | } | |
7379 | ||
7380 | /* OpenMP 2.5: | |
7381 | private ( variable-list ) */ | |
7382 | ||
7383 | static tree | |
7384 | c_parser_omp_clause_private (c_parser *parser, tree list) | |
7385 | { | |
7386 | return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_PRIVATE, list); | |
7387 | } | |
7388 | ||
7389 | /* OpenMP 2.5: | |
7390 | reduction ( reduction-operator : variable-list ) | |
7391 | ||
7392 | reduction-operator: | |
7393 | One of: + * - & ^ | && || */ | |
7394 | ||
7395 | static tree | |
7396 | c_parser_omp_clause_reduction (c_parser *parser, tree list) | |
7397 | { | |
c2255bc4 | 7398 | location_t clause_loc = c_parser_peek_token (parser)->location; |
953ff289 DN |
7399 | if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>")) |
7400 | { | |
7401 | enum tree_code code; | |
7402 | ||
7403 | switch (c_parser_peek_token (parser)->type) | |
7404 | { | |
7405 | case CPP_PLUS: | |
7406 | code = PLUS_EXPR; | |
7407 | break; | |
7408 | case CPP_MULT: | |
7409 | code = MULT_EXPR; | |
7410 | break; | |
7411 | case CPP_MINUS: | |
7412 | code = MINUS_EXPR; | |
7413 | break; | |
7414 | case CPP_AND: | |
7415 | code = BIT_AND_EXPR; | |
7416 | break; | |
7417 | case CPP_XOR: | |
7418 | code = BIT_XOR_EXPR; | |
7419 | break; | |
7420 | case CPP_OR: | |
7421 | code = BIT_IOR_EXPR; | |
7422 | break; | |
7423 | case CPP_AND_AND: | |
7424 | code = TRUTH_ANDIF_EXPR; | |
7425 | break; | |
7426 | case CPP_OR_OR: | |
7427 | code = TRUTH_ORIF_EXPR; | |
7428 | break; | |
7429 | default: | |
7430 | c_parser_error (parser, | |
7431 | "expected %<+%>, %<*%>, %<-%>, %<&%>, " | |
7432 | "%<^%>, %<|%>, %<&&%>, or %<||%>"); | |
7433 | c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0); | |
7434 | return list; | |
7435 | } | |
7436 | c_parser_consume_token (parser); | |
7437 | if (c_parser_require (parser, CPP_COLON, "expected %<:%>")) | |
7438 | { | |
7439 | tree nl, c; | |
7440 | ||
c2255bc4 AH |
7441 | nl = c_parser_omp_variable_list (parser, clause_loc, |
7442 | OMP_CLAUSE_REDUCTION, list); | |
953ff289 DN |
7443 | for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c)) |
7444 | OMP_CLAUSE_REDUCTION_CODE (c) = code; | |
7445 | ||
7446 | list = nl; | |
7447 | } | |
7448 | c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>"); | |
7449 | } | |
7450 | return list; | |
7451 | } | |
7452 | ||
7453 | /* OpenMP 2.5: | |
7454 | schedule ( schedule-kind ) | |
7455 | schedule ( schedule-kind , expression ) | |
7456 | ||
7457 | schedule-kind: | |
a68ab351 | 7458 | static | dynamic | guided | runtime | auto |
953ff289 DN |
7459 | */ |
7460 | ||
7461 | static tree | |
7462 | c_parser_omp_clause_schedule (c_parser *parser, tree list) | |
7463 | { | |
7464 | tree c, t; | |
c2255bc4 | 7465 | location_t loc = c_parser_peek_token (parser)->location; |
953ff289 DN |
7466 | |
7467 | if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>")) | |
7468 | return list; | |
7469 | ||
c2255bc4 | 7470 | c = build_omp_clause (loc, OMP_CLAUSE_SCHEDULE); |
953ff289 DN |
7471 | |
7472 | if (c_parser_next_token_is (parser, CPP_NAME)) | |
7473 | { | |
7474 | tree kind = c_parser_peek_token (parser)->value; | |
7475 | const char *p = IDENTIFIER_POINTER (kind); | |
7476 | ||
7477 | switch (p[0]) | |
7478 | { | |
7479 | case 'd': | |
7480 | if (strcmp ("dynamic", p) != 0) | |
7481 | goto invalid_kind; | |
7482 | OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC; | |
7483 | break; | |
7484 | ||
7485 | case 'g': | |
7486 | if (strcmp ("guided", p) != 0) | |
7487 | goto invalid_kind; | |
7488 | OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED; | |
7489 | break; | |
7490 | ||
7491 | case 'r': | |
7492 | if (strcmp ("runtime", p) != 0) | |
7493 | goto invalid_kind; | |
7494 | OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME; | |
7495 | break; | |
7496 | ||
7497 | default: | |
7498 | goto invalid_kind; | |
7499 | } | |
7500 | } | |
7501 | else if (c_parser_next_token_is_keyword (parser, RID_STATIC)) | |
7502 | OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC; | |
a68ab351 JJ |
7503 | else if (c_parser_next_token_is_keyword (parser, RID_AUTO)) |
7504 | OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO; | |
953ff289 DN |
7505 | else |
7506 | goto invalid_kind; | |
7507 | ||
7508 | c_parser_consume_token (parser); | |
7509 | if (c_parser_next_token_is (parser, CPP_COMMA)) | |
7510 | { | |
c7412148 | 7511 | location_t here; |
953ff289 DN |
7512 | c_parser_consume_token (parser); |
7513 | ||
c7412148 | 7514 | here = c_parser_peek_token (parser)->location; |
953ff289 | 7515 | t = c_parser_expr_no_commas (parser, NULL).value; |
928c19bb | 7516 | t = c_fully_fold (t, false, NULL); |
953ff289 DN |
7517 | |
7518 | if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME) | |
3ba09659 AH |
7519 | error_at (here, "schedule %<runtime%> does not take " |
7520 | "a %<chunk_size%> parameter"); | |
a68ab351 | 7521 | else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO) |
3ba09659 AH |
7522 | error_at (here, |
7523 | "schedule %<auto%> does not take " | |
7524 | "a %<chunk_size%> parameter"); | |
953ff289 DN |
7525 | else if (TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE) |
7526 | OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t; | |
7527 | else | |
7528 | c_parser_error (parser, "expected integer expression"); | |
7529 | ||
7530 | c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>"); | |
7531 | } | |
7532 | else | |
7533 | c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, | |
7534 | "expected %<,%> or %<)%>"); | |
7535 | ||
7536 | check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule"); | |
7537 | OMP_CLAUSE_CHAIN (c) = list; | |
7538 | return c; | |
7539 | ||
7540 | invalid_kind: | |
7541 | c_parser_error (parser, "invalid schedule kind"); | |
7542 | c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0); | |
7543 | return list; | |
7544 | } | |
7545 | ||
7546 | /* OpenMP 2.5: | |
7547 | shared ( variable-list ) */ | |
7548 | ||
7549 | static tree | |
7550 | c_parser_omp_clause_shared (c_parser *parser, tree list) | |
7551 | { | |
7552 | return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_SHARED, list); | |
7553 | } | |
7554 | ||
a68ab351 JJ |
7555 | /* OpenMP 3.0: |
7556 | untied */ | |
7557 | ||
7558 | static tree | |
7559 | c_parser_omp_clause_untied (c_parser *parser ATTRIBUTE_UNUSED, tree list) | |
7560 | { | |
7561 | tree c; | |
7562 | ||
7563 | /* FIXME: Should we allow duplicates? */ | |
7564 | check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied"); | |
7565 | ||
c2255bc4 AH |
7566 | c = build_omp_clause (c_parser_peek_token (parser)->location, |
7567 | OMP_CLAUSE_UNTIED); | |
a68ab351 | 7568 | OMP_CLAUSE_CHAIN (c) = list; |
c2255bc4 | 7569 | |
a68ab351 JJ |
7570 | return c; |
7571 | } | |
7572 | ||
953ff289 DN |
7573 | /* Parse all OpenMP clauses. The set clauses allowed by the directive |
7574 | is a bitmask in MASK. Return the list of clauses found; the result | |
7575 | of clause default goes in *pdefault. */ | |
7576 | ||
7577 | static tree | |
7578 | c_parser_omp_all_clauses (c_parser *parser, unsigned int mask, | |
7579 | const char *where) | |
7580 | { | |
7581 | tree clauses = NULL; | |
8085ca15 | 7582 | bool first = true; |
953ff289 DN |
7583 | |
7584 | while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL)) | |
7585 | { | |
8085ca15 JJ |
7586 | location_t here; |
7587 | pragma_omp_clause c_kind; | |
953ff289 DN |
7588 | const char *c_name; |
7589 | tree prev = clauses; | |
7590 | ||
8085ca15 JJ |
7591 | if (!first && c_parser_next_token_is (parser, CPP_COMMA)) |
7592 | c_parser_consume_token (parser); | |
7593 | ||
7594 | first = false; | |
7595 | here = c_parser_peek_token (parser)->location; | |
7596 | c_kind = c_parser_omp_clause_name (parser); | |
7597 | ||
953ff289 DN |
7598 | switch (c_kind) |
7599 | { | |
a68ab351 JJ |
7600 | case PRAGMA_OMP_CLAUSE_COLLAPSE: |
7601 | clauses = c_parser_omp_clause_collapse (parser, clauses); | |
7602 | c_name = "collapse"; | |
7603 | break; | |
953ff289 DN |
7604 | case PRAGMA_OMP_CLAUSE_COPYIN: |
7605 | clauses = c_parser_omp_clause_copyin (parser, clauses); | |
7606 | c_name = "copyin"; | |
7607 | break; | |
7608 | case PRAGMA_OMP_CLAUSE_COPYPRIVATE: | |
7609 | clauses = c_parser_omp_clause_copyprivate (parser, clauses); | |
7610 | c_name = "copyprivate"; | |
7611 | break; | |
7612 | case PRAGMA_OMP_CLAUSE_DEFAULT: | |
7613 | clauses = c_parser_omp_clause_default (parser, clauses); | |
7614 | c_name = "default"; | |
7615 | break; | |
7616 | case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE: | |
7617 | clauses = c_parser_omp_clause_firstprivate (parser, clauses); | |
7618 | c_name = "firstprivate"; | |
7619 | break; | |
7620 | case PRAGMA_OMP_CLAUSE_IF: | |
7621 | clauses = c_parser_omp_clause_if (parser, clauses); | |
7622 | c_name = "if"; | |
7623 | break; | |
7624 | case PRAGMA_OMP_CLAUSE_LASTPRIVATE: | |
7625 | clauses = c_parser_omp_clause_lastprivate (parser, clauses); | |
7626 | c_name = "lastprivate"; | |
7627 | break; | |
7628 | case PRAGMA_OMP_CLAUSE_NOWAIT: | |
7629 | clauses = c_parser_omp_clause_nowait (parser, clauses); | |
7630 | c_name = "nowait"; | |
7631 | break; | |
7632 | case PRAGMA_OMP_CLAUSE_NUM_THREADS: | |
7633 | clauses = c_parser_omp_clause_num_threads (parser, clauses); | |
7634 | c_name = "num_threads"; | |
7635 | break; | |
7636 | case PRAGMA_OMP_CLAUSE_ORDERED: | |
7637 | clauses = c_parser_omp_clause_ordered (parser, clauses); | |
7638 | c_name = "ordered"; | |
7639 | break; | |
7640 | case PRAGMA_OMP_CLAUSE_PRIVATE: | |
7641 | clauses = c_parser_omp_clause_private (parser, clauses); | |
7642 | c_name = "private"; | |
7643 | break; | |
7644 | case PRAGMA_OMP_CLAUSE_REDUCTION: | |
7645 | clauses = c_parser_omp_clause_reduction (parser, clauses); | |
7646 | c_name = "reduction"; | |
7647 | break; | |
7648 | case PRAGMA_OMP_CLAUSE_SCHEDULE: | |
7649 | clauses = c_parser_omp_clause_schedule (parser, clauses); | |
7650 | c_name = "schedule"; | |
7651 | break; | |
7652 | case PRAGMA_OMP_CLAUSE_SHARED: | |
7653 | clauses = c_parser_omp_clause_shared (parser, clauses); | |
7654 | c_name = "shared"; | |
7655 | break; | |
a68ab351 JJ |
7656 | case PRAGMA_OMP_CLAUSE_UNTIED: |
7657 | clauses = c_parser_omp_clause_untied (parser, clauses); | |
7658 | c_name = "untied"; | |
7659 | break; | |
953ff289 DN |
7660 | default: |
7661 | c_parser_error (parser, "expected %<#pragma omp%> clause"); | |
7662 | goto saw_error; | |
7663 | } | |
7664 | ||
7665 | if (((mask >> c_kind) & 1) == 0 && !parser->error) | |
7666 | { | |
7667 | /* Remove the invalid clause(s) from the list to avoid | |
7668 | confusing the rest of the compiler. */ | |
7669 | clauses = prev; | |
3ba09659 | 7670 | error_at (here, "%qs is not valid for %qs", c_name, where); |
953ff289 DN |
7671 | } |
7672 | } | |
7673 | ||
7674 | saw_error: | |
7675 | c_parser_skip_to_pragma_eol (parser); | |
7676 | ||
7677 | return c_finish_omp_clauses (clauses); | |
7678 | } | |
7679 | ||
7680 | /* OpenMP 2.5: | |
7681 | structured-block: | |
7682 | statement | |
7683 | ||
7684 | In practice, we're also interested in adding the statement to an | |
7685 | outer node. So it is convenient if we work around the fact that | |
7686 | c_parser_statement calls add_stmt. */ | |
7687 | ||
7688 | static tree | |
7689 | c_parser_omp_structured_block (c_parser *parser) | |
7690 | { | |
7691 | tree stmt = push_stmt_list (); | |
7692 | c_parser_statement (parser); | |
7693 | return pop_stmt_list (stmt); | |
7694 | } | |
7695 | ||
7696 | /* OpenMP 2.5: | |
7697 | # pragma omp atomic new-line | |
7698 | expression-stmt | |
7699 | ||
7700 | expression-stmt: | |
7701 | x binop= expr | x++ | ++x | x-- | --x | |
7702 | binop: | |
7703 | +, *, -, /, &, ^, |, <<, >> | |
7704 | ||
c2255bc4 AH |
7705 | where x is an lvalue expression with scalar type. |
7706 | ||
7707 | LOC is the location of the #pragma token. */ | |
953ff289 DN |
7708 | |
7709 | static void | |
c2255bc4 | 7710 | c_parser_omp_atomic (location_t loc, c_parser *parser) |
953ff289 DN |
7711 | { |
7712 | tree lhs, rhs; | |
fe89d797 | 7713 | tree stmt; |
953ff289 | 7714 | enum tree_code code; |
79addd1f | 7715 | struct c_expr rhs_expr; |
953ff289 DN |
7716 | |
7717 | c_parser_skip_to_pragma_eol (parser); | |
7718 | ||
7719 | lhs = c_parser_unary_expression (parser).value; | |
928c19bb | 7720 | lhs = c_fully_fold (lhs, false, NULL); |
953ff289 DN |
7721 | switch (TREE_CODE (lhs)) |
7722 | { | |
7723 | case ERROR_MARK: | |
7724 | saw_error: | |
7725 | c_parser_skip_to_end_of_block_or_statement (parser); | |
7726 | return; | |
7727 | ||
7728 | case PREINCREMENT_EXPR: | |
7729 | case POSTINCREMENT_EXPR: | |
7730 | lhs = TREE_OPERAND (lhs, 0); | |
7731 | code = PLUS_EXPR; | |
7732 | rhs = integer_one_node; | |
7733 | break; | |
7734 | ||
7735 | case PREDECREMENT_EXPR: | |
7736 | case POSTDECREMENT_EXPR: | |
7737 | lhs = TREE_OPERAND (lhs, 0); | |
7738 | code = MINUS_EXPR; | |
7739 | rhs = integer_one_node; | |
7740 | break; | |
7741 | ||
7742 | default: | |
7743 | switch (c_parser_peek_token (parser)->type) | |
7744 | { | |
7745 | case CPP_MULT_EQ: | |
7746 | code = MULT_EXPR; | |
7747 | break; | |
7748 | case CPP_DIV_EQ: | |
7749 | code = TRUNC_DIV_EXPR; | |
7750 | break; | |
7751 | case CPP_PLUS_EQ: | |
7752 | code = PLUS_EXPR; | |
7753 | break; | |
7754 | case CPP_MINUS_EQ: | |
7755 | code = MINUS_EXPR; | |
7756 | break; | |
7757 | case CPP_LSHIFT_EQ: | |
7758 | code = LSHIFT_EXPR; | |
7759 | break; | |
7760 | case CPP_RSHIFT_EQ: | |
7761 | code = RSHIFT_EXPR; | |
7762 | break; | |
7763 | case CPP_AND_EQ: | |
7764 | code = BIT_AND_EXPR; | |
7765 | break; | |
7766 | case CPP_OR_EQ: | |
7767 | code = BIT_IOR_EXPR; | |
7768 | break; | |
7769 | case CPP_XOR_EQ: | |
7770 | code = BIT_XOR_EXPR; | |
7771 | break; | |
7772 | default: | |
7773 | c_parser_error (parser, | |
7774 | "invalid operator for %<#pragma omp atomic%>"); | |
7775 | goto saw_error; | |
7776 | } | |
7777 | ||
7778 | c_parser_consume_token (parser); | |
c2255bc4 AH |
7779 | { |
7780 | location_t rhs_loc = c_parser_peek_token (parser)->location; | |
7781 | rhs_expr = c_parser_expression (parser); | |
7782 | rhs_expr = default_function_array_conversion (rhs_loc, rhs_expr); | |
7783 | } | |
79addd1f | 7784 | rhs = rhs_expr.value; |
928c19bb | 7785 | rhs = c_fully_fold (rhs, false, NULL); |
953ff289 DN |
7786 | break; |
7787 | } | |
c2255bc4 | 7788 | stmt = c_finish_omp_atomic (loc, code, lhs, rhs); |
fe89d797 MM |
7789 | if (stmt != error_mark_node) |
7790 | add_stmt (stmt); | |
953ff289 DN |
7791 | c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>"); |
7792 | } | |
7793 | ||
7794 | ||
7795 | /* OpenMP 2.5: | |
7796 | # pragma omp barrier new-line | |
7797 | */ | |
7798 | ||
7799 | static void | |
7800 | c_parser_omp_barrier (c_parser *parser) | |
7801 | { | |
c2255bc4 | 7802 | location_t loc = c_parser_peek_token (parser)->location; |
953ff289 DN |
7803 | c_parser_consume_pragma (parser); |
7804 | c_parser_skip_to_pragma_eol (parser); | |
7805 | ||
c2255bc4 | 7806 | c_finish_omp_barrier (loc); |
953ff289 DN |
7807 | } |
7808 | ||
7809 | /* OpenMP 2.5: | |
7810 | # pragma omp critical [(name)] new-line | |
7811 | structured-block | |
c2255bc4 AH |
7812 | |
7813 | LOC is the location of the #pragma itself. */ | |
953ff289 DN |
7814 | |
7815 | static tree | |
c2255bc4 | 7816 | c_parser_omp_critical (location_t loc, c_parser *parser) |
953ff289 DN |
7817 | { |
7818 | tree stmt, name = NULL; | |
7819 | ||
7820 | if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)) | |
7821 | { | |
7822 | c_parser_consume_token (parser); | |
7823 | if (c_parser_next_token_is (parser, CPP_NAME)) | |
7824 | { | |
7825 | name = c_parser_peek_token (parser)->value; | |
7826 | c_parser_consume_token (parser); | |
7827 | c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"); | |
7828 | } | |
7829 | else | |
7830 | c_parser_error (parser, "expected identifier"); | |
7831 | } | |
7832 | else if (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL)) | |
7833 | c_parser_error (parser, "expected %<(%> or end of line"); | |
7834 | c_parser_skip_to_pragma_eol (parser); | |
7835 | ||
7836 | stmt = c_parser_omp_structured_block (parser); | |
c2255bc4 | 7837 | return c_finish_omp_critical (loc, stmt, name); |
953ff289 DN |
7838 | } |
7839 | ||
7840 | /* OpenMP 2.5: | |
7841 | # pragma omp flush flush-vars[opt] new-line | |
7842 | ||
7843 | flush-vars: | |
7844 | ( variable-list ) */ | |
7845 | ||
7846 | static void | |
7847 | c_parser_omp_flush (c_parser *parser) | |
7848 | { | |
c2255bc4 | 7849 | location_t loc = c_parser_peek_token (parser)->location; |
953ff289 DN |
7850 | c_parser_consume_pragma (parser); |
7851 | if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)) | |
d75d71e0 | 7852 | c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL); |
953ff289 DN |
7853 | else if (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL)) |
7854 | c_parser_error (parser, "expected %<(%> or end of line"); | |
7855 | c_parser_skip_to_pragma_eol (parser); | |
7856 | ||
c2255bc4 | 7857 | c_finish_omp_flush (loc); |
953ff289 DN |
7858 | } |
7859 | ||
fa10beec | 7860 | /* Parse the restricted form of the for statement allowed by OpenMP. |
953ff289 | 7861 | The real trick here is to determine the loop control variable early |
c2255bc4 AH |
7862 | so that we can push a new decl if necessary to make it private. |
7863 | LOC is the location of the OMP in "#pragma omp". */ | |
953ff289 DN |
7864 | |
7865 | static tree | |
c2255bc4 AH |
7866 | c_parser_omp_for_loop (location_t loc, |
7867 | c_parser *parser, tree clauses, tree *par_clauses) | |
953ff289 | 7868 | { |
a68ab351 JJ |
7869 | tree decl, cond, incr, save_break, save_cont, body, init, stmt, cl; |
7870 | tree declv, condv, incrv, initv, for_block = NULL, ret = NULL; | |
a68ab351 JJ |
7871 | bool fail = false, open_brace_parsed = false; |
7872 | int i, collapse = 1, nbraces = 0; | |
c2255bc4 | 7873 | location_t for_loc; |
a68ab351 JJ |
7874 | |
7875 | for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl)) | |
7876 | if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE) | |
7877 | collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0); | |
7878 | ||
7879 | gcc_assert (collapse >= 1); | |
7880 | ||
7881 | declv = make_tree_vec (collapse); | |
7882 | initv = make_tree_vec (collapse); | |
7883 | condv = make_tree_vec (collapse); | |
7884 | incrv = make_tree_vec (collapse); | |
953ff289 DN |
7885 | |
7886 | if (!c_parser_next_token_is_keyword (parser, RID_FOR)) | |
7887 | { | |
7888 | c_parser_error (parser, "for statement expected"); | |
7889 | return NULL; | |
7890 | } | |
c2255bc4 | 7891 | for_loc = c_parser_peek_token (parser)->location; |
953ff289 DN |
7892 | c_parser_consume_token (parser); |
7893 | ||
a68ab351 | 7894 | for (i = 0; i < collapse; i++) |
953ff289 | 7895 | { |
a68ab351 | 7896 | int bracecount = 0; |
953ff289 | 7897 | |
a68ab351 JJ |
7898 | if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>")) |
7899 | goto pop_scopes; | |
953ff289 | 7900 | |
a68ab351 JJ |
7901 | /* Parse the initialization declaration or expression. */ |
7902 | if (c_parser_next_token_starts_declspecs (parser)) | |
7903 | { | |
7904 | if (i > 0) | |
7905 | for_block | |
7906 | = tree_cons (NULL, c_begin_compound_stmt (true), for_block); | |
7907 | c_parser_declaration_or_fndef (parser, true, true, true, true); | |
c2255bc4 | 7908 | decl = check_for_loop_decls (for_loc); |
a68ab351 JJ |
7909 | if (decl == NULL) |
7910 | goto error_init; | |
7911 | if (DECL_INITIAL (decl) == error_mark_node) | |
7912 | decl = error_mark_node; | |
7913 | init = decl; | |
7914 | } | |
7915 | else if (c_parser_next_token_is (parser, CPP_NAME) | |
7916 | && c_parser_peek_2nd_token (parser)->type == CPP_EQ) | |
7917 | { | |
32e8bb8e | 7918 | struct c_expr decl_exp; |
a68ab351 | 7919 | struct c_expr init_exp; |
c9f9eb5d | 7920 | location_t init_loc; |
a68ab351 | 7921 | |
32e8bb8e ILT |
7922 | decl_exp = c_parser_postfix_expression (parser); |
7923 | decl = decl_exp.value; | |
a68ab351 JJ |
7924 | |
7925 | c_parser_require (parser, CPP_EQ, "expected %<=%>"); | |
7926 | ||
c2255bc4 | 7927 | init_loc = c_parser_peek_token (parser)->location; |
a68ab351 | 7928 | init_exp = c_parser_expr_no_commas (parser, NULL); |
c2255bc4 | 7929 | init_exp = default_function_array_conversion (init_loc, init_exp); |
32e8bb8e | 7930 | init = build_modify_expr (init_loc, decl, decl_exp.original_type, |
c2255bc4 | 7931 | NOP_EXPR, init_loc, init_exp.value, |
bbbbb16a | 7932 | init_exp.original_type); |
c2255bc4 | 7933 | init = c_process_expr_stmt (init_loc, init); |
953ff289 | 7934 | |
a68ab351 JJ |
7935 | c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>"); |
7936 | } | |
7937 | else | |
7938 | { | |
7939 | error_init: | |
7940 | c_parser_error (parser, | |
7941 | "expected iteration declaration or initialization"); | |
7942 | c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, | |
7943 | "expected %<)%>"); | |
7944 | fail = true; | |
7945 | goto parse_next; | |
7946 | } | |
7947 | ||
7948 | /* Parse the loop condition. */ | |
7949 | cond = NULL_TREE; | |
7950 | if (c_parser_next_token_is_not (parser, CPP_SEMICOLON)) | |
7951 | { | |
3ba09659 | 7952 | location_t cond_loc = c_parser_peek_token (parser)->location; |
c5cdb03f | 7953 | struct c_expr cond_expr = c_parser_binary_expression (parser, NULL); |
3ba09659 | 7954 | |
c5cdb03f | 7955 | cond = cond_expr.value; |
3ba09659 | 7956 | cond = c_objc_common_truthvalue_conversion (cond_loc, cond); |
928c19bb | 7957 | cond = c_fully_fold (cond, false, NULL); |
c5cdb03f JJ |
7958 | switch (cond_expr.original_code) |
7959 | { | |
7960 | case GT_EXPR: | |
7961 | case GE_EXPR: | |
7962 | case LT_EXPR: | |
7963 | case LE_EXPR: | |
7964 | break; | |
7965 | default: | |
7966 | /* Can't be cond = error_mark_node, because we want to preserve | |
7967 | the location until c_finish_omp_for. */ | |
7968 | cond = build1 (NOP_EXPR, boolean_type_node, error_mark_node); | |
7969 | break; | |
7970 | } | |
c9f9eb5d | 7971 | protected_set_expr_location (cond, cond_loc); |
a68ab351 | 7972 | } |
953ff289 | 7973 | c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>"); |
953ff289 | 7974 | |
a68ab351 JJ |
7975 | /* Parse the increment expression. */ |
7976 | incr = NULL_TREE; | |
7977 | if (c_parser_next_token_is_not (parser, CPP_CLOSE_PAREN)) | |
c9f9eb5d AH |
7978 | { |
7979 | location_t incr_loc = c_parser_peek_token (parser)->location; | |
7980 | ||
c2255bc4 AH |
7981 | incr = c_process_expr_stmt (incr_loc, |
7982 | c_parser_expression (parser).value); | |
c9f9eb5d | 7983 | } |
a68ab351 | 7984 | c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>"); |
953ff289 | 7985 | |
a68ab351 JJ |
7986 | if (decl == NULL || decl == error_mark_node || init == error_mark_node) |
7987 | fail = true; | |
7988 | else | |
7989 | { | |
7990 | TREE_VEC_ELT (declv, i) = decl; | |
7991 | TREE_VEC_ELT (initv, i) = init; | |
7992 | TREE_VEC_ELT (condv, i) = cond; | |
7993 | TREE_VEC_ELT (incrv, i) = incr; | |
7994 | } | |
7995 | ||
7996 | parse_next: | |
7997 | if (i == collapse - 1) | |
7998 | break; | |
7999 | ||
8000 | /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed | |
8001 | in between the collapsed for loops to be still considered perfectly | |
8002 | nested. Hopefully the final version clarifies this. | |
8003 | For now handle (multiple) {'s and empty statements. */ | |
8004 | do | |
8005 | { | |
8006 | if (c_parser_next_token_is_keyword (parser, RID_FOR)) | |
8007 | { | |
8008 | c_parser_consume_token (parser); | |
8009 | break; | |
8010 | } | |
8011 | else if (c_parser_next_token_is (parser, CPP_OPEN_BRACE)) | |
8012 | { | |
8013 | c_parser_consume_token (parser); | |
8014 | bracecount++; | |
8015 | } | |
8016 | else if (bracecount | |
8017 | && c_parser_next_token_is (parser, CPP_SEMICOLON)) | |
8018 | c_parser_consume_token (parser); | |
8019 | else | |
8020 | { | |
8021 | c_parser_error (parser, "not enough perfectly nested loops"); | |
8022 | if (bracecount) | |
8023 | { | |
8024 | open_brace_parsed = true; | |
8025 | bracecount--; | |
8026 | } | |
8027 | fail = true; | |
8028 | collapse = 0; | |
8029 | break; | |
8030 | } | |
8031 | } | |
8032 | while (1); | |
8033 | ||
8034 | nbraces += bracecount; | |
8035 | } | |
953ff289 | 8036 | |
953ff289 DN |
8037 | save_break = c_break_label; |
8038 | c_break_label = size_one_node; | |
8039 | save_cont = c_cont_label; | |
8040 | c_cont_label = NULL_TREE; | |
8041 | body = push_stmt_list (); | |
8042 | ||
a68ab351 JJ |
8043 | if (open_brace_parsed) |
8044 | { | |
c2255bc4 | 8045 | location_t here = c_parser_peek_token (parser)->location; |
a68ab351 JJ |
8046 | stmt = c_begin_compound_stmt (true); |
8047 | c_parser_compound_statement_nostart (parser); | |
c2255bc4 | 8048 | add_stmt (c_end_compound_stmt (here, stmt, true)); |
a68ab351 JJ |
8049 | } |
8050 | else | |
8051 | add_stmt (c_parser_c99_block_statement (parser)); | |
953ff289 | 8052 | if (c_cont_label) |
c2255bc4 AH |
8053 | { |
8054 | tree t = build1 (LABEL_EXPR, void_type_node, c_cont_label); | |
8055 | SET_EXPR_LOCATION (t, loc); | |
8056 | add_stmt (t); | |
8057 | } | |
953ff289 DN |
8058 | |
8059 | body = pop_stmt_list (body); | |
8060 | c_break_label = save_break; | |
8061 | c_cont_label = save_cont; | |
8062 | ||
a68ab351 JJ |
8063 | while (nbraces) |
8064 | { | |
8065 | if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE)) | |
8066 | { | |
8067 | c_parser_consume_token (parser); | |
8068 | nbraces--; | |
8069 | } | |
8070 | else if (c_parser_next_token_is (parser, CPP_SEMICOLON)) | |
8071 | c_parser_consume_token (parser); | |
8072 | else | |
8073 | { | |
8074 | c_parser_error (parser, "collapsed loops not perfectly nested"); | |
8075 | while (nbraces) | |
8076 | { | |
c2255bc4 | 8077 | location_t here = c_parser_peek_token (parser)->location; |
a68ab351 JJ |
8078 | stmt = c_begin_compound_stmt (true); |
8079 | add_stmt (body); | |
8080 | c_parser_compound_statement_nostart (parser); | |
c2255bc4 | 8081 | body = c_end_compound_stmt (here, stmt, true); |
a68ab351 JJ |
8082 | nbraces--; |
8083 | } | |
8084 | goto pop_scopes; | |
8085 | } | |
8086 | } | |
8087 | ||
61c3a446 | 8088 | /* Only bother calling c_finish_omp_for if we haven't already generated |
953ff289 | 8089 | an error from the initialization parsing. */ |
a68ab351 JJ |
8090 | if (!fail) |
8091 | { | |
8092 | stmt = c_finish_omp_for (loc, declv, initv, condv, incrv, body, NULL); | |
8093 | if (stmt) | |
8094 | { | |
8095 | if (par_clauses != NULL) | |
8096 | { | |
8097 | tree *c; | |
8098 | for (c = par_clauses; *c ; ) | |
8099 | if (OMP_CLAUSE_CODE (*c) != OMP_CLAUSE_FIRSTPRIVATE | |
8100 | && OMP_CLAUSE_CODE (*c) != OMP_CLAUSE_LASTPRIVATE) | |
8101 | c = &OMP_CLAUSE_CHAIN (*c); | |
8102 | else | |
8103 | { | |
8104 | for (i = 0; i < collapse; i++) | |
8105 | if (TREE_VEC_ELT (declv, i) == OMP_CLAUSE_DECL (*c)) | |
8106 | break; | |
8107 | if (i == collapse) | |
8108 | c = &OMP_CLAUSE_CHAIN (*c); | |
8109 | else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE) | |
8110 | { | |
3ba09659 AH |
8111 | error_at (loc, |
8112 | "iteration variable %qD should not be firstprivate", | |
8113 | OMP_CLAUSE_DECL (*c)); | |
a68ab351 JJ |
8114 | *c = OMP_CLAUSE_CHAIN (*c); |
8115 | } | |
8116 | else | |
8117 | { | |
8118 | /* Copy lastprivate (decl) clause to OMP_FOR_CLAUSES, | |
8119 | change it to shared (decl) in | |
8120 | OMP_PARALLEL_CLAUSES. */ | |
c2255bc4 AH |
8121 | tree l = build_omp_clause (OMP_CLAUSE_LOCATION (*c), |
8122 | OMP_CLAUSE_LASTPRIVATE); | |
a68ab351 JJ |
8123 | OMP_CLAUSE_DECL (l) = OMP_CLAUSE_DECL (*c); |
8124 | OMP_CLAUSE_CHAIN (l) = clauses; | |
8125 | clauses = l; | |
8126 | OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED); | |
8127 | } | |
8128 | } | |
8129 | } | |
8130 | OMP_FOR_CLAUSES (stmt) = clauses; | |
8131 | } | |
8132 | ret = stmt; | |
8133 | } | |
8134 | pop_scopes: | |
8135 | while (for_block) | |
8136 | { | |
c2255bc4 AH |
8137 | /* FIXME diagnostics: LOC below should be the actual location of |
8138 | this particular for block. We need to build a list of | |
8139 | locations to go along with FOR_BLOCK. */ | |
8140 | stmt = c_end_compound_stmt (loc, TREE_VALUE (for_block), true); | |
a68ab351 JJ |
8141 | add_stmt (stmt); |
8142 | for_block = TREE_CHAIN (for_block); | |
8143 | } | |
8144 | return ret; | |
953ff289 DN |
8145 | } |
8146 | ||
8147 | /* OpenMP 2.5: | |
8148 | #pragma omp for for-clause[optseq] new-line | |
8149 | for-loop | |
c2255bc4 AH |
8150 | |
8151 | LOC is the location of the #pragma token. | |
953ff289 DN |
8152 | */ |
8153 | ||
8154 | #define OMP_FOR_CLAUSE_MASK \ | |
8155 | ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \ | |
8156 | | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \ | |
8157 | | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \ | |
8158 | | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \ | |
8159 | | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \ | |
8160 | | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \ | |
a68ab351 | 8161 | | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE) \ |
953ff289 DN |
8162 | | (1u << PRAGMA_OMP_CLAUSE_NOWAIT)) |
8163 | ||
8164 | static tree | |
c2255bc4 | 8165 | c_parser_omp_for (location_t loc, c_parser *parser) |
953ff289 DN |
8166 | { |
8167 | tree block, clauses, ret; | |
8168 | ||
8169 | clauses = c_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK, | |
8170 | "#pragma omp for"); | |
8171 | ||
8172 | block = c_begin_compound_stmt (true); | |
c2255bc4 AH |
8173 | ret = c_parser_omp_for_loop (loc, parser, clauses, NULL); |
8174 | block = c_end_compound_stmt (loc, block, true); | |
953ff289 DN |
8175 | add_stmt (block); |
8176 | ||
8177 | return ret; | |
8178 | } | |
8179 | ||
8180 | /* OpenMP 2.5: | |
8181 | # pragma omp master new-line | |
8182 | structured-block | |
c2255bc4 AH |
8183 | |
8184 | LOC is the location of the #pragma token. | |
953ff289 DN |
8185 | */ |
8186 | ||
8187 | static tree | |
c2255bc4 | 8188 | c_parser_omp_master (location_t loc, c_parser *parser) |
953ff289 DN |
8189 | { |
8190 | c_parser_skip_to_pragma_eol (parser); | |
c2255bc4 | 8191 | return c_finish_omp_master (loc, c_parser_omp_structured_block (parser)); |
953ff289 DN |
8192 | } |
8193 | ||
8194 | /* OpenMP 2.5: | |
8195 | # pragma omp ordered new-line | |
8196 | structured-block | |
c2255bc4 AH |
8197 | |
8198 | LOC is the location of the #pragma itself. | |
953ff289 DN |
8199 | */ |
8200 | ||
8201 | static tree | |
c2255bc4 | 8202 | c_parser_omp_ordered (location_t loc, c_parser *parser) |
953ff289 DN |
8203 | { |
8204 | c_parser_skip_to_pragma_eol (parser); | |
c2255bc4 | 8205 | return c_finish_omp_ordered (loc, c_parser_omp_structured_block (parser)); |
953ff289 DN |
8206 | } |
8207 | ||
8208 | /* OpenMP 2.5: | |
8209 | ||
8210 | section-scope: | |
8211 | { section-sequence } | |
8212 | ||
8213 | section-sequence: | |
8214 | section-directive[opt] structured-block | |
c2255bc4 AH |
8215 | section-sequence section-directive structured-block |
8216 | ||
8217 | SECTIONS_LOC is the location of the #pragma omp sections. */ | |
953ff289 DN |
8218 | |
8219 | static tree | |
c2255bc4 | 8220 | c_parser_omp_sections_scope (location_t sections_loc, c_parser *parser) |
953ff289 DN |
8221 | { |
8222 | tree stmt, substmt; | |
8223 | bool error_suppress = false; | |
8224 | location_t loc; | |
8225 | ||
c2255bc4 | 8226 | loc = c_parser_peek_token (parser)->location; |
953ff289 DN |
8227 | if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>")) |
8228 | { | |
8229 | /* Avoid skipping until the end of the block. */ | |
8230 | parser->error = false; | |
8231 | return NULL_TREE; | |
8232 | } | |
8233 | ||
8234 | stmt = push_stmt_list (); | |
8235 | ||
953ff289 DN |
8236 | if (c_parser_peek_token (parser)->pragma_kind != PRAGMA_OMP_SECTION) |
8237 | { | |
8238 | substmt = push_stmt_list (); | |
8239 | ||
8240 | while (1) | |
8241 | { | |
8242 | c_parser_statement (parser); | |
8243 | ||
8244 | if (c_parser_peek_token (parser)->pragma_kind == PRAGMA_OMP_SECTION) | |
8245 | break; | |
8246 | if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE)) | |
8247 | break; | |
8248 | if (c_parser_next_token_is (parser, CPP_EOF)) | |
8249 | break; | |
8250 | } | |
8251 | ||
8252 | substmt = pop_stmt_list (substmt); | |
8253 | substmt = build1 (OMP_SECTION, void_type_node, substmt); | |
8254 | SET_EXPR_LOCATION (substmt, loc); | |
8255 | add_stmt (substmt); | |
8256 | } | |
8257 | ||
8258 | while (1) | |
8259 | { | |
8260 | if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE)) | |
8261 | break; | |
8262 | if (c_parser_next_token_is (parser, CPP_EOF)) | |
8263 | break; | |
8264 | ||
8265 | loc = c_parser_peek_token (parser)->location; | |
8266 | if (c_parser_peek_token (parser)->pragma_kind == PRAGMA_OMP_SECTION) | |
8267 | { | |
8268 | c_parser_consume_pragma (parser); | |
8269 | c_parser_skip_to_pragma_eol (parser); | |
8270 | error_suppress = false; | |
8271 | } | |
8272 | else if (!error_suppress) | |
8273 | { | |
3ba09659 | 8274 | error_at (loc, "expected %<#pragma omp section%> or %<}%>"); |
953ff289 DN |
8275 | error_suppress = true; |
8276 | } | |
8277 | ||
8278 | substmt = c_parser_omp_structured_block (parser); | |
8279 | substmt = build1 (OMP_SECTION, void_type_node, substmt); | |
8280 | SET_EXPR_LOCATION (substmt, loc); | |
8281 | add_stmt (substmt); | |
8282 | } | |
8283 | c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, | |
8284 | "expected %<#pragma omp section%> or %<}%>"); | |
8285 | ||
8286 | substmt = pop_stmt_list (stmt); | |
8287 | ||
8288 | stmt = make_node (OMP_SECTIONS); | |
c2255bc4 | 8289 | SET_EXPR_LOCATION (stmt, sections_loc); |
953ff289 DN |
8290 | TREE_TYPE (stmt) = void_type_node; |
8291 | OMP_SECTIONS_BODY (stmt) = substmt; | |
8292 | ||
8293 | return add_stmt (stmt); | |
8294 | } | |
8295 | ||
8296 | /* OpenMP 2.5: | |
8297 | # pragma omp sections sections-clause[optseq] newline | |
8298 | sections-scope | |
c2255bc4 AH |
8299 | |
8300 | LOC is the location of the #pragma token. | |
953ff289 DN |
8301 | */ |
8302 | ||
8303 | #define OMP_SECTIONS_CLAUSE_MASK \ | |
8304 | ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \ | |
8305 | | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \ | |
8306 | | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \ | |
8307 | | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \ | |
8308 | | (1u << PRAGMA_OMP_CLAUSE_NOWAIT)) | |
8309 | ||
8310 | static tree | |
c2255bc4 | 8311 | c_parser_omp_sections (location_t loc, c_parser *parser) |
953ff289 DN |
8312 | { |
8313 | tree block, clauses, ret; | |
8314 | ||
8315 | clauses = c_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK, | |
8316 | "#pragma omp sections"); | |
8317 | ||
8318 | block = c_begin_compound_stmt (true); | |
c2255bc4 | 8319 | ret = c_parser_omp_sections_scope (loc, parser); |
953ff289 DN |
8320 | if (ret) |
8321 | OMP_SECTIONS_CLAUSES (ret) = clauses; | |
c2255bc4 | 8322 | block = c_end_compound_stmt (loc, block, true); |
953ff289 DN |
8323 | add_stmt (block); |
8324 | ||
8325 | return ret; | |
8326 | } | |
8327 | ||
8328 | /* OpenMP 2.5: | |
8329 | # pragma parallel parallel-clause new-line | |
8330 | # pragma parallel for parallel-for-clause new-line | |
8331 | # pragma parallel sections parallel-sections-clause new-line | |
c2255bc4 AH |
8332 | |
8333 | LOC is the location of the #pragma token. | |
953ff289 DN |
8334 | */ |
8335 | ||
8336 | #define OMP_PARALLEL_CLAUSE_MASK \ | |
8337 | ( (1u << PRAGMA_OMP_CLAUSE_IF) \ | |
8338 | | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \ | |
8339 | | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \ | |
8340 | | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \ | |
8341 | | (1u << PRAGMA_OMP_CLAUSE_SHARED) \ | |
8342 | | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \ | |
8343 | | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \ | |
8344 | | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS)) | |
8345 | ||
8346 | static tree | |
c2255bc4 | 8347 | c_parser_omp_parallel (location_t loc, c_parser *parser) |
953ff289 DN |
8348 | { |
8349 | enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL; | |
8350 | const char *p_name = "#pragma omp parallel"; | |
8351 | tree stmt, clauses, par_clause, ws_clause, block; | |
8352 | unsigned int mask = OMP_PARALLEL_CLAUSE_MASK; | |
8353 | ||
8354 | if (c_parser_next_token_is_keyword (parser, RID_FOR)) | |
8355 | { | |
8356 | c_parser_consume_token (parser); | |
8357 | p_kind = PRAGMA_OMP_PARALLEL_FOR; | |
8358 | p_name = "#pragma omp parallel for"; | |
8359 | mask |= OMP_FOR_CLAUSE_MASK; | |
8360 | mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT); | |
8361 | } | |
8362 | else if (c_parser_next_token_is (parser, CPP_NAME)) | |
8363 | { | |
8364 | const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value); | |
8365 | if (strcmp (p, "sections") == 0) | |
8366 | { | |
8367 | c_parser_consume_token (parser); | |
8368 | p_kind = PRAGMA_OMP_PARALLEL_SECTIONS; | |
8369 | p_name = "#pragma omp parallel sections"; | |
8370 | mask |= OMP_SECTIONS_CLAUSE_MASK; | |
8371 | mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT); | |
8372 | } | |
8373 | } | |
8374 | ||
8375 | clauses = c_parser_omp_all_clauses (parser, mask, p_name); | |
8376 | ||
8377 | switch (p_kind) | |
8378 | { | |
8379 | case PRAGMA_OMP_PARALLEL: | |
8380 | block = c_begin_omp_parallel (); | |
8381 | c_parser_statement (parser); | |
c2255bc4 | 8382 | stmt = c_finish_omp_parallel (loc, clauses, block); |
953ff289 DN |
8383 | break; |
8384 | ||
8385 | case PRAGMA_OMP_PARALLEL_FOR: | |
8386 | block = c_begin_omp_parallel (); | |
c2255bc4 AH |
8387 | c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause); |
8388 | c_parser_omp_for_loop (loc, parser, ws_clause, &par_clause); | |
8389 | stmt = c_finish_omp_parallel (loc, par_clause, block); | |
761041be | 8390 | OMP_PARALLEL_COMBINED (stmt) = 1; |
953ff289 DN |
8391 | break; |
8392 | ||
8393 | case PRAGMA_OMP_PARALLEL_SECTIONS: | |
8394 | block = c_begin_omp_parallel (); | |
c2255bc4 AH |
8395 | c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause); |
8396 | stmt = c_parser_omp_sections_scope (loc, parser); | |
953ff289 DN |
8397 | if (stmt) |
8398 | OMP_SECTIONS_CLAUSES (stmt) = ws_clause; | |
c2255bc4 | 8399 | stmt = c_finish_omp_parallel (loc, par_clause, block); |
761041be | 8400 | OMP_PARALLEL_COMBINED (stmt) = 1; |
953ff289 DN |
8401 | break; |
8402 | ||
8403 | default: | |
8404 | gcc_unreachable (); | |
8405 | } | |
8406 | ||
8407 | return stmt; | |
8408 | } | |
8409 | ||
8410 | /* OpenMP 2.5: | |
8411 | # pragma omp single single-clause[optseq] new-line | |
8412 | structured-block | |
c2255bc4 AH |
8413 | |
8414 | LOC is the location of the #pragma. | |
953ff289 DN |
8415 | */ |
8416 | ||
8417 | #define OMP_SINGLE_CLAUSE_MASK \ | |
8418 | ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \ | |
8419 | | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \ | |
8420 | | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \ | |
8421 | | (1u << PRAGMA_OMP_CLAUSE_NOWAIT)) | |
8422 | ||
8423 | static tree | |
c2255bc4 | 8424 | c_parser_omp_single (location_t loc, c_parser *parser) |
953ff289 DN |
8425 | { |
8426 | tree stmt = make_node (OMP_SINGLE); | |
c2255bc4 | 8427 | SET_EXPR_LOCATION (stmt, loc); |
953ff289 DN |
8428 | TREE_TYPE (stmt) = void_type_node; |
8429 | ||
8430 | OMP_SINGLE_CLAUSES (stmt) | |
8431 | = c_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK, | |
8432 | "#pragma omp single"); | |
8433 | OMP_SINGLE_BODY (stmt) = c_parser_omp_structured_block (parser); | |
8434 | ||
8435 | return add_stmt (stmt); | |
8436 | } | |
8437 | ||
a68ab351 JJ |
8438 | /* OpenMP 3.0: |
8439 | # pragma omp task task-clause[optseq] new-line | |
c2255bc4 AH |
8440 | |
8441 | LOC is the location of the #pragma. | |
a68ab351 JJ |
8442 | */ |
8443 | ||
8444 | #define OMP_TASK_CLAUSE_MASK \ | |
8445 | ( (1u << PRAGMA_OMP_CLAUSE_IF) \ | |
8446 | | (1u << PRAGMA_OMP_CLAUSE_UNTIED) \ | |
8447 | | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \ | |
8448 | | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \ | |
8449 | | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \ | |
8450 | | (1u << PRAGMA_OMP_CLAUSE_SHARED)) | |
8451 | ||
8452 | static tree | |
c2255bc4 | 8453 | c_parser_omp_task (location_t loc, c_parser *parser) |
a68ab351 JJ |
8454 | { |
8455 | tree clauses, block; | |
8456 | ||
8457 | clauses = c_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK, | |
8458 | "#pragma omp task"); | |
8459 | ||
8460 | block = c_begin_omp_task (); | |
8461 | c_parser_statement (parser); | |
c2255bc4 | 8462 | return c_finish_omp_task (loc, clauses, block); |
a68ab351 JJ |
8463 | } |
8464 | ||
8465 | /* OpenMP 3.0: | |
8466 | # pragma omp taskwait new-line | |
8467 | */ | |
8468 | ||
8469 | static void | |
8470 | c_parser_omp_taskwait (c_parser *parser) | |
8471 | { | |
c2255bc4 | 8472 | location_t loc = c_parser_peek_token (parser)->location; |
a68ab351 JJ |
8473 | c_parser_consume_pragma (parser); |
8474 | c_parser_skip_to_pragma_eol (parser); | |
8475 | ||
c2255bc4 | 8476 | c_finish_omp_taskwait (loc); |
a68ab351 | 8477 | } |
953ff289 DN |
8478 | |
8479 | /* Main entry point to parsing most OpenMP pragmas. */ | |
8480 | ||
8481 | static void | |
8482 | c_parser_omp_construct (c_parser *parser) | |
8483 | { | |
8484 | enum pragma_kind p_kind; | |
8485 | location_t loc; | |
8486 | tree stmt; | |
8487 | ||
8488 | loc = c_parser_peek_token (parser)->location; | |
8489 | p_kind = c_parser_peek_token (parser)->pragma_kind; | |
8490 | c_parser_consume_pragma (parser); | |
8491 | ||
5c386a95 JJ |
8492 | /* For all constructs below except #pragma omp atomic |
8493 | MUST_NOT_THROW catch handlers are needed when exceptions | |
8494 | are enabled. */ | |
8495 | if (p_kind != PRAGMA_OMP_ATOMIC) | |
8496 | c_maybe_initialize_eh (); | |
8497 | ||
953ff289 DN |
8498 | switch (p_kind) |
8499 | { | |
8500 | case PRAGMA_OMP_ATOMIC: | |
c2255bc4 | 8501 | c_parser_omp_atomic (loc, parser); |
953ff289 DN |
8502 | return; |
8503 | case PRAGMA_OMP_CRITICAL: | |
c2255bc4 | 8504 | stmt = c_parser_omp_critical (loc, parser); |
953ff289 DN |
8505 | break; |
8506 | case PRAGMA_OMP_FOR: | |
c2255bc4 | 8507 | stmt = c_parser_omp_for (loc, parser); |
953ff289 DN |
8508 | break; |
8509 | case PRAGMA_OMP_MASTER: | |
c2255bc4 | 8510 | stmt = c_parser_omp_master (loc, parser); |
953ff289 DN |
8511 | break; |
8512 | case PRAGMA_OMP_ORDERED: | |
c2255bc4 | 8513 | stmt = c_parser_omp_ordered (loc, parser); |
953ff289 DN |
8514 | break; |
8515 | case PRAGMA_OMP_PARALLEL: | |
c2255bc4 | 8516 | stmt = c_parser_omp_parallel (loc, parser); |
953ff289 DN |
8517 | break; |
8518 | case PRAGMA_OMP_SECTIONS: | |
c2255bc4 | 8519 | stmt = c_parser_omp_sections (loc, parser); |
953ff289 DN |
8520 | break; |
8521 | case PRAGMA_OMP_SINGLE: | |
c2255bc4 | 8522 | stmt = c_parser_omp_single (loc, parser); |
953ff289 | 8523 | break; |
a68ab351 | 8524 | case PRAGMA_OMP_TASK: |
c2255bc4 | 8525 | stmt = c_parser_omp_task (loc, parser); |
a68ab351 | 8526 | break; |
953ff289 DN |
8527 | default: |
8528 | gcc_unreachable (); | |
8529 | } | |
8530 | ||
8531 | if (stmt) | |
c2255bc4 | 8532 | gcc_assert (EXPR_LOCATION (stmt) != UNKNOWN_LOCATION); |
953ff289 DN |
8533 | } |
8534 | ||
8535 | ||
8536 | /* OpenMP 2.5: | |
8537 | # pragma omp threadprivate (variable-list) */ | |
8538 | ||
8539 | static void | |
8540 | c_parser_omp_threadprivate (c_parser *parser) | |
8541 | { | |
8542 | tree vars, t; | |
c2255bc4 | 8543 | location_t loc; |
953ff289 DN |
8544 | |
8545 | c_parser_consume_pragma (parser); | |
c2255bc4 | 8546 | loc = c_parser_peek_token (parser)->location; |
d75d71e0 | 8547 | vars = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL); |
953ff289 | 8548 | |
953ff289 DN |
8549 | /* Mark every variable in VARS to be assigned thread local storage. */ |
8550 | for (t = vars; t; t = TREE_CHAIN (t)) | |
8551 | { | |
8552 | tree v = TREE_PURPOSE (t); | |
8553 | ||
c2255bc4 AH |
8554 | /* FIXME diagnostics: Ideally we should keep individual |
8555 | locations for all the variables in the var list to make the | |
8556 | following errors more precise. Perhaps | |
8557 | c_parser_omp_var_list_parens() should construct a list of | |
8558 | locations to go along with the var list. */ | |
8559 | ||
953ff289 DN |
8560 | /* If V had already been marked threadprivate, it doesn't matter |
8561 | whether it had been used prior to this point. */ | |
5df27e4a | 8562 | if (TREE_CODE (v) != VAR_DECL) |
c2255bc4 | 8563 | error_at (loc, "%qD is not a variable", v); |
5df27e4a | 8564 | else if (TREE_USED (v) && !C_DECL_THREADPRIVATE_P (v)) |
c2255bc4 | 8565 | error_at (loc, "%qE declared %<threadprivate%> after first use", v); |
953ff289 | 8566 | else if (! TREE_STATIC (v) && ! DECL_EXTERNAL (v)) |
c2255bc4 | 8567 | error_at (loc, "automatic variable %qE cannot be %<threadprivate%>", v); |
5df27e4a JJ |
8568 | else if (TREE_TYPE (v) == error_mark_node) |
8569 | ; | |
953ff289 | 8570 | else if (! COMPLETE_TYPE_P (TREE_TYPE (v))) |
c2255bc4 | 8571 | error_at (loc, "%<threadprivate%> %qE has incomplete type", v); |
953ff289 DN |
8572 | else |
8573 | { | |
8574 | if (! DECL_THREAD_LOCAL_P (v)) | |
8575 | { | |
8576 | DECL_TLS_MODEL (v) = decl_default_tls_model (v); | |
8577 | /* If rtl has been already set for this var, call | |
8578 | make_decl_rtl once again, so that encode_section_info | |
8579 | has a chance to look at the new decl flags. */ | |
8580 | if (DECL_RTL_SET_P (v)) | |
8581 | make_decl_rtl (v); | |
8582 | } | |
8583 | C_DECL_THREADPRIVATE_P (v) = 1; | |
8584 | } | |
8585 | } | |
8586 | ||
8587 | c_parser_skip_to_pragma_eol (parser); | |
8588 | } | |
8589 | ||
bc4071dd | 8590 | \f |
27bf414c JM |
8591 | /* Parse a single source file. */ |
8592 | ||
8593 | void | |
8594 | c_parse_file (void) | |
8595 | { | |
bc4071dd RH |
8596 | /* Use local storage to begin. If the first token is a pragma, parse it. |
8597 | If it is #pragma GCC pch_preprocess, then this will load a PCH file | |
8598 | which will cause garbage collection. */ | |
8599 | c_parser tparser; | |
8600 | ||
8601 | memset (&tparser, 0, sizeof tparser); | |
8602 | the_parser = &tparser; | |
8603 | ||
8604 | if (c_parser_peek_token (&tparser)->pragma_kind == PRAGMA_GCC_PCH_PREPROCESS) | |
8605 | c_parser_pragma_pch_preprocess (&tparser); | |
8606 | ||
8607 | the_parser = GGC_NEW (c_parser); | |
8608 | *the_parser = tparser; | |
8609 | ||
27bf414c JM |
8610 | c_parser_translation_unit (the_parser); |
8611 | the_parser = NULL; | |
8612 | } | |
8613 | ||
8614 | #include "gt-c-parser.h" |