summaryrefslogtreecommitdiff
path: root/compiler_and_linker/FrontEnd/C/CPreprocess.c
blob: 4e6b3c0f0f59da752cbd4212471809074ee457cb (plain)
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
#include "compiler/CPreprocess.h"
#include "compiler/CError.h"
#include "compiler/CMachine.h"
#include "compiler/CParser.h"
#include "compiler/CPrep.h"
#include "compiler/CPrepTokenizer.h"
#include "compiler/CompilerTools.h"
#include "cos.h"

void CPrep_PreprocessDumpNewLine(void) {
    if (copts.line_prepdump && pplist.data && filesp >= 0)
        AppendGListData(&pplist, "\r", 1);
}

void CPrep_PreprocessDumpFileInfo(Boolean flag) {
    char linebuf[512];
    int size;
    SInt16 tmp16;
    SInt32 tmp32;
    Str255 filename;

    if (pplist.data && filesp >= 0) {
        if (nlflag && flag && pplist.size > 0)
            AppendGListName(&pplist, "\r");

        if (copts.line_prepdump)
            size = sprintf(linebuf, "#line % " PRId32 "\t\"", linenumber);
        else
            size = sprintf(linebuf, "/* #line % " PRId32 "\t\"", linenumber);
        AppendGListData(&pplist, linebuf, size);

        if (copts.fullpath_prepdump) {
            if (prep_file->nameNode) {
                AppendGListData(&pplist, prep_file->nameNode->name, strlen(prep_file->nameNode->name));
            } else {
                COS_FileGetPathName(linebuf, &prep_file->textfile, &tmp32);
                AppendGListData(&pplist, linebuf, strlen(linebuf));
            }
        } else {
            if (prep_file->nameNode) {
                char *work = prep_file->nameNode->name + strlen(prep_file->nameNode->name);
                while (work > prep_file->nameNode->name && !strchr("/\\:", work[-1]))
                    work--;

                AppendGListData(&pplist, work, strlen(work));
            } else {
                COS_FileGetFSSpecInfo(&prep_file->textfile, &tmp16, &tmp32, filename);
                AppendGListData(&pplist, &filename[1], filename[0]);
            }
        }

        size = sprintf(linebuf, "\"\t/* stack depth % " PRId32 " */", filesp);
        AppendGListData(&pplist, linebuf, size);

        if (copts.line_prepdump && flag)
            CPrep_PreprocessDumpNewLine();

        nlflag = 1;
    }
}

static void CPrep_DumpWString(UInt16 *str, short len) {
    int divisor;
    int i;

    while (len--) {
        if (*str < 32) {
            AppendGListByte(&pplist, '\\');
            switch (*str) {
                case 7:
                    AppendGListByte(&pplist, 'a');
                    break;
                case 8:
                    AppendGListByte(&pplist, 'b');
                    break;
                case 27:
                    AppendGListByte(&pplist, 'e');
                    break;
                case 12:
                    AppendGListByte(&pplist, 'f');
                    break;
                case 10:
                    AppendGListByte(&pplist, 'n');
                    break;
                case 13:
                    AppendGListByte(&pplist, 'r');
                    break;
                case 9:
                    AppendGListByte(&pplist, 't');
                    break;
                case 11:
                    AppendGListByte(&pplist, 'v');
                    break;
                default:
                    if (*str >= 8)
                        AppendGListByte(&pplist, '0' + (*str / 8));
                    AppendGListByte(&pplist, '0' + (*str % 8));
            }
        } else if (*str > 255) {
            AppendGListByte(&pplist, '\\');
            AppendGListByte(&pplist, 'x');

            divisor = 0x1000;
            for (i = 0; i < 4; i++) {
                AppendGListByte(&pplist, "0123456789ABCDEF"[(*str / divisor) % 16]);
                divisor /= 16;
            }
        } else {
            switch (*str) {
                case '"':
                case '\\':
                    AppendGListByte(&pplist, '\\');
                default:
                    AppendGListByte(&pplist, *str);
            }
        }
        str++;
    }
}

