File: classfunctiondeclaration.cpp

package info (click to toggle)
kdevelop 4%3A5.6.2-4
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 57,892 kB
  • sloc: cpp: 278,773; javascript: 3,558; python: 3,385; sh: 1,317; ansic: 689; xml: 273; php: 95; makefile: 40; lisp: 13; sed: 12
file content (235 lines) | stat: -rw-r--r-- 7,678 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/* This  is part of KDevelop
    Copyright 2002-2005 Roberto Raggi <roberto@kdevelop.org>
    Copyright 2006 Adam Treat <treat@kde.org>
    Copyright 2006 Hamish Rodda <rodda@kde.org>
    Copyright 2007-2008 David Nolden <david.nolden.kdevelop@art-master.de>

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License version 2 as published by the Free Software Foundation.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
   Boston, MA 02110-1301, USA.
 */

#include "classfunctiondeclaration.h"
#include "ducontext.h"
#include "types/functiontype.h"
#include "duchainregister.h"
#include <debug.h>

namespace KDevelop {
static Identifier& conversionIdentifier()
{
    static Identifier conversionIdentifierObject(QStringLiteral("operator{...cast...}"));
    return conversionIdentifierObject;
}

REGISTER_DUCHAIN_ITEM(ClassFunctionDeclaration);

ClassFunctionDeclaration::ClassFunctionDeclaration(const ClassFunctionDeclaration& rhs)
    : ClassFunctionDeclarationBase(*new ClassFunctionDeclarationData(*rhs.d_func()))
{
}

void ClassFunctionDeclaration::setAbstractType(AbstractType::Ptr type)
{
    ///TODO: write testcase for typealias case which used to trigger this warning:
    ///      typedef bool (*EventFilter)(void *message, long *result);
    ///      in e.g. qcoreapplication.h:172
    if (type && !dynamic_cast<FunctionType*>(type.data()) && type->whichType() != AbstractType::TypeAlias) {
        qCWarning(LANGUAGE) << "WARNING: Non-function type assigned to function declaration. Type is: "
                            << type->toString() << "whichType:" << type->whichType()
                            << "Declaration is:" << toString()
                            << topContext()->url().str() << range().castToSimpleRange();
    }
    ClassMemberDeclaration::setAbstractType(type);
}

DEFINE_LIST_MEMBER_HASH(ClassFunctionDeclarationData, m_defaultParameters, IndexedString)

ClassFunctionDeclaration::ClassFunctionDeclaration(ClassFunctionDeclarationData& data) : ClassFunctionDeclarationBase(
        data)
{
}

ClassFunctionDeclaration::ClassFunctionDeclaration(const RangeInRevision& range, DUContext* context)
    : ClassFunctionDeclarationBase(*new ClassFunctionDeclarationData, range)
{
    d_func_dynamic()->setClassId(this);
    if (context)
        setContext(context);
}

ClassFunctionDeclaration::ClassFunctionDeclaration(ClassFunctionDeclarationData& data, const RangeInRevision& range,
                                                   DUContext* context)
    : ClassFunctionDeclarationBase(data, range)
{
    if (context)
        setContext(context);
}

Declaration* ClassFunctionDeclaration::clonePrivate() const
{
    return new ClassFunctionDeclaration(*this);
}

ClassFunctionDeclaration::~ClassFunctionDeclaration()
{
}

bool ClassFunctionDeclaration::isFunctionDeclaration() const
{
    return true;
}

QString ClassFunctionDeclaration::toString() const
{
    if (!abstractType())
        return ClassMemberDeclaration::toString();

    TypePtr<FunctionType> function = type<FunctionType>();
    if (function) {
        return QStringLiteral("%1 %2 %3").arg(function->partToString(FunctionType::SignatureReturn),
                                              identifier().toString(),
                                              function->partToString(FunctionType::SignatureArguments));
    } else {
        QString type = abstractType() ? abstractType()->toString() : QStringLiteral("<notype>");
        qCDebug(LANGUAGE) << "A function has a bad type attached:" << type;
        return i18n("invalid member-function %1 type %2", identifier().toString(), type);
    }
}

/*bool ClassFunctionDeclaration::isSimilar(KDevelop::CodeItem *other, bool strict ) const
   {
   if (!CppClassMemberType::isSimilar(other,strict))
    return false;

   FunctionModelItem func = dynamic_cast<ClassFunctionDeclaration*>(other);

   if (isConstant() != func->isConstant())
    return false;

   if (arguments().count() != func->arguments().count())
    return false;

   for (int i=0; i<arguments().count(); ++i)
    {
      ArgumentModelItem arg1 = arguments().at(i);
      ArgumentModelItem arg2 = arguments().at(i);

      if (arg1->type() != arg2->type())
        return false;
    }

   return true;
   }*/

uint setFlag(bool enable, uint flag, uint flags)
{
    if (enable)
        return flags | flag;
    else
        return flags & (~flag);
}

bool ClassFunctionDeclaration::isAbstract() const
{
    return d_func()->m_functionFlags & AbstractFunctionFlag;
}

void ClassFunctionDeclaration::setIsAbstract(bool abstract)
{
    d_func_dynamic()->m_functionFlags = ( ClassFunctionFlags )setFlag(abstract, AbstractFunctionFlag,
                                                                      d_func()->m_functionFlags);
}

bool ClassFunctionDeclaration::isFinal() const
{
    return d_func()->m_functionFlags & FinalFunctionFlag;
}

void ClassFunctionDeclaration::setIsFinal(bool final)
{
    d_func_dynamic()->m_functionFlags = ( ClassFunctionFlags )setFlag(final, FinalFunctionFlag,
                                                                      d_func()->m_functionFlags);
}

bool ClassFunctionDeclaration::isSignal() const
{
    return d_func()->m_functionFlags & FunctionSignalFlag;
}

void ClassFunctionDeclaration::setIsSignal(bool isSignal)
{
    d_func_dynamic()->m_functionFlags = ( ClassFunctionFlags )setFlag(isSignal, FunctionSignalFlag,
                                                                      d_func()->m_functionFlags);
}

bool ClassFunctionDeclaration::isSlot() const
{
    return d_func()->m_functionFlags & FunctionSlotFlag;
}

void ClassFunctionDeclaration::setIsSlot(bool isSlot)
{
    d_func_dynamic()->m_functionFlags = ( ClassFunctionFlags )setFlag(isSlot, FunctionSlotFlag,
                                                                      d_func()->m_functionFlags);
}

bool ClassFunctionDeclaration::isConversionFunction() const
{
    return identifier() == conversionIdentifier();
}

bool ClassFunctionDeclaration::isConstructor() const
{
    DUContext* ctx = context();
    if (ctx && ctx->type() == DUContext::Class && ctx->localScopeIdentifier().top().nameEquals(identifier()))
        return true;
    return false;
}

bool ClassFunctionDeclaration::isDestructor() const
{
    DUContext* ctx = context();
    QString id = identifier().toString();
    return ctx && ctx->type() == DUContext::Class && id.startsWith(QLatin1Char('~')) &&
           id.midRef(1) == ctx->localScopeIdentifier().top().toString();
}

uint ClassFunctionDeclaration::additionalIdentity() const
{
    if (abstractType())
        return abstractType()->hash();
    else
        return 0;
}

const IndexedString* ClassFunctionDeclaration::defaultParameters() const
{
    return d_func()->m_defaultParameters();
}

unsigned int ClassFunctionDeclaration::defaultParametersSize() const
{
    return d_func()->m_defaultParametersSize();
}

void ClassFunctionDeclaration::addDefaultParameter(const IndexedString& str)
{
    d_func_dynamic()->m_defaultParametersList().append(str);
}

void ClassFunctionDeclaration::clearDefaultParameters()
{
    d_func_dynamic()->m_defaultParametersList().clear();
}
}