Revert "Allow moc to handle symbols that have been redefined."
[profile/ivi/qtbase.git] / src / tools / moc / moc.h
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
5 **
6 ** This file is part of the tools applications of the Qt Toolkit.
7 **
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.
16 **
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.
20 **
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.
28 **
29 ** Other Usage
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.
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #ifndef MOC_H
43 #define MOC_H
44
45 #include "parser.h"
46 #include <QStringList>
47 #include <QMap>
48 #include <QPair>
49 #include <QJsonDocument>
50 #include <stdio.h>
51 #include <ctype.h>
52
53 QT_BEGIN_NAMESPACE
54
55 struct QMetaObject;
56
57 struct Type
58 {
59     enum ReferenceType { NoReference, Reference, RValueReference, Pointer };
60
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) {}
64     QByteArray name;
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
67     QByteArray rawName;
68     uint isVolatile : 1;
69     uint isScoped : 1;
70     Token firstToken;
71     ReferenceType referenceType;
72 };
73
74 struct EnumDef
75 {
76     QByteArray name;
77     QList<QByteArray> values;
78     bool isEnumClass; // c++11 enum class
79     EnumDef() : isEnumClass(false) {}
80 };
81
82 struct ArgumentDef
83 {
84     ArgumentDef() : isDefault(false) {}
85     Type type;
86     QByteArray rightType, normalizedType, name;
87     QByteArray typeNameForCast; // type name to be used in cast from void * in metacall
88     bool isDefault;
89 };
90
91 struct FunctionDef
92 {
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) {}
97     Type type;
98     QByteArray normalizedType;
99     QByteArray tag;
100     QByteArray name;
101     bool returnTypeIsVolatile;
102
103     QList<ArgumentDef> arguments;
104
105     enum Access { Private, Protected, Public };
106     Access access;
107     bool isConst;
108     bool isVirtual;
109     bool isStatic;
110     bool inlineCode;
111     bool wasCloned;
112
113     QByteArray inPrivateClass;
114     bool isCompat;
115     bool isInvokable;
116     bool isScriptable;
117     bool isSlot;
118     bool isSignal;
119     bool isConstructor;
120     bool isDestructor;
121     bool isAbstract;
122
123     int revision;
124 };
125
126 struct PropertyDef
127 {
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;
130     int notifyId;
131     bool constant;
132     bool final;
133     enum Specification  { ValueSpec, ReferenceSpec, PointerSpec };
134     Specification gspec;
135     bool stdCppSet() const {
136         QByteArray s("set");
137         s += toupper(name[0]);
138         s += name.mid(1);
139         return (s == write);
140     }
141     int revision;
142 };
143
144
145 struct ClassInfoDef
146 {
147     QByteArray name;
148     QByteArray value;
149 };
150
151 struct ClassDef {
152     ClassDef():
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;
158
159     struct Interface
160     {
161         inline explicit Interface(const QByteArray &_className)
162             : className(_className) {}
163         QByteArray className;
164         QByteArray interfaceId;
165     };
166     QList<QList<Interface> >interfaceList;
167
168     bool hasQObject;
169     bool hasQGadget;
170
171     struct PluginData {
172         QByteArray iid;
173         QJsonDocument metaData;
174     } pluginData;
175
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;
186
187     int begin;
188     int end;
189 };
190
191 struct NamespaceDef {
192     QByteArray name;
193     int begin;
194     int end;
195 };
196
197 class Moc : public Parser
198 {
199 public:
200     Moc()
201         : noInclude(false), generatedCode(false), mustIncludeQPluginH(false)
202         {}
203
204     QByteArray filename;
205
206     bool noInclude;
207     bool generatedCode;
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;
215
216     void parse();
217     void generate(FILE *out);
218
219     bool parseClassHead(ClassDef *def);
220     inline bool inClass(const ClassDef *def) const {
221         return index > def->begin && index < def->end - 1;
222     }
223
224     inline bool inNamespace(const NamespaceDef *def) const {
225         return index > def->begin && index < def->end - 1;
226     }
227
228     Type parseType();
229
230     bool parseEnum(EnumDef *def);
231
232     bool parseFunction(FunctionDef *def, bool inMacro = false);
233     bool parseMaybeFunction(const ClassDef *cdef, FunctionDef *def);
234
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);
248
249     void parseFunctionArguments(FunctionDef *def);
250
251     QByteArray lexemUntil(Token);
252     bool until(Token);
253
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);
259
260     void checkSuperClasses(ClassDef *def);
261     void checkProperties(ClassDef* cdef);
262 };
263
264 inline QByteArray noRef(const QByteArray &type)
265 {
266     if (type.endsWith('&')) {
267         if (type.endsWith("&&"))
268             return type.left(type.length()-2);
269         return type.left(type.length()-1);
270     }
271     return type;
272 }
273
274 QT_END_NAMESPACE
275
276 #endif // MOC_H