-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsym_tab.c
More file actions
1583 lines (1501 loc) · 47.7 KB
/
sym_tab.c
File metadata and controls
1583 lines (1501 loc) · 47.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
This software was developed by employees of the National Institute
of Standards and Technology (NIST), an agency of the Federal
Government and is being made available as a public service. Pursuant
to title 17 United States Code Section 105, works of NIST employees
are not subject to copyright protection in the United States. This
software may be subject to foreign copyright. Permission in the
United States and in foreign countries, to the extent that NIST may
hold copyright, to use, copy, modify, create derivative works, and
distribute this software and its documentation without fee is hereby
granted on a non-exclusive basis, provided that this notice and
disclaimer of warranty appears in all copies.
THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND,
EITHER EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED
TO, ANY WARRANTY THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS,
ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
PURPOSE, AND FREEDOM FROM INFRINGEMENT, AND ANY WARRANTY THAT THE
DOCUMENTATION WILL CONFORM TO THE SOFTWARE, OR ANY WARRANTY THAT THE
SOFTWARE WILL BE ERROR FREE. IN NO EVENT SHALL NIST BE LIABLE FOR
ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, INDIRECT, SPECIAL
OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, OR IN ANY
WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY,
CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY
PERSONS OR PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS
SUSTAINED FROM, OR AROSE OUT OF THE RESULTS OF, OR USE OF, THE
SOFTWARE OR SERVICES PROVIDED HEREUNDER.
*/
#include <stdlib.h>
#include <string.h>
#include "ansi_parse.h"
#include "lif.h"
#include "sym_tab.h"
/*
static char sccsid[] = "@(#)sym_tab.c 1.4 4/26/95";
*/
static int scope_level = 1;
static symbol_table_entries symbol_table[MAX_NEST];
static int max_local_id = 1;
static int max_global_id = 1;
static int in_typedef = 0;
#define MAX_ST 10000
static symbol_table_entries closed_symbol_tables[MAX_ST];
static int n_st = 0;
void
declared(char *var)
{
if (scope_level > 1)
return;
if (var)
fprintf(h_file, "\t%s\n", var);
}
int
is_var_ptr_mod(token_ptr t)
{
type_ptr mod;
if (!t)
return False;
mod = t->desc.type_desc;
while (mod)
{
if (mod->is == IS_PTR_TO)
return True;
mod = mod->next;
}
return False;
}
int
is_type_ptr(type_ste_ptr t)
{
if (!t)
return False;
if (is_var_ptr_mod(t->token))
return True;
if (t->detail != STE_TYPEDEF)
return False;
if (is_type_ptr(t->type_entry))
return True;
return False;
}
int
is_var_ptr(var_ste_ptr var)
{
/* is var declared pointer */
if (is_var_ptr_mod(var->token))
return True;
if (var->detail != STE_TYPEDEF)
return False;
/* var is a user defined type (might be pointer type) */
if (is_type_ptr(var->type_entry))
return True;
return False;
}
var_ste_ptr
get_var_members(var_ste_ptr var)
{
type_ste_ptr t;
if (!var)
return NULL;
if (var->detail == STE_ANON_STRUCT)
return var->anon_entry;
if (var->detail == STE_TAGGED_STRUCT)
return var->tag_entry->vars;
if (var->detail != STE_TYPEDEF)
return NULL;
t = var->type_entry;
while (t)
{
if (t->detail == STE_ANON_STRUCT)
return t->anon_entry;
if (t->detail == STE_TAGGED_STRUCT)
return t->tag_entry->vars;
if (t->detail != STE_TYPEDEF)
return NULL;
t = t->type_entry;
}
return NULL;
}
/*
struct hh { STE for variable
token_ptr token;
token_ptr type_decl;
int detail;
type_ste_ptr type_entry;
tag_ste_ptr tag_entry;
var_ste_ptr anon_entry;
int id;
var_ste_ptr next;
};
*/
void
is_decl_array_or_pointer(type_ste_ptr t, int *is_array, int *is_pointer)
{
type_ptr details;
if (!t)
return;
details = t->token->desc.type_desc;
while (details)
{
if (details->is == IS_ARRAY_OF)
*is_array = 1;
if (details->is == IS_PTR_TO)
*is_pointer = 1;
details = details->next;
}
if ((!(*is_pointer && *is_array)) &&
(t->type_decl->desc.decl->decl_flags & FLAG_TYPE_USER_NAME))
{
/* look at user defined type unless both array and pointer on */
if (t->detail == STE_TYPEDEF)
is_decl_array_or_pointer(t->type_entry, is_array, is_pointer);
}
}
int is_var_array(v) var_ste_ptr v;
{
int is_array, is_pointer;
type_ptr details;
if (!v)
return 0;
details = v->token->desc.type_desc;
while (details)
{
if (details->is == IS_ARRAY_OF)
return 1;
details = details->next;
}
is_pointer = 0;
is_array = 0;
is_decl_array_or_pointer(v->type_entry, &is_array, &is_pointer);
return is_array;
}
/*
*********************************************************************
* *
* do_ids is called when a scope level is closed to output a list *
* of all identifiers in LIF format *
* *
*********************************************************************
*/
void
do_ids()
{
var_ste_ptr v;
int class;
int is_array, is_pointer;
type_ptr details;
class = scope_level == 1 ? LIF_GLOBAL_ID : LIF_LOCAL_ID;
v = symbol_table[scope_level].var;
while (v)
{
is_array = 0;
is_pointer = 0;
details = v->token->desc.type_desc;
while (details)
{
if (details->is == IS_ARRAY_OF)
is_array = 1;
if (details->is == IS_PTR_TO)
is_pointer = 1;
details = details->next;
}
if ((!(is_pointer && is_array)) &&
(v->type_decl->desc.decl->decl_flags & FLAG_TYPE_USER_NAME))
{
/* look at user defined type unless both array and pointer on */
if (v->detail == STE_TYPEDEF)
is_decl_array_or_pointer(v->type_entry, &is_array, &is_pointer);
}
fprintf(outfile, "%d(%d,%s", class, v->id, v->token->text);
if (FLAG_SC_STATIC & v->type_decl->desc.decl->decl_flags)
fprintf(outfile, ",S");
if (is_pointer)
fprintf(outfile, ",P");
if (FLAG_SC_EXTERN & v->type_decl->desc.decl->decl_flags)
fprintf(outfile, ",X");
if (is_array)
fprintf(outfile, ",A");
fprintf(outfile, ")");
if (z_opt)
{
fprintf(outfile, " %s id", class == LIF_GLOBAL_ID ? "Global" : "local");
if (is_pointer)
fprintf(outfile, " pointer");
if (is_array)
fprintf(outfile, " array");
}
fprintf(outfile, "\n");
v = v->next;
}
}
/*
*********************************************************************
* *
* do_formals is called when a fun decl is done to output a list *
* of all formal parms in LIF format *
* *
*********************************************************************
*/
void
do_formals()
{
var_ste_ptr v;
type_ptr details;
int is_array = 0, is_pointer = 0;
v = symbol_table[scope_level].var;
while (v)
{
is_array = 0;
is_pointer = 0;
details = v->token->desc.type_desc;
while (details)
{
if (details->is == IS_ARRAY_OF)
is_array = 1;
if (details->is == IS_PTR_TO)
is_pointer = 1;
details = details->next;
}
if ((!(is_pointer && is_array)) &&
(v->type_decl->desc.decl->decl_flags & FLAG_TYPE_USER_NAME))
{
/* look at user defined type unless both array and pointer on */
if (v->detail == STE_TYPEDEF)
is_decl_array_or_pointer(v->type_entry, &is_array, &is_pointer);
}
fprintf(outfile, "%d(%d,%s", LIF_FORMAL_ID, v->id, v->token->text);
if (is_pointer)
fprintf(outfile, ",P");
if (is_array)
fprintf(outfile, ",A");
fprintf(outfile, ")");
if (z_opt)
{
fprintf(outfile, " Formal parm");
if (is_pointer)
fprintf(outfile, " pointer");
if (is_array)
fprintf(outfile, " array");
}
fprintf(outfile, "\n");
v = v->next;
}
}
/*
*********************************************************************
* *
* reset id counter for a new procedure *
* *
*********************************************************************
*/
void
start_local_decls()
{
max_local_id = 1;
}
/*
*********************************************************************
* *
* allocate an id from locals or globals *
* *
*********************************************************************
*/
int
current_global()
{
return max_global_id - 1;
}
int
current_local()
{
return max_local_id - 1;
}
int
alloc_local()
{
return max_local_id++;
}
int
alloc_global()
{
return max_global_id++;
}
int
current_id()
{
if (scope_level == 1)
return max_global_id - 1;
return max_local_id - 1;
}
int
alloc_id(void)
{
int id;
if (scope_level == 1)
id = max_global_id++;
else
id = max_local_id++;
return id;
}
/*
*********************************************************************
* *
* modify the token (some typename e.g. int) with a flag to *
* indicate the type being used *
* *
*********************************************************************
*/
void
make_decl(token_ptr token, unsigned int flag)
{
token->desc.decl = alloc_decl();
token->desc.decl->decl_flags = flag;
token->desc.decl->vars = NULL;
token->desc.decl->tag = NULL;
token->desc.decl->members = NULL;
token->desc.decl->member_ste = NULL;
token->desc.decl->offset = 0;
}
/*
*********************************************************************
* *
* modify an identifier (token) to indicate pointers, arrays, *
* or style of function declaration *
* *
*********************************************************************
*/
void
modify_type(token_ptr token, unsigned int flag, int style, token_ptr formals)
{
type_ptr new, old;
new = alloc_type();
new->is = flag;
new->next = NULL;
new->style = style;
new->formals = formals;
if (old = token->desc.type_desc)
{
while (old->next)
old = old->next;
old->next = new;
}
else
token->desc.type_desc = new;
}
type_ptr
make_abstract_type(unsigned int flag, int style, token_ptr formals)
{
type_ptr new;
new = alloc_type();
new->is = flag;
new->next = NULL;
new->style = style;
new->formals = formals;
return new;
}
void
merge_abstract_type(type_ptr old, type_ptr new)
{
if (old)
{
while (old->next)
old = old->next;
old->next = new;
}
}
/*
*********************************************************************
* *
* stack a new scope level for the symbol table *
* *
*********************************************************************
*/
void
open_scope(void)
{
scope_level++;
if (c_opt)
printf("Open scope level %d\n", scope_level);
symbol_table[scope_level].var = NULL;
symbol_table[scope_level].type = NULL;
symbol_table[scope_level].tag = NULL;
}
/*
*********************************************************************
* *
* close a scope level and if c_opt print the variables, tags and *
* types. *
* *
*********************************************************************
*/
void
close_scope(void)
{
type_ste_ptr types;
var_ste_ptr vars;
tag_ste_ptr tags;
if (c_opt)
printf("Close scope level %d ==> st[%d]\n", scope_level, n_st);
if (scope_level == 2)
do_formals();
else
do_ids();
closed_symbol_tables[n_st].var = symbol_table[scope_level].var;
closed_symbol_tables[n_st].type = symbol_table[scope_level].type;
closed_symbol_tables[n_st++].tag = symbol_table[scope_level].tag;
if (c_opt)
{
vars = symbol_table[scope_level].var;
printf("\nS C O P E L E V E L %d => st[%d]\n", scope_level, n_st - 1);
printf("Declared variables:\n");
while (vars)
{
printf("%s\n", vars->token->text);
vars = vars->next;
}
types = symbol_table[scope_level].type;
printf("Declared types:\n");
while (types)
{
printf("%s\n", types->token->text);
types = types->next;
}
tags = symbol_table[scope_level].tag;
printf("Declared tags:\n");
while (tags)
{
printf("%s\n", tags->tag->desc.decl->tag->text);
tags = tags->next;
}
}
scope_level--;
}
/*
*********************************************************************
* *
* look for a tag, return the ste and if tag is found in *
* the local scope, set local_flag to true. *
* *
*********************************************************************
*/
tag_ste_ptr
find_tag(char *tag, int *local_flag)
{
int level;
tag_ste_ptr vars;
level = scope_level;
/*printf ("look for tag %s\n",tag);*/
*local_flag = True;
while (level >= 0)
{
/* printf ("searching level %d\n",level); */
vars = symbol_table[level].tag;
while (vars)
{
if (strcmp(vars->tag->desc.decl->tag->text, tag) == 0)
return vars;
vars = vars->next;
}
*local_flag = False;
level--;
}
/*printf ("tag %s not found\n",tag);*/
return NULL;
}
/*
*********************************************************************
* *
* search symbol table for a type name *
* *
*********************************************************************
*/
type_ste_ptr
find_type(char *type_name)
{
int level;
type_ste_ptr t;
if (!type_name)
return NULL;
level = scope_level;
/*
printf ("look for type_name %s\n",type_name);
*/
while (level >= 0)
{
/* printf ("searching level %d\n",level); */
t = symbol_table[level].type;
while (t)
{
if (strcmp(t->token->text, type_name) == 0)
return t;
t = t->next;
}
level--;
}
return NULL;
}
/*
*********************************************************************
* *
* *
* *
*********************************************************************
*/
static void tag_decl(
type_desc, var_list, tag_entry_ptr, detail_ptr, with_members) token_ptr type_desc,
var_list;
tag_ste_ptr *tag_entry_ptr;
int *detail_ptr, with_members;
{
tag_ste_ptr new_tag, tag_entry = NULL;
int no_tag = 0;
int is_local_tag;
if (type_desc->desc.decl->decl_flags & FLAG_TYPE_US)
{
/*printf ("Do struct/union\n");*/
/* if this decl has a tag, see if it exists already */
if (type_desc->desc.decl->tag)
tag_entry = find_tag(type_desc->desc.decl->tag->text, &is_local_tag);
else /* this structure has no tag => anon struct */
{
tag_entry = NULL;
no_tag = 1;
}
if (c_opt)
printf("entry %p no_tag (%d): ", tag_entry, no_tag);
if (!tag_entry && !no_tag)
{ /* tag not found anywhere */
tag_entry = new_tag = alloc_tag_ste();
new_tag->next = symbol_table[scope_level].tag;
new_tag->tag = type_desc;
new_tag->vars = type_desc->desc.decl->member_ste;
symbol_table[scope_level].tag = new_tag;
if (c_opt)
printf("declare new %s tag (%p) at scope level %d ",
is_local_tag ? "local" : "non-local",
tag_entry,
scope_level);
}
else if (is_local_tag)
{ /* tag exists locally */
/* if the new decl has members and the existing
decl does not, then move the member list from
the new decl to the tagged_ste */
if (tag_entry)
{
if (!tag_entry->vars)
tag_entry->vars = type_desc->desc.decl->member_ste;
}
}
else if (no_tag)
{ /* anon struct */
}
else
{ /* tag found non-local */
/* declare tag as local if either:
(1) tag has members or
(2) tag does not have variables
*/
if ((type_desc->desc.decl->members) || !var_list || with_members)
{
tag_entry = new_tag = alloc_tag_ste();
new_tag->next = symbol_table[scope_level].tag;
new_tag->tag = type_desc;
new_tag->vars = type_desc->desc.decl->member_ste;
symbol_table[scope_level].tag = new_tag;
if (c_opt)
printf(
"declare new masking tag (%p) at scope level %d ", tag_entry, scope_level);
}
}
if (c_opt)
if (type_desc->desc.decl->tag)
printf("tag %s ", type_desc->desc.decl->tag->text);
if (c_opt)
if (type_desc->desc.decl->decl_flags & FLAG_TYPE_UNION)
printf("union ");
else
printf("struct ");
if (c_opt)
printf("declared %s members ", type_desc->desc.decl->members ? "with" : "without");
if (c_opt)
printf("and %s variables\n", var_list ? "with" : "without");
}
*tag_entry_ptr = tag_entry;
*detail_ptr = no_tag;
if (c_opt)
printf("tag decl returns tag_entry %p and no tag %d\n", tag_entry, no_tag);
}
/*
*********************************************************************
* *
* struct_decl is called to reduce the member list of a struct/union*
* *
* type_desc is a list of declarations (type tokens) *
* each declaration has a type and a list of variables *
* *
* struct_decl returns a symbol table for the members of the struct *
* *
*********************************************************************
struct_or_union_specifier
: struct_or_union identifier
{
$1->desc.decl->tag = $2;
decl ($1,NULL,True);
}
'{' struct_declaration_list '}'
{
$1->desc.decl->member_ste = struct_decl($5);
$1->desc.decl->members = $5;
}
| struct_or_union '{' struct_declaration_list '}'
{
$1->desc.decl->members = $3;
$1->desc.decl->member_ste = struct_decl($3);
}
*/
var_ste_ptr
struct_decl(token_ptr type_desc, int *off)
{
token_ptr d, vars;
var_ste_ptr st = NULL, new_var;
int detail, no_tag;
int off_set = 0;
var_ste_ptr anon, last;
tag_ste_ptr tag_entry;
type_ste_ptr type_entry = NULL;
if (c_opt)
printf("struct decl: ");
d = type_desc;
while (d)
{
vars = d->desc.decl->vars;
if (c_opt)
printf("(%08o) ", d->desc.decl->decl_flags);
detail = STE_NONE;
anon = NULL;
tag_entry = NULL;
if (d->desc.decl->decl_flags & FLAG_TYPE_US)
{
/*
if (d->desc.decl->tag){
tag_entry = find_tag(d->desc.decl->tag->text,
&is_local_tag);
if (tag_entry) detail = STE_TAGGED_STRUCT;
else detail = STE_NONE;
}
else {
detail = STE_ANON_STRUCT;
anon = d->desc.decl->member_ste;
}
*/
tag_decl(d, vars, &tag_entry, &no_tag, d->desc.decl->members != NULL);
if (no_tag)
{
detail = STE_ANON_STRUCT;
anon = d->desc.decl->member_ste;
}
else
{
if (tag_entry)
detail = STE_TAGGED_STRUCT;
else
detail = STE_NONE;
}
}
else if (d->desc.decl->decl_flags & FLAG_TYPE_USER_NAME)
{
type_entry = find_type(d->text);
if (type_entry)
detail = STE_TYPEDEF;
}
else
{
detail = STE_NONE;
anon = NULL;
}
last = st;
if (last)
while (last->next)
last = last->next;
while (vars)
{
if (c_opt)
printf(" %s <detail %d> ", vars->text, detail);
new_var = alloc_var_ste();
new_var->detail = detail;
new_var->next = NULL;
new_var->type_entry = type_entry;
new_var->tag_entry = tag_entry;
new_var->anon_entry = anon;
new_var->token = vars;
new_var->type_decl = d;
new_var->id = ++off_set;
new_var->addr = 0;
if (last)
last->next = new_var;
else
st = new_var;
last = new_var;
if (!is_var_ptr(new_var))
{
if (c_opt)
printf("call insert from struct_decl add(%s)\n", vars->text);
insert_struct_members(vars->text, get_var_members(new_var), &st, &off_set);
if (last)
while (last->next)
last = last->next;
}
vars = vars->next;
}
d = d->next;
}
if (c_opt)
printf("\n");
if (c_opt)
{
var_ste_ptr s;
printf("Declare a struct: (members)\n");
s = st;
while (s)
{
printf("\t%s \n", s->token->text);
fflush(stdout);
s = s->next;
}
}
*off = off_set;
return st;
}
void
insert_ptr_var(int scope, var_ste_ptr var)
{
var->next = symbol_table[scope].var;
symbol_table[scope].var = var;
declared(var->token->text);
}
/*
*********************************************************************
* *
* used to declare an undeclared old style parm (declared as int) *
* *
*********************************************************************
*/
void
insert_var_decl(token_ptr var)
{
var_ste_ptr new_var;
new_var = alloc_var_ste();
new_var->token = var;
new_var->type_decl = create_token((char *)NULL);
new_var->next = symbol_table[scope_level].var;
new_var->detail = 0;
new_var->type_entry = NULL;
new_var->tag_entry = NULL;
new_var->anon_entry = NULL;
new_var->id = alloc_id();
new_var->addr = 0;
symbol_table[scope_level].var = new_var;
make_decl(new_var->type_decl, FLAG_TYPE_INT);
/* used to declare an undeclared old style parm
(declared as int) */
}
void
insert_struct_members(char *base, var_ste_ptr members, var_ste_ptr *st, int *off_set)
{
char name[3000];
var_ste_ptr new_var, last;
last = *st;
if (last)
{
while (last->next)
last = last->next;
}
while (members)
{
sprintf(name, "%s.%s", base, members->token->text);
if (c_opt)
printf("INS struct mem new: %s\n", name);
new_var = alloc_var_ste();
new_var->token = create_token(name);
new_var->token->desc = members->token->desc;
new_var->type_decl = members->type_decl;
new_var->next = NULL;
new_var->detail = members->detail;
new_var->type_entry = members->type_entry;
new_var->tag_entry = members->tag_entry;
new_var->anon_entry = members->anon_entry;
new_var->id = ++(*off_set);
new_var->addr = 0;
if (last)
{
last->next = new_var;
}
else
{
*st = new_var;
}
last = new_var;
members = members->next;
}
}
void
insert_members(char *base, var_ste_ptr members)
{
char name[3000];
var_ste_ptr new_var, last;
last = symbol_table[scope_level].var;
if (last)
{
while (last->next)
last = last->next;
}
while (members)
{
sprintf(name, "%s.%s", base, members->token->text);
/* printf ("new: %s\n",name); */
new_var = alloc_var_ste();
new_var->token = create_token(name);
new_var->token->desc = members->token->desc;
new_var->type_decl = members->type_decl;
new_var->next = NULL;
new_var->detail = members->detail;
new_var->type_entry = members->type_entry;
new_var->tag_entry = members->tag_entry;
new_var->anon_entry = members->anon_entry;
new_var->id = alloc_id();
new_var->addr = 0;
/*
symbol_table[scope_level].var = new_var;
*/
declared(name);
if (last)
{
last->next = new_var;
}
else
{
symbol_table[scope_level].var = new_var;
}
last = new_var;
/*
if (!is_var_ptr(new_var)){
insert_members (name,
get_var_members (new_var));
}
*/
members = members->next;
}
}
/*
*********************************************************************
* *
* Declare the variables on var_list as type type_desc in the *
* current symbol table level. *
* *
* with_members is used to declare a structure tag with members *
* from inside a structure (i.e. nested structures) *
* *
*********************************************************************
*/
void
decl(token_ptr type_desc, token_ptr var_list, int with_members)
{
token_ptr var, enum_const;
type_ste_ptr new_type, type_entry;
var_ste_ptr new_var;
tag_ste_ptr tag_entry = NULL;
int no_tag = 0, pid;
type_ptr type_mod;
if (c_opt)
printf("Do Decl at line %d\n", type_desc ? type_desc->at.line_start : lineno);
var = var_list;
if (type_desc)
{
if (type_desc->desc.decl->decl_flags & FLAG_TYPE_USER_NAME)
{
type_entry = find_type(type_desc->text);
if (c_opt)
printf("at line %d using user defined type: ", lineno);
if (c_opt)
printf("%s", type_desc->text);
if (c_opt)
if (type_entry)
printf(" %s\n", type_entry->token->text);
else
printf(" not found\n");
}
else
type_entry = NULL;
if (type_desc->desc.decl->decl_flags & FLAG_TYPE_US)
{
tag_decl(type_desc, var_list, &tag_entry, &no_tag, with_members);
}
if (type_desc->desc.decl->decl_flags & FLAG_TYPE_ENUM)
{
enum_const = type_desc->desc.decl->members;
while (enum_const)
{
if (c_opt)
printf("enum %s\n", enum_const->text);
new_var = alloc_var_ste();