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 "qbuiltintypes_p.h"
41#include "qcommonsequencetypes_p.h"
42#include "qcommonvalues_p.h"
43#include "qliteral_p.h"
44#include "qschemanumeric_p.h"
45
46#include "qdeepequalfn_p.h"
47
48QT_BEGIN_NAMESPACE
49
50using namespace QPatternist;
51
52bool DeepEqualFN::evaluateEBV(const DynamicContext::Ptr &context) const
53{
54 const Item::Iterator::Ptr it1(m_operands.first()->evaluateSequence(context));
55 const Item::Iterator::Ptr it2(m_operands.at(i: 1)->evaluateSequence(context));
56
57 while(true)
58 {
59 const Item item1(it1->next());
60 const Item item2(it2->next());
61
62 if(!item1)
63 {
64 if(item2)
65 return false;
66 else
67 return true;
68 }
69 else if(!item2)
70 {
71 if(item1)
72 return false;
73 else
74 return true;
75 }
76 else if(item1.isNode())
77 {
78 if(item2.isNode())
79 {
80 if(item1.asNode().isDeepEqual(other: item2.asNode()))
81 continue;
82 else
83 return false;
84 }
85 else
86 return false;
87 }
88 else if(item2.isNode())
89 {
90 /* We know that item1 is not a node due to the check above. */
91 return false;
92 }
93 else if(flexibleCompare(it1: item1, it2: item2, context))
94 continue;
95 else if(BuiltinTypes::numeric->itemMatches(item: item1) &&
96 item1.as<Numeric>()->isNaN() &&
97 item2.as<Numeric>()->isNaN())
98 {
99 // TODO
100 /* Handle the specific NaN circumstances. item2 isn't checked whether it's of
101 * type numeric, since the AtomicComparator lookup would have failed if both weren't
102 * numeric. */
103 continue;
104 }
105 else
106 return false;
107 };
108}
109
110Expression::Ptr DeepEqualFN::typeCheck(const StaticContext::Ptr &context,
111 const SequenceType::Ptr &reqType)
112{
113 const Expression::Ptr me(FunctionCall::typeCheck(context, reqType));
114 const ItemType::Ptr t1(m_operands.first()->staticType()->itemType());
115 const ItemType::Ptr t2(m_operands.at(i: 1)->staticType()->itemType());
116 /* TODO This can be much more improved, and the optimizations should be moved
117 * to compress(). */
118
119 if(*CommonSequenceTypes::Empty == *t1)
120 {
121 if(*CommonSequenceTypes::Empty == *t2)
122 return wrapLiteral(item: CommonValues::BooleanTrue, context, r: this);
123 else
124 return me;
125 }
126 else if(*CommonSequenceTypes::Empty == *t2)
127 {
128 if(*CommonSequenceTypes::Empty == *t1)
129 return wrapLiteral(item: CommonValues::BooleanTrue, context, r: this);
130 else
131 return me;
132 }
133 else if(BuiltinTypes::node->xdtTypeMatches(other: t1) &&
134 BuiltinTypes::node->xdtTypeMatches(other: t2))
135 return me; /* We're comparing nodes. */
136 else if(BuiltinTypes::xsAnyAtomicType->xdtTypeMatches(other: t1) &&
137 BuiltinTypes::xsAnyAtomicType->xdtTypeMatches(other: t2))
138 {
139 prepareComparison(c: fetchComparator(t1, t2, context));
140 return me;
141 }
142 else
143 {
144 if ((BuiltinTypes::node->xdtTypeMatches(other: t1) && BuiltinTypes::xsAnyAtomicType->xdtTypeMatches(other: t2))
145 || (BuiltinTypes::node->xdtTypeMatches(other: t2) && BuiltinTypes::xsAnyAtomicType->xdtTypeMatches(other: t1)))
146 {
147 /* One operand contains nodes and the other atomic values, or vice versa. They can never
148 * be identical. */
149 // TODO warn?
150 return wrapLiteral(item: CommonValues::BooleanFalse, context, r: this);
151 }
152 else
153 {
154 // TODO Warn?
155 return me;
156 }
157 }
158}
159
160QT_END_NAMESPACE
161

source code of qtxmlpatterns/src/xmlpatterns/functions/qdeepequalfn.cpp