comparison generator/cppgenerator.cpp @ 1:e78566595089

initial import
author mandel
date Mon, 11 May 2009 16:01:50 +0000
parents
children cf8a415f3f32
comparison
equal deleted inserted replaced
0:36fb74dc547d 1:e78566595089
1 /****************************************************************************
2 **
3 ** Copyright (C) 1992-2008 Nokia. All rights reserved.
4 **
5 ** This file is part of Qt Jambi.
6 **
7 ** * Commercial Usage
8 * Licensees holding valid Qt Commercial licenses may use this file in
9 * accordance with the Qt Commercial License Agreement provided with the
10 * Software or, alternatively, in accordance with the terms contained in
11 * a written agreement between you and Nokia.
12 *
13 *
14 * GNU General Public License Usage
15 * Alternatively, this file may be used under the terms of the GNU
16 * General Public License versions 2.0 or 3.0 as published by the Free
17 * Software Foundation and appearing in the file LICENSE.GPL included in
18 * the packaging of this file. Please review the following information
19 * to ensure GNU General Public Licensing requirements will be met:
20 * http://www.fsf.org/licensing/licenses/info/GPLv2.html and
21 * http://www.gnu.org/copyleft/gpl.html. In addition, as a special
22 * exception, Nokia gives you certain additional rights. These rights
23 * are described in the Nokia Qt GPL Exception version 1.2, included in
24 * the file GPL_EXCEPTION.txt in this package.
25 *
26 * Qt for Windows(R) Licensees
27 * As a special exception, Nokia, as the sole copyright holder for Qt
28 * Designer, grants users of the Qt/Eclipse Integration plug-in the
29 * right for the Qt/Eclipse Integration to link to functionality
30 * provided by Qt Designer and its related libraries.
31 *
32 *
33 * If you are unsure which license is appropriate for your use, please
34 * contact the sales department at qt-sales@nokia.com.
35
36 **
37 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
38 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
39 **
40 ****************************************************************************/
41
42 #include "cppgenerator.h"
43 #include "reporthandler.h"
44
45 #include "metajava.h"
46
47 // ### There's a bug in Qt causing it to fail at normalizing signatures
48 // on the form FooBar<T> const&, which is the form the C++ parser uses
49 // for all types, so connections between Java and C++ with const& templates
50 // will fail. This is a work around which is only needed until that bug is fixed.
51 // Since Qt works correctly with const FooBar<T> &, we simply change the
52 // signature to that.
53 QString CppGenerator::fixNormalizedSignatureForQt(const QString &signature)
54 {
55 QString ret = signature;
56 if (signature.contains("<") && signature.endsWith("const&")) {
57 ret = "const "
58 + signature.mid(0, signature.size() - 6)
59 + "&";
60 }
61 return ret;
62 }
63
64 void CppGenerator::writeTypeInfo(QTextStream &s, const AbstractMetaType *type, Option options)
65 {
66 if ((options & OriginalTypeDescription) && !type->originalTypeDescription().isEmpty()) {
67 QString originalTypeDescription = type->originalTypeDescription();
68
69 if (options & NormalizeAndFixTypeSignature) {
70 originalTypeDescription = QMetaObject::normalizedSignature(originalTypeDescription.toLatin1().constData());
71 originalTypeDescription = fixNormalizedSignatureForQt(originalTypeDescription);
72 }
73
74 s << originalTypeDescription;
75 return;
76 }
77
78 if (type->isArray()) {
79 writeTypeInfo(s, type->arrayElementType(), options);
80 if (options & ArrayAsPointer) {
81 s << "*";
82 } else {
83 s << "[" << type->arrayElementCount() << "]";
84 }
85 return;
86 }
87
88 const TypeEntry *te = type->typeEntry();
89
90 if (type->isConstant() && !(options & ExcludeConst))
91 s << "const ";
92
93 if ((options & EnumAsInts) && (te->isEnum() || te->isFlags())) {
94 s << "int";
95 } else if (te->isFlags()) {
96 s << ((FlagsTypeEntry *) te)->originalName();
97 } else {
98 if (options & VirtualDispatch && te->name() == "QModelIndex")
99 s << "QModelIndexAccessor";
100 else
101 s << fixCppTypeName(te->qualifiedCppName());
102 }
103
104 if (type->instantiations().size() > 0
105 && (!type->isContainer()
106 || (static_cast<const ContainerTypeEntry *>(te))->type() != ContainerTypeEntry::StringListContainer)) {
107 s << '<';
108 QList<AbstractMetaType *> args = type->instantiations();
109 bool nested_template = false;
110 for (int i=0; i<args.size(); ++i) {
111 if (i != 0)
112 s << ", ";
113 nested_template |= args.at(i)->isContainer();
114 writeTypeInfo(s, args.at(i));
115 }
116 if (nested_template)
117 s << ' ';
118 s << '>';
119 }
120
121 int actual_indirections = type->indirections();
122 // for getting C++ elements from array we want pointers even if elements are
123 // values because wrapper actually contains pointers
124 if ((options & ForcePointer) && actual_indirections == 0
125 && !type->isPrimitive() && !type->typeEntry()->isStructInD()
126 && type->name() != "QModelIndex")
127 actual_indirections = 1;
128
129 s << QString(actual_indirections, '*');
130
131 if (type->isReference() && !(options & ExcludeReference))
132 s << "&";
133
134 if (!(options & SkipName))
135 s << ' ';
136 }
137
138
139 void CppGenerator::writeFunctionArguments(QTextStream &s,
140 const AbstractMetaArgumentList &arguments,
141 Option option,
142 int numArguments)
143 {
144 if (numArguments < 0) numArguments = arguments.size();
145
146 for (int i=0; i<numArguments; ++i) {
147 if (i != 0)
148 s << ", ";
149 AbstractMetaArgument *arg = arguments.at(i);
150 writeTypeInfo(s, arg->type(), option);
151 if (!(option & SkipName))
152 s << " " << arg->indexedName();
153 if ((option & IncludeDefaultExpression) && !arg->originalDefaultValueExpression().isEmpty()) {
154 s << " = ";
155
156 QString expr = arg->originalDefaultValueExpression();
157 if (arg->type()->typeEntry()->isEnum() && expr.indexOf("::") < 0)
158 s << ((EnumTypeEntry *)arg->type()->typeEntry())->qualifier() << "::";
159
160 s << expr;
161 }
162 }
163 }
164
165 /*!
166 * Writes the function \a java_function signature to the textstream \a s.
167 *
168 * The \a name_prefix can be used to give the function name a prefix,
169 * like "__public_" or "__override_" and \a classname_prefix can
170 * be used to give the class name a prefix.
171 *
172 * The \a option flags can be used to tweak various parameters, such as
173 * showing static, original vs renamed name, underscores for space etc.
174 *
175 * The \a extra_arguments list is a list of extra arguments on the
176 * form "bool static_call".
177 */
178
179 void CppGenerator::writeFunctionSignature(QTextStream &s,
180 const AbstractMetaFunction *java_function,
181 const AbstractMetaClass *implementor,
182 const QString &name_prefix,
183 Option option,
184 const QString &classname_prefix,
185 const QStringList &extra_arguments,
186 int numArguments)
187 {
188 // ### remove the implementor
189 AbstractMetaType *function_type = java_function->type();
190
191 if (java_function->isStatic() && (option & ShowStatic))
192 s << "static ";
193
194 if ((option & SkipReturnType) == 0) {
195 if (function_type) {
196 writeTypeInfo(s, function_type, option);
197 s << " ";
198 } else if (!java_function->isConstructor()) {
199 s << "void ";
200 }
201 }
202
203 if (implementor) {
204 if (classname_prefix.isEmpty())
205 s << shellClassName(implementor) << "::";
206 else
207 s << classname_prefix << implementor->name() << "::";
208 }
209
210
211 QString function_name;
212 if (option & OriginalName)
213 function_name = java_function->originalName();
214 else
215 function_name = java_function->name();
216
217 if (option & UnderscoreSpaces)
218 function_name = function_name.replace(' ', '_');
219
220 if (java_function->isConstructor())
221 function_name = shellClassName(java_function->ownerClass());
222
223 s << name_prefix << function_name;
224
225 if (java_function->attributes() & AbstractMetaAttributes::SetterFunction)
226 s << "_setter";
227 else if (java_function->attributes() & AbstractMetaAttributes::GetterFunction)
228 s << "_getter";
229
230 s << "(";
231 const AbstractMetaClass *owner = java_function->ownerClass();
232
233 bool has_d_ptr = java_function->isConstructor() && owner && (owner->hasVirtualFunctions()/* || owner->typeEntry()->isObject()*/ );
234 const AbstractMetaArgumentList arg_list = java_function->arguments();
235 if (has_d_ptr) {
236 s << "void *d_ptr";
237 if (arg_list.size() > 0)
238 s << ", ";
239 }
240 writeFunctionArguments(s, arg_list, option, numArguments);
241
242 // The extra arguments...
243 for (int i=0; i<extra_arguments.size(); ++i) {
244 if (i > 0 || java_function->arguments().size() != 0)
245 s << ", ";
246 s << extra_arguments.at(i);
247 }
248
249 s << ")";
250 if (java_function->isConstant())
251 s << " const";
252 }