comparison d1/qtd/MOC.d @ 344:96a75b1e5b26

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