Qt 4.8
Public Functions | Private Types | List of all members
QPatternist::ExpressionSequence Class Reference

Implements the comma(",") operator, the sequence constructor. More...

#include <qexpressionsequence_p.h>

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

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 &) const
 
virtual void evaluateToSequenceReceiver (const DynamicContext::Ptr &context) const
 
virtual SequenceType::List expectedOperandTypes () const
 
 ExpressionSequence (const Expression::List &operands)
 
virtual ID id () const
 
Item::Iterator::Ptr mapToSequence (const Expression::Ptr &, const DynamicContext::Ptr &) const
 
virtual Expression::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::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 Item evaluateSingleton (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 ()
 

Private Types

typedef QExplicitlySharedDataPointer< const ExpressionSequenceConstPtr
 

Additional Inherited Members

- 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 comma(",") operator, the sequence constructor.

For example, the expression alpha, beta evaluates to a sequence containing the items the nodetest alpha evaluates to, concatenated with the items the nodetest beta evaluates to.

See also
XML Path Language (XPath) 2.0, 3.3.1 Constructing Sequences
Author
Frans Englich frans.nosp@m..eng.nosp@m.lich@.nosp@m.noki.nosp@m.a.com

Definition at line 75 of file qexpressionsequence_p.h.

Typedefs

◆ ConstPtr

Definition at line 119 of file qexpressionsequence_p.h.

Constructors and Destructors

◆ ExpressionSequence()

ExpressionSequence::ExpressionSequence ( const Expression::List operands)

Creates an ExpressionSequence with the operands operands. operands must contain two or more Expression instances.

Definition at line 53 of file qexpressionsequence.cpp.

53  : UnlimitedContainer(ops)
54 {
55  Q_ASSERT_X(1 < ops.count(), Q_FUNC_INFO,
56  "It makes no sense to have an ExpressionSequence containing less than two expressions.");
57 }
#define Q_ASSERT_X(cond, where, what)
Definition: qglobal.h:1837
UnlimitedContainer(const Expression::List &operands=Expression::List())
#define Q_FUNC_INFO
Definition: qglobal.h:1871

Functions

◆ accept()

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

Implements QPatternist::Expression.

Definition at line 196 of file qexpressionsequence.cpp.

197 {
198  return visitor->visit(this);
199 }

◆ compress()

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

Removes any empty sequences, typically "()", from its list of children. If after that rewrite has no children, it rewrites itself to the CommonValues::empty; if it has only one, it rewrites to the child.

This optimization is not very usable by itself, but potentially becomes effective after other optimizations have rewritten themselves into empty sequences. Thus, saving memory consumption and runtime overhead.

Reimplemented from QPatternist::Expression.

Definition at line 82 of file qexpressionsequence.cpp.

83 {
85 
86  if(me != this)
87  return me;
88 
91  Expression::List result;
92 
93  for(; it != end; ++it)
94  {
95  const ID Id = (*it)->id();
96 
97  /* Remove empty sequences. This is rather important because we have some steps in the parser that
98  * intentionally, unconditionally and for temporary reasons create expressions like (expr, ()). Of course,
99  * empty sequences also occur as part of optimizations.
100  *
101  * User function call sites that are of type empty-sequence() must be avoided since
102  * they may contain calls to fn:error(), which we would rewrite away otherwise. */
103  if(Id != IDUserFunctionCallsite && (*it)->staticType()->cardinality().isEmpty())
104  {
105  /* Rewrite "(1, (), 2)" into "(1, 2)" by not
106  * adding (*it) to result. */
107  continue;
108  }
109  else if(Id == IDExpressionSequence)
110  {
111  /* Rewrite "(1, (2, 3), 4)" into "(1, 2, 3, 4)" */
112  Expression::List::const_iterator seqIt((*it)->operands().constBegin());
113  const Expression::List::const_iterator seqEnd((*it)->operands().constEnd());
114 
115  for(; seqIt != seqEnd; ++seqIt)
116  result.append(*seqIt);
117  }
118  else
119  result.append(*it);
120  }
121 
122  if(result.isEmpty())
123  return EmptySequence::create(this, context);
124  else if(result.count() == 1)
125  return result.first();
126  else
127  {
128  m_operands = result;
129  return me;
130  }
131 }
#define it(className, varName)
const_iterator constBegin() const
Returns a const STL-style iterator pointing to the first item in the list.
Definition: qlist.h:269
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)
friend class const_iterator
Definition: qlist.h:264
static const KeyPair *const end
const_iterator constEnd() const
Returns a const STL-style iterator pointing to the imaginary item after the last item in the list...
Definition: qlist.h:272

◆ evaluateSequence()

Item::Iterator::Ptr ExpressionSequence::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 65 of file qexpressionsequence.cpp.

66 {
67  return makeSequenceMappingIterator<Item>(ConstPtr(this),
69  context);
70 }
QAbstractXmlForwardIterator< T >::Ptr makeListIterator(const QList< T > &list)
QExplicitlySharedDataPointer< const ExpressionSequence > ConstPtr

◆ evaluateToSequenceReceiver()

