Qt 4.8
Public Functions | Properties | List of all members
QPatternist::AvgFN Class Reference

Implements the function fn:avg(). More...

#include <qaggregatefns_p.h>

Inheritance diagram for QPatternist::AvgFN:
QPatternist::AddingAggregate QPatternist::FunctionCall QPatternist::UnlimitedContainer QPatternist::Expression QSharedData QPatternist::CppCastingHelper< Expression > QPatternist::SourceLocationReflection

Public Functions

virtual Item evaluateSingleton (const DynamicContext::Ptr &context) const
 
virtual SequenceType::Ptr staticType () const
 
virtual Expression::Ptr typeCheck (const StaticContext::Ptr &context, const SequenceType::Ptr &reqType)
 
- Public Functions inherited from QPatternist::FunctionCall
virtual ExpressionVisitorResult::Ptr accept (const ExpressionVisitor::Ptr &visitor) const
 
virtual SequenceType::List expectedOperandTypes () const
 
virtual ID id () const
 
virtual Expression::Properties properties () const
 
virtual void setSignature (const FunctionSignature::Ptr &sign)
 
virtual FunctionSignature::Ptr signature () const
 
- Public Functions inherited from QPatternist::UnlimitedContainer
virtual bool compressOperands (const StaticContext::Ptr &)
 
virtual Expression::List operands () const
 
virtual void setOperands (const Expression::List &list)
 
 UnlimitedContainer (const Expression::List &operands=Expression::List())
 
- Public Functions inherited from QPatternist::Expression
virtual const SourceLocationReflectionactualReflection () const
 Returns this. More...
 
virtual void announceFocusType (const ItemType::Ptr &itemType)
 
virtual Expression::Ptr compress (const StaticContext::Ptr &context)
 
Properties deepProperties () const
 Computes the union of properties for this Expression and all its descending children. More...
 
virtual Properties dependencies () const
 
virtual QString description () const
 
virtual bool evaluateEBV (const DynamicContext::Ptr &context) const
 
virtual Item::Iterator::Ptr evaluateSequence (const DynamicContext::Ptr &context) const
 
virtual void evaluateToSequenceReceiver (const DynamicContext::Ptr &context) const
 
virtual ItemType::Ptr expectedContextItemType () const
 
 Expression ()
 
bool has (const Property prop) const
 
bool hasDependency (const Property prop) const
 
bool is (const ID id) const
 
bool isEvaluated () const
 
virtual ItemType::Ptr newFocusType () const
 
virtual QList< QExplicitlySharedDataPointer< OptimizationPass > > optimizationPasses () const
 
virtual PatternPriority patternPriority () const
 
const Expression::Ptrrewrite (const Expression::Ptr &to, const StaticContext::Ptr &context) const
 Rewrites this Expression to to, and return to. More...
 
virtual ~Expression ()
 
- Public Functions inherited from QSharedData
 QSharedData ()
 Constructs a QSharedData object with a reference count of 0. More...
 
 QSharedData (const QSharedData &)
 Constructs a QSharedData object with reference count 0. More...
 
- Public Functions inherited from QPatternist::CppCastingHelper< Expression >
const TCastTarget * as () const
 
TCastTarget * as ()
 
- Public Functions inherited from QPatternist::SourceLocationReflection
virtual QSourceLocation sourceLocation () const
 
 SourceLocationReflection ()
 
virtual ~SourceLocationReflection ()
 

Properties

AtomicMathematician::Ptr m_adder
 
AtomicMathematician::Ptr m_divider
 

Additional Inherited Members

- Public Types inherited from QPatternist::FunctionCall
typedef QExplicitlySharedDataPointer< FunctionCallPtr
 
- Public Types inherited from QPatternist::UnlimitedContainer
enum  CardinalityComputation { ProductOfCardinality, UnionOfCardinality }
 
- Public Types inherited from QPatternist::Expression
typedef QExplicitlySharedDataPointer< const ExpressionConstPtr
 A smart pointer wrapping const Expression instances. More...
 
enum  ID {
  IDBooleanValue = 1, IDCountFN, IDEmptyFN, IDExistsFN,
  IDExpressionSequence, IDGeneralComparison, IDIfThenClause, IDIgnorableExpression,
  IDIntegerValue, IDPositionFN, IDStringValue, IDValueComparison,
  IDRangeVariableReference, IDContextItem, IDUserFunctionCallsite, IDExpressionVariableReference,
  IDAttributeConstructor, IDUpperCaseFN, IDLowerCaseFN, IDFirstItemPredicate,
  IDEmptySequence, IDReturnOrderBy, IDLetClause, IDForClause,
  IDPath, IDNamespaceConstructor, IDArgumentReference, IDGenericPredicate,
  IDAxisStep, IDFloat, IDCombineNodes, IDUnresolvedVariableReference,
  IDCardinalityVerifier
}
 
typedef QList< Expression::PtrList
 
typedef QFlags< PropertyProperties
 
enum  Property {
  UseContextItem = 1, DisableElimination = 1 << 1, IsEvaluated = 1 << 2, DisableTypingDeduction = 1 << 3,
  EmptynessFollowsChild = 1 << 4, RewriteToEmptyOnEmpty = 1 << 5, RequiresFocus = 1 << 6, AffectsOrderOnly = 1 << 7,
  RequiresContextItem = (1 << 8) | RequiresFocus, CreatesFocusForLast = 1 << 9, LastOperandIsCollation = 1 << 10, DependsOnLocalVariable = (1 << 11) | DisableElimination,
  EvaluationCacheRedundant = (1 << 12), IsNodeConstructor = 1 << 13, RequiresCurrentItem = 1 << 14
}
 
typedef QExplicitlySharedDataPointer< ExpressionPtr
 A smart pointer wrapping mutable Expression instances. More...
 
