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