| 1 | /**************************************************************************** | 
|---|
| 2 | ** | 
|---|
| 3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). | 
|---|
| 4 | ** Contact: Qt Software Information (qt-info@nokia.com) | 
|---|
| 5 | ** | 
|---|
| 6 | ** This file is part of the tools applications of the Qt Toolkit. | 
|---|
| 7 | ** | 
|---|
| 8 | ** $QT_BEGIN_LICENSE:LGPL$ | 
|---|
| 9 | ** Commercial Usage | 
|---|
| 10 | ** Licensees holding valid Qt Commercial licenses may use this file in | 
|---|
| 11 | ** accordance with the Qt Commercial License Agreement provided with the | 
|---|
| 12 | ** Software or, alternatively, in accordance with the terms contained in | 
|---|
| 13 | ** a written agreement between you and Nokia. | 
|---|
| 14 | ** | 
|---|
| 15 | ** GNU Lesser General Public License Usage | 
|---|
| 16 | ** Alternatively, this file may be used under the terms of the GNU Lesser | 
|---|
| 17 | ** General Public License version 2.1 as published by the Free Software | 
|---|
| 18 | ** Foundation and appearing in the file LICENSE.LGPL included in the | 
|---|
| 19 | ** packaging of this file.  Please review the following information to | 
|---|
| 20 | ** ensure the GNU Lesser General Public License version 2.1 requirements | 
|---|
| 21 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. | 
|---|
| 22 | ** | 
|---|
| 23 | ** In addition, as a special exception, Nokia gives you certain | 
|---|
| 24 | ** additional rights. These rights are described in the Nokia Qt LGPL | 
|---|
| 25 | ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this | 
|---|
| 26 | ** package. | 
|---|
| 27 | ** | 
|---|
| 28 | ** GNU General Public License Usage | 
|---|
| 29 | ** Alternatively, this file may be used under the terms of the GNU | 
|---|
| 30 | ** General Public License version 3.0 as published by the Free Software | 
|---|
| 31 | ** Foundation and appearing in the file LICENSE.GPL included in the | 
|---|
| 32 | ** packaging of this file.  Please review the following information to | 
|---|
| 33 | ** ensure the GNU General Public License version 3.0 requirements will be | 
|---|
| 34 | ** met: http://www.gnu.org/copyleft/gpl.html. | 
|---|
| 35 | ** | 
|---|
| 36 | ** If you are unsure which license is appropriate for your use, please | 
|---|
| 37 | ** contact the sales department at qt-sales@nokia.com. | 
|---|
| 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 <stdio.h> | 
|---|
| 50 | #include <ctype.h> | 
|---|
| 51 |  | 
|---|
| 52 | QT_BEGIN_NAMESPACE | 
|---|
| 53 |  | 
|---|
| 54 | struct QMetaObject; | 
|---|
| 55 |  | 
|---|
| 56 | struct Type | 
|---|
| 57 | { | 
|---|
| 58 | enum ReferenceType { NoReference, Reference, Pointer }; | 
|---|
| 59 |  | 
|---|
| 60 | inline Type() : isVolatile(false), isScoped(false), firstToken(NOTOKEN), referenceType(NoReference) {} | 
|---|
| 61 | inline explicit Type(const QByteArray &_name) : name(_name), isVolatile(false), firstToken(NOTOKEN), referenceType(NoReference) {} | 
|---|
| 62 | QByteArray name; | 
|---|
| 63 | uint isVolatile : 1; | 
|---|
| 64 | uint isScoped : 1; | 
|---|
| 65 | Token firstToken; | 
|---|
| 66 | ReferenceType referenceType; | 
|---|
| 67 | }; | 
|---|
| 68 |  | 
|---|
| 69 | struct EnumDef | 
|---|
| 70 | { | 
|---|
| 71 | QByteArray name; | 
|---|
| 72 | QList<QByteArray> values; | 
|---|
| 73 | }; | 
|---|
| 74 |  | 
|---|
| 75 | struct ArgumentDef | 
|---|
| 76 | { | 
|---|
| 77 | ArgumentDef() : isDefault(false) {} | 
|---|
| 78 | Type type; | 
|---|
| 79 | QByteArray rightType, normalizedType, name; | 
|---|
| 80 | QByteArray typeNameForCast; // type name to be used in cast from void * in metacall | 
|---|
| 81 | bool isDefault; | 
|---|
| 82 | }; | 
|---|
| 83 |  | 
|---|
| 84 | struct FunctionDef | 
|---|
| 85 | { | 
|---|
| 86 | FunctionDef(): returnTypeIsVolatile(false), access(Private), isConst(false), isVirtual(false), | 
|---|
| 87 | inlineCode(false), wasCloned(false), isCompat(false), isInvokable(false), | 
|---|
| 88 | isScriptable(false), isSlot(false), isSignal(false), | 
|---|
| 89 | isConstructor(false), isDestructor(false), isAbstract(false) {} | 
|---|
| 90 | Type type; | 
|---|
| 91 | QByteArray normalizedType; | 
|---|
| 92 | QByteArray tag; | 
|---|
| 93 | QByteArray name; | 
|---|
| 94 | bool returnTypeIsVolatile; | 
|---|
| 95 |  | 
|---|
| 96 | QList<ArgumentDef> arguments; | 
|---|
| 97 |  | 
|---|
| 98 | enum Access { Private, Protected, Public }; | 
|---|
| 99 | Access access; | 
|---|
| 100 | bool isConst; | 
|---|
| 101 | bool isVirtual; | 
|---|
| 102 | bool inlineCode; | 
|---|
| 103 | bool wasCloned; | 
|---|
| 104 |  | 
|---|
| 105 | QByteArray inPrivateClass; | 
|---|
| 106 | bool isCompat; | 
|---|
| 107 | bool isInvokable; | 
|---|
| 108 | bool isScriptable; | 
|---|
| 109 | bool isSlot; | 
|---|
| 110 | bool isSignal; | 
|---|
| 111 | bool isConstructor; | 
|---|
| 112 | bool isDestructor; | 
|---|
| 113 | bool isAbstract; | 
|---|
| 114 | }; | 
|---|
| 115 |  | 
|---|
| 116 | struct PropertyDef | 
|---|
| 117 | { | 
|---|
| 118 | PropertyDef():notifyId(-1), gspec(ValueSpec){} | 
|---|
| 119 | QByteArray name, type, read, write, reset, designable, scriptable, editable, stored, user, notify; | 
|---|
| 120 | int notifyId; | 
|---|
| 121 | enum Specification  { ValueSpec, ReferenceSpec, PointerSpec }; | 
|---|
| 122 | Specification gspec; | 
|---|
| 123 | bool stdCppSet() const { | 
|---|
| 124 | QByteArray s("set"); | 
|---|
| 125 | s += toupper(name[0]); | 
|---|
| 126 | s += name.mid(1); | 
|---|
| 127 | return (s == write); | 
|---|
| 128 | } | 
|---|
| 129 | }; | 
|---|
| 130 |  | 
|---|
| 131 |  | 
|---|
| 132 | struct ClassInfoDef | 
|---|
| 133 | { | 
|---|
| 134 | QByteArray name; | 
|---|
| 135 | QByteArray value; | 
|---|
| 136 | }; | 
|---|
| 137 |  | 
|---|
| 138 | struct ClassDef { | 
|---|
| 139 | ClassDef(): | 
|---|
| 140 | hasQObject(false), hasQGadget(false), notifyableProperties(0){} | 
|---|
| 141 | QByteArray classname; | 
|---|
| 142 | QByteArray qualified; | 
|---|
| 143 | QList<QPair<QByteArray, FunctionDef::Access> > superclassList; | 
|---|
| 144 |  | 
|---|
| 145 | struct Interface | 
|---|
| 146 | { | 
|---|
| 147 | inline explicit Interface(const QByteArray &_className) | 
|---|
| 148 | : className(_className) {} | 
|---|
| 149 | QByteArray className; | 
|---|
| 150 | QByteArray interfaceId; | 
|---|
| 151 | }; | 
|---|
| 152 | QList<QList<Interface> >interfaceList; | 
|---|
| 153 |  | 
|---|
| 154 | bool hasQObject; | 
|---|
| 155 | bool hasQGadget; | 
|---|
| 156 |  | 
|---|
| 157 | QList<FunctionDef> constructorList; | 
|---|
| 158 | QList<FunctionDef> signalList, slotList, methodList, publicList; | 
|---|
| 159 | int notifyableProperties; | 
|---|
| 160 | QList<PropertyDef> propertyList; | 
|---|
| 161 | QList<ClassInfoDef> classInfoList; | 
|---|
| 162 | QMap<QByteArray, bool> enumDeclarations; | 
|---|
| 163 | QList<EnumDef> enumList; | 
|---|
| 164 | QMap<QByteArray, QByteArray> flagAliases; | 
|---|
| 165 |  | 
|---|
| 166 | int begin; | 
|---|
| 167 | int end; | 
|---|
| 168 | }; | 
|---|
| 169 |  | 
|---|
| 170 | struct NamespaceDef { | 
|---|
| 171 | QByteArray name; | 
|---|
| 172 | int begin; | 
|---|
| 173 | int end; | 
|---|
| 174 | }; | 
|---|
| 175 |  | 
|---|
| 176 | class Moc : public Parser | 
|---|
| 177 | { | 
|---|
| 178 | public: | 
|---|
| 179 | Moc() | 
|---|
| 180 | : noInclude(false), generatedCode(false) | 
|---|
| 181 | {} | 
|---|
| 182 |  | 
|---|
| 183 | QByteArray filename; | 
|---|
| 184 |  | 
|---|
| 185 | bool noInclude; | 
|---|
| 186 | bool generatedCode; | 
|---|
| 187 | QByteArray includePath; | 
|---|
| 188 | QList<QByteArray> includeFiles; | 
|---|
| 189 | QList<ClassDef> classList; | 
|---|
| 190 | QMap<QByteArray, QByteArray> interface2IdMap; | 
|---|
| 191 | QList<QByteArray> metaTypes; | 
|---|
| 192 | QSet<QByteArray> knownQObjectClasses; | 
|---|
| 193 |  | 
|---|
| 194 | void parse(); | 
|---|
| 195 | void generate(FILE *out); | 
|---|
| 196 | QList<QMetaObject*> generate(bool ignoreProperties); | 
|---|
| 197 |  | 
|---|
| 198 | bool parseClassHead(ClassDef *def); | 
|---|
| 199 | inline bool inClass(const ClassDef *def) const { | 
|---|
| 200 | return index > def->begin && index < def->end - 1; | 
|---|
| 201 | } | 
|---|
| 202 |  | 
|---|
| 203 | inline bool inNamespace(const NamespaceDef *def) const { | 
|---|
| 204 | return index > def->begin && index < def->end - 1; | 
|---|
| 205 | } | 
|---|
| 206 |  | 
|---|
| 207 | Type parseType(); | 
|---|
| 208 |  | 
|---|
| 209 | bool parseEnum(EnumDef *def); | 
|---|
| 210 |  | 
|---|
| 211 | bool parseFunction(FunctionDef *def, bool inMacro = false); | 
|---|
| 212 | bool parseMaybeFunction(const ClassDef *cdef, FunctionDef *def); | 
|---|
| 213 |  | 
|---|
| 214 | void parseSlots(ClassDef *def, FunctionDef::Access access); | 
|---|
| 215 | void parseSignals(ClassDef *def); | 
|---|
| 216 | void parseProperty(ClassDef *def); | 
|---|
| 217 | void parseEnumOrFlag(ClassDef *def, bool isFlag); | 
|---|
| 218 | void parseFlag(ClassDef *def); | 
|---|
| 219 | void parseClassInfo(ClassDef *def); | 
|---|
| 220 | void parseInterfaces(ClassDef *def); | 
|---|
| 221 | void parseDeclareInterface(); | 
|---|
| 222 | void parseDeclareMetatype(); | 
|---|
| 223 | void parseSlotInPrivate(ClassDef *def, FunctionDef::Access access); | 
|---|
| 224 |  | 
|---|
| 225 | void parseFunctionArguments(FunctionDef *def); | 
|---|
| 226 |  | 
|---|
| 227 | QByteArray lexemUntil(Token); | 
|---|
| 228 | bool until(Token); | 
|---|
| 229 |  | 
|---|
| 230 | // test for Q_INVOCABLE, Q_SCRIPTABLE, etc. and set the flags | 
|---|
| 231 | // in FunctionDef accordingly | 
|---|
| 232 | bool testFunctionAttribute(FunctionDef *def); | 
|---|
| 233 | bool testFunctionAttribute(Token tok, FunctionDef *def); | 
|---|
| 234 |  | 
|---|
| 235 | void checkSuperClasses(ClassDef *def); | 
|---|
| 236 | }; | 
|---|
| 237 |  | 
|---|
| 238 | inline QByteArray noRef(const QByteArray &type) | 
|---|
| 239 | { | 
|---|
| 240 | if (type.endsWith('&')) | 
|---|
| 241 | return type.left(type.length()-1); | 
|---|
| 242 | return type; | 
|---|
| 243 | } | 
|---|
| 244 |  | 
|---|
| 245 | QT_END_NAMESPACE | 
|---|
| 246 |  | 
|---|
| 247 | #endif // MOC_H | 
|---|