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