Index: arrayop.c
===================================================================
--- arrayop.c	(revision 317)
+++ arrayop.c	(working copy)
@@ -273,7 +273,7 @@
 		new ExpStatement(0, loopbody));
 #else
 	    // foreach (i; 0 .. p.length)
-	    Statement *s1 = new ForeachRangeStatement(0, TOKforeach,
+	    Statement *s1 = new ForeachRangeStatement(0, 0, TOKforeach,
 		new Parameter(0, NULL, Id::p, NULL),
 		new IntegerExp(0, 0, Type::tint32),
 		new ArrayLengthExp(0, new IdentifierExp(0, p->ident)),
Index: backend/cc.h
===================================================================
--- backend/cc.h	(revision 317)
+++ backend/cc.h	(working copy)
@@ -1403,6 +1403,10 @@
 #if SOURCE_4SYMS
     Srcpos Ssrcpos;		// file position for definition
 #endif
+
+    unsigned lnoscopestart; // life time of var
+    unsigned lnoscopeend;   // the line after the scope
+
     // Target Additions
     TARGET_structSYMBOL
 #if TX86
Index: backend/cdef.h
===================================================================
--- backend/cdef.h	(revision 317)
+++ backend/cdef.h	(working copy)
@@ -988,7 +988,7 @@
 	 (symbol *)0,(symbol *)0,(symbol *)0,(dt_t *)0,(type *)0,{0},\
 	 SYMBOLZERO\
 	 UNIXFIELDS\
-	 SCextern,(fl),(flags),0,0,0,0,0,0,0,{0},(regsaved),{name}}
+	 SCextern,(fl),(flags),0,0,0,0,0,0,0,{0},(regsaved),0,0,{name}}
 
 /**********************************
  * Storage classes
Index: cgcv.c
===================================================================
--- backend/cgcv.c	(revision 317)
+++ backend/cgcv.c	(working copy)
@@ -2321,6 +2321,151 @@
 	cv_outsym((Symbol *) list_pop(&cgcv.list));
 }
 
+//////////////////////////////////////////////////////
+struct VarStats
+{
+    // data per line
+    int numNew;
+    int numDel;
+    int endLine;
+    int offset; // only evaluated for lines that are the beginning or the end of a scope
+};
+
+VarStats* varStats = 0;
+int cntAllocVarStats;
+int cntUsedVarStats;
+int firstVarStatsLine = 0;
+int nextVarStatsLine = 0;
+unsigned* scopeStack = 0;
+unsigned scopeStackPos = 0;
+
+STATIC void cv4_markVarStats(int startLine, int endLine)
+{
+    if (endLine == startLine) // single line functions
+	endLine++;
+    if (cntUsedVarStats == 0)
+	firstVarStatsLine = startLine;
+    
+    if(startLine < firstVarStatsLine)
+    {
+	int cnt = firstVarStatsLine - startLine;
+	if (cnt + cntUsedVarStats > cntAllocVarStats)
+	    varStats = (VarStats*) util_realloc(varStats, cnt + cntUsedVarStats, sizeof(*varStats));
+	memmove(varStats + cnt, varStats, cntUsedVarStats * sizeof(*varStats));
+	memset(varStats, 0, cnt * sizeof(*varStats));
+	cntUsedVarStats += cnt;
+	firstVarStatsLine = startLine;
+    }
+    
+    int cnt = endLine - firstVarStatsLine + 1;
+    if (cnt > cntAllocVarStats)
+    {
+	varStats = (VarStats*) util_realloc(varStats, cnt, sizeof(*varStats));
+	cntAllocVarStats = cnt;
+    }
+    if (cnt > cntUsedVarStats)
+    {
+	memset(varStats + cntUsedVarStats, 0, (cnt - cntUsedVarStats) * sizeof(*varStats));
+	cntUsedVarStats = cnt;
+    }
+
+    varStats[startLine - firstVarStatsLine].numNew++;
+    varStats[endLine   - firstVarStatsLine].numDel++;
+    varStats[startLine - firstVarStatsLine].endLine = endLine;
+}
+
+STATIC void cv4_calcLexicalScope()
+{
+    cntUsedVarStats = 0;
+
+    SYMIDX si;
+    for (si = 0; si < globsym.top; si++)
+    {
+	symbol *sa = globsym.tab[si];
+	if (sa->lnoscopestart > 0 && sa->lnoscopestart <= sa->lnoscopeend)
+	    cv4_markVarStats(sa->lnoscopestart, sa->lnoscopeend);
+    }
+
+    Srcpos src = funcsym_p->Sfunc->Fstartline;
+    for (int i = 0; i < cntUsedVarStats; i++)
+	if (varStats[i].numDel > 0 || varStats[i].numNew > 0)
+	{
+	    src.Slinnum = firstVarStatsLine + i;
+	    varStats[i].offset = obj_getlineoffset (src);
+	}
+
+    nextVarStatsLine = firstVarStatsLine;
+    scopeStack = (unsigned*) util_realloc(scopeStack, globsym.top + 1, sizeof(*scopeStack));
+    scopeStackPos = 0;
+    scopeStack[0] = 0;
+}
+
+struct block32_data { 
+    unsigned short len;
+    unsigned short id;
+    unsigned int pParent;
+    unsigned int pEnd;
+    unsigned int length;
+    unsigned int offset;
+    unsigned short seg;
+    unsigned char name[2];
+};
+
+struct bprel32_data { 
+    unsigned short len;
+    unsigned short id;
+    unsigned int offset;
+    unsigned short type;
+    unsigned char name[6];
+};
+
+STATIC void cv4_writeLexicalScope(SYMIDX si)
+{
+    if (cntUsedVarStats == 0)
+	return;
+
+    unsigned line = (si < globsym.top ? globsym.tab[si]->lnoscopestart + 1 : firstVarStatsLine + cntUsedVarStats);
+    while (nextVarStatsLine < line)
+    {
+	unsigned idx = nextVarStatsLine - firstVarStatsLine;
+	for(int d = 0; d < varStats[idx].numDel; d++)
+	{   
+#if 0
+	    static unsigned short s_end[] = { 2, S_SKIP }; // S_END };
+	    obj_write_bytes(SegData[DEBSYM], sizeof(s_end), s_end);
+#else
+	    bprel32_data bprel32 = { sizeof(bprel32_data) - 2, S_BPREL32, 0, 0, { 5, '@', 's', 'e', 'n', 'd' } };
+	    obj_write_bytes(SegData[DEBSYM], sizeof(bprel32), &bprel32);
+#endif
+	    scopeStackPos--;
+	}
+	for(int n = 0; n < varStats[idx].numNew; n++)
+	{
+	    unsigned offset = varStats[idx].offset;
+	    unsigned length = varStats[varStats[idx].endLine - firstVarStatsLine].offset - offset;
+#if 0
+	    unsigned parent = 0; //scopeStack[scopeStackPos];
+	    unsigned end = 0; //SegData[DEBSYM]->SDoffset + sizeof(block32_data) + 12;
+	    unsigned short segment = 0; // cseg;
+	    block32_data block32 = { sizeof(block32_data) - 2, S_BLOCK32, parent, end, length, offset, segment, { 1, 'x' } };
+	    scopeStack[++scopeStackPos] = SegData[DEBSYM]->SDoffset;
+	    obj_write_bytes(SegData[DEBSYM], sizeof(block32), &block32);
+#else
+	    bprel32_data bprel32 = { sizeof(bprel32_data) - 2, S_BPREL32, offset | (length << 16), 0, { 5, '@', 's', 'b', 'l', 'k' } };
+	    scopeStack[++scopeStackPos] = SegData[DEBSYM]->SDoffset;
+	    obj_write_bytes(SegData[DEBSYM], sizeof(bprel32), &bprel32);
+#endif
+	}
+	nextVarStatsLine++;
+    }
+}
+
+STATIC void cv4_flushLexicalScope()
+{
+    cv4_writeLexicalScope(globsym.top);
+    assert(scopeStackPos == 0);
+}
+
 /******************************************
  * Write out symbol table for current function.
  */
