1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the QtXmlPatterns module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL3 included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 3 requirements
23 ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24 **
25 ** GNU General Public License Usage
26 ** Alternatively, this file may be used under the terms of the GNU
27 ** General Public License version 2.0 or (at your option) the GNU General
28 ** Public license version 3 or any later version approved by the KDE Free
29 ** Qt Foundation. The licenses are as published by the Free Software
30 ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31 ** included in the packaging of this file. Please review the following
32 ** information to ensure the GNU General Public License requirements will
33 ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34 ** https://www.gnu.org/licenses/gpl-3.0.html.
35 **
36 ** $QT_END_LICENSE$
37 **
38 ****************************************************************************/
39
40 #include "qcommonsequencetypes_p.h"
41 #include "qdynamiccontextstore_p.h"
42 #include "qevaluationcache_p.h"
43
44 #include "quserfunctioncallsite_p.h"
45
46 QT_BEGIN_NAMESPACE
47
48 using namespace QPatternist;
49
UserFunctionCallsite(const QXmlName nameP,const FunctionSignature::Arity ar)50 UserFunctionCallsite::UserFunctionCallsite(const QXmlName nameP,
51 const FunctionSignature::Arity ar) : CallSite(nameP)
52 , m_arity(ar)
53 , m_expressionSlotOffset(-2)
54
55 {
56 }
57
evaluateSequence(const DynamicContext::Ptr & context) const58 Item::Iterator::Ptr UserFunctionCallsite::evaluateSequence(const DynamicContext::Ptr &context) const
59 {
60 return m_body->evaluateSequence(bindVariables(context));
61 }
62
evaluateSingleton(const DynamicContext::Ptr & context) const63 Item UserFunctionCallsite::evaluateSingleton(const DynamicContext::Ptr &context) const
64 {
65 return m_body->evaluateSingleton(bindVariables(context));
66 }
67
evaluateEBV(const DynamicContext::Ptr & context) const68 bool UserFunctionCallsite::evaluateEBV(const DynamicContext::Ptr &context) const
69 {
70 return m_body->evaluateEBV(bindVariables(context));
71 }
72
evaluateToSequenceReceiver(const DynamicContext::Ptr & context) const73 void UserFunctionCallsite::evaluateToSequenceReceiver(const DynamicContext::Ptr &context) const
74 {
75 m_body->evaluateToSequenceReceiver(bindVariables(context));
76 }
77
bindVariables(const DynamicContext::Ptr & context) const78 DynamicContext::Ptr UserFunctionCallsite::bindVariables(const DynamicContext::Ptr &context) const
79 {
80 const DynamicContext::Ptr stackContext(context->createStack());
81 Q_ASSERT(stackContext);
82
83 const Expression::List::const_iterator end(m_operands.constEnd());
84 Expression::List::const_iterator it(m_operands.constBegin());
85
86 VariableSlotID slot = m_expressionSlotOffset;
87
88 for(; it != end; ++it)
89 {
90 stackContext->setExpressionVariable(slot,
91 Expression::Ptr(new DynamicContextStore(*it, context)));
92 ++slot;
93 }
94
95 return stackContext;
96 }
97
expectedOperandTypes() const98 SequenceType::List UserFunctionCallsite::expectedOperandTypes() const
99 {
100 SequenceType::List result;
101
102 if(m_functionDeclaration)
103 {
104 const FunctionArgument::List args(m_functionDeclaration->signature()->arguments());
105 const FunctionArgument::List::const_iterator end(args.constEnd());
106 FunctionArgument::List::const_iterator it(args.constBegin());
107
108 for(; it != end; ++it)
109 result.append((*it)->type());
110 }
111 else
112 result.append(CommonSequenceTypes::ZeroOrMoreItems);
113
114 return result;
115 }
116
typeCheck(const StaticContext::Ptr & context,const SequenceType::Ptr & reqType)117 Expression::Ptr UserFunctionCallsite::typeCheck(const StaticContext::Ptr &context,
118 const SequenceType::Ptr &reqType)
119 {
120 /* The parser calls TypeChecker::applyFunctionConversion() on user function
121 * bodies, possibly indirectly, before all function call sites have been
122 * resolved. Hence it's possible that we're called before before the usual
123 * typeCheck() pass, and hence before we have been resolved/checked and
124 * subsequently m_functionDeclaration set. Therefore, encounter for that below.
125 *
126 * UnresolvedVariableReference::typeCheck() has the same dilemma.
127 */
128
129 /* Ensure that the return value of the function is properly
130 * converted/does match from where it is called(which is here). */
131 if(isRecursive() || !m_functionDeclaration)
132 return CallSite::typeCheck(context, reqType);
133 else
134 {
135 /* Update, such that we use a recent version of the body that has typeCheck()
136 * and compress() rewrites included. */
137 m_body = m_functionDeclaration->body();
138
139 /* Note, we can't assign to m_functionDeclaration->body() because UserFunction can apply
140 * to several different callsites. Hence we need our own version. */
141 m_body = m_body->typeCheck(context, reqType);
142
143 /* We just act as a pipe for m_body, so we don't have to typecheck ourselves. However,
144 * the arguments must match the function declaration. */
145 typeCheckOperands(context);
146 return Expression::Ptr(this);
147 }
148 }
149
compress(const StaticContext::Ptr & context)150 Expression::Ptr UserFunctionCallsite::compress(const StaticContext::Ptr &context)
151 {
152 if(!isRecursive())
153 rewrite(m_body, m_body->compress(context), context);
154
155 return CallSite::compress(context);
156 }
157
properties() const158 Expression::Properties UserFunctionCallsite::properties() const
159 {
160 return DisableElimination;
161 }
162
staticType() const163 SequenceType::Ptr UserFunctionCallsite::staticType() const
164 {
165 /* Our return type, is the static type of the function body. We could have also used
166 * m_functionDeclaration->signature()->returnType(), but it doesn't get updated
167 * when function conversion is applied.
168 * We can't use m_body's type if we're recursive, because m_body computes its type
169 * from its children, and we're at least one of the children. Hence, we would
170 * recurse infinitely if we did.
171 *
172 * m_body can be null here if we're called before setSource().
173 */
174 if(isRecursive() || !m_body)
175 return CommonSequenceTypes::ZeroOrMoreItems; // TODO use the declaration, it can have a type explicitly.
176 else
177 return m_body->staticType();
178 }
179
accept(const ExpressionVisitor::Ptr & visitor) const180 ExpressionVisitorResult::Ptr UserFunctionCallsite::accept(const ExpressionVisitor::Ptr &visitor) const
181 {
182 return visitor->visit(this);
183 }
184
id() const185 Expression::ID UserFunctionCallsite::id() const
186 {
187 return IDUserFunctionCallsite;
188 }
189
isSignatureValid(const FunctionSignature::Ptr & sign) const190 bool UserFunctionCallsite::isSignatureValid(const FunctionSignature::Ptr &sign) const
191 {
192 Q_ASSERT(sign);
193
194 return sign->name() == name()
195 &&
196 sign->isArityValid(m_arity);
197 }
198
configureRecursion(const CallTargetDescription::Ptr & sign)199 bool UserFunctionCallsite::configureRecursion(const CallTargetDescription::Ptr &sign)
200 {
201 Q_ASSERT(sign);
202
203 setIsRecursive(isSignatureValid(sign));
204 return isRecursive();
205 }
206
setSource(const UserFunction::Ptr & userFunction,const VariableSlotID cacheSlotOffset)207 void UserFunctionCallsite::setSource(const UserFunction::Ptr &userFunction,
208 const VariableSlotID cacheSlotOffset)
209 {
210 m_functionDeclaration = userFunction;
211 m_body = userFunction->body();
212 m_expressionSlotOffset = userFunction->expressionSlotOffset();
213
214 const int len = m_operands.size();
215
216 const VariableDeclaration::List varDecls(userFunction->argumentDeclarations());
217
218 for(int i = 0; i < len; ++i)
219 {
220 /* We don't want evaluation caches for range variables, it's not necessary since
221 * the item is already cached in DynamicContext::rangeVariable(). */
222 if(m_operands.at(i)->is(IDRangeVariableReference))
223 continue;
224
225 /* Note that we pass in cacheSlotOffset + i here instead of varDecls.at(i)->slot since
226 * we want independent caches for each callsite. */
227 m_operands[i] = Expression::Ptr(new EvaluationCache<false>(m_operands.at(i),
228 varDecls.at(i),
229 cacheSlotOffset + i));
230 }
231 }
232
arity() const233 FunctionSignature::Arity UserFunctionCallsite::arity() const
234 {
235 return m_arity;
236 }
237
callTargetDescription() const238 CallTargetDescription::Ptr UserFunctionCallsite::callTargetDescription() const
239 {
240 return m_functionDeclaration->signature();
241 }
242
243 QT_END_NAMESPACE
244