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

Implements XPath 2.0's cast as expression. More...

#include <qcastas_p.h>

Inheritance diagram for QPatternist::CastAs:
QPatternist::SingleContainer QPatternist::CastingPlatform< CastAs, true > QPatternist::Expression QSharedData QPatternist::CppCastingHelper< Expression > QPatternist::SourceLocationReflection

Public Functions

virtual ExpressionVisitorResult::Ptr accept (const ExpressionVisitor::Ptr &visitor) const
 
 CastAs (const Expression::Ptr &source, const SequenceType::Ptr &targetType)
 
virtual Expression::Ptr compress (const StaticContext::Ptr &context)
 
virtual Item evaluateSingleton (const DynamicContext::Ptr &) const
 
virtual SequenceType::List expectedOperandTypes () const
 
virtual SequenceType::Ptr staticType () const
 
SequenceType::Ptr targetSequenceType () const
 
ItemType::Ptr targetType () 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::Iterator::Ptr evaluateSequence (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
 
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 ()
 
- 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 Functions

Expression::Ptr castToQName (const StaticContext::Ptr &context) const
 

Properties

const SequenceType::Ptr m_targetType
 

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
 
- 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 Functions inherited from QPatternist::CastingPlatform< CastAs, true >
Item cast (const Item &sourceValue, const ReportContext::Ptr &context) const
 
 CastingPlatform (const ReportContext::ErrorCode code=ReportContext::FORG0001)
 
void checkTargetType (const ReportContext::Ptr &context) const
 
bool prepareCasting (const ReportContext::Ptr &context, const ItemType::Ptr &sourceType)
 
- Protected Variables inherited from QPatternist::SingleContainer
Expression::Ptr m_operand
 

Detailed Description

Implements XPath 2.0's cast as expression.

Implements the casting expression, such as '3' cast as xs:integer. This class also implements constructor functions, which are created in the ConstructorFunctionsFactory.

CastAs uses CastingPlatform for carrying out the actual casting.

See also
XQuery 1.0 and XPath 2.0 Functions and Operators, 7 Casting
XML Path Language (XPath) 2.0, 3.10.2 Cast
Author
Frans Englich frans.nosp@m..eng.nosp@m.lich@.nosp@m.noki.nosp@m.a.com

Definition at line 79 of file qcastas_p.h.

Constructors and Destructors

◆ CastAs()

CastAs::CastAs ( const Expression::Ptr source,
const SequenceType::Ptr targetType 
)

Creates a cast expression for the type name via the schema type factory factory. This function is used by parser when creating 'cast to' expressions, and the ConstructorFunctionsFactory, when creating constructor functions.

Parameters
targetTypethe type which the CastAs should cast to
sourcethe operand to evaluate and then cast from

Definition at line 62 of file qcastas.cpp.

63  : SingleContainer(source),
64  m_targetType(tType)
65 {
66  Q_ASSERT(source);
67  Q_ASSERT(tType);
68  Q_ASSERT(!tType->cardinality().allowsMany());
69  Q_ASSERT(tType->itemType()->isAtomicType());
70 }
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
SingleContainer(const Expression::Ptr &operand)
const SequenceType::Ptr m_targetType
Definition: qcastas_p.h:140

Functions

◆ accept()

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

Implements QPatternist::Expression.

Definition at line 199 of file qcastas.cpp.

200 {
201  return visitor->visit(this);
202 }

◆ castToQName()

Expression::Ptr CastAs::castToQName ( const StaticContext::Ptr context) const
private

Performs casting to xs:QName. This case is special, and is always done at compile time.

Definition at line 162 of file qcastas.cpp.

Referenced by targetSequenceType(), and typeCheck().

163 {
164  /* Apply the whitespace facet by calling trimmed(). */
165  /* We can assume m_operand is an Expression because this is a requirement
166  * for casting to xs:QName. */
167  const QString lexQName(m_operand->as<Literal>()->item().as<AtomicValue>()->stringValue().trimmed());
168 
169  const QXmlName
172  ReportContext::FONS0004>(lexQName,
173  context,
174  context->namespaceBindings(), this));
175  return wrapLiteral(toItem(QNameValue::fromValue(context->namePool(), expName)), context, this);
176 }
virtual NamePool::Ptr namePool() const =0
Item item() const
Definition: qliteral_p.h:102
Item toItem(const QExplicitlySharedDataPointer< T > atomicValue)
Definition: qitem_p.h:431
static QXmlName expandQName(const QString &lexicalQName, const TReportContext &context, const NamespaceResolver::Ptr &nsResolver, const SourceLocationReflection *const r, const bool asForAttribute=false)
The QString class provides a Unicode character string.
Definition: qstring.h:83
const TCastTarget * as() const
static QNameValue::Ptr fromValue(const NamePool::Ptr &np, const QXmlName name)
Definition: qqnamevalue.cpp:59
Base class for all classes representing atomic values.
Definition: qitem_p.h:118
virtual NamespaceResolver::Ptr namespaceBindings() const =0
Houses an AtomicValue, making it available as an Expression.
Definition: qliteral_p.h:74
The QXmlName class represents the name of an XML node, in an efficient, namespace-aware way...
Definition: qxmlname.h:58
TCastTarget * as() const
Definition: qitem_p.h:278

