History

implemented C99 for loop with variable declaration

Claudio Bley committed 16 years ago

Commit 433ecdf1 parent 632ee5a1 file · +37 −4Browse files

Unified · Split

tccgen.c+37−4
@@ -4291,14 +4291,28 @@ static void block(int *bsym, int *csym, int *case_sym, int *def_sym, · 4290 unmodified lines
42914291 next();
42924292 skip(';');
42934293 } else if (tok == TOK_FOR) {
4294− int e;
4294+ int e, c99_for_decl = 0;
42954295 next();
42964296 skip('(');
42974297 if (tok != ';') {
4298− gexpr();
4299− vpop();
4298+ if (tok < TOK_UIDENT) {
4299+ // handle C99 for loop construct
4300+ c99_for_decl = 1;
4301+
4302+ /* record local declaration stack position */
4303+ s = local_stack;
4304+
4305+ decl(VT_LOCAL);
4306+ if (is_expr)
4307+ vpop();
4308+ } else {
4309+ gexpr();
4310+ vpop();
4311+ skip(';');
4312+ }
4313+ } else {
4314+ skip(';');
43004315 }
4301− skip(';');
43024316 d = ind;
43034317 c = ind;
43044318 a = 0;
@@ -4321,6 +4335,25 @@ static void block(int *bsym, int *csym, int *case_sym, int *def_sym, · 16 unmodified lines
43214335 gjmp_addr(c);
43224336 gsym(a);
43234337 gsym_addr(b, c);
4338+
4339+ if (c99_for_decl) {
4340+ /* pop locally defined symbols */
4341+ if(is_expr) {
4342+ /* XXX: this solution makes only valgrind happy...
4343+ triggered by gcc.c-torture/execute/20000917-1.c */
4344+ Sym *p;
4345+ switch(vtop->type.t & VT_BTYPE) {
4346+ case VT_PTR:
4347+ case VT_STRUCT:
4348+ case VT_ENUM:
4349+ case VT_FUNC:
4350+ for(p=vtop->type.ref;p;p=p->prev)
4351+ if(p->prev==s)
4352+ error("unsupported expression type");
4353+ }
4354+ }
4355+ sym_pop(&local_stack, s);
4356+ }
43244357 } else
43254358 if (tok == TOK_DO) {
43264359 next();

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