OpenCores
URL https://opencores.org/ocsvn/thor/thor/trunk

Subversion Repositories thor

[/] [thor/] [trunk/] [FT64v5/] [software/] [CC64/] [source/] [ParseDeclarations.cpp] - Blame information for rev 48

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 48 robfinch
// ============================================================================
2
//        __
3
//   \\__/ o\    (C) 2012-2018  Robert Finch, Waterloo
4
//    \  __ /    All rights reserved.
5
//     \/_//     robfinch<remove>@finitron.ca
6
//       ||
7
//
8
// CC64 - 'C' derived language compiler
9
//  - 64 bit CPU
10
//
11
// This source file is free software: you can redistribute it and/or modify 
12
// it under the terms of the GNU Lesser General Public License as published 
13
// by the Free Software Foundation, either version 3 of the License, or     
14
// (at your option) any later version.                                      
15
//                                                                          
16
// This source file is distributed in the hope that it will be useful,      
17
// but WITHOUT ANY WARRANTY; without even the implied warranty of           
18
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            
19
// GNU General Public License for more details.                             
20
//                                                                          
21
// You should have received a copy of the GNU General Public License        
22
// along with this program.  If not, see <http://www.gnu.org/licenses/>.    
23
//                                                                          
24
// ============================================================================
25
//
26
#include "stdafx.h"
27
 
28
TYP *head = (TYP *)NULL;
29
TYP *tail = (TYP *)NULL;
30
std::string *declid;
31
//char *Declaration::declid = (char *)NULL;
32
TABLE tagtable;
33
TYP stdconst;
34
TYP stdvector;
35
TYP *stdvectormask;
36
Stringx names[20];
37
int nparms = 0;
38
int funcdecl = 0;                //0,1, or 2
39
int nfc = 0;
40
int isFirstCall = 0;
41
int catchdecl = FALSE;
42
int isTypedef = FALSE;
43
bool isUnion = false;
44
int isUnsigned = FALSE;
45
int isSigned = FALSE;
46
int isVolatile = FALSE;
47
int isVirtual = FALSE;
48
bool isInline = false;
49
int isIO = FALSE;
50
int isConst = FALSE;
51
bool isRegister = false;
52
bool isAuto = false;
53
bool isFuncBody;
54
bool isFuncPtr;
55
int missingArgumentName = FALSE;
56
int disableSubs;
57
int parsingParameterList = FALSE;
58
int unnamedCnt = 0;
59
int needParseFunction = 0;
60
int isStructDecl = FALSE;
61
int worstAlignment = 0;
62
char *stkname = 0;
63
std::string *classname;
64
bool isPrivate = true;
65
SYM *currentClass;
66
int mangledNames = FALSE;
67
 
68
/* variable for bit fields */
69
static int              bit_max;        // largest bitnumber
70
int bit_offset; /* the actual offset */
71
int      bit_width;     /* the actual width */
72
int bit_next;   /* offset for next variable */
73
 
74
int declbegin(int st);
75
void dodecl(int defclass);
76
SYM *ParseDeclarationSuffix(SYM *);
77
void declstruct(int ztype);
78
void structbody(TYP *tp, int ztype);
79
void ParseEnumDeclaration(TABLE *table);
80
void enumbody(TABLE *table);
81
extern int ParseClassDeclaration(int ztype);
82
extern ENODE *ArgumentList(ENODE *hidden,int*,int);
83
TYP *nameref2(std::string name, ENODE **node,int nt,bool alloc,TypeArray*, TABLE* tbl);
84
SYM *search2(std::string na,TABLE *tbl,int *typearray);
85
SYM *gsearch2(std::string na, __int16, int *typearray, bool exact);
86
extern ENODE *makesnode(int,std::string *,std::string *,__int64);
87
 
88
extern TYP *CopyType(TYP *src);
89
 
90
int     imax(int i, int j)
91
{       return (i > j) ? i : j;
92
}
93
 
94
 
95
char *my_strdup(char *s)
96
{
97
        char *p;
98
        int n = strlen(s);
99
        int m = sizeof(char);
100
        p = (char *)allocx(sizeof(char)*(n+1));
101
        memcpy(p,s,sizeof(char)*(n));
102
        p[n] = '\0';
103
        return p;
104
}
105
 
106
void Declaration::SetType(SYM *sp)
107
{
108
        if (head) {
109
                if (bit_width <= 0) {
110
                        sp->tp = head;
111
                }
112
                else {
113
                        if (head->IsFloatType()) {
114
                                sp->tp = head;
115
                                sp->tp->precision = bit_width;
116
                        }
117
                        else if (head->IsVectorType()) {
118
                                sp->tp = head;
119
                                sp->tp->numele = bit_width;
120
                        }
121
                        else {
122
                                sp->tp = TYP::Make(bt_bitfield,head->size);
123
                                //              *(sp->tp) = *head;
124
                                sp->tp->bit_width = bit_width;
125
                                sp->tp->bit_offset = bit_offset;
126
                        }
127
                }
128
        }
129
        else {
130
                sp->tp = TYP::Make(bt_long,sizeOfWord);
131
                sp->tp->lst.head = sp->GetIndex();
132
        }
133
}
134
 
135
// Ignore const
136
void Declaration::ParseConst()
137
{
138
        isConst = TRUE;
139
        NextToken();
140
}
141
 
142
void Declaration::ParseTypedef()
143
{
144
        isTypedef = TRUE;
145
        NextToken();
146
}
147
 
148
void Declaration::ParseNaked()
149
{
150
        isNocall = TRUE;
151
        head = (TYP *)TYP::Make(bt_oscall,2);
152
        tail = head;
153
        NextToken();
154
}
155
 
156
void Declaration::ParseVoid()
157
{
158
        head = (TYP *)TYP::Make(bt_void,0);
159
        tail = head;
160
        head->isVolatile = isVolatile;
161
        head->isIO = isIO;
162
        head->isConst = isConst;
163
        NextToken();
164
        if (lastst==kw_interrupt) {
165
                isInterrupt = TRUE;
166
                NextToken();
167
        }
168
        if (lastst==kw_nocall || lastst==kw_naked) {
169
                isNocall = TRUE;
170
                NextToken();
171
        }
172
        bit_max = 0;
173
}
174
 
175
void Declaration::ParseShort()
176
{
177
        bit_max = 32;
178
        NextToken();
179
        switch(lastst) {
180
        case kw_int:
181
                NextToken();
182
                if (isUnsigned) {
183
                        head = (TYP *)TYP::Make(bt_ushort,4);
184
                        tail = head;
185
                }
186
                else {
187
                        head = (TYP *)TYP::Make(bt_short,4);
188
                        tail = head;
189
                }
190
                break;
191
        default:
192
                if (isUnsigned) {
193
                        head = (TYP *)TYP::Make(bt_ushort,4);
194
                        tail = head;
195
                }
196
                else {
197
                        head = (TYP *)TYP::Make(bt_short,4);
198
                        tail = head;
199
                }
200
                break;
201
        }
202
        head->isUnsigned = isUnsigned;
203
        head->isVolatile = isVolatile;
204
        head->isIO = isIO;
205
        head->isConst = isConst;
206
        head->isShort = TRUE;
207
}
208
 
209
void Declaration::ParseLong()
210
{
211
        NextToken();
212
        if (lastst==kw_int) {
213
                bit_max = 64;
214
                NextToken();
215
        }
216
        else if (lastst==kw_float) {
217
                head = (TYP *)TYP::Make(bt_double,8);
218
                tail = head;
219
                bit_max = head->precision;
220
                NextToken();
221
        }
222
        else if (lastst==kw_double) {
223
                head = TYP::Copy(&stddouble);
224
                //head = (TYP *)TYP::Make(bt_quad,8);
225
                tail = head;
226
                bit_max = head->precision;
227
                NextToken();
228
        }
229
        else {
230
                if (isUnsigned) {
231
                        head =(TYP *)TYP::Make(bt_ulong,8);
232
                        tail = head;
233
                        bit_max = head->precision;
234
                }
235
                else {
236
                        head = (TYP *)TYP::Make(bt_long,8);
237
                        tail = head;
238
                        bit_max = head->precision;
239
                }
240
        }
241
        //NextToken();
242
        if (lastst==kw_task) {
243
                isTask = TRUE;
244
                NextToken();
245
                bit_max = 64;
246
        }
247
        if (lastst==kw_oscall) {
248
                isOscall = TRUE;
249
                NextToken();
250
                bit_max = 64;
251
        }
252
        else if (lastst==kw_nocall || lastst==kw_naked) {
253
                isNocall = TRUE;
254
                NextToken();
255
                bit_max = 64;
256
        }
257
        head->isUnsigned = isUnsigned;
258
        head->isVolatile = isVolatile;
259
        head->isIO = isIO;
260
        head->isConst = isConst;
261
}
262
 