◆ compress()

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

If the target type is the same as the source type, it is rewritten to the operand.

Reimplemented from QPatternist::Expression.

Definition at line 153 of file qcastas.cpp.

154 {
155  /* Simplify casts to itself. */
157  return m_operand->compress(context);
158  else
159  return SingleContainer::compress(context);
160 }
virtual Expression::Ptr compress(const StaticContext::Ptr &context)
virtual SequenceType::Ptr staticType() const =0
virtual ItemType::Ptr itemType() const =0
const SequenceType::Ptr m_targetType
Definition: qcastas_p.h:140

◆ evaluateSingleton()

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

Reimplemented from QPatternist::Expression.

Definition at line 72 of file qcastas.cpp.

73 {
74  Q_ASSERT(context);
75  const Item val(m_operand->evaluateSingleton(context));
76 
77  if(val)
78  return cast(val, context);
79  else
80  {
81  /* No item supplied, let's handle the cardinality part. */
82 
84  return Item();
85  else
86  {
87  Q_ASSERT(context);
88  context->error(QtXmlPatterns::tr("Type error in cast, expected %1, "
89  "received %2.")
93  return Item();
94  }
95  }
96 }
Item cast(const Item &sourceValue, const ReportContext::Ptr &context) const
QString formatType(const NamePool::Ptr &np, const T &type)
Formats ItemType and SequenceType.
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
static Cardinality empty()
void error(const QString &message, const ReportContext::ErrorCode errorCode, const QSourceLocation &sourceLocation)
virtual Cardinality cardinality() const =0
Represents an item in the XPath 2.0 Data Model.
Definition: qitem_p.h:182
static Cardinality exactlyOne()
virtual Item evaluateSingleton(const DynamicContext::Ptr &context) const
const SequenceType::Ptr m_targetType
Definition: qcastas_p.h:140

◆ expectedOperandTypes()

SequenceType::List CastAs::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 qcastas.cpp.

188 {
189  SequenceType::List result;
190 
193  else
195 
196  return result;
197 }
static const SequenceType::Ptr ExactlyOneAtomicType
void append(const T &t)
Inserts value at the end of the list.
Definition: qlist.h:507
virtual Cardinality cardinality() const =0
static const SequenceType::Ptr ZeroOrOneAtomicType
const SequenceType::Ptr m_targetType
Definition: qcastas_p.h:140
The QList class is a template class that provides lists.
Definition: qdatastream.h:62

◆ staticType()

SequenceType::Ptr CastAs::staticType ( ) const
virtual
Returns
a SequenceType where the ItemType is this CastAs's target type, as per targetType(), and the Cardinality is inferred from the source operand to reflect whether this CastAs always will evaluate to exactly-one or zero-or-one values.

Implements QPatternist::Expression.

Definition at line 178 of file qcastas.cpp.

179 {
181  return m_targetType;
182  else
183  return makeGenericSequenceType(m_targetType->itemType(),
185 }
virtual Cardinality cardinality() const =0
virtual SequenceType::Ptr staticType() const =0
static Cardinality exactlyOne()
virtual ItemType::Ptr itemType() const =0
const SequenceType::Ptr m_targetType
Definition: qcastas_p.h:140

◆ targetSequenceType()

SequenceType::Ptr QPatternist::CastAs::targetSequenceType ( ) const
inline

Definition at line 129 of file qcastas_p.h.

130  {
131  return m_targetType;
132  }
const SequenceType::Ptr m_targetType
Definition: qcastas_p.h:140

◆ targetType()

ItemType::Ptr QPatternist::CastAs::targetType ( ) const
inline

Definition at line 124 of file qcastas_p.h.

125  {
126  return m_targetType->itemType();
127  }
const SequenceType::Ptr m_targetType
Definition: qcastas_p.h:140

◆ typeCheck()

Expression::Ptr CastAs::typeCheck ( const StaticContext::Ptr context,
const SequenceType::Ptr reqType 
)
virtual

Overridden in order to check that casting to an abstract type is not attempted.

Reimplemented from QPatternist::Expression.

Definition at line 98 of file qcastas.cpp.

100 {
101  checkTargetType(context);
102  const SequenceType::Ptr seqt(m_operand->staticType());
103  ItemType::Ptr t(seqt->itemType());
104 
105  /* Special case xs:QName */
106  if(BuiltinTypes::xsQName->xdtTypeMatches(m_targetType->itemType()))
107  {
108  /* Ok, We're casting to xs:QName. */
109  if(m_operand->is(IDStringValue)) /* A valid combination, let's do the cast. */
110  return castToQName(context)->typeCheck(context, reqType);
111  else if(BuiltinTypes::xsQName->xdtTypeMatches(t))
112  return m_operand->typeCheck(context, reqType);
113  else if(seqt->cardinality().isEmpty() && m_targetType->cardinality().allowsEmpty())
114  return EmptySequence::create(this, context);
115  else if(!(seqt->cardinality().isEmpty() && !m_targetType->cardinality().allowsEmpty()))
116  {
117  context->error(QtXmlPatterns::tr("When casting to %1 or types "
118  "derived from it, the source "
119  "value must be of the same type, "
120  "or it must be a string literal. "
121  "Type %2 is not allowed.")
122  .arg(formatType(context->namePool(), BuiltinTypes::xsQName))
123  .arg(formatType(context->namePool(), seqt)),
125  return Expression::Ptr(this);
126  }
127  }
128 
129  const Expression::Ptr me(SingleContainer::typeCheck(context, reqType));
130  /* Type may have changed, such as that atomization has been applied. */
131  t = m_operand->staticType()->itemType();
132 
134  !BuiltinTypes::xsDayTimeDuration->xdtTypeMatches(t) &&
135  !BuiltinTypes::xsYearMonthDuration->xdtTypeMatches(t))
136  { /* At least casting is superflorous. */
138  return m_operand; /* The whole cast expression is redundant. */
139  else
140  { /* Only cardinality check is needed, rewrite to CardinalityVerifier. */
144  }
145  }
146 
147  /* Let the CastingPlatform look up its AtomicCaster. */
148  prepareCasting(context, t);
149 
150  return me;
151 }
static const AtomicType::Ptr xsDayTimeDuration
Verifies that the sequence an Expression evaluates to conforms to a Cardinality.
QString formatType(const NamePool::Ptr &np, const T &type)
Formats ItemType and SequenceType.
virtual NamePool::Ptr namePool() const =0
virtual Expression::Ptr typeCheck(const StaticContext::Ptr &context, const SequenceType::Ptr &reqType)
Definition: qexpression.cpp:70
static Expression::Ptr create(const Expression *const replacementFor, const StaticContext::Ptr &context)
Creates an EmptySequence that is a replacement for replacementFor.
static const AtomicType::Ptr xsYearMonthDuration
void error(const QString &message, const ReportContext::ErrorCode errorCode, const QSourceLocation &sourceLocation)
bool prepareCasting(const ReportContext::Ptr &context, const ItemType::Ptr &sourceType)
virtual Cardinality cardinality() const =0
Expression::Ptr castToQName(const StaticContext::Ptr &context) const
Definition: qcastas.cpp:162
QExplicitlySharedDataPointer< Expression > Ptr
A smart pointer wrapping mutable Expression instances.
virtual SequenceType::Ptr staticType() const =0
static const AtomicType::Ptr xsQName
void checkTargetType(const ReportContext::Ptr &context) const
virtual bool xdtTypeMatches(const ItemType::Ptr &other) const =0
Returns true if other matches this type. That is, if other is equal to this type or a subtype of this...
virtual ItemType::Ptr itemType() const =0
bool isMatch(const Cardinality &other) const
bool is(const ID id) const
const SequenceType::Ptr m_targetType
Definition: qcastas_p.h:140

Properties

◆ m_targetType

const SequenceType::Ptr QPatternist::CastAs::m_targetType
private

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