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

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

#include <qsequencefns_p.h>

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

Public Functions

virtual Expression::Ptr compress (const StaticContext::Ptr &context)
 
virtual Item::Iterator::Ptr evaluateSequence (const DynamicContext::Ptr &context) const
 
virtual Item evaluateSingleton (const DynamicContext::Ptr &context) const
 
virtual SequenceType::Ptr staticType () const
 
 SubsequenceFN ()
 
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)
 
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 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

bool m_hasTypeChecked
 

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::UnlimitedContainer
Expression::List m_operands
 

Detailed Description

Implements the function fn:subsequence().

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

Definition at line 310 of file qsequencefns_p.h.

Constructors and Destructors

◆ SubsequenceFN()

SubsequenceFN::SubsequenceFN ( )

Definition at line 216 of file qsequencefns.cpp.

216  : m_hasTypeChecked(false)
217 {
218 }

Functions

◆ compress()

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

This function implements rewrites the SubsequenceFN instance into an empty sequence if its third argument, the sequence length argument, is evaluated and is effectively equal or less than zero.

Reimplemented from QPatternist::Expression.

Definition at line 257 of file qsequencefns.cpp.

258 {
259  const Expression::Ptr me(FunctionCall::compress(context));
260  if(me != this)
261  return me;
262 
263  const Expression::Ptr lenArg(m_operands.value(2));
264  if(lenArg && lenArg->isEvaluated())
265  {
266  const xsInteger length = lenArg->as<Literal>()->item().as<Numeric>()->round()->toInteger();
267 
268  if(length <= 0)
269  return EmptySequence::create(this, context);
270  }
271 
272  return me;
273 }
qint64 xsInteger
static Expression::Ptr create(const Expression *const replacementFor, const StaticContext::Ptr &context)
Creates an EmptySequence that is a replacement for replacementFor.
virtual Expression::Ptr compress(const StaticContext::Ptr &context)
const TCastTarget * as() const
T value(int i) const
Returns the value at index position i in the list.
Definition: qlist.h:661
Houses an AtomicValue, making it available as an Expression.
Definition: qliteral_p.h:74
Base class for all numeric values.
virtual xsInteger toInteger() const =0

◆ evaluateSequence()

Item::Iterator::Ptr SubsequenceFN::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 227 of file qsequencefns.cpp.

Referenced by evaluateSingleton().

228 {
230 
231  xsInteger startingLoc = m_operands.at(1)->evaluateSingleton(context).as<Numeric>()->round()->toInteger();
232  xsInteger length = -1;
233 
234  if(m_operands.count() == 3)
235  {
236  length = m_operands.last()->evaluateSingleton(context).as<Numeric>()->toInteger();
237 
238  if(startingLoc + length < 1 || (startingLoc > (startingLoc + length)))
240  }
241 
242  /* F&O, 15.1.10, "If $startingLoc is zero or negative, the
243  * subsequence includes items from the beginning of the $sourceSeq." */
244  if(startingLoc < 1)
245  startingLoc = 1;
246 
247  if(length < 1 && length != -1)
249  return Item::Iterator::Ptr(new SubsequenceIterator(it, startingLoc, length));
250 }
#define it(className, varName)
qint64 xsInteger
The QExplicitlySharedDataPointer class represents a pointer to an explicitly shared object...
Definition: qshareddata.h:136
int count(const T &t) const
Returns the number of occurrences of value in the list.
Definition: qlist.h:891
static const EmptyIterator< Item >::Ptr emptyIterator
Picks out a slice from another QAbstractXmlForwardIterator, specified by a start and end position...
QExplicitlySharedDataPointer< QAbstractXmlForwardIterator< Item > > Ptr
A smart pointer wrapping an instance of a QAbstractXmlForwardIterator subclass.
const T & at(int i) const
Returns the item at index position i in the list.
Definition: qlist.h:468
T & first()
Returns a reference to the first item in the list.
Definition: qlist.h:282
Base class for all numeric values.
virtual xsInteger toInteger() const =0
T & last()
Returns a reference to the last item in the list.
Definition: qlist.h:284
TCastTarget * as() const
Definition: qitem_p.h:278
virtual Item evaluateSingleton(const DynamicContext::Ptr &context) const
virtual Item::Iterator::Ptr evaluateSequence(const DynamicContext::Ptr &context) const

◆ evaluateSingleton()

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

Reimplemented from QPatternist::Expression.

Definition at line 252 of file qsequencefns.cpp.

253 {
254  return evaluateSequence(context)->next();
255 }
virtual Item::Iterator::Ptr evaluateSequence(const DynamicContext::Ptr &context) const

◆ staticType()

SequenceType::Ptr SubsequenceFN::staticType ( ) const
virtual

Partially implements the static type inference rules.

See also
XQuery 1.0 and XPath 2.0 Formal Semantics, 7.2.13 The fn:subsequence function

Reimplemented from QPatternist::FunctionCall.

Definition at line 275 of file qsequencefns.cpp.

276 {
277  const SequenceType::Ptr opType(m_operands.first()->staticType());
278  const Cardinality opCard(opType->cardinality());
279 
280  /* Optimization: we can do much stronger inference here. If the length is a
281  * constant, we can constrain the range at least upwards of the
282  * cardinality, for instance. */
283 
284  /* The subsequence(expr, 1, 1), add empty-sequence() to the static type.
285  *
286  * Note that we cannot do all these inferences before we've typechecked our
287  * operands. The only known case of where our staticType() is called before
288  * typeCheck() is through xmlpatternsview, although it wouldn't be
289  * surprising if the more exotic paths can achieve that too.
290  */
291  if(m_hasTypeChecked &&
292  m_operands.at(1)->isEvaluated() &&
293  m_operands.count() == 3 &&
294  m_operands.at(2)->isEvaluated() &&
295  m_operands.at(1)->as<Literal>()->item().as<Numeric>()->round()->toInteger() == 1 &&
296  m_operands.at(2)->as<Literal>()->item().as<Numeric>()->round()->toInteger() == 1)
297  {
298  return makeGenericSequenceType(opType->itemType(),
299  opCard.toWithoutMany());
300  }
301  else
302  {
303  return makeGenericSequenceType(opType->itemType(),
304  opCard | Cardinality::zeroOrOne());
305  }
306 }
int count(const T &t) const
Returns the number of occurrences of value in the list.
Definition: qlist.h:891
const TCastTarget * as() const
const T & at(int i) const
Returns the item at index position i in the list.
Definition: qlist.h:468
Houses an AtomicValue, making it available as an Expression.
Definition: qliteral_p.h:74
T & first()
Returns a reference to the first item in the list.
Definition: qlist.h:282
virtual SequenceType::Ptr staticType() const =0
static Cardinality zeroOrOne()
Base class for all numeric values.
virtual xsInteger toInteger() const =0
Represents a cardinality, a possible , often represented by occurrence indicators.

◆ typeCheck()

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

Definition at line 220 of file qsequencefns.cpp.

222 {
223  m_hasTypeChecked = true;
224  return FunctionCall::typeCheck(context, reqType);
225 }
virtual Expression::Ptr typeCheck(const StaticContext::Ptr &context, const SequenceType::Ptr &reqType)

Properties

◆ m_hasTypeChecked

bool QPatternist::SubsequenceFN::m_hasTypeChecked
private

Definition at line 336 of file qsequencefns_p.h.

Referenced by staticType(), and typeCheck().


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