| 1 |
;;; |
| 2 |
;;; C Parser |
| 3 |
;;; |
| 4 |
;;; Copyright (c) 2021-2024 Shiro Kawai <[email protected]> |
| 5 |
;;; |
| 6 |
;;; Redistribution and use in source and binary forms, with or without |
| 7 |
;;; modification, are permitted provided that the following conditions |
| 8 |
;;; are met: |
| 9 |
;;; |
| 10 |
;;; 1. Redistributions of source code must retain the above copyright |
| 11 |
;;; notice, this list of conditions and the following disclaimer. |
| 12 |
;;; |
| 13 |
;;; 2. Redistributions in binary form must reproduce the above copyright |
| 14 |
;;; notice, this list of conditions and the following disclaimer in the |
| 15 |
;;; documentation and/or other materials provided with the distribution. |
| 16 |
;;; |
| 17 |
;;; 3. Neither the name of the authors nor the names of its contributors |
| 18 |
;;; may be used to endorse or promote products derived from this |
| 19 |
;;; software without specific prior written permission. |
| 20 |
;;; |
| 21 |
;;; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 22 |
;;; "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 23 |
;;; LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 24 |
;;; A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 25 |
;;; OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 26 |
;;; SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED |
| 27 |
;;; TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 28 |
;;; PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 29 |
;;; LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 30 |
;;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 31 |
;;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 32 |
;;; |
| 33 |
|
| 34 |
(define-module lang.c.parser |
| 35 |
(use file.util) |
| 36 |
(use util.match) |
| 37 |
(use parser.peg) |
| 38 |
(use gauche.process) |
| 39 |
(use gauche.config) |
| 40 |
(use gauche.dictionary) |
| 41 |
(use gauche.lazy) |
| 42 |
(use control.cseq) |
| 43 |
(use lang.c.lexer) |
| 44 |
(use lang.c.type) |
| 45 |
(use srfi.13) |
| 46 |
(use lang.c.parameter) |
| 47 |
|
| 48 |
(export c-tokenize-file |
| 49 |
c-tokenize-string |
| 50 |
c-parse-file |
| 51 |
c-parse-string |
| 52 |
cpp-include-paths ;re-export from lang.c.parameter |
| 53 |
) |
| 54 |
) |
| 55 |
(select-module lang.c.parser) |
| 56 |
|
| 57 |
|
| 58 |
;; |
| 59 |
;; Typename catalog. |
| 60 |
;; symbol -> c-type |
| 61 |
;; |
| 62 |
;; C syntax is ambiguous that identifier can also be a type name. We need |
| 63 |
;; to track previously declared type names. |
| 64 |
;; |
| 65 |
(define *default-typedef-table* |
| 66 |
(make-hash-table eq-comparator)) |
| 67 |
|
| 68 |
(define (default-typedefs) (hash-table-copy *default-typedef-table*)) |
| 69 |
|
| 70 |
(define typedef-names |
| 71 |
(make-parameter (make-stacked-map (default-typedefs)))) |
| 72 |
|
| 73 |
(define-syntax $with-scope |
| 74 |
(syntax-rules () |
| 75 |
[(_ parser) |
| 76 |
($parameterize ((typedef-names (make-stacked-map |
| 77 |
(make-hash-table eq-comparator) |
| 78 |
(typedef-names)))) |
| 79 |
parser)])) |
| 80 |
|
| 81 |
;;; |
| 82 |
;;; Parser |
| 83 |
;;; |
| 84 |
|
| 85 |
;; Token recognition |
| 86 |
(define %identifier ($match1 ('ident _))) |
| 87 |
(define %constant ($match1 ('const . _))) |
| 88 |
(define %string-literal |
| 89 |
($lift string-concatenate |
| 90 |
($many1 ($match1 ((or 'string 'wstring) s) s)))) |
| 91 |
|
| 92 |
(define %LP ($. #\( )) |
| 93 |
(define %RP ($. #\) )) |
| 94 |
(define %LC ($. #\{ )) |
| 95 |
(define %RC ($. #\} )) |
| 96 |
(define %LB ($. #\[ )) |
| 97 |
(define %RB ($. #\] )) |
| 98 |
|
| 99 |
;; typical infix operator parser |
| 100 |
;; recognize term sep term sep ... term, returns (op term ...) |
| 101 |
;; if ther's no sep, just return the result of term. |
| 102 |
(define ($infix term sep op) |
| 103 |
($binding ($: xs ($sep-by term ($. sep))) |
| 104 |
(=> fail) |
| 105 |
(match xs |
| 106 |
[() (fail (format "at least one expression required for ~s" op))] |
| 107 |
[(x0) x0] |
| 108 |
[xs `(,op ,@xs)]))) |
| 109 |
|
| 110 |
;; multiple choice infix. assuming sep and op are the same. |
| 111 |
(define ($infix-n term associativity seps) |
| 112 |
($binding ($: x term) |
| 113 |
($: xs ($many ($list ($one-of seps) term))) |
| 114 |
(ecase associativity |
| 115 |
[(left) (fold (^[e r] `(,(car e) ,(cadr e) ,r)) x xs)] |
| 116 |
[(right) (fold-right (^[e r] `(,(car e) ,(cadr e) ,r)) x xs)]))) |
| 117 |
|
| 118 |
;; |
| 119 |
;; nonstandard elements |
| 120 |
;; |
| 121 |
|
| 122 |
;; __attribute__ extension can appear in various places. |
| 123 |
(define %attribute-list-item |
| 124 |
($lbinding ($: name %identifier) |
| 125 |
($: params ($optional |
| 126 |
($between %LP |
| 127 |
($sep-by %expression ($. '|,|)) |
| 128 |
%RP))) |
| 129 |
(if params |
| 130 |
`(,name ,@params) |
| 131 |
name))) |
| 132 |
|
| 133 |
(define %attribute-specifier |
| 134 |
($binding ($. '__attribute__) |
| 135 |
%LP %LP |
| 136 |
($: attrs ($sep-by %attribute-list-item ($. '|,|))) |
| 137 |
%RP %RP |
| 138 |
`(attribute ,@attrs))) |
| 139 |
|
| 140 |
(define %attribute-specifier-list |
| 141 |
($many %attribute-specifier)) |
| 142 |
|
| 143 |
;; asm can appear as a statement, or a part of declaration. |
| 144 |
(define %asm-decl-suffix |
| 145 |
($binding ($. 'asm) |
| 146 |
%LP ($: s %string-literal) %RP |
| 147 |
`(asm ,s))) |
| 148 |
|
| 149 |
(define %asm-operand |
| 150 |
($lbinding ($: symbolic-name ($optional ($between %LB %identifier %RB))) |
| 151 |
($: constraint %string-literal) |
| 152 |
($: expr ($optional ($between %LP %expression %RP))) |
| 153 |
`(,symbolic-name ,constraint ,expr))) |
| 154 |
|
| 155 |
(define %asm-statement |
| 156 |
($binding ($. 'asm) |
| 157 |
($: quals ($many ($one-of '(volatile inline goto)))) |
| 158 |
%LP |
| 159 |
($: template %string-literal) |
| 160 |
($optional |
| 161 |
($seq |
| 162 |
($. '|:|) |
| 163 |
($: outs ($sep-by %asm-operand ($. '|,|))) |
| 164 |
($optional |
| 165 |
($seq |
| 166 |
($. '|:|) |
| 167 |
($: ins ($sep-by %asm-operand ($. '|,|))) |
| 168 |
($optional |
| 169 |
($seq |
| 170 |
($. '|:|) |
| 171 |
($: clobbers ($sep-by %string-literal ($. '|,|))) |
| 172 |
($optional |
| 173 |
($seq |
| 174 |
($. '|:|) |
| 175 |
($: gotos ($sep-by %identifier ($. '|,|))))))))))) |
| 176 |
%RP |
| 177 |
($. '|\;|) |
| 178 |
`(asm ,template |
| 179 |
,(if (undefined? outs) '() outs) |
| 180 |
,(if (undefined? ins) '() ins) |
| 181 |
,(if (undefined? clobbers) '() clobbers) |
| 182 |
,(if (undefined? gotos) '() clobbers)))) |
| 183 |
|
| 184 |
;; gcc's va_arg expands into __builtin_va_arg and handled by the compiler. |
| 185 |
(define %va-arg |
| 186 |
($lbinding ($match1 ('ident '__builtin_va_arg)) |
| 187 |
%LP ($: e %assignment-expression) ($. '|,|) ($: t %type-name) %RP |
| 188 |
`(va-arg ,e ,t))) |
| 189 |
|
| 190 |
;; |
| 191 |
;; Standard syntax |
| 192 |
;; |
| 193 |
|
| 194 |
;; 6.5.1 Primary expressions |
| 195 |
(define %primary-expression |
| 196 |
($lazy |
| 197 |
($or %va-arg ; gcc specific |
| 198 |
%identifier |
| 199 |
%constant |
| 200 |
%string-literal |
| 201 |
($binding ($between %LP |
| 202 |
($or ($: stmt-expr %compound-statement) ; gcc |
| 203 |
($: expr %expression)) |
| 204 |
%RP) |
| 205 |
(if (undefined? stmt-expr) |
| 206 |
`(paren ,expr) |
| 207 |
`(stmt-expr ,stmt-expr)))))) |
| 208 |
|
| 209 |
;; 6.5.2 Postfix operators |
| 210 |
(define %postfix-expression |
| 211 |
($lbinding ($or ($: prim %primary-expression) |
| 212 |
($seq ($: cast ($between %LP %type-name %RP)) |
| 213 |
($: init ($between %LC %initializer-list %RC)))) |
| 214 |
($: pfx ($many %postfix-element)) |
| 215 |
(let1 core (if (undefined? prim) |
| 216 |
`(cast ,cast ,init) |
| 217 |
prim) |
| 218 |
(fold (^[pfx r] (match pfx |
| 219 |
[(op . params) `(,op ,r ,@params)] |
| 220 |
[op `(,op ,r)])) |
| 221 |
core pfx)))) |
| 222 |
|
| 223 |
(define %postfix-element |
| 224 |
($or ($lbinding %LB ($: e %expression) %RB |
| 225 |
`(aref ,e)) |
| 226 |
($lbinding %LP ($: as %argument-expression-list) %RP |
| 227 |
`(call ,@as)) |
| 228 |
($binding ($. '|.|) ($: field %identifier) |
| 229 |
`(ref ,field)) |
| 230 |
($binding ($. '->) ($: field %identifier) |
| 231 |
`(-> ,field)) |
| 232 |
($seq ($. '++) ($return 'post++)) |
| 233 |
($seq ($. '--) ($return 'post--)))) |
| 234 |
|
| 235 |
(define %argument-expression-list |
| 236 |
($lazy ($sep-by %assignment-expression ($. '|,|)))) |
| 237 |
|
| 238 |
;; 6.5.3 Unary operators |
| 239 |
(define %unary-expression |
| 240 |
($or ($try ($lbinding ($: op ($one-of '(sizeof __alignof__))) |
| 241 |
%LP |
| 242 |
($: t %type-name) |
| 243 |
%RP |
| 244 |
`(,op ,t))) |
| 245 |
($lbinding ($: op ($one-of '(& * + - ~ !))) |
| 246 |
($: main %cast-expression) |
| 247 |
`(,op ,main)) |
| 248 |
($binding ($: ops ($many ($one-of '(++ -- sizeof)))) |
| 249 |
($: main %postfix-expression) |
| 250 |
(fold-right (^[op r] `(,op ,r)) main ops)) |
| 251 |
($binding ($. '&&) ;gcc label as value |
| 252 |
($: label %identifier) |
| 253 |
`(&& ,label)))) |
| 254 |
|
| 255 |
;; 6.5.4 Cast operators |
| 256 |
(define %cast-expression |
| 257 |
($lbinding ($: types ($many ($try ($between %LP %type-name %RP)))) |
| 258 |
($: main %unary-expression) |
| 259 |
(fold-right (^[type r] `(cast ,type ,r)) main types))) |
| 260 |
|
| 261 |
;; 6.5.5 Multiplicative operators |
| 262 |
(define %multiplicative-expression |
| 263 |
($infix-n %cast-expression 'left '(* / %))) |
| 264 |
|
| 265 |
;; 6.5.6 Additive operators |
| 266 |
(define %additive-expression |
| 267 |
($infix-n %multiplicative-expression 'left '(+ -))) |
| 268 |
|
| 269 |
;; 6.5.7 Bitwise shift operators |
| 270 |
(define %shift-expression |
| 271 |
($infix-n %additive-expression 'left '(<< >>))) |
| 272 |
|
| 273 |
;; 6.5.8 Relational operators |
| 274 |
(define %relational-expression |
| 275 |
($infix-n %shift-expression 'left '(< > <= >=))) |
| 276 |
|
| 277 |
;; 6.5.9 Equality expression |
| 278 |
(define %equality-expression |
| 279 |
($infix-n %relational-expression 'right '(== !=))) |
| 280 |
|
| 281 |
;; 6.5.10 Bitwise AND operator |
| 282 |
(define %and-expression |
| 283 |
($infix %equality-expression '& 'logand)) |
| 284 |
|
| 285 |
;; 6.5.11 Bitwise exclusive OR operator |
| 286 |
(define %exclusive-or-expression |
| 287 |
($infix %and-expression '^ 'logxor)) |
| 288 |
|
| 289 |
;; 6.5.12 Bitwise inclusive OR operator |
| 290 |
(define %inclusive-or-expression |
| 291 |
($infix %exclusive-or-expression '|\|| 'logior)) |
| 292 |
|
| 293 |
;; 6.5.13 Logical AND operator |
| 294 |
(define %logical-and-expression |
| 295 |
($infix %inclusive-or-expression '&& 'and)) |
| 296 |
|
| 297 |
;; 6.5.14 Logical OR operator |
| 298 |
(define %logical-or-expression |
| 299 |
($infix %logical-and-expression '|\|\|| 'or)) |
| 300 |
|
| 301 |
;; 6.5.15 Conditional operator |
| 302 |
(define %conditional-expression |
| 303 |
($lbinding ($: main %logical-or-expression) |
| 304 |
($optional ($seq ($. '?) |
| 305 |
($: then %expression) |
| 306 |
($. '|:|) |
| 307 |
($: else %conditional-expression))) |
| 308 |
(if (undefined? then) |
| 309 |
main |
| 310 |
`(?: ,main ,then ,else)))) |
| 311 |
|
| 312 |
;; 6.5.16 Assignment operators |
| 313 |
(define %assignment-expression |
| 314 |
($binding ($: uexprs ($many |
| 315 |
($try ($list %unary-expression |
| 316 |
($one-of '(= *= /= %= += -= |
| 317 |
<<= >>= &= ^= |\|=|)))))) |
| 318 |
($: main %conditional-expression) |
| 319 |
(fold-right (^[uexpr r] `(,(cadr uexpr) ,(car uexpr) ,r)) |
| 320 |
main uexprs))) |
| 321 |
|
| 322 |
;; 6.5.17 Comma operator |
| 323 |
(define %expression |
| 324 |
($binding ($: xs ($sep-by %assignment-expression ($. '|,|))) |
| 325 |
(match xs |
| 326 |
[(x) x] |
| 327 |
[xs `(comma ,@xs)]))) |
| 328 |
|
| 329 |
;; 6.6 Constant expression |
| 330 |
;; ... is syntactically equivalent as %conditional-expression, |
| 331 |
;; but all values are constant. We don't check it at the parser level. |
| 332 |
(define %constant-expression %conditional-expression) |
| 333 |
|
| 334 |
;; 6.7.1 |
| 335 |
(define-constant *storage-classes* '(typedef extern static auto register)) |
| 336 |
|
| 337 |
(define %storage-class-specifier ($one-of *storage-classes*)) |
| 338 |
|
| 339 |
;; 6.7.2 Type specifiers |
| 340 |
;; NB: Typedef-name is removed to be distinguished from identifiers. It is |
| 341 |
;; handled in declaration-specifier and type-specifier-qualifier-list. |
| 342 |
(define %type-specifier |
| 343 |
($lazy ($or ($one-of '(void char short int long float double signed unsigned |
| 344 |
_Bool _Complex |
| 345 |
;; gcc/clang extension |
| 346 |
__builtin_va_list |
| 347 |
;; gcc additional floating types |
| 348 |
__float128 _Float128 __float80 _Float64x __ibm128)) |
| 349 |
%struct-or-union-specifier |
| 350 |
%enum-specifier))) |
| 351 |
|
| 352 |
;; 6.7.3 Type qualifiers |
| 353 |
(define %type-qualifier |
| 354 |
($one-of '(const volatile restrict |
| 355 |
;; clang additional qualifiers |
| 356 |
_Nullable _Nonnull _Null_unspecified _Nullable_result))) |
| 357 |
|
| 358 |
;; This is a custom rule specially handle typedef-name. typedef-name and |
| 359 |
;; other type-specifiers are mutually exclusive. |
| 360 |
(define %type-specifier-qualifier-list |
| 361 |
($or ($try ($lbinding ($: pre ($many %type-qualifier)) |
| 362 |
($: type %typedef-name) |
| 363 |
($: post ($many %type-qualifier)) |
| 364 |
(append pre (list type) post))) |
| 365 |
($many1 ($or %type-qualifier %type-specifier)))) |
| 366 |
|
| 367 |
;; 6.7.2.1 Structure and union specifiers |
| 368 |
(define %struct-declarator |
| 369 |
($lbinding ($or ($seq ($: decl %declarator) |
| 370 |
($optional ($: bitfield ($seq ($. '|:|) |
| 371 |
%constant-expression)))) |
| 372 |
($: bitfield ($seq ($. '|:|) |
| 373 |
%constant-expression))) |
| 374 |
(if (undefined? bitfield) |
| 375 |
decl |
| 376 |
`(bitfield ,(if (undefined? decl) #f decl) ,bitfield)))) |
| 377 |
|
| 378 |
;; modified to handle typedef-name. see %type-specifier above. |
| 379 |
(define %struct-declaration |
| 380 |
($list* %type-specifier-qualifier-list |
| 381 |
($sep-by %struct-declarator ($. '|,|)))) |
| 382 |
|
| 383 |
(define %struct-members |
| 384 |
($between %LC ($end-by %struct-declaration ($. '|\;|)) %RC)) |
| 385 |
|
| 386 |
(define %struct-or-union-specifier |
| 387 |
($binding ($: key ($one-of '(struct union))) |
| 388 |
($or ($seq ($: tag %identifier) |
| 389 |
($optional ($: mem %struct-members))) |
| 390 |
($: mem %struct-members)) |
| 391 |
(=> fail) |
| 392 |
;; need to catch error from grok-c-type |
| 393 |
(guard (e [else (fail 'error (~ e'message))]) |
| 394 |
;; mem := (quals&spec decl ...) |
| 395 |
(let* ([tdict (typedef-names)] |
| 396 |
[member-decls |
| 397 |
(append-map |
| 398 |
(^m (map (cut grok-member-declaration (car m) <>) |
| 399 |
(cdr m))) |
| 400 |
(if (undefined? mem) '() mem))]) |
| 401 |
`(,key ,(if (undefined? tag) #f tag) |
| 402 |
,@member-decls))))) |
| 403 |
|
| 404 |
(define (grok-member-declaration specs decl) |
| 405 |
(match decl |
| 406 |
[('bitfield #f size) `(#f (int ()) ,size)] ;padding |
| 407 |
[('bitfield d size) (match-let1 (id sorage type init) |
| 408 |
(grok-declaration specs `(,d)) |
| 409 |
`(,id ,type ,size))] |
| 410 |
[d (match-let1 (id sorage type init) |
| 411 |
(grok-declaration specs `(,d)) |
| 412 |
`(,id ,type))])) |
| 413 |
|
| 414 |
;; 6.7.2.2 Enumeration specifiers |
| 415 |
(define %enumerator |
| 416 |
($binding ($: enum %identifier) |
| 417 |
($: init ($optional ($seq ($. '=) %constant-expression))) |
| 418 |
`(,(cadr enum) ,init))) |
| 419 |
|
| 420 |
(define %enumerators |
| 421 |
($between %LC ($sep-end-by %enumerator ($. '|,|)) %RC)) |
| 422 |
|
| 423 |
;; returns (enum <tag> ((<symbol> <init-expr>) ...)) |
| 424 |
(define %enum-specifier |
| 425 |
($binding ($. 'enum) |
| 426 |
($or ($seq ($: tag %identifier) |
| 427 |
($optional ($: lis %enumerators))) |
| 428 |
($: lis %enumerators)) |
| 429 |
`(enum ,(if (undefined? tag) #f tag) |
| 430 |
,@(if (undefined? lis) '() lis)))) |
| 431 |
|
| 432 |
;; 6.7.4 Function specifiers |
| 433 |
(define %function-specifier ($. 'inline)) |
| 434 |
|
| 435 |
;; 6.7.5 Declarators |
| 436 |
;; Actual type of the identifier can't be determined until we see the |
| 437 |
;; entire declaration. A declarator simply returns a list: |
| 438 |
;; (<identifier> <type-spec> ...) |
| 439 |
;; where each type-spec can be |
| 440 |
;; (* qualifier ...) |
| 441 |
;; (array-dimension (qualifier ...) dim-expr) |
| 442 |
;; (function params) ; params := (param-spec ...) | no-args | unknown-args |
| 443 |
;; This is a transient information. Declaration constructs a complete |
| 444 |
;; type. |
| 445 |
(define %declarator |
| 446 |
($lbinding ($: ptr ($many %pointer)) |
| 447 |
($: main ($or %identifier |
| 448 |
($between %LP %declarator %RP))) |
| 449 |
($: post ($many ($or %array-decl-suffix |
| 450 |
%function-decl-suffix |
| 451 |
%asm-decl-suffix |
| 452 |
%attribute-specifier))) |
| 453 |
(match main |
| 454 |
[('ident id) `((ident ,id) ,@post ,@ptr)] |
| 455 |
[(('ident id) . specs) `((ident ,id) ,@ptr ,@specs ,@post)] |
| 456 |
[else (errorf "something wrong with %declarator. main=~s" |
| 457 |
main)]))) |
| 458 |
|
| 459 |
(define %pointer |
| 460 |
($lift cons ($. '*) ($many ($or %type-qualifier %attribute-specifier)))) |
| 461 |
|
| 462 |
(define %array-decl-suffix |
| 463 |
($binding %LB |
| 464 |
($: quals ($many ($or ($. 'static) |
| 465 |
%type-qualifier |
| 466 |
%attribute-specifier))) |
| 467 |
($: assign ($optional ($or ($. '*) |
| 468 |
%assignment-expression))) |
| 469 |
%RB |
| 470 |
`(array-dimension ,quals ,assign))) |
| 471 |
|
| 472 |
;; For parameter declaration, we only return (<name> <type>), |
| 473 |
;; where <name> can be an ident or #f. |
| 474 |
(define %parameter-declaration |
| 475 |
($lbinding ($: specs %declaration-specifiers) |
| 476 |
($: decl ($or ($try %declarator) |
| 477 |
($optional %abstract-declarator))) |
| 478 |
(=> fail) |
| 479 |
;; need to catch error from grok-c-type |
| 480 |
(guard (e [else (fail 'error (~ e'message))]) |
| 481 |
(match-let1 (id storage type init) |
| 482 |
(if decl |
| 483 |
(grok-declaration specs `(,decl)) |
| 484 |
(grok-declaration specs '((#f)))) |
| 485 |
`(,id ,type))))) |
| 486 |
|
| 487 |
(define %parameter-type-list |
| 488 |
;; NB: We require at least one %parameter-declaration, for the empty parameter |
| 489 |
;; case will be handled by the next branch in %function-decl-suffix. |
| 490 |
($binding ($: params ($sep-by ($or %parameter-declaration |
| 491 |
($. '...)) |
| 492 |
($. '|,|) |
| 493 |
1)) |
| 494 |
(=> fail) |
| 495 |
(or (and-let* ([p (memq '... params)] |
| 496 |
[ (not (null? (cdr p))) ]) |
| 497 |
(fail 'error "'...' appear in middle of parameter list")) |
| 498 |
(and (member '(#f (void ())) params) |
| 499 |
(if (length=? params 1) |
| 500 |
'no-args ; (void) means no parameters |
| 501 |
(fail 'error "void in parameter list must appear by itself"))) |
| 502 |
params))) |
| 503 |
|
| 504 |
(define %function-decl-suffix |
| 505 |
($binding %LP |
| 506 |
($: params ($or ($try %parameter-type-list) |
| 507 |
($sep-by %identifier ($. '|,|)))) |
| 508 |
%RP |
| 509 |
(if (null? params) |
| 510 |
`(function unknown-args) |
| 511 |
`(function ,params)))) |
| 512 |
|
| 513 |
;; 6.7.7 Type definitions |
| 514 |
;; The semantic value of typedef name is the same as identifier. |
| 515 |
(define %typedef-name |
| 516 |
($try ($binding ($: id %identifier) |
| 517 |
(=> fail) |
| 518 |
(match-let1 ('ident x) id |
| 519 |
(if (dict-exists? (typedef-names) x) |
| 520 |
`(ident ,x) |
| 521 |
(fail "typedef name")))))) |
| 522 |
|
| 523 |
;; 6.7 Declarations |
| 524 |
;; Returns (decl (identifier storage-class type init) ...) |
| 525 |
;; The type part is constructed by grok-declaration |
| 526 |
(define %declaration |
| 527 |
($lbinding ($: specs %declaration-specifiers) |
| 528 |
($: declarators ($sep-by %init-declarator ($. '|,|))) |
| 529 |
%attribute-specifier-list ;ignore for now |
| 530 |
($. '|\;|) |
| 531 |
(=> fail) |
| 532 |
;; need to catch error from grok-c-type |
| 533 |
(guard (e [else (fail 'error (~ e'message))]) |
| 534 |
(let ([decls (map (cut grok-declaration specs <>) declarators)]) |
| 535 |
(when (memq 'typedef specs) |
| 536 |
(map (^d (register-typedefs! specs d)) decls)) |
| 537 |
`(decl ,@decls))))) |
| 538 |
|
| 539 |
(define (register-typedefs! specs decl) |
| 540 |
(match decl |
| 541 |
[((? symbol? typename) 'typedef type _) |
| 542 |
(dict-put! (typedef-names) typename type)] |
| 543 |
[else |
| 544 |
(error "something wrong with typedef decl" decl)])) |
| 545 |
|
| 546 |
(define (grok-declaration specs decl) |
| 547 |
(define (build-decl id type init) |
| 548 |
(let* ([sc (find (cut memq <> *storage-classes*) specs)] |
| 549 |
[ty (delete sc specs)]) |
| 550 |
`(,id ,sc ,(grok-c-type `(,@type ,ty) (typedef-names)) ,init))) |
| 551 |
(match decl |
| 552 |
[((('ident id) . type) init) (build-decl id type init)] |
| 553 |
[((('ident id) . type)) (build-decl id type #f)] |
| 554 |
[((#f . type)) (build-decl #f type #f)])) ; abstract-decl |
| 555 |
|
| 556 |
;; modified to handle typedef-name. see %type-specifier above. |
| 557 |
(define %declaration-specifiers |
| 558 |
($or ($try ($binding ($: pre ($many ($or %storage-class-specifier |
| 559 |
%type-qualifier |
| 560 |
%function-specifier |
| 561 |
%attribute-specifier))) |
| 562 |
($: type %typedef-name) |
| 563 |
($: post ($many ($or %storage-class-specifier |
| 564 |
%type-qualifier |
| 565 |
%function-specifier |
| 566 |
%attribute-specifier))) |
| 567 |
(append pre (list type) post))) |
| 568 |
($many1 ($or %storage-class-specifier |
| 569 |
%type-specifier |
| 570 |
%type-qualifier |
| 571 |
%function-specifier |
| 572 |
%attribute-specifier)))) |
| 573 |
|
| 574 |
(define %init-declarator |
| 575 |
($lbinding ($: decl %declarator) |
| 576 |
($: init ($optional ($seq ($. '=) %initializer))) |
| 577 |
(if init |
| 578 |
`(,decl ,init) |
| 579 |
`(,decl)))) |
| 580 |
|
| 581 |
;; 6.7.6 Type names |
| 582 |
|
| 583 |
;; Value of %abstract-declarator is similar to %declarator, except that |
| 584 |
;; it has #f in place of the identifier. It's an intermediate value and |
| 585 |
;; eventually parsed by grok-declaration. |
| 586 |
;; (#f <type-spec> ...) |
| 587 |
(define %abstract-declarator |
| 588 |
($lbinding ($: ptr ($many %pointer)) |
| 589 |
($: main ($optional ($between %LP %abstract-declarator %RP))) |
| 590 |
($: suff ($many ($or ($try ($between %LB ($. '*) %RB)) |
| 591 |
%array-decl-suffix |
| 592 |
%function-decl-suffix))) |
| 593 |
(if main |
| 594 |
(let ((main-specs (cdr main))) |
| 595 |
`(#f ,@suff ,@ptr ,@main-specs)) |
| 596 |
`(#f ,@suff ,@ptr)))) |
| 597 |
|
| 598 |
(define %type-name |
| 599 |
($binding ($: specs %type-specifier-qualifier-list) |
| 600 |
($: decl ($optional %abstract-declarator)) |
| 601 |
(list specs decl))) |
| 602 |
|
| 603 |
;; 6.7.8 Initialization |
| 604 |
(define %designator |
| 605 |
($or ($lift (^e `(aref ,e)) ($between %LB %constant-expression %RB)) |
| 606 |
($lift (^e `(ref ,e)) ($seq ($. '|.|) %identifier)))) |
| 607 |
|
| 608 |
(define %designation |
| 609 |
($seq0 ($many %designator) ($. '=))) |
| 610 |
|
| 611 |
(define %initializer-list |
| 612 |
($lazy |
| 613 |
($sep-end-by ($lift (^[desig init] `(initializer ,init ,desig)) |
| 614 |
($optional %designation) %initializer) |
| 615 |
($. '|,|)))) |
| 616 |
|
| 617 |
(define %initializer |
| 618 |
($or %assignment-expression |
| 619 |
($between %LC %initializer-list %RC))) |
| 620 |
|
| 621 |
;; 6,8 Statements and blocks |
| 622 |
;; we need $try for %labeled-statement, for it may consume the first |
| 623 |
;; identifier before failing. |
| 624 |
(define %statement |
| 625 |
($lazy ($cut ($or %compound-statement |
| 626 |
%selection-statement |
| 627 |
%iteration-statement |
| 628 |
%jump-statement |
| 629 |
%asm-statement ; gcc specific |
| 630 |
($try %labeled-statement) |
| 631 |
%expression-statement)))) |
| 632 |
|
| 633 |
;; 6.8.1 Labeled statement |
| 634 |
(define %labeled-statement |
| 635 |
($or ($binding ($. 'case) ($: expr %constant-expression) ($. ':) |
| 636 |
($: stmt %statement) |
| 637 |
`(case ,expr ,stmt)) |
| 638 |
($binding ($. 'default) ($. ':) ($: stmt %statement) |
| 639 |
`(default ,stmt)) |
| 640 |
($binding ($: label %identifier) ($. ':) ($: stmt %statement) |
| 641 |
`(label ,label ,stmt)))) |
| 642 |
|
| 643 |
;; 6.8.2 Compound statement |
| 644 |
(define %compound-statement |
| 645 |
($binding %LC |
| 646 |
($with-scope |
| 647 |
($: stmts ($many ($or %declaration %statement)))) |
| 648 |
%RC |
| 649 |
`(begin ,stmts))) |
| 650 |
|
| 651 |
;; 6.8.3 Expression and null statements |
| 652 |
(define %expression-statement |
| 653 |
($binding ($: expr ($optional %expression)) |
| 654 |
%attribute-specifier-list ;for now, we ignore this |
| 655 |
($. '|\;|) |
| 656 |
(or expr '(begin)))) |
| 657 |
|
| 658 |
;; 6.8.4 Selection statements |
| 659 |
(define %selection-statement |
| 660 |
($or ($binding ($. 'if) %LP ($: test %expression) %RP ($: then %statement) |
| 661 |
($optional ($seq ($. 'else) ($: else %statement))) |
| 662 |
(if (undefined? else) |
| 663 |
`(if ,test ,then) |
| 664 |
`(if ,test ,then ,else))) |
| 665 |
($binding ($. 'switch) %LP ($: test %expression) %RP ($: stmt %statement) |
| 666 |
`(switch ,test ,stmt)))) |
| 667 |
|
| 668 |
;; 6.8.5 Iteration statements |
| 669 |
(define %iteration-statement |
| 670 |
($or ($binding ($. 'while) %LP ($: test %expression) %RP ($: body %statement) |
| 671 |
`(while ,test ,body)) |
| 672 |
($binding ($. 'do) ($: body %statement) |
| 673 |
($. 'while) %LP ($: test %expression) %RP ($. '|\;|) |
| 674 |
`(do-while ,test ,body)) |
| 675 |
($binding ($. 'for) %LP |
| 676 |
($or ($: decl %declaration) |
| 677 |
($seq ($: init ($optional %expression)) |
| 678 |
($. '|\;|))) |
| 679 |
($: test ($optional %expression)) |
| 680 |
($. '|\;|) |
| 681 |
($: update ($optional %expression)) |
| 682 |
%RP |
| 683 |
($: body %statement) |
| 684 |
(if (undefined? init) |
| 685 |
`(for (,decl ,test ,update) ,body) |
| 686 |
`(for (,init ,test ,update) ,body))))) |
| 687 |
|
| 688 |
;; 6.8.6 Jump statement |
| 689 |
(define %jump-statement |
| 690 |
($or ($binding ($. 'goto) |
| 691 |
($: dest ($or %identifier |
| 692 |
($list ($. '*) %expression))) ;gcc computed goto |
| 693 |
($. '|\;|) |
| 694 |
`(goto ,dest)) |
| 695 |
($seq ($. 'continue) ($. '|\;|) ($return 'continue)) |
| 696 |
($seq ($. 'break) ($. '|\;|) ($return 'break)) |
| 697 |
($binding ($. 'return) ($: expr ($optional %expression)) ($. '|\;|) |
| 698 |
`(return ,@(if expr `(,expr) '()))))) |
| 699 |
|
| 700 |
;; 6.9 External definitions |
| 701 |
(define %external-declaration |
| 702 |
($lazy ($or ($try %function-definition) %declaration))) |
| 703 |
|
| 704 |
(define %translation-unit ($many1 %external-declaration)) |
| 705 |
|
| 706 |
;; 6.9.1 Function definitions |
| 707 |
(define %function-definition |
| 708 |
($binding ($: spec %declaration-specifiers) |
| 709 |
($: decl %declarator) |
| 710 |
($: lis ($many %declaration)) |
| 711 |
($assert ($. #\{)) |
| 712 |
($: body ($cut %compound-statement)) |
| 713 |
`(,spec ,decl ,lis ,body))) |
| 714 |
|
| 715 |
;;; |
| 716 |
;;; Preprocessor |
| 717 |
;;; |
| 718 |
|
| 719 |
;; NB: We may eventually implement our own preprocessor, for we can do |
| 720 |
;; more things (such as caching), but for the time being, we call cpp. |
| 721 |
;; |
| 722 |
;; gcc exits with non-zero status if output isn't fully read, which happens |
| 723 |
;; when PROC throws an error. We don't want to lose that error, so we set |
| 724 |
;; :on-abnormal-exit to :ignore. |
| 725 |
;; |
| 726 |
;; INCLUDES is a list of include paths, or #f to use the system default |
| 727 |
;; (gauche-config --incdirs). DEFS is an list of (var val ...) which will |
| 728 |
;; be used as -Dvar=val -Dvar=val ..., or just var for -Dvar. |
| 729 |
|
| 730 |
(define (call-with-cpp file proc |
| 731 |
:key (includes #f) |
| 732 |
(defs '())) |
| 733 |
(define Is |
| 734 |
(cond [(list? includes) |
| 735 |
(map (^d (shell-escape-string #"-I~d")) includes)] |
| 736 |
[(not includes) |
| 737 |
(map (^d #"-I~d") |
| 738 |
(string-split (gauche-config "--incdirs") #\:))] |
| 739 |
[else (error "includes keyword arg must be a list of \ |
| 740 |
directories or #f, but got:" includes)])) |
| 741 |
(define Ds |
| 742 |
(append-map (^[def] |
| 743 |
(match def |
| 744 |
[(var val ...) |
| 745 |
(map (^[val] (shell-escape-string #"-D~|var|=~val")) val)] |
| 746 |
[var `(,#"-D~var")])) |
| 747 |
defs)) |
| 748 |
(call-with-input-process `(,(gauche-config "--cc") "-E" ,@Is ,@Ds ,file) |
| 749 |
proc |
| 750 |
:on-abnormal-exit :ignore)) |
| 751 |
|
| 752 |
;;; |
| 753 |
;;; Driver |
| 754 |
;;; |
| 755 |
|
| 756 |
;; For testing - won't be official APIs. |
| 757 |
|
| 758 |
(define (file-check file) |
| 759 |
(unless (file-is-readable? file) |
| 760 |
(error "file does not exist or unreadable:" file))) |
| 761 |
|
| 762 |
(define (c-tokenize-file file) |
| 763 |
(file-check file) |
| 764 |
(call-with-cpp file |
| 765 |
(^p ($ lseq->list $ c-tokenize |
| 766 |
$ port->char-lseq/position p |
| 767 |
:source-name file :line-adjusters `((#\# . ,cc1-line-adjuster)))) |
| 768 |
:includes (cpp-include-paths) |
| 769 |
:defs (cpp-definitions))) |
| 770 |
|
| 771 |
(define (c-tokenize-file-coroutine file) |
| 772 |
(^[yield] |
| 773 |
($ call-with-cpp file |
| 774 |
(^p |
| 775 |
(let loop ([tokens ($ c-tokenize |
| 776 |
$ port->char-lseq/position p |
| 777 |
:source-name file |
| 778 |
:line-adjusters `((#\# . ,cc1-line-adjuster)))]) |
| 779 |
(unless (null? tokens) |
| 780 |
(yield (car tokens) |
| 781 |
(pair-attributes tokens)) |
| 782 |
(loop (cdr tokens))))) |
| 783 |
:includes (cpp-include-paths) |
| 784 |
:defs (cpp-definitions)))) |
| 785 |
|
| 786 |
(define (c-parse-file file :optional (parser %translation-unit)) |
| 787 |
(file-check file) |
| 788 |
(parameterize ((typedef-names (default-typedefs))) |
| 789 |
(if (use-concurrent-lexer) |
| 790 |
(peg-run-parser parser (coroutine->cseq (c-tokenize-file-coroutine file))) |
| 791 |
(peg-run-parser parser (c-tokenize-file file))))) |
| 792 |
|
| 793 |
(define (%operate-on-string string proc) |
| 794 |
(let1 f (format "x~8,'0x.c" (receive (s us) (sys-gettimeofday) |
| 795 |
(modulo (* s us) 99999989))) |
| 796 |
(with-output-to-file f (cut display string)) |
| 797 |
(unwind-protect |
| 798 |
(proc f) |
| 799 |
(sys-remove f)))) |
| 800 |
|
| 801 |
(define (c-tokenize-string string) |
| 802 |
(%operate-on-string string c-tokenize-file)) |
| 803 |
|
| 804 |
(define (c-parse-string string :optional (parser %translation-unit)) |
| 805 |
(%operate-on-string string (^f (c-parse-file f parser)))) |