view dmd/TypeEnum.d @ 0:10317f0c89a5

Initial commit
author korDen
date Sat, 24 Oct 2009 08:42:06 +0400
parents
children ee3a9f34dc48
line wrap: on
line source

module dmd.TypeEnum;

import dmd.Type;
import dmd.EnumDeclaration;
import dmd.Scope;
import dmd.Loc;
import dmd.Id;
import dmd.ErrorExp;
import dmd.Dsymbol;
import dmd.EnumMember;
import dmd.OutBuffer;
import dmd.HdrGenState;
import dmd.Expression;
import dmd.Identifier;
import dmd.MATCH;
import dmd.OutBuffer;
import dmd.CppMangleState;
import dmd.TypeInfoDeclaration;
import dmd.TypeInfoEnumDeclaration;
import dmd.ArrayTypes;
import dmd.TY;
import dmd.Util;

import dmd.backend.TYPE;

class TypeEnum : Type
{
    EnumDeclaration sym;

    this(EnumDeclaration sym)
	{
		super(TY.Tenum);
		this.sym = sym;
	}
	
version (DumbClone) {
} else {
	Type clone()
	{
		assert(false);
	}
}
    Type syntaxCopy()
	{
		assert(false);
	}
	
    ulong size(Loc loc)
	{
		if (!sym.memtype)
		{
			error(loc, "enum %s is forward referenced", sym.toChars());
			return 4;
		}
		return sym.memtype.size(loc);
	}
	
    uint alignsize()
	{
		assert(false);
	}
	
    string toChars()
	{
		assert(false);
	}
	
    Type semantic(Loc loc, Scope sc)
	{
		//printf("TypeEnum::semantic() %s\n", toChars());
		//sym.semantic(sc);
		return merge();
	}
	
    Dsymbol toDsymbol(Scope sc)
	{
		return sym;
	}
	
    void toDecoBuffer(OutBuffer buf, int flag)
	{
		string name = sym.mangle();
		Type.toDecoBuffer(buf, flag);
		buf.printf("%s", name);
	}
	
    void toCBuffer2(OutBuffer buf, HdrGenState* hgs, int mod)
	{
		assert(false);
	}

    Expression dotExp(Scope sc, Expression e, Identifier ident)
	{
	version (LOGDOTEXP) {
		printf("TypeEnum::dotExp(e = '%s', ident = '%s') '%s'\n", e.toChars(), ident.toChars(), toChars());
	}
		Dsymbol s = sym.search(e.loc, ident, 0);
		if (!s)
		{
			if (ident is Id.max ||
				ident is Id.min ||
				ident is Id.init_ ||
				ident is Id.stringof_ ||
				!sym.memtype
			   )
			{
				return getProperty(e.loc, ident);
			}

			return sym.memtype.dotExp(sc, e, ident);
		}

		EnumMember m = s.isEnumMember();
		Expression em = m.value.copy();
		em.loc = e.loc;
		return em;
	}
	
    Expression getProperty(Loc loc, Identifier ident)
	{
		assert(false);
	}
	
    bool isintegral()
	{
		return true;
	}
	
    bool isfloating()
	{
		return false;
	}
	
    bool isscalar()
	{
		return true;
		//return sym.memtype.isscalar();
	}
	
    bool isunsigned()
	{
		return sym.memtype.isunsigned();
	}
	
    MATCH implicitConvTo(Type to)
	{
		MATCH m;

		//printf("TypeEnum::implicitConvTo()\n");
		if (ty == to.ty && sym == (cast(TypeEnum)to).sym)
			m = (mod == to.mod) ? MATCHexact : MATCHconst;
		else if (sym.memtype.implicitConvTo(to))
			m = MATCHconvert;	// match with conversions
		else
			m = MATCHnomatch;	// no match
		return m;
	}
	
    MATCH constConv(Type to)
	{
		assert(false);
	}
	
    Type toBasetype()
	{
		if (!sym.memtype)
		{
			debug writef("2: ");
			error(sym.loc, "enum %s is forward referenced", sym.toChars());
			return tint32;
		}

		return sym.memtype.toBasetype();
	}
	
    Expression defaultInit(Loc loc)
	{
	version (LOGDEFAULTINIT) {
		printf("TypeEnum::defaultInit() '%s'\n", toChars());
	}
		// Initialize to first member of enum
		//printf("%s\n", sym.defaultval.type.toChars());
		if (!sym.defaultval)
		{
			error(loc, "forward reference of %s.init", toChars());
			return new ErrorExp();
		}
		return sym.defaultval;
	}
	
    bool isZeroInit(Loc loc)
	{
		if (!sym.defaultval)
		{
			debug writef("3: ");
			error(loc, "enum %s is forward referenced", sym.toChars());
			return 0;
		}
		return sym.defaultval.isBool(false);
	}
	
    MATCH deduceType(Scope sc, Type tparam, TemplateParameters parameters, Objects dedtypes)
	{
		assert(false);
	}
	
    TypeInfoDeclaration getTypeInfoDeclaration()
	{
		return new TypeInfoEnumDeclaration(this);
	}
	
    bool hasPointers()
	{
		return toBasetype().hasPointers();
	}
	
version (CPP_MANGLE) {
    void toCppMangle(OutBuffer buf, CppMangleState* cms)
	{
		assert(false);
	}
}

    type* toCtype()
	{
		return sym.memtype.toCtype();
	}
}