@@ -2332,6 +2477,8 @@
 
     cv4_outsym(s);		// put out function symbol
 
+    cv4_calcLexicalScope();
+
     // Put out local symbols
     endarg = 0;
     for (si = 0; si < globsym.top; si++)
@@ -2345,9 +2492,13 @@
 	    endarg = 1;
 	}
 #endif
+	if (sa->lnoscopestart)
+	    cv4_writeLexicalScope(si);
 	cv4_outsym(sa);
     }
 
+    cv4_flushLexicalScope();
+
     // Put out function return record
     if (1)
     {	unsigned char sreturn[2+2+2+1+1+4];
Index: backend/cgobj.c
===================================================================
--- backend/cgobj.c	(revision 317)
+++ backend/cgobj.c	(working copy)
@@ -931,6 +931,51 @@
     }
 }
 
+#define FROMWORD(ptr)	(*(unsigned short*)(ptr))
+#define FROMLONG(ptr)	(*(unsigned long*)(ptr))
+
+targ_size_t obj_getlineoffset(Srcpos srcpos)
+{
+    unsigned linnum = srcpos.Slinnum;
+    unsigned closestLine = 0;
+    targ_size_t closestOffset = 0;
+
+    if (!obj.term &&
+	(cseg < 0 || (srcpos.Sfilename && srcpos.Sfilename != obj.modname)))
+    {
+	list_t ll;
+	struct Linnum *ln;
+
+	for (ll = obj.linnum_list; ll; ll = list_next(ll))
+	{
+	    ln = (Linnum *)list_ptr(ll);
+	    if (
+#if MARS
+		(ln->filename == srcpos.Sfilename) &&
+#endif
+#if SCPP
+		(ln->filptr == *srcpos.Sfilptr) &&
+#endif
+		ln->cseg == cseg)
+	    {
+		for (int i = 0; i < ln->i; i += 2 + intsize)
+		{
+		    unsigned line = FROMWORD(&ln->data[i]);
+		    if (line == linnum)
+			return FROMLONG(&ln->data[i + 2]);
+		    if (line > linnum)
+			if (closestLine == 0 || line < closestLine)
+			{
+			    closestLine = line;
+			    closestOffset = FROMLONG(&ln->data[i + 2]);
+			}
+		}
+	    }
+	}
+    }
+    return closestOffset;
+}
+
 /***************************
  * Flush any pending line number records.
  */
