view dmd/Optimize.d @ 178:e3afd1303184

Many small bugs fixed Made all classes derive from TObject to detect memory leaks (functionality is disabled for now) Began work on overriding backend memory allocations (to avoid memory leaks)
author korDen
date Sun, 17 Oct 2010 07:42:00 +0400
parents 09c858522d55
children
line wrap: on
line source

module dmd.Optimize;

import dmd.common;
import dmd.Expression;
import dmd.TOK;
import dmd.VarExp;
import dmd.VarDeclaration;
import dmd.STC;
import dmd.AssignExp;
import dmd.Type;
import dmd.WANT;
import dmd.TY;

/*************************************
 * If variable has a const initializer,
 * return that initializer.
 */

Expression expandVar(int result, VarDeclaration v)
{
    //printf("expandVar(result = %d, v = %p, %s)\n", result, v, v ? v.toChars() : "null");

    Expression e = null;
    if (!v)
    	return e;

    if (v.isConst() || v.isImmutable() || v.storage_class & STC.STCmanifest)
    {
	if (!v.type)
	{
	    //error("ICE");
	    return e;
	}

	Type tb = v.type.toBasetype();
	if (result & WANT.WANTinterpret ||
	    v.storage_class & STC.STCmanifest ||
	    (tb.ty != TY.Tsarray && tb.ty != TY.Tstruct)
	   )
	{
	    if (v.init)
	    {
		if (v.inuse)
		{   if (v.storage_class & STC.STCmanifest)
			v.error("recursive initialization of constant");
		    goto L1;
		}
		Expression ei = v.init.toExpression();
		if (!ei)
		    goto L1;
		if (ei.op == TOK.TOKconstruct || ei.op == TOK.TOKblit)
		{   AssignExp ae = cast(AssignExp)ei;
		    ei = ae.e2;
		    if (ei.isConst() != 1 && ei.op != TOK.TOKstring)
			goto L1;
		    if (ei.type != v.type)
			goto L1;
		}
		if (v.scope_)
		{
		    v.inuse++;
		    e = ei.syntaxCopy();
		    e = e.semantic(v.scope_);
		    e = e.implicitCastTo(v.scope_, v.type);
		    // enabling this line causes test22 in test suite to fail
		    //ei.type = e.type;
		    v.scope_ = null;
		    v.inuse--;
		}
		else if (!ei.type)
		{
		    goto L1;
		}
		else
		    // Should remove the copy() operation by
		    // making all mods to expressions copy-on-write
		    e = ei.copy();
	    }
	    else
	    {
static if (true) {
		goto L1;
} else {
		// BUG: what if const is initialized in constructor?
		e = v.type.defaultInit();
		e.loc = e1.loc;
}
	    }
	    if (e.type != v.type)
	    {
		e = e.castTo(null, v.type);
	    }
	    v.inuse++;
	    e = e.optimize(result);
	    v.inuse--;
	}
    }
L1:
    //if (e) printf("\te = %s, e.type = %s\n", e.toChars(), e.type.toChars());
    return e;
}