File indexing completed on 2024-05-05 16:41:08

0001 /*
0002     SPDX-FileCopyrightText: 2008 Milian Wolff <mail@milianw.de>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "classmethoddeclaration.h"
0008 
0009 #include <language/duchain/duchainregister.h>
0010 #include <language/duchain/types/functiontype.h>
0011 
0012 #include <duchaindebug.h>
0013 
0014 using namespace KDevelop;
0015 
0016 namespace Php
0017 {
0018 REGISTER_DUCHAIN_ITEM(ClassMethodDeclaration);
0019 
0020 ClassMethodDeclaration::ClassMethodDeclaration(const ClassMethodDeclaration& rhs)
0021         : KDevelop::ClassFunctionDeclaration(*new ClassMethodDeclarationData(*rhs.d_func()))
0022 {
0023 }
0024 
0025 ClassMethodDeclaration::ClassMethodDeclaration(const KDevelop::RangeInRevision& range, KDevelop::DUContext* context)
0026         : KDevelop::ClassFunctionDeclaration(*new ClassMethodDeclarationData, range, context)
0027 {
0028     d_func_dynamic()->setClassId(this);
0029     if (context)
0030         setContext(context);
0031 }
0032 
0033 ClassMethodDeclaration::ClassMethodDeclaration(ClassMethodDeclarationData& data)
0034         : KDevelop::ClassFunctionDeclaration(data)
0035 {
0036 }
0037 
0038 ClassMethodDeclaration::ClassMethodDeclaration(ClassMethodDeclarationData& data, const KDevelop::RangeInRevision& range, KDevelop::DUContext* context)
0039         : KDevelop::ClassFunctionDeclaration(data, range, context)
0040 {
0041 }
0042 
0043 ClassMethodDeclaration::~ClassMethodDeclaration()
0044 {
0045 }
0046 
0047 bool ClassMethodDeclaration::isConstructor() const
0048 {
0049     static const auto constructId = IndexedIdentifier(Identifier(QStringLiteral("__construct")));
0050     const auto indexed = indexedIdentifier();
0051     return indexed == constructId
0052            || indexed == context()->indexedLocalScopeIdentifier().identifier().indexedFirst();
0053 }
0054 
0055 bool ClassMethodDeclaration::isDestructor() const
0056 {
0057     //TODO: register_shutdown_function
0058     static const auto destructId = IndexedIdentifier(Identifier(QStringLiteral("__destruct")));
0059     const auto indexed = indexedIdentifier();
0060     return indexed == destructId;
0061 }
0062 
0063 Declaration* ClassMethodDeclaration::clonePrivate() const
0064 {
0065     return new ClassMethodDeclaration(*this);
0066 }
0067 
0068 KDevelop::IndexedString ClassMethodDeclaration::prettyName() const
0069 {
0070     return d_func()->prettyName;
0071 }
0072 
0073 void ClassMethodDeclaration::setPrettyName( const KDevelop::IndexedString& name )
0074 {
0075     d_func_dynamic()->prettyName = name;
0076 }
0077 
0078 QString ClassMethodDeclaration::toString() const
0079 {
0080     if( !abstractType() )
0081         return ClassMemberDeclaration::toString();
0082 
0083     TypePtr<FunctionType> function = type<FunctionType>();
0084     if(function) {
0085         return QStringLiteral("%1 %2 %3").arg(function->partToString( FunctionType::SignatureReturn ),
0086                                        prettyName().str(),
0087                                        function->partToString( FunctionType::SignatureArguments ));
0088     } else {
0089         QString type = abstractType() ? abstractType()->toString() : QStringLiteral("<notype>");
0090         qCDebug(DUCHAIN) << "A function has a bad type attached:" << type;
0091         return QStringLiteral("invalid member-function %1 type %2").arg(prettyName().str(),type);
0092     }
0093 }
0094 
0095 
0096 }