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

When combined with other components, implements xsl:apply-templates. More...

#include <qapplytemplate_p.h>

Inheritance diagram for QPatternist::ApplyTemplate:
QPatternist::TemplateInvoker QPatternist::CallSite QPatternist::UnlimitedContainer QPatternist::Expression QSharedData QPatternist::CppCastingHelper< Expression > QPatternist::SourceLocationReflection

Public Types

typedef QExplicitlySharedDataPointer< ApplyTemplatePtr
 
- 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
 

Public Functions

virtual ExpressionVisitorResult::Ptr accept (const ExpressionVisitor::Ptr &visitor) const
 
 ApplyTemplate (const TemplateMode::Ptr &mode, const WithParam::Hash &withParams, const TemplateMode::Ptr &defaultMode)
 mode may be null. If it is, ApplyTemplate interprets that as that it should use the #current mode. More...
 
virtual Expression::Ptr body () const
 Returns the body of the function/template/component that is being called. More...
 
virtual CallTargetDescription::Ptr callTargetDescription () const
 
Expression::Ptr compress (const StaticContext::Ptr &context)
 
virtual bool configureRecursion (const CallTargetDescription::Ptr &sign)
 
virtual Item::Iterator::Ptr evaluateSequence (const DynamicContext::Ptr &context) const
 
Item mapToItem (const QXmlNodeModelIndex &node, const DynamicContext::Ptr &context) const
 
Item::Iterator::Ptr mapToSequence (const Item &item, const DynamicContext::Ptr &context) const
 
TemplateMode::Ptr mode () const
 
virtual Properties properties () const
 
virtual SequenceType::Ptr staticType () const
 
- Public Functions inherited from QPatternist::TemplateInvoker
virtual SequenceType::List expectedOperandTypes () const
 
const WithParam::HashwithParams () const
 
- Public Functions inherited from QPatternist::CallSite
bool isRecursive () const
 
QXmlName name () const
 
void setIsRecursive (const bool value)
 
- 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 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
 
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 ()
 

Private Types

typedef QExplicitlySharedDataPointer< const ApplyTemplateConstPtr
 

Private Functions

TemplateMode::Ptr effectiveMode (const DynamicContext::Ptr &context) const
 
Template::Ptr findTemplate (const DynamicContext::Ptr &context, const TemplateMode::Ptr &templateMode) const
 

Properties

TemplateMode::Ptr m_defaultMode
 
const TemplateMode::Ptr m_mode
 

Additional Inherited Members

- 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 QPatternist::TemplateInvoker
WithParam::Hash m_withParams
 
- Public Variables inherited from QSharedData
QAtomicInt ref
 
- Protected Functions inherited from QPatternist::TemplateInvoker
 TemplateInvoker (const WithParam::Hash &withParams, const QXmlName &name=QXmlName())
 
- Protected Functions inherited from QPatternist::CallSite
 CallSite (const QXmlName &name=QXmlName())
 
- 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

When combined with other components, implements xsl:apply-templates.

Note that ApplyTemplate isn't named ApplyTemplates. The reason for this is that ApplyTemplate doesn't do the iteration part. An <xsl:apply-templates/> instruction is rewritten into:

child::node/() map apply-template()

Hence, this expression requires a focus, although it can consist of atomic values.

Since
4.5
Author
Frans Englich frans.nosp@m..eng.nosp@m.lich@.nosp@m.noki.nosp@m.a.com

Definition at line 81 of file qapplytemplate_p.h.

Typedefs

◆ ConstPtr

Definition at line 121 of file qapplytemplate_p.h.

◆ Ptr

Definition at line 84 of file qapplytemplate_p.h.

Constructors and Destructors

◆ ApplyTemplate()

ApplyTemplate::ApplyTemplate ( const TemplateMode::Ptr mode,
const WithParam::Hash withParams,
const TemplateMode::Ptr defaultMode 
)

mode may be null. If it is, ApplyTemplate interprets that as that it should use the #current mode.

See also
StaticContext::currentTemplateMode()

