| 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_VariableDeclaration_H |
| 51 | #define Patternist_VariableDeclaration_H |
| 52 | |
| 53 | #include <QSharedData> |
| 54 | |
| 55 | #include <private/qexpression_p.h> |
| 56 | #include <private/qpatternistlocale_p.h> |
| 57 | #include <private/qvariablereference_p.h> |
| 58 | |
| 59 | QT_BEGIN_NAMESPACE |
| 60 | |
| 61 | template<typename T> class QStack; |
| 62 | |
| 63 | namespace QPatternist |
| 64 | { |
| 65 | /** |
| 66 | * @short Represents a declared variable. Only used at |
| 67 | * the compilation stage. |
| 68 | * |
| 69 | * @see FunctionArgument |
| 70 | * @author Frans Englich <frans.englich@nokia.com> |
| 71 | * @ingroup Patternist_expressions |
| 72 | */ |
| 73 | class VariableDeclaration : public QSharedData |
| 74 | { |
| 75 | public: |
| 76 | typedef QExplicitlySharedDataPointer<VariableDeclaration> Ptr; |
| 77 | typedef QStack<VariableDeclaration::Ptr> Stack; |
| 78 | typedef QList<VariableDeclaration::Ptr> List; |
| 79 | |
| 80 | /** |
| 81 | * @short The key is the variable name. |
| 82 | */ |
| 83 | typedef QHash<QXmlName, VariableDeclaration::Ptr> Hash; |
| 84 | |
| 85 | enum Type |
| 86 | { |
| 87 | RangeVariable, |
| 88 | ExpressionVariable, |
| 89 | FunctionArgument, |
| 90 | PositionalVariable, |
| 91 | TemplateParameter, |
| 92 | |
| 93 | /** |
| 94 | * A global variable is always an external variable, but it is |
| 95 | * cached differently. |
| 96 | * |
| 97 | * @see DynamicContext::globalItemCacheCell() |
| 98 | */ |
| 99 | GlobalVariable, |
| 100 | |
| 101 | /** |
| 102 | * External variables doesn't use slots, that's a big difference |
| 103 | * compared to the other types. |
| 104 | */ |
| 105 | ExternalVariable |
| 106 | }; |
| 107 | |
| 108 | /** |
| 109 | * Creates a VariableDeclaration. |
| 110 | * |
| 111 | * @p sourceExpr and @p seqType may be @c null. |
| 112 | */ |
| 113 | VariableDeclaration(const QXmlName n, |
| 114 | const VariableSlotID varSlot, |
| 115 | const Type t, |
| 116 | const SequenceType::Ptr &seqType) : name(n) |
| 117 | , slot(varSlot) |
| 118 | , type(t) |
| 119 | , sequenceType(seqType) |
| 120 | , canSourceRewrite(true) |
| 121 | { |
| 122 | Q_ASSERT(!name.isNull()); |
| 123 | Q_ASSERT(t == ExternalVariable || t == TemplateParameter || varSlot > -1); |
| 124 | } |
| 125 | |
| 126 | inline bool isUsed() const |
| 127 | { |
| 128 | return !references.isEmpty(); |
| 129 | } |
| 130 | |
| 131 | inline const Expression::Ptr &expression() const |
| 132 | { |
| 133 | return m_expression; |
| 134 | } |
| 135 | |
| 136 | inline void setExpression(const Expression::Ptr &expr) |
| 137 | { |
| 138 | m_expression = expr; |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * @short Returns how many times this variable is used. |
| 143 | */ |
| 144 | inline bool usedByMany() const |
| 145 | { |
| 146 | return references.count() > 1; |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * @short Returns @c true if @p list contains @p lookup. |
| 151 | */ |
| 152 | static bool contains(const VariableDeclaration::List &list, |
| 153 | const QXmlName &lookup); |
| 154 | |
| 155 | const QXmlName name; |
| 156 | const VariableSlotID slot; |
| 157 | const Type type; |
| 158 | |
| 159 | /** |
| 160 | * The declared type of the variable. What the value might be, depends |
| 161 | * on the context which VariableDeclaration is used in. Note that |
| 162 | * sequenceType is hence not in anyway obligated to the type of |
| 163 | * expression(). |
| 164 | */ |
| 165 | const SequenceType::Ptr sequenceType; |
| 166 | VariableReference::List references; |
| 167 | |
| 168 | /** |
| 169 | * @short Whether a reference can rewrite itself to expression(). |
| 170 | * |
| 171 | * The default value is @c true. |
| 172 | */ |
| 173 | bool canSourceRewrite; |
| 174 | |
| 175 | private: |
| 176 | Expression::Ptr m_expression; |
| 177 | Q_DISABLE_COPY(VariableDeclaration) |
| 178 | }; |
| 179 | |
| 180 | /** |
| 181 | * @short Formats @p var appropriately for display. |
| 182 | * |
| 183 | * @relates VariableDeclaration |
| 184 | */ |
| 185 | static inline QString formatKeyword(const VariableDeclaration::Ptr &var, |
| 186 | const NamePool::Ptr &np) |
| 187 | { |
| 188 | Q_ASSERT(var); |
| 189 | Q_ASSERT(np); |
| 190 | return formatKeyword(keyword: np->displayName(qName: var->name)); |
| 191 | } |
| 192 | |
| 193 | } |
| 194 | |
| 195 | QT_END_NAMESPACE |
| 196 | |
| 197 | #endif |
| 198 | |