263
void Declaration::ParseInt()
264
{
265
//printf("Enter ParseInt\r\n");
266
        if (isUnsigned) {
267
                head = TYP::Make(bt_ulong,8);
268
                tail = head;
269
        }
270
        else {
271
                head = TYP::Make(bt_long,8);
272
                tail = head;
273
        }
274
        bit_max = 64;
275
        if (head==nullptr)
276
                return;
277
        head->isUnsigned = isUnsigned;
278
        head->isVolatile = isVolatile;
279
        head->isIO = isIO;
280
        head->isConst = isConst;
281
        NextToken();
282
        if (lastst==kw_vector) {
283
                int btp = head->GetIndex();
284
                head = TYP::Make(bt_vector,512);
285
                head->numele = maxVL;
286
                head->btp = btp;
287
                tail = head;
288
                NextToken();
289
        }
290
        if (lastst==kw_task) {
291
                isTask = TRUE;
292
                NextToken();
293
        }
294
        if (lastst==kw_oscall) {
295
                isOscall = TRUE;
296
                NextToken();
297
        }
298
        else if (lastst==kw_nocall || lastst==kw_naked) {
299
                isNocall = TRUE;
300
                NextToken();
301
        }
302
//printf("Leave ParseInt\r\n");
303
}
304
 
305
void Declaration::ParseFloat()
306
{
307
        head = TYP::Copy(&stddouble);
308
        tail = head;
309
        head->isVolatile = isVolatile;
310
        head->isIO = isIO;
311
        head->isConst = isConst;
312
        NextToken();
313
        if (lastst==kw_vector) {
314
                int btp = head->GetIndex();
315
                head = TYP::Make(bt_vector,512);
316
                head->numele = maxVL;
317
                head->btp = btp;
318
                tail = head;
319
                NextToken();
320
        }
321
        bit_max = head->precision;
322
}
323
 
324
void Declaration::ParseDouble()
325
{
326
        head = (TYP *)TYP::Make(bt_double,8);
327
        tail = head;
328
        head->isVolatile = isVolatile;
329
        head->isIO = isIO;
330
        head->isConst = isConst;
331
        NextToken();
332
        if (lastst==kw_vector) {
333
                int btp = head->GetIndex();
334
                head = TYP::Make(bt_vector,512);
335
                head->numele = maxVL;
336
                head->btp = btp;
337
                tail = head;
338
                NextToken();
339
        }
340
        bit_max = head->precision;
341
}
342
 
343
void Declaration::ParseTriple()
344
{
345
        head = (TYP *)TYP::Make(bt_triple, 12);
346
        head->precision = 96;
347
        tail = head;
348
        head->isVolatile = isVolatile;
349
        head->isIO = isIO;
350
        head->isConst = isConst;
351
        NextToken();
352
        if (lastst == kw_vector) {
353
                int btp = head->GetIndex();
354
                head = TYP::Make(bt_vector, 512);
355
                head->numele = maxVL;
356
                head->btp = btp;
357
                tail = head;
358
                NextToken();
359
        }
360
        bit_max = head->precision;
361
}
362
 
363
void Declaration::ParseFloat128()
364
{
365
        head = (TYP *)TYP::Make(bt_quad, 16);
366
        tail = head;
367
        head->precision = 128;
368
        head->isVolatile = isVolatile;
369
        head->isIO = isIO;
370
        head->isConst = isConst;
371
        NextToken();
372
        if (lastst == kw_vector) {
373
                int btp = head->GetIndex();
374
                head = TYP::Make(bt_vector, 512);
375
                head->numele = maxVL;
376
                head->btp = btp;
377
                tail = head;
378
                NextToken();
379
        }
380
        bit_max = head->precision;
381
}
382
 
383
void Declaration::ParseVector()
384
{
385
        int btp;
386
 
387
        head = (TYP *)TYP::Make(bt_double,sizeOfFPD);
388
        tail = head;
389
        head->isVolatile = isVolatile;
390
        head->isIO = isIO;
391
        head->isConst = isConst;
392
        NextToken();
393
        btp = head->GetIndex();
394
        head = TYP::Make(bt_vector,512);
395
        head->numele = maxVL;
396
        head->btp = btp;
397
        tail = head;
398
        NextToken();
399
        bit_max = head->precision;
400
}
401
 
402
void Declaration::ParseVectorMask()
403
{
404
        head = (TYP *)TYP::Make(bt_vector_mask,sizeOfWord);
405
        tail = head;
406
        head->isVolatile = isVolatile;
407
        head->isIO = isIO;
408
        head->isConst = isConst;
409
        head->numele = maxVL;
410
        NextToken();
411
        bit_max = head->precision;
412
}
413
 
414
void Declaration::ParseInt32()
415
{
416
        if (isUnsigned) {
417
                head = (TYP *)TYP::Make(bt_ushort,4);
418
                tail = head;
419
        }
420
        else {
421
                head = (TYP *)TYP::Make(bt_short,4);
422
                tail = head;
423
        }
424
        bit_max = 32;
425
        NextToken();
426
        if( lastst == kw_int )
427
                NextToken();
428
        head->isUnsigned = isUnsigned;
429
        head->isVolatile = isVolatile;
430
        head->isIO = isIO;
431
        head->isConst = isConst;
432
        head->isShort = TRUE;
433
}
434
 
435
void Declaration::ParseInt64()
436
{
437
        if (isUnsigned) {
438
                head = (TYP *)TYP::Make(bt_ulong,8);
439
                tail = head;
440
        }
441
        else {
442
                head = (TYP *)TYP::Make(bt_long,8);
443
                tail = head;
444
        }
445
        bit_max = 64;
446
        NextToken();
447
        if( lastst == kw_int )
448
                NextToken();
449
        head->isUnsigned = isUnsigned;
450
        head->isVolatile = isVolatile;
451
        head->isIO = isIO;
452
        head->isConst = isConst;
453
        head->isShort = TRUE;
454
}
455
 
456
void Declaration::ParseInt16()
457
{
458
        if (isUnsigned) {
459
                head = (TYP *)TYP::Make(bt_uchar,2);
460
                tail = head;
461
        }
462
        else {
463
                head =(TYP *)TYP::Make(bt_char,2);
464
                tail = head;
465
        }
466
        head->isUnsigned = isUnsigned;
467
        head->isVolatile = isVolatile;
468
        head->isIO = isIO;
469
        head->isConst = isConst;
470
        NextToken();
471
        if (lastst==kw_oscall) {
472
                isOscall = TRUE;
473
                NextToken();
474
        }
475
        if (lastst==kw_nocall || lastst==kw_naked) {
476
                isNocall = TRUE;
477
                NextToken();
478
        }
479
        bit_max = 16;
480
}
481
 
482
void Declaration::ParseInt8()
483
{
484
        if (isUnsigned) {
485
                head = (TYP *)TYP::Make(bt_ubyte,1);
486
                tail = head;
487
        }
488
        else {
489
                head =(TYP *)TYP::Make(bt_byte,1);
490
                tail = head;
491
        }
492
        head->isUnsigned = isUnsigned;
493
        head->isVolatile = isVolatile;
494
        head->isIO = isIO;
495
        head->isConst = isConst;
496
        NextToken();
497
        if (lastst==kw_oscall) {
498
                isOscall = TRUE;
499
                NextToken();
500
        }
501
        if (lastst==kw_nocall || lastst==kw_naked) {
502
                isNocall = TRUE;
503
                NextToken();
504
        }
505
        bit_max = head->precision;
506
}
507
 
508
void Declaration::ParseByte()
509
{
510
        if (isUnsigned) {
511
                head = (TYP *)TYP::Make(bt_ubyte,1);
512
                tail = head;
513
        }
514
        else {
515
                head =(TYP *)TYP::Make(bt_byte,1);
516
                tail = head;
517
        }
518
        NextToken();
519
        head->isUnsigned = !isSigned;
520
        head->isVolatile = isVolatile;
521
        head->isIO = isIO;
522
        head->isConst = isConst;
523
        bit_max = head->precision;
524
}
525
 
