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

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

#include <qcastableas_p.h>

Inheritance diagram for QPatternist::CastableAs:
QPatternist::SingleContainer QPatternist::CastingPlatform< CastableAs, false > QPatternist::Expression QSharedData QPatternist::CppCastingHelper< Expression > QPatternist::SourceLocationReflection

Public Functions

virtual ExpressionVisitorResult::Ptr accept (const ExpressionVisitor::Ptr &visitor) const
 
 CastableAs (const Expression::Ptr &operand, const SequenceType::Ptr &targetType)
 
virtual Expression::Ptr compress (const StaticContext::Ptr &context)
 
virtual bool evaluateEBV (const DynamicContext::Ptr &) const
 
virtual SequenceType::List expectedOperandTypes () const
 
virtual SequenceType::Ptr staticType () 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 Item::Iterator::Ptr evaluateSequence (const DynamicContext::Ptr &context) const
 
virtual Item evaluateSingleton (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 ()
 

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< CastableAs, false >
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 castable as expression.

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

Definition at line 73 of file qcastableas_p.h.

Constructors and Destructors

◆ CastableAs()

CastableAs::CastableAs ( const Expression::Ptr operand,
const SequenceType::Ptr targetType 
)

Definition at line 56 of file qcastableas.cpp.

57  : SingleContainer(operand),
58  m_targetType(tType)
59 {
60  Q_ASSERT(tType);
61  Q_ASSERT(!tType->cardinality().allowsMany());
62  Q_ASSERT(tType->itemType()->isAtomicType());
63 }
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
SingleContainer(const Expression::Ptr &operand)
const SequenceType::Ptr m_targetType

Functions

◆ accept()

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

Implements QPatternist::Expression.

Definition at line 152 of file qcastableas.cpp.

153 {
154  return visitor->visit(this);
155 }

◆ compress()

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

Overridden to const fold to true when the target type is a type which casting to always succeeds. This is the type identical to the target type, xs:string, and xs:untypedAtomic.

Reimplemented from QPatternist::Expression.

Definition at line 86 of file qcastableas.cpp.

87 {
88  const Expression::Ptr me(SingleContainer::compress(context));
89 
90  if(me != this) /* We already managed to const fold, how convenient. */
91  return me;
92 
94 
95  const SequenceType::Ptr opType(m_operand->staticType());
96 
97  /* Casting to these always succeeds, assuming the cardinality also matches,
98  * although the cardinality can fail. */
99  if(( *t == *BuiltinTypes::xsString
101  || *t == *opType->itemType())
102  &&(m_targetType->cardinality().isMatch(opType->cardinality())))
103  {
104  return wrapLiteral(CommonValues::BooleanTrue, context, this);
105  }
106  else
107  return me;
108 }
static const AtomicValue::Ptr BooleanTrue
virtual Expression::Ptr compress(const StaticContext::Ptr &context)
static const AtomicType::Ptr xsString
virtual Cardinality cardinality() const =0
static const AtomicType::Ptr xsUntypedAtomic
virtual SequenceType::Ptr staticType() const =0
const SequenceType::Ptr m_targetType
virtual ItemType::Ptr itemType() const =0
bool isMatch(const Cardinality &other) const

◆ evaluateEBV()

bool CastableAs::evaluateEBV ( const DynamicContext::Ptr context) const
virtual

Determines the Effective Boolean Value of the expression.

The Effective Boolean Value of a value is not necessarily the same as converting the value to a new value of type xs:boolean.

Note that this function cannot return the empty sequence, evaluateSingleton() must be overridden in order to be able to do that.

The default implementation results in a type error. Hence, this function must be overridden if such behavior is not of interest.

See also
XML Path Language (XPath) 2.0, 2.4.3 Effective Boolean Value

Reimplemented from QPatternist::Expression.

Definition at line 65 of file qcastableas.cpp.

66 {
67  Item item;
68 
70  {
72  item = it->next();
73 
74  if(it->next())
75  return false;
76  }
77  else
78  item = m_operand->evaluateSingleton(context);
79 
80  if(item)
81  return !cast(item, context).as<AtomicValue>()->hasError();
82  else
84 }
Item cast(const Item &sourceValue, const ReportContext::Ptr &context) const
#define it(className, varName)
The QExplicitlySharedDataPointer class represents a pointer to an explicitly shared object...
Definition: qshareddata.h:136
Base class for all classes representing atomic values.
Definition: qitem_p.h:118
virtual Cardinality cardinality() const =0
virtual SequenceType::Ptr staticType() const =0
const SequenceType::Ptr m_targetType
Represents an item in the XPath 2.0 Data Model.
Definition: qitem_p.h:182
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

◆ expectedOperandTypes()

SequenceType::List CastableAs::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 140 of file qcastableas.cpp.

141 {
142  SequenceType::List result;
144  return result;
145 }
void append(const T &t)
Inserts value at the end of the list.
Definition: qlist.h:507
static const SequenceType::Ptr ZeroOrMoreAtomicTypes
The QList class is a template class that provides lists.
Definition: qdatastream.h:62

◆ staticType()

SequenceType::Ptr CastableAs::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 147 of file qcastableas.cpp.

148 {
150 }
static const SequenceType::Ptr ExactlyOneBoolean

◆ targetType()

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

Definition at line 97 of file qcastableas_p.h.

98  {
99  return m_targetType->itemType();
100  }
const SequenceType::Ptr m_targetType

◆ typeCheck()

Expression::Ptr CastableAs::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 110 of file qcastableas.cpp.

112 {
113  checkTargetType(context);
114  const Expression::Ptr me(SingleContainer::typeCheck(context, reqType));
115 
116  return me;
117  if(BuiltinTypes::xsQName->xdtTypeMatches(m_targetType->itemType()))
118  {
119  const SequenceType::Ptr seqt(m_operand->staticType());
120  /* We can cast a string literal, an xs:QName value, and an
121  * empty sequence(if empty is allowed), to xs:QName. */
122  if(m_operand->is(IDStringValue) ||
123  BuiltinTypes::xsQName->xdtTypeMatches(seqt->itemType()) ||
124  (*seqt->itemType() == *CommonSequenceTypes::Empty && m_targetType->cardinality().allowsEmpty()))
125  {
126  return wrapLiteral(CommonValues::BooleanTrue, context, this)->typeCheck(context, reqType);
127  }
128  else
129  return wrapLiteral(CommonValues::BooleanFalse, context, this)->typeCheck(context, reqType);
130  }
131  else
132  {
133  /* Let the CastingPlatform look up its AtomicCaster. */
135 
136  return me;
137  }
138 }
static const AtomicValue::Ptr BooleanTrue
virtual Expression::Ptr typeCheck(const StaticContext::Ptr &context, const SequenceType::Ptr &reqType)
Definition: qexpression.cpp:70
bool prepareCasting(const ReportContext::Ptr &context, const ItemType::Ptr &sourceType)
virtual Cardinality cardinality() const =0
virtual SequenceType::Ptr staticType() const =0
const SequenceType::Ptr m_targetType
static const EmptySequenceType::Ptr Empty
static const AtomicType::Ptr xsQName
void checkTargetType(const ReportContext::Ptr &context) const
virtual ItemType::Ptr itemType() const =0
static const AtomicValue::Ptr BooleanFalse
bool is(const ID id) const

Properties

◆ m_targetType

const SequenceType::Ptr QPatternist::CastableAs::m_targetType
private

Definition at line 103 of file qcastableas_p.h.

Referenced by compress(), evaluateEBV(), targetType(), and typeCheck().


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