Qt 4.8
Public Functions | Static Public Functions | Static Private Functions | Properties | List of all members
QPatternist::CardinalityVerifier Class Reference

Verifies that the sequence an Expression evaluates to conforms to a Cardinality. More...

#include <qcardinalityverifier_p.h>

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

Public Functions

virtual ExpressionVisitorResult::Ptr accept (const ExpressionVisitor::Ptr &visitor) const
 
virtual const SourceLocationReflectionactualReflection () const
 Returns this. More...
 
 CardinalityVerifier (const Expression::Ptr &operand, const Cardinality &card, const ReportContext::ErrorCode code)
 
virtual Expression::Ptr compress (const StaticContext::Ptr &context)
 
virtual Item::Iterator::Ptr evaluateSequence (const DynamicContext::Ptr &context) const
 
virtual Item evaluateSingleton (const DynamicContext::Ptr &) const
 
virtual SequenceType::List expectedOperandTypes () const
 
ID id () const
 
virtual SequenceType::Ptr staticType () const
 
- 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 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
 
virtual Properties properties () const
 
const Expression::Ptrrewrite (const Expression::Ptr &to, const StaticContext::Ptr &context) const
 Rewrites this Expression to to, and return to. More...
 
virtual Expression::Ptr typeCheck (const StaticContext::Ptr &context, const SequenceType::Ptr &reqType)
 
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 ()
 

Static Public Functions

static Expression::Ptr verifyCardinality (const Expression::Ptr &operand, const Cardinality &card, const StaticContext::Ptr &context, const ReportContext::ErrorCode code=ReportContext::XPTY0004)
 
- Static Public Functions inherited from QPatternist::Expression
static void rewrite (Expression::Ptr &old, const Expression::Ptr &New, const StaticContext::Ptr &context)
 

Static Private Functions

static QString wrongCardinality (const Cardinality &req, const Cardinality &got=Cardinality::empty())
 

Properties

const bool m_allowsMany
 
const ReportContext::ErrorCode m_errorCode
 
const Cardinality m_reqCard
 

Additional Inherited Members

- 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 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

Verifies that the sequence an Expression evaluates to conforms to a Cardinality.

See also
XQuery 1.0 and XPath 2.0 Functions and Operators, 15.2 Functions That Test the Cardinality of Sequences
Author
Frans Englich frans.nosp@m..eng.nosp@m.lich@.nosp@m.noki.nosp@m.a.com

Definition at line 72 of file qcardinalityverifier_p.h.

Constructors and Destructors

◆ CardinalityVerifier()

CardinalityVerifier::CardinalityVerifier ( const Expression::Ptr operand,
const Cardinality card,
const ReportContext::ErrorCode  code 
)

Definition at line 86 of file qcardinalityverifier.cpp.

Referenced by verifyCardinality().

89  : SingleContainer(operand),
90  m_reqCard(card),
92  m_errorCode(code)
93 {
95  "It makes no sense to use CardinalityVerifier for cardinality zero-or-more.");
96 }
static Cardinality zeroOrMore()
SingleContainer(const Expression::Ptr &operand)
virtual Cardinality cardinality() const =0
virtual SequenceType::Ptr staticType() const =0
#define Q_ASSERT_X(cond, where, what)
Definition: qglobal.h:1837
const ReportContext::ErrorCode m_errorCode
#define Q_FUNC_INFO
Definition: qglobal.h:1871

Functions

◆ accept()

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

Implements QPatternist::Expression.

Definition at line 214 of file qcardinalityverifier.cpp.

215 {
216  return visitor->visit(this);
217 }

◆ actualReflection()

const SourceLocationReflection * CardinalityVerifier::actualReflection ( ) const
virtual

Returns this.

Reimplemented from QPatternist::Expression.

Definition at line 189 of file qcardinalityverifier.cpp.

190 {
191  return m_operand->actualReflection();
192 }
virtual const SourceLocationReflection * actualReflection() const
Returns this.

◆ compress()

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

