1 /* GObject introspection: C parser
3 * Copyright (c) 1997 Sandro Sigala <ssigala@globalnet.it>
4 * Copyright (c) 2007-2008 Jürg Billeter <j@bitron.ch>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
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.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40 extern
int yylex (GIGenerator
*igenerator
);
41 static void yyerror(GIGenerator
*igenerator
, const char *s
);
43 static int last_enum_value
= -1;
44 static GHashTable
*const_table
= NULL
;
47 csymbol_new
(CSymbolType type
)
49 CSymbol
*s
= g_new0
(CSymbol
, 1);
55 ctype_free
(CType
* type
)
59 g_list_foreach
(type
->child_list
, (GFunc
)ctype_free
, NULL
);
60 g_list_free
(type
->child_list
);
64 csymbol_free
(CSymbol
* symbol
)
66 g_free
(symbol
->ident
);
67 ctype_free
(symbol
->base_type
);
68 g_free
(symbol
->const_string
);
70 g_slist_foreach
(symbol
->directives
, (GFunc
)cdirective_free
, NULL
);
71 g_slist_free
(symbol
->directives
);
75 csymbol_get_const_boolean
(CSymbol
* symbol
)
77 return
(symbol
->const_int_set
&& symbol
->const_int
) || symbol
->const_string
;
81 ctype_new
(CTypeType type
)
83 CType
*t
= g_new0
(CType
, 1);
89 ctype_copy
(CType
* type
)
91 return g_memdup
(type
, sizeof
(CType
));
95 cbasic_type_new
(const char *name
)
97 CType
*basic_type
= ctype_new
(CTYPE_BASIC_TYPE
);
98 basic_type
->name
= g_strdup
(name
);
103 ctypedef_new
(const char *name
)
105 CType
*typedef_
= ctype_new
(CTYPE_TYPEDEF
);
106 typedef_
->name
= g_strdup
(name
);
111 cstruct_new
(const char *name
)
113 CType
*struct_
= ctype_new
(CTYPE_STRUCT
);
114 struct_
->name
= g_strdup
(name
);
119 cunion_new
(const char *name
)
121 CType
*union_
= ctype_new
(CTYPE_UNION
);
122 union_
->name
= g_strdup
(name
);
127 cenum_new
(const char *name
)
129 CType
*enum_
= ctype_new
(CTYPE_ENUM
);
130 enum_
->name
= g_strdup
(name
);
135 cpointer_new
(CType
* base_type
)
137 CType
*pointer
= ctype_new
(CTYPE_POINTER
);
138 pointer
->base_type
= ctype_copy
(base_type
);
145 CType
*array
= ctype_new
(CTYPE_ARRAY
);
152 CType
*func
= ctype_new
(CTYPE_FUNCTION
);
156 /* use specified type as base type of symbol */
158 csymbol_merge_type
(CSymbol
*symbol
, CType
*type
)
160 CType
**foundation_type
= &(symbol
->base_type
);
161 while
(*foundation_type
!= NULL
) {
162 foundation_type
= &((*foundation_type
)->base_type
);
164 *foundation_type
= ctype_copy
(type
);
168 cdirective_new
(const gchar
*name
,
171 CDirective
*directive
;
173 directive
= g_slice_new
(CDirective
);
174 directive
->name
= g_strdup
(name
);
175 directive
->value
= g_strdup
(value
);
180 cdirective_free
(CDirective
*directive
)
182 g_free
(directive
->name
);
183 g_free
(directive
->value
);
184 g_slice_free
(CDirective
, directive
);
195 StorageClassSpecifier storage_class_specifier
;
196 TypeQualifier type_qualifier
;
197 FunctionSpecifier function_specifier
;
198 UnaryOperator unary_operator
;
201 %parse
-param
{ GIGenerator
* igenerator
}
202 %lex
-param
{ GIGenerator
* igenerator
}
204 %token
<str
> IDENTIFIER
"identifier"
205 %token
<str
> TYPEDEF_NAME
"typedef-name"
207 %token INTEGER FLOATING CHARACTER STRING
209 %token ELLIPSIS ADDEQ SUBEQ MULEQ DIVEQ MODEQ XOREQ ANDEQ OREQ SL SR
210 %token SLEQ SREQ EQ NOTEQ LTEQ GTEQ ANDAND OROR PLUSPLUS MINUSMINUS ARROW
212 %token AUTO BOOL BREAK CASE CHAR CONST CONTINUE DEFAULT DO DOUBLE ELSE ENUM
213 %token EXTERN FLOAT FOR GOTO IF INLINE INT LONG REGISTER RESTRICT RETURN SHORT
214 %token SIGNED SIZEOF STATIC STRUCT SWITCH TYPEDEF UNION UNSIGNED VOID VOLATILE
217 %token FUNCTION_MACRO OBJECT_MACRO
219 %start translation_unit
221 %type
<ctype
> declaration_specifiers
222 %type
<ctype
> enum_specifier
223 %type
<ctype
> pointer
224 %type
<ctype
> specifier_qualifier_list
225 %type
<ctype
> struct_or_union
226 %type
<ctype
> struct_or_union_specifier
227 %type
<ctype
> type_specifier
228 %type
<str
> identifier
229 %type
<str
> typedef_name
230 %type
<str
> identifier_or_typedef_name
231 %type
<symbol
> abstract_declarator
232 %type
<symbol
> init_declarator
233 %type
<symbol
> declarator
234 %type
<symbol
> enumerator
235 %type
<symbol
> direct_abstract_declarator
236 %type
<symbol
> direct_declarator
237 %type
<symbol
> parameter_declaration
238 %type
<symbol
> struct_declarator
239 %type
<list
> enumerator_list
240 %type
<list
> identifier_list
241 %type
<list
> init_declarator_list
242 %type
<list
> parameter_type_list
243 %type
<list
> parameter_list
244 %type
<list
> struct_declaration
245 %type
<list
> struct_declaration_list
246 %type
<list
> struct_declarator_list
247 %type
<storage_class_specifier
> storage_class_specifier
248 %type
<type_qualifier
> type_qualifier
249 %type
<type_qualifier
> type_qualifier_list
250 %type
<function_specifier
> function_specifier
251 %type
<symbol
> expression
252 %type
<symbol
> constant_expression
253 %type
<symbol
> conditional_expression
254 %type
<symbol
> logical_and_expression
255 %type
<symbol
> logical_or_expression
256 %type
<symbol
> inclusive_or_expression
257 %type
<symbol
> exclusive_or_expression
258 %type
<symbol
> multiplicative_expression
259 %type
<symbol
> additive_expression
260 %type
<symbol
> shift_expression
261 %type
<symbol
> relational_expression
262 %type
<symbol
> equality_expression
263 %type
<symbol
> and_expression
264 %type
<symbol
> cast_expression
265 %type
<symbol
> assignment_expression
266 %type
<symbol
> unary_expression
267 %type
<symbol
> postfix_expression
268 %type
<symbol
> primary_expression
269 %type
<unary_operator
> unary_operator
270 %type
<str
> function_macro
271 %type
<str
> object_macro
272 %type
<symbol
> strings
276 /* A.2.1 Expressions. */
281 $$
= g_hash_table_lookup
(const_table
, $1);
283 $$
= csymbol_new
(CSYMBOL_TYPE_INVALID
);
288 $$
= csymbol_new
(CSYMBOL_TYPE_CONST
);
289 $$
->const_int_set
= TRUE
;
290 if
(g_str_has_prefix
(yytext
, "0x") && strlen
(yytext
) > 2) {
291 $$
->const_int
= strtol
(yytext
+ 2, NULL
, 16);
292 } else if
(g_str_has_prefix
(yytext
, "0") && strlen
(yytext
) > 1) {
293 $$
->const_int
= strtol
(yytext
+ 1, NULL
, 8);
295 $$
->const_int
= atoi
(yytext
);
300 $$
= csymbol_new
(CSYMBOL_TYPE_INVALID
);
304 $$
= csymbol_new
(CSYMBOL_TYPE_INVALID
);
313 /* concatenate adjacent string literal tokens */
317 $$
= csymbol_new
(CSYMBOL_TYPE_CONST
);
318 yytext
[strlen
(yytext
) - 1] = '\0';
319 $$
->const_string
= g_strcompress
(yytext
+ 1);
323 char *strings
, *string2
;
325 yytext
[strlen
(yytext
) - 1] = '\0';
326 string2
= g_strcompress
(yytext
+ 1);
327 strings
= g_strconcat
($$
->const_string
, string2
, NULL
);
328 g_free
($$
->const_string
);
330 $$
->const_string
= strings
;
337 $$
= g_strdup
(yytext
);
341 identifier_or_typedef_name
348 | postfix_expression
'[' expression
']'
350 $$
= csymbol_new
(CSYMBOL_TYPE_INVALID
);
352 | postfix_expression
'(' argument_expression_list
')'
354 $$
= csymbol_new
(CSYMBOL_TYPE_INVALID
);
356 | postfix_expression
'(' ')'
358 $$
= csymbol_new
(CSYMBOL_TYPE_INVALID
);
360 | postfix_expression
'.' identifier_or_typedef_name
362 $$
= csymbol_new
(CSYMBOL_TYPE_INVALID
);
364 | postfix_expression ARROW identifier_or_typedef_name
366 $$
= csymbol_new
(CSYMBOL_TYPE_INVALID
);
368 | postfix_expression PLUSPLUS
370 $$
= csymbol_new
(CSYMBOL_TYPE_INVALID
);
372 | postfix_expression MINUSMINUS
374 $$
= csymbol_new
(CSYMBOL_TYPE_INVALID
);
378 argument_expression_list
379 : assignment_expression
380 | argument_expression_list
',' assignment_expression
385 | PLUSPLUS unary_expression
387 $$
= csymbol_new
(CSYMBOL_TYPE_INVALID
);
389 | MINUSMINUS unary_expression
391 $$
= csymbol_new
(CSYMBOL_TYPE_INVALID
);
393 | unary_operator cast_expression
401 $$
->const_int
= -$2->const_int
;
403 case UNARY_BITWISE_COMPLEMENT
:
405 $$
->const_int
= ~
$2->const_int
;
407 case UNARY_LOGICAL_NEGATION
:
409 $$
->const_int
= !csymbol_get_const_boolean
($2);
412 $$
= csymbol_new
(CSYMBOL_TYPE_INVALID
);
416 | SIZEOF unary_expression
418 $$
= csymbol_new
(CSYMBOL_TYPE_INVALID
);
420 | SIZEOF
'(' type_name
')'
422 $$
= csymbol_new
(CSYMBOL_TYPE_INVALID
);
429 $$
= UNARY_ADDRESS_OF
;
433 $$
= UNARY_POINTER_INDIRECTION
;
445 $$
= UNARY_BITWISE_COMPLEMENT
;
449 $$
= UNARY_LOGICAL_NEGATION
;
455 |
'(' type_name
')' cast_expression
461 multiplicative_expression
463 | multiplicative_expression
'*' cast_expression
465 $$
= csymbol_new
(CSYMBOL_TYPE_CONST
);
466 $$
->const_int_set
= TRUE
;
467 $$
->const_int
= $1->const_int
* $3->const_int
;
469 | multiplicative_expression
'/' cast_expression
471 $$
= csymbol_new
(CSYMBOL_TYPE_CONST
);
472 $$
->const_int_set
= TRUE
;
473 if
($3->const_int
!= 0) {
474 $$
->const_int
= $1->const_int
/ $3->const_int
;
477 | multiplicative_expression
'%' cast_expression
479 $$
= csymbol_new
(CSYMBOL_TYPE_CONST
);
480 $$
->const_int_set
= TRUE
;
481 $$
->const_int
= $1->const_int %
$3->const_int
;
486 : multiplicative_expression
487 | additive_expression
'+' multiplicative_expression
489 $$
= csymbol_new
(CSYMBOL_TYPE_CONST
);
490 $$
->const_int_set
= TRUE
;
491 $$
->const_int
= $1->const_int
+ $3->const_int
;
493 | additive_expression
'-' multiplicative_expression
495 $$
= csymbol_new
(CSYMBOL_TYPE_CONST
);
496 $$
->const_int_set
= TRUE
;
497 $$
->const_int
= $1->const_int
- $3->const_int
;
502 : additive_expression
503 | shift_expression SL additive_expression
505 $$
= csymbol_new
(CSYMBOL_TYPE_CONST
);
506 $$
->const_int_set
= TRUE
;
507 $$
->const_int
= $1->const_int
<< $3->const_int
;
509 | shift_expression SR additive_expression
511 $$
= csymbol_new
(CSYMBOL_TYPE_CONST
);
512 $$
->const_int_set
= TRUE
;
513 $$
->const_int
= $1->const_int
>> $3->const_int
;
517 relational_expression
519 | relational_expression
'<' shift_expression
521 $$
= csymbol_new
(CSYMBOL_TYPE_CONST
);
522 $$
->const_int_set
= TRUE
;
523 $$
->const_int
= $1->const_int
< $3->const_int
;
525 | relational_expression
'>' shift_expression
527 $$
= csymbol_new
(CSYMBOL_TYPE_CONST
);
528 $$
->const_int_set
= TRUE
;
529 $$
->const_int
= $1->const_int
> $3->const_int
;
531 | relational_expression LTEQ shift_expression
533 $$
= csymbol_new
(CSYMBOL_TYPE_CONST
);
534 $$
->const_int_set
= TRUE
;
535 $$
->const_int
= $1->const_int
<= $3->const_int
;
537 | relational_expression GTEQ shift_expression
539 $$
= csymbol_new
(CSYMBOL_TYPE_CONST
);
540 $$
->const_int_set
= TRUE
;
541 $$
->const_int
= $1->const_int
>= $3->const_int
;
546 : relational_expression
547 | equality_expression EQ relational_expression
549 $$
= csymbol_new
(CSYMBOL_TYPE_CONST
);
550 $$
->const_int_set
= TRUE
;
551 $$
->const_int
= $1->const_int
== $3->const_int
;
553 | equality_expression NOTEQ relational_expression
555 $$
= csymbol_new
(CSYMBOL_TYPE_CONST
);
556 $$
->const_int_set
= TRUE
;
557 $$
->const_int
= $1->const_int
!= $3->const_int
;
562 : equality_expression
563 | and_expression
'&' equality_expression
565 $$
= csymbol_new
(CSYMBOL_TYPE_CONST
);
566 $$
->const_int_set
= TRUE
;
567 $$
->const_int
= $1->const_int
& $3->const_int
;
571 exclusive_or_expression
573 | exclusive_or_expression
'^' and_expression
575 $$
= csymbol_new
(CSYMBOL_TYPE_CONST
);
576 $$
->const_int_set
= TRUE
;
577 $$
->const_int
= $1->const_int ^
$3->const_int
;
581 inclusive_or_expression
582 : exclusive_or_expression
583 | inclusive_or_expression
'|' exclusive_or_expression
585 $$
= csymbol_new
(CSYMBOL_TYPE_CONST
);
586 $$
->const_int_set
= TRUE
;
587 $$
->const_int
= $1->const_int |
$3->const_int
;
591 logical_and_expression
592 : inclusive_or_expression
593 | logical_and_expression ANDAND inclusive_or_expression
595 $$
= csymbol_new
(CSYMBOL_TYPE_CONST
);
596 $$
->const_int_set
= TRUE
;
597 $$
->const_int
= csymbol_get_const_boolean
($1) && csymbol_get_const_boolean
($3);
601 logical_or_expression
602 : logical_and_expression
603 | logical_or_expression OROR logical_and_expression
605 $$
= csymbol_new
(CSYMBOL_TYPE_CONST
);
606 $$
->const_int_set
= TRUE
;
607 $$
->const_int
= csymbol_get_const_boolean
($1) || csymbol_get_const_boolean
($3);
611 conditional_expression
612 : logical_or_expression
613 | logical_or_expression
'?' expression
':' conditional_expression
615 $$
= csymbol_get_const_boolean
($1) ?
$3 : $5;
619 assignment_expression
620 : conditional_expression
621 | unary_expression assignment_operator assignment_expression
623 $$
= csymbol_new
(CSYMBOL_TYPE_INVALID
);
642 : assignment_expression
643 | expression
',' assignment_expression
645 $$
= csymbol_new
(CSYMBOL_TYPE_INVALID
);
650 : conditional_expression
653 /* A.2.2 Declarations. */
656 : declaration_specifiers init_declarator_list
';'
659 for
(l
= $2; l
!= NULL
; l
= l
->next
) {
660 CSymbol
*sym
= l
->data
;
661 csymbol_merge_type
(sym
, $1);
662 if
($1->storage_class_specifier
& STORAGE_CLASS_TYPEDEF
) {
663 sym
->type
= CSYMBOL_TYPE_TYPEDEF
;
664 } else if
(sym
->base_type
->type
== CTYPE_FUNCTION
) {
665 sym
->type
= CSYMBOL_TYPE_FUNCTION
;
667 sym
->type
= CSYMBOL_TYPE_OBJECT
;
669 g_igenerator_add_symbol
(igenerator
, sym
);
672 | declaration_specifiers
';'
675 declaration_specifiers
676 : storage_class_specifier declaration_specifiers
679 $$
->storage_class_specifier |
= $1;
681 | storage_class_specifier
683 $$
= ctype_new
(CTYPE_INVALID
);
684 $$
->storage_class_specifier |
= $1;
686 | type_specifier declaration_specifiers
692 | type_qualifier declaration_specifiers
695 $$
->type_qualifier |
= $1;
699 $$
= ctype_new
(CTYPE_INVALID
);
700 $$
->type_qualifier |
= $1;
702 | function_specifier declaration_specifiers
705 $$
->function_specifier |
= $1;
709 $$
= ctype_new
(CTYPE_INVALID
);
710 $$
->function_specifier |
= $1;
717 $$
= g_list_append
(NULL
, $1);
719 | init_declarator_list
',' init_declarator
721 $$
= g_list_append
($1, $3);
727 | declarator
'=' initializer
730 storage_class_specifier
733 $$
= STORAGE_CLASS_TYPEDEF
;
737 $$
= STORAGE_CLASS_EXTERN
;
741 $$
= STORAGE_CLASS_STATIC
;
745 $$
= STORAGE_CLASS_AUTO
;
749 $$
= STORAGE_CLASS_REGISTER
;
756 $$
= ctype_new
(CTYPE_VOID
);
760 $$
= cbasic_type_new
("char");
764 $$
= cbasic_type_new
("short");
768 $$
= cbasic_type_new
("int");
772 $$
= cbasic_type_new
("long");
776 $$
= cbasic_type_new
("float");
780 $$
= cbasic_type_new
("double");
784 $$
= cbasic_type_new
("signed");
788 $$
= cbasic_type_new
("unsigned");
792 $$
= cbasic_type_new
("bool");
794 | struct_or_union_specifier
798 $$
= ctypedef_new
($1);
802 struct_or_union_specifier
803 : struct_or_union identifier_or_typedef_name
'{' struct_declaration_list
'}'
809 CSymbol
*sym
= csymbol_new
(CSYMBOL_TYPE_INVALID
);
810 if
($$
->type
== CTYPE_STRUCT
) {
811 sym
->type
= CSYMBOL_TYPE_STRUCT
;
812 } else if
($$
->type
== CTYPE_UNION
) {
813 sym
->type
= CSYMBOL_TYPE_UNION
;
815 g_assert_not_reached
();
817 sym
->ident
= g_strdup
($$
->name
);
818 sym
->base_type
= ctype_copy
($$
);
819 g_igenerator_add_symbol
(igenerator
, sym
);
821 | struct_or_union
'{' struct_declaration_list
'}'
826 | struct_or_union identifier_or_typedef_name
836 $$
= cstruct_new
(NULL
);
840 $$
= cunion_new
(NULL
);
844 struct_declaration_list
846 | struct_declaration_list struct_declaration
848 $$
= g_list_concat
($1, $2);
853 : specifier_qualifier_list struct_declarator_list
';'
857 for
(l
= $2; l
!= NULL
; l
= l
->next
) {
858 CSymbol
*sym
= l
->data
;
859 if
($1->storage_class_specifier
& STORAGE_CLASS_TYPEDEF
) {
860 sym
->type
= CSYMBOL_TYPE_TYPEDEF
;
862 csymbol_merge_type
(sym
, $1);
863 $$
= g_list_append
($$
, sym
);
868 specifier_qualifier_list
869 : type_specifier specifier_qualifier_list
875 | type_qualifier specifier_qualifier_list
878 $$
->type_qualifier |
= $1;
882 $$
= ctype_new
(CTYPE_INVALID
);
883 $$
->type_qualifier |
= $1;
887 struct_declarator_list
890 $$
= g_list_append
(NULL
, $1);
892 | struct_declarator_list
',' struct_declarator
894 $$
= g_list_append
($1, $3);
899 : /* empty, support for anonymous structs and unions */
901 $$
= csymbol_new
(CSYMBOL_TYPE_INVALID
);
904 |
':' constant_expression
906 $$
= csymbol_new
(CSYMBOL_TYPE_INVALID
);
908 | declarator
':' constant_expression
912 : ENUM identifier_or_typedef_name
'{' enumerator_list
'}'
916 last_enum_value
= -1;
918 | ENUM
'{' enumerator_list
'}'
920 $$
= cenum_new
(NULL
);
922 last_enum_value
= -1;
924 | ENUM identifier_or_typedef_name
'{' enumerator_list
',' '}'
928 last_enum_value
= -1;
930 | ENUM
'{' enumerator_list
',' '}'
932 $$
= cenum_new
(NULL
);
934 last_enum_value
= -1;
936 | ENUM identifier_or_typedef_name
945 $$
= g_list_append
(NULL
, $1);
947 | enumerator_list
',' enumerator
949 $$
= g_list_append
($1, $3);
956 $$
= csymbol_new
(CSYMBOL_TYPE_OBJECT
);
958 $$
->const_int_set
= TRUE
;
959 $$
->const_int
= ++last_enum_value
;
960 g_hash_table_insert
(const_table
, g_strdup
($$
->ident
), $$
);
962 | identifier
'=' constant_expression
964 $$
= csymbol_new
(CSYMBOL_TYPE_OBJECT
);
966 $$
->const_int_set
= TRUE
;
967 $$
->const_int
= $3->const_int
;
968 last_enum_value
= $$
->const_int
;
969 g_hash_table_insert
(const_table
, g_strdup
($$
->ident
), $$
);
976 $$
= TYPE_QUALIFIER_CONST
;
980 $$
= TYPE_QUALIFIER_RESTRICT
;
984 $$
= TYPE_QUALIFIER_VOLATILE
;
991 $$
= FUNCTION_INLINE
;
996 : pointer direct_declarator
999 csymbol_merge_type
($$
, $1);
1007 $$
= csymbol_new
(CSYMBOL_TYPE_INVALID
);
1010 |
'(' declarator
')'
1014 | direct_declarator
'[' assignment_expression
']'
1017 csymbol_merge_type
($$
, carray_new
());
1019 | direct_declarator
'[' ']'
1022 csymbol_merge_type
($$
, carray_new
());
1024 | direct_declarator
'(' parameter_type_list
')'
1026 CType
*func
= cfunction_new
();
1027 // ignore (void) parameter list
1028 if
($3 != NULL
&& ($3->next
!= NULL ||
((CSymbol
*) $3->data
)->base_type
->type
!= CTYPE_VOID
)) {
1029 func
->child_list
= $3;
1032 csymbol_merge_type
($$
, func
);
1034 | direct_declarator
'(' identifier_list
')'
1036 CType
*func
= cfunction_new
();
1037 func
->child_list
= $3;
1039 csymbol_merge_type
($$
, func
);
1041 | direct_declarator
'(' ')'
1043 CType
*func
= cfunction_new
();
1045 csymbol_merge_type
($$
, func
);
1050 : '*' type_qualifier_list
1052 $$
= cpointer_new
(NULL
);
1053 $$
->type_qualifier
= $2;
1057 $$
= cpointer_new
(NULL
);
1059 |
'*' type_qualifier_list pointer
1061 $$
= cpointer_new
($3);
1062 $$
->type_qualifier
= $2;
1066 $$
= cpointer_new
($2);
1072 | type_qualifier_list type_qualifier
1080 | parameter_list
',' ELLIPSIS
1084 : parameter_declaration
1086 $$
= g_list_append
(NULL
, $1);
1088 | parameter_list
',' parameter_declaration
1090 $$
= g_list_append
($1, $3);
1094 parameter_declaration
1095 : declaration_specifiers declarator
1098 csymbol_merge_type
($$
, $1);
1100 | declaration_specifiers abstract_declarator
1103 csymbol_merge_type
($$
, $1);
1105 | declaration_specifiers
1107 $$
= csymbol_new
(CSYMBOL_TYPE_INVALID
);
1115 CSymbol
*sym
= csymbol_new
(CSYMBOL_TYPE_INVALID
);
1117 $$
= g_list_append
(NULL
, sym
);
1119 | identifier_list
',' identifier
1121 CSymbol
*sym
= csymbol_new
(CSYMBOL_TYPE_INVALID
);
1123 $$
= g_list_append
($1, sym
);
1128 : specifier_qualifier_list
1129 | specifier_qualifier_list abstract_declarator
1135 $$
= csymbol_new
(CSYMBOL_TYPE_INVALID
);
1136 csymbol_merge_type
($$
, $1);
1138 | direct_abstract_declarator
1139 | pointer direct_abstract_declarator
1142 csymbol_merge_type
($$
, $1);
1146 direct_abstract_declarator
1147 : '(' abstract_declarator
')'
1153 $$
= csymbol_new
(CSYMBOL_TYPE_INVALID
);
1154 csymbol_merge_type
($$
, carray_new
());
1156 |
'[' assignment_expression
']'
1158 $$
= csymbol_new
(CSYMBOL_TYPE_INVALID
);
1159 csymbol_merge_type
($$
, carray_new
());
1161 | direct_abstract_declarator
'[' ']'
1164 csymbol_merge_type
($$
, carray_new
());
1166 | direct_abstract_declarator
'[' assignment_expression
']'
1169 csymbol_merge_type
($$
, carray_new
());
1173 CType
*func
= cfunction_new
();
1174 $$
= csymbol_new
(CSYMBOL_TYPE_INVALID
);
1175 csymbol_merge_type
($$
, func
);
1177 |
'(' parameter_type_list
')'
1179 CType
*func
= cfunction_new
();
1180 // ignore (void) parameter list
1181 if
($2 != NULL
&& ($2->next
!= NULL ||
((CSymbol
*) $2->data
)->base_type
->type
!= CTYPE_VOID
)) {
1182 func
->child_list
= $2;
1184 $$
= csymbol_new
(CSYMBOL_TYPE_INVALID
);
1185 csymbol_merge_type
($$
, func
);
1187 | direct_abstract_declarator
'(' ')'
1189 CType
*func
= cfunction_new
();
1191 csymbol_merge_type
($$
, func
);
1193 | direct_abstract_declarator
'(' parameter_type_list
')'
1195 CType
*func
= cfunction_new
();
1196 // ignore (void) parameter list
1197 if
($3 != NULL
&& ($3->next
!= NULL ||
((CSymbol
*) $3->data
)->base_type
->type
!= CTYPE_VOID
)) {
1198 func
->child_list
= $3;
1201 csymbol_merge_type
($$
, func
);
1208 $$
= g_strdup
(yytext
);
1213 : assignment_expression
1214 |
'{' initializer_list
'}'
1215 |
'{' initializer_list
',' '}'
1220 | initializer_list
',' initializer
1223 /* A.2.3 Statements. */
1227 | compound_statement
1228 | expression_statement
1229 | selection_statement
1230 | iteration_statement
1235 : identifier_or_typedef_name
':' statement
1236 | CASE constant_expression
':' statement
1237 | DEFAULT
':' statement
1242 |
'{' block_item_list
'}'
1247 | block_item_list block_item
1255 expression_statement
1261 : IF
'(' expression
')' statement
1262 | IF
'(' expression
')' statement ELSE statement
1263 | SWITCH
'(' expression
')' statement
1267 : WHILE
'(' expression
')' statement
1268 | DO statement WHILE
'(' expression
')' ';'
1269 | FOR
'(' ';' ';' ')' statement
1270 | FOR
'(' expression
';' ';' ')' statement
1271 | FOR
'(' ';' expression
';' ')' statement
1272 | FOR
'(' expression
';' expression
';' ')' statement
1273 | FOR
'(' ';' ';' expression
')' statement
1274 | FOR
'(' expression
';' ';' expression
')' statement
1275 | FOR
'(' ';' expression
';' expression
')' statement
1276 | FOR
'(' expression
';' expression
';' expression
')' statement
1280 : GOTO identifier_or_typedef_name
';'
1284 | RETURN expression
';'
1287 /* A.2.4 External definitions. */
1290 : external_declaration
1291 | translation_unit external_declaration
1294 external_declaration
1295 : function_definition
1301 : declaration_specifiers declarator declaration_list compound_statement
1302 | declaration_specifiers declarator compound_statement
1307 | declaration_list declaration
1315 $$
= g_strdup
(yytext
+ strlen
("#define "));
1322 $$
= g_strdup
(yytext
+ strlen
("#define "));
1326 function_macro_define
1327 : function_macro
'(' identifier_list
')'
1331 : object_macro constant_expression
1333 if
($2->const_int_set ||
$2->const_string
!= NULL
) {
1335 g_igenerator_add_symbol
(igenerator
, $2);
1341 : function_macro_define
1342 | object_macro_define
1349 yyerror (GIGenerator
*igenerator
, const char *s
)
1351 /* ignore errors while doing a macro scan as not all object macros
1352 * have valid expressions */
1353 if
(!igenerator
->macro_scan
)
1355 fprintf
(stderr
, "%s:%d: %s\n",
1356 igenerator
->current_filename
, lineno
, s
);
1361 g_igenerator_parse_file
(GIGenerator
*igenerator
, FILE *file
)
1363 g_return_val_if_fail
(file
!= NULL
, FALSE
);
1365 const_table
= g_hash_table_new_full
(g_str_hash
, g_str_equal
,
1370 yyparse (igenerator
);
1372 g_hash_table_destroy
(const_table
);