File indexing completed on 2024-04-28 04:35:54

0001 /*
0002  * This file is part of KDevelop
0003  * Copyright (C) 2012-2015 Miquel Sabaté Solà <mikisabate@gmail.com>
0004  *
0005  * This program is free software: you can redistribute it and/or modify
0006  * it under the terms of the GNU General Public License as published by
0007  * the Free Software Foundation, either version 3 of the License, or
0008  * (at your option) any later version.
0009  *
0010  * This program is distributed in the hope that it will be useful,
0011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0013  * GNU General Public License for more details.
0014  *
0015  * You should have received a copy of the GNU General Public License
0016  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
0017  */
0018 
0019 #include <klocalizedstring.h>
0020 
0021 #include <duchain/helpers.h>
0022 #include <duchain/types/classtype.h>
0023 #include <language/duchain/types/typeregister.h>
0024 
0025 using namespace KDevelop;
0026 using namespace ruby;
0027 
0028 REGISTER_TYPE(ClassType);
0029 
0030 ClassType::ClassType() : KDevelop::StructureType(createData<ClassType>())
0031 {
0032 }
0033 
0034 ClassType::ClassType(const ClassType &rhs)
0035     : KDevelop::StructureType(copyData<ClassType>(*rhs.d_func()))
0036 {
0037 }
0038 
0039 ClassType::ClassType(ClassTypeData &data)
0040     : KDevelop::StructureType(data)
0041 {
0042 }
0043 
0044 void ClassType::addContentType(AbstractType::Ptr typeToAdd)
0045 {
0046     if (!typeToAdd) { // TODO: not sure :/
0047         return;
0048     }
0049     auto type = mergeTypes(contentType().abstractType(), typeToAdd);
0050     d_func_dynamic()->contentType = IndexedType(type);
0051 }
0052 
0053 const IndexedType & ClassType::contentType() const
0054 {
0055     return d_func()->contentType;
0056 }
0057 
0058 bool ClassType::isUseful() const
0059 {
0060     // TODO: this is utter crap.
0061     return KDevelop::StructureType::toString() != QStringLiteral("Object");
0062 }
0063 
0064 AbstractType * ClassType::clone() const
0065 {
0066     return new ClassType(*this);
0067 }
0068 
0069 uint ClassType::hash() const
0070 {
0071     return KDevHash(StructureType::hash()) <<
0072         (contentType().abstractType() ? contentType().abstractType()->hash() : 0);
0073 }
0074 
0075 bool ClassType::equals(const AbstractType *rhs) const
0076 {
0077     if (!KDevelop::StructureType::equals(rhs)) {
0078         return false;
0079     }
0080 
0081     if (const ClassType *rhsClass = dynamic_cast<const ClassType *>(rhs)) {
0082         return rhsClass->contentType() == contentType();
0083     }
0084     return false;
0085 }
0086 
0087 QString ClassType::toString() const
0088 {
0089     QString prefix = StructureType::toString();
0090     AbstractType::Ptr content = contentType().abstractType();
0091     return content ? i18n("%1 of %2", prefix, content->toString()) : prefix;
0092 }
0093 
0094 QString ClassType::containerToString() const
0095 {
0096     return KDevelop::StructureType::toString();
0097 }
0098