static void CPrep_DumpString(UInt8 *str, short len) {
    while (len--) {
        if (*str < 32) {
            AppendGListByte(&pplist, '\\');
            switch (*str) {
                case 7:
                    AppendGListByte(&pplist, 'a');
                    break;
                case 8:
                    AppendGListByte(&pplist, 'b');
                    break;
                case 12:
                    AppendGListByte(&pplist, 'f');
                    break;
                case 10:
                    AppendGListByte(&pplist, 'n');
                    break;
                case 13:
                    AppendGListByte(&pplist, 'r');
                    break;
                case 9:
                    AppendGListByte(&pplist, 't');
                    break;
                case 11:
                    AppendGListByte(&pplist, 'v');
                    break;
                default:
                    if (*str >= 8)
                        AppendGListByte(&pplist, '0' + (*str / 8));
                    AppendGListByte(&pplist, '0' + (*str % 8));
            }
        } else {
            switch (*str) {
                case '"':
                case '\\':
                    AppendGListByte(&pplist, '\\');
                default:
                    AppendGListByte(&pplist, *str);
            }
        }
        str++;
    }
}

void CPrep_Preprocess(void) {
    short innertoken;
    short token;
    char startToken;
    char endToken;
    int depth;
    Boolean save_asmpoundcomment; // r16
    Boolean save_cplusplus; // r15
    char *p;

    startToken = 0;
    depth = 0;

    if (InitGList(&pplist, 10000))
        CError_NoMem();

    nlflag = 0;
    spaceskip = 0;

    if ((token = lex())) {
        do {
            if (nlflag) {
                if (!copts.line_prepdump)
                    AppendGListData(&pplist, "\r", 1);
            } else {
                if (spaceskip)
                    AppendGListByte(&pplist, ' ');
            }

            while (1) {
                switch ((innertoken = token)) {
                    case '(':
                    case ')':
                    case '{':
                    case '}':
                        AppendGListByte(&pplist, token);
                        if (cprep_nostring) {
                            if (innertoken == startToken) {
                                depth++;
                            } else if (innertoken == endToken) {
                                if (--depth == 0) {
                                    cprep_nostring = 0;
                                    in_assembler = 0;
                                    copts.cplusplus = save_cplusplus;
                                    copts.asmpoundcomment = save_asmpoundcomment;
                                }
                            }
                        }
                        break;

                    case TK_INTCONST:
                    case TK_FLOATCONST:
                        if (tokenstacklevel > 0)
                            p = macropos;
                        else
                            p = prep_file_start + ts_current[-1].tokenoffset;
                        AppendGListData(&pplist, p, pos - p);
                        break;

                    case TK_IDENTIFIER:
                        AppendGListData(&pplist, tkidentifier->name, strlen(tkidentifier->name));
                        break;

                    case TK_AUTO:
                        AppendGListData(&pplist, "auto", 4);
                        break;
                    case TK_REGISTER:
                        AppendGListData(&pplist, "register", 8);
                        break;
                    case TK_STATIC:
                        AppendGListData(&pplist, "static", 6);
                        break;
                    case TK_EXTERN:
                        AppendGListData(&pplist, "extern", 6);
                        break;
                    case TK_TYPEDEF:
                        AppendGListData(&pplist, "typedef", 7);
                        break;
                    case TK_INLINE:
                        AppendGListData(&pplist, "inline", 6);
                        break;
                    case TK_VOID:
                        AppendGListData(&pplist, "void", 4);
                        break;
                    case TK_CHAR:
                        AppendGListData(&pplist, "char", 4);
                        break;
                    case TK_SHORT:
                        AppendGListData(&pplist, "short", 5);
                        break;
                    case TK_INT:
                        AppendGListData(&pplist, "int", 3);
                        break;
                    case TK_LONG:
                        AppendGListData(&pplist, "long", 4);
                        break;
                    case TK_FLOAT:
                        AppendGListData(&pplist, "float", 5);
                        break;
                    case TK_DOUBLE:
                        AppendGListData(&pplist, "double", 6);
                        break;
                    case TK_SIGNED:
                        AppendGListData(&pplist, "signed", 6);
                        break;
                    case TK_UNSIGNED:
                        AppendGListData(&pplist, "unsigned", 8);
                        break;
                    case TK_STRUCT:
                        AppendGListData(&pplist, "struct", 6);
                        break;
                    case TK_UNION:
                        AppendGListData(&pplist, "union", 5);
                        break;
                    case TK_ENUM:
                        AppendGListData(&pplist, "enum", 4);
                        break;
                    case TK_CLASS:
                        AppendGListData(&pplist, "class", 5);
                        break;
                    case TK_CONST:
                        AppendGListData(&pplist, "const", 5);
                        break;
                    case TK_VOLATILE:
                        AppendGListData(&pplist, "volatile", 8);
                        break;
                    case TK_PASCAL:
                        AppendGListData(&pplist, "pascal", 6);
                        break;
                    case TK_UU_FAR:
                        AppendGListData(&pplist, "__far", 5);
                        break;
                    case TK_ONEWAY:
                        AppendGListData(&pplist, "oneway", 6);
                        break;
                    case TK_IN:
                        AppendGListData(&pplist, "in", 2);
                        break;
                    case TK_OUT:
                        AppendGListData(&pplist, "out", 3);
                        break;
                    case TK_INOUT:
                        AppendGListData(&pplist, "inout", 5);
                        break;
                    case TK_BYCOPY:
                        AppendGListData(&pplist, "bycopy", 6);
                        break;
                    case TK_BYREF:
                        AppendGListData(&pplist, "byref", 5);
                        break;
                    case TK_ASM:
                        AppendGListData(&pplist, "asm", 3);
                        endToken = 0;
                        startToken = 0;
                        AppendGListByte(&pplist, ' ');
                        token = lex();
                        if (token == TK_VOLATILE || (token == TK_IDENTIFIER && !strcmp(tkidentifier->name, "__volatile__"))) {
                            AppendGListData(&pplist, "volatile", 8);
                            token = lex();
                        }
                        if (token) {
                            if (token < ' ' || token > 255)
                                continue;
                            AppendGListByte(&pplist, token);

                            if (token == '(') {
                                startToken = '(';
                                endToken = ')';
                            } else if (token == '{') {
                                startToken = '{';
                                endToken = '}';
                            } else {
                                continue;
                            }

                            cprep_nostring = 1;
                            in_assembler = 1;
                            depth = 1;
                            save_asmpoundcomment = copts.asmpoundcomment;
                            save_cplusplus = copts.cplusplus;

                            token = lex();
                            if (token == '"') {
                                AppendGListByte(&pplist, token);
                                copts.cplusplus = 0;
                                copts.asmpoundcomment = 1;
                                break;
                            } else if (token == 0) {
                                break;
                            }
                            continue;
                        }
                        break;

                    case TK_CASE:
                        AppendGListData(&pplist, "case", 4);
                        break;
                    case TK_DEFAULT:
                        AppendGListData(&pplist, "default", 7);
                        break;
                    case TK_IF:
                        AppendGListData(&pplist, "if", 2);
                        break;
                    case TK_ELSE:
                        AppendGListData(&pplist, "else", 4);
                        break;
                    case TK_SWITCH:
                        AppendGListData(&pplist, "switch", 6);
                        break;
                    case TK_WHILE:
                        AppendGListData(&pplist, "while", 5);
                        break;
                    case TK_DO:
                        AppendGListData(&pplist, "do", 2);
                        break;
                    case TK_FOR:
                        AppendGListData(&pplist, "for", 3);
                        break;
                    case TK_GOTO:
                        AppendGListData(&pplist, "goto", 4);
                        break;
                    case TK_CONTINUE:
                        AppendGListData(&pplist, "continue", 8);
                        break;
                    case TK_BREAK:
                        AppendGListData(&pplist, "break", 5);
                        break;
                    case TK_RETURN:
                        AppendGListData(&pplist, "return", 6);
                        break;
                    case TK_SIZEOF:
                        AppendGListData(&pplist, "sizeof", 6);
                        break;
                    case TK_CATCH:
                        AppendGListData(&pplist, "catch", 5);
                        break;
                    case TK_DELETE:
                        AppendGListData(&pplist, "delete", 6);
                        break;
                    case TK_FRIEND:
                        AppendGListData(&pplist, "friend", 6);
                        break;
                    case TK_NEW:
                        AppendGListData(&pplist, "new", 3);
                        break;
                    case TK_OPERATOR:
                        AppendGListData(&pplist, "operator", 8);
                        break;
                    case TK_PRIVATE:
                        AppendGListData(&pplist, "private", 7);
                        break;
                    case TK_PROTECTED:
                        AppendGListData(&pplist, "protected", 9);
                        break;
                    case TK_PUBLIC:
                        AppendGListData(&pplist, "public", 6);
                        break;
                    case TK_TEMPLATE:
                        AppendGListData(&pplist, "template", 8);
                        break;
                    case TK_THIS:
                        AppendGListData(&pplist, "this", 4);
                        break;
                    case TK_THROW:
                        AppendGListData(&pplist, "throw", 5);
                        break;
                    case TK_TRY:
                        AppendGListData(&pplist, "try", 3);
                        break;
                    case TK_VIRTUAL:
                        AppendGListData(&pplist, "virtual", 7);
                        break;
                    case TK_INHERITED:
                        AppendGListData(&pplist, "inherited", 9);
                        break;
                    case TK_CONST_CAST:
                        AppendGListData(&pplist, "const_cast", 10);
                        break;
                    case TK_DYNAMIC_CAST:
                        AppendGListData(&pplist, "dynamic_cast", 12);
                        break;
                    case TK_EXPLICIT:
                        AppendGListData(&pplist, "explicit", 8);
                        break;
                    case TK_MUTABLE:
                        AppendGListData(&pplist, "mutable", 7);
                        break;
                    case TK_NAMESPACE:
                        AppendGListData(&pplist, "namespace", 9);
                        break;
                    case TK_REINTERPRET_CAST:
                        AppendGListData(&pplist, "reinterpret_cast", 16);
                        break;
                    case TK_STATIC_CAST:
                        AppendGListData(&pplist, "static_cast", 11);
                        break;
                    case TK_USING:
                        AppendGListData(&pplist, "using", 5);
                        break;
                    case TK_WCHAR_T:
                        AppendGListData(&pplist, "wchar_t", 7);
                        break;
                    case TK_TYPENAME:
                        AppendGListData(&pplist, "typename", 8);
                        break;
                    case TK_TRUE:
                        AppendGListData(&pplist, "true", 4);
                        break;
                    case TK_FALSE:
                        AppendGListData(&pplist, "false", 5);
                        break;
                    case TK_TYPEID:
                        AppendGListData(&pplist, "typeid", 6);
                        break;
                    case TK_EXPORT:
                        AppendGListData(&pplist, "export", 6);
                        break;
                    case TK_UU_STDCALL:
                        AppendGListData(&pplist, "__stdcall", 9);
                        break;
                    case TK_UU_CDECL:
                        AppendGListData(&pplist, "__cdecl", 7);
                        break;
                    case TK_UU_FASTCALL:
                        AppendGListData(&pplist, "__fastcall", 10);
                        break;
                    case TK_UU_DECLSPEC:
                        AppendGListData(&pplist, "__declspec", 10);
                        break;
                    case TK_MULT_ASSIGN:
                        AppendGListData(&pplist, "*=", 2);
                        break;
                    case TK_DIV_ASSIGN:
                        AppendGListData(&pplist, "/=", 2);
                        break;
                    case TK_MOD_ASSIGN:
                        AppendGListData(&pplist, "%=", 2);
                        break;
                    case TK_ADD_ASSIGN:
                        AppendGListData(&pplist, "+=", 2);
                        break;
                    case TK_SUB_ASSIGN:
                        AppendGListData(&pplist, "-=", 2);
                        break;
                    case TK_SHL_ASSIGN:
                        AppendGListData(&pplist, "<<=", 3);
                        break;
                    case TK_SHR_ASSIGN:
                        AppendGListData(&pplist, ">>=", 3);
                        break;
                    case TK_AND_ASSIGN:
                        AppendGListData(&pplist, "&=", 2);
                        break;
                    case TK_XOR_ASSIGN:
                        AppendGListData(&pplist, "^=", 2);
                        break;
                    case TK_OR_ASSIGN:
                        AppendGListData(&pplist, "|=", 2);
                        break;
                    case TK_LOGICAL_OR:
                        AppendGListData(&pplist, "||", 2);
                        break;
                    case TK_LOGICAL_AND:
                        AppendGListData(&pplist, "&&", 2);
                        break;
                    case TK_LOGICAL_EQ:
                        AppendGListData(&pplist, "==", 2);
                        break;
                    case TK_LOGICAL_NE:
                        AppendGListData(&pplist, "!=", 2);
                        break;
                    case TK_LESS_EQUAL:
                        AppendGListData(&pplist, "<=", 2);
                        break;
                    case TK_GREATER_EQUAL:
                        AppendGListData(&pplist, ">=", 2);
                        break;
                    case TK_SHL:
                        AppendGListData(&pplist, "<<", 2);
                        break;
                    case TK_SHR:
                        AppendGListData(&pplist, ">>", 2);
                        break;
                    case TK_INCREMENT:
                        AppendGListData(&pplist, "++", 2);
                        break;
                    case TK_DECREMENT:
                        AppendGListData(&pplist, "--", 2);
                        break;
                    case TK_ARROW:
                        AppendGListData(&pplist, "->", 2);
                        break;
                    case TK_ELLIPSIS:
                        AppendGListData(&pplist, "...", 3);
                        break;
                    case TK_DOT_STAR:
                        AppendGListData(&pplist, ".*", 2);
                        break;
                    case TK_ARROW_STAR:
                        AppendGListData(&pplist, "->*", 3);
                        break;
                    case TK_COLON_COLON:
                        AppendGListData(&pplist, "::", 2);
                        break;
                    case TK_AT_INTERFACE:
                        AppendGListData(&pplist, "@interface", 10);
                        break;
                    case TK_AT_IMPLEMENTATION:
                        AppendGListData(&pplist, "@implementation", 15);
                        break;
                    case TK_AT_PROTOCOL:
                        AppendGListData(&pplist, "@protocol", 9);
                        break;
                    case TK_AT_END:
                        AppendGListData(&pplist, "@end", 4);
                        break;
                    case TK_AT_PRIVATE:
                        AppendGListData(&pplist, "@private", 8);
                        break;
                    case TK_AT_PROTECTED:
                        AppendGListData(&pplist, "@protected", 10);
                        break;
                    case TK_AT_PUBLIC:
                        AppendGListData(&pplist, "@public", 7);
                        break;
                    case TK_AT_CLASS:
                        AppendGListData(&pplist, "@class", 6);
                        break;
                    case TK_AT_SELECTOR:
                        AppendGListData(&pplist, "@selector", 9);
                        break;
                    case TK_AT_ENCODE:
                        AppendGListData(&pplist, "@encode", 7);
                        break;
                    case TK_AT_DEFS:
                        AppendGListData(&pplist, "@defs", 5);
                        break;
                    case TK_SELF:
                        AppendGListData(&pplist, "self", 4);
                        break;
                    case TK_SUPER:
                        AppendGListData(&pplist, "super", 5);
                        break;
                    case TK_BOOL:
                        if (!copts.cplusplus && copts.c9x)
                            AppendGListData(&pplist, "_Bool", 5);
                        else
                            AppendGListData(&pplist, "bool", 4);
                        break;
                    case TK_RESTRICT:
                        if (copts.c9x)
                            AppendGListData(&pplist, "restrict", 8);
                        else
                            AppendGListData(&pplist, "__restrict", 10);
                        break;
                    case TK_UU_VECTOR:
                        AppendGListData(&pplist, "__vector", 8);
                        break;
                    case TK_UU_TYPEOF_UU:
                        AppendGListData(&pplist, "__typeof__", 10);
                        break;
                    case TK_UU_ATTRIBUTE_UU:
                        AppendGListData(&pplist, "__attribute__", 13);
                        break;
                    case TK_UU_ALIGNOF_UU:
                        AppendGListData(&pplist, "__alignof__", 11);
                        break;
                    case TK_UU_UUIDOF:
                        AppendGListData(&pplist, "__uuidof", 8);
                        break;
                    case TK_U_COMPLEX:
                        AppendGListData(&pplist, "_Complex", 8);
                        break;
                    case TK_U_IMAGINARY:
                        AppendGListData(&pplist, "_Imaginary", 10);
                        break;

                    case TK_STRING:
                        if (ispascalstring) {
                            AppendGListData(&pplist, "\"\\p", 3);
                            CPrep_DumpString((UInt8 *) tkstring + 1, tksize - 1);
                        } else {
                            AppendGListByte(&pplist, '"');
                            CPrep_DumpString((UInt8 *) tkstring, tksize - 1);
                        }
                        AppendGListByte(&pplist, '"');
                        break;

                    case TK_STRING_WIDE:
                        AppendGListData(&pplist, "L\"", 2);
                        CPrep_DumpWString((UInt16 *) tkstring, (tksize / stwchar.size) - 1);
                        AppendGListByte(&pplist, '"');
                        break;

                    default:
                        if (token >= 32 && token <= 255)
                            AppendGListByte(&pplist, token);
                        else
                            CError_FATAL(563);
                }
                break;
            }

            CPrep_TokenStreamFlush();
            nlflag = 0;
            spaceskip = 0;
        } while ((token = lex()));
    }

    AppendGListByte(&pplist, 0);
    COS_ResizeHandle(pplist.data, pplist.size);
}