1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the QtXmlPatterns module of the Qt Toolkit.
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
42 #include "qcommonsequencetypes_p.h"
43 #include "qdynamiccontextstore_p.h"
44 #include "qevaluationcache_p.h"
46 #include "quserfunctioncallsite_p.h"
50 using namespace QPatternist;
52 UserFunctionCallsite::UserFunctionCallsite(const QXmlName nameP,
53 const FunctionSignature::Arity ar) : CallSite(nameP)
55 , m_expressionSlotOffset(-2)
60 Item::Iterator::Ptr UserFunctionCallsite::evaluateSequence(const DynamicContext::Ptr &context) const
62 return m_body->evaluateSequence(bindVariables(context));
65 Item UserFunctionCallsite::evaluateSingleton(const DynamicContext::Ptr &context) const
67 return m_body->evaluateSingleton(bindVariables(context));
70 bool UserFunctionCallsite::evaluateEBV(const DynamicContext::Ptr &context) const
72 return m_body->evaluateEBV(bindVariables(context));
75 void UserFunctionCallsite::evaluateToSequenceReceiver(const DynamicContext::Ptr &context) const
77 m_body->evaluateToSequenceReceiver(bindVariables(context));
80 DynamicContext::Ptr UserFunctionCallsite::bindVariables(const DynamicContext::Ptr &context) const
82 const DynamicContext::Ptr stackContext(context->createStack());
83 Q_ASSERT(stackContext);
85 const Expression::List::const_iterator end(m_operands.constEnd());
86 Expression::List::const_iterator it(m_operands.constBegin());
88 VariableSlotID slot = m_expressionSlotOffset;
90 for(; it != end; ++it)
92 stackContext->setExpressionVariable(slot,
93 Expression::Ptr(new DynamicContextStore(*it, context)));
100 SequenceType::List UserFunctionCallsite::expectedOperandTypes() const
102 SequenceType::List result;
104 if(m_functionDeclaration)
106 const FunctionArgument::List args(m_functionDeclaration->signature()->arguments());
107 const FunctionArgument::List::const_iterator end(args.constEnd());
108 FunctionArgument::List::const_iterator it(args.constBegin());
110 for(; it != end; ++it)
111 result.append((*it)->type());
114 result.append(CommonSequenceTypes::ZeroOrMoreItems);
119 Expression::Ptr UserFunctionCallsite::typeCheck(const StaticContext::Ptr &context,
120 const SequenceType::Ptr &reqType)
122 /* The parser calls TypeChecker::applyFunctionConversion() on user function
123 * bodies, possibly indirectly, before all function call sites have been
124 * resolved. Hence it's possible that we're called before before the usual
125 * typeCheck() pass, and hence before we have been resolved/checked and
126 * subsequently m_functionDeclaration set. Therefore, encounter for that below.
128 * UnresolvedVariableReference::typeCheck() has the same dilemma.
131 /* Ensure that the return value of the function is properly
132 * converted/does match from where it is called(which is here). */
133 if(isRecursive() || !m_functionDeclaration)
134 return CallSite::typeCheck(context, reqType);
137 /* Update, such that we use a recent version of the body that has typeCheck()
138 * and compress() rewrites included. */
139 m_body = m_functionDeclaration->body();
141 /* Note, we can't assign to m_functionDeclaration->body() because UserFunction can apply
142 * to several different callsites. Hence we need our own version. */
143 m_body = m_body->typeCheck(context, reqType);
145 /* We just act as a pipe for m_body, so we don't have to typecheck ourselves. However,
146 * the arguments must match the function declaration. */
147 typeCheckOperands(context);
148 return Expression::Ptr(this);
152 Expression::Ptr UserFunctionCallsite::compress(const StaticContext::Ptr &context)
155 rewrite(m_body, m_body->compress(context), context);
157 return CallSite::compress(context);
160 Expression::Properties UserFunctionCallsite::properties() const
162 return DisableElimination;
165 SequenceType::Ptr UserFunctionCallsite::staticType() const
167 /* Our return type, is the static type of the function body. We could have also used
168 * m_functionDeclaration->signature()->returnType(), but it doesn't get updated
169 * when function conversion is applied.
170 * We can't use m_body's type if we're recursive, because m_body computes its type
171 * from its children, and we're at least one of the children. Hence, we would
172 * recurse infinitely if we did.
174 * m_body can be null here if we're called before setSource().
176 if(isRecursive() || !m_body)
177 return CommonSequenceTypes::ZeroOrMoreItems; // TODO use the declaration, it can have a type explicitly.
179 return m_body->staticType();
182 ExpressionVisitorResult::Ptr UserFunctionCallsite::accept(const ExpressionVisitor::Ptr &visitor) const
184 return visitor->visit(this);
187 Expression::ID UserFunctionCallsite::id() const
189 return IDUserFunctionCallsite;
192 bool UserFunctionCallsite::isSignatureValid(const FunctionSignature::Ptr &sign) const
196 return sign->name() == name()
198 sign->isArityValid(m_arity);
201 bool UserFunctionCallsite::configureRecursion(const CallTargetDescription::Ptr &sign)
205 setIsRecursive(isSignatureValid(sign));
206 return isRecursive();
209 void UserFunctionCallsite::setSource(const UserFunction::Ptr &userFunction,
210 const VariableSlotID cacheSlotOffset)
212 m_functionDeclaration = userFunction;
213 m_body = userFunction->body();
214 m_expressionSlotOffset = userFunction->expressionSlotOffset();
216 const int len = m_operands.size();
218 const VariableDeclaration::List varDecls(userFunction->argumentDeclarations());
220 for(int i = 0; i < len; ++i)
222 /* We don't want evaluation caches for range variables, it's not necessary since
223 * the item is already cached in DynamicContext::rangeVariable(). */
224 if(m_operands.at(i)->is(IDRangeVariableReference))
227 /* Note that we pass in cacheSlotOffset + i here instead of varDecls.at(i)->slot since
228 * we want independent caches for each callsite. */
229 m_operands[i] = Expression::Ptr(new EvaluationCache<false>(m_operands.at(i),
231 cacheSlotOffset + i));
235 FunctionSignature::Arity UserFunctionCallsite::arity() const
240 CallTargetDescription::Ptr UserFunctionCallsite::callTargetDescription() const
242 return m_functionDeclaration->signature();