56a6afec0a6cefa4672a614246b5ea442e958f74
[profile/ivi/qtxmlpatterns.git] / src / xmlpatterns / expr / qvariabledeclaration_p.h
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
5 **
6 ** This file is part of the QtXmlPatterns module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
16 **
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
20 **
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
28 **
29 ** Other Usage
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 //
43 //  W A R N I N G
44 //  -------------
45 //
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.
49 //
50 // We mean it.
51
52 #ifndef Patternist_VariableDeclaration_H
53 #define Patternist_VariableDeclaration_H
54
55 #include <QSharedData>
56
57 #include <private/qexpression_p.h>
58 #include <private/qpatternistlocale_p.h>
59 #include <private/qvariablereference_p.h>
60
61 QT_BEGIN_HEADER
62
63 QT_BEGIN_NAMESPACE
64
65 template<typename T> class QStack;
66
67 namespace QPatternist
68 {
69     /**
70      * @short Represents a declared variable. Only used at
71      * the compilation stage.
72      *
73      * @see FunctionArgument
74      * @author Frans Englich <frans.englich@nokia.com>
75      * @ingroup Patternist_expressions
76      */
77     class VariableDeclaration : public QSharedData
78     {
79     public:
80         typedef QExplicitlySharedDataPointer<VariableDeclaration> Ptr;
81         typedef QStack<VariableDeclaration::Ptr> Stack;
82         typedef QList<VariableDeclaration::Ptr> List;
83
84         /**
85          * @short The key is the variable name.
86          */
87         typedef QHash<QXmlName, VariableDeclaration::Ptr> Hash;
88
89         enum Type
90         {
91             RangeVariable,
92             ExpressionVariable,
93             FunctionArgument,
94             PositionalVariable,
95             TemplateParameter,
96
97             /**
98              * A global variable is always an external variable, but it is
99              * cached differently.
100              *
101              * @see DynamicContext::globalItemCacheCell()
102              */
103             GlobalVariable,
104
105             /**
106              * External variables doesn't use slots, that's a big difference
107              * compared to the other types.
108              */
109             ExternalVariable
110         };
111
112         /**
113          * Creates a VariableDeclaration.
114          *
115          * @p sourceExpr and @p seqType may be @c null.
116          */
117         VariableDeclaration(const QXmlName n,
118                             const VariableSlotID varSlot,
119                             const Type t,
120                             const SequenceType::Ptr &seqType) : name(n)
121                                                               , slot(varSlot)
122                                                               , type(t)
123                                                               , sequenceType(seqType)
124                                                               , canSourceRewrite(true)
125         {
126             Q_ASSERT(!name.isNull());
127             Q_ASSERT(t == ExternalVariable || t == TemplateParameter || varSlot > -1);
128         }
129
130         inline bool isUsed() const
131         {
132             return !references.isEmpty();
133         }
134
135         inline const Expression::Ptr &expression() const
136         {
137             return m_expression;
138         }
139
140         inline void setExpression(const Expression::Ptr &expr)
141         {
142             m_expression = expr;
143         }
144
145         /**
146          * @short Returns how many times this variable is used.
147          */
148         inline bool usedByMany() const
149         {
150             return references.count() > 1;
151         }
152
153         /**
154          * @short Returns @c true if @p list contains @p lookup.
155          */
156         static bool contains(const VariableDeclaration::List &list,
157                              const QXmlName &lookup);
158
159         const QXmlName                  name;
160         const VariableSlotID            slot;
161         const Type                      type;
162
163         /**
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
167          * expression().
168          */
169         const SequenceType::Ptr         sequenceType;
170         VariableReference::List         references;
171
172         /**
173          * @short Whether a reference can rewrite itself to expression().
174          *
175          * The default value is @c true.
176          */
177         bool canSourceRewrite;
178
179     private:
180         Expression::Ptr                 m_expression;
181         Q_DISABLE_COPY(VariableDeclaration)
182     };
183
184     /**
185      * @short Formats @p var appropriately for display.
186      *
187      * @relates VariableDeclaration
188      */
189     static inline QString formatKeyword(const VariableDeclaration::Ptr &var,
190                                         const NamePool::Ptr &np)
191     {
192         Q_ASSERT(var);
193         Q_ASSERT(np);
194         return formatKeyword(np->displayName(var->name));
195     }
196
197 }
198
199 QT_END_NAMESPACE
200
201 QT_END_HEADER
202
203 #endif