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 "qcommonnamespaces_p.h"
41
42#include "qcommonsequencetypes_p.h"
43#include "qfunctionfactory_p.h"
44#include "qgeneralcomparison_p.h"
45#include "qliteral_p.h"
46#include "qschemanumeric_p.h"
47#include "qvaluecomparison_p.h"
48
49#include "qoptimizerblocks_p.h"
50
51QT_BEGIN_NAMESPACE
52
53using namespace QPatternist;
54
55ByIDIdentifier::ByIDIdentifier(const Expression::ID id) : m_id(id)
56{
57}
58
59bool ByIDIdentifier::matches(const Expression::Ptr &expr) const
60{
61 return expr->is(i: m_id);
62}
63
64ComparisonIdentifier::ComparisonIdentifier(const QVector<Expression::ID> hosts,
65 const AtomicComparator::Operator op) : m_hosts(hosts),
66 m_op(op)
67{
68}
69
70bool ComparisonIdentifier::matches(const Expression::Ptr &e) const
71{
72 const Expression::ID eID = e->id();
73
74 if(eID == Expression::IDGeneralComparison)
75 {
76 if(m_hosts.contains(t: Expression::IDGeneralComparison))
77 return e->as<GeneralComparison>()->operatorID() == m_op;
78 else
79 return false;
80 }
81 else if(eID == Expression::IDValueComparison)
82 {
83 if(m_hosts.contains(t: Expression::IDValueComparison))
84 return e->as<ValueComparison>()->operatorID() == m_op;
85 else
86 return false;
87 }
88 else
89 return false;
90}
91
92BySequenceTypeIdentifier::BySequenceTypeIdentifier(const SequenceType::Ptr &seqType) : m_seqType(seqType)
93{
94 Q_ASSERT(seqType);
95}
96
97bool BySequenceTypeIdentifier::matches(const Expression::Ptr &expr) const
98{
99 const SequenceType::Ptr t(expr->staticType());
100
101 return m_seqType->itemType()->xdtTypeMatches(other: t->itemType())
102 &&
103 m_seqType->cardinality().isMatch(other: t->cardinality());
104}
105
106IntegerIdentifier::IntegerIdentifier(const xsInteger num) : m_num(num)
107{
108}
109
110bool IntegerIdentifier::matches(const Expression::Ptr &expr) const
111{
112 return expr->is(i: Expression::IDIntegerValue) &&
113 expr->as<Literal>()->item().as<Numeric>()->toInteger() == m_num;
114}
115
116BooleanIdentifier::BooleanIdentifier(const bool value) : m_value(value)
117{
118}
119
120bool BooleanIdentifier::matches(const Expression::Ptr &expr) const
121{
122 return expr->is(i: Expression::IDBooleanValue) &&
123 expr->evaluateEBV(context: DynamicContext::Ptr()) == m_value;
124}
125
126ByIDCreator::ByIDCreator(const Expression::ID id) : m_id(id)
127{
128 Q_ASSERT(id != Expression::IDIgnorableExpression);
129}
130
131Expression::Ptr ByIDCreator::create(const Expression::List &operands,
132 const StaticContext::Ptr &context,
133 const SourceLocationReflection *const r) const
134{
135 return create(id: m_id, operands, context, r);
136}
137
138Expression::Ptr ByIDCreator::create(const Expression::ID id,
139 const Expression::List &operands,
140 const StaticContext::Ptr &context,
141 const SourceLocationReflection *const r)
142{
143 Q_ASSERT(context);
144
145 QXmlName::LocalNameCode fnName;
146
147 switch(id)
148 {
149 case Expression::IDExistsFN:
150 {
151 fnName = StandardLocalNames::exists;
152 break;
153 }
154 case Expression::IDEmptyFN:
155 {
156 fnName = StandardLocalNames::empty;
157 break;
158 }
159 default:
160 {
161 Q_ASSERT_X(false, Q_FUNC_INFO,
162 "Cannot create an expression of requested type; m_id is wrong.");
163 return Expression::Ptr();
164 }
165 }
166
167 /* The reason we don't simply do 'new ExistsFN()' ourselves, is that all FunctionCall
168 * instances needs their FunctionSignature in order to function, and the FunctionFactories
169 * sets that. */
170 const QXmlName qName(StandardNamespaces::fn, fnName);
171
172 const Expression::Ptr result(context->functionSignatures()->createFunctionCall(name: qName, arguments: operands, context, r));
173 context->wrapExpressionWith(existingNode: r, newNode: result);
174 return result;
175}
176
177QT_END_NAMESPACE
178

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