526
SYM *Declaration::ParseId()
527
{
528
        SYM *sp;
529
 
530
  dfs.printf("<ParseId>%s",lastid);
531
        sp = tagtable.Find(lastid,false);//gsyms[0].Find(lastid);
532
        if (sp==nullptr)
533
                sp = gsyms[0].Find(lastid,false);
534
        if (sp) {
535
                dfs.printf("Actually found type.\r\n");
536
                if (sp->storage_class==sc_typedef || sp->storage_class==sc_type) {
537
                        NextToken();
538
                        head = tail = sp->tp;
539
                }
540
                else
541
                        head = tail = sp->tp;
542
//                                      head = tail = maketype(bt_long,4);
543
        }
544
        else {
545
                head = (TYP *)TYP::Make(bt_long,8);
546
                tail = head;
547
                bit_max = head->precision;
548
        }
549
        dfs.puts("</ParseId>");
550
        return (sp);
551
}
552
 
553
// Parse a specifier. This is the first part of a declaration.
554
// Returns:
555
// 0 usually, 1 if only a specifier is present
556
//
557
int Declaration::ParseSpecifier(TABLE *table)
558
{
559
        SYM *sp;
560
 
561
        dfs.printf("<ParseSpecifier>\n");
562
        isUnsigned = FALSE;
563
        isSigned = FALSE;
564
        isVolatile = FALSE;
565
        isVirtual = FALSE;
566
        isIO = FALSE;
567
        isConst = FALSE;
568
        dfs.printf("A");
569
        for (;;) {
570
                switch (lastst) {
571
 
572
                        case kw_const:          ParseConst();   break;
573
                        case kw_typedef:        ParseTypedef(); break;
574
                        case kw_nocall:
575
                        case kw_naked:          ParseNaked();   break;
576
 
577
                        case kw_oscall:
578
                                isOscall = TRUE;
579
                                head = tail = (TYP *)TYP::Make(bt_oscall,2);
580
                                NextToken();
581
                                goto lxit;
582
 
583
                        case kw_interrupt:
584
                                isInterrupt = TRUE;
585
                                head = (TYP *)TYP::Make(bt_interrupt,2);
586
                                tail = head;
587
                                NextToken();
588
                                if (lastst==openpa) {
589
                    NextToken();
590
                    if (lastst!=id)
591
                       error(ERR_IDEXPECT);
592
                    needpunc(closepa,49);
593
                    stkname = my_strdup(lastid);
594
                }
595
                                goto lxit;
596
 
597
      case kw_virtual:
598
        dfs.printf("virtual");
599
        isVirtual = TRUE;
600
        NextToken();
601
        break;
602
 
603
                        case kw_kernel:
604
                                isKernel = TRUE;
605
                                head =(TYP *) TYP::Make(bt_kernel,2);
606
                                tail = head;
607
                                NextToken();
608
                                goto lxit;
609
 
610
                        case kw_pascal:
611
                                isPascal = TRUE;
612
                                NextToken();
613
                                break;
614
 
615
                        case kw_inline:
616
                                isInline = TRUE;
617
                                NextToken();
618
                                break;
619
 
620
                        case kw_register:
621
                                isRegister = TRUE;
622
                                NextToken();
623
                                break;
624
 
625
                        // byte and char default to unsigned unless overridden using
626
                        // the 'signed' keyword
627
                        //
628
                        case kw_byte:   ParseByte(); goto lxit;
629
                        case kw_char:   ParseInt16(); goto lxit;
630
                        case kw_int8:   ParseInt8(); goto lxit;
631
                        case kw_int16:  ParseInt16(); goto lxit;
632
                        case kw_int32:  ParseInt32(); goto lxit;
633
                        case kw_int64:  ParseInt64(); goto lxit;
634
                        case kw_short:  ParseShort();   goto lxit;
635
                        case kw_long:   ParseLong();    goto lxit;      // long, long int
636
                        case kw_int:    ParseInt();             goto lxit;
637
 
638
            case kw_task:
639
                isTask = TRUE;
640
                NextToken();
641
                                break;
642
 
643
                        case kw_signed:
644
                                isSigned = TRUE;
645
                                NextToken();
646
                                break;
647
 
648
                        case kw_unsigned:
649
                                NextToken();
650
                                isUnsigned = TRUE;
651
                                break;
652
 
653
                        case kw_volatile:
654
                                NextToken();
655
                                if (lastst==kw_inout) {
656
                    NextToken();
657
                    isIO = TRUE;
658
                }
659
                                isVolatile = TRUE;
660
                                break;
661
 
662
                        case ellipsis:
663
                                head = (TYP *)TYP::Make(bt_ellipsis,4);
664
                                tail = head;
665
                                head->isVolatile = isVolatile;
666
                                head->isIO = isIO;
667
                                head->isConst = isConst;
668
                                NextToken();
669
                                bit_max = 32;
670
                                goto lxit;
671
 
672
                        case id:        sp = ParseId(); goto lxit;
673
 
674
                        case kw_float:  ParseFloat(); goto lxit;
675
                        case kw_double: ParseDouble(); goto lxit;
676
                        case kw_float128:       ParseFloat128(); goto lxit;
677
 
678
                        case kw_triple:
679
                                head = TYP::Copy(&stdtriple);
680
                                tail = head;
681
                                head->isVolatile = isVolatile;
682
                                head->isIO = isIO;
683
                                head->isConst = isConst;
684
                                NextToken();
685
                                bit_max = head->precision;
686
                                goto lxit;
687
 
688
                        case kw_vector: ParseVector(); goto lxit;
689
                        case kw_vector_mask: ParseVectorMask(); goto lxit;
690
 
691
                        case kw_void:   ParseVoid(); goto lxit;
692
 
693
                        case kw_enum:
694
                                NextToken();
695
                                ParseEnumDeclaration(table);
696
                                bit_max = 16;
697
                                goto lxit;
698
 
699
                        case kw_class:
700
                                ClassDeclaration::Parse(bt_class);
701
                                goto lxit;
702
 
703
                        case kw_struct:
704
                                NextToken();
705
                                if (StructDeclaration::Parse(bt_struct))
706
                                        return 1;
707
                                goto lxit;
708
 
709
                        case kw_union:
710
                                NextToken();
711
                                if (StructDeclaration::Parse(bt_union))
712
                                        return 1;
713
                                goto lxit;
714
 
715
      case kw_exception:
716
                                head = (TYP *)TYP::Make(bt_exception,8);
717
                                tail = head;
718
                                head->isVolatile = isVolatile;
719
                                head->isIO = isIO;
720
                                head->isConst = isConst;
721
                                NextToken();
722
                                bit_max = 64;
723
                                goto lxit;
724
 
725
                        default:
726
                                goto lxit;
727
                        }
728
        }
729
lxit:;
730
        dfs.printf("</ParseSpecifier>\n");
731
        return 0;
732
}
733
 
734
void Declaration::ParseDoubleColon(SYM *sp)
735
{
736
        SYM *sym;
737
        bool gotDouble = false;
738
 
739
        while (lastst==double_colon) {
740
                gotDouble = true;
741
                sym = tagtable.Find(lastid,false);
742
                if (sym)
743
                        sp->parent = sym->GetIndex();//gsearch(lastid);
744
                else {
745
                        sp->parent = 0;
746
                        break;
747
                }
748
                NextToken();
749
                if (lastst != id) {
750
                        error(ERR_IDEXPECT);
751
                        break;
752
                }
753
        }
754
        if (gotDouble)
755
            NextToken();
756
        currentClass = sp->GetParentPtr();
757
        if (sp->parent)
758
                dfs.printf("Setting parent:%s|\r\n",
759
        (char *)sp->GetParentPtr()->name->c_str());
760
}
761
 
762
void Declaration::ParseBitfieldSpec(bool isUnion)
763
{
764
        dfs.puts("<ParseBitfieldSpec>");
765
        NextToken();
766
        bit_width = (int)GetIntegerExpression((ENODE **)NULL);
767
        if (isUnion)
768
                bit_offset = 0;
769
        else
770
                bit_offset = bit_next;
771
        if (bit_width < 0 || bit_width > bit_max) {
772
                error(ERR_BITFIELD_WIDTH);
773
                bit_width = 1;
774
        }
775
        if (bit_width == 0 || bit_offset + bit_width > bit_max)
776
                bit_offset = 0;
777
        bit_next = bit_offset + bit_width;
778
        dfs.puts("</ParseBitfieldSpec>\n");
779
}
780
 
781
SYM *Declaration::ParsePrefixId()
782
{
783
        SYM *sp;
784
 
785
        dfs.puts("<ParsePrefixId>");
786
        if (declid) delete declid;
787
        declid = new std::string(lastid);
788
        dfs.printf("B|%s|",(char *)declid->c_str());
789
        sp = allocSYM();
790
        dfs.printf("C");
791
        if (funcdecl==1) {
792
                sp->fi = allocFunction(sp->id);
793
                sp->fi->sym = sp;
794
                if (nparms > 19)
795
                        error(ERR_TOOMANY_PARAMS);
796
                else {
797
                        names[nparms].str = *declid;
798
                        nparms++;
799
                }
800
        }
801
        dfs.printf("D");
802
        NextToken();
803
        ParseDoubleColon(sp);
804
        if (declid) delete declid;
805
        declid = new std::string(lastid);
806
        sp->SetName(*declid);
807
        dfs.printf("E");
808
        if (lastst == colon) {
809
                ParseBitfieldSpec(isUnion);
810
                goto lxit;      // no ParseDeclarationSuffix()
811
        }
812
        sp->SetName(*declid);
813
        sp = ParseSuffix(sp);
814
        lxit:
815
        dfs.puts("</ParsePrefixId>");
816
        return sp;
817
}
818
 
819
SYM *Declaration::ParsePrefixOpenpa(bool isUnion)
820
{
821
        TYP *temp1, *temp2, *temp3, *temp4;
822
        SYM *sp;
823
 
824
        dfs.puts("<ParsePrefixOpenpa>\n");
825
        NextToken();
826
        temp1 = head;
827
        temp2 = tail;
828
        head = tail = (TYP *)NULL;      // It might be a typecast following.
829
        // Do we have (getchar)()
830
        // This processing is difficult to do with a loop, so a recursive
831
        // call is made.
832
        sp = ParsePrefix(isUnion);
833
        needpunc(closepa,20);
834
        // Head could be NULL still if a type hasn't been found
835
        // eg. int (getchar)();
836
        if (head)
837
                isFuncPtr = head->type == bt_pointer;
838
        temp3 = head;
839
        temp4 = tail;
840
        head = temp1;
841
        tail = temp2;
842
        sp = ParseSuffix(sp);
843
        // (getchar)() returns temp4 = NULL
844
        if (temp4!=NULL) {
845
                temp4->btp = head->GetIndex();
846
                if(temp4->type == bt_pointer && temp4->val_flag != 0 && head != NULL)
847
                        temp4->size *= head->size;
848
                head = temp3;
849
        }
850
        dfs.puts("</ParsePrefixOpenpa>\n");
851
        return sp;
852
}
853
 
854
// There may be only a single identifier in the prefix. This identifier may
855
// contain a class spec or namespace spec.
856
 
857
SYM *Declaration::ParsePrefix(bool isUnion)
858
{
859
        TYP *temp1;
860
        SYM *sp;
861
 
862
        dfs.printf("<ParseDeclPrefix>(%d)\n",lastst);
863
 
864
        sp = nullptr;
865
j1:
866
        switch (lastst) {
867
 
868
        case kw_const:
869
                isConst = TRUE;
870
                NextToken();
871
                goto j1;
872
 
873
//              case ellipsis:
874
        case id:
875
                sp = ParsePrefixId();
876
                goto lxit;
877
 
878
        case star:
879
                bit_max = 64;
880
                dfs.putch('*');
881
                temp1 = TYP::Make(bt_pointer,sizeOfPtr);
882
                temp1->btp = head->GetIndex();
883
                head = temp1;
884
                if(tail == NULL)
885
                        tail = head;
886
                NextToken();
887
                if (lastst==closepa)
888
                        goto lxit;
889
                sp = ParsePrefix(isUnion);
890
                goto lxit;
891
 
892
        case openpa:
893
                sp = ParsePrefixOpenpa(isUnion);
894
                goto lxit;
895
 
896
        default:
897
                sp = ParseSuffix(sp);
898
                dfs.printf("Z");
899
                goto lxit;
900
        }
901
lxit:
902
        dfs.puts("</ParseDeclPrefix>\n");
903
        return sp;
904
}
905
 
906
 
907
// Take care of trailing [] in a declaration. These indicate array indicies.
908
 
909
void Declaration::ParseSuffixOpenbr()
910
{
911
        TYP *temp1;
912
        long sz2;
913
 
914
        NextToken();
915
        temp1 = (TYP *)TYP::Make(bt_pointer,sizeOfPtr);
916
        temp1->val_flag = 1;
917
        temp1->isArray = TRUE;
918
        temp1->btp = head->GetIndex();
919
        if(lastst == closebr) {
920
                temp1->size = 0;
921
                temp1->numele = 0;
922
                if (head)
923
                        temp1->dimen = head->dimen + 1;
924
                else
925
                        temp1->dimen = 1;
926
                NextToken();
927
        }
928
        else if(head != NULL) {
929
                sz2 = (int)GetIntegerExpression((ENODE **)NULL);
930
                temp1->size = sz2 * head->size;
931
                temp1->numele = sz2;
932
                temp1->dimen = head->dimen + 1;
933
                dfs.printf("Setting array size:%d\n", (int)temp1->size);
934
                temp1->alignment = head->alignment;
935
                needpunc(closebr,21);
936
        }
937
        else {
938
                sz2 = (int)GetIntegerExpression((ENODE **)NULL);
939
                temp1->size = sz2;
940
                temp1->numele = sz2;
941
                temp1->dimen = 1;
942
                needpunc(closebr,22);
943
        }
944
        head = temp1;
945
        if( tail == NULL)
946
                tail = head;
947
}
948
 
949
void Declaration::ParseFunctionAttribute(Function *sym)
950
{
951
        NextToken();
952
        needpunc(openpa,0);
953
        do {
954
                switch(lastst) {
955
                case kw_no_temps:
956
                        sym->UsesTemps = false;
957
                        NextToken();
958
                        break;
959
                /*
960
                case kw_no_parms:
961
                        sym->UsesStackParms = false;
962
                        NextToken();
963
                        break;
964
                */
965
                }
966
        } while (lastst==comma);
967
        needpunc(closepa,0);
968
}
969
 
970
 
971
// Take care of following open parenthesis (). These indicate a function
972
// call. There may or may not be following parameters. A following '{' is
973
// looked for and if found a flag is set to parse the function body.
974
 
