Qt 4.8
Public Functions | Static Public Functions | Protected Functions | Private Types | List of all members
QPatternist::GenericPredicate Class Reference

A predicate that can handle all kinds of predicates and is therefore not very efficient, but can cope with all the tricky scenarios. More...

#include <qgenericpredicate_p.h>

Inheritance diagram for QPatternist::GenericPredicate:
QPatternist::PairContainer QPatternist::Expression QSharedData QPatternist::CppCastingHelper< Expression > QPatternist::SourceLocationReflection QPatternist::TruthPredicate

Public Functions

virtual ExpressionVisitorResult::Ptr accept (const ExpressionVisitor::Ptr &visitor) const
 
virtual QString description () const
 
virtual Item::Iterator::Ptr evaluateSequence (const DynamicContext::Ptr &context) const
 
virtual Item evaluateSingleton (const DynamicContext::Ptr &context) const
 
virtual SequenceType::List expectedOperandTypes () const
 
virtual ID id () const
 
Item mapToItem (const Item &subject, const DynamicContext::Ptr &) const
 
virtual Properties properties () const
 
virtual SequenceType::Ptr staticType () const
 
- Public Functions inherited from QPatternist::PairContainer
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)
 
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 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 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::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 create (const Expression::Ptr &sourceExpression, const Expression::Ptr &predicateExpression, const StaticContext::Ptr &context, const QSourceLocation &location)
 
static Expression::Ptr createFirstItem (const Expression::Ptr &sourceExpression)
 
- Static Public Functions inherited from QPatternist::Expression
static void rewrite (Expression::Ptr &old, const Expression::Ptr &New, const StaticContext::Ptr &context)
 

Protected Functions

 GenericPredicate (const Expression::Ptr &sourceExpression, const Expression::Ptr &predicate)
 
virtual ItemType::Ptr newFocusType () const
 
- Protected Functions inherited from QPatternist::PairContainer
 PairContainer (const Expression::Ptr &operand1, const Expression::Ptr &operand2)
 
- Protected Functions inherited from QPatternist::Expression
void typeCheckOperands (const StaticContext::Ptr &context)
 
- Protected Functions inherited from QPatternist::CppCastingHelper< Expression >
 CppCastingHelper ()
 

Private Types

typedef QExplicitlySharedDataPointer< const GenericPredicateConstPtr
 

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 Variables inherited from QPatternist::PairContainer
Expression::Ptr m_operand1
 
Expression::Ptr m_operand2
 

Detailed Description

A predicate that can handle all kinds of predicates and is therefore not very efficient, but can cope with all the tricky scenarios.

See also
FirstItemPredicate
TruthPredicate
Author
Frans Englich frans.nosp@m..eng.nosp@m.lich@.nosp@m.noki.nosp@m.a.com

Definition at line 72 of file qgenericpredicate_p.h.

Typedefs

◆ ConstPtr

Definition at line 140 of file qgenericpredicate_p.h.

Constructors and Destructors

◆ GenericPredicate()

GenericPredicate::GenericPredicate ( const Expression::Ptr sourceExpression,
const Expression::Ptr predicate 
)
protected

Creates a GenericPredicate which filters the items from the sourceExpression through predicate.

This constructor is protected. The proper way to create predicates is via the static create() function.

Definition at line 60 of file qgenericpredicate.cpp.

Referenced by create().

61  : PairContainer(sourceExpression,
62  predicate)
63 {
64 }
PairContainer(const Expression::Ptr &operand1, const Expression::Ptr &operand2)

Functions

◆ accept()

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

Implements QPatternist::Expression.

Reimplemented in QPatternist::TruthPredicate.

Definition at line 193 of file qgenericpredicate.cpp.

194 {
195  return visitor->visit(this);
196 }

◆ create()

Expression::Ptr GenericPredicate::create ( const Expression::Ptr sourceExpression,
const Expression::Ptr predicateExpression,
const StaticContext::Ptr context,
const QSourceLocation location 
)
static

Creates a predicate expression that filters the items gained from evaluating sourceExpression through the filter predicateExpression.

This function performs type analyzis on the passed expressions, and may return more specialized expressions depending on the analyzis.

If predicateExpression is an invalid predicate, an error is issued via the context.

Definition at line 66 of file qgenericpredicate.cpp.

Referenced by QPatternist::createIdPatternPath(), QPatternist::createPatternPath(), and QPatternist::yyparse().

