File: helper.cpp

package info (click to toggle)
kdevelop 4%3A25.04.0-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 73,508 kB
  • sloc: cpp: 291,803; python: 4,322; javascript: 3,518; sh: 1,316; ansic: 703; xml: 414; php: 95; lisp: 66; makefile: 31; sed: 12
file content (306 lines) | stat: -rw-r--r-- 10,331 bytes parent folder | download | duplicates (2)
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
/*
    SPDX-FileCopyrightText: 2013 Andrea Scarpino <scarpino@kde.org>

    SPDX-License-Identifier: GPL-2.0-or-later
*/

#include "helper.h"
#include "functiondeclaration.h"
#include "functiontype.h"
#include "parsesession.h"
#include "frameworks/nodejs.h"
#include "qmljsducontext.h"

#include <language/duchain/duchain.h>
#include <language/duchain/duchainlock.h>
#include <language/duchain/duchainregister.h>
#include <language/duchain/functiondeclaration.h>
#include <language/duchain/classfunctiondeclaration.h>
#include <language/duchain/namespacealiasdeclaration.h>
#include <language/duchain/types/unsuretype.h>
#include <language/duchain/types/integraltype.h>
#include <language/duchain/types/structuretype.h>
#include <language/duchain/types/functiontype.h>
#include <language/duchain/types/typeutils.h>
#include <language/duchain/types/typeregister.h>

