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

0001 /*
0002     SPDX-FileCopyrightText: 2014 Denis Steckelmacher <steckdenis@yahoo.fr>
0003 
0004     SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0005 */
0006 
0007 #ifndef __FUNCTIONDECLARATION_H__
0008 #define __FUNCTIONDECLARATION_H__
0009 
0010 #include "duchainexport.h"
0011 
0012 #include <language/duchain/functiondeclaration.h>
0013 #include <language/duchain/indexedducontext.h>
0014 #include <language/duchain/duchainregister.h>
0015 
0016 namespace QmlJS {
0017 
0018 class KDEVQMLJSDUCHAIN_EXPORT FunctionDeclarationData : public KDevelop::FunctionDeclarationData
0019 {
0020 public:
0021     KDevelop::IndexedDUContext m_prototypeContext;
0022 };
0023 
0024 /**
0025  * @brief Function declaration keeping track of a "prototype" context
0026  *
0027  * The prototype of a function can be used, in Javascript, to add methods and
0028  * members to the objects instantiated by calling the function.
0029  *
0030  * The prototype is also used to resolve "this". If a function is assigned to
0031  * an object member, its "prototype" becomes the internal context of the object.
0032  * This way, functions assigned to members of the prototype of a class can use
0033  * "this" to refer to the object on which they are called.
0034  *
0035  * @code
0036  * function Class() { this.name = "Me"; }
0037  *
0038  * Class.prototype.print = function() { console.log(this.name) }
0039  * @endcode
0040  */
0041 class KDEVQMLJSDUCHAIN_EXPORT FunctionDeclaration : public KDevelop::FunctionDeclaration
0042 {
0043 public:
0044     FunctionDeclaration(const FunctionDeclaration &rhs);
0045     FunctionDeclaration(const KDevelop::RangeInRevision &range, KDevelop::DUContext *context);
0046     explicit FunctionDeclaration(FunctionDeclarationData &data);
0047     ~FunctionDeclaration() override;
0048 
0049     /**
0050      * @brief Return the context representing the prototype of this function
0051      *
0052      * The returned context, if not null, contains the declarations of the members
0053      * of the prototype.
0054      *
0055      * @note The DUChain must be read-locked
0056      */
0057     KDevelop::DUContext* prototypeContext() const;
0058 
0059     /**
0060      * @brief Set the prototype context of this function
0061      *
0062      * @note The DUChain must be write-locked
0063      */
0064     void setPrototypeContext(KDevelop::DUContext* context);
0065 
0066     enum {
0067         Identity = 112
0068     };
0069 
0070     using Ptr = KDevelop::DUChainPointer<FunctionDeclaration>;
0071 
0072 private:
0073     DUCHAIN_DECLARE_DATA(FunctionDeclaration)
0074 };
0075 
0076 }
0077 
0078 DUCHAIN_DECLARE_TYPE(QmlJS::FunctionDeclaration)
0079 
0080 #endif