f278ee80c028fed841d508dee46ef6c815e505fb
[profile/ivi/qtxmlpatterns.git] / src / xmlpatterns / functions / qfunctionsignature.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
6 **
7 ** This file is part of the QtXmlPatterns module of the Qt Toolkit.
8 **
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.
17 **
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.
21 **
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.
29 **
30 ** Other Usage
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.
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include "qxmlname.h"
43
44 #include "qfunctionsignature_p.h"
45
46 QT_BEGIN_NAMESPACE
47
48 using namespace QPatternist;
49
50 FunctionSignature::FunctionSignature(const QXmlName nameP,
51                                      const Arity minArgs,
52                                      const Arity maxArgs,
53                                      const SequenceType::Ptr &returnTypeP,
54                                      const Expression::Properties props,
55                                      const Expression::ID idP) : CallTargetDescription(nameP)
56                                                                , m_minArgs(minArgs)
57                                                                , m_maxArgs(maxArgs)
58                                                                , m_returnType(returnTypeP)
59                                                                , m_arguments()
60                                                                , m_props(props)
61                                                                , m_id(idP)
62 {
63     Q_ASSERT(minArgs <= maxArgs || maxArgs == FunctionSignature::UnlimitedArity);
64     Q_ASSERT(m_maxArgs >= -1);
65     Q_ASSERT(returnTypeP);
66 }
67
68 void FunctionSignature::appendArgument(const QXmlName::LocalNameCode nameP,
69                                        const SequenceType::Ptr &type)
70 {
71     Q_ASSERT(type);
72
73     m_arguments.append(FunctionArgument::Ptr(new FunctionArgument(QXmlName(StandardNamespaces::empty, nameP), type)));
74 }
75
76 QString FunctionSignature::displayName(const NamePool::Ptr &np) const
77 {
78     QString result;
79     result += np->displayName(name());
80     result += QLatin1Char('(');
81
82     FunctionArgument::List::const_iterator it(m_arguments.constBegin());
83     const FunctionArgument::List::const_iterator end(m_arguments.constEnd());
84
85     if(it != end)
86     {
87         while(true)
88         {
89             result += QLatin1Char('$');
90             result += np->displayName((*it)->name());
91             result += QLatin1String(" as ");
92             result += (*it)->type()->displayName(np);
93
94             ++it;
95             if(it == end)
96                 break;
97
98             result += QLatin1String(", ");
99         }
100     }
101
102     if(m_maxArgs == FunctionSignature::UnlimitedArity)
103         result += QLatin1String(", ...");
104
105     result += QLatin1String(") as ");
106     result += m_returnType->displayName(np);
107
108     return result;
109 }
110
111 bool FunctionSignature::operator==(const FunctionSignature &other) const
112 {
113     return name() == other.name() &&
114            isArityValid(other.maximumArguments()) &&
115            isArityValid(other.minimumArguments());
116 }
117
118 void FunctionSignature::setArguments(const FunctionArgument::List &args)
119 {
120     m_arguments = args;
121 }
122
123 FunctionArgument::List FunctionSignature::arguments() const
124 {
125     return m_arguments;
126 }
127
128 bool FunctionSignature::isArityValid(const xsInteger arity) const
129 {
130     return arity >= m_minArgs && arity <= m_maxArgs;
131 }
132
133 FunctionSignature::Arity FunctionSignature::minimumArguments() const
134 {
135     return m_minArgs;
136 }
137
138 FunctionSignature::Arity FunctionSignature::maximumArguments() const
139 {
140     return m_maxArgs;
141 }
142
143 SequenceType::Ptr FunctionSignature::returnType() const
144 {
145     return m_returnType;
146 }
147
148 Expression::Properties FunctionSignature::properties() const
149 {
150     return m_props;
151 }
152
153 Expression::ID FunctionSignature::id() const
154 {
155     return m_id;
156 }
157
158 QT_END_NAMESPACE