1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the QtXmlPatterns module of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
42 #include "qdynamiccontextstore_p.h"
43 #include "qpatternistlocale_p.h"
45 #include "qtemplate_p.h"
49 using namespace QPatternist;
51 const SourceLocationReflection* Template::actualReflection() const
56 DynamicContext::TemplateParameterHash Template::parametersAsHash() const
58 DynamicContext::TemplateParameterHash retval;
59 const int len = templateParameters.count();
61 for(int i = 0; i < len; ++i)
63 const VariableDeclaration::Ptr &at = templateParameters.at(i);
64 retval.insert(at->name, at->expression());
70 void Template::raiseXTSE0680(const ReportContext::Ptr &context,
72 const SourceLocationReflection *const reflection)
74 context->error(QtXmlPatterns::tr("The parameter %1 is passed, but no corresponding %2 exists.")
75 .arg(formatKeyword(context->namePool(), name),
76 formatKeyword(QLatin1String("xsl:param"))),
77 ReportContext::XTSE0680,
81 DynamicContext::Ptr Template::createContext(const TemplateInvoker *const invoker,
82 const DynamicContext::Ptr &context,
83 const bool isCallTemplate) const
89 * - xsl:params in the target template (if any) which may provide
91 * - xsl:with-params in the caller (if any) which provides values.
93 * We need to, for each parameter:
94 * - If the called template provides no default value and the caller
95 * has no value, it's an error
96 * - If the called template has a default value and the caller provides
97 * none, it should be used
98 * - In any case the caller provides a value, it needs to be used.
100 * Problems to look out for:
102 * - Each xsl:param is in scope for the subsequent xsl:params. Hence,
103 * the evaluation of one xsl:param can depend on another xsl:param,
105 * - The focus for xsl:params is different from the focus for
106 * the xsl:with-params
107 * - The xsl:with-params are not in scope for the xsl:params.
110 WithParam::Hash withParams(invoker->withParams());
113 * Parameters or not, we must in any case create a new stack frame
114 * for the template invocation since otherwise we will trash our existing
115 * variables. Hence it's as with calling user functions.
117 * This is especially reproducible with recursive functions.
119 DynamicContext::Ptr newStack(context->createStack());
121 /* We have no parameters, and we have no further error checking to
122 * do in the case of not being xsl:apply-templates, so we need to do nothing. */
123 if(templateParameters.isEmpty() && (!isCallTemplate || withParams.isEmpty()))
126 const DynamicContext::TemplateParameterHash hashedParams(parametersAsHash());
127 DynamicContext::TemplateParameterHash sewnTogether(hashedParams);
129 const DynamicContext::TemplateParameterHash::iterator end(sewnTogether.end());
131 for(DynamicContext::TemplateParameterHash::iterator it(sewnTogether.begin());
135 Expression::Ptr ¶m = it.value();
137 WithParam::Ptr &withParam = withParams[it.key()];
140 param = Expression::Ptr(new DynamicContextStore(withParam->sourceExpression(), context));
143 /* Ops, no xsl:with-param and no default value to cover up for it.
145 context->error(QtXmlPatterns::tr("The parameter %1 is required, but no corresponding %2 is supplied.")
146 .arg(formatKeyword(context->namePool(), it.key()),
147 formatKeyword(QLatin1String("xsl:with-param"))),
148 ReportContext::XTSE0690,
155 /* Find xsl:with-param that has no corresponding xsl:param. */
156 /* Optimization: candidate for threading? */
158 const WithParam::Hash::const_iterator end(withParams.constEnd());
160 for(WithParam::Hash::const_iterator it(withParams.constBegin()); it != end; ++it)
162 if(!hashedParams.contains(it.key()))
163 raiseXTSE0680(context, it.key(), this);
168 newStack->templateParameterStore() = sewnTogether;
172 void Template::compileParameters(const StaticContext::Ptr &context)
176 const int len = templateParameters.count();
178 for(int i = 0; i < len; ++i)
180 const VariableDeclaration::Ptr &at = templateParameters.at(i);
182 /* If our value is required, we don't have a default value. */
185 // TODO why do we pass in its own type here?
186 at->setExpression(at->expression()->typeCheck(context, at->expression()->staticType()));
188 at->setExpression(at->expression()->compress(context));
193 Expression::Properties Template::properties() const
195 return Expression::DisableElimination; /* We're having issues with recursion detection, so this path currently loops infintely. */
197 Expression::Properties collect(body->properties());
199 VariableDeclaration::List::const_iterator end(templateParameters.constEnd());
201 for(VariableDeclaration::List::const_iterator it(templateParameters.constBegin());
205 if((*it)->expression())
206 collect |= (*it)->expression()->properties();
210 return collect & (Expression::RequiresFocus | Expression::IsEvaluated | Expression::DisableElimination);
213 Expression::Properties Template::dependencies() const
215 return Expression::DisableElimination; /* We're having issues with recursion detection, so this path currently loops infintely. */
217 Expression::Properties collect(body->dependencies());
219 VariableDeclaration::List::const_iterator end(templateParameters.constEnd());
221 for(VariableDeclaration::List::const_iterator it(templateParameters.constBegin());
225 if((*it)->expression())
226 collect |= (*it)->expression()->dependencies();
229 return collect & (Expression::RequiresFocus | Expression::IsEvaluated | Expression::DisableElimination);