Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /// A variant type used to hold a single value in the CTFE machine stack. CTFE is basically restricted to value types
- /// (with the exception of dynamic arrays). Aggregate value types - structs, static arrays, and tuples - are simply stored
- /// as consecutive stack slots.
- struct CtfeValue
- {
- Type mType;
- union
- {
- bool mBool;
- ulong mInt;
- real mFloat;
- dchar mChar;
- void[] mArray;
- SmartPtr mPtr;
- FuncSym mFunc;
- }
- static CtfeValue opCall(T)(T val)
- {
- CtfeValue ret;
- ret = val;
- return ret;
- }
- static CtfeValue errVal()
- {
- CtfeValue ret;
- ret.mType = ErrorType.get();
- return ret;
- }
- static CtfeValue stackPtr(size_t offset)
- {
- CtfeValue ret;
- ret.mType = PointerType.get(BasicType.get(Tvoid));
- ret.mPtr.isStack = true;
- ret.mPtr.mStackOffset = offset;
- return ret;
- }
- static CtfeValue heapPtr(CtfeValue* ptr)
- {
- CtfeValue ret;
- ret.mType = PointerType.get(BasicType.get(Tvoid));
- ret.mPtr.mPtr = ptr;
- return ret;
- }
- void opAssign(bool b)
- {
- mType = BasicType.get(Tbool);
- mBool = b;
- }
- void opAssign(byte b)
- {
- mType = BasicType.get(Tint8);
- mInt = b;
- }
- void opAssign(ubyte b)
- {
- mType = BasicType.get(Tuint8);
- mInt = b;
- }
- void opAssign(short b)
- {
- mType = BasicType.get(Tint16);
- mInt = b;
- }
- void opAssign(ushort b)
- {
- mType = BasicType.get(Tuint16);
- mInt = b;
- }
- void opAssign(int b)
- {
- mType = BasicType.get(Tint32);
- mInt = b;
- }
- void opAssign(uint b)
- {
- mType = BasicType.get(Tuint32);
- mInt = b;
- }
- void opAssign(long b)
- {
- mType = BasicType.get(Tint64);
- mInt = b;
- }
- void opAssign(ulong b)
- {
- mType = BasicType.get(Tuint64);
- mInt = b;
- }
- void opAssign(float f)
- {
- mType = BasicType.get(Tfloat32);
- mFloat = f;
- }
- void opAssign(double f)
- {
- mType = BasicType.get(Tfloat64);
- mFloat = f;
- }
- void opAssign(real f)
- {
- mType = BasicType.get(Treal);
- mFloat = f;
- }
- void opAssign(char c)
- {
- mType = BasicType.get(Tchar);
- mChar = c;
- }
- void opAssign(wchar c)
- {
- mType = BasicType.get(Twchar);
- mChar = c;
- }
- void opAssign(dchar c)
- {
- mType = BasicType.get(Tdchar);
- mChar = c;
- }
- void opAssign(char[] s)
- {
- mType = arrayOf(BasicType.get(Tchar));
- mArray = s;
- }
- void opAssign(wchar[] s)
- {
- mType = arrayOf(BasicType.get(Twchar));
- mArray = s;
- }
- void opAssign(dchar[] s)
- {
- mType = arrayOf(BasicType.get(Tdchar));
- mArray = s;
- }
- void opAssign(FuncSym s)
- {
- mType = s.mType;
- mFunc = s;
- }
- bool toBool()
- {
- assert(mType.mTypeTag == Tbool);
- return mBool;
- }
- byte toI8()
- {
- assert(mType.mTypeTag == Tint8);
- return cast(byte)((cast(long)mInt) & 0xFF);
- }
- ubyte toU8()
- {
- assert(mType.mTypeTag == Tuint8);
- return cast(ubyte)(mInt & 0xFF);
- }
- short toI16()
- {
- assert(mType.mTypeTag == Tint16);
- return cast(short)((cast(long)mInt) & 0xFFFF);
- }
- ushort toU16()
- {
- assert(mType.mTypeTag == Tuint16);
- return cast(ushort)(mInt & 0xFFFF);
- }
- int toI32()
- {
- assert(mType.mTypeTag == Tint32, typeTagToString(mType.mTypeTag));
- return cast(int)((cast(long)mInt) & 0xFFFF_FFFF);
- }
- uint toU32()
- {
- assert(mType.mTypeTag == Tuint32);
- return cast(uint)(mInt & 0xFFFF_FFFF);
- }
- long toI64()
- {
- assert(mType.mTypeTag == Tint64);
- return cast(long)mInt;
- }
- ulong toU64()
- {
- assert(mType.mTypeTag == Tuint64);
- return mInt;
- }
- float toF32()
- {
- assert(mType.mTypeTag == Tfloat32);
- return cast(float)mFloat;
- }
- double toF64()
- {
- assert(mType.mTypeTag == Tfloat64);
- return cast(double)mFloat;
- }
- real toF80()
- {
- assert(mType.mTypeTag == Treal);
- return mFloat;
- }
- char toChar()
- {
- assert(mType.mTypeTag == Tchar);
- return mChar & 0xFF;
- }
- wchar toWChar()
- {
- assert(mType.mTypeTag == Twchar);
- return mChar & 0xFFFF;
- }
- dchar toDChar()
- {
- assert(mType.mTypeTag == Tdchar);
- return mChar;
- }
- FuncSym toFunc()
- {
- assert(mType.mTypeTag == Tfunction);
- return mFunc;
- }
- CtfeValue* toPtr(CtfeValue* stackBottom)
- {
- assert(mType.mTypeTag == Tpointer);
- return mPtr.isStack ? stackBottom + mPtr.mStackOffset : cast(CtfeValue*)mPtr.mPtr;
- }
- char[] toStr()
- {
- assert(mType.mTypeTag == Tdarray && mType.next.mTypeTag == Tchar);
- return cast(char[])mArray;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement