History

better type checks - syntax fixes (gnu_ext added) - variable external defs

bellard committed 24 years ago

Commit d7e05e11 parent 8523baf1 file · +120 −27Browse files

Unified · Split

tcc.c+120−27
@@ -111,7 +111,7 @@ typedef struct { · 110 unmodified lines
111 anon_sym: anonymous symbol index111 anon_sym: anonymous symbol index
112*/112*/
113FILE *file;113FILE *file;
114int tok, tok1, tokc, rsym, anon_sym,114int tok, tokc, tok1, tok1c, rsym, anon_sym,
115 prog, ind, loc, glo, vt, vc, const_wanted, line_num;115 prog, ind, loc, glo, vt, vc, const_wanted, line_num;
116int global_expr; /* true if compound literals must be allocated116int global_expr; /* true if compound literals must be allocated
117 globally (used during initializers parsing */117 globally (used during initializers parsing */
@@ -131,6 +131,9 @@ int ifdef_stack[IFDEF_STACK_SIZE], *ifdef_stack_ptr; · 13 unmodified lines
131char *include_paths[INCLUDE_PATHS_MAX];131char *include_paths[INCLUDE_PATHS_MAX];
132int nb_include_paths;132int nb_include_paths;
133 133
134/* use GNU C extensions */
135int gnu_ext = 1;
136
134/* The current value can be: */137/* The current value can be: */
135#define VT_VALMASK 0x000f138#define VT_VALMASK 0x000f
136#define VT_CONST 0x000a /* constant in vc 139#define VT_CONST 0x000a /* constant in vc
@@ -260,7 +263,8 @@ enum { · 123 unmodified lines
260 TOK_SIZEOF,263 TOK_SIZEOF,
261 264
262 /* preprocessor only */265 /* preprocessor only */
263 TOK_DEFINE,266 TOK_UIDENT, /* first "user" ident (not keyword) */
267 TOK_DEFINE = TOK_UIDENT,
264 TOK_INCLUDE,268 TOK_INCLUDE,
265 TOK_IFDEF,269 TOK_IFDEF,
266 TOK_IFNDEF,270 TOK_IFNDEF,
@@ -1410,6 +1414,7 @@ void next() · 1143 unmodified lines
1410 /* special 'ungettok' case for label parsing */1414 /* special 'ungettok' case for label parsing */
1411 if (tok1) {1415 if (tok1) {
1412 tok = tok1;1416 tok = tok1;
1417 tokc = tok1c;
1413 tok1 = 0;1418 tok1 = 0;
1414 } else {1419 } else {
1415 redo:1420 redo:
@@ -1517,6 +1522,7 @@ void greloc_patch(Sym *s, int val) · 101 unmodified lines
1517 p = p1;1522 p = p1;
1518 }1523 }
1519 s->c = val;1524 s->c = val;
1525 s->t &= ~VT_FORWARD;
1520}1526}
1521 1527
1522/* output a symbol and patch all calls to it */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 */2811int is_compatible_types(int t1, int t2)
2806int same_types(int t1, int t2)
2807{2812{
2813 Sym *s1, *s2;
2814
2808 t1 &= VT_TYPE;2815 t1 &= VT_TYPE;
2809 t2 &= VT_TYPE;2816 t2 &= VT_TYPE;
2810 if (t1 & VT_PTR) {2817 if (t1 & VT_PTR) {
2811 /* XXX: zero test ? */
2812 if (!(t2 & VT_PTR))
2813 return 0;
2814 t1 = pointed_type(t1);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 /* void matches everything */2825 /* void matches everything */
2826 t1 &= VT_TYPE;
2827 t2 &= VT_TYPE;
2817 if (t1 == VT_VOID || t2 == VT_VOID)2828 if (t1 == VT_VOID || t2 == VT_VOID)
2818 return 1;2829 return 1;
2819 return same_types(t1, t2);2830 return is_compatible_types(t1, t2);
2820 } else if (t1 & VT_STRUCT) {2831 } else if (t1 & VT_STRUCT) {
2821 return (t2 == t1);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 } else {2856 } else {
2823 /* XXX: not complete */2857 /* XXX: not complete */
2824 return 1;2858 return 1;
2825 }2859 }
2826}2860}
2827 2861
2862int 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
2829void uneq()2874void uneq()
2830{2875{
@@ -2841,8 +2886,8 @@ void uneq() · 10 unmodified lines
2841 next();2886 next();
2842 if (t == '=') {2887 if (t == '=') {
2843 expr_eq();2888 expr_eq();
2844 if (!same_types(vt, vstack_ptr[-2]))2889 if (!check_assign_types(vstack_ptr[-2], vt))
2845 error("incompatible types");2890 warning("incompatible types");
2846 } else {2891 } else {
2847 vpush();2892 vpush();
2848 expr_eq();2893 expr_eq();
@@ -2979,6 +3024,31 @@ int expr_const() · 130 unmodified lines
2979 return vc;3024 return vc;
2980}3025}
2981 3026
3027/* return the label token if current token is a label, otherwise
3028 return zero */
3029int 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
2982void block(int *bsym, int *csym, int *case_sym, int *def_sym, int case_reg)3052void block(int *bsym, int *csym, int *case_sym, int *def_sym, int case_reg)
2983{3053{
2984 int a, b, c, d;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 next();3231 next();
3162 skip(';');3232 skip(';');
3163 } else {3233 } else {
3164 b = tok;3234 b = is_label();
3165 next();3235 if (b) {
3166 if (tok == ':') {
3167 next();
3168 /* label case */3236 /* label case */
3169 s = sym_find1(&label_stack, b);3237 s = sym_find1(&label_stack, b);
3170 if (s) {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 } else {3244 } else {
3177 sym_push1(&label_stack, b, 0, ind);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 } else {3252 } else {
3181 /* expression case: go backward of one token */3253 /* expression case */
3182 /* XXX: currently incorrect if number/string/char */
3183 tok1 = tok;
3184 tok = b;
3185 if (tok != ';') {3254 if (tok != ';') {
3186 expr();3255 expr();
3187 }3256 }
@@ -3199,9 +3268,12 @@ void decl_designator(int t, int c, · 11 unmodified lines
3199 int size_only)3268 int size_only)
3200{3269{
3201 Sym *s, *f;3270 Sym *s, *f;
3202 int notfirst, index, align;3271 int notfirst, index, align, l;
3203 3272
3204 notfirst = 0;3273 notfirst = 0;
3274 if (gnu_ext && (l = is_label()) != 0)
3275 goto struct_field;
3276
3205 while (tok == '[' || tok == '.') {3277 while (tok == '[' || tok == '.') {
3206 if (tok == '[') {3278 if (tok == '[') {
3207 if (!(t & VT_ARRAY))3279 if (!(t & VT_ARRAY))
@@ -3217,20 +3289,22 @@ void decl_designator(int t, int c, · 9 unmodified lines
3217 t = pointed_type(t);3289 t = pointed_type(t);
3218 c += index * type_size(t, &align);3290 c += index * type_size(t, &align);
3219 } else {3291 } else {
3292 next();
3293 l = tok;
3294 next();
3295 struct_field:
3220 if (!(t & VT_STRUCT))3296 if (!(t & VT_STRUCT))
3221 expect("struct/union type");3297 expect("struct/union type");
3222 next();
3223 s = sym_find(((unsigned)t >> VT_STRUCT_SHIFT) | SYM_STRUCT);3298 s = sym_find(((unsigned)t >> VT_STRUCT_SHIFT) | SYM_STRUCT);
3224 tok |= SYM_FIELD;3299 l |= SYM_FIELD;
3225 f = s->next;3300 f = s->next;
3226 while (f) {3301 while (f) {
3227 if (f->v == tok)3302 if (f->v == l)
3228 break;3303 break;
3229 f = f->next;3304 f = f->next;
3230 }3305 }
3231 if (!f)3306 if (!f)
3232 expect("field");3307 expect("field");
3233 next();
3234 if (!notfirst)3308 if (!notfirst)
3235 *cur_field = f;3309 *cur_field = f;
3236 t = f->t | (t & VT_TYPEN);3310 t = f->t | (t & VT_TYPEN);
@@ -3239,7 +3313,12 @@ void decl_designator(int t, int c, · 2 unmodified lines
3239 notfirst = 1;3313 notfirst = 1;
3240 }3314 }
3241 if (notfirst) {3315 if (notfirst) {
3242 skip('=');3316 if (tok == '=') {
3317 next();
3318 } else {
3319 if (!gnu_ext)
3320 expect("=");
3321 }
3243 } else {3322 } else {
3244 if (t & VT_ARRAY) {3323 if (t & VT_ARRAY) {
3245 index = *cur_index;3324 index = *cur_index;
@@ -3643,7 +3722,21 @@ void decl(int l) · 397 unmodified lines
3643 if (has_init)3722 if (has_init)
3644 next();3723 next();
3645 addr = decl_initializer_alloc(u, has_init);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 if (tok != ',') {3742 if (tok != ',') {

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t