1 /****************************************************************************
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the QtXmlPatterns module of the Qt Toolkit.
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
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 "qexpression_p.h"
58 #include "qpatternistlocale_p.h"
59 #include "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));