Definition at line 57 of file qapplytemplate.cpp.

59  : TemplateInvoker(withParams)
60  , m_mode(mode)
61  , m_defaultMode(defaultMode)
62 {
64  "Either a mode, or the default mode must be supplied.");
65 }
const TemplateMode::Ptr m_mode
TemplateInvoker(const WithParam::Hash &withParams, const QXmlName &name=QXmlName())
TemplateMode::Ptr m_defaultMode
#define Q_ASSERT_X(cond, where, what)
Definition: qglobal.h:1837
#define Q_FUNC_INFO
Definition: qglobal.h:1871

Functions

◆ accept()

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

Implements QPatternist::Expression.

Definition at line 181 of file qapplytemplate.cpp.

182 {
183  return visitor->visit(this);
184 }

◆ body()

Expression::Ptr ApplyTemplate::body ( ) const
virtual

Returns the body of the function/template/component that is being called.

Implements QPatternist::CallSite.

Definition at line 198 of file qapplytemplate.cpp.

199 {
200  Q_ASSERT_X(false, Q_FUNC_INFO, "We're not expected to be called.");
201  return Expression::Ptr();
202 }
QExplicitlySharedDataPointer< Expression > Ptr
A smart pointer wrapping mutable Expression instances.
#define Q_ASSERT_X(cond, where, what)
Definition: qglobal.h:1837
#define Q_FUNC_INFO
Definition: qglobal.h:1871

◆ callTargetDescription()

CallTargetDescription::Ptr ApplyTemplate::callTargetDescription ( ) const
virtual

Implements QPatternist::CallSite.

Definition at line 204 of file qapplytemplate.cpp.

205 {
206  Q_ASSERT_X(false, Q_FUNC_INFO, "We're not expected to be called.");
208 }
#define Q_ASSERT_X(cond, where, what)
Definition: qglobal.h:1837
QExplicitlySharedDataPointer< CallTargetDescription > Ptr
#define Q_FUNC_INFO
Definition: qglobal.h:1871

◆ compress()

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

compress() is the last stage performs in compiling an expression, done after the initial AST build and calling typeCheck(). compress() performs crucial simplifications, either by having drastic performance implications or that some expressions depend on it for proper behavior.

The default implementation performs a sparse conditional constant propagation. In short, a recursive process is performed in the AST which examines if the Expression's operands are constant values, and if so, performs a const fold(AST rewrite) into the result of evaluating the expression in question. This default behavior can be disabled by letting properties() return DisableElimination.

This compress() stage can be relative effective due to the design of XPath, in part because intrinsic functions are heavily used. Many Expressions override compress() and do optimizations specific to what they do. Also, many Expressions performs optimizations in their typeCheck().

Parameters
contextthe static context. Supplies compile time information, and is the channel for communicating error messages.
See also
Wikipedia, the free encyclopedia, Sparse conditional constant propagation
Wikipedia, the free encyclopedia, Intrinsic function
Wikipedia, the free encyclopedia, Compiler optimization

Reimplemented from QPatternist::TemplateInvoker.

Definition at line 167 of file qapplytemplate.cpp.

168 {
169  /* If we have a mode, we will never need the default mode. */
170  if(m_mode)
172 
173  return TemplateInvoker::compress(context);
174 }
virtual Expression::Ptr compress(const StaticContext::Ptr &context)
const TemplateMode::Ptr m_mode
TemplateMode::Ptr m_defaultMode
void reset()
Resets this to be null.
Definition: qshareddata.h:150

◆ configureRecursion()

bool ApplyTemplate::configureRecursion ( const CallTargetDescription::Ptr sign)
virtual

Called in the earliest stages of the compilation process. sign can be any function signature for a user declared function. If sign matches this UserFunctionCallsite, it means the UserFunction represented by sign is recursive and that this UserFunctionCallsite should take appropriate measures.

Returns
true if is recursive, otherwise false

Implements QPatternist::CallSite.

Definition at line 191 of file qapplytemplate.cpp.

