-
Notifications
You must be signed in to change notification settings - Fork 2
/
parse.c
3101 lines (2708 loc) · 84.7 KB
/
parse.c
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
/*
* Stupid C parser, version 1e-6.
*
* Let's see how hard this is to do.
*
* Copyright (C) 2003 Transmeta Corp.
* 2003-2004 Linus Torvalds
* Copyright (C) 2004 Christopher Li
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <stdarg.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <unistd.h>
#include <fcntl.h>
#include <limits.h>
#include "lib.h"
#include "allocate.h"
#include "token.h"
#include "parse.h"
#include "symbol.h"
#include "scope.h"
#include "expression.h"
#include "target.h"
static struct symbol_list **function_symbol_list;
struct symbol_list *function_computed_target_list;
struct statement_list *function_computed_goto_list;
static struct token *statement(struct token *token, struct statement **tree);
static struct token *handle_attributes(struct token *token, struct decl_state *ctx);
typedef struct token *declarator_t(struct token *, struct symbol *, struct decl_state *);
static declarator_t
struct_specifier, union_specifier, enum_specifier,
attribute_specifier, typeof_specifier,
storage_specifier, thread_specifier;
static declarator_t generic_qualifier;
static declarator_t autotype_specifier;
static struct token *parse_if_statement(struct token *token, struct statement *stmt);
static struct token *parse_return_statement(struct token *token, struct statement *stmt);
static struct token *parse_loop_iterator(struct token *token, struct statement *stmt);
static struct token *parse_default_statement(struct token *token, struct statement *stmt);
static struct token *parse_case_statement(struct token *token, struct statement *stmt);
static struct token *parse_switch_statement(struct token *token, struct statement *stmt);
static struct token *parse_for_statement(struct token *token, struct statement *stmt);
static struct token *parse_while_statement(struct token *token, struct statement *stmt);
static struct token *parse_do_statement(struct token *token, struct statement *stmt);
static struct token *parse_goto_statement(struct token *token, struct statement *stmt);
static struct token *parse_context_statement(struct token *token, struct statement *stmt);
static struct token *parse_range_statement(struct token *token, struct statement *stmt);
static struct token *parse_asm_statement(struct token *token, struct statement *stmt);
static struct token *toplevel_asm_declaration(struct token *token, struct symbol_list **list);
static struct token *parse_static_assert(struct token *token, struct symbol_list **unused);
typedef struct token *attr_t(struct token *, struct symbol *,
struct decl_state *);
static attr_t
attribute_packed, attribute_aligned, attribute_modifier,
attribute_function,
attribute_bitwise,
attribute_address_space, attribute_context,
attribute_cleanup,
attribute_designated_init,
attribute_transparent_union, ignore_attribute,
attribute_mode, attribute_force;
typedef struct symbol *to_mode_t(struct symbol *);
static to_mode_t
to_QI_mode, to_HI_mode, to_SI_mode, to_DI_mode, to_TI_mode;
static to_mode_t to_pointer_mode, to_word_mode;
enum {
Set_T = 1,
Set_S = 2,
Set_Char = 4,
Set_Int = 8,
Set_Double = 16,
Set_Float = 32,
Set_Signed = 64,
Set_Unsigned = 128,
Set_Short = 256,
Set_Long = 512,
Set_Vlong = 1024,
Set_Int128 = 2048,
Set_Any = Set_T | Set_Short | Set_Long | Set_Signed | Set_Unsigned
};
enum {
CInt = 0, CSInt, CUInt, CReal,
};
static void asm_modifier(struct token *token, unsigned long *mods, unsigned long mod)
{
if (*mods & mod)
warning(token->pos, "duplicated asm modifier");
*mods |= mod;
}
static struct symbol_op typedef_op = {
.type = KW_MODIFIER,
.declarator = storage_specifier,
};
static struct symbol_op inline_op = {
.type = KW_MODIFIER,
.declarator = generic_qualifier,
.asm_modifier = asm_modifier,
};
static struct symbol_op noreturn_op = {
.type = KW_MODIFIER,
.declarator = generic_qualifier,
};
static declarator_t alignas_specifier;
static struct symbol_op alignas_op = {
.type = KW_MODIFIER,
.declarator = alignas_specifier,
};
static struct symbol_op auto_op = {
.type = KW_MODIFIER,
.declarator = storage_specifier,
};
static struct symbol_op register_op = {
.type = KW_MODIFIER,
.declarator = storage_specifier,
};
static struct symbol_op static_op = {
.type = KW_MODIFIER|KW_STATIC,
.declarator = storage_specifier,
};
static struct symbol_op extern_op = {
.type = KW_MODIFIER,
.declarator = storage_specifier,
};
static struct symbol_op thread_op = {
.type = KW_MODIFIER,
.declarator = thread_specifier,
};
static struct symbol_op const_op = {
.type = KW_QUALIFIER,
.declarator = generic_qualifier,
};
static struct symbol_op volatile_op = {
.type = KW_QUALIFIER,
.declarator = generic_qualifier,
.asm_modifier = asm_modifier,
};
static struct symbol_op restrict_op = {
.type = KW_QUALIFIER,
.declarator = generic_qualifier,
};
static struct symbol_op atomic_op = {
.type = KW_QUALIFIER,
.declarator = generic_qualifier,
};
static struct symbol_op typeof_op = {
.type = KW_SPECIFIER,
.declarator = typeof_specifier,
.test = Set_Any,
.set = Set_S|Set_T,
};
static struct symbol_op autotype_op = {
.type = KW_SPECIFIER,
.declarator = autotype_specifier,
.test = Set_Any,
.set = Set_S|Set_T,
};
static struct symbol_op attribute_op = {
.type = KW_ATTRIBUTE,
.declarator = attribute_specifier,
};
static struct symbol_op struct_op = {
.type = KW_SPECIFIER,
.declarator = struct_specifier,
.test = Set_Any,
.set = Set_S|Set_T,
};
static struct symbol_op union_op = {
.type = KW_SPECIFIER,
.declarator = union_specifier,
.test = Set_Any,
.set = Set_S|Set_T,
};
static struct symbol_op enum_op = {
.type = KW_SPECIFIER,
.declarator = enum_specifier,
.test = Set_Any,
.set = Set_S|Set_T,
};
static struct symbol_op spec_op = {
.type = KW_SPECIFIER | KW_EXACT,
.test = Set_Any,
.set = Set_S|Set_T,
};
static struct symbol_op char_op = {
.type = KW_SPECIFIER,
.test = Set_T|Set_Long|Set_Short,
.set = Set_T|Set_Char,
.class = CInt,
};
static struct symbol_op int_op = {
.type = KW_SPECIFIER,
.test = Set_T,
.set = Set_T|Set_Int,
};
static struct symbol_op double_op = {
.type = KW_SPECIFIER,
.test = Set_T|Set_Signed|Set_Unsigned|Set_Short|Set_Vlong,
.set = Set_T|Set_Double,
.class = CReal,
};
static struct symbol_op float_op = {
.type = KW_SPECIFIER,
.test = Set_T|Set_Signed|Set_Unsigned|Set_Short|Set_Long,
.set = Set_T|Set_Float,
.class = CReal,
};
static struct symbol_op short_op = {
.type = KW_SPECIFIER,
.test = Set_S|Set_Char|Set_Float|Set_Double|Set_Long|Set_Short,
.set = Set_Short,
};
static struct symbol_op signed_op = {
.type = KW_SPECIFIER,
.test = Set_S|Set_Float|Set_Double|Set_Signed|Set_Unsigned,
.set = Set_Signed,
.class = CSInt,
};
static struct symbol_op unsigned_op = {
.type = KW_SPECIFIER,
.test = Set_S|Set_Float|Set_Double|Set_Signed|Set_Unsigned,
.set = Set_Unsigned,
.class = CUInt,
};
static struct symbol_op long_op = {
.type = KW_SPECIFIER,
.test = Set_S|Set_Char|Set_Float|Set_Short|Set_Vlong,
.set = Set_Long,
};
static struct symbol_op int128_op = {
.type = KW_SPECIFIER,
.test = Set_S|Set_T|Set_Char|Set_Short|Set_Int|Set_Float|Set_Double|Set_Long|Set_Vlong|Set_Int128,
.set = Set_T|Set_Int128|Set_Vlong,
.class = CInt,
};
static struct symbol_op if_op = {
.statement = parse_if_statement,
};
static struct symbol_op return_op = {
.statement = parse_return_statement,
};
static struct symbol_op loop_iter_op = {
.statement = parse_loop_iterator,
};
static struct symbol_op default_op = {
.statement = parse_default_statement,
};
static struct symbol_op case_op = {
.statement = parse_case_statement,
};
static struct symbol_op switch_op = {
.statement = parse_switch_statement,
};
static struct symbol_op for_op = {
.statement = parse_for_statement,
};
static struct symbol_op while_op = {
.statement = parse_while_statement,
};
static struct symbol_op do_op = {
.statement = parse_do_statement,
};
static struct symbol_op goto_op = {
.statement = parse_goto_statement,
};
static struct symbol_op __context___op = {
.statement = parse_context_statement,
.attribute = attribute_context,
};
static struct symbol_op range_op = {
.statement = parse_range_statement,
};
static struct symbol_op asm_op = {
.type = KW_ASM,
.statement = parse_asm_statement,
.toplevel = toplevel_asm_declaration,
};
static struct symbol_op static_assert_op = {
.toplevel = parse_static_assert,
};
static struct symbol_op packed_op = {
.attribute = attribute_packed,
};
static struct symbol_op aligned_op = {
.attribute = attribute_aligned,
};
static struct symbol_op cleanup_op = {
.attribute = attribute_cleanup,
};
static struct symbol_op attr_mod_op = {
.attribute = attribute_modifier,
};
static struct symbol_op attr_fun_op = {
.attribute = attribute_function,
};
static struct symbol_op attr_bitwise_op = {
.attribute = attribute_bitwise,
};
static struct symbol_op attr_force_op = {
.attribute = attribute_force,
};
static struct symbol_op address_space_op = {
.attribute = attribute_address_space,
};
static struct symbol_op mode_op = {
.attribute = attribute_mode,
};
static struct symbol_op context_op = {
.attribute = attribute_context,
};
static struct symbol_op designated_init_op = {
.attribute = attribute_designated_init,
};
static struct symbol_op transparent_union_op = {
.attribute = attribute_transparent_union,
};
static struct symbol_op ignore_attr_op = {
.attribute = ignore_attribute,
};
static struct symbol_op mode_QI_op = {
.type = KW_MODE,
.to_mode = to_QI_mode
};
static struct symbol_op mode_HI_op = {
.type = KW_MODE,
.to_mode = to_HI_mode
};
static struct symbol_op mode_SI_op = {
.type = KW_MODE,
.to_mode = to_SI_mode
};
static struct symbol_op mode_DI_op = {
.type = KW_MODE,
.to_mode = to_DI_mode
};
static struct symbol_op mode_TI_op = {
.type = KW_MODE,
.to_mode = to_TI_mode
};
static struct symbol_op mode_pointer_op = {
.type = KW_MODE,
.to_mode = to_pointer_mode
};
static struct symbol_op mode_word_op = {
.type = KW_MODE,
.to_mode = to_word_mode
};
/*
* Define the keyword and their effects.
* The entries in the 'typedef' and put in NS_TYPEDEF and
* are automatically set as reserved keyword while the ones
* in the 'keyword' table are just put in NS_KEYWORD.
*
* The entries are added via the 3 macros:
* N() for entries with "name" only,
* D() for entries with "name" & "__name__",
* A() for entries with "name", "__name" & "__name__",
* U() for entries with "__name" & "__name__".
*/
static struct init_keyword {
const char *name;
struct symbol_op *op;
struct symbol *type;
unsigned long mods;
} typedefs[] = {
#define N(I, O,...) { I, O,##__VA_ARGS__ }
#define D(I, O,...) N(I,O,##__VA_ARGS__ ), \
N("__" I "__",O,##__VA_ARGS__)
#define A(I, O,...) N(I,O,##__VA_ARGS__ ), \
N("__" I,O,##__VA_ARGS__), \
N("__" I "__",O,##__VA_ARGS__)
#define U(I, O,...) N("__" I,O,##__VA_ARGS__), \
N("__" I "__",O,##__VA_ARGS__)
/* Storage classes */
N("auto", &auto_op, .mods = MOD_AUTO),
N("register", ®ister_op, .mods = MOD_REGISTER),
N("static", &static_op, .mods = MOD_STATIC),
N("extern", &extern_op, .mods = MOD_EXTERN),
N("__thread", &thread_op),
N("_Thread_local", &thread_op),
A("inline", &inline_op, .mods = MOD_INLINE),
/* Typedef ... */
N("typedef", &typedef_op, .mods = MOD_USERTYPE),
A("typeof", &typeof_op),
N("__auto_type", &autotype_op),
/* Type qualifiers */
A("const", &const_op, .mods = MOD_CONST),
A("volatile", &volatile_op, .mods = MOD_VOLATILE),
A("restrict", &restrict_op, .mods = MOD_RESTRICT),
N("_Atomic", &atomic_op, .mods = MOD_ATOMIC),
N("_Noreturn", &noreturn_op, .mods = MOD_NORETURN),
N("_Alignas", &alignas_op),
U("attribute", &attribute_op),
/* Type specifiers */
N("struct", &struct_op),
N("union", &union_op),
N("enum", &enum_op),
N("void", &spec_op, .type = &void_ctype),
N("char", &char_op),
N("short", &short_op),
N("int", &int_op),
N("long", &long_op),
N("float", &float_op),
N("double", &double_op),
A("signed", &signed_op),
N("unsigned", &unsigned_op),
N("__int128", &int128_op),
N("_Bool", &spec_op, .type = &bool_ctype),
/* Predeclared types */
N("__builtin_va_list", &spec_op, .type = &ptr_ctype),
N("__builtin_ms_va_list",&spec_op, .type = &ptr_ctype),
N("__int128_t", &spec_op, .type = &sint128_ctype),
N("__uint128_t", &spec_op, .type = &uint128_ctype),
N("_Float32", &spec_op, .type = &float32_ctype),
N("_Float32x", &spec_op, .type = &float32x_ctype),
N("_Float64", &spec_op, .type = &float64_ctype),
N("_Float64x", &spec_op, .type = &float64x_ctype),
N("_Float128", &spec_op, .type = &float128_ctype),
}, keywords[] = {
/* Statements */
N("if", &if_op),
N("return", &return_op),
N("break", &loop_iter_op),
N("continue", &loop_iter_op),
N("default", &default_op),
N("case", &case_op),
N("switch", &switch_op),
N("for", &for_op),
N("while", &while_op),
N("do", &do_op),
N("goto", &goto_op),
A("asm", &asm_op),
N("context", &context_op),
N("__context__", &__context___op),
N("__range__", &range_op),
N("_Static_assert", &static_assert_op),
/* Attributes */
D("packed", &packed_op),
D("aligned", &aligned_op),
D("cleanup", &cleanup_op),
D("nocast", &attr_mod_op, .mods = MOD_NOCAST),
D("noderef", &attr_mod_op, .mods = MOD_NODEREF),
D("safe", &attr_mod_op, .mods = MOD_SAFE),
D("unused", &attr_mod_op, .mods = MOD_UNUSED),
D("externally_visible", &attr_mod_op, .mods = MOD_EXT_VISIBLE),
D("force", &attr_force_op),
D("bitwise", &attr_bitwise_op, .mods = MOD_BITWISE),
D("address_space", &address_space_op),
D("designated_init", &designated_init_op),
D("transparent_union", &transparent_union_op),
D("noreturn", &attr_fun_op, .mods = MOD_NORETURN),
D("pure", &attr_fun_op, .mods = MOD_PURE),
A("const", &attr_fun_op, .mods = MOD_PURE),
D("gnu_inline", &attr_fun_op, .mods = MOD_GNU_INLINE),
/* Modes */
D("mode", &mode_op),
D("QI", &mode_QI_op),
D("HI", &mode_HI_op),
D("SI", &mode_SI_op),
D("DI", &mode_DI_op),
D("TI", &mode_TI_op),
D("byte", &mode_QI_op),
D("pointer", &mode_pointer_op),
D("word", &mode_word_op),
};
static const char *ignored_attributes[] = {
#define GCC_ATTR(x) \
STRINGIFY(x), \
STRINGIFY(__##x##__),
#include "gcc-attr-list.h"
#undef GCC_ATTR
"bounded",
"__bounded__",
"__noclone",
"__nonnull",
"__nothrow",
};
static void init_keyword(int stream, struct init_keyword *kw, enum namespace ns)
{
struct symbol *sym = create_symbol(stream, kw->name, SYM_KEYWORD, ns);
sym->ident->keyword = 1;
sym->ident->reserved |= (ns == NS_TYPEDEF);
sym->ctype.modifiers = kw->mods;
sym->ctype.base_type = kw->type;
sym->op = kw->op;
}
void init_parser(int stream)
{
int i;
for (i = 0; i < ARRAY_SIZE(typedefs); i++)
init_keyword(stream, &typedefs[i], NS_TYPEDEF);
for (i = 0; i < ARRAY_SIZE(keywords); i++)
init_keyword(stream, &keywords[i], NS_KEYWORD);
for (i = 0; i < ARRAY_SIZE(ignored_attributes); i++) {
const char * name = ignored_attributes[i];
struct symbol *sym = create_symbol(stream, name, SYM_KEYWORD,
NS_KEYWORD);
if (!sym->op) {
sym->ident->keyword = 1;
sym->op = &ignore_attr_op;
}
}
}
static struct token *skip_to(struct token *token, int op)
{
while (!match_op(token, op) && !eof_token(token))
token = token->next;
return token;
}
static struct token bad_token = { .pos.type = TOKEN_BAD };
struct token *expect(struct token *token, int op, const char *where)
{
if (!match_op(token, op)) {
if (token != &bad_token) {
bad_token.next = token;
sparse_error(token->pos, "Expected %s %s", show_special(op), where);
sparse_error(token->pos, "got %s", show_token(token));
}
if (op == ';')
return skip_to(token, op);
return &bad_token;
}
return token->next;
}
///
// issue an error message on new parsing errors
// @token: the current token
// @errmsg: the error message
// If the current token is from a previous error, an error message
// has already been issued, so nothing more is done.
// Otherwise, @errmsg is displayed followed by the current token.
static void unexpected(struct token *token, const char *errmsg)
{
if (token == &bad_token)
return;
sparse_error(token->pos, "%s", errmsg);
sparse_error(token->pos, "got %s", show_token(token));
}
// Add a symbol to the list of function-local symbols
static void fn_local_symbol(struct symbol *sym)
{
if (function_symbol_list)
add_symbol(function_symbol_list, sym);
}
struct statement *alloc_statement(struct position pos, int type)
{
struct statement *stmt = __alloc_statement(0);
stmt->type = type;
stmt->pos = pos;
return stmt;
}
static struct token *struct_declaration_list(struct token *token, struct symbol_list **list);
static void apply_ctype(struct position pos, struct ctype *dst, struct ctype *src);
static void apply_modifiers(struct position pos, struct decl_state *ctx)
{
struct symbol *ctype;
if (!ctx->mode)
return;
ctype = ctx->mode->to_mode(ctx->ctype.base_type);
if (!ctype)
sparse_error(pos, "don't know how to apply mode to %s",
show_typename(ctx->ctype.base_type));
else
ctx->ctype.base_type = ctype;
}
static struct symbol * alloc_indirect_symbol(struct position pos, struct ctype *ctype, int type)
{
struct symbol *sym = alloc_symbol(pos, type);
sym->ctype.base_type = ctype->base_type;
sym->ctype.modifiers = ctype->modifiers;
ctype->base_type = sym;
ctype->modifiers = 0;
return sym;
}
/*
* NOTE! NS_LABEL is not just a different namespace,
* it also ends up using function scope instead of the
* regular symbol scope.
*/
struct symbol *label_symbol(struct token *token, int used)
{
struct symbol *sym = lookup_symbol(token->ident, NS_LABEL);
if (!sym) {
sym = alloc_symbol(token->pos, SYM_LABEL);
bind_symbol(sym, token->ident, NS_LABEL);
if (used)
sym->used = 1;
fn_local_symbol(sym);
}
return sym;
}
static struct token *struct_union_enum_specifier(enum type type,
struct token *token, struct decl_state *ctx,
struct token *(*parse)(struct token *, struct symbol *))
{
struct decl_state attr = { };
struct symbol *sym;
struct position *repos;
token = handle_attributes(token, &attr);
if (token_type(token) == TOKEN_IDENT) {
sym = lookup_symbol(token->ident, NS_STRUCT);
if (!sym ||
(is_outer_scope(sym->scope) &&
(match_op(token->next,';') || match_op(token->next,'{')))) {
// Either a new symbol, or else an out-of-scope
// symbol being redefined.
sym = alloc_symbol(token->pos, type);
bind_symbol(sym, token->ident, NS_STRUCT);
}
if (sym->type != type)
error_die(token->pos, "invalid tag applied to %s", show_typename (sym));
ctx->ctype.base_type = sym;
repos = &token->pos;
token = token->next;
if (!match_op(token, '{'))
return token;
// The following test is actually wrong for empty
// structs, but (1) they are not C99, (2) gcc does
// the same thing, and (3) it's easier.
if (sym->symbol_list)
error_die(token->pos, "redefinition of %s", show_typename (sym));
sym->pos = *repos;
// Mark the structure as needing re-examination
sym->examined = 0;
} else if (match_op(token, '{')) {
// private struct/union/enum type
sym = alloc_symbol(token->pos, type);
set_current_scope(sym); // used by dissect
ctx->ctype.base_type = sym;
} else {
sparse_error(token->pos, "expected declaration");
ctx->ctype.base_type = &bad_ctype;
return token;
}
token = parse(token->next, sym);
token = expect(token, '}', "at end of specifier");
attr.ctype.base_type = sym;
token = handle_attributes(token, &attr);
apply_ctype(token->pos, &sym->ctype, &attr.ctype);
sym->packed = attr.packed;
sym->endpos = token->pos;
return token;
}
static struct token *parse_struct_declaration(struct token *token, struct symbol *sym)
{
struct symbol *field, *last = NULL;
struct token *res;
res = struct_declaration_list(token, &sym->symbol_list);
FOR_EACH_PTR(sym->symbol_list, field) {
if (!field->ident) {
struct symbol *base = field->ctype.base_type;
if (base && base->type == SYM_BITFIELD)
continue;
}
if (last)
last->next_subobject = field;
last = field;
} END_FOR_EACH_PTR(field);
return res;
}
static struct token *parse_union_declaration(struct token *token, struct symbol *sym)
{
return struct_declaration_list(token, &sym->symbol_list);
}
static struct token *struct_specifier(struct token *token, struct symbol *sym, struct decl_state *ctx)
{
return struct_union_enum_specifier(SYM_STRUCT, token, ctx, parse_struct_declaration);
}
static struct token *union_specifier(struct token *token, struct symbol *sym, struct decl_state *ctx)
{
return struct_union_enum_specifier(SYM_UNION, token, ctx, parse_union_declaration);
}
///
// safe right shift
//
// This allow to use a shift amount as big (or bigger)
// than the width of the value to be shifted, in which case
// the result is, of course, 0.
static unsigned long long rshift(unsigned long long val, unsigned int n)
{
if (n >= (sizeof(val) * 8))
return 0;
return val >> n;
}
struct range {
long long neg;
unsigned long long pos;
};
static void update_range(struct range *range, unsigned long long uval, struct symbol *vtype)
{
long long sval = uval;
if (is_signed_type(vtype) && (sval < 0)) {
if (sval < range->neg)
range->neg = sval;
} else {
if (uval > range->pos)
range->pos = uval;
}
}
static int type_is_ok(struct symbol *type, struct range range)
{
int shift = type->bit_size;
int is_unsigned = type->ctype.modifiers & MOD_UNSIGNED;
if (!is_unsigned)
shift--;
if (rshift(range.pos, shift))
return 0;
if (range.neg == 0)
return 1;
if (is_unsigned)
return 0;
if (rshift(~range.neg, shift))
return 0;
return 1;
}
static struct range type_range(struct symbol *type)
{
struct range range;
unsigned int size = type->bit_size;
unsigned long long max;
long long min;
if (is_signed_type(type)) {
min = sign_bit(size);
max = min - 1;
} else {
min = 0;
max = bits_mask(size);
}
range.pos = max;
range.neg = min;
return range;
}
static int val_in_range(struct range *range, long long sval, struct symbol *vtype)
{
unsigned long long uval = sval;
if (is_signed_type(vtype) && (sval < 0))
return range->neg <= sval;
else
return uval <= range->pos;
}
static void cast_enum_list(struct symbol_list *list, struct symbol *base_type)
{
struct range irange = type_range(&int_ctype);
struct symbol *sym;
FOR_EACH_PTR(list, sym) {
struct expression *expr = sym->initializer;
struct symbol *ctype;
long long val;
if (expr->type != EXPR_VALUE)
continue;
ctype = expr->ctype;
val = get_expression_value(expr);
if (is_int_type(ctype) && val_in_range(&irange, val, ctype)) {
expr->ctype = &int_ctype;
continue;
}
cast_value(expr, base_type, expr);
} END_FOR_EACH_PTR(sym);
}
static struct token *parse_enum_declaration(struct token *token, struct symbol *parent)
{
unsigned long long lastval = 0;
struct symbol *ctype = NULL, *base_type = NULL;
struct range range = { };
int mix_bitwise = 0;
parent->examined = 1;
parent->ctype.base_type = &int_ctype;
while (token_type(token) == TOKEN_IDENT) {
struct expression *expr = NULL;
struct token *next = token->next;
struct decl_state ctx = { };
struct symbol *sym;
// FIXME: only 'deprecated' should be accepted
next = handle_attributes(next, &ctx);
if (match_op(next, '=')) {
next = constant_expression(next->next, &expr);
lastval = get_expression_value(expr);
ctype = &void_ctype;
if (expr && expr->ctype)
ctype = expr->ctype;
} else if (!ctype) {
ctype = &int_ctype;
} else if (is_int_type(ctype)) {
lastval++;
} else {
error_die(token->pos, "can't increment the last enum member");
}
if (!expr) {
expr = alloc_expression(token->pos, EXPR_VALUE);
expr->value = lastval;
expr->ctype = ctype;
}
sym = alloc_symbol(token->pos, SYM_NODE);
bind_symbol(sym, token->ident, NS_SYMBOL);
sym->ctype.modifiers &= ~MOD_ADDRESSABLE;
sym->initializer = expr;
sym->enum_member = 1;
sym->ctype.base_type = parent;
add_ptr_list(&parent->symbol_list, sym);
if (base_type != &bad_ctype) {
if (ctype->type == SYM_NODE)
ctype = ctype->ctype.base_type;
if (ctype->type == SYM_ENUM) {
if (ctype == parent)
ctype = base_type;
else
ctype = ctype->ctype.base_type;
}
/*
* base_type rules:
* - if all enums are of the same type, then
* the base_type is that type (two first
* cases)
* - if enums are of different types, they
* all have to be integer types, and the
* base type is at least "int_ctype".
* - otherwise the base_type is "bad_ctype".
*/
if (!base_type || ctype == &bad_ctype) {
base_type = ctype;
} else if (ctype == base_type) {
/* nothing */
} else if (is_int_type(base_type) && is_int_type(ctype)) {
base_type = &int_ctype;
} else if (is_restricted_type(base_type) != is_restricted_type(ctype)) {
if (!mix_bitwise++) {
warning(expr->pos, "mixed bitwiseness");
}
} else if (is_restricted_type(base_type) && base_type != ctype) {
sparse_error(expr->pos, "incompatible restricted type");
info(expr->pos, " expected: %s", show_typename(base_type));
info(expr->pos, " got: %s", show_typename(ctype));
base_type = &bad_ctype;
} else if (base_type != &bad_ctype) {
sparse_error(token->pos, "bad enum definition");
base_type = &bad_ctype;
}
parent->ctype.base_type = base_type;