Commit 94b1ed6c authored by ussrhero's avatar ussrhero

Merge branch 'expreval' into 'vs2017-support'

refactored VariantNum

See merge request !9
parents 59a5ce90 e66d9b93
......@@ -30,10 +30,18 @@ typedef std::list< SymbolOffset > SymbolOffsetList;
typedef std::list< std::wstring > TypeNameList;
class Module : private boost::noncopyable, public NumBehavior {
class Module : private boost::noncopyable {
public:
operator NumVariant() {
return getBase();
}
operator NumVariant() const {
return getBase();
}
virtual std::wstring getName() = 0;
virtual MEMOFFSET_64 getBase() const = 0;
......
......@@ -70,7 +70,7 @@ TypedVarPtr loadFloatVar( float var );
TypedVarPtr loadDoubleVar( double var );
TypedVarPtr loadWCharVar( wchar_t var );
class TypedVar : public NumBehavior {
class TypedVar : private boost::noncopyable {
friend TypedVarPtr loadTypedVar( const SymbolPtr &symbol );
......@@ -87,7 +87,25 @@ class TypedVar : public NumBehavior {
friend TypedVarPtr loadTypedVar( const std::wstring &funcName, const std::wstring &prototype);
public:
operator NumVariant() {
return getValue();
}
operator NumVariant() const {
return getValue();
}
template <typename T>
operator T() {
return static_cast<T>(getValue());
}
template <typename T>
operator T() const {
return static_cast<T>(getValue());
}
virtual std::wstring str() = 0;
virtual VarStorage getStorage() const = 0;
virtual std::wstring getRegisterName() const = 0;
......@@ -136,9 +154,20 @@ protected:
///////////////////////////////////////////////////////////////////////////////
class TypedValue : public NumBehavior
class TypedValue
{
public:
operator NumVariant() {
return getValue();
}
operator NumVariant() const {
return getValue();
}
TypedValue();
TypedValue(const TypedVarPtr& var) : m_value(var){}
......@@ -294,13 +323,15 @@ private:
TypedVarPtr m_value;
};
inline
NumBehavior::operator TypedValue() {
return TypedValue( getValue() );
}
//inline
//NumBehavior::operator TypedValue() {
// return TypedValue( getValue() );
//}
TypedValue callRaw(MEMOFFSET_64 addr, CallingConventionType callingConvention, const TypedValueList& arglst);
TypedValue evalExpr(const std::wstring& expr, const std::list< std::pair<std::wstring, TypedValue> >& scope = {});
///////////////////////////////////////////////////////////////////////////////
} // end kdlib namespace
......@@ -34,7 +34,7 @@ MEMOFFSET_64 getSymbolOffset( const std::wstring &name );
std::wstring findSymbol( MEMOFFSET_64 offset);
std::wstring findSymbol( MEMOFFSET_64 offset, MEMDISPLACEMENT &displacement );
class TypeInfo : private boost::noncopyable, public NumBehavior {
class TypeInfo : private boost::noncopyable {
friend TypeInfoPtr loadType( const std::wstring &symName );
friend TypeInfoPtr loadType( SymbolPtr &symbol );
......@@ -43,6 +43,14 @@ class TypeInfo : private boost::noncopyable, public NumBehavior {
public:
operator NumVariant() {
return getValue();
}
operator NumVariant() const {
return getValue();
}
virtual std::wstring str() = 0;
virtual std::wstring getName() = 0;
......@@ -135,7 +143,7 @@ protected:
static bool isComplexType( const std::wstring &typeName );
static TypeInfoPtr getTypeInfoFromCache(const std::wstring &typeName );
static TypeInfoPtr getBaseTypeInfo( const std::wstring &typeName, size_t ptrSize );
static TypeInfoPtr getBaseTypeInfo( const std::wstring &typeName, size_t ptrSize = 0);
static TypeInfoPtr getBaseTypeInfo( SymbolPtr &symbolScope );
static TypeInfoPtr getComplexTypeInfo( const std::wstring &typeName, SymbolPtr &symbolScope );
......
......@@ -117,7 +117,7 @@ struct rshift_op {
class NumVariant : boost::operators<NumVariant>, boost::left_shiftable<NumVariant>, boost::right_shiftable<NumVariant>
class NumVariant
{
public:
......@@ -491,6 +491,41 @@ public:
m_doubleVal = val;
}
friend bool operator==(const NumVariant& v1, const NumVariant& v2);
friend bool operator!=(const NumVariant& v1, const NumVariant& v2);
friend bool operator<(const NumVariant& v1, const NumVariant& v2);
friend bool operator<=(const NumVariant& v1, const NumVariant& v2);
friend bool operator>(const NumVariant& v1, const NumVariant& v2);
friend bool operator>=(const NumVariant& v1, const NumVariant& v2);
friend bool operator&&(const NumVariant& v1, const NumVariant& v2);
friend bool operator||(const NumVariant& v1, const NumVariant& v2);
friend NumVariant operator+(const NumVariant& v1, const NumVariant& v2);
friend NumVariant operator-(const NumVariant& v1, const NumVariant& v2);
friend NumVariant operator*(const NumVariant& v1, const NumVariant& v2);
friend NumVariant operator/(const NumVariant& v1, const NumVariant& v2);
friend NumVariant operator^(const NumVariant& v1, const NumVariant& v2);
friend NumVariant operator&(const NumVariant& v1, const NumVariant& v2);
friend NumVariant operator|(const NumVariant& v1, const NumVariant& v2);
friend NumVariant operator%(const NumVariant& v1, const NumVariant& v2);
friend NumVariant operator<<(const NumVariant& v1, const NumVariant& v2);
friend NumVariant operator>>(const NumVariant& v1, const NumVariant& v2);
friend NumVariant operator~(const NumVariant& v1);
friend bool operator!(const NumVariant& v1);
friend NumVariant operator++(NumVariant& v1);
friend NumVariant operator--(NumVariant& v1);
friend NumVariant operator++(NumVariant& v1, int);
friend NumVariant operator--(NumVariant& v1, int);
friend NumVariant operator-(const NumVariant& v1);
/*
friend bool operator==(const NumVariant& v1, const NumVariant& v2)
{
return NumVariant::op<bool, equal_op >( v1, v2 );
......@@ -602,10 +637,11 @@ public:
{
return NumVariant::op<NumVariant, sub_op >( NumVariant(0), v1 );
}
*/
template< typename RetT, template<class> class FuncT>
static
RetT op( const NumVariant& v1, const NumVariant& v2 )
RetT all_op( const NumVariant& v1, const NumVariant& v2 )
{
NumVariant::NumType t = maxType( v1, v2 );
NumVariant v3 = v1.cast( t );
......@@ -620,12 +656,12 @@ public:
return FuncT<double>()(v3.m_doubleVal, v4.m_doubleVal);
}
return whole_op<RetT, FuncT>( v3, v4 );
return int_op<RetT, FuncT>( v3, v4 );
}
template< typename RetT, template<class> class FuncT>
static
RetT whole_op( const NumVariant& v1, const NumVariant& v2 )
RetT int_op( const NumVariant& v1, const NumVariant& v2 )
{
NumVariant::NumType t = maxType( v1, v2 );
NumVariant v3 = v1.cast( t );
......@@ -671,6 +707,9 @@ public:
throw NumVariantError();
}
template <typename T>
operator T();
private:
enum NumType { charT, ucharT, shortT, ushortT, longT, ulongT, longlongT, ulonglongT, intT, uintT, floatT, doubleT, maxT };
......@@ -835,7 +874,201 @@ private:
};
inline bool operator==(const NumVariant& v1, const NumVariant& v2)
{
return NumVariant::all_op<bool, equal_op>(v1, v2);
}
inline bool operator!=(const NumVariant& v1, const NumVariant& v2)
{
return !(v1 == v2);
}
inline bool operator<(const NumVariant& v1, const NumVariant& v2)
{
return NumVariant::all_op<bool, gt_op>(v1, v2);
}
inline bool operator<=(const NumVariant& v1, const NumVariant& v2)
{
return !(v1 > v2);
}
inline bool operator>(const NumVariant& v1, const NumVariant& v2)
{
return v2 < v1;
}
inline bool operator>=(const NumVariant& v1, const NumVariant& v2)
{
return !(v1 < v2);
}
inline bool operator&&(const NumVariant& v1, const NumVariant& v2)
{
return !(NumVariant::all_op<bool, equal_op >(v1, NumVariant(0)) || NumVariant::all_op<bool, equal_op >(v2, NumVariant(0)));
}
inline bool operator||(const NumVariant& v1, const NumVariant& v2)
{
return !(NumVariant::all_op<bool, equal_op >(v1, NumVariant(0)) && NumVariant::all_op<bool, equal_op >(v2, NumVariant(0)));
}
inline NumVariant operator+(const NumVariant& v1, const NumVariant& v2)
{
return NumVariant::all_op<NumVariant, add_op>(v1, v2);
}
inline NumVariant operator-(const NumVariant& v1, const NumVariant& v2)
{
return NumVariant::all_op<NumVariant, sub_op>(v1, v2);
}
inline NumVariant operator*(const NumVariant& v1, const NumVariant& v2)
{
return NumVariant::all_op<NumVariant, mul_op>(v1, v2);
}
inline NumVariant operator/(const NumVariant& v1, const NumVariant& v2)
{
return NumVariant::all_op<NumVariant, div_op>(v1, v2);
}
inline NumVariant operator%(const NumVariant& v1, const NumVariant& v2)
{
return NumVariant::int_op<NumVariant, mod_op>(v1, v2);
}
inline NumVariant operator^(const NumVariant& v1, const NumVariant& v2)
{
return NumVariant::int_op<NumVariant, xor_op >(v1, v2);
}
inline NumVariant operator& (const NumVariant& v1, const NumVariant& v2)
{
return NumVariant::int_op<NumVariant, and_op >(v1, v2);
}
inline NumVariant operator| (const NumVariant& v1, const NumVariant& v2)
{
return NumVariant::int_op<NumVariant, or_op >(v1, v2);
}
inline NumVariant operator<<(const NumVariant& v1, const NumVariant& v2)
{
NumVariant::NumType safeArgType = v1.m_numType;
return NumVariant::int_op<NumVariant, lshift_op >(v1, v2).cast(safeArgType);
}
inline NumVariant operator>>(const NumVariant& v1, const NumVariant& v2)
{
NumVariant::NumType safeArgType = v1.m_numType;
return NumVariant::int_op<NumVariant, rshift_op >(v1, v2).cast(safeArgType);
}
inline NumVariant operator~(const NumVariant& v1)
{
NumVariant v2(~0ULL);
v2 = v2.cast(v1.m_numType);
return NumVariant::int_op<NumVariant, xor_op >(v1, v2);
}
inline bool operator!(const NumVariant& v1)
{
return NumVariant::all_op<bool, equal_op >(v1, NumVariant(0));
}
inline NumVariant operator++(NumVariant& v1)
{
v1 = v1 + NumVariant(1).cast(v1.m_numType);
return v1;
}
inline NumVariant operator--(NumVariant& v1)
{
v1 = v1 - NumVariant(1).cast(v1.m_numType);
return v1;
}
inline NumVariant operator++(NumVariant& v1, int)
{
auto t = v1;
v1 = v1 + NumVariant(1).cast(v1.m_numType);
return t;
}
inline NumVariant operator--(NumVariant& v1, int)
{
auto t = v1;
v1 = v1 - NumVariant(1).cast(v1.m_numType);
return t;
}
inline NumVariant operator-(const NumVariant& v1)
{
return NumVariant(0).cast(v1.m_numType) - v1;
}
inline
NumVariant::operator char() {
return asChar();
}
inline
NumVariant::operator unsigned char() {
return asUChar();
}
inline
NumVariant::operator short() {
return asShort();
}
inline
NumVariant::operator unsigned short() {
return asUShort();
}
inline
NumVariant::operator unsigned long() {
return asULong();
}
inline
NumVariant::operator long() {
return asLong();
}
inline
NumVariant::operator unsigned long long() {
return asULongLong();
}
inline
NumVariant::operator long long() {
return asLongLong();
}
inline
NumVariant::operator float() {
return asFloat();
}
inline
NumVariant::operator double() {
return asDouble();
}
inline
NumVariant::operator bool() {
return asChar() != 0;
}
/*
class NumBehavior : boost::operators<NumBehavior>, boost::left_shiftable<NumBehavior>, boost::right_shiftable<NumBehavior>
{
......@@ -965,6 +1198,7 @@ inline
NumBehavior::operator void*() {
return reinterpret_cast<void*>(asULongLong());
}
*/
///////////////////////////////////////////////////////////////////////////////
......
......@@ -975,7 +975,7 @@ TypedVarPtr TypedVarUdt::getVirtualMethodRecursive(
name += methodName;
MEMOFFSET_64 thisValue = m_varData->getAddress();
MEMOFFSET_64 methodAddr = vtbl->getElement( methodOffset / vtblType->getPtrSize() )->asULongLong();
MEMOFFSET_64 methodAddr = vtbl->getElement(methodOffset / vtblType->getPtrSize())->getValue().asULongLong();
return TypedVarPtr( new TypedVarMethodBound(methodType, getMemoryAccessor(methodAddr, 0 ), name, thisValue ) );
}
......
......@@ -260,7 +260,7 @@ TypeInfoPtr loadType( const std::wstring &typeName )
throw TypeException(L"type name is empty");
if ( TypeInfo::isBaseType( typeName ) )
return TypeInfo::getBaseTypeInfo( typeName, ptrSize() );
return TypeInfo::getBaseTypeInfo( typeName );
splitSymName( typeName, moduleName, symName );
......@@ -552,7 +552,7 @@ bool TypeInfo::isComplexType( const std::wstring &typeName )
///////////////////////////////////////////////////////////////////////////////
TypeInfoPtr TypeInfo::getBaseTypeInfo( const std::wstring &typeName, size_t ptrSize )
TypeInfoPtr TypeInfo::getBaseTypeInfo( const std::wstring &typeName, size_t ptrSize )
{
if ( isComplexType( typeName ) )
return getComplexTypeInfo( typeName, SymbolPtr() );
......
......@@ -663,7 +663,7 @@ protected:
}
virtual size_t getPtrSize() {
return m_ptrSize;
return m_ptrSize != 0 ? m_ptrSize : kdlib::ptrSize();
}
virtual size_t getAlignReq() {
......
......@@ -160,6 +160,9 @@ TEST_F( TypedVarTest, GetElementName )
TEST_F( TypedVarTest, ArithmOp )
{
EXPECT_EQ( ucharVar + 20, *loadTypedVar(L"ucharVar") + 20);
EXPECT_EQ(ushortVar * ulonglongVar, *loadTypedVar(L"ushortVar") * *loadTypedVar(L"ulonglongVar"));
EXPECT_FLOAT_EQ(floatVar / ulongConst, *loadTypedVar(L"floatVar") / *loadTypedVar(L"ulongConst"));
EXPECT_EQ(longVar % 1234, *loadTypedVar(L"longVar") % *makeLongConst(1234) );
}
TEST_F( TypedVarTest, Enum )
......@@ -676,7 +679,7 @@ TEST_F(TypedVarTest, InvalidAddress)
ASSERT_NO_THROW(var = loadTypedVar(structType, 0x0) );
EXPECT_EQ( structType->getSize(), var->getSize() );
EXPECT_EQ( 0x0, var->asULongLong() );
EXPECT_EQ( 0x0, *var);
EXPECT_NO_THROW( var->str() );
EXPECT_NO_THROW( var->getElement(0) );
}
......@@ -33,7 +33,7 @@ TEST_F(WinApiTest, CreateFileCall)
kdlib::ModulePtr kernel32;
ASSERT_NO_THROW( kernel32 = loadModule(L"kernel32") );
ASSERT_NE( 0, kernel32->asULongLong() );
ASSERT_NE( 0, *kernel32 );
kdlib::MEMOFFSET_64 createFileOffset;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment