-
Notifications
You must be signed in to change notification settings - Fork 6
/
preprocessor.lisp
1560 lines (1430 loc) · 71.9 KB
/
preprocessor.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
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
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
(in-package #:with-c-syntax.core)
(defun find-asdf-system-relative-file (base name)
"See `find-with-c-syntax-default-include-file'"
(let* ((name-list (if (listp name)
name
(split-sequence:split-sequence #\/ (string name))))
(component (or (asdf:find-component base name-list)
(asdf:find-component base (mapcar 'string-downcase name-list)))))
(if component
(asdf:component-pathname component))))
(defun find-with-c-syntax-default-include-file (name)
"Makes a pathname of the directory containing with-c-syntax include
files. This function is used for making the default value of
`*with-c-syntax-find-include-file-function-list*'"
(find-asdf-system-relative-file '(:with-c-syntax . "include") name))
(defvar *with-c-syntax-find-include-file-function-list*
(list 'find-with-c-syntax-default-include-file)
"List of functions used for searching include files with '#include <...>' style.
Each function should take one argument and returns a pathname, or nil if failed.
See `find-include-<header>-file'.")
;;; Preprocessor macro expansion
(defun va-args-identifier-p (token)
(token-equal-p token :__VA_ARGS__))
(defclass macro-definition ()
((name :initarg :name :reader macro-definition-name)
(replacement-list :initarg :replacement-list
:reader macro-definition-replacement-list)))
(defmethod cl:initialize-instance :before ((macro-definition macro-definition)
&rest args &key name)
(declare (ignorable args))
(when (va-args-identifier-p name)
(error 'preprocess-error
:format-control "Macro name '__VA_ARGS__' is not allowed as an user defined macro.")))
(defmethod cl:initialize-instance :after ((macro-definition macro-definition) &rest args)
(declare (ignorable args))
(with-slots (replacement-list) macro-definition
(when (eq (first replacement-list) +whitespace-marker+)
(pop replacement-list))
(when (eq (lastcar replacement-list) +whitespace-marker+)
(setf replacement-list (butlast replacement-list)))))
(defgeneric check-valid-macro-redefinition (macro-definition1 macro-definition2 name)
(:method (mdef1 mdef2 name)
(error 'preprocess-error
:format-control "Macro '~A' redefinition from ~A type to ~A type is invalid."
:format-arguments (list name (type-of mdef1) (type-of mdef2)))))
(defgeneric check-replacement-list-equal (mdef1 mdef2 name)
(:method ((mdef1 macro-definition) (mdef2 macro-definition) name)
(let ((r-list-1 (macro-definition-replacement-list mdef1))
(r-list-2 (macro-definition-replacement-list mdef2)))
(unless (replacement-list-equal r-list-1 r-list-2)
(error 'preprocess-error
:format-control "Macro '~A' redefinition is invalid because replacement-lists are different.~%Old: ~A~%New: ~A"
:format-arguments (list name r-list-1 r-list-2))))))
(defclass object-like-macro (macro-definition)
())
(defmethod cl:print-object ((macro-definition object-like-macro) stream)
(print-unreadable-object (macro-definition stream :type t :identity t)
(princ (macro-definition-name macro-definition) stream)))
(defmethod cl:initialize-instance :before ((macro-definition object-like-macro)
&rest args &key replacement-list)
(declare (ignorable args))
(when (some #'va-args-identifier-p replacement-list)
(error 'preprocess-error
:format-control "Object-like-macro cannot have '__VA_ARGS__' in its replacement-list.")))
(defun replacement-list-equal (list1 list2)
(tree-equal list1 list2
:test
(lambda (x y)
(if (and (preprocessing-number-p x) (preprocessing-number-p y))
(equal (preprocessing-number-string x) (preprocessing-number-string y))
(equal x y)))))
(defmethod check-valid-macro-redefinition ((mdef1 object-like-macro) (mdef2 object-like-macro) name)
(check-replacement-list-equal mdef1 mdef2 name))
(defclass function-like-macro (macro-definition)
((identifier-list :initarg :identifier-list
:reader function-like-macro-identifier-list)
(variadicp :initarg :variadicp
:reader function-like-macro-variadicp)))
(defmethod cl:print-object ((macro-definition function-like-macro) stream)
(print-unreadable-object (macro-definition stream :type t :identity t)
(format stream "~A~:A"
(macro-definition-name macro-definition)
(function-like-macro-identifier-list macro-definition))))
(defmethod cl:initialize-instance :before ((macro-definition function-like-macro)
&rest args &key identifier-list variadicp replacement-list)
(declare (ignorable args))
(when (some #'va-args-identifier-p identifier-list)
(error 'preprocess-error
:format-control "Function-like-macro cannot have '__VA_ARGS__' in its identifier-list."))
(unless (length= identifier-list (remove-duplicates identifier-list))
(error 'preprocess-error
:format-control "Function-like-macro has duplicated parameter names."))
(when (and (not variadicp)
(some #'va-args-identifier-p replacement-list))
(error 'preprocess-error
:format-control "Non-variadic Function-like-macro cannot have '__VA_ARGS__' in its replacement-list.")))
(defmethod check-valid-macro-redefinition ((mdef1 function-like-macro) (mdef2 function-like-macro) name)
(check-replacement-list-equal mdef1 mdef2 name)
(let ((i-list-1 (function-like-macro-identifier-list mdef1))
(i-list-2 (function-like-macro-identifier-list mdef2)))
(unless (equal i-list-1 i-list-2)
(error 'preprocess-error
:format-control "Macro '~A' redefinition is invalid because identifier-lists are different.~%Old: ~A~%New: ~A"
:format-arguments (list name i-list-1 i-list-2))))
(let ((variadicp-1 (function-like-macro-variadicp mdef1))
(variadicp-2 (function-like-macro-variadicp mdef2)))
(unless (equal variadicp-1 variadicp-2)
(error 'preprocess-error
:format-control "Macro '~A' redefinition is invalid because variance are different.~%Old: ~A~%New: ~A"
:format-arguments (list name variadicp-1 variadicp-2)))))
(defun preprocessor-macro-exists-p (macro-alist symbol)
(or (find-symbol (symbol-name symbol) '#:with-c-syntax.predefined-macro)
(when-let ((entry (assoc symbol macro-alist)))
(not (eql (cdr entry) :macro-suppressed)))))
(defun whitespace-like-token-p (token)
(or (eq token +whitespace-marker+)
(eq token +newline-marker+)))
(defun delete-consecutive-whitespace-marker (token-list)
(loop for cons = token-list then next
for next = (cond
((and (whitespace-like-token-p (first cons))
(whitespace-like-token-p (second cons)))
(setf (cdr cons) (cddr cons))
cons)
(t
(cdr cons)))
while cons)
token-list)
(define-condition incompleted-macro-arguments-error (preprocess-error)
((token-list :initarg :token-list))
(:report
(lambda (condition stream)
(format stream "preprocessor macro argument is incompleted. tokens: ~S"
(slot-value condition 'token-list)))))
(defstruct pp-macro-argument
(token-list) ; Still holds `:end-of-preprocessor-macro-scope'.
(token-list-expansion nil)
(macro-alist)) ; The context for macro expansion.
(defun macro-scoping-marker-p (token)
(or (eql token :end-of-preprocessor-macro-scope)
(typep token 'macro-definition)))
(defun macro-control-marker-p (token)
(or (whitespace-like-token-p token)
(macro-scoping-marker-p token)))
(defun collect-one-macro-argument (token-list macro-alist)
(loop
with macro-alist-result = macro-alist
with nest-level of-type fixnum = 0
for token-cons on token-list
as token = (car token-cons)
if (typep token 'macro-definition)
do (push (cons (macro-definition-name token) :macro-suppressed)
macro-alist-result)
else if (eq token :end-of-preprocessor-macro-scope)
do (pop macro-alist-result)
else
do (flet ((return-this-arg ()
(return-from collect-one-macro-argument
(values (make-pp-macro-argument :token-list arg-tokens
:macro-alist macro-alist)
(cdr token-cons)
macro-alist-result
token))))
(switch (token :test 'token-equal-p)
("("
(incf nest-level))
(","
(when (zerop nest-level)
(return-this-arg)))
(")"
(cond ((zerop nest-level)
(return-this-arg))
(t
(decf nest-level))))))
and collect token into arg-tokens
finally
(error 'incompleted-macro-arguments-error :token-list token-list)))
(defun collect-preprocessor-macro-arguments (token-list macro-alist)
(let ((args-start
(loop for token-cons on token-list
as token = (car token-cons)
if (whitespace-like-token-p token)
do (progn)
else if (typep token 'macro-definition)
do (push (cons (macro-definition-name token) :macro-suppressed)
macro-alist)
else if (eql token :end-of-preprocessor-macro-scope)
do (pop macro-alist)
else
do (assert (token-equal-p token "(") ()
'preprocess-error
:format-control "This is a BUG of function-like-macro-invocation-p.")
and return (cdr token-cons))))
(loop
with token-cons = args-start
while token-cons
for (pp-macro-arg rest-token-list new-macro-alist separator-token)
= (multiple-value-list
(collect-one-macro-argument token-cons macro-alist))
collect pp-macro-arg into macro-arg-results
do (setf token-cons rest-token-list
macro-alist new-macro-alist)
(switch (separator-token :test 'token-equal-p)
(","
(progn))
(")"
(return (values macro-arg-results token-cons macro-alist))))
finally
(error 'incompleted-macro-arguments-error :token-list token-list))))
(defun expand-each-preprocessor-macro-in-list (token-list macro-alist pp-state)
"Used by `expand-macro-argument', #line and #include."
(loop for token-cons on token-list ; Do not use `pop-preprocessor-directive-token' for preserving `+whitespace-marker+'.
as token = (first token-cons)
if (and (symbolp token)
(preprocessor-macro-exists-p macro-alist token))
append (multiple-value-bind (expansion rest-tokens new-macro-alist)
(expand-preprocessor-macro token (rest token-cons) macro-alist pp-state)
(setf token-cons (list* :loop-on-clause-dummy rest-tokens)
macro-alist new-macro-alist)
expansion)
else if (typep token 'macro-definition)
do (push (cons (macro-definition-name token) :macro-suppressed)
macro-alist)
else if (eql token :end-of-preprocessor-macro-scope)
do (pop macro-alist)
else
collect token))
(defun expand-macro-argument (pp-macro-argument pp-state)
(let ((token-list (pp-macro-argument-token-list pp-macro-argument))
(macro-alist (pp-macro-argument-macro-alist pp-macro-argument)))
(let ((all-expansions
(expand-each-preprocessor-macro-in-list token-list macro-alist pp-state)))
(setf all-expansions (delete-consecutive-whitespace-marker all-expansions)
(pp-macro-argument-token-list-expansion pp-macro-argument) all-expansions)
all-expansions)))
(defun make-macro-arguments-alist (macro-definition macro-arg-list)
"Makes an alist of (<symbol> . <macro-argument>) for function-like-macro expansion."
(loop
with macro-identifier-list = (function-like-macro-identifier-list macro-definition)
for marg-cons on macro-arg-list
and identifier-cons on macro-identifier-list
collect (cons (first identifier-cons) (first marg-cons))
into result-alist
finally
(cond
((assert (not (and marg-cons identifier-cons))))
((and (endp marg-cons) identifier-cons)
(error 'preprocess-error
:format-control "Insufficient arguments for macro '~A'"
:format-arguments (list (macro-definition-name macro-definition))))
((and marg-cons (endp identifier-cons))
(cond
((and (length= 0 macro-identifier-list)
(length= 1 macro-arg-list)
(length= 0 (pp-macro-argument-token-list (first macro-arg-list)))
(not (function-like-macro-variadicp macro-definition)))
;; Macro invocation like 'X()' may mean one of followings:
;; - Passing no arguments to X.
;; - Passing one argument consists of empty tokens.
;; This check looks where the first situation or not.
;;
;; See 'q()' macro usage of the Example3, in ISO/IEC 9899:1999
;; page 156, '6.1.3.5 Scope of macro definitions'.
(push (cons (first macro-identifier-list)
(make-pp-macro-argument :token-list nil
:token-list-expansion nil
:macro-alist nil))
result-alist))
((not (function-like-macro-variadicp macro-definition))
(error 'preprocess-error
:format-control "Too many arguments for macro '~A'"
:format-arguments (list (macro-definition-name macro-definition))))
(t
(loop with comma-token = 'with-c-syntax.punctuator:|,|
for at-first = t then nil
for marg in marg-cons
as last-macro-alist = (pp-macro-argument-macro-alist marg)
unless at-first ; Insert comma between each args.
collect comma-token into tokens
and collect comma-token into expansions
append (pp-macro-argument-token-list marg) into tokens
append (pp-macro-argument-token-list-expansion marg) into expansions
finally
(push (cons (intern (string :__VA_ARGS__))
(make-pp-macro-argument :token-list tokens
:token-list-expansion expansions
:macro-alist last-macro-alist))
result-alist)))))
(t ; (and (endp marg-cons) (endp identifier-cons))
(when (function-like-macro-variadicp macro-definition)
(warn 'with-c-syntax-style-warning
:message "Variadic macro does not have any arguments. __VA_ARGS__ bound to nil.")
(push (cons (intern (string :__VA_ARGS__))
(make-pp-macro-argument :token-list nil
:token-list-expansion nil
:macro-alist nil))
result-alist))))
(return result-alist)))
(defgeneric stringification-separator-required-p (prev-token)
(:method ((prev-token (eql +whitespace-marker+)))
nil)
(:method ((prev-token (eql +newline-marker+)))
nil)
(:method ((prev-token string))
nil)
(:method ((prev-token character))
nil)
(:method ((prev-token preprocessing-number))
t)
(:method ((prev-token symbol))
(not (find-punctuator (symbol-name prev-token) :no-warn))))
(defun expand-stringify-operator (token macro-arg-alist)
(unless (symbolp token)
(error 'preprocess-error
:format-control "# operator is used to bad argument: '~A'"
:format-arguments (list token)))
(unless (assoc token macro-arg-alist)
(error 'preprocess-error
:format-control "# operator argument '~A' is not a macro parameter."
:format-arguments (list token)))
(with-output-to-string (out)
(loop with marg = (cdr (assoc token macro-arg-alist))
for prev = +whitespace-marker+ then i
for i in (pp-macro-argument-token-list marg)
do (cond
((macro-scoping-marker-p i)
(setf i prev)) ; Keeps prev.
((whitespace-like-token-p i)
(unless (whitespace-like-token-p prev)
(write-char #\space out)))
((preprocessing-number-p i)
(when (stringification-separator-required-p prev)
(write-char #\space out))
(princ (preprocessing-number-string i) out))
((characterp i)
(format out "'~C'" i))
((stringp i)
(princ "\"" out)
(loop for c across i
if (or (char= c #\") (char= c #\\))
do (write-char #\\ out) ; escape double-quote and backslash
do (write-char c out))
(princ "\"" out))
((symbolp i)
(assert (not (macro-control-marker-p i)))
(when (and (not (find-punctuator (symbol-name i) :no-warn))
(stringification-separator-required-p prev))
(write-char #\space out))
(princ i out))
(t
(assert (not (macro-control-marker-p i)))
(princ i out))))))
(defconstant +placemaker-token+ '||)
(defgeneric concatenate-token (token1 token2)
(:documentation "Perform token concatenation caused by '##' operator.")
(:method (token1 token2)
(error 'preprocess-error
:format-control "Token concatenation '~A' and '~A' is not supported."
:format-arguments (list token1 token2))))
(defmethod concatenate-token ((token1 (eql +placemaker-token+)) token2)
token2)
(defmethod concatenate-token (token1 (token2 (eql +placemaker-token+)))
token1)
(defmethod concatenate-token ((token1 symbol) (token2 symbol))
(cond
((eq token1 +placemaker-token+) token2)
((eq token2 +placemaker-token+) token1)
(t
(unless (eql (symbol-package token1) (symbol-package token2))
(warn 'with-c-syntax-style-warning
:message (format nil "## operand '~A' and '~A' belong different packages. The package of '~A' is used."
token1 token2 token1)))
(intern (concatenate 'string (symbol-name token1) (symbol-name token2))))))
(defun stringify-conced-integer (int)
(with-standard-io-syntax
(princ-to-string int)))
(defmethod concatenate-token ((token1 symbol) (token2 integer))
(intern (concatenate 'string (symbol-name token1)
(stringify-conced-integer token2))))
(defmethod concatenate-token ((token1 integer) (token2 symbol))
(intern (concatenate 'string
(stringify-conced-integer token1)
(symbol-name token2))))
(defun intern-conc-pp-number (str1 str2 &optional (package *package*))
(let* ((conc (concatenate 'string str1 str2)))
(handler-case
(read-preprocessing-number-from-string conc)
(with-c-syntax-reader-error ()
(intern conc package)))))
(defmethod concatenate-token ((token1 integer) (token2 integer))
;; Firstly I thought this can implement with numeric operatons, but
;; how to treat '10 ## -10' ?
(intern-conc-pp-number (stringify-conced-integer token1)
(stringify-conced-integer token2)))
(defmethod concatenate-token ((token1 preprocessing-number) (token2 preprocessing-number))
(intern-conc-pp-number (preprocessing-number-string token1)
(preprocessing-number-string token2)))
(defmethod concatenate-token ((token1 preprocessing-number) (token2 symbol))
(intern-conc-pp-number (preprocessing-number-string token1)
(symbol-name token2)))
(defmethod concatenate-token ((token1 symbol) (token2 preprocessing-number))
(intern-conc-pp-number (symbol-name token1)
(preprocessing-number-string token2)))
(defmethod concatenate-token ((token1 preprocessing-number) (token2 integer))
(intern-conc-pp-number (preprocessing-number-string token1)
(stringify-conced-integer token2)))
(defmethod concatenate-token ((token1 integer) (token2 preprocessing-number))
(intern-conc-pp-number (stringify-conced-integer token1)
(preprocessing-number-string token2)))
(defun split-concatenate-operator-left-operand (token-list)
(let ((last-token-pos
(position-if-not #'macro-control-marker-p token-list :from-end t)))
(if last-token-pos
(multiple-value-bind (head rest)
(split-list-at last-token-pos token-list)
(values head
(first rest)
(remove-whitespace-marker (rest rest))))
(values (copy-list token-list)
+placemaker-token+
nil))))
(defun split-concatenate-operator-right-operand (token-list)
(let ((first-token-pos
(position-if-not #'macro-control-marker-p token-list)))
(if first-token-pos
(multiple-value-bind (head rest)
(split-list-at first-token-pos token-list)
(values (delete-whitespace-marker head)
(first rest)
(copy-list (rest rest))))
(values nil
+placemaker-token+
(copy-list token-list)))))
(defun expand-concatenate-operator (token1-list token2-list)
(multiple-value-bind (t1-before t1-token t1-after)
(split-concatenate-operator-left-operand token1-list)
(multiple-value-bind (t2-before t2-token t2-after)
(split-concatenate-operator-right-operand token2-list)
(values
(nconc t1-before t1-after)
(concatenate-token t1-token t2-token)
(nconc t2-before t2-after)))))
(defun expand-macro-replacement-list (replacement-list macro-arg-alist)
(loop
with rev-expansion = nil
for token-cons on replacement-list
as token = (first token-cons)
as next-token-cons = (if (whitespace-like-token-p (second token-cons))
(cddr token-cons)
(cdr token-cons))
as macro-arg-alist-cons = (assoc token macro-arg-alist)
if (or (token-equal-p token "##") ; Concatenation.
(and *with-c-syntax-preprocessor-process-digraph* (token-equal-p token "%:%:")))
do (let* ((token1-list
(loop
initially
(when (null rev-expansion)
(error 'preprocess-error
:format-control "The first operand of '##' does not exists."))
with ret = nil
for i = (pop rev-expansion)
while i
do (push i ret)
while (macro-control-marker-p i)
finally (return ret)))
(token2-list
(if (endp next-token-cons)
(error 'preprocess-error
:format-control "The second operand of '##' does not exists.")
(let ((token2 (car next-token-cons)))
(if-let ((entry (assoc token2 macro-arg-alist)))
(pp-macro-argument-token-list (cdr entry))
(list token2))))))
(multiple-value-bind (before-token-list conc-result after-token-list)
(expand-concatenate-operator token1-list token2-list)
(setf token-cons
(list* :loop-on-clause-dummy
(nconc after-token-list
(rest next-token-cons)))) ; removes token2
(nreconcf rev-expansion before-token-list)
(push conc-result rev-expansion)))
else if (or (token-equal-p token "#") ; Stringify.
(and *with-c-syntax-preprocessor-process-digraph* (token-equal-p token "%:")))
do (cond
((endp next-token-cons)
(error 'preprocess-error
:format-control "The second operand of '#' does not exists."))
((token-equal-p (car next-token-cons) "##")
;; Special case. See `with-c-syntax.test::test-pp-6.10.3.3-example'.
(push token rev-expansion))
(t
(setf token-cons (list* :loop-on-clause-dummy (cdr next-token-cons)))
(push (expand-stringify-operator (car next-token-cons) macro-arg-alist)
rev-expansion)))
else if macro-arg-alist-cons ; Replacement
do (revappendf rev-expansion
(pp-macro-argument-token-list-expansion (cdr macro-arg-alist-cons)))
else
do (push token rev-expansion)
finally
(return (delete-consecutive-whitespace-marker
(delete +placemaker-token+ ; Placemaker-token should not be left after macro expansion.
(nreverse rev-expansion))))))
(defun expand-function-like-macro (macro-definition rest-token-list macro-alist pp-state)
(multiple-value-bind (macro-arg-list tail-of-rest-token-list new-macro-alist)
(collect-preprocessor-macro-arguments rest-token-list macro-alist)
(dolist (marg macro-arg-list)
(expand-macro-argument marg pp-state))
(let ((macro-arg-alist
(make-macro-arguments-alist macro-definition macro-arg-list)))
(values
(expand-macro-replacement-list (macro-definition-replacement-list macro-definition)
macro-arg-alist)
tail-of-rest-token-list
new-macro-alist))))
(defun function-like-macro-invocation-p (token-list)
(let ((first-token
(find-if-not #'macro-control-marker-p token-list)))
(and first-token
(token-equal-p first-token "("))))
(defun expand-object-like-macro (macro-definition pp-state)
(expand-macro-replacement-list (macro-definition-replacement-list macro-definition)
nil))
(defun expand-predefined-macro (predefined-macro pp-state)
(cond ((boundp predefined-macro)
(symbol-value predefined-macro))
((fboundp predefined-macro)
(funcall predefined-macro pp-state))
(t
(error 'preprocess-error
:format-control "Predefined macro ~A is not bound nor fbound."
:format-arguments (list predefined-macro)))))
(defun expand-preprocessor-macro (token rest-token-list macro-alist pp-state)
"Expand preprocessor macro named TOKEN. If it is a function-like
macro, its arguments are taken from REST-TOKEN-LIST. MACRO-ALIST is an
alist of preprocessor macro definitions. PP-STATE is a
`preprocessor-state' object, used for expanding predefined macros.
Returns four values:
1. A list of tokens made by expansion.
2. A list, tail of REST-TOKEN-LIST, left after collecting function-like macro arguments.
3. An alist derived from MACRO-ALIST changed after expansion.
4. A boolean tells whether TOKEN is a function-like-macro but
REST-TOKEN-LIST is not a form of invocation."
(when-let ((predefined-macro
(find-symbol (symbol-name token) '#:with-c-syntax.predefined-macro)))
(let ((expansion (expand-predefined-macro predefined-macro pp-state)))
(return-from expand-preprocessor-macro
(values (list expansion)
rest-token-list
macro-alist))))
(let* ((pp-macro-entry (assoc token macro-alist))
(pp-macro (cdr pp-macro-entry)))
(etypecase pp-macro
(function-like-macro
(if (function-like-macro-invocation-p rest-token-list)
(multiple-value-bind (expansion tail-token-list new-macro-alist)
(expand-function-like-macro pp-macro rest-token-list macro-alist pp-state)
(values (nconc (list pp-macro)
expansion
(list :end-of-preprocessor-macro-scope))
tail-token-list
new-macro-alist))
(values (list token)
rest-token-list
macro-alist
t)))
(object-like-macro
(let ((expansion (expand-object-like-macro pp-macro pp-state)))
(values (nconc (list pp-macro)
expansion
(list :end-of-preprocessor-macro-scope))
rest-token-list
macro-alist))))))
;;; Identifier split.
(defun find-operator-on-prefix (operator symbol)
"This function tries to find OPERATOR in front of SYMBOL.
If found, it returns T and list of of splited symbols. If not found,
it returns NIL."
(multiple-value-bind (found suffix)
(starts-with-subseq (symbol-name operator) (symbol-name symbol)
:return-suffix t)
(if found
(values t
(list operator
(intern suffix))))))
(defun find-operator-on-suffix (operator symbol)
"This function tries to find OPERATOR in back of SYMBOL.
If found, it returns T and list of of splited symbols. If not found,
it returns NIL."
(let ((sym-name (symbol-name symbol))
(op-name (symbol-name operator)))
(if (ends-with-subseq op-name sym-name)
(values t
(list (intern (subseq sym-name 0 (- (length sym-name) (length op-name))))
operator)))))
(defun earmuff-lengthes (string earmuff-char)
"Calculates '*earmuff*' lengthes in STRING with EARMUFF-CHAR, and
returns it lengthes in front and in last.
e.g. (earmuff-lengthes \"*foo**\" #\*) ; => (values 1 2)"
(let* ((len (length string))
(prefix-len
(if (char/= (char string 0) earmuff-char)
0 ; not found
(or (position earmuff-char string :test #'char/=)
len))) ; STRING is fully filled with the EARMUFF-CHAR.
(suffix-len
(if (char/= (char string (1- len)) earmuff-char)
0 ; not found
(if-let (suffix-pos--1 (position earmuff-char string :test #'char/= :from-end t))
(- len (1+ suffix-pos--1))
len)))) ; STRING is fully filled with the EARMUFF-CHAR.
(values prefix-len suffix-len)))
(defun preprocessor-try-split (symbol) ; TODO: consider this name
"This function tries to find some C operators in SYMBOL.
If found, it returns T and list of splited symbols. If not found, it
returns NIL."
;; I think operators their precedence is equal or more than '~'
;; are candidates, in my personal C style.
(mv-cond-let (found trimed)
;; -- Operator precedence 1 --
;; TODO: add '.' and '->' operator.
((find-operator-on-suffix 'with-c-syntax.syntax:++ symbol)) ; FIXME: what to do on '+constant++ 1' ?
((find-operator-on-suffix 'with-c-syntax.syntax:-- symbol))
;; -- Operator precedence 2 --
((find-operator-on-prefix 'with-c-syntax.syntax:++ symbol))
((find-operator-on-prefix 'with-c-syntax.syntax:-- symbol))
((find-operator-on-prefix 'with-c-syntax.syntax:|sizeof| symbol))
;; I think these are rarely used in Lisp symbols.
((find-operator-on-prefix 'with-c-syntax.syntax:- symbol))
((find-operator-on-prefix 'with-c-syntax.syntax:! symbol))
((find-operator-on-prefix 'with-c-syntax.syntax:~ symbol))
((find-operator-on-prefix 'with-c-syntax.syntax:& symbol))
;; These are used as *earmuffs*. I handle them specially.
((find-operator-on-prefix 'with-c-syntax.syntax:* symbol)
(multiple-value-bind (prefix-len suffix-len)
(earmuff-lengthes (symbol-name symbol) #\*)
(if (> prefix-len suffix-len)
(values t trimed)
(values nil symbol))))
((find-operator-on-prefix 'with-c-syntax.syntax:+ symbol)
(multiple-value-bind (prefix-len suffix-len)
(earmuff-lengthes (symbol-name symbol) #\+)
(if (> prefix-len suffix-len)
(values t trimed)
(values nil symbol))))
;; FIXME: What to do when (>= prefix-len 2) or (>= suffix-len 2) ?
;; Should I accept 'int n = ++a-constant++ 10' as '(+ (+ +a-constant+) 10)'?
(t
(values nil symbol))))
;;; preprocessor-state object held in the main loop.
(defclass preprocessor-state ()
((file-pathname :initarg :file-pathname :initform nil
:accessor pp-state-file-pathname)
(token-list :initarg :token-list :type list
:accessor pp-state-token-list)
(result-list :initform nil :type list
:accessor pp-state-result-list)
(line-number :initform 1 :type integer
:accessor pp-state-line-number)
(tokens-in-line :initform 0 :type integer
:accessor pp-state-tokens-in-line)
(macro-alist :initform () :type list
:accessor pp-state-macro-alist)
(if-section-stack :initform nil :type list
:accessor pp-state-if-section-stack)
(if-section-skip-reason :initform nil
:accessor pp-state-if-section-skip-reason)
(include-stack :initform nil
:accessor pp-state-include-stack)))
;; TODO: reduce its usage
(defmacro with-preprocessor-state-slots ((state) &body body)
`(with-accessors ((file-pathname pp-state-file-pathname)
(token-list pp-state-token-list)
(result-list pp-state-result-list)
(line-number pp-state-line-number)
(tokens-in-line pp-state-tokens-in-line)
(macro-alist pp-state-macro-alist)
(if-section-stack pp-state-if-section-stack)
(if-section-skip-reason pp-state-if-section-skip-reason)
(include-stack pp-state-include-stack))
,state
,@body))
(defun draw-preprocessor-directive-line-tokens (state)
(with-preprocessor-state-slots (state)
(loop for i = (first token-list)
while token-list
until (eq i +newline-marker+)
do (pop token-list)
collect i)))
(defun preprocessor-token-exists-p (token-list)
(loop for token in token-list
thereis (not (whitespace-like-token-p token))))
(defun token-equal-p (token name) ; TODO: move to the top of this file. TODO: rename to 'token-name-equal-p'
(and (symbolp token)
(string= token name)))
(defun raise-no-preprocessor-token-error (directive-name)
(error 'preprocess-error
:format-control "No token after #~A"
:format-arguments (list directive-name)))
(defmacro check-and-pop-pp-directive-token-1 (token-list directive-name errorp)
"Used by `pop-preprocessor-directive-token'"
`(if (null ,token-list)
,(if errorp
`(raise-no-preprocessor-token-error ,directive-name)
nil)
(pop ,token-list)))
(defmacro pop-preprocessor-directive-token (token-list directive-name &key (errorp t))
"Pops the next token in TOKEN-LIST ignoring `+whitespace-marker+'"
(with-gensyms (head_)
`(let ((,head_
(check-and-pop-pp-directive-token-1 ,token-list ,directive-name ,errorp)))
(if (eq +whitespace-marker+ ,head_)
(check-and-pop-pp-directive-token-1 ,token-list ,directive-name ,errorp)
,head_))))
(defun check-no-preprocessor-token (token-list directive-name)
"Checks no preprocessing token is left in TOKEN-LIST."
(when (preprocessor-token-exists-p token-list)
(error 'preprocess-error
:format-control "Extra tokens are found after #~A"
:format-arguments (list (string directive-name)))))
(defmacro pop-last-preprocessor-directive-token (token-list directive-name)
"For #ifdef, #ifndef, and #undef"
(with-gensyms (dsym_)
`(let ((,dsym_ ,directive-name))
(prog1 (pop-preprocessor-directive-token ,token-list ,dsym_)
(check-no-preprocessor-token ,token-list ,dsym_)))))
(defgeneric process-preprocessing-directive (directive-symbol token-list state))
;;; #if sections.
(defclass if-section ()
((group-conditions :initform (make-array 1 :fill-pointer 0 :adjustable t)
:type vector)))
(defmethod cl:print-object ((obj if-section) stream)
(print-unreadable-object (obj stream :type t :identity t)))
(defmethod if-section-add-group (if-section form result)
(with-slots (group-conditions) if-section
(vector-push-extend (cons form result) group-conditions)))
(defmethod if-section-processed-p (if-section)
(with-slots (group-conditions) if-section
(loop with processed? = nil
for (condition . eval-result) across group-conditions
when (eq condition :else)
do (error 'preprocess-error
:format-control "#else already appeared.")
when eval-result
do (setf processed? t)
finally (return processed?))))
(defun begin-if-section (state condition-form eval-result)
(let ((if-section-obj (make-instance 'if-section)))
(if-section-add-group if-section-obj condition-form eval-result)
(with-preprocessor-state-slots (state)
(push if-section-obj if-section-stack)
(when (and (null if-section-skip-reason)
(not eval-result))
(setf if-section-skip-reason if-section-obj)))))
(defun expand-defined-operator (token-list macro-alist directive-symbol)
(loop while (preprocessor-token-exists-p token-list) ; FIXME: this is too slow..
collect
(let ((token (pop-preprocessor-directive-token token-list directive-symbol :errorp nil)))
(cond
((pp-defined-operator-p token)
(let* ((next-token
(pop-preprocessor-directive-token token-list directive-symbol))
(param
(if (token-equal-p next-token "(")
(prog1 (pop-preprocessor-directive-token token-list directive-symbol)
(let ((r-paren?
(pop-preprocessor-directive-token token-list directive-symbol)))
(unless (token-equal-p r-paren? ")")
(error
'preprocess-error
:format-control "'defined' operator does not have corresponding ')'. '~A' was found."
:format-arguments (list r-paren?)))))
next-token)))
(when (or (not (symbolp param))
(find-punctuator (symbol-name param)))
(error
'preprocess-error
:format-control "'defined' operator takes only identifiers. '~A' was passed."
:format-arguments (list param)))
(if (preprocessor-macro-exists-p macro-alist param) 1 nil)))
(t
token)))))
(defun eval-if-expression (directive-symbol directive-token-list state)
(with-preprocessor-state-slots (state)
(let* ((expansion
(expand-defined-operator directive-token-list macro-alist
directive-symbol))
(expansion
(expand-each-preprocessor-macro-in-list expansion macro-alist state))
(expansion
(remove-if #'macro-scoping-marker-p expansion)) ; TODO: move to the next lexer?
(lexer
(pp-if-expression-lexer expansion))
(parsed-form
(handler-case
(with-c-compilation-unit ()
(parse-with-lexer lexer *expression-parser*))
(yacc-parse-error (condition)
(error 'preprocess-if-expression-parse-error :yacc-error condition))))
(check-left-token
(when (funcall lexer)
(error 'preprocess-error
:format-control "Extra tokens are found after #if")))
(value (eval parsed-form))
(result (and value
(not (eql value 0))))) ; Special case for "#if 0" idiom.
(declare (ignore check-left-token))
result)))
(defmethod process-preprocessing-directive ((directive-symbol
(eql 'with-c-syntax.preprocessor-directive:|if|))
directive-token-list state)
(with-preprocessor-state-slots (state)
(when if-section-skip-reason
(begin-if-section state '(:skipped :if) nil)
(return-from process-preprocessing-directive nil))
(unless (preprocessor-token-exists-p directive-token-list)
(raise-no-preprocessor-token-error directive-symbol))
(let ((result (eval-if-expression directive-symbol directive-token-list state)))
(begin-if-section state directive-token-list result))))
(defmethod process-preprocessing-directive ((directive-symbol
(eql 'with-c-syntax.preprocessor-directive:|ifdef|))
directive-token-list state)
(with-preprocessor-state-slots (state)
(when if-section-skip-reason
(begin-if-section state '(:skipped :ifdef) nil)
(return-from process-preprocessing-directive nil))
(let* ((identifier
(pop-last-preprocessor-directive-token directive-token-list directive-symbol))
(condition `(:ifdef ,identifier))
(result (preprocessor-macro-exists-p macro-alist identifier)))
(begin-if-section state condition result))))
(defmethod process-preprocessing-directive ((directive-symbol
(eql 'with-c-syntax.preprocessor-directive:|ifndef|))
directive-token-list state)
;; TODO: merge with 'ifdef'.
(with-preprocessor-state-slots (state)
(when if-section-skip-reason
(begin-if-section state '(:skipped :ifndef) nil)
(return-from process-preprocessing-directive nil))
(let* ((identifier
(pop-last-preprocessor-directive-token directive-token-list directive-symbol))
(condition `(:ifndef ,identifier))
(result (not (preprocessor-macro-exists-p macro-alist identifier))))
(begin-if-section state condition result))))
(defun check-in-if-section (state directive-symbol)
(with-preprocessor-state-slots (state)
(unless if-section-stack
(error 'preprocess-error
:format-control "#~A appeared outside of #if section."
:format-arguments (list (symbol-name directive-symbol))))))
(defmethod process-preprocessing-directive ((directive-symbol
(eql 'with-c-syntax.preprocessor-directive:|elif|))
directive-token-list state)
(check-in-if-section state directive-symbol)
(with-preprocessor-state-slots (state)
(let* ((current-if-section (first if-section-stack))
(if-section-processed-p (if-section-processed-p current-if-section)))
(cond
((and if-section-skip-reason
(not (eq if-section-skip-reason current-if-section))) ; Skipped because of outer if-section.
(if-section-add-group current-if-section '(:skipped :elif) nil))
(if-section-processed-p
(if-section-add-group current-if-section '(:processed :elif) nil)
(setf if-section-skip-reason current-if-section))
(t
(unless (preprocessor-token-exists-p directive-token-list)
(raise-no-preprocessor-token-error directive-symbol))
(let ((result (eval-if-expression directive-symbol directive-token-list state)))
(if-section-add-group current-if-section directive-token-list result)
(setf if-section-skip-reason (if result
nil
current-if-section))))))))
(defmethod process-preprocessing-directive ((directive-symbol
(eql 'with-c-syntax.preprocessor-directive:|else|))
directive-token-list state)
(check-no-preprocessor-token directive-token-list directive-symbol)
(check-in-if-section state directive-symbol)
(with-preprocessor-state-slots (state)
(let* ((current-if-section (first if-section-stack))
(if-section-processed-p (if-section-processed-p current-if-section)))
(cond
((and if-section-skip-reason
(not (eq if-section-skip-reason current-if-section))) ; Skipped because of outer if-section.
(if-section-add-group current-if-section '(:skipped :else) nil))
(if-section-processed-p
(if-section-add-group current-if-section :else nil)
(setf if-section-skip-reason current-if-section))
(t
(if-section-add-group current-if-section :else t)
(setf if-section-skip-reason nil))))))
(defmethod process-preprocessing-directive ((directive-symbol
(eql 'with-c-syntax.preprocessor-directive:|endif|))
directive-token-list state)
(check-no-preprocessor-token directive-token-list directive-symbol)
(check-in-if-section state directive-symbol)
(with-preprocessor-state-slots (state)
(let ((removed-if-section (pop if-section-stack)))
(when (eq if-section-skip-reason removed-if-section)
(setf if-section-skip-reason nil)))))
;;; #include
(defun parse-header-name (token-list directive-symbol try-pp-macro-expand macro-alist pp-state)
;; FIXME: Current header-name implementation does not recognize implementation-defined points.
(let ((token1 (pop-preprocessor-directive-token token-list directive-symbol)))
(cond
((stringp token1)