If the static cardinality of the operand is within the required cardinality, the operand is returned as is, since results will always be valid and hence is not a CardinalityVerifier necessary.

Reimplemented from QPatternist::Expression.

Definition at line 194 of file qcardinalityverifier.cpp.

195 {
197  return m_operand->compress(context);
198  else
199  return SingleContainer::compress(context);
200 }
virtual Expression::Ptr compress(const StaticContext::Ptr &context)
virtual Cardinality cardinality() const =0
virtual SequenceType::Ptr staticType() const =0
bool isMatch(const Cardinality &other) const

◆ evaluateSequence()

Item::Iterator::Ptr CardinalityVerifier::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 98 of file qcardinalityverifier.cpp.

99 {
101  const Item next(it->next());
102 
103  if(next)
104  {
105  const Item next2(it->next());
106 
107  if(next2)
108  {
109  if(m_reqCard.allowsMany())
110  {
111  Item::List start;
112  start.append(next);
113  start.append(next2);
114 
116  }
117  else
118  {
121  }
122  }
123  else
124  {
125  /* We might be instantiated for the empty sequence. */
126  if(m_reqCard.isEmpty())
127  {
130  }
131  else
132  return makeSingletonIterator(next);
133  }
134  }
135  else
136  {
137  if(m_reqCard.allowsEmpty())
139  else
140  {
143  }
144  }
145 }
#define it(className, varName)
The QExplicitlySharedDataPointer class represents a pointer to an explicitly shared object...
Definition: qshareddata.h:136
QAbstractXmlForwardIterator< T >::Ptr makeListIterator(const QList< T > &list)
static const EmptyIterator< Item >::Ptr emptyIterator
static QString wrongCardinality(const Cardinality &req, const Cardinality &got=Cardinality::empty())
static Cardinality twoOrMore()
QExplicitlySharedDataPointer< QAbstractXmlForwardIterator< Item > > Ptr
A smart pointer wrapping an instance of a QAbstractXmlForwardIterator subclass.
void append(const T &t)
Inserts value at the end of the list.
Definition: qlist.h:507
void error(const QString &message, const ReportContext::ErrorCode errorCode, const QSourceLocation &sourceLocation)
Conceptually inserts one QAbstractXmlForwardIterator into another, make two QAbstractXmlForwardIterat...
Represents an item in the XPath 2.0 Data Model.
Definition: qitem_p.h:182
QList< Item > List
Definition: qitem_p.h:195
const ReportContext::ErrorCode m_errorCode
virtual Item::Iterator::Ptr evaluateSequence(const DynamicContext::Ptr &context) const

◆ evaluateSingleton()

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

Reimplemented from QPatternist::Expression.

Definition at line 147 of file qcardinalityverifier.cpp.

148 {
149  if(m_allowsMany)
150  {
152  const Item item(it->next());
153 
154  if(item)
155  {
156  if(it->next())
157  {
159  m_errorCode, this);
160  return Item();
161  }
162  else
163  return item;
164  }
165  else if(m_reqCard.allowsEmpty())
166  return Item();
167  else
168  {
169  context->error(wrongCardinality(m_reqCard), m_errorCode, this);
170  return Item();
171  }
172  }
173  else
174  {
175  const Item item(m_operand->evaluateSingleton(context));
176 
177  if(item)
178  return item;
179  else if(m_reqCard.allowsEmpty())
180  return Item();
181  else
182  {
183  context->error(wrongCardinality(m_reqCard), m_errorCode, this);
184  return Item();
185  }
186  }
187 }
#define it(className, varName)
The QExplicitlySharedDataPointer class represents a pointer to an explicitly shared object...
Definition: qshareddata.h:136
static QString wrongCardinality(const Cardinality &req, const Cardinality &got=Cardinality::empty())
static Cardinality twoOrMore()
void error(const QString &message, const ReportContext::ErrorCode errorCode, const QSourceLocation &sourceLocation)
Represents an item in the XPath 2.0 Data Model.
Definition: qitem_p.h:182
const ReportContext::ErrorCode m_errorCode
virtual Item evaluateSingleton(const DynamicContext::Ptr &context) const
virtual Item::Iterator::Ptr evaluateSequence(const DynamicContext::Ptr &context) const