975
void Declaration::ParseSuffixOpenpa(Function *sp)
976
{
977
        TYP *temp1;
978
        TYP *tempHead, *tempTail;
979
        int fd;
980
        std::string odecl;
981
        int isd;
982
        int nump = 0;
983
        int numa = 0;
984
        Function *cf;
985
 
986
        dfs.printf("<openpa>\n");
987
        dfs.printf("****************************\n");
988
        dfs.printf("****************************\n");
989
        dfs.printf("Function: %s\n", (char *)sp->sym->name->c_str());
990
        dfs.printf("****************************\n");
991
        dfs.printf("****************************\n");
992
        NextToken();
993
        sp->IsPascal = isPascal;
994
        sp->IsInline = isInline;
995
 
996
        // An asterik before the function name indicates a function pointer but only
997
        // if it's bracketed properly, otherwise it could be the return value that's
998
        // a pointer.
999
        //  isFuncPtr = head->type==bt_pointer;
1000
        temp1 =(TYP *) TYP::Make(bt_func,0/*isFuncPtr ? bt_func : bt_ifunc,0*/);
1001
        temp1->val_flag = 1;
1002
        dfs.printf("o ");
1003
        if (isFuncPtr) {
1004
                dfs.printf("Got function pointer in declarations.\n");
1005
                temp1->btp = head->btp;
1006
                head->btp = temp1->GetIndex();
1007
        }
1008
        else {
1009
                temp1->btp= head->GetIndex();
1010
                head = temp1;
1011
        }
1012
        dfs.printf("p ");
1013
        if (tail==NULL) {
1014
                if (temp1->GetBtp())
1015
                        tail = temp1->GetBtp();
1016
                else
1017
                        tail = temp1;
1018
        }
1019
        dfs.printf("q ");
1020
        needParseFunction = 1;
1021
        sp->params.Clear();
1022
        sp->sym->parent = currentClass->GetIndex();
1023
        if(lastst == closepa) {
1024
                NextToken();
1025
                while (lastst == kw_attribute)
1026
                        ParseFunctionAttribute(sp);
1027
          if(lastst == begin) {
1028
                  temp1->type = bt_ifunc;
1029
                  needParseFunction = 2;
1030
          }
1031
          else {
1032
                  if (lastst != semicolon) {
1033
                                goto j2;
1034
                        cf = currentFn;
1035
                        currentFn = sp;
1036
                        nump = 0;
1037
                        sp->BuildParameterList(&nump, &numa);
1038
                        currentFn = cf;
1039
                        if (lastst==begin) {
1040
                                temp1->type = bt_ifunc;
1041
                                currentFn = sp;
1042
                                sp->NumParms = nump;
1043
                                sp->numa = numa;
1044
                                needParseFunction = 2;
1045
                                goto j1;
1046
                        }
1047
                }
1048
              temp1->type = bt_func;
1049
                  needParseFunction = 0;
1050
                  dfs.printf("Set false\n");
1051
          }
1052
          currentFn = sp;
1053
          sp->NumParms = 0;
1054
          sp->numa = 0;
1055
j1: ;
1056
  }
1057
  else {
1058
j2:
1059
    dfs.printf("r");
1060
          currentFn = sp;
1061
    dfs.printf("s");
1062
    temp1->type = bt_func;
1063
        // Parse the parameter list for a function pointer passed as a
1064
        // parameter.
1065
        // Parse parameter list for a function pointer defined within
1066
        // a structure.
1067
        if (parsingParameterList || isStructDecl) {
1068
      dfs.printf("s ");
1069
                fd = funcdecl;
1070
                needParseFunction = FALSE;
1071
          dfs.printf("Set false\n");
1072
                if (declid)
1073
                        odecl = *declid;
1074
                else
1075
                        odecl = "";
1076
                tempHead = head;
1077
                tempTail = tail;
1078
                isd = isStructDecl;
1079
                //ParseParameterDeclarations(10);       // parse and discard
1080
                funcdecl = 10;
1081
  //                            SetType(sp);
1082
                sp->BuildParameterList(&nump, &numa);
1083
                needParseFunction = 0;
1084
          dfs.printf("Set false\n");
1085
  //                            sp->parms = sym;
1086
                sp->NumParms = nump;
1087
                isStructDecl = isd;
1088
                head = tempHead;
1089
                tail = tempTail;
1090
                if (declid) delete declid;
1091
                declid = new std::string(odecl);
1092
                funcdecl = fd;
1093
                // There may be more parameters in the list.
1094
                if (lastst==comma) {
1095
                        return;
1096
                }
1097
                needpunc(closepa,23);
1098
 
1099
                if (lastst==begin) {
1100
                  needParseFunction = 2;
1101
                  dfs.printf("Set true1\n");
1102
                        if (sp->params.GetHead() && sp->proto.GetHead()) {
1103
                          dfs.printf("Matching parameter types to prototype.\n");
1104
                          if (!sp->ParameterTypesMatch(sp))
1105
                             error(ERR_PARMLIST_MISMATCH);
1106
                  }
1107
                        temp1->type = bt_ifunc;
1108
                }
1109
                // If the declaration is ending in a semicolon then it was really
1110
                // a function prototype, so move the parameters to the prototype
1111
                // area.
1112
                else if (lastst==semicolon) {
1113
                        sp->params.CopyTo(&sp->proto);
1114
      }
1115
          else {
1116
                if (funcdecl > 0 && lastst==closepa)
1117
                        ;
1118
                else
1119
                        error(ERR_SYNTAX);
1120
          }
1121
      dfs.printf("Z\r\n");
1122
//                              if (isFuncPtr)
1123
//                                      temp1->type = bt_func;
1124
//                              if (lastst != begin)
1125
//                                      temp1->type = bt_func;
1126
//                              if (lastst==begin) {
1127
//                                      ParseFunction(sp);
1128
//                              }
1129
    }
1130
    dfs.printf("Y");
1131
          sp->PrintParameterTypes();
1132
    dfs.printf("X");
1133
  }
1134
  dfs.printf("</openpa>\n");
1135
}
1136
 
1137
 
1138
// Take care of the () or [] trailing part of a declaration.
1139
// There could be multiple sets of [] so a loop is formed to accomodate
1140
// this. There will be only a single set of () indicating parameters.
1141
 
1142
SYM *Declaration::ParseSuffix(SYM *sp)
1143
{
1144
        dfs.printf("<ParseDeclSuffix>\n");
1145
 
1146
        while(true) {
1147
                switch (lastst) {
1148
 
1149
                case openbr:
1150
                        ParseSuffixOpenbr();
1151
                        break;                // We want to loop back for more brackets
1152
 
1153
                case openpa:
1154
                        // The declaration doesn't have to have an identifier name; it could
1155
                        // just be a type chain. so sp incoming might be null. We need a place
1156
                        // to stuff the parameter / protoype list so we may as well create
1157
                        // the symbol here if it isn't yet defined.
1158
                        if (sp == nullptr) {
1159
                                sp = allocSYM();
1160
                                sp->fi = allocFunction(sp->id);
1161
                                sp->fi->sym = sp;
1162
                        }
1163
                        else if (sp->fi == nullptr) {
1164
                                sp->fi = allocFunction(sp->id);
1165
                                sp->fi->sym = sp;
1166
                        }
1167
                        ParseSuffixOpenpa(sp->fi);
1168
                        goto lxit;
1169
 
1170
                default:
1171
                        goto lxit;
1172
                }
1173
        }
1174
lxit:
1175
        dfs.printf("</ParseDeclSuffix>\n");
1176
        return (sp);
1177
}
1178
 
1179
void Declaration::AssignParameterName()
1180
{
1181
        char buf[20];
1182
 
1183
        sprintf_s(buf, sizeof(buf), "_p%d", nparms);
1184
        delete declid;
1185
        declid = new std::string(buf);
1186
        if (nparms > 19) {
1187
                error(ERR_TOOMANY_PARAMS);
1188
        }
1189
        else {
1190
                names[nparms].str = *declid;
1191
                nparms++;
1192
        }
1193
        missingArgumentName = TRUE;
1194
}
1195
 
1196
 
1197
int Declaration::GenStorage(int nbytes, int al, int ilc)
1198
{
1199
        static long old_nbytes;
1200
        int bcnt;
1201
 
1202
        if (bit_width > 0 && bit_offset > 0) {
1203
                // share the storage word with the previously defined field
1204
                nbytes = old_nbytes - ilc;
1205
        }
1206
        old_nbytes = ilc + nbytes;
1207
        dfs.printf("E");
1208
        if ((ilc + nbytes) % head->roundAlignment()) {
1209
                if (al == sc_thread)
1210
                        tseg();
1211
                else
1212
                        dseg();
1213
        }
1214
        bcnt = 0;
1215
        while ((ilc + nbytes) % head->roundAlignment()) {
1216
                ++nbytes;
1217
                bcnt++;
1218
        }
1219
        if (al != sc_member && al != sc_external && al != sc_auto) {
1220
                if (bcnt > 0)
1221
                        genstorage(bcnt);
1222
        }
1223
        return (nbytes);
1224
}
1225
 
1226
 
1227
/*
1228
 *      process declarations of the form:
1229
 *
1230
 *              <type>  <specifier>, <specifier>...;
1231
 *
1232
 *      leaves the declarations in the symbol table pointed to by
1233
 *      table and returns the number of bytes declared. al is the
1234
 *      allocation type to assign, ilc is the initial location
1235
 *      counter. if al is sc_member then no initialization will
1236
 *      be processed. ztype should be bt_struct for normal and in
1237
 *      structure ParseSpecifierarations and sc_union for in union ParseSpecifierarations.
1238
 */
