| 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 | |
| 41 | #include "qcontextitem_p.h" |
| 42 | #include "qcommonsequencetypes_p.h" |
| 43 | #include "qemptysequence_p.h" |
| 44 | #include "qfunctionsignature_p.h" |
| 45 | #include "qgenericsequencetype_p.h" |
| 46 | #include "qcollationchecker_p.h" |
| 47 | #include "qcommonnamespaces_p.h" |
| 48 | |
| 49 | #include "qfunctioncall_p.h" |
| 50 | |
| 51 | QT_BEGIN_NAMESPACE |
| 52 | |
| 53 | using namespace QPatternist; |
| 54 | |
| 55 | SequenceType::List FunctionCall::expectedOperandTypes() const |
| 56 | { |
| 57 | const FunctionArgument::List args(signature()->arguments()); |
| 58 | FunctionArgument::List::const_iterator it(args.constBegin()); |
| 59 | const FunctionArgument::List::const_iterator end(args.constEnd()); |
| 60 | // TODO reserve/resize() |
| 61 | SequenceType::List result; |
| 62 | |
| 63 | for(; it != end; ++it) |
| 64 | result.append(t: (*it)->type()); |
| 65 | |
| 66 | return result; |
| 67 | } |
| 68 | |
| 69 | Expression::Ptr FunctionCall::typeCheck(const StaticContext::Ptr &context, |
| 70 | const SequenceType::Ptr &reqType) |
| 71 | { |
| 72 | /* We don't cache properties() at some stages because it can be invalidated |
| 73 | * by the typeCheck(). */ |
| 74 | |
| 75 | const FunctionSignature::Arity maxArgs = signature()->maximumArguments(); |
| 76 | /* We do this before the typeCheck() such that the appropriate conversions |
| 77 | * are applied to the ContextItem. */ |
| 78 | if(m_operands.count() < maxArgs && |
| 79 | has(prop: UseContextItem)) |
| 80 | { |
| 81 | m_operands.append(t: Expression::Ptr(new ContextItem())); |
| 82 | context->wrapExpressionWith(existingNode: this, newNode: m_operands.last()); |
| 83 | } |
| 84 | |
| 85 | const Expression::Ptr me(UnlimitedContainer::typeCheck(context, reqType)); |
| 86 | if(me != this) |
| 87 | return me; |
| 88 | |
| 89 | const Properties props(properties()); |
| 90 | |
| 91 | if(props.testFlag(flag: RewriteToEmptyOnEmpty) && |
| 92 | *CommonSequenceTypes::Empty == *m_operands.first()->staticType()->itemType()) |
| 93 | { |
| 94 | return EmptySequence::create(replacementFor: this, context); |
| 95 | } |
| 96 | |
| 97 | if(props.testFlag(flag: LastOperandIsCollation) && |
| 98 | m_operands.count() == maxArgs) |
| 99 | { |
| 100 | m_operands.last() = Expression::Ptr(new CollationChecker(m_operands.last())); |
| 101 | context->wrapExpressionWith(existingNode: this, newNode: m_operands.last()); |
| 102 | } |
| 103 | |
| 104 | return me; |
| 105 | } |
| 106 | |
| 107 | void FunctionCall::setSignature(const FunctionSignature::Ptr &sign) |
| 108 | { |
| 109 | m_signature = sign; |
| 110 | } |
| 111 | |
| 112 | FunctionSignature::Ptr FunctionCall::signature() const |
| 113 | { |
| 114 | Q_ASSERT(m_signature); /* It really should be set. */ |
| 115 | return m_signature; |
| 116 | } |
| 117 | |
| 118 | SequenceType::Ptr FunctionCall::staticType() const |
| 119 | { |
| 120 | Q_ASSERT(m_signature); |
| 121 | if(has(prop: EmptynessFollowsChild)) |
| 122 | { |
| 123 | if(m_operands.isEmpty()) |
| 124 | { |
| 125 | /* This is a function which uses the context item when having no arguments. */ |
| 126 | return signature()->returnType(); |
| 127 | } |
| 128 | const Cardinality card(m_operands.first()->staticType()->cardinality()); |
| 129 | if(card.allowsEmpty()) |
| 130 | return signature()->returnType(); |
| 131 | else |
| 132 | { |
| 133 | /* Remove empty. */ |
| 134 | return makeGenericSequenceType(itemType: signature()->returnType()->itemType(), |
| 135 | cardinality: card & Cardinality::oneOrMore()); |
| 136 | } |
| 137 | } |
| 138 | return signature()->returnType(); |
| 139 | } |
| 140 | |
| 141 | Expression::Properties FunctionCall::properties() const |
| 142 | { |
| 143 | Q_ASSERT(m_signature); |
| 144 | return signature()->properties(); |
| 145 | } |
| 146 | |
| 147 | ExpressionVisitorResult::Ptr FunctionCall::accept(const ExpressionVisitor::Ptr &visitor) const |
| 148 | { |
| 149 | return visitor->visit(this); |
| 150 | } |
| 151 | |
| 152 | Expression::ID FunctionCall::id() const |
| 153 | { |
| 154 | Q_ASSERT(m_signature); |
| 155 | return m_signature->id(); |
| 156 | } |
| 157 | |
| 158 | QT_END_NAMESPACE |
| 159 | |