1 | // Copyright (C) 2016 The Qt Company Ltd. |
2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0 |
3 | |
4 | #ifndef MOC_H |
5 | #define MOC_H |
6 | |
7 | #include "parser.h" |
8 | #include <qstringlist.h> |
9 | #include <qmap.h> |
10 | #include <qjsondocument.h> |
11 | #include <qjsonarray.h> |
12 | #include <qjsonobject.h> |
13 | #include <qtyperevision.h> |
14 | #include <stdio.h> |
15 | |
16 | #include <private/qtools_p.h> |
17 | |
18 | QT_BEGIN_NAMESPACE |
19 | |
20 | struct QMetaObject; |
21 | |
22 | struct Type |
23 | { |
24 | enum ReferenceType { NoReference, Reference, RValueReference, Pointer }; |
25 | |
26 | inline Type() : isVolatile(false), isScoped(false), firstToken(NOTOKEN), referenceType(NoReference) {} |
27 | inline explicit Type(const QByteArray &_name) |
28 | : name(_name), rawName(name), isVolatile(false), isScoped(false), firstToken(NOTOKEN), referenceType(NoReference) {} |
29 | QByteArray name; |
30 | //When used as a return type, the type name may be modified to remove the references. |
31 | // rawName is the type as found in the function signature |
32 | QByteArray rawName; |
33 | uint isVolatile : 1; |
34 | uint isScoped : 1; |
35 | Token firstToken; |
36 | ReferenceType referenceType; |
37 | }; |
38 | Q_DECLARE_TYPEINFO(Type, Q_RELOCATABLE_TYPE); |
39 | |
40 | struct ClassDef; |
41 | struct EnumDef |
42 | { |
43 | QByteArray name; |
44 | QByteArray enumName; |
45 | QByteArray type; |
46 | QList<QByteArray> values; |
47 | bool isEnumClass; // c++11 enum class |
48 | EnumDef() : isEnumClass(false) {} |
49 | QJsonObject toJson(const ClassDef &cdef) const; |
50 | QByteArray qualifiedType(const ClassDef *cdef) const; |
51 | }; |
52 | Q_DECLARE_TYPEINFO(EnumDef, Q_RELOCATABLE_TYPE); |
53 | |
54 | struct ArgumentDef |
55 | { |
56 | ArgumentDef() : isDefault(false) {} |
57 | Type type; |
58 | QByteArray rightType, normalizedType, name; |
59 | QByteArray typeNameForCast; // type name to be used in cast from void * in metacall |
60 | bool isDefault; |
61 | |
62 | QJsonObject toJson() const; |
63 | }; |
64 | Q_DECLARE_TYPEINFO(ArgumentDef, Q_RELOCATABLE_TYPE); |
65 | |
66 | struct FunctionDef |
67 | { |
68 | Type type; |
69 | QList<ArgumentDef> arguments; |
70 | QByteArray normalizedType; |
71 | QByteArray tag; |
72 | QByteArray name; |
73 | QByteArray inPrivateClass; |
74 | |
75 | enum Access { Private, Protected, Public }; |
76 | Access access = Private; |
77 | int revision = 0; |
78 | |
79 | bool isConst = false; |
80 | bool isVirtual = false; |
81 | bool isStatic = false; |
82 | bool inlineCode = false; |
83 | bool wasCloned = false; |
84 | |
85 | bool returnTypeIsVolatile = false; |
86 | |
87 | bool isCompat = false; |
88 | bool isInvokable = false; |
89 | bool isScriptable = false; |
90 | bool isSlot = false; |
91 | bool isSignal = false; |
92 | bool isPrivateSignal = false; |
93 | bool isConstructor = false; |
94 | bool isDestructor = false; |
95 | bool isAbstract = false; |
96 | bool isRawSlot = false; |
97 | |
98 | QJsonObject toJson(int index) const; |
99 | static void accessToJson(QJsonObject *obj, Access acs); |
100 | }; |
101 | Q_DECLARE_TYPEINFO(FunctionDef, Q_RELOCATABLE_TYPE); |
102 | |
103 | struct PropertyDef |
104 | { |
105 | bool stdCppSet() const { |
106 | if (name.isEmpty()) |
107 | return false; |
108 | QByteArray s("set" ); |
109 | s += QtMiscUtils::toAsciiUpper(ch: name[0]); |
110 | s += name.mid(index: 1); |
111 | return (s == write); |
112 | } |
113 | |
114 | QByteArray name, type, member, read, write, bind, reset, designable, scriptable, stored, user, notify, inPrivateClass; |
115 | int notifyId = -1; // -1 means no notifyId, >= 0 means signal defined in this class, < -1 means signal not defined in this class |
116 | enum Specification { ValueSpec, ReferenceSpec, PointerSpec }; |
117 | Specification gspec = ValueSpec; |
118 | int revision = 0; |
119 | bool constant = false; |
120 | bool final = false; |
121 | bool required = false; |
122 | int relativeIndex = -1; // property index in current metaobject |
123 | |
124 | qsizetype location = -1; // token index, used for error reporting |
125 | |
126 | QJsonObject toJson() const; |
127 | }; |
128 | Q_DECLARE_TYPEINFO(PropertyDef, Q_RELOCATABLE_TYPE); |
129 | |
130 | struct PrivateQPropertyDef |
131 | { |
132 | Type type; |
133 | QByteArray name; |
134 | QByteArray setter; |
135 | QByteArray accessor; |
136 | QByteArray storage; |
137 | }; |
138 | Q_DECLARE_TYPEINFO(PrivateQPropertyDef, Q_RELOCATABLE_TYPE); |
139 | |
140 | struct ClassInfoDef |
141 | { |
142 | QByteArray name; |
143 | QByteArray value; |
144 | }; |
145 | Q_DECLARE_TYPEINFO(ClassInfoDef, Q_RELOCATABLE_TYPE); |
146 | |
147 | struct BaseDef { |
148 | QByteArray classname; |
149 | QByteArray qualified; |
150 | QList<ClassInfoDef> classInfoList; |
151 | QMap<QByteArray, bool> enumDeclarations; |
152 | QList<EnumDef> enumList; |
153 | QMap<QByteArray, QByteArray> flagAliases; |
154 | qsizetype begin = 0; |
155 | qsizetype end = 0; |
156 | qsizetype lineNumber = 0; |
157 | }; |
158 | |
159 | struct SuperClass { |
160 | QByteArray classname; |
161 | QByteArray qualified; |
162 | FunctionDef::Access access; |
163 | }; |
164 | Q_DECLARE_TYPEINFO(SuperClass, Q_RELOCATABLE_TYPE); |
165 | |
166 | struct ClassDef : BaseDef { |
167 | QList<SuperClass> superclassList; |
168 | |
169 | struct Interface |
170 | { |
171 | Interface() { } // for QList, don't use |
172 | inline explicit Interface(const QByteArray &_className) |
173 | : className(_className) {} |
174 | QByteArray className; |
175 | QByteArray interfaceId; |
176 | }; |
177 | QList<QList<Interface>> interfaceList; |
178 | |
179 | struct PluginData { |
180 | QByteArray iid; |
181 | QByteArray uri; |
182 | QMap<QString, QJsonArray> metaArgs; |
183 | QJsonDocument metaData; |
184 | } pluginData; |
185 | |
186 | QList<FunctionDef> constructorList; |
187 | QList<FunctionDef> signalList, slotList, methodList, publicList; |
188 | QList<QByteArray> nonClassSignalList; |
189 | QList<PropertyDef> propertyList; |
190 | int revisionedMethods = 0; |
191 | |
192 | bool hasQObject = false; |
193 | bool hasQGadget = false; |
194 | bool hasQNamespace = false; |
195 | bool requireCompleteMethodTypes = false; |
196 | |
197 | QJsonObject toJson() const; |
198 | }; |
199 | Q_DECLARE_TYPEINFO(ClassDef, Q_RELOCATABLE_TYPE); |
200 | Q_DECLARE_TYPEINFO(ClassDef::Interface, Q_RELOCATABLE_TYPE); |
201 | |
202 | struct NamespaceDef : BaseDef { |
203 | bool hasQNamespace = false; |
204 | bool doGenerate = false; |
205 | }; |
206 | Q_DECLARE_TYPEINFO(NamespaceDef, Q_RELOCATABLE_TYPE); |
207 | |
208 | class Moc : public Parser |
209 | { |
210 | public: |
211 | enum PropertyMode { Named, Anonymous }; |
212 | |
213 | Moc() |
214 | : noInclude(false), mustIncludeQPluginH(false), requireCompleteTypes(false) |
215 | {} |
216 | |
217 | QByteArray filename; |
218 | |
219 | bool noInclude; |
220 | bool mustIncludeQPluginH; |
221 | bool requireCompleteTypes; |
222 | QByteArray includePath; |
223 | QList<QByteArray> includeFiles; |
224 | QList<ClassDef> classList; |
225 | QMap<QByteArray, QByteArray> interface2IdMap; |
226 | QList<QByteArray> metaTypes; |
227 | // map from class name to fully qualified name |
228 | QHash<QByteArray, QByteArray> knownQObjectClasses; |
229 | QHash<QByteArray, QByteArray> knownGadgets; |
230 | QMap<QString, QJsonArray> metaArgs; |
231 | QList<QString> parsedPluginMetadataFiles; |
232 | |
233 | void parse(); |
234 | void generate(FILE *out, FILE *jsonOutput); |
235 | |
236 | bool parseClassHead(ClassDef *def); |
237 | inline bool inClass(const ClassDef *def) const { |
238 | return index > def->begin && index < def->end - 1; |
239 | } |
240 | |
241 | inline bool inNamespace(const NamespaceDef *def) const { |
242 | return index > def->begin && index < def->end - 1; |
243 | } |
244 | |
245 | const QByteArray &toFullyQualified(const QByteArray &name) const noexcept; |
246 | |
247 | void prependNamespaces(BaseDef &def, const QList<NamespaceDef> &namespaceList) const; |
248 | |
249 | Type parseType(); |
250 | |
251 | bool parseEnum(EnumDef *def); |
252 | |
253 | bool parseFunction(FunctionDef *def, bool inMacro = false); |
254 | bool parseMaybeFunction(const ClassDef *cdef, FunctionDef *def); |
255 | |
256 | void parseSlots(ClassDef *def, FunctionDef::Access access); |
257 | void parseSignals(ClassDef *def); |
258 | void parseProperty(ClassDef *def, PropertyMode mode); |
259 | void parsePluginData(ClassDef *def); |
260 | |
261 | void createPropertyDef(PropertyDef &def, int propertyIndex, PropertyMode mode); |
262 | |
263 | void parsePropertyAttributes(PropertyDef &propDef); |
264 | void parseEnumOrFlag(BaseDef *def, bool isFlag); |
265 | void parseFlag(BaseDef *def); |
266 | enum class EncounteredQmlMacro {Yes, No}; |
267 | EncounteredQmlMacro parseClassInfo(BaseDef *def); |
268 | void parseClassInfo(ClassDef *def); |
269 | void parseInterfaces(ClassDef *def); |
270 | void parseDeclareInterface(); |
271 | void parseDeclareMetatype(); |
272 | void parseMocInclude(); |
273 | void parseSlotInPrivate(ClassDef *def, FunctionDef::Access access); |
274 | QByteArray parsePropertyAccessor(); |
275 | void parsePrivateProperty(ClassDef *def, PropertyMode mode); |
276 | |
277 | void parseFunctionArguments(FunctionDef *def); |
278 | |
279 | QByteArray lexemUntil(Token); |
280 | bool until(Token); |
281 | |
282 | // test for Q_INVOCABLE, Q_SCRIPTABLE, etc. and set the flags |
283 | // in FunctionDef accordingly |
284 | bool testFunctionAttribute(FunctionDef *def); |
285 | bool testFunctionAttribute(Token tok, FunctionDef *def); |
286 | bool testFunctionRevision(FunctionDef *def); |
287 | QTypeRevision parseRevision(); |
288 | |
289 | bool skipCxxAttributes(); |
290 | |
291 | void checkSuperClasses(ClassDef *def); |
292 | void checkProperties(ClassDef* cdef); |
293 | bool testForFunctionModifiers(FunctionDef *def); |
294 | |
295 | void checkListSizes(const ClassDef &def); |
296 | }; |
297 | |
298 | inline QByteArray noRef(const QByteArray &type) |
299 | { |
300 | if (type.endsWith(c: '&')) { |
301 | if (type.endsWith(bv: "&&" )) |
302 | return type.left(n: type.size()-2); |
303 | return type.left(n: type.size()-1); |
304 | } |
305 | return type; |
306 | } |
307 | |
308 | QT_END_NAMESPACE |
309 | |
310 | #endif // MOC_H |
311 | |