forked from lamproae/ncc
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathparser.C
1609 lines (1404 loc) · 36.9 KB
/
parser.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
/*******************************************************************************
C parser
*******************************************************************************/
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <assert.h>
#include "global.h"
exprtree CExpr;
lrt last_result_type;
//***************************************************************************
// Forward
//***************************************************************************
static NormPtr constant_int_expression (NormPtr, int&);
static NormPtr parse_expression (NormPtr);
static NormPtr parse_structure (NormPtr);
//***************************************************************************
// Declarations I
//***************************************************************************
static NormPtr skip_parens (NormPtr);
static NormPtr skip_brackets (NormPtr);
static int initializer_nsize (NormPtr, int);
static NormPtr get_enum_consts (NormPtr);
class declarator
{
NormPtr p;
int dp;
inline void dcl ();
void dirdcl ();
void bitfield ();
void arglist_to_specs ();
NormPtr parse (NormPtr);
#ifdef GNU_VIOLATIONS
NormPtr bt_typeof (NormPtr);
#endif
NormPtr builtin (NormPtr);
NormPtr bt_enum (NormPtr);
NormPtr bt_typedef (NormPtr);
NormPtr bt_struct (NormPtr);
void complete_size ();
void argument_conversions ();
void semantics ();
bool do_argument_conversions;
public:
ObjPtr basetype;
bool have_extern, have_static, have_typedef, have_const,
have_init, have_code, is_anonymous, is_struct_dcl;
declarator (bool b = false) { do_argument_conversions = b; is_struct_dcl = false; }
NormPtr parse_base (NormPtr);
NormPtr parse_dcl (NormPtr);
typeID gentype;
NormPtr args;
NormPtr psst, pslen;
int symbol;
int spec [MSPEC];
};
//***********************************************************************
// definitions : declarator
//***********************************************************************
NormPtr declarator::parse (NormPtr i)
{
p = i; dp = 0; args = symbol = -1; have_init = have_code = false;
if (CODE [i] == ':' || (ISSYMBOL (CODE [i]) && CODE [i + 1] == ':'))
bitfield ();
else dcl ();
/* Ben Lau for M68K Kernel from uClinux */
if (CODE [p] == RESERVED___asm__)
p = skip_parens (p + 1);
spec [dp] = -1;
have_init = CODE [p] == '=';
have_code = CODE [p] == '{';
return p;
}
void declarator::bitfield ()
{
if (CODE [p] != ':') symbol = CODE [p++];
spec [0] = ':';
p = constant_int_expression (++p, spec [1]);
dp = 2;
}
void declarator::dcl ()
{
int ns = 0;
for (;; p++)
if (CODE [p] == '*') ++ns;
else if (CODE [p] != RESERVED_const
&& CODE [p] != RESERVED_volatile) break;
dirdcl ();
while (ns--) spec [dp++] = '*';
}
NormPtr array_size_expression (NormPtr p, int &r)
{
NormPtr pe = constant_int_expression (p, r);
if (CODE [p] != ']' && infuncs) {
pe = parse_expression (p);
r = 0;
}
return pe;
}
void declarator::dirdcl ()
{
if (CODE [p] == '(') {
++p;
dcl ();
if (CODE [p++] != ')') syntax_error (p, "Missing parenthesis");
} else if (ISSYMBOL (CODE [p]))
symbol = CODE [p++];
for (;;) {
switch (CODE [p]) {
case '(':
if (args == -1) args = p;
spec [dp++] = '(';
arglist_to_specs ();
continue;
case '[':
if (CODE [++p] == ']') {
spec [dp++] = '[';
spec [dp++] = 0;
p++; continue;
}
spec [dp++] = '[';
p = array_size_expression (p, spec [dp++]);
if (CODE [p++] != ']')
syntax_error (p, "No :]");
continue;
}
break;
}
}
void declarator::complete_size ()
{
int t = CODE [p + 1];
if (ISSTRING (t)) gstr: spec [1] = strlen (C_Strings [t - STRINGBASE]) + 1;
else if (t == '(' && ISSTRING (CODE [p + 2]) && CODE [p + 3] == ')') {
t = CODE [p + 2];
goto gstr;
} else if (t == '{') {
int i, a = esizeof_objptr (basetype);
for (i = 2; spec [i] == '['; i += 2)
a *= spec [i + 1];
spec [1] = initializer_nsize (p + 1, a);
} else syntax_error (p, "incomplete array initializer single");
}
void declarator::semantics ()
{
int i = 0, v, dpi = dp;
if (spec [0] == '(') {
if (have_init) syntax_error (p, "Function != Variable");
} else if (have_code) syntax_error (p, "Variable != Function");
if (spec [0] == ':') {
if (spec [1] < 0 || spec [1] > BITFIELD_Q)
syntax_error (p, "absurd");
return;
}
while (i < dpi)
if (spec [i] == '[') {
i += 2;
if (spec [i] == '(')
syntax_error (p, "array of functions");
} else if (spec [i] == '(') {
i += 2;
v = spec [i];
if (v == '(' || v == '[')
syntax_error (p, "Function returning invalid");
} else i++;
}
static int initializer_nsize (NormPtr p, int na)
{
int ec = 1;
NormPtr e = skip_brackets (p++);
while (p < e)
if (CODE [p] == ',') ec++, p++;
else if (CODE [p] == '{') {
ec += na;
p = skip_brackets (p);
} else p++;
return na ? ec / na : ec;
}
static NormPtr skip_parens (NormPtr p)
{
int ns = 0;
for (; p < C_Ntok; p++)
if (CODE [p] == '(') ++ns;
else if (CODE [p] == ')')
if (--ns == 0) return p + 1;
return syntax_error (p, "Unclosed parenthesis:)");
}
static NormPtr skip_brackets (NormPtr p)
{
int ns = 0;
for (; p < C_Ntok; p++)
if (CODE [p] == '{') ++ns;
else if (CODE [p] == '}')
if (--ns == 0) return p + 1;
return syntax_error (p, "Unclosed brackets:}");
}
//***********************************************************************
// definitions : declarator
//***********************************************************************
void declarator::argument_conversions ()
{
int tspec [MSPEC], *tp = tspec, *vp = spec;
if (basetype >= TYPEDEF_BASE && *vp == -1) {
type t;
opentype (basetype - TYPEDEF_BASE, t);
if (t.spec [0] == '(') *tp++ = '*';
}
if (*vp == '(') *tp++ = '*';
for (;;vp++) {
switch (*vp) {
case '*': *tp++ = '*'; continue;
case '[': *tp++ = '*'; vp++; continue;
case '(': *tp++ = '('; *tp++ = *++vp; continue;
}
*tp = -1;
break;
}
intcpy (spec, tspec);
}
NormPtr declarator::parse_dcl (NormPtr p)
{
p = parse (p);
if (have_init && spec [0] == '[' && spec [1] == 0) complete_size ();
semantics ();
if (do_argument_conversions) argument_conversions ();
gentype = gettype (basetype, spec);
return p;
}
NormPtr declarator::parse_base (NormPtr p)
{
have_extern = have_static = have_typedef = have_const = is_anonymous = false;
while (ISDCLFLAG (CODE [p]))
switch (CODE [p++]) {
case RESERVED_extern: have_extern = true; break;
case RESERVED_static: have_static = true; break;
case RESERVED_typedef: have_typedef = true; break;
case RESERVED_const: have_const = true; break;
}
if ((have_extern && have_static) || (have_extern && have_typedef)
|| (have_static && have_typedef)) syntax_error (p, "Decide");
if (ISBASETYPE (CODE [p]) || ISHBASETYPE (CODE [p]))
p = builtin (p);
else if (CODE [p] == RESERVED_struct || CODE [p] == RESERVED_union)
p = bt_struct (p);
else if (ISSYMBOL (CODE [p]))
p = bt_typedef (p);
else if (CODE [p] == RESERVED_enum)
p = bt_enum (p + 1);
#ifdef GNU_VIOLATIONS
else if (CODE [p] == RESERVED___typeof__)
p = bt_typeof (p + 1);
else if (CODE [p] == RESERVED___label__) {
while (CODE [p] != ';') p++;
}
#endif
else basetype = S_INT;
while (ISDCLFLAG (CODE [p]))
switch (CODE [p++]) {
case RESERVED_extern: have_extern = true; break;
case RESERVED_static: have_static = true; break;
case RESERVED_typedef: have_typedef = true; break;
case RESERVED_const: have_const = true; break;
}
return p;
}
NormPtr declarator::builtin (NormPtr p)
{
int bt, sh, lo, si, us;
bt = sh = lo = si = us = 0;
for (;ISBASETYPE (CODE [p]) || ISHBASETYPE (CODE [p])
|| CODE [p] == RESERVED_const; p++)
switch (CODE [p]) {
case RESERVED_long: lo++; break;
case RESERVED_short: sh++; break;
case RESERVED_signed: si++; break;
case RESERVED_unsigned: us++; break;
case RESERVED_const: continue;
default: if (bt) syntax_error (p, "Please specify");
bt = CODE [p];
}
if (bt == 0) bt = RESERVED_int;
if ((lo && sh) || (si && us)) syntax_error (p, "AMBIGUOUS specifiers");
switch (bt) {
case RESERVED_float: basetype = FLOAT; break;
case RESERVED_double: basetype = DOUBLE; break;
case RESERVED_void: basetype = VOID; break;
case RESERVED_char: basetype = (us) ? U_CHAR : S_CHAR; break;
case RESERVED_int:
if (sh) basetype = (us) ? U_SINT : S_SINT;
else if (lo == 1) basetype = (us) ? U_LINT : S_LINT;
else if (lo > 1) basetype = (us) ? U_LONG : S_LONG;
else basetype = (us) ? U_INT : S_INT;
}
return p;
}
NormPtr declarator::bt_enum (NormPtr p)
{
basetype = S_INT;
if (!ISSYMBOL (CODE [p]) && CODE [p] != '{')
syntax_error (p, "DEAD rats after enum");
if (CODE [p] != '{' && CODE [p + 1] != '{') {
if (!valid_enumtag (CODE [p]) &&
!introduce_enumtag (CODE [p], true))
syntax_error (p, "enum tag REDEFINED");
return p + 1;
}
if (CODE [p] != '{')
if (!introduce_enumtag (CODE [p++]))
syntax_error (p, "enum tag REDEFINED");
return get_enum_consts (p);
}
NormPtr declarator::bt_typedef (NormPtr p)
{
if ((basetype = lookup_typedef (CODE [p])) == -1) {
basetype = S_INT;
return p;
}
++p;
return ISHBASETYPE (CODE [p]) ? p + 1 : p;
}
NormPtr declarator::bt_struct (NormPtr p)
{
bool isst = CODE [p++] == RESERVED_struct;
if (!ISSYMBOL (CODE [p]) && CODE [p] != '{')
syntax_error (p, "DEAD RATS after struct");
if (CODE [p] != '{' && CODE [p + 1] != '{') {
basetype = (CODE [p + 1] == ';')
? fwd_struct_tag (CODE [p], isst)
: use_struct_tag (CODE [p], isst);
return p + 1;
}
if ((is_anonymous = CODE [p] == '{'))
basetype = introduce_anon_struct (isst);
else if ((basetype = introduce_named_struct (CODE [p++], isst)) == -1)
syntax_error (p, "Redefined structure tag");
is_struct_dcl = true;
psst = p++ - 1;
p = parse_structure (p);
pslen = p - psst;
return p;
}
static NormPtr get_enum_consts (NormPtr p)
{
int counter = 0, s;
for (++p;;) {
s = CODE [p++];
if (s == '}') break;
if (!ISSYMBOL (s))
syntax_error (p, "Random NOISE INSIDE ENUM");
if (CODE [p] == '=')
p = constant_int_expression (++p, counter);
if (!introduce_enumconst (s, counter++))
syntax_error (p, "Enumeration constant exists");
s = CODE [p++];
if (s == '}') break;
if (s != ',')
syntax_error (p, "RANDOM noise inside enum");
}
return p;
}
//***************************************************************************
// Forward
//***************************************************************************
static NormPtr initializer_expr (Symbol, NormPtr);
static NormPtr initializer_aggregate (Symbol, NormPtr);
static NormPtr parse_function (Symbol, NormPtr, NormPtr, NormPtr);
static bool is_dcl_start (int);
//***************************************************************************
// Declarations II
//
// Four places of declarators:
// 1) Declarator in code or global
// 2) Declarator inside structure block
// 3) Function argument list
// 4) (type cast)
//***************************************************************************
class cast_type
{
public:
typeID gentype;
NormPtr parse (NormPtr);
};
class arglist
{
NormPtr argument (NormPtr);
NormPtr parse_newstyle (NormPtr);
bool old_declaration (NormPtr);
NormPtr parse_oldstyle (NormPtr, bool = false);
public:
bool nolist;
typeID types [100];
int names [100], ii;
void parse_declare (NormPtr);
NormPtr parse (NormPtr);
};
class declaration
{
protected:
virtual void semantics (NormPtr);
NormPtr parse_initializer (NormPtr);
public:
declarator D;
declaration (bool b = false) : D (b) { }
NormPtr parse (NormPtr);
};
class declaration_instruct : public declaration
{
void semantics (NormPtr);
};
//***********************************************************************
// definitions: cast, arglist
//***********************************************************************
NormPtr cast_type::parse (NormPtr p)
{
declarator B;
p = B.parse_dcl (B.parse_base (p));
if (B.have_typedef || B.have_extern || B.have_static
|| B.have_code || B.have_init || B.symbol != -1
|| B.spec [0] == ':') syntax_error (p, "Improper cast");
gentype = B.gentype;
return p;
}
NormPtr arglist::argument (NormPtr p)
{
declarator B (true);
p = B.parse_dcl (B.parse_base (p));
if (B.have_typedef || B.have_extern || B.have_static
|| B.have_code || B.have_init || B.spec [0] == ':')
syntax_error (p, "Crap argument");
names [ii] = B.symbol;
types [ii++] = B.gentype;
return p;
}
bool arglist::old_declaration (NormPtr p)
{
return ISSYMBOL (CODE [p])
&& (CODE [p + 1] == ',' || CODE [p + 1] == ')')
&& lookup_typedef (CODE [p]) == -1;
}
NormPtr arglist::parse_newstyle (NormPtr p)
{
ii = 0;
if (CODE [p] == ')') {
nolist = true;
return p + 1;
}
if (CODE [p] == RESERVED_void && CODE [p + 1] == ')')
return p + 2;
while (1) {
if (CODE [p] == ELLIPSIS) {
types [ii++] = SPECIAL_ELLIPSIS;
if (CODE [p + 1] != ')')
syntax_error (p, "'...' must be last");
return p + 2;
}
switch (CODE [p = argument (p)]) {
default: syntax_error (p, "Invalid argument list");
case ')': return p + 1;
case ',': p++;
}
}
return p + 1;
}
NormPtr arglist::parse_oldstyle (NormPtr p, bool dcl)
{
// few syntax/semantics tests. We suppose old program, which
// is syntactically correct for the last 10 years
int i = 0, j;
ii = 1;
types [0] = ARGLIST_OPEN;
types [1] = -1;
if (CODE [p] != ')')
do names [i++] = CODE [p++]; while (CODE [p++] == ',');
if (dcl) {
declaration D (true);
while (CODE [p] != '{') {
p = D.parse (p);
for (j = 0; j < i; j++)
if (names [j] == D.D.symbol) {
names [j] = -1;
break;
}
}
for (j = 0; j < i; j++) if (names [j] != -1)
introduce_obj (names [j], SIntType, DEFAULT);
} else {
if (!is_dcl_start (CODE [p])) return p;
while (CODE [p] != '{') p++;
}
return p;
}
void arglist::parse_declare (NormPtr p)
{
if (old_declaration (p)) {
parse_oldstyle (p, true);
return;
}
int i;
parse_newstyle (p);
for (i = 0; i < ii; i++)
if (types [i] != SPECIAL_ELLIPSIS) {
if (names [i] == -1)
syntax_error (p, "Abstract argument");
introduce_obj (names [i], types [i], DEFAULT);
}
}
NormPtr arglist::parse (NormPtr p)
{
nolist = false;
return old_declaration (p) ? parse_oldstyle (p) : parse_newstyle (p);
}
void declarator::arglist_to_specs ()
{
arglist A;
p = A.parse (p + 1);
if (A.nolist) A.types [A.ii++] = ARGLIST_OPEN;
A.types [A.ii] = -1;
spec [dp++] = make_arglist (A.types);
}
//***********************************************************************
// definitions: declaration, instructure
//***********************************************************************
NormPtr declaration::parse (NormPtr p)
{
VARSPC vs;
bool ok;
typeID t;
NormPtr dclstart = p; // to report beginning of function...
D.is_struct_dcl = false;
p = D.parse_base (p);
if (CODE [p] == ';') {
if (D.is_struct_dcl && usage_only && report_structs && !D.is_anonymous) {
D.is_struct_dcl = false;
struct_location (D.basetype, D.psst, D.pslen);
} else if (D.is_struct_dcl && D.is_anonymous)
spill_anonymous (D.basetype);
} else while (CODE [p] != ';') {
p = D.parse_dcl (p);
semantics (p);
t = D.gentype;
vs = (D.have_extern)?EXTERN:(D.have_static)?STATIC:DEFAULT;
if (D.have_typedef) ok = introduce_tdef (D.symbol, t);
else ok = introduce_obj (D.symbol, t, vs);
if (!ok) syntax_error (p, "redefined: ", expand (D.symbol));
if (D.is_anonymous) D.is_anonymous = !rename_struct (t, D.symbol);
if (D.is_struct_dcl && usage_only && report_structs) {
D.is_struct_dcl = false;
struct_location (D.basetype, D.psst, D.pslen);
}
if (D.have_code) return parse_function (D.symbol, D.args, p, dclstart);
if (D.have_init) p = parse_initializer (p + 1);
if (CODE [p] == ';') break;
if (CODE [p] != ',') {
if (CODE [p] != RESERVED___asm__)
syntax_error(p, "unaccaptable declaration, separator;",
expand (CODE [p]));
p = skip_parens (p + 1);
continue;
}
p++;
}
return p + 1;
}
NormPtr declaration::parse_initializer (NormPtr p)
{
if (CODE [p] != '{')
return initializer_expr (D.symbol, p);
#ifdef PARSE_ARRAY_INITIALIZERS
return initializer_aggregate (D.symbol, p);
#else
return base_of (D.gentype) < _BTLIMIT ?
skip_brackets (p) : initializer_aggregate (D.symbol, p);
#endif
}
void declaration::semantics (NormPtr p)
{
if (D.spec [0] == ':'
|| (D.have_typedef && (D.have_init || D.have_code)))
syntax_error (p, "Not the \"right thing\"");
if (D.symbol == -1) syntax_error (p, "ABSENT symbol");
if (D.have_code && !INGLOBAL)
syntax_error (p, "Do you like nested functions?");
}
void declaration_instruct::semantics (NormPtr p)
{
if (D.have_static || D.have_extern || D.have_typedef
|| D.spec [0] == '(' || D.have_init || D.have_code
|| (D.symbol == -1 && D.spec [0] != ':'))
syntax_error (p, "Not good dcl in struct");
}
static bool is_typename (int token)
{
return token == RESERVED_struct || token == RESERVED_union
|| ISBASETYPE(token) || ISHBASETYPE (token)
|| token == RESERVED_enum
|| (ISSYMBOL (token) && is_typedef (token));
}
static bool is_dcl_start (int token)
{
return ISDCLFLAG (token) || is_typename (token)
#ifdef GNU_VIOLATIONS
|| token == RESERVED___label__
|| token == RESERVED___typeof__
#endif
;
}
//**************************************************************************
// Part II
//
// By now we are ok with the declarations (introducing new names
// to the program). Whenever we see a declaration, invoke a declarator.
// We can at any time lookup what's an identifier, with lookup().
//
// Missing:
// parsing expressions in initializers
//
// But this is ok, because this is exactly what we are going to
// do now.
//**************************************************************************
// C expressions
//**************************************************************************
NormPtr ExpressionPtr;
static int bopid;
static int priority (int op)
{
if (op > 256) switch (op) {
case LSH: bopid = SHL; return 11;
case RSH: bopid = SHR; return 11;
case GEQCMP: bopid = CGRE; return 10;
case LEQCMP: bopid = CLEE; return 10;
case EQCMP: bopid = BEQ; return 9;
case NEQCMP: bopid = BNEQ; return 9;
case ANDAND: bopid = IAND; return 5;
case OROR: bopid = IOR; return 4;
default: return 0;
}
switch (op) {
case '*': bopid = MUL; return 13;
case '/': bopid = DIV; return 13;
case '%': bopid = REM; return 13;
case '+': bopid = ADD; return 12;
case '-': bopid = SUB; return 12;
case '>': bopid = CGR; return 10;
case '<': bopid = CLE; return 10;
case '&': bopid = BAND; return 8;
}
if (op == '^') {
bopid = BXOR;
return 7;
}
if (op != '|') return 0;
bopid = BOR;
return 6;
}
static int xlate_uop (int op)
{
switch (op) {
case '!': return LNEG;
case '*': return PTRIND;
case '+': return UPLUS;
case '-': return UMINUS;
case '&': return ADDROF;
}
switch (op) {
case PLUSPLUS: return PPPRE;
case MINUSMINUS: return MMPRE;
default: return OCPL;
}
}
subexpr *&ee = CExpr.ee;
int &NeTop = CExpr.ne;
class expression_parser
{
inline void reloc ();
void value_to_expression (exprID, Symbol);
#ifdef GNU_VIOLATIONS
exprID gnu_st_expr ();
exprID gnu_ctor_expr (typeID);
#endif
#ifdef LABEL_VALUES
exprID gnu_label_value ();
#endif
exprID sizeof_typename ();
exprID unary_expression ();
exprID argument_expression_list ();
exprID primary_expression ();
exprID prefix_expression ();
exprID binary_expression (exprID, int);
exprID logicalOR_expression ();
int nee, extra;
public:
expression_parser (NormPtr, subexpr*);
exprID postfix_expression ();
exprID assignment_expression ();
exprID conditional_expression ();
exprID expression ();
NormPtr EP;
subexpr *ee;
int iee;
~expression_parser ();
};
//***********************************************************************
// definitions
//***********************************************************************
exprID expression_parser::sizeof_typename ()
{
cast_type T;
EP = T.parse (EP + 2);
if (CODE [EP++] != ')') syntax_error (EP, "erroneous sizeof");
ee [iee].action = VALUE;
exprID c = sizeof_typeID (T.gentype);
ee [iee].voici.value = c;
return iee++;
}
#define ISUNARY(x) \
((x<'~') ? (x=='!'||x=='*'||x=='&'||x=='-'||x=='+')\
: (x==PLUSPLUS || x==MINUSMINUS || x=='~'))
exprID expression_parser::unary_expression ()
{
int prefix [40];
typeID casts [40];
int ipr = 0, t;
exprID e = -1;
for (t = CODE [EP]; t < STRINGBASE; t = CODE [++EP])
if (ISUNARY (t)) prefix [ipr++] = xlate_uop (t);
else if (t == '(')
if (is_dcl_start (CODE [EP + 1])) {
cast_type C;
EP = C.parse (EP + 1);
casts [ipr] = C.gentype;
prefix [ipr++] = CAST;
#ifdef GNU_VIOLATIONS
if (CODE [EP + 1] == '{') {
e = gnu_ctor_expr (C.gentype);
goto have_unary;
}
#endif
} else break;
else if (t == RESERVED_sizeof)
if (CODE [EP+1] == '(' && is_dcl_start (CODE [EP+2])) {
e = sizeof_typename ();
goto have_unary;
} else prefix [ipr++] = SIZEOF;
#ifdef LABEL_VALUES
else if (t == ANDAND) {
e = gnu_label_value ();
goto have_unary;
}
#endif
else break;
if ((e = postfix_expression ()) == -1 && ipr > 0)
syntax_error (EP, "prefix operator w/o operand");
have_unary:
reloc (); /* * */
while (ipr--) {
ee [iee].voici.e = e;
if ((ee [iee].action = prefix [ipr]) == CAST)
ee [iee].voila.cast = casts [ipr];
e = iee++;
}
return e;
}
exprID expression_parser::primary_expression ()
{
int t = CODE [EP++];
if (ISSYMBOL (t)) {
ee [iee].action = SYMBOL;
ee [iee].voici.symbol = t;
return iee++;
}
if (ISNUMBER (t) || ISSTRING (t)) {
value_to_expression (iee, t);
return iee++;
}
if (t != '(') {
EP--;
return -1;
}
exprID e;
#ifdef GNU_VIOLATIONS
if (CODE [EP] == '{') e = gnu_st_expr (); else
#endif
e = expression ();
if (CODE [EP++] != ')')
syntax_error (EP, "parse error");
return e;
}
exprID expression_parser::postfix_expression ()
{
exprID e = primary_expression (), c;
if (e == -1) return -1;
int t;
for (t = CODE [EP];; t = CODE [++EP]) {
ee [iee].voici.e = e;
switch (t) {
case PLUSPLUS:
ee [e = iee++].action = PPPOST; break;
case MINUSMINUS:
ee [e = iee++].action = MMPOST; break;
case '[':
EP++;
ee [e = iee++].action = ARRAY;
c = expression ();
ee [e].e = c;
if (CODE [EP] != ']')
syntax_error (EP, "missing ']'");
break;
case '(':
EP++;
ee [e = iee++].action = FCALL;
c = argument_expression_list ();
ee [e].e = c;
if (CODE [EP] != ')')
syntax_error (EP, "missing ')'");
break;
case POINTSAT:
ee [e = iee++].action = PTRIND;
ee [iee].voici.e = e;
case '.':
ee [e = iee++].action = MEMB;
ee [e].voila.member = CODE [++EP];
if (!ISSYMBOL (ee [e].voila.member))
syntax_error (EP, "->members only");
break;
default:
return e;
}
}
}
exprID expression_parser::binary_expression (exprID lop, int pri)
{
static int p2;
int coperator = bopid;
exprID e;
EP++;
if ((e = unary_expression ()) == -1)
syntax_error (EP, "two operands expected");
p2 = priority (CODE [EP]);
while (p2 > pri)
e = binary_expression (e, p2);
ee [iee].voici.e = lop;
ee [iee].action = coperator;
ee [iee].e = e;
e = iee++;
return (p2 < pri) ? e : binary_expression (e, pri);
}
exprID expression_parser::logicalOR_expression ()
{
int p;
exprID e = unary_expression ();
if (e == -1) return -1;
while ((p = priority (CODE [EP])))
e = binary_expression (e, p);
return e;
}
exprID expression_parser::conditional_expression ()
{
exprID e = logicalOR_expression (), c;
if (e == -1) return -1;
if (CODE [EP] == '?') {
ee [iee].voici.e = e;