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
62 static struct token *parse_if_statement(struct token *token, struct statement *stmt);
63 static struct token *parse_return_statement(struct token *token, struct statement *stmt);
64 static struct token *parse_loop_iterator(struct token *token, struct statement *stmt);
65 static struct token *parse_default_statement(struct token *token, struct statement *stmt);
66 static struct token *parse_case_statement(struct token *token, struct statement *stmt);
67 static struct token *parse_switch_statement(struct token *token, struct statement *stmt);
68 static struct token *parse_for_statement(struct token *token, struct statement *stmt);
69 static struct token *parse_while_statement(struct token *token, struct statement *stmt);
70 static struct token *parse_do_statement(struct token *token, struct statement *stmt);
71 static struct token *parse_goto_statement(struct token *token, struct statement *stmt);
72 static struct token *parse_context_statement(struct token *token, struct statement *stmt);
73 static struct token *parse_range_statement(struct token *token, struct statement *stmt);
74 static struct token *parse_asm_statement(struct token *token, struct statement *stmt);
75 static struct token *toplevel_asm_declaration(struct token *token, struct symbol_list **list);
76 static struct token *parse_static_assert(struct token *token, struct symbol_list **unused);
77
78 typedef struct token *attr_t(struct token *, struct symbol *,
79 struct decl_state *);
80
81 static attr_t
82 attribute_packed, attribute_aligned, attribute_modifier,
83 attribute_bitwise,
84 attribute_address_space, attribute_context,
85 attribute_designated_init,
86 attribute_transparent_union, ignore_attribute,
87 attribute_mode, attribute_force;
88
89 typedef struct symbol *to_mode_t(struct symbol *);
90
91 static to_mode_t
92 to_QI_mode, to_HI_mode, to_SI_mode, to_DI_mode, to_TI_mode, to_word_mode;
93
94 enum {
95 Set_T = 1,
96 Set_S = 2,
97 Set_Char = 4,
98 Set_Int = 8,
99 Set_Double = 16,
100 Set_Float = 32,
101 Set_Signed = 64,
102 Set_Unsigned = 128,
103 Set_Short = 256,
104 Set_Long = 512,
105 Set_Vlong = 1024,
106 Set_Int128 = 2048,
107 Set_Any = Set_T | Set_Short | Set_Long | Set_Signed | Set_Unsigned
108 };
109
110 enum {
111 CInt = 0, CSInt, CUInt, CReal, CChar, CSChar, CUChar,
112 };
113
114 enum {
115 SNone = 0, STypedef, SAuto, SRegister, SExtern, SStatic, SForced, SMax,
116 };
117
118 static struct symbol_op typedef_op = {
119 .type = KW_MODIFIER,
120 .declarator = typedef_specifier,
121 };
122
123 static struct symbol_op inline_op = {
124 .type = KW_MODIFIER,
125 .declarator = inline_specifier,
126 };
127
128 static declarator_t noreturn_specifier;
129 static struct symbol_op noreturn_op = {
130 .type = KW_MODIFIER,
131 .declarator = noreturn_specifier,
132 };
133
134 static declarator_t alignas_specifier;
135 static struct symbol_op alignas_op = {
136 .type = KW_MODIFIER,
137 .declarator = alignas_specifier,
138 };
139
140 static struct symbol_op auto_op = {
141 .type = KW_MODIFIER,
142 .declarator = auto_specifier,
143 };
144
145 static struct symbol_op register_op = {
153 };
154
155 static struct symbol_op extern_op = {
156 .type = KW_MODIFIER,
157 .declarator = extern_specifier,
158 };
159
160 static struct symbol_op thread_op = {
161 .type = KW_MODIFIER,
162 .declarator = thread_specifier,
163 };
164
165 static struct symbol_op const_op = {
166 .type = KW_QUALIFIER,
167 .declarator = const_qualifier,
168 };
169
170 static struct symbol_op volatile_op = {
171 .type = KW_QUALIFIER,
172 .declarator = volatile_qualifier,
173 };
174
175 static struct symbol_op restrict_op = {
176 .type = KW_QUALIFIER,
177 };
178
179 static struct symbol_op typeof_op = {
180 .type = KW_SPECIFIER,
181 .declarator = typeof_specifier,
182 .test = Set_Any,
183 .set = Set_S|Set_T,
184 };
185
186 static struct symbol_op attribute_op = {
187 .type = KW_ATTRIBUTE,
188 .declarator = attribute_specifier,
189 };
190
191 static struct symbol_op struct_op = {
192 .type = KW_SPECIFIER,
193 .declarator = struct_specifier,
194 .test = Set_Any,
195 .set = Set_S|Set_T,
196 };
197
198 static struct symbol_op union_op = {
218 static struct symbol_op char_op = {
219 .type = KW_SPECIFIER,
220 .test = Set_T|Set_Long|Set_Short,
221 .set = Set_T|Set_Char,
222 .class = CChar,
223 };
224
225 static struct symbol_op int_op = {
226 .type = KW_SPECIFIER,
227 .test = Set_T,
228 .set = Set_T|Set_Int,
229 };
230
231 static struct symbol_op double_op = {
232 .type = KW_SPECIFIER,
233 .test = Set_T|Set_Signed|Set_Unsigned|Set_Short|Set_Vlong,
234 .set = Set_T|Set_Double,
235 .class = CReal,
236 };
237
238 /* FIXME: this is not even slightly right. */
239 static struct symbol_op complex_op = {
240 .type = KW_SPECIFIER,
241 .test = 0, //Set_T|Set_Signed|Set_Unsigned|Set_Short|Set_Vlong,
242 .set = 0, //Set_Double, //Set_T,Set_Double,
243 .class = CReal,
244 };
245
246 static struct symbol_op float_op = {
247 .type = KW_SPECIFIER | KW_SHORT,
248 .test = Set_T|Set_Signed|Set_Unsigned|Set_Short|Set_Long,
249 .set = Set_T|Set_Float,
250 .class = CReal,
251 };
252
253 static struct symbol_op short_op = {
254 .type = KW_SPECIFIER | KW_SHORT,
255 .test = Set_S|Set_Char|Set_Float|Set_Double|Set_Long|Set_Short,
256 .set = Set_Short,
257 };
258
259 static struct symbol_op signed_op = {
260 .type = KW_SPECIFIER,
261 .test = Set_S|Set_Float|Set_Double|Set_Signed|Set_Unsigned,
262 .set = Set_Signed,
263 .class = CSInt,
264 };
265
336 .statement = parse_asm_statement,
337 .toplevel = toplevel_asm_declaration,
338 };
339
340 static struct symbol_op static_assert_op = {
341 .toplevel = parse_static_assert,
342 };
343
344 static struct symbol_op packed_op = {
345 .attribute = attribute_packed,
346 };
347
348 static struct symbol_op aligned_op = {
349 .attribute = attribute_aligned,
350 };
351
352 static struct symbol_op attr_mod_op = {
353 .attribute = attribute_modifier,
354 };
355
356 static struct symbol_op attr_bitwise_op = {
357 .attribute = attribute_bitwise,
358 };
359
360 static struct symbol_op attr_force_op = {
361 .attribute = attribute_force,
362 };
363
364 static struct symbol_op address_space_op = {
365 .attribute = attribute_address_space,
366 };
367
368 static struct symbol_op mode_op = {
369 .attribute = attribute_mode,
370 };
371
372 static struct symbol_op context_op = {
373 .attribute = attribute_context,
374 };
375
393 static struct symbol_op mode_HI_op = {
394 .type = KW_MODE,
395 .to_mode = to_HI_mode
396 };
397
398 static struct symbol_op mode_SI_op = {
399 .type = KW_MODE,
400 .to_mode = to_SI_mode
401 };
402
403 static struct symbol_op mode_DI_op = {
404 .type = KW_MODE,
405 .to_mode = to_DI_mode
406 };
407
408 static struct symbol_op mode_TI_op = {
409 .type = KW_MODE,
410 .to_mode = to_TI_mode
411 };
412
413 static struct symbol_op mode_word_op = {
414 .type = KW_MODE,
415 .to_mode = to_word_mode
416 };
417
418 /* Using NS_TYPEDEF will also make the keyword a reserved one */
419 static struct init_keyword {
420 const char *name;
421 enum namespace ns;
422 unsigned long modifiers;
423 struct symbol_op *op;
424 struct symbol *type;
425 } keyword_table[] = {
426 /* Type qualifiers */
427 { "const", NS_TYPEDEF, .op = &const_op },
428 { "__const", NS_TYPEDEF, .op = &const_op },
429 { "__const__", NS_TYPEDEF, .op = &const_op },
430 { "volatile", NS_TYPEDEF, .op = &volatile_op },
431 { "__volatile", NS_TYPEDEF, .op = &volatile_op },
432 { "__volatile__", NS_TYPEDEF, .op = &volatile_op },
433
434 /* Typedef.. */
435 { "typedef", NS_TYPEDEF, .op = &typedef_op },
436
437 /* Type specifiers */
438 { "void", NS_TYPEDEF, .type = &void_ctype, .op = &spec_op},
439 { "char", NS_TYPEDEF, .op = &char_op },
440 { "short", NS_TYPEDEF, .op = &short_op },
441 { "int", NS_TYPEDEF, .op = &int_op },
442 { "long", NS_TYPEDEF, .op = &long_op },
443 { "float", NS_TYPEDEF, .op = &float_op },
444 { "double", NS_TYPEDEF, .op = &double_op },
445 { "signed", NS_TYPEDEF, .op = &signed_op },
446 { "__signed", NS_TYPEDEF, .op = &signed_op },
447 { "__signed__", NS_TYPEDEF, .op = &signed_op },
448 { "unsigned", NS_TYPEDEF, .op = &unsigned_op },
449 { "__int128", NS_TYPEDEF, .op = &int128_op },
450 { "_Bool", NS_TYPEDEF, .type = &bool_ctype, .op = &spec_op },
451 { "_Complex", NS_TYPEDEF, .op = &complex_op },
452
453 /* Predeclared types */
454 { "__builtin_va_list", NS_TYPEDEF, .type = &ptr_ctype, .op = &spec_op },
455 { "__builtin_ms_va_list", NS_TYPEDEF, .type = &ptr_ctype, .op = &spec_op },
456 { "__int128_t", NS_TYPEDEF, .type = &lllong_ctype, .op = &spec_op },
457 { "__uint128_t",NS_TYPEDEF, .type = &ulllong_ctype, .op = &spec_op },
458
459 /* Extended types */
460 { "typeof", NS_TYPEDEF, .op = &typeof_op },
461 { "__typeof", NS_TYPEDEF, .op = &typeof_op },
462 { "__typeof__", NS_TYPEDEF, .op = &typeof_op },
463
464 { "__attribute", NS_TYPEDEF, .op = &attribute_op },
465 { "__attribute__", NS_TYPEDEF, .op = &attribute_op },
466
467 { "struct", NS_TYPEDEF, .op = &struct_op },
468 { "union", NS_TYPEDEF, .op = &union_op },
469 { "enum", NS_TYPEDEF, .op = &enum_op },
470
471 { "inline", NS_TYPEDEF, .op = &inline_op },
472 { "__inline", NS_TYPEDEF, .op = &inline_op },
473 { "__inline__", NS_TYPEDEF, .op = &inline_op },
474
475 { "_Noreturn", NS_TYPEDEF, .op = &noreturn_op },
476
477 { "_Alignas", NS_TYPEDEF, .op = &alignas_op },
478
479 /* Ignored for now.. */
480 { "restrict", NS_TYPEDEF, .op = &restrict_op},
481 { "__restrict", NS_TYPEDEF, .op = &restrict_op},
482 { "__restrict__", NS_TYPEDEF, .op = &restrict_op},
483
484 /* Static assertion */
485 { "_Static_assert", NS_KEYWORD, .op = &static_assert_op },
486
487 /* Storage class */
488 { "auto", NS_TYPEDEF, .op = &auto_op },
489 { "register", NS_TYPEDEF, .op = ®ister_op },
490 { "static", NS_TYPEDEF, .op = &static_op },
491 { "extern", NS_TYPEDEF, .op = &extern_op },
492 { "__thread", NS_TYPEDEF, .op = &thread_op },
493 { "_Thread_local", NS_TYPEDEF, .op = &thread_op },
494
495 /* Statement */
496 { "if", NS_KEYWORD, .op = &if_op },
497 { "return", NS_KEYWORD, .op = &return_op },
498 { "break", NS_KEYWORD, .op = &loop_iter_op },
499 { "continue", NS_KEYWORD, .op = &loop_iter_op },
500 { "default", NS_KEYWORD, .op = &default_op },
501 { "case", NS_KEYWORD, .op = &case_op },
502 { "switch", NS_KEYWORD, .op = &switch_op },
503 { "for", NS_KEYWORD, .op = &for_op },
505 { "do", NS_KEYWORD, .op = &do_op },
506 { "goto", NS_KEYWORD, .op = &goto_op },
507 { "__context__",NS_KEYWORD, .op = &__context___op },
508 { "__range__", NS_KEYWORD, .op = &range_op },
509 { "asm", NS_KEYWORD, .op = &asm_op },
510 { "__asm", NS_KEYWORD, .op = &asm_op },
511 { "__asm__", NS_KEYWORD, .op = &asm_op },
512
513 /* Attribute */
514 { "packed", NS_KEYWORD, .op = &packed_op },
515 { "__packed__", NS_KEYWORD, .op = &packed_op },
516 { "aligned", NS_KEYWORD, .op = &aligned_op },
517 { "__aligned__",NS_KEYWORD, .op = &aligned_op },
518 { "nocast", NS_KEYWORD, MOD_NOCAST, .op = &attr_mod_op },
519 { "noderef", NS_KEYWORD, MOD_NODEREF, .op = &attr_mod_op },
520 { "safe", NS_KEYWORD, MOD_SAFE, .op = &attr_mod_op },
521 { "force", NS_KEYWORD, .op = &attr_force_op },
522 { "bitwise", NS_KEYWORD, MOD_BITWISE, .op = &attr_bitwise_op },
523 { "__bitwise__",NS_KEYWORD, MOD_BITWISE, .op = &attr_bitwise_op },
524 { "address_space",NS_KEYWORD, .op = &address_space_op },
525 { "mode", NS_KEYWORD, .op = &mode_op },
526 { "context", NS_KEYWORD, .op = &context_op },
527 { "designated_init", NS_KEYWORD, .op = &designated_init_op },
528 { "__transparent_union__", NS_KEYWORD, .op = &transparent_union_op },
529 { "noreturn", NS_KEYWORD, MOD_NORETURN, .op = &attr_mod_op },
530 { "__noreturn__", NS_KEYWORD, MOD_NORETURN, .op = &attr_mod_op },
531 { "pure", NS_KEYWORD, MOD_PURE, .op = &attr_mod_op },
532 {"__pure__", NS_KEYWORD, MOD_PURE, .op = &attr_mod_op },
533 {"const", NS_KEYWORD, MOD_PURE, .op = &attr_mod_op },
534 {"__const", NS_KEYWORD, MOD_PURE, .op = &attr_mod_op },
535 {"__const__", NS_KEYWORD, MOD_PURE, .op = &attr_mod_op },
536
537 { "__mode__", NS_KEYWORD, .op = &mode_op },
538 { "QI", NS_KEYWORD, MOD_CHAR, .op = &mode_QI_op },
539 { "__QI__", NS_KEYWORD, MOD_CHAR, .op = &mode_QI_op },
540 { "HI", NS_KEYWORD, MOD_SHORT, .op = &mode_HI_op },
541 { "__HI__", NS_KEYWORD, MOD_SHORT, .op = &mode_HI_op },
542 { "SI", NS_KEYWORD, .op = &mode_SI_op },
543 { "__SI__", NS_KEYWORD, .op = &mode_SI_op },
544 { "DI", NS_KEYWORD, MOD_LONGLONG, .op = &mode_DI_op },
545 { "__DI__", NS_KEYWORD, MOD_LONGLONG, .op = &mode_DI_op },
546 { "TI", NS_KEYWORD, MOD_LONGLONGLONG, .op = &mode_TI_op },
547 { "__TI__", NS_KEYWORD, MOD_LONGLONGLONG, .op = &mode_TI_op },
548 { "word", NS_KEYWORD, MOD_LONG, .op = &mode_word_op },
549 { "__word__", NS_KEYWORD, MOD_LONG, .op = &mode_word_op },
550 };
551
552
553 static const char *ignored_attributes[] = {
554
555 #define GCC_ATTR(x) \
556 STRINGIFY(x), \
557 STRINGIFY(__##x##__),
558
559 #include "gcc-attr-list.h"
560
561 #undef GCC_ATTR
562
563 "bounded",
564 "__bounded__",
565 "__noclone",
566 "__nonnull",
567 "__nothrow",
568 };
569
750 last = field;
751 } END_FOR_EACH_PTR(field);
752 return res;
753 }
754
755 static struct token *parse_union_declaration(struct token *token, struct symbol *sym)
756 {
757 return struct_declaration_list(token, &sym->symbol_list);
758 }
759
760 static struct token *struct_specifier(struct token *token, struct decl_state *ctx)
761 {
762 return struct_union_enum_specifier(SYM_STRUCT, token, ctx, parse_struct_declaration);
763 }
764
765 static struct token *union_specifier(struct token *token, struct decl_state *ctx)
766 {
767 return struct_union_enum_specifier(SYM_UNION, token, ctx, parse_union_declaration);
768 }
769
770
771 typedef struct {
772 int x;
773 unsigned long long y;
774 } Num;
775
776 static void upper_boundary(Num *n, Num *v)
777 {
778 if (n->x > v->x)
779 return;
780 if (n->x < v->x) {
781 *n = *v;
782 return;
783 }
784 if (n->y < v->y)
785 n->y = v->y;
786 }
787
788 static void lower_boundary(Num *n, Num *v)
789 {
790 if (n->x < v->x)
791 return;
792 if (n->x > v->x) {
793 *n = *v;
794 return;
795 }
796 if (n->y > v->y)
797 n->y = v->y;
798 }
799
800 static int type_is_ok(struct symbol *type, Num *upper, Num *lower)
801 {
802 int shift = type->bit_size;
803 int is_unsigned = type->ctype.modifiers & MOD_UNSIGNED;
804
805 if (!is_unsigned)
806 shift--;
807 if (upper->x == 0 && upper->y >> shift)
808 return 0;
809 if (lower->x == 0 || (!is_unsigned && (~lower->y >> shift) == 0))
810 return 1;
811 return 0;
812 }
813
814 static struct symbol *bigger_enum_type(struct symbol *s1, struct symbol *s2)
815 {
816 if (s1->bit_size < s2->bit_size) {
817 s1 = s2;
818 } else if (s1->bit_size == s2->bit_size) {
819 if (s2->ctype.modifiers & MOD_UNSIGNED)
820 s1 = s2;
821 }
822 if (s1->bit_size < bits_in_int)
823 return &int_ctype;
824 return s1;
825 }
826
827 static void cast_enum_list(struct symbol_list *list, struct symbol *base_type)
828 {
829 struct symbol *sym;
830
831 FOR_EACH_PTR(list, sym) {
832 struct expression *expr = sym->initializer;
833 struct symbol *ctype;
834 if (expr->type != EXPR_VALUE)
835 continue;
836 ctype = expr->ctype;
837 if (ctype->bit_size == base_type->bit_size)
838 continue;
839 cast_value(expr, base_type, expr, ctype);
840 } END_FOR_EACH_PTR(sym);
841 }
842
843 static struct token *parse_enum_declaration(struct token *token, struct symbol *parent)
844 {
845 unsigned long long lastval = 0;
846 struct symbol *ctype = NULL, *base_type = NULL;
847 Num upper = {-1, 0}, lower = {1, 0};
848
849 parent->examined = 1;
850 parent->ctype.base_type = &int_ctype;
851 while (token_type(token) == TOKEN_IDENT) {
852 struct expression *expr = NULL;
853 struct token *next = token->next;
854 struct symbol *sym;
855
856 if (match_op(next, '=')) {
857 next = constant_expression(next->next, &expr);
858 lastval = get_expression_value(expr);
859 ctype = &void_ctype;
860 if (expr && expr->ctype)
861 ctype = expr->ctype;
862 } else if (!ctype) {
863 ctype = &int_ctype;
864 } else if (is_int_type(ctype)) {
865 lastval++;
866 } else {
867 error_die(token->pos, "can't increment the last enum member");
883
884 if (base_type != &bad_ctype) {
885 if (ctype->type == SYM_NODE)
886 ctype = ctype->ctype.base_type;
887 if (ctype->type == SYM_ENUM) {
888 if (ctype == parent)
889 ctype = base_type;
890 else
891 ctype = ctype->ctype.base_type;
892 }
893 /*
894 * base_type rules:
895 * - if all enums are of the same type, then
896 * the base_type is that type (two first
897 * cases)
898 * - if enums are of different types, they
899 * all have to be integer types, and the
900 * base type is at least "int_ctype".
901 * - otherwise the base_type is "bad_ctype".
902 */
903 if (!base_type) {
904 base_type = ctype;
905 } else if (ctype == base_type) {
906 /* nothing */
907 } else if (is_int_type(base_type) && is_int_type(ctype)) {
908 base_type = bigger_enum_type(base_type, ctype);
909 } else
910 base_type = &bad_ctype;
911 parent->ctype.base_type = base_type;
912 }
913 if (is_int_type(base_type)) {
914 Num v = {.y = lastval};
915 if (ctype->ctype.modifiers & MOD_UNSIGNED)
916 v.x = 0;
917 else if ((long long)lastval >= 0)
918 v.x = 0;
919 else
920 v.x = -1;
921 upper_boundary(&upper, &v);
922 lower_boundary(&lower, &v);
923 }
924 token = next;
925
926 sym->endpos = token->pos;
927
928 if (!match_op(token, ','))
929 break;
930 token = token->next;
931 }
932 if (!base_type) {
933 sparse_error(token->pos, "bad enum definition");
934 base_type = &bad_ctype;
935 }
936 else if (!is_int_type(base_type))
937 base_type = base_type;
938 else if (type_is_ok(base_type, &upper, &lower))
939 base_type = base_type;
940 else if (type_is_ok(&int_ctype, &upper, &lower))
941 base_type = &int_ctype;
942 else if (type_is_ok(&uint_ctype, &upper, &lower))
943 base_type = &uint_ctype;
944 else if (type_is_ok(&long_ctype, &upper, &lower))
945 base_type = &long_ctype;
946 else if (type_is_ok(&ulong_ctype, &upper, &lower))
947 base_type = &ulong_ctype;
948 else if (type_is_ok(&llong_ctype, &upper, &lower))
949 base_type = &llong_ctype;
950 else if (type_is_ok(&ullong_ctype, &upper, &lower))
951 base_type = &ullong_ctype;
952 else
953 base_type = &bad_ctype;
954 parent->ctype.base_type = base_type;
955 parent->ctype.modifiers |= (base_type->ctype.modifiers & MOD_UNSIGNED);
956 parent->examined = 0;
957
958 cast_enum_list(parent->symbol_list, base_type);
959
960 return token;
961 }
962
963 static struct token *enum_specifier(struct token *token, struct decl_state *ctx)
964 {
965 struct token *ret = struct_union_enum_specifier(SYM_ENUM, token, ctx, parse_enum_declaration);
966 struct ctype *ctype = &ctx->ctype.base_type->ctype;
967
968 if (!ctype->base_type)
969 ctype->base_type = &incomplete_ctype;
970
971 return ret;
972 }
973
974 static struct token *typeof_specifier(struct token *token, struct decl_state *ctx)
975 {
976 struct symbol *sym;
977
1026 warning(token->pos, "I don't like non-power-of-2 alignments");
1027 return token;
1028 } else if (alignment > ctx->ctype.alignment)
1029 ctx->ctype.alignment = alignment;
1030 return token;
1031 }
1032
1033 static void apply_qualifier(struct position *pos, struct ctype *ctx, unsigned long qual)
1034 {
1035 if (ctx->modifiers & qual)
1036 warning(*pos, "duplicate %s", modifier_string(qual));
1037 ctx->modifiers |= qual;
1038 }
1039
1040 static struct token *attribute_modifier(struct token *token, struct symbol *attr, struct decl_state *ctx)
1041 {
1042 apply_qualifier(&token->pos, &ctx->ctype, attr->ctype.modifiers);
1043 return token;
1044 }
1045
1046 static struct token *attribute_bitwise(struct token *token, struct symbol *attr, struct decl_state *ctx)
1047 {
1048 if (Wbitwise)
1049 attribute_modifier(token, attr, ctx);
1050 return token;
1051 }
1052
1053 static struct token *attribute_address_space(struct token *token, struct symbol *attr, struct decl_state *ctx)
1054 {
1055 struct expression *expr = NULL;
1056 int as;
1057 token = expect(token, '(', "after address_space attribute");
1058 token = conditional_expression(token, &expr);
1059 if (expr) {
1060 as = const_expression_value(expr);
1061 if (Waddress_space && as)
1062 ctx->ctype.as = as;
1063 }
1064 token = expect(token, ')', "after address_space attribute");
1065 return token;
1066 }
1067
1068 static struct symbol *to_QI_mode(struct symbol *ctype)
1069 {
1070 if (ctype->ctype.base_type != &int_type)
1071 return NULL;
1072 if (ctype == &char_ctype)
1073 return ctype;
1074 return ctype->ctype.modifiers & MOD_UNSIGNED ? &uchar_ctype
1075 : &schar_ctype;
1076 }
1077
1078 static struct symbol *to_HI_mode(struct symbol *ctype)
1079 {
1080 if (ctype->ctype.base_type != &int_type)
1081 return NULL;
1082 return ctype->ctype.modifiers & MOD_UNSIGNED ? &ushort_ctype
1083 : &sshort_ctype;
1084 }
1090 return ctype->ctype.modifiers & MOD_UNSIGNED ? &uint_ctype
1091 : &sint_ctype;
1092 }
1093
1094 static struct symbol *to_DI_mode(struct symbol *ctype)
1095 {
1096 if (ctype->ctype.base_type != &int_type)
1097 return NULL;
1098 return ctype->ctype.modifiers & MOD_UNSIGNED ? &ullong_ctype
1099 : &sllong_ctype;
1100 }
1101
1102 static struct symbol *to_TI_mode(struct symbol *ctype)
1103 {
1104 if (ctype->ctype.base_type != &int_type)
1105 return NULL;
1106 return ctype->ctype.modifiers & MOD_UNSIGNED ? &ulllong_ctype
1107 : &slllong_ctype;
1108 }
1109
1110 static struct symbol *to_word_mode(struct symbol *ctype)
1111 {
1112 if (ctype->ctype.base_type != &int_type)
1113 return NULL;
1114 return ctype->ctype.modifiers & MOD_UNSIGNED ? &ulong_ctype
1115 : &slong_ctype;
1116 }
1117
1118 static struct token *attribute_mode(struct token *token, struct symbol *attr, struct decl_state *ctx)
1119 {
1120 token = expect(token, '(', "after mode attribute");
1121 if (token_type(token) == TOKEN_IDENT) {
1122 struct symbol *mode = lookup_keyword(token->ident, NS_KEYWORD);
1123 if (mode && mode->op->type == KW_MODE)
1124 ctx->mode = mode->op;
1125 else
1126 sparse_error(token->pos, "unknown mode attribute %s\n", show_ident(token->ident));
1127 token = token->next;
1128 } else
1129 sparse_error(token->pos, "expect attribute mode symbol\n");
1130 token = expect(token, ')', "after mode attribute");
1131 return token;
1132 }
1133
1134 static struct token *attribute_context(struct token *token, struct symbol *attr, struct decl_state *ctx)
1135 {
1136 struct context *context = alloc_context();
1137 struct expression *args[3];
1138 int argc = 0;
1139
1140 token = expect(token, '(', "after context attribute");
1141 while (!match_op(token, ')')) {
1142 struct expression *expr = NULL;
1143 token = conditional_expression(token, &expr);
1144 if (!expr)
1145 break;
1146 if (argc < 3)
1147 args[argc++] = expr;
1148 if (!match_op(token, ','))
1149 break;
1150 token = token->next;
1151 }
1152
1153 switch(argc) {
1154 case 0:
1155 sparse_error(token->pos, "expected context input/output values");
1156 break;
1157 case 1:
1158 context->in = get_expression_value(args[0]);
1159 break;
1160 case 2:
1161 context->in = get_expression_value(args[0]);
1162 context->out = get_expression_value(args[1]);
1163 break;
1164 case 3:
1165 context->context = args[0];
1166 context->in = get_expression_value(args[1]);
1167 context->out = get_expression_value(args[2]);
1168 break;
1169 }
1170
1171 if (argc)
1172 add_ptr_list(&ctx->ctype.contexts, context);
1173
1174 token = expect(token, ')', "after context attribute");
1175 return token;
1176 }
1177
1178 static struct token *attribute_designated_init(struct token *token, struct symbol *attr, struct decl_state *ctx)
1179 {
1180 if (ctx->ctype.base_type && ctx->ctype.base_type->type == SYM_STRUCT)
1181 ctx->ctype.base_type->designated_init = 1;
1182 else
1183 warning(token->pos, "attribute designated_init applied to non-structure type");
1184 return token;
1185 }
1186
1187 static struct token *attribute_transparent_union(struct token *token, struct symbol *attr, struct decl_state *ctx)
1188 {
1189 if (Wtransparent_union)
1190 warning(token->pos, "attribute __transparent_union__");
1191
1192 if (ctx->ctype.base_type && ctx->ctype.base_type->type == SYM_UNION)
1193 ctx->ctype.base_type->transparent_union = 1;
1194 else
1195 warning(token->pos, "attribute __transparent_union__ applied to non-union type");
1196 return token;
1197 }
1198
1199 static struct token *recover_unknown_attribute(struct token *token)
1200 {
1201 struct expression *expr = NULL;
1202
1203 if (Wunknown_attribute)
1204 warning(token->pos, "attribute '%s': unknown attribute", show_ident(token->ident));
1205 token = token->next;
1206 if (match_op(token, '('))
1207 token = parens_expression(token, &expr, "in attribute");
1208 return token;
1209 }
1210
1211 static struct token *attribute_specifier(struct token *token, struct decl_state *ctx)
1212 {
1213 token = expect(token, '(', "after attribute");
1214 token = expect(token, '(', "after attribute");
1215
1216 for (;;) {
1217 struct ident *attribute_name;
1218 struct symbol *attr;
1219
1220 if (eof_token(token))
1221 break;
1222 if (match_op(token, ';'))
1223 break;
1224 if (token_type(token) != TOKEN_IDENT)
1243 static const char *storage_class[] =
1244 {
1245 [STypedef] = "typedef",
1246 [SAuto] = "auto",
1247 [SExtern] = "extern",
1248 [SStatic] = "static",
1249 [SRegister] = "register",
1250 [SForced] = "[force]"
1251 };
1252
1253 static unsigned long storage_modifiers(struct decl_state *ctx)
1254 {
1255 static unsigned long mod[SMax] =
1256 {
1257 [SAuto] = MOD_AUTO,
1258 [SExtern] = MOD_EXTERN,
1259 [SStatic] = MOD_STATIC,
1260 [SRegister] = MOD_REGISTER
1261 };
1262 return mod[ctx->storage_class] | (ctx->is_inline ? MOD_INLINE : 0)
1263 | (ctx->is_tls ? MOD_TLS : 0);
1264 }
1265
1266 static void set_storage_class(struct position *pos, struct decl_state *ctx, int class)
1267 {
1268 /* __thread can be used alone, or with extern or static */
1269 if (ctx->is_tls && (class != SStatic && class != SExtern)) {
1270 sparse_error(*pos, "__thread can only be used alone, or with "
1271 "extern or static");
1272 return;
1273 }
1274
1275 if (!ctx->storage_class) {
1276 ctx->storage_class = class;
1277 return;
1278 }
1279 if (ctx->storage_class == class)
1280 sparse_error(*pos, "duplicate %s", storage_class[class]);
1281 else
1282 sparse_error(*pos, "multiple storage classes");
1283 }
1374 warning(token->pos, "non-power-of-2 alignment");
1375 return token;
1376 }
1377 if (alignment > ctx->ctype.alignment)
1378 ctx->ctype.alignment = alignment;
1379 return token;
1380 }
1381
1382 static struct token *const_qualifier(struct token *next, struct decl_state *ctx)
1383 {
1384 apply_qualifier(&next->pos, &ctx->ctype, MOD_CONST);
1385 return next;
1386 }
1387
1388 static struct token *volatile_qualifier(struct token *next, struct decl_state *ctx)
1389 {
1390 apply_qualifier(&next->pos, &ctx->ctype, MOD_VOLATILE);
1391 return next;
1392 }
1393
1394 static void apply_ctype(struct position pos, struct ctype *thistype, struct ctype *ctype)
1395 {
1396 unsigned long mod = thistype->modifiers;
1397
1398 if (mod)
1399 apply_qualifier(&pos, ctype, mod);
1400
1401 /* Context */
1402 concat_ptr_list((struct ptr_list *)thistype->contexts,
1403 (struct ptr_list **)&ctype->contexts);
1404
1405 /* Alignment */
1406 if (thistype->alignment > ctype->alignment)
1407 ctype->alignment = thistype->alignment;
1408
1409 /* Address space */
1410 if (thistype->as)
1411 ctype->as = thistype->as;
1412 }
1413
1688 struct ident **n,
1689 int prefer_abstract)
1690 {
1691 struct token *next = token->next;
1692
1693 if (token_type(next) == TOKEN_IDENT) {
1694 if (lookup_type(next))
1695 return Proto;
1696 /* identifier list not in definition; complain */
1697 if (prefer_abstract)
1698 warning(token->pos,
1699 "identifier list not in definition");
1700 return K_R;
1701 }
1702
1703 if (token_type(next) != TOKEN_SPECIAL)
1704 return Bad_Func;
1705
1706 if (next->special == ')') {
1707 /* don't complain about those */
1708 if (!n || match_op(next->next, ';'))
1709 if (!n || match_op(next->next, ';') || match_op(next->next, ','))
1710 return Empty;
1711 if (Wstrict_prototypes)
1712 warning(next->pos,
1713 "non-ANSI function declaration of function '%s'",
1714 show_ident(*n));
1715 return Empty;
1716 }
1717
1718 if (next->special == SPECIAL_ELLIPSIS) {
1719 warning(next->pos,
1720 "variadic functions must have one named argument");
1721 return Proto;
1722 }
1723
1724 return Bad_Func;
1725 }
1726
1727 static struct token *direct_declarator(struct token *token, struct decl_state *ctx)
1728 {
1764 struct symbol *array;
1765 array = alloc_indirect_symbol(token->pos, ctype, SYM_ARRAY);
1766 token = abstract_array_declarator(token->next, array);
1767 token = expect(token, ']', "in abstract_array_declarator");
1768 array->endpos = token->pos;
1769 ctype = &array->ctype;
1770 }
1771 return token;
1772 }
1773
1774 static struct token *pointer(struct token *token, struct decl_state *ctx)
1775 {
1776 while (match_op(token,'*')) {
1777 struct symbol *ptr = alloc_symbol(token->pos, SYM_PTR);
1778 ptr->ctype.modifiers = ctx->ctype.modifiers;
1779 ptr->ctype.base_type = ctx->ctype.base_type;
1780 ptr->ctype.as = ctx->ctype.as;
1781 ptr->ctype.contexts = ctx->ctype.contexts;
1782 ctx->ctype.modifiers = 0;
1783 ctx->ctype.base_type = ptr;
1784 ctx->ctype.as = 0;
1785 ctx->ctype.contexts = NULL;
1786 ctx->ctype.alignment = 0;
1787
1788 token = handle_qualifiers(token->next, ctx);
1789 ctx->ctype.base_type->endpos = token->pos;
1790 }
1791 return token;
1792 }
1793
1794 static struct token *declarator(struct token *token, struct decl_state *ctx)
1795 {
1796 token = pointer(token, ctx);
1797 return direct_declarator(token, ctx);
1798 }
1799
1800 static struct token *handle_bitfield(struct token *token, struct decl_state *ctx)
1801 {
1802 struct ctype *ctype = &ctx->ctype;
1803 struct expression *expr;
1804 struct symbol *bitfield;
1947 if (next->pos.type != TOKEN_STRING)
1948 return next;
1949 next = next->next;
1950 if (!match_op(next, ')'))
1951 return next;
1952 return next->next;
1953 }
1954
1955 static struct token *expression_statement(struct token *token, struct expression **tree)
1956 {
1957 if (match_ident(token, &_Pragma_ident))
1958 return parse_underscore_Pragma(token);
1959
1960 token = parse_expression(token, tree);
1961 return expect(token, ';', "at end of statement");
1962 }
1963
1964 static struct token *parse_asm_operands(struct token *token, struct statement *stmt,
1965 struct expression_list **inout)
1966 {
1967 struct expression *expr;
1968
1969 /* Allow empty operands */
1970 if (match_op(token->next, ':') || match_op(token->next, ')'))
1971 return token->next;
1972 do {
1973 struct ident *ident = NULL;
1974 if (match_op(token->next, '[') &&
1975 token_type(token->next->next) == TOKEN_IDENT &&
1976 match_op(token->next->next->next, ']')) {
1977 ident = token->next->next->ident;
1978 token = token->next->next->next;
1979 }
1980 add_expression(inout, (struct expression *)ident); /* UGGLEE!!! */
1981 token = primary_expression(token->next, &expr);
1982 add_expression(inout, expr);
1983 token = parens_expression(token, &expr, "in asm parameter");
1984 add_expression(inout, expr);
1985 } while (match_op(token, ','));
1986 return token;
1987 }
1988
1989 static struct token *parse_asm_clobbers(struct token *token, struct statement *stmt,
1990 struct expression_list **clobbers)
1991 {
1992 struct expression *expr;
1993
1994 do {
1995 token = primary_expression(token->next, &expr);
1996 if (expr)
1997 add_expression(clobbers, expr);
1998 } while (match_op(token, ','));
1999 return token;
2000 }
2001
2002 static struct token *parse_asm_labels(struct token *token, struct statement *stmt,
2003 struct symbol_list **labels)
2004 {
2005 struct symbol *label;
2006
2007 do {
2008 token = token->next; /* skip ':' and ',' */
2009 if (token_type(token) != TOKEN_IDENT)
2010 return token;
2011 label = label_symbol(token);
2012 add_symbol(labels, label);
2013 token = token->next;
2014 } while (match_op(token, ','));
2015 return token;
2016 }
2017
2018 static struct token *parse_asm_statement(struct token *token, struct statement *stmt)
2019 {
2020 int is_goto = 0;
2021
2022 token = token->next;
2023 stmt->type = STMT_ASM;
2024 if (match_idents(token, &__volatile___ident, &__volatile_ident, &volatile_ident, NULL)) {
2025 token = token->next;
2026 }
2027 if (token_type(token) == TOKEN_IDENT && token->ident == &goto_ident) {
2028 is_goto = 1;
2029 token = token->next;
2030 }
2031 token = expect(token, '(', "after asm");
2032 token = parse_expression(token, &stmt->asm_string);
2033 if (match_op(token, ':'))
2034 token = parse_asm_operands(token, stmt, &stmt->asm_outputs);
2035 if (match_op(token, ':'))
2036 token = parse_asm_operands(token, stmt, &stmt->asm_inputs);
2037 if (match_op(token, ':'))
2038 token = parse_asm_clobbers(token, stmt, &stmt->asm_clobbers);
2039 if (is_goto && match_op(token, ':'))
2040 token = parse_asm_labels(token, stmt, &stmt->asm_labels);
2041 token = expect(token, ')', "after asm");
2042 return expect(token, ';', "at end of asm-statement");
2043 }
2044
2045 static struct token *parse_asm_declarator(struct token *token, struct decl_state *ctx)
2046 {
2047 struct expression *expr;
2048 token = expect(token, '(', "after asm");
2049 token = parse_expression(token->next, &expr);
2050 token = expect(token, ')', "after asm");
2051 return token;
2052 }
2053
2054 static struct token *parse_static_assert(struct token *token, struct symbol_list **unused)
2055 {
2056 struct expression *cond = NULL, *message = NULL;
2057
2058 token = expect(token->next, '(', "after _Static_assert");
2059 token = constant_expression(token, &cond);
2112 stmt->type = STMT_ITERATOR;
2113 stmt->iterator_break = brk;
2114 stmt->iterator_continue = cont;
2115 fn_local_symbol(brk);
2116 fn_local_symbol(cont);
2117 }
2118
2119 static void end_iterator(struct statement *stmt)
2120 {
2121 end_symbol_scope();
2122 }
2123
2124 static struct statement *start_function(struct symbol *sym)
2125 {
2126 struct symbol *ret;
2127 struct statement *stmt = alloc_statement(sym->pos, STMT_COMPOUND);
2128
2129 start_function_scope(sym->pos);
2130 ret = alloc_symbol(sym->pos, SYM_NODE);
2131 ret->ctype = sym->ctype.base_type->ctype;
2132 ret->ctype.modifiers &= ~(MOD_STORAGE | MOD_CONST | MOD_VOLATILE | MOD_TLS | MOD_INLINE | MOD_ADDRESSABLE | MOD_NOCAST | MOD_NODEREF | MOD_ACCESSED | MOD_TOPLEVEL);
2133 ret->ctype.modifiers |= (MOD_AUTO | MOD_REGISTER);
2134 bind_symbol(ret, &return_ident, NS_ITERATOR);
2135 stmt->ret = ret;
2136 fn_local_symbol(ret);
2137
2138 // Currently parsed symbol for __func__/__FUNCTION__/__PRETTY_FUNCTION__
2139 current_fn = sym;
2140
2141 return stmt;
2142 }
2143
2144 static void end_function(struct symbol *sym)
2145 {
2146 current_fn = NULL;
2147 end_function_scope();
2148 }
2149
2150 /*
2151 * A "switch()" statement, like an iterator, has a
2152 * the "break" symbol associated with it. It works
2355
2356 static struct token *parse_goto_statement(struct token *token, struct statement *stmt)
2357 {
2358 stmt->type = STMT_GOTO;
2359 token = token->next;
2360 if (match_op(token, '*')) {
2361 token = parse_expression(token->next, &stmt->goto_expression);
2362 add_statement(&function_computed_goto_list, stmt);
2363 } else if (token_type(token) == TOKEN_IDENT) {
2364 stmt->goto_label = label_symbol(token);
2365 token = token->next;
2366 } else {
2367 sparse_error(token->pos, "Expected identifier or goto expression");
2368 }
2369 return expect(token, ';', "at end of statement");
2370 }
2371
2372 static struct token *parse_context_statement(struct token *token, struct statement *stmt)
2373 {
2374 stmt->type = STMT_CONTEXT;
2375 token = parse_expression(token->next, &stmt->expression);
2376 if (stmt->expression->type == EXPR_PREOP
2377 && stmt->expression->op == '('
2378 && stmt->expression->unop->type == EXPR_COMMA) {
2379 struct expression *expr;
2380 expr = stmt->expression->unop;
2381 stmt->context = expr->left;
2382 stmt->expression = expr->right;
2383 }
2384 return expect(token, ';', "at end of statement");
2385 }
2386
2387 static struct token *parse_range_statement(struct token *token, struct statement *stmt)
2388 {
2389 stmt->type = STMT_RANGE;
2390 token = assignment_expression(token->next, &stmt->range_expression);
2391 token = expect(token, ',', "after range expression");
2392 token = assignment_expression(token, &stmt->range_low);
2393 token = expect(token, ',', "after low range");
2394 token = assignment_expression(token, &stmt->range_high);
2395 return expect(token, ';', "after range statement");
2396 }
2397
2398 static struct token *statement(struct token *token, struct statement **tree)
2399 {
2400 struct statement *stmt = alloc_statement(token->pos, STMT_NONE);
2401
2402 *tree = stmt;
2403 if (token_type(token) == TOKEN_IDENT) {
2404 struct symbol *s = lookup_keyword(token->ident, NS_KEYWORD);
2405 if (s && s->op->statement)
2406 return s->op->statement(token, stmt);
2407
2408 if (match_op(token->next, ':')) {
2409 struct symbol *s = label_symbol(token);
2410 stmt->type = STMT_LABEL;
2411 stmt->label_identifier = s;
2412 if (s->stmt)
2413 sparse_error(stmt->pos, "label '%s' redefined", show_ident(token->ident));
2414 s->stmt = stmt;
2415 token = skip_attributes(token->next->next);
2416 return statement(token, &stmt->label_statement);
2417 }
2418 }
2419
2420 if (match_op(token, '{')) {
2421 stmt->type = STMT_COMPOUND;
2422 start_symbol_scope(stmt->pos);
2423 token = compound_statement(token->next, stmt);
2424 end_symbol_scope();
2425
2426 return expect(token, '}', "at end of compound statement");
2427 }
2428
2429 stmt->type = STMT_EXPRESSION;
2430 return expression_statement(token, &stmt->expression);
2431 }
2432
2433 /* gcc extension - __label__ ident-list; in the beginning of compound stmt */
2434 static struct token *label_statement(struct token *token)
2435 {
2680 static struct token *parse_function_body(struct token *token, struct symbol *decl,
2681 struct symbol_list **list)
2682 {
2683 struct symbol_list **old_symbol_list;
2684 struct symbol *base_type = decl->ctype.base_type;
2685 struct statement *stmt, **p;
2686 struct symbol *prev;
2687 struct symbol *arg;
2688
2689 old_symbol_list = function_symbol_list;
2690 if (decl->ctype.modifiers & MOD_INLINE) {
2691 function_symbol_list = &decl->inline_symbol_list;
2692 p = &base_type->inline_stmt;
2693 } else {
2694 function_symbol_list = &decl->symbol_list;
2695 p = &base_type->stmt;
2696 }
2697 function_computed_target_list = NULL;
2698 function_computed_goto_list = NULL;
2699
2700 if (decl->ctype.modifiers & MOD_EXTERN &&
2701 !(decl->ctype.modifiers & MOD_INLINE) &&
2702 Wexternal_function_has_definition)
2703 warning(decl->pos, "function '%s' with external linkage has definition", show_ident(decl->ident));
2704
2705 if (!(decl->ctype.modifiers & MOD_STATIC))
2706 decl->ctype.modifiers |= MOD_EXTERN;
2707
2708 stmt = start_function(decl);
2709
2710 *p = stmt;
2711 FOR_EACH_PTR (base_type->arguments, arg) {
2712 declare_argument(arg, base_type);
2713 } END_FOR_EACH_PTR(arg);
2714
2715 token = compound_statement(token->next, stmt);
2716
2717 end_function(decl);
2718 if (!(decl->ctype.modifiers & MOD_INLINE))
2719 add_symbol(list, decl);
2720 else if (is_syscall(decl)) {
2721 add_symbol(list, decl);
2722 /*
2723 printf("parse.c decl: %s\n", decl->ident->name);
2724 char *macro = get_macro_name(decl->pos);
2760 }
2761 }
2762
2763 static void apply_k_r_types(struct symbol_list *argtypes, struct symbol *fn)
2764 {
2765 struct symbol_list *real_args = fn->ctype.base_type->arguments;
2766 struct symbol *arg;
2767
2768 FOR_EACH_PTR(real_args, arg) {
2769 struct symbol *type;
2770
2771 /* This is quadratic in the number of arguments. We _really_ don't care */
2772 FOR_EACH_PTR(argtypes, type) {
2773 if (type->ident == arg->ident)
2774 goto match;
2775 } END_FOR_EACH_PTR(type);
2776 if (Wimplicit_int) {
2777 sparse_error(arg->pos, "missing type declaration for parameter '%s'",
2778 show_ident(arg->ident));
2779 }
2780 continue;
2781 match:
2782 type->used = 1;
2783 /* "char" and "short" promote to "int" */
2784 promote_k_r_types(type);
2785
2786 arg->ctype = type->ctype;
2787 } END_FOR_EACH_PTR(arg);
2788
2789 FOR_EACH_PTR(argtypes, arg) {
2790 if (!arg->used)
2791 warning(arg->pos, "nonsensical parameter declaration '%s'", show_ident(arg->ident));
2792 } END_FOR_EACH_PTR(arg);
2793
2794 }
2795
2796 static struct token *parse_k_r_arguments(struct token *token, struct symbol *decl,
2797 struct symbol_list **list)
2798 {
2799 struct symbol_list *args = NULL;
2800
2933 token = initializer(&decl->initializer, token->next);
2934 }
2935 if (!is_typedef) {
2936 if (validate_decl)
2937 validate_decl(decl);
2938
2939 if (decl->initializer && decl->ctype.modifiers & MOD_EXTERN) {
2940 warning(decl->pos, "symbol with external linkage has initializer");
2941 decl->ctype.modifiers &= ~MOD_EXTERN;
2942 }
2943
2944 if (!(decl->ctype.modifiers & (MOD_EXTERN | MOD_INLINE))) {
2945 add_symbol(list, decl);
2946 fn_local_symbol(decl);
2947 }
2948 }
2949 check_declaration(decl);
2950 if (decl->same_symbol) {
2951 decl->definition = decl->same_symbol->definition;
2952 decl->op = decl->same_symbol->op;
2953 }
2954
2955 if (!match_op(token, ','))
2956 break;
2957
2958 token = token->next;
2959 ident = NULL;
2960 decl = alloc_symbol(token->pos, SYM_NODE);
2961 ctx.ctype = saved;
2962 token = handle_attributes(token, &ctx, KW_ATTRIBUTE);
2963 token = declarator(token, &ctx);
2964 token = handle_attributes(token, &ctx, KW_ATTRIBUTE | KW_ASM);
2965 apply_modifiers(token->pos, &ctx);
2966 decl->ctype = ctx.ctype;
2967 decl->ctype.modifiers |= mod;
2968 decl->endpos = token->pos;
2969 if (!ident) {
2970 sparse_error(token->pos, "expected identifier name in type definition");
2971 return token;
2972 }
2973
|
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
122 static void asm_modifier(struct token *token, unsigned long *mods, unsigned long mod)
123 {
124 if (*mods & mod)
125 warning(token->pos, "duplicated asm modifier");
126 *mods |= mod;
127 }
128
129 static void asm_modifier_volatile(struct token *token, unsigned long *mods)
130 {
131 asm_modifier(token, mods, MOD_VOLATILE);
132 }
133
134 static void asm_modifier_inline(struct token *token, unsigned long *mods)
135 {
136 asm_modifier(token, mods, MOD_INLINE);
137 }
138
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 = {
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 = {
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
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
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 = ®ister_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 },
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
791 last = field;
792 } END_FOR_EACH_PTR(field);
793 return res;
794 }
795
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
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
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.
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
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
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
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
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
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
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");
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
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
1051 static struct token *typeof_specifier(struct token *token, struct decl_state *ctx)
1052 {
1053 struct symbol *sym;
1054
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
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
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
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
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
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
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
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
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 }
1204 return ctype->ctype.modifiers & MOD_UNSIGNED ? &uint_ctype
1205 : &sint_ctype;
1206 }
1207
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
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
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
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
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
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
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
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
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
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)
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
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
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 }
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
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
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
1498 static struct token *restrict_qualifier(struct token *next, struct decl_state *ctx)
1499 {
1500 apply_qualifier(&next->pos, &ctx->ctype, MOD_RESTRICT);
1501 return next;
1502 }
1503
1504 static struct token *atomic_qualifier(struct token *next, struct decl_state *ctx)
1505 {
1506 apply_qualifier(&next->pos, &ctx->ctype, MOD_ATOMIC);
1507 return next;
1508 }
1509
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
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
1842 static struct token *direct_declarator(struct token *token, struct decl_state *ctx)
1843 {
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
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
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
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;
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
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
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
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
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
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
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
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);
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
2231 static void end_iterator(struct statement *stmt)
2232 {
2233 end_symbol_scope();
2234 }
2235
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
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
2467
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
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
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
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 */
2556 static struct token *label_statement(struct token *token)
2557 {
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);
2881 }
2882 }
2883
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
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
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
|