Index: backend/global.h
===================================================================
--- backend/global.h	(revision 317)
+++ backend/global.h	(working copy)
@@ -383,6 +383,8 @@
 void obj_far16thunk(Symbol *s);
 void obj_fltused();
 
+targ_size_t obj_getlineoffset(Srcpos srcpos);
+
 // objrecor.c
 void objfile_open(const char *);
 void objfile_close(void *data, unsigned len);
Index: declaration.c
===================================================================
--- declaration.c	(revision 317)
+++ declaration.c	(working copy)
@@ -655,6 +655,7 @@
     canassign = 0;
     value = NULL;
     rundtor = NULL;
+    endlinnum = 0;
 }
 
 Dsymbol *VarDeclaration::syntaxCopy(Dsymbol *s)
@@ -1245,6 +1246,9 @@
 	}
 	sc = sc->pop();
     }
+
+    if(sc->scopesym)
+	endlinnum = sc->scopesym->endlinnum;
 }
 
 void VarDeclaration::semantic2(Scope *sc)
Index: declaration.h
===================================================================
--- declaration.h	(revision 317)
+++ declaration.h	(working copy)
@@ -254,6 +254,7 @@
 				// if the destructor should be run. Used to prevent
 				// dtor calls on postblitted vars
 #endif
+    unsigned endlinnum;	// line number of end of scope that this var lives in
 
     VarDeclaration(Loc loc, Type *t, Identifier *id, Initializer *init);
     Dsymbol *syntaxCopy(Dsymbol *);
