This repository has been archived by the owner on Apr 2, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
generate.lisp
422 lines (372 loc) · 16.9 KB
/
generate.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
#|
This file is a part of Qtools
(c) 2015 Shirakumo http://tymoon.eu (shinmera@tymoon.eu)
Author: Nicolas Hafner <shinmera@tymoon.eu>
|#
(in-package #:org.shirakumo.qtools)
(defvar *target-package* (or (find-package "Q+")
(make-package "Q+" :use ())))
(defvar *smoke-modules* '(:qt3support :qtcore :qtdbus
:qtdeclarative :qtgui :qthelp
:qtmultimedia :qtnetwork
:qtopengl :qtscript :qtsql
:qtsvg :qttest :qtuitools
:qtwebkit :qtxml :qtxmlpatterns
:phonon :qimageblitz :sci))
(defvar *operator-map*
(let ((table (make-hash-table :test 'equalp)))
(loop for (op . name) in '(("==" . "=") ("!=" . "/=")
(">" . ">") (">=" . ">=")
("<" . "<") ("<=" . "<=")
("!" . "NOT") ("%" . "MOD")
("*" . "*") ("/" . "/")
("-" . "-") ("+" . "+")
("~" . "LOGNOT") ("&" . "LOGAND")
("|" . "LOGIOR") ("^" . "LOGXOR")
(">>" . "ASH-") ("<<" . "ASH")
("&&" . "AND") ("||" . "OR")
("[]" . "AREF") ("() ." . "FUNCALL")
("+=" . "INCF") ("-=" . "DECF")
("=" . "SET"))
do (setf (gethash (format NIL "operator~a" op) table) name))
table))
(defvar *qmethods* (make-hash-table :test 'equal))
(defvar *generated-modules* ())
(defun load-all-smoke-modules (&rest mods)
(dolist (mod mods)
(let ((mod (intern (string-upcase mod) "KEYWORD")))
(ensure-smoke mod)
(pushnew mod *smoke-modules*))))
(defun loaded-smoke-modules ()
(remove-if-not #'(lambda (a) (qt::named-module-number (string-downcase a)))
*smoke-modules*))
(defun clear-method-info ()
(setf *qmethods* (make-hash-table :test 'equal))
T)
(defun string-starts-with-p (start string &key (offset 0))
(and (< (length start) (+ offset (length string)))
(string= start string :start2 offset :end2 (+ offset (length start)))))
(defun qmethod-operator-p (method)
(let ((name (qmethod-name method)))
(string-starts-with-p "operator" name)))
(defun qmethod-cast-operator-p (method)
(let ((name (qmethod-name method)))
(string-starts-with-p "operator " name)))
(defun qmethod-bogus-p (method)
(let ((name (qmethod-name method)))
(string-starts-with-p "_" name)))
(defun qmethod-translatable-operator-p (method)
(not (null (gethash (qmethod-name method) *operator-map*))))
(defun qmethod-globalspace-p (method)
(= (qt::qmethod-class method)
(find-qclass "QGlobalSpace")))
(defun clean-method-name (method)
(string-trim "#$?" (etypecase method
(integer (qmethod-name method))
(string method))))
(defun target-symbol (format-string &rest format-args)
(let ((name (apply #'format NIL format-string format-args)))
(or (find-symbol name *target-package*)
(intern name *target-package*))))
(defmacro with-output-to-target-symbol ((stream) &body body)
`(target-symbol
"~a"
(with-output-to-string (,stream)
,@body)))
(defun write-qclass-name (qclass stream)
(loop with prev-colon = NIL
for char across (etypecase qclass
(integer (qclass-name qclass))
(string qclass))
do (cond ((char= char #\:)
(setf prev-colon T))
(T
(when prev-colon
(write-char #\- stream))
(setf prev-colon NIL)
(write-char (char-upcase char) stream)))))
(defun write-qmethod-name (qmethod stream)
(loop with prev-cap = T
for char across (clean-method-name
(etypecase qmethod
(integer (qmethod-name qmethod))
(string qmethod)))
do (cond ((find char "ABCDEFGHIJKLMNOPQRSTUVWXYZ")
(unless prev-cap
(write-char #\- stream))
(setf prev-cap T)
(write-char char stream))
((char= char #\_)
(setf prev-cap T)
(write-char char stream))
(T (setf prev-cap NIL)
(write-char (char-upcase char) stream)))))
(defun cl-constructor-name (method)
(with-output-to-target-symbol (stream)
(write-string "MAKE-" stream)
(write-qclass-name (qt::qmethod-class method) stream)))
(defun cl-constant-name (method)
(with-output-to-target-symbol (stream)
(write-qclass-name (qt::qmethod-class method) stream)
(write-char #\. stream)
(write-qmethod-name method stream)))
(defun cl-variable-name (method)
(with-output-to-target-symbol (stream)
(write-char #\* stream)
(write-qclass-name (qt::qmethod-class method) stream)
(write-char #\- stream)
(write-qmethod-name method stream)
(write-char #\* stream)))
(defun cl-operator-name (method)
(let ((name (gethash (qmethod-name method) *operator-map*)))
(if name
(target-symbol name)
(error "Method ~a is not a translatable operator." method))))
(defun cl-static-method-name (method)
(with-output-to-target-symbol (stream)
(write-qclass-name (qt::qmethod-class method) stream)
(write-char #\- stream)
(write-qmethod-name method stream)))
(defun cl-method-name (method)
(with-output-to-target-symbol (stream)
(write-qmethod-name method stream)))
(defun method-needed-p (method)
(and (not (qmethod-bogus-p method))
(not (qt::qmethod-dtor-p method))
(not (qt::qmethod-internal-p method))
(not (qmethod-cast-operator-p method))
(or (not (qmethod-operator-p method))
(qmethod-translatable-operator-p method))))
(defun method-symbol (method)
(cond
((qt::qmethod-enum-p method)
(cl-constant-name method))
((or (qt::qmethod-ctor-p method)
(qt::qmethod-copyctor-p method))
(cl-constructor-name method))
((qmethod-operator-p method)
(cl-operator-name method))
((qt::qmethod-static-p method)
(cl-static-method-name method))
(T
(cl-method-name method))))
(defun process-method (method)
(when (method-needed-p method)
(pushnew method (gethash (method-symbol method) *qmethods*))))
(defun process-all-methods ()
(clear-method-info)
(setf *generated-modules* (loaded-smoke-modules))
(qt::map-methods #'process-method))
(defun ensure-methods-processed ()
(unless (equal (loaded-smoke-modules) *generated-modules*)
(process-all-methods)
(repopulate-class-map)))
(defun ensure-methods (method)
(or (etypecase method
(symbol (gethash method *qmethods*))
(list method)
(fixnum (ensure-methods (method-symbol method)))
(string (ensure-methods (or (find-symbol method *target-package*)
(error "No methods found with name ~s" method)))))
(error "No methods found for ~s" method)))
(defun compile-wrapper (method)
(let* ((methods (ensure-methods method))
(method (first methods)))
(cond
((qt::qmethod-enum-p method)
(compile-constant methods))
((or (qt::qmethod-ctor-p method)
(qt::qmethod-copyctor-p method))
(compile-constructor methods))
((qmethod-operator-p method)
(compile-operator methods))
((qt::qmethod-static-p method)
(compile-static-method methods))
(T
(compile-method methods)))))
(defun map-compile-all (function)
(loop for name being the hash-keys of *qmethods*
do (funcall function (compile-wrapper name))))
(defun q+apropos (term)
(ensure-methods-processed)
(flet ((strip (string)
(cl-ppcre:regex-replace-all "[\\-_\\.]" string "")))
(let ((terms (cl-ppcre:split " +" (strip (string term)))))
(loop for k being the hash-keys of *qmethods*
for method = (strip (string k))
do (when (every (lambda (term) (search term method :test #'char-equal)) terms)
(print k))))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; COMPILERS
(defun %method-see (stream method &rest rest)
(declare (ignore rest))
(format stream "~a::~a(~{~a~^, ~})"
(qclass-name (qt::qmethod-class method))
(qmethod-name method)
(mapcar #'qt::qtype-name (qt::list-qmethod-argument-types method))))
(defun generate-method-docstring (methods)
(format NIL "Call to Qt method ~a
~{See ~/org.shirakumo.qtools::%method-see/~^~%~}"
(clean-method-name (first methods))
(sort (copy-list methods) #'< :key #'qt::qmethod-argument-number)))
(defun generate-constant-docstring (method)
(format NIL "Constant for Qt enum ~a::~a"
(qclass-name (qt::qmethod-class method))
(clean-method-name method)))
(defmacro with-args ((required optional optional-p) methods &body body)
(let ((argnums (gensym "ARGNUMS"))
(maxargs (gensym "MAXARGS"))
(minargs (gensym "MINARGS"))
(i (gensym "I")))
`(let* ((,argnums (mapcar #'qt::qmethod-argument-number ,methods))
(,maxargs (apply #'max ,argnums))
(,minargs (apply #'min ,argnums))
(,required (loop for ,i from 0 below ,minargs
collect (target-symbol "%REQ-~d" ,i)))
(,optional (loop for ,i from 0 below (- ,maxargs ,minargs)
collect (target-symbol "%OPT-~d" ,i)))
(,optional-p (loop for ,i in ,optional
collect `(,,i NIL ,(target-symbol "~a-P" ,i)))))
,@body)))
(defmacro define-extern-inline-fun (name lambda-list &body body)
`(progn
(export ',name ,(package-name (symbol-package name)))
(declaim (inline ,name))
(defun ,name ,lambda-list ,@body)))
(defmacro define-extern-macro (name lambda-list &body body)
`(progn
(export ',name ,(package-name (symbol-package name)))
(defmacro ,name ,lambda-list ,@body)))
(defun compile-method (methods)
(let ((method (clean-method-name (first methods)))
(name (cl-method-name (first methods)))
(whole (target-symbol "%WHOLE"))
(instance (target-symbol "%INSTANCE")))
(with-args (reqargs optargs optargs-p) methods
`(progn
(define-extern-inline-fun ,name (,instance ,@reqargs ,@(when optargs `(&optional ,@optargs-p)))
,(generate-method-docstring methods)
,(if optargs
`(cond
,@(loop for arg in (reverse optargs-p)
for arg-p = (third arg)
for count from 0
collect `(,arg-p
(optimized-call T ,instance ,method ,@reqargs ,@(butlast optargs count))))
(T (optimized-call T ,instance ,method ,@reqargs)))
`(optimized-call T ,instance ,method ,@reqargs)))
,@(when optargs
`((define-compiler-macro ,name (&whole ,whole ,instance ,@reqargs ,@(when optargs `(&optional ,@optargs)))
(declare (ignore ,@reqargs ,@optargs))
`(optimized-call T ,,instance ,,method ,@(cddr ,whole)))))))))
(defun compile-static-method (methods)
(let ((class-name (qclass-name (qt::qmethod-class (first methods))))
(method (clean-method-name (qmethod-name (first methods))))
(name (cl-static-method-name (first methods)))
(whole (target-symbol "%WHOLE")))
(with-args (reqargs optargs optargs-p) methods
`(progn
(define-extern-inline-fun ,name (,@reqargs ,@(when optargs `(&optional ,@optargs-p)))
,(generate-method-docstring methods)
,(if optargs
`(cond
,@(loop for arg in (reverse optargs-p)
for arg-p = (third arg)
for count from 0
collect `(,arg-p
(optimized-call T ,class-name ,method ,@reqargs ,@(butlast optargs count))))
(T (optimized-call T ,class-name ,method ,@reqargs)))
`(optimized-call T ,class-name ,method ,@reqargs)))
,@(when optargs
`((define-compiler-macro ,name (&whole ,whole ,@reqargs ,@(when optargs `(&optional ,@optargs)))
(declare (ignore ,@reqargs ,@optargs))
`(optimized-call T ,,class-name ,,method ,@(cdr ,whole)))))))))
(defun emit-operator-call (methods instance &rest args)
(let ((method (qmethod-name (first methods)))
(instance-class (target-symbol "%INSTANCE-CLASS"))
(methods (remove-if (lambda (method) (string= (qclass-name (qt::qmethod-class method)) "QGlobalSpace"))
methods)))
(if methods
`(let ((,instance-class (qt::qobject-class ,instance)))
(if (or ,@(loop for method in methods
for class = (qt::qmethod-class method)
collect `(qt:qsubclassp ,instance-class (load-time-value (find-qclass ,(qclass-name class))))))
(optimized-call T ,instance ,method ,@args)
(optimized-call T "QGlobalSpace" ,method ,instance ,@args)))
`(optimized-call T "QGlobalSpace" ,method ,instance ,@args))))
(defun compile-operator (methods)
(let ((name (cl-operator-name (first methods)))
(method (clean-method-name (qmethod-name (first methods))))
(rest (target-symbol "%REST"))
(instance (target-symbol "%INSTANCE"))
(operand (target-symbol "%OPERAND"))
(value (target-symbol "%VALUE")))
(cond ((not name)
NIL)
;; Special case 0 args
((string= method "operator!")
`(define-extern-inline-fun ,name (,instance)
,(generate-method-docstring methods)
,(emit-operator-call methods instance)))
;; Special case n args
;; Not bothering with QGlobalSpace here since it has no () op.
((string= method "operator()")
`(progn
(define-extern-inline-fun ,name (,instance &rest ,rest)
,(generate-method-docstring methods)
(apply #'interpret-call ,instance ,method ,rest))
(define-compiler-macro ,name (,instance &rest ,rest)
`(optimized-call T ,,instance ,,method ,@,rest))))
;; Special case setters
((string= method "operator=")
`(define-extern-macro ,name (,instance ,value)
,(generate-method-docstring methods)
`(setf ,,instance ,(emit-operator-call ',methods ,instance ,value))))
((or (string= method "operator+=")
(string= method "operator-="))
`(define-extern-macro ,name (,instance &optional (,value 1))
,(generate-method-docstring methods)
`(setf ,,instance ,(emit-operator-call ',methods ,instance ,value))))
;; Default case 1 arg
(T
`(define-extern-inline-fun ,name (,instance ,operand)
,(generate-method-docstring methods)
,(emit-operator-call methods instance operand))))))
(defun compile-constructor (methods)
(let ((name (cl-constructor-name (first methods)))
(class (qclass-name (qt::qmethod-class (first methods))))
(whole (target-symbol "%WHOLE")))
(with-args (reqargs optargs optargs-p) methods
`(progn
(define-extern-inline-fun ,name (,@reqargs ,@(when optargs `(&optional ,@optargs-p)))
,(generate-method-docstring methods)
,(if optargs
`(cond
,@(loop for arg in (reverse optargs-p)
for arg-p = (third arg)
for count from 0
collect `(,arg-p
(optimized-new ,class ,@reqargs ,@(butlast optargs count))))
(T (optimized-new ,class ,@reqargs)))
`(optimized-new ,class ,@reqargs)))
,@(when optargs
`((define-compiler-macro ,name (&whole ,whole ,@reqargs ,@(when optargs `(&optional ,@optargs)))
(declare (ignore ,@reqargs ,@optargs))
`(optimized-new ,,class ,@(cdr ,whole)))))))))
(defmacro define-qt-constant (name (class method) &optional documentation)
`(progn
(defvar ,name)
(define-extern-inline-fun ,name ()
(cond ((boundp ',name) (symbol-value ',name))
((find-qclass ,class)
(setf (symbol-value ',name) (enum-value (optimized-call T ,class ,method))))
(T (error ,(format NIL "Cannot fetch enum value ~a::~a. Does the class exist?"
class method)))))
,@(when documentation
`((setf (documentation ',name 'variable) ,documentation)))))
(defun compile-constant (methods)
(let ((method (first methods)))
`(define-qt-constant ,(cl-constant-name method)
(,(qclass-name (qt::qmethod-class method))
,(qmethod-name method))
,(generate-constant-docstring method))))