1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the QtXmlPatterns module of the Qt Toolkit.
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.
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.
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.
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.
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_TypeChecker_H
53 #define Patternist_TypeChecker_H
55 #include <private/qstaticcontext_p.h>
56 #include <private/qexpression_p.h>
65 * @short Contains functions that applies Function Conversion Rules and other
66 * kinds of compile-time type checking tasks.
68 * @ingroup Patternist_types
69 * @author Frans Englich <frans.englich@nokia.com>
77 * @short When set, the function conversion rules are applied.
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.
83 AutomaticallyConvert = 1,
86 * @short Whether the focus should be checked or not.
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.
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
101 * However, when GeneratePromotion is set, code will be generated
102 * that performs this conversion regardless of what any receiver
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.
111 typedef QFlags<Option> Options;
114 * @short Builds a pipeline of artificial AST nodes that ensures @p operand
115 * conforms to the type @p reqType by applying the Function
118 * This new Expression is returned, or, if no conversions were necessary,
119 * @p operand as it is.
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().
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
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));
138 static inline Expression::Ptr typeCheck(Expression *const op,
139 const StaticContext::Ptr &context,
140 const SequenceType::Ptr &reqType);
142 * @short Implements the type checking and promotion part of the Function Conversion Rules.
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);
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.
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>
159 static bool promotionPossible(const ItemType::Ptr &fromType,
160 const ItemType::Ptr &toType,
161 const StaticContext::Ptr &context);
164 * @short Centralizes a message-string to reduce work for translators
165 * and increase consistency.
167 static inline QString wrongType(const NamePool::Ptr &np,
168 const ItemType::Ptr &reqType,
169 const ItemType::Ptr &opType);
172 * No implementation is provided for this constructor. This class
173 * is not supposed to be instantiated.
175 inline TypeChecker();
177 Q_DISABLE_COPY(TypeChecker)