File indexing completed on 2024-04-28 15:52:49

0001 /*
0002     SPDX-FileCopyrightText: 2011 Sven Brauch <svenbrauch@googlemail.com>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "indexedcontainer.h"
0008 
0009 #include <language/duchain/types/typeregister.h>
0010 #include <language/duchain/duchain.h>
0011 #include <language/duchain/duchainlock.h>
0012 
0013 #include <KLocalizedString>
0014 
0015 using namespace KDevelop;
0016 
0017 namespace Php {
0018 
0019 DEFINE_LIST_MEMBER_HASH(IndexedContainerData, m_values, IndexedType)
0020 REGISTER_TYPE(IndexedContainer);
0021 
0022 IndexedContainer::IndexedContainer() : Php::StructureType(createData<IndexedContainer>())
0023 {
0024 
0025 }
0026 
0027 IndexedContainer::IndexedContainer(const IndexedContainer& rhs)
0028     : StructureType(copyData<IndexedContainer>(*rhs.d_func()))
0029 {
0030 
0031 }
0032 
0033 IndexedContainer::IndexedContainer(IndexedContainerData& data)
0034     : StructureType(data)
0035 {
0036 
0037 }
0038 
0039 void IndexedContainer::addEntry(AbstractType::Ptr typeToAdd)
0040 {
0041     Q_ASSERT(typeToAdd && "trying to add a null type to indexedContainer");
0042     d_func_dynamic()->m_valuesList().append(typeToAdd->indexed());
0043 }
0044 
0045 const IndexedType& IndexedContainer::typeAt(int index) const
0046 {
0047     Q_ASSERT((uint) index < d_func()->m_valuesSize());
0048     return d_func()->m_values()[index];
0049 }
0050 
0051 void IndexedContainer::replaceType(int index, AbstractType::Ptr newType)
0052 {
0053     Q_ASSERT((uint) index < d_func()->m_valuesSize());
0054     d_func_dynamic()->m_valuesList()[index] = newType->indexed();
0055 }
0056 
0057 KDevelop::AbstractType* IndexedContainer::clone() const
0058 {
0059     auto* n = new IndexedContainer(*this);
0060     return n;
0061 }
0062 
0063 QString IndexedContainer::toString() const
0064 {
0065     QString prefix = Php::StructureType::toString();
0066     QStringList typesArray;
0067     for ( int i = 0; i < typesCount(); i++ ) {
0068         if ( i >= 5 ) {
0069             // Don't print more than five types explicitly
0070             typesArray << "...";
0071             break;
0072         }
0073         typesArray << typeAt(i).abstractType()->toString();
0074     }
0075     const QString contentType = QStringLiteral("(") + typesArray.join(", ") + ")";
0076     return i18nc("as in list of int, set of string", "%1 of %2", prefix, contentType);
0077 }
0078 
0079 QString IndexedContainer::containerToString() const
0080 {
0081     return Php::StructureType::toString();
0082 }
0083 
0084 int IndexedContainer::typesCount() const
0085 {
0086     return d_func()->m_valuesSize();
0087 }
0088 
0089 bool IndexedContainer::equals(const AbstractType* rhs) const
0090 {
0091     if ( this == rhs ) {
0092         return true;
0093     }
0094     if ( ! Php::StructureType::equals(rhs) ) {
0095         return false;
0096     }
0097     const IndexedContainer* c = dynamic_cast<const IndexedContainer*>(rhs);
0098     if ( ! c ) {
0099         return false;
0100     }
0101     if ( typesCount() != c->typesCount() ) {
0102         return false;
0103     }
0104     for ( int i = 0; i < typesCount(); i++ ) {
0105         if ( c->typeAt(i) != typeAt(i) ) {
0106             return false;
0107         }
0108     }
0109     return true;
0110 }
0111 
0112 uint IndexedContainer::hash() const
0113 {
0114     uint h = StructureType::hash();
0115     for ( uint i = 0; i < d_func()->m_valuesSize(); i++ ) {
0116         h += i*d_func()->m_values()[i];
0117     }
0118     return h;
0119 }
0120 
0121 }