Unified · Split
| @@ -111,7 +111,7 @@ typedef struct { · 110 unmodified lines | ||
| 111 | 111 | anon_sym: anonymous symbol index |
| 112 | 112 | */ |
| 113 | 113 | FILE *file; |
| 114 | −int tok, tok1, tokc, rsym, anon_sym, | |
| 114 | +int tok, tokc, tok1, tok1c, rsym, anon_sym, | |
| 115 | 115 | prog, ind, loc, glo, vt, vc, const_wanted, line_num; |
| 116 | 116 | int global_expr; /* true if compound literals must be allocated |
| 117 | 117 | globally (used during initializers parsing */ |
| @@ -131,6 +131,9 @@ int ifdef_stack[IFDEF_STACK_SIZE], *ifdef_stack_ptr; · 13 unmodified lines | ||
| 131 | 131 | char *include_paths[INCLUDE_PATHS_MAX]; |
| 132 | 132 | int nb_include_paths; |
| 133 | 133 | |
| 134 | +/* use GNU C extensions */ | |
| 135 | +int gnu_ext = 1; | |
| 136 | + | |
| 134 | 137 | /* The current value can be: */ |
| 135 | 138 | #define VT_VALMASK 0x000f |
| 136 | 139 | #define VT_CONST 0x000a /* constant in vc |
| @@ -260,7 +263,8 @@ enum { · 123 unmodified lines | ||
| 260 | 263 | TOK_SIZEOF, |
| 261 | 264 | |
| 262 | 265 | /* preprocessor only */ |
| 263 | − TOK_DEFINE, | |
| 266 | + TOK_UIDENT, /* first "user" ident (not keyword) */ | |
| 267 | + TOK_DEFINE = TOK_UIDENT, | |
| 264 | 268 | TOK_INCLUDE, |
| 265 | 269 | TOK_IFDEF, |
| 266 | 270 | TOK_IFNDEF, |
| @@ -1410,6 +1414,7 @@ void next() · 1143 unmodified lines | ||
| 1410 | 1414 | /* special 'ungettok' case for label parsing */ |
| 1411 | 1415 | if (tok1) { |
| 1412 | 1416 | tok = tok1; |
| 1417 | + tokc = tok1c; | |
| 1413 | 1418 | tok1 = 0; |
| 1414 | 1419 | } else { |
| 1415 | 1420 | redo: |
| @@ -1517,6 +1522,7 @@ void greloc_patch(Sym *s, int val) · 101 unmodified lines | ||
| 1517 | 1522 | p = p1; |
| 1518 | 1523 | } |
| 1519 | 1524 | s->c = val; |
| 1525 | + s->t &= ~VT_FORWARD; | |
| 1520 | 1526 | } |
| 1521 | 1527 | |
| 1522 | 1528 | /* output a symbol and patch all calls to it */ |
| @@ -2802,29 +2808,68 @@ void unary(void) · 1279 unmodified lines | ||
| 2802 | 2808 | } |
| 2803 | 2809 | } |
| 2804 | 2810 | |
| 2805 | −/* check if types are compatible for assignation */ | |
| 2806 | −int same_types(int t1, int t2) | |
| 2811 | +int is_compatible_types(int t1, int t2) | |
| 2807 | 2812 | { |
| 2813 | + Sym *s1, *s2; | |
| 2814 | + | |
| 2808 | 2815 | t1 &= VT_TYPE; |
| 2809 | 2816 | t2 &= VT_TYPE; |
| 2810 | 2817 | if (t1 & VT_PTR) { |
| 2811 | − /* XXX: zero test ? */ | |
| 2812 | − if (!(t2 & VT_PTR)) | |
| 2813 | − return 0; | |
| 2814 | 2818 | t1 = pointed_type(t1); |
| 2815 | − t2 = pointed_type(t2); | |
| 2819 | + /* if function, then convert implictely to function pointer */ | |
| 2820 | + if (!(t2 & VT_FUNC)) { | |
| 2821 | + if (!(t2 & VT_PTR)) | |
| 2822 | + return 0; | |
| 2823 | + t2 = pointed_type(t2); | |
| 2824 | + } | |
| 2816 | 2825 | /* void matches everything */ |
| 2826 | + t1 &= VT_TYPE; | |
| 2827 | + t2 &= VT_TYPE; | |
| 2817 | 2828 | if (t1 == VT_VOID || t2 == VT_VOID) |
| 2818 | 2829 | return 1; |
| 2819 | − return same_types(t1, t2); | |
| 2830 | + return is_compatible_types(t1, t2); | |
| 2820 | 2831 | } else if (t1 & VT_STRUCT) { |
| 2821 | 2832 | return (t2 == t1); |
| 2833 | + } else if (t1 & VT_FUNC) { | |
| 2834 | + if (!(t2 & VT_FUNC)) | |
| 2835 | + return 0; | |
| 2836 | + s1 = sym_find(((unsigned)t1 >> VT_STRUCT_SHIFT)); | |
| 2837 | + s2 = sym_find(((unsigned)t2 >> VT_STRUCT_SHIFT)); | |
| 2838 | + if (!is_compatible_types(s1->t, s2->t)) | |
| 2839 | + return 0; | |
| 2840 | + /* XXX: not complete */ | |
| 2841 | + if (s1->c == FUNC_OLD || s2->c == FUNC_OLD) | |
| 2842 | + return 1; | |
| 2843 | + if (s1->c != s2->c) | |
| 2844 | + return 0; | |
| 2845 | + while (s1 != NULL) { | |
| 2846 | + if (s2 == NULL) | |
| 2847 | + return 0; | |
| 2848 | + if (!is_compatible_types(s1->t, s2->t)) | |
| 2849 | + return 0; | |
| 2850 | + s1 = s1->next; | |
| 2851 | + s2 = s2->next; | |
| 2852 | + } | |
| 2853 | + if (s2) | |
| 2854 | + return 0; | |
| 2855 | + return 1; | |
| 2822 | 2856 | } else { |
| 2823 | 2857 | /* XXX: not complete */ |
| 2824 | 2858 | return 1; |
| 2825 | 2859 | } |
| 2826 | 2860 | } |
| 2827 | 2861 | |
| 2862 | +int check_assign_types(int t1, int t2) | |
| 2863 | +{ | |
| 2864 | + t1 &= VT_TYPE; | |
| 2865 | + t2 &= VT_TYPE; | |
| 2866 | + if ((t1 & VT_PTR) && (t2 & VT_FUNC)) { | |
| 2867 | + return is_compatible_types(pointed_type(t1), t2); | |
| 2868 | + } else { | |
| 2869 | + return is_compatible_types(t1, t2); | |
| 2870 | + } | |
| 2871 | +} | |
| 2872 | + | |
| 2828 | 2873 | |
| 2829 | 2874 | void uneq() |
| 2830 | 2875 | { |
| @@ -2841,8 +2886,8 @@ void uneq() · 10 unmodified lines | ||
| 2841 | 2886 | next(); |
| 2842 | 2887 | if (t == '=') { |
| 2843 | 2888 | expr_eq(); |
| 2844 | − if (!same_types(vt, vstack_ptr[-2])) | |
| 2845 | − error("incompatible types"); | |
| 2889 | + if (!check_assign_types(vstack_ptr[-2], vt)) | |
| 2890 | + warning("incompatible types"); | |
| 2846 | 2891 | } else { |
| 2847 | 2892 | vpush(); |
| 2848 | 2893 | expr_eq(); |
| @@ -2979,6 +3024,31 @@ int expr_const() · 130 unmodified lines | ||
| 2979 | 3024 | return vc; |
| 2980 | 3025 | } |
| 2981 | 3026 | |
| 3027 | +/* return the label token if current token is a label, otherwise | |
| 3028 | + return zero */ | |
| 3029 | +int is_label(void) | |
| 3030 | +{ | |
| 3031 | + int t, c; | |
| 3032 | + | |
| 3033 | + /* fast test first */ | |
| 3034 | + if (tok < TOK_UIDENT) | |
| 3035 | + return 0; | |
| 3036 | + t = tok; | |
| 3037 | + c = tokc; | |
| 3038 | + next(); | |
| 3039 | + if (tok == ':') { | |
| 3040 | + next(); | |
| 3041 | + return t; | |
| 3042 | + } else { | |
| 3043 | + /* XXX: may not work in all cases (macros ?) */ | |
| 3044 | + tok1 = tok; | |
| 3045 | + tok1c = tokc; | |
| 3046 | + tok = t; | |
| 3047 | + tokc = c; | |
| 3048 | + return 0; | |
| 3049 | + } | |
| 3050 | +} | |
| 3051 | + | |
| 2982 | 3052 | void block(int *bsym, int *csym, int *case_sym, int *def_sym, int case_reg) |
| 2983 | 3053 | { |
| 2984 | 3054 | int a, b, c, d; |
| @@ -3161,10 +3231,8 @@ void block(int *bsym, int *csym, int *case_sym, int *def_sym, int case_reg) · 176 unmodified lines | ||
| 3161 | 3231 | next(); |
| 3162 | 3232 | skip(';'); |
| 3163 | 3233 | } else { |
| 3164 | − b = tok; | |
| 3165 | − next(); | |
| 3166 | − if (tok == ':') { | |
| 3167 | − next(); | |
| 3234 | + b = is_label(); | |
| 3235 | + if (b) { | |
| 3168 | 3236 | /* label case */ |
| 3169 | 3237 | s = sym_find1(&label_stack, b); |
| 3170 | 3238 | if (s) { |
| @@ -3176,12 +3244,13 @@ void block(int *bsym, int *csym, int *case_sym, int *def_sym, int case_reg) · 5 unmodified lines | ||
| 3176 | 3244 | } else { |
| 3177 | 3245 | sym_push1(&label_stack, b, 0, ind); |
| 3178 | 3246 | } |
| 3179 | − block(bsym, csym, case_sym, def_sym, case_reg); | |
| 3247 | + /* we accept this, but it is a mistake */ | |
| 3248 | + if (tok == '}') | |
| 3249 | + warning("deprecated use of label at end of compound statement"); | |
| 3250 | + else | |
| 3251 | + block(bsym, csym, case_sym, def_sym, case_reg); | |
| 3180 | 3252 | } else { |
| 3181 | − /* expression case: go backward of one token */ | |
| 3182 | − /* XXX: currently incorrect if number/string/char */ | |
| 3183 | − tok1 = tok; | |
| 3184 | − tok = b; | |
| 3253 | + /* expression case */ | |
| 3185 | 3254 | if (tok != ';') { |
| 3186 | 3255 | expr(); |
| 3187 | 3256 | } |
| @@ -3199,9 +3268,12 @@ void decl_designator(int t, int c, · 11 unmodified lines | ||
| 3199 | 3268 | int size_only) |
| 3200 | 3269 | { |
| 3201 | 3270 | Sym *s, *f; |
| 3202 | − int notfirst, index, align; | |
| 3271 | + int notfirst, index, align, l; | |
| 3203 | 3272 | |
| 3204 | 3273 | notfirst = 0; |
| 3274 | + if (gnu_ext && (l = is_label()) != 0) | |
| 3275 | + goto struct_field; | |
| 3276 | + | |
| 3205 | 3277 | while (tok == '[' || tok == '.') { |
| 3206 | 3278 | if (tok == '[') { |
| 3207 | 3279 | if (!(t & VT_ARRAY)) |
| @@ -3217,20 +3289,22 @@ void decl_designator(int t, int c, · 9 unmodified lines | ||
| 3217 | 3289 | t = pointed_type(t); |
| 3218 | 3290 | c += index * type_size(t, &align); |
| 3219 | 3291 | } else { |
| 3292 | + next(); | |
| 3293 | + l = tok; | |
| 3294 | + next(); | |
| 3295 | + struct_field: | |
| 3220 | 3296 | if (!(t & VT_STRUCT)) |
| 3221 | 3297 | expect("struct/union type"); |
| 3222 | − next(); | |
| 3223 | 3298 | s = sym_find(((unsigned)t >> VT_STRUCT_SHIFT) | SYM_STRUCT); |
| 3224 | − tok |= SYM_FIELD; | |
| 3299 | + l |= SYM_FIELD; | |
| 3225 | 3300 | f = s->next; |
| 3226 | 3301 | while (f) { |
| 3227 | − if (f->v == tok) | |
| 3302 | + if (f->v == l) | |
| 3228 | 3303 | break; |
| 3229 | 3304 | f = f->next; |
| 3230 | 3305 | } |
| 3231 | 3306 | if (!f) |
| 3232 | 3307 | expect("field"); |
| 3233 | − next(); | |
| 3234 | 3308 | if (!notfirst) |
| 3235 | 3309 | *cur_field = f; |
| 3236 | 3310 | t = f->t | (t & VT_TYPEN); |
| @@ -3239,7 +3313,12 @@ void decl_designator(int t, int c, · 2 unmodified lines | ||
| 3239 | 3313 | notfirst = 1; |
| 3240 | 3314 | } |
| 3241 | 3315 | if (notfirst) { |
| 3242 | − skip('='); | |
| 3316 | + if (tok == '=') { | |
| 3317 | + next(); | |
| 3318 | + } else { | |
| 3319 | + if (!gnu_ext) | |
| 3320 | + expect("="); | |
| 3321 | + } | |
| 3243 | 3322 | } else { |
| 3244 | 3323 | if (t & VT_ARRAY) { |
| 3245 | 3324 | index = *cur_index; |
| @@ -3643,7 +3722,21 @@ void decl(int l) · 397 unmodified lines | ||
| 3643 | 3722 | if (has_init) |
| 3644 | 3723 | next(); |
| 3645 | 3724 | addr = decl_initializer_alloc(u, has_init); |
| 3646 | − sym_push(v, u, addr); | |
| 3725 | + if (l == VT_CONST) { | |
| 3726 | + /* global scope: see if already defined */ | |
| 3727 | + sym = sym_find(v); | |
| 3728 | + if (!sym) | |
| 3729 | + goto do_def; | |
| 3730 | + if (!is_compatible_types(sym->t, u)) | |
| 3731 | + error("incompatible types for redefinition of '%s'", | |
| 3732 | + get_tok_str(v, 0)); | |
| 3733 | + if (!(sym->t & VT_FORWARD)) | |
| 3734 | + error("redefinition of '%s'", get_tok_str(v, 0)); | |
| 3735 | + greloc_patch(sym, addr); | |
| 3736 | + } else { | |
| 3737 | + do_def: | |
| 3738 | + sym_push(v, u, addr); | |
| 3739 | + } | |
| 3647 | 3740 | } |
| 3648 | 3741 | } |
| 3649 | 3742 | if (tok != ',') { |