From 5eb82755db1e46a4c7cb7ded62d8176daf1da572 Mon Sep 17 00:00:00 2001 From: Joe Soroka Date: Tue, 8 Mar 2011 13:36:04 -0800 Subject: [PATCH] support c99 for-loop init decls (2nd attempt) --- tccgen.c | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/tccgen.c b/tccgen.c index f28caedb..b3355276 100644 --- a/tccgen.c +++ b/tccgen.c @@ -76,6 +76,7 @@ static void parse_expr_type(CType *type); static void decl_initializer(CType *type, Section *sec, unsigned long c, int first, int size_only); static void block(int *bsym, int *csym, int *case_sym, int *def_sym, int case_reg, int is_expr); static void decl_initializer_alloc(CType *type, AttributeDef *ad, int r, int has_init, int v, char *asm_label, int scope); +static int decl0(int l, int is_for_loop_init); static void expr_eq(void); static void unary_type(CType *type); static int is_compatible_parameter_types(CType *type1, CType *type2); @@ -4379,8 +4380,12 @@ static void block(int *bsym, int *csym, int *case_sym, int *def_sym, next(); skip('('); if (tok != ';') { - gexpr(); - vpop(); + /* c99 for-loop init decl? */ + if (!decl0(VT_LOCAL, 1)) { + /* no, regular for-loop init expr */ + gexpr(); + vpop(); + } } skip(';'); d = ind; @@ -5406,7 +5411,7 @@ ST_FUNC void gen_inline_functions(void) } /* 'l' is VT_LOCAL or VT_CONST to define default storage type */ -ST_FUNC void decl(int l) +static int decl0(int l, int is_for_loop_init) { int v, has_init, r; CType type, btype; @@ -5415,6 +5420,8 @@ ST_FUNC void decl(int l) while (1) { if (!parse_btype(&btype, &ad)) { + if (is_for_loop_init) + return 0; /* skip redundant ';' */ /* XXX: find more elegant solution */ if (tok == ';') { @@ -5629,6 +5636,8 @@ ST_FUNC void decl(int l) } } if (tok != ',') { + if (is_for_loop_init) + return 1; skip(';'); break; } @@ -5636,4 +5645,10 @@ ST_FUNC void decl(int l) } } } + return 0; +} + +ST_FUNC void decl(int l) +{ + decl0(l, 0); }