File indexing completed on 2024-05-19 04:42:00

0001 /*
0002     SPDX-FileCopyrightText: 2013 Andrea Scarpino <scarpino@kde.org>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #ifndef QMLJSDUCHAINHELPERS_H_
0008 #define QMLJSDUCHAINHELPERS_H_
0009 
0010 #include <language/duchain/declaration.h>
0011 #include <qmljs/parser/qmljsast_p.h>
0012 
0013 #include "duchainexport.h"
0014 
0015 namespace QmlJS
0016 {
0017 using namespace KDevelop;
0018 
0019     /**
0020      * Act like TypeUtils::mergeTypes except that functions take precedence over
0021      * everything else (mergeTypes(a, function) returns function).
0022      */
0023     KDEVQMLJSDUCHAIN_EXPORT AbstractType::Ptr mergeTypes(AbstractType::Ptr type,
0024                                                          const AbstractType::Ptr& newType);
0025 
0026     /**
0027      * QML attribute value, with its source location
0028      */
0029     struct QMLAttributeValue
0030     {
0031         QString value;
0032         AST::SourceLocation location;
0033     };
0034 
0035     /**
0036      * Find the declaration for the specified identifier.
0037      *
0038      * If no declaration was found, it will return a null pointer.
0039      *
0040      * @param id The qualified identifier that identifies our node.
0041      * @param context A pointer to the DUContext of this node.
0042      *
0043      * @note This method already acquires a read lock for the DUChain.
0044      */
0045     KDEVQMLJSDUCHAIN_EXPORT DeclarationPointer getDeclaration(const QualifiedIdentifier& id,
0046                                                               const DUContext* context,
0047                                                               bool searchInParent = true);
0048 
0049     /**
0050      * Find the declaration for the specified identifier, with "onFoo" replaced
0051      * with "Foo".
0052      *
0053      * This function is an extended version of getDeclaration and can resolve
0054      * QML slots to their corresponding signals.
0055      */
0056     KDEVQMLJSDUCHAIN_EXPORT DeclarationPointer getDeclarationOrSignal(const QualifiedIdentifier& id,
0057                                                                       const DUContext* context,
0058                                                                       bool searchInParent = true);
0059 
0060     /**
0061      * Find an attribute of a QML object
0062      */
0063     KDEVQMLJSDUCHAIN_EXPORT AST::Statement* getQMLAttribute(AST::UiObjectMemberList* members,
0064                                                             const QString &attribute);
0065 
0066     /**
0067      * Get the value of a QML string, identifier or boolean expression
0068      */
0069     KDEVQMLJSDUCHAIN_EXPORT QString getNodeValue(AST::Node* node);
0070 
0071     /**
0072      * Find the string value of a QML attribute
0073      */
0074     KDEVQMLJSDUCHAIN_EXPORT QMLAttributeValue getQMLAttributeValue(AST::UiObjectMemberList* members,
0075                                                                    const QString& attribute);
0076 
0077     /**
0078      * Get the internal context of a declaration
0079      */
0080     KDEVQMLJSDUCHAIN_EXPORT DUContext* getInternalContext(const DeclarationPointer &declaration);
0081 
0082     /**
0083      * Get the owner of a context
0084      *
0085      * If the context is of type Function and has no owner, then its parent context
0086      * is used to find the owner.
0087      */
0088     KDEVQMLJSDUCHAIN_EXPORT Declaration* getOwnerOfContext(const DUContext* context);
0089 
0090     /**
0091      * Build a zero-length range on the line of the given SourceLocation
0092      */
0093     KDEVQMLJSDUCHAIN_EXPORT RangeInRevision emptyRangeOnLine(const AST::SourceLocation& location);
0094 
0095     /**
0096      * Import the internal context of a declaration in a context.
0097      *
0098      * @note The DUChain write lock must be held
0099      */
0100     KDEVQMLJSDUCHAIN_EXPORT void importDeclarationInContext(DUContext* context,
0101                                                             const DeclarationPointer& declaration);
0102 
0103     /**
0104      * Import the internal context of "Object" (the Javascript base type) in a context.
0105      *
0106      * @note The DUChain write lock must be held
0107      */
0108     KDEVQMLJSDUCHAIN_EXPORT void importObjectContext(DUContext* context,
0109                                                      TopDUContext* topContext);
0110 
0111     /**
0112      * Return whether an identifier is "prototype" or "__proto__" or any future
0113      * identifier that should be considered to represent a prototype.
0114      */
0115     KDEVQMLJSDUCHAIN_EXPORT bool isPrototypeIdentifier(const QString& identifier);
0116 
0117     /**
0118      * Return whether a context belongs to a QML file (not a JS one)
0119      */
0120     KDEVQMLJSDUCHAIN_EXPORT bool isQmlFile(const DUContext* context);
0121 
0122     KDEVQMLJSDUCHAIN_EXPORT void registerDUChainItems();
0123     KDEVQMLJSDUCHAIN_EXPORT void unregisterDUChainItems();
0124 
0125 
0126 } // End of namespace QmlJS
0127 
0128 #endif /* QMLJSDUCHAINHELPERS_H_ */