-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathdecl.c
878 lines (752 loc) · 25.8 KB
/
decl.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
#include "defs.h"
#include "data.h"
#include "expr.h"
#include "gen.h"
#include "misc.h"
#include "opt.h"
#include "parse.h"
#include "stmt.h"
#include "sym.h"
#include "target.h"
#include "tree.h"
#include "types.h"
// Parsing of declarations
// Copyright (c) 2019 Warren Toomey, GPL3
static struct symtable *composite_declaration(int type);
static int typedef_declaration(struct symtable **ctype);
static int type_of_typedef(char *name, struct symtable **ctype);
static void enum_declaration(void);
int declaration_list(struct symtable **ctype, int class, int et1, int et2, struct ASTnode **gluetree);
// Parse the current token and return a primitive type enum value,
// a pointer to any composite type and possibly modify
// the class of the type.
int parse_type(struct symtable **ctype, int *class) {
int type = 0, exstatic = 1;
*ctype=NULL;
// See if the visibility class has been changed to extern or static
while (exstatic) {
switch (Token.token) {
case T_EXTERN:
if (*class == V_STATIC)
fatal("Illegal to have extern and static at the same time");
*class = V_EXTERN;
scan(&Token);
break;
case T_STATIC:
if (*class == V_LOCAL)
fatal("Compiler doesn't support static local declarations");
if (*class == V_EXTERN)
fatal("Illegal to have extern and static at the same time");
*class = V_STATIC;
scan(&Token);
break;
default:
exstatic = 0;
}
}
// Now work on the actual type keyword
switch (Token.token) {
case T_VOID:
type = P_VOID;
scan(&Token);
break;
case T_CHAR:
type = P_CHAR;
scan(&Token);
break;
case T_INT:
type = P_INT;
scan(&Token);
break;
case T_LONG:
type = P_LONG;
scan(&Token);
break;
// For the following, if we have a ';' after the
// parsing then there is no type, so return -1.
// Example: struct x {int y; int z};
case T_STRUCT:
type = P_STRUCT;
*ctype = composite_declaration(P_STRUCT);
if (Token.token == T_SEMI)
type = -1;
break;
case T_UNION:
type = P_UNION;
*ctype = composite_declaration(P_UNION);
if (Token.token == T_SEMI)
type = -1;
break;
case T_ENUM:
type = P_INT; // Enums are really ints
enum_declaration();
if (Token.token == T_SEMI)
type = -1;
break;
case T_TYPEDEF:
type = typedef_declaration(ctype);
if (Token.token == T_SEMI)
type = -1;
break;
case T_IDENT:
type = type_of_typedef(Text, ctype);
break;
default:
fatals("Illegal type, token", Tstring[Token.token]);
}
return (type);
}
// Given a type parsed by parse_type(), scan in any following
// '*' tokens and return the new type
int parse_stars(int type) {
while (1) {
if (Token.token != T_STAR)
break;
type = pointer_to(type);
scan(&Token);
}
return (type);
}
// Parse a type which appears inside a cast
int parse_cast(struct symtable **ctype) {
int type = 0, class = 0;
// Get the type inside the parentheses
type = parse_stars(parse_type(ctype, &class));
// Do some error checking. I'm sure more can be done
if (type == P_STRUCT || type == P_UNION || type == P_VOID)
fatal("Cannot cast to a struct, union or void type");
return (type);
}
// Given a type, parse an expression of literals and ensure
// that the type of this expression matches the given type.
// Parse any type cast that precedes the expression.
// If an integer literal, return this value.
// If a string literal, return the label number of the string.
int parse_literal(int type) {
struct ASTnode *tree;
struct symtable *sym;
// Parse the expression and optimise the resulting AST tree
tree = optimise(binexpr(0));
// If there's a cast, get the child and
// mark it as having the type from the cast
if (tree->op == A_CAST) {
tree->left->type = tree->type;
tree = tree->left;
}
// The tree must now have an integer or string literal
if (tree->op != A_INTLIT && tree->op != A_STRLIT)
fatal("Cannot initialise globals with a general expression");
// Deal with pointer to literals
if (ptrtype(type)) {
// If the type is char * and we have a string literal
if (type == pointer_to(P_CHAR) && tree->op == A_STRLIT) {
// Add it to the string literal symbol
// table and return the symbol's id
sym= addglob(tree->name, type, NULL, S_STRLIT, V_GLOBAL, 0, 0);
return (sym->id);
}
// We have a zero int literal, so that's a NULL
if (tree->op == A_INTLIT && tree->a_intvalue == 0)
return (0);
}
// We only get here with an integer literal.
// If the tree is an A_INTLIT and the left type is P_CHAR,
// and the INTLIT is in the range 0 to 255, change the trees's
// type to PCHAR to ensure we can do the assignment
if ((tree->op == A_INTLIT) && (type == P_CHAR) &&
(tree->a_intvalue >= 0) && (tree->a_intvalue < 256))
tree->type = P_CHAR;
// Check that the input type is an integer type
// and is wide enough to hold the literal value
if (inttype(type) && typesize(type, NULL) >= typesize(tree->type, NULL))
return (tree->a_intvalue);
fatal("Type mismatch: literal vs. variable");
return (0); // Keep -Wall happy
}
// Given a pointer to a symbol that may already exist
// return true if this symbol doesn't exist. We use
// this function to convert externs into globals
static int is_new_symbol(struct symtable *sym, int class,
int type, struct symtable *ctype) {
// There is no existing symbol, thus is new
if (sym == NULL)
return (1);
// global versus extern: if they match that it's not new
// and we can convert the class to global
if ((sym->class == V_GLOBAL && class == V_EXTERN)
|| (sym->class == V_EXTERN && class == V_GLOBAL)) {
// If the types don't match, there's a problem
if (type != sym->type)
fatals("Type mismatch between global/extern", sym->name);
// Struct/unions, also compare the ctype
if (type >= P_STRUCT && ctype != sym->ctype)
fatals("Type mismatch between global/extern", sym->name);
// If we get to here, the types match, so mark the symbol
// as global
sym->class = V_GLOBAL;
// Return that symbol is not new
return (0);
}
// It must be a duplicate symbol if we get here
fatals("Duplicate global variable declaration", sym->name);
return (-1); // Keep -Wall happy
}
// Given the type, name and class of a scalar variable,
// parse any initialisation value and allocate storage for it.
// Return the variable's symbol table entry.
static struct symtable *scalar_declaration(char *varname, int type,
struct symtable *ctype,
int class, struct ASTnode **tree) {
struct symtable *sym = NULL;
struct ASTnode *varnode, *exprnode;
*tree = NULL;
// Add this as a known scalar
switch (class) {
case V_STATIC:
case V_EXTERN:
case V_GLOBAL:
// See if this variable is new or already exists
sym = findSymbol(varname, S_NOTATYPE, 0);
if (is_new_symbol(sym, class, type, ctype))
sym = addglob(varname, type, ctype, S_VARIABLE, class, 1, 0);
break;
case V_LOCAL:
sym = addmemb(varname, type, ctype, V_LOCAL, S_VARIABLE, 1);
break;
case V_PARAM:
sym = addmemb(varname, type, ctype, V_PARAM, S_VARIABLE, 1);
break;
case V_MEMBER:
sym = addmemb(varname, type, ctype, V_MEMBER, S_VARIABLE, 1);
break;
}
// The variable is being initialised
if (Token.token == T_ASSIGN) {
// Only possible for a global or local
if (class != V_GLOBAL && class != V_LOCAL && class != V_STATIC)
fatals("Variable can not be initialised", varname);
scan(&Token);
// Globals must be assigned a literal value
if (class == V_GLOBAL || class == V_STATIC) {
// Create one initial value for the variable and
// parse this value
sym->initlist = (int *) malloc(sizeof(int));
sym->initlist[0] = parse_literal(type);
}
if (class == V_LOCAL) {
// Make an A_IDENT AST node with the variable
varnode = mkastleaf(A_IDENT, sym->type, sym->ctype, sym, 0);
// Get the expression for the assignment, make into a rvalue
exprnode = binexpr(0);
exprnode->rvalue = 1;
// If the exprnode is an A_INTLIT and the variable type is P_CHAR,
// and the INTLIT is in the range 0 to 255, change the exprnode's
// type to PCHAR to ensure we can do the assignment
if ((exprnode->op == A_INTLIT) && (varnode->type == P_CHAR) &&
(exprnode->a_intvalue >= 0) && (exprnode->a_intvalue < 256))
exprnode->type = P_CHAR;
// Ensure the expression's type matches the variable
exprnode = modify_type(exprnode, varnode->type, varnode->ctype, 0);
if (exprnode == NULL)
fatal("Incompatible expression in assignment");
// Make an assignment AST tree
*tree = mkastnode(A_ASSIGN, exprnode->type, exprnode->ctype, exprnode,
NULL, varnode, NULL, 0);
}
}
return (sym);
}
// Given the type, name and class of an array variable, parse
// the size of the array, if any. Then parse any initialisation
// value and allocate storage for it.
// Return the variable's symbol table entry.
static struct symtable *array_declaration(char *varname, int type,
struct symtable *ctype, int class) {
struct symtable *sym = NULL; // New symbol table entry
int nelems = -1; // Assume the number of elements won't be given
int maxelems; // The maximum number of elements in the init list
int *initlist; // The list of initial elements
int i = 0, j;
// Skip past the '['
scan(&Token);
// See if we have an array size
if (Token.token != T_RBRACKET) {
nelems = parse_literal(P_INT);
if (nelems <= 0)
fatald("Array size is illegal", nelems);
}
// Ensure we have a following ']'
match(T_RBRACKET, "]");
// Add this as a known array. We treat the
// array as a pointer to its elements' type
switch (class) {
case V_STATIC:
case V_EXTERN:
case V_GLOBAL:
// See if this variable is new or already exists
sym = findSymbol(varname, S_NOTATYPE, 0);
if (is_new_symbol(sym, class, pointer_to(type), ctype))
sym = addglob(varname, pointer_to(type), ctype, S_ARRAY, class, 0, 0);
break;
case V_LOCAL:
// Add the array to the local symbol table.
sym = addmemb(varname, pointer_to(type), ctype, V_LOCAL, S_ARRAY, 0);
sym->st_hasaddr = 1;
break;
default:
fatal("Declaration of array parameters is not implemented");
}
// Array initialisation
if (Token.token == T_ASSIGN) {
if (class != V_GLOBAL && class != V_STATIC)
fatals("Variable can not be initialised", varname);
scan(&Token);
// Get the following left curly bracket
match(T_LBRACE, "{");
#define TABLE_INCREMENT 10
// If the array already has nelems, allocate that many elements
// in the list. Otherwise, start with TABLE_INCREMENT.
if (nelems != -1)
maxelems = nelems;
else
maxelems = TABLE_INCREMENT;
initlist = (int *) malloc(maxelems * sizeof(int));
// Loop getting a new literal value from the list
while (1) {
// Check we can add the next value, then parse and add it
if (nelems != -1 && i == maxelems)
fatal("Too many values in initialisation list");
initlist[i++] = parse_literal(type);
// Increase the list size if the original size was
// not set and we have hit the end of the current list
if (nelems == -1 && i == maxelems) {
maxelems += TABLE_INCREMENT;
initlist = (int *) realloc(initlist, maxelems * sizeof(int));
}
// Leave when we hit the right curly bracket
if (Token.token == T_RBRACE) {
scan(&Token);
break;
}
// Next token must be a comma, then
comma();
}
// Zero any unused elements in the initlist.
// Attach the list to the symbol table entry
for (j = i; j < sym->nelems; j++)
initlist[j] = 0;
if (i > nelems)
nelems = i;
sym->initlist = initlist;
}
// Set the size of the array and the number of elements
// Only externs can have no elements.
if (class != V_EXTERN && nelems <= 0)
fatals("Array must have non-zero elements", sym->name);
sym->nelems = nelems;
sym->size = sym->nelems * typesize(type, ctype);
return (sym);
}
// Given a pointer to the new function being declared and
// a possibly NULL pointer to the function's previous declaration,
// parse a list of parameters and cross-check them against the
// previous declaration. Return the count of parameters
static int param_declaration_list(struct symtable *oldfuncsym,
struct symtable *newfuncsym) {
int type, paramcnt = 0;
struct symtable *ctype;
struct symtable *protoptr = NULL;
struct ASTnode *unused;
// Get the pointer to the first prototype parameter
if (oldfuncsym != NULL)
protoptr = oldfuncsym->member;
// Loop getting any parameters
while (Token.token != T_RPAREN) {
// If the first token is 'void'
if (Token.token == T_VOID) {
// Peek at the next token. If a ')', the function
// has no parameters, so leave the loop.
scan(&Peektoken);
if (Peektoken.token == T_RPAREN) {
// Move the Peektoken into the Token
paramcnt = 0;
scan(&Token);
break;
}
}
// If an ellipsis (...), mark the function as such
if (Token.token == T_ELLIPSIS) {
newfuncsym->has_ellipsis= 1;
// This must be the last parameter, so expect a ')'
scan(&Token);
if (Token.token != T_RPAREN)
fatal("Expecting right parenthesis after ellipsis");
// Leave the parameter loop
break;
}
// Get the type of the next parameter
type = declaration_list(&ctype, V_PARAM, T_COMMA, T_RPAREN, &unused);
if (type == -1)
fatal("Bad type in parameter list");
if (protoptr != NULL) {
// Ensure the type of this parameter matches the prototype
if (type != protoptr->type) {
fatald("Type doesn't match prototype for parameter", paramcnt + 1);
}
// Ensure the old/new parameter names also match
if (strcmp(Text, protoptr->name)) {
fatals("New parameter name doesn't match prototype", Text);
}
protoptr = protoptr->next;
}
paramcnt++;
// Stop when we hit the right parenthesis
if (Token.token == T_RPAREN)
break;
// We need a comma as separator
comma();
}
if (oldfuncsym != NULL && paramcnt != oldfuncsym->nelems)
fatals("Parameter count mismatch for function", oldfuncsym->name);
// Return the count of parameters
return (paramcnt);
}
//
// function_declaration: type identifier '(' parameter_list ')' ;
// | type identifier '(' parameter_list ')' compound_statement ;
//
// Parse the declaration of function.
static struct symtable *function_declaration(char *funcname, int type,
struct symtable *ctype,
int class) {
struct ASTnode *tree;
struct symtable *oldfuncsym, *newfuncsym = NULL;
int endlabel = 0, paramcnt;
int linenum = Line;
// Search for an existing symbol with this name
// and point oldfuncsym at it, or NULL.
if ((oldfuncsym = findSymbol(funcname, S_NOTATYPE, 0)) != NULL)
if (oldfuncsym->stype != S_FUNCTION)
oldfuncsym = NULL;
// Add the function to the symbol table.
// Assumption: functions only return scalar types, so NULL below
newfuncsym = addglob(funcname, type, NULL, S_FUNCTION, class, 0, 0);
newfuncsym->has_ellipsis=0; // Assume no ellipsis for now
// NULL the global Functionid so that we don't try to match this
// function's parameters against the ones in the previous function
Functionid= NULL;
// Scan in the '(', any parameters and the ')'.
// Pass in any existing function prototype pointer
lparen();
paramcnt = param_declaration_list(oldfuncsym, newfuncsym);
rparen();
// If this is a new function declaration, update the
// function symbol entry with the number of parameters.
// Also copy the parameter list into the function's node.
if (newfuncsym) {
newfuncsym->nelems = paramcnt;
oldfuncsym = newfuncsym;
}
// If the declaration ends in a semicolon, only a prototype.
if (Token.token == T_SEMI) {
return (oldfuncsym);
}
// This is not just a prototype.
// Set the Functionid global to the function's symbol pointer
Functionid = oldfuncsym;
// Get the AST tree for the compound statement and mark
// that we have parsed no loops or switches yet
Looplevel = 0;
Switchlevel = 0;
lbrace();
tree = compound_statement(0);
rbrace();
// If the function type isn't P_VOID ...
if (type != P_VOID) {
// Error if no statements in the function
if (tree == NULL)
fatal("No statements in function with non-void type");
// Check that the last AST operation in the
// compound statement was a return statement
// NOTE! Because we have free'd the tree,
// we can't do this any more
#if 0
finalstmt = (tree->op == A_GLUE) ? tree->right : tree;
if (finalstmt == NULL || finalstmt->op != A_RETURN)
fatal("No return for function with non-void type");
#endif
}
// Build the A_FUNCTION node which has the function's symbol pointer
// and the compound statement sub-tree
tree = mkastunary(A_FUNCTION, type, ctype, tree, oldfuncsym, endlabel);
tree->linenum = linenum;
// Do optimisations on the AST tree
// WAS tree = optimise(tree);
// Serialise the tree
serialiseAST(tree);
freetree(tree, 0);
// Flush out the in-memory symbol table.
// We are no longer in a function.
flushSymtable();
Functionid= NULL;
return (oldfuncsym);
}
// Parse composite type declarations: structs or unions.
// Either find an existing struct/union declaration, or build
// a struct/union symbol table entry and return its pointer.
static struct symtable *composite_declaration(int type) {
struct symtable *ctype = NULL;
struct symtable *m;
struct ASTnode *unused;
int offset;
int t;
// Skip the struct/union keyword
scan(&Token);
// See if there is a following struct/union name
if (Token.token == T_IDENT) {
// Find any matching composite type
if (type == P_STRUCT)
ctype = findstruct(Text);
else
ctype = findunion(Text);
scan(&Token);
}
// If the next token isn't an LBRACE , this is
// the usage of an existing struct/union type.
// Return the pointer to the type.
if (Token.token != T_LBRACE) {
if (ctype == NULL)
fatals("unknown struct/union type", Text);
return (ctype);
}
// Ensure this struct/union type hasn't been
// previously defined
if (ctype)
fatals("previously defined struct/union", Text);
// Build the composite type and skip the left brace
if (type == P_STRUCT)
ctype = addtype(Text, P_STRUCT, NULL, S_STRUCT, V_GLOBAL, 0, 0);
else
ctype = addtype(Text, P_UNION, NULL, S_UNION, V_GLOBAL, 0, 0);
scan(&Token);
// Scan in the list of members
while (1) {
// Get the next member. m is used as a dummy
t = declaration_list(&m, V_MEMBER, T_SEMI, T_RBRACE, &unused);
if (t == -1)
fatal("Bad type in member list");
if (Token.token == T_SEMI)
scan(&Token);
if (Token.token == T_RBRACE)
break;
}
// Find the closing parenthesis
rbrace();
// Set the offset of the initial member
// and find the first free byte after it
m = ctype->member;
m->st_posn = 0;
offset = typesize(m->type, m->ctype);
// Set the position of each successive member in the composite type
// Unions are easy. For structs, align the member and find the next free byte
for (m = m->next; m != NULL; m = m->next) {
// Set the offset for this member
if (type == P_STRUCT)
m->st_posn = genalign(m->type, offset, 1);
else
m->st_posn = 0;
// Get the offset of the next free byte after this member
offset += typesize(m->type, m->ctype);
}
// Set the overall size of the composite type
ctype->size = offset;
return (ctype);
}
// Parse an enum declaration
static void enum_declaration(void) {
struct symtable *etype = NULL;
char *name = NULL;
int intval = 0;
// Skip the enum keyword.
scan(&Token);
// If there's a following enum type name, get a
// pointer to any existing enum type node.
if (Token.token == T_IDENT) {
etype = findenumtype(Text);
name = strdup(Text); // As it gets tromped soon
scan(&Token);
}
// If the next token isn't a LBRACE, check
// that we have an enum type name, then return
if (Token.token != T_LBRACE) {
if (etype == NULL)
fatals("undeclared enum type:", name);
return;
}
// We do have an LBRACE. Skip it
scan(&Token);
// If we have an enum type name, ensure that it
// hasn't been declared before.
if (etype != NULL)
fatals("enum type redeclared:", etype->name);
// Build an enum type node for this identifier
// if there is a name
if (name!=NULL) {
etype = addtype(name, P_INT, NULL, S_ENUMTYPE, V_GLOBAL, 0, 0);
free(name);
}
// Loop to get all the enum values
while (1) {
// Ensure we have an identifier
// Copy it in case there's an int literal coming up
ident();
name = strdup(Text);
// Ensure this enum value hasn't been declared before
etype = findenumval(name);
if (etype != NULL)
fatals("enum value redeclared:", name);
// If the next token is an '=', skip it and
// get the following int literal
if (Token.token == T_ASSIGN) {
scan(&Token);
if ((Token.token != T_INTLIT) && (Token.token != T_CHARLIT))
fatal("Expected int literal after '='");
intval = Token.intvalue;
scan(&Token);
}
// Build an enum value node for this identifier.
// Increment the value for the next enum identifier.
etype = addglob(name, P_INT, NULL, S_ENUMVAL, V_GLOBAL, 0, intval++);
free(name);
// Bail out on a right curly bracket, else get a comma
if (Token.token == T_RBRACE)
break;
comma();
}
scan(&Token); // Skip over the right curly bracket
}
// Parse a typedef declaration and return the type
// and ctype that it represents
static int typedef_declaration(struct symtable **ctype) {
int type, class = 0;
// Skip the typedef keyword.
scan(&Token);
// Get the actual type following the keyword
type = parse_type(ctype, &class);
if (class != 0)
fatal("Can't have static/extern in a typedef declaration");
// Get any following '*' tokens
type = parse_stars(type);
// See if the typedef identifier already exists
if (findtypedef(Text) != NULL)
fatals("redefinition of typedef", Text);
// It doesn't exist so add it to the type list
addtype(Text, type, *ctype, S_TYPEDEF, class, 0, 0);
scan(&Token);
return (type);
}
// Given a typedef name, return the type it represents
static int type_of_typedef(char *name, struct symtable **ctype) {
struct symtable *t;
// Look up the typedef in the list
t = findtypedef(name);
if (t == NULL)
fatals("unknown type", name);
scan(&Token);
*ctype = t->ctype;
return (t->type);
}
// Parse the declaration of a variable or function.
// The type and any following '*'s have been scanned, and we
// have the identifier in the Token variable.
// The class argument is the symbol's class.
// Return a pointer to the symbol's entry in the symbol table
static struct symtable *symbol_declaration(int type, struct symtable *ctype,
int class, struct ASTnode **tree) {
struct symtable *sym = NULL;
char *varname = strdup(Text);
// Ensure that we have an identifier.
// We copied it above so we can scan more tokens in, e.g.
// an assignment expression for a local variable.
ident();
// Deal with function declarations
if (Token.token == T_LPAREN) {
sym= function_declaration(varname, type, ctype, class);
free(varname);
return(sym);
}
// See if this array or scalar variable has already been declared
switch (class) {
case V_EXTERN:
case V_STATIC:
case V_GLOBAL:
case V_LOCAL:
case V_PARAM:
if (findlocl(varname, 0) != NULL)
fatals("Duplicate local variable declaration", varname);
break;
case V_MEMBER:
if (findmember(varname) != NULL)
fatals("Duplicate struct/union member declaration", varname);
}
// Add the array or scalar variable to the symbol table
if (Token.token == T_LBRACKET) {
sym = array_declaration(varname, type, ctype, class);
*tree = NULL; // Local arrays are not initialised
} else
sym = scalar_declaration(varname, type, ctype, class, tree);
free(varname);
return (sym);
}
// Parse a list of symbols where there is an initial type.
// Return the type of the symbols. et1 and et2 are end tokens.
int declaration_list(struct symtable **ctype, int class, int et1, int et2,
struct ASTnode **gluetree) {
int inittype, type;
struct symtable *sym;
struct ASTnode *tree = NULL;
*gluetree = NULL;
// Get the initial type. If -1, it was
// a composite type definition, return this
if ((inittype = parse_type(ctype, &class)) == -1)
return (inittype);
// Now parse the list of symbols
while (1) {
// See if this symbol is a pointer
type = parse_stars(inittype);
// Parse this symbol
sym = symbol_declaration(type, *ctype, class, &tree);
// We parsed a function, there is no list so leave
if (sym->stype == S_FUNCTION) {
if (class != V_GLOBAL && class != V_STATIC)
fatal("Function definition not at global level");
return (type);
}
// Glue any AST tree from a local declaration
// to build a sequence of assignments to perform
if (*gluetree == NULL)
*gluetree = tree;
else
*gluetree =
mkastnode(A_GLUE, P_NONE, NULL, *gluetree, NULL, tree, NULL, 0);
// We are at the end of the list, leave
if (Token.token == et1 || Token.token == et2)
return (type);
// Otherwise, we need a comma as separator
comma();
}
return (0); // Keep -Wall happy
}
// Parse one or more global declarations,
// either variables, functions or structs
void global_declarations(void) {
struct symtable *ctype = NULL;
struct ASTnode *unused;
// Loop parsing one declaration list until the end of file
while (Token.token != T_EOF) {
declaration_list(&ctype, V_GLOBAL, T_SEMI, T_EOF, &unused);
// Skip any separating semicolons
if (Token.token == T_SEMI)
scan(&Token);
}
}