288
|
1 module qt.qtd.MOC;
|
|
2
|
|
3 import qt.qtd.ctfe.Format;
|
|
4
|
|
5 import std.typetuple;
|
|
6
|
|
7 import qt.Signal;
|
|
8 import qt.qtd.MetaMarshall;
|
302
|
9 public import qt.core.QString;
|
288
|
10
|
|
11 public import std.traits;
|
|
12 /**
|
|
13 Utils.
|
|
14 */
|
|
15
|
|
16 T qMin(T)(T a,T b) { if (a < b) return a; return b; }
|
|
17 T qMax(T)(T a, T b) { if (a < b) return b; return a; }
|
|
18 T qBound(T)(T min, T val,T max) { return qMax(min, qMin(max, val)); }
|
|
19
|
|
20 bool is_digit_char(const char s)
|
|
21 {
|
|
22 return (s >= '0' && s <= '9');
|
|
23 }
|
|
24
|
|
25 bool is_octal_char(const char s)
|
|
26 {
|
|
27 return (s >= '0' && s <= '7');
|
|
28 }
|
|
29
|
|
30 bool is_hex_char(const char s)
|
|
31 {
|
|
32 return ((s >= 'a' && s <= 'f')
|
|
33 || (s >= 'A' && s <= 'F')
|
|
34 || (s >= '0' && s <= '9')
|
|
35 );
|
|
36 }
|
|
37
|
|
38 int lastIndexOf(T)(T[] haystack, T[] needle, int from = -1)
|
|
39 {
|
|
40 auto l = haystack.length;
|
|
41 auto ol = needle.length;
|
|
42 int delta = l - ol;
|
|
43 if (from < 0)
|
|
44 from = delta;
|
|
45 if (from < 0 || from > l)
|
|
46 return -1;
|
|
47 if (from > delta)
|
|
48 from = delta;
|
|
49
|
|
50 while(from >= 0)
|
|
51 {
|
|
52 if (haystack[from..from+ol] == needle)
|
|
53 return from;
|
|
54 from--;
|
|
55 }
|
|
56 return -1;
|
|
57 }
|
|
58
|
|
59
|
|
60 T[] newArray(T)(size_t len, T[] from = [])
|
|
61 {
|
|
62 if (len == from.length)
|
|
63 return from;
|
|
64
|
|
65 if (!from.length)
|
|
66 from = [T.init];
|
|
67
|
|
68 if (from.length < len)
|
|
69 return newArray!T(len, from ~ from);
|
|
70
|
|
71 return from[0..len];
|
|
72 }
|
|
73
|
|
74 string replicate(int n, char value)
|
|
75 {
|
|
76 char[] ret = "".dup;
|
|
77 if (n > 0)
|
|
78 {
|
|
79 // ret = newArray!char(n);
|
|
80 for(int i = 0; i < n; i++)
|
|
81 ret ~= value;
|
|
82 }
|
|
83 return cast(string)ret;
|
|
84 }
|
|
85
|
|
86 template Repeat(T, int I)
|
|
87 {
|
|
88 static if (!I) alias TypeTuple!() Repeat;
|
|
89 else alias TypeTuple!(T, Repeat!(T, I - 1)) Repeat;
|
|
90 }
|
|
91
|
|
92 /**
|
|
93 CTFE MOC port.
|
|
94 */
|
|
95
|
|
96 enum MethodFlags {
|
|
97 AccessPrivate = 0x00,
|
|
98 AccessProtected = 0x01,
|
|
99 AccessPublic = 0x02,
|
|
100 MethodMethod = 0x00,
|
|
101 MethodSignal = 0x04,
|
|
102 MethodSlot = 0x08,
|
|
103 MethodConstructor = 0x0c,
|
|
104 MethodCompatibility = 0x10,
|
|
105 MethodCloned = 0x20,
|
|
106 MethodScriptable = 0x40
|
|
107 }
|
|
108
|
|
109 enum Access { Private, Protected, Public }
|
|
110
|
|
111 struct FunctionDef
|
|
112 {
|
|
113 /* FunctionDef(): returnTypeIsVolatile(false), access(Private), isConst(false), isVirtual(false),
|
|
114 inlineCode(false), wasCloned(false), isCompat(false), isInvokable(false),
|
|
115 isScriptable(false), isSlot(false), isSignal(false),
|
|
116 isConstructor(false), isDestructor(false), isAbstract(false) {}
|
|
117 */
|
|
118 // Type type;
|
|
119 // string normalizedType;
|
|
120 // string tag;
|
|
121 // string name;
|
|
122 string sig;
|
|
123 string arguments;
|
|
124 Access access;
|
|
125 /* bool returnTypeIsVolatile;
|
|
126
|
|
127 QList<ArgumentDef> arguments;
|
|
128
|
|
129 enum Access { Private, Protected, Public };
|
|
130 bool isConst;
|
|
131 bool isVirtual;
|
|
132 bool inlineCode;
|
|
133 bool wasCloned;
|
|
134
|
|
135 QByteArray inPrivateClass;
|
|
136 bool isCompat;
|
|
137 bool isInvokable;
|
|
138 bool isScriptable;
|
|
139 bool isSlot;
|
|
140 bool isSignal;
|
|
141 bool isConstructor;
|
|
142 bool isDestructor;
|
|
143 bool isAbstract;
|
|
144 */
|
|
145 }
|
|
146
|
|
147 FunctionDef newSlot(string sig, string args)
|
|
148 {
|
|
149 return FunctionDef(sig, args, Access.Public);
|
|
150 }
|
|
151
|
|
152 FunctionDef newSignal(string sig, string args)
|
|
153 {
|
|
154 return FunctionDef(sig, args, Access.Protected);
|
|
155 }
|
|
156
|
|
157 struct Generator
|
|
158 {
|
|
159 string output;
|
|
160 string[] strings;
|
|
161 // QByteArray purestSuperClass;
|
|
162 // QList<QByteArray> metaTypes;
|
|
163 }
|
|
164
|
|
165 int lengthOfEscapeSequence(string s, uint i)
|
|
166 {
|
|
167 if (s[i] != '\\' || i >= s.length - 1)
|
|
168 return 1;
|
|
169 const int startPos = i;
|
|
170 ++i;
|
|
171 auto ch = s[i];
|
|
172 if (ch == 'x') {
|
|
173 ++i;
|
|
174 while (i < s.length && is_hex_char(s[i]))
|
|
175 ++i;
|
|
176 } else if (is_octal_char(ch)) {
|
|
177 while (i < startPos + 4
|
|
178 && i < s.length
|
|
179 && is_octal_char(s[i])) {
|
|
180 ++i;
|
|
181 }
|
|
182 } else { // single character escape sequence
|
|
183 i = qMin(i + 1, s.length);
|
|
184 }
|
|
185 return i - startPos;
|
|
186 }
|
|
187
|
|
188 int strreg(ref Generator gen, string s)
|
|
189 {
|
|
190 int idx = 0;
|
|
191 foreach (str; gen.strings) {
|
|
192 if (str == s)
|
|
193 return idx;
|
|
194 idx += str.length + 1;
|
|
195 foreach (i, c; str) {
|
|
196 if (c == '\\') {
|
|
197 int cnt = lengthOfEscapeSequence(str, i) - 1;
|
|
198 idx -= cnt;
|
|
199 i += cnt;
|
|
200 }
|
|
201 }
|
|
202 }
|
|
203 gen.strings ~= s;
|
|
204 return idx;
|
|
205 }
|
|
206
|
|
207 void generateFunctions(ref Generator gen, FunctionDef[] list, string functype, byte type)
|
|
208 {
|
|
209 if (!list.length)
|
|
210 return;
|
|
211 gen.output ~= format_ctfe("\n // ${}s: signature, parameters, type, tag, flags\n", functype);
|
|
212
|
|
213 foreach (i, f; list) {
|
|
214 byte flags = type;
|
|
215
|
|
216 if (f.access == Access.Private)
|
|
217 flags |= MethodFlags.AccessPrivate;
|
|
218 else if (f.access == Access.Public)
|
|
219 flags |= MethodFlags.AccessPublic;
|
|
220 else if (f.access == Access.Protected)
|
|
221 flags |= MethodFlags.AccessProtected;
|
|
222
|
|
223 gen.output ~= format_ctfe(" ${}, ${}, ${}, ${}, 0x${:x},\n", strreg(gen, f.sig),
|
|
224 strreg(gen, f.arguments), strreg(gen, ""/*f.normalizedType*/), strreg(gen, ""/*f.tag*/), flags);
|
|
225 }
|
|
226 }
|
|
227
|
|
228 string generateCode(string className, FunctionDef[] signalList, FunctionDef[] slotList)
|
|
229 {
|
|
230 auto gen = Generator("", []);
|
|
231
|
|
232 /* bool isQt = (cdef->classname == "Qt");
|
|
233 bool isQObject = (cdef->classname == "QObject");
|
|
234 bool isConstructible = !cdef->constructorList.isEmpty();
|
|
235
|
|
236 //
|
|
237 // build the data array
|
|
238 //
|
|
239 int i = 0;
|
|
240
|
|
241
|
|
242 // filter out undeclared enumerators and sets
|
|
243 {
|
|
244 QList<EnumDef> enumList;
|
|
245 for (i = 0; i < cdef->enumList.count(); ++i) {
|
|
246 EnumDef def = cdef->enumList.at(i);
|
|
247 if (cdef->enumDeclarations.contains(def.name)) {
|
|
248 enumList += def;
|
|
249 }
|
|
250 QByteArray alias = cdef->flagAliases.value(def.name);
|
|
251 if (cdef->enumDeclarations.contains(alias)) {
|
|
252 def.name = alias;
|
|
253 enumList += def;
|
|
254 }
|
|
255 }
|
|
256 cdef->enumList = enumList;
|
|
257 }
|
|
258
|
|
259
|
|
260 QByteArray qualifiedClassNameIdentifier = cdef->qualified;
|
|
261 qualifiedClassNameIdentifier.replace(':', '_');
|
|
262 */
|
|
263 bool isConstructible = false;
|
|
264
|
|
265 FunctionDef[] propertyList, enumList, constructorList;
|
|
266 int index = 12;
|
|
267 gen.output ~= format_ctfe("static const uint[] qt_meta_data_${} = [\n", className);
|
|
268 gen.output ~= format_ctfe("\n // content:\n");
|
|
269 gen.output ~= format_ctfe(" ${}, // revision\n", 2);
|
|
270 gen.output ~= format_ctfe(" ${}, // classname\n", strreg(gen, className));
|
|
271 gen.output ~= format_ctfe(" ${}, ${}, // classinfo\n", 0, 0);
|
|
272 // index += cdef->classInfoList.count() * 2;
|
|
273
|
|
274 int methodCount = signalList.length + slotList.length;// + cdef->methodList.count();
|
|
275 gen.output ~= format_ctfe(" ${}, ${}, // methods\n", methodCount, methodCount ? index : 0);
|
|
276 index += methodCount * 5;
|
|
277 gen.output ~= format_ctfe(" ${}, ${}, // properties\n", propertyList.length, propertyList.length ? index : 0);
|
|
278 index += propertyList.length * 3;
|
|
279 // if(cdef->notifyableProperties)
|
|
280 // index += cdef->propertyList.count();
|
|
281 gen.output ~= format_ctfe(" ${}, ${}, // enums/sets\n", enumList.length, enumList.length ? index : 0);
|
|
282
|
|
283 // int enumsIndex = index;
|
|
284 // for (i = 0; i < cdef->enumList.count(); ++i)
|
|
285 // index += 4 + (cdef->enumList.at(i).values.count() * 2);
|
|
286 gen.output ~= format_ctfe(" ${}, ${}, // constructors\n", isConstructible ? constructorList.length : 0,
|
|
287 isConstructible ? index : 0);
|
|
288
|
|
289 //
|
|
290 // Build classinfo array
|
|
291 //
|
|
292 // generateClassInfos();
|
|
293
|
|
294 //
|
|
295 // Build signals array first, otherwise the signal indices would be wrong
|
|
296 //
|
|
297 generateFunctions(gen, signalList, "signal", MethodFlags.MethodSignal);
|
|
298
|
|
299 //
|
|
300 // Build slots array
|
|
301 //
|
|
302 generateFunctions(gen, slotList, "slot", MethodFlags.MethodSlot);
|
|
303
|
|
304 //
|
|
305 // Build method array
|
|
306 //
|
|
307 // generateFunctions(cdef->methodList, "method", MethodMethod);
|
|
308
|
|
309
|
|
310 //
|
|
311 // Build property array
|
|
312 //
|
|
313 // generateProperties();
|
|
314
|
|
315 //
|
|
316 // Build enums array
|
|
317 //
|
|
318 // generateEnums(enumsIndex);
|
|
319
|
|
320 //
|
|
321 // Build constructors array
|
|
322 //
|
|
323 // if (isConstructible)
|
|
324 // generateFunctions(cdef->constructorList, "constructor", MethodConstructor);
|
|
325
|
|
326 //
|
|
327 // Terminate data array
|
|
328 //
|
|
329 gen.output ~= format_ctfe("\n 0 // eod\n];\n\n");
|
|
330
|
|
331 //
|
|
332 // Build stringdata array
|
|
333 //
|
|
334 gen.output ~= format_ctfe("static const string qt_meta_stringdata_${} = \n", className);
|
|
335 gen.output ~= format_ctfe(" \"");
|
|
336 int col = 0;
|
|
337 int len = 0;
|
|
338 foreach (i, s; gen.strings) {
|
|
339 len = s.length;
|
|
340 if (col && col + len >= 72) {
|
|
341 gen.output ~= format_ctfe("\"\n \"");
|
|
342 col = 0;
|
|
343 } else if (len && s[0] >= '0' && s[0] <= '9') {
|
|
344 gen.output ~= format_ctfe("\"\"");
|
|
345 len += 2;
|
|
346 }
|
|
347 int idx = 0;
|
|
348 while (idx < s.length) {
|
|
349 if (idx > 0) {
|
|
350 col = 0;
|
|
351 gen.output ~= format_ctfe("\"\n \"");
|
|
352 }
|
|
353 int spanLen = qMin(cast(uint)70, s.length - idx);
|
|
354 // don't cut escape sequences at the end of a line
|
|
355 int backSlashPos = s.lastIndexOf("\\", idx + spanLen - 1);
|
|
356 if (backSlashPos >= idx) {
|
|
357 int escapeLen = lengthOfEscapeSequence(s, backSlashPos);
|
|
358 spanLen = qBound(spanLen, backSlashPos + escapeLen - idx, cast(int)(s.length - idx));
|
|
359 }
|
|
360 gen.output ~= s[idx..idx+spanLen];
|
|
361 idx += spanLen;
|
|
362 col += spanLen;
|
|
363 }
|
|
364
|
|
365 gen.output ~= "\\0";
|
|
366 col += len + 2;
|
|
367 }
|
|
368 gen.output ~= "\";\n\n";
|
|
369
|
|
370 return gen.output;
|
|
371 }
|
|
372
|
|
373 string metaCallArgs(Args...)()
|
|
374 {
|
|
375 string res;
|
|
376 foreach(i, _; Args) {
|
|
377 if (i > 0)
|
|
378 res ~= ",";
|
|
379 res ~= metaCallArgument!(Args[i])("_a[" ~ __toString(i+1) ~ "]");
|
|
380 }
|
|
381 return res;
|
|
382 }
|
|
383
|
|
384 string qtDeclArgs(Args...)()
|
|
385 {
|
|
386 string ret;
|
|
387 foreach(i, _; Args)
|
|
388 {
|
|
389 if(i > 0)
|
|
390 ret ~= ",";
|
|
391 ret ~= qtDeclArg!(Args[i]);
|
|
392 }
|
|
393 return ret;
|
|
394 }
|
|
395
|
|
396 string generate_qt_metacall(alias Signals, alias Slots)()
|
|
397 {
|
|
398 string res = "
|
|
399 protected int qt_metacall(QMetaObject.Call _c, int _id, void **_a)
|
|
400 {
|
|
401 _id = super.qt_metacall(_c, _id, _a);
|
|
402 if (_id < 0)
|
|
403 return _id;\n";
|
|
404
|
|
405 alias TypeTuple!(Signals.at, Slots.at) Methods;
|
|
406 enum methodCount = Methods.length;
|
|
407 if(methodCount)
|
|
408 {
|
|
409 res ~= "
|
|
410 if (_c == QMetaObject.Call.InvokeMetaMethod) {
|
|
411 switch (_id) {";
|
|
412 foreach(i, bogus; Repeat!(void, methodCount)) {
|
|
413 res ~= "
|
|
414 case " ~ __toString(i) ~ ": " ~ MetaEntryName!(Methods[i].at) ~ "(" ~ metaCallArgs!(MetaEntryArgs!(Methods[i].at))() ~ "); break;";
|
|
415 }
|
|
416 res ~= "\n default: ;\n }\n";
|
|
417 res ~= " _id -= " ~ __toString(methodCount) ~ ";";
|
|
418 res ~= "\n }";
|
|
419 }
|
|
420
|
|
421 res ~= "\n return _id;
|
|
422 }";
|
|
423 return res;
|
|
424 }
|
|
425
|
|
426 string dDeclArgs(Args...)()
|
|
427 {
|
|
428 string ret;
|
|
429 foreach(i, _; Args)
|
|
430 ret ~= ", " ~ Args[i].stringof;
|
|
431 return ret;
|
|
432 }
|
|
433 string genMetaMethodsConstr(alias Funcs)(string className)
|
|
434 {
|
|
435 string res;
|
|
436 enum funcsCount = Funcs.at.length;
|
|
437 foreach(i, bogus; Repeat!(void, funcsCount))
|
|
438 {
|
|
439 res ~= " index++;\n" ~
|
|
440 " _staticMetaObject.addMethod(new " ~ className ~ "(signature!(\"" ~ MetaEntryName!(Funcs.at[i].at) ~ "\"" ~ dDeclArgs!(MetaEntryArgs!(Funcs.at[i].at))()~ "), index));\n\n";
|
|
441 }
|
|
442 return res;
|
|
443 }
|
|
444 string generateMetaObjectConstruction(alias Signals, alias Slots)()
|
|
445 {
|
|
446 string res;
|
|
447 res ~= "\n
|
|
448 private static void _populateMetaInfo() {
|
|
449 alias BaseClassesTuple!(typeof(this))[0] BaseClass;
|
|
450 int index = BaseClass.staticMetaObject().methodCount() - 1;\n\n";
|
|
451
|
|
452 res ~= genMetaMethodsConstr!(Signals)("QMetaSignal");
|
|
453 res ~= genMetaMethodsConstr!(Slots)("QMetaSlot");
|
|
454
|
|
455 res ~= "
|
|
456 }\n";
|
|
457 return res;
|
|
458 }
|
|
459
|
|
460 string generateQMetaObject(string className)
|
|
461 {
|
|
462 string res;
|
|
463 res ~= "
|
|
464 QMetaObject metaObject() { return staticMetaObject; }
|
|
465 private static QMetaObject _staticMetaObject;
|
|
466 private static QMetaObjectNative _nativeStaticMetaObject;
|
|
467 public static QMetaObject staticMetaObject() { return _staticMetaObject; }
|
|
468 protected static void createStaticMetaObject() {
|
|
469 assert(!_staticMetaObject);
|
|
470 alias BaseClassesTuple!(typeof(this))[0] BaseClass;
|
|
471 if (!BaseClass._staticMetaObject)
|
|
472 BaseClass.createStaticMetaObject;
|
|
473 auto base = BaseClass._staticMetaObject;
|
|
474 _nativeStaticMetaObject = QMetaObjectNative(base.nativeId, qt_meta_stringdata_" ~ className ~ ".ptr,
|
|
475 qt_meta_data_" ~ className ~ ".ptr, null );
|
|
476
|
|
477 _staticMetaObject = new QMetaObject(&_nativeStaticMetaObject, base);
|
|
478 // _staticMetaObject.construct!(typeof(this));
|
|
479 _populateMetaInfo();
|
|
480 }
|
|
481 static this()
|
|
482 {
|
|
483 createStaticMetaObject();
|
|
484 }\n\n";
|
|
485 return res;
|
|
486 }
|
|
487
|
|
488 size_t commaCount(int argCount)
|
|
489 {
|
|
490 size_t ret = 0;
|
|
491 if(argCount > 1)
|
|
492 ret = argCount - 1;
|
|
493 return ret;
|
|
494 }
|
|
495
|
|
496 FunctionDef[] genFuncDefs(alias Funcs, alias newFunc)()
|
|
497 {
|
|
498 typeof(return) res;
|
|
499 enum funcsCount = Funcs.at.length;
|
|
500 foreach(i, bogus; Repeat!(void, funcsCount))
|
|
501 {
|
|
502 string args = replicate(commaCount((MetaEntryArgs!(Funcs.at[i].at)).length), ',');
|
|
503 string funcSig = MetaEntryName!(Funcs.at[i].at) ~ "(" ~ qtDeclArgs!(MetaEntryArgs!(Funcs.at[i].at))() ~ ")";
|
|
504 res ~= newFunc(funcSig, args);
|
|
505 }
|
|
506 return res;
|
|
507 }
|
|
508
|
|
509 string generateMetaInfo(string className, alias Signals, alias Slots)()
|
|
510 {
|
|
511 string res = "";
|
|
512 auto signalList = genFuncDefs!(Signals, newSignal)();
|
|
513 auto slotList = genFuncDefs!(Slots, newSlot)();
|
|
514 res ~= generateCode(className, signalList, slotList);
|
|
515 res ~= generate_qt_metacall!(Signals, Slots);
|
|
516 res ~= generateMetaObjectConstruction!(Signals, Slots);
|
|
517 res ~= generateQMetaObject(className);
|
|
518 return res;
|
|
519 }
|
|
520
|
|
521 template Q_OBJECT_BIND()
|
|
522 {
|
|
523 mixin ("enum lastSignalIndex_" ~ typeof(this).stringof ~ " = " ~ toStringNow!(lastSignalIndex!(typeof(this))) ~ ";");
|
|
524 }
|
|
525
|
|
526 template Q_OBJECT()
|
|
527 {
|
|
528 // pragma(msg, toStringNow!(lastSignalIndex!(typeof(this))));
|
|
529 mixin ("enum lastSignalIndex_" ~ typeof(this).stringof ~ " = " ~ toStringNow!(lastSignalIndex!(typeof(this))) ~ ";");
|
|
530
|
|
531 alias TupleWrapper!(findSymbols!(slotPrefix, typeof(this), ByOwner!(typeof(this)))) Slots;
|
|
532 alias TupleWrapper!(findSymbols!(signalPrefix, typeof(this), ByOwner!(typeof(this)))) Signals;
|
302
|
533 pragma(msg, generateMetaInfo!((typeof(this)).stringof, Signals, Slots)());
|
288
|
534 mixin(generateMetaInfo!((typeof(this)).stringof, Signals, Slots)());
|
|
535 }
|