File indexing completed on 2024-04-28 04:35:52

0001 /* This file is part of KDevelop
0002  *
0003  * Copyright (C) 2011-2015 Miquel Sabaté Solà <mikisabate@gmail.com>
0004  *
0005  * This program is free software: you can redistribute it and/or modify
0006  * it under the terms of the GNU General Public License as published by
0007  * the Free Software Foundation, either version 3 of the License, or
0008  * (at your option) any later version.
0009  *
0010  * This program is distributed in the hope that it will be useful,
0011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0013  * GNU General Public License for more details.
0014  *
0015  * You should have received a copy of the GNU General Public License
0016  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
0017  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
0018  */
0019 
0020 #ifndef RUBY_METHOD_DECLARATION_H
0021 #define RUBY_METHOD_DECLARATION_H
0022 
0023 #include <duchain/duchainexport.h>
0024 #include <language/duchain/appendedlist.h>
0025 #include <language/duchain/functiondeclaration.h>
0026 
0027 namespace ruby {
0028 
0029 /// Struct used in the appended list.
0030 struct KDEVRUBYDUCHAIN_EXPORT YieldType {
0031     KDevelop::IndexedType type;
0032 };
0033 
0034 KDEVRUBYDUCHAIN_EXPORT DECLARE_LIST_MEMBER_HASH(MethodDeclarationData, yieldTypes, YieldType)
0035 
0036 class KDEVRUBYDUCHAIN_EXPORT MethodDeclarationData
0037     : public KDevelop::FunctionDeclarationData
0038 {
0039 public:
0040     MethodDeclarationData()
0041         : KDevelop::FunctionDeclarationData()
0042         , classMethod(false)
0043         , accessPolicy(KDevelop::Declaration::Public)
0044     {
0045         initializeAppendedLists();
0046     }
0047 
0048     explicit MethodDeclarationData(const MethodDeclarationData &rhs)
0049         : KDevelop::FunctionDeclarationData(rhs)
0050     {
0051         initializeAppendedLists();
0052         copyListsFrom(rhs);
0053         classMethod = rhs.classMethod;
0054         accessPolicy = rhs.accessPolicy;
0055     }
0056 
0057     ~MethodDeclarationData()
0058     {
0059         freeAppendedLists();
0060     }
0061 
0062     /// True if this is a Class method
0063     bool classMethod;
0064 
0065     /// The access policy for this method.
0066     KDevelop::Declaration::AccessPolicy accessPolicy;
0067 
0068     /// The list of yield types.
0069     START_APPENDED_LISTS_BASE(MethodDeclarationData, KDevelop::DeclarationData);
0070     APPENDED_LIST_FIRST(MethodDeclarationData, YieldType, yieldTypes);
0071     END_APPENDED_LISTS(MethodDeclarationData, yieldTypes);
0072 };
0073 
0074 /**
0075  * @class MethodDeclaration
0076  *
0077  * In Ruby there are class methods (methods that belong to a class) and
0078  * instance methods (methods that belong to instances). This class stores
0079  * this information and, therefore, this is the one to be used instead
0080  * of the regular KDevelop::FunctionDeclaration.
0081  */
0082 class KDEVRUBYDUCHAIN_EXPORT MethodDeclaration
0083     : public KDevelop::FunctionDeclaration
0084 {
0085 public:
0086     MethodDeclaration(const KDevelop::RangeInRevision &range,
0087                       KDevelop::DUContext *ctx);
0088     explicit MethodDeclaration(const MethodDeclaration &rhs);
0089     explicit MethodDeclaration(MethodDeclarationData &data);
0090 
0091     /**
0092      * Set if this is a class or an instance method.
0093      * @param isClass True if this is a class method, false otherwise.
0094      */
0095     void setClassMethod(const bool isClass);
0096 
0097     /// @returns true if this is a class method, false otherwise.
0098     bool isClassMethod() const;
0099 
0100     /// Set the access policy to this methods according to the given @p policy.
0101     void setAccessPolicy(const KDevelop::Declaration::AccessPolicy &policy);
0102 
0103     /// @returns the access policy for this method.
0104     KDevelop::Declaration::AccessPolicy accessPolicy() const;
0105 
0106     /// Clear the list of yield types.
0107     void clearYieldTypes();
0108 
0109     /// @returns the size of the yield types list.
0110     uint yieldTypesSize();
0111 
0112     /// @returns the list of the yield types
0113     const YieldType * yieldTypes() const;
0114 
0115     /**
0116      * Replace the nth element of the yield list with the given one. If the
0117      * given n is greater or equal than the size of the list, the element will
0118      * be appended instead.
0119      * @param yield The new yield type.
0120      * @param n The index on the yield list where the given element should be
0121      * placed.
0122      */
0123     void replaceYieldTypes(YieldType yield, uint n);
0124 
0125     enum { Identity = 42 /** The id of this Type. */ };
0126 
0127 private:
0128     /// Re-implemented from KDevelop::Declaration.
0129     KDevelop::Declaration * clonePrivate() const override;
0130 
0131 private:
0132     DUCHAIN_DECLARE_DATA(MethodDeclaration)
0133 };
0134 
0135 }
0136 
0137 #endif // RUBY_METHOD_DECLARATION_H