-
Notifications
You must be signed in to change notification settings - Fork 2
/
mcpp_eval.c
1340 lines (1257 loc) · 46.6 KB
/
mcpp_eval.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
/*-
* Copyright (c) 1998, 2002-2008 Kiyoshi Matsui <kmatsui@t3.rim.or.jp>
* All rights reserved.
*
* Some parts of this code are derived from the public domain software
* DECUS cpp (1984,1985) written by Martin Minow.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/*
* E V A L . C
* E x p r e s s i o n E v a l u a t i o n
*
* The routines to evaluate #if expression are placed here.
* Some routines are used also to evaluate the value of numerical tokens.
*/
#include "system.H"
#include "internal.H"
typedef struct optab {
char op; /* Operator */
char prec; /* Its precedence */
char skip; /* Short-circuit: non-0 to skip */
} OPTAB;
static int eval_lex( void);
/* Get type and value of token */
static int chk_ops( void);
/* Check identifier-like ops */
static VAL_SIGN * eval_char( char * const token);
/* Evaluate character constant */
static expr_t eval_one( char ** seq_pp, int wide, int mbits, int * ucn8);
/* Evaluate a character */
static VAL_SIGN * eval_eval( VAL_SIGN * valp, int op);
/* Entry to #if arithmetic */
static expr_t eval_signed( VAL_SIGN ** valpp, expr_t v1, expr_t v2, int op);
/* Do signed arithmetic of expr.*/
static expr_t eval_unsigned( VAL_SIGN ** valpp, uexpr_t v1u, uexpr_t v2u
, int op);
/* Do unsigned arithmetic */
static void overflow( const char * op_name, VAL_SIGN ** valpp
, int ll_overflow);
/* Diagnose overflow of expr. */
static int look_type( int typecode);
/* Look for type of the name */
/* For debug and error messages. */
static const char * const opname[ OP_END + 1] = {
"end of expression", "val", "(",
"unary +", "unary -", "~", "!",
"*", "/", "%",
"+", "-", "<<", ">>",
"<", "<=", ">", ">=", "==", "!=",
"&", "^", "|", "&&", "||",
"?", ":",
")", "(none)"
};
/*
* opdope[] has the operator (and operand) precedence:
* Bits
* 7 Unused (so the value is always positive)
* 6-2 Precedence (0000 .. 0174)
* 1-0 Binary op. flags:
* 10 The next binop flag (binop should/not follow).
* 01 The binop flag (should be set/cleared when this op is seen).
* Note: next binop
* value 1 0 Value doesn't follow value.
* Binop, ), END should follow value, value or unop doesn't.
* ( 0 0 ( doesn't follow value. Value follows.
* unary 0 0 Unop doesn't follow value. Value follows.
* binary 0 1 Binary op follows value. Value follows.
* ) 1 1 ) follows value. Binop, ), END follows.
* END 0 1 END follows value, doesn't follow ops.
*/
static const char opdope[ OP_END + 1] = {
0001, /* End of expression */
0002, 0170, /* VAL (constant), LPA */
/* Unary op's */
0160, 0160, 0160, 0160, /* PLU, NEG, COM, NOT */
/* Binary op's */
0151, 0151, 0151, /* MUL, DIV, MOD, */
0141, 0141, 0131, 0131, /* ADD, SUB, SL, SR */
0121, 0121, 0121, 0121, 0111, 0111, /* LT, LE, GT, GE, EQ, NE */
0101, 0071, 0061, 0051, 0041, /* AND, XOR, OR, ANA, ORO */
0031, 0031, /* QUE, COL */
/* Parens */
0013, 0023 /* RPA, END */
};
/*
* OP_QUE, OP_RPA and unary operators have alternate precedences:
*/
#define OP_RPA_PREC 0013
#define OP_QUE_PREC 0024 /* From right to left grouping */
#define OP_UNOP_PREC 0154 /* ditto */
/*
* S_ANDOR and S_QUEST signal "short-circuit" boolean evaluation, so that
* #if FOO != 0 && 10 / FOO ...
* doesn't generate an error message. They are stored in optab.skip.
*/
#define S_ANDOR 2
#define S_QUEST 1
static VAL_SIGN ev; /* Current value and signedness */
static int skip = 0; /* 3-way signal of skipping expr*/
static const char * const non_eval
= " (in non-evaluated sub-expression)"; /* _W8_ */
static int w_level = 1; /* warn_level at overflow of long */
/*
* In KR and OLD_PREP modes.
* Define bits for the basic types and their adjectives.
*/
#define T_CHAR 1
#define T_INT 2
#define T_FLOAT 4
#define T_DOUBLE 8
#define T_LONGDOUBLE 16
#define T_SHORT 32
#define T_LONG 64
#define T_LONGLONG 128
#define T_SIGNED 256
#define T_UNSIGNED 512
#define T_PTR 1024 /* Pointer to data objects */
#define T_FPTR 2048 /* Pointer to functions */
/*
* The SIZES structure is used to store the values for #if sizeof.
*/
typedef struct sizes {
int bits; /* If this bit is set, */
int size; /* this is the datum size value */
int psize; /* this is the pointer size */
} SIZES;
/*
* S_CHAR, etc. define the sizeof the basic TARGET machine word types.
* By default, sizes are set to the values for the HOST computer. If
* this is inappropriate, see those tables for details on what to change.
* Also, if you have a machine where sizeof (signed int) differs from
* sizeof (unsigned int), you will have to edit those tables and code in
* eval.c.
* Note: sizeof in #if expression is disallowed by Standard.
*/
#define S_CHAR (sizeof (char))
#define S_SINT (sizeof (short int))
#define S_INT (sizeof (int))
#define S_LINT (sizeof (long int))
#define S_FLOAT (sizeof (float))
#define S_DOUBLE (sizeof (double))
#define S_PCHAR (sizeof (char *))
#define S_PSINT (sizeof (short int *))
#define S_PINT (sizeof (int *))
#define S_PLINT (sizeof (long int *))
#define S_PFLOAT (sizeof (float *))
#define S_PDOUBLE (sizeof (double *))
#define S_PFPTR (sizeof (int (*)()))
#if HAVE_LONG_LONG
#define S_LLINT (sizeof (long long int))
#define S_PLLINT (sizeof (long long int *))
#endif
#define S_LDOUBLE (sizeof (long double))
#define S_PLDOUBLE (sizeof (long double *))
typedef struct types {
int type; /* This is the bits for types */
char * token_name; /* this is the token word */
int excluded; /* but these aren't legal here. */
} TYPES;
#define ANYSIGN (T_SIGNED | T_UNSIGNED)
#define ANYFLOAT (T_FLOAT | T_DOUBLE | T_LONGDOUBLE)
#if HAVE_LONG_LONG
#define ANYINT (T_CHAR | T_SHORT | T_INT | T_LONG | T_LONGLONG)
#else
#define ANYINT (T_CHAR | T_SHORT | T_INT | T_LONG)
#endif
static const TYPES basic_types[] = {
{ T_CHAR, "char", ANYFLOAT | ANYINT },
{ T_SHORT, "short", ANYFLOAT | ANYINT },
{ T_INT, "int", ANYFLOAT | T_CHAR | T_INT },
{ T_LONG, "long", ANYFLOAT | ANYINT },
#if HAVE_LONG_LONG
{ T_LONGLONG, "long long", ANYFLOAT | ANYINT },
#endif
{ T_FLOAT, "float", ANYFLOAT | ANYINT | ANYSIGN },
{ T_DOUBLE, "double", ANYFLOAT | ANYINT | ANYSIGN },
{ T_LONGDOUBLE, "long double", ANYFLOAT | ANYINT | ANYSIGN },
{ T_SIGNED, "signed", ANYFLOAT | ANYINT | ANYSIGN },
{ T_UNSIGNED, "unsigned", ANYFLOAT | ANYINT | ANYSIGN },
{ 0, NULL, 0 } /* Signal end */
};
/*
* In this table, T_FPTR (pointer to function) should be placed last.
*/
static const SIZES size_table[] = {
{ T_CHAR, S_CHAR, S_PCHAR }, /* char */
{ T_SHORT, S_SINT, S_PSINT }, /* short int */
{ T_INT, S_INT, S_PINT }, /* int */
{ T_LONG, S_LINT, S_PLINT }, /* long */
#if HAVE_LONG_LONG
{ T_LONGLONG, S_LLINT, S_PLLINT }, /* long long */
#endif
{ T_FLOAT, S_FLOAT, S_PFLOAT }, /* float */
{ T_DOUBLE, S_DOUBLE, S_PDOUBLE }, /* double */
{ T_LONGDOUBLE, S_LDOUBLE, S_PLDOUBLE }, /* long double */
{ T_FPTR, 0, S_PFPTR }, /* int (*()) */
{ 0, 0, 0 } /* End of table */
};
#define is_binary(op) (FIRST_BINOP <= op && op <= LAST_BINOP)
#define is_unary(op) (FIRST_UNOP <= op && op <= LAST_UNOP)
void init_eval( void)
{
skip = 0;
}
expr_t eval_if( void)
/*
* Evaluate a #if expression. Straight-forward operator precedence.
* This is called from directive() on encountering an #if directive.
* It calls the following routines:
* eval_lex() Lexical analyser -- returns the type and value of
* the next input token.
* eval_eval() Evaluates the current operator, given the values on the
* value stack. Returns a pointer to the (new) value stack.
*/
{
VAL_SIGN value[ NEXP * 2 + 1]; /* Value stack */
OPTAB opstack[ NEXP * 3 + 1]; /* Operator stack */
int parens = 0; /* Nesting levels of (, ) */
int prec; /* Operator precedence */
int binop = 0; /* Set if binary op. needed */
int op1; /* Operator from stack */
int skip_cur; /* For short-circuit testing */
VAL_SIGN * valp = value; /* -> Value and signedness */
OPTAB * opp = opstack; /* -> Operator stack */
int op; /* Current operator */
opp->op = OP_END; /* Mark bottom of stack */
opp->prec = opdope[ OP_END]; /* And its precedence */
skip = skip_cur = opp->skip = 0; /* Not skipping now */
while (1) {
skip = opp->skip;
op = eval_lex();
skip = 0; /* Reset to be ready to return */
switch (op) {
case OP_SUB :
if (binop == 0)
op = OP_NEG; /* Unary minus */
break;
case OP_ADD :
if (binop == 0)
op = OP_PLU; /* Unary plus */
break;
case OP_FAIL:
return 0L; /* Token error */
}
if (op == VAL) { /* Value? */
if (binop != 0) { /* Binop is needed */
cerror( "Misplaced constant \"%s\"" /* _E_ */
, work_buf, 0L, NULL);
return 0L;
} else if (& value[ NEXP * 2] <= valp) {
cerror( "More than %.0s%ld constants stacked at %s" /* _E_ */
, NULL, (long) (NEXP * 2 - 1), work_buf);
return 0L;
} else {
valp->val = ev.val;
(valp++)->sign = ev.sign;
binop = 1; /* Binary operator or so should follow */
}
continue;
} /* Else operators */
prec = opdope[ op];
if (binop != (prec & 1)) {
if (op == OP_EOE)
cerror( "Unterminated expression" /* _E_ */
, NULL, 0L, NULL);
else
cerror( "Operator \"%s\" in incorrect context" /* _E_ */
, opname[ op], 0L, NULL);
return 0L;
}
binop = (prec & 2) >> 1; /* Binop should follow? */
while (1) {
/* Stack coming sub-expression of higher precedence. */
if (opp->prec < prec) {
if (op == OP_LPA) {
prec = OP_RPA_PREC;
if (standard && (warn_level & 4)
&& ++parens == std_limits.exp_nest + 1)
cwarn(
"More than %.0s%ld nesting of parens" /* _W4_ */
, NULL, (long) std_limits.exp_nest, NULL);
} else if (op == OP_QUE) {
prec = OP_QUE_PREC;
} else if (is_unary( op)) {
prec = OP_UNOP_PREC;
}
op1 = opp->skip; /* Save skip for test */
/*
* Push operator onto operator stack.
*/
opp++;
if (& opstack[ NEXP * 3] <= opp) {
cerror(
"More than %.0s%ld operators and parens stacked at %s" /* _E_ */
, NULL, (long) (NEXP * 3 - 1), opname[ op]);
return 0L;
}
opp->op = op;
opp->prec = prec;
if (&value[0] < valp)
skip_cur = (valp[-1].val != 0L);
/* Short-circuit tester */
/*
* Do the short-circuit stuff here. Short-circuiting
* stops automagically when operators are evaluated.
*/
if ((op == OP_ANA && ! skip_cur)
|| (op == OP_ORO && skip_cur)) {
opp->skip = S_ANDOR; /* And/or skip starts */
if (skip_cur) /* Evaluate non-zero */
valp[-1].val = 1L; /* value to 1 */
} else if (op == OP_QUE) { /* Start of ?: operator */
opp->skip = (op1 & S_ANDOR) | (!skip_cur ? S_QUEST : 0);
} else if (op == OP_COL) { /* : inverts S_QUEST */
opp->skip = (op1 & S_ANDOR)
| (((op1 & S_QUEST) != 0) ? 0 : S_QUEST);
} else { /* Other operators leave*/
opp->skip = op1; /* skipping unchanged. */
}
break;
}
/*
* Coming sub-expression is of lower precedence.
* Evaluate stacked sub-expression.
* Pop operator from operator stack and evaluate it.
* End of stack and '(', ')' are specials.
*/
skip_cur = opp->skip; /* Remember skip value */
switch ((op1 = opp->op)) { /* Look at stacked op */
case OP_END: /* Stack end marker */
if (op == OP_RPA) { /* No corresponding ( */
cerror( "Excessive \")\"", NULL, 0L, NULL); /* _E_ */
return 0L;
}
if (op == OP_EOE)
return valp[-1].val; /* Finished ok. */
break;
case OP_LPA: /* ( on stack */
if (op != OP_RPA) { /* Matches ) on input? */
cerror( "Missing \")\"", NULL, 0L, NULL); /* _E_ */
return 0L;
}
opp--; /* Unstack it */
parens--; /* Count down nest level*/
break;
case OP_QUE: /* Evaluate true expr. */
break;
case OP_COL: /* : on stack */
opp--; /* Unstack : */
if (opp->op != OP_QUE) { /* Matches ? on stack? */
cerror(
"Misplaced \":\", previous operator is \"%s\"" /* _E_ */
, opname[opp->op], 0L, NULL);
return 0L;
}
/* Evaluate op1. Fall through */
default: /* Others: */
opp--; /* Unstack the operator */
if (op1 == OP_COL)
skip = 0;
else
skip = skip_cur;
valp = eval_eval( valp, op1);
if (valp->sign == VAL_ERROR)
return 0L; /* Out of range or divide by 0 */
valp++;
skip = 0;
} /* op1 switch end */
if (op1 == OP_END || op1 == OP_LPA || op1 == OP_QUE)
break; /* Read another op. */
} /* Stack unwind loop */
}
return 0L; /* Never reach here */
}
static int eval_lex( void)
/*
* Return next operator or constant to evaluate. Called from eval_if(). It
* calls a special-purpose routines for character constants and numeric values:
* eval_char() called to evaluate 'x'
* eval_num() called to evaluate numbers
* C++98 treats 11 identifier-like tokens as operators.
* POST_STD forbids character constants in #if expression.
*/
{
int c1;
VAL_SIGN * valp;
int warn = ! skip || (warn_level & 8);
int token_type;
int c;
ev.sign = SIGNED; /* Default signedness */
ev.val = 0L; /* Default value (on error or 0 value) */
in_if = ! skip; /* Inform to expand_macro() that the macro is */
/* in #if line and not skipped expression. */
c = skip_ws();
if (c == '\n') {
unget_ch();
return OP_EOE; /* End of expression */
}
token_type = get_unexpandable( c, warn);
if (standard && macro_line == MACRO_ERROR)
return OP_FAIL; /* Unterminated macro call */
if (token_type == NO_TOKEN)
return OP_EOE; /* Only macro(s) expanding to 0-token */
switch (token_type) {
case NAM:
if (standard && str_eq( identifier, "defined")) { /* defined name */
c1 = c = skip_ws();
if (c == '(') /* Allow defined (name) */
c = skip_ws();
if (scan_token( c, (workp = work_buf, &workp), work_end) == NAM) {
DEFBUF * defp = look_id( identifier);
if (warn) {
ev.val = (defp != NULL);
}
if (c1 != '(' || skip_ws() == ')') /* Balanced ? */
return VAL; /* Parsed ok */
}
cerror( "Bad defined syntax: %s" /* _E_ */
, infile->fp ? "" : infile->buffer, 0L, NULL);
break;
} else if (cplus_val) {
if (str_eq( identifier, "true")) {
ev.val = 1L;
return VAL;
} else if (str_eq( identifier, "false")) {
ev.val = 0L;
return VAL;
} else if (mcpp_mode != POST_STD
&& (openum = id_operator( identifier)) != 0) {
/* Identifier-like operator in C++98 */
strcpy( work_buf, identifier);
return chk_ops();
}
}
/*
* The ANSI C Standard says that an undefined symbol
* in an #if has the value zero. We are a bit pickier,
* warning except where the programmer was careful to write
* #if defined(foo) ? foo : 0
*/
if ((! skip && (warn_level & 4)) || (skip && (warn_level & 8)))
cwarn( "Undefined symbol \"%s\"%.0ld%s" /* _W4_ _W8_ */
, identifier, 0L, skip ? non_eval : ", evaluated to 0");
return VAL;
case CHR: /* Character constant */
case WCHR: /* Wide char constant */
valp = eval_char( work_buf); /* 'valp' points 'ev' */
if (valp->sign == VAL_ERROR)
break;
return VAL; /* Return a value */
case STR: /* String literal */
case WSTR: /* Wide string literal */
cerror(
"Can't use a string literal %s", work_buf, 0L, NULL); /* _E_ */
break;
case NUM: /* Numbers are harder */
valp = eval_num( work_buf); /* 'valp' points 'ev' */
if (valp->sign == VAL_ERROR)
break;
return VAL;
case OPE: /* Operator or punctuator */
return chk_ops();
default: /* Total nonsense */
cerror( "Can't use the character %.0s0x%02lx" /* _E_ */
, NULL, (long) c, NULL);
break;
}
return OP_FAIL; /* Any errors */
}
static int chk_ops( void)
/*
* Check the operator.
* If it can't be used in #if expression return OP_FAIL
* else return openum.
*/
{
switch (openum) {
case OP_STR: case OP_CAT: case OP_ELL:
case OP_1: case OP_2: case OP_3:
cerror( "Can't use the operator \"%s\"" /* _E_ */
, work_buf, 0L, NULL);
return OP_FAIL;
default:
return openum;
}
}
static int look_type(
int typecode
)
{
const char * const unknown_type
= "sizeof: Unknown type \"%s\"%.0ld%s"; /* _E_ _W8_ */
const char * const illeg_comb
= "sizeof: Illegal type combination with \"%s\"%.0ld%s"; /* _E_ _W8_ */
int token_type;
const TYPES * tp;
if (str_eq( identifier, "long")) {
if ((token_type
= get_unexpandable( skip_ws(), !skip || (warn_level & 8)))
== NO_TOKEN)
return typecode;
if (token_type == NAM) {
#if HAVE_LONG_LONG
if (str_eq( identifier, "long")) {
strcpy( work_buf, "long long");
goto basic;
}
#endif
if (str_eq( identifier, "double")) {
strcpy( work_buf, "long double");
goto basic;
}
}
unget_string( work_buf, NULL); /* Not long long */
strcpy( work_buf, "long"); /* nor long double */
}
/*
* Look for this unexpandable token in basic_types.
*/
basic:
for (tp = basic_types; tp->token_name != NULL; tp++) {
if (str_eq( work_buf, tp->token_name))
break;
}
if (tp->token_name == NULL) {
if (! skip) {
cerror( unknown_type, work_buf, 0L, NULL);
return 0;
} else if (warn_level & 8) {
cwarn( unknown_type, work_buf, 0L, non_eval);
}
}
if ((typecode & tp->excluded) != 0) {
if (! skip) {
cerror( illeg_comb, work_buf, 0L, NULL);
return 0;
} else if (warn_level & 8) {
cwarn( illeg_comb, work_buf, 0L, non_eval);
}
}
return typecode |= tp->type; /* Or in the type bit */
}
VAL_SIGN * eval_num(
const char * nump /* Preprocessing number */
)
/*
* Evaluate number for #if lexical analysis. Note: eval_num recognizes
* the unsigned suffix, but only returns a signed expr_t value, and stores
* the signedness to ev.sign, which is set UNSIGNED (== unsigned) if the
* value is not in the range of positive (signed) expr_t.
*/
{
const char * const not_integer = "Not an integer \"%s\""; /* _E_ */
const char * const out_of_range
= "Constant \"%s\"%.0ld%s is out of range"; /* _E_ _W1_ _W8_ */
expr_t value;
uexpr_t v, v1; /* unsigned long long or unsigned long */
int uflag = FALSE;
int lflag = FALSE;
int erange = FALSE;
int base;
int c, c1;
const char * cp = nump;
#if HAVE_LONG_LONG
const char * const out_of_range_long =
"Constant \"%s\"%.0ld%s is out of range " /* _E_ _W1_ _W2_ _W8_ */
"of (unsigned) long";
const char * const ll_suffix =
"LL suffix is used in other than C99 mode \"%s\"%.0ld%s"; /* _W1_ _W2_ _W8_ */
int llflag = FALSE;
int erange_long = FALSE;
#endif
ev.sign = SIGNED; /* Default signedness */
ev.val = 0L; /* Default value */
if ((char_type[ c = *cp++ & UCHARMAX] & DIG) == 0) /* Dot */
goto num_err;
if (c != '0') { /* Decimal */
base = 10;
} else if ((c = *cp++ & UCHARMAX) == 'x' || c == 'X') {
base = 16; /* Hexadecimal */
c = *cp++ & UCHARMAX;
} else if (c == EOS) { /* 0 */
return & ev;
} else { /* Octal or illegal */
base = 8;
}
v = v1 = 0L;
for (;;) {
c1 = c;
if (isupper( c1))
c1 = tolower( c1);
if (c1 >= 'a')
c1 -= ('a' - 10);
else
c1 -= '0';
if (c1 < 0 || base <= c1)
break;
v1 *= base;
v1 += c1;
if (v1 / base < v) { /* Overflow */
if (! skip)
goto range_err;
else
erange = TRUE;
}
#if HAVE_LONG_LONG
if (! stdc3 && v1 > ULONGMAX)
/* Overflow of long or unsigned long */
erange_long = TRUE;
#endif
v = v1;
c = *cp++ & UCHARMAX;
}
value = v;
while (c == 'u' || c == 'U' || c == 'l' || c == 'L') {
if (c == 'u' || c == 'U') {
if (uflag)
goto num_err;
uflag = TRUE;
} else if (c == 'l' || c == 'L') {
#if HAVE_LONG_LONG
if (llflag) {
goto num_err;
} else if (lflag) {
llflag = TRUE;
if (! stdc3 && ((! skip && (warn_level & w_level))
|| (skip && (warn_level & 8))))
cwarn( ll_suffix, nump, 0L, skip ? non_eval : NULL);
} else {
lflag = TRUE;
}
#else
if (lflag)
goto num_err;
else
lflag = TRUE;
#endif
}
c = *cp++;
}
if (c != EOS)
goto num_err;
if (standard) {
if (uflag) /* If 'U' suffixed, uexpr_t is treated as unsigned */
ev.sign = UNSIGNED;
else
ev.sign = (value >= 0L);
#if HAVE_LONG_LONG
} else {
if (value > LONGMAX)
erange_long = TRUE;
#endif
}
ev.val = value;
if (erange && (warn_level & 8))
cwarn( out_of_range, nump, 0L, non_eval);
#if HAVE_LONG_LONG
else if (erange_long && ((skip && (warn_level & 8))
|| (! stdc3 && ! skip && (warn_level & w_level))))
cwarn( out_of_range_long, nump, 0L, skip ? non_eval : NULL);
#endif
return & ev;
range_err:
cerror( out_of_range, nump, 0L, NULL);
ev.sign = VAL_ERROR;
return & ev;
num_err:
cerror( not_integer, nump, 0L, NULL);
ev.sign = VAL_ERROR;
return & ev;
}
static VAL_SIGN * eval_char(
char * const token
)
/*
* Evaluate a character constant.
* This routine is never called in POST_STD mode.
*/
{
const char * const w_out_of_range
= "Wide character constant %s%.0ld%s is out of range"; /* _E_ _W8_ */
const char * const c_out_of_range
= "Integer character constant %s%.0ld%s is out of range"; /* _E_ _W8_ */
uexpr_t value;
uexpr_t tmp;
expr_t cl;
int erange = FALSE;
int wide = (*token == 'L');
int ucn8;
int i;
int bits, mbits, u8bits, bits_save;
char * cp = token + 1; /* Character content */
#if HAVE_LONG_LONG
const char * const w_out_of_range_long =
"Wide character constant %s%.0ld%s is " /* _E_ _W1_ _W2_ _W8_ */
"out of range of unsigned long";
const char * const c_out_of_range_long =
"Integer character constant %s%.0ld%s is " /* _E_ _W1_ _W2_ _W8_ */
"out of range of unsigned long";
int erange_long = FALSE;
#endif
bits = CHARBIT;
u8bits = CHARBIT * 4;
if (mbchar & UTF8)
mbits = CHARBIT * 4;
else
mbits = CHARBIT * 2;
if (mcpp_mode == STD && wide) { /* Wide character constant */
cp++; /* Skip 'L' */
bits = mbits;
}
if ((cl = eval_one( &cp, wide, mbits, (ucn8 = FALSE, &ucn8)))
== -1L) {
ev.sign = VAL_ERROR;
return & ev;
}
bits_save = bits;
value = cl;
for (i = 0; *cp != '\'' && *cp != EOS; i++) {
cl = eval_one( &cp, wide, mbits, (ucn8 = FALSE, &ucn8));
if (cl == -1L) {
ev.sign = VAL_ERROR;
return & ev;
}
tmp = value;
value = (value << bits) | cl; /* Multi-char or multi-byte char */
if ((value >> bits) < tmp) { /* Overflow */
if (! skip)
goto range_err;
else
erange = TRUE;
}
#if HAVE_LONG_LONG
if ((mcpp_mode == STD && (! stdc3 && value > ULONGMAX))
|| (! standard && value > LONGMAX))
erange_long = TRUE;
#endif
}
ev.sign = ((expr_t) value >= 0L);
ev.val = value;
if (erange && skip && (warn_level & 8)) {
if (wide)
cwarn( w_out_of_range, token, 0L, non_eval);
else
cwarn( c_out_of_range, token, 0L, non_eval);
#if HAVE_LONG_LONG
} else if (erange_long && ((skip && (warn_level & 8))
|| (! stdc3 && ! skip && (warn_level & w_level)))) {
if (wide)
cwarn( w_out_of_range_long, token, 0L, skip ? non_eval : NULL);
else
cwarn( c_out_of_range_long, token, 0L, skip ? non_eval : NULL);
#endif
}
if (i == 0) /* Constant of single (character or wide-character) */
return & ev;
if ((! skip && (warn_level & 4)) || (skip && (warn_level & 8))) {
if (mcpp_mode == STD && wide)
cwarn(
"Multi-character wide character constant %s%.0ld%s isn't portable" /* _W4_ _W8_ */
, token, 0L, skip ? non_eval : NULL);
else
cwarn(
"Multi-character or multi-byte character constant %s%.0ld%s isn't portable" /* _W4_ _W8_ */
, token, 0L, skip ? non_eval : NULL);
}
return & ev;
range_err:
if (wide)
cerror( w_out_of_range, token, 0L, NULL);
else
cerror( c_out_of_range, token, 0L, NULL);
ev.sign = VAL_ERROR;
return & ev;
}
static expr_t eval_one(
char ** seq_pp, /* Address of pointer to sequence */
/* eval_one() advances the pointer to sequence */
int wide, /* Flag of wide-character */
int mbits, /* Number of bits of a wide-char */
int * ucn8 /* Flag of UCN-32 bits */
)
/*
* Called from eval_char() above to get a single character, single multi-
* byte character or wide character (with or without \ escapes).
* Returns the value of the character or -1L on error.
*/
{
const char * const out_of_range
= "%s%ld bits can't represent escape sequence '%s'"; /* _E_ _W8_ */
uexpr_t value;
int erange = FALSE;
char * seq = *seq_pp; /* Initial seq_pp for diagnostic*/
const char * cp;
const char * digits;
unsigned uc;
unsigned uc1;
int count;
int bits;
size_t wchar_max;
uc = *(*seq_pp)++ & UCHARMAX;
if (uc != '\\') /* Other than escape sequence */
return (expr_t) uc;
/* escape sequence */
uc1 = uc = *(*seq_pp)++ & UCHARMAX;
switch (uc) {
case 'a':
return '\a';
case 'b':
return '\b';
case 'f':
return '\f';
case 'n':
return '\n';
case 'r':
return '\r';
case 't':
return '\t';
case 'v':
return '\v';
case 'x': /* '\xFF' */
if (! standard)
goto undefined;
digits = "0123456789abcdef";
bits = 4;
uc = *(*seq_pp)++ & UCHARMAX;
break;
case '0': case '1': case '2': case '3':
case '4': case '5': case '6': case '7':
digits = "01234567";
bits = 3;
break;
case '\'': case '"': case '?': case '\\':
return (expr_t) uc;
default:
goto undefined;
}
wchar_max = (UCHARMAX << CHARBIT) | UCHARMAX;
if (mbits == CHARBIT * 4) {
if (mcpp_mode == STD)
wchar_max = (wchar_max << CHARBIT * 2) | wchar_max;
else
wchar_max = LONGMAX;
}
value = 0L;
for (count = 0; ; ++count) {
if (isupper( uc))
uc = tolower( uc);
if ((cp = strchr( digits, uc)) == NULL)
break;
if (count >= 3 && bits == 3)
break; /* Octal escape sequence at most 3 digits */
value = (value << bits) | (cp - digits);
if (wchar_max < value)
{
if (! skip)
goto range_err;
else
erange = TRUE;
}
uc = *(*seq_pp)++ & UCHARMAX;
}
(*seq_pp)--;
if (erange) {
value &= wchar_max;
goto range_err;
}
if (count == 0 && bits == 4) /* '\xnonsense' */
goto undefined;
if (! wide && (UCHARMAX < value)) {
value &= UCHARMAX;
goto range_err;
}
return (expr_t) value;
undefined:
uc1 = **seq_pp;
**seq_pp = EOS; /* For diagnostic */
if ((! skip && (warn_level & 1)) || (skip && (warn_level & 8)))
cwarn(
"Undefined escape sequence%s %.0ld'%s'" /* _W1_ _W8_ */
, skip ? non_eval : NULL, 0L, seq);
**seq_pp = uc1;
*seq_pp = seq + 1;
return (expr_t) '\\'; /* Returns the escape char */
range_err:
uc1 = **seq_pp;
**seq_pp = EOS; /* For diagnostic */
if (wide) {
if (! skip)
cerror( out_of_range, NULL, (long) mbits, seq);
else if (warn_level & 8)
cwarn( out_of_range, non_eval, (long) mbits, seq);
} else {
if (! skip)
cerror( out_of_range, NULL, (long) CHARBIT, seq);
else if (warn_level & 8)
cwarn( out_of_range, non_eval, (long) CHARBIT, seq);
}
**seq_pp = uc1;
if (! skip)
return -1L;
else
return (expr_t) value;
}
static VAL_SIGN * eval_eval(
VAL_SIGN * valp,
int op
)
/*
* One or two values are popped from the value stack and do arithmetic.
* The result is pushed onto the value stack.
* eval_eval() returns the new pointer to the top of the value stack.