192 {
193  Q_ASSERT_X(false, Q_FUNC_INFO, "We're not expected to be called.");
194  Q_UNUSED(sign);
195  return false;
196 }
#define Q_ASSERT_X(cond, where, what)
Definition: qglobal.h:1837
#define Q_UNUSED(x)
Indicates to the compiler that the parameter with the specified name is not used in the body of a fun...
Definition: qglobal.h:1729
#define Q_FUNC_INFO
Definition: qglobal.h:1871

◆ effectiveMode()

TemplateMode::Ptr ApplyTemplate::effectiveMode ( const DynamicContext::Ptr context) const
inlineprivate

Definition at line 80 of file qapplytemplate.cpp.

Referenced by evaluateSequence().

81 {
82  if(m_mode)
83  return m_mode;
84  else
85  {
86  const TemplateMode::Ptr currentMode(context->currentTemplateMode());
87 
88  if(currentMode)
89  return currentMode;
90  else
91  return m_defaultMode;
92  }
93 }
const TemplateMode::Ptr m_mode
TemplateMode::Ptr m_defaultMode
virtual QExplicitlySharedDataPointer< TemplateMode > currentTemplateMode() const =0
Returns the current template mode that is in effect.

◆ evaluateSequence()

Item::Iterator::Ptr ApplyTemplate::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 137 of file qapplytemplate.cpp.

Referenced by mapToSequence().

138 {
139  const TemplateMode::Ptr templateMode(effectiveMode(context));
140  const Template::Ptr &templateMatch = findTemplate(context, templateMode);
141 
142  if(templateMatch)
143  return templateMatch->body->evaluateSequence(templateMatch->createContext(this, context, false));
144  else
145  {
146  /* None of our templates matched. Proceed with a built-in. */
147  const Item current(context->contextItem());
148  // TODO it can be an atomic value?
149  const QXmlNodeModelIndex::NodeKind kind(current.asNode().kind());
150 
152  {
153  pDebug() << "No template match, using builtin template for element() | document-node()";
154 
155  const Item::Iterator::Ptr focusIterator(makeItemMappingIterator<Item>(ConstPtr(this),
156  current.asNode().iterate(QXmlNodeModelIndex::AxisChild),
157  context));
158 
159  const DynamicContext::Ptr focus(context->createFocus());
160  focus->setFocusIterator(focusIterator);
161  return makeSequenceMappingIterator<Item>(ConstPtr(this), focusIterator, focus);
162  }
164  }
165 }
QExplicitlySharedDataPointer< const ApplyTemplate > ConstPtr
static const EmptyIterator< Item >::Ptr emptyIterator
Expression::Ptr body
Definition: qtemplate_p.h:98
DynamicContext::Ptr createFocus()
virtual Item contextItem() const =0
virtual void setFocusIterator(const Item::Iterator::Ptr &it)=0
Template::Ptr findTemplate(const DynamicContext::Ptr &context, const TemplateMode::Ptr &templateMode) const
Represents an item in the XPath 2.0 Data Model.
Definition: qitem_p.h:182
NodeKind
Identifies a kind of node.
TemplateMode::Ptr effectiveMode(const DynamicContext::Ptr &context) const
DynamicContext::Ptr createContext(const TemplateInvoker *const invoker, const DynamicContext::Ptr &context, const bool isCallTemplate) const
Definition: qtemplate.cpp:81
virtual Item::Iterator::Ptr evaluateSequence(const DynamicContext::Ptr &context) const
QNoDebug pDebug()
Definition: qdebug_p.h:99

◆ findTemplate()

Template::Ptr ApplyTemplate::findTemplate ( const DynamicContext::Ptr context,
const TemplateMode::Ptr templateMode 
) const
private

Definition at line 95 of file qapplytemplate.cpp.

Referenced by evaluateSequence().

