Debian Codegraph @

c2hs/c/CParser.y

raw
1 -- C -> Haskell Compiler: Parser for C Header Files
2 --
3 -- Author : Duncan Coutts, Manuel M T Chakravarty
4 -- Created: 29 May 2005
5 --
6 -- Copyright (c) 2005-2007 Duncan Coutts
7 -- Copyright (c) [1999..2004] Manuel M T Chakravarty
8 -- Portions Copyright (c) 1989, 1990 James A. Roskind
9 --
10 -- This file is free software; you can redistribute it and/or modify
11 -- it under the terms of the GNU General Public License as published by
12 -- the Free Software Foundation; either version 2 of the License, or
13 -- (at your option) any later version.
14 --
15 -- This file is distributed in the hope that it will be useful,
16 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
17 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 -- GNU General Public License for more details.
19 --
20 --- DESCRIPTION ---------------------------------------------------------------
21 --
22 -- Parser for C translation units, which have already been run through the C
23 -- preprocessor.
24 --
25 --- DOCU ----------------------------------------------------------------------
26 --
27 -- language: Haskell 98
28 --
29 -- The parser recognizes all of ISO C 99 and most common GNU C extensions.
30 --
31 -- With C99 we refer to the ISO C99 standard, specifically the section numbers
32 -- used below refer to this report:
33 --
34 -- http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1124.pdf
35 --
36 --
37 -- Since some of the grammar productions are quite difficult to read
38 -- (especially those involved with the declaration syntax) we document them
39 -- with an extended syntax that allows a more consise representation:
40 --
41 -- Ordinary rules
42 --
43 -- foo named terminal or non-terminal
44 --
45 -- 'c' terminal, literal character token
46 --
47 -- A B concatenation
48 --
49 -- A | B alternation
50 --
51 -- (A) grouping
52 --
53 -- Extended rules
54 --
55 -- A? optional, short hand for (A|) or [A]{ 0==A || 1==A }
56 --
57 -- ... stands for some part of the grammar omitted for clarity
58 --
59 -- [A] represents sequences, 0 or more.
60 --
61 -- [A]{C} sequences with some constraint, usually on the number of
62 -- terminals or non-terminals appearing in the sequence.
63 --
64 -- Constraints on sequences
65 --
66 -- n==t terminal or non-terminal t must appear exactly n times
67 --
68 -- n>=t terminal or non-terminal t must appear at least n times
69 --
70 -- C1 && C1 conjunction of constraints
71 --
72 -- C1 || C2 disjunction of constraints
73 --
74 -- C1 |x| C2 exclusive disjunction of constraints
75 --
76 --
77 -- Comments:
78 --
79 -- * Subtrees representing empty declarators of the form `CVarDeclr Nothing
80 -- at' have *no* valid attribute handle in `at' (only a `newAttrsOnlyPos
81 -- nopos').
82 --
83 -- * Builtin type names are imported from `CBuiltin'.
84 --
85 --- TODO ----------------------------------------------------------------------
86 --
87 -- * GNUC __attribute__s should be entered into the parse tree since they
88 -- contain useful api/abi information.
89 --
90 -- * Some other extensions are currently recognised by the parser but not
91 -- entered into the parse tree.
92 --
93
94 {
95 {-# LANGUAGE BangPatterns #-}
96 {-# OPTIONS_GHC -w #-}
97 module CParser (parseC) where
98
99 import Prelude hiding (reverse)
100 import qualified Data.List as List
101
102 import Position (Position, Pos(..), nopos)
103 import UNames (names)
104 import Idents (Ident)
105 import Attributes (Attrs, newAttrs, newAttrsOnlyPos)
106
107 import State (PreCST, raiseFatal, getNameSupply)
108 import CLexer (lexC, parseError)
109 import CAST (CHeader(..), CExtDecl(..), CFunDef(..), CStat(..),
110 CBlockItem(..), CDecl(..), CDeclSpec(..), CStorageSpec(..),
111 CTypeSpec(..), CTypeQual(..), CStructUnion(..),
112 CStructTag(..), CEnum(..), CDeclr(..), CInit(..), CInitList,
113 CDesignator(..), CExpr(..), CAssignOp(..), CBinaryOp(..),
114 CUnaryOp(..), CConst (..))
115 import CBuiltin (builtinTypeNames)
116 import CTokens (CToken(..), GnuCTok(..))
117 import CParserMonad (P, execParser, getNewName, addTypedef, shadowTypedef,
118 enterScope, leaveScope )
119 }
120
121 %name header header
122 %tokentype { CToken }
123
124 %monad { P } { >>= } { return }
125 %lexer { lexC } { CTokEof }
126
127 %expect 1
128
129 %token
130
131 '(' { CTokLParen _ }
132 ')' { CTokRParen _ }
133 '[' { CTokLBracket _ }
134 ']' { CTokRBracket _ }
135 "->" { CTokArrow _ }
136 '.' { CTokDot _ }
137 '!' { CTokExclam _ }
138 '~' { CTokTilde _ }
139 "++" { CTokInc _ }
140 "--" { CTokDec _ }
141 '+' { CTokPlus _ }
142 '-' { CTokMinus _ }
143 '*' { CTokStar _ }
144 '/' { CTokSlash _ }
145 '%' { CTokPercent _ }
146 '&' { CTokAmper _ }
147 "<<" { CTokShiftL _ }
148 ">>" { CTokShiftR _ }
149 '<' { CTokLess _ }
150 "<=" { CTokLessEq _ }
151 '>' { CTokHigh _ }
152 ">=" { CTokHighEq _ }
153 "==" { CTokEqual _ }
154 "!=" { CTokUnequal _ }
155 '^' { CTokHat _ }
156 '|' { CTokBar _ }
157 "&&" { CTokAnd _ }
158 "||" { CTokOr _ }
159 '?' { CTokQuest _ }
160 ':' { CTokColon _ }
161 '=' { CTokAssign _ }
162 "+=" { CTokPlusAss _ }
163 "-=" { CTokMinusAss _ }
164 "*=" { CTokStarAss _ }
165 "/=" { CTokSlashAss _ }
166 "%=" { CTokPercAss _ }
167 "&=" { CTokAmpAss _ }
168 "^=" { CTokHatAss _ }
169 "|=" { CTokBarAss _ }
170 "<<=" { CTokSLAss _ }
171 ">>=" { CTokSRAss _ }
172 ',' { CTokComma _ }
173 ';' { CTokSemic _ }
174 '{' { CTokLBrace _ }
175 '}' { CTokRBrace _ }
176 "..." { CTokEllipsis _ }
177 alignof { CTokAlignof _ }
178 asm { CTokAsm _ }
179 auto { CTokAuto _ }
180 break { CTokBreak _ }
181 "_Bool" { CTokBool _ }
182 case { CTokCase _ }
183 char { CTokChar _ }
184 const { CTokConst _ }
185 continue { CTokContinue _ }
186 "_Complex" { CTokComplex _ }
187 default { CTokDefault _ }
188 do { CTokDo _ }
189 double { CTokDouble _ }
190 else { CTokElse _ }
191 enum { CTokEnum _ }
192 extern { CTokExtern _ }
193 float { CTokFloat _ }
194 "__float128" { CTokFloat128 _ }
195 for { CTokFor _ }
196 goto { CTokGoto _ }
197 if { CTokIf _ }
198 inline { CTokInline _ }
199 int { CTokInt _ }
200 long { CTokLong _ }
201 "__label__" { CTokLabel _ }
202 register { CTokRegister _ }
203 restrict { CTokRestrict _ }
204 return { CTokReturn _ }
205 short { CTokShort _ }
206 signed { CTokSigned _ }
207 sizeof { CTokSizeof _ }
208 static { CTokStatic _ }
209 struct { CTokStruct _ }
210 switch { CTokSwitch _ }
211 typedef { CTokTypedef _ }
212 typeof { CTokTypeof _ }
213 "__thread" { CTokThread _ }
214 union { CTokUnion _ }
215 unsigned { CTokUnsigned _ }
216 void { CTokVoid _ }
217 volatile { CTokVolatile _ }
218 while { CTokWhile _ }
219 cchar { CTokCLit _ _ } -- character constant
220 cint { CTokILit _ _ } -- integer constant
221 cfloat { CTokFLit _ _ } -- float constant
222 cstr { CTokSLit _ _ } -- string constant (no escapes)
223 ident { CTokIdent _ $$ } -- identifier
224 tyident { CTokTyIdent _ $$ } -- `typedef-name' identifier
225 "__attribute__" { CTokGnuC GnuCAttrTok _ } -- special GNU C tokens
226 "__extension__" { CTokGnuC GnuCExtTok _ } -- special GNU C tokens
227
228 -- special GNU C builtin 'functions' that actually take types as parameters:
229 "__builtin_va_arg" { CTokGnuC GnuCVaArg _ }
230 "__builtin_offsetof" { CTokGnuC GnuCOffsetof _ }
231 "__builtin_types_compatible_p" { CTokGnuC GnuCTyCompat _ }
232
233 %%
234
235
236 -- parse a complete C header file
237 --
238 header :: { CHeader }
239 header
240 : translation_unit {% withAttrs $1 $ CHeader (reverse $1) }
241
242
243 -- parse a complete C translation unit (C99 6.9)
244 --
245 -- * GNU extensions:
246 -- allow empty translation_unit
247 -- allow redundant ';'
248 --
249 translation_unit :: { Reversed [CExtDecl] }
250 translation_unit
251 : {- empty -} { empty }
252 | translation_unit ';' { $1 }
253 | translation_unit external_declaration { $1 `snoc` $2 }
254
255
256 -- parse external C declaration (C99 6.9)
257 --
258 -- * GNU extensions:
259 -- allow extension keyword before external declaration
260 -- asm definitions
261 --
262 external_declaration :: { CExtDecl }
263 external_declaration
264 : attrs_opt function_definition { CFDefExt $2 }
265 | attrs_opt declaration { CDeclExt $2 }
266 | "__extension__" external_declaration { $2 }
267 | asm '(' string_literal ')' ';' {% withAttrs $2 CAsmExt }
268
269
270 -- parse C function definition (C99 6.9.1)
271 --
272 function_definition :: { CFunDef }
273 function_definition
274 : function_declarator compound_statement
275 {% leaveScope >> (withAttrs $1 $ CFunDef [] $1 [] $2) }
276
277 | declaration_specifier function_declarator compound_statement
278 {% leaveScope >> (withAttrs $1 $ CFunDef $1 $2 [] $3) }
279
280 | type_specifier function_declarator compound_statement
281 {% leaveScope >> (withAttrs $1 $ CFunDef $1 $2 [] $3) }
282
283 | declaration_qualifier_list function_declarator compound_statement
284 {% leaveScope >> (withAttrs $1 $ CFunDef (reverse $1) $2 [] $3) }
285
286 | type_qualifier_list function_declarator compound_statement
287 {% leaveScope >> (withAttrs $1 $ CFunDef (liftTypeQuals $1) $2 [] $3) }
288
289 | old_function_declarator declaration_list compound_statement
290 {% withAttrs $1 $ CFunDef [] $1 (reverse $2) $3 }
291
292 | declaration_specifier old_function_declarator declaration_list compound_statement
293 {% withAttrs $1 $ CFunDef $1 $2 (reverse $3) $4 }
294
295 | type_specifier old_function_declarator declaration_list compound_statement
296 {% withAttrs $1 $ CFunDef $1 $2 (reverse $3) $4 }
297
298 | declaration_qualifier_list old_function_declarator declaration_list compound_statement
299 {% withAttrs $1 $ CFunDef (reverse $1) $2 (reverse $3) $4 }
300
301 | type_qualifier_list old_function_declarator declaration_list compound_statement
302 {% withAttrs $1 $ CFunDef (liftTypeQuals $1) $2 (reverse $3) $4 }
303
304
305 function_declarator :: { CDeclr }
306 function_declarator
307 : identifier_declarator
308 {% enterScope >> doFuncParamDeclIdent $1 >> return $1 }
309
310
311 declaration_list :: { Reversed [CDecl] }
312 declaration_list
313 : {- empty -} { empty }
314 | declaration_list declaration { $1 `snoc` $2 }
315
316
317 -- parse C statement (C99 6.8)
318 --
319 -- * GNU extension: ' __asm__ (...); ' statements
320 --
321 statement :: { CStat }
322 statement
323 : labeled_statement { $1 }
324 | compound_statement { $1 }
325 | expression_statement { $1 }
326 | selection_statement { $1 }
327 | iteration_statement { $1 }
328 | jump_statement { $1 }
329 | asm_statement { $1 }
330
331
332 -- parse C labeled statement (C99 6.8.1)
333 --
334 -- * GNU extension: case ranges
335 --
336 labeled_statement :: { CStat }
337 labeled_statement
338 : identifier ':' attrs_opt statement {% withAttrs $2 $ CLabel $1 $4}
339 | case constant_expression ':' statement {% withAttrs $1 $ CCase $2 $4 }
340 | default ':' statement {% withAttrs $1 $ CDefault $3 }
341 | case constant_expression "..." constant_expression ':' statement
342 {% withAttrs $1 $ CCases $2 $4 $6 }
343
344
345 -- parse C compound statement (C99 6.8.2)
346 --
347 -- * GNU extension: '__label__ ident;' declarations
348 --
349 compound_statement :: { CStat }
350 compound_statement
351 : '{' enter_scope block_item_list leave_scope '}'
352 {% withAttrs $1 $ CCompound (reverse $3) }
353
354 | '{' enter_scope label_declarations block_item_list leave_scope '}'
355 {% withAttrs $1 $ CCompound (reverse $4) }
356
357
358 -- No syntax for these, just side effecting semantic actions.
359 --
360 enter_scope :: { () }
361 enter_scope : {% enterScope }
362 leave_scope :: { () }
363 leave_scope : {% leaveScope }
364
365
366 block_item_list :: { Reversed [CBlockItem] }
367 block_item_list
368 : {- empty -} { empty }
369 | block_item_list block_item { $1 `snoc` $2 }
370
371
372 block_item :: { CBlockItem }
373 block_item
374 : statement { CBlockStmt $1 }
375 | nested_declaration { $1 }
376
377
378 nested_declaration :: { CBlockItem }
379 nested_declaration
380 : declaration { CBlockDecl $1 }
381 | attrs declaration { CBlockDecl $2 }
382 | nested_function_definition { CNestedFunDef $1 }
383 | attrs nested_function_definition { CNestedFunDef $2 }
384 | "__extension__" nested_declaration { $2 }
385
386
387 nested_function_definition :: { CFunDef }
388 nested_function_definition
389 : declaration_specifier function_declarator compound_statement
390 {% leaveScope >> (withAttrs $1 $ CFunDef $1 $2 [] $3) }
391
392 | type_specifier function_declarator compound_statement
393 {% leaveScope >> (withAttrs $1 $ CFunDef $1 $2 [] $3) }
394
395 | declaration_qualifier_list function_declarator compound_statement
396 {% leaveScope >> (withAttrs $1 $ CFunDef (reverse $1) $2 [] $3) }
397
398 | type_qualifier_list function_declarator compound_statement
399 {% leaveScope >> (withAttrs $1 $ CFunDef (liftTypeQuals $1) $2 [] $3) }
400
401
402 label_declarations :: { () }
403 label_declarations
404 : "__label__" identifier_list ';' { () }
405 | label_declarations "__label__" identifier_list ';' { () }
406
407
408 -- parse C expression statement (C99 6.8.3)
409 --
410 expression_statement :: { CStat }
411 expression_statement
412 : ';' {% withAttrs $1 $ CExpr Nothing }
413 | expression ';' {% withAttrs $1 $ CExpr (Just $1) }
414
415
416 -- parse C selection statement (C99 6.8.4)
417 --
418 selection_statement :: { CStat }
419 selection_statement
420 : if '(' expression ')' statement
421 {% withAttrs $1 $ CIf $3 $5 Nothing }
422
423 | if '(' expression ')' statement else statement
424 {% withAttrs $1 $ CIf $3 $5 (Just $7) }
425
426 | switch '(' expression ')' statement
427 {% withAttrs $1 $ CSwitch $3 $5 }
428
429
430 -- parse C iteration statement (C99 6.8.5)
431 --
432 iteration_statement :: { CStat }
433 iteration_statement
434 : while '(' expression ')' statement
435 {% withAttrs $1 $ CWhile $3 $5 False }
436
437 | do statement while '(' expression ')' ';'
438 {% withAttrs $1 $ CWhile $5 $2 True }
439
440 | for '(' expression_opt ';' expression_opt ';' expression_opt ')' statement
441 {% withAttrs $1 $ CFor (Left $3) $5 $7 $9 }
442
443 | for '(' enter_scope declaration expression_opt ';' expression_opt ')' statement leave_scope
444 {% withAttrs $1 $ CFor (Right $4) $5 $7 $9 }
445
446
447 -- parse C jump statement (C99 6.8.6)
448 --
449 -- * GNU extension: computed gotos
450 --
451 jump_statement :: { CStat }
452 jump_statement
453 : goto identifier ';' {% withAttrs $1 $ CGoto $2 }
454 | goto '*' expression ';' {% withAttrs $1 $ CGotoPtr $3 }
455 | continue ';' {% withAttrs $1 $ CCont }
456 | break ';' {% withAttrs $1 $ CBreak }
457 | return expression_opt ';' {% withAttrs $1 $ CReturn $2 }
458
459
460 -- parse GNU C __asm__ (...) statement (recording only a place holder result)
461 --
462 asm_statement :: { CStat }
463 asm_statement
464 : asm maybe_type_qualifier '(' expression ')' ';'
465 {% withAttrs $1 CAsm }
466
467 | asm maybe_type_qualifier '(' expression ':' asm_operands ')' ';'
468 {% withAttrs $1 CAsm }
469
470 | asm maybe_type_qualifier '(' expression ':' asm_operands
471 ':' asm_operands ')' ';'
472 {% withAttrs $1 CAsm }
473 | asm maybe_type_qualifier '(' expression ':' asm_operands ':' asm_operands
474 ':' asm_clobbers ')' ';'
475 {% withAttrs $1 CAsm }
476
477
478 maybe_type_qualifier :: { () }
479 maybe_type_qualifier
480 : {- empty -} { () }
481 | type_qualifier { () }
482
483
484 asm_operands :: { () }
485 asm_operands
486 : {- empty -} { () }
487 | nonnull_asm_operands { () }
488
489
490 nonnull_asm_operands :: { () }
491 nonnull_asm_operands
492 : asm_operand { () }
493 | nonnull_asm_operands ',' asm_operand { () }
494
495
496 asm_operand :: { () }
497 asm_operand
498 : string_literal '(' expression ')' { () }
499 | '[' ident ']' string_literal '(' expression ')' { () }
500 | '[' tyident ']' string_literal '(' expression ')' { () }
501
502
503 asm_clobbers :: { () }
504 asm_clobbers
505 : string_literal { () }
506 | asm_clobbers ',' string_literal { () }
507
508
509 -- parse C declaration (C99 6.7)
510 --
511 declaration :: { CDecl }
512 declaration
513 : sue_declaration_specifier ';'
514 {% withAttrs $1 $ CDecl (reverse $1) [] }
515
516 | sue_type_specifier ';'
517 {% withAttrs $1 $ CDecl (reverse $1) [] }
518
519 | declaring_list ';'
520 { case $1 of
521 CDecl declspecs dies attr ->
522 CDecl declspecs (List.reverse dies) attr }
523
524 | default_declaring_list ';'
525 { case $1 of
526 CDecl declspecs dies attr ->
527 CDecl declspecs (List.reverse dies) attr }
528
529
530 -- Note that if a typedef were redeclared, then a declaration
531 -- specifier must be supplied
532 --
533 -- Can't redeclare typedef names
534 --
535 default_declaring_list :: { CDecl }
536 default_declaring_list
537 : declaration_qualifier_list identifier_declarator asm_opt attrs_opt {-{}-} initializer_opt
538 {% let declspecs = reverse $1 in
539 doDeclIdent declspecs $2
540 >> (withAttrs $1 $ CDecl declspecs [(Just $2, $5, Nothing)]) }
541
542 | type_qualifier_list identifier_declarator asm_opt attrs_opt {-{}-} initializer_opt
543 {% let declspecs = liftTypeQuals $1 in
544 doDeclIdent declspecs $2
545 >> (withAttrs $1 $ CDecl declspecs [(Just $2, $5, Nothing)]) }
546
547 | default_declaring_list ',' identifier_declarator asm_opt attrs_opt {-{}-} initializer_opt
548 {% case $1 of
549 CDecl declspecs dies attr -> do
550 doDeclIdent declspecs $3
551 return (CDecl declspecs ((Just $3, $6, Nothing) : dies) attr) }
552
553
554 declaring_list :: { CDecl }
555 declaring_list
556 : declaration_specifier declarator asm_opt attrs_opt {-{}-} initializer_opt
557 {% doDeclIdent $1 $2
558 >> (withAttrs $1 $ CDecl $1 [(Just $2, $5, Nothing)]) }
559
560 | type_specifier declarator asm_opt attrs_opt {-{}-} initializer_opt
561 {% doDeclIdent $1 $2
562 >> (withAttrs $1 $ CDecl $1 [(Just $2, $5, Nothing)]) }
563
564 | declaring_list ',' declarator asm_opt attrs_opt {-{}-} initializer_opt
565 {% case $1 of
566 CDecl declspecs dies attr -> do
567 doDeclIdent declspecs $3
568 return (CDecl declspecs ((Just $3, $6, Nothing) : dies) attr) }
569
570
571 -- parse C declaration specifiers (C99 6.7)
572 --
573 -- * summary:
574 -- [ type_qualifier | storage_class
575 -- | basic_type_name | elaborated_type_name | tyident ]{
576 -- ( 1 >= basic_type_name
577 -- |x| 1 == elaborated_type_name
578 -- |x| 1 == tyident
579 -- ) && 1 >= storage_class
580 -- }
581 --
582 declaration_specifier :: { [CDeclSpec] }
583 declaration_specifier
584 : basic_declaration_specifier { reverse $1 } -- Arithmetic or void
585 | sue_declaration_specifier { reverse $1 } -- Struct/Union/Enum
586 | typedef_declaration_specifier { reverse $1 } -- Typedef
587
588
589 -- A mixture of type qualifiers and storage class specifiers in any order, but
590 -- containing at least one storage class specifier.
591 --
592 -- * summary:
593 -- [type_qualifier | storage_class]{ 1 >= storage_class }
594 --
595 -- * detail:
596 -- [type_qualifier] storage_class [type_qualifier | storage_class]
597 --
598 declaration_qualifier_list :: { Reversed [CDeclSpec] }
599 declaration_qualifier_list
600 : storage_class
601 { singleton (CStorageSpec $1) }
602
603 | type_qualifier_list storage_class
604 { rmap CTypeQual $1 `snoc` CStorageSpec $2 }
605
606 | declaration_qualifier_list declaration_qualifier
607 { $1 `snoc` $2 }
608
609 | declaration_qualifier_list attr
610 { $1 }
611
612
613 declaration_qualifier :: { CDeclSpec }
614 declaration_qualifier
615 : storage_class { CStorageSpec $1 }
616 | type_qualifier { CTypeQual $1 } -- const or volatile
617
618
619 -- parse C storage class specifier (C99 6.7.1)
620 --
621 -- * GNU extensions: '__thread' thread local storage
622 --
623 storage_class :: { CStorageSpec }
624 storage_class
625 : typedef {% withAttrs $1 $ CTypedef }
626 | extern {% withAttrs $1 $ CExtern }
627 | static {% withAttrs $1 $ CStatic }
628 | auto {% withAttrs $1 $ CAuto }
629 | register {% withAttrs $1 $ CRegister }
630 | "__thread" {% withAttrs $1 $ CThread }
631
632
633 -- parse C type specifier (C99 6.7.2)
634 --
635 -- This recignises a whole list of type specifiers rather than just one
636 -- as in the C99 grammar.
637 --
638 -- * summary:
639 -- [type_qualifier | basic_type_name | elaborated_type_name | tyident]{
640 -- 1 >= basic_type_name
641 -- |x| 1 == elaborated_type_name
642 -- |x| 1 == tyident
643 -- }
644 --
645 type_specifier :: { [CDeclSpec] }
646 type_specifier
647 : basic_type_specifier { reverse $1 } -- Arithmetic or void
648 | sue_type_specifier { reverse $1 } -- Struct/Union/Enum
649 | typedef_type_specifier { reverse $1 } -- Typedef
650
651
652 basic_type_name :: { CTypeSpec }
653 basic_type_name
654 : void {% withAttrs $1 $ CVoidType }
655 | char {% withAttrs $1 $ CCharType }
656 | short {% withAttrs $1 $ CShortType }
657 | int {% withAttrs $1 $ CIntType }
658 | long {% withAttrs $1 $ CLongType }
659 | float {% withAttrs $1 $ CFloatType }
660 | "__float128" {% withAttrs $1 $ CFloat128Type }
661 | double {% withAttrs $1 $ CDoubleType }
662 | signed {% withAttrs $1 $ CSignedType }
663 | unsigned {% withAttrs $1 $ CUnsigType }
664 | "_Bool" {% withAttrs $1 $ CBoolType }
665 | "_Complex" {% withAttrs $1 $ CComplexType }
666
667
668 -- A mixture of type qualifiers, storage class and basic type names in any
669 -- order, but containing at least one basic type name and at least one storage
670 -- class specifier.
671 --
672 -- * summary:
673 -- [type_qualifier | storage_class | basic_type_name]{
674 -- 1 >= storage_class && 1 >= basic_type_name
675 -- }
676 --
677 basic_declaration_specifier :: { Reversed [CDeclSpec] }
678 basic_declaration_specifier
679 : declaration_qualifier_list basic_type_name
680 { $1 `snoc` CTypeSpec $2 }
681
682 | basic_type_specifier storage_class
683 { $1 `snoc` CStorageSpec $2 }
684
685 | basic_declaration_specifier declaration_qualifier
686 { $1 `snoc` $2 }
687
688 | basic_declaration_specifier basic_type_name
689 { $1 `snoc` CTypeSpec $2 }
690
691 | basic_declaration_specifier attr
692 { $1 }
693
694
695 -- A mixture of type qualifiers and basic type names in any order, but
696 -- containing at least one basic type name.
697 --
698 -- * summary:
699 -- [type_qualifier | basic_type_name]{ 1 >= basic_type_name }
700 --
701 basic_type_specifier :: { Reversed [CDeclSpec] }
702 basic_type_specifier
703 -- Arithmetic or void
704 : basic_type_name
705 { singleton (CTypeSpec $1) }
706
707 | type_qualifier_list basic_type_name
708 { rmap CTypeQual $1 `snoc` CTypeSpec $2 }
709
710 | basic_type_specifier type_qualifier
711 { $1 `snoc` CTypeQual $2 }
712
713 | basic_type_specifier basic_type_name
714 { $1 `snoc` CTypeSpec $2 }
715
716 | basic_type_specifier attr
717 { $1 }
718
719
720 -- A named or anonymous struct, union or enum type along with at least one
721 -- storage class and any mix of type qualifiers.
722 --
723 -- * summary:
724 -- [type_qualifier | storage_class | elaborated_type_name]{
725 -- 1 == elaborated_type_name && 1 >= storage_class
726 -- }
727 --
728 sue_declaration_specifier :: { Reversed [CDeclSpec] }
729 sue_declaration_specifier
730 : declaration_qualifier_list elaborated_type_name
731 { $1 `snoc` CTypeSpec $2 }
732
733 | sue_type_specifier storage_class
734 { $1 `snoc` CStorageSpec $2 }
735
736 | sue_declaration_specifier declaration_qualifier
737 { $1 `snoc` $2 }
738
739 | sue_declaration_specifier attr
740 { $1 }
741
742
743 -- A struct, union or enum type (named or anonymous) with optional leading and
744 -- trailing type qualifiers.
745 --
746 -- * summary:
747 -- [type_qualifier] elaborated_type_name [type_qualifier]
748 --
749 sue_type_specifier :: { Reversed [CDeclSpec] }
750 sue_type_specifier
751 -- struct/union/enum
752 : elaborated_type_name
753 { singleton (CTypeSpec $1) }
754
755 | type_qualifier_list elaborated_type_name
756 { rmap CTypeQual $1 `snoc` CTypeSpec $2 }
757
758 | sue_type_specifier type_qualifier
759 { $1 `snoc` CTypeQual $2 }
760
761 | sue_type_specifier attr
762 { $1 }
763
764
765 -- A typedef'ed type identifier with at least one storage qualifier and any
766 -- number of type qualifiers
767 --
768 -- * Summary:
769 -- [type_qualifier | storage_class | tyident]{
770 -- 1 == tyident && 1 >= storage_class
771 -- }
772 --
773 -- * Note:
774 -- the tyident can also be a: typeof '(' ... ')'
775 --
776 typedef_declaration_specifier :: { Reversed [CDeclSpec] }
777 typedef_declaration_specifier
778 : typedef_type_specifier storage_class
779 { $1 `snoc` CStorageSpec $2 }
780
781 | declaration_qualifier_list tyident
782 {% withAttrs $1 $ \attr -> $1 `snoc` CTypeSpec (CTypeDef $2 attr) }
783
784 | declaration_qualifier_list typeof '(' expression ')'
785 {% withAttrs $1 $ \attr -> $1 `snoc` CTypeSpec (CTypeOfExpr $4 attr) }
786
787 | declaration_qualifier_list typeof '(' type_name ')'
788 {% withAttrs $1 $ \attr -> $1 `snoc` CTypeSpec (CTypeOfType $4 attr) }
789
790 | typedef_declaration_specifier declaration_qualifier
791 { $1 `snoc` $2 }
792
793 | typedef_declaration_specifier attr
794 { $1 }
795
796
797 -- typedef'ed type identifier with optional leading and trailing type qualifiers
798 --
799 -- * Summary:
800 -- [type_qualifier] ( tyident | typeof '('...')' ) [type_qualifier]
801 --
802 typedef_type_specifier :: { Reversed [CDeclSpec] }
803 typedef_type_specifier
804 : tyident
805 {% withAttrs $1 $ \attr -> singleton (CTypeSpec (CTypeDef $1 attr)) }
806
807 | typeof '(' expression ')'
808 {% withAttrs $1 $ \attr -> singleton (CTypeSpec (CTypeOfExpr $3 attr)) }
809
810 | typeof '(' type_name ')'
811 {% withAttrs $1 $ \attr -> singleton (CTypeSpec (CTypeOfType $3 attr)) }
812
813 | type_qualifier_list tyident
814 {% withAttrs $2 $ \attr -> rmap CTypeQual $1 `snoc` CTypeSpec (CTypeDef $2 attr) }
815
816 | type_qualifier_list typeof '(' expression ')'
817 {% withAttrs $2 $ \attr -> rmap CTypeQual $1 `snoc` CTypeSpec (CTypeOfExpr $4 attr) }
818
819 | type_qualifier_list typeof '(' type_name ')'
820 {% withAttrs $2 $ \attr -> rmap CTypeQual $1 `snoc` CTypeSpec (CTypeOfType $4 attr) }
821
822 | typedef_type_specifier type_qualifier
823 { $1 `snoc` CTypeQual $2 }
824
825 | typedef_type_specifier attr
826 { $1 }
827
828
829 -- A named or anonymous struct, union or enum type.
830 --
831 -- * summary:
832 -- (struct|union|enum) (identifier? '{' ... '}' | identifier)
833 --
834 elaborated_type_name :: { CTypeSpec }
835 elaborated_type_name
836 : struct_or_union_specifier {% withAttrs $1 $ CSUType $1 }
837 | enum_specifier {% withAttrs $1 $ CEnumType $1 }
838
839
840 -- parse C structure or union declaration (C99 6.7.2.1)
841 --
842 -- * summary:
843 -- (struct|union) (identifier? '{' ... '}' | identifier)
844 --
845 struct_or_union_specifier :: { CStructUnion }
846 struct_or_union_specifier
847 : struct_or_union attrs_opt identifier '{' struct_declaration_list '}'
848 {% withAttrs $1 $ CStruct (unL $1) (Just $3) (reverse $5) }
849
850 | struct_or_union attrs_opt '{' struct_declaration_list '}'
851 {% withAttrs $1 $ CStruct (unL $1) Nothing (reverse $4) }
852
853 | struct_or_union attrs_opt identifier
854 {% withAttrs $1 $ CStruct (unL $1) (Just $3) [] }
855
856
857 struct_or_union :: { Located CStructTag }
858 struct_or_union
859 : struct { L CStructTag (posOf $1) }
860 | union { L CUnionTag (posOf $1) }
861
862
863 struct_declaration_list :: { Reversed [CDecl] }
864 struct_declaration_list
865 : {- empty -} { empty }
866 | struct_declaration_list ';' { $1 }
867 | struct_declaration_list struct_declaration { $1 `snoc` $2 }
868
869
870 -- parse C structure declaration (C99 6.7.2.1)
871 --
872 struct_declaration :: { CDecl }
873 struct_declaration
874 : struct_declaring_list ';'
875 { case $1 of CDecl declspecs dies attr -> CDecl declspecs (List.reverse dies) attr }
876
877 | struct_default_declaring_list ';'
878 { case $1 of CDecl declspecs dies attr -> CDecl declspecs (List.reverse dies) attr }
879
880 | "__extension__" struct_declaration { $2 }
881
882
883 -- doesn't redeclare typedef
884 struct_default_declaring_list :: { CDecl }
885 struct_default_declaring_list
886 : attrs_opt type_qualifier_list struct_identifier_declarator attrs_opt
887 {% withAttrs $2 $ case $3 of (d,s) -> CDecl (liftTypeQuals $2) [(d,Nothing,s)] }
888
889 | struct_default_declaring_list ',' attrs_opt struct_identifier_declarator attrs_opt
890 { case $1 of
891 CDecl declspecs dies attr ->
892 case $4 of
893 (d,s) -> CDecl declspecs ((d,Nothing,s) : dies) attr }
894
895
896 -- * GNU extensions:
897 -- allow anonymous nested structures and unions
898 --
899 struct_declaring_list :: { CDecl }
900 struct_declaring_list
901 : attrs_opt type_specifier struct_declarator attrs_opt
902 {% withAttrs $2 $ case $3 of (d,s) -> CDecl $2 [(d,Nothing,s)] }
903
904 | struct_declaring_list ',' attrs_opt struct_declarator attrs_opt
905 { case $1 of
906 CDecl declspecs dies attr ->
907 case $4 of
908 (d,s) -> CDecl declspecs ((d,Nothing,s) : dies) attr }
909
910 -- We're being far too liberal in the parsing here, we really want to just
911 -- allow unnamed struct and union fields but we're actually allowing any
912 -- unnamed struct member. Making it allow only unnamed structs or unions in
913 -- the parser is far too tricky, it makes things ambiguous. So we'll have to
914 -- diagnose unnamed fields that are not structs/unions in a later stage.
915 | attrs_opt type_specifier
916 {% withAttrs $2 $ CDecl $2 [] }
917
918
919 -- parse C structure declarator (C99 6.7.2.1)
920 --
921 struct_declarator :: { (Maybe CDeclr, Maybe CExpr) }
922 struct_declarator
923 : declarator { (Just $1, Nothing) }
924 | ':' constant_expression { (Nothing, Just $2) }
925 | declarator ':' constant_expression { (Just $1, Just $3) }
926
927
928 struct_identifier_declarator :: { (Maybe CDeclr, Maybe CExpr) }
929 struct_identifier_declarator
930 : identifier_declarator { (Just $1, Nothing) }
931 | ':' constant_expression { (Nothing, Just $2) }
932 | identifier_declarator ':' constant_expression { (Just $1, Just $3) }
933
934
935 -- parse C enumeration declaration (C99 6.7.2.2)
936 --
937 -- * summary:
938 -- enum (identifier? '{' ... '}' | identifier)
939 --
940 enum_specifier :: { CEnum }
941 enum_specifier
942 : enum attrs_opt '{' enumerator_list '}'
943 {% withAttrs $1 $ CEnum Nothing (reverse $4) }
944
945 | enum attrs_opt '{' enumerator_list ',' '}'
946 {% withAttrs $1 $ CEnum Nothing (reverse $4) }
947
948 | enum attrs_opt identifier '{' enumerator_list '}'
949 {% withAttrs $1 $ CEnum (Just $3) (reverse $5) }
950
951 | enum attrs_opt identifier '{' enumerator_list ',' '}'
952 {% withAttrs $1 $ CEnum (Just $3) (reverse $5) }
953
954 | enum attrs_opt identifier
955 {% withAttrs $1 $ CEnum (Just $3) [] }
956
957
958 enumerator_list :: { Reversed [(Ident, Maybe CExpr)] }
959 enumerator_list
960 : enumerator { singleton $1 }
961 | enumerator_list ',' enumerator { $1 `snoc` $3 }
962
963
964 enumerator :: { (Ident, Maybe CExpr) }
965 enumerator
966 : identifier { ($1, Nothing) }
967 | identifier '=' constant_expression { ($1, Just $3) }
968
969
970 -- parse C type qualifier (C99 6.7.3)
971 --
972 type_qualifier :: { CTypeQual }
973 type_qualifier
974 : const {% withAttrs $1 $ CConstQual }
975 | volatile {% withAttrs $1 $ CVolatQual }
976 | restrict {% withAttrs $1 $ CRestrQual }
977 | inline {% withAttrs $1 $ CInlinQual }
978
979
980 -- parse C declarator (C99 6.7.5)
981 --
982 declarator :: { CDeclr }
983 declarator
984 : identifier_declarator { $1 }
985 | typedef_declarator { $1 }
986
987
988 -- Parse GNU C's asm annotations
989 --
990 asm_opt :: { () }
991 asm_opt
992 : {- empty -} { () }
993 | asm '(' string_literal_list ')' { () }
994
995
996 typedef_declarator :: { CDeclr }
997 typedef_declarator
998 -- would be ambiguous as parameter
999 : paren_typedef_declarator { $1 }
1000
1001 -- not ambiguous as param
1002 | parameter_typedef_declarator { $1 }
1003
1004
1005 parameter_typedef_declarator :: { CDeclr }
1006 parameter_typedef_declarator
1007 : tyident
1008 {% withAttrs $1 $ CVarDeclr (Just $1) }
1009
1010 | tyident postfixing_abstract_declarator
1011 {% withAttrs $1 $ \attrs -> $2 (CVarDeclr (Just $1) attrs) }
1012
1013 | clean_typedef_declarator
1014 { $1 }
1015
1016
1017 -- The following have at least one '*'.
1018 -- There is no (redundant) '(' between the '*' and the tyident.
1019 clean_typedef_declarator :: { CDeclr }
1020 clean_typedef_declarator
1021 : clean_postfix_typedef_declarator
1022 { $1 }
1023
1024 | '*' parameter_typedef_declarator
1025 {% withAttrs $1 $ CPtrDeclr [] $2 }
1026
1027 | '*' type_qualifier_list parameter_typedef_declarator
1028 {% withAttrs $1 $ CPtrDeclr (reverse $2) $3 }
1029
1030 | '*' attrs parameter_typedef_declarator
1031 {% withAttrs $1 $ CPtrDeclr [] $3 }
1032
1033 | '*' attrs type_qualifier_list parameter_typedef_declarator
1034 {% withAttrs $1 $ CPtrDeclr (reverse $3) $4 }
1035
1036
1037 clean_postfix_typedef_declarator :: { CDeclr }
1038 clean_postfix_typedef_declarator
1039 : '(' clean_typedef_declarator ')' { $2 }
1040 | '(' attrs clean_typedef_declarator ')' { $3 }
1041 | '(' clean_typedef_declarator ')' postfixing_abstract_declarator { $4 $2 }
1042 | '(' attrs clean_typedef_declarator ')' postfixing_abstract_declarator { $5 $3 }
1043
1044
1045 -- The following have a redundant '(' placed
1046 -- immediately to the left of the tyident
1047 paren_typedef_declarator :: { CDeclr }
1048 paren_typedef_declarator
1049 : paren_postfix_typedef_declarator
1050 { $1 }
1051
1052 -- redundant paren
1053 | '*' '(' simple_paren_typedef_declarator ')'
1054 {% withAttrs $1 $ CPtrDeclr [] $3 }
1055
1056 -- redundant paren
1057 | '*' type_qualifier_list '(' simple_paren_typedef_declarator ')'
1058 {% withAttrs $1 $ CPtrDeclr (reverse $2) $4 }
1059
1060 | '*' paren_typedef_declarator
1061 {% withAttrs $1 $ CPtrDeclr [] $2 }
1062
1063 | '*' type_qualifier_list paren_typedef_declarator
1064 {% withAttrs $1 $ CPtrDeclr (reverse $2) $3 }
1065
1066 | '*' attrs '(' simple_paren_typedef_declarator ')'
1067 {% withAttrs $1 $ CPtrDeclr [] $4 }
1068
1069 -- redundant paren
1070 | '*' attrs type_qualifier_list '(' simple_paren_typedef_declarator ')'
1071 {% withAttrs $1 $ CPtrDeclr (reverse $3) $5 }
1072
1073 | '*' attrs paren_typedef_declarator
1074 {% withAttrs $1 $ CPtrDeclr [] $3 }
1075
1076 | '*' attrs type_qualifier_list paren_typedef_declarator
1077 {% withAttrs $1 $ CPtrDeclr (reverse $3) $4 }
1078
1079
1080 -- redundant paren to left of tname
1081 paren_postfix_typedef_declarator :: { CDeclr }
1082 paren_postfix_typedef_declarator
1083 : '(' paren_typedef_declarator ')'
1084 { $2 }
1085
1086 -- redundant paren
1087 | '(' simple_paren_typedef_declarator postfixing_abstract_declarator ')'
1088 { $3 $2 }
1089
1090 | '(' paren_typedef_declarator ')' postfixing_abstract_declarator
1091 { $4 $2 }
1092
1093
1094 -- Just a type name in any number of nested brackets
1095 --
1096 simple_paren_typedef_declarator :: { CDeclr }
1097 simple_paren_typedef_declarator
1098 : tyident
1099 {% withAttrs $1 $ CVarDeclr (Just $1) }
1100
1101 | '(' simple_paren_typedef_declarator ')'
1102 { $2 }
1103
1104
1105 identifier_declarator :: { CDeclr }
1106 identifier_declarator
1107 : unary_identifier_declarator { $1 }
1108 | paren_identifier_declarator { $1 }
1109
1110
1111 unary_identifier_declarator :: { CDeclr }
1112 unary_identifier_declarator
1113 : postfix_identifier_declarator
1114 { $1 }
1115
1116 | '*' identifier_declarator
1117 {% withAttrs $1 $ CPtrDeclr [] $2 }
1118
1119 | '*' type_qualifier_list identifier_declarator
1120 {% withAttrs $1 $ CPtrDeclr (reverse $2) $3 }
1121
1122 | '*' attrs identifier_declarator
1123 {% withAttrs $1 $ CPtrDeclr [] $3 }
1124
1125 | '*' attrs type_qualifier_list identifier_declarator
1126 {% withAttrs $1 $ CPtrDeclr (reverse $3) $4 }
1127
1128
1129 postfix_identifier_declarator :: { CDeclr }
1130 postfix_identifier_declarator
1131 : paren_identifier_declarator postfixing_abstract_declarator
1132 { $2 $1 }
1133
1134 | '(' unary_identifier_declarator ')'
1135 { $2 }
1136
1137 | '(' unary_identifier_declarator ')' postfixing_abstract_declarator
1138 { $4 $2 }
1139
1140 | '(' attrs unary_identifier_declarator ')'
1141 { $3 }
1142
1143 | '(' attrs unary_identifier_declarator ')' postfixing_abstract_declarator
1144 { $5 $3 }
1145
1146
1147 paren_identifier_declarator :: { CDeclr }
1148 paren_identifier_declarator
1149 : ident
1150 {% withAttrs $1 $ CVarDeclr (Just $1) }
1151
1152 | '(' paren_identifier_declarator ')'
1153 { $2 }
1154
1155
1156 old_function_declarator :: { CDeclr }
1157 old_function_declarator
1158 : postfix_old_function_declarator
1159 { $1 }
1160
1161 | '*' old_function_declarator
1162 {% withAttrs $1 $ CPtrDeclr [] $2 }
1163
1164 | '*' type_qualifier_list old_function_declarator
1165 {% withAttrs $1 $ CPtrDeclr (reverse $2) $3 }
1166
1167
1168 postfix_old_function_declarator :: { CDeclr }
1169 postfix_old_function_declarator
1170 : paren_identifier_declarator '(' identifier_list ')'
1171 {% withAttrs $2 $ CFunDeclr $1 [] False }
1172
1173 | '(' old_function_declarator ')'
1174 { $2 }
1175
1176 | '(' old_function_declarator ')' postfixing_abstract_declarator
1177 { $4 $2 }
1178
1179
1180 type_qualifier_list :: { Reversed [CTypeQual] }
1181 type_qualifier_list
1182 : type_qualifier { singleton $1 }
1183 | type_qualifier_list type_qualifier { $1 `snoc` $2 }
1184 | type_qualifier_list attr { $1 }
1185
1186
1187 -- parse C parameter type list (C99 6.7.5)
1188 --
1189 parameter_type_list :: { ([CDecl], Bool) }
1190 parameter_type_list
1191 : {- empty -} { ([], False)}
1192 | parameter_list { (reverse $1, False) }
1193 | parameter_list ',' "..." { (reverse $1, True) }
1194
1195
1196 parameter_list :: { Reversed [CDecl] }
1197 parameter_list
1198 : parameter_declaration { singleton $1 }
1199 | attrs parameter_declaration { singleton $2 }
1200 | parameter_list ',' attrs_opt parameter_declaration { $1 `snoc` $4 }
1201
1202
1203 parameter_declaration :: { CDecl }
1204 parameter_declaration
1205 : declaration_specifier
1206 {% withAttrs $1 $ CDecl $1 [] }
1207
1208 | declaration_specifier abstract_declarator
1209 {% withAttrs $1 $ CDecl $1 [(Just $2, Nothing, Nothing)] }
1210
1211 | declaration_specifier identifier_declarator attrs_opt
1212 {% withAttrs $1 $ CDecl $1 [(Just $2, Nothing, Nothing)] }
1213
1214 | declaration_specifier parameter_typedef_declarator attrs_opt
1215 {% withAttrs $1 $ CDecl $1 [(Just $2, Nothing, Nothing)] }
1216
1217 | declaration_qualifier_list
1218 {% withAttrs $1 $ CDecl (reverse $1) [] }
1219
1220 | declaration_qualifier_list abstract_declarator
1221 {% withAttrs $1 $ CDecl (reverse $1) [(Just $2, Nothing, Nothing)] }
1222
1223 | declaration_qualifier_list identifier_declarator attrs_opt
1224 {% withAttrs $1 $ CDecl (reverse $1) [(Just $2, Nothing, Nothing)] }
1225
1226 | type_specifier
1227 {% withAttrs $1 $ CDecl $1 [] }
1228
1229 | type_specifier abstract_declarator
1230 {% withAttrs $1 $ CDecl $1 [(Just $2, Nothing, Nothing)] }
1231
1232 | type_specifier identifier_declarator attrs_opt
1233 {% withAttrs $1 $ CDecl $1 [(Just $2, Nothing, Nothing)] }
1234
1235 | type_specifier parameter_typedef_declarator attrs_opt
1236 {% withAttrs $1 $ CDecl $1 [(Just $2, Nothing, Nothing)] }
1237
1238 | type_qualifier_list
1239 {% withAttrs $1 $ CDecl (liftTypeQuals $1) [] }
1240
1241 | type_qualifier_list abstract_declarator
1242 {% withAttrs $1 $ CDecl (liftTypeQuals $1) [(Just $2, Nothing, Nothing)] }
1243
1244 | type_qualifier_list identifier_declarator attrs_opt
1245 {% withAttrs $1 $ CDecl (liftTypeQuals $1) [(Just $2, Nothing, Nothing)] }
1246
1247
1248 identifier_list :: { Reversed [Ident] }
1249 identifier_list
1250 : ident { singleton $1 }
1251 | identifier_list ',' ident { $1 `snoc` $3 }
1252
1253
1254 -- parse C type name (C99 6.7.6)
1255 --
1256 type_name :: { CDecl }
1257 type_name
1258 : attrs_opt type_specifier
1259 {% withAttrs $2 $ CDecl $2 [] }
1260
1261 | attrs_opt type_specifier abstract_declarator
1262 {% withAttrs $2 $ CDecl $2 [(Just $3, Nothing, Nothing)] }
1263
1264 | attrs_opt type_qualifier_list
1265 {% withAttrs $2 $ CDecl (liftTypeQuals $2) [] }
1266
1267 | attrs_opt type_qualifier_list abstract_declarator
1268 {% withAttrs $2 $ CDecl (liftTypeQuals $2) [(Just $3, Nothing, Nothing)] }
1269
1270
1271 -- parse C abstract declarator (C99 6.7.6)
1272 --
1273 abstract_declarator :: { CDeclr }
1274 abstract_declarator
1275 : unary_abstract_declarator { $1 }
1276 | postfix_abstract_declarator { $1 }
1277 | postfixing_abstract_declarator attrs_opt { $1 emptyDeclr }
1278
1279
1280 postfixing_abstract_declarator :: { CDeclr -> CDeclr }
1281 postfixing_abstract_declarator
1282 : array_abstract_declarator
1283 { $1 }
1284
1285 | '(' parameter_type_list ')'
1286 {% withAttrs $1 $ \attrs declr -> case $2 of
1287 (params, variadic) -> CFunDeclr declr params variadic attrs }
1288
1289
1290 -- * Note that we recognise but ignore the C99 static keyword (see C99 6.7.5.3)
1291 --
1292 -- * We do not distinguish in the AST between incomplete array types and
1293 -- complete variable length arrays ([ '*' ] means the latter). (see C99 6.7.5.2)
1294 --
1295 array_abstract_declarator :: { CDeclr -> CDeclr }
1296 array_abstract_declarator
1297 : postfix_array_abstract_declarator
1298 { $1 }
1299
1300 | array_abstract_declarator postfix_array_abstract_declarator
1301 { \decl -> $2 ($1 decl) }
1302
1303
1304 postfix_array_abstract_declarator :: { CDeclr -> CDeclr }
1305 postfix_array_abstract_declarator
1306 : '[' assignment_expression_opt ']'
1307 {% withAttrs $1 $ \attrs declr -> CArrDeclr declr [] $2 attrs }
1308
1309 | '[' type_qualifier_list assignment_expression_opt ']'
1310 {% withAttrs $1 $ \attrs declr -> CArrDeclr declr (reverse $2) $3 attrs }
1311
1312 | '[' static assignment_expression ']'
1313 {% withAttrs $1 $ \attrs declr -> CArrDeclr declr [] (Just $3) attrs }
1314
1315 | '[' static type_qualifier_list assignment_expression ']'
1316 {% withAttrs $1 $ \attrs declr -> CArrDeclr declr (reverse $3) (Just $4) attrs }
1317
1318 | '[' type_qualifier_list static assignment_expression ']'
1319 {% withAttrs $1 $ \attrs declr -> CArrDeclr declr (reverse $2) (Just $4) attrs }
1320
1321 | '[' '*' ']'
1322 {% withAttrs $1 $ \attrs declr -> CArrDeclr declr [] Nothing attrs }
1323
1324 | '[' type_qualifier_list '*' ']'
1325 {% withAttrs $1 $ \attrs declr -> CArrDeclr declr (reverse $2) Nothing attrs }
1326
1327
1328 unary_abstract_declarator :: { CDeclr }
1329 unary_abstract_declarator
1330 : '*'
1331 {% withAttrs $1 $ CPtrDeclr [] emptyDeclr }
1332
1333 | '*' type_qualifier_list
1334 {% withAttrs $1 $ CPtrDeclr (reverse $2) emptyDeclr }
1335
1336 | '*' abstract_declarator
1337 {% withAttrs $1 $ CPtrDeclr [] $2 }
1338
1339 | '*' type_qualifier_list abstract_declarator
1340 {% withAttrs $1 $ CPtrDeclr (reverse $2) $3 }
1341
1342 | '*' attrs
1343 {% withAttrs $1 $ CPtrDeclr [] emptyDeclr }
1344
1345 | '*' attrs type_qualifier_list
1346 {% withAttrs $1 $ CPtrDeclr (reverse $3) emptyDeclr }
1347
1348 | '*' attrs abstract_declarator
1349 {% withAttrs $1 $ CPtrDeclr [] $3 }
1350
1351 | '*' attrs type_qualifier_list abstract_declarator
1352 {% withAttrs $1 $ CPtrDeclr (reverse $3) $4 }
1353
1354
1355 postfix_abstract_declarator :: { CDeclr }
1356 postfix_abstract_declarator
1357 : '(' unary_abstract_declarator ')' { $2 }
1358 | '(' postfix_abstract_declarator ')' { $2 }
1359 | '(' postfixing_abstract_declarator ')' { $2 emptyDeclr }
1360 | '(' unary_abstract_declarator ')' postfixing_abstract_declarator { $4 $2 }
1361 | '(' attrs unary_abstract_declarator ')' { $3 }
1362 | '(' attrs postfix_abstract_declarator ')' { $3 }
1363 | '(' attrs postfixing_abstract_declarator ')' { $3 emptyDeclr }
1364 | '(' attrs unary_abstract_declarator ')' postfixing_abstract_declarator { $5 $3 }
1365 | postfix_abstract_declarator attr { $1 }
1366
1367
1368 -- parse C initializer (C99 6.7.8)
1369 --
1370 initializer :: { CInit }
1371 initializer
1372 : assignment_expression {% withAttrs $1 $ CInitExpr $1 }
1373 | '{' initializer_list '}' {% withAttrs $1 $ CInitList (reverse $2) }
1374 | '{' initializer_list ',' '}' {% withAttrs $1 $ CInitList (reverse $2) }
1375
1376
1377 initializer_opt :: { Maybe CInit }
1378 initializer_opt
1379 : {- empty -} { Nothing }
1380 | '=' initializer { Just $2 }
1381
1382
1383 initializer_list :: { Reversed CInitList }
1384 initializer_list
1385 : {- empty -} { empty }
1386 | initializer { singleton ([],$1) }
1387 | designation initializer { singleton ($1,$2) }
1388 | initializer_list ',' initializer { $1 `snoc` ([],$3) }
1389 | initializer_list ',' designation initializer { $1 `snoc` ($3,$4) }
1390
1391
1392 -- designation
1393 --
1394 -- * GNU extensions:
1395 -- old style member designation: 'ident :'
1396 -- array range designation
1397 --
1398 designation :: { [CDesignator] }
1399 designation
1400 : designator_list '=' { reverse $1 }
1401 | identifier ':' {% withAttrs $1 $ \at -> [CMemberDesig $1 at] }
1402 | array_designator { [$1] }
1403
1404
1405 designator_list :: { Reversed [CDesignator] }
1406 designator_list
1407 : designator { singleton $1 }
1408 | designator_list designator { $1 `snoc` $2 }
1409
1410
1411 designator :: { CDesignator }
1412 designator
1413 : '[' constant_expression ']' {% withAttrs $1 $ CArrDesig $2 }
1414 | '.' identifier {% withAttrs $1 $ CMemberDesig $2 }
1415 | array_designator { $1 }
1416
1417
1418 array_designator :: { CDesignator }
1419 array_designator
1420 : '[' constant_expression "..." constant_expression ']'
1421 {% withAttrs $1 $ CRangeDesig $2 $4 }
1422
1423
1424 -- parse C primary expression (C99 6.5.1)
1425 --
1426 -- We cannot use a typedef name as a variable
1427 --
1428 -- * GNU extensions:
1429 -- allow a compound statement as an expression
1430 -- various __builtin_* forms that take type parameters
1431 --
1432 primary_expression :: { CExpr }
1433 primary_expression
1434 : ident {% withAttrs $1 $ CVar $1 }
1435 | constant {% withAttrs $1 $ CConst $1 }
1436 | string_literal {% withAttrs $1 $ CConst $1 }
1437 | '(' expression ')' { $2 }
1438 | '(' compound_statement ')'
1439 {% withAttrs $1 $ CStatExpr $2 }
1440
1441 | "__builtin_va_arg" '(' assignment_expression ',' type_name ')'
1442 {% withAttrs $1 CBuiltinExpr }
1443
1444 | "__builtin_offsetof" '(' type_name ',' offsetof_member_designator ')'
1445 {% withAttrs $1 CBuiltinExpr }
1446
1447 | "__builtin_types_compatible_p" '(' type_name ',' type_name ')'
1448 {% withAttrs $1 CBuiltinExpr }
1449
1450
1451 offsetof_member_designator :: { () }
1452 offsetof_member_designator
1453 : ident { () }
1454 | offsetof_member_designator '.' ident { () }
1455 | offsetof_member_designator '[' expression ']' { () }
1456
1457
1458 --parse C postfix expression (C99 6.5.2)
1459 --
1460 postfix_expression :: { CExpr }
1461 postfix_expression
1462 : primary_expression
1463 { $1 }
1464
1465 | postfix_expression '[' expression ']'
1466 {% withAttrs $2 $ CIndex $1 $3 }
1467
1468 | postfix_expression '(' ')'
1469 {% withAttrs $2 $ CCall $1 [] }
1470
1471 | postfix_expression '(' argument_expression_list ')'
1472 {% withAttrs $2 $ CCall $1 (reverse $3) }
1473
1474 | postfix_expression '.' identifier
1475 {% withAttrs $2 $ CMember $1 $3 False }
1476
1477 | postfix_expression "->" identifier
1478 {% withAttrs $2 $ CMember $1 $3 True }
1479
1480 | postfix_expression "++"
1481 {% withAttrs $2 $ CUnary CPostIncOp $1 }
1482
1483 | postfix_expression "--"
1484 {% withAttrs $2 $ CUnary CPostDecOp $1 }
1485
1486 | '(' type_name ')' '{' initializer_list '}'
1487 {% withAttrs $4 $ CCompoundLit $2 (reverse $5) }
1488
1489 | '(' type_name ')' '{' initializer_list ',' '}'
1490 {% withAttrs $4 $ CCompoundLit $2 (reverse $5) }
1491
1492
1493 argument_expression_list :: { Reversed [CExpr] }
1494 argument_expression_list
1495 : assignment_expression { singleton $1 }
1496 | argument_expression_list ',' assignment_expression { $1 `snoc` $3 }
1497
1498
1499 -- parse C unary expression (C99 6.5.3)
1500 --
1501 -- * GNU extensions:
1502 -- 'alignof' expression or type
1503 -- '__extension__' to suppress warnings about extensions
1504 -- allow taking address of a label with: && label
1505 --
1506 unary_expression :: { CExpr }
1507 unary_expression
1508 : postfix_expression { $1 }
1509 | "++" unary_expression {% withAttrs $1 $ CUnary CPreIncOp $2 }
1510 | "--" unary_expression {% withAttrs $1 $ CUnary CPreDecOp $2 }
1511 | "__extension__" cast_expression { $2 }
1512 | unary_operator cast_expression {% withAttrs $1 $ CUnary (unL $1) $2 }
1513 | sizeof unary_expression {% withAttrs $1 $ CSizeofExpr $2 }
1514 | sizeof '(' type_name ')' {% withAttrs $1 $ CSizeofType $3 }
1515 | alignof unary_expression {% withAttrs $1 $ CAlignofExpr $2 }
1516 | alignof '(' type_name ')' {% withAttrs $1 $ CAlignofType $3 }
1517 | "&&" identifier {% withAttrs $1 $ CLabAddrExpr $2 }
1518
1519
1520 unary_operator :: { Located CUnaryOp }
1521 unary_operator
1522 : '&' { L CAdrOp (posOf $1) }
1523 | '*' { L CIndOp (posOf $1) }
1524 | '+' { L CPlusOp (posOf $1) }
1525 | '-' { L CMinOp (posOf $1) }
1526 | '~' { L CCompOp (posOf $1) }
1527 | '!' { L CNegOp (posOf $1) }
1528
1529
1530 -- parse C cast expression (C99 6.5.4)
1531 --
1532 cast_expression :: { CExpr }
1533 cast_expression
1534 : unary_expression { $1 }
1535 | '(' type_name ')' cast_expression {% withAttrs $1 $ CCast $2 $4 }
1536
1537
1538 -- parse C multiplicative expression (C99 6.5.5)
1539 --
1540 multiplicative_expression :: { CExpr }
1541 multiplicative_expression
1542 : cast_expression
1543 { $1 }
1544
1545 | multiplicative_expression '*' cast_expression
1546 {% withAttrs $2 $ CBinary CMulOp $1 $3 }
1547
1548 | multiplicative_expression '/' cast_expression
1549 {% withAttrs $2 $ CBinary CDivOp $1 $3 }
1550
1551 | multiplicative_expression '%' cast_expression
1552 {% withAttrs $2 $ CBinary CRmdOp $1 $3 }
1553
1554
1555 -- parse C additive expression (C99 6.5.6)
1556 --
1557 additive_expression :: { CExpr }
1558 additive_expression
1559 : multiplicative_expression
1560 { $1 }
1561
1562 | additive_expression '+' multiplicative_expression
1563 {% withAttrs $2 $ CBinary CAddOp $1 $3 }
1564
1565 | additive_expression '-' multiplicative_expression
1566 {% withAttrs $2 $ CBinary CSubOp $1 $3 }
1567
1568
1569 -- parse C shift expression (C99 6.5.7)
1570 --
1571 shift_expression :: { CExpr }
1572 shift_expression
1573 : additive_expression
1574 { $1 }
1575
1576 | shift_expression "<<" additive_expression
1577 {% withAttrs $2 $ CBinary CShlOp $1 $3 }
1578
1579 | shift_expression ">>" additive_expression
1580 {% withAttrs $2 $ CBinary CShrOp $1 $3 }
1581
1582
1583 -- parse C relational expression (C99 6.5.8)
1584 --
1585 relational_expression :: { CExpr }
1586 relational_expression
1587 : shift_expression
1588 { $1 }
1589
1590 | relational_expression '<' shift_expression
1591 {% withAttrs $2 $ CBinary CLeOp $1 $3 }
1592
1593 | relational_expression '>' shift_expression
1594 {% withAttrs $2 $ CBinary CGrOp $1 $3 }
1595
1596 | relational_expression "<=" shift_expression
1597 {% withAttrs $2 $ CBinary CLeqOp $1 $3 }
1598
1599 | relational_expression ">=" shift_expression
1600 {% withAttrs $2 $ CBinary CGeqOp $1 $3 }
1601
1602
1603 -- parse C equality expression (C99 6.5.9)
1604 --
1605 equality_expression :: { CExpr }
1606 equality_expression
1607 : relational_expression
1608 { $1 }
1609
1610 | equality_expression "==" relational_expression
1611 {% withAttrs $2 $ CBinary CEqOp $1 $3 }
1612
1613 | equality_expression "!=" relational_expression
1614 {% withAttrs $2 $ CBinary CNeqOp $1 $3 }
1615
1616
1617 -- parse C bitwise and expression (C99 6.5.10)
1618 --
1619 and_expression :: { CExpr }
1620 and_expression
1621 : equality_expression
1622 { $1 }
1623
1624 | and_expression '&' equality_expression
1625 {% withAttrs $2 $ CBinary CAndOp $1 $3 }
1626
1627
1628 -- parse C bitwise exclusive or expression (C99 6.5.11)
1629 --
1630 exclusive_or_expression :: { CExpr }
1631 exclusive_or_expression
1632 : and_expression
1633 { $1 }
1634
1635 | exclusive_or_expression '^' and_expression
1636 {% withAttrs $2 $ CBinary CXorOp $1 $3 }
1637
1638
1639 -- parse C bitwise or expression (C99 6.5.12)
1640 --
1641 inclusive_or_expression :: { CExpr }
1642 inclusive_or_expression
1643 : exclusive_or_expression
1644 { $1 }
1645
1646 | inclusive_or_expression '|' exclusive_or_expression
1647 {% withAttrs $2 $ CBinary COrOp $1 $3 }
1648
1649
1650 -- parse C logical and expression (C99 6.5.13)
1651 --
1652 logical_and_expression :: { CExpr }
1653 logical_and_expression
1654 : inclusive_or_expression
1655 { $1 }
1656
1657 | logical_and_expression "&&" inclusive_or_expression
1658 {% withAttrs $2 $ CBinary CLndOp $1 $3 }
1659
1660
1661 -- parse C logical or expression (C99 6.5.14)
1662 --
1663 logical_or_expression :: { CExpr }
1664 logical_or_expression
1665 : logical_and_expression
1666 { $1 }
1667
1668 | logical_or_expression "||" logical_and_expression
1669 {% withAttrs $2 $ CBinary CLorOp $1 $3 }
1670
1671
1672 -- parse C conditional expression (C99 6.5.15)
1673 --
1674 -- * GNU extensions:
1675 -- omitting the `then' part
1676 --
1677 conditional_expression :: { CExpr }
1678 conditional_expression
1679 : logical_or_expression
1680 { $1 }
1681
1682 | logical_or_expression '?' expression ':' conditional_expression
1683 {% withAttrs $2 $ CCond $1 (Just $3) $5 }
1684
1685 | logical_or_expression '?' ':' conditional_expression
1686 {% withAttrs $2 $ CCond $1 Nothing $4 }
1687
1688
1689 -- parse C assignment expression (C99 6.5.16)
1690 --
1691 assignment_expression :: { CExpr }
1692 assignment_expression
1693 : conditional_expression
1694 { $1 }
1695
1696 | unary_expression assignment_operator assignment_expression
1697 {% withAttrs $2 $ CAssign (unL $2) $1 $3 }
1698
1699
1700 assignment_operator :: { Located CAssignOp }
1701 assignment_operator
1702 : '=' { L CAssignOp (posOf $1) }
1703 | "*=" { L CMulAssOp (posOf $1) }
1704 | "/=" { L CDivAssOp (posOf $1) }
1705 | "%=" { L CRmdAssOp (posOf $1) }
1706 | "+=" { L CAddAssOp (posOf $1) }
1707 | "-=" { L CSubAssOp (posOf $1) }
1708 | "<<=" { L CShlAssOp (posOf $1) }
1709 | ">>=" { L CShrAssOp (posOf $1) }
1710 | "&=" { L CAndAssOp (posOf $1) }
1711 | "^=" { L CXorAssOp (posOf $1) }
1712 | "|=" { L COrAssOp (posOf $1) }
1713
1714
1715 -- parse C expression (C99 6.5.17)
1716 --
1717 expression :: { CExpr }
1718 expression
1719 : assignment_expression
1720 { $1 }
1721
1722 | assignment_expression ',' comma_expression
1723 {% let es = reverse $3 in withAttrs es $ CComma ($1:es) }
1724
1725
1726 comma_expression :: { Reversed [CExpr] }
1727 comma_expression
1728 : assignment_expression { singleton $1 }
1729 | comma_expression ',' assignment_expression { $1 `snoc` $3 }
1730
1731
1732 -- The following was used for clarity
1733 expression_opt :: { Maybe CExpr }
1734 expression_opt
1735 : {- empty -} { Nothing }
1736 | expression { Just $1 }
1737
1738
1739 -- The following was used for clarity
1740 assignment_expression_opt :: { Maybe CExpr }
1741 assignment_expression_opt
1742 : {- empty -} { Nothing }
1743 | assignment_expression { Just $1 }
1744
1745
1746 -- parse C constant expression (C99 6.6)
1747 --
1748 constant_expression :: { CExpr }
1749 constant_expression
1750 : conditional_expression { $1 }
1751
1752
1753 -- parse C constants
1754 --
1755 constant :: { CConst }
1756 constant
1757 : cint {% withAttrs $1 $ case $1 of CTokILit _ i -> CIntConst i }
1758 | cchar {% withAttrs $1 $ case $1 of CTokCLit _ c -> CCharConst c }
1759 | cfloat {% withAttrs $1 $ case $1 of CTokFLit _ f -> CFloatConst f }
1760
1761
1762 string_literal :: { CConst }
1763 string_literal
1764 : cstr
1765 {% withAttrs $1 $ case $1 of CTokSLit _ s -> CStrConst s }
1766
1767 | cstr string_literal_list
1768 {% withAttrs $1 $ case $1 of CTokSLit _ s -> CStrConst (concat (s : reverse $2)) }
1769
1770
1771 string_literal_list :: { Reversed [String] }
1772 string_literal_list
1773 : cstr { case $1 of CTokSLit _ s -> singleton s }
1774 | string_literal_list cstr { case $2 of CTokSLit _ s -> $1 `snoc` s }
1775
1776
1777 identifier :: { Ident }
1778 identifier
1779 : ident { $1 }
1780 | tyident { $1 }
1781
1782
1783
1784 -- parse GNU C attribute annotation (junking the result)
1785 --
1786 attrs_opt :: { () }
1787 attrs_opt
1788 : {- empty -} { () }
1789 | attrs_opt attr { () }
1790
1791
1792 attrs :: { () }
1793 attrs
1794 : attr { () }
1795 | attrs attr { () }
1796
1797
1798 attr :: { () }
1799 attr
1800 : "__attribute__" '(' '(' attribute_list ')' ')' { () }
1801
1802
1803 attribute_list :: { () }
1804 : attribute { () }
1805 | attribute_list ',' attribute { () }
1806
1807
1808 attribute :: { () }
1809 attribute
1810 : {- empty -} { () }
1811 | ident { () }
1812 | const { () }
1813 | ident '(' attribute_params ')' { () }
1814 | ident '(' ')' { () }
1815
1816
1817 attribute_params :: { () }
1818 attribute_params
1819 : attribute_param { () }
1820 | attribute_params ',' attribute_param { () }
1821
1822 attribute_param :: { () }
1823 attribute_param
1824 : constant_expression { () }
1825 | ident '=' cfloat { () }
1826
1827 {
1828
1829 infixr 5 `snoc`
1830
1831 -- Due to the way the grammar is constructed we very often have to build lists
1832 -- in reverse. To make sure we do this consistently and correctly we have a
1833 -- newtype to wrap the reversed style of list:
1834 --
1835 newtype Reversed a = Reversed a
1836
1837 empty :: Reversed [a]
1838 empty = Reversed []
1839
1840 singleton :: a -> Reversed [a]
1841 singleton x = Reversed [x]
1842
1843 snoc :: Reversed [a] -> a -> Reversed [a]
1844 snoc (Reversed xs) x = Reversed (x : xs)
1845
1846 rmap :: (a -> b) -> Reversed [a] -> Reversed [b]
1847 rmap f (Reversed xs) = Reversed (map f xs)
1848
1849 reverse :: Reversed [a] -> [a]
1850 reverse (Reversed xs) = List.reverse xs
1851
1852 -- We occasionally need things to have a location when they don't naturally
1853 -- have one built in as tokens and most AST elements do.
1854 --
1855 data Located a = L !a !Position
1856
1857 unL :: Located a -> a
1858 unL (L a pos) = a
1859
1860 instance Pos (Located a) where
1861 posOf (L _ pos) = pos
1862
1863 {-# INLINE withAttrs #-}
1864 withAttrs :: Pos node => node -> (Attrs -> a) -> P a
1865 withAttrs node mkAttributedNode = do
1866 name <- getNewName
1867 let attrs = newAttrs (posOf node) name
1868 attrs `seq` return (mkAttributedNode attrs)
1869
1870 -- this functions gets used repeatedly so take them out of line:
1871 --
1872 liftTypeQuals :: Reversed [CTypeQual] -> [CDeclSpec]
1873 liftTypeQuals (Reversed xs) = revmap [] xs
1874 where revmap a [] = a
1875 revmap a (x:xs) = revmap (CTypeQual x : a) xs
1876
1877
1878 -- convenient instance, the position of a list of things is the position of
1879 -- the first thing in the list
1880 --
1881 instance Pos a => Pos [a] where
1882 posOf (x:_) = posOf x
1883
1884 instance Pos a => Pos (Reversed a) where
1885 posOf (Reversed x) = posOf x
1886
1887 emptyDeclr = CVarDeclr Nothing (newAttrsOnlyPos nopos)
1888
1889 -- Take the identifiers and use them to update the typedef'ed identifier set
1890 -- if the decl is defining a typedef then we add it to the set,
1891 -- if it's a var decl then that shadows typedefed identifiers
1892 --
1893 doDeclIdent :: [CDeclSpec] -> CDeclr -> P ()
1894 doDeclIdent declspecs declr =
1895 case getCDeclrIdent declr of
1896 Nothing -> return ()
1897 Just ident | any isTypeDef declspecs -> addTypedef ident
1898 | otherwise -> shadowTypedef ident
1899
1900 where isTypeDef (CStorageSpec (CTypedef _)) = True
1901 isTypeDef _ = False
1902
1903 doFuncParamDeclIdent :: CDeclr -> P ()
1904 doFuncParamDeclIdent (CFunDeclr _ params _ _) =
1905 sequence_
1906 [ case getCDeclrIdent declr of
1907 Nothing -> return ()
1908 Just ident -> shadowTypedef ident
1909 | CDecl _ dle _ <- params
1910 , (Just declr, _, _) <- dle ]
1911 doFuncParamDeclIdent (CPtrDeclr _ declr _ ) = doFuncParamDeclIdent declr
1912 doFuncParamDeclIdent _ = return ()
1913
1914 -- extract all identifiers
1915 getCDeclrIdent :: CDeclr -> Maybe Ident
1916 getCDeclrIdent (CVarDeclr optIde _) = optIde
1917 getCDeclrIdent (CPtrDeclr _ declr _) = getCDeclrIdent declr
1918 getCDeclrIdent (CArrDeclr declr _ _ _) = getCDeclrIdent declr
1919 getCDeclrIdent (CFunDeclr declr _ _ _) = getCDeclrIdent declr
1920
1921
1922 happyError :: P a
1923 happyError = parseError
1924
1925 parseC :: String -> Position -> PreCST s s' CHeader
1926 parseC input initialPosition = do
1927 nameSupply <- getNameSupply
1928 let ns = names nameSupply
1929 case execParser header input
1930 initialPosition (map fst builtinTypeNames) ns of
1931 Left header -> return header
1932 Right (message, position) -> raiseFatal "Error in C header file."
1933 position message
1934 }