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 "qatomictype_p.h" |
41 | #include "qitem_p.h" |
42 | #include "qboolean_p.h" |
43 | #include "qbuiltintypes_p.h" |
44 | #include "qcommonsequencetypes_p.h" |
45 | #include "qcommonvalues_p.h" |
46 | #include "qliteral_p.h" |
47 | |
48 | #include "qcastableas_p.h" |
49 | |
50 | QT_BEGIN_NAMESPACE |
51 | |
52 | using namespace QPatternist; |
53 | |
54 | CastableAs::CastableAs(const Expression::Ptr &operand, |
55 | const SequenceType::Ptr &tType) : SingleContainer(operand), |
56 | m_targetType(tType) |
57 | { |
58 | Q_ASSERT(tType); |
59 | Q_ASSERT(!tType->cardinality().allowsMany()); |
60 | Q_ASSERT(tType->itemType()->isAtomicType()); |
61 | } |
62 | |
63 | bool CastableAs::evaluateEBV(const DynamicContext::Ptr &context) const |
64 | { |
65 | Item item; |
66 | |
67 | if(m_operand->staticType()->cardinality().allowsMany()) |
68 | { |
69 | const Item::Iterator::Ptr it(m_operand->evaluateSequence(context)); |
70 | item = it->next(); |
71 | |
72 | if(it->next()) |
73 | return false; |
74 | } |
75 | else |
76 | item = m_operand->evaluateSingleton(context); |
77 | |
78 | if(item) |
79 | return !cast(sourceValue: item, context).as<AtomicValue>()->hasError(); |
80 | else |
81 | return m_targetType->cardinality().allowsEmpty(); |
82 | } |
83 | |
84 | Expression::Ptr CastableAs::compress(const StaticContext::Ptr &context) |
85 | { |
86 | const Expression::Ptr me(SingleContainer::compress(context)); |
87 | |
88 | if(me != this) /* We already managed to const fold, how convenient. */ |
89 | return me; |
90 | |
91 | const AtomicType::Ptr t(m_targetType->itemType()); |
92 | |
93 | const SequenceType::Ptr opType(m_operand->staticType()); |
94 | |
95 | /* Casting to these always succeeds, assuming the cardinality also matches, |
96 | * although the cardinality can fail. */ |
97 | if(( *t == *BuiltinTypes::xsString |
98 | || *t == *BuiltinTypes::xsUntypedAtomic |
99 | || *t == *opType->itemType()) |
100 | &&(m_targetType->cardinality().isMatch(other: opType->cardinality()))) |
101 | { |
102 | return wrapLiteral(item: CommonValues::BooleanTrue, context, r: this); |
103 | } |
104 | else |
105 | return me; |
106 | } |
107 | |
108 | Expression::Ptr CastableAs::typeCheck(const StaticContext::Ptr &context, |
109 | const SequenceType::Ptr &reqType) |
110 | { |
111 | checkTargetType(context); |
112 | const Expression::Ptr me(SingleContainer::typeCheck(context, reqType)); |
113 | |
114 | return me; |
115 | if(BuiltinTypes::xsQName->xdtTypeMatches(other: m_targetType->itemType())) |
116 | { |
117 | const SequenceType::Ptr seqt(m_operand->staticType()); |
118 | /* We can cast a string literal, an xs:QName value, and an |
119 | * empty sequence(if empty is allowed), to xs:QName. */ |
120 | if(m_operand->is(i: IDStringValue) || |
121 | BuiltinTypes::xsQName->xdtTypeMatches(other: seqt->itemType()) || |
122 | (*seqt->itemType() == *CommonSequenceTypes::Empty && m_targetType->cardinality().allowsEmpty())) |
123 | { |
124 | return wrapLiteral(item: CommonValues::BooleanTrue, context, r: this)->typeCheck(context, reqType); |
125 | } |
126 | else |
127 | return wrapLiteral(item: CommonValues::BooleanFalse, context, r: this)->typeCheck(context, reqType); |
128 | } |
129 | else |
130 | { |
131 | /* Let the CastingPlatform look up its AtomicCaster. */ |
132 | prepareCasting(context, sourceType: m_operand->staticType()->itemType()); |
133 | |
134 | return me; |
135 | } |
136 | } |
137 | |
138 | SequenceType::List CastableAs::expectedOperandTypes() const |
139 | { |
140 | SequenceType::List result; |
141 | result.append(t: CommonSequenceTypes::ZeroOrMoreAtomicTypes); |
142 | return result; |
143 | } |
144 | |
145 | SequenceType::Ptr CastableAs::staticType() const |
146 | { |
147 | return CommonSequenceTypes::ExactlyOneBoolean; |
148 | } |
149 | |
150 | ExpressionVisitorResult::Ptr CastableAs::accept(const ExpressionVisitor::Ptr &visitor) const |
151 | { |
152 | return visitor->visit(this); |
153 | } |
154 | |
155 | QT_END_NAMESPACE |
156 | |