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 ****************************************************************************/
43 #include "qcontextitem_p.h"
44 #include "qcommonsequencetypes_p.h"
45 #include "qemptysequence_p.h"
46 #include "qfunctionsignature_p.h"
47 #include "qgenericsequencetype_p.h"
48 #include "qcollationchecker_p.h"
49 #include "qcommonnamespaces_p.h"
51 #include "qfunctioncall_p.h"
55 using namespace QPatternist;
57 SequenceType::List FunctionCall::expectedOperandTypes() const
59 const FunctionArgument::List args(signature()->arguments());
60 FunctionArgument::List::const_iterator it(args.constBegin());
61 const FunctionArgument::List::const_iterator end(args.constEnd());
62 // TODO reserve/resize()
63 SequenceType::List result;
65 for(; it != end; ++it)
66 result.append((*it)->type());
71 Expression::Ptr FunctionCall::typeCheck(const StaticContext::Ptr &context,
72 const SequenceType::Ptr &reqType)
74 /* We don't cache properties() at some stages because it can be invalidated
75 * by the typeCheck(). */
77 const FunctionSignature::Arity maxArgs = signature()->maximumArguments();
78 /* We do this before the typeCheck() such that the appropriate conversions
79 * are applied to the ContextItem. */
80 if(m_operands.count() < maxArgs &&
83 m_operands.append(Expression::Ptr(new ContextItem()));
84 context->wrapExpressionWith(this, m_operands.last());
87 const Expression::Ptr me(UnlimitedContainer::typeCheck(context, reqType));
91 const Properties props(properties());
93 if(props.testFlag(RewriteToEmptyOnEmpty) &&
94 *CommonSequenceTypes::Empty == *m_operands.first()->staticType()->itemType())
96 return EmptySequence::create(this, context);
99 if(props.testFlag(LastOperandIsCollation) &&
100 m_operands.count() == maxArgs)
102 m_operands.last() = Expression::Ptr(new CollationChecker(m_operands.last()));
103 context->wrapExpressionWith(this, m_operands.last());
109 void FunctionCall::setSignature(const FunctionSignature::Ptr &sign)
114 FunctionSignature::Ptr FunctionCall::signature() const
116 Q_ASSERT(m_signature); /* It really should be set. */
120 SequenceType::Ptr FunctionCall::staticType() const
122 Q_ASSERT(m_signature);
123 if(has(EmptynessFollowsChild))
125 if(m_operands.isEmpty())
127 /* This is a function which uses the context item when having no arguments. */
128 return signature()->returnType();
130 const Cardinality card(m_operands.first()->staticType()->cardinality());
131 if(card.allowsEmpty())
132 return signature()->returnType();
136 return makeGenericSequenceType(signature()->returnType()->itemType(),
137 card & Cardinality::oneOrMore());
140 return signature()->returnType();
143 Expression::Properties FunctionCall::properties() const
145 Q_ASSERT(m_signature);
146 return signature()->properties();
149 ExpressionVisitorResult::Ptr FunctionCall::accept(const ExpressionVisitor::Ptr &visitor) const
151 return visitor->visit(this);
154 Expression::ID FunctionCall::id() const
156 Q_ASSERT(m_signature);
157 return m_signature->id();