-
Notifications
You must be signed in to change notification settings - Fork 273
/
Copy pathparser.y
3781 lines (3489 loc) · 113 KB
/
parser.y
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
%{
/*
* This parser is based on:
*
* c5.y, a ANSI-C grammar written by James A. Roskind.
* "Portions Copyright (c) 1989, 1990 James A. Roskind".
* (http://www.idiom.com/free-compilers/,
* ftp://ftp.infoseek.com/ftp/pub/c++grammar/,
* ftp://ftp.sra.co.jp/.a/pub/cmd/c++grammar2.0.tar.gz)
*/
#ifdef ANSI_C_DEBUG
#define YYDEBUG 1
#endif
#define PARSER (*ansi_c_parser)
#include "ansi_c_parser.h"
int yyansi_clex();
extern char *yyansi_ctext;
static ansi_c_parsert *ansi_c_parser;
int yyansi_cparse(void);
int yyansi_cparse(ansi_c_parsert &_ansi_c_parser)
{
ansi_c_parser = &_ansi_c_parser;
return yyansi_cparse();
}
int yyansi_cerror(const std::string &error);
#include "parser_static.inc"
#include "literals/convert_integer_literal.h"
#include "ansi_c_y.tab.h"
#include <util/mathematical_expr.h>
#ifdef _MSC_VER
// possible loss of data
#pragma warning(disable:4242)
// possible loss of data
#pragma warning(disable:4244)
// signed/unsigned mismatch
#pragma warning(disable:4365)
// switch with default but no case labels
#pragma warning(disable:4065)
// unreachable code
#pragma warning(disable:4702)
#endif
// statements have right recursion, deep nesting of statements thus
// requires more stack space
#define YYMAXDEPTH 25600
%}
/*** token declaration **************************************************/
/*** ANSI-C keywords ***/
%token TOK_AUTO "auto"
%token TOK_BOOL "bool"
%token TOK_COMPLEX "complex"
%token TOK_BREAK "break"
%token TOK_CASE "case"
%token TOK_CHAR "char"
%token TOK_CONST "const"
%token TOK_CONTINUE "continue"
%token TOK_DEFAULT "default"
%token TOK_DO "do"
%token TOK_DOUBLE "double"
%token TOK_ELSE "else"
%token TOK_ENUM "enum"
%token TOK_EXTERN "extern"
%token TOK_FLOAT "float"
%token TOK_FOR "for"
%token TOK_GOTO "goto"
%token TOK_IF "if"
%token TOK_INLINE "inline"
%token TOK_INT "int"
%token TOK_LONG "long"
%token TOK_REGISTER "register"
%token TOK_RESTRICT "restrict"
%token TOK_RETURN "return"
%token TOK_SHORT "short"
%token TOK_SIGNED "signed"
%token TOK_SIZEOF "sizeof"
%token TOK_STATIC "static"
%token TOK_STRUCT "struct"
%token TOK_SWITCH "switch"
%token TOK_TYPEDEF "typedef"
%token TOK_UNION "union"
%token TOK_UNSIGNED "unsigned"
%token TOK_VOID "void"
%token TOK_VOLATILE "volatile"
%token TOK_WCHAR_T "wchar_t"
%token TOK_WHILE "while"
/*** multi-character operators ***/
%token TOK_ARROW "->"
%token TOK_INCR "++"
%token TOK_DECR "--"
%token TOK_SHIFTLEFT "<<"
%token TOK_SHIFTRIGHT ">>"
%token TOK_LE "<="
%token TOK_GE ">="
%token TOK_EQ "=="
%token TOK_NE "!="
%token TOK_ANDAND "&&"
%token TOK_OROR "||"
%token TOK_ELLIPSIS "..."
/*** modifying assignment operators ***/
%token TOK_MULTASSIGN "*="
%token TOK_DIVASSIGN "/="
%token TOK_MODASSIGN "%="
%token TOK_PLUSASSIGN "+="
%token TOK_MINUSASSIGN "-="
%token TOK_SHLASSIGN "<<="
%token TOK_SHRASSIGN ">>="
%token TOK_ANDASSIGN "&="
%token TOK_XORASSIGN "^="
%token TOK_ORASSIGN "|="
/*** scanner parsed tokens (these have a value!) ***/
%token TOK_GCC_IDENTIFIER
%token TOK_MSC_IDENTIFIER
%token TOK_TYPEDEFNAME
%token TOK_INTEGER
%token TOK_FLOATING
%token TOK_CHARACTER
%token TOK_STRING
%token TOK_ASM_STRING
/*** extensions ***/
%token TOK_INT8 "__int8"
%token TOK_INT16 "__int16"
%token TOK_INT32 "__int32"
%token TOK_INT64 "__int64"
%token TOK_PTR32 "__ptr32"
%token TOK_PTR64 "__ptr64"
%token TOK_TYPEOF "typeof"
%token TOK_GCC_AUTO_TYPE "__auto_type"
%token TOK_GCC_FLOAT16 "_Float16"
%token TOK_GCC_FLOAT32 "_Float32"
%token TOK_GCC_FLOAT32X "_Float32x"
%token TOK_GCC_FLOAT80 "__float80"
%token TOK_GCC_FLOAT64 "_Float64"
%token TOK_GCC_FLOAT64X "_Float64x"
%token TOK_GCC_FLOAT128 "_Float128"
%token TOK_GCC_FLOAT128X "_Float128x"
%token TOK_GCC_INT128 "__int128"
%token TOK_GCC_DECIMAL32 "_Decimal32"
%token TOK_GCC_DECIMAL64 "_Decimal64"
%token TOK_GCC_DECIMAL128 "_Decimal128"
%token TOK_GCC_ASM "__asm__"
%token TOK_GCC_ASM_PAREN "__asm__ (with parentheses)"
%token TOK_GCC_ATTRIBUTE "__attribute__"
%token TOK_GCC_ATTRIBUTE_ALIGNED "aligned"
%token TOK_GCC_ATTRIBUTE_TRANSPARENT_UNION "transparent_union"
%token TOK_GCC_ATTRIBUTE_PACKED "packed"
%token TOK_GCC_ATTRIBUTE_VECTOR_SIZE "vector_size"
%token TOK_GCC_ATTRIBUTE_MODE "mode"
%token TOK_GCC_ATTRIBUTE_GNU_INLINE "__gnu_inline__"
%token TOK_GCC_ATTRIBUTE_WEAK "weak"
%token TOK_GCC_ATTRIBUTE_ALIAS "alias"
%token TOK_GCC_ATTRIBUTE_SECTION "section"
%token TOK_GCC_ATTRIBUTE_NORETURN "noreturn"
%token TOK_GCC_ATTRIBUTE_CONSTRUCTOR "constructor"
%token TOK_GCC_ATTRIBUTE_DESTRUCTOR "destructor"
%token TOK_GCC_ATTRIBUTE_FALLTHROUGH "fallthrough"
%token TOK_GCC_ATTRIBUTE_USED "used"
%token TOK_GCC_LABEL "__label__"
%token TOK_MSC_ASM "__asm"
%token TOK_MSC_BASED "__based"
%token TOK_CW_VAR_ARG_TYPEOF "_var_arg_typeof"
%token TOK_BUILTIN_VA_ARG "__builtin_va_arg"
%token TOK_GCC_BUILTIN_TYPES_COMPATIBLE_P "__builtin_types_compatible_p"
%token TOK_CLANG_BUILTIN_CONVERTVECTOR "__builtin_convertvector"
%token TOK_OFFSETOF "__offsetof"
%token TOK_ALIGNOF "__alignof__"
%token TOK_MSC_TRY "__try"
%token TOK_MSC_FINALLY "__finally"
%token TOK_MSC_EXCEPT "__except"
%token TOK_MSC_LEAVE "__leave"
%token TOK_MSC_DECLSPEC "__declspec"
%token TOK_MSC_FORCEINLINE "__forceinline"
%token TOK_INTERFACE "__interface"
%token TOK_CDECL "__cdecl"
%token TOK_STDCALL "__stdcall"
%token TOK_FASTCALL "__fastcall"
%token TOK_CLRCALL "__clrcall"
%token TOK_FORALL "forall"
%token TOK_EXISTS "exists"
%token TOK_ACSL_FORALL "\\forall"
%token TOK_ACSL_EXISTS "\\exists"
%token TOK_ACSL_LAMBDA "\\lambda"
%token TOK_ACSL_LET "\\let"
%token TOK_ARRAY_OF "array_of"
%token TOK_CPROVER_BITVECTOR "__CPROVER_bitvector"
%token TOK_CPROVER_FLOATBV "__CPROVER_floatbv"
%token TOK_CPROVER_FIXEDBV "__CPROVER_fixedbv"
%token TOK_CPROVER_ATOMIC "__CPROVER_atomic"
%token TOK_CPROVER_BOOL "__CPROVER_bool"
%token TOK_CPROVER_THROW "__CPROVER_throw"
%token TOK_CPROVER_CATCH "__CPROVER_catch"
%token TOK_CPROVER_TRY "__CPROVER_try"
%token TOK_CPROVER_FINALLY "__CPROVER_finally"
%token TOK_CPROVER_ID "__CPROVER_ID"
%token TOK_CPROVER_LOOP_INVARIANT "__CPROVER_loop_invariant"
%token TOK_CPROVER_DECREASES "__CPROVER_decreases"
%token TOK_CPROVER_REQUIRES "__CPROVER_requires"
%token TOK_CPROVER_ENSURES "__CPROVER_ensures"
%token TOK_CPROVER_ASSIGNS "__CPROVER_assigns"
%token TOK_CPROVER_FREES "__CPROVER_frees"
%token TOK_IMPLIES "==>"
%token TOK_EQUIVALENT "<==>"
%token TOK_XORXOR "^^"
%token TOK_TRUE "TRUE"
%token TOK_FALSE "FALSE"
%token TOK_REAL "__real__"
%token TOK_IMAG "__imag__"
%token TOK_ALIGNAS "_Alignas"
%token TOK_ATOMIC_TYPE_QUALIFIER "_Atomic"
%token TOK_ATOMIC_TYPE_SPECIFIER "_Atomic()"
%token TOK_GENERIC "_Generic"
%token TOK_IMAGINARY "_Imaginary"
%token TOK_NORETURN "_Noreturn"
%token TOK_STATIC_ASSERT "_Static_assert"
%token TOK_THREAD_LOCAL "_Thread_local"
%token TOK_NULLPTR "nullptr"
%token TOK_CONSTEXPR "constexpr"
%token TOK_BIT_CAST "__builtin_bit_cast"
/*** special scanner reports ***/
%token TOK_SCANNER_ERROR /* used by scanner to report errors */
%token TOK_SCANNER_EOF /* used by scanner to report end of import */
/*** these exist only for the benefit of the C++ frontend */
%token TOK_CATCH "catch"
%token TOK_CHAR16_T "char16_t"
%token TOK_CHAR32_T "char32_t"
%token TOK_CLASS "class"
%token TOK_DELETE "delete"
%token TOK_DECLTYPE "decltype"
%token TOK_EXPLICIT "explicit"
%token TOK_FRIEND "friend"
%token TOK_MUTABLE "mutable"
%token TOK_NAMESPACE "namespace"
%token TOK_NEW "new"
%token TOK_NODISCARD "nodiscard"
%token TOK_NOEXCEPT "noexcept"
%token TOK_OPERATOR "operator"
%token TOK_PRIVATE "private"
%token TOK_PROTECTED "protected"
%token TOK_PUBLIC "public"
%token TOK_TEMPLATE "template"
%token TOK_THIS "this"
%token TOK_THROW "throw"
%token TOK_TYPEID "typeid"
%token TOK_TYPENAME "typename"
%token TOK_TRY "try"
%token TOK_USING "using"
%token TOK_VIRTUAL "virtual"
%token TOK_SCOPE "::"
%token TOK_DOTPM ".*"
%token TOK_ARROWPM "->*"
%token TOK_UNARY_TYPE_PREDICATE
%token TOK_BINARY_TYPE_PREDICATE
%token TOK_MSC_UUIDOF "__uuidof"
%token TOK_MSC_IF_EXISTS "__if_exists"
%token TOK_MSC_IF_NOT_EXISTS "__if_not_exists"
%token TOK_UNDERLYING_TYPE "__underlying_type"
%token TOK_DYNAMIC_CAST "dynamic_cast"
%token TOK_STATIC_CAST "static_cast"
%token TOK_REINTERPRET_CAST "reinterpret_cast"
%token TOK_CONST_CAST "const_cast"
/*** priority, associativity, etc. definitions **************************/
%start grammar
%expect 2 /* the famous "dangling `else'" ambiguity */
/* results in one shift/reduce conflict */
/* that we don't want to be reported */
/* a second shift/reduce conflict arises due to enum underlying */
/* type specifications and bitfield specifications, which are both */
/* introduced by a ':' and follow a type */
%{
/************************************************************************/
/*** rules **************************************************************/
/************************************************************************/
%}
%%
grammar:
translation_unit
;
/*** Token with values **************************************************/
identifier:
TOK_GCC_IDENTIFIER
| TOK_MSC_IDENTIFIER
| TOK_CPROVER_ID TOK_STRING
{
// construct an identifier from a string that would otherwise not be a
// valid identifier in C
$$=$1;
parser_stack($$).id(ID_symbol);
irep_idt value=parser_stack($2).get(ID_value);
parser_stack($$).set(ID_C_base_name, value);
parser_stack($$).set(ID_identifier, value);
parser_stack($$).set(
ID_C_id_class, static_cast<int>(ansi_c_id_classt::ANSI_C_SYMBOL));
}
;
typedef_name:
TOK_TYPEDEFNAME
;
integer:
TOK_INTEGER
;
floating:
TOK_FLOATING
;
character:
TOK_CHARACTER
;
string:
TOK_STRING
;
/*** Constants **********************************************************/
constant: integer
| floating
| character
| string
;
/*** Expressions ********************************************************/
primary_expression:
identifier
| constant
| '(' comma_expression ')'
{ $$ = $2; }
| statement_expression
| gcc_builtin_expressions
| clang_builtin_expressions
| cw_builtin_expressions
| offsetof
| quantifier_expression
| generic_selection
;
generic_selection:
TOK_GENERIC '(' assignment_expression ',' generic_assoc_list ')'
{
$$=$1;
set($$, ID_generic_selection);
mto($$, $3);
parser_stack($$).add(ID_generic_associations).get_sub().swap((irept::subt&)parser_stack($5).operands());
}
;
generic_assoc_list:
generic_association
{
init($$); mto($$, $1);
}
| generic_assoc_list ',' generic_association
{
$$=$1; mto($$, $3);
}
;
generic_association:
type_name ':' assignment_expression
{
$$=$2;
parser_stack($$).id(ID_generic_association);
parser_stack($$).set(ID_type_arg, parser_stack($1));
parser_stack($$).set(ID_value, parser_stack($3));
}
| TOK_DEFAULT ':' assignment_expression
{
$$=$2;
parser_stack($$).id(ID_generic_association);
parser_stack($$).set(ID_type_arg, irept(ID_default));
parser_stack($$).set(ID_value, parser_stack($3));
}
;
gcc_builtin_expressions:
TOK_BUILTIN_VA_ARG '(' assignment_expression ',' type_name ')'
{
$$=$1;
parser_stack($$).id(ID_gcc_builtin_va_arg);
mto($$, $3);
parser_stack($$).type().swap(parser_stack($5));
}
| TOK_GCC_BUILTIN_TYPES_COMPATIBLE_P '('
type_name ',' type_name ')'
{
$$=$1;
parser_stack($$).id(ID_gcc_builtin_types_compatible_p);
auto &type_arg=static_cast<type_with_subtypest &>(parser_stack($$).add(ID_type_arg));
auto &subtypes=type_arg.subtypes();
subtypes.resize(2);
subtypes[0].swap(parser_stack($3));
subtypes[1].swap(parser_stack($5));
}
;
clang_builtin_expressions:
TOK_CLANG_BUILTIN_CONVERTVECTOR '(' assignment_expression ',' type_name ')'
{
$$=$1;
parser_stack($$).id(ID_clang_builtin_convertvector);
mto($$, $3);
parser_stack($$).type().swap(parser_stack($5));
}
;
cw_builtin_expressions:
TOK_CW_VAR_ARG_TYPEOF '(' type_name ')'
{
$$=$1;
parser_stack($$).id(ID_cw_va_arg_typeof);
parser_stack($$).add(ID_type_arg).swap(parser_stack($3));
}
;
offsetof:
TOK_OFFSETOF '(' type_name ',' offsetof_member_designator ')'
{
$$=$1;
parser_stack($$).id(ID_builtin_offsetof);
parser_stack($$).add(ID_type_arg).swap(parser_stack($3));
parser_stack($$).add(ID_designator).swap(parser_stack($5));
}
;
offsetof_member_designator:
member_name
{
init($$);
exprt op{ID_member};
op.add_source_location()=parser_stack($1).source_location();
op.set(ID_component_name, parser_stack($1).get(ID_C_base_name));
parser_stack($$).add_to_operands(std::move(op));
}
| offsetof_member_designator '.' member_name
{
$$=$1;
set($2, ID_member);
parser_stack($2).set(ID_component_name, parser_stack($3).get(ID_C_base_name));
mto($$, $2);
}
| offsetof_member_designator '[' comma_expression ']'
{
$$=$1;
set($2, ID_index);
mto($2, $3);
mto($$, $2);
}
| offsetof_member_designator TOK_ARROW member_name
{
$$=$1;
set($2, ID_index);
parser_stack($2).add_to_operands(convert_integer_literal("0"));
mto($$, $2);
set($2, ID_member);
parser_stack($2).set(ID_component_name, parser_stack($3).get(ID_C_base_name));
mto($$, $2);
}
;
quantifier_expression:
TOK_FORALL compound_scope '{' declaration comma_expression '}'
{
$$=$1;
set($$, ID_forall);
parser_stack($$).add_to_operands(tuple_exprt( { std::move(parser_stack($4)) } ));
mto($$, $5);
PARSER.pop_scope();
}
| TOK_EXISTS compound_scope '{' declaration comma_expression '}'
{
$$=$1;
set($$, ID_exists);
parser_stack($$).add_to_operands(tuple_exprt( { std::move(parser_stack($4)) } ));
mto($$, $5);
PARSER.pop_scope();
}
;
cprover_contract_loop_invariant:
TOK_CPROVER_LOOP_INVARIANT '(' ACSL_binding_expression ')'
{ $$=$3; }
;
cprover_contract_loop_invariant_list:
cprover_contract_loop_invariant
{ init($$); mto($$, $1); }
| cprover_contract_loop_invariant_list cprover_contract_loop_invariant
{ $$=$1; mto($$, $2); }
;
cprover_contract_loop_invariant_list_opt:
/* nothing */
{ init($$); parser_stack($$).make_nil(); }
| cprover_contract_loop_invariant_list
;
ACSL_binding_expression_list:
ACSL_binding_expression
{ init($$); mto($$, $1); }
| ACSL_binding_expression_list ',' ACSL_binding_expression
{ $$=$1; mto($$, $3); }
;
cprover_contract_decreases_opt:
/* nothing */
{ init($$); parser_stack($$).make_nil(); }
| TOK_CPROVER_DECREASES '(' ACSL_binding_expression_list ')'
{ $$=$3; }
;
statement_expression: '(' compound_statement ')'
{
$$=$1;
set($$, ID_side_effect);
parser_stack($$).set(ID_statement, ID_statement_expression);
mto($$, $2);
}
;
postfix_expression:
primary_expression
| postfix_expression '[' comma_expression ']'
{ binary($$, $1, $2, ID_index, $3); }
| postfix_expression '(' ')'
{ $$=$2;
set($$, ID_side_effect);
auto &side_effect = to_side_effect_expr(parser_stack($$));
side_effect.set_statement(ID_function_call);
side_effect.operands().resize(2);
to_binary_expr(side_effect).op0().swap(parser_stack($1));
to_binary_expr(side_effect).op1().clear();
to_binary_expr(side_effect).op1().id(ID_arguments);
}
| postfix_expression '(' argument_expression_list ')'
{ $$=$2;
set($$, ID_side_effect);
auto &side_effect = to_side_effect_expr(parser_stack($$));
side_effect.set_statement(ID_function_call);
side_effect.operands().resize(2);
to_binary_expr(side_effect).op0().swap(parser_stack($1));
to_binary_expr(side_effect).op1().swap(parser_stack($3));
to_binary_expr(side_effect).op1().id(ID_arguments);
}
| postfix_expression '.' member_name
{ $$=$2;
set($$, ID_member);
mto($$, $1);
parser_stack($$).set(ID_component_name, parser_stack($3).get(ID_C_base_name));
}
| postfix_expression TOK_ARROW member_name
{ $$=$2;
set($$, ID_ptrmember);
mto($$, $1);
parser_stack($$).set(ID_component_name, parser_stack($3).get(ID_C_base_name));
}
| postfix_expression TOK_INCR
{ $$=$2;
set($$, ID_side_effect);
parser_stack($$).set(ID_statement, ID_postincrement);
mto($$, $1);
}
| postfix_expression TOK_DECR
{ $$=$2;
set($$, ID_side_effect);
parser_stack($$).set(ID_statement, ID_postdecrement);
mto($$, $1);
}
/* The following is a) GCC and b) ISO C 11 compliant */
| '(' type_name ')' '{' initializer_list_opt '}'
{
exprt tmp(ID_initializer_list);
tmp.add_source_location()=parser_stack($4).source_location();
tmp.operands().swap(parser_stack($5).operands());
$$=$1;
set($$, ID_typecast);
parser_stack($$).add_to_operands(std::move(tmp));
parser_stack($$).type().swap(parser_stack($2));
}
| '(' type_name ')' '{' initializer_list ',' '}'
{
// same as above
exprt tmp(ID_initializer_list);
tmp.add_source_location()=parser_stack($4).source_location();
tmp.operands().swap(parser_stack($5).operands());
$$=$1;
set($$, ID_typecast);
parser_stack($$).add_to_operands(std::move(tmp));
parser_stack($$).type().swap(parser_stack($2));
}
;
member_name:
identifier
| typedef_name
;
argument_expression_list:
assignment_expression
{
init($$, ID_expression_list);
mto($$, $1);
}
| argument_expression_list ',' assignment_expression
{
$$=$1;
mto($$, $3);
}
;
unary_expression:
postfix_expression
| TOK_INCR unary_expression
{ $$=$1;
set($$, ID_side_effect);
parser_stack($$).set(ID_statement, ID_preincrement);
mto($$, $2);
}
| TOK_DECR unary_expression
{ $$=$1;
set($$, ID_side_effect);
parser_stack($$).set(ID_statement, ID_predecrement);
mto($$, $2);
}
| '&' cast_expression
{ $$=$1;
set($$, ID_address_of);
mto($$, $2);
}
| TOK_ANDAND gcc_local_label
{ // this takes the address of a label (a gcc extension)
$$=$1;
irep_idt identifier=PARSER.lookup_label(parser_stack($2).get(ID_C_base_name));
set($$, ID_address_of);
parser_stack($$).operands().resize(1);
auto &op = to_unary_expr(parser_stack($$)).op();
op=parser_stack($2);
op.id(ID_label);
op.set(ID_identifier, identifier);
}
| '*' cast_expression
{ $$=$1;
set($$, ID_dereference);
mto($$, $2);
}
| '+' cast_expression
{ $$=$1;
set($$, ID_unary_plus);
mto($$, $2);
}
| '-' cast_expression
{ $$=$1;
set($$, ID_unary_minus);
mto($$, $2);
}
| '~' cast_expression
{ $$=$1;
set($$, ID_bitnot);
mto($$, $2);
}
| '!' cast_expression
{ $$=$1;
set($$, ID_not);
mto($$, $2);
}
| TOK_SIZEOF unary_expression
{ $$=$1;
set($$, ID_sizeof);
mto($$, $2);
}
| TOK_SIZEOF '(' type_name ')'
{ $$=$1;
set($$, ID_sizeof);
parser_stack($$).add(ID_type_arg).swap(parser_stack($3));
}
| TOK_ALIGNOF unary_expression
{ // note no parentheses for expressions, just like sizeof
$$=$1;
set($$, ID_alignof);
mto($$, $2);
}
| TOK_ALIGNOF '(' type_name ')'
{
$$=$1;
parser_stack($$).id(ID_alignof);
parser_stack($$).add(ID_type_arg).swap(parser_stack($3));
}
| TOK_BIT_CAST '(' type_name ',' unary_expression ')'
{ $$=$1;
set($$, ID_bit_cast);
mto($$, $5);
parser_stack($$).type().swap(parser_stack($3));
}
;
cast_expression:
unary_expression
| '(' type_name ')' cast_expression
{
$$=$1;
set($$, ID_typecast);
mto($$, $4);
parser_stack($$).type().swap(parser_stack($2));
}
| TOK_REAL cast_expression
{ $$=$1;
set($$, ID_complex_real);
mto($$, $2);
}
| TOK_IMAG cast_expression
{ $$=$1;
set($$, ID_complex_imag);
mto($$, $2);
}
;
multiplicative_expression:
cast_expression
| multiplicative_expression '*' cast_expression
{ binary($$, $1, $2, ID_mult, $3); }
| multiplicative_expression '/' cast_expression
{ binary($$, $1, $2, ID_div, $3); }
| multiplicative_expression '%' cast_expression
{ binary($$, $1, $2, ID_mod, $3); }
;
additive_expression:
multiplicative_expression
| additive_expression '+' multiplicative_expression
{ binary($$, $1, $2, ID_plus, $3); }
| additive_expression '-' multiplicative_expression
{ binary($$, $1, $2, ID_minus, $3); }
;
shift_expression:
additive_expression
| shift_expression TOK_SHIFTLEFT additive_expression
{ binary($$, $1, $2, ID_shl, $3); }
| shift_expression TOK_SHIFTRIGHT additive_expression
{ binary($$, $1, $2, ID_shr, $3); }
;
relational_expression:
shift_expression
| relational_expression '<' shift_expression
{ binary($$, $1, $2, ID_lt, $3); }
| relational_expression '>' shift_expression
{ binary($$, $1, $2, ID_gt, $3); }
| relational_expression TOK_LE shift_expression
{ binary($$, $1, $2, ID_le, $3); }
| relational_expression TOK_GE shift_expression
{ binary($$, $1, $2, ID_ge, $3); }
;
equality_expression:
relational_expression
| equality_expression TOK_EQ relational_expression
{ binary($$, $1, $2, ID_equal, $3); }
| equality_expression TOK_NE relational_expression
{ binary($$, $1, $2, ID_notequal, $3); }
;
and_expression:
equality_expression
| and_expression '&' equality_expression
{ binary($$, $1, $2, ID_bitand, $3); }
;
exclusive_or_expression:
and_expression
| exclusive_or_expression '^' and_expression
{ binary($$, $1, $2, ID_bitxor, $3); }
;
inclusive_or_expression:
exclusive_or_expression
| inclusive_or_expression '|' exclusive_or_expression
{ binary($$, $1, $2, ID_bitor, $3); }
;
logical_and_expression:
inclusive_or_expression
| logical_and_expression TOK_ANDAND inclusive_or_expression
{ binary($$, $1, $2, ID_and, $3); }
;
logical_xor_expression:
logical_and_expression
| logical_xor_expression TOK_XORXOR logical_and_expression
{ binary($$, $1, $2, ID_xor, $3); }
;
logical_or_expression:
logical_xor_expression
| logical_or_expression TOK_OROR logical_xor_expression
{ binary($$, $1, $2, ID_or, $3); }
;
/* This is obviously non-standard, but inspired by Spec#. */
/* Implication is generally considered to be right-associative,
and binds weaker than 'OR', and stronger than bi-implication. */
logical_implication_expression:
logical_or_expression
| logical_or_expression TOK_IMPLIES logical_implication_expression
{ binary($$, $1, $2, ID_implies, $3); }
;
/* This is obviously non-standard, but inspired by Spec#. */
/* Bi-Implication is generally considered to be left-associative,
and binds weaker than '==>', and stronger than quantifiers. */
logical_equivalence_expression:
logical_implication_expression
| logical_equivalence_expression TOK_EQUIVALENT logical_implication_expression
{ binary($$, $1, $2, ID_equal, $3); }
;
/* Non-standard, defined by ACSL. Lowest precedence of all operators. */
ACSL_binding_expression:
conditional_expression
| TOK_ACSL_FORALL compound_scope declaration ACSL_binding_expression
{
$$=$1;
set($$, ID_forall);
parser_stack($$).add_to_operands(tuple_exprt( { std::move(parser_stack($3)) } ));
mto($$, $4);
PARSER.pop_scope();
}
| TOK_ACSL_EXISTS compound_scope declaration ACSL_binding_expression
{
$$=$1;
set($$, ID_exists);
parser_stack($$).add_to_operands(tuple_exprt( { std::move(parser_stack($3)) } ));
mto($$, $4);
PARSER.pop_scope();
}
| TOK_ACSL_LAMBDA compound_scope declaration ACSL_binding_expression
{
$$=$1;
set($$, ID_lambda);
parser_stack($$).add_to_operands(tuple_exprt( { std::move(parser_stack($3)) } ));
mto($$, $4);
PARSER.pop_scope();
}
;
conditional_expression:
logical_equivalence_expression
| logical_equivalence_expression '?' comma_expression ':' conditional_expression
{ $$=$2;
parser_stack($$).id(ID_if);
mto($$, $1);
mto($$, $3);
mto($$, $5);
}
| logical_equivalence_expression '?' ':' conditional_expression
{ $$=$2;
parser_stack($$).id(ID_side_effect);
parser_stack($$).set(ID_statement, ID_gcc_conditional_expression);
mto($$, $1);
mto($$, $4);
}
;
assignment_expression:
ACSL_binding_expression /* usually conditional_expression */
| cast_expression '=' assignment_expression
{ binary($$, $1, $2, ID_side_effect, $3); parser_stack($$).set(ID_statement, ID_assign); }
| cast_expression TOK_MULTASSIGN assignment_expression
{ binary($$, $1, $2, ID_side_effect, $3); parser_stack($$).set(ID_statement, ID_assign_mult); }
| cast_expression TOK_DIVASSIGN assignment_expression
{ binary($$, $1, $2, ID_side_effect, $3); parser_stack($$).set(ID_statement, ID_assign_div); }
| cast_expression TOK_MODASSIGN assignment_expression
{ binary($$, $1, $2, ID_side_effect, $3); parser_stack($$).set(ID_statement, ID_assign_mod); }
| cast_expression TOK_PLUSASSIGN assignment_expression
{ binary($$, $1, $2, ID_side_effect, $3); parser_stack($$).set(ID_statement, ID_assign_plus); }
| cast_expression TOK_MINUSASSIGN assignment_expression
{ binary($$, $1, $2, ID_side_effect, $3); parser_stack($$).set(ID_statement, ID_assign_minus); }
| cast_expression TOK_SHLASSIGN assignment_expression
{ binary($$, $1, $2, ID_side_effect, $3); parser_stack($$).set(ID_statement, ID_assign_shl); }
| cast_expression TOK_SHRASSIGN assignment_expression
{ binary($$, $1, $2, ID_side_effect, $3); parser_stack($$).set(ID_statement, ID_assign_shr); }
| cast_expression TOK_ANDASSIGN assignment_expression
{ binary($$, $1, $2, ID_side_effect, $3); parser_stack($$).set(ID_statement, ID_assign_bitand); }
| cast_expression TOK_XORASSIGN assignment_expression
{ binary($$, $1, $2, ID_side_effect, $3); parser_stack($$).set(ID_statement, ID_assign_bitxor); }
| cast_expression TOK_ORASSIGN assignment_expression
{ binary($$, $1, $2, ID_side_effect, $3); parser_stack($$).set(ID_statement, ID_assign_bitor); }
;
comma_expression:
assignment_expression
| comma_expression ',' assignment_expression
{ binary($$, $1, $2, ID_comma, $3); }
;
constant_expression:
assignment_expression
;
comma_expression_opt:
/* nothing */
{ init($$); parser_stack($$).make_nil(); }
| comma_expression
;
/*** Declarations *******************************************************/
declaration:
declaration_specifier ';'
{
// type only, no declarator!
init($$, ID_declaration);
parser_stack($$).type().swap(parser_stack($1));
}
| type_specifier ';'
{
// type only, no identifier!
init($$, ID_declaration);
parser_stack($$).type().swap(parser_stack($1));
}
| static_assert_declaration ';'
| declaring_list ';'
| default_declaring_list ';'
;
static_assert_declaration:
TOK_STATIC_ASSERT '(' assignment_expression ',' assignment_expression ')'
{
$$=$1;
set($$, ID_declaration);
to_ansi_c_declaration(parser_stack($$)).set_is_static_assert(true);
mto($$, $3);
mto($$, $5);
}
;
default_declaring_list:
declaration_qualifier_list identifier_declarator
{
init($$, ID_declaration);
parser_stack($$).type().swap(parser_stack($1));
PARSER.add_declarator(parser_stack($$), parser_stack($2));
}
initializer_opt
{
// patch on the initializer
$$=$3;
to_ansi_c_declaration(parser_stack($$)).add_initializer(parser_stack($4));
}
| type_qualifier_list identifier_declarator
{
init($$, ID_declaration);
parser_stack($$).type().swap(parser_stack($1));
PARSER.add_declarator(parser_stack($$), parser_stack($2));
}
initializer_opt
{
// patch on the initializer
$$=$3;
to_ansi_c_declaration(parser_stack($$)).add_initializer(parser_stack($4));
}
| default_declaring_list ',' identifier_declarator
{
// just add the declarator
PARSER.add_declarator(parser_stack($1), parser_stack($3));