1239
int Declaration::declare(SYM *parent,TABLE *table,int al,int ilc,int ztype)
1240
{
1241
        SYM *sp;
1242
        SYM *sp1;
1243
        Function *fn;
1244
        TYP *dhead, *tp1, *tp2;
1245
        ENODE *ep1, *ep2;
1246
        int op;
1247
        int fn_doneinit = 0;
1248
        bool flag;
1249
        int parentBytes = 0;
1250
        std::string name;
1251
 
1252
    int nbytes;
1253
 
1254
        dfs.printf("Enter declare()\r\n");
1255
        nbytes = 0;
1256
        dfs.printf("A");
1257
        classname = new std::string("");
1258
        sp1 = nullptr;
1259
        if (ParseSpecifier(table))
1260
                goto xit1;
1261
        dfs.printf("B");
1262
        dhead = head;
1263
        for(;;) {
1264
            if (declid) delete declid;
1265
                declid = nullptr;
1266
                dfs.printf("b");
1267
                bit_width = -1;
1268
                sp = ParsePrefix(ztype==bt_union);
1269
                if (declid==nullptr)
1270
                        declid = new std::string("");
1271
                // If a function declaration is taking place and just the type is
1272
                // specified without a parameter name, assign an internal compiler
1273
                // generated name.
1274
                if (funcdecl>0 && funcdecl != 10 && declid->length()==0)
1275
                        AssignParameterName();
1276
 
1277
                dfs.printf("C");
1278
                if( declid->length() > 0 || classname->length()!=0) {      // otherwise just struct tag...
1279
                        if (sp == nullptr) {
1280
                                sp = allocSYM();
1281
                        }
1282
                        SetType(sp);
1283
                        if (funcdecl > 0) {
1284
                                sp->fi = allocFunction(sp->id);
1285
                                sp->fi->sym = sp;
1286
                                sp->fi->IsPascal = isPascal;
1287
                                sp->fi->IsInline = isInline;
1288
                        }
1289
                        sp->IsRegister = isRegister;
1290
                        isRegister = false;
1291
                        sp->IsAuto = isAuto;
1292
                        sp->IsParameter = parsingParameterList > 0;
1293
                        if (sp->parent < 0)// was nullptr
1294
                                sp->parent = parent->GetIndex();
1295
                        if (al==sc_member)
1296
                                sp->IsPrivate = isPrivate;
1297
                        else
1298
                                sp->IsPrivate = false;
1299
                        //if (declid==nullptr)
1300
                        //      declid = new std::string("");
1301
                        sp->SetName(classname->length() > 0 ? *classname : *declid);
1302
                        dfs.printf("D");
1303
                        if (classname) delete classname;
1304
                        classname = new std::string("");
1305
                        if (sp->tp->type == bt_func || sp->tp->type == bt_ifunc)
1306
                                sp->fi->IsVirtual = isVirtual;
1307
                        sp->storage_class = al;
1308
                        sp->isConst = isConst;
1309
                        if (isConst)
1310
                                sp->tp->isConst = TRUE;
1311
                        if (al != sc_member) {
1312
                                //                                                      sp->isTypedef = isTypedef;
1313
                                if (isTypedef)
1314
                                        sp->storage_class = sc_typedef;
1315
                                isTypedef = FALSE;
1316
                        }
1317
 
1318
                        nbytes = GenStorage(nbytes, al, ilc);
1319
/*
1320
      dfs.printf("F");
1321
                  if (sp->parent) {
1322
        dfs.printf("f:%d",sp->parent);
1323
        if (sp->GetParentPtr()->tp==nullptr) {
1324
          dfs.printf("f:%d",sp->parent);
1325
          dfs.printf("null type pointer.\n");
1326
          parentBytes = 0;
1327
        }
1328
        else {
1329
                            parentBytes = sp->GetParentPtr()->tp->size;
1330
                            dfs.printf("ParentBytes=%d\n",parentBytes);
1331
                    }
1332
                  }
1333
                  else
1334
                          parentBytes = 0;
1335
*/
1336
                        dfs.printf("G");
1337
                        if ((sp->tp->type == bt_func) && sp->storage_class == sc_global)
1338
                                sp->storage_class = sc_external;
1339
 
1340
                        // Set the (struct member) storage offset.
1341
                        sp->SetStorageOffset(head, nbytes, al, ilc, ztype);
1342
 
1343
                        // Increase the storage allocation by the type size.
1344
                        nbytes = sp->AdjustNbytes(nbytes, al, ztype);
1345
 
1346
                        dfs.printf("H");
1347
                        // For a class declaration there may not be any variables declared as
1348
                        // part of the declaration. In that case the symbol name is an empty
1349
                        // string. There's nothing to insert in the symbol table.
1350
                        name = *sp->name;
1351
                        if (sp->name->length() > 0) {
1352
                                //dfs.printf("Table:%p, sp:%p Fn:%p\r\n", table, sp, currentFn);
1353
                                if (sp->parent) {
1354
                                        int nn;
1355
                                        // If a function body is being processed we want to look for
1356
                                        // symbols by rising through the hierarchy. Otherwise we want a
1357
                                        // lower level defined symbol to shadow one at a hight level.
1358
                                        if (isFuncBody) {
1359
                                                nn = sp->GetParentPtr()->tp->lst.FindRising(*sp->name);
1360
                                                if (nn)
1361
                                                        sp1 = sp->FindRisingMatch(false);
1362
                                        }
1363
                                        else {
1364
                                                nn = sp->GetParentPtr()->tp->lst.Find(*sp->name);
1365
                                                if (nn) {
1366
                                                        sp1 = sp->fi->FindExactMatch(TABLE::matchno)->sym;
1367
                                                }
1368
                                        }
1369
                                }
1370
                                else
1371
                                        sp1 = table->Find(*sp->name,false);
1372
 
1373
                                dfs.printf("h");
1374
                                if (sp->tp) {
1375
                                        dfs.printf("h1");
1376
                                        if (sp->tp->type == bt_ifunc || sp->tp->type==bt_func) {
1377
                                                dfs.printf("h2");
1378
                                                fn = sp->fi->FindExactMatch(TABLE::matchno);
1379
                                                if (fn)
1380
                                                        sp1 = fn->sym;
1381
                                                else
1382
                                                        sp1 = nullptr;
1383
                                                dfs.printf("i");
1384
                                        }
1385
                                }
1386
                                else {
1387
  dfs.printf("j");
1388
                                        if (TABLE::matchno)
1389
                                                sp1 = TABLE::match[TABLE::matchno-1];
1390
                                        else
1391
                                                sp1 = nullptr;
1392
                                }
1393
  dfs.printf("k");
1394
                                flag = false;
1395
                                if (sp1) {
1396
                                        if (sp1->tp) {
1397
  dfs.printf("l");
1398
                                                flag = sp1->tp->type == bt_func;
1399
                                        }
1400
                                }
1401
  dfs.printf("I");
1402
                        if (sp->tp->type == bt_ifunc && flag)
1403
                        {
1404
  dfs.printf("Ia");
1405
                                dfs.printf("bt_ifunc\r\n");
1406
                                sp1->SetType(sp->tp);
1407
                                sp1->storage_class = sp->storage_class;
1408
                          sp1->value.i = sp->value.i;
1409
                          if (!sp1->fi) {
1410
                                  sp1->fi = allocFunction(sp1->id);
1411
                                  sp1->fi->sym = sp1;
1412
                          }
1413
                  sp1->fi->IsPascal = sp->fi->IsPascal;
1414
                                sp1->fi->IsPrototype = sp->fi->IsPrototype;
1415
                                sp1->fi->IsVirtual = sp->fi->IsVirtual;
1416
                                sp1->parent = sp->parent;
1417
                                sp1->fi->params = sp->fi->params;
1418
                                sp1->fi->proto = sp->fi->proto;
1419
                                sp1->lsyms = sp->lsyms;
1420
                                sp = sp1;
1421
              }
1422
                        else {
1423
  dfs.printf("Ib");
1424
                                // Here the symbol wasn't found in the table.
1425
                                if (sp1 == nullptr) {
1426
  dfs.printf("Ic");
1427
            if ((sp->tp->type==bt_class)
1428
               && (sp->storage_class == sc_type || sp->storage_class==sc_typedef))
1429
              ; // Do nothing. The class was already entered in the tag table.
1430
            else if ((sp->tp->type == bt_struct || sp->tp->type==bt_union)
1431
               && (sp->storage_class == sc_type && sp->storage_class!=sc_typedef)
1432
               && sp->tp->sname->length() > 0)
1433
              ; // If there was no struct tag and this is a typedef, then it
1434
                // still needs to be inserted into the table.
1435
            else {
1436
              dfs.printf("insert type: %d\n", sp->tp->type);
1437
                                          dfs.printf("***Inserting:%s into %p\n",(char *)sp->name->c_str(), (char *) table);
1438
                                           // Need to know the type before a name can be generated.
1439
                                          if (sp->tp->type == bt_func || sp->tp->type == bt_ifunc)
1440
                                                sp->mangledName = sp->BuildSignature(!sp->fi->IsPrototype);
1441
                                          if (sp->parent && ((sp->tp->type==bt_func || sp->tp->type==bt_ifunc)
1442
                                          || (sp->tp->type==bt_pointer && (sp->tp->GetBtp()->type==bt_func || sp->tp->GetBtp()->type==bt_ifunc))))
1443
              {
1444
                                                  sp->fi->InsertMethod();
1445
                              }
1446
                              else {
1447
                table->insert(sp);
1448
                                          }
1449
                                  }
1450
                                }
1451
                        }
1452
  dfs.printf("J");
1453
                        if (needParseFunction) {
1454
                                needParseFunction = FALSE;
1455
                                fn_doneinit = sp->fi->Parse();
1456
                                if (sp->tp->type != bt_pointer)
1457
                                        return (nbytes);
1458
                        }
1459
     //         if(sp->tp->type == bt_ifunc) { /* function body follows */
1460
     //             ParseFunction(sp);
1461
     //             return nbytes;
1462
     //         }
1463
  dfs.printf("K");
1464
              if( (al == sc_global || al == sc_static || al==sc_thread) && !fn_doneinit &&
1465
                      sp->tp->type != bt_func && sp->tp->type != bt_ifunc && sp->storage_class!=sc_typedef)
1466
                      doinit(sp);
1467
          }
1468
      }
1469
 
1470
                if (funcdecl>0) {
1471
                        if (lastst==comma || lastst==semicolon) {
1472
                                break;
1473
                        }
1474
                        if (lastst==closepa) {
1475
                                goto xit1;
1476
                        }
1477
                }
1478
                else if (catchdecl==TRUE) {
1479
                        if (lastst==closepa)
1480
                                goto xit1;
1481
                }
1482
 
1483
                // If semi-colon is encountered we are at the end of the declaration.
1484
                else if (lastst == semicolon) {
1485
                        if (sp) {
1486
                                if (sp->tp) {
1487
                                        if (sp->tp->type==bt_class && (sp->storage_class != sc_type
1488
                                                && sp->storage_class != sc_typedef)) {
1489
                                                int nn;
1490
                                                nn = sp->tp->lst.FindRising(*sp->tp->sname);
1491
                                                if (nn > 0) {
1492
                                                        if (sp1) {
1493
                                                                ENODE *ep1,*ep2;
1494
                                                                ep1 = nullptr;
1495
                                                                // Build an expression that references the ctor.
1496
                                                                tp1 = nameref2(*sp->tp->sname,&ep1,TRUE,false,nullptr,nullptr);
1497
                                                                // Create a function call node for the ctor.
1498
                                                                if (tp1!=nullptr) {
1499
                                                                        // Make an expresison that references the var name as the
1500
                                                                        // argument to the ctor.
1501
                                                                        ep2 = makesnode(en_nacon,sp->name,sp->mangledName,sp->value.i);
1502
                                                                        ep1 = makenode(en_fcall, ep1, ep2);
1503
                                                                }
1504
                                                                sp->initexp = ep1;
1505
                                                        }
1506
                                                }
1507
                /*
1508
                                                // First see if there is a ctor. If there are no ctors there's
1509
                                                // nothing to do.
1510
                                                memset(typearray,0,sizeof(typearray));
1511
                                                sp1 = search2(sp->tp->sname,&sp->tp->lst,typearray);
1512
                                                if (sp1) {
1513
                                                        // Build an expression that references the ctor.
1514
                                                        tp1 = nameref2(sp->tp->sname,&ep1,TRUE,false,typearray);
1515
                                                        // Create a function call node for the ctor.
1516
                                                        if (tp1!=nullptr) {
1517
                                                                memcpy(typearray,GetParameterTypes(sp),sizeof(typearray));
1518
                                                                ep1 = makenode(en_fcall, ep1, nullptr);
1519
                                                        }
1520
                                                        sp->initexp = ep1;
1521
                                                }
1522
                */
1523
                                        }
1524
                                }
1525
                        }
1526
                        break;  // semicolon
1527
                }
1528
 
1529
                // Handle an assignment
1530
                else if (lastst == assign) {
1531
                        tp1 = nameref(&ep1,TRUE);
1532
            op = en_assign;
1533
//            NextToken();
1534
            tp2 = asnop(&ep2);
1535
            if( tp2 == 0 || !IsLValue(ep1) )
1536
                error(ERR_LVALUE);
1537
            else {
1538
                tp1 = forcefit(&ep1,tp1,&ep2,tp2,false);
1539
                ep1 = makenode(op,ep1,ep2);
1540
            }
1541
                        sp->initexp = ep1;
1542
                        if (lastst==semicolon)
1543
                                break;
1544
                }
1545
 
1546
                // See if there is a list of variable declarations
1547
        needpunc(comma,24);
1548
        if(declbegin(lastst) == 0)
1549
            break;
1550
        head = dhead;
1551
    }
1552
    NextToken();
1553
xit1:
1554
//      printf("Leave declare()\r\n");
1555
    return (nbytes);
1556
}
1557
 
