language/duchain
constantintegraltype.cpp
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "constantintegraltype.h"
00023
00024 #include "typesystemdata.h"
00025 #include "typeregister.h"
00026
00027 namespace KDevelop {
00028
00029 REGISTER_TYPE(ConstantIntegralType);
00030
00031 ConstantIntegralType::ConstantIntegralType(const ConstantIntegralType& rhs)
00032 : IntegralType(copyData<ConstantIntegralType>(*rhs.d_func()))
00033 {
00034 }
00035
00036 ConstantIntegralType::ConstantIntegralType(ConstantIntegralTypeData& data)
00037 : IntegralType(data)
00038 {
00039 }
00040
00041 ConstantIntegralType::ConstantIntegralType(uint type)
00042 : IntegralType(createData<ConstantIntegralType>())
00043 {
00044 setDataType(type);
00045 setModifiers(ConstModifier);
00046 }
00047
00048 qint64 ConstantIntegralType::plainValue() const
00049 {
00050 return d_func()->m_value;
00051 }
00052
00053 AbstractType* ConstantIntegralType::clone() const
00054 {
00055 return new ConstantIntegralType(*this);
00056 }
00057
00058 bool ConstantIntegralType::equals(const AbstractType* _rhs) const
00059 {
00060 if( this == _rhs )
00061 return true;
00062
00063 if (!IntegralType::equals(_rhs))
00064 return false;
00065
00066 Q_ASSERT(fastCast<const ConstantIntegralType*>(_rhs));
00067
00068 const ConstantIntegralType* rhs = static_cast<const ConstantIntegralType*>(_rhs);
00069
00070 return d_func()->m_value == rhs->d_func()->m_value;
00071 }
00072
00073 QString ConstantIntegralType::toString() const
00074 {
00075 QString ret;
00076
00077 switch(dataType()) {
00078 case TypeNone:
00079 ret += "none";
00080 break;
00081 case TypeChar:
00082 ret += QString("%1").arg((char)d_func()->m_value);
00083 break;
00084 case TypeWchar_t:
00085 ret += QString("%1").arg((wchar_t)d_func()->m_value);
00086 break;
00087 case TypeBoolean:
00088 ret += d_func()->m_value ? "true" : "false";
00089 break;
00090 case TypeInt:
00091 ret += (modifiers() & UnsignedModifier) ? QString("%1u").arg((uint)d_func()->m_value) : QString("%1").arg((int)d_func()->m_value);
00092 break;
00093 case TypeFloat:
00094 ret += QString("%1").arg( value<float>() );
00095 break;
00096 case TypeDouble:
00097 ret += QString("%1").arg( value<double>() );
00098 break;
00099 case TypeVoid:
00100 ret += "void";
00101 break;
00102 default:
00103 ret += "<unknown_value>";
00104 break;
00105 }
00106
00107 return ret;
00108 }
00109
00110 uint ConstantIntegralType::hash() const
00111 {
00112 uint ret = IntegralType::hash();
00113 ret += 47 * (uint)d_func()->m_value;
00114 return ret;
00115 }
00116
00117 template<>
00118 KDEVPLATFORMLANGUAGE_EXPORT
00119 void ConstantIntegralType::setValueInternal<qint64>(qint64 value) {
00120 if((modifiers() & UnsignedModifier)) {
00121 kDebug() << "setValue(signed) called on unsigned type";
00122 }
00123 d_func_dynamic()->m_value = value;
00124 }
00125
00126 template<>
00127 KDEVPLATFORMLANGUAGE_EXPORT
00128 void ConstantIntegralType::setValueInternal<quint64>(quint64 value) {
00129 if(!(modifiers() & UnsignedModifier)) {
00130 kDebug() << "setValue(unsigned) called on not unsigned type";
00131 }
00132 d_func_dynamic()->m_value = (qint64)value;
00133 }
00134
00135 template<>
00136 KDEVPLATFORMLANGUAGE_EXPORT
00137 void ConstantIntegralType::setValueInternal<float>(float value) {
00138 if(dataType() != TypeFloat) {
00139 kDebug() << "setValue(float) called on non-float type";
00140 }
00141 memcpy(&d_func_dynamic()->m_value, &value, sizeof(float));
00142 }
00143
00144 template<>
00145 KDEVPLATFORMLANGUAGE_EXPORT
00146 void ConstantIntegralType::setValueInternal<double>(double value) {
00147 if(dataType() != TypeDouble) {
00148 kDebug() << "setValue(double) called on non-double type";
00149 }
00150 memcpy(&d_func_dynamic()->m_value, &value, sizeof(double));
00151 }
00152
00153 }