| 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 | #include "qitem_p.h" |
| 41 | #include "qbuiltintypes_p.h" |
| 42 | #include "qcardinalityverifier_p.h" |
| 43 | #include "qcommonsequencetypes_p.h" |
| 44 | #include "qemptysequence_p.h" |
| 45 | #include "qgenericsequencetype_p.h" |
| 46 | #include "qliteral_p.h" |
| 47 | #include "qpatternistlocale_p.h" |
| 48 | #include "qnamespaceresolver_p.h" |
| 49 | #include "qqnameconstructor_p.h" |
| 50 | #include "qqnamevalue_p.h" |
| 51 | #include "qatomicstring_p.h" |
| 52 | #include "qvalidationerror_p.h" |
| 53 | |
| 54 | #include "qcastas_p.h" |
| 55 | |
| 56 | QT_BEGIN_NAMESPACE |
| 57 | |
| 58 | using namespace QPatternist; |
| 59 | |
| 60 | CastAs::CastAs(const Expression::Ptr &source, |
| 61 | const SequenceType::Ptr &tType) : SingleContainer(source), |
| 62 | m_targetType(tType) |
| 63 | { |
| 64 | Q_ASSERT(source); |
| 65 | Q_ASSERT(tType); |
| 66 | Q_ASSERT(!tType->cardinality().allowsMany()); |
| 67 | Q_ASSERT(tType->itemType()->isAtomicType()); |
| 68 | } |
| 69 | |
| 70 | Item CastAs::evaluateSingleton(const DynamicContext::Ptr &context) const |
| 71 | { |
| 72 | Q_ASSERT(context); |
| 73 | const Item val(m_operand->evaluateSingleton(context)); |
| 74 | |
| 75 | if(val) |
| 76 | return cast(sourceValue: val, context); |
| 77 | else |
| 78 | { |
| 79 | /* No item supplied, let's handle the cardinality part. */ |
| 80 | |
| 81 | if(m_targetType->cardinality().allowsEmpty()) |
| 82 | return Item(); |
| 83 | else |
| 84 | { |
| 85 | Q_ASSERT(context); |
| 86 | context->error(message: QtXmlPatterns::tr(sourceText: "Type error in cast, expected %1, " |
| 87 | "received %2." ) |
| 88 | .arg(a: formatType(type: Cardinality::exactlyOne())) |
| 89 | .arg(a: formatType(type: Cardinality::empty())), |
| 90 | errorCode: ReportContext::XPTY0004, reflection: this); |
| 91 | return Item(); |
| 92 | } |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | Expression::Ptr CastAs::typeCheck(const StaticContext::Ptr &context, |
| 97 | const SequenceType::Ptr &reqType) |
| 98 | { |
| 99 | checkTargetType(context); |
| 100 | const SequenceType::Ptr seqt(m_operand->staticType()); |
| 101 | ItemType::Ptr t(seqt->itemType()); |
| 102 | |
| 103 | /* Special case xs:QName */ |
| 104 | if(BuiltinTypes::xsQName->xdtTypeMatches(other: m_targetType->itemType())) |
| 105 | { |
| 106 | /* Ok, We're casting to xs:QName. */ |
| 107 | if(m_operand->is(i: IDStringValue)) /* A valid combination, let's do the cast. */ |
| 108 | return castToQName(context)->typeCheck(context, reqType); |
| 109 | else if(BuiltinTypes::xsQName->xdtTypeMatches(other: t)) |
| 110 | return m_operand->typeCheck(context, reqType); |
| 111 | else if(seqt->cardinality().isEmpty() && m_targetType->cardinality().allowsEmpty()) |
| 112 | return EmptySequence::create(replacementFor: this, context); |
| 113 | else if(!(seqt->cardinality().isEmpty() && !m_targetType->cardinality().allowsEmpty())) |
| 114 | { |
| 115 | context->error(message: QtXmlPatterns::tr(sourceText: "When casting to %1 or types " |
| 116 | "derived from it, the source " |
| 117 | "value must be of the same type, " |
| 118 | "or it must be a string literal. " |
| 119 | "Type %2 is not allowed." ) |
| 120 | .arg(a: formatType(np: context->namePool(), type: BuiltinTypes::xsQName)) |
| 121 | .arg(a: formatType(np: context->namePool(), type: seqt)), |
| 122 | errorCode: ReportContext::XPTY0004, reflection: this); |
| 123 | return Expression::Ptr(this); |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | const Expression::Ptr me(SingleContainer::typeCheck(context, reqType)); |
| 128 | /* Type may have changed, such as that atomization has been applied. */ |
| 129 | t = m_operand->staticType()->itemType(); |
| 130 | |
| 131 | if(m_targetType->itemType()->xdtTypeMatches(other: t) && |
| 132 | !BuiltinTypes::xsDayTimeDuration->xdtTypeMatches(other: t) && |
| 133 | !BuiltinTypes::xsYearMonthDuration->xdtTypeMatches(other: t)) |
| 134 | { /* At least casting is superflorous. */ |
| 135 | if(m_operand->staticType()->cardinality().isMatch(other: m_targetType->cardinality())) |
| 136 | return m_operand; /* The whole cast expression is redundant. */ |
| 137 | else |
| 138 | { /* Only cardinality check is needed, rewrite to CardinalityVerifier. */ |
| 139 | return Expression::Ptr(new CardinalityVerifier(m_operand, |
| 140 | m_targetType->cardinality(), |
| 141 | ReportContext::FORG0001)); |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | /* Let the CastingPlatform look up its AtomicCaster. */ |
| 146 | prepareCasting(context, sourceType: t); |
| 147 | |
| 148 | return me; |
| 149 | } |
| 150 | |
| 151 | Expression::Ptr CastAs::compress(const StaticContext::Ptr &context) |
| 152 | { |
| 153 | /* Simplify casts to itself. */ |
| 154 | if(*m_targetType->itemType() == *m_operand->staticType()->itemType()) |
| 155 | return m_operand->compress(context); |
| 156 | else |
| 157 | return SingleContainer::compress(context); |
| 158 | } |
| 159 | |
| 160 | Expression::Ptr CastAs::castToQName(const StaticContext::Ptr &context) const |
| 161 | { |
| 162 | /* Apply the whitespace facet by calling trimmed(). */ |
| 163 | /* We can assume m_operand is an Expression because this is a requirement |
| 164 | * for casting to xs:QName. */ |
| 165 | const QString lexQName(m_operand->as<Literal>()->item().as<AtomicValue>()->stringValue().trimmed()); |
| 166 | |
| 167 | const QXmlName |
| 168 | expName(QNameConstructor::expandQName<StaticContext::Ptr, |
| 169 | ReportContext::FORG0001, |
| 170 | ReportContext::FONS0004>(lexicalQName: lexQName, |
| 171 | context, |
| 172 | nsResolver: context->namespaceBindings(), r: this)); |
| 173 | return wrapLiteral(item: toItem(atomicValue: QNameValue::fromValue(np: context->namePool(), name: expName)), context, r: this); |
| 174 | } |
| 175 | |
| 176 | SequenceType::Ptr CastAs::staticType() const |
| 177 | { |
| 178 | if(m_operand->staticType()->cardinality().allowsEmpty()) |
| 179 | return m_targetType; |
| 180 | else |
| 181 | return makeGenericSequenceType(itemType: m_targetType->itemType(), |
| 182 | cardinality: Cardinality::exactlyOne()); |
| 183 | } |
| 184 | |
| 185 | SequenceType::List CastAs::expectedOperandTypes() const |
| 186 | { |
| 187 | SequenceType::List result; |
| 188 | |
| 189 | if(m_targetType->cardinality().allowsEmpty()) |
| 190 | result.append(t: CommonSequenceTypes::ZeroOrOneAtomicType); |
| 191 | else |
| 192 | result.append(t: CommonSequenceTypes::ExactlyOneAtomicType); |
| 193 | |
| 194 | return result; |
| 195 | } |
| 196 | |
| 197 | ExpressionVisitorResult::Ptr CastAs::accept(const ExpressionVisitor::Ptr &visitor) const |
| 198 | { |
| 199 | return visitor->visit(this); |
| 200 | } |
| 201 | |
| 202 | QT_END_NAMESPACE |
| 203 | |