Index: dsymbol.c
===================================================================
--- dsymbol.c	(revision 317)
+++ dsymbol.c	(working copy)
@@ -682,6 +682,7 @@
     symtab = NULL;
     imports = NULL;
     prots = NULL;
+    endlinnum = 0;
 }
 
 ScopeDsymbol::ScopeDsymbol(Identifier *id)
@@ -691,6 +692,7 @@
     symtab = NULL;
     imports = NULL;
     prots = NULL;
+    endlinnum = 0;
 }
 
 Dsymbol *ScopeDsymbol::syntaxCopy(Dsymbol *s)
Index: dsymbol.h
===================================================================
--- dsymbol.h	(revision 317)
+++ dsymbol.h	(working copy)
@@ -238,6 +238,8 @@
     Array *imports;		// imported ScopeDsymbol's
     unsigned char *prots;	// array of PROT, one for each import
 
+    unsigned endlinnum;		// the linnumber of the statement after the scope (0 if unknown)
+
     ScopeDsymbol();
     ScopeDsymbol(Identifier *id);
     Dsymbol *syntaxCopy(Dsymbol *s);
Index: func.c
===================================================================
--- func.c	(revision 317)
+++ func.c	(working copy)
@@ -765,6 +765,9 @@
 	// Establish function scope
 	ScopeDsymbol *ss = new ScopeDsymbol();
 	ss->parent = sc->scopesym;
+	ss->loc = loc;
+	ss->endlinnum = endloc.linnum;
+
 	Scope *sc2 = sc->push(ss);
 	sc2->func = this;
 	sc2->parent = this;
@@ -1028,6 +1031,8 @@
 
 	    ScopeDsymbol *sym = new ScopeDsymbol();
 	    sym->parent = sc2->scopesym;
+	    sym->loc = loc;
+	    sym->endlinnum = endloc.linnum;
 	    sc2 = sc2->push(sym);
 
 	    assert(type->nextOf());
Index: parse.c
===================================================================
--- parse.c	(revision 317)
+++ parse.c	(working copy)
@@ -3412,7 +3412,7 @@
 		Dsymbol *d = new StorageClassDeclaration(STCstatic, a);
 		s = new DeclarationStatement(loc, d);
 		if (flags & PSscope)
-		    s = new ScopeStatement(loc, s);
+		    s = new ScopeStatement(loc, this->loc, s);
 		break;
 	    }
 	    goto Ldeclaration;
@@ -3469,7 +3469,7 @@
 	    else
 		assert(0);
 	    if (flags & PSscope)
-		s = new ScopeStatement(loc, s);
+		s = new ScopeStatement(loc, this->loc, s);
 	    break;
 	}
 
@@ -3535,7 +3535,7 @@
 	    endloc = this->loc;
 	    s = new CompoundStatement(loc, statements);
 	    if (flags & (PSscope | PScurlyscope))
-		s = new ScopeStatement(loc, s);
+		s = new ScopeStatement(loc, this->loc, s);
 	    nextToken();
 	    break;
 	}
@@ -3549,7 +3549,7 @@
 	    condition = parseExpression();
 	    check(TOKrparen);
 	    body = parseStatement(PSscope);
-	    s = new WhileStatement(loc, condition, body);
+	    s = new WhileStatement(loc, this->loc, condition, body);
 	    break;
 	}
 
@@ -3609,9 +3609,9 @@
 		check(TOKrparen);
 	    }
 	    body = parseStatement(PSscope);
-	    s = new ForStatement(loc, init, condition, increment, body);
+	    s = new ForStatement(loc, this->loc, init, condition, increment, body);
 	    if (init)
-		s = new ScopeStatement(loc, s);
+		s = new ScopeStatement(loc, this->loc, s);
 	    break;
 	}
 
@@ -3672,13 +3672,13 @@
 		Expression *upr = parseExpression();
 		check(TOKrparen);
 		Statement *body = parseStatement(0);
