| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2016 The Qt Company Ltd. |
| 4 | ** Contact: https://www.qt.io/licensing/ |
| 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 The Qt Company. For licensing terms |
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
| 15 | ** information use the contact form at https://www.qt.io/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 3 as published by the Free Software |
| 20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
| 21 | ** packaging of this file. Please review the following information to |
| 22 | ** ensure the GNU Lesser General Public License version 3 requirements |
| 23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
| 24 | ** |
| 25 | ** GNU General Public License Usage |
| 26 | ** Alternatively, this file may be used under the terms of the GNU |
| 27 | ** General Public License version 2.0 or (at your option) the GNU General |
| 28 | ** Public license version 3 or any later version approved by the KDE Free |
| 29 | ** Qt Foundation. The licenses are as published by the Free Software |
| 30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
| 31 | ** included in the packaging of this file. Please review the following |
| 32 | ** information to ensure the GNU General Public License requirements will |
| 33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
| 34 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
| 35 | ** |
| 36 | ** $QT_END_LICENSE$ |
| 37 | ** |
| 38 | ****************************************************************************/ |
| 39 | |
| 40 | // |
| 41 | // W A R N I N G |
| 42 | // ------------- |
| 43 | // |
| 44 | // This file is not part of the Qt API. It exists purely as an |
| 45 | // implementation detail. This header file may change from version to |
| 46 | // version without notice, or even be removed. |
| 47 | // |
| 48 | // We mean it. |
| 49 | |
| 50 | #ifndef Patternist_TypeChecker_H |
| 51 | #define Patternist_TypeChecker_H |
| 52 | |
| 53 | #include <private/qstaticcontext_p.h> |
| 54 | #include <private/qexpression_p.h> |
| 55 | |
| 56 | QT_BEGIN_NAMESPACE |
| 57 | |
| 58 | namespace QPatternist |
| 59 | { |
| 60 | /** |
| 61 | * @short Contains functions that applies Function Conversion Rules and other |
| 62 | * kinds of compile-time type checking tasks. |
| 63 | * |
| 64 | * @ingroup Patternist_types |
| 65 | * @author Frans Englich <frans.englich@nokia.com> |
| 66 | */ |
| 67 | class TypeChecker |
| 68 | { |
| 69 | public: |
| 70 | enum Option |
| 71 | { |
| 72 | /** |
| 73 | * @short When set, the function conversion rules are applied. |
| 74 | * |
| 75 | * For instance, this is type promotion and conversions from @c |
| 76 | * xs:untypedAtomic to @c xs:date. This is done for function calls, |
| 77 | * but not when binding an expression to a variable. |
| 78 | */ |
| 79 | AutomaticallyConvert = 1, |
| 80 | |
| 81 | /** |
| 82 | * @short Whether the focus should be checked or not. |
| 83 | * |
| 84 | * Sometimes the focus is unknown at the time |
| 85 | * applyFunctionConversion() is called, and therefore it is |
| 86 | * of interest to post pone the check to later on. |
| 87 | */ |
| 88 | CheckFocus = 2, |
| 89 | |
| 90 | /** |
| 91 | * When applyFunctionConversion() is passed AutomaticallyConvert |
| 92 | * and promotion is required, such as from @c xs:integer to |
| 93 | * @c xs:float, there will be no conversion performed, with the |
| 94 | * assumption that the receiver will call Numeric::toFloat() or |
| 95 | * similar. |
| 96 | * |
| 97 | * However, when GeneratePromotion is set, code will be generated |
| 98 | * that performs this conversion regardless of what any receiver |
| 99 | * do. |
| 100 | * |
| 101 | * This is useful in the case where one Expression only pipes the |
| 102 | * result of another. The only known case of that as of this |
| 103 | * writing is when UserFunctionCallsite evaluates its body. |
| 104 | */ |
| 105 | GeneratePromotion |
| 106 | }; |
| 107 | typedef QFlags<Option> Options; |
| 108 | |
| 109 | /** |
| 110 | * @short Builds a pipeline of artificial AST nodes that ensures @p operand |
| 111 | * conforms to the type @p reqType by applying the Function |
| 112 | * Conversion Rules. |
| 113 | * |
| 114 | * This new Expression is returned, or, if no conversions were necessary, |
| 115 | * @p operand as it is. |
| 116 | * |
| 117 | * applyFunctionConversion() also performs various checks, such as if |
| 118 | * @p operand needs the focus and that the focus is defined in the |
| 119 | * @p context. These checks are largely guided by @p operand's |
| 120 | * Expression::properties(). |
| 121 | * |
| 122 | * @see <a href="http://www.w3.org/TR/xpath20/\#id-function-calls">XML Path |
| 123 | * Language (XPath) 2.0, 3.1.5 Function Calls</a>, more specifically the |
| 124 | * Function Conversion Rules |
| 125 | */ |
| 126 | static Expression::Ptr |
| 127 | applyFunctionConversion(const Expression::Ptr &operand, |
| 128 | const SequenceType::Ptr &reqType, |
| 129 | const StaticContext::Ptr &context, |
| 130 | const ReportContext::ErrorCode code = ReportContext::XPTY0004, |
| 131 | const Options = Options(AutomaticallyConvert | CheckFocus)); |
| 132 | private: |
| 133 | |
| 134 | static inline Expression::Ptr typeCheck(Expression *const op, |
| 135 | const StaticContext::Ptr &context, |
| 136 | const SequenceType::Ptr &reqType); |
| 137 | /** |
| 138 | * @short Implements the type checking and promotion part of the Function Conversion Rules. |
| 139 | */ |
| 140 | static Expression::Ptr verifyType(const Expression::Ptr &operand, |
| 141 | const SequenceType::Ptr &reqSeqType, |
| 142 | const StaticContext::Ptr &context, |
| 143 | const ReportContext::ErrorCode code, |
| 144 | const Options options); |
| 145 | |
| 146 | /** |
| 147 | * Determines whether type promotion is possible from one type to another. False |
| 148 | * is returned when a promotion is not possible or if a promotion is not needed(as when |
| 149 | * the types are identical), since that can be considered to not be type promotion. |
| 150 | * |
| 151 | * @returns @c true if @p fromType can be promoted to @p toType. |
| 152 | * @see <a href="http://www.w3.org/TR/xpath20/#promotion">XML Path Language |
| 153 | * (XPath) 2.0, B.1 Type Promotion</a> |
| 154 | */ |
| 155 | static bool promotionPossible(const ItemType::Ptr &fromType, |
| 156 | const ItemType::Ptr &toType, |
| 157 | const StaticContext::Ptr &context); |
| 158 | |
| 159 | /** |
| 160 | * @short Centralizes a message-string to reduce work for translators |
| 161 | * and increase consistency. |
| 162 | */ |
| 163 | static inline QString wrongType(const NamePool::Ptr &np, |
| 164 | const ItemType::Ptr &reqType, |
| 165 | const ItemType::Ptr &opType); |
| 166 | |
| 167 | /** |
| 168 | * No implementation is provided for this constructor. This class |
| 169 | * is not supposed to be instantiated. |
| 170 | */ |
| 171 | inline TypeChecker(); |
| 172 | |
| 173 | Q_DISABLE_COPY(TypeChecker) |
| 174 | }; |
| 175 | } |
| 176 | |
| 177 | QT_END_NAMESPACE |
| 178 | |
| 179 | #endif |
| 180 | |