Change copyrights from Nokia to Digia
[profile/ivi/qtxmlpatterns.git] / src / xmlpatterns / type / qtypechecker_p.h
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
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 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.
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 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.
24 **
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.
28 **
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.
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_TypeChecker_H
53 #define Patternist_TypeChecker_H
54
55 #include <private/qstaticcontext_p.h>
56 #include <private/qexpression_p.h>
57
58 QT_BEGIN_HEADER
59
60 QT_BEGIN_NAMESPACE
61
62 namespace QPatternist
63 {
64     /**
65      * @short Contains functions that applies Function Conversion Rules and other
66      * kinds of compile-time type checking tasks.
67      *
68      * @ingroup Patternist_types
69      * @author Frans Englich <frans.englich@nokia.com>
70      */
71     class TypeChecker
72     {
73     public:
74         enum Option
75         {
76             /**
77              * @short When set, the function conversion rules are applied.
78              *
79              * For instance, this is type promotion and conversions from @c
80              * xs:untypedAtomic to @c xs:date. This is done for function calls,
81              * but not when binding an expression to a variable.
82              */
83             AutomaticallyConvert = 1,
84
85             /**
86              * @short Whether the focus should be checked or not.
87              *
88              * Sometimes the focus is unknown at the time
89              * applyFunctionConversion() is called, and therefore it is
90              * of interest to post pone the check to later on.
91              */
92             CheckFocus = 2,
93
94             /**
95              * When applyFunctionConversion() is passed AutomaticallyConvert
96              * and promotion is required, such as from @c xs:integer to
97              * @c xs:float, there will be no conversion performed, with the
98              * assumption that the receiver will call Numeric::toFloat() or
99              * similar.
100              *
101              * However, when GeneratePromotion is set, code will be generated
102              * that performs this conversion regardless of what any receiver
103              * do.
104              *
105              * This is useful in the case where one Expression only pipes the
106              * result of another. The only known case of that as of this
107              * writing is when UserFunctionCallsite evaluates its body.
108              */
109             GeneratePromotion
110         };
111         typedef QFlags<Option> Options;
112
113         /**
114          * @short Builds a pipeline of artificial AST nodes that ensures @p operand
115          * conforms to the type @p reqType by applying the Function
116          * Conversion Rules.
117          *
118          * This new Expression is returned, or, if no conversions were necessary,
119          * @p operand as it is.
120          *
121          * applyFunctionConversion() also performs various checks, such as if
122          * @p operand needs the focus and that the focus is defined in the
123          * @p context. These checks are largely guided by @p operand's
124          * Expression::properties().
125          *
126          * @see <a href="http://www.w3.org/TR/xpath20/\#id-function-calls">XML Path
127          * Language (XPath) 2.0, 3.1.5 Function Calls</a>, more specifically the
128          * Function Conversion Rules
129          */
130         static Expression::Ptr
131         applyFunctionConversion(const Expression::Ptr &operand,
132                                 const SequenceType::Ptr &reqType,
133                                 const StaticContext::Ptr &context,
134                                 const ReportContext::ErrorCode code = ReportContext::XPTY0004,
135                                 const Options = Options(AutomaticallyConvert | CheckFocus));
136     private:
137
138         static inline Expression::Ptr typeCheck(Expression *const op,
139                                                 const StaticContext::Ptr &context,
140                                                 const SequenceType::Ptr &reqType);
141         /**
142          * @short Implements the type checking and promotion part of the Function Conversion Rules.
143          */
144         static Expression::Ptr verifyType(const Expression::Ptr &operand,
145                                           const SequenceType::Ptr &reqSeqType,
146                                           const StaticContext::Ptr &context,
147                                           const ReportContext::ErrorCode code,
148                                           const Options options);
149
150         /**
151          * Determines whether type promotion is possible from one type to another. False
152          * is returned when a promotion is not possible or if a promotion is not needed(as when
153          * the types are identical), since that can be considered to not be type promotion.
154          *
155          * @returns @c true if @p fromType can be promoted to @p toType.
156          * @see <a href="http://www.w3.org/TR/xpath20/#promotion">XML Path Language
157          * (XPath) 2.0, B.1 Type Promotion</a>
158          */
159         static bool promotionPossible(const ItemType::Ptr &fromType,
160                                       const ItemType::Ptr &toType,
161                                       const StaticContext::Ptr &context);
162
163         /**
164          * @short Centralizes a message-string to reduce work for translators
165          * and increase consistency.
166          */
167         static inline QString wrongType(const NamePool::Ptr &np,
168                                         const ItemType::Ptr &reqType,
169                                         const ItemType::Ptr &opType);
170
171         /**
172          * No implementation is provided for this constructor. This class
173          * is not supposed to be instantiated.
174          */
175         inline TypeChecker();
176
177         Q_DISABLE_COPY(TypeChecker)
178     };
179 }
180
181 QT_END_NAMESPACE
182
183 QT_END_HEADER
184
185 #endif