File indexing completed on 2024-06-23 04:34:45

0001 /*
0002     SPDX-FileCopyrightText: 2006 Roberto Raggi <roberto@kdevelop.org>
0003     SPDX-FileCopyrightText: 2006-2008 Hamish Rodda <rodda@kde.org>
0004     SPDX-FileCopyrightText: 2007-2008 David Nolden <david.nolden.kdevelop@art-master.de>
0005 
0006     SPDX-License-Identifier: LGPL-2.0-only
0007 */
0008 
0009 #ifndef KDEVPLATFORM_FUNCTIONTYPE_H
0010 #define KDEVPLATFORM_FUNCTIONTYPE_H
0011 
0012 #include "abstracttype.h"
0013 
0014 namespace KDevelop {
0015 class FunctionTypeData;
0016 
0017 /**
0018  * \short A type representing function types.
0019  *
0020  * A FunctionType is represents the type of a function.  It provides access
0021  * to the return type, and number and types of the arguments.
0022  */
0023 class KDEVPLATFORMLANGUAGE_EXPORT FunctionType
0024     : public AbstractType
0025 {
0026 public:
0027     using Ptr = TypePtr<FunctionType>;
0028 
0029     /// An enumeration of sections of the function signature that can be returned.
0030     enum SignaturePart {
0031         SignatureWhole /**< When this is given to toString(..), a string link "RETURNTYPE (ARGTYPE1, ARGTYPE1, ..)" is returned */,
0032         SignatureReturn /**< When this is given, only a string that represents the return-type is returned */,
0033         SignatureArguments /**< When this is given, a string that represents the arguments like "(ARGTYPE1, ARGTYPE1, ..)"
0034                             * is returned. This does _not_ include a trailing "const" if the function is constant */
0035     };
0036 
0037     /// Default constructor
0038     FunctionType();
0039     /// Copy constructor. \param rhs type to copy
0040     FunctionType(const FunctionType& rhs);
0041     /// Constructor using raw data. \param data internal data.
0042     explicit FunctionType(FunctionTypeData& data);
0043     /// Destructor
0044     ~FunctionType() override;
0045 
0046     FunctionType& operator=(const FunctionType& rhs) = delete;
0047 
0048     /**
0049      * Retrieve the return type of the function.
0050      *
0051      * \returns the return type.
0052      */
0053     AbstractType::Ptr returnType () const;
0054 
0055     /**
0056      * Sets the return type of the function.
0057      *
0058      * \param returnType the return type.
0059      */
0060     void setReturnType(const AbstractType::Ptr& returnType);
0061 
0062     /**
0063      * Retrieve the list of types of the function's arguments.
0064      *
0065      * \returns the argument types.
0066      */
0067     QList<AbstractType::Ptr> arguments () const;
0068 
0069     /**
0070      * Returns the same arguments as arguments(), but without converting them to a QList.
0071      * This is much faster, and should be preferred for very tight loops when the performance counts.
0072      * \return an array that contains the arguments. For the count of arguments, call indexedArgumentsSize
0073      */
0074     const IndexedType* indexedArguments() const;
0075 
0076     /**
0077      * Returns the size of the array returned by indexedArguments(). This is much faster than working with arguments().
0078      */
0079     uint indexedArgumentsSize() const;
0080 
0081     /**
0082      * Add an argument to the function, specifying what type it takes.
0083      *
0084      * \param argument the argument's type
0085      * \param index where to insert the argument; the default "-1" will insert it at the end of the list
0086      */
0087     void addArgument(const AbstractType::Ptr& argument, int index = -1);
0088 
0089     /**
0090      * Remove the argument with number i from the function.
0091      *
0092      * \param i index (starting from 0 with the first argument) to remove
0093      */
0094     void removeArgument(int i);
0095 
0096     AbstractType* clone() const override;
0097 
0098     bool equals(const AbstractType* rhs) const override;
0099 
0100     QString toString() const override;
0101 
0102     /**
0103      * This function creates a string that represents the requested part of
0104      * this function's signature.
0105      *
0106      * \param sigPart part of the signature requested.
0107      * \returns the signature as text.
0108      */
0109     virtual QString partToString(SignaturePart sigPart) const;
0110 
0111     uint hash() const override;
0112 
0113     WhichType whichType() const override;
0114 
0115     void exchangeTypes(TypeExchanger* exchanger) override;
0116 
0117     enum {
0118         Identity = 5
0119     };
0120 
0121     using Data = FunctionTypeData;
0122 
0123 protected:
0124     void accept0 (TypeVisitor* v) const override;
0125 
0126     TYPE_DECLARE_DATA(FunctionType)
0127 };
0128 }
0129 
0130 #endif