source: trunk/src/xmlpatterns/expr/qgenericpredicate.cpp@ 243

Last change on this file since 243 was 2, checked in by Dmitry A. Kuminov, 16 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 8.1 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** Contact: Qt Software Information ([email protected])
5**
6** This file is part of the QtXmlPatterns module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial Usage
10** Licensees holding valid Qt Commercial licenses may use this file in
11** accordance with the Qt Commercial License Agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and Nokia.
14**
15** GNU Lesser General Public License Usage
16** Alternatively, this file may be used under the terms of the GNU Lesser
17** General Public License version 2.1 as published by the Free Software
18** Foundation and appearing in the file LICENSE.LGPL included in the
19** packaging of this file. Please review the following information to
20** ensure the GNU Lesser General Public License version 2.1 requirements
21** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
22**
23** In addition, as a special exception, Nokia gives you certain
24** additional rights. These rights are described in the Nokia Qt LGPL
25** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
26** package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you are unsure which license is appropriate for your use, please
37** contact the sales department at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include "qabstractfloat_p.h"
43#include "qboolean_p.h"
44#include "qbuiltintypes_p.h"
45#include "qcommonsequencetypes_p.h"
46#include "qemptysequence_p.h"
47#include "qfirstitempredicate_p.h"
48#include "qgenericsequencetype_p.h"
49#include "qitemmappingiterator_p.h"
50#include "qliteral_p.h"
51#include "qpatternistlocale_p.h"
52#include "qtruthpredicate_p.h"
53
54#include "qgenericpredicate_p.h"
55
56QT_BEGIN_NAMESPACE
57
58using namespace QPatternist;
59
60GenericPredicate::GenericPredicate(const Expression::Ptr &sourceExpression,
61 const Expression::Ptr &predicate) : PairContainer(sourceExpression,
62 predicate)
63{
64}
65
66Expression::Ptr GenericPredicate::create(const Expression::Ptr &sourceExpression,
67 const Expression::Ptr &predicateExpression,
68 const StaticContext::Ptr &context,
69 const QSourceLocation &location)
70{
71 Q_ASSERT(sourceExpression);
72 Q_ASSERT(predicateExpression);
73 Q_ASSERT(context);
74 const ItemType::Ptr type(predicateExpression->staticType()->itemType());
75
76 if(predicateExpression->is(IDIntegerValue) &&
77 predicateExpression->as<Literal>()->item().as<Numeric>()->toInteger() == 1)
78 { /* Handle [1] */
79 return createFirstItem(sourceExpression);
80 }
81 else if(BuiltinTypes::numeric->xdtTypeMatches(type))
82 { /* A numeric predicate, other than [1]. */
83 /* TODO at somepoint we'll return a specialized expr here, NumericPredicate or so.
84 * Dependency analysis is a bit tricky, since the contained expression can depend on
85 * some loop component. */
86 return Expression::Ptr(new GenericPredicate(sourceExpression, predicateExpression));
87 }
88 else if(*CommonSequenceTypes::Empty == *type)
89 {
90 return EmptySequence::create(predicateExpression.data(), context);
91 }
92 else if(*BuiltinTypes::item == *type ||
93 *BuiltinTypes::xsAnyAtomicType == *type)
94 {
95 /* The type couldn't be narrowed at compile time, so we use
96 * a generic predicate. This check is before the CommonSequenceTypes::EBV check,
97 * because the latter matches these types as well. */
98 return Expression::Ptr(new GenericPredicate(sourceExpression, predicateExpression));
99 }
100 else if(CommonSequenceTypes::EBV->itemType()->xdtTypeMatches(type))
101 {
102 return Expression::Ptr(new TruthPredicate(sourceExpression, predicateExpression));
103 }
104 else
105 {
106 context->error(QtXmlPatterns::tr("A value of type %1 cannot be a "
107 "predicate. A predicate must have "
108 "either a numeric type or an "
109 "Effective Boolean Value type.")
110 .arg(formatType(context->namePool(),
111 sourceExpression->staticType())),
112 ReportContext::FORG0006, location);
113 return Expression::Ptr(); /* Silence compiler warning. */
114 }
115}
116
117Expression::Ptr GenericPredicate::createFirstItem(const Expression::Ptr &sourceExpression)
118
119{
120 return Expression::Ptr(new FirstItemPredicate(sourceExpression));
121}
122
123Item GenericPredicate::mapToItem(const Item &item,
124 const DynamicContext::Ptr &context) const
125{
126 const Item::Iterator::Ptr it(m_operand2->evaluateSequence(context));
127 const Item pcateItem(it->next());