view dmd/FuncLiteralDeclaration.d @ 187:b0d41ff5e0df

Added expandability scheme outlined in http://www.dsource.org/forums/viewtopic.php?t=5659&sid=6f2150ff5b0bffcd47512a6a7608d218
author Abscissa
date Tue, 07 Jun 2011 23:37:34 -0400
parents e3afd1303184
children
line wrap: on
line source

module dmd.FuncLiteralDeclaration;

import dmd.common;
import dmd.FuncDeclaration;
import dmd.TOK;
import dmd.Loc;
import dmd.Type;
import dmd.ForeachStatement;
import dmd.OutBuffer;
import dmd.HdrGenState;
import dmd.Dsymbol;
import dmd.STC;
import dmd.Lexer;

import dmd.DDMDExtensions;

class FuncLiteralDeclaration : FuncDeclaration
{
	mixin insertMemberExtension!(typeof(this));

    TOK tok;			// TOKfunction or TOKdelegate

    this(Loc loc, Loc endloc, Type type, TOK tok, ForeachStatement fes)
	{
		register();
		super(loc, endloc, null, STC.STCundefined, type);
		
		string id;

		if (fes)
			id = "__foreachbody";
		else if (tok == TOK.TOKdelegate)
			id = "__dgliteral";
		else
			id = "__funcliteral";

		this.ident = Lexer.uniqueId(id);
		this.tok = tok;
		this.fes = fes;

		//printf("FuncLiteralDeclaration() id = '%s', type = '%s'\n", this->ident->toChars(), type->toChars());
	}
	
    override void toCBuffer(OutBuffer buf, HdrGenState* hgs)
	{
    	buf.writestring(kind());
        buf.writeByte(' ');
        type.toCBuffer(buf, null, hgs);
        bodyToCBuffer(buf, hgs);
	}

    override Dsymbol syntaxCopy(Dsymbol s)
	{
		FuncLiteralDeclaration f;

		//printf("FuncLiteralDeclaration.syntaxCopy('%s')\n", toChars());
		if (s)
			f = cast(FuncLiteralDeclaration)s;
		else
		{	
			f = new FuncLiteralDeclaration(loc, endloc, type.syntaxCopy(), tok, fes);
			f.ident = ident;		// keep old identifier
		}
		FuncDeclaration.syntaxCopy(f);
		return f;
	}
	
    override bool isNested()
	{
		//printf("FuncLiteralDeclaration::isNested() '%s'\n", toChars());
		return (tok == TOK.TOKdelegate);
	}
	
    override bool isVirtual()
	{
		return false;
	}

    override FuncLiteralDeclaration isFuncLiteralDeclaration() { return this; }

    override string kind()
	{
		return (tok == TOKdelegate) ? "delegate" : "function";
	}
}