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 tools applications 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 ****************************************************************************/
46 #include <QStringList>
49 #include <QJsonDocument>
59 enum ReferenceType { NoReference, Reference, RValueReference, Pointer };
61 inline Type() : isVolatile(false), isScoped(false), firstToken(NOTOKEN), referenceType(NoReference) {}
62 inline explicit Type(const QByteArray &_name)
63 : name(_name), rawName(name), isVolatile(false), isScoped(false), firstToken(NOTOKEN), referenceType(NoReference) {}
65 //When used as a return type, the type name may be modified to remove the references.
66 // rawName is the type as found in the function signature
71 ReferenceType referenceType;
77 QList<QByteArray> values;
78 bool isEnumClass; // c++11 enum class
79 EnumDef() : isEnumClass(false) {}
84 ArgumentDef() : isDefault(false) {}
86 QByteArray rightType, normalizedType, name;
87 QByteArray typeNameForCast; // type name to be used in cast from void * in metacall
93 FunctionDef(): returnTypeIsVolatile(false), access(Private), isConst(false), isVirtual(false), isStatic(false),
94 inlineCode(false), wasCloned(false), isCompat(false), isInvokable(false),
95 isScriptable(false), isSlot(false), isSignal(false),
96 isConstructor(false), isDestructor(false), isAbstract(false), revision(0) {}
98 QByteArray normalizedType;
101 bool returnTypeIsVolatile;
103 QList<ArgumentDef> arguments;
105 enum Access { Private, Protected, Public };
113 QByteArray inPrivateClass;
128 PropertyDef():notifyId(-1), constant(false), final(false), gspec(ValueSpec), revision(0){}
129 QByteArray name, type, read, write, reset, designable, scriptable, editable, stored, user, notify, inPrivateClass;
133 enum Specification { ValueSpec, ReferenceSpec, PointerSpec };
135 bool stdCppSet() const {
137 s += toupper(name[0]);
153 hasQObject(false), hasQGadget(false), notifyableProperties(0)
154 , revisionedMethods(0), revisionedProperties(0), begin(0), end(0){}
155 QByteArray classname;
156 QByteArray qualified;
157 QList<QPair<QByteArray, FunctionDef::Access> > superclassList;
161 inline explicit Interface(const QByteArray &_className)
162 : className(_className) {}
163 QByteArray className;
164 QByteArray interfaceId;
166 QList<QList<Interface> >interfaceList;
173 QJsonDocument metaData;
176 QList<FunctionDef> constructorList;
177 QList<FunctionDef> signalList, slotList, methodList, publicList;
178 int notifyableProperties;
179 QList<PropertyDef> propertyList;
180 QList<ClassInfoDef> classInfoList;
181 QMap<QByteArray, bool> enumDeclarations;
182 QList<EnumDef> enumList;
183 QMap<QByteArray, QByteArray> flagAliases;
184 int revisionedMethods;
185 int revisionedProperties;
191 struct NamespaceDef {
197 class Moc : public Parser
201 : noInclude(false), generatedCode(false), mustIncludeQPluginH(false)
208 bool mustIncludeQPluginH;
209 QByteArray includePath;
210 QList<QByteArray> includeFiles;
211 QList<ClassDef> classList;
212 QMap<QByteArray, QByteArray> interface2IdMap;
213 QList<QByteArray> metaTypes;
214 QSet<QByteArray> knownQObjectClasses;
217 void generate(FILE *out);
219 bool parseClassHead(ClassDef *def);
220 inline bool inClass(const ClassDef *def) const {
221 return index > def->begin && index < def->end - 1;
224 inline bool inNamespace(const NamespaceDef *def) const {
225 return index > def->begin && index < def->end - 1;
230 bool parseEnum(EnumDef *def);
232 bool parseFunction(FunctionDef *def, bool inMacro = false);
233 bool parseMaybeFunction(const ClassDef *cdef, FunctionDef *def);
235 void parseSlots(ClassDef *def, FunctionDef::Access access);
236 void parseSignals(ClassDef *def);
237 void parseProperty(ClassDef *def);
238 void parsePluginData(ClassDef *def);
239 void createPropertyDef(PropertyDef &def);
240 void parseEnumOrFlag(ClassDef *def, bool isFlag);
241 void parseFlag(ClassDef *def);
242 void parseClassInfo(ClassDef *def);
243 void parseInterfaces(ClassDef *def);
244 void parseDeclareInterface();
245 void parseDeclareMetatype();
246 void parseSlotInPrivate(ClassDef *def, FunctionDef::Access access);
247 void parsePrivateProperty(ClassDef *def);
249 void parseFunctionArguments(FunctionDef *def);
251 QByteArray lexemUntil(Token);
254 // test for Q_INVOCABLE, Q_SCRIPTABLE, etc. and set the flags
255 // in FunctionDef accordingly
256 bool testFunctionAttribute(FunctionDef *def);
257 bool testFunctionAttribute(Token tok, FunctionDef *def);
258 bool testFunctionRevision(FunctionDef *def);
260 void checkSuperClasses(ClassDef *def);
261 void checkProperties(ClassDef* cdef);
264 inline QByteArray noRef(const QByteArray &type)
266 if (type.endsWith('&')) {
267 if (type.endsWith("&&"))
268 return type.left(type.length()-2);
269 return type.left(type.length()-1);