view dmd/CommaExp.d @ 63:cab4c37afb89

A bunch of implementations
author korDen
date Mon, 23 Aug 2010 16:52:24 +0400
parents 51605de93870
children 2e2a5c3f943a
line wrap: on
line source

module dmd.CommaExp;

import dmd.Loc;
import dmd.BinExp;
import dmd.IRState;
import dmd.Scope;
import dmd.IntRange;
import dmd.Expression;
import dmd.MATCH;
import dmd.WANT;
import dmd.TOK;
import dmd.Type;
import dmd.InterState;

import dmd.backend.elem;
import dmd.backend.Util;

class CommaExp : BinExp
{
    this(Loc loc, Expression e1, Expression e2)
	{
		super(loc, TOK.TOKcomma, CommaExp.sizeof, e1, e2);
	}
	
    Expression semantic(Scope sc)
	{
		if (!type)
		{	
			BinExp.semanticp(sc);
			type = e2.type;
		}
		return this;
	}
	
    void checkEscape()
	{
		e2.checkEscape();
	}
	
    IntRange getIntRange()
	{
		assert(false);
	}

version (DMDV2) {
    int isLvalue()
	{
		return e2.isLvalue();
	}
}
    Expression toLvalue(Scope sc, Expression e)
	{
		e2 = e2.toLvalue(sc, null);
		return this;
	}
	
    Expression modifiableLvalue(Scope sc, Expression e)
	{
		e2 = e2.modifiableLvalue(sc, e);
		return this;
	}
	
    bool isBool(bool result)
	{
		return e2.isBool(result);
	}
	
    bool checkSideEffect(int flag)
	{
		if (flag == 2)
			return e1.checkSideEffect(2) || e2.checkSideEffect(2);
		else
		{
			// Don't check e1 until we cast(void) the a,b code generation
			return e2.checkSideEffect(flag);
		}
	}
	
    MATCH implicitConvTo(Type t)
	{
		return e2.implicitConvTo(t);
	}
	
    Expression castTo(Scope sc, Type t)
	{
		Expression e2c = e2.castTo(sc, t);
		Expression e;

		if (e2c != e2)
		{
			e = new CommaExp(loc, e1, e2c);
			e.type = e2c.type;
		}
		else
		{	
			e = this;
			e.type = e2.type;
		}
		return e;
	}
	
    Expression optimize(int result)
	{
		Expression e;

		//printf("CommaExp.optimize(result = %d) %s\n", result, toChars());
		e1 = e1.optimize(result & WANTinterpret);
		e2 = e2.optimize(result);
		if (!e1 || e1.op == TOKint64 || e1.op == TOKfloat64 || !e1.checkSideEffect(2))
		{
			e = e2;
			if (e)
				e.type = type;
		}
		else
			e = this;
		//printf("-CommaExp.optimize(result = %d) %s\n", result, e.toChars());
		return e;
	}
	
    Expression interpret(InterState istate)
	{	
		assert(false);
	}
	
    elem* toElem(IRState* irs)
	{
		assert(e1 && e2);
		elem* eleft  = e1.toElem(irs);
		elem* eright = e2.toElem(irs);
		elem* e = el_combine(eleft, eright);
		if (e)
			el_setLoc(e, loc);
		return e;
	}
}