1 /****************************************************************************
3 ** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved.
5 ** This file is part of the Qt Script Generator project on Trolltech Labs.
7 ** This file may be used under the terms of the GNU General Public
8 ** License version 2.0 as published by the Free Software Foundation
9 ** and appearing in the file LICENSE.GPL included in the packaging of
10 ** this file. Please review the following information to ensure GNU
11 ** General Public Licensing requirements will be met:
12 ** http://www.trolltech.com/products/qt/opensource.html
14 ** If you are unsure which license is appropriate for your use, please
15 ** review the following information:
16 ** http://www.trolltech.com/products/qt/licensing.html or contact the
17 ** sales department at sales@trolltech.com.
19 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
20 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
22 ****************************************************************************/
24 #include "shellgenerator.h"
25 #include "reporthandler.h"
27 #include "metaqtscript.h"
29 bool ShellGenerator::shouldGenerate(const AbstractMetaClass
*meta_class
) const
31 uint cg
= meta_class
->typeEntry()->codeGeneration();
32 return ((cg
& TypeEntry::GenerateCode
) != 0) && meta_class
->generateShellClass();
35 void ShellGenerator::writeTypeInfo(QTextStream
&s
, const AbstractMetaType
*type
, Option options
)
37 if ((options
& OriginalTypeDescription
) && !type
->originalTypeDescription().isEmpty()) {
38 s
<< type
->originalTypeDescription();
42 if (type
->isArray()) {
43 writeTypeInfo(s
, type
->arrayElementType(), options
);
44 if (options
& ArrayAsPointer
) {
47 s
<< "[" << type
->arrayElementCount() << "]";
52 const TypeEntry
*te
= type
->typeEntry();
54 if (type
->isConstant() && !(options
& ExcludeConst
))
57 if ((options
& EnumAsInts
) && (te
->isEnum() || te
->isFlags())) {
59 } else if (te
->isFlags()) {
60 s
<< ((FlagsTypeEntry
*) te
)->originalName();
62 s
<< fixCppTypeName(te
->qualifiedCppName());
65 if (type
->instantiations().size() > 0
66 && (!type
->isContainer()
67 || (static_cast<const ContainerTypeEntry
*>(te
))->type() != ContainerTypeEntry::StringListContainer
)) {
69 QList
<AbstractMetaType
*> args
= type
->instantiations();
70 bool nested_template
= false;
71 for (int i
=0; i
<args
.size(); ++i
) {
74 nested_template
|= args
.at(i
)->isContainer();
75 writeTypeInfo(s
, args
.at(i
));
82 s
<< QString(type
->indirections(), '*');
84 if (type
->isReference() && !(options
& ExcludeReference
))
87 if (!(options
& SkipName
))
92 void ShellGenerator::writeFunctionArguments(QTextStream
&s
,
93 const AbstractMetaArgumentList
&arguments
,
97 if (numArguments
< 0) numArguments
= arguments
.size();
99 for (int i
=0; i
<numArguments
; ++i
) {
102 AbstractMetaArgument
*arg
= arguments
.at(i
);
103 writeTypeInfo(s
, arg
->type(), option
);
104 if (!(option
& SkipName
))
105 s
<< " " << arg
->argumentName();
106 if ((option
& IncludeDefaultExpression
) && !arg
->originalDefaultValueExpression().isEmpty()) {
109 QString expr
= arg
->originalDefaultValueExpression();
110 if (arg
->type()->typeEntry()->isEnum() && expr
.indexOf("::") < 0)
111 s
<< ((EnumTypeEntry
*)arg
->type()->typeEntry())->qualifier() << "::";
119 * Writes the function \a meta_function signature to the textstream \a s.
121 * The \a name_prefix can be used to give the function name a prefix,
122 * like "__public_" or "__override_" and \a classname_prefix can
123 * be used to give the class name a prefix.
125 * The \a option flags can be used to tweak various parameters, such as
126 * showing static, original vs renamed name, underscores for space etc.
128 * The \a extra_arguments list is a list of extra arguments on the
129 * form "bool static_call".
132 void ShellGenerator::writeFunctionSignature(QTextStream
&s
,
133 const AbstractMetaFunction
*meta_function
,
134 const AbstractMetaClass
*implementor
,
135 const QString
&name_prefix
,
137 const QString
&classname_prefix
,
138 const QStringList
&extra_arguments
,
141 // ### remove the implementor
142 AbstractMetaType
*function_type
= meta_function
->type();
144 if (meta_function
->isStatic() && (option
& ShowStatic
))
147 if ((option
& SkipReturnType
) == 0) {
149 writeTypeInfo(s
, function_type
, option
);
151 } else if (!meta_function
->isConstructor()) {
157 if (classname_prefix
.isEmpty())
158 s
<< shellClassName(implementor
) << "::";
160 s
<< classname_prefix
<< implementor
->name() << "::";
164 QString function_name
;
165 if (option
& OriginalName
)
166 function_name
= meta_function
->originalName();
168 function_name
= meta_function
->name();
170 if (option
& UnderscoreSpaces
)
171 function_name
= function_name
.replace(' ', '_');
173 if (meta_function
->isConstructor())
174 function_name
= shellClassName(meta_function
->ownerClass());
176 s
<< name_prefix
<< function_name
;
178 if (meta_function
->attributes() & AbstractMetaAttributes::SetterFunction
)
180 else if (meta_function
->attributes() & AbstractMetaAttributes::GetterFunction
)
185 writeFunctionArguments(s
, meta_function
->arguments(), option
, numArguments
);
187 // The extra arguments...
188 for (int i
=0; i
<extra_arguments
.size(); ++i
) {
189 if (i
> 0 || meta_function
->arguments().size() != 0)
191 s
<< extra_arguments
.at(i
);
195 if (meta_function
->isConstant())