70 {
71  Q_ASSERT(sourceExpression);
72  Q_ASSERT(predicateExpression);
73  Q_ASSERT(context);
74  const ItemType::Ptr type(predicateExpression->staticType()->itemType());
75 
76  if(predicateExpression->is(IDIntegerValue) &&
77  predicateExpression->as<Literal>()->item().as<Numeric>()->toInteger() == 1)
78  { /* Handle [1] */
79  return createFirstItem(sourceExpression);
80  }
81  else if(BuiltinTypes::numeric->xdtTypeMatches(type))
82  { /* A numeric predicate, other than [1]. */
83  /* TODO at somepoint we'll return a specialized expr here, NumericPredicate or so.
84  * Dependency analysis is a bit tricky, since the contained expression can depend on
85  * some loop component. */
86  return Expression::Ptr(new GenericPredicate(sourceExpression, predicateExpression));
87  }
88  else if(*CommonSequenceTypes::Empty == *type)
89  {
90  return EmptySequence::create(predicateExpression.data(), context);
91  }
92  else if(*BuiltinTypes::item == *type ||
94  {
95  /* The type couldn't be narrowed at compile time, so we use
96  * a generic predicate. This check is before the CommonSequenceTypes::EBV check,
97  * because the latter matches these types as well. */
98  return Expression::Ptr(new GenericPredicate(sourceExpression, predicateExpression));
99  }
100  else if(CommonSequenceTypes::EBV->itemType()->xdtTypeMatches(type))
101  {
102  return Expression::Ptr(new TruthPredicate(sourceExpression, predicateExpression));
103  }
104  else
105  {
106  context->error(QtXmlPatterns::tr("A value of type %1 cannot be a "
107  "predicate. A predicate must have "
108  "either a numeric type or an "
109  "Effective Boolean Value type.")
110  .arg(formatType(context->namePool(),
111  sourceExpression->staticType())),
113  return Expression::Ptr(); /* Silence compiler warning. */
114  }
115 }
int type
Definition: qmetatype.cpp:239
GenericPredicate(const Expression::Ptr &sourceExpression, const Expression::Ptr &predicate)
QString formatType(const NamePool::Ptr &np, const T &type)
Formats ItemType and SequenceType.
virtual NamePool::Ptr namePool() const =0
static Expression::Ptr create(const Expression *const replacementFor, const StaticContext::Ptr &context)
Creates an EmptySequence that is a replacement for replacementFor.
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
T * data() const
Returns a pointer to the shared data object.
Definition: qshareddata.h:145
const TCastTarget * as() const
void error(const QString &message, const ReportContext::ErrorCode errorCode, const QSourceLocation &sourceLocation)
static const ItemType::Ptr item
Houses an AtomicValue, making it available as an Expression.
Definition: qliteral_p.h:74
QExplicitlySharedDataPointer< Expression > Ptr
A smart pointer wrapping mutable Expression instances.
virtual SequenceType::Ptr staticType() const =0
static const EmptySequenceType::Ptr Empty
Base class for all numeric values.
static QTestResult::TestLocation location
Definition: qtestresult.cpp:63
static const AtomicType::Ptr numeric
static const SequenceType::Ptr EBV
A predicate which is optimized for filter expressions that are of type xs:boolean.
static const AtomicType::Ptr xsAnyAtomicType
virtual ItemType::Ptr itemType() const =0
bool is(const ID id) const
static Expression::Ptr createFirstItem(const Expression::Ptr &sourceExpression)

◆ createFirstItem()

Expression::Ptr GenericPredicate::createFirstItem ( const Expression::Ptr sourceExpression)
static

Definition at line 117 of file qgenericpredicate.cpp.

Referenced by create(), QPatternist::CardinalityVerifier::verifyCardinality(), and QPatternist::yyparse().

119 {
120  return Expression::Ptr(new FirstItemPredicate(sourceExpression));
121 }
QExplicitlySharedDataPointer< Expression > Ptr
A smart pointer wrapping mutable Expression instances.
A predicate that always selects the first item from its sequence.

◆ description()

QString GenericPredicate::description ( ) const
virtual

Reimplementation of SourceLocationReflection::description().

Reimplemented from QPatternist::Expression.

Definition at line 208 of file qgenericpredicate.cpp.

209 {
210  return QLatin1String("predicate");
211 }
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString

◆ evaluateSequence()

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

Creates a source iterator which is passed to the ItemMappingIterator and the Focus. The ItemMappingIterator modifies it with its QAbstractXmlForwardIterator::next() calls, and since the Focus references the same QAbstractXmlForwardIterator, the focus is automatically moved.

Reimplemented from QPatternist::Expression.

Definition at line 159 of file qgenericpredicate.cpp.

160 {
161  const Item::Iterator::Ptr focus(m_operand1->evaluateSequence(context));
162  const DynamicContext::Ptr newContext(context->createFocus());
163  newContext->setFocusIterator(focus);
164 
165  return makeItemMappingIterator<Item>(ConstPtr(this),
166  focus,
167  newContext);
168 }
The QExplicitlySharedDataPointer class represents a pointer to an explicitly shared object...
Definition: qshareddata.h:136
DynamicContext::Ptr createFocus()
virtual void setFocusIterator(const Item::Iterator::Ptr &it)=0
QExplicitlySharedDataPointer< const GenericPredicate > ConstPtr
virtual Item::Iterator::Ptr evaluateSequence(const DynamicContext::Ptr &context) const

◆ evaluateSingleton()

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

Doesn't return the first item from calling evaluateSequence(), but does the mapping manually. This avoid allocating an ItemMappingIterator.

Reimplemented from QPatternist::Expression.

Definition at line 170 of file qgenericpredicate.cpp.

171 {
172  const Item::Iterator::Ptr focus(m_operand1->evaluateSequence(context));
173  const DynamicContext::Ptr newContext(context->createFocus());
174  newContext->setFocusIterator(focus);
175  return mapToItem(focus->next(), newContext);
176 }
The QExplicitlySharedDataPointer class represents a pointer to an explicitly shared object...
Definition: qshareddata.h:136
DynamicContext::Ptr createFocus()
virtual void setFocusIterator(const Item::Iterator::Ptr &it)=0
Item mapToItem(const Item &subject, const DynamicContext::Ptr &) const
virtual Item::Iterator::Ptr evaluateSequence(const DynamicContext::Ptr &context) const

◆ expectedOperandTypes()

SequenceType::List GenericPredicate::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.

Reimplemented in QPatternist::TruthPredicate.

Definition at line 178 of file qgenericpredicate.cpp.

179 {
180  SequenceType::List result;
183  return result;
184 }
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 GenericPredicate::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 213 of file qgenericpredicate.cpp.

◆ mapToItem()

Item GenericPredicate::mapToItem ( const Item subject,
const DynamicContext::Ptr context 
) const
inline

Definition at line 123 of file qgenericpredicate.cpp.

Referenced by evaluateSingleton().

125 {
127  const Item pcateItem(it->next());
128 
129  if(!pcateItem)
130  return Item(); /* The predicate evaluated to the empty sequence */
131  else if(pcateItem.isNode())
132  return item;
133  /* Ok, now it must be an AtomicValue */
134  else if(BuiltinTypes::numeric->xdtTypeMatches(pcateItem.type()))
135  { /* It's a positional predicate. */
136  if(it->next())
137  {
138  context->error(QtXmlPatterns::tr("A positional predicate must "
139  "evaluate to a single numeric "
140  "value."),
142  return Item();
143  }
144 
145  if(Double::isEqual(static_cast<xsDouble>(context->contextPosition()),
146  pcateItem.as<Numeric>()->toDouble()))
147  {
148  return item;
149  }
150  else
151  return Item();
152  }
153  else if(Boolean::evaluateEBV(pcateItem, it, context)) /* It's a truth predicate. */
154  return item;
155  else
156  return Item();
157 }
#define it(className, varName)
The QExplicitlySharedDataPointer class represents a pointer to an explicitly shared object...
Definition: qshareddata.h:136
static bool evaluateEBV(const Item::Iterator::Ptr &e, const QExplicitlySharedDataPointer< DynamicContext > &)
Definition: qboolean.cpp:55
virtual xsInteger contextPosition() const =0
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
static bool isEqual(const xsDouble a, const xsDouble b)
Base class for all numeric values.
virtual xsDouble toDouble() const =0
static const AtomicType::Ptr numeric
virtual Item::Iterator::Ptr evaluateSequence(const DynamicContext::Ptr &context) const

◆ newFocusType()

ItemType::Ptr GenericPredicate::newFocusType ( ) const
protectedvirtual
Returns
the ItemType of the first operand's staticType().

Reimplemented from QPatternist::Expression.

Definition at line 198 of file qgenericpredicate.cpp.

199 {
200  return m_operand1->staticType()->itemType();
201 }
virtual SequenceType::Ptr staticType() const =0
virtual ItemType::Ptr itemType() const =0

◆ properties()

Expression::Properties GenericPredicate::properties ( ) const
virtual
Returns
always CreatesFocusForLast.

Reimplemented from QPatternist::Expression.

Definition at line 203 of file qgenericpredicate.cpp.

◆ staticType()

SequenceType::Ptr GenericPredicate::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 186 of file qgenericpredicate.cpp.

187 {
189  return makeGenericSequenceType(type->itemType(),
190  type->cardinality() | Cardinality::zeroOrOne());
191 }
int type
Definition: qmetatype.cpp:239
virtual SequenceType::Ptr staticType() const =0
static Cardinality zeroOrOne()

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