File indexing completed on 2024-06-16 04:23:16

0001 /*
0002     SPDX-FileCopyrightText: 2011-2014 Sven Brauch <svenbrauch@googlemail.com>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "containertypes.h"
0008 
0009 #include "typeutils.h"
0010 #include "typeregister.h"
0011 
0012 #include "../duchain.h"
0013 #include "../duchainlock.h"
0014 
0015 #include <KLocalizedString>
0016 
0017 namespace KDevelop {
0018 REGISTER_TYPE(ListType);
0019 REGISTER_TYPE(MapType);
0020 
0021 ListType::ListType()
0022     : KDevelop::StructureType(createData<ListType>()) { }
0023 
0024 ListType::ListType(StructureTypeData& data)
0025     : KDevelop::StructureType(data) { }
0026 
0027 ListType::ListType(const ListType& rhs)
0028     : KDevelop::StructureType(copyData<ListType>(*rhs.d_func())) { }
0029 
0030 MapType::MapType()
0031     : ListType(createData<MapType>()) { }
0032 
0033 MapType::MapType(ListTypeData& data)
0034     : ListType(data) { }
0035 
0036 MapType::MapType(const MapType& rhs)
0037     : ListType(copyData<MapType>(*rhs.d_func())) { }
0038 
0039 void ListType::replaceContentType(const AbstractType::Ptr& newType)
0040 {
0041     d_func_dynamic()->m_contentType = IndexedType(newType);
0042 }
0043 
0044 void MapType::replaceKeyType(const AbstractType::Ptr& newType)
0045 {
0046     d_func_dynamic()->m_keyType = IndexedType(newType);
0047 }
0048 
0049 IndexedType ListType::contentType() const
0050 {
0051     return d_func()->m_contentType;
0052 }
0053 
0054 IndexedType MapType::keyType() const
0055 {
0056     return d_func()->m_keyType;
0057 }
0058 
0059 AbstractType* ListType::clone() const
0060 {
0061     return new ListType(*this);
0062 }
0063 
0064 AbstractType* MapType::clone() const
0065 {
0066     return new MapType(*this);
0067 }
0068 
0069 QString ListType::toString() const
0070 {
0071     QString prefix = KDevelop::StructureType::toString();
0072     auto content = contentType().abstractType();
0073     if (content) {
0074         return i18n("%1 of %2", prefix, content->toString());
0075     }
0076     return prefix;
0077 }
0078 
0079 QString MapType::toString() const
0080 {
0081     QString prefix = KDevelop::StructureType::toString();
0082     auto content = contentType().abstractType();
0083     auto key = keyType().abstractType();
0084     auto key_str = (key ? key->toString() : i18n("unknown"));
0085     auto content_str = (content ? content->toString() : i18n("unknown"));
0086     if (key || content) {
0087         return i18n("%1 of %2 : %3", prefix, key_str, content_str);
0088     }
0089     return prefix;
0090 }
0091 
0092 QString ListType::containerToString() const
0093 {
0094     return KDevelop::StructureType::toString();
0095 }
0096 
0097 bool ListType::equals(const AbstractType* rhs) const
0098 {
0099     if (this == rhs) {
0100         return true;
0101     }
0102     if (!KDevelop::StructureType::equals(rhs)) {
0103         return false;
0104     }
0105     auto c = dynamic_cast<const ListType*>(rhs);
0106     if (!c) {
0107         return false;
0108     }
0109     if (c->contentType() != d_func()->m_contentType) {
0110         return false;
0111     }
0112     return true;
0113 }
0114 
0115 bool MapType::equals(const AbstractType* rhs) const
0116 {
0117     if (!ListType::equals(rhs)) {
0118         return false;
0119     }
0120     auto c = dynamic_cast<const MapType*>(rhs);
0121 
0122     return c && c->keyType() == d_func()->m_keyType;
0123 }
0124 
0125 uint ListType::hash() const
0126 {
0127     return StructureType::hash() + (contentType().abstractType() ? contentType().abstractType()->hash() : 1);
0128 }
0129 
0130 uint MapType::hash() const
0131 {
0132     return ListType::hash() + (keyType().abstractType() ? keyType().abstractType()->hash() : 1);
0133 }
0134 } // namespace KDevelop