◆ expectedOperandTypes()

SequenceType::List CardinalityVerifier::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 202 of file qcardinalityverifier.cpp.

203 {
204  SequenceType::List result;
206  return result;
207 }
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 CardinalityVerifier::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 219 of file qcardinalityverifier.cpp.

◆ staticType()

SequenceType::Ptr CardinalityVerifier::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 209 of file qcardinalityverifier.cpp.

210 {
211  return makeGenericSequenceType(m_operand->staticType()->itemType(), m_reqCard);
212 }
virtual SequenceType::Ptr staticType() const =0
virtual ItemType::Ptr itemType() const =0

◆ verifyCardinality()

Expression::Ptr CardinalityVerifier::verifyCardinality ( const Expression::Ptr operand,
const Cardinality card,
const StaticContext::Ptr context,
const ReportContext::ErrorCode  code = ReportContext::XPTY0004 
)
static

A utility function for determining whether the static type of an Expression matches a cardinality. More specifically, this function performs the cardinality verification part of the Function Conversion Rules.

Definition at line 62 of file qcardinalityverifier.cpp.

Referenced by QPatternist::TypeChecker::applyFunctionConversion(), and QPatternist::ExpressionSequence::typeCheck().

66 {
67  const Cardinality opCard(operand->staticType()->cardinality());
68 
69  if(requiredCard.isMatch(opCard))
70  return operand;
71  else if(requiredCard.canMatch(opCard))
72  return Expression::Ptr(new CardinalityVerifier(operand, requiredCard, code));
73  else if(context->compatModeEnabled() &&
74  !opCard.isEmpty())
75  {
76  return GenericPredicate::createFirstItem(operand);
77  }
78  else
79  {
80  /* Sequences within this cardinality can never match. */
81  context->error(wrongCardinality(requiredCard, opCard), code, operand.data());
82  return operand;
83  }
84 }
virtual bool compatModeEnabled() const =0
static QString wrongCardinality(const Cardinality &req, const Cardinality &got=Cardinality::empty())
T * data() const
Returns a pointer to the shared data object.
Definition: qshareddata.h:145
void error(const QString &message, const ReportContext::ErrorCode errorCode, const QSourceLocation &sourceLocation)
virtual Cardinality cardinality() const =0
virtual SequenceType::Ptr staticType() const =0
CardinalityVerifier(const Expression::Ptr &operand, const Cardinality &card, const ReportContext::ErrorCode code)
Represents a cardinality, a possible , often represented by occurrence indicators.
static Expression::Ptr createFirstItem(const Expression::Ptr &sourceExpression)

◆ wrongCardinality()

QString CardinalityVerifier::wrongCardinality ( const Cardinality req,
const Cardinality got = Cardinality::empty() 
)
inlinestaticprivate

Centralizes a message string in order to increase consistency and reduce work for translators.

Definition at line 55 of file qcardinalityverifier.cpp.

Referenced by evaluateSequence(), evaluateSingleton(), and verifyCardinality().

57 {
58  return QtXmlPatterns::tr("Required cardinality is %1; got cardinality %2.")
59  .arg(formatType(req), formatType(got));
60 }
QString formatType(const NamePool::Ptr &np, const T &type)
Formats ItemType and SequenceType.

Properties

◆ m_allowsMany

const bool QPatternist::CardinalityVerifier::m_allowsMany
private

Definition at line 119 of file qcardinalityverifier_p.h.

Referenced by evaluateSingleton().

◆ m_errorCode

const ReportContext::ErrorCode QPatternist::CardinalityVerifier::m_errorCode
private

Definition at line 120 of file qcardinalityverifier_p.h.

Referenced by evaluateSequence(), and evaluateSingleton().

◆ m_reqCard

const Cardinality QPatternist::CardinalityVerifier::m_reqCard
private

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