97 {
98  const int count = templateMode->templatePatterns.count();
99  Template::Ptr result;
100  /* It's redundant to initialize these values, but it suppresses false
101  * positives with GCC. */
102  PatternPriority priority = 0;
103  TemplatePattern::ID id = -1;
104 
105  /* Possible optimization: detecting ambiguous rule matches could be forked off to a
106  * low prioirity thread. */
107  for(int i = 0; i < count; ++i)
108  {
109  const TemplatePattern::Ptr &candidate = templateMode->templatePatterns.at(i);
110  if(candidate->matchPattern()->evaluateEBV(context))
111  {
112  if(result)
113  {
114  if( candidate->id() != id
115  && candidate->priority() == priority
116  && candidate->templateTarget()->importPrecedence ==
117  result->importPrecedence)
118  {
119  context->error(QtXmlPatterns::tr("Ambiguous rule match."),
121  }
122  else
123  break;
124  }
125  else
126  {
127  result = candidate->templateTarget();
128  priority = candidate->priority();
129  id = candidate->id();
130  }
131  }
132  }
133 
134  return result;
135 }
const ImportPrecedence importPrecedence
Definition: qtemplate_p.h:105
int count(const T &t) const
Returns the number of occurrences of value in the vector.
Definition: qvector.h:742
TemplatePattern::Vector templatePatterns
void error(const QString &message, const ReportContext::ErrorCode errorCode, const QSourceLocation &sourceLocation)
const T & at(int i) const
Returns the item at index position i in the vector.
Definition: qvector.h:350
xsDouble PatternPriority

◆ mapToItem()

Item ApplyTemplate::mapToItem ( const QXmlNodeModelIndex node,
const DynamicContext::Ptr context 
) const
inline

The only reason this function exists, is in order to convert QXmlNodeModelIndex to QPatternist::Item. So, this is a huge performance setback. It applies for one of the builtin templates.

Definition at line 67 of file qapplytemplate.cpp.

69 {
70  return Item(node);
71 }
Represents an item in the XPath 2.0 Data Model.
Definition: qitem_p.h:182

◆ mapToSequence()

Item::Iterator::Ptr ApplyTemplate::mapToSequence ( const Item item,
const DynamicContext::Ptr context 
) const
inline

Definition at line 73 of file qapplytemplate.cpp.

75 {
76  Q_UNUSED(item);
77  return evaluateSequence(context);
78 }
virtual Item::Iterator::Ptr evaluateSequence(const DynamicContext::Ptr &context) const
#define Q_UNUSED(x)
Indicates to the compiler that the parameter with the specified name is not used in the body of a fun...
Definition: qglobal.h:1729

◆ mode()

TemplateMode::Ptr QPatternist::ApplyTemplate::mode ( ) const
inline

Definition at line 135 of file qapplytemplate_p.h.

136  {
137  return m_mode;
138  }
const TemplateMode::Ptr m_mode

◆ properties()

Expression::Properties ApplyTemplate::properties ( ) const
virtual
Returns
a bitwise OR'd value of properties, describing the characteristics of the expression. These properties affects how this Expression is treated in for example type checking stages.

The default implementation returns 0. Override and let the function return a different value, if that's of interest.

An important decision when re-implementing properties() is whether to OR in the properties() of ones operands. For instance, if an operand has RequiresFocus set, that flag nost likely applies to the apparent as well, since it depends on its operand.

See also
deepProperties()
Returns
Expression::None, meaning no special properties

Reimplemented from QPatternist::Expression.

Definition at line 186 of file qapplytemplate.cpp.

◆ staticType()

SequenceType::Ptr ApplyTemplate::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 176 of file qapplytemplate.cpp.

177 {
179 }
static const SequenceType::Ptr ZeroOrMoreItems

Properties

◆ m_defaultMode

TemplateMode::Ptr QPatternist::ApplyTemplate::m_defaultMode
private

Definition at line 130 of file qapplytemplate_p.h.

Referenced by ApplyTemplate(), compress(), and effectiveMode().

◆ m_mode

const TemplateMode::Ptr QPatternist::ApplyTemplate::m_mode
private
Note
You typically want to use effectiveMode().

Definition at line 128 of file qapplytemplate_p.h.

Referenced by ApplyTemplate(), compress(), effectiveMode(), and mode().


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