diff dmd/ForStatement.d @ 0:10317f0c89a5

Initial commit
author korDen
date Sat, 24 Oct 2009 08:42:06 +0400
parents
children cab4c37afb89
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/dmd/ForStatement.d	Sat Oct 24 08:42:06 2009 +0400
@@ -0,0 +1,251 @@
+module dmd.ForStatement;
+
+import dmd.Statement;
+import dmd.Expression;
+import dmd.Loc;
+import dmd.Scope;
+import dmd.InterState;
+import dmd.OutBuffer;
+import dmd.HdrGenState;
+import dmd.InlineScanState;
+import dmd.WANT;
+import dmd.ScopeDsymbol;
+import dmd.IRState;
+import dmd.BE;
+
+import dmd.backend.Blockx;
+import dmd.backend.block;
+import dmd.backend.Util;
+import dmd.backend.BC;
+
+class ForStatement : Statement
+{
+    Statement init;
+    Expression condition;
+    Expression increment;
+    Statement body_;
+
+    this(Loc loc, Statement init, Expression condition, Expression increment, Statement body_)
+	{
+		super(loc);
+		
+		this.init = init;
+		this.condition = condition;
+		this.increment = increment;
+		this.body_ = body_;
+	}
+
+    Statement syntaxCopy()
+	{
+		Statement i = null;
+		if (init)
+			i = init.syntaxCopy();
+		Expression c = null;
+		if (condition)
+			c = condition.syntaxCopy();
+		Expression inc = null;
+		if (increment)
+			inc = increment.syntaxCopy();
+		ForStatement s = new ForStatement(loc, i, c, inc, body_.syntaxCopy());
+		return s;
+	}
+	
+    Statement semantic(Scope sc)
+	{
+		ScopeDsymbol sym = new ScopeDsymbol();
+		sym.parent = sc.scopesym;
+		sc = sc.push(sym);
+		if (init)
+			init = init.semantic(sc);
+		sc.noctor++;
+		if (condition)
+		{
+			condition = condition.semantic(sc);
+			condition = resolveProperties(sc, condition);
+			condition = condition.optimize(WANTvalue);
+			condition = condition.checkToBoolean();
+		}
+		if (increment)
+		{
+			increment = increment.semantic(sc);
+			increment = resolveProperties(sc, increment);
+		}
+
+		sc.sbreak = this;
+		sc.scontinue = this;
+		if (body_)
+			body_ = body_.semantic(sc);
+		sc.noctor--;
+
+		sc.pop();
+		return this;
+	}
+	
+    void scopeCode(Scope sc, Statement* sentry, Statement* sexception, Statement* sfinally)
+	{
+		//printf("ForStatement::scopeCode()\n");
+		//print();
+		if (init)
+			init.scopeCode(sc, sentry, sexception, sfinally);
+		else
+			Statement.scopeCode(sc, sentry, sexception, sfinally);
+	}
+	
+    bool hasBreak()
+	{
+		//printf("ForStatement.hasBreak()\n");
+		return true;
+	}
+	
+    bool hasContinue()
+	{
+		return true;
+	}
+	
+    bool usesEH()
+	{
+		return (init && init.usesEH()) || body_.usesEH();
+	}
+	
+    BE blockExit()
+	{
+		BE result = BE.BEfallthru;
+
+		if (init)
+		{	
+			result = init.blockExit();
+			if (!(result & BE.BEfallthru))
+				return result;
+		}
+		if (condition)
+		{	
+			if (condition.canThrow())
+				result |= BE.BEthrow;
+			if (condition.isBool(true))
+				result &= ~BE.BEfallthru;
+			else if (condition.isBool(false))
+				return result;
+		}
+		else
+			result &= ~BE.BEfallthru;	// the body must do the exiting
+		if (body_)
+		{	
+			int r = body_.blockExit();
+			if (r & (BE.BEbreak | BE.BEgoto))
+				result |= BE.BEfallthru;
+			result |= r & ~(BE.BEfallthru | BE.BEbreak | BE.BEcontinue);
+		}
+		if (increment && increment.canThrow())
+			result |= BE.BEthrow;
+		return result;
+	}
+	
+    bool comeFrom()
+	{
+		//printf("ForStatement.comeFrom()\n");
+		if (body_)
+		{	
+			bool result = body_.comeFrom();
+			//printf("result = %d\n", result);
+			return result;
+		}
+		return false;
+	}
+	
+    Expression interpret(InterState* istate)
+	{
+		assert(false);
+	}
+	
+    void toCBuffer(OutBuffer buf, HdrGenState* hgs)
+	{
+		buf.writestring("for (");
+		if (init)
+		{
+			hgs.FLinit.init++;
+			init.toCBuffer(buf, hgs);
+			hgs.FLinit.init--;
+		}
+		else
+			buf.writebyte(';');
+		if (condition)
+		{   buf.writebyte(' ');
+			condition.toCBuffer(buf, hgs);
+		}
+		buf.writebyte(';');
+		if (increment)
+		{   
+			buf.writebyte(' ');
+			increment.toCBuffer(buf, hgs);
+		}
+		buf.writebyte(')');
+		buf.writenl();
+		buf.writebyte('{');
+		buf.writenl();
+		body_.toCBuffer(buf, hgs);
+		buf.writebyte('}');
+		buf.writenl();
+	}
+	
+    Statement inlineScan(InlineScanState* iss)
+	{
+		if (init)
+			init = init.inlineScan(iss);
+		if (condition)
+			condition = condition.inlineScan(iss);
+		if (increment)
+			increment = increment.inlineScan(iss);
+		if (body_)
+			body_ = body_.inlineScan(iss);
+		return this;
+	}
+	
+    void toIR(IRState* irs)
+	{
+		Blockx* blx = irs.blx;
+
+		IRState mystate = IRState(irs,this);
+		mystate.breakBlock = block_calloc(blx);
+		mystate.contBlock = block_calloc(blx);
+
+		if (init)
+			init.toIR(&mystate);
+		block* bpre = blx.curblock;
+		block_next(blx,BCgoto,null);
+		block* bcond = blx.curblock;
+		list_append(&bpre.Bsucc, bcond);
+		list_append(&mystate.contBlock.Bsucc, bcond);
+		if (condition)
+		{
+			incUsage(irs, condition.loc);
+			block_appendexp(bcond, condition.toElem(&mystate));
+			block_next(blx,BCiftrue,null);
+			list_append(&bcond.Bsucc, blx.curblock);
+			list_append(&bcond.Bsucc, mystate.breakBlock);
+		}
+		else
+		{
+			/* No conditional, it's a straight goto
+			 */
+			block_next(blx,BCgoto,null);
+			list_append(&bcond.Bsucc, blx.curblock);
+		}
+
+		if (body_)
+			body_.toIR(&mystate);
+		/* End of the body goes to the continue block
+		 */
+		list_append(&blx.curblock.Bsucc, mystate.contBlock);
+		block_next(blx, BCgoto, mystate.contBlock);
+
+		if (increment)
+		{
+			incUsage(irs, increment.loc);
+			block_appendexp(mystate.contBlock, increment.toElem(&mystate));
+		}
+
+		/* The 'break' block follows the for statement.
+		 */
+		block_next(blx,BCgoto, mystate.breakBlock);
+	}
+}
\ No newline at end of file