2 * Stupid C parser, version 1e-6.
4 * Let's see how hard this is to do.
6 * Copyright (C) 2003 Transmeta Corp.
7 * 2003-2004 Linus Torvalds
8 * Copyright (C) 2004 Christopher Li
10 * Licensed under the Open Software License version 1.1
28 #include "expression.h"
31 static struct symbol_list
**function_symbol_list
;
32 struct symbol_list
*function_computed_target_list
;
33 struct statement_list
*function_computed_goto_list
;
35 static struct token
*statement(struct token
*token
, struct statement
**tree
);
36 static struct token
*handle_attributes(struct token
*token
, struct ctype
*ctype
, unsigned int keywords
);
38 static struct token
*struct_specifier(struct token
*token
, struct ctype
*ctype
);
39 static struct token
*union_specifier(struct token
*token
, struct ctype
*ctype
);
40 static struct token
*enum_specifier(struct token
*token
, struct ctype
*ctype
);
41 static struct token
*attribute_specifier(struct token
*token
, struct ctype
*ctype
);
42 static struct token
*typeof_specifier(struct token
*token
, struct ctype
*ctype
);
44 static struct token
*parse_if_statement(struct token
*token
, struct statement
*stmt
);
45 static struct token
*parse_return_statement(struct token
*token
, struct statement
*stmt
);
46 static struct token
*parse_loop_iterator(struct token
*token
, struct statement
*stmt
);
47 static struct token
*parse_default_statement(struct token
*token
, struct statement
*stmt
);
48 static struct token
*parse_case_statement(struct token
*token
, struct statement
*stmt
);
49 static struct token
*parse_switch_statement(struct token
*token
, struct statement
*stmt
);
50 static struct token
*parse_for_statement(struct token
*token
, struct statement
*stmt
);
51 static struct token
*parse_while_statement(struct token
*token
, struct statement
*stmt
);
52 static struct token
*parse_do_statement(struct token
*token
, struct statement
*stmt
);
53 static struct token
*parse_goto_statement(struct token
*token
, struct statement
*stmt
);
54 static struct token
*parse_context_statement(struct token
*token
, struct statement
*stmt
);
55 static struct token
*parse_range_statement(struct token
*token
, struct statement
*stmt
);
56 static struct token
*parse_asm_statement(struct token
*token
, struct statement
*stmt
);
57 static struct token
*toplevel_asm_declaration(struct token
*token
, struct symbol_list
**list
);
58 static struct token
*parse_asm_declarator(struct token
*token
, struct ctype
*ctype
);
61 static struct token
*attribute_packed(struct token
*token
, struct symbol
*attr
, struct ctype
*ctype
);
62 static struct token
*attribute_modifier(struct token
*token
, struct symbol
*attr
, struct ctype
*ctype
);
63 static struct token
*attribute_address_space(struct token
*token
, struct symbol
*attr
, struct ctype
*ctype
);
64 static struct token
*attribute_aligned(struct token
*token
, struct symbol
*attr
, struct ctype
*ctype
);
65 static struct token
*attribute_mode(struct token
*token
, struct symbol
*attr
, struct ctype
*ctype
);
66 static struct token
*attribute_context(struct token
*token
, struct symbol
*attr
, struct ctype
*ctype
);
67 static struct token
*attribute_transparent_union(struct token
*token
, struct symbol
*attr
, struct ctype
*ctype
);
68 static struct token
*ignore_attribute(struct token
*token
, struct symbol
*attr
, struct ctype
*ctype
);
71 static struct symbol_op modifier_op
= {
75 static struct symbol_op qualifier_op
= {
79 static struct symbol_op typeof_op
= {
81 .declarator
= typeof_specifier
,
84 static struct symbol_op attribute_op
= {
86 .declarator
= attribute_specifier
,
89 static struct symbol_op struct_op
= {
91 .declarator
= struct_specifier
,
94 static struct symbol_op union_op
= {
96 .declarator
= union_specifier
,
99 static struct symbol_op enum_op
= {
100 .type
= KW_SPECIFIER
,
101 .declarator
= enum_specifier
,
104 static struct symbol_op spec_op
= {
108 static struct symbol_op if_op
= {
109 .statement
= parse_if_statement
,
112 static struct symbol_op return_op
= {
113 .statement
= parse_return_statement
,
116 static struct symbol_op loop_iter_op
= {
117 .statement
= parse_loop_iterator
,
120 static struct symbol_op default_op
= {
121 .statement
= parse_default_statement
,
124 static struct symbol_op case_op
= {
125 .statement
= parse_case_statement
,
128 static struct symbol_op switch_op
= {
129 .statement
= parse_switch_statement
,
132 static struct symbol_op for_op
= {
133 .statement
= parse_for_statement
,
136 static struct symbol_op while_op
= {
137 .statement
= parse_while_statement
,
140 static struct symbol_op do_op
= {
141 .statement
= parse_do_statement
,
144 static struct symbol_op goto_op
= {
145 .statement
= parse_goto_statement
,
148 static struct symbol_op __context___op
= {
149 .statement
= parse_context_statement
,
152 static struct symbol_op range_op
= {
153 .statement
= parse_range_statement
,
156 static struct symbol_op asm_op
= {
158 .declarator
= parse_asm_declarator
,
159 .statement
= parse_asm_statement
,
160 .toplevel
= toplevel_asm_declaration
,
163 static struct symbol_op packed_op
= {
164 .attribute
= attribute_packed
,
167 static struct symbol_op aligned_op
= {
168 .attribute
= attribute_aligned
,
171 static struct symbol_op attr_mod_op
= {
172 .attribute
= attribute_modifier
,
175 static struct symbol_op address_space_op
= {
176 .attribute
= attribute_address_space
,
179 static struct symbol_op mode_op
= {
180 .attribute
= attribute_mode
,
183 static struct symbol_op context_op
= {
184 .attribute
= attribute_context
,
187 static struct symbol_op transparent_union_op
= {
188 .attribute
= attribute_transparent_union
,
191 static struct symbol_op ignore_attr_op
= {
192 .attribute
= ignore_attribute
,
195 static struct symbol_op mode_spec_op
= {
199 static struct init_keyword
{
202 unsigned long modifiers
;
203 struct symbol_op
*op
;
205 } keyword_table
[] = {
206 /* Type qualifiers */
207 { "const", NS_TYPEDEF
, MOD_CONST
, .op
= &qualifier_op
},
208 { "__const", NS_TYPEDEF
, MOD_CONST
, .op
= &qualifier_op
},
209 { "__const__", NS_TYPEDEF
, MOD_CONST
, .op
= &qualifier_op
},
210 { "volatile", NS_TYPEDEF
, MOD_VOLATILE
, .op
= &qualifier_op
},
211 { "__volatile", NS_TYPEDEF
, MOD_VOLATILE
, .op
= &qualifier_op
},
212 { "__volatile__", NS_TYPEDEF
, MOD_VOLATILE
, .op
= &qualifier_op
},
215 { "typedef", NS_TYPEDEF
, MOD_TYPEDEF
, .op
= &modifier_op
},
217 /* Type specifiers */
218 { "void", NS_TYPEDEF
, .type
= &void_ctype
, .op
= &spec_op
},
219 { "char", NS_TYPEDEF
, MOD_CHAR
, .op
= &spec_op
},
220 { "short", NS_TYPEDEF
, MOD_SHORT
, .op
= &spec_op
},
221 { "int", NS_TYPEDEF
, .type
= &int_type
, .op
= &spec_op
},
222 { "long", NS_TYPEDEF
, MOD_LONG
, .op
= &spec_op
},
223 { "float", NS_TYPEDEF
, .type
= &fp_type
, .op
= &spec_op
},
224 { "double", NS_TYPEDEF
, MOD_LONG
, .type
= &fp_type
, .op
= &spec_op
},
225 { "signed", NS_TYPEDEF
, MOD_SIGNED
| MOD_EXPLICITLY_SIGNED
, .op
= &spec_op
},
226 { "__signed", NS_TYPEDEF
, MOD_SIGNED
| MOD_EXPLICITLY_SIGNED
, .op
= &spec_op
},
227 { "__signed__", NS_TYPEDEF
, MOD_SIGNED
| MOD_EXPLICITLY_SIGNED
, .op
= &spec_op
},
228 { "unsigned", NS_TYPEDEF
, MOD_UNSIGNED
, .op
= &spec_op
},
229 { "__label__", NS_TYPEDEF
, MOD_LABEL
| MOD_UNSIGNED
,
230 .type
=&label_ctype
, .op
= &spec_op
},
231 { "_Bool", NS_TYPEDEF
, MOD_UNSIGNED
, .type
= &bool_ctype
,
234 /* Predeclared types */
235 { "__builtin_va_list", NS_TYPEDEF
, .type
= &int_type
, .op
= &spec_op
},
238 { "typeof", NS_TYPEDEF
, .op
= &typeof_op
},
239 { "__typeof", NS_TYPEDEF
, .op
= &typeof_op
},
240 { "__typeof__", NS_TYPEDEF
, .op
= &typeof_op
},
242 { "__attribute", NS_TYPEDEF
, .op
= &attribute_op
},
243 { "__attribute__", NS_TYPEDEF
, .op
= &attribute_op
},
245 { "struct", NS_TYPEDEF
, .op
= &struct_op
},
246 { "union", NS_TYPEDEF
, .op
= &union_op
},
247 { "enum", NS_TYPEDEF
, .op
= &enum_op
},
249 { "inline", NS_TYPEDEF
, MOD_INLINE
, .op
= &modifier_op
},
250 { "__inline", NS_TYPEDEF
, MOD_INLINE
, .op
= &modifier_op
},
251 { "__inline__", NS_TYPEDEF
, MOD_INLINE
, .op
= &modifier_op
},
253 /* Ignored for now.. */
254 { "restrict", NS_TYPEDEF
, .op
= &qualifier_op
},
255 { "__restrict", NS_TYPEDEF
, .op
= &qualifier_op
},
258 { "auto", NS_TYPEDEF
, MOD_AUTO
, .op
= &modifier_op
},
259 { "register", NS_TYPEDEF
, MOD_REGISTER
, .op
= &modifier_op
},
260 { "static", NS_TYPEDEF
, MOD_STATIC
, .op
= &modifier_op
},
261 { "extern", NS_TYPEDEF
, MOD_EXTERN
, .op
= &modifier_op
},
264 { "if", NS_KEYWORD
, .op
= &if_op
},
265 { "return", NS_KEYWORD
, .op
= &return_op
},
266 { "break", NS_KEYWORD
, .op
= &loop_iter_op
},
267 { "continue", NS_KEYWORD
, .op
= &loop_iter_op
},
268 { "default", NS_KEYWORD
, .op
= &default_op
},
269 { "case", NS_KEYWORD
, .op
= &case_op
},
270 { "switch", NS_KEYWORD
, .op
= &switch_op
},
271 { "for", NS_KEYWORD
, .op
= &for_op
},
272 { "while", NS_KEYWORD
, .op
= &while_op
},
273 { "do", NS_KEYWORD
, .op
= &do_op
},
274 { "goto", NS_KEYWORD
, .op
= &goto_op
},
275 { "__context__",NS_KEYWORD
, .op
= &__context___op
},
276 { "__range__", NS_KEYWORD
, .op
= &range_op
},
277 { "asm", NS_KEYWORD
, .op
= &asm_op
},
278 { "__asm", NS_KEYWORD
, .op
= &asm_op
},
279 { "__asm__", NS_KEYWORD
, .op
= &asm_op
},
282 { "packed", NS_KEYWORD
, .op
= &packed_op
},
283 { "__packed__", NS_KEYWORD
, .op
= &packed_op
},
284 { "aligned", NS_KEYWORD
, .op
= &aligned_op
},
285 { "__aligned__",NS_KEYWORD
, .op
= &aligned_op
},
286 { "nocast", NS_KEYWORD
, MOD_NOCAST
, .op
= &attr_mod_op
},
287 { "noderef", NS_KEYWORD
, MOD_NODEREF
, .op
= &attr_mod_op
},
288 { "safe", NS_KEYWORD
, MOD_SAFE
, .op
= &attr_mod_op
},
289 { "force", NS_KEYWORD
, MOD_FORCE
, .op
= &attr_mod_op
},
290 { "bitwise", NS_KEYWORD
, MOD_BITWISE
, .op
= &attr_mod_op
},
291 { "__bitwise__",NS_KEYWORD
, MOD_BITWISE
, .op
= &attr_mod_op
},
292 { "address_space",NS_KEYWORD
, .op
= &address_space_op
},
293 { "mode", NS_KEYWORD
, .op
= &mode_op
},
294 { "context", NS_KEYWORD
, .op
= &context_op
},
295 { "__transparent_union__", NS_KEYWORD
, .op
= &transparent_union_op
},
297 { "__mode__", NS_KEYWORD
, .op
= &mode_op
},
298 { "QI", NS_KEYWORD
, MOD_CHAR
, .op
= &mode_spec_op
},
299 { "__QI__", NS_KEYWORD
, MOD_CHAR
, .op
= &mode_spec_op
},
300 { "HI", NS_KEYWORD
, MOD_SHORT
, .op
= &mode_spec_op
},
301 { "__HI__", NS_KEYWORD
, MOD_SHORT
, .op
= &mode_spec_op
},
302 { "SI", NS_KEYWORD
, .op
= &mode_spec_op
},
303 { "__SI__", NS_KEYWORD
, .op
= &mode_spec_op
},
304 { "DI", NS_KEYWORD
, MOD_LONGLONG
, .op
= &mode_spec_op
},
305 { "__DI__", NS_KEYWORD
, MOD_LONGLONG
, .op
= &mode_spec_op
},
306 { "word", NS_KEYWORD
, MOD_LONG
, .op
= &mode_spec_op
},
307 { "__word__", NS_KEYWORD
, MOD_LONG
, .op
= &mode_spec_op
},
309 /* Ignored attributes */
310 { "nothrow", NS_KEYWORD
, .op
= &ignore_attr_op
},
311 { "__nothrow", NS_KEYWORD
, .op
= &ignore_attr_op
},
312 { "__nothrow__", NS_KEYWORD
, .op
= &ignore_attr_op
},
313 { "malloc", NS_KEYWORD
, .op
= &ignore_attr_op
},
314 { "__malloc__", NS_KEYWORD
, .op
= &ignore_attr_op
},
315 { "nonnull", NS_KEYWORD
, .op
= &ignore_attr_op
},
316 { "__nonnull", NS_KEYWORD
, .op
= &ignore_attr_op
},
317 { "__nonnull__", NS_KEYWORD
, .op
= &ignore_attr_op
},
318 { "format", NS_KEYWORD
, .op
= &ignore_attr_op
},
319 { "__format__", NS_KEYWORD
, .op
= &ignore_attr_op
},
320 { "format_arg", NS_KEYWORD
, .op
= &ignore_attr_op
},
321 { "__format_arg__", NS_KEYWORD
, .op
= &ignore_attr_op
},
322 { "section", NS_KEYWORD
, .op
= &ignore_attr_op
},
323 { "__section__",NS_KEYWORD
, .op
= &ignore_attr_op
},
324 { "unused", NS_KEYWORD
, .op
= &ignore_attr_op
},
325 { "__unused__", NS_KEYWORD
, .op
= &ignore_attr_op
},
326 { "const", NS_KEYWORD
, .op
= &ignore_attr_op
},
327 { "__const", NS_KEYWORD
, .op
= &ignore_attr_op
},
328 { "__const__", NS_KEYWORD
, .op
= &ignore_attr_op
},
329 { "noreturn", NS_KEYWORD
, .op
= &ignore_attr_op
},
330 { "__noreturn__", NS_KEYWORD
, .op
= &ignore_attr_op
},
331 { "no_instrument_function", NS_KEYWORD
, .op
= &ignore_attr_op
},
332 { "__no_instrument_function__", NS_KEYWORD
, .op
= &ignore_attr_op
},
333 { "sentinel", NS_KEYWORD
, .op
= &ignore_attr_op
},
334 { "__sentinel__", NS_KEYWORD
, .op
= &ignore_attr_op
},
335 { "regparm", NS_KEYWORD
, .op
= &ignore_attr_op
},
336 { "__regparm__", NS_KEYWORD
, .op
= &ignore_attr_op
},
337 { "weak", NS_KEYWORD
, .op
= &ignore_attr_op
},
338 { "__weak__", NS_KEYWORD
, .op
= &ignore_attr_op
},
339 { "alias", NS_KEYWORD
, .op
= &ignore_attr_op
},
340 { "__alias__", NS_KEYWORD
, .op
= &ignore_attr_op
},
341 { "pure", NS_KEYWORD
, .op
= &ignore_attr_op
},
342 { "__pure__", NS_KEYWORD
, .op
= &ignore_attr_op
},
343 { "always_inline", NS_KEYWORD
, .op
= &ignore_attr_op
},
344 { "__always_inline__", NS_KEYWORD
, .op
= &ignore_attr_op
},
345 { "syscall_linkage", NS_KEYWORD
, .op
= &ignore_attr_op
},
346 { "__syscall_linkage__", NS_KEYWORD
, .op
= &ignore_attr_op
},
347 { "visibility", NS_KEYWORD
, .op
= &ignore_attr_op
},
348 { "__visibility__", NS_KEYWORD
, .op
= &ignore_attr_op
},
349 { "deprecated", NS_KEYWORD
, .op
= &ignore_attr_op
},
350 { "__deprecated__", NS_KEYWORD
, .op
= &ignore_attr_op
},
351 { "noinline", NS_KEYWORD
, .op
= &ignore_attr_op
},
352 { "__noinline__", NS_KEYWORD
, .op
= &ignore_attr_op
},
353 { "used", NS_KEYWORD
, .op
= &ignore_attr_op
},
354 { "__used__", NS_KEYWORD
, .op
= &ignore_attr_op
},
355 { "warn_unused_result", NS_KEYWORD
, .op
= &ignore_attr_op
},
356 { "__warn_unused_result__", NS_KEYWORD
, .op
= &ignore_attr_op
},
357 { "model", NS_KEYWORD
, .op
= &ignore_attr_op
},
358 { "__model__", NS_KEYWORD
, .op
= &ignore_attr_op
},
359 { "cdecl", NS_KEYWORD
, .op
= &ignore_attr_op
},
360 { "__cdecl__", NS_KEYWORD
, .op
= &ignore_attr_op
},
361 { "stdcall", NS_KEYWORD
, .op
= &ignore_attr_op
},
362 { "__stdcall__", NS_KEYWORD
, .op
= &ignore_attr_op
},
363 { "fastcall", NS_KEYWORD
, .op
= &ignore_attr_op
},
364 { "__fastcall__", NS_KEYWORD
, .op
= &ignore_attr_op
},
365 { "dllimport", NS_KEYWORD
, .op
= &ignore_attr_op
},
366 { "__dllimport__", NS_KEYWORD
, .op
= &ignore_attr_op
},
367 { "dllexport", NS_KEYWORD
, .op
= &ignore_attr_op
},
368 { "__dllexport__", NS_KEYWORD
, .op
= &ignore_attr_op
},
369 { "constructor", NS_KEYWORD
, .op
= &ignore_attr_op
},
370 { "__constructor__", NS_KEYWORD
, .op
= &ignore_attr_op
},
371 { "destructor", NS_KEYWORD
, .op
= &ignore_attr_op
},
372 { "__destructor__", NS_KEYWORD
, .op
= &ignore_attr_op
},
373 { "cold", NS_KEYWORD
, .op
= &ignore_attr_op
},
374 { "__cold__", NS_KEYWORD
, .op
= &ignore_attr_op
},
375 { "hot", NS_KEYWORD
, .op
= &ignore_attr_op
},
376 { "__hot__", NS_KEYWORD
, .op
= &ignore_attr_op
},
379 void init_parser(int stream
)
382 for (i
= 0; i
< sizeof keyword_table
/sizeof keyword_table
[0]; i
++) {
383 struct init_keyword
*ptr
= keyword_table
+ i
;
384 struct symbol
*sym
= create_symbol(stream
, ptr
->name
, SYM_KEYWORD
, ptr
->ns
);
385 sym
->ident
->keyword
= 1;
386 if (ptr
->ns
== NS_TYPEDEF
)
387 sym
->ident
->reserved
= 1;
388 sym
->ctype
.modifiers
= ptr
->modifiers
;
389 sym
->ctype
.base_type
= ptr
->type
;
394 // Add a symbol to the list of function-local symbols
395 static void fn_local_symbol(struct symbol
*sym
)
397 if (function_symbol_list
)
398 add_symbol(function_symbol_list
, sym
);
401 static int SENTINEL_ATTR
match_idents(struct token
*token
, ...)
406 if (token_type(token
) != TOKEN_IDENT
)
409 va_start(args
, token
);
411 next
= va_arg(args
, struct ident
*);
412 } while (next
&& token
->ident
!= next
);
415 return next
&& token
->ident
== next
;
419 struct statement
*alloc_statement(struct position pos
, int type
)
421 struct statement
*stmt
= __alloc_statement(0);
427 static struct token
*struct_declaration_list(struct token
*token
, struct symbol_list
**list
);
429 static int apply_modifiers(struct position pos
, struct ctype
*ctype
)
433 while ((base
= ctype
->base_type
)) {
434 switch (base
->type
) {
440 ctype
= &base
->ctype
;
446 /* Turn the "virtual types" into real types with real sizes etc */
447 if (ctype
->base_type
== &int_type
) {
448 ctype
->base_type
= ctype_integer(ctype
->modifiers
);
449 ctype
->modifiers
&= ~MOD_SPECIFIER
;
450 } else if (ctype
->base_type
== &fp_type
) {
451 ctype
->base_type
= ctype_fp(ctype
->modifiers
);
452 ctype
->modifiers
&= ~MOD_SPECIFIER
;
455 if (ctype
->modifiers
& MOD_BITWISE
) {
457 ctype
->modifiers
&= ~(MOD_BITWISE
| MOD_SPECIFIER
);
458 if (!is_int_type(ctype
->base_type
)) {
459 sparse_error(pos
, "invalid modifier");
462 type
= alloc_symbol(pos
, SYM_BASETYPE
);
463 *type
= *ctype
->base_type
;
464 type
->ctype
.base_type
= ctype
->base_type
;
465 type
->type
= SYM_RESTRICT
;
466 type
->ctype
.modifiers
&= ~MOD_SPECIFIER
;
467 ctype
->base_type
= type
;
473 static struct symbol
* alloc_indirect_symbol(struct position pos
, struct ctype
*ctype
, int type
)
475 struct symbol
*sym
= alloc_symbol(pos
, type
);
477 sym
->ctype
.base_type
= ctype
->base_type
;
478 sym
->ctype
.modifiers
= ctype
->modifiers
& ~MOD_STORAGE
;
480 ctype
->base_type
= sym
;
481 ctype
->modifiers
&= MOD_STORAGE
;
485 static struct symbol
*lookup_or_create_symbol(enum namespace ns
, enum type type
, struct token
*token
)
487 struct symbol
*sym
= lookup_symbol(token
->ident
, ns
);
489 sym
= alloc_symbol(token
->pos
, type
);
490 bind_symbol(sym
, token
->ident
, ns
);
491 if (type
== SYM_LABEL
)
492 fn_local_symbol(sym
);
497 static struct symbol
* local_label(struct token
*token
)
499 struct symbol
*sym
= lookup_symbol(token
->ident
, NS_SYMBOL
);
501 if (sym
&& sym
->ctype
.modifiers
& MOD_LABEL
)
508 * NOTE! NS_LABEL is not just a different namespace,
509 * it also ends up using function scope instead of the
510 * regular symbol scope.
512 struct symbol
*label_symbol(struct token
*token
)
514 struct symbol
*sym
= local_label(token
);
517 return lookup_or_create_symbol(NS_LABEL
, SYM_LABEL
, token
);
520 static struct token
*struct_union_enum_specifier(enum type type
,
521 struct token
*token
, struct ctype
*ctype
,
522 struct token
*(*parse
)(struct token
*, struct symbol
*))
525 struct position
*repos
;
527 ctype
->modifiers
= 0;
528 token
= handle_attributes(token
, ctype
, KW_ATTRIBUTE
);
529 if (token_type(token
) == TOKEN_IDENT
) {
530 sym
= lookup_symbol(token
->ident
, NS_STRUCT
);
532 (is_outer_scope(sym
->scope
) &&
533 (match_op(token
->next
,';') || match_op(token
->next
,'{')))) {
534 // Either a new symbol, or else an out-of-scope
535 // symbol being redefined.
536 sym
= alloc_symbol(token
->pos
, type
);
537 bind_symbol(sym
, token
->ident
, NS_STRUCT
);
539 if (sym
->type
!= type
)
540 error_die(token
->pos
, "invalid tag applied to %s", show_typename (sym
));
541 ctype
->base_type
= sym
;
544 if (match_op(token
, '{')) {
545 // The following test is actually wrong for empty
546 // structs, but (1) they are not C99, (2) gcc does
547 // the same thing, and (3) it's easier.
548 if (sym
->symbol_list
)
549 error_die(token
->pos
, "redefinition of %s", show_typename (sym
));
551 token
= parse(token
->next
, sym
);
552 token
= expect(token
, '}', "at end of struct-union-enum-specifier");
554 // Mark the structure as needing re-examination
556 sym
->endpos
= token
->pos
;
561 // private struct/union/enum type
562 if (!match_op(token
, '{')) {
563 sparse_error(token
->pos
, "expected declaration");
564 ctype
->base_type
= &bad_ctype
;
568 sym
= alloc_symbol(token
->pos
, type
);
569 token
= parse(token
->next
, sym
);
570 ctype
->base_type
= sym
;
571 token
= expect(token
, '}', "at end of specifier");
572 sym
->endpos
= token
->pos
;
577 static struct token
*parse_struct_declaration(struct token
*token
, struct symbol
*sym
)
579 struct symbol
*field
, *last
= NULL
;
581 res
= struct_declaration_list(token
, &sym
->symbol_list
);
582 FOR_EACH_PTR(sym
->symbol_list
, field
) {
584 struct symbol
*base
= field
->ctype
.base_type
;
585 if (base
&& base
->type
== SYM_BITFIELD
)
589 last
->next_subobject
= field
;
591 } END_FOR_EACH_PTR(field
);
595 static struct token
*parse_union_declaration(struct token
*token
, struct symbol
*sym
)
597 return struct_declaration_list(token
, &sym
->symbol_list
);
600 static struct token
*struct_specifier(struct token
*token
, struct ctype
*ctype
)
602 return struct_union_enum_specifier(SYM_STRUCT
, token
, ctype
, parse_struct_declaration
);
605 static struct token
*union_specifier(struct token
*token
, struct ctype
*ctype
)
607 return struct_union_enum_specifier(SYM_UNION
, token
, ctype
, parse_union_declaration
);
613 unsigned long long y
;
616 static void upper_boundary(Num
*n
, Num
*v
)
628 static void lower_boundary(Num
*n
, Num
*v
)
640 static int type_is_ok(struct symbol
*type
, Num
*upper
, Num
*lower
)
642 int shift
= type
->bit_size
;
643 int is_unsigned
= type
->ctype
.modifiers
& MOD_UNSIGNED
;
647 if (upper
->x
== 0 && upper
->y
>> shift
)
649 if (lower
->x
== 0 || (!is_unsigned
&& (~lower
->y
>> shift
) == 0))
654 static struct symbol
*bigger_enum_type(struct symbol
*s1
, struct symbol
*s2
)
656 if (s1
->bit_size
< s2
->bit_size
) {
658 } else if (s1
->bit_size
== s2
->bit_size
) {
659 if (s2
->ctype
.modifiers
& MOD_UNSIGNED
)
662 if (s1
->bit_size
< bits_in_int
)
667 static void cast_enum_list(struct symbol_list
*list
, struct symbol
*base_type
)
671 FOR_EACH_PTR(list
, sym
) {
672 struct expression
*expr
= sym
->initializer
;
673 struct symbol
*ctype
;
674 if (expr
->type
!= EXPR_VALUE
)
677 if (ctype
->bit_size
== base_type
->bit_size
)
679 cast_value(expr
, base_type
, expr
, ctype
);
680 } END_FOR_EACH_PTR(sym
);
683 static struct token
*parse_enum_declaration(struct token
*token
, struct symbol
*parent
)
685 unsigned long long lastval
= 0;
686 struct symbol
*ctype
= NULL
, *base_type
= NULL
;
687 Num upper
= {-1, 0}, lower
= {1, 0};
689 parent
->examined
= 1;
690 parent
->ctype
.base_type
= &int_ctype
;
691 while (token_type(token
) == TOKEN_IDENT
) {
692 struct expression
*expr
= NULL
;
693 struct token
*next
= token
->next
;
696 sym
= alloc_symbol(token
->pos
, SYM_NODE
);
697 bind_symbol(sym
, token
->ident
, NS_SYMBOL
);
698 sym
->ctype
.modifiers
&= ~MOD_ADDRESSABLE
;
700 if (match_op(next
, '=')) {
701 next
= constant_expression(next
->next
, &expr
);
702 lastval
= get_expression_value(expr
);
704 if (expr
&& expr
->ctype
)
708 } else if (is_int_type(ctype
)) {
711 error_die(token
->pos
, "can't increment the last enum member");
715 expr
= alloc_expression(token
->pos
, EXPR_VALUE
);
716 expr
->value
= lastval
;
720 sym
->initializer
= expr
;
721 sym
->enum_member
= 1;
722 sym
->ctype
.base_type
= parent
;
723 add_ptr_list(&parent
->symbol_list
, sym
);
725 if (base_type
!= &bad_ctype
) {
726 if (ctype
->type
== SYM_NODE
)
727 ctype
= ctype
->ctype
.base_type
;
728 if (ctype
->type
== SYM_ENUM
) {
732 ctype
= ctype
->ctype
.base_type
;
736 * - if all enums are of the same type, then
737 * the base_type is that type (two first
739 * - if enums are of different types, they
740 * all have to be integer types, and the
741 * base type is at least "int_ctype".
742 * - otherwise the base_type is "bad_ctype".
746 } else if (ctype
== base_type
) {
748 } else if (is_int_type(base_type
) && is_int_type(ctype
)) {
749 base_type
= bigger_enum_type(base_type
, ctype
);
751 base_type
= &bad_ctype
;
752 parent
->ctype
.base_type
= base_type
;
754 if (is_int_type(base_type
)) {
755 Num v
= {.y
= lastval
};
756 if (ctype
->ctype
.modifiers
& MOD_UNSIGNED
)
758 else if ((long long)lastval
>= 0)
762 upper_boundary(&upper
, &v
);
763 lower_boundary(&lower
, &v
);
767 sym
->endpos
= token
->pos
;
769 if (!match_op(token
, ','))
774 sparse_error(token
->pos
, "bad enum definition");
775 base_type
= &bad_ctype
;
777 else if (!is_int_type(base_type
))
778 base_type
= base_type
;
779 else if (type_is_ok(base_type
, &upper
, &lower
))
780 base_type
= base_type
;
781 else if (type_is_ok(&int_ctype
, &upper
, &lower
))
782 base_type
= &int_ctype
;
783 else if (type_is_ok(&uint_ctype
, &upper
, &lower
))
784 base_type
= &uint_ctype
;
785 else if (type_is_ok(&long_ctype
, &upper
, &lower
))
786 base_type
= &long_ctype
;
787 else if (type_is_ok(&ulong_ctype
, &upper
, &lower
))
788 base_type
= &ulong_ctype
;
789 else if (type_is_ok(&llong_ctype
, &upper
, &lower
))
790 base_type
= &llong_ctype
;
791 else if (type_is_ok(&ullong_ctype
, &upper
, &lower
))
792 base_type
= &ullong_ctype
;
794 base_type
= &bad_ctype
;
795 parent
->ctype
.base_type
= base_type
;
796 parent
->ctype
.modifiers
|= (base_type
->ctype
.modifiers
& MOD_UNSIGNED
);
797 parent
->examined
= 0;
799 cast_enum_list(parent
->symbol_list
, base_type
);
804 static struct token
*enum_specifier(struct token
*token
, struct ctype
*ctype
)
806 struct token
*ret
= struct_union_enum_specifier(SYM_ENUM
, token
, ctype
, parse_enum_declaration
);
808 ctype
= &ctype
->base_type
->ctype
;
809 if (!ctype
->base_type
)
810 ctype
->base_type
= &incomplete_ctype
;
815 static struct token
*typeof_specifier(struct token
*token
, struct ctype
*ctype
)
819 if (!match_op(token
, '(')) {
820 sparse_error(token
->pos
, "expected '(' after typeof");
823 if (lookup_type(token
->next
)) {
824 token
= typename(token
->next
, &sym
, 0);
827 struct symbol
*typeof_sym
= alloc_symbol(token
->pos
, SYM_TYPEOF
);
828 token
= parse_expression(token
->next
, &typeof_sym
->initializer
);
830 ctype
->modifiers
= 0;
831 typeof_sym
->endpos
= token
->pos
;
832 ctype
->base_type
= typeof_sym
;
834 return expect(token
, ')', "after typeof");
837 static struct token
*ignore_attribute(struct token
*token
, struct symbol
*attr
, struct ctype
*ctype
)
839 struct expression
*expr
= NULL
;
840 if (match_op(token
, '('))
841 token
= parens_expression(token
, &expr
, "in attribute");
845 static struct token
*attribute_packed(struct token
*token
, struct symbol
*attr
, struct ctype
*ctype
)
847 ctype
->alignment
= 1;
851 static struct token
*attribute_aligned(struct token
*token
, struct symbol
*attr
, struct ctype
*ctype
)
853 int alignment
= max_alignment
;
854 struct expression
*expr
= NULL
;
856 if (match_op(token
, '(')) {
857 token
= parens_expression(token
, &expr
, "in attribute");
859 alignment
= const_expression_value(expr
);
861 ctype
->alignment
= alignment
;
865 static struct token
*attribute_modifier(struct token
*token
, struct symbol
*attr
, struct ctype
*ctype
)
867 ctype
->modifiers
|= attr
->ctype
.modifiers
;
871 static struct token
*attribute_address_space(struct token
*token
, struct symbol
*attr
, struct ctype
*ctype
)
873 struct expression
*expr
= NULL
;
874 token
= expect(token
, '(', "after address_space attribute");
875 token
= conditional_expression(token
, &expr
);
877 ctype
->as
= const_expression_value(expr
);
878 token
= expect(token
, ')', "after address_space attribute");
882 static struct token
*attribute_mode(struct token
*token
, struct symbol
*attr
, struct ctype
*ctype
)
884 token
= expect(token
, '(', "after mode attribute");
885 if (token_type(token
) == TOKEN_IDENT
) {
886 struct symbol
*mode
= lookup_keyword(token
->ident
, NS_KEYWORD
);
887 if (mode
&& mode
->op
->type
== KW_MODE
)
888 ctype
->modifiers
|= mode
->ctype
.modifiers
;
890 sparse_error(token
->pos
, "unknown mode attribute %s\n", show_ident(token
->ident
));
893 sparse_error(token
->pos
, "expect attribute mode symbol\n");
894 token
= expect(token
, ')', "after mode attribute");
898 static struct token
*attribute_context(struct token
*token
, struct symbol
*attr
, struct ctype
*ctype
)
900 struct context
*context
= alloc_context();
901 struct expression
*args
[3];
904 token
= expect(token
, '(', "after context attribute");
905 while (!match_op(token
, ')')) {
906 struct expression
*expr
= NULL
;
907 token
= conditional_expression(token
, &expr
);
912 if (!match_op(token
, ','))
919 sparse_error(token
->pos
, "expected context input/output values");
922 context
->in
= get_expression_value(args
[0]);
925 context
->in
= get_expression_value(args
[0]);
926 context
->out
= get_expression_value(args
[1]);
929 context
->context
= args
[0];
930 context
->in
= get_expression_value(args
[1]);
931 context
->out
= get_expression_value(args
[2]);
936 add_ptr_list(&ctype
->contexts
, context
);
938 token
= expect(token
, ')', "after context attribute");
942 static struct token
*attribute_transparent_union(struct token
*token
, struct symbol
*attr
, struct ctype
*ctype
)
944 if (Wtransparent_union
)
945 warning(token
->pos
, "ignoring attribute __transparent_union__");
949 static struct token
*recover_unknown_attribute(struct token
*token
)
951 struct expression
*expr
= NULL
;
953 sparse_error(token
->pos
, "attribute '%s': unknown attribute", show_ident(token
->ident
));
955 if (match_op(token
, '('))
956 token
= parens_expression(token
, &expr
, "in attribute");
960 static struct token
*attribute_specifier(struct token
*token
, struct ctype
*ctype
)
962 ctype
->modifiers
= 0;
963 token
= expect(token
, '(', "after attribute");
964 token
= expect(token
, '(', "after attribute");
967 struct ident
*attribute_name
;
970 if (eof_token(token
))
972 if (match_op(token
, ';'))
974 if (token_type(token
) != TOKEN_IDENT
)
976 attribute_name
= token
->ident
;
977 attr
= lookup_keyword(attribute_name
, NS_KEYWORD
);
978 if (attr
&& attr
->op
->attribute
)
979 token
= attr
->op
->attribute(token
->next
, attr
, ctype
);
981 token
= recover_unknown_attribute(token
);
983 if (!match_op(token
, ','))
988 token
= expect(token
, ')', "after attribute");
989 token
= expect(token
, ')', "after attribute");
993 struct symbol
* ctype_integer(unsigned long spec
)
995 static struct symbol
*const integer_ctypes
[][3] = {
996 { &llong_ctype
, &sllong_ctype
, &ullong_ctype
},
997 { &long_ctype
, &slong_ctype
, &ulong_ctype
},
998 { &short_ctype
, &sshort_ctype
, &ushort_ctype
},
999 { &char_ctype
, &schar_ctype
, &uchar_ctype
},
1000 { &int_ctype
, &sint_ctype
, &uint_ctype
},
1002 struct symbol
*const (*ctype
)[3];
1005 ctype
= integer_ctypes
;
1006 if (!(spec
& MOD_LONGLONG
)) {
1008 if (!(spec
& MOD_LONG
)) {
1010 if (!(spec
& MOD_SHORT
)) {
1012 if (!(spec
& MOD_CHAR
))
1018 sub
= ((spec
& MOD_UNSIGNED
)
1020 : ((spec
& MOD_EXPLICITLY_SIGNED
)
1024 return ctype
[0][sub
];
1027 struct symbol
* ctype_fp(unsigned long spec
)
1029 if (spec
& MOD_LONGLONG
)
1030 return &ldouble_ctype
;
1031 if (spec
& MOD_LONG
)
1032 return &double_ctype
;
1033 return &float_ctype
;
1036 static void apply_ctype(struct position pos
, struct ctype
*thistype
, struct ctype
*ctype
)
1038 unsigned long mod
= thistype
->modifiers
;
1041 unsigned long old
= ctype
->modifiers
;
1042 unsigned long extra
= 0, dup
, conflict
;
1044 if (mod
& old
& MOD_LONG
) {
1045 extra
= MOD_LONGLONG
| MOD_LONG
;
1049 dup
= (mod
& old
) | (extra
& old
) | (extra
& mod
);
1051 sparse_error(pos
, "Just how %sdo you want this type to be?",
1052 modifier_string(dup
));
1054 conflict
= !(~mod
& ~old
& (MOD_LONG
| MOD_SHORT
));
1056 sparse_error(pos
, "You cannot have both long and short modifiers.");
1058 conflict
= !(~mod
& ~old
& (MOD_SIGNED
| MOD_UNSIGNED
));
1060 sparse_error(pos
, "You cannot have both signed and unsigned modifiers.");
1062 // Only one storage modifier allowed, except that "inline" doesn't count.
1063 conflict
= (mod
| old
) & (MOD_STORAGE
& ~MOD_INLINE
);
1064 conflict
&= (conflict
- 1);
1066 sparse_error(pos
, "multiple storage classes");
1068 ctype
->modifiers
= old
| mod
| extra
;
1072 concat_ptr_list((struct ptr_list
*)thistype
->contexts
,
1073 (struct ptr_list
**)&ctype
->contexts
);
1076 if (thistype
->alignment
& (thistype
->alignment
-1)) {
1077 warning(pos
, "I don't like non-power-of-2 alignments");
1078 thistype
->alignment
= 0;
1080 if (thistype
->alignment
> ctype
->alignment
)
1081 ctype
->alignment
= thistype
->alignment
;
1085 ctype
->as
= thistype
->as
;
1088 static void check_modifiers(struct position
*pos
, struct symbol
*s
, unsigned long mod
)
1090 unsigned long banned
, wrong
;
1091 const unsigned long BANNED_SIZE
= MOD_LONG
| MOD_LONGLONG
| MOD_SHORT
;
1092 const unsigned long BANNED_SIGN
= MOD_SIGNED
| MOD_UNSIGNED
;
1094 if (!(s
->op
->type
& KW_SPEC
))
1095 banned
= s
->op
->type
== KW_SPECIFIER
? (BANNED_SIZE
| BANNED_SIGN
) : 0;
1096 else if (s
->ctype
.base_type
== &fp_type
)
1097 banned
= BANNED_SIGN
;
1098 else if (s
->ctype
.base_type
== &int_type
|| !s
->ctype
.base_type
|| is_int_type (s
))
1104 // vector_type <-- whatever that is
1105 banned
= BANNED_SIZE
| BANNED_SIGN
;
1108 wrong
= mod
& banned
;
1110 sparse_error(*pos
, "modifier %sis invalid in this context",
1111 modifier_string (wrong
));
1114 static struct token
*declaration_specifiers(struct token
*next
, struct decl_state
*ctx
, int qual
)
1116 struct token
*token
;
1118 while ( (token
= next
) != NULL
) {
1119 struct ctype thistype
;
1120 struct ident
*ident
;
1121 struct symbol
*s
, *type
;
1125 if (token_type(token
) != TOKEN_IDENT
)
1127 ident
= token
->ident
;
1129 s
= lookup_symbol(ident
, NS_TYPEDEF
);
1133 if (s
->type
!= SYM_KEYWORD
)
1135 if (!(s
->op
->type
& (KW_ATTRIBUTE
| KW_QUALIFIER
)))
1138 if (s
->ctype
.modifiers
& MOD_USERTYPE
) {
1139 if (ctx
->ctype
.base_type
)
1141 if (ctx
->ctype
.modifiers
& MOD_SPECIFIER
)
1143 ctx
->ctype
.base_type
= s
->ctype
.base_type
;
1144 apply_ctype(token
->pos
, &s
->ctype
, &ctx
->ctype
);
1147 thistype
= s
->ctype
;
1148 mod
= thistype
.modifiers
;
1149 if (s
->type
== SYM_KEYWORD
&& s
->op
->declarator
) {
1150 next
= s
->op
->declarator(next
, &thistype
);
1151 mod
= thistype
.modifiers
;
1153 type
= thistype
.base_type
;
1155 if (ctx
->ctype
.base_type
)
1157 ctx
->ctype
.base_type
= type
;
1160 check_modifiers(&token
->pos
, s
, ctx
->ctype
.modifiers
);
1161 apply_ctype(token
->pos
, &thistype
, &ctx
->ctype
);
1164 if (!ctx
->ctype
.base_type
) {
1165 struct symbol
*base
= &incomplete_ctype
;
1168 * If we have modifiers, we'll default to an integer
1169 * type, and "ctype_integer()" will turn this into
1172 if (ctx
->ctype
.modifiers
& MOD_SPECIFIER
)
1174 ctx
->ctype
.base_type
= base
;
1179 static struct token
*abstract_array_declarator(struct token
*token
, struct symbol
*sym
)
1181 struct expression
*expr
= NULL
;
1183 token
= parse_expression(token
, &expr
);
1184 sym
->array_size
= expr
;
1188 static struct token
*parameter_type_list(struct token
*, struct symbol
*);
1189 static struct token
*identifier_list(struct token
*, struct symbol
*);
1190 static struct token
*declarator(struct token
*token
, struct decl_state
*ctx
);
1192 static struct token
*skip_attribute(struct token
*token
)
1194 token
= token
->next
;
1195 if (match_op(token
, '(')) {
1197 token
= token
->next
;
1198 while (depth
&& !eof_token(token
)) {
1199 if (token_type(token
) == TOKEN_SPECIAL
) {
1200 if (token
->special
== '(')
1202 else if (token
->special
== ')')
1205 token
= token
->next
;
1211 static struct token
*skip_attributes(struct token
*token
)
1213 struct symbol
*keyword
;
1215 if (token_type(token
) != TOKEN_IDENT
)
1217 keyword
= lookup_keyword(token
->ident
, NS_KEYWORD
| NS_TYPEDEF
);
1218 if (!keyword
|| keyword
->type
!= SYM_KEYWORD
)
1220 if (!(keyword
->op
->type
& KW_ATTRIBUTE
))
1222 token
= expect(token
->next
, '(', "after attribute");
1223 token
= expect(token
, '(', "after attribute");
1225 if (eof_token(token
))
1227 if (match_op(token
, ';'))
1229 if (token_type(token
) != TOKEN_IDENT
)
1231 token
= skip_attribute(token
);
1232 if (!match_op(token
, ','))
1234 token
= token
->next
;
1236 token
= expect(token
, ')', "after attribute");
1237 token
= expect(token
, ')', "after attribute");
1242 static struct token
*handle_attributes(struct token
*token
, struct ctype
*ctype
, unsigned int keywords
)
1244 struct symbol
*keyword
;
1246 struct ctype thistype
= { 0, };
1247 if (token_type(token
) != TOKEN_IDENT
)
1249 keyword
= lookup_keyword(token
->ident
, NS_KEYWORD
| NS_TYPEDEF
);
1250 if (!keyword
|| keyword
->type
!= SYM_KEYWORD
)
1252 if (!(keyword
->op
->type
& keywords
))
1254 token
= keyword
->op
->declarator(token
->next
, &thistype
);
1255 apply_ctype(token
->pos
, &thistype
, ctype
);
1256 keywords
&= KW_ATTRIBUTE
;
1261 static int is_nested(struct token
*token
, struct token
**p
,
1262 int prefer_abstract
)
1265 * This can be either a parameter list or a grouping.
1266 * For the direct (non-abstract) case, we know if must be
1267 * a parameter list if we already saw the identifier.
1268 * For the abstract case, we know if must be a parameter
1269 * list if it is empty or starts with a type.
1271 struct token
*next
= token
->next
;
1273 *p
= next
= skip_attributes(next
);
1275 if (token_type(next
) == TOKEN_IDENT
) {
1276 if (lookup_type(next
))
1277 return !prefer_abstract
;
1281 if (match_op(next
, ')') || match_op(next
, SPECIAL_ELLIPSIS
))
1288 Empty
, K_R
, Proto
, Bad_Func
,
1291 static enum kind
which_func(struct token
*token
,
1293 int prefer_abstract
)
1295 struct token
*next
= token
->next
;
1297 if (token_type(next
) == TOKEN_IDENT
) {
1298 if (lookup_type(next
))
1300 /* identifier list not in definition; complain */
1301 if (prefer_abstract
)
1303 "identifier list not in definition");
1307 if (token_type(next
) != TOKEN_SPECIAL
)
1310 if (next
->special
== ')') {
1311 /* don't complain about those */
1312 if (!n
|| match_op(next
->next
, ';'))
1315 "non-ANSI function declaration of function '%s'",
1320 if (next
->special
== SPECIAL_ELLIPSIS
) {
1322 "variadic functions must have one named argument");
1329 static struct token
*direct_declarator(struct token
*token
, struct decl_state
*ctx
)
1331 struct ctype
*ctype
= &ctx
->ctype
;
1333 struct ident
**p
= ctx
->ident
;
1335 if (ctx
->ident
&& token_type(token
) == TOKEN_IDENT
) {
1336 *ctx
->ident
= token
->ident
;
1337 token
= token
->next
;
1338 } else if (match_op(token
, '(') &&
1339 is_nested(token
, &next
, ctx
->prefer_abstract
)) {
1340 struct symbol
*base_type
= ctype
->base_type
;
1341 if (token
->next
!= next
)
1342 next
= handle_attributes(token
->next
, ctype
,
1344 token
= declarator(next
, ctx
);
1345 token
= expect(token
, ')', "in nested declarator");
1346 while (ctype
->base_type
!= base_type
)
1347 ctype
= &ctype
->base_type
->ctype
;
1351 if (match_op(token
, '(')) {
1352 enum kind kind
= which_func(token
, p
, ctx
->prefer_abstract
);
1354 fn
= alloc_indirect_symbol(token
->pos
, ctype
, SYM_FN
);
1355 token
= token
->next
;
1357 token
= identifier_list(token
, fn
);
1358 else if (kind
== Proto
)
1359 token
= parameter_type_list(token
, fn
);
1360 token
= expect(token
, ')', "in function declarator");
1361 fn
->endpos
= token
->pos
;
1365 while (match_op(token
, '[')) {
1366 struct symbol
*array
;
1367 array
= alloc_indirect_symbol(token
->pos
, ctype
, SYM_ARRAY
);
1368 token
= abstract_array_declarator(token
->next
, array
);
1369 token
= expect(token
, ']', "in abstract_array_declarator");
1370 array
->endpos
= token
->pos
;
1371 ctype
= &array
->ctype
;
1376 static struct token
*pointer(struct token
*token
, struct decl_state
*ctx
)
1378 unsigned long modifiers
;
1379 struct symbol
*base_type
;
1381 modifiers
= ctx
->ctype
.modifiers
& ~MOD_TYPEDEF
;
1382 base_type
= ctx
->ctype
.base_type
;
1383 ctx
->ctype
.modifiers
= modifiers
;
1385 while (match_op(token
,'*')) {
1386 struct symbol
*ptr
= alloc_symbol(token
->pos
, SYM_PTR
);
1387 ptr
->ctype
.modifiers
= modifiers
& ~MOD_STORAGE
;
1388 ptr
->ctype
.as
= ctx
->ctype
.as
;
1389 concat_ptr_list((struct ptr_list
*)ctx
->ctype
.contexts
,
1390 (struct ptr_list
**)&ptr
->ctype
.contexts
);
1391 ptr
->ctype
.base_type
= base_type
;
1394 ctx
->ctype
.modifiers
= modifiers
& MOD_STORAGE
;
1395 ctx
->ctype
.base_type
= base_type
;
1397 free_ptr_list(&ctx
->ctype
.contexts
);
1399 token
= declaration_specifiers(token
->next
, ctx
, 1);
1400 modifiers
= ctx
->ctype
.modifiers
;
1401 ctx
->ctype
.base_type
->endpos
= token
->pos
;
1406 static struct token
*declarator(struct token
*token
, struct decl_state
*ctx
)
1408 token
= pointer(token
, ctx
);
1409 return direct_declarator(token
, ctx
);
1412 static struct token
*handle_bitfield(struct token
*token
, struct decl_state
*ctx
)
1414 struct ctype
*ctype
= &ctx
->ctype
;
1415 struct expression
*expr
;
1416 struct symbol
*bitfield
;
1419 if (ctype
->base_type
!= &int_type
&& !is_int_type(ctype
->base_type
)) {
1420 sparse_error(token
->pos
, "invalid bitfield specifier for type %s.",
1421 show_typename(ctype
->base_type
));
1422 // Parse this to recover gracefully.
1423 return conditional_expression(token
->next
, &expr
);
1426 bitfield
= alloc_indirect_symbol(token
->pos
, ctype
, SYM_BITFIELD
);
1427 token
= conditional_expression(token
->next
, &expr
);
1428 width
= const_expression_value(expr
);
1429 bitfield
->bit_size
= width
;
1431 if (width
< 0 || width
> INT_MAX
) {
1432 sparse_error(token
->pos
, "invalid bitfield width, %lld.", width
);
1434 } else if (*ctx
->ident
&& width
== 0) {
1435 sparse_error(token
->pos
, "invalid named zero-width bitfield `%s'",
1436 show_ident(*ctx
->ident
));
1438 } else if (*ctx
->ident
) {
1439 struct symbol
*base_type
= bitfield
->ctype
.base_type
;
1440 struct symbol
*bitfield_type
= base_type
== &int_type
? bitfield
: base_type
;
1441 int is_signed
= !(bitfield_type
->ctype
.modifiers
& MOD_UNSIGNED
);
1442 if (Wone_bit_signed_bitfield
&& width
== 1 && is_signed
) {
1443 // Valid values are either {-1;0} or {0}, depending on integer
1444 // representation. The latter makes for very efficient code...
1445 sparse_error(token
->pos
, "dubious one-bit signed bitfield");
1447 if (Wdefault_bitfield_sign
&&
1448 bitfield_type
->type
!= SYM_ENUM
&&
1449 !(bitfield_type
->ctype
.modifiers
& MOD_EXPLICITLY_SIGNED
) &&
1451 // The sign of bitfields is unspecified by default.
1452 warning(token
->pos
, "dubious bitfield without explicit `signed' or `unsigned'");
1455 bitfield
->bit_size
= width
;
1456 bitfield
->endpos
= token
->pos
;
1460 static struct token
*declaration_list(struct token
*token
, struct symbol_list
**list
)
1462 struct decl_state ctx
= {.prefer_abstract
= 0};
1465 token
= declaration_specifiers(token
, &ctx
, 0);
1468 struct symbol
*decl
= alloc_symbol(token
->pos
, SYM_NODE
);
1469 ctx
.ident
= &decl
->ident
;
1471 token
= declarator(token
, &ctx
);
1472 if (match_op(token
, ':'))
1473 token
= handle_bitfield(token
, &ctx
);
1475 token
= handle_attributes(token
, &ctx
.ctype
, KW_ATTRIBUTE
);
1476 apply_modifiers(token
->pos
, &ctx
.ctype
);
1478 decl
->ctype
= ctx
.ctype
;
1479 decl
->endpos
= token
->pos
;
1480 add_symbol(list
, decl
);
1481 if (!match_op(token
, ','))
1483 token
= token
->next
;
1489 static struct token
*struct_declaration_list(struct token
*token
, struct symbol_list
**list
)
1491 while (!match_op(token
, '}')) {
1492 if (!match_op(token
, ';'))
1493 token
= declaration_list(token
, list
);
1494 if (!match_op(token
, ';')) {
1495 sparse_error(token
->pos
, "expected ; at end of declaration");
1498 token
= token
->next
;
1503 static struct token
*parameter_declaration(struct token
*token
, struct symbol
*sym
)
1505 struct decl_state ctx
= {.prefer_abstract
= 1};
1507 token
= declaration_specifiers(token
, &ctx
, 0);
1508 ctx
.ident
= &sym
->ident
;
1509 token
= declarator(token
, &ctx
);
1510 token
= handle_attributes(token
, &ctx
.ctype
, KW_ATTRIBUTE
);
1511 apply_modifiers(token
->pos
, &ctx
.ctype
);
1512 sym
->ctype
= ctx
.ctype
;
1513 sym
->endpos
= token
->pos
;
1517 struct token
*typename(struct token
*token
, struct symbol
**p
, int mod
)
1519 struct decl_state ctx
= {.prefer_abstract
= 1};
1520 struct symbol
*sym
= alloc_symbol(token
->pos
, SYM_NODE
);
1522 token
= declaration_specifiers(token
, &ctx
, 0);
1523 token
= declarator(token
, &ctx
);
1524 apply_modifiers(token
->pos
, &ctx
.ctype
);
1525 if (ctx
.ctype
.modifiers
& MOD_STORAGE
& ~mod
)
1526 warning(sym
->pos
, "storage class in typename (%s)",
1527 show_typename(sym
));
1528 sym
->ctype
= ctx
.ctype
;
1529 sym
->endpos
= token
->pos
;
1533 static struct token
*expression_statement(struct token
*token
, struct expression
**tree
)
1535 token
= parse_expression(token
, tree
);
1536 return expect(token
, ';', "at end of statement");
1539 static struct token
*parse_asm_operands(struct token
*token
, struct statement
*stmt
,
1540 struct expression_list
**inout
)
1542 struct expression
*expr
;
1544 /* Allow empty operands */
1545 if (match_op(token
->next
, ':') || match_op(token
->next
, ')'))
1548 struct ident
*ident
= NULL
;
1549 if (match_op(token
->next
, '[') &&
1550 token_type(token
->next
->next
) == TOKEN_IDENT
&&
1551 match_op(token
->next
->next
->next
, ']')) {
1552 ident
= token
->next
->next
->ident
;
1553 token
= token
->next
->next
->next
;
1555 add_expression(inout
, (struct expression
*)ident
); /* UGGLEE!!! */
1556 token
= primary_expression(token
->next
, &expr
);
1557 add_expression(inout
, expr
);
1558 token
= parens_expression(token
, &expr
, "in asm parameter");
1559 add_expression(inout
, expr
);
1560 } while (match_op(token
, ','));
1564 static struct token
*parse_asm_clobbers(struct token
*token
, struct statement
*stmt
,
1565 struct expression_list
**clobbers
)
1567 struct expression
*expr
;
1570 token
= primary_expression(token
->next
, &expr
);
1571 add_expression(clobbers
, expr
);
1572 } while (match_op(token
, ','));
1576 static struct token
*parse_asm_statement(struct token
*token
, struct statement
*stmt
)
1578 token
= token
->next
;
1579 stmt
->type
= STMT_ASM
;
1580 if (match_idents(token
, &__volatile___ident
, &__volatile_ident
, &volatile_ident
, NULL
)) {
1581 token
= token
->next
;
1583 token
= expect(token
, '(', "after asm");
1584 token
= parse_expression(token
, &stmt
->asm_string
);
1585 if (match_op(token
, ':'))
1586 token
= parse_asm_operands(token
, stmt
, &stmt
->asm_outputs
);
1587 if (match_op(token
, ':'))
1588 token
= parse_asm_operands(token
, stmt
, &stmt
->asm_inputs
);
1589 if (match_op(token
, ':'))
1590 token
= parse_asm_clobbers(token
, stmt
, &stmt
->asm_clobbers
);
1591 token
= expect(token
, ')', "after asm");
1592 return expect(token
, ';', "at end of asm-statement");
1595 static struct token
*parse_asm_declarator(struct token
*token
, struct ctype
*ctype
)
1597 struct expression
*expr
;
1598 token
= expect(token
, '(', "after asm");
1599 token
= parse_expression(token
->next
, &expr
);
1600 token
= expect(token
, ')', "after asm");
1604 /* Make a statement out of an expression */
1605 static struct statement
*make_statement(struct expression
*expr
)
1607 struct statement
*stmt
;
1611 stmt
= alloc_statement(expr
->pos
, STMT_EXPRESSION
);
1612 stmt
->expression
= expr
;
1617 * All iterators have two symbols associated with them:
1618 * the "continue" and "break" symbols, which are targets
1619 * for continue and break statements respectively.
1621 * They are in a special name-space, but they follow
1622 * all the normal visibility rules, so nested iterators
1623 * automatically work right.
1625 static void start_iterator(struct statement
*stmt
)
1627 struct symbol
*cont
, *brk
;
1629 start_symbol_scope();
1630 cont
= alloc_symbol(stmt
->pos
, SYM_NODE
);
1631 bind_symbol(cont
, &continue_ident
, NS_ITERATOR
);
1632 brk
= alloc_symbol(stmt
->pos
, SYM_NODE
);
1633 bind_symbol(brk
, &break_ident
, NS_ITERATOR
);
1635 stmt
->type
= STMT_ITERATOR
;
1636 stmt
->iterator_break
= brk
;
1637 stmt
->iterator_continue
= cont
;
1638 fn_local_symbol(brk
);
1639 fn_local_symbol(cont
);
1642 static void end_iterator(struct statement
*stmt
)
1647 static struct statement
*start_function(struct symbol
*sym
)
1650 struct statement
*stmt
= alloc_statement(sym
->pos
, STMT_COMPOUND
);
1652 start_function_scope();
1653 ret
= alloc_symbol(sym
->pos
, SYM_NODE
);
1654 ret
->ctype
= sym
->ctype
.base_type
->ctype
;
1655 ret
->ctype
.modifiers
&= ~(MOD_STORAGE
| MOD_CONST
| MOD_VOLATILE
| MOD_INLINE
| MOD_ADDRESSABLE
| MOD_NOCAST
| MOD_NODEREF
| MOD_ACCESSED
| MOD_TOPLEVEL
);
1656 ret
->ctype
.modifiers
|= (MOD_AUTO
| MOD_REGISTER
);
1657 bind_symbol(ret
, &return_ident
, NS_ITERATOR
);
1659 fn_local_symbol(ret
);
1661 // Currently parsed symbol for __func__/__FUNCTION__/__PRETTY_FUNCTION__
1667 static void end_function(struct symbol
*sym
)
1670 end_function_scope();
1674 * A "switch()" statement, like an iterator, has a
1675 * the "break" symbol associated with it. It works
1676 * exactly like the iterator break - it's the target
1677 * for any break-statements in scope, and means that
1678 * "break" handling doesn't even need to know whether
1679 * it's breaking out of an iterator or a switch.
1681 * In addition, the "case" symbol is a marker for the
1682 * case/default statements to find the switch statement
1683 * that they are associated with.
1685 static void start_switch(struct statement
*stmt
)
1687 struct symbol
*brk
, *switch_case
;
1689 start_symbol_scope();
1690 brk
= alloc_symbol(stmt
->pos
, SYM_NODE
);
1691 bind_symbol(brk
, &break_ident
, NS_ITERATOR
);
1693 switch_case
= alloc_symbol(stmt
->pos
, SYM_NODE
);
1694 bind_symbol(switch_case
, &case_ident
, NS_ITERATOR
);
1695 switch_case
->stmt
= stmt
;
1697 stmt
->type
= STMT_SWITCH
;
1698 stmt
->switch_break
= brk
;
1699 stmt
->switch_case
= switch_case
;
1701 fn_local_symbol(brk
);
1702 fn_local_symbol(switch_case
);
1705 static void end_switch(struct statement
*stmt
)
1707 if (!stmt
->switch_case
->symbol_list
)
1708 warning(stmt
->pos
, "switch with no cases");
1712 static void add_case_statement(struct statement
*stmt
)
1714 struct symbol
*target
= lookup_symbol(&case_ident
, NS_ITERATOR
);
1718 sparse_error(stmt
->pos
, "not in switch scope");
1719 stmt
->type
= STMT_NONE
;
1722 sym
= alloc_symbol(stmt
->pos
, SYM_NODE
);
1723 add_symbol(&target
->symbol_list
, sym
);
1725 stmt
->case_label
= sym
;
1726 fn_local_symbol(sym
);
1729 static struct token
*parse_return_statement(struct token
*token
, struct statement
*stmt
)
1731 struct symbol
*target
= lookup_symbol(&return_ident
, NS_ITERATOR
);
1734 error_die(token
->pos
, "internal error: return without a function target");
1735 stmt
->type
= STMT_RETURN
;
1736 stmt
->ret_target
= target
;
1737 return expression_statement(token
->next
, &stmt
->ret_value
);
1740 static struct token
*parse_for_statement(struct token
*token
, struct statement
*stmt
)
1742 struct symbol_list
*syms
;
1743 struct expression
*e1
, *e2
, *e3
;
1744 struct statement
*iterator
;
1746 start_iterator(stmt
);
1747 token
= expect(token
->next
, '(', "after 'for'");
1751 /* C99 variable declaration? */
1752 if (lookup_type(token
)) {
1753 token
= external_declaration(token
, &syms
);
1755 token
= parse_expression(token
, &e1
);
1756 token
= expect(token
, ';', "in 'for'");
1758 token
= parse_expression(token
, &e2
);
1759 token
= expect(token
, ';', "in 'for'");
1760 token
= parse_expression(token
, &e3
);
1761 token
= expect(token
, ')', "in 'for'");
1762 token
= statement(token
, &iterator
);
1764 stmt
->iterator_syms
= syms
;
1765 stmt
->iterator_pre_statement
= make_statement(e1
);
1766 stmt
->iterator_pre_condition
= e2
;
1767 stmt
->iterator_post_statement
= make_statement(e3
);
1768 stmt
->iterator_post_condition
= NULL
;
1769 stmt
->iterator_statement
= iterator
;
1775 static struct token
*parse_while_statement(struct token
*token
, struct statement
*stmt
)
1777 struct expression
*expr
;
1778 struct statement
*iterator
;
1780 start_iterator(stmt
);
1781 token
= parens_expression(token
->next
, &expr
, "after 'while'");
1782 token
= statement(token
, &iterator
);
1784 stmt
->iterator_pre_condition
= expr
;
1785 stmt
->iterator_post_condition
= NULL
;
1786 stmt
->iterator_statement
= iterator
;
1792 static struct token
*parse_do_statement(struct token
*token
, struct statement
*stmt
)
1794 struct expression
*expr
;
1795 struct statement
*iterator
;
1797 start_iterator(stmt
);
1798 token
= statement(token
->next
, &iterator
);
1799 if (token_type(token
) == TOKEN_IDENT
&& token
->ident
== &while_ident
)
1800 token
= token
->next
;
1802 sparse_error(token
->pos
, "expected 'while' after 'do'");
1803 token
= parens_expression(token
, &expr
, "after 'do-while'");
1805 stmt
->iterator_post_condition
= expr
;
1806 stmt
->iterator_statement
= iterator
;
1809 if (iterator
&& iterator
->type
!= STMT_COMPOUND
&& Wdo_while
)
1810 warning(iterator
->pos
, "do-while statement is not a compound statement");
1812 return expect(token
, ';', "after statement");
1815 static struct token
*parse_if_statement(struct token
*token
, struct statement
*stmt
)
1817 stmt
->type
= STMT_IF
;
1818 token
= parens_expression(token
->next
, &stmt
->if_conditional
, "after if");
1819 token
= statement(token
, &stmt
->if_true
);
1820 if (token_type(token
) != TOKEN_IDENT
)
1822 if (token
->ident
!= &else_ident
)
1824 return statement(token
->next
, &stmt
->if_false
);
1827 static inline struct token
*case_statement(struct token
*token
, struct statement
*stmt
)
1829 stmt
->type
= STMT_CASE
;
1830 token
= expect(token
, ':', "after default/case");
1831 add_case_statement(stmt
);
1832 return statement(token
, &stmt
->case_statement
);
1835 static struct token
*parse_case_statement(struct token
*token
, struct statement
*stmt
)
1837 token
= parse_expression(token
->next
, &stmt
->case_expression
);
1838 if (match_op(token
, SPECIAL_ELLIPSIS
))
1839 token
= parse_expression(token
->next
, &stmt
->case_to
);
1840 return case_statement(token
, stmt
);
1843 static struct token
*parse_default_statement(struct token
*token
, struct statement
*stmt
)
1845 return case_statement(token
->next
, stmt
);
1848 static struct token
*parse_loop_iterator(struct token
*token
, struct statement
*stmt
)
1850 struct symbol
*target
= lookup_symbol(token
->ident
, NS_ITERATOR
);
1851 stmt
->type
= STMT_GOTO
;
1852 stmt
->goto_label
= target
;
1854 sparse_error(stmt
->pos
, "break/continue not in iterator scope");
1855 return expect(token
->next
, ';', "at end of statement");
1858 static struct token
*parse_switch_statement(struct token
*token
, struct statement
*stmt
)
1860 stmt
->type
= STMT_SWITCH
;
1862 token
= parens_expression(token
->next
, &stmt
->switch_expression
, "after 'switch'");
1863 token
= statement(token
, &stmt
->switch_statement
);
1868 static struct token
*parse_goto_statement(struct token
*token
, struct statement
*stmt
)
1870 stmt
->type
= STMT_GOTO
;
1871 token
= token
->next
;
1872 if (match_op(token
, '*')) {
1873 token
= parse_expression(token
->next
, &stmt
->goto_expression
);
1874 add_statement(&function_computed_goto_list
, stmt
);
1875 } else if (token_type(token
) == TOKEN_IDENT
) {
1876 stmt
->goto_label
= label_symbol(token
);
1877 token
= token
->next
;
1879 sparse_error(token
->pos
, "Expected identifier or goto expression");
1881 return expect(token
, ';', "at end of statement");
1884 static struct token
*parse_context_statement(struct token
*token
, struct statement
*stmt
)
1886 stmt
->type
= STMT_CONTEXT
;
1887 token
= parse_expression(token
->next
, &stmt
->expression
);
1888 if(stmt
->expression
->type
== EXPR_PREOP
1889 && stmt
->expression
->op
== '('
1890 && stmt
->expression
->unop
->type
== EXPR_COMMA
) {
1891 struct expression
*expr
;
1892 expr
= stmt
->expression
->unop
;
1893 stmt
->context
= expr
->left
;
1894 stmt
->expression
= expr
->right
;
1896 return expect(token
, ';', "at end of statement");
1899 static struct token
*parse_range_statement(struct token
*token
, struct statement
*stmt
)
1901 stmt
->type
= STMT_RANGE
;
1902 token
= assignment_expression(token
->next
, &stmt
->range_expression
);
1903 token
= expect(token
, ',', "after range expression");
1904 token
= assignment_expression(token
, &stmt
->range_low
);
1905 token
= expect(token
, ',', "after low range");
1906 token
= assignment_expression(token
, &stmt
->range_high
);
1907 return expect(token
, ';', "after range statement");
1910 static struct token
*statement(struct token
*token
, struct statement
**tree
)
1912 struct statement
*stmt
= alloc_statement(token
->pos
, STMT_NONE
);
1915 if (token_type(token
) == TOKEN_IDENT
) {
1916 struct symbol
*s
= lookup_keyword(token
->ident
, NS_KEYWORD
);
1917 if (s
&& s
->op
->statement
)
1918 return s
->op
->statement(token
, stmt
);
1920 if (match_op(token
->next
, ':')) {
1921 stmt
->type
= STMT_LABEL
;
1922 stmt
->label_identifier
= label_symbol(token
);
1923 token
= handle_attributes(token
->next
->next
, &stmt
->label_identifier
->ctype
, KW_ATTRIBUTE
);
1924 return statement(token
, &stmt
->label_statement
);
1928 if (match_op(token
, '{')) {
1929 stmt
->type
= STMT_COMPOUND
;
1930 start_symbol_scope();
1931 token
= compound_statement(token
->next
, stmt
);
1934 return expect(token
, '}', "at end of compound statement");
1937 stmt
->type
= STMT_EXPRESSION
;
1938 return expression_statement(token
, &stmt
->expression
);
1941 static struct token
* statement_list(struct token
*token
, struct statement_list
**list
)
1943 int seen_statement
= 0;
1945 struct statement
* stmt
;
1946 if (eof_token(token
))
1948 if (match_op(token
, '}'))
1950 if (lookup_type(token
)) {
1951 if (seen_statement
) {
1952 warning(token
->pos
, "mixing declarations and code");
1955 stmt
= alloc_statement(token
->pos
, STMT_DECLARATION
);
1956 token
= external_declaration(token
, &stmt
->declaration
);
1958 seen_statement
= Wdeclarationafterstatement
;
1959 token
= statement(token
, &stmt
);
1961 add_statement(list
, stmt
);
1966 static struct token
*identifier_list(struct token
*token
, struct symbol
*fn
)
1968 struct symbol_list
**list
= &fn
->arguments
;
1970 struct symbol
*sym
= alloc_symbol(token
->pos
, SYM_NODE
);
1971 sym
->ident
= token
->ident
;
1972 token
= token
->next
;
1973 sym
->endpos
= token
->pos
;
1974 add_symbol(list
, sym
);
1975 if (!match_op(token
, ',') ||
1976 token_type(token
->next
) != TOKEN_IDENT
||
1977 lookup_type(token
->next
))
1979 token
= token
->next
;
1984 static struct token
*parameter_type_list(struct token
*token
, struct symbol
*fn
)
1986 struct symbol_list
**list
= &fn
->arguments
;
1991 if (match_op(token
, SPECIAL_ELLIPSIS
)) {
1993 token
= token
->next
;
1997 sym
= alloc_symbol(token
->pos
, SYM_NODE
);
1998 token
= parameter_declaration(token
, sym
);
1999 if (sym
->ctype
.base_type
== &void_ctype
) {
2000 /* Special case: (void) */
2001 if (!*list
&& !sym
->ident
)
2003 warning(token
->pos
, "void parameter");
2005 add_symbol(list
, sym
);
2006 if (!match_op(token
, ','))
2008 token
= token
->next
;
2013 struct token
*compound_statement(struct token
*token
, struct statement
*stmt
)
2015 token
= statement_list(token
, &stmt
->stmts
);
2019 static struct expression
*identifier_expression(struct token
*token
)
2021 struct expression
*expr
= alloc_expression(token
->pos
, EXPR_IDENTIFIER
);
2022 expr
->expr_ident
= token
->ident
;
2026 static struct expression
*index_expression(struct expression
*from
, struct expression
*to
)
2028 int idx_from
, idx_to
;
2029 struct expression
*expr
= alloc_expression(from
->pos
, EXPR_INDEX
);
2031 idx_from
= const_expression_value(from
);
2034 idx_to
= const_expression_value(to
);
2035 if (idx_to
< idx_from
|| idx_from
< 0)
2036 warning(from
->pos
, "nonsense array initializer index range");
2038 expr
->idx_from
= idx_from
;
2039 expr
->idx_to
= idx_to
;
2043 static struct token
*single_initializer(struct expression
**ep
, struct token
*token
)
2045 int expect_equal
= 0;
2046 struct token
*next
= token
->next
;
2047 struct expression
**tail
= ep
;
2052 if ((token_type(token
) == TOKEN_IDENT
) && match_op(next
, ':')) {
2053 struct expression
*expr
= identifier_expression(token
);
2054 if (Wold_initializer
)
2055 warning(token
->pos
, "obsolete struct initializer, use C99 syntax");
2056 token
= initializer(&expr
->ident_expression
, next
->next
);
2057 if (expr
->ident_expression
)
2062 for (tail
= ep
, nested
= 0; ; nested
++, next
= token
->next
) {
2063 if (match_op(token
, '.') && (token_type(next
) == TOKEN_IDENT
)) {
2064 struct expression
*expr
= identifier_expression(next
);
2066 tail
= &expr
->ident_expression
;
2069 } else if (match_op(token
, '[')) {
2070 struct expression
*from
= NULL
, *to
= NULL
, *expr
;
2071 token
= constant_expression(token
->next
, &from
);
2073 sparse_error(token
->pos
, "Expected constant expression");
2076 if (match_op(token
, SPECIAL_ELLIPSIS
))
2077 token
= constant_expression(token
->next
, &to
);
2078 expr
= index_expression(from
, to
);
2080 tail
= &expr
->idx_expression
;
2081 token
= expect(token
, ']', "at end of initializer index");
2088 if (nested
&& !expect_equal
) {
2089 if (!match_op(token
, '='))
2090 warning(token
->pos
, "obsolete array initializer, use C99 syntax");
2095 token
= expect(token
, '=', "at end of initializer index");
2097 token
= initializer(tail
, token
);
2103 static struct token
*initializer_list(struct expression_list
**list
, struct token
*token
)
2105 struct expression
*expr
;
2108 token
= single_initializer(&expr
, token
);
2111 add_expression(list
, expr
);
2112 if (!match_op(token
, ','))
2114 token
= token
->next
;
2119 struct token
*initializer(struct expression
**tree
, struct token
*token
)
2121 if (match_op(token
, '{')) {
2122 struct expression
*expr
= alloc_expression(token
->pos
, EXPR_INITIALIZER
);
2124 token
= initializer_list(&expr
->expr_list
, token
->next
);
2125 return expect(token
, '}', "at end of initializer");
2127 return assignment_expression(token
, tree
);
2130 static void declare_argument(struct symbol
*sym
, struct symbol
*fn
)
2133 sparse_error(sym
->pos
, "no identifier for function argument");
2136 bind_symbol(sym
, sym
->ident
, NS_SYMBOL
);
2139 static struct token
*parse_function_body(struct token
*token
, struct symbol
*decl
,
2140 struct symbol_list
**list
)
2142 struct symbol_list
**old_symbol_list
;
2143 struct symbol
*base_type
= decl
->ctype
.base_type
;
2144 struct statement
*stmt
, **p
;
2145 struct symbol
*prev
;
2148 old_symbol_list
= function_symbol_list
;
2149 if (decl
->ctype
.modifiers
& MOD_INLINE
) {
2150 function_symbol_list
= &decl
->inline_symbol_list
;
2151 p
= &base_type
->inline_stmt
;
2153 function_symbol_list
= &decl
->symbol_list
;
2154 p
= &base_type
->stmt
;
2156 function_computed_target_list
= NULL
;
2157 function_computed_goto_list
= NULL
;
2159 if (decl
->ctype
.modifiers
& MOD_EXTERN
) {
2160 if (!(decl
->ctype
.modifiers
& MOD_INLINE
))
2161 warning(decl
->pos
, "function '%s' with external linkage has definition", show_ident(decl
->ident
));
2163 if (!(decl
->ctype
.modifiers
& MOD_STATIC
))
2164 decl
->ctype
.modifiers
|= MOD_EXTERN
;
2166 stmt
= start_function(decl
);
2169 FOR_EACH_PTR (base_type
->arguments
, arg
) {
2170 declare_argument(arg
, base_type
);
2171 } END_FOR_EACH_PTR(arg
);
2173 token
= compound_statement(token
->next
, stmt
);
2176 if (!(decl
->ctype
.modifiers
& MOD_INLINE
))
2177 add_symbol(list
, decl
);
2178 check_declaration(decl
);
2179 decl
->definition
= decl
;
2180 prev
= decl
->same_symbol
;
2181 if (prev
&& prev
->definition
) {
2182 warning(decl
->pos
, "multiple definitions for function '%s'",
2183 show_ident(decl
->ident
));
2184 info(prev
->definition
->pos
, " the previous one is here");
2187 prev
->definition
= decl
;
2188 prev
= prev
->same_symbol
;
2191 function_symbol_list
= old_symbol_list
;
2192 if (function_computed_goto_list
) {
2193 if (!function_computed_target_list
)
2194 warning(decl
->pos
, "function '%s' has computed goto but no targets?", show_ident(decl
->ident
));
2196 FOR_EACH_PTR(function_computed_goto_list
, stmt
) {
2197 stmt
->target_list
= function_computed_target_list
;
2198 } END_FOR_EACH_PTR(stmt
);
2201 return expect(token
, '}', "at end of function");
2204 static void promote_k_r_types(struct symbol
*arg
)
2206 struct symbol
*base
= arg
->ctype
.base_type
;
2207 if (base
&& base
->ctype
.base_type
== &int_type
&& (base
->ctype
.modifiers
& (MOD_CHAR
| MOD_SHORT
))) {
2208 arg
->ctype
.base_type
= &int_ctype
;
2212 static void apply_k_r_types(struct symbol_list
*argtypes
, struct symbol
*fn
)
2214 struct symbol_list
*real_args
= fn
->ctype
.base_type
->arguments
;
2217 FOR_EACH_PTR(real_args
, arg
) {
2218 struct symbol
*type
;
2220 /* This is quadratic in the number of arguments. We _really_ don't care */
2221 FOR_EACH_PTR(argtypes
, type
) {
2222 if (type
->ident
== arg
->ident
)
2224 } END_FOR_EACH_PTR(type
);
2225 sparse_error(arg
->pos
, "missing type declaration for parameter '%s'", show_ident(arg
->ident
));
2229 /* "char" and "short" promote to "int" */
2230 promote_k_r_types(type
);
2232 arg
->ctype
= type
->ctype
;
2233 } END_FOR_EACH_PTR(arg
);
2235 FOR_EACH_PTR(argtypes
, arg
) {
2237 warning(arg
->pos
, "nonsensical parameter declaration '%s'", show_ident(arg
->ident
));
2238 } END_FOR_EACH_PTR(arg
);
2242 static struct token
*parse_k_r_arguments(struct token
*token
, struct symbol
*decl
,
2243 struct symbol_list
**list
)
2245 struct symbol_list
*args
= NULL
;
2247 warning(token
->pos
, "non-ANSI definition of function '%s'", show_ident(decl
->ident
));
2249 token
= declaration_list(token
, &args
);
2250 if (!match_op(token
, ';')) {
2251 sparse_error(token
->pos
, "expected ';' at end of parameter declaration");
2254 token
= token
->next
;
2255 } while (lookup_type(token
));
2257 apply_k_r_types(args
, decl
);
2259 if (!match_op(token
, '{')) {
2260 sparse_error(token
->pos
, "expected function body");
2263 return parse_function_body(token
, decl
, list
);
2266 static struct token
*toplevel_asm_declaration(struct token
*token
, struct symbol_list
**list
)
2268 struct symbol
*anon
= alloc_symbol(token
->pos
, SYM_NODE
);
2269 struct symbol
*fn
= alloc_symbol(token
->pos
, SYM_FN
);
2270 struct statement
*stmt
;
2272 anon
->ctype
.base_type
= fn
;
2273 stmt
= alloc_statement(token
->pos
, STMT_NONE
);
2276 token
= parse_asm_statement(token
, stmt
);
2278 add_symbol(list
, anon
);
2282 struct token
*external_declaration(struct token
*token
, struct symbol_list
**list
)
2284 struct ident
*ident
= NULL
;
2285 struct symbol
*decl
;
2286 struct decl_state ctx
= { .ident
= &ident
};
2288 struct symbol
*base_type
;
2291 /* Top-level inline asm? */
2292 if (token_type(token
) == TOKEN_IDENT
) {
2293 struct symbol
*s
= lookup_keyword(token
->ident
, NS_KEYWORD
);
2294 if (s
&& s
->op
->toplevel
)
2295 return s
->op
->toplevel(token
, list
);
2298 /* Parse declaration-specifiers, if any */
2299 token
= declaration_specifiers(token
, &ctx
, 0);
2300 decl
= alloc_symbol(token
->pos
, SYM_NODE
);
2301 /* Just a type declaration? */
2302 if (match_op(token
, ';')) {
2303 apply_modifiers(token
->pos
, &ctx
.ctype
);
2308 token
= declarator(token
, &ctx
);
2309 token
= handle_attributes(token
, &ctx
.ctype
, KW_ATTRIBUTE
| KW_ASM
);
2310 apply_modifiers(token
->pos
, &ctx
.ctype
);
2312 decl
->ctype
= ctx
.ctype
;
2313 decl
->endpos
= token
->pos
;
2315 /* Just a type declaration? */
2317 warning(token
->pos
, "missing identifier in declaration");
2318 return expect(token
, ';', "at the end of type declaration");
2321 /* type define declaration? */
2322 is_typedef
= (saved
.modifiers
& MOD_TYPEDEF
) != 0;
2324 /* Typedefs don't have meaningful storage */
2326 saved
.modifiers
&= ~MOD_STORAGE
;
2327 decl
->ctype
.modifiers
&= ~MOD_STORAGE
;
2328 decl
->ctype
.modifiers
|= MOD_USERTYPE
;
2331 bind_symbol(decl
, ident
, is_typedef
? NS_TYPEDEF
: NS_SYMBOL
);
2333 base_type
= decl
->ctype
.base_type
;
2336 if (base_type
&& !base_type
->ident
) {
2337 switch (base_type
->type
) {
2342 base_type
->ident
= ident
;
2345 } else if (base_type
&& base_type
->type
== SYM_FN
) {
2346 /* K&R argument declaration? */
2347 if (lookup_type(token
))
2348 return parse_k_r_arguments(token
, decl
, list
);
2349 if (match_op(token
, '{'))
2350 return parse_function_body(token
, decl
, list
);
2352 if (!(decl
->ctype
.modifiers
& MOD_STATIC
))
2353 decl
->ctype
.modifiers
|= MOD_EXTERN
;
2354 } else if (base_type
== &void_ctype
&& !(decl
->ctype
.modifiers
& MOD_EXTERN
)) {
2355 sparse_error(token
->pos
, "void declaration");
2359 if (!is_typedef
&& match_op(token
, '=')) {
2360 if (decl
->ctype
.modifiers
& MOD_EXTERN
) {
2361 warning(decl
->pos
, "symbol with external linkage has initializer");
2362 decl
->ctype
.modifiers
&= ~MOD_EXTERN
;
2364 token
= initializer(&decl
->initializer
, token
->next
);
2367 if (!(decl
->ctype
.modifiers
& (MOD_EXTERN
| MOD_INLINE
))) {
2368 add_symbol(list
, decl
);
2369 fn_local_symbol(decl
);
2372 check_declaration(decl
);
2373 if (decl
->same_symbol
)
2374 decl
->definition
= decl
->same_symbol
->definition
;
2376 if (!match_op(token
, ','))
2379 token
= token
->next
;
2381 decl
= alloc_symbol(token
->pos
, SYM_NODE
);
2383 token
= handle_attributes(token
, &ctx
.ctype
, KW_ATTRIBUTE
);
2384 token
= declarator(token
, &ctx
);
2385 token
= handle_attributes(token
, &ctx
.ctype
, KW_ATTRIBUTE
| KW_ASM
);
2386 apply_modifiers(token
->pos
, &ctx
.ctype
);
2387 decl
->ctype
= ctx
.ctype
;
2388 decl
->endpos
= token
->pos
;
2390 sparse_error(token
->pos
, "expected identifier name in type definition");
2395 decl
->ctype
.modifiers
|= MOD_USERTYPE
;
2397 bind_symbol(decl
, ident
, is_typedef
? NS_TYPEDEF
: NS_SYMBOL
);
2399 /* Function declarations are automatically extern unless specifically static */
2400 base_type
= decl
->ctype
.base_type
;
2401 if (!is_typedef
&& base_type
&& base_type
->type
== SYM_FN
) {
2402 if (!(decl
->ctype
.modifiers
& MOD_STATIC
))
2403 decl
->ctype
.modifiers
|= MOD_EXTERN
;
2406 return expect(token
, ';', "at end of declaration");