namespace QmlJS
{
using namespace KDevelop;

AbstractType::Ptr mergeTypes(AbstractType::Ptr type, const AbstractType::Ptr& newType)
{
    if (newType && newType->whichType() == AbstractType::TypeFunction) {
        return newType;
    } else {
        return TypeUtils::mergeTypes(std::move(type), newType);
    }
}

DeclarationPointer getDeclaration(const QualifiedIdentifier& id, const DUContext* context, bool searchInParent)
{
    DUChainReadLocker lock;
    if (context) {
        auto declarations = context->findDeclarations(
            id.indexedLast(),
            CursorInRevision(INT_MAX, INT_MAX),
            nullptr,
            searchInParent ? DUContext::NoSearchFlags : DUContext::DontSearchInParent
        );

        if (declarations.count() > 0) {
            return DeclarationPointer(declarations.last());
        }
    }
    return DeclarationPointer();
}

DeclarationPointer getDeclarationOrSignal(const QualifiedIdentifier& id, const DUContext* context, bool searchInParent)
{
    QString identifier = id.last().toString();

    if (identifier.startsWith(QLatin1String("on")) && identifier.size() > 2) {
        // The use may have typed the name of a QML slot (onFoo), try to get
        // the declaration of its corresponding signal (foo)
        identifier = identifier.at(2).toLower() + identifier.midRef(3);
        DeclarationPointer decl = getDeclaration(QualifiedIdentifier(identifier), context, searchInParent);

        if (decl) {
            auto* classFuncDecl = dynamic_cast<ClassFunctionDeclaration *>(decl.data());

            if (classFuncDecl && classFuncDecl->isSignal()) {
                // Removing "on" has given the identifier of a QML signal, return
                // it instead of the name of its slot
                return decl;
            }
        }
    }

    // No signal found, fall back to normal behavior
    return getDeclaration(id, context, searchInParent);
}

QmlJS::AST::Statement* getQMLAttribute(QmlJS::AST::UiObjectMemberList* members, const QString& attribute)
{
    for (QmlJS::AST::UiObjectMemberList *it = members; it; it = it->next) {
        // The member needs to be a script binding whose name matches attribute
        auto* binding = QmlJS::AST::cast<QmlJS::AST::UiScriptBinding*>(it->member);

        if (binding && binding->qualifiedId && binding->qualifiedId->name == attribute) {
            return binding->statement;
        }
    }

    return nullptr;
}

QString getNodeValue(AST::Node* node)
{
    auto identifier = QmlJS::AST::cast<QmlJS::AST::IdentifierExpression*>(node);
    auto identifier_name = QmlJS::AST::cast<QmlJS::AST::IdentifierPropertyName*>(node);
    auto string = QmlJS::AST::cast<QmlJS::AST::StringLiteral*>(node);
    auto string_name = QmlJS::AST::cast<QmlJS::AST::StringLiteralPropertyName*>(node);
    auto true_literal = QmlJS::AST::cast<QmlJS::AST::TrueLiteral*>(node);
    auto false_literal = QmlJS::AST::cast<QmlJS::AST::FalseLiteral*>(node);

    if (identifier) {
        return identifier->name.toString();
    } else if (identifier_name) {
        return identifier_name->id.toString();
    } else if (string) {
        return string->value.toString();
    } else if (string_name) {
        return string_name->id.toString();
    } else if (true_literal) {
        return QStringLiteral("true");
    } else if (false_literal) {
        return QStringLiteral("false");
    } else {
        return QString();
    }
}

QMLAttributeValue getQMLAttributeValue(QmlJS::AST::UiObjectMemberList* members, const QString& attribute)
{
    QMLAttributeValue res;
    QmlJS::AST::Statement* node = getQMLAttribute(members, attribute);

    // The value of the binding must be an expression
    auto* statement = QmlJS::AST::cast<QmlJS::AST::ExpressionStatement*>(node);

    if (!statement) {
        return res;
    }

    // The expression must be an identifier or a string literal
    res.value = getNodeValue(statement->expression);

    if (res.value.isEmpty()) {
        return res;
    }

    res.location = statement->expression->firstSourceLocation();

    return res;
}

DUContext* getInternalContext(const DeclarationPointer& declaration)
{
    DUChainReadLocker lock;

    if (!declaration) {
        return nullptr;
    }

    // The internal context of declarations having a function type is the prototype
    // context of the function (if any), or the internal context of Function
    auto functionType = declaration->abstractType().dynamicCast<QmlJS::FunctionType>();

    if (functionType) {
        Declaration* decl = functionType->declaration(declaration->topContext());
        QmlJS::FunctionDeclaration* funcDecl;

        if (decl && (funcDecl = dynamic_cast<QmlJS::FunctionDeclaration*>(decl)) &&
            funcDecl->prototypeContext()) {
            return funcDecl->prototypeContext();
        }
    }

    // The declaration can either be a class definition (its internal context
    // can be used) or an instance (use the internal context of its type)
    switch (declaration->kind()) {
    case Declaration::Type:
    case Declaration::Namespace:
        return declaration->internalContext();

    case Declaration::NamespaceAlias:
    {
        auto alias = declaration.dynamicCast<NamespaceAliasDeclaration>();

        return getInternalContext(getDeclaration(alias->importIdentifier(), alias->context()));
    }

    default:
    {
        auto structureType = declaration->abstractType().dynamicCast<StructureType>();
        auto integralType = declaration->abstractType().dynamicCast<IntegralType>();

        static const IndexedIdentifier indexedObject(Identifier(QStringLiteral("Object")));
        if (structureType) {
            auto structureDeclaration = structureType->declaration(declaration->topContext());

            if (structureDeclaration != declaration.data()) {
                return getInternalContext(
                    DeclarationPointer(structureDeclaration)
                );
            } else {
                return nullptr;
            }
        } else if ((integralType || functionType) && declaration->indexedIdentifier() != indexedObject) {
            QString baseClass;

            // Compute from which base Javascript class a type inherits
            if (integralType) {
                switch (integralType->dataType()) {
                    case IntegralType::TypeBoolean:
                        baseClass = QStringLiteral("Boolean");
                        break;
                    case IntegralType::TypeString:
                        baseClass = QStringLiteral("String");
                        break;
                    case IntegralType::TypeInt:
                    case IntegralType::TypeHalf:
                    case IntegralType::TypeFloat:
                    case IntegralType::TypeDouble:
                        baseClass = QStringLiteral("Number");
                        break;
                    case IntegralType::TypeArray:
                        baseClass = QStringLiteral("Array");
                        break;
                    default:
                        baseClass = QStringLiteral("Object");
                        break;
                }
            } else if (functionType) {
                baseClass = QStringLiteral("Function");
            }

            return getInternalContext(
                NodeJS::instance().moduleMember(QStringLiteral("__builtin_ecmascript"), baseClass, declaration->topContext()->url())
            );
        } else {
            return nullptr;
        }
    }
    }
}

Declaration* getOwnerOfContext(const DUContext* context)
{
    if (context->owner()) {
        return context->owner();
    } else if (context->type() == DUContext::Function && context->parentContext()) {
        return context->parentContext()->owner();
    } else {
        return nullptr;
    }
}

RangeInRevision emptyRangeOnLine(const AST::SourceLocation& location)
{
    return RangeInRevision(location.startLine - 1, 0, location.startLine - 1, 0);
}

void importDeclarationInContext(DUContext* context, const DeclarationPointer& declaration)
{
    DUContext* importedContext = QmlJS::getInternalContext(declaration);

    if (!importedContext || importedContext == context) {
        return;
    }

    {
        DUChainWriteLocker lock;
        context->addImportedParentContext(importedContext);
    }
}

void importObjectContext(DUContext* context, TopDUContext* topContext)
{
    DeclarationPointer objectDeclaration =
        NodeJS::instance().moduleMember(QStringLiteral("__builtin_ecmascript"), QStringLiteral("Object"), topContext->url());

    if (objectDeclaration) {
        importDeclarationInContext(context, objectDeclaration);
    }
}

bool isPrototypeIdentifier(const QString& identifier)
{
    return (identifier == QLatin1String("prototype") ||
            identifier == QLatin1String("__proto__"));
}

bool isQmlFile(const DUContext* context)
{
    DUChainReadLocker lock;
    return ParseSession::guessLanguageFromSuffix(context->topContext()->url().str()) == Dialect::Qml;
}

void registerDUChainItems()
{
    duchainRegisterType<QmlJSTopDUContext>();
    duchainRegisterType<QmlJSNormalDUContext>();
    duchainRegisterType<FunctionDeclaration>();

    TypeSystem::self().registerTypeClass<FunctionType>();
}

void unregisterDUChainItems()
{
    TypeSystem::self().unregisterTypeClass<FunctionType>();

    // rest not supported, see comment in kdev-clang
}

} // End of namespace QmlJS