summarylogtreecommitdiffstats
path: root/14_extend_parser.patch
blob: 5e25afd79e0e831fe2a88f80332e9afb71e5f84f (plain)
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
Description: Include __int128, make statements before delcarations in functions
possible and add some workaround to load x86intrin.h successfully.
Author: Fabian Brosda <fabi3141@gmx.de>
Last-Update: 2020-07-10

diff --unified --recursive --text --new-file c-wrapper-0.6.1/lib/c-wrapper/c-parser.scm c-wrapper-0.6.1-new/lib/c-wrapper/c-parser.scm
--- c-wrapper-0.6.1/lib/c-wrapper/c-parser.scm	2020-07-10 12:05:57.306445343 +0200
+++ c-wrapper-0.6.1-new/lib/c-wrapper/c-parser.scm	2020-07-10 14:37:16.260584754 +0200
@@ -181,6 +181,26 @@
        ,@(reverse init-list)
        ,@statements)))
 
+(define-maybe (%COMPOUND-STATEMENT-WITH-DECLARATION-EXT statements-before declaration-list statements)
+  (let ((var-list '())
+        (init-list '()))
+    (for-each (lambda (declaration)
+                (let* ((type (type-of declaration))
+                       (identifier (name-of declaration))
+                       (init-val (value-of declaration)))
+                  ;; TODO: typedef in compound_statement is not supported
+                  (push! var-list `(,identifier (make ,type)))
+                  (when init-val
+                    (push! init-list
+                           `(set! ((with-module c-wrapper.c-ffi ref) ,identifier)
+                                  ,init-val)))))
+              declaration-list)
+    `(begin 
+       ,@statements-before
+       (let* ,(reverse var-list)
+       ,@(reverse init-list)
+       ,@statements))))
+
 (define-maybe (%REF-ARRAY v index)
   `((with-module c-wrapper.c-ffi ref) ,v ,(%INT index)))
 
diff --unified --recursive --text --new-file c-wrapper-0.6.1/src/c-grammar.scm c-wrapper-0.6.1-new/src/c-grammar.scm
--- c-wrapper-0.6.1/src/c-grammar.scm	2009-08-08 16:44:50.000000000 +0200
+++ c-wrapper-0.6.1-new/src/c-grammar.scm	2020-07-10 15:15:09.096916631 +0200
@@ -122,6 +122,7 @@
   (LPAREN expr RPAREN) : (%EXPR-IN-PARENS $2)
   (LPAREN compound_statement RPAREN) : (%COMPOUND-STATEMENT $2)
   (LPAREN type_name RPAREN LCBRA initializer_list RCBRA) : #f
+  (LPAREN type_name RPAREN LCBRA initializer_list COMMA RCBRA) : #f
   (objc_message_expr) : $1
   (objc_selector_expr) : $1
   (objc_protocol_expr) : #f
@@ -474,8 +475,8 @@
   (LCBRA RCBRA) : (%COMPOUND-STATEMENT '(0))
   (LCBRA statement_list RCBRA) : (%COMPOUND-STATEMENT $2)
   (LCBRA declaration_list RCBRA) : (%COMPOUND-STATEMENT '(0))
-  (LCBRA declaration_list statement_list RCBRA)
-  : (%COMPOUND-STATEMENT-WITH-DECLARATION $2 $3)
+  (LCBRA declaration_list statement_list RCBRA) : (%COMPOUND-STATEMENT-WITH-DECLARATION $2 $3)
+  (LCBRA statement_list declaration_list statement_list RCBRA) : (%COMPOUND-STATEMENT-WITH-DECLARATION-EXT $2 $3 $4)
   (error RCBRA) : #f
   )
 
diff --unified --recursive --text --new-file c-wrapper-0.6.1/src/c-lex.c c-wrapper-0.6.1-new/src/c-lex.c
--- c-wrapper-0.6.1/src/c-lex.c	2020-07-10 12:05:57.309778665 +0200
+++ c-wrapper-0.6.1-new/src/c-lex.c	2020-07-10 14:26:17.378712926 +0200
@@ -361,6 +361,7 @@
         "void",
         "_Bool",
         "_Float128",
+        "__int128",
         NULL,
     };
     int i;
diff --unified --recursive --text --new-file c-wrapper-0.6.1/src/c-parser.c c-wrapper-0.6.1-new/src/c-parser.c
--- c-wrapper-0.6.1/src/c-parser.c	2020-07-10 12:05:57.313111989 +0200
+++ c-wrapper-0.6.1-new/src/c-parser.c	2020-07-10 15:49:47.417172126 +0200
@@ -104,6 +104,7 @@
 DEFINE_SYM(void);
 DEFINE_SYM(_Bool);
 DEFINE_SYM(_Float128);
+DEFINE_SYM(__int128);
 DEFINE_SYM(__builtin_va_list);
 DEFINE_SYM(U_struct);
 DEFINE_SYM(U_union);
@@ -472,7 +473,9 @@
     } else if (SCM_EQ(car, SYM(_Bool))) {
         SCM_RETURN(SYM(c_int));
     } else if (SCM_EQ(car, SYM(_Float128))) {
         SCM_RETURN(SYM(c_double));
+    } else if (SCM_EQ(car, SYM(__int128))) {
+        SCM_RETURN(SYM(c_longlong));
     } else if (SCM_EQ(car, SYM(__builtin_va_list))) {
         SCM_RETURN(SCM_LIST2(SCM_LIST3(SYM(with_module), SYM(c_wrapper_c_ffi), SYM(ptr)), SYM(c_void)));
     } else if (SCM_PAIRP(car) && SCM_EQ(SCM_CAR(car), SYM(U_struct))) {
@@ -1024,7 +1027,11 @@
     Scm_ParserAttributeClear();
     td_list = Scm_MakeTypeDeclList(type_spec_list, declarator_list);
     SCM_FOR_EACH(pair, td_list) {
-        Scm_ArgPoolAdd(SCM_TYPE_DECL_NAME(SCM_CAR(pair)));
+        // hack to avoid segfault when loading x86intrin.h
+        // TODO: why is this necessary?
+        if (!SCM_EQ(SCM_CAR(pair), SCM_FALSE)) {
+            Scm_ArgPoolAdd(SCM_TYPE_DECL_NAME(SCM_CAR(pair)));
+        }
     }
 
     SCM_RETURN(td_list);
@@ -1865,6 +1872,7 @@
     INIT_SYM(void, "void");
     INIT_SYM(_Bool, "_Bool");
     INIT_SYM(_Float128, "_Float128");
+    INIT_SYM(__int128, "__int128");
     INIT_SYM(__builtin_va_list, "__builtin_va_list");
     INIT_SYM(U_struct, "STRUCT");
     INIT_SYM(U_union, "UNION");
diff --unified --recursive --text --new-file c-wrapper-0.6.1/testsuite/Makefile.in c-wrapper-0.6.1-new/testsuite/Makefile.in
--- c-wrapper-0.6.1/testsuite/Makefile.in	2020-07-10 12:05:57.313111989 +0200
+++ c-wrapper-0.6.1-new/testsuite/Makefile.in	2020-07-10 15:45:31.480594159 +0200
@@ -78,6 +78,7 @@
 	$(GOSH) -I../src -I../lib stdio-test.scm >> test.log
 	$(GOSH) -I../src -I../lib math-test.scm >> test.log
 	$(GOSH) -I../src -I../lib local-typedef.scm >> test.log
+	$(GOSH) -I../src -I../lib stmt-decl.scm >> test.log
 	$(GOSH) -I../src -I../lib inline-test.scm >> test.log
 	$(GOSH) -I../src -I../lib fptr_array-test.scm >> test.log
 	$(GOSH) -I../src -I../lib array_qualifier-test.scm >> test.log
diff --unified --recursive --text --new-file c-wrapper-0.6.1/testsuite/stmt_decl.h c-wrapper-0.6.1-new/testsuite/stmt_decl.h
--- c-wrapper-0.6.1/testsuite/stmt_decl.h	1970-01-01 01:00:00.000000000 +0100
+++ c-wrapper-0.6.1-new/testsuite/stmt_decl.h	2020-07-10 15:43:54.123881023 +0200
@@ -0,0 +1,6 @@
+void f(int arg1)
+{
+  arg1 = 3;
+  double tmp = arg1;
+  return tmp;
+}
diff --unified --recursive --text --new-file c-wrapper-0.6.1/testsuite/stmt-decl.scm c-wrapper-0.6.1-new/testsuite/stmt-decl.scm
--- c-wrapper-0.6.1/testsuite/stmt-decl.scm	1970-01-01 01:00:00.000000000 +0100
+++ c-wrapper-0.6.1-new/testsuite/stmt-decl.scm	2020-07-10 15:45:16.127256968 +0200
@@ -0,0 +1,13 @@
+;;;
+;;; Test include math.h
+;;;
+
+(use gauche.test)
+
+(test-start "test for statement before and after declaration in c function")
+(use c-wrapper)
+
+(c-include "stmt_decl.h")
+
+;; epilogue
+(test-end)