Qt 4.8
qaggregatefns.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
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 Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/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 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 #include "qabstractfloat_p.h"
44 #include "qbuiltintypes_p.h"
45 #include "qcommonsequencetypes_p.h"
46 #include "qcommonvalues_p.h"
47 #include "qdecimal_p.h"
48 #include "qgenericsequencetype_p.h"
49 #include "qinteger_p.h"
50 #include "qoptimizerblocks_p.h"
51 #include "qsequencefns_p.h"
53 
54 #include "qaggregatefns_p.h"
55 
57 
58 using namespace QPatternist;
59 
61 {
62  return Integer::fromValue(m_operands.first()->evaluateSequence(context)->count());
63 }
64 
66  const SequenceType::Ptr &reqType)
67 {
68  if(*CommonSequenceTypes::EBV->itemType() == *reqType->itemType())
69  {
70  return ByIDCreator::create(IDExistsFN, operands(), context, this)->typeCheck(context, reqType);
71  }
72  else
73  return FunctionCall::typeCheck(context, reqType);
74 }
75 
77 {
78  const Expression::Ptr me(FunctionCall::compress(context));
79  if(me != this)
80  return me;
81 
83  if(card.isExactlyOne())
84  return wrapLiteral(CommonValues::IntegerOne, context, this);
85  else if(card.isEmpty())
86  {
87  /* One might think that if the operand is (), that compress() would have
88  * evaluated us and therefore this line never be reached, but "()" can
89  * be combined with the DisableElimination flag. */
90  return wrapLiteral(CommonValues::IntegerZero, context, this);
91  }
92  else if(card.isExact())
93  return wrapLiteral(Integer::fromValue(card.minimum()), context, this);
94  else
95  return me;
96 }
97 
99  const SequenceType::Ptr &reqType)
100 {
101  const Expression::Ptr me(FunctionCall::typeCheck(context, reqType));
103 
104  if(*CommonSequenceTypes::Empty == *t1)
105  return me;
106  else if(*BuiltinTypes::xsAnyAtomicType == *t1 ||
107  *BuiltinTypes::numeric == *t1)
108  return me;
109  else if(BuiltinTypes::xsUntypedAtomic->xdtTypeMatches(t1))
110  {
113  t1 = m_operands.first()->staticType()->itemType();
114  }
115  else if(!BuiltinTypes::numeric->xdtTypeMatches(t1) &&
116  !BuiltinTypes::xsDayTimeDuration->xdtTypeMatches(t1) &&
117  !BuiltinTypes::xsYearMonthDuration->xdtTypeMatches(t1))
118  {
119  /* Translator, don't translate the type names. */
120  context->error(QtXmlPatterns::tr("The first argument to %1 cannot be "
121  "of type %2. It must be a numeric "
122  "type, xs:yearMonthDuration or "
123  "xs:dayTimeDuration.")
124  .arg(formatFunction(context->namePool(), signature()))
125  .arg(formatType(context->namePool(),
126  m_operands.first()->staticType())),
128  }
129 
131  return m_operands.first();
132 
133  /* We know fetchMathematician won't attempt a rewrite of the operand, so this is safe. */
135  AtomicMathematician::Add, true, context,
136  this,
138  return me;
139 }
140 
142 {
144  Item sum(it->next());
145 
146  xsInteger count = 0;
147  while(sum)
148  {
149  ++count;
150  const Item next(it->next());
151  if(!next)
152  break;
153 
155  next, m_adder, context,
156  this,
158  };
159 
160  if(!sum)
161  return Item();
162 
163  /* Note that we use the same m_mather which was used for adding,
164  * can be worth to think about. */
166  Integer::fromValue(count),
167  m_divider, context,
168  this,
170 }
171 
173  const SequenceType::Ptr &reqType)
174 {
175  const Expression::Ptr me(FunctionCall::typeCheck(context, reqType));
177 
178  if(*CommonSequenceTypes::Empty == *t1)
179  return me;
180  else if(*BuiltinTypes::xsAnyAtomicType == *t1 ||
181  *BuiltinTypes::numeric == *t1)
182  return me;
183  else if(BuiltinTypes::xsUntypedAtomic->xdtTypeMatches(t1))
184  {
187  t1 = m_operands.first()->staticType()->itemType();
188  }
189  else if(!BuiltinTypes::numeric->xdtTypeMatches(t1) &&
190  !BuiltinTypes::xsDayTimeDuration->xdtTypeMatches(t1) &&
191  !BuiltinTypes::xsYearMonthDuration->xdtTypeMatches(t1))
192  {
193  /* Translator, don't translate the type names. */
194  context->error(QtXmlPatterns::tr("The first argument to %1 cannot be "
195  "of type %2. It must be of type %3, "
196  "%4, or %5.")
197  .arg(signature())
198  .arg(formatType(context->namePool(), m_operands.first()->staticType()))
199  .arg(formatType(context->namePool(), BuiltinTypes::numeric))
203  }
204 
206  return m_operands.first();
207 
208  /* We use CommonValues::IntegerOne here because it is an arbitrary Expression
209  * of type xs:integer */
210  Expression::Ptr op2(wrapLiteral(CommonValues::IntegerOne, context, this));
212  AtomicMathematician::Add, true, context, this);
214  AtomicMathematician::Div, true, context, this);
215  return me;
216 }
217 
219 {
221  ItemType::Ptr t(opt->itemType());
222 
223  if(BuiltinTypes::xsUntypedAtomic->xdtTypeMatches(t))
224  t = BuiltinTypes::xsDouble; /* xsUntypedAtomics are converted to xsDouble. */
225  else if(BuiltinTypes::xsInteger->xdtTypeMatches(t))
227 
228  /* else, it means the type is xsDayTimeDuration, xsYearMonthDuration,
229  * xsDouble, xsFloat or xsAnyAtomicType, which we use as is. */
230  return makeGenericSequenceType(BuiltinTypes::xsAnyAtomicType->xdtTypeMatches(t) ? t : ItemType::Ptr(BuiltinTypes::xsAnyAtomicType),
231  opt->cardinality().toWithoutMany());
232 }
233 
235 {
237  Item sum(it->next());
238 
239  while(sum)
240  {
241  const Item next(it->next());
242  if(!next)
243  break;
244 
246  next, m_mather, context, this,
248  };
249 
250  if(!sum)
251  {
252  if(m_operands.count() == 1)
254  else
255  return m_operands.last()->evaluateSingleton(context);
256  }
257 
258  return sum;
259 }
260 
262  const SequenceType::Ptr &reqType)
263 {
264  const Expression::Ptr me(AddingAggregate::typeCheck(context, reqType));
265 
267  {
268  if(m_operands.count() == 1)
269  return wrapLiteral(CommonValues::IntegerZero, context, this);
270  else
271  return m_operands.at(1);
272  }
273 
274  if(m_operands.count() == 1)
275  return me;
276 
277  const ItemType::Ptr t(m_operands.at(1)->staticType()->itemType());
278 
279  if(!BuiltinTypes::numeric->xdtTypeMatches(t) &&
280  !BuiltinTypes::xsAnyAtomicType->xdtTypeMatches(t) &&
282  !BuiltinTypes::xsDayTimeDuration->xdtTypeMatches(t) &&
283  !BuiltinTypes::xsYearMonthDuration->xdtTypeMatches(t))
284  {
285  context->error(QtXmlPatterns::tr("The second argument to %1 cannot be "
286  "of type %2. It must be of type %3, "
287  "%4, or %5.")
288  .arg(formatFunction(context->namePool(), signature()))
289  .arg(formatType(context->namePool(), m_operands.at(1)->staticType()))
290  .arg(formatType(context->namePool(), BuiltinTypes::numeric))
294  return me;
295  }
296 
297  return me;
298 }
299 
301 {
303 
304  if(m_operands.count() == 1)
305  {
306  return makeGenericSequenceType(t->itemType() | BuiltinTypes::xsInteger,
308  }
309  else
310  {
311  return makeGenericSequenceType(t->itemType() | m_operands.at(1)->staticType()->itemType(),
312  t->cardinality().toWithoutMany());
313  }
314 }
315 
virtual Expression::Ptr typeCheck(const StaticContext::Ptr &context, const SequenceType::Ptr &reqType)
virtual Expression::Ptr compress(const StaticContext::Ptr &context)
static const AtomicType::Ptr xsDayTimeDuration
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
#define it(className, varName)
qint64 xsInteger
QString formatType(const NamePool::Ptr &np, const T &type)
Formats ItemType and SequenceType.
virtual NamePool::Ptr namePool() const =0
static const Item IntegerOne
virtual Expression::Ptr typeCheck(const StaticContext::Ptr &context, const SequenceType::Ptr &reqType)
Definition: qexpression.cpp:70
virtual Expression::Ptr typeCheck(const StaticContext::Ptr &context, const SequenceType::Ptr &reqType)
int count(const T &t) const
Returns the number of occurrences of value in the list.
Definition: qlist.h:891
virtual Expression::Ptr typeCheck(const StaticContext::Ptr &context, const SequenceType::Ptr &reqType)
static const AtomicType::Ptr xsYearMonthDuration
static const Item IntegerZero
static Item fromValue(const xsInteger num)
Definition: qinteger.cpp:52
virtual Item evaluateSingleton(const DynamicContext::Ptr &context) const
virtual Expression::Ptr compress(const StaticContext::Ptr &context)
virtual SequenceType::Ptr staticType() const
virtual Item evaluateSingleton(const DynamicContext::Ptr &context) const
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
void error(const QString &message, const ReportContext::ErrorCode errorCode, const QSourceLocation &sourceLocation)
static QString formatFunction(const NamePool::Ptr &np, const FunctionSignature::Ptr &func)
Formats FunctionSignature.
virtual Expression::List operands() const
const T & at(int i) const
Returns the item at index position i in the list.
Definition: qlist.h:468
The namespace for the internal API of QtXmlPatterns.
virtual Expression::Ptr typeCheck(const StaticContext::Ptr &context, const SequenceType::Ptr &reqType)
static const AtomicType::Ptr xsDouble
virtual FunctionSignature::Ptr signature() const
virtual Cardinality cardinality() const =0
void replace(int i, const T &t)
Replaces the item at index position i with value.
Definition: qlist.h:609
Contains classes implementing the functions found in XQuery 1.0 and XPath 2.0 Functions and Operators...
static const AtomicType::Ptr xsUntypedAtomic
T & first()
Returns a reference to the first item in the list.
Definition: qlist.h:282
static AtomicMathematician::Ptr fetchMathematician(Expression::Ptr &t1, Expression::Ptr &t2, const AtomicMathematician::Operator op, const bool issueError, const ReportContext::Ptr &context, const SourceLocationReflection *const reflection, const ReportContext::ErrorCode code=ReportContext::XPTY0004, const bool isCompat=false)
virtual SequenceType::Ptr staticType() const
virtual SequenceType::Ptr staticType() const =0
static const EmptySequenceType::Ptr Empty
virtual Expression::Ptr typeCheck(const StaticContext::Ptr &context, const SequenceType::Ptr &reqType)
Represents an item in the XPath 2.0 Data Model.
Definition: qitem_p.h:182
T & last()
Returns a reference to the last item in the list.
Definition: qlist.h:284
static const AtomicType::Ptr numeric
static const AtomicType::Ptr xsInteger
static Cardinality exactlyOne()
Casts every item in a sequence obtained from evaluating an Expression, to a requested atomic type...
static const SequenceType::Ptr EBV
static const AtomicType::Ptr xsDecimal
virtual Expression::Ptr create(const Expression::List &operands, const StaticContext::Ptr &context, const SourceLocationReflection *const r) const
static const AtomicType::Ptr xsAnyAtomicType
Represents a cardinality, a possible , often represented by occurrence indicators.
virtual ItemType::Ptr itemType() const =0
static Item flexiblyCalculate(const Item &op1, const AtomicMathematician::Operator op, const Item &op2, const AtomicMathematician::Ptr &mather, const DynamicContext::Ptr &context, const SourceLocationReflection *const reflection, const ReportContext::ErrorCode code=ReportContext::XPTY0004, const bool isCompat=false)
virtual Item evaluateSingleton(const DynamicContext::Ptr &context) const
Contains classes implementing the functions found in XQuery 1.0 and XPath 2.0 Functions and Operators...
virtual Item::Iterator::Ptr evaluateSequence(const DynamicContext::Ptr &context) const
virtual Item evaluateSingleton(const DynamicContext::Ptr &context) const