-
Notifications
You must be signed in to change notification settings - Fork 6
/
xelb-gen
executable file
·727 lines (635 loc) · 28.2 KB
/
xelb-gen
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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
#!/usr/bin/env -S emacs -Q --script
;;; xelb-gen --- XELB Code Generator -*- lexical-binding: t; no-byte-compile: t -*-
;; Copyright (C) 2015-2025 Free Software Foundation, Inc.
;; Author: Chris Feng <chris.w.feng@gmail.com>
;; This file is part of GNU Emacs.
;; GNU Emacs is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;; 'xelb-gen' is responsible for converting XCB XML description files into
;; Elisp libraries. Here are a few design guidelines:
;; + The generated codes should be human-readable and conform to the Elisp
;; coding conventions. Names mentioned in X specifications are preferred.
;; + Deprecated features such as <valueparam> should be dropped, for
;; - they would generate incompatible codes, and
;; - they are probably already dropped upstream.
;; + All documentations (within <doc> tags) and comments should be stripped
;; out to reduce the overall amount of code. XELB application developers are
;; then encouraged to refer to the corresponding specifications to get an
;; authoritative explanation.
;; This file is only intended to be run as a script.
;; References:
;; + xcb/proto (git://anongit.freedesktop.org/xcb/proto)
;;; Code:
(require 'cl-lib)
(require 'eieio)
(require 'pp)
;; Only used to eliminate compile warnings when distributed.
(require 'xcb-types nil t)
;;;; Variables
(defvar xelb-prefix "xcb:" "Namespace of this module.")
(defvar xelb-error-alist nil "Record X errors in this module.")
(defvar xelb-event-alist nil "Record X events in this module.")
(defvar xelb-xge-alist nil "Record X generic events in this module.")
(defvar xelb-imports nil "Record imported libraries.")
(defvar xelb-pad-count -1 "<pad> node counter.")
(defvar xelb-request-fields nil "Fields in the current request.")
(defconst xelb-xproto-namespace "xproto:" "The namespace of the core protocol.")
;;;; Helper functions
(defsubst xelb-node-name (node)
"Return the tag name of node NODE."
(car node))
(defsubst xelb-node-attr (node attr)
"Return the attribute ATTR of node NODE."
(cdr (assoc attr (cadr node))))
(defsubst xelb-resolve-type (name)
"Resolve NAME relative to the current module."
(if (string-prefix-p xelb-xproto-namespace name)
;; Defined explicitly.
(or (intern-soft (concat "xcb:" (substring name (length xelb-xproto-namespace))))
(error "Undefined type: %s" name))
(or
;; defined by this extension
(intern-soft (concat xelb-prefix name))
;; defined by the core protocol
(intern-soft (concat "xcb:" name))
;; Defined by an imported extension.
(cl-dolist (i xelb-imports)
(when-let ((type (intern-soft (concat i name))))
(cl-return type)))
;; Not defined.
(error "Undefined type: %s" name))))
(defsubst xelb-node-type (node)
"Return the type of node NODE."
(xelb-resolve-type (xelb-node-attr node 'type)))
(defsubst xelb-escape-name (name)
"Replace underscores in NAME with dashes."
(replace-regexp-in-string "_" "-" name))
(defsubst xelb-node-name-escape (node)
"Return the tag name of node NODE and escape it."
(xelb-escape-name (xelb-node-name node)))
(defsubst xelb-node-attr-escape (node attr)
"Return the attribute ATTR of node NODE and escape it."
(xelb-escape-name (xelb-node-attr node attr)))
(defsubst xelb-node-subnodes (node &optional mark-auto-padding)
"Return all the subnodes of node NODE as a list.
If MARK-AUTO-PADDING is non-nil, all <list>'s fitting for padding will include
an `xelb-auto-padding' attribute."
(let ((subnodes (cddr node)))
(when mark-auto-padding
;; Remove all <comment>'s and <doc>'s
(setq subnodes
(cl-delete-if (lambda (i) (or (eq 'comment (car i)) (eq 'doc (car i))))
subnodes))
(dotimes (i (1- (length subnodes)))
(when (and (eq 'list (xelb-node-name (elt subnodes i)))
(pcase (xelb-node-name (elt subnodes (1+ i)))
((or `reply `pad))
(_ t)))
(setf (cadr (elt subnodes i))
(nconc (cadr (elt subnodes i)) `((xelb-auto-padding . t)))))))
subnodes))
(defsubst xelb-node-subnode (node)
"Return the (only) subnode of node NODE with useless contents skipped."
(let ((result (xelb-node-subnodes node)))
(catch 'break
(dolist (i result)
(unless (and (listp i)
(or (eq (xelb-node-name i) 'comment)
(eq (xelb-node-name i) 'doc)))
(throw 'break i))))))
(defun xelb-node-size (node)
"Return the size of NODE in bytes."
(pcase (xelb-node-name node)
(`pad (xelb-node-attr node 'bytes))
(`field (xelb-type-size (xelb-node-type node)))
(`list (* (xelb-type-size (xelb-node-type node))
(xelb-parse-expression (xelb-node-subnode node))))
((or `comment `doc) 0)
(x (error "Unexpected element: <%s>" x))))
(defun xelb-type-size (type &optional slot)
"Return size of TYPE in bytes."
(pcase (or (get type 'xcb--typealias) type)
(`xcb:-ignore 0)
((or `xcb:-u1 `xcb:-i1 `xcb:void) 1)
((or `xcb:-u2 `xcb:-i2) 2)
((or `xcb:-u4 `xcb:-i4) 4)
(`xcb:-u8 8)
(`xcb:-pad (cl--slot-descriptor-initform slot))
(`xcb:-list
(let ((initform (cadr (cl--slot-descriptor-initform slot))))
(* (plist-get initform 'size)
(xelb-type-size (plist-get initform 'type)))))
((and x (guard (child-of-class-p x 'xcb:-struct)))
(apply #'+
(mapcar (lambda (slot)
(xelb-type-size (cl--slot-descriptor-type slot) slot))
(eieio-class-slots x))))
(x (error "Unknown size of type: %s" x))))
(defsubst xelb-generate-pad-name ()
"Generate a new slot name for <pad>."
(make-symbol (format "pad~%d" (cl-incf xelb-pad-count))))
;;;; Entry & root element
(defun xelb-parse (file)
"Parse an XCB protocol description file FILE (XML)."
(let ((pp-escape-newlines nil) ;do not escape newlines
(pp-default-function 'pp-28) ;avoid unecessary churn
(indent-tabs-mode nil)
result header)
(with-temp-buffer
(insert-file-contents file)
(setq result (libxml-parse-xml-region (point-min) (point-max)))
(unless (eq 'xcb (xelb-node-name result))
;; There's an extra comment.
(setq result (xelb-node-subnode result)))
(cl-assert (eq 'xcb (xelb-node-name result)))
(setq header (xelb-node-attr result 'header))
(unless (string= header "xproto")
(setq xelb-prefix (concat xelb-prefix header ":")))
;; Print header
(princ (format "\
;;; xcb-%s.el --- X11 %s -*- lexical-binding: t -*-
;; Copyright (C) 2015-2025 Free Software Foundation, Inc.
;; This file is part of GNU Emacs.
;; GNU Emacs is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;; This file was generated by 'xelb-gen' from '%s',
;; which you can retrieve from <git://anongit.freedesktop.org/xcb/proto>.
;;; Code:
\(require 'xcb-types)
"
header
(let ((extension-name (xelb-node-attr result
'extension-name)))
(if extension-name
(concat extension-name " extension")
"core protocol"))
(file-name-nondirectory file)))
;; Print extension info (if any)
(let ((extension-xname (xelb-node-attr result 'extension-xname))
(extension-name (xelb-node-attr result 'extension-name))
(major-version (xelb-node-attr result 'major-version))
(minor-version (xelb-node-attr result 'minor-version)))
(when extension-xname
(pp `(defconst ,(intern (concat xelb-prefix "-extension-xname"))
,extension-xname)))
(when extension-name
(pp `(defconst ,(intern (concat xelb-prefix "-extension-name"))
,extension-name)))
(when major-version
(pp `(defconst ,(intern (concat xelb-prefix "-major-version"))
,(string-to-number major-version))))
(when minor-version
(pp `(defconst ,(intern (concat xelb-prefix "-minor-version"))
,(string-to-number minor-version))))
(when (or extension-xname extension-name major-version minor-version)
(princ "\n")))
;; Print contents
(dolist (i (xelb-node-subnodes result))
(let ((result (xelb-parse-top-level-element i)))
(when result ;skip <doc>, comments, etc
(dolist (j result)
(eval j) ;Make it immediately available.
(pp j))
(princ "\n"))))
;; Print error/event alists
(when xelb-error-alist
(pp
`(defconst ,(intern (concat xelb-prefix "error-number-class-alist"))
',xelb-error-alist "(error-number . error-class) alist."))
(princ "\n"))
(when xelb-event-alist
(pp
`(defconst ,(intern (concat xelb-prefix "event-number-class-alist"))
',xelb-event-alist "(event-number . event-class) alist."))
(princ "\n"))
(when xelb-xge-alist
(pp
`(defconst ,(intern (concat xelb-prefix "xge-number-class-alist"))
',xelb-xge-alist "(xge-number . event-class) alist."))
(princ "\n"))
;; Print footer
(princ (format "\
(provide 'xcb-%s)
;;; xcb-%s.el ends here
" header header)))))
;;;; XCB: top-level elements
(defun xelb-parse-top-level-element (node)
"Parse a top-level node NODE."
(setq xelb-pad-count -1)
(pcase (xelb-node-name node)
(`import (xelb-parse-import node))
(`struct (xelb-parse-struct node))
(`union (xelb-parse-union node))
((or `xidtype `xidunion)
(xelb-parse-xidtype node)) ;they are basically the same
(`enum (xelb-parse-enum node))
(`typedef (xelb-parse-typedef node))
(`request (xelb-parse-request node))
(`event (xelb-parse-event node))
(`error (xelb-parse-error node))
(`eventcopy (xelb-parse-eventcopy node))
(`errorcopy (xelb-parse-errorcopy node))
(`eventstruct (xelb-parse-eventstruct node))
((or `comment `doc)) ;ignored
(x (error "Unsupported top-level element: <%s>" x))))
(defun xelb-parse-import (node)
"Parse <import>."
(let* ((name (xelb-node-subnode node))
(header (intern (concat "xcb-" name))))
(require header)
(push (concat "xcb:" name ":") xelb-imports)
`((require ',header))))
(defun xelb-parse-struct (node)
"Parse <struct>."
(let ((name (intern (concat xelb-prefix (xelb-node-attr node 'name))))
(contents (xelb-node-subnodes node t)))
`((defclass ,name (xcb:-struct)
,(apply #'nconc (mapcar #'xelb-parse-structure-content contents))))))
(defun xelb-parse-union (node)
"Parse <union>."
(let ((name (intern (concat xelb-prefix (xelb-node-attr node 'name))))
(contents (xelb-node-subnodes node)))
`((defclass ,name (xcb:-union)
,(apply #'nconc
`((~size :initform
,(apply #'max (mapcar #'xelb-node-size contents))))
(mapcar #'xelb-parse-structure-content contents))))))
(defun xelb-parse-xidtype (node)
"Parse <xidtype>."
(let ((name (intern (concat xelb-prefix (xelb-node-attr node 'name)))))
`((xcb:deftypealias ',name 'xcb:-u4))))
(defun xelb-parse-enum (node)
"Parse <enum>."
(let ((name-prefix (concat xelb-prefix (xelb-node-attr node 'name) ":"))
(items (xelb-node-subnodes node))
(value 0))
(delq nil ;remove nil's produced by tags like <doc>
(mapcar (lambda (i)
(when (eq (xelb-node-name i) 'item)
;; Only handle <item> tags
(let* ((name (xelb-node-attr i 'name))
(name (intern (concat name-prefix name)))
(expression (xelb-node-subnode i)))
(if expression
(setq value (xelb-parse-expression expression))
(cl-incf value))
;; Omit the rare enums that do not fit in a fixnum in
;; 32-bit Emacs, so that the resulting .el and .elc
;; files are portable to 32-bit Emacs. Admittedly
;; this is a kludge.
(unless (and (integerp value)
(not (<= -536870912 value 536870911)))
`(defconst ,name ,value)))))
items))))
(defun xelb-parse-typedef (node)
"Parse <typedef>."
(let* ((oldname (xelb-node-attr node 'oldname))
(oldname (xelb-resolve-type oldname))
(newname (intern (concat xelb-prefix
(xelb-node-attr node 'newname)))))
`((xcb:deftypealias ',newname ',oldname))))
(defun xelb-parse-request (node)
"Parse <request>.
The `combine-adjacent' attribute is simply ignored."
(let* ((name (intern (concat xelb-prefix (xelb-node-attr node 'name))))
(opcode (string-to-number (xelb-node-attr node 'opcode)))
(contents `((~opcode :initform ,opcode :type xcb:-u1)))
(subnodes (xelb-node-subnodes node t))
expressions
result reply-name reply-contents)
;; Fill `xelb-request-fields'.
(setq xelb-request-fields nil)
(dolist (i subnodes)
(unless (eq (xelb-node-name i) 'reply)
(let ((name (xelb-node-attr i 'name)))
(when name
(push (intern (xelb-escape-name name)) xelb-request-fields)))))
(dolist (i subnodes)
(if (not (eq (xelb-node-name i) 'reply))
(progn
(setq result (xelb-parse-structure-content i))
(if (eq 'exprfield (xelb-node-name i))
;; Split into field and expression
(setq contents (nconc contents (list (car result)))
expressions (nconc expressions (list (cadr result))))
(setq contents (nconc contents result))))
;; Parse <reply>
(setq xelb-pad-count -1) ;reset padding counter
(setq xelb-request-fields nil) ;Clear `xelb-request-fields'.
(setq reply-name
(intern (concat xelb-prefix (xelb-node-attr node 'name)
"~reply")))
(setq reply-contents (xelb-node-subnodes i t))
(setq reply-contents
(apply #'nconc
(mapcar #'xelb-parse-structure-content reply-contents)))))
(setq xelb-request-fields nil) ;Clear `xelb-request-fields'.
(delq nil contents)
(delq nil
`((defclass ,name (xcb:-request) ,contents)
;; The optional expressions
,(when expressions
`(cl-defmethod xcb:marshal ((obj ,name)) nil
,@expressions
(cl-call-next-method obj)))
;; The optional reply body
,(when reply-name
(delq nil reply-contents)
;; Insert slots for sequence number and reply length.
(setcdr reply-contents (append '((~sequence :type xcb:CARD16)
(length :type xcb:CARD32))
(cdr reply-contents)))
`(defclass ,reply-name (xcb:-reply) ,reply-contents))))))
(defun xelb-parse-event (node)
"Parse <event>."
(let ((name (intern (concat xelb-prefix (xelb-node-attr node 'name))))
(event-number (string-to-number (xelb-node-attr node 'number)))
(no-sequence-number (xelb-node-attr node 'no-sequence-number))
(xge (xelb-node-attr node 'xge))
(contents (xelb-node-subnodes node t))
xge-extension)
(setq xge-extension (and xge (not (eq name 'xcb:GeGeneric))))
(setq contents
(apply #'nconc (mapcar #'xelb-parse-structure-content contents)))
(unless (or no-sequence-number xge)
(setcdr contents (append '((~sequence :type xcb:CARD16))
(cdr contents))))
;; Add the event code.
(unless (and xge (not xge-extension))
(push `(,(if xge '~evtype '~code) :initform ,event-number) contents))
(if xge-extension
(setq xelb-xge-alist
(nconc xelb-xge-alist `((,event-number . ,name))))
(setq xelb-event-alist
(nconc xelb-event-alist `((,event-number . ,name)))))
`((defclass ,name (,(if xge 'xcb:-generic-event 'xcb:-event)) ,contents))))
(defun xelb-parse-error (node)
"Parse <error>."
(let ((name (intern (concat xelb-prefix (xelb-node-attr node 'name))))
(error-number (string-to-number (xelb-node-attr node 'number)))
(contents (xelb-node-subnodes node t)))
(setq xelb-error-alist (nconc xelb-error-alist `((,error-number . ,name))))
`((defclass ,name (xcb:-error)
,(append
;; The error code.
`((~code :initform ,error-number))
;; The contents.
(apply #'nconc (mapcar #'xelb-parse-structure-content contents)))))))
(defun xelb-parse-eventcopy (node)
"Parse <eventcopy>."
(let* ((name (intern (concat xelb-prefix (xelb-node-attr node 'name))))
(refname (xelb-node-attr node 'ref))
(refname (xelb-resolve-type refname))
(xge (child-of-class-p refname 'xcb:-generic-event))
(event-number (string-to-number (xelb-node-attr node 'number))))
(if xge
(setq xelb-xge-alist
(nconc xelb-xge-alist `((,event-number . ,name))))
(setq xelb-event-alist
(nconc xelb-event-alist `((,event-number . ,name)))))
`((defclass ,name (,refname) ;Shadow the method of ref.
((,(if xge '~evtype '~code) :initform ,event-number))))))
(defun xelb-parse-errorcopy (node)
"Parse <errorcopy>."
(let* ((name (intern (concat xelb-prefix (xelb-node-attr node 'name))))
(refname (xelb-node-attr node 'ref))
(refname (xelb-resolve-type refname))
(error-number (string-to-number (xelb-node-attr node 'number))))
(setq xelb-error-alist (nconc xelb-error-alist `((,error-number . ,name))))
`((defclass ,name (xcb:-error ,refname) ;Shadow the method of ref
((~code :initform ,error-number))))))
(defun xelb-parse-eventstruct (node)
"Parse <eventstruct>."
(let ((name (intern (concat xelb-prefix (xelb-node-attr node 'name)))))
;; Only conventional events are supported (and we don't check opcode).
`((defclass ,name (xcb:-event) nil))))
;;;; XCB: structure contents
(defun xelb-parse-structure-content (node)
"Parse a structure content node NODE."
(pcase (xelb-node-name node)
(`pad (xelb-parse-pad node))
(`required_start_align (xelb-parse-required_start_align node))
(`field (xelb-parse-field node))
(`length (xelb-parse-length node))
(`fd (xelb-parse-fd node))
(`list (xelb-parse-list node))
(`exprfield (xelb-parse-exprfield node))
(`switch (xelb-parse-switch node))
((or `comment `doc)) ;simply ignored
(x (error "Unsupported structure content: <%s>" x))))
;; The car of the result shall be renamed to prevent duplication of slot names
(defun xelb-parse-pad (node)
"Parse <pad>."
(let ((bytes (xelb-node-attr node 'bytes))
(align (xelb-node-attr node 'align)))
(if bytes
`((,(xelb-generate-pad-name)
:initform ,(string-to-number bytes) :type xcb:-pad))
(if align
`((,(xelb-generate-pad-name)
:initform ,(string-to-number align) :type xcb:-pad-align))
(error "Invalid <pad> field")))))
(defun xelb-parse-required_start_align (node)
"Parse <required_start_align>."
(let ((align (xelb-node-attr node 'align))
(offset (xelb-node-attr node 'offset)))
`((,(xelb-generate-pad-name)
:initform ,(if offset
(vector (string-to-number align)
(string-to-number offset))
(string-to-number align))
:type xcb:-pad-align))))
(defun xelb-parse-field (node)
"Parse <field>."
(let* ((name (intern (xelb-node-attr-escape node 'name)))
(type (xelb-node-type node)))
`((,name :initarg ,(intern (concat ":" (symbol-name name))) :type ,type))))
(defun xelb-parse-length (node)
"Parse <length>."
(let ((length (xelb-parse-expression (xelb-node-subnode node))))
`((~size :initform ',length))))
(defun xelb-parse-fd (node)
"Parse <fd>."
(let ((name (intern (xelb-node-attr-escape node 'name))))
`((,name :type xcb:fd))))
(defun xelb-parse-list (node)
"Parse <list>."
(let* ((name (intern (xelb-node-attr-escape node 'name)))
(name-alt (intern (concat (xelb-node-attr-escape node 'name) "~")))
(type (xelb-node-type node))
(size (xelb-parse-expression (xelb-node-subnode node))))
`((,name-alt :initform '(name ,name type ,type size ,size)
:type xcb:-list)
(,name :initarg ,(intern (concat ":" (symbol-name name)))
:type xcb:-ignore))))
;; The car of result is the field declaration, and the cadr is the expression
;; to be evaluated.
(defun xelb-parse-exprfield (node)
"Parse <exprfield>."
(let* ((name (intern (xelb-node-attr-escape node 'name)))
(type (xelb-node-type node))
(value (xelb-parse-expression (xelb-node-subnode node))))
`((,name :type ,type)
(setf (slot-value obj ',name) ,value))))
;; The only difference between <bitcase> and <case> is whether the `condition'
;; is a list
;; The name attribute of <bitcase> and <case> seems not useful here.
(defun xelb-parse-switch (node)
"Parse <switch>."
(let ((name (intern (xelb-node-attr-escape node 'name)))
(expression (xelb-parse-expression (car (xelb-node-subnodes node))))
;; <case> and <bitcase> only
(cases (cl-remove-if-not (lambda (i)
(memq (xelb-node-name i) '(case bitcase)))
(xelb-node-subnodes node)))
fields)
;; Avoid duplicated slot names by appending "*" if necessary
(let (names name)
(dolist (case cases)
(pcase (xelb-node-name case)
((or `bitcase `case)
(dolist (field (xelb-node-subnodes case))
(pcase (xelb-node-name field)
((or `enumref `pad `doc `comment `required_start_align))
(_
(setq name (xelb-node-attr field 'name))
(when (member name names)
(while (member name names)
(setq name (concat name "*")))
(setcdr (assoc 'name (cadr field)) name))
(cl-pushnew name names :test #'equal))))))))
(setq cases
(mapcar (lambda (i)
(let ((case-name (xelb-node-name i))
condition name-list tmp)
(when (or (eq case-name 'bitcase) (eq case-name 'case))
(dolist (j (xelb-node-subnodes i t))
(pcase (xelb-node-name j)
(`enumref
(setq condition
(nconc condition
(list (xelb-parse-enumref j)))))
(_
(setq tmp (xelb-parse-structure-content j))
(setq fields (nconc fields tmp))
(setq name-list
(nconc name-list (list (caar tmp)))))))
(when (eq case-name 'bitcase)
(setq condition (if (= 1 (length condition))
;; Flatten 1-element list.
(car condition)
(if (cl-every #'integerp condition)
(apply #'logior condition)
`(logior ,@condition))))))
`(,condition ,@name-list)))
cases))
`((,name :initform '(expression ,expression cases ,cases)
:type xcb:-switch)
,@fields)))
;;;; XCB: expressions
(defun xelb-parse-expression (node)
"Parse an expression node NODE."
(when node
(pcase (xelb-node-name node)
(`op (xelb-parse-op node))
(`fieldref (xelb-parse-fieldref node))
(`paramref (xelb-parse-paramref node))
(`value (xelb-parse-value node))
(`bit (xelb-parse-bit node))
(`enumref (xelb-parse-enumref node))
(`unop (xelb-parse-unop node))
(`sumof (xelb-parse-sumof node))
(`popcount (xelb-parse-popcount node))
(`listelement-ref (xelb-parse-listelement-ref node))
((or `comment `doc)) ;simply ignored
(x (error "Unsupported expression: <%s>" x)))))
(defun xelb-parse-op (node)
"Parse <op>."
(let* ((subnodes (xelb-node-subnodes node))
(x (xelb-parse-expression (car subnodes)))
(y (xelb-parse-expression (cadr subnodes))))
(pcase (xelb-node-attr node 'op)
("+" `(+ ,x ,y))
("-" `(- ,x ,y))
("*" `(* ,x ,y))
("/" `(/ ,x ,y))
("&" `(logand ,x ,y))
("<<" `(ash ,x ,y))
(x (error "Unsupported operator: `%s'" x)))))
(defun xelb-parse-fieldref (node)
"Parse <fieldref>."
(let ((name (intern (xelb-escape-name (xelb-node-subnode node)))))
(if (or (not xelb-request-fields) ;Probably not a request.
(memq name xelb-request-fields)
(not (string-suffix-p "-len" (symbol-name name))))
`(xcb:-fieldref ',name)
`(length
(xcb:-fieldref ',(intern (substring (symbol-name name) 0 -4)))))))
(defun xelb-parse-paramref (node)
"Parse <paramref>."
`(xcb:-paramref ',(intern (xelb-escape-name (xelb-node-subnode node)))))
(defun xelb-parse-value (node)
"Parse <value>."
(string-to-number
(replace-regexp-in-string "^0x" "#x" (xelb-node-subnode node))))
(defun xelb-parse-bit (node)
"Parse <bit>."
(let ((bit (string-to-number (xelb-node-subnode node))))
(cl-assert (<= 0 bit 31))
(ash 1 bit)))
(defun xelb-parse-enumref (node)
"Parse <enumref>."
(let ((name (concat (xelb-node-attr node 'ref) ":"
(xelb-node-subnode node))))
(symbol-value (xelb-resolve-type name))))
(defun xelb-parse-unop (node)
"Parse <unop>."
(cl-assert (string= "~" (xelb-node-attr node 'op)))
`(lognot ,(xelb-parse-expression (xelb-node-subnode node))))
(defun xelb-parse-sumof (node)
"Parse <sumof>."
(let* ((ref (intern (xelb-node-attr-escape node 'ref)))
(expression (xelb-node-subnode node))
(list-data `(slot-value obj ',ref)))
(if (not expression)
`(apply #'+ ,list-data)
(setq expression (xelb-parse-expression expression))
`(apply #'+ (mapcar (lambda (i)
(eval ',expression (list (nconc '(obj) i))))
,list-data)))))
(defun xelb-parse-popcount (node)
"Parse <popcount>."
(let ((expression (xelb-parse-expression (xelb-node-subnode node))))
`(logcount ,expression)))
(defun xelb-parse-listelement-ref (_node)
"Parse <listelement-ref>."
'obj) ;a list element is internally named 'obj'
;;;; The entry
(setq debug-on-error t)
(setq edebug-all-forms t)
(if (not argv)
(error "Usage: xelb-gen <protocol.xml> [additional_load_paths]")
(add-to-list 'load-path default-directory)
(dolist (i (cdr argv))
(add-to-list 'load-path i))
(require 'xcb-types)
(xelb-parse (car argv)))
;;; xelb-gen ends here