1558
int declbegin(int st)
1559
{
1560
        return st == star || st == id || st == openpa || st == openbr;
1561
}
1562
 
1563
void GlobalDeclaration::Parse()
1564
{
1565
        dfs.puts("<ParseGlobalDecl>\n");
1566
        for(;;) {
1567
                lc_auto = 0;
1568
                bool notVal = false;
1569
                isFuncPtr = false;
1570
                isPascal = FALSE;
1571
                isInline = false;
1572
                currentClass = nullptr;
1573
                currentFn = nullptr;
1574
                currentStmt = nullptr;
1575
                isFuncBody = false;
1576
                worstAlignment = 0;
1577
                funcdecl = 0;
1578
 
1579
                switch(lastst) {
1580
                case kw_pascal:
1581
                  NextToken();
1582
                  isPascal = TRUE;
1583
                  break;
1584
                case kw_inline:
1585
                  NextToken();
1586
                  isInline = true;
1587
                  break;
1588
                case ellipsis:
1589
                case id:
1590
        case kw_kernel:
1591
                case kw_interrupt:
1592
        case kw_task:
1593
                case kw_cdecl:
1594
                case kw_naked:
1595
                case kw_nocall:
1596
                case kw_oscall:
1597
                case kw_typedef:
1598
    case kw_virtual:
1599
                case kw_volatile: case kw_const:
1600
        case kw_exception:
1601
                case kw_int8: case kw_int16: case kw_int32: case kw_int64: case kw_int40: case kw_int80:
1602
                case kw_byte: case kw_char: case kw_int: case kw_short: case kw_unsigned: case kw_signed:
1603
        case kw_long: case kw_struct: case kw_union: case kw_class:
1604
        case kw_enum: case kw_void:
1605
        case kw_float: case kw_double: case kw_float128:
1606
                case kw_vector: case kw_vector_mask:
1607
                lc_static += declare(NULL,&gsyms[0],sc_global,lc_static,bt_struct);
1608
                                isInline = false;
1609
                                break;
1610
        case kw_thread:
1611
                                NextToken();
1612
                lc_thread += declare(NULL,&gsyms[0],sc_thread,lc_thread,bt_struct);
1613
                                isInline = false;
1614
                                break;
1615
                case kw_register:
1616
                                NextToken();
1617
                error(ERR_ILLCLASS);
1618
                lc_static += declare(NULL,&gsyms[0],sc_global,lc_static,bt_struct);
1619
                                isInline = false;
1620
                                break;
1621
                case kw_private:
1622
        case kw_static:
1623
                NextToken();
1624
                                lc_static += declare(NULL,&gsyms[0],sc_static,lc_static,bt_struct);
1625
                                isInline = false;
1626
                break;
1627
    case kw_extern:
1628
        NextToken();
1629
                                if (lastst==kw_pascal) {
1630
                                        isPascal = TRUE;
1631
                                        NextToken();
1632
                                }
1633
                                if (lastst==kw_kernel) {
1634
                                        isKernel = TRUE;
1635
                                        NextToken();
1636
                                }
1637
                                else if (lastst==kw_oscall || lastst==kw_interrupt || lastst==kw_nocall || lastst==kw_naked)
1638
                                        NextToken();
1639
          ++global_flag;
1640
          declare(NULL,&gsyms[0],sc_external,0,bt_struct);
1641
          isInline = false;
1642
          --global_flag;
1643
          break;
1644
 
1645
    case kw_not:
1646
      NextToken();
1647
      notVal = !notVal;
1648
      break;
1649
 
1650
    case kw_using:
1651
      NextToken();
1652
      if (lastst==id) {
1653
        if (strcmp(lastid,"_name")==0) {
1654
          NextToken();
1655
          if (lastst==id) {
1656
            if (strcmp(lastid, "_mangler")==0) {
1657
              NextToken();
1658
              mangledNames = !notVal;
1659
            }
1660
          }
1661
        }
1662
        else if (strcmp(lastid,"_real")==0) {
1663
          NextToken();
1664
          if (lastst==id) {
1665
            if (strcmp(lastid,"_names")==0) {
1666
              NextToken();
1667
              mangledNames = notVal;
1668
            }
1669
          }
1670
        }
1671
      }
1672
          else if (lastst==kw_short) {
1673
                  NextToken();
1674
                  if (lastst==id) {
1675
                          if (strcmp(lastid,"_pointers")==0)
1676
                                  sizeOfPtr = 4;
1677
                  }
1678
          }
1679
          else if (lastst==kw_long) {
1680
                  NextToken();
1681
                  if (lastst==id) {
1682
                          if (strcmp(lastid,"_pointers")==0)
1683
                                  sizeOfPtr = 8;
1684
                  }
1685
          }
1686
      break;
1687
 
1688
    default:
1689
      goto xit;
1690
                }
1691
        }
1692
xit:
1693
        dfs.puts("</ParseGlobalDecl>\n");
1694
        ;
1695
}
1696
 
