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