void ExpressionSequence::evaluateToSequenceReceiver ( const DynamicContext::Ptr context) const
virtual

Forwards the call to its children.

Reimplemented from QPatternist::Expression.

Definition at line 72 of file qexpressionsequence.cpp.

73 {
76  Expression::List result;
77 
78  for(; it != end; ++it)
79  (*it)->evaluateToSequenceReceiver(context);
80 }
#define it(className, varName)
const_iterator constBegin() const
Returns a const STL-style iterator pointing to the first item in the list.
Definition: qlist.h:269
friend class const_iterator
Definition: qlist.h:264
static const KeyPair *const end
const_iterator constEnd() const
Returns a const STL-style iterator pointing to the imaginary item after the last item in the list...
Definition: qlist.h:272

◆ expectedOperandTypes()

SequenceType::List ExpressionSequence::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 187 of file qexpressionsequence.cpp.

188 {
189  SequenceType::List result;
190  /* ExpressionSequence is a bit strange type wise since it has an
191  * infinite amount of operands. */
193  return result;
194 }
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

◆ id()

Expression::ID ExpressionSequence::id ( ) const
virtual

This property, which has no setter, returns an enum value that uniquely identifies this Expression. Patternist makes no use of C++'s dynamic_cast feature, but uses this polymorphic function instead.

Returns
always IgnorableExpression.

Reimplemented from QPatternist::Expression.

Definition at line 201 of file qexpressionsequence.cpp.

◆ mapToSequence()

Item::Iterator::Ptr ExpressionSequence::mapToSequence ( const Expression::Ptr expr,
const DynamicContext::Ptr context 
) const
inline

Definition at line 59 of file qexpressionsequence.cpp.

61 {
62  return expr->evaluateSequence(context);
63 }
virtual Item::Iterator::Ptr evaluateSequence(const DynamicContext::Ptr &context) const

◆ properties()

Expression::Properties ExpressionSequence::properties ( ) const
virtual
Returns
Expression::DisableElimination, plus the union of all this ExpressionSequence's children's properties. If any child does not have IsEvaluated, it is removed from the result.

Reimplemented from QPatternist::Expression.

Definition at line 155 of file qexpressionsequence.cpp.

156 {
159  bool allEvaled = true;
160  Expression::Properties props(DisableElimination); /* Why do we have this flag? */
161 
162  for(it = m_operands.constBegin(); it != end; ++it)
163  {
164  const Expression::Properties newp((*it)->properties());
165  props |= newp;
166 
167  if((newp & IsEvaluated) != IsEvaluated)
168  {
169  allEvaled = false;
170  break;
171  }
172  }
173 
174  if(!allEvaled)
175  props &= ~IsEvaluated; /* Remove IsEvaluated. */
176 
177  /* One of our children might need the focus, but we don't, so
178  * cut it out. */
179  return props & ~RequiresFocus;
180 }
#define it(className, varName)
const_iterator constBegin() const
Returns a const STL-style iterator pointing to the first item in the list.
Definition: qlist.h:269
friend class const_iterator
Definition: qlist.h:264
The QFlags class provides a type-safe way of storing OR-combinations of enum values.
Definition: qglobal.h:2313
static const KeyPair *const end
const_iterator constEnd() const
Returns a const STL-style iterator pointing to the imaginary item after the last item in the list...
Definition: qlist.h:272

◆ staticType()

SequenceType::Ptr ExpressionSequence::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 182 of file qexpressionsequence.cpp.

183 {
184  return operandsUnionType<ProductOfCardinality>();
185 }

◆ typeCheck()

Expression::Ptr ExpressionSequence::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 133 of file qexpressionsequence.cpp.

135 {
136  Q_ASSERT(reqType);
139 
140  /* We treat the cardinality differently here by allowing the empty sequence
141  * for each individual Expression, since the Cardinality can be conformed to by
142  * the ExpressionSequence as a whole(which we check for at the end). */
143  const SequenceType::Ptr testOnlyIT(makeGenericSequenceType(reqType->itemType(),
145  reqType->cardinality()));
146 
147  for(; it != end; ++it)
148  *it = (*it)->typeCheck(context, testOnlyIT);
149 
150  /* The above loop is only guaranteed to find item type errors, but the cardinality
151  * can still be wrong since the operands were treated individually. */
152  return CardinalityVerifier::verifyCardinality(Expression::Ptr(this), reqType->cardinality(), context);
153 }
#define it(className, varName)
iterator begin()
Returns an STL-style iterator pointing to the first item in the list.
Definition: qlist.h:267
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
static Expression::Ptr verifyCardinality(const Expression::Ptr &operand, const Cardinality &card, const StaticContext::Ptr &context, const ReportContext::ErrorCode code=ReportContext::XPTY0004)
static Cardinality empty()
iterator end()
Returns an STL-style iterator pointing to the imaginary item after the last item in the list...
Definition: qlist.h:270
virtual Cardinality cardinality() const =0
friend class iterator
Definition: qlist.h:226
virtual ItemType::Ptr itemType() const =0
static const KeyPair *const end

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