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
111111 anon_sym: anonymous symbol index
112112 */
113113 FILE *file;
114−int tok, tok1, tokc, rsym, anon_sym,
114+int tok, tokc, tok1, tok1c, rsym, anon_sym,
115115 prog, ind, loc, glo, vt, vc, const_wanted, line_num;
116116 int global_expr; /* true if compound literals must be allocated
117117 globally (used during initializers parsing */
@@ -131,6 +131,9 @@ int ifdef_stack[IFDEF_STACK_SIZE], *ifdef_stack_ptr; · 13 unmodified lines
131131 char *include_paths[INCLUDE_PATHS_MAX];
132132 int nb_include_paths;
133133
134+/* use GNU C extensions */
135+int gnu_ext = 1;
136+
134137 /* The current value can be: */
135138 #define VT_VALMASK 0x000f
136139 #define VT_CONST 0x000a /* constant in vc
@@ -260,7 +263,8 @@ enum { · 123 unmodified lines
260263 TOK_SIZEOF,
261264
262265 /* preprocessor only */
263− TOK_DEFINE,
266+ TOK_UIDENT, /* first "user" ident (not keyword) */
267+ TOK_DEFINE = TOK_UIDENT,
264268 TOK_INCLUDE,
265269 TOK_IFDEF,
266270 TOK_IFNDEF,
@@ -1410,6 +1414,7 @@ void next() · 1143 unmodified lines
14101414 /* special 'ungettok' case for label parsing */
14111415 if (tok1) {
14121416 tok = tok1;
1417+ tokc = tok1c;
14131418 tok1 = 0;
14141419 } else {
14151420 redo:
@@ -1517,6 +1522,7 @@ void greloc_patch(Sym *s, int val) · 101 unmodified lines
15171522 p = p1;
15181523 }
15191524 s->c = val;
1525+ s->t &= ~VT_FORWARD;
15201526 }
15211527
15221528 /* output a symbol and patch all calls to it */
@@ -2802,29 +2808,68 @@ void unary(void) · 1279 unmodified lines
28022808 }
28032809 }
28042810
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)
28072812 {
2813+ Sym *s1, *s2;
2814+
28082815 t1 &= VT_TYPE;
28092816 t2 &= VT_TYPE;
28102817 if (t1 & VT_PTR) {
2811− /* XXX: zero test ? */
2812− if (!(t2 & VT_PTR))
2813− return 0;
28142818 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+ }
28162825 /* void matches everything */
2826+ t1 &= VT_TYPE;
2827+ t2 &= VT_TYPE;
28172828 if (t1 == VT_VOID || t2 == VT_VOID)
28182829 return 1;
2819− return same_types(t1, t2);
2830+ return is_compatible_types(t1, t2);
28202831 } else if (t1 & VT_STRUCT) {
28212832 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;
28222856 } else {
28232857 /* XXX: not complete */
28242858 return 1;
28252859 }
28262860 }
28272861
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+
28282873
28292874 void uneq()
28302875 {
@@ -2841,8 +2886,8 @@ void uneq() · 10 unmodified lines
28412886 next();
28422887 if (t == '=') {
28432888 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");
28462891 } else {
28472892 vpush();
28482893 expr_eq();
@@ -2979,6 +3024,31 @@ int expr_const() · 130 unmodified lines
29793024 return vc;
29803025 }
29813026
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+
29823052 void block(int *bsym, int *csym, int *case_sym, int *def_sym, int case_reg)
29833053 {
29843054 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
31613231 next();
31623232 skip(';');
31633233 } else {
3164− b = tok;
3165− next();
3166− if (tok == ':') {
3167− next();
3234+ b = is_label();
3235+ if (b) {
31683236 /* label case */
31693237 s = sym_find1(&label_stack, b);
31703238 if (s) {
@@ -3176,12 +3244,13 @@ void block(int *bsym, int *csym, int *case_sym, int *def_sym, int case_reg) · 5 unmodified lines
31763244 } else {
31773245 sym_push1(&label_stack, b, 0, ind);
31783246 }
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);
31803252 } 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 */
31853254 if (tok != ';') {
31863255 expr();
31873256 }
@@ -3199,9 +3268,12 @@ void decl_designator(int t, int c, · 11 unmodified lines
31993268 int size_only)
32003269 {
32013270 Sym *s, *f;
3202− int notfirst, index, align;
3271+ int notfirst, index, align, l;
32033272
32043273 notfirst = 0;
3274+ if (gnu_ext && (l = is_label()) != 0)
3275+ goto struct_field;
3276+
32053277 while (tok == '[' || tok == '.') {
32063278 if (tok == '[') {
32073279 if (!(t & VT_ARRAY))
@@ -3217,20 +3289,22 @@ void decl_designator(int t, int c, · 9 unmodified lines
32173289 t = pointed_type(t);
32183290 c += index * type_size(t, &align);
32193291 } else {
3292+ next();
3293+ l = tok;
3294+ next();
3295+ struct_field:
32203296 if (!(t & VT_STRUCT))
32213297 expect("struct/union type");
3222− next();
32233298 s = sym_find(((unsigned)t >> VT_STRUCT_SHIFT) | SYM_STRUCT);
3224− tok |= SYM_FIELD;
3299+ l |= SYM_FIELD;
32253300 f = s->next;
32263301 while (f) {
3227− if (f->v == tok)
3302+ if (f->v == l)
32283303 break;
32293304 f = f->next;
32303305 }
32313306 if (!f)
32323307 expect("field");
3233− next();
32343308 if (!notfirst)
32353309 *cur_field = f;
32363310 t = f->t | (t & VT_TYPEN);
@@ -3239,7 +3313,12 @@ void decl_designator(int t, int c, · 2 unmodified lines
32393313 notfirst = 1;
32403314 }
32413315 if (notfirst) {
3242− skip('=');
3316+ if (tok == '=') {
3317+ next();
3318+ } else {
3319+ if (!gnu_ext)
3320+ expect("=");
3321+ }
32433322 } else {
32443323 if (t & VT_ARRAY) {
32453324 index = *cur_index;
@@ -3643,7 +3722,21 @@ void decl(int l) · 397 unmodified lines
36433722 if (has_init)
36443723 next();
36453724 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+ }
36473740 }
36483741 }
36493742 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