-		s = new ForeachRangeStatement(loc, op, a, aggr, upr, body);
+		s = new ForeachRangeStatement(loc, this->loc, op, a, aggr, upr, body);
 	    }
 	    else
 	    {
 		check(TOKrparen);
 		Statement *body = parseStatement(0);
-		s = new ForeachStatement(loc, op, arguments, aggr, body);
+		s = new ForeachStatement(loc, this->loc, op, arguments, aggr, body);
 	    }
 	    break;
 	}
@@ -3882,7 +3882,7 @@
 		statements->push(parseStatement(PSsemi | PScurlyscope));
 	    }
 	    s = new CompoundStatement(loc, statements);
-	    s = new ScopeStatement(loc, s);
+	    s = new ScopeStatement(loc, this->loc, s);
 
 #if DMDV2
 	    if (last)
@@ -3918,7 +3918,7 @@
 		statements->push(parseStatement(PSsemi | PScurlyscope));
 	    }
 	    s = new CompoundStatement(loc, statements);
-	    s = new ScopeStatement(loc, s);
+	    s = new ScopeStatement(loc, this->loc, s);
 	    s = new DefaultStatement(loc, s);
 	    break;
 	}
Index: scope.c
===================================================================
--- scope.c	(revision 317)
+++ scope.c	(working copy)
@@ -138,6 +138,8 @@
     sc->module = module;
     sc->scopesym = new ScopeDsymbol();
     sc->scopesym->symtab = new DsymbolTable();
+    sc->scopesym->loc.filename = module->srcfile->toChars();
+    sc->scopesym->endlinnum = module->numlines;
 
     // Add top level package as member of this global scope
     Dsymbol *m = module;
Index: statement.c
===================================================================
--- statement.c	(revision 317)
+++ statement.c	(working copy)
@@ -499,7 +499,7 @@
 			    a->push(statements->data[j]);
 			}
 			body = new CompoundStatement(0, a);
-			body = new ScopeStatement(0, body);
+			body = new ScopeStatement(0, 0, body);
 
 			Identifier *id = Lexer::uniqueId("__o");
 
@@ -842,10 +842,11 @@
 
 /******************************** ScopeStatement ***************************/
 
-ScopeStatement::ScopeStatement(Loc loc, Statement *s)
+ScopeStatement::ScopeStatement(Loc loc, Loc endloc, Statement *s)
     : Statement(loc)
 {
     this->statement = s;
+    this->endloc = endloc;
 }
 
 Statement *ScopeStatement::syntaxCopy()
@@ -853,7 +854,7 @@
     Statement *s;
 
     s = statement ? statement->syntaxCopy() : NULL;
-    s = new ScopeStatement(loc, s);
+    s = new ScopeStatement(loc, endloc, s);
     return s;
 }
 
@@ -867,6 +868,9 @@
 
 	sym = new ScopeDsymbol();
 	sym->parent = sc->scopesym;
+	sym->loc = loc;
+	sym->endlinnum = endloc.linnum;
+
 	sc = sc->push(sym);
 
 	a = statement->flatten(sc);
@@ -944,16 +948,17 @@
 
 /******************************** WhileStatement ***************************/
 
-WhileStatement::WhileStatement(Loc loc, Expression *c, Statement *b)
+WhileStatement::WhileStatement(Loc loc, Loc endloc, Expression *c, Statement *b)
     : Statement(loc)
 {
     condition = c;
     body = b;
+    endloc = loc;
 }
 
 Statement *WhileStatement::syntaxCopy()
 {
-    WhileStatement *s = new WhileStatement(loc, condition->syntaxCopy(), body ? body->syntaxCopy() : NULL);
+    WhileStatement *s = new WhileStatement(loc, endloc, condition->syntaxCopy(), body ? body->syntaxCopy() : NULL);
     return s;
 }
 
@@ -963,7 +968,7 @@
     /* Rewrite as a for(;condition;) loop
      */
 
-    Statement *s = new ForStatement(loc, NULL, condition, NULL, body);
+    Statement *s = new ForStatement(loc, endloc, NULL, condition, NULL, body);
     s = s->semantic(sc);
     return s;
 }
@@ -1123,13 +1128,14 @@
 
 /******************************** ForStatement ***************************/
 
