| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2016 The Qt Company Ltd. |
| 4 | ** Contact: https://www.qt.io/licensing/ |
| 5 | ** |
| 6 | ** This file is part of the QtXmlPatterns module of the Qt Toolkit. |
| 7 | ** |
| 8 | ** $QT_BEGIN_LICENSE:LGPL$ |
| 9 | ** Commercial License Usage |
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in |
| 11 | ** accordance with the commercial license agreement provided with the |
| 12 | ** Software or, alternatively, in accordance with the terms contained in |
| 13 | ** a written agreement between you and The Qt Company. For licensing terms |
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
| 15 | ** information use the contact form at https://www.qt.io/contact-us. |
| 16 | ** |
| 17 | ** GNU Lesser General Public License Usage |
| 18 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
| 19 | ** General Public License version 3 as published by the Free Software |
| 20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
| 21 | ** packaging of this file. Please review the following information to |
| 22 | ** ensure the GNU Lesser General Public License version 3 requirements |
| 23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
| 24 | ** |
| 25 | ** GNU General Public License Usage |
| 26 | ** Alternatively, this file may be used under the terms of the GNU |
| 27 | ** General Public License version 2.0 or (at your option) the GNU General |
| 28 | ** Public license version 3 or any later version approved by the KDE Free |
| 29 | ** Qt Foundation. The licenses are as published by the Free Software |
| 30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
| 31 | ** included in the packaging of this file. Please review the following |
| 32 | ** information to ensure the GNU General Public License requirements will |
| 33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
| 34 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
| 35 | ** |
| 36 | ** $QT_END_LICENSE$ |
| 37 | ** |
| 38 | ****************************************************************************/ |
| 39 | |
| 40 | // |
| 41 | // W A R N I N G |
| 42 | // ------------- |
| 43 | // |
| 44 | // This file is not part of the Qt API. It exists purely as an |
| 45 | // implementation detail. This header file may change from version to |
| 46 | // version without notice, or even be removed. |
| 47 | // |
| 48 | // We mean it. |
| 49 | |
| 50 | #ifndef Patternist_FunctionSignature_H |
| 51 | #define Patternist_FunctionSignature_H |
| 52 | |
| 53 | #include <QSharedData> |
| 54 | |
| 55 | #include <private/qcalltargetdescription_p.h> |
| 56 | #include <private/qexpression_p.h> |
| 57 | #include <private/qfunctionargument_p.h> |
| 58 | #include <private/qpatternistlocale_p.h> |
| 59 | #include <private/qprimitives_p.h> |
| 60 | |
| 61 | QT_BEGIN_NAMESPACE |
| 62 | |
| 63 | template<typename Key, typename Value> class QHash; |
| 64 | template<typename T> class QList; |
| 65 | |
| 66 | namespace QPatternist |
| 67 | { |
| 68 | |
| 69 | /** |
| 70 | * @short Represents the signature of an XPath function. |
| 71 | * |
| 72 | * FunctionSignature represents and allows inspection of a function signature, |
| 73 | * such as <tt>fn:string-join($arg1 as xs:string*, $arg2 as xs:string) as xs:string</tt>. |
| 74 | * No XPath related languages allows polymorphism on the type of the arguments, only the |
| 75 | * amount(arity) of the arguments. For example, <tt>fn:string() as xs:string</tt> and |
| 76 | * <tt>fn:string($arg as item()?) as xs:string</tt> can happily co-exist, but |
| 77 | * <tt>fn:string($arg as item()?) as xs:string</tt> and |
| 78 | * <tt>fn:string($arg as xs:anyAtomicType?) as xs:string</tt> would be an error. This |
| 79 | * fact is reflected by FunctionSignature that if minimumArguments() and maximumArguments() |
| 80 | * are not equal, it means that this FunctionSignature represents several |
| 81 | * function signatures. |
| 82 | * |
| 83 | * @ingroup Patternist_functions |
| 84 | * @see <a href="http://www.w3.org/TR/xpath-functions/#func-signatures">XQuery 1.0 and |
| 85 | * XPath 2.0 Functions and Operators, 1.4 Function Signatures and Descriptions</a> |
| 86 | * @see <a href="http://en.wikipedia.org/wiki/Arity">Wikipedia, the free encyclopedia, Arity</a> |
| 87 | * @author Frans Englich <frans.englich@nokia.com> |
| 88 | */ |
| 89 | class Q_AUTOTEST_EXPORT FunctionSignature : public CallTargetDescription |
| 90 | { |
| 91 | public: |
| 92 | enum |
| 93 | { |
| 94 | /** |
| 95 | * Flags the function as allowing an unlimited amount of arguments. |
| 96 | */ |
| 97 | UnlimitedArity = -1 |
| 98 | }; |
| 99 | |
| 100 | typedef QExplicitlySharedDataPointer<FunctionSignature> Ptr; |
| 101 | typedef QHash<QXmlName, FunctionSignature::Ptr> Hash; |
| 102 | typedef QList<FunctionSignature::Ptr> List; |
| 103 | |
| 104 | /** |
| 105 | * A number which tells the amount of arguments a function has. |
| 106 | */ |
| 107 | typedef qint16 Arity; |
| 108 | |
| 109 | FunctionSignature(const QXmlName name, |
| 110 | const Arity minArgs, |
| 111 | const Arity maxArgs, |
| 112 | const SequenceType::Ptr &returnType, |
| 113 | const Expression::Properties chars = Expression::Properties(), |
| 114 | const Expression::ID id = Expression::IDIgnorableExpression); |
| 115 | |
| 116 | void setArguments(const FunctionArgument::List &args); |
| 117 | FunctionArgument::List arguments() const; |
| 118 | |
| 119 | /** |
| 120 | * This is a convenience function. Calling this once, is equal to |
| 121 | * calling setArguments() with a list containing a FunctionsArgument with name @p name |
| 122 | * and type @p type. |
| 123 | */ |
| 124 | void appendArgument(const QXmlName::LocalNameCode name, |
| 125 | const SequenceType::Ptr &type); |
| 126 | |
| 127 | /** |
| 128 | * Checks whether @p arity is within the range of allowed count of arguments. For example, |
| 129 | * when the minimum arguments is 1 and maximum arguments 2, @c false will be returned for |
| 130 | * passing 0 while @c true will be returned when 2 is passed. |
| 131 | */ |
| 132 | bool isArityValid(const xsInteger arity) const; |
| 133 | |
| 134 | Arity minimumArguments() const; |
| 135 | Arity maximumArguments() const; |
| 136 | |
| 137 | /** |
| 138 | * The return type of this function signature. For example, if the represented function |
| 139 | * signature is <tt>fn:string() as xs:string</tt>, the return type is <tt>xs:string</tt>. |
| 140 | */ |
| 141 | SequenceType::Ptr returnType() const; |
| 142 | |
| 143 | /** |
| 144 | * The properties that the corresponding FunctionCall instance should return in |
| 145 | * Expression::properties(). |
| 146 | */ |
| 147 | Expression::Properties properties() const; |
| 148 | |
| 149 | /** |
| 150 | * Determines whether this FunctionSignature is equal to @p other, taking |
| 151 | * into account XPath's function polymorphism. @p other is equal to this |
| 152 | * FunctionSignature if their name() instances are equal, and that the maximumArguments() |
| 153 | * and minimumArguments() arguments of @p other are allowed, as per isArityValid(). |
| 154 | * |
| 155 | * In other words, this equalness operator can return @c true for different |
| 156 | * signatures, but it do make sense since a FunctionSignature can represent |
| 157 | * multiple signatures. |
| 158 | * |
| 159 | * @returns @c true if this FunctionSignature is equal to @p other, otherwise @c false |
| 160 | */ |
| 161 | bool operator==(const FunctionSignature &other) const; |
| 162 | |
| 163 | /** |
| 164 | * Builds a string representation for this function signature. The syntax |
| 165 | * used is the one used in the XQuery. It looks like this: |
| 166 | * |
| 167 | * <tt>prefix:function-name($parameter-name as parameter-type, ...) as return-type</tt> |
| 168 | * |
| 169 | * The prefix used for the name is conventional. For example, for constructor functions |
| 170 | * is @c xs used. |
| 171 | * |
| 172 | * @see <a href="http://www.w3.org/TR/xpath-functions/#func-signatures">XQuery 1.0 and |
| 173 | * XPath 2.0 Functions and Operators, 1.4 Function Signatures and Descriptions</a> |
| 174 | */ |
| 175 | QString displayName(const NamePool::Ptr &np) const; |
| 176 | |
| 177 | /** |
| 178 | * The ID that the corresponding FunctionCall instance should return in |
| 179 | * Expression::id(). |
| 180 | */ |
| 181 | Expression::ID id() const; |
| 182 | |
| 183 | private: |
| 184 | Q_DISABLE_COPY(FunctionSignature) |
| 185 | |
| 186 | const Arity m_minArgs; |
| 187 | const Arity m_maxArgs; |
| 188 | const SequenceType::Ptr m_returnType; |
| 189 | FunctionArgument::List m_arguments; |
| 190 | const Expression::Properties m_props; |
| 191 | const Expression::ID m_id; |
| 192 | }; |
| 193 | |
| 194 | /** |
| 195 | * @short Formats FunctionSignature. |
| 196 | */ |
| 197 | static inline QString formatFunction(const NamePool::Ptr &np, const FunctionSignature::Ptr &func) |
| 198 | { |
| 199 | return QLatin1String("<span class='XQuery-function'>" ) + |
| 200 | escape(input: func->displayName(np)) + |
| 201 | QLatin1String("</span>" ); |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | QT_END_NAMESPACE |
| 206 | |
| 207 | #endif |
| 208 | |