1 /*
2     SPDX-FileCopyrightText: 2008 Milian Wolff <mail@milianw.de>
3 
4     SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6 
7 #include "classmethoddeclaration.h"
8 
9 #include <language/duchain/duchainregister.h>
10 #include <language/duchain/types/functiontype.h>
11 
12 #include <duchaindebug.h>
13 
14 using namespace KDevelop;
15 
16 namespace Php
17 {
18 REGISTER_DUCHAIN_ITEM(ClassMethodDeclaration);
19 
ClassMethodDeclaration(const ClassMethodDeclaration & rhs)20 ClassMethodDeclaration::ClassMethodDeclaration(const ClassMethodDeclaration& rhs)
21         : KDevelop::ClassFunctionDeclaration(*new ClassMethodDeclarationData(*rhs.d_func()))
22 {
23 }
24 
ClassMethodDeclaration(const KDevelop::RangeInRevision & range,KDevelop::DUContext * context)25 ClassMethodDeclaration::ClassMethodDeclaration(const KDevelop::RangeInRevision& range, KDevelop::DUContext* context)
26         : KDevelop::ClassFunctionDeclaration(*new ClassMethodDeclarationData, range, context)
27 {
28     d_func_dynamic()->setClassId(this);
29     if (context)
30         setContext(context);
31 }
32 
ClassMethodDeclaration(ClassMethodDeclarationData & data)33 ClassMethodDeclaration::ClassMethodDeclaration(ClassMethodDeclarationData& data)
34         : KDevelop::ClassFunctionDeclaration(data)
35 {
36 }
37 
ClassMethodDeclaration(ClassMethodDeclarationData & data,const KDevelop::RangeInRevision & range,KDevelop::DUContext * context)38 ClassMethodDeclaration::ClassMethodDeclaration(ClassMethodDeclarationData& data, const KDevelop::RangeInRevision& range, KDevelop::DUContext* context)
39         : KDevelop::ClassFunctionDeclaration(data, range, context)
40 {
41 }
42 
~ClassMethodDeclaration()43 ClassMethodDeclaration::~ClassMethodDeclaration()
44 {
45 }
46 
isConstructor() const47 bool ClassMethodDeclaration::isConstructor() const
48 {
49     static const auto constructId = IndexedIdentifier(Identifier(QStringLiteral("__construct")));
50     const auto indexed = indexedIdentifier();
51     return indexed == constructId
52            || indexed == context()->indexedLocalScopeIdentifier().identifier().indexedFirst();
53 }
54 
isDestructor() const55 bool ClassMethodDeclaration::isDestructor() const
56 {
57     //TODO: register_shutdown_function
58     static const auto destructId = IndexedIdentifier(Identifier(QStringLiteral("__destruct")));
59     const auto indexed = indexedIdentifier();
60     return indexed == destructId;
61 }
62 
clonePrivate() const63 Declaration* ClassMethodDeclaration::clonePrivate() const
64 {
65     return new ClassMethodDeclaration(*this);
66 }
67 
prettyName() const68 KDevelop::IndexedString ClassMethodDeclaration::prettyName() const
69 {
70     return d_func()->prettyName;
71 }
72 
setPrettyName(const KDevelop::IndexedString & name)73 void ClassMethodDeclaration::setPrettyName( const KDevelop::IndexedString& name )
74 {
75     d_func_dynamic()->prettyName = name;
76 }
77 
toString() const78 QString ClassMethodDeclaration::toString() const
79 {
80     if( !abstractType() )
81         return ClassMemberDeclaration::toString();
82 
83     TypePtr<FunctionType> function = type<FunctionType>();
84     if(function) {
85         return QStringLiteral("%1 %2 %3").arg(function->partToString( FunctionType::SignatureReturn ),
86                                        prettyName().str(),
87                                        function->partToString( FunctionType::SignatureArguments ));
88     } else {
89         QString type = abstractType() ? abstractType()->toString() : QStringLiteral("<notype>");
90         qCDebug(DUCHAIN) << "A function has a bad type attached:" << type;
91         return QStringLiteral("invalid member-function %1 type %2").arg(prettyName().str(),type);
92     }
93 }
94 
95 
96 }
97