-ForStatement::ForStatement(Loc loc, Statement *init, Expression *condition, Expression *increment, Statement *body)
+ForStatement::ForStatement(Loc loc, Loc endloc, Statement *init, Expression *condition, Expression *increment, Statement *body)
     : Statement(loc)
 {
     this->init = init;
     this->condition = condition;
     this->increment = increment;
     this->body = body;
+    this->endloc = endloc;
 }
 
 Statement *ForStatement::syntaxCopy()
@@ -1143,7 +1149,7 @@
     Expression *inc = NULL;
     if (increment)
 	inc = increment->syntaxCopy();
-    ForStatement *s = new ForStatement(loc, i, c, inc, body->syntaxCopy());
+    ForStatement *s = new ForStatement(loc, endloc, i, c, inc, body->syntaxCopy());
     return s;
 }
 
@@ -1151,6 +1157,8 @@
 {
     ScopeDsymbol *sym = new ScopeDsymbol();
     sym->parent = sc->scopesym;
+    sym->loc = loc;
+    sym->endlinnum = endloc.linnum;
     sc = sc->push(sym);
     if (init)
 	init = init->semantic(sc);
@@ -1276,7 +1284,7 @@
 
 /******************************** ForeachStatement ***************************/
 
-ForeachStatement::ForeachStatement(Loc loc, enum TOK op, Parameters *arguments,
+ForeachStatement::ForeachStatement(Loc loc, Loc endloc, enum TOK op, Parameters *arguments,
 	Expression *aggr, Statement *body)
     : Statement(loc)
 {
@@ -1289,13 +1297,14 @@
     this->value = NULL;
 
     this->func = NULL;
+    this->endloc = endloc;
 }
 
 Statement *ForeachStatement::syntaxCopy()
 {
     Parameters *args = Parameter::arraySyntaxCopy(arguments);
     Expression *exp = aggr->syntaxCopy();
-    ForeachStatement *s = new ForeachStatement(loc, op, args, exp,
+    ForeachStatement *s = new ForeachStatement(loc, endloc, op, args, exp,
 	body ? body->syntaxCopy() : NULL);
     return s;
 }
@@ -1423,7 +1432,7 @@
 
 	    st->push(body->syntaxCopy());
 	    s = new CompoundStatement(loc, st);
-	    s = new ScopeStatement(loc, s);
+	    s = new ScopeStatement(loc, endloc, s);
 	    statements->push(s);
 	}
 
@@ -1434,6 +1443,8 @@
 
     sym = new ScopeDsymbol();
     sym->parent = sc->scopesym;
+    sym->loc = loc;
+    sym->endlinnum = endloc.linnum;
     sc = sc->push(sym);
 
     sc->noctor++;
@@ -1562,7 +1573,7 @@
 
 	    body = new CompoundStatement(loc, ds, body);
 
-	    ForStatement *fs = new ForStatement(loc, forinit, cond, increment, body);
+	    ForStatement *fs = new ForStatement(loc, endloc, forinit, cond, increment, body);
 	    s = fs->semantic(sc);
 	    break;
 	}
@@ -1697,7 +1708,7 @@
 	    Statement *body = new CompoundStatement(loc,
 		new DeclarationStatement(loc, de), this->body);
 
-	    s = new ForStatement(loc, init, condition, increment, body);
+	    s = new ForStatement(loc, endloc, init, condition, increment, body);
 #if 0
 	    printf("init: %s\n", init->toChars());
 	    printf("condition: %s\n", condition->toChars());
@@ -2018,7 +2029,7 @@
 
 #if DMDV2
 
-ForeachRangeStatement::ForeachRangeStatement(Loc loc, enum TOK op, Parameter *arg,
+ForeachRangeStatement::ForeachRangeStatement(Loc loc, Loc endloc, enum TOK op, Parameter *arg,
 	Expression *lwr, Expression *upr, Statement *body)
     : Statement(loc)
 {
@@ -2027,13 +2038,14 @@
     this->lwr = lwr;
     this->upr = upr;
     this->body = body;
+    this->endloc = endloc;
 
     this->key = NULL;
 }
 
 Statement *ForeachRangeStatement::syntaxCopy()
 {
-    ForeachRangeStatement *s = new ForeachRangeStatement(loc, op,
+    ForeachRangeStatement *s = new ForeachRangeStatement(loc, endloc, op,
 	arg->syntaxCopy(),
 	lwr->syntaxCopy(),
 	upr->syntaxCopy(),
@@ -2126,7 +2138,7 @@
 	// key += 1
 	increment = new AddAssignExp(loc, new VarExp(loc, key), new IntegerExp(1));
 
-    ForStatement *fs = new ForStatement(loc, forinit, cond, increment, body);
+    ForStatement *fs = new ForStatement(loc, endloc, forinit, cond, increment, body);
     s = fs->semantic(sc);
     return s;
 #else
@@ -2135,6 +2147,8 @@
 
     sym = new ScopeDsymbol();
     sym->parent = sc->scopesym;
+    sym->loc = loc;
+    sym->endlinnum = endloc.linnum;
     sc = sc->push(sym);
 
     sc->noctor++;
Index: statement.h
===================================================================
--- statement.h	(revision 317)
+++ statement.h	(working copy)
@@ -236,8 +236,9 @@
 struct ScopeStatement : Statement
 {
     Statement *statement;
+    Loc endloc;
 
-    ScopeStatement(Loc loc, Statement *s);
+    ScopeStatement(Loc loc, Loc endloc, Statement *s);
     Statement *syntaxCopy();
     void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
     ScopeStatement *isScopeStatement() { return this; }
@@ -259,8 +260,9 @@
 {
     Expression *condition;
     Statement *body;
+    Loc endloc;
 
-    WhileStatement(Loc loc, Expression *c, Statement *b);
+    WhileStatement(Loc loc, Loc endloc, Expression *c, Statement *b);
     Statement *syntaxCopy();
     Statement *semantic(Scope *sc);
     int hasBreak();
@@ -303,8 +305,9 @@
     Expression *condition;
     Expression *increment;
     Statement *body;
+    Loc endloc;
 
-    ForStatement(Loc loc, Statement *init, Expression *condition, Expression *increment, Statement *body);
+    ForStatement(Loc loc, Loc endloc, Statement *init, Expression *condition, Expression *increment, Statement *body);
     Statement *syntaxCopy();
     Statement *semantic(Scope *sc);
     void scopeCode(Scope *sc, Statement **sentry, Statement **sexit, Statement **sfinally);
@@ -332,11 +335,12 @@
     VarDeclaration *value;
 
     FuncDeclaration *func;	// function we're lexically in
+    Loc endloc;
 
     Array cases;	// put breaks, continues, gotos and returns here
     Array gotos;	// forward referenced goto's go here
 
-    ForeachStatement(Loc loc, enum TOK op, Parameters *arguments, Expression *aggr, Statement *body);
+    ForeachStatement(Loc loc, Loc endloc, enum TOK op, Parameters *arguments, Expression *aggr, Statement *body);
     Statement *syntaxCopy();
     Statement *semantic(Scope *sc);
     bool checkForArgTypes();
@@ -361,10 +365,11 @@
     Expression *lwr;
     Expression *upr;
     Statement *body;
+    Loc endloc;
 
     VarDeclaration *key;
 
-    ForeachRangeStatement(Loc loc, enum TOK op, Parameter *arg,
+    ForeachRangeStatement(Loc loc, Loc endloc, enum TOK op, Parameter *arg,
 	Expression *lwr, Expression *upr, Statement *body);
     Statement *syntaxCopy();
     Statement *semantic(Scope *sc);
Index: tocsym.c
===================================================================
--- tocsym.c	(revision 317)
+++ tocsym.c	(working copy)
@@ -268,6 +268,8 @@
 	s->Stype = t;
 
 	csym = s;
+	csym->lnoscopestart = loc.linnum;
+	csym->lnoscopeend = endlinnum;
     }
     return csym;
 }
