Update copyright year in license headers.
[profile/ivi/qtxmlpatterns.git] / src / xmlpatterns / expr / qoptimizerblocks.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
6 **
7 ** This file is part of the QtXmlPatterns module of the Qt Toolkit.
8 **
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 **
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 **
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
29 **
30 ** Other Usage
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include "qcommonnamespaces_p.h"
43
44 #include "qcommonsequencetypes_p.h"
45 #include "qfunctionfactory_p.h"
46 #include "qgeneralcomparison_p.h"
47 #include "qliteral_p.h"
48 #include "qschemanumeric_p.h"
49 #include "qvaluecomparison_p.h"
50
51 #include "qoptimizerblocks_p.h"
52
53 QT_BEGIN_NAMESPACE
54
55 using namespace QPatternist;
56
57 ByIDIdentifier::ByIDIdentifier(const Expression::ID id) : m_id(id)
58 {
59 }
60
61 bool ByIDIdentifier::matches(const Expression::Ptr &expr) const
62 {
63     return expr->is(m_id);
64 }
65
66 ComparisonIdentifier::ComparisonIdentifier(const QVector<Expression::ID> hosts,
67                                            const AtomicComparator::Operator op) : m_hosts(hosts),
68                                                                                   m_op(op)
69 {
70 }
71
72 bool ComparisonIdentifier::matches(const Expression::Ptr &e) const
73 {
74     const Expression::ID eID = e->id();
75
76     if(eID == Expression::IDGeneralComparison)
77     {
78         if(m_hosts.contains(Expression::IDGeneralComparison))
79             return e->as<GeneralComparison>()->operatorID() == m_op;
80         else
81             return false;
82     }
83     else if(eID == Expression::IDValueComparison)
84     {
85         if(m_hosts.contains(Expression::IDValueComparison))
86             return e->as<ValueComparison>()->operatorID() == m_op;
87         else
88             return false;
89     }
90     else
91         return false;
92 }
93
94 BySequenceTypeIdentifier::BySequenceTypeIdentifier(const SequenceType::Ptr &seqType) : m_seqType(seqType)
95 {
96     Q_ASSERT(seqType);
97 }
98
99 bool BySequenceTypeIdentifier::matches(const Expression::Ptr &expr) const
100 {
101     const SequenceType::Ptr t(expr->staticType());
102
103     return m_seqType->itemType()->xdtTypeMatches(t->itemType())
104            &&
105            m_seqType->cardinality().isMatch(t->cardinality());
106 }
107
108 IntegerIdentifier::IntegerIdentifier(const xsInteger num) : m_num(num)
109 {
110 }
111
112 bool IntegerIdentifier::matches(const Expression::Ptr &expr) const
113 {
114     return expr->is(Expression::IDIntegerValue) &&
115            expr->as<Literal>()->item().as<Numeric>()->toInteger() == m_num;
116 }
117
118 BooleanIdentifier::BooleanIdentifier(const bool value) : m_value(value)
119 {
120 }
121
122 bool BooleanIdentifier::matches(const Expression::Ptr &expr) const
123 {
124     return expr->is(Expression::IDBooleanValue) &&
125            expr->evaluateEBV(DynamicContext::Ptr()) == m_value;
126 }
127
128 ByIDCreator::ByIDCreator(const Expression::ID id) : m_id(id)
129 {
130     Q_ASSERT(id != Expression::IDIgnorableExpression);
131 }
132
133 Expression::Ptr ByIDCreator::create(const Expression::List &operands,
134                                     const StaticContext::Ptr &context,
135                                     const SourceLocationReflection *const r) const
136 {
137     return create(m_id, operands, context, r);
138 }
139
140 Expression::Ptr ByIDCreator::create(const Expression::ID id,
141                                     const Expression::List &operands,
142                                     const StaticContext::Ptr &context,
143                                     const SourceLocationReflection *const r)
144 {
145     Q_ASSERT(context);
146
147     QXmlName::LocalNameCode fnName;
148
149     switch(id)
150     {
151         case Expression::IDExistsFN:
152         {
153             fnName = StandardLocalNames::exists;
154             break;
155         }
156         case Expression::IDEmptyFN:
157         {
158             fnName = StandardLocalNames::empty;
159             break;
160         }
161         default:
162         {
163             Q_ASSERT_X(false, Q_FUNC_INFO,
164                        "Cannot create an expression of requested type; m_id is wrong.");
165             return Expression::Ptr();
166         }
167     }
168
169     /* The reason we don't simply do 'new ExistsFN()' ourselves, is that all FunctionCall
170      * instances needs their FunctionSignature in order to function, and the FunctionFactories
171      * sets that. */
172     const QXmlName qName(StandardNamespaces::fn, fnName);
173
174     const Expression::Ptr result(context->functionSignatures()->createFunctionCall(qName, operands, context, r));
175     context->wrapExpressionWith(r, result);
176     return result;
177 }
178
179 QT_END_NAMESPACE