Qt 4.8
Classes | Public Types | Public Functions | Private Types | Properties | List of all members
QPatternist::OrderBy Class Reference

Performs the sorting by being a parent to ForClause. More...

#include <qorderby_p.h>

Inheritance diagram for QPatternist::OrderBy:
QPatternist::SingleContainer QPatternist::Expression QSharedData QPatternist::CppCastingHelper< Expression > QPatternist::SourceLocationReflection

Classes

class  OrderSpec
 

Public Types

enum  Stability { StableOrder, UnstableOrder }
 
- 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
 

Public Functions

virtual ExpressionVisitorResult::Ptr accept (const ExpressionVisitor::Ptr &visitor) const
 
virtual Expression::Ptr compress (const StaticContext::Ptr &context)
 
virtual Item::Iterator::Ptr evaluateSequence (const DynamicContext::Ptr &context) const
 
virtual SequenceType::List expectedOperandTypes () const
 
Item::Iterator::Ptr mapToSequence (const Item &i, const DynamicContext::Ptr &context) const
 
 OrderBy (const Stability stability, const OrderSpec::Vector &orderSpecs, const Expression::Ptr &operand, ReturnOrderBy *const returnOrderBy)
 
virtual Properties properties () const
 
virtual SequenceType::Ptr staticType () const
 
virtual Expression::Ptr typeCheck (const StaticContext::Ptr &context, const SequenceType::Ptr &reqType)
 
- Public Functions inherited from QPatternist::SingleContainer
virtual bool compressOperands (const StaticContext::Ptr &)
 
virtual Expression::List operands () const
 
virtual void setOperands (const Expression::List &operands)
 
- Public Functions inherited from QPatternist::Expression
virtual const SourceLocationReflectionactualReflection () const
 Returns this. More...
 
