1 /****************************************************************************
3 ** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
6 ** This file is part of the QtXmlPatterns module of the Qt Toolkit.
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 Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
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 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
40 ****************************************************************************/
46 // This file is not part of the Qt API. It exists purely as an
47 // implementation detail. This header file may change from version to
48 // version without notice, or even be removed.
52 #ifndef Patternist_VariableDeclaration_H
53 #define Patternist_VariableDeclaration_H
55 #include <QSharedData>
57 #include <private/qexpression_p.h>
58 #include <private/qpatternistlocale_p.h>
59 #include <private/qvariablereference_p.h>
65 template<typename T> class QStack;
70 * @short Represents a declared variable. Only used at
71 * the compilation stage.
73 * @see FunctionArgument
74 * @author Frans Englich <frans.englich@nokia.com>
75 * @ingroup Patternist_expressions
77 class VariableDeclaration : public QSharedData
80 typedef QExplicitlySharedDataPointer<VariableDeclaration> Ptr;
81 typedef QStack<VariableDeclaration::Ptr> Stack;
82 typedef QList<VariableDeclaration::Ptr> List;
85 * @short The key is the variable name.
87 typedef QHash<QXmlName, VariableDeclaration::Ptr> Hash;
98 * A global variable is always an external variable, but it is
101 * @see DynamicContext::globalItemCacheCell()
106 * External variables doesn't use slots, that's a big difference
107 * compared to the other types.
113 * Creates a VariableDeclaration.
115 * @p sourceExpr and @p seqType may be @c null.
117 VariableDeclaration(const QXmlName n,
118 const VariableSlotID varSlot,
120 const SequenceType::Ptr &seqType) : name(n)
123 , sequenceType(seqType)
124 , canSourceRewrite(true)
126 Q_ASSERT(!name.isNull());
127 Q_ASSERT(t == ExternalVariable || t == TemplateParameter || varSlot > -1);
130 inline bool isUsed() const
132 return !references.isEmpty();
135 inline const Expression::Ptr &expression() const
140 inline void setExpression(const Expression::Ptr &expr)
146 * @short Returns how many times this variable is used.
148 inline bool usedByMany() const
150 return references.count() > 1;
154 * @short Returns @c true if @p list contains @p lookup.
156 static bool contains(const VariableDeclaration::List &list,
157 const QXmlName &lookup);
160 const VariableSlotID slot;
164 * The declared type of the variable. What the value might be, depends
165 * on the context which VariableDeclaration is used in. Note that
166 * sequenceType is hence not in anyway obligated to the type of
169 const SequenceType::Ptr sequenceType;
170 VariableReference::List references;
173 * @short Whether a reference can rewrite itself to expression().
175 * The default value is @c true.
177 bool canSourceRewrite;
180 Expression::Ptr m_expression;
181 Q_DISABLE_COPY(VariableDeclaration)
185 * @short Formats @p var appropriately for display.
187 * @relates VariableDeclaration
189 static inline QString formatKeyword(const VariableDeclaration::Ptr &var,
190 const NamePool::Ptr &np)
194 return formatKeyword(np->displayName(var->name));