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