1697
void AutoDeclaration::Parse(SYM *parent, TABLE *ssyms)
1698
{
1699
        SYM *sp;
1700
 
1701
//      printf("Enter ParseAutoDecls\r\n");
1702
    for(;;) {
1703
                funcdecl = 0;
1704
                isFuncPtr = false;
1705
                worstAlignment = 0;
1706
                switch(lastst) {
1707
                case kw_cdecl:
1708
    case kw_kernel:
1709
                case kw_interrupt:
1710
                case kw_naked:
1711
                case kw_nocall:
1712
                case kw_oscall:
1713
                case kw_pascal:
1714
                case kw_typedef:
1715
                error(ERR_ILLCLASS);
1716
                    lc_auto += declare(parent,ssyms,sc_auto,lc_auto,bt_struct);
1717
                                break;
1718
                case ellipsis:
1719
                case id: //return;
1720
        dfs.printf("Found %s\n", lastid);
1721
                                sp = tagtable.Find(lastid,false);
1722
                                if (sp)
1723
                                   dfs.printf("Found in tagtable");
1724
                                if (sp==nullptr)
1725
                                        sp = gsyms[0].Find(lastid,false);
1726
                                if (sp) {
1727
                                  dfs.printf("sp okay sc=%d\n", sp->storage_class);
1728
                                        if (sp->storage_class==sc_typedef || sp->storage_class==sc_type) {
1729
                                          dfs.printf("Declaring var of type\n");
1730
                                    lc_auto += declare(parent,ssyms,sc_auto,lc_auto,bt_struct);
1731
                                                break;
1732
                                        }
1733
                                }
1734
                                goto xit;
1735
        case kw_register:
1736
                NextToken();
1737
        case kw_exception:
1738
                case kw_volatile: case kw_const:
1739
                case kw_int8: case kw_int16: case kw_int32: case kw_int64: case kw_int40: case kw_int80:
1740
                case kw_byte: case kw_char: case kw_int: case kw_short: case kw_unsigned: case kw_signed:
1741
        case kw_long: case kw_struct: case kw_union: case kw_class:
1742
        case kw_enum: case kw_void:
1743
        case kw_float: case kw_double: case kw_float128:
1744
                case kw_vector: case kw_vector_mask:
1745
            lc_auto += declare(parent,ssyms,sc_auto,lc_auto,bt_struct);
1746
            break;
1747
        case kw_thread:
1748
                NextToken();
1749
                                lc_thread += declare(parent,ssyms,sc_thread,lc_thread,bt_struct);
1750
                                break;
1751
        case kw_static:
1752
                NextToken();
1753
                                lc_static += declare(parent,ssyms,sc_static,lc_static,bt_struct);
1754
                                break;
1755
        case kw_extern:
1756
                NextToken();
1757
                                if (lastst==kw_oscall || lastst==kw_interrupt || lastst == kw_nocall || lastst==kw_naked || lastst==kw_kernel)
1758
                                        NextToken();
1759
                ++global_flag;
1760
                declare(NULL,&gsyms[0],sc_external,0,bt_struct);
1761
                --global_flag;
1762
                break;
1763
        default:
1764
                goto xit;
1765
                }
1766
        }
1767
xit:
1768
        ;
1769
//      printf("Leave ParseAutoDecls\r\n");
1770
}
1771
 
1772
int ParameterDeclaration::Parse(int fd)
1773
{
1774
        int ofd;
1775
  int opascal;
1776
 
1777
        dfs.puts("<ParseParmDecls>\n");
1778
        ofd = funcdecl;
1779
        opascal = isPascal;
1780
        isPascal = FALSE;
1781
        funcdecl = fd;
1782
        parsingParameterList++;
1783
        nparms = 0;
1784
        for(;;) {
1785
                worstAlignment = 0;
1786
                isFuncPtr = false;
1787
                isAuto = false;
1788
                isRegister = false;
1789
                missingArgumentName = FALSE;
1790
                dfs.printf("A(%d)",lastst);
1791
j1:
1792
                switch(lastst) {
1793
                case kw_auto:
1794
                        NextToken();
1795
                        isAuto = true;
1796
                        goto j1;
1797
                case kw_pascal:
1798
                        NextToken();
1799
                  isPascal = TRUE;
1800
                  goto j1;
1801
                case kw_cdecl:
1802
    case kw_kernel:
1803
                case kw_interrupt:
1804
                case kw_naked:
1805
                case kw_nocall:
1806
                case kw_oscall:
1807
                case kw_typedef:
1808
dfs.printf("B");
1809
      error(ERR_ILLCLASS);
1810
      declare(NULL,&currentFn->params,sc_auto,0,bt_struct);
1811
                                isAuto = false;
1812
                        break;
1813
                case ellipsis:
1814
                case id:
1815
                case kw_volatile: case kw_const:
1816
        case kw_exception:
1817
                case kw_int8: case kw_int16: case kw_int32: case kw_int64: case kw_int40: case kw_int80:
1818
                case kw_byte: case kw_char: case kw_int: case kw_short: case kw_unsigned: case kw_signed:
1819
    case kw_long: case kw_struct: case kw_union: case kw_class:
1820
    case kw_enum: case kw_void:
1821
        case kw_float: case kw_double: case kw_float128:
1822
        case kw_vector: case kw_vector_mask:
1823
dfs.printf("C");
1824
    declare(NULL,&currentFn->params,sc_auto,0,bt_struct);
1825
                                isAuto = false;
1826
                    break;
1827
        case kw_thread:
1828
                NextToken();
1829
                error(ERR_ILLCLASS);
1830
                                lc_thread += declare(NULL,&gsyms[0],sc_thread,lc_thread,bt_struct);
1831
                                isAuto = false;
1832
                                break;
1833
        case kw_static:
1834
                NextToken();
1835
                error(ERR_ILLCLASS);
1836
                                lc_static += declare(NULL,&gsyms[0],sc_static,lc_static,bt_struct);
1837
                                isAuto = false;
1838
                                break;
1839
        case kw_extern:
1840
dfs.printf("D");
1841
                NextToken();
1842
                error(ERR_ILLCLASS);
1843
                                if (lastst==kw_oscall || lastst==kw_interrupt || lastst == kw_nocall || lastst==kw_naked || lastst==kw_kernel)
1844
                                        NextToken();
1845
                ++global_flag;
1846
                declare(NULL,&gsyms[0],sc_external,0,bt_struct);
1847
                                isAuto = false;
1848
                --global_flag;
1849
                break;
1850
                case kw_register:
1851
                                isRegister = true;
1852
                                NextToken();
1853
                                goto j1;
1854
        default:
1855
                                goto xit;
1856
                }
1857
dfs.printf("E");
1858
        }
1859
xit:
1860
        parsingParameterList--;
1861
        funcdecl = ofd;
1862
        isPascal = opascal;
1863
        dfs.printf("</ParseParmDecls>\n");
1864
        return nparms;
1865
}
1866
 
1867
GlobalDeclaration *GlobalDeclaration::Make()
1868
{
1869
  GlobalDeclaration *p = (GlobalDeclaration *)allocx(sizeof(GlobalDeclaration));
1870
  return p;
1871
}
1872
 
1873
void compile()
1874
{
1875
}
1876
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.