Print this page
10703 smatch unreachable code checking needs reworking
Reviewed by: Toomas Soome <tsoome@me.com>
Reviewed by: Yuri Pankov <yuri.pankov@nexenta.com>
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/tools/smatch/src/parse.c
+++ new/usr/src/tools/smatch/src/parse.c
1 1 /*
2 2 * Stupid C parser, version 1e-6.
3 3 *
4 4 * Let's see how hard this is to do.
5 5 *
6 6 * Copyright (C) 2003 Transmeta Corp.
7 7 * 2003-2004 Linus Torvalds
8 8 * Copyright (C) 2004 Christopher Li
9 9 *
10 10 * Permission is hereby granted, free of charge, to any person obtaining a copy
11 11 * of this software and associated documentation files (the "Software"), to deal
12 12 * in the Software without restriction, including without limitation the rights
13 13 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 14 * copies of the Software, and to permit persons to whom the Software is
15 15 * furnished to do so, subject to the following conditions:
16 16 *
17 17 * The above copyright notice and this permission notice shall be included in
18 18 * all copies or substantial portions of the Software.
19 19 *
20 20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 25 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26 26 * THE SOFTWARE.
27 27 */
28 28
29 29 #include <stdarg.h>
30 30 #include <stdlib.h>
31 31 #include <stdio.h>
32 32 #include <string.h>
33 33 #include <ctype.h>
34 34 #include <unistd.h>
35 35 #include <fcntl.h>
36 36 #include <limits.h>
37 37
38 38 #include "lib.h"
39 39 #include "allocate.h"
40 40 #include "token.h"
41 41 #include "parse.h"
42 42 #include "symbol.h"
43 43 #include "scope.h"
44 44 #include "expression.h"
45 45 #include "target.h"
46 46
47 47 static struct symbol_list **function_symbol_list;
48 48 struct symbol_list *function_computed_target_list;
49 49 struct statement_list *function_computed_goto_list;
50 50
51 51 static struct token *statement(struct token *token, struct statement **tree);
52 52 static struct token *handle_attributes(struct token *token, struct decl_state *ctx, unsigned int keywords);
53 53
54 54 typedef struct token *declarator_t(struct token *, struct decl_state *);
55 55 static declarator_t
56 56 struct_specifier, union_specifier, enum_specifier,
57 57 attribute_specifier, typeof_specifier, parse_asm_declarator,
58 58 typedef_specifier, inline_specifier, auto_specifier,
59 59 register_specifier, static_specifier, extern_specifier,
60 60 thread_specifier, const_qualifier, volatile_qualifier;
61 61
62 62 static struct token *parse_if_statement(struct token *token, struct statement *stmt);
63 63 static struct token *parse_return_statement(struct token *token, struct statement *stmt);
64 64 static struct token *parse_loop_iterator(struct token *token, struct statement *stmt);
65 65 static struct token *parse_default_statement(struct token *token, struct statement *stmt);
66 66 static struct token *parse_case_statement(struct token *token, struct statement *stmt);
67 67 static struct token *parse_switch_statement(struct token *token, struct statement *stmt);
68 68 static struct token *parse_for_statement(struct token *token, struct statement *stmt);
69 69 static struct token *parse_while_statement(struct token *token, struct statement *stmt);
70 70 static struct token *parse_do_statement(struct token *token, struct statement *stmt);
71 71 static struct token *parse_goto_statement(struct token *token, struct statement *stmt);
72 72 static struct token *parse_context_statement(struct token *token, struct statement *stmt);
73 73 static struct token *parse_range_statement(struct token *token, struct statement *stmt);
74 74 static struct token *parse_asm_statement(struct token *token, struct statement *stmt);
75 75 static struct token *toplevel_asm_declaration(struct token *token, struct symbol_list **list);
76 76 static struct token *parse_static_assert(struct token *token, struct symbol_list **unused);
77 77
78 78 typedef struct token *attr_t(struct token *, struct symbol *,
79 79 struct decl_state *);
80 80
81 81 static attr_t
82 82 attribute_packed, attribute_aligned, attribute_modifier,
83 83 attribute_bitwise,
84 84 attribute_address_space, attribute_context,
85 85 attribute_designated_init,
86 86 attribute_transparent_union, ignore_attribute,
87 87 attribute_mode, attribute_force;
88 88
89 89 typedef struct symbol *to_mode_t(struct symbol *);
90 90
91 91 static to_mode_t
92 92 to_QI_mode, to_HI_mode, to_SI_mode, to_DI_mode, to_TI_mode, to_word_mode;
93 93
94 94 enum {
95 95 Set_T = 1,
96 96 Set_S = 2,
97 97 Set_Char = 4,
98 98 Set_Int = 8,
99 99 Set_Double = 16,
100 100 Set_Float = 32,
101 101 Set_Signed = 64,
102 102 Set_Unsigned = 128,
103 103 Set_Short = 256,
104 104 Set_Long = 512,
105 105 Set_Vlong = 1024,
106 106 Set_Int128 = 2048,
107 107 Set_Any = Set_T | Set_Short | Set_Long | Set_Signed | Set_Unsigned
108 108 };
109 109
110 110 enum {
111 111 CInt = 0, CSInt, CUInt, CReal, CChar, CSChar, CUChar,
112 112 };
113 113
114 114 enum {
115 115 SNone = 0, STypedef, SAuto, SRegister, SExtern, SStatic, SForced, SMax,
116 116 };
117 117
118 118 static struct symbol_op typedef_op = {
119 119 .type = KW_MODIFIER,
120 120 .declarator = typedef_specifier,
121 121 };
122 122
123 123 static struct symbol_op inline_op = {
124 124 .type = KW_MODIFIER,
125 125 .declarator = inline_specifier,
126 126 };
127 127
128 128 static declarator_t noreturn_specifier;
129 129 static struct symbol_op noreturn_op = {
130 130 .type = KW_MODIFIER,
131 131 .declarator = noreturn_specifier,
132 132 };
133 133
134 134 static declarator_t alignas_specifier;
135 135 static struct symbol_op alignas_op = {
136 136 .type = KW_MODIFIER,
137 137 .declarator = alignas_specifier,
138 138 };
139 139
140 140 static struct symbol_op auto_op = {
141 141 .type = KW_MODIFIER,
142 142 .declarator = auto_specifier,
143 143 };
144 144
145 145 static struct symbol_op register_op = {
146 146 .type = KW_MODIFIER,
147 147 .declarator = register_specifier,
148 148 };
149 149
150 150 static struct symbol_op static_op = {
151 151 .type = KW_MODIFIER,
152 152 .declarator = static_specifier,
153 153 };
154 154
155 155 static struct symbol_op extern_op = {
156 156 .type = KW_MODIFIER,
157 157 .declarator = extern_specifier,
158 158 };
159 159
160 160 static struct symbol_op thread_op = {
161 161 .type = KW_MODIFIER,
162 162 .declarator = thread_specifier,
163 163 };
164 164
165 165 static struct symbol_op const_op = {
166 166 .type = KW_QUALIFIER,
167 167 .declarator = const_qualifier,
168 168 };
169 169
170 170 static struct symbol_op volatile_op = {
171 171 .type = KW_QUALIFIER,
172 172 .declarator = volatile_qualifier,
173 173 };
174 174
175 175 static struct symbol_op restrict_op = {
176 176 .type = KW_QUALIFIER,
177 177 };
178 178
179 179 static struct symbol_op typeof_op = {
180 180 .type = KW_SPECIFIER,
181 181 .declarator = typeof_specifier,
182 182 .test = Set_Any,
183 183 .set = Set_S|Set_T,
184 184 };
185 185
186 186 static struct symbol_op attribute_op = {
187 187 .type = KW_ATTRIBUTE,
188 188 .declarator = attribute_specifier,
189 189 };
190 190
191 191 static struct symbol_op struct_op = {
192 192 .type = KW_SPECIFIER,
193 193 .declarator = struct_specifier,
194 194 .test = Set_Any,
195 195 .set = Set_S|Set_T,
196 196 };
197 197
198 198 static struct symbol_op union_op = {
199 199 .type = KW_SPECIFIER,
200 200 .declarator = union_specifier,
201 201 .test = Set_Any,
202 202 .set = Set_S|Set_T,
203 203 };
204 204
205 205 static struct symbol_op enum_op = {
206 206 .type = KW_SPECIFIER,
207 207 .declarator = enum_specifier,
208 208 .test = Set_Any,
209 209 .set = Set_S|Set_T,
210 210 };
211 211
212 212 static struct symbol_op spec_op = {
213 213 .type = KW_SPECIFIER | KW_EXACT,
214 214 .test = Set_Any,
215 215 .set = Set_S|Set_T,
216 216 };
217 217
218 218 static struct symbol_op char_op = {
219 219 .type = KW_SPECIFIER,
220 220 .test = Set_T|Set_Long|Set_Short,
221 221 .set = Set_T|Set_Char,
222 222 .class = CChar,
223 223 };
224 224
225 225 static struct symbol_op int_op = {
226 226 .type = KW_SPECIFIER,
227 227 .test = Set_T,
228 228 .set = Set_T|Set_Int,
229 229 };
230 230
231 231 static struct symbol_op double_op = {
232 232 .type = KW_SPECIFIER,
233 233 .test = Set_T|Set_Signed|Set_Unsigned|Set_Short|Set_Vlong,
234 234 .set = Set_T|Set_Double,
235 235 .class = CReal,
236 236 };
237 237
238 238 /* FIXME: this is not even slightly right. */
239 239 static struct symbol_op complex_op = {
240 240 .type = KW_SPECIFIER,
241 241 .test = 0, //Set_T|Set_Signed|Set_Unsigned|Set_Short|Set_Vlong,
242 242 .set = 0, //Set_Double, //Set_T,Set_Double,
243 243 .class = CReal,
244 244 };
245 245
246 246 static struct symbol_op float_op = {
247 247 .type = KW_SPECIFIER | KW_SHORT,
248 248 .test = Set_T|Set_Signed|Set_Unsigned|Set_Short|Set_Long,
249 249 .set = Set_T|Set_Float,
250 250 .class = CReal,
251 251 };
252 252
253 253 static struct symbol_op short_op = {
254 254 .type = KW_SPECIFIER | KW_SHORT,
255 255 .test = Set_S|Set_Char|Set_Float|Set_Double|Set_Long|Set_Short,
256 256 .set = Set_Short,
257 257 };
258 258
259 259 static struct symbol_op signed_op = {
260 260 .type = KW_SPECIFIER,
261 261 .test = Set_S|Set_Float|Set_Double|Set_Signed|Set_Unsigned,
262 262 .set = Set_Signed,
263 263 .class = CSInt,
264 264 };
265 265
266 266 static struct symbol_op unsigned_op = {
267 267 .type = KW_SPECIFIER,
268 268 .test = Set_S|Set_Float|Set_Double|Set_Signed|Set_Unsigned,
269 269 .set = Set_Unsigned,
270 270 .class = CUInt,
271 271 };
272 272
273 273 static struct symbol_op long_op = {
274 274 .type = KW_SPECIFIER | KW_LONG,
275 275 .test = Set_S|Set_Char|Set_Float|Set_Short|Set_Vlong,
276 276 .set = Set_Long,
277 277 };
278 278
279 279 static struct symbol_op int128_op = {
280 280 .type = KW_SPECIFIER | KW_LONG,
281 281 .test = Set_S|Set_T|Set_Char|Set_Short|Set_Int|Set_Float|Set_Double|Set_Long|Set_Vlong|Set_Int128,
282 282 .set = Set_T|Set_Int128,
283 283 };
284 284
285 285 static struct symbol_op if_op = {
286 286 .statement = parse_if_statement,
287 287 };
288 288
289 289 static struct symbol_op return_op = {
290 290 .statement = parse_return_statement,
291 291 };
292 292
293 293 static struct symbol_op loop_iter_op = {
294 294 .statement = parse_loop_iterator,
295 295 };
296 296
297 297 static struct symbol_op default_op = {
298 298 .statement = parse_default_statement,
299 299 };
300 300
301 301 static struct symbol_op case_op = {
302 302 .statement = parse_case_statement,
303 303 };
304 304
305 305 static struct symbol_op switch_op = {
306 306 .statement = parse_switch_statement,
307 307 };
308 308
309 309 static struct symbol_op for_op = {
310 310 .statement = parse_for_statement,
311 311 };
312 312
313 313 static struct symbol_op while_op = {
314 314 .statement = parse_while_statement,
315 315 };
316 316
317 317 static struct symbol_op do_op = {
318 318 .statement = parse_do_statement,
319 319 };
320 320
321 321 static struct symbol_op goto_op = {
322 322 .statement = parse_goto_statement,
323 323 };
324 324
325 325 static struct symbol_op __context___op = {
326 326 .statement = parse_context_statement,
327 327 };
328 328
329 329 static struct symbol_op range_op = {
330 330 .statement = parse_range_statement,
331 331 };
332 332
333 333 static struct symbol_op asm_op = {
334 334 .type = KW_ASM,
335 335 .declarator = parse_asm_declarator,
336 336 .statement = parse_asm_statement,
337 337 .toplevel = toplevel_asm_declaration,
338 338 };
339 339
340 340 static struct symbol_op static_assert_op = {
341 341 .toplevel = parse_static_assert,
342 342 };
343 343
344 344 static struct symbol_op packed_op = {
345 345 .attribute = attribute_packed,
346 346 };
347 347
348 348 static struct symbol_op aligned_op = {
349 349 .attribute = attribute_aligned,
350 350 };
351 351
352 352 static struct symbol_op attr_mod_op = {
353 353 .attribute = attribute_modifier,
354 354 };
355 355
356 356 static struct symbol_op attr_bitwise_op = {
357 357 .attribute = attribute_bitwise,
358 358 };
359 359
360 360 static struct symbol_op attr_force_op = {
361 361 .attribute = attribute_force,
362 362 };
363 363
364 364 static struct symbol_op address_space_op = {
365 365 .attribute = attribute_address_space,
366 366 };
367 367
368 368 static struct symbol_op mode_op = {
369 369 .attribute = attribute_mode,
370 370 };
371 371
372 372 static struct symbol_op context_op = {
373 373 .attribute = attribute_context,
374 374 };
375 375
376 376 static struct symbol_op designated_init_op = {
377 377 .attribute = attribute_designated_init,
378 378 };
379 379
380 380 static struct symbol_op transparent_union_op = {
381 381 .attribute = attribute_transparent_union,
382 382 };
383 383
384 384 static struct symbol_op ignore_attr_op = {
385 385 .attribute = ignore_attribute,
386 386 };
387 387
388 388 static struct symbol_op mode_QI_op = {
389 389 .type = KW_MODE,
390 390 .to_mode = to_QI_mode
391 391 };
392 392
393 393 static struct symbol_op mode_HI_op = {
394 394 .type = KW_MODE,
395 395 .to_mode = to_HI_mode
396 396 };
397 397
398 398 static struct symbol_op mode_SI_op = {
399 399 .type = KW_MODE,
400 400 .to_mode = to_SI_mode
401 401 };
402 402
403 403 static struct symbol_op mode_DI_op = {
404 404 .type = KW_MODE,
405 405 .to_mode = to_DI_mode
406 406 };
407 407
408 408 static struct symbol_op mode_TI_op = {
409 409 .type = KW_MODE,
410 410 .to_mode = to_TI_mode
411 411 };
412 412
413 413 static struct symbol_op mode_word_op = {
414 414 .type = KW_MODE,
415 415 .to_mode = to_word_mode
416 416 };
417 417
418 418 /* Using NS_TYPEDEF will also make the keyword a reserved one */
419 419 static struct init_keyword {
420 420 const char *name;
421 421 enum namespace ns;
422 422 unsigned long modifiers;
423 423 struct symbol_op *op;
424 424 struct symbol *type;
425 425 } keyword_table[] = {
426 426 /* Type qualifiers */
427 427 { "const", NS_TYPEDEF, .op = &const_op },
428 428 { "__const", NS_TYPEDEF, .op = &const_op },
429 429 { "__const__", NS_TYPEDEF, .op = &const_op },
430 430 { "volatile", NS_TYPEDEF, .op = &volatile_op },
431 431 { "__volatile", NS_TYPEDEF, .op = &volatile_op },
432 432 { "__volatile__", NS_TYPEDEF, .op = &volatile_op },
433 433
434 434 /* Typedef.. */
435 435 { "typedef", NS_TYPEDEF, .op = &typedef_op },
436 436
437 437 /* Type specifiers */
438 438 { "void", NS_TYPEDEF, .type = &void_ctype, .op = &spec_op},
439 439 { "char", NS_TYPEDEF, .op = &char_op },
440 440 { "short", NS_TYPEDEF, .op = &short_op },
441 441 { "int", NS_TYPEDEF, .op = &int_op },
442 442 { "long", NS_TYPEDEF, .op = &long_op },
443 443 { "float", NS_TYPEDEF, .op = &float_op },
444 444 { "double", NS_TYPEDEF, .op = &double_op },
445 445 { "signed", NS_TYPEDEF, .op = &signed_op },
446 446 { "__signed", NS_TYPEDEF, .op = &signed_op },
447 447 { "__signed__", NS_TYPEDEF, .op = &signed_op },
448 448 { "unsigned", NS_TYPEDEF, .op = &unsigned_op },
449 449 { "__int128", NS_TYPEDEF, .op = &int128_op },
450 450 { "_Bool", NS_TYPEDEF, .type = &bool_ctype, .op = &spec_op },
451 451 { "_Complex", NS_TYPEDEF, .op = &complex_op },
452 452
453 453 /* Predeclared types */
454 454 { "__builtin_va_list", NS_TYPEDEF, .type = &ptr_ctype, .op = &spec_op },
455 455 { "__builtin_ms_va_list", NS_TYPEDEF, .type = &ptr_ctype, .op = &spec_op },
456 456 { "__int128_t", NS_TYPEDEF, .type = &lllong_ctype, .op = &spec_op },
457 457 { "__uint128_t",NS_TYPEDEF, .type = &ulllong_ctype, .op = &spec_op },
458 458
459 459 /* Extended types */
460 460 { "typeof", NS_TYPEDEF, .op = &typeof_op },
461 461 { "__typeof", NS_TYPEDEF, .op = &typeof_op },
462 462 { "__typeof__", NS_TYPEDEF, .op = &typeof_op },
463 463
464 464 { "__attribute", NS_TYPEDEF, .op = &attribute_op },
465 465 { "__attribute__", NS_TYPEDEF, .op = &attribute_op },
466 466
467 467 { "struct", NS_TYPEDEF, .op = &struct_op },
468 468 { "union", NS_TYPEDEF, .op = &union_op },
469 469 { "enum", NS_TYPEDEF, .op = &enum_op },
470 470
471 471 { "inline", NS_TYPEDEF, .op = &inline_op },
472 472 { "__inline", NS_TYPEDEF, .op = &inline_op },
473 473 { "__inline__", NS_TYPEDEF, .op = &inline_op },
474 474
475 475 { "_Noreturn", NS_TYPEDEF, .op = &noreturn_op },
476 476
477 477 { "_Alignas", NS_TYPEDEF, .op = &alignas_op },
478 478
479 479 /* Ignored for now.. */
480 480 { "restrict", NS_TYPEDEF, .op = &restrict_op},
481 481 { "__restrict", NS_TYPEDEF, .op = &restrict_op},
482 482 { "__restrict__", NS_TYPEDEF, .op = &restrict_op},
483 483
484 484 /* Static assertion */
485 485 { "_Static_assert", NS_KEYWORD, .op = &static_assert_op },
486 486
487 487 /* Storage class */
488 488 { "auto", NS_TYPEDEF, .op = &auto_op },
489 489 { "register", NS_TYPEDEF, .op = ®ister_op },
490 490 { "static", NS_TYPEDEF, .op = &static_op },
491 491 { "extern", NS_TYPEDEF, .op = &extern_op },
492 492 { "__thread", NS_TYPEDEF, .op = &thread_op },
493 493 { "_Thread_local", NS_TYPEDEF, .op = &thread_op },
494 494
495 495 /* Statement */
496 496 { "if", NS_KEYWORD, .op = &if_op },
497 497 { "return", NS_KEYWORD, .op = &return_op },
498 498 { "break", NS_KEYWORD, .op = &loop_iter_op },
499 499 { "continue", NS_KEYWORD, .op = &loop_iter_op },
500 500 { "default", NS_KEYWORD, .op = &default_op },
501 501 { "case", NS_KEYWORD, .op = &case_op },
502 502 { "switch", NS_KEYWORD, .op = &switch_op },
503 503 { "for", NS_KEYWORD, .op = &for_op },
504 504 { "while", NS_KEYWORD, .op = &while_op },
505 505 { "do", NS_KEYWORD, .op = &do_op },
506 506 { "goto", NS_KEYWORD, .op = &goto_op },
507 507 { "__context__",NS_KEYWORD, .op = &__context___op },
508 508 { "__range__", NS_KEYWORD, .op = &range_op },
509 509 { "asm", NS_KEYWORD, .op = &asm_op },
510 510 { "__asm", NS_KEYWORD, .op = &asm_op },
511 511 { "__asm__", NS_KEYWORD, .op = &asm_op },
512 512
513 513 /* Attribute */
514 514 { "packed", NS_KEYWORD, .op = &packed_op },
515 515 { "__packed__", NS_KEYWORD, .op = &packed_op },
516 516 { "aligned", NS_KEYWORD, .op = &aligned_op },
517 517 { "__aligned__",NS_KEYWORD, .op = &aligned_op },
518 518 { "nocast", NS_KEYWORD, MOD_NOCAST, .op = &attr_mod_op },
519 519 { "noderef", NS_KEYWORD, MOD_NODEREF, .op = &attr_mod_op },
520 520 { "safe", NS_KEYWORD, MOD_SAFE, .op = &attr_mod_op },
521 521 { "force", NS_KEYWORD, .op = &attr_force_op },
522 522 { "bitwise", NS_KEYWORD, MOD_BITWISE, .op = &attr_bitwise_op },
523 523 { "__bitwise__",NS_KEYWORD, MOD_BITWISE, .op = &attr_bitwise_op },
524 524 { "address_space",NS_KEYWORD, .op = &address_space_op },
525 525 { "mode", NS_KEYWORD, .op = &mode_op },
526 526 { "context", NS_KEYWORD, .op = &context_op },
527 527 { "designated_init", NS_KEYWORD, .op = &designated_init_op },
528 528 { "__transparent_union__", NS_KEYWORD, .op = &transparent_union_op },
529 529 { "noreturn", NS_KEYWORD, MOD_NORETURN, .op = &attr_mod_op },
530 530 { "__noreturn__", NS_KEYWORD, MOD_NORETURN, .op = &attr_mod_op },
531 531 { "pure", NS_KEYWORD, MOD_PURE, .op = &attr_mod_op },
532 532 {"__pure__", NS_KEYWORD, MOD_PURE, .op = &attr_mod_op },
533 533 {"const", NS_KEYWORD, MOD_PURE, .op = &attr_mod_op },
534 534 {"__const", NS_KEYWORD, MOD_PURE, .op = &attr_mod_op },
535 535 {"__const__", NS_KEYWORD, MOD_PURE, .op = &attr_mod_op },
536 536
537 537 { "__mode__", NS_KEYWORD, .op = &mode_op },
538 538 { "QI", NS_KEYWORD, MOD_CHAR, .op = &mode_QI_op },
539 539 { "__QI__", NS_KEYWORD, MOD_CHAR, .op = &mode_QI_op },
540 540 { "HI", NS_KEYWORD, MOD_SHORT, .op = &mode_HI_op },
541 541 { "__HI__", NS_KEYWORD, MOD_SHORT, .op = &mode_HI_op },
542 542 { "SI", NS_KEYWORD, .op = &mode_SI_op },
543 543 { "__SI__", NS_KEYWORD, .op = &mode_SI_op },
544 544 { "DI", NS_KEYWORD, MOD_LONGLONG, .op = &mode_DI_op },
545 545 { "__DI__", NS_KEYWORD, MOD_LONGLONG, .op = &mode_DI_op },
546 546 { "TI", NS_KEYWORD, MOD_LONGLONGLONG, .op = &mode_TI_op },
547 547 { "__TI__", NS_KEYWORD, MOD_LONGLONGLONG, .op = &mode_TI_op },
548 548 { "word", NS_KEYWORD, MOD_LONG, .op = &mode_word_op },
549 549 { "__word__", NS_KEYWORD, MOD_LONG, .op = &mode_word_op },
550 550 };
551 551
552 552
553 553 static const char *ignored_attributes[] = {
554 554
555 555 #define GCC_ATTR(x) \
556 556 STRINGIFY(x), \
557 557 STRINGIFY(__##x##__),
558 558
559 559 #include "gcc-attr-list.h"
560 560
561 561 #undef GCC_ATTR
562 562
563 563 "bounded",
564 564 "__bounded__",
565 565 "__noclone",
566 566 "__nonnull",
567 567 "__nothrow",
568 568 };
569 569
570 570
571 571 void init_parser(int stream)
572 572 {
573 573 int i;
574 574 for (i = 0; i < ARRAY_SIZE(keyword_table); i++) {
575 575 struct init_keyword *ptr = keyword_table + i;
576 576 struct symbol *sym = create_symbol(stream, ptr->name, SYM_KEYWORD, ptr->ns);
577 577 sym->ident->keyword = 1;
578 578 if (ptr->ns == NS_TYPEDEF)
579 579 sym->ident->reserved = 1;
580 580 sym->ctype.modifiers = ptr->modifiers;
581 581 sym->ctype.base_type = ptr->type;
582 582 sym->op = ptr->op;
583 583 }
584 584
585 585 for (i = 0; i < ARRAY_SIZE(ignored_attributes); i++) {
586 586 const char * name = ignored_attributes[i];
587 587 struct symbol *sym = create_symbol(stream, name, SYM_KEYWORD,
588 588 NS_KEYWORD);
589 589 if (!sym->op) {
590 590 sym->ident->keyword = 1;
591 591 sym->op = &ignore_attr_op;
592 592 }
593 593 }
594 594 }
595 595
596 596
597 597 // Add a symbol to the list of function-local symbols
598 598 static void fn_local_symbol(struct symbol *sym)
599 599 {
600 600 if (function_symbol_list)
601 601 add_symbol(function_symbol_list, sym);
602 602 }
603 603
604 604 static int SENTINEL_ATTR match_idents(struct token *token, ...)
605 605 {
606 606 va_list args;
607 607 struct ident * next;
608 608
609 609 if (token_type(token) != TOKEN_IDENT)
610 610 return 0;
611 611
612 612 va_start(args, token);
613 613 do {
614 614 next = va_arg(args, struct ident *);
615 615 } while (next && token->ident != next);
616 616 va_end(args);
617 617
618 618 return next && token->ident == next;
619 619 }
620 620
621 621
622 622 struct statement *alloc_statement(struct position pos, int type)
623 623 {
624 624 struct statement *stmt = __alloc_statement(0);
625 625 stmt->type = type;
626 626 stmt->pos = pos;
627 627 return stmt;
628 628 }
629 629
630 630 static struct token *struct_declaration_list(struct token *token, struct symbol_list **list);
631 631
632 632 static void apply_ctype(struct position pos, struct ctype *thistype, struct ctype *ctype);
633 633
634 634 static void apply_modifiers(struct position pos, struct decl_state *ctx)
635 635 {
636 636 struct symbol *ctype;
637 637 if (!ctx->mode)
638 638 return;
639 639 ctype = ctx->mode->to_mode(ctx->ctype.base_type);
640 640 if (!ctype)
641 641 sparse_error(pos, "don't know how to apply mode to %s",
642 642 show_typename(ctx->ctype.base_type));
643 643 else
644 644 ctx->ctype.base_type = ctype;
645 645
646 646 }
647 647
648 648 static struct symbol * alloc_indirect_symbol(struct position pos, struct ctype *ctype, int type)
649 649 {
650 650 struct symbol *sym = alloc_symbol(pos, type);
651 651
652 652 sym->ctype.base_type = ctype->base_type;
653 653 sym->ctype.modifiers = ctype->modifiers;
654 654
655 655 ctype->base_type = sym;
656 656 ctype->modifiers = 0;
657 657 return sym;
658 658 }
659 659
660 660 /*
661 661 * NOTE! NS_LABEL is not just a different namespace,
662 662 * it also ends up using function scope instead of the
663 663 * regular symbol scope.
664 664 */
665 665 struct symbol *label_symbol(struct token *token)
666 666 {
667 667 struct symbol *sym = lookup_symbol(token->ident, NS_LABEL);
668 668 if (!sym) {
669 669 sym = alloc_symbol(token->pos, SYM_LABEL);
670 670 bind_symbol(sym, token->ident, NS_LABEL);
671 671 fn_local_symbol(sym);
672 672 }
673 673 return sym;
674 674 }
675 675
676 676 static struct token *struct_union_enum_specifier(enum type type,
677 677 struct token *token, struct decl_state *ctx,
678 678 struct token *(*parse)(struct token *, struct symbol *))
679 679 {
680 680 struct symbol *sym;
681 681 struct position *repos;
682 682
683 683 token = handle_attributes(token, ctx, KW_ATTRIBUTE);
684 684 if (token_type(token) == TOKEN_IDENT) {
685 685 sym = lookup_symbol(token->ident, NS_STRUCT);
686 686 if (!sym ||
687 687 (is_outer_scope(sym->scope) &&
688 688 (match_op(token->next,';') || match_op(token->next,'{')))) {
689 689 // Either a new symbol, or else an out-of-scope
690 690 // symbol being redefined.
691 691 sym = alloc_symbol(token->pos, type);
692 692 bind_symbol(sym, token->ident, NS_STRUCT);
693 693 }
694 694 if (sym->type != type)
695 695 error_die(token->pos, "invalid tag applied to %s", show_typename (sym));
696 696 ctx->ctype.base_type = sym;
697 697 repos = &token->pos;
698 698 token = token->next;
699 699 if (match_op(token, '{')) {
700 700 struct decl_state attr = { .ctype.base_type = sym, };
701 701
702 702 // The following test is actually wrong for empty
703 703 // structs, but (1) they are not C99, (2) gcc does
704 704 // the same thing, and (3) it's easier.
705 705 if (sym->symbol_list)
706 706 error_die(token->pos, "redefinition of %s", show_typename (sym));
707 707 sym->pos = *repos;
708 708 token = parse(token->next, sym);
709 709 token = expect(token, '}', "at end of struct-union-enum-specifier");
710 710
711 711 token = handle_attributes(token, &attr, KW_ATTRIBUTE);
712 712 apply_ctype(token->pos, &attr.ctype, &sym->ctype);
713 713
714 714 // Mark the structure as needing re-examination
715 715 sym->examined = 0;
716 716 sym->endpos = token->pos;
717 717 }
718 718 return token;
719 719 }
720 720
721 721 // private struct/union/enum type
722 722 if (!match_op(token, '{')) {
723 723 sparse_error(token->pos, "expected declaration");
724 724 ctx->ctype.base_type = &bad_ctype;
725 725 return token;
726 726 }
727 727
728 728 sym = alloc_symbol(token->pos, type);
729 729 token = parse(token->next, sym);
730 730 ctx->ctype.base_type = sym;
731 731 token = expect(token, '}', "at end of specifier");
732 732 sym->endpos = token->pos;
733 733
734 734 return token;
735 735 }
736 736
737 737 static struct token *parse_struct_declaration(struct token *token, struct symbol *sym)
738 738 {
739 739 struct symbol *field, *last = NULL;
740 740 struct token *res;
741 741 res = struct_declaration_list(token, &sym->symbol_list);
742 742 FOR_EACH_PTR(sym->symbol_list, field) {
743 743 if (!field->ident) {
744 744 struct symbol *base = field->ctype.base_type;
745 745 if (base && base->type == SYM_BITFIELD)
746 746 continue;
747 747 }
748 748 if (last)
749 749 last->next_subobject = field;
750 750 last = field;
751 751 } END_FOR_EACH_PTR(field);
752 752 return res;
753 753 }
754 754
755 755 static struct token *parse_union_declaration(struct token *token, struct symbol *sym)
756 756 {
757 757 return struct_declaration_list(token, &sym->symbol_list);
758 758 }
759 759
760 760 static struct token *struct_specifier(struct token *token, struct decl_state *ctx)
761 761 {
762 762 return struct_union_enum_specifier(SYM_STRUCT, token, ctx, parse_struct_declaration);
763 763 }
764 764
765 765 static struct token *union_specifier(struct token *token, struct decl_state *ctx)
766 766 {
767 767 return struct_union_enum_specifier(SYM_UNION, token, ctx, parse_union_declaration);
768 768 }
769 769
770 770
771 771 typedef struct {
772 772 int x;
773 773 unsigned long long y;
774 774 } Num;
775 775
776 776 static void upper_boundary(Num *n, Num *v)
777 777 {
778 778 if (n->x > v->x)
779 779 return;
780 780 if (n->x < v->x) {
781 781 *n = *v;
782 782 return;
783 783 }
784 784 if (n->y < v->y)
785 785 n->y = v->y;
786 786 }
787 787
788 788 static void lower_boundary(Num *n, Num *v)
789 789 {
790 790 if (n->x < v->x)
791 791 return;
792 792 if (n->x > v->x) {
793 793 *n = *v;
794 794 return;
795 795 }
796 796 if (n->y > v->y)
797 797 n->y = v->y;
798 798 }
799 799
800 800 static int type_is_ok(struct symbol *type, Num *upper, Num *lower)
801 801 {
802 802 int shift = type->bit_size;
803 803 int is_unsigned = type->ctype.modifiers & MOD_UNSIGNED;
804 804
805 805 if (!is_unsigned)
806 806 shift--;
807 807 if (upper->x == 0 && upper->y >> shift)
808 808 return 0;
809 809 if (lower->x == 0 || (!is_unsigned && (~lower->y >> shift) == 0))
810 810 return 1;
811 811 return 0;
812 812 }
813 813
814 814 static struct symbol *bigger_enum_type(struct symbol *s1, struct symbol *s2)
815 815 {
816 816 if (s1->bit_size < s2->bit_size) {
817 817 s1 = s2;
818 818 } else if (s1->bit_size == s2->bit_size) {
819 819 if (s2->ctype.modifiers & MOD_UNSIGNED)
820 820 s1 = s2;
821 821 }
822 822 if (s1->bit_size < bits_in_int)
823 823 return &int_ctype;
824 824 return s1;
825 825 }
826 826
827 827 static void cast_enum_list(struct symbol_list *list, struct symbol *base_type)
828 828 {
829 829 struct symbol *sym;
830 830
831 831 FOR_EACH_PTR(list, sym) {
832 832 struct expression *expr = sym->initializer;
833 833 struct symbol *ctype;
834 834 if (expr->type != EXPR_VALUE)
835 835 continue;
836 836 ctype = expr->ctype;
837 837 if (ctype->bit_size == base_type->bit_size)
838 838 continue;
839 839 cast_value(expr, base_type, expr, ctype);
840 840 } END_FOR_EACH_PTR(sym);
841 841 }
842 842
843 843 static struct token *parse_enum_declaration(struct token *token, struct symbol *parent)
844 844 {
845 845 unsigned long long lastval = 0;
846 846 struct symbol *ctype = NULL, *base_type = NULL;
847 847 Num upper = {-1, 0}, lower = {1, 0};
848 848
849 849 parent->examined = 1;
850 850 parent->ctype.base_type = &int_ctype;
851 851 while (token_type(token) == TOKEN_IDENT) {
852 852 struct expression *expr = NULL;
853 853 struct token *next = token->next;
854 854 struct symbol *sym;
855 855
856 856 if (match_op(next, '=')) {
857 857 next = constant_expression(next->next, &expr);
858 858 lastval = get_expression_value(expr);
859 859 ctype = &void_ctype;
860 860 if (expr && expr->ctype)
861 861 ctype = expr->ctype;
862 862 } else if (!ctype) {
863 863 ctype = &int_ctype;
864 864 } else if (is_int_type(ctype)) {
865 865 lastval++;
866 866 } else {
867 867 error_die(token->pos, "can't increment the last enum member");
868 868 }
869 869
870 870 if (!expr) {
871 871 expr = alloc_expression(token->pos, EXPR_VALUE);
872 872 expr->value = lastval;
873 873 expr->ctype = ctype;
874 874 }
875 875
876 876 sym = alloc_symbol(token->pos, SYM_NODE);
877 877 bind_symbol(sym, token->ident, NS_SYMBOL);
878 878 sym->ctype.modifiers &= ~MOD_ADDRESSABLE;
879 879 sym->initializer = expr;
880 880 sym->enum_member = 1;
881 881 sym->ctype.base_type = parent;
882 882 add_ptr_list(&parent->symbol_list, sym);
883 883
884 884 if (base_type != &bad_ctype) {
885 885 if (ctype->type == SYM_NODE)
886 886 ctype = ctype->ctype.base_type;
887 887 if (ctype->type == SYM_ENUM) {
888 888 if (ctype == parent)
889 889 ctype = base_type;
890 890 else
891 891 ctype = ctype->ctype.base_type;
892 892 }
893 893 /*
894 894 * base_type rules:
895 895 * - if all enums are of the same type, then
896 896 * the base_type is that type (two first
897 897 * cases)
898 898 * - if enums are of different types, they
899 899 * all have to be integer types, and the
900 900 * base type is at least "int_ctype".
901 901 * - otherwise the base_type is "bad_ctype".
902 902 */
903 903 if (!base_type) {
904 904 base_type = ctype;
905 905 } else if (ctype == base_type) {
906 906 /* nothing */
907 907 } else if (is_int_type(base_type) && is_int_type(ctype)) {
908 908 base_type = bigger_enum_type(base_type, ctype);
909 909 } else
910 910 base_type = &bad_ctype;
911 911 parent->ctype.base_type = base_type;
912 912 }
913 913 if (is_int_type(base_type)) {
914 914 Num v = {.y = lastval};
915 915 if (ctype->ctype.modifiers & MOD_UNSIGNED)
916 916 v.x = 0;
917 917 else if ((long long)lastval >= 0)
918 918 v.x = 0;
919 919 else
920 920 v.x = -1;
921 921 upper_boundary(&upper, &v);
922 922 lower_boundary(&lower, &v);
923 923 }
924 924 token = next;
925 925
926 926 sym->endpos = token->pos;
927 927
928 928 if (!match_op(token, ','))
929 929 break;
930 930 token = token->next;
931 931 }
932 932 if (!base_type) {
933 933 sparse_error(token->pos, "bad enum definition");
934 934 base_type = &bad_ctype;
935 935 }
936 936 else if (!is_int_type(base_type))
937 937 base_type = base_type;
938 938 else if (type_is_ok(base_type, &upper, &lower))
939 939 base_type = base_type;
940 940 else if (type_is_ok(&int_ctype, &upper, &lower))
941 941 base_type = &int_ctype;
942 942 else if (type_is_ok(&uint_ctype, &upper, &lower))
943 943 base_type = &uint_ctype;
944 944 else if (type_is_ok(&long_ctype, &upper, &lower))
945 945 base_type = &long_ctype;
946 946 else if (type_is_ok(&ulong_ctype, &upper, &lower))
947 947 base_type = &ulong_ctype;
948 948 else if (type_is_ok(&llong_ctype, &upper, &lower))
949 949 base_type = &llong_ctype;
950 950 else if (type_is_ok(&ullong_ctype, &upper, &lower))
951 951 base_type = &ullong_ctype;
952 952 else
953 953 base_type = &bad_ctype;
954 954 parent->ctype.base_type = base_type;
955 955 parent->ctype.modifiers |= (base_type->ctype.modifiers & MOD_UNSIGNED);
956 956 parent->examined = 0;
957 957
958 958 cast_enum_list(parent->symbol_list, base_type);
959 959
960 960 return token;
961 961 }
962 962
963 963 static struct token *enum_specifier(struct token *token, struct decl_state *ctx)
964 964 {
965 965 struct token *ret = struct_union_enum_specifier(SYM_ENUM, token, ctx, parse_enum_declaration);
966 966 struct ctype *ctype = &ctx->ctype.base_type->ctype;
967 967
968 968 if (!ctype->base_type)
969 969 ctype->base_type = &incomplete_ctype;
970 970
971 971 return ret;
972 972 }
973 973
974 974 static struct token *typeof_specifier(struct token *token, struct decl_state *ctx)
975 975 {
976 976 struct symbol *sym;
977 977
978 978 if (!match_op(token, '(')) {
979 979 sparse_error(token->pos, "expected '(' after typeof");
980 980 return token;
981 981 }
982 982 if (lookup_type(token->next)) {
983 983 token = typename(token->next, &sym, NULL);
984 984 ctx->ctype.base_type = sym->ctype.base_type;
985 985 apply_ctype(token->pos, &sym->ctype, &ctx->ctype);
986 986 } else {
987 987 struct symbol *typeof_sym = alloc_symbol(token->pos, SYM_TYPEOF);
988 988 token = parse_expression(token->next, &typeof_sym->initializer);
989 989
990 990 typeof_sym->endpos = token->pos;
991 991 if (!typeof_sym->initializer) {
992 992 sparse_error(token->pos, "expected expression after the '(' token");
993 993 typeof_sym = &bad_ctype;
994 994 }
995 995 ctx->ctype.base_type = typeof_sym;
996 996 }
997 997 return expect(token, ')', "after typeof");
998 998 }
999 999
1000 1000 static struct token *ignore_attribute(struct token *token, struct symbol *attr, struct decl_state *ctx)
1001 1001 {
1002 1002 struct expression *expr = NULL;
1003 1003 if (match_op(token, '('))
1004 1004 token = parens_expression(token, &expr, "in attribute");
1005 1005 return token;
1006 1006 }
1007 1007
1008 1008 static struct token *attribute_packed(struct token *token, struct symbol *attr, struct decl_state *ctx)
1009 1009 {
1010 1010 if (!ctx->ctype.alignment)
1011 1011 ctx->ctype.alignment = 1;
1012 1012 return token;
1013 1013 }
1014 1014
1015 1015 static struct token *attribute_aligned(struct token *token, struct symbol *attr, struct decl_state *ctx)
1016 1016 {
1017 1017 int alignment = max_alignment;
1018 1018 struct expression *expr = NULL;
1019 1019
1020 1020 if (match_op(token, '(')) {
1021 1021 token = parens_expression(token, &expr, "in attribute");
1022 1022 if (expr)
1023 1023 alignment = const_expression_value(expr);
1024 1024 }
1025 1025 if (alignment & (alignment-1)) {
1026 1026 warning(token->pos, "I don't like non-power-of-2 alignments");
1027 1027 return token;
1028 1028 } else if (alignment > ctx->ctype.alignment)
1029 1029 ctx->ctype.alignment = alignment;
1030 1030 return token;
1031 1031 }
1032 1032
1033 1033 static void apply_qualifier(struct position *pos, struct ctype *ctx, unsigned long qual)
1034 1034 {
1035 1035 if (ctx->modifiers & qual)
1036 1036 warning(*pos, "duplicate %s", modifier_string(qual));
1037 1037 ctx->modifiers |= qual;
1038 1038 }
1039 1039
1040 1040 static struct token *attribute_modifier(struct token *token, struct symbol *attr, struct decl_state *ctx)
1041 1041 {
1042 1042 apply_qualifier(&token->pos, &ctx->ctype, attr->ctype.modifiers);
1043 1043 return token;
1044 1044 }
1045 1045
1046 1046 static struct token *attribute_bitwise(struct token *token, struct symbol *attr, struct decl_state *ctx)
1047 1047 {
1048 1048 if (Wbitwise)
1049 1049 attribute_modifier(token, attr, ctx);
1050 1050 return token;
1051 1051 }
1052 1052
1053 1053 static struct token *attribute_address_space(struct token *token, struct symbol *attr, struct decl_state *ctx)
1054 1054 {
1055 1055 struct expression *expr = NULL;
1056 1056 int as;
1057 1057 token = expect(token, '(', "after address_space attribute");
1058 1058 token = conditional_expression(token, &expr);
1059 1059 if (expr) {
1060 1060 as = const_expression_value(expr);
1061 1061 if (Waddress_space && as)
1062 1062 ctx->ctype.as = as;
1063 1063 }
1064 1064 token = expect(token, ')', "after address_space attribute");
1065 1065 return token;
1066 1066 }
1067 1067
1068 1068 static struct symbol *to_QI_mode(struct symbol *ctype)
1069 1069 {
1070 1070 if (ctype->ctype.base_type != &int_type)
1071 1071 return NULL;
1072 1072 if (ctype == &char_ctype)
1073 1073 return ctype;
1074 1074 return ctype->ctype.modifiers & MOD_UNSIGNED ? &uchar_ctype
1075 1075 : &schar_ctype;
1076 1076 }
1077 1077
1078 1078 static struct symbol *to_HI_mode(struct symbol *ctype)
1079 1079 {
1080 1080 if (ctype->ctype.base_type != &int_type)
1081 1081 return NULL;
1082 1082 return ctype->ctype.modifiers & MOD_UNSIGNED ? &ushort_ctype
1083 1083 : &sshort_ctype;
1084 1084 }
1085 1085
1086 1086 static struct symbol *to_SI_mode(struct symbol *ctype)
1087 1087 {
1088 1088 if (ctype->ctype.base_type != &int_type)
1089 1089 return NULL;
1090 1090 return ctype->ctype.modifiers & MOD_UNSIGNED ? &uint_ctype
1091 1091 : &sint_ctype;
1092 1092 }
1093 1093
1094 1094 static struct symbol *to_DI_mode(struct symbol *ctype)
1095 1095 {
1096 1096 if (ctype->ctype.base_type != &int_type)
1097 1097 return NULL;
1098 1098 return ctype->ctype.modifiers & MOD_UNSIGNED ? &ullong_ctype
1099 1099 : &sllong_ctype;
1100 1100 }
1101 1101
1102 1102 static struct symbol *to_TI_mode(struct symbol *ctype)
1103 1103 {
1104 1104 if (ctype->ctype.base_type != &int_type)
1105 1105 return NULL;
1106 1106 return ctype->ctype.modifiers & MOD_UNSIGNED ? &ulllong_ctype
1107 1107 : &slllong_ctype;
1108 1108 }
1109 1109
1110 1110 static struct symbol *to_word_mode(struct symbol *ctype)
1111 1111 {
1112 1112 if (ctype->ctype.base_type != &int_type)
1113 1113 return NULL;
1114 1114 return ctype->ctype.modifiers & MOD_UNSIGNED ? &ulong_ctype
1115 1115 : &slong_ctype;
1116 1116 }
1117 1117
1118 1118 static struct token *attribute_mode(struct token *token, struct symbol *attr, struct decl_state *ctx)
1119 1119 {
1120 1120 token = expect(token, '(', "after mode attribute");
1121 1121 if (token_type(token) == TOKEN_IDENT) {
1122 1122 struct symbol *mode = lookup_keyword(token->ident, NS_KEYWORD);
1123 1123 if (mode && mode->op->type == KW_MODE)
1124 1124 ctx->mode = mode->op;
1125 1125 else
1126 1126 sparse_error(token->pos, "unknown mode attribute %s\n", show_ident(token->ident));
1127 1127 token = token->next;
1128 1128 } else
1129 1129 sparse_error(token->pos, "expect attribute mode symbol\n");
1130 1130 token = expect(token, ')', "after mode attribute");
1131 1131 return token;
1132 1132 }
1133 1133
1134 1134 static struct token *attribute_context(struct token *token, struct symbol *attr, struct decl_state *ctx)
1135 1135 {
1136 1136 struct context *context = alloc_context();
1137 1137 struct expression *args[3];
1138 1138 int argc = 0;
1139 1139
1140 1140 token = expect(token, '(', "after context attribute");
1141 1141 while (!match_op(token, ')')) {
1142 1142 struct expression *expr = NULL;
1143 1143 token = conditional_expression(token, &expr);
1144 1144 if (!expr)
1145 1145 break;
1146 1146 if (argc < 3)
1147 1147 args[argc++] = expr;
1148 1148 if (!match_op(token, ','))
1149 1149 break;
1150 1150 token = token->next;
1151 1151 }
1152 1152
1153 1153 switch(argc) {
1154 1154 case 0:
1155 1155 sparse_error(token->pos, "expected context input/output values");
1156 1156 break;
1157 1157 case 1:
1158 1158 context->in = get_expression_value(args[0]);
1159 1159 break;
1160 1160 case 2:
1161 1161 context->in = get_expression_value(args[0]);
1162 1162 context->out = get_expression_value(args[1]);
1163 1163 break;
1164 1164 case 3:
1165 1165 context->context = args[0];
1166 1166 context->in = get_expression_value(args[1]);
1167 1167 context->out = get_expression_value(args[2]);
1168 1168 break;
1169 1169 }
1170 1170
1171 1171 if (argc)
1172 1172 add_ptr_list(&ctx->ctype.contexts, context);
1173 1173
1174 1174 token = expect(token, ')', "after context attribute");
1175 1175 return token;
1176 1176 }
1177 1177
1178 1178 static struct token *attribute_designated_init(struct token *token, struct symbol *attr, struct decl_state *ctx)
1179 1179 {
1180 1180 if (ctx->ctype.base_type && ctx->ctype.base_type->type == SYM_STRUCT)
1181 1181 ctx->ctype.base_type->designated_init = 1;
1182 1182 else
1183 1183 warning(token->pos, "attribute designated_init applied to non-structure type");
1184 1184 return token;
1185 1185 }
1186 1186
1187 1187 static struct token *attribute_transparent_union(struct token *token, struct symbol *attr, struct decl_state *ctx)
1188 1188 {
1189 1189 if (Wtransparent_union)
1190 1190 warning(token->pos, "attribute __transparent_union__");
1191 1191
1192 1192 if (ctx->ctype.base_type && ctx->ctype.base_type->type == SYM_UNION)
1193 1193 ctx->ctype.base_type->transparent_union = 1;
1194 1194 else
1195 1195 warning(token->pos, "attribute __transparent_union__ applied to non-union type");
1196 1196 return token;
1197 1197 }
1198 1198
1199 1199 static struct token *recover_unknown_attribute(struct token *token)
1200 1200 {
1201 1201 struct expression *expr = NULL;
1202 1202
1203 1203 if (Wunknown_attribute)
1204 1204 warning(token->pos, "attribute '%s': unknown attribute", show_ident(token->ident));
1205 1205 token = token->next;
1206 1206 if (match_op(token, '('))
1207 1207 token = parens_expression(token, &expr, "in attribute");
1208 1208 return token;
1209 1209 }
1210 1210
1211 1211 static struct token *attribute_specifier(struct token *token, struct decl_state *ctx)
1212 1212 {
1213 1213 token = expect(token, '(', "after attribute");
1214 1214 token = expect(token, '(', "after attribute");
1215 1215
1216 1216 for (;;) {
1217 1217 struct ident *attribute_name;
1218 1218 struct symbol *attr;
1219 1219
1220 1220 if (eof_token(token))
1221 1221 break;
1222 1222 if (match_op(token, ';'))
1223 1223 break;
1224 1224 if (token_type(token) != TOKEN_IDENT)
1225 1225 break;
1226 1226 attribute_name = token->ident;
1227 1227 attr = lookup_keyword(attribute_name, NS_KEYWORD);
1228 1228 if (attr && attr->op->attribute)
1229 1229 token = attr->op->attribute(token->next, attr, ctx);
1230 1230 else
1231 1231 token = recover_unknown_attribute(token);
1232 1232
1233 1233 if (!match_op(token, ','))
1234 1234 break;
1235 1235 token = token->next;
1236 1236 }
1237 1237
1238 1238 token = expect(token, ')', "after attribute");
1239 1239 token = expect(token, ')', "after attribute");
1240 1240 return token;
1241 1241 }
1242 1242
1243 1243 static const char *storage_class[] =
1244 1244 {
1245 1245 [STypedef] = "typedef",
1246 1246 [SAuto] = "auto",
1247 1247 [SExtern] = "extern",
1248 1248 [SStatic] = "static",
1249 1249 [SRegister] = "register",
1250 1250 [SForced] = "[force]"
1251 1251 };
1252 1252
1253 1253 static unsigned long storage_modifiers(struct decl_state *ctx)
1254 1254 {
1255 1255 static unsigned long mod[SMax] =
1256 1256 {
1257 1257 [SAuto] = MOD_AUTO,
1258 1258 [SExtern] = MOD_EXTERN,
1259 1259 [SStatic] = MOD_STATIC,
1260 1260 [SRegister] = MOD_REGISTER
1261 1261 };
1262 1262 return mod[ctx->storage_class] | (ctx->is_inline ? MOD_INLINE : 0)
1263 1263 | (ctx->is_tls ? MOD_TLS : 0);
1264 1264 }
1265 1265
1266 1266 static void set_storage_class(struct position *pos, struct decl_state *ctx, int class)
1267 1267 {
1268 1268 /* __thread can be used alone, or with extern or static */
1269 1269 if (ctx->is_tls && (class != SStatic && class != SExtern)) {
1270 1270 sparse_error(*pos, "__thread can only be used alone, or with "
1271 1271 "extern or static");
1272 1272 return;
1273 1273 }
1274 1274
1275 1275 if (!ctx->storage_class) {
1276 1276 ctx->storage_class = class;
1277 1277 return;
1278 1278 }
1279 1279 if (ctx->storage_class == class)
1280 1280 sparse_error(*pos, "duplicate %s", storage_class[class]);
1281 1281 else
1282 1282 sparse_error(*pos, "multiple storage classes");
1283 1283 }
1284 1284
1285 1285 static struct token *typedef_specifier(struct token *next, struct decl_state *ctx)
1286 1286 {
1287 1287 set_storage_class(&next->pos, ctx, STypedef);
1288 1288 return next;
1289 1289 }
1290 1290
1291 1291 static struct token *auto_specifier(struct token *next, struct decl_state *ctx)
1292 1292 {
1293 1293 set_storage_class(&next->pos, ctx, SAuto);
1294 1294 return next;
1295 1295 }
1296 1296
1297 1297 static struct token *register_specifier(struct token *next, struct decl_state *ctx)
1298 1298 {
1299 1299 set_storage_class(&next->pos, ctx, SRegister);
1300 1300 return next;
1301 1301 }
1302 1302
1303 1303 static struct token *static_specifier(struct token *next, struct decl_state *ctx)
1304 1304 {
1305 1305 set_storage_class(&next->pos, ctx, SStatic);
1306 1306 return next;
1307 1307 }
1308 1308
1309 1309 static struct token *extern_specifier(struct token *next, struct decl_state *ctx)
1310 1310 {
1311 1311 set_storage_class(&next->pos, ctx, SExtern);
1312 1312 return next;
1313 1313 }
1314 1314
1315 1315 static struct token *thread_specifier(struct token *next, struct decl_state *ctx)
1316 1316 {
1317 1317 /* This GCC extension can be used alone, or with extern or static */
1318 1318 if (!ctx->storage_class || ctx->storage_class == SStatic
1319 1319 || ctx->storage_class == SExtern) {
1320 1320 ctx->is_tls = 1;
1321 1321 } else {
1322 1322 sparse_error(next->pos, "__thread can only be used alone, or "
1323 1323 "with extern or static");
1324 1324 }
1325 1325
1326 1326 return next;
1327 1327 }
1328 1328
1329 1329 static struct token *attribute_force(struct token *token, struct symbol *attr, struct decl_state *ctx)
1330 1330 {
1331 1331 set_storage_class(&token->pos, ctx, SForced);
1332 1332 return token;
1333 1333 }
1334 1334
1335 1335 static struct token *inline_specifier(struct token *next, struct decl_state *ctx)
1336 1336 {
1337 1337 ctx->is_inline = 1;
1338 1338 return next;
1339 1339 }
1340 1340
1341 1341 static struct token *noreturn_specifier(struct token *next, struct decl_state *ctx)
1342 1342 {
1343 1343 apply_qualifier(&next->pos, &ctx->ctype, MOD_NORETURN);
1344 1344 return next;
1345 1345 }
1346 1346
1347 1347 static struct token *alignas_specifier(struct token *token, struct decl_state *ctx)
1348 1348 {
1349 1349 int alignment = 0;
1350 1350
1351 1351 if (!match_op(token, '(')) {
1352 1352 sparse_error(token->pos, "expected '(' after _Alignas");
1353 1353 return token;
1354 1354 }
1355 1355 if (lookup_type(token->next)) {
1356 1356 struct symbol *sym = NULL;
1357 1357 token = typename(token->next, &sym, NULL);
1358 1358 sym = examine_symbol_type(sym);
1359 1359 alignment = sym->ctype.alignment;
1360 1360 token = expect(token, ')', "after _Alignas(...");
1361 1361 } else {
1362 1362 struct expression *expr = NULL;
1363 1363 token = parens_expression(token, &expr, "after _Alignas");
1364 1364 if (!expr)
1365 1365 return token;
1366 1366 alignment = const_expression_value(expr);
1367 1367 }
1368 1368
1369 1369 if (alignment < 0) {
1370 1370 warning(token->pos, "non-positive alignment");
1371 1371 return token;
1372 1372 }
1373 1373 if (alignment & (alignment-1)) {
1374 1374 warning(token->pos, "non-power-of-2 alignment");
1375 1375 return token;
1376 1376 }
1377 1377 if (alignment > ctx->ctype.alignment)
1378 1378 ctx->ctype.alignment = alignment;
1379 1379 return token;
1380 1380 }
1381 1381
1382 1382 static struct token *const_qualifier(struct token *next, struct decl_state *ctx)
1383 1383 {
1384 1384 apply_qualifier(&next->pos, &ctx->ctype, MOD_CONST);
1385 1385 return next;
1386 1386 }
1387 1387
1388 1388 static struct token *volatile_qualifier(struct token *next, struct decl_state *ctx)
1389 1389 {
1390 1390 apply_qualifier(&next->pos, &ctx->ctype, MOD_VOLATILE);
1391 1391 return next;
1392 1392 }
1393 1393
1394 1394 static void apply_ctype(struct position pos, struct ctype *thistype, struct ctype *ctype)
1395 1395 {
1396 1396 unsigned long mod = thistype->modifiers;
1397 1397
1398 1398 if (mod)
1399 1399 apply_qualifier(&pos, ctype, mod);
1400 1400
1401 1401 /* Context */
1402 1402 concat_ptr_list((struct ptr_list *)thistype->contexts,
1403 1403 (struct ptr_list **)&ctype->contexts);
1404 1404
1405 1405 /* Alignment */
1406 1406 if (thistype->alignment > ctype->alignment)
1407 1407 ctype->alignment = thistype->alignment;
1408 1408
1409 1409 /* Address space */
1410 1410 if (thistype->as)
1411 1411 ctype->as = thistype->as;
1412 1412 }
1413 1413
1414 1414 static void specifier_conflict(struct position pos, int what, struct ident *new)
1415 1415 {
1416 1416 const char *old;
1417 1417 if (what & (Set_S | Set_T))
1418 1418 goto Catch_all;
1419 1419 if (what & Set_Char)
1420 1420 old = "char";
1421 1421 else if (what & Set_Double)
1422 1422 old = "double";
1423 1423 else if (what & Set_Float)
1424 1424 old = "float";
1425 1425 else if (what & Set_Signed)
1426 1426 old = "signed";
1427 1427 else if (what & Set_Unsigned)
1428 1428 old = "unsigned";
1429 1429 else if (what & Set_Short)
1430 1430 old = "short";
1431 1431 else if (what & Set_Long)
1432 1432 old = "long";
1433 1433 else
1434 1434 old = "long long";
1435 1435 sparse_error(pos, "impossible combination of type specifiers: %s %s",
1436 1436 old, show_ident(new));
1437 1437 return;
1438 1438
1439 1439 Catch_all:
1440 1440 sparse_error(pos, "two or more data types in declaration specifiers");
1441 1441 }
1442 1442
1443 1443 static struct symbol * const int_types[] =
1444 1444 {&short_ctype, &int_ctype, &long_ctype, &llong_ctype, &lllong_ctype};
1445 1445 static struct symbol * const signed_types[] =
1446 1446 {&sshort_ctype, &sint_ctype, &slong_ctype, &sllong_ctype,
1447 1447 &slllong_ctype};
1448 1448 static struct symbol * const unsigned_types[] =
1449 1449 {&ushort_ctype, &uint_ctype, &ulong_ctype, &ullong_ctype,
1450 1450 &ulllong_ctype};
1451 1451 static struct symbol * const real_types[] =
1452 1452 {&float_ctype, &double_ctype, &ldouble_ctype};
1453 1453 static struct symbol * const char_types[] =
1454 1454 {&char_ctype, &schar_ctype, &uchar_ctype};
1455 1455 static struct symbol * const * const types[] = {
1456 1456 int_types + 1, signed_types + 1, unsigned_types + 1,
1457 1457 real_types + 1, char_types, char_types + 1, char_types + 2
1458 1458 };
1459 1459
1460 1460 struct symbol *ctype_integer(int size, int want_unsigned)
1461 1461 {
1462 1462 return types[want_unsigned ? CUInt : CInt][size];
1463 1463 }
1464 1464
1465 1465 static struct token *handle_qualifiers(struct token *t, struct decl_state *ctx)
1466 1466 {
1467 1467 while (token_type(t) == TOKEN_IDENT) {
1468 1468 struct symbol *s = lookup_symbol(t->ident, NS_TYPEDEF);
1469 1469 if (!s)
1470 1470 break;
1471 1471 if (s->type != SYM_KEYWORD)
1472 1472 break;
1473 1473 if (!(s->op->type & (KW_ATTRIBUTE | KW_QUALIFIER)))
1474 1474 break;
1475 1475 t = t->next;
1476 1476 if (s->op->declarator)
1477 1477 t = s->op->declarator(t, ctx);
1478 1478 }
1479 1479 return t;
1480 1480 }
1481 1481
1482 1482 static struct token *declaration_specifiers(struct token *token, struct decl_state *ctx)
1483 1483 {
1484 1484 int seen = 0;
1485 1485 int class = CInt;
1486 1486 int size = 0;
1487 1487
1488 1488 while (token_type(token) == TOKEN_IDENT) {
1489 1489 struct symbol *s = lookup_symbol(token->ident,
1490 1490 NS_TYPEDEF | NS_SYMBOL);
1491 1491 if (!s || !(s->namespace & NS_TYPEDEF))
1492 1492 break;
1493 1493 if (s->type != SYM_KEYWORD) {
1494 1494 if (seen & Set_Any)
1495 1495 break;
1496 1496 seen |= Set_S | Set_T;
1497 1497 ctx->ctype.base_type = s->ctype.base_type;
1498 1498 apply_ctype(token->pos, &s->ctype, &ctx->ctype);
1499 1499 token = token->next;
1500 1500 continue;
1501 1501 }
1502 1502 if (s->op->type & KW_SPECIFIER) {
1503 1503 if (seen & s->op->test) {
1504 1504 specifier_conflict(token->pos,
1505 1505 seen & s->op->test,
1506 1506 token->ident);
1507 1507 break;
1508 1508 }
1509 1509 seen |= s->op->set;
1510 1510 class += s->op->class;
1511 1511 if (s->op->set & Set_Int128)
1512 1512 size = 2;
1513 1513 if (s->op->type & KW_SHORT) {
1514 1514 size = -1;
1515 1515 } else if (s->op->type & KW_LONG && size++) {
1516 1516 if (class == CReal) {
1517 1517 specifier_conflict(token->pos,
1518 1518 Set_Vlong,
1519 1519 &double_ident);
1520 1520 break;
1521 1521 }
1522 1522 seen |= Set_Vlong;
1523 1523 }
1524 1524 }
1525 1525 token = token->next;
1526 1526 if (s->op->declarator)
1527 1527 token = s->op->declarator(token, ctx);
1528 1528 if (s->op->type & KW_EXACT) {
1529 1529 ctx->ctype.base_type = s->ctype.base_type;
1530 1530 ctx->ctype.modifiers |= s->ctype.modifiers;
1531 1531 }
1532 1532 }
1533 1533
1534 1534 if (!(seen & Set_S)) { /* not set explicitly? */
1535 1535 struct symbol *base = &incomplete_ctype;
1536 1536 if (seen & Set_Any)
1537 1537 base = types[class][size];
1538 1538 ctx->ctype.base_type = base;
1539 1539 }
1540 1540
1541 1541 if (ctx->ctype.modifiers & MOD_BITWISE) {
1542 1542 struct symbol *type;
1543 1543 ctx->ctype.modifiers &= ~MOD_BITWISE;
1544 1544 if (!is_int_type(ctx->ctype.base_type)) {
1545 1545 sparse_error(token->pos, "invalid modifier");
1546 1546 return token;
1547 1547 }
1548 1548 type = alloc_symbol(token->pos, SYM_BASETYPE);
1549 1549 *type = *ctx->ctype.base_type;
1550 1550 type->ctype.modifiers &= ~MOD_SPECIFIER;
1551 1551 type->ctype.base_type = ctx->ctype.base_type;
1552 1552 type->type = SYM_RESTRICT;
1553 1553 ctx->ctype.base_type = type;
1554 1554 create_fouled(type);
1555 1555 }
1556 1556 return token;
1557 1557 }
1558 1558
1559 1559 static struct token *abstract_array_static_declarator(struct token *token, int *has_static)
1560 1560 {
1561 1561 while (token->ident == &static_ident) {
1562 1562 if (*has_static)
1563 1563 sparse_error(token->pos, "duplicate array static declarator");
1564 1564
1565 1565 *has_static = 1;
1566 1566 token = token->next;
1567 1567 }
1568 1568 return token;
1569 1569
1570 1570 }
1571 1571
1572 1572 static struct token *abstract_array_declarator(struct token *token, struct symbol *sym)
1573 1573 {
1574 1574 struct expression *expr = NULL;
1575 1575 int has_static = 0;
1576 1576
1577 1577 token = abstract_array_static_declarator(token, &has_static);
1578 1578
1579 1579 if (match_idents(token, &restrict_ident, &__restrict_ident, &__restrict___ident, NULL))
1580 1580 token = abstract_array_static_declarator(token->next, &has_static);
1581 1581 token = parse_expression(token, &expr);
1582 1582 sym->array_size = expr;
1583 1583 return token;
1584 1584 }
1585 1585
1586 1586 static struct token *parameter_type_list(struct token *, struct symbol *);
1587 1587 static struct token *identifier_list(struct token *, struct symbol *);
1588 1588 static struct token *declarator(struct token *token, struct decl_state *ctx);
1589 1589
1590 1590 static struct token *skip_attribute(struct token *token)
1591 1591 {
1592 1592 token = token->next;
1593 1593 if (match_op(token, '(')) {
1594 1594 int depth = 1;
1595 1595 token = token->next;
1596 1596 while (depth && !eof_token(token)) {
1597 1597 if (token_type(token) == TOKEN_SPECIAL) {
1598 1598 if (token->special == '(')
1599 1599 depth++;
1600 1600 else if (token->special == ')')
1601 1601 depth--;
1602 1602 }
1603 1603 token = token->next;
1604 1604 }
1605 1605 }
1606 1606 return token;
1607 1607 }
1608 1608
1609 1609 static struct token *skip_attributes(struct token *token)
1610 1610 {
1611 1611 struct symbol *keyword;
1612 1612 for (;;) {
1613 1613 if (token_type(token) != TOKEN_IDENT)
1614 1614 break;
1615 1615 keyword = lookup_keyword(token->ident, NS_KEYWORD | NS_TYPEDEF);
1616 1616 if (!keyword || keyword->type != SYM_KEYWORD)
1617 1617 break;
1618 1618 if (!(keyword->op->type & KW_ATTRIBUTE))
1619 1619 break;
1620 1620 token = expect(token->next, '(', "after attribute");
1621 1621 token = expect(token, '(', "after attribute");
1622 1622 for (;;) {
1623 1623 if (eof_token(token))
1624 1624 break;
1625 1625 if (match_op(token, ';'))
1626 1626 break;
1627 1627 if (token_type(token) != TOKEN_IDENT)
1628 1628 break;
1629 1629 token = skip_attribute(token);
1630 1630 if (!match_op(token, ','))
1631 1631 break;
1632 1632 token = token->next;
1633 1633 }
1634 1634 token = expect(token, ')', "after attribute");
1635 1635 token = expect(token, ')', "after attribute");
1636 1636 }
1637 1637 return token;
1638 1638 }
1639 1639
1640 1640 static struct token *handle_attributes(struct token *token, struct decl_state *ctx, unsigned int keywords)
1641 1641 {
1642 1642 struct symbol *keyword;
1643 1643 for (;;) {
1644 1644 if (token_type(token) != TOKEN_IDENT)
1645 1645 break;
1646 1646 keyword = lookup_keyword(token->ident, NS_KEYWORD | NS_TYPEDEF);
1647 1647 if (!keyword || keyword->type != SYM_KEYWORD)
1648 1648 break;
1649 1649 if (!(keyword->op->type & keywords))
1650 1650 break;
1651 1651 token = keyword->op->declarator(token->next, ctx);
1652 1652 keywords &= KW_ATTRIBUTE;
1653 1653 }
1654 1654 return token;
1655 1655 }
1656 1656
1657 1657 static int is_nested(struct token *token, struct token **p,
1658 1658 int prefer_abstract)
1659 1659 {
1660 1660 /*
1661 1661 * This can be either a parameter list or a grouping.
1662 1662 * For the direct (non-abstract) case, we know if must be
1663 1663 * a parameter list if we already saw the identifier.
1664 1664 * For the abstract case, we know if must be a parameter
1665 1665 * list if it is empty or starts with a type.
1666 1666 */
1667 1667 struct token *next = token->next;
1668 1668
1669 1669 *p = next = skip_attributes(next);
1670 1670
1671 1671 if (token_type(next) == TOKEN_IDENT) {
1672 1672 if (lookup_type(next))
1673 1673 return !prefer_abstract;
1674 1674 return 1;
1675 1675 }
1676 1676
1677 1677 if (match_op(next, ')') || match_op(next, SPECIAL_ELLIPSIS))
1678 1678 return 0;
1679 1679
1680 1680 return 1;
1681 1681 }
1682 1682
1683 1683 enum kind {
1684 1684 Empty, K_R, Proto, Bad_Func,
1685 1685 };
1686 1686
1687 1687 static enum kind which_func(struct token *token,
1688 1688 struct ident **n,
1689 1689 int prefer_abstract)
1690 1690 {
1691 1691 struct token *next = token->next;
1692 1692
1693 1693 if (token_type(next) == TOKEN_IDENT) {
1694 1694 if (lookup_type(next))
1695 1695 return Proto;
1696 1696 /* identifier list not in definition; complain */
1697 1697 if (prefer_abstract)
1698 1698 warning(token->pos,
1699 1699 "identifier list not in definition");
1700 1700 return K_R;
1701 1701 }
1702 1702
1703 1703 if (token_type(next) != TOKEN_SPECIAL)
1704 1704 return Bad_Func;
1705 1705
1706 1706 if (next->special == ')') {
1707 1707 /* don't complain about those */
1708 1708 if (!n || match_op(next->next, ';'))
1709 1709 if (!n || match_op(next->next, ';') || match_op(next->next, ','))
1710 1710 return Empty;
1711 1711 if (Wstrict_prototypes)
1712 1712 warning(next->pos,
1713 1713 "non-ANSI function declaration of function '%s'",
1714 1714 show_ident(*n));
1715 1715 return Empty;
1716 1716 }
1717 1717
1718 1718 if (next->special == SPECIAL_ELLIPSIS) {
1719 1719 warning(next->pos,
1720 1720 "variadic functions must have one named argument");
1721 1721 return Proto;
1722 1722 }
1723 1723
1724 1724 return Bad_Func;
1725 1725 }
1726 1726
1727 1727 static struct token *direct_declarator(struct token *token, struct decl_state *ctx)
1728 1728 {
1729 1729 struct ctype *ctype = &ctx->ctype;
1730 1730 struct token *next;
1731 1731 struct ident **p = ctx->ident;
1732 1732
1733 1733 if (ctx->ident && token_type(token) == TOKEN_IDENT) {
1734 1734 *ctx->ident = token->ident;
1735 1735 token = token->next;
1736 1736 } else if (match_op(token, '(') &&
1737 1737 is_nested(token, &next, ctx->prefer_abstract)) {
1738 1738 struct symbol *base_type = ctype->base_type;
1739 1739 if (token->next != next)
1740 1740 next = handle_attributes(token->next, ctx,
1741 1741 KW_ATTRIBUTE);
1742 1742 token = declarator(next, ctx);
1743 1743 token = expect(token, ')', "in nested declarator");
1744 1744 while (ctype->base_type != base_type)
1745 1745 ctype = &ctype->base_type->ctype;
1746 1746 p = NULL;
1747 1747 }
1748 1748
1749 1749 if (match_op(token, '(')) {
1750 1750 enum kind kind = which_func(token, p, ctx->prefer_abstract);
1751 1751 struct symbol *fn;
1752 1752 fn = alloc_indirect_symbol(token->pos, ctype, SYM_FN);
1753 1753 token = token->next;
1754 1754 if (kind == K_R)
1755 1755 token = identifier_list(token, fn);
1756 1756 else if (kind == Proto)
1757 1757 token = parameter_type_list(token, fn);
1758 1758 token = expect(token, ')', "in function declarator");
1759 1759 fn->endpos = token->pos;
1760 1760 return token;
1761 1761 }
1762 1762
1763 1763 while (match_op(token, '[')) {
1764 1764 struct symbol *array;
1765 1765 array = alloc_indirect_symbol(token->pos, ctype, SYM_ARRAY);
1766 1766 token = abstract_array_declarator(token->next, array);
1767 1767 token = expect(token, ']', "in abstract_array_declarator");
1768 1768 array->endpos = token->pos;
1769 1769 ctype = &array->ctype;
1770 1770 }
1771 1771 return token;
1772 1772 }
1773 1773
1774 1774 static struct token *pointer(struct token *token, struct decl_state *ctx)
1775 1775 {
1776 1776 while (match_op(token,'*')) {
1777 1777 struct symbol *ptr = alloc_symbol(token->pos, SYM_PTR);
1778 1778 ptr->ctype.modifiers = ctx->ctype.modifiers;
1779 1779 ptr->ctype.base_type = ctx->ctype.base_type;
1780 1780 ptr->ctype.as = ctx->ctype.as;
1781 1781 ptr->ctype.contexts = ctx->ctype.contexts;
1782 1782 ctx->ctype.modifiers = 0;
1783 1783 ctx->ctype.base_type = ptr;
1784 1784 ctx->ctype.as = 0;
1785 1785 ctx->ctype.contexts = NULL;
1786 1786 ctx->ctype.alignment = 0;
1787 1787
1788 1788 token = handle_qualifiers(token->next, ctx);
1789 1789 ctx->ctype.base_type->endpos = token->pos;
1790 1790 }
1791 1791 return token;
1792 1792 }
1793 1793
1794 1794 static struct token *declarator(struct token *token, struct decl_state *ctx)
1795 1795 {
1796 1796 token = pointer(token, ctx);
1797 1797 return direct_declarator(token, ctx);
1798 1798 }
1799 1799
1800 1800 static struct token *handle_bitfield(struct token *token, struct decl_state *ctx)
1801 1801 {
1802 1802 struct ctype *ctype = &ctx->ctype;
1803 1803 struct expression *expr;
1804 1804 struct symbol *bitfield;
1805 1805 long long width;
1806 1806
1807 1807 if (ctype->base_type != &int_type && !is_int_type(ctype->base_type)) {
1808 1808 sparse_error(token->pos, "invalid bitfield specifier for type %s.",
1809 1809 show_typename(ctype->base_type));
1810 1810 // Parse this to recover gracefully.
1811 1811 return conditional_expression(token->next, &expr);
1812 1812 }
1813 1813
1814 1814 bitfield = alloc_indirect_symbol(token->pos, ctype, SYM_BITFIELD);
1815 1815 token = conditional_expression(token->next, &expr);
1816 1816 width = const_expression_value(expr);
1817 1817 bitfield->bit_size = width;
1818 1818
1819 1819 if (width < 0 || width > INT_MAX) {
1820 1820 sparse_error(token->pos, "invalid bitfield width, %lld.", width);
1821 1821 width = -1;
1822 1822 } else if (*ctx->ident && width == 0) {
1823 1823 sparse_error(token->pos, "invalid named zero-width bitfield `%s'",
1824 1824 show_ident(*ctx->ident));
1825 1825 width = -1;
1826 1826 } else if (*ctx->ident) {
1827 1827 struct symbol *base_type = bitfield->ctype.base_type;
1828 1828 struct symbol *bitfield_type = base_type == &int_type ? bitfield : base_type;
1829 1829 int is_signed = !(bitfield_type->ctype.modifiers & MOD_UNSIGNED);
1830 1830 if (Wone_bit_signed_bitfield && width == 1 && is_signed) {
1831 1831 // Valid values are either {-1;0} or {0}, depending on integer
1832 1832 // representation. The latter makes for very efficient code...
1833 1833 sparse_error(token->pos, "dubious one-bit signed bitfield");
1834 1834 }
1835 1835 if (Wdefault_bitfield_sign &&
1836 1836 bitfield_type->type != SYM_ENUM &&
1837 1837 !(bitfield_type->ctype.modifiers & MOD_EXPLICITLY_SIGNED) &&
1838 1838 is_signed) {
1839 1839 // The sign of bitfields is unspecified by default.
1840 1840 warning(token->pos, "dubious bitfield without explicit `signed' or `unsigned'");
1841 1841 }
1842 1842 }
1843 1843 bitfield->bit_size = width;
1844 1844 bitfield->endpos = token->pos;
1845 1845 return token;
1846 1846 }
1847 1847
1848 1848 static struct token *declaration_list(struct token *token, struct symbol_list **list)
1849 1849 {
1850 1850 struct decl_state ctx = {.prefer_abstract = 0};
1851 1851 struct ctype saved;
1852 1852 unsigned long mod;
1853 1853
1854 1854 token = declaration_specifiers(token, &ctx);
1855 1855 mod = storage_modifiers(&ctx);
1856 1856 saved = ctx.ctype;
1857 1857 for (;;) {
1858 1858 struct symbol *decl = alloc_symbol(token->pos, SYM_NODE);
1859 1859 ctx.ident = &decl->ident;
1860 1860
1861 1861 token = declarator(token, &ctx);
1862 1862 if (match_op(token, ':'))
1863 1863 token = handle_bitfield(token, &ctx);
1864 1864
1865 1865 token = handle_attributes(token, &ctx, KW_ATTRIBUTE);
1866 1866 apply_modifiers(token->pos, &ctx);
1867 1867
1868 1868 decl->ctype = ctx.ctype;
1869 1869 decl->ctype.modifiers |= mod;
1870 1870 decl->endpos = token->pos;
1871 1871 add_symbol(list, decl);
1872 1872 if (!match_op(token, ','))
1873 1873 break;
1874 1874 token = token->next;
1875 1875 ctx.ctype = saved;
1876 1876 }
1877 1877 return token;
1878 1878 }
1879 1879
1880 1880 static struct token *struct_declaration_list(struct token *token, struct symbol_list **list)
1881 1881 {
1882 1882 while (!match_op(token, '}')) {
1883 1883 if (match_ident(token, &_Static_assert_ident)) {
1884 1884 token = parse_static_assert(token, NULL);
1885 1885 continue;
1886 1886 }
1887 1887 if (!match_op(token, ';'))
1888 1888 token = declaration_list(token, list);
1889 1889 if (!match_op(token, ';')) {
1890 1890 sparse_error(token->pos, "expected ; at end of declaration");
1891 1891 break;
1892 1892 }
1893 1893 token = token->next;
1894 1894 }
1895 1895 return token;
1896 1896 }
1897 1897
1898 1898 static struct token *parameter_declaration(struct token *token, struct symbol *sym)
1899 1899 {
1900 1900 struct decl_state ctx = {.prefer_abstract = 1};
1901 1901
1902 1902 token = declaration_specifiers(token, &ctx);
1903 1903 ctx.ident = &sym->ident;
1904 1904 token = declarator(token, &ctx);
1905 1905 token = handle_attributes(token, &ctx, KW_ATTRIBUTE);
1906 1906 apply_modifiers(token->pos, &ctx);
1907 1907 sym->ctype = ctx.ctype;
1908 1908 sym->ctype.modifiers |= storage_modifiers(&ctx);
1909 1909 sym->endpos = token->pos;
1910 1910 sym->forced_arg = ctx.storage_class == SForced;
1911 1911 return token;
1912 1912 }
1913 1913
1914 1914 struct token *typename(struct token *token, struct symbol **p, int *forced)
1915 1915 {
1916 1916 struct decl_state ctx = {.prefer_abstract = 1};
1917 1917 int class;
1918 1918 struct symbol *sym = alloc_symbol(token->pos, SYM_NODE);
1919 1919 *p = sym;
1920 1920 token = declaration_specifiers(token, &ctx);
1921 1921 token = declarator(token, &ctx);
1922 1922 apply_modifiers(token->pos, &ctx);
1923 1923 sym->ctype = ctx.ctype;
1924 1924 sym->endpos = token->pos;
1925 1925 class = ctx.storage_class;
1926 1926 if (forced) {
1927 1927 *forced = 0;
1928 1928 if (class == SForced) {
1929 1929 *forced = 1;
1930 1930 class = 0;
1931 1931 }
1932 1932 }
1933 1933 if (class)
1934 1934 warning(sym->pos, "storage class in typename (%s %s)",
1935 1935 storage_class[class], show_typename(sym));
1936 1936 return token;
1937 1937 }
1938 1938
1939 1939 static struct token *parse_underscore_Pragma(struct token *token)
1940 1940 {
1941 1941 struct token *next;
1942 1942
1943 1943 next = token->next;
1944 1944 if (!match_op(next, '('))
1945 1945 return next;
1946 1946 next = next->next;
1947 1947 if (next->pos.type != TOKEN_STRING)
1948 1948 return next;
1949 1949 next = next->next;
1950 1950 if (!match_op(next, ')'))
1951 1951 return next;
1952 1952 return next->next;
1953 1953 }
1954 1954
1955 1955 static struct token *expression_statement(struct token *token, struct expression **tree)
1956 1956 {
1957 1957 if (match_ident(token, &_Pragma_ident))
1958 1958 return parse_underscore_Pragma(token);
1959 1959
1960 1960 token = parse_expression(token, tree);
1961 1961 return expect(token, ';', "at end of statement");
1962 1962 }
1963 1963
1964 1964 static struct token *parse_asm_operands(struct token *token, struct statement *stmt,
1965 1965 struct expression_list **inout)
1966 1966 {
1967 1967 struct expression *expr;
1968 1968
1969 1969 /* Allow empty operands */
1970 1970 if (match_op(token->next, ':') || match_op(token->next, ')'))
1971 1971 return token->next;
1972 1972 do {
1973 1973 struct ident *ident = NULL;
1974 1974 if (match_op(token->next, '[') &&
1975 1975 token_type(token->next->next) == TOKEN_IDENT &&
1976 1976 match_op(token->next->next->next, ']')) {
1977 1977 ident = token->next->next->ident;
1978 1978 token = token->next->next->next;
1979 1979 }
1980 1980 add_expression(inout, (struct expression *)ident); /* UGGLEE!!! */
1981 1981 token = primary_expression(token->next, &expr);
1982 1982 add_expression(inout, expr);
1983 1983 token = parens_expression(token, &expr, "in asm parameter");
1984 1984 add_expression(inout, expr);
1985 1985 } while (match_op(token, ','));
1986 1986 return token;
1987 1987 }
1988 1988
1989 1989 static struct token *parse_asm_clobbers(struct token *token, struct statement *stmt,
1990 1990 struct expression_list **clobbers)
1991 1991 {
1992 1992 struct expression *expr;
1993 1993
1994 1994 do {
1995 1995 token = primary_expression(token->next, &expr);
1996 1996 if (expr)
1997 1997 add_expression(clobbers, expr);
1998 1998 } while (match_op(token, ','));
1999 1999 return token;
2000 2000 }
2001 2001
2002 2002 static struct token *parse_asm_labels(struct token *token, struct statement *stmt,
2003 2003 struct symbol_list **labels)
2004 2004 {
2005 2005 struct symbol *label;
2006 2006
2007 2007 do {
2008 2008 token = token->next; /* skip ':' and ',' */
2009 2009 if (token_type(token) != TOKEN_IDENT)
2010 2010 return token;
2011 2011 label = label_symbol(token);
2012 2012 add_symbol(labels, label);
2013 2013 token = token->next;
2014 2014 } while (match_op(token, ','));
2015 2015 return token;
2016 2016 }
2017 2017
2018 2018 static struct token *parse_asm_statement(struct token *token, struct statement *stmt)
2019 2019 {
2020 2020 int is_goto = 0;
2021 2021
2022 2022 token = token->next;
2023 2023 stmt->type = STMT_ASM;
2024 2024 if (match_idents(token, &__volatile___ident, &__volatile_ident, &volatile_ident, NULL)) {
2025 2025 token = token->next;
2026 2026 }
2027 2027 if (token_type(token) == TOKEN_IDENT && token->ident == &goto_ident) {
2028 2028 is_goto = 1;
2029 2029 token = token->next;
2030 2030 }
2031 2031 token = expect(token, '(', "after asm");
2032 2032 token = parse_expression(token, &stmt->asm_string);
2033 2033 if (match_op(token, ':'))
2034 2034 token = parse_asm_operands(token, stmt, &stmt->asm_outputs);
2035 2035 if (match_op(token, ':'))
2036 2036 token = parse_asm_operands(token, stmt, &stmt->asm_inputs);
2037 2037 if (match_op(token, ':'))
2038 2038 token = parse_asm_clobbers(token, stmt, &stmt->asm_clobbers);
2039 2039 if (is_goto && match_op(token, ':'))
2040 2040 token = parse_asm_labels(token, stmt, &stmt->asm_labels);
2041 2041 token = expect(token, ')', "after asm");
2042 2042 return expect(token, ';', "at end of asm-statement");
2043 2043 }
2044 2044
2045 2045 static struct token *parse_asm_declarator(struct token *token, struct decl_state *ctx)
2046 2046 {
2047 2047 struct expression *expr;
2048 2048 token = expect(token, '(', "after asm");
2049 2049 token = parse_expression(token->next, &expr);
2050 2050 token = expect(token, ')', "after asm");
2051 2051 return token;
2052 2052 }
2053 2053
2054 2054 static struct token *parse_static_assert(struct token *token, struct symbol_list **unused)
2055 2055 {
2056 2056 struct expression *cond = NULL, *message = NULL;
2057 2057
2058 2058 token = expect(token->next, '(', "after _Static_assert");
2059 2059 token = constant_expression(token, &cond);
2060 2060 if (!cond)
2061 2061 sparse_error(token->pos, "Expected constant expression");
2062 2062 token = expect(token, ',', "after conditional expression in _Static_assert");
2063 2063 token = parse_expression(token, &message);
2064 2064 if (!message || message->type != EXPR_STRING) {
2065 2065 struct position pos;
2066 2066
2067 2067 pos = message ? message->pos : token->pos;
2068 2068 sparse_error(pos, "bad or missing string literal");
2069 2069 cond = NULL;
2070 2070 }
2071 2071 token = expect(token, ')', "after diagnostic message in _Static_assert");
2072 2072
2073 2073 token = expect(token, ';', "after _Static_assert()");
2074 2074
2075 2075 if (cond && !const_expression_value(cond) && cond->type == EXPR_VALUE)
2076 2076 sparse_error(cond->pos, "static assertion failed: %s",
2077 2077 show_string(message->string));
2078 2078 return token;
2079 2079 }
2080 2080
2081 2081 /* Make a statement out of an expression */
2082 2082 static struct statement *make_statement(struct expression *expr)
2083 2083 {
2084 2084 struct statement *stmt;
2085 2085
2086 2086 if (!expr)
2087 2087 return NULL;
2088 2088 stmt = alloc_statement(expr->pos, STMT_EXPRESSION);
2089 2089 stmt->expression = expr;
2090 2090 return stmt;
2091 2091 }
2092 2092
2093 2093 /*
2094 2094 * All iterators have two symbols associated with them:
2095 2095 * the "continue" and "break" symbols, which are targets
2096 2096 * for continue and break statements respectively.
2097 2097 *
2098 2098 * They are in a special name-space, but they follow
2099 2099 * all the normal visibility rules, so nested iterators
2100 2100 * automatically work right.
2101 2101 */
2102 2102 static void start_iterator(struct statement *stmt)
2103 2103 {
2104 2104 struct symbol *cont, *brk;
2105 2105
2106 2106 start_symbol_scope(stmt->pos);
2107 2107 cont = alloc_symbol(stmt->pos, SYM_NODE);
2108 2108 bind_symbol(cont, &continue_ident, NS_ITERATOR);
2109 2109 brk = alloc_symbol(stmt->pos, SYM_NODE);
2110 2110 bind_symbol(brk, &break_ident, NS_ITERATOR);
2111 2111
2112 2112 stmt->type = STMT_ITERATOR;
2113 2113 stmt->iterator_break = brk;
2114 2114 stmt->iterator_continue = cont;
2115 2115 fn_local_symbol(brk);
2116 2116 fn_local_symbol(cont);
2117 2117 }
2118 2118
2119 2119 static void end_iterator(struct statement *stmt)
2120 2120 {
2121 2121 end_symbol_scope();
2122 2122 }
2123 2123
2124 2124 static struct statement *start_function(struct symbol *sym)
2125 2125 {
2126 2126 struct symbol *ret;
2127 2127 struct statement *stmt = alloc_statement(sym->pos, STMT_COMPOUND);
2128 2128
2129 2129 start_function_scope(sym->pos);
2130 2130 ret = alloc_symbol(sym->pos, SYM_NODE);
2131 2131 ret->ctype = sym->ctype.base_type->ctype;
2132 2132 ret->ctype.modifiers &= ~(MOD_STORAGE | MOD_CONST | MOD_VOLATILE | MOD_TLS | MOD_INLINE | MOD_ADDRESSABLE | MOD_NOCAST | MOD_NODEREF | MOD_ACCESSED | MOD_TOPLEVEL);
2133 2133 ret->ctype.modifiers |= (MOD_AUTO | MOD_REGISTER);
2134 2134 bind_symbol(ret, &return_ident, NS_ITERATOR);
2135 2135 stmt->ret = ret;
2136 2136 fn_local_symbol(ret);
2137 2137
2138 2138 // Currently parsed symbol for __func__/__FUNCTION__/__PRETTY_FUNCTION__
2139 2139 current_fn = sym;
2140 2140
2141 2141 return stmt;
2142 2142 }
2143 2143
2144 2144 static void end_function(struct symbol *sym)
2145 2145 {
2146 2146 current_fn = NULL;
2147 2147 end_function_scope();
2148 2148 }
2149 2149
2150 2150 /*
2151 2151 * A "switch()" statement, like an iterator, has a
2152 2152 * the "break" symbol associated with it. It works
2153 2153 * exactly like the iterator break - it's the target
2154 2154 * for any break-statements in scope, and means that
2155 2155 * "break" handling doesn't even need to know whether
2156 2156 * it's breaking out of an iterator or a switch.
2157 2157 *
2158 2158 * In addition, the "case" symbol is a marker for the
2159 2159 * case/default statements to find the switch statement
2160 2160 * that they are associated with.
2161 2161 */
2162 2162 static void start_switch(struct statement *stmt)
2163 2163 {
2164 2164 struct symbol *brk, *switch_case;
2165 2165
2166 2166 start_symbol_scope(stmt->pos);
2167 2167 brk = alloc_symbol(stmt->pos, SYM_NODE);
2168 2168 bind_symbol(brk, &break_ident, NS_ITERATOR);
2169 2169
2170 2170 switch_case = alloc_symbol(stmt->pos, SYM_NODE);
2171 2171 bind_symbol(switch_case, &case_ident, NS_ITERATOR);
2172 2172 switch_case->stmt = stmt;
2173 2173
2174 2174 stmt->type = STMT_SWITCH;
2175 2175 stmt->switch_break = brk;
2176 2176 stmt->switch_case = switch_case;
2177 2177
2178 2178 fn_local_symbol(brk);
2179 2179 fn_local_symbol(switch_case);
2180 2180 }
2181 2181
2182 2182 static void end_switch(struct statement *stmt)
2183 2183 {
2184 2184 if (!stmt->switch_case->symbol_list)
2185 2185 warning(stmt->pos, "switch with no cases");
2186 2186 end_symbol_scope();
2187 2187 }
2188 2188
2189 2189 static void add_case_statement(struct statement *stmt)
2190 2190 {
2191 2191 struct symbol *target = lookup_symbol(&case_ident, NS_ITERATOR);
2192 2192 struct symbol *sym;
2193 2193
2194 2194 if (!target) {
2195 2195 sparse_error(stmt->pos, "not in switch scope");
2196 2196 stmt->type = STMT_NONE;
2197 2197 return;
2198 2198 }
2199 2199 sym = alloc_symbol(stmt->pos, SYM_NODE);
2200 2200 add_symbol(&target->symbol_list, sym);
2201 2201 sym->stmt = stmt;
2202 2202 stmt->case_label = sym;
2203 2203 fn_local_symbol(sym);
2204 2204 }
2205 2205
2206 2206 static struct token *parse_return_statement(struct token *token, struct statement *stmt)
2207 2207 {
2208 2208 struct symbol *target = lookup_symbol(&return_ident, NS_ITERATOR);
2209 2209
2210 2210 if (!target)
2211 2211 error_die(token->pos, "internal error: return without a function target");
2212 2212 stmt->type = STMT_RETURN;
2213 2213 stmt->ret_target = target;
2214 2214 return expression_statement(token->next, &stmt->ret_value);
2215 2215 }
2216 2216
2217 2217 static void validate_for_loop_decl(struct symbol *sym)
2218 2218 {
2219 2219 unsigned long storage = sym->ctype.modifiers & MOD_STORAGE;
2220 2220
2221 2221 if (storage & ~(MOD_AUTO | MOD_REGISTER)) {
2222 2222 const char *name = show_ident(sym->ident);
2223 2223 sparse_error(sym->pos, "non-local var '%s' in for-loop initializer", name);
2224 2224 sym->ctype.modifiers &= ~MOD_STORAGE;
2225 2225 }
2226 2226 }
2227 2227
2228 2228 static struct token *parse_for_statement(struct token *token, struct statement *stmt)
2229 2229 {
2230 2230 struct symbol_list *syms;
2231 2231 struct expression *e1, *e2, *e3;
2232 2232 struct statement *iterator;
2233 2233
2234 2234 start_iterator(stmt);
2235 2235 token = expect(token->next, '(', "after 'for'");
2236 2236
2237 2237 syms = NULL;
2238 2238 e1 = NULL;
2239 2239 /* C99 variable declaration? */
2240 2240 if (lookup_type(token)) {
2241 2241 token = external_declaration(token, &syms, validate_for_loop_decl);
2242 2242 } else {
2243 2243 token = parse_expression(token, &e1);
2244 2244 token = expect(token, ';', "in 'for'");
2245 2245 }
2246 2246 token = parse_expression(token, &e2);
2247 2247 token = expect(token, ';', "in 'for'");
2248 2248 token = parse_expression(token, &e3);
2249 2249 token = expect(token, ')', "in 'for'");
2250 2250 token = statement(token, &iterator);
2251 2251
2252 2252 stmt->iterator_syms = syms;
2253 2253 stmt->iterator_pre_statement = make_statement(e1);
2254 2254 stmt->iterator_pre_condition = e2;
2255 2255 stmt->iterator_post_statement = make_statement(e3);
2256 2256 stmt->iterator_post_condition = NULL;
2257 2257 stmt->iterator_statement = iterator;
2258 2258 end_iterator(stmt);
2259 2259
2260 2260 return token;
2261 2261 }
2262 2262
2263 2263 static struct token *parse_while_statement(struct token *token, struct statement *stmt)
2264 2264 {
2265 2265 struct expression *expr;
2266 2266 struct statement *iterator;
2267 2267
2268 2268 start_iterator(stmt);
2269 2269 token = parens_expression(token->next, &expr, "after 'while'");
2270 2270 token = statement(token, &iterator);
2271 2271
2272 2272 stmt->iterator_pre_condition = expr;
2273 2273 stmt->iterator_post_condition = NULL;
2274 2274 stmt->iterator_statement = iterator;
2275 2275 end_iterator(stmt);
2276 2276
2277 2277 return token;
2278 2278 }
2279 2279
2280 2280 static struct token *parse_do_statement(struct token *token, struct statement *stmt)
2281 2281 {
2282 2282 struct expression *expr;
2283 2283 struct statement *iterator;
2284 2284
2285 2285 start_iterator(stmt);
2286 2286 token = statement(token->next, &iterator);
2287 2287 if (token_type(token) == TOKEN_IDENT && token->ident == &while_ident)
2288 2288 token = token->next;
2289 2289 else
2290 2290 sparse_error(token->pos, "expected 'while' after 'do'");
2291 2291 token = parens_expression(token, &expr, "after 'do-while'");
2292 2292
2293 2293 stmt->iterator_post_condition = expr;
2294 2294 stmt->iterator_statement = iterator;
2295 2295 end_iterator(stmt);
2296 2296
2297 2297 if (iterator && iterator->type != STMT_COMPOUND && Wdo_while)
2298 2298 warning(iterator->pos, "do-while statement is not a compound statement");
2299 2299
2300 2300 return expect(token, ';', "after statement");
2301 2301 }
2302 2302
2303 2303 static struct token *parse_if_statement(struct token *token, struct statement *stmt)
2304 2304 {
2305 2305 stmt->type = STMT_IF;
2306 2306 token = parens_expression(token->next, &stmt->if_conditional, "after if");
2307 2307 token = statement(token, &stmt->if_true);
2308 2308 if (token_type(token) != TOKEN_IDENT)
2309 2309 return token;
2310 2310 if (token->ident != &else_ident)
2311 2311 return token;
2312 2312 return statement(token->next, &stmt->if_false);
2313 2313 }
2314 2314
2315 2315 static inline struct token *case_statement(struct token *token, struct statement *stmt)
2316 2316 {
2317 2317 stmt->type = STMT_CASE;
2318 2318 token = expect(token, ':', "after default/case");
2319 2319 add_case_statement(stmt);
2320 2320 return statement(token, &stmt->case_statement);
2321 2321 }
2322 2322
2323 2323 static struct token *parse_case_statement(struct token *token, struct statement *stmt)
2324 2324 {
2325 2325 token = parse_expression(token->next, &stmt->case_expression);
2326 2326 if (match_op(token, SPECIAL_ELLIPSIS))
2327 2327 token = parse_expression(token->next, &stmt->case_to);
2328 2328 return case_statement(token, stmt);
2329 2329 }
2330 2330
2331 2331 static struct token *parse_default_statement(struct token *token, struct statement *stmt)
2332 2332 {
2333 2333 return case_statement(token->next, stmt);
2334 2334 }
2335 2335
2336 2336 static struct token *parse_loop_iterator(struct token *token, struct statement *stmt)
2337 2337 {
2338 2338 struct symbol *target = lookup_symbol(token->ident, NS_ITERATOR);
2339 2339 stmt->type = STMT_GOTO;
2340 2340 stmt->goto_label = target;
2341 2341 if (!target)
2342 2342 sparse_error(stmt->pos, "break/continue not in iterator scope");
2343 2343 return expect(token->next, ';', "at end of statement");
2344 2344 }
2345 2345
2346 2346 static struct token *parse_switch_statement(struct token *token, struct statement *stmt)
2347 2347 {
2348 2348 stmt->type = STMT_SWITCH;
2349 2349 start_switch(stmt);
2350 2350 token = parens_expression(token->next, &stmt->switch_expression, "after 'switch'");
2351 2351 token = statement(token, &stmt->switch_statement);
2352 2352 end_switch(stmt);
2353 2353 return token;
2354 2354 }
2355 2355
2356 2356 static struct token *parse_goto_statement(struct token *token, struct statement *stmt)
2357 2357 {
2358 2358 stmt->type = STMT_GOTO;
2359 2359 token = token->next;
2360 2360 if (match_op(token, '*')) {
2361 2361 token = parse_expression(token->next, &stmt->goto_expression);
2362 2362 add_statement(&function_computed_goto_list, stmt);
2363 2363 } else if (token_type(token) == TOKEN_IDENT) {
2364 2364 stmt->goto_label = label_symbol(token);
2365 2365 token = token->next;
2366 2366 } else {
2367 2367 sparse_error(token->pos, "Expected identifier or goto expression");
2368 2368 }
2369 2369 return expect(token, ';', "at end of statement");
2370 2370 }
2371 2371
2372 2372 static struct token *parse_context_statement(struct token *token, struct statement *stmt)
2373 2373 {
2374 2374 stmt->type = STMT_CONTEXT;
2375 2375 token = parse_expression(token->next, &stmt->expression);
2376 2376 if (stmt->expression->type == EXPR_PREOP
2377 2377 && stmt->expression->op == '('
2378 2378 && stmt->expression->unop->type == EXPR_COMMA) {
2379 2379 struct expression *expr;
2380 2380 expr = stmt->expression->unop;
2381 2381 stmt->context = expr->left;
2382 2382 stmt->expression = expr->right;
2383 2383 }
2384 2384 return expect(token, ';', "at end of statement");
2385 2385 }
2386 2386
2387 2387 static struct token *parse_range_statement(struct token *token, struct statement *stmt)
2388 2388 {
2389 2389 stmt->type = STMT_RANGE;
2390 2390 token = assignment_expression(token->next, &stmt->range_expression);
2391 2391 token = expect(token, ',', "after range expression");
2392 2392 token = assignment_expression(token, &stmt->range_low);
2393 2393 token = expect(token, ',', "after low range");
2394 2394 token = assignment_expression(token, &stmt->range_high);
2395 2395 return expect(token, ';', "after range statement");
2396 2396 }
2397 2397
2398 2398 static struct token *statement(struct token *token, struct statement **tree)
2399 2399 {
2400 2400 struct statement *stmt = alloc_statement(token->pos, STMT_NONE);
2401 2401
2402 2402 *tree = stmt;
2403 2403 if (token_type(token) == TOKEN_IDENT) {
2404 2404 struct symbol *s = lookup_keyword(token->ident, NS_KEYWORD);
2405 2405 if (s && s->op->statement)
2406 2406 return s->op->statement(token, stmt);
2407 2407
2408 2408 if (match_op(token->next, ':')) {
2409 2409 struct symbol *s = label_symbol(token);
2410 2410 stmt->type = STMT_LABEL;
2411 2411 stmt->label_identifier = s;
2412 2412 if (s->stmt)
2413 2413 sparse_error(stmt->pos, "label '%s' redefined", show_ident(token->ident));
2414 2414 s->stmt = stmt;
2415 2415 token = skip_attributes(token->next->next);
2416 2416 return statement(token, &stmt->label_statement);
2417 2417 }
2418 2418 }
2419 2419
2420 2420 if (match_op(token, '{')) {
2421 2421 stmt->type = STMT_COMPOUND;
2422 2422 start_symbol_scope(stmt->pos);
2423 2423 token = compound_statement(token->next, stmt);
2424 2424 end_symbol_scope();
2425 2425
2426 2426 return expect(token, '}', "at end of compound statement");
2427 2427 }
2428 2428
2429 2429 stmt->type = STMT_EXPRESSION;
2430 2430 return expression_statement(token, &stmt->expression);
2431 2431 }
2432 2432
2433 2433 /* gcc extension - __label__ ident-list; in the beginning of compound stmt */
2434 2434 static struct token *label_statement(struct token *token)
2435 2435 {
2436 2436 while (token_type(token) == TOKEN_IDENT) {
2437 2437 struct symbol *sym = alloc_symbol(token->pos, SYM_LABEL);
2438 2438 /* it's block-scope, but we want label namespace */
2439 2439 bind_symbol(sym, token->ident, NS_SYMBOL);
2440 2440 sym->namespace = NS_LABEL;
2441 2441 fn_local_symbol(sym);
2442 2442 token = token->next;
2443 2443 if (!match_op(token, ','))
2444 2444 break;
2445 2445 token = token->next;
2446 2446 }
2447 2447 return expect(token, ';', "at end of label declaration");
2448 2448 }
2449 2449
2450 2450 static struct token * statement_list(struct token *token, struct statement_list **list)
2451 2451 {
2452 2452 int seen_statement = 0;
2453 2453 while (token_type(token) == TOKEN_IDENT &&
2454 2454 token->ident == &__label___ident)
2455 2455 token = label_statement(token->next);
2456 2456 for (;;) {
2457 2457 struct statement * stmt;
2458 2458 if (eof_token(token))
2459 2459 break;
2460 2460 if (match_op(token, '}'))
2461 2461 break;
2462 2462 if (match_ident(token, &_Static_assert_ident)) {
2463 2463 token = parse_static_assert(token, NULL);
2464 2464 continue;
2465 2465 }
2466 2466 if (lookup_type(token)) {
2467 2467 if (seen_statement) {
2468 2468 warning(token->pos, "mixing declarations and code");
2469 2469 seen_statement = 0;
2470 2470 }
2471 2471 stmt = alloc_statement(token->pos, STMT_DECLARATION);
2472 2472 token = external_declaration(token, &stmt->declaration, NULL);
2473 2473 } else {
2474 2474 seen_statement = Wdeclarationafterstatement;
2475 2475 token = statement(token, &stmt);
2476 2476 }
2477 2477 add_statement(list, stmt);
2478 2478 }
2479 2479 return token;
2480 2480 }
2481 2481
2482 2482 static struct token *identifier_list(struct token *token, struct symbol *fn)
2483 2483 {
2484 2484 struct symbol_list **list = &fn->arguments;
2485 2485 for (;;) {
2486 2486 struct symbol *sym = alloc_symbol(token->pos, SYM_NODE);
2487 2487 sym->ident = token->ident;
2488 2488 token = token->next;
2489 2489 sym->endpos = token->pos;
2490 2490 sym->ctype.base_type = &incomplete_ctype;
2491 2491 add_symbol(list, sym);
2492 2492 if (!match_op(token, ',') ||
2493 2493 token_type(token->next) != TOKEN_IDENT ||
2494 2494 lookup_type(token->next))
2495 2495 break;
2496 2496 token = token->next;
2497 2497 }
2498 2498 return token;
2499 2499 }
2500 2500
2501 2501 static struct token *parameter_type_list(struct token *token, struct symbol *fn)
2502 2502 {
2503 2503 struct symbol_list **list = &fn->arguments;
2504 2504
2505 2505 for (;;) {
2506 2506 struct symbol *sym;
2507 2507
2508 2508 if (match_op(token, SPECIAL_ELLIPSIS)) {
2509 2509 fn->variadic = 1;
2510 2510 token = token->next;
2511 2511 break;
2512 2512 }
2513 2513
2514 2514 sym = alloc_symbol(token->pos, SYM_NODE);
2515 2515 token = parameter_declaration(token, sym);
2516 2516 if (sym->ctype.base_type == &void_ctype) {
2517 2517 /* Special case: (void) */
2518 2518 if (!*list && !sym->ident)
2519 2519 break;
2520 2520 warning(token->pos, "void parameter");
2521 2521 }
2522 2522 add_symbol(list, sym);
2523 2523 if (!match_op(token, ','))
2524 2524 break;
2525 2525 token = token->next;
2526 2526 }
2527 2527 return token;
2528 2528 }
2529 2529
2530 2530 struct token *compound_statement(struct token *token, struct statement *stmt)
2531 2531 {
2532 2532 token = statement_list(token, &stmt->stmts);
2533 2533 return token;
2534 2534 }
2535 2535
2536 2536 static struct expression *identifier_expression(struct token *token)
2537 2537 {
2538 2538 struct expression *expr = alloc_expression(token->pos, EXPR_IDENTIFIER);
2539 2539 expr->expr_ident = token->ident;
2540 2540 return expr;
2541 2541 }
2542 2542
2543 2543 static struct expression *index_expression(struct expression *from, struct expression *to)
2544 2544 {
2545 2545 int idx_from, idx_to;
2546 2546 struct expression *expr = alloc_expression(from->pos, EXPR_INDEX);
2547 2547
2548 2548 idx_from = const_expression_value(from);
2549 2549 idx_to = idx_from;
2550 2550 if (to) {
2551 2551 idx_to = const_expression_value(to);
2552 2552 if (idx_to < idx_from || idx_from < 0)
2553 2553 warning(from->pos, "nonsense array initializer index range");
2554 2554 }
2555 2555 expr->idx_from = idx_from;
2556 2556 expr->idx_to = idx_to;
2557 2557 return expr;
2558 2558 }
2559 2559
2560 2560 static struct token *single_initializer(struct expression **ep, struct token *token)
2561 2561 {
2562 2562 int expect_equal = 0;
2563 2563 struct token *next = token->next;
2564 2564 struct expression **tail = ep;
2565 2565 int nested;
2566 2566
2567 2567 *ep = NULL;
2568 2568
2569 2569 if ((token_type(token) == TOKEN_IDENT) && match_op(next, ':')) {
2570 2570 struct expression *expr = identifier_expression(token);
2571 2571 if (Wold_initializer)
2572 2572 warning(token->pos, "obsolete struct initializer, use C99 syntax");
2573 2573 token = initializer(&expr->ident_expression, next->next);
2574 2574 if (expr->ident_expression)
2575 2575 *ep = expr;
2576 2576 return token;
2577 2577 }
2578 2578
2579 2579 for (tail = ep, nested = 0; ; nested++, next = token->next) {
2580 2580 if (match_op(token, '.') && (token_type(next) == TOKEN_IDENT)) {
2581 2581 struct expression *expr = identifier_expression(next);
2582 2582 *tail = expr;
2583 2583 tail = &expr->ident_expression;
2584 2584 expect_equal = 1;
2585 2585 token = next->next;
2586 2586 } else if (match_op(token, '[')) {
2587 2587 struct expression *from = NULL, *to = NULL, *expr;
2588 2588 token = constant_expression(token->next, &from);
2589 2589 if (!from) {
2590 2590 sparse_error(token->pos, "Expected constant expression");
2591 2591 break;
2592 2592 }
2593 2593 if (match_op(token, SPECIAL_ELLIPSIS))
2594 2594 token = constant_expression(token->next, &to);
2595 2595 expr = index_expression(from, to);
2596 2596 *tail = expr;
2597 2597 tail = &expr->idx_expression;
2598 2598 token = expect(token, ']', "at end of initializer index");
2599 2599 if (nested)
2600 2600 expect_equal = 1;
2601 2601 } else {
2602 2602 break;
2603 2603 }
2604 2604 }
2605 2605 if (nested && !expect_equal) {
2606 2606 if (!match_op(token, '='))
2607 2607 warning(token->pos, "obsolete array initializer, use C99 syntax");
2608 2608 else
2609 2609 expect_equal = 1;
2610 2610 }
2611 2611 if (expect_equal)
2612 2612 token = expect(token, '=', "at end of initializer index");
2613 2613
2614 2614 token = initializer(tail, token);
2615 2615 if (!*tail)
2616 2616 *ep = NULL;
2617 2617 return token;
2618 2618 }
2619 2619
2620 2620 static struct token *initializer_list(struct expression_list **list, struct token *token)
2621 2621 {
2622 2622 struct expression *expr;
2623 2623
2624 2624 for (;;) {
2625 2625 token = single_initializer(&expr, token);
2626 2626 if (!expr)
2627 2627 break;
2628 2628 add_expression(list, expr);
2629 2629 if (!match_op(token, ','))
2630 2630 break;
2631 2631 token = token->next;
2632 2632 }
2633 2633 return token;
2634 2634 }
2635 2635
2636 2636 struct token *initializer(struct expression **tree, struct token *token)
2637 2637 {
2638 2638 if (match_op(token, '{')) {
2639 2639 struct expression *expr = alloc_expression(token->pos, EXPR_INITIALIZER);
2640 2640 *tree = expr;
2641 2641 token = initializer_list(&expr->expr_list, token->next);
2642 2642 return expect(token, '}', "at end of initializer");
2643 2643 }
2644 2644 return assignment_expression(token, tree);
2645 2645 }
2646 2646
2647 2647 static void declare_argument(struct symbol *sym, struct symbol *fn)
2648 2648 {
2649 2649 if (!sym->ident) {
2650 2650 sparse_error(sym->pos, "no identifier for function argument");
2651 2651 return;
2652 2652 }
2653 2653 bind_symbol(sym, sym->ident, NS_SYMBOL);
2654 2654 }
2655 2655
2656 2656 static int is_syscall(struct symbol *sym)
2657 2657 {
2658 2658 char *macro;
2659 2659 char *name;
2660 2660 int is_syscall = 0;
2661 2661
2662 2662 macro = get_macro_name(sym->pos);
2663 2663 if (macro &&
2664 2664 (strncmp("SYSCALL_DEFINE", macro, strlen("SYSCALL_DEFINE")) == 0 ||
2665 2665 strncmp("COMPAT_SYSCALL_DEFINE", macro, strlen("COMPAT_SYSCALL_DEFINE")) == 0))
2666 2666 is_syscall = 1;
2667 2667
2668 2668 name = sym->ident->name;
2669 2669
2670 2670 if (name && strncmp(name, "sys_", 4) ==0)
2671 2671 is_syscall = 1;
2672 2672
2673 2673 if (name && strncmp(name, "compat_sys_", 11) == 0)
2674 2674 is_syscall = 1;
2675 2675
2676 2676 return is_syscall;
2677 2677 }
2678 2678
2679 2679
2680 2680 static struct token *parse_function_body(struct token *token, struct symbol *decl,
2681 2681 struct symbol_list **list)
2682 2682 {
2683 2683 struct symbol_list **old_symbol_list;
2684 2684 struct symbol *base_type = decl->ctype.base_type;
2685 2685 struct statement *stmt, **p;
2686 2686 struct symbol *prev;
2687 2687 struct symbol *arg;
2688 2688
2689 2689 old_symbol_list = function_symbol_list;
2690 2690 if (decl->ctype.modifiers & MOD_INLINE) {
2691 2691 function_symbol_list = &decl->inline_symbol_list;
2692 2692 p = &base_type->inline_stmt;
2693 2693 } else {
2694 2694 function_symbol_list = &decl->symbol_list;
2695 2695 p = &base_type->stmt;
2696 2696 }
2697 2697 function_computed_target_list = NULL;
2698 2698 function_computed_goto_list = NULL;
2699 2699
2700 2700 if (decl->ctype.modifiers & MOD_EXTERN &&
2701 2701 !(decl->ctype.modifiers & MOD_INLINE) &&
2702 2702 Wexternal_function_has_definition)
2703 2703 warning(decl->pos, "function '%s' with external linkage has definition", show_ident(decl->ident));
2704 2704
2705 2705 if (!(decl->ctype.modifiers & MOD_STATIC))
2706 2706 decl->ctype.modifiers |= MOD_EXTERN;
2707 2707
2708 2708 stmt = start_function(decl);
2709 2709
2710 2710 *p = stmt;
2711 2711 FOR_EACH_PTR (base_type->arguments, arg) {
2712 2712 declare_argument(arg, base_type);
2713 2713 } END_FOR_EACH_PTR(arg);
2714 2714
2715 2715 token = compound_statement(token->next, stmt);
2716 2716
2717 2717 end_function(decl);
2718 2718 if (!(decl->ctype.modifiers & MOD_INLINE))
2719 2719 add_symbol(list, decl);
2720 2720 else if (is_syscall(decl)) {
2721 2721 add_symbol(list, decl);
2722 2722 /*
2723 2723 printf("parse.c decl: %s\n", decl->ident->name);
2724 2724 char *macro = get_macro_name(decl->pos);
2725 2725 printf("decl macro: %s\n", macro);
2726 2726 */
2727 2727 }
2728 2728 check_declaration(decl);
2729 2729 decl->definition = decl;
2730 2730 prev = decl->same_symbol;
2731 2731 if (prev && prev->definition) {
2732 2732 warning(decl->pos, "multiple definitions for function '%s'",
2733 2733 show_ident(decl->ident));
2734 2734 info(prev->definition->pos, " the previous one is here");
2735 2735 } else {
2736 2736 while (prev) {
2737 2737 rebind_scope(prev, decl->scope);
2738 2738 prev->definition = decl;
2739 2739 prev = prev->same_symbol;
2740 2740 }
2741 2741 }
2742 2742 function_symbol_list = old_symbol_list;
2743 2743 if (function_computed_goto_list) {
2744 2744 if (!function_computed_target_list)
2745 2745 warning(decl->pos, "function '%s' has computed goto but no targets?", show_ident(decl->ident));
2746 2746 else {
2747 2747 FOR_EACH_PTR(function_computed_goto_list, stmt) {
2748 2748 stmt->target_list = function_computed_target_list;
2749 2749 } END_FOR_EACH_PTR(stmt);
2750 2750 }
2751 2751 }
2752 2752 return expect(token, '}', "at end of function");
2753 2753 }
2754 2754
2755 2755 static void promote_k_r_types(struct symbol *arg)
2756 2756 {
2757 2757 struct symbol *base = arg->ctype.base_type;
2758 2758 if (base && base->ctype.base_type == &int_type && (base->ctype.modifiers & (MOD_CHAR | MOD_SHORT))) {
2759 2759 arg->ctype.base_type = &int_ctype;
2760 2760 }
2761 2761 }
2762 2762
2763 2763 static void apply_k_r_types(struct symbol_list *argtypes, struct symbol *fn)
2764 2764 {
2765 2765 struct symbol_list *real_args = fn->ctype.base_type->arguments;
2766 2766 struct symbol *arg;
2767 2767
2768 2768 FOR_EACH_PTR(real_args, arg) {
2769 2769 struct symbol *type;
2770 2770
2771 2771 /* This is quadratic in the number of arguments. We _really_ don't care */
2772 2772 FOR_EACH_PTR(argtypes, type) {
2773 2773 if (type->ident == arg->ident)
2774 2774 goto match;
2775 2775 } END_FOR_EACH_PTR(type);
2776 2776 if (Wimplicit_int) {
2777 2777 sparse_error(arg->pos, "missing type declaration for parameter '%s'",
2778 2778 show_ident(arg->ident));
2779 2779 }
2780 2780 continue;
2781 2781 match:
2782 2782 type->used = 1;
2783 2783 /* "char" and "short" promote to "int" */
2784 2784 promote_k_r_types(type);
2785 2785
2786 2786 arg->ctype = type->ctype;
2787 2787 } END_FOR_EACH_PTR(arg);
2788 2788
2789 2789 FOR_EACH_PTR(argtypes, arg) {
2790 2790 if (!arg->used)
2791 2791 warning(arg->pos, "nonsensical parameter declaration '%s'", show_ident(arg->ident));
2792 2792 } END_FOR_EACH_PTR(arg);
2793 2793
2794 2794 }
2795 2795
2796 2796 static struct token *parse_k_r_arguments(struct token *token, struct symbol *decl,
2797 2797 struct symbol_list **list)
2798 2798 {
2799 2799 struct symbol_list *args = NULL;
2800 2800
2801 2801 if (Wold_style_definition)
2802 2802 warning(token->pos, "non-ANSI definition of function '%s'", show_ident(decl->ident));
2803 2803
2804 2804 do {
2805 2805 token = declaration_list(token, &args);
2806 2806 if (!match_op(token, ';')) {
2807 2807 sparse_error(token->pos, "expected ';' at end of parameter declaration");
2808 2808 break;
2809 2809 }
2810 2810 token = token->next;
2811 2811 } while (lookup_type(token));
2812 2812
2813 2813 apply_k_r_types(args, decl);
2814 2814
2815 2815 if (!match_op(token, '{')) {
2816 2816 sparse_error(token->pos, "expected function body");
2817 2817 return token;
2818 2818 }
2819 2819 return parse_function_body(token, decl, list);
2820 2820 }
2821 2821
2822 2822 static struct token *toplevel_asm_declaration(struct token *token, struct symbol_list **list)
2823 2823 {
2824 2824 struct symbol *anon = alloc_symbol(token->pos, SYM_NODE);
2825 2825 struct symbol *fn = alloc_symbol(token->pos, SYM_FN);
2826 2826 struct statement *stmt;
2827 2827
2828 2828 anon->ctype.base_type = fn;
2829 2829 stmt = alloc_statement(token->pos, STMT_NONE);
2830 2830 fn->stmt = stmt;
2831 2831
2832 2832 token = parse_asm_statement(token, stmt);
2833 2833
2834 2834 add_symbol(list, anon);
2835 2835 return token;
2836 2836 }
2837 2837
2838 2838 struct token *external_declaration(struct token *token, struct symbol_list **list,
2839 2839 validate_decl_t validate_decl)
2840 2840 {
2841 2841 struct ident *ident = NULL;
2842 2842 struct symbol *decl;
2843 2843 struct decl_state ctx = { .ident = &ident };
2844 2844 struct ctype saved;
2845 2845 struct symbol *base_type;
2846 2846 unsigned long mod;
2847 2847 int is_typedef;
2848 2848
2849 2849 if (match_ident(token, &_Pragma_ident))
2850 2850 return parse_underscore_Pragma(token);
2851 2851
↓ open down ↓ |
2851 lines elided |
↑ open up ↑ |
2852 2852 /* Top-level inline asm or static assertion? */
2853 2853 if (token_type(token) == TOKEN_IDENT) {
2854 2854 struct symbol *s = lookup_keyword(token->ident, NS_KEYWORD);
2855 2855 if (s && s->op->toplevel)
2856 2856 return s->op->toplevel(token, list);
2857 2857 }
2858 2858
2859 2859 /* Parse declaration-specifiers, if any */
2860 2860 token = declaration_specifiers(token, &ctx);
2861 2861 mod = storage_modifiers(&ctx);
2862 + mod |= ctx.ctype.modifiers & MOD_NORETURN;
2862 2863 decl = alloc_symbol(token->pos, SYM_NODE);
2863 2864 /* Just a type declaration? */
2864 2865 if (match_op(token, ';')) {
2865 2866 apply_modifiers(token->pos, &ctx);
2866 2867 return token->next;
2867 2868 }
2868 2869
2869 2870 saved = ctx.ctype;
2870 2871 token = declarator(token, &ctx);
2871 2872 token = handle_attributes(token, &ctx, KW_ATTRIBUTE | KW_ASM);
2872 2873 apply_modifiers(token->pos, &ctx);
2873 2874
2874 2875 decl->ctype = ctx.ctype;
2875 2876 decl->ctype.modifiers |= mod;
2876 2877 decl->endpos = token->pos;
2877 2878
2878 2879 /* Just a type declaration? */
2879 2880 if (!ident) {
2880 2881 warning(token->pos, "missing identifier in declaration");
2881 2882 return expect(token, ';', "at the end of type declaration");
2882 2883 }
2883 2884
2884 2885 /* type define declaration? */
2885 2886 is_typedef = ctx.storage_class == STypedef;
2886 2887
2887 2888 /* Typedefs don't have meaningful storage */
2888 2889 if (is_typedef)
2889 2890 decl->ctype.modifiers |= MOD_USERTYPE;
2890 2891
2891 2892 bind_symbol(decl, ident, is_typedef ? NS_TYPEDEF: NS_SYMBOL);
2892 2893
2893 2894 base_type = decl->ctype.base_type;
2894 2895
2895 2896 if (is_typedef) {
2896 2897 if (base_type && !base_type->ident) {
2897 2898 switch (base_type->type) {
2898 2899 case SYM_STRUCT:
2899 2900 case SYM_UNION:
2900 2901 case SYM_ENUM:
2901 2902 case SYM_RESTRICT:
2902 2903 base_type->ident = ident;
2903 2904 break;
2904 2905 default:
2905 2906 break;
2906 2907 }
2907 2908 }
2908 2909 } else if (base_type && base_type->type == SYM_FN) {
2909 2910 if (base_type->ctype.base_type == &incomplete_ctype) {
2910 2911 warning(decl->pos, "'%s()' has implicit return type",
2911 2912 show_ident(decl->ident));
2912 2913 base_type->ctype.base_type = &int_ctype;
2913 2914 }
2914 2915 /* K&R argument declaration? */
2915 2916 if (lookup_type(token))
2916 2917 return parse_k_r_arguments(token, decl, list);
2917 2918 if (match_op(token, '{'))
2918 2919 return parse_function_body(token, decl, list);
2919 2920
2920 2921 if (!(decl->ctype.modifiers & MOD_STATIC))
2921 2922 decl->ctype.modifiers |= MOD_EXTERN;
2922 2923 } else if (base_type == &void_ctype && !(decl->ctype.modifiers & MOD_EXTERN)) {
2923 2924 sparse_error(token->pos, "void declaration");
2924 2925 }
2925 2926 if (base_type == &incomplete_ctype) {
2926 2927 warning(decl->pos, "'%s' has implicit type", show_ident(decl->ident));
2927 2928 decl->ctype.base_type = &int_ctype;;
2928 2929 }
2929 2930
2930 2931 for (;;) {
2931 2932 if (!is_typedef && match_op(token, '=')) {
2932 2933 token = initializer(&decl->initializer, token->next);
2933 2934 }
2934 2935 if (!is_typedef) {
2935 2936 if (validate_decl)
2936 2937 validate_decl(decl);
2937 2938
2938 2939 if (decl->initializer && decl->ctype.modifiers & MOD_EXTERN) {
2939 2940 warning(decl->pos, "symbol with external linkage has initializer");
2940 2941 decl->ctype.modifiers &= ~MOD_EXTERN;
2941 2942 }
2942 2943
2943 2944 if (!(decl->ctype.modifiers & (MOD_EXTERN | MOD_INLINE))) {
2944 2945 add_symbol(list, decl);
2945 2946 fn_local_symbol(decl);
2946 2947 }
2947 2948 }
2948 2949 check_declaration(decl);
2949 2950 if (decl->same_symbol) {
2950 2951 decl->definition = decl->same_symbol->definition;
2951 2952 decl->op = decl->same_symbol->op;
2952 2953 }
2953 2954
2954 2955 if (!match_op(token, ','))
2955 2956 break;
2956 2957
2957 2958 token = token->next;
2958 2959 ident = NULL;
2959 2960 decl = alloc_symbol(token->pos, SYM_NODE);
2960 2961 ctx.ctype = saved;
2961 2962 token = handle_attributes(token, &ctx, KW_ATTRIBUTE);
2962 2963 token = declarator(token, &ctx);
2963 2964 token = handle_attributes(token, &ctx, KW_ATTRIBUTE | KW_ASM);
2964 2965 apply_modifiers(token->pos, &ctx);
2965 2966 decl->ctype = ctx.ctype;
2966 2967 decl->ctype.modifiers |= mod;
2967 2968 decl->endpos = token->pos;
2968 2969 if (!ident) {
2969 2970 sparse_error(token->pos, "expected identifier name in type definition");
2970 2971 return token;
2971 2972 }
2972 2973
2973 2974 if (is_typedef)
2974 2975 decl->ctype.modifiers |= MOD_USERTYPE;
2975 2976
2976 2977 bind_symbol(decl, ident, is_typedef ? NS_TYPEDEF: NS_SYMBOL);
2977 2978
2978 2979 /* Function declarations are automatically extern unless specifically static */
2979 2980 base_type = decl->ctype.base_type;
2980 2981 if (!is_typedef && base_type && base_type->type == SYM_FN) {
2981 2982 if (!(decl->ctype.modifiers & MOD_STATIC))
2982 2983 decl->ctype.modifiers |= MOD_EXTERN;
2983 2984 }
2984 2985 }
2985 2986 return expect(token, ';', "at end of declaration");
2986 2987 }
↓ open down ↓ |
115 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX