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 "qboolean_p.h"
42#include "qbuiltintypes_p.h"
43#include "qcommonsequencetypes_p.h"
44#include "qemptysequence_p.h"
45#include "qoptimizationpasses_p.h"
46
47#include "qvaluecomparison_p.h"
48
49QT_BEGIN_NAMESPACE
50
51using namespace QPatternist;
52
53ValueComparison::ValueComparison(const Expression::Ptr &op1,
54 const AtomicComparator::Operator op,
55 const Expression::Ptr &op2) : PairContainer(op1, op2),
56 m_operator(op)
57{
58}
59
60/*! \internal */
61ValueComparison::~ValueComparison()
62{
63}
64
65Item ValueComparison::evaluateSingleton(const DynamicContext::Ptr &context) const
66{
67 const Item it1(m_operand1->evaluateSingleton(context));
68 if(!it1)
69 return Item();
70
71 const Item it2(m_operand2->evaluateSingleton(context));
72 if(!it2)
73 return Item();
74
75 return Boolean::fromValue(value: flexibleCompare(it1, it2, context));
76}
77
78Expression::Ptr ValueComparison::typeCheck(const StaticContext::Ptr &context,
79 const SequenceType::Ptr &reqType)
80{
81 const Expression::Ptr me(PairContainer::typeCheck(context, reqType));
82 const ItemType::Ptr t1(m_operand1->staticType()->itemType());
83 const ItemType::Ptr t2(m_operand2->staticType()->itemType());
84 Q_ASSERT(t1);
85 Q_ASSERT(t2);
86
87 if(*CommonSequenceTypes::Empty == *t1 ||
88 *CommonSequenceTypes::Empty == *t2)
89 {
90 return EmptySequence::create(replacementFor: this, context);
91 }
92 else
93 {
94 prepareComparison(c: fetchComparator(t1, t2, context));
95
96 return me;
97 }
98}
99
100Expression::Ptr ValueComparison::compress(const StaticContext::Ptr &context)
101{
102 const Expression::Ptr me(PairContainer::compress(context));
103
104 if(me != this)
105 return me;
106
107 if(isCaseInsensitiveCompare(op1&: m_operand1, op2&: m_operand2))
108 useCaseInsensitiveComparator();
109
110 return me;
111}
112
113bool ValueComparison::isCaseInsensitiveCompare(Expression::Ptr &op1, Expression::Ptr &op2)
114{
115 Q_ASSERT(op1);
116 Q_ASSERT(op2);
117
118 const ID iD = op1->id();
119
120 if((iD == IDLowerCaseFN || iD == IDUpperCaseFN) && iD == op2->id())
121 {
122 /* Both are either fn:lower-case() or fn:upper-case(). */
123
124 /* Replace the calls to the functions with its operands. */
125 op1 = op1->operands().first();
126 op2 = op2->operands().first();
127
128 return true;
129 }
130 else
131 return false;
132}
133
134OptimizationPass::List ValueComparison::optimizationPasses() const
135{
136 return OptimizationPasses::comparisonPasses;
137}
138
139SequenceType::List ValueComparison::expectedOperandTypes() const
140{
141 SequenceType::List result;
142 result.append(t: CommonSequenceTypes::ZeroOrOneAtomicType);
143 result.append(t: CommonSequenceTypes::ZeroOrOneAtomicType);
144 return result;
145}
146
147SequenceType::Ptr ValueComparison::staticType() const
148{
149 if(m_operand1->staticType()->cardinality().allowsEmpty() ||
150 m_operand2->staticType()->cardinality().allowsEmpty())
151 return CommonSequenceTypes::ZeroOrOneBoolean;
152 else
153 return CommonSequenceTypes::ExactlyOneBoolean;
154}
155
156ExpressionVisitorResult::Ptr ValueComparison::accept(const ExpressionVisitor::Ptr &visitor) const
157{
158 return visitor->visit(this);
159}
160
161Expression::ID ValueComparison::id() const
162{
163 return IDValueComparison;
164}
165
166QT_END_NAMESPACE
167

source code of qtxmlpatterns/src/xmlpatterns/expr/qvaluecomparison.cpp