virtual void announceFocusType (const ItemType::Ptr &itemType)
 
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 evaluateSingleton (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
 
virtual ID id () 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 ()
 

Private Types

typedef QExplicitlySharedDataPointer< const OrderByConstPtr
 

Properties

OrderSpec::Vector m_orderSpecs
 
ReturnOrderBy *const m_returnOrderBy
 
const Stability m_stability
 

Additional Inherited Members

- 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::SingleContainer
 SingleContainer (const Expression::Ptr &operand)
 
- 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::SingleContainer
Expression::Ptr m_operand
 

Detailed Description

Performs the sorting by being a parent to ForClause.

The child of the ForClause is a ReturnOrderBy expression, which collects the sort keys and values.

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

Definition at line 76 of file qorderby_p.h.

Typedefs

◆ ConstPtr

Needed when calling makeSequenceMappingIterator().

Definition at line 168 of file qorderby_p.h.

Enumerations

◆ Stability

Enumerator
StableOrder 
UnstableOrder 

Definition at line 79 of file qorderby_p.h.

Constructors and Destructors

◆ OrderBy()

OrderBy::OrderBy ( const Stability  stability,
const OrderSpec::Vector orderSpecs,
const Expression::Ptr operand,
ReturnOrderBy *const  returnOrderBy 
)

Definition at line 58 of file qorderby.cpp.

Referenced by QPatternist::OrderBy::OrderSpec::operatorID().

61  : SingleContainer(op)
62  , m_stability(stability)
63  , m_orderSpecs(aOrderSpecs)
64  , m_returnOrderBy(returnOrderBy)
65 {
67 }
const Stability m_stability
Definition: qorderby_p.h:170
ReturnOrderBy *const m_returnOrderBy
Definition: qorderby_p.h:172
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
SingleContainer(const Expression::Ptr &operand)
OrderSpec::Vector m_orderSpecs
Definition: qorderby_p.h:171

Functions

◆ accept()

ExpressionVisitorResult::Ptr OrderBy::accept ( const ExpressionVisitor::Ptr visitor) const
virtual

Implements QPatternist::Expression.

Definition at line 256 of file qorderby.cpp.

Referenced by QPatternist::OrderBy::OrderSpec::operatorID().

257 {
258  return visitor->visit(this);
259 }

◆ compress()

Expression::Ptr OrderBy::compress ( const StaticContext::Ptr context)
virtual

compress() is the last stage performs in compiling an expression, done after the initial AST build and calling typeCheck(). compress() performs crucial simplifications, either by having drastic performance implications or that some expressions depend on it for proper behavior.

The default implementation performs a sparse conditional constant propagation. In short, a recursive process is performed in the AST which examines if the Expression's operands are constant values, and if so, performs a const fold(AST rewrite) into the result of evaluating the expression in question. This default behavior can be disabled by letting properties() return DisableElimination.

This compress() stage can be relative effective due to the design of XPath, in part because intrinsic functions are heavily used. Many Expressions override compress() and do optimizations specific to what they do. Also, many Expressions performs optimizations in their typeCheck().

Parameters
contextthe static context. Supplies compile time information, and is the channel for communicating error messages.
See also
Wikipedia, the free encyclopedia, Sparse conditional constant propagation
Wikipedia, the free encyclopedia, Intrinsic function
Wikipedia, the free encyclopedia, Compiler optimization

Reimplemented from QPatternist::Expression.

Definition at line 231 of file qorderby.cpp.

Referenced by QPatternist::OrderBy::OrderSpec::operatorID().

232 {
233  /* If we only will produce one item, there's no point in sorting. */
235  return SingleContainer::compress(context);
236  else
237  {
238  m_returnOrderBy->setStay(false);
239  return m_operand->compress(context);
240  }
241 }
ReturnOrderBy *const m_returnOrderBy
Definition: qorderby_p.h:172
virtual Expression::Ptr compress(const StaticContext::Ptr &context)
void setStay(const bool a)
virtual Cardinality cardinality() const =0
virtual SequenceType::Ptr staticType() const =0

◆ evaluateSequence()

Item::Iterator::Ptr OrderBy::evaluateSequence ( const DynamicContext::Ptr context) const
virtual

Evaluate this Expression by iterating over it. This is a central function for evaluating expressions.

Expressions must always always return a valid QAbstractXmlForwardIterator and may never return 0. If an empty result is of interest to be returned, the EmptyIterator should be returned.

The default implementation returns a SingletonIterator over the item returned from evaluateSingleton().

Note
This function may raise an exception when calling, not only when QAbstractXmlForwardIterator::next() is called on the return value. This is because in some cases evaluateSingleton() is called directly.

Reimplemented from QPatternist::Expression.

Definition at line 174 of file qorderby.cpp.

Referenced by QPatternist::OrderBy::OrderSpec::operatorID().

175 {
176  Item::List tuples(m_operand->evaluateSequence(context)->toList());
177 
178  const qLess<Item::List> sorter(m_orderSpecs, context);
179 
181 
182  /* On one hand we could just disregard stability and always use qStableSort(), but maybe qSort()
183  * is a bit faster? */
184  if(m_stability == StableOrder)
185  qStableSort(tuples.begin(), tuples.end(), sorter);
186  else
187  {
189  qSort(tuples.begin(), tuples.end(), sorter);
190  }
191 
192  return makeSequenceMappingIterator<Item>(ConstPtr(this),
193  makeListIterator(tuples),
194  context);
195 }
const Stability m_stability
Definition: qorderby_p.h:170
QAbstractXmlForwardIterator< T >::Ptr makeListIterator(const QList< T > &list)
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
Functor used by Qt&#39;s qSort() and qStableSort(). Used for FLWOR&#39;s order by expression.
Definition: qorderby.cpp:85
QExplicitlySharedDataPointer< const OrderBy > ConstPtr
Definition: qorderby_p.h:168
OrderSpec::Vector m_orderSpecs
Definition: qorderby_p.h:171
void qSort(RandomAccessIterator start, RandomAccessIterator end)
Definition: qalgorithms.h:177
void qStableSort(RandomAccessIterator start, RandomAccessIterator end)
Definition: qalgorithms.h:202
QList< Item > List
Definition: qitem_p.h:195
virtual Item::Iterator::Ptr evaluateSequence(const DynamicContext::Ptr &context) const

◆ expectedOperandTypes()

SequenceType::List OrderBy::expectedOperandTypes ( ) const
virtual

Returns a list of Sequence Types, describing the type of each of the expression's operands. Hence, this function has a relationship to the operands() function:

- The lengths of the lists returned by expectedOperandTypes()
and operands() should always be equal in length, since one
cannot describe the type of a non-existent operand(and all
operands must have type information).
- A significant difference between the two functions is that while
the type of objects in the list returned by operands() may vary
between compilations/static context, simply because the particular
Expression is part of different XPath expressions, the
types in the list returned by expectedOperandTypes is always the same
since the function/operator signature never changes.

This function should not be confused with staticType(), which returns the static type of the expression itself, not its operands. The function call is an expression where this is clear: the type of the return value is not the same as the arguments' types. The static type of the operands supplied to the expression can be determined via the staticType() function of the instances returned by operands().

If the expression has no operands, an empty list should be returned.

Implements QPatternist::Expression.

Definition at line 248 of file qorderby.cpp.

Referenced by QPatternist::OrderBy::OrderSpec::operatorID().

249 {
250  SequenceType::List result;
252  return result;
253 }
static const SequenceType::Ptr ZeroOrMoreItems
void append(const T &t)
Inserts value at the end of the list.
Definition: qlist.h:507
The QList class is a template class that provides lists.
Definition: qdatastream.h:62

◆ mapToSequence()

Item::Iterator::Ptr OrderBy::mapToSequence ( const Item i,
const DynamicContext::Ptr context 
) const
inline

Definition at line 168 of file qorderby.cpp.

Referenced by QPatternist::OrderBy::OrderSpec::operatorID().

170 {
171  return i.as<SortTuple>()->value();
172 }
Represents a value and its sort keys in FLOWR&#39;s order by clause.
Definition: qsorttuple_p.h:81
TCastTarget * as() const
Definition: qitem_p.h:278

◆ properties()

Expression::Properties OrderBy::properties ( ) const
virtual
Returns
a bitwise OR'd value of properties, describing the characteristics of the expression. These properties affects how this Expression is treated in for example type checking stages.

The default implementation returns 0. Override and let the function return a different value, if that's of interest.

An important decision when re-implementing properties() is whether to OR in the properties() of ones operands. For instance, if an operand has RequiresFocus set, that flag nost likely applies to the apparent as well, since it depends on its operand.

See also
deepProperties()
Returns
Expression::None, meaning no special properties

Reimplemented from QPatternist::Expression.

Definition at line 226 of file qorderby.cpp.

Referenced by QPatternist::OrderBy::OrderSpec::operatorID().

◆ staticType()

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

Implements QPatternist::Expression.

Definition at line 243 of file qorderby.cpp.

Referenced by QPatternist::OrderBy::OrderSpec::operatorID().

244 {
245  return m_operand->staticType();
246 }
virtual SequenceType::Ptr staticType() const =0

◆ typeCheck()

Expression::Ptr OrderBy::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::Expression.

Definition at line 197 of file qorderby.cpp.

Referenced by QPatternist::OrderBy::OrderSpec::operatorID().

199 {
200  m_returnOrderBy->setStay(true);
201 
202  /* It's important we do the typeCheck() before calling OrderSpec::prepare(), since
203  * atomizers must first be inserted. */
204  const Expression::Ptr me(SingleContainer::typeCheck(context, reqType));
205 
207  const int len = ops.count();
208  Q_ASSERT(ops.count() > 1);
209  Q_ASSERT(m_orderSpecs.count() == ops.count() - 1);
210 
211  for(int i = 1; i < len; ++i)
212  m_orderSpecs[i - 1].prepare(ops.at(i), context);
213 
214  return me;
215 
216  /* It's not meaningful to sort a single item or less, so rewrite ourselves
217  * away if that is the case. This is an optimization. */
218  /* TODO: How do we remove ReturnOrderBy?
219  if(Cardinality::zeroOrOne().isMatch(m_operand->staticType()->cardinality()))
220  return m_operand->typeCheck(context, reqType);
221  else
222  return SingleContainer::typeCheck(context, reqType);
223  */
224 }
int count(const T &t) const
Returns the number of occurrences of value in the vector.
Definition: qvector.h:742
ReturnOrderBy *const m_returnOrderBy
Definition: qorderby_p.h:172
virtual Expression::Ptr typeCheck(const StaticContext::Ptr &context, const SequenceType::Ptr &reqType)
Definition: qexpression.cpp:70
int count(const T &t) const
Returns the number of occurrences of value in the list.
Definition: qlist.h:891
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
void setStay(const bool a)
virtual Expression::List operands() const
OrderSpec::Vector m_orderSpecs
Definition: qorderby_p.h:171

Properties

◆ m_orderSpecs

OrderSpec::Vector QPatternist::OrderBy::m_orderSpecs
private

◆ m_returnOrderBy

ReturnOrderBy* const QPatternist::OrderBy::m_returnOrderBy
private

Definition at line 172 of file qorderby_p.h.

Referenced by compress(), OrderBy(), and typeCheck().

◆ m_stability

const Stability QPatternist::OrderBy::m_stability
private

Definition at line 170 of file qorderby_p.h.

Referenced by evaluateSequence().


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