typedef ::QAbstractXmlForwardIterator< Expression::PtrQAbstractXmlForwardIterator
 
typedef QVector< Expression::PtrVector
 
- Static Public Functions inherited from QPatternist::Expression
static void rewrite (Expression::Ptr &old, const Expression::Ptr &New, const StaticContext::Ptr &context)
 
- Public Variables inherited from QSharedData
QAtomicInt ref
 
- Protected Functions inherited from QPatternist::UnlimitedContainer
template<CardinalityComputation suppliedCard>
SequenceType::Ptr operandsUnionType () const
 
- Protected Functions inherited from QPatternist::Expression
void typeCheckOperands (const StaticContext::Ptr &context)
 
- Protected Functions inherited from QPatternist::CppCastingHelper< Expression >
 CppCastingHelper ()
 
- Protected Variables inherited from QPatternist::AddingAggregate
AtomicMathematician::Ptr m_mather
 
- Protected Variables inherited from QPatternist::UnlimitedContainer
Expression::List m_operands
 

Detailed Description

Implements the function fn:avg().

Author
Frans Englich frans.nosp@m..eng.nosp@m.lich@.nosp@m.noki.nosp@m.a.com

Definition at line 123 of file qaggregatefns_p.h.

Functions

◆ evaluateSingleton()

Item AvgFN::evaluateSingleton ( const DynamicContext::Ptr context) const
virtual

Reimplemented from QPatternist::Expression.

Definition at line 141 of file qaggregatefns.cpp.

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 }
#define it(className, varName)
qint64 xsInteger
The QExplicitlySharedDataPointer class represents a pointer to an explicitly shared object...
Definition: qshareddata.h:136
static Item fromValue(const xsInteger num)
Definition: qinteger.cpp:52
AtomicMathematician::Ptr m_divider
AtomicMathematician::Ptr m_adder
T & first()
Returns a reference to the first item in the list.
Definition: qlist.h:282
Represents an item in the XPath 2.0 Data Model.
Definition: qitem_p.h:182
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::Iterator::Ptr evaluateSequence(const DynamicContext::Ptr &context) const

◆ staticType()

SequenceType::Ptr AvgFN::staticType ( ) const
virtual
Returns
the static type of this Expression. For example, an 'and' expression have as static type xs:boolean

Reimplemented from QPatternist::FunctionCall.

Definition at line 218 of file qaggregatefns.cpp.

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 }
static const AtomicType::Ptr xsDouble
static const AtomicType::Ptr xsUntypedAtomic
T & first()
Returns a reference to the first item in the list.
Definition: qlist.h:282
virtual SequenceType::Ptr staticType() const =0
static const AtomicType::Ptr xsInteger
static const AtomicType::Ptr xsDecimal
static const AtomicType::Ptr xsAnyAtomicType

◆ typeCheck()

Expression::Ptr AvgFN::typeCheck ( const StaticContext::Ptr context,
const SequenceType::Ptr reqType 
)
virtual

This implementation guarantees to never rewrite away this Expression, but at most rewrite it as a child of another expression(that presumably have a type checking role). It is therefore always safe to override this function and call this implementation and not worry about that this Expression becomes deleted.

Many Expressions override typeCheck() and performs optimizations, as opposed to doing it in the compress() stage. This is due to that the design of those Expressions often are tied to that certain simplifications are done at the typeCheck() stage of the compilation process or that it in some other way is related to what the typeCheck() do. Also, the earlier the AST can be simplified, the better the chances are for subsequent optimizations.

It is important that the super class's typeCheck() is called before doing any custom type checking, since the call can change the children(notably, the childrens' static types). For example, if the Expression, MyExpression in the example, does not match the required type, typeCheck returns the Expression wrapped in for example ItemVerifier, CardinalityVerifier, or both.

typeCheck() may be called many times. typeCheck() must either raise an error if this Expression is an invalid expression. Thus, it is guaranteed that an Expression is valid after typeCheck() is called.

Parameters
contextsupplies information, such as namespace bindings and available function signatures, that can be needed at compilation time. context is guaranteed by the caller to never null.
reqTypethe static type that this Expression must match when evaluated. reqType is guaranteed by the caller to never null.
Returns
an Expression that can be this Expression, or another expression, which somehow is necessary for making this Expression conforming to reqType

Reimplemented from QPatternist::AddingAggregate.

Definition at line 172 of file qaggregatefns.cpp.

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 }
virtual Expression::Ptr typeCheck(const StaticContext::Ptr &context, const SequenceType::Ptr &reqType)
static const AtomicType::Ptr xsDayTimeDuration
QString formatType(const NamePool::Ptr &np, const T &type)
Formats ItemType and SequenceType.
virtual NamePool::Ptr namePool() const =0
static const Item IntegerOne
static const AtomicType::Ptr xsYearMonthDuration
AtomicMathematician::Ptr m_divider
AtomicMathematician::Ptr m_adder
void error(const QString &message, const ReportContext::ErrorCode errorCode, const QSourceLocation &sourceLocation)
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
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 =0
static const EmptySequenceType::Ptr Empty
static const AtomicType::Ptr numeric
Casts every item in a sequence obtained from evaluating an Expression, to a requested atomic type...
static const AtomicType::Ptr xsAnyAtomicType
virtual ItemType::Ptr itemType() const =0

Properties

◆ m_adder

AtomicMathematician::Ptr QPatternist::AvgFN::m_adder
private

Definition at line 132 of file qaggregatefns_p.h.

◆ m_divider

AtomicMathematician::Ptr QPatternist::AvgFN::m_divider
private

Definition at line 133 of file qaggregatefns_p.h.


The documentation for this class was generated from the following files: