summaryrefslogtreecommitdiffabout
path: root/src/parser.c
blob: d2832a1bd93387c01247b335172fb8f412433de4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
/* This file is part of GNU cflow
   Copyright (C) 1997, 2005-2007, 2009-2011, 2014-2016 Sergey Poznyakoff
 
   GNU cflow is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3 of the License, or
   (at your option) any later version.
 
   GNU cflow is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>. */

#include <cflow.h>
#include <parser.h>
#include <ctype.h>

typedef struct {
     char *name;
     int type_end;
     int parmcnt;
     int line;
     enum storage storage;
} Ident;

void parse_declaration(Ident*, int);
void parse_variable_declaration(Ident*, int);
void parse_function_declaration(Ident*, int);
void parse_dcl(Ident*, int maybe_knr);
void parse_knr_dcl(Ident*);
void parse_typedef();
void expression();
void initializer_list();
void func_body();
void declare(Ident*, int maybe_knr);
void declare_type(Ident*);
int dcl(Ident*);
int parmdcl(Ident*);
int dirdcl(Ident*);
void skip_struct();
Symbol *get_symbol(char *name);
void maybe_parm_list(int *parm_cnt_return);
    
void call(char*, int);
void reference(char*, int);

int level;                  /* Current nesting level */
Symbol *caller;             /* Current caller */
struct obstack text_stk;    /* Obstack for composing declaration line */

int parm_level;             /* Parameter declaration nesting level */

typedef struct {
     int type;
     char *token;
     int line;
} TOKSTK;

typedef int Stackpos[1];

TOKSTK tok;
TOKSTK *token_stack;
int tos;
int curs;
int token_stack_length = 64;
int token_stack_increase = 32;
static int need_space;

void mark(Stackpos);
void restore(Stackpos);
void tokpush(int,int,char*);
void save_token(TOKSTK *);

static void
print_token(TOKSTK *tokptr)
{
     switch (tokptr->type) {
     case IDENTIFIER:
     case TYPE:
     case WORD:
     case MODIFIER:
     case STRUCT:
     case PARM_WRAPPER:
     case QUALIFIER:
     case OP:
	  fprintf(stderr, "`%s'", tokptr->token);
	  break;
     case LBRACE0:
     case LBRACE:
	  fprintf(stderr, "`{'");
	  break;
     case RBRACE0:
     case RBRACE:
	  fprintf(stderr, "`}'");
	  break;
     case EXTERN:
	  fprintf(stderr, "`extern'");
	  break;
     case STATIC:
	  fprintf(stderr, "`static'");
	  break;
     case TYPEDEF:
	  fprintf(stderr, "`typedef'");
	  break;
     case STRING:
	  fprintf(stderr, "\"%s\"", tokptr->token);
	  break;
     default:
	  fprintf(stderr, "`%c'", tokptr->type);
     }
}

static char *
token_type_str(int t)
{
     static char buf[80];
     switch (t) {
     case 0:
	  return "EOF";
     case WORD:
	  return "WORD";
     case LBRACE0:
	  return "'{'";
     case RBRACE0:
	  return "'}'";
     case IDENTIFIER:
	  return "IDENTIFIER";
     case EXTERN:
	  return "EXTERN";
     case STATIC:
	  return "STATIC";
     case TYPEDEF:
	  return "TYPEDEF";
     case STRUCT:
	  return "STRUCT";
     case MODIFIER:
	  return "MODIFIER";
     case OP:
	  return "OP";
     case UNION:
	  return "UNION";
     case ENUM:
	  return "ENUM";
     case LBRACE:
	  return "' {'";
     case RBRACE:
	  return "' }'";
     case MEMBER_OF:
	  return "MEMBER_OF";
     case TYPE:
	  return "TYPE";
     case STRING:
	  return "STRING";
     case PARM_WRAPPER:
	  return "PARM_WRAPPER";
     case QUALIFIER:
	  return "QUALIFIER";
     }	  
     if (isprint(t))
	  snprintf(buf, sizeof(buf), "'%c'(%d)", t, t);
     else
	  snprintf(buf, sizeof(buf), "%d", t);
     return buf;
}

static void
dbgtok(TOKSTK *t, int delim)
{
     if (delim)
	  fputc(delim, stderr);
     fprintf(stderr, "{ %s ", token_type_str(t->type));
     if (t->type)
	  fprintf(stderr, ", %s, %d ", t->token ? t->token : "NULL", t->line);
     fputc('}', stderr);
}

static void
debugtoken(TOKSTK *t, char *fmt, ...)
{
     if (debug > 1) {
	  va_list ap;
	  int i;
	  
	  if (fmt) {
	       va_start(ap, fmt);
	       vfprintf(stderr, fmt, ap);
	       va_end(ap);
	       fprintf(stderr, ": ");
	  }
	  if (t) {
	       dbgtok(t, 0);
	       fprintf(stderr, "; ");
	  }
	  fprintf(stderr, "%d: {", curs);
	  for (i = curs; i < tos; i++)
	       dbgtok(token_stack + i, i == curs ? 0 : ',');
	  fprintf(stderr, "}\n");
     }
}

static void
file_error(char *msg, TOKSTK *tokptr)
{
    fprintf(stderr, "%s:%d: %s", filename, tok.line, msg);
    if (tokptr) {
	fprintf(stderr, _(" near "));
	print_token(tokptr);
    }
    fprintf(stderr, "\n");
}

void
mark(Stackpos pos)
{
     pos[0] = curs;
     if (debug > 1)
	  fprintf(stderr, "marking stack at %d\n", curs);
}

void
restore(Stackpos pos)
{
     curs = pos[0];
     if (curs) {
	  tok = token_stack[curs-1];
	  debugtoken(&tok, "restored stack");
     }
}

void
tokdel(int beg, int end)
{
     if (end >= beg) {
	  if (end < tos)
	       memmove(token_stack + beg, token_stack + end + 1,
		       (end - beg + 1) * sizeof(token_stack[0]));
	  tos -= (end - beg + 1);
     }
}

void
tokins(int pos, int type, int line, char *token)
{
     if (++tos == token_stack_length) {
	  token_stack_length += token_stack_increase;
	  token_stack = xrealloc(token_stack,
				 token_stack_length*sizeof(*token_stack));
     }
     memmove(token_stack + pos + 1, token_stack + pos,
	     (tos - pos - 1) * sizeof(token_stack[0]));
     token_stack[pos].type = type;
     token_stack[pos].token = token;
     token_stack[pos].line = line;
     debugtoken(&token_stack[pos], "insert at %d", pos);
}

void
tokpush(int type, int line, char *token)
{
     token_stack[tos].type = type;
     token_stack[tos].token = token;
     token_stack[tos].line = line;
     if (++tos == token_stack_length) {
	  token_stack_length += token_stack_increase;
	  token_stack = xrealloc(token_stack,
				 token_stack_length*sizeof(*token_stack));
     }
}

void
cleanup_stack()
{
     int delta = tos - curs;

     if (delta > 0) 
	  memmove(token_stack, token_stack+curs, delta*sizeof(token_stack[0]));
     else if (delta < 0) /* Invalid input */
	  delta = 0;
     tos = delta;
     curs = 0;
}

void
clearstack()
{
     tos = curs = 0;
}

int
nexttoken()
{
     int type;
     
     if (curs == tos) {
	  type = get_token();
	  tokpush(type, line_num, yylval.str);
	  yylval.str = NULL;
     }
     tok = token_stack[curs];
     curs++;
     debugtoken(&tok, "next token");
     return tok.type;
}

int
putback()
{
     if (curs == 0) {
	  error(0, 0, _("INTERNAL ERROR: cannot return token to stream"));
	  abort();
     }
     curs--;
     if (curs > 0) {
	  tok = token_stack[curs-1];
     } else
	  tok.type = 0;
     debugtoken(&tok, "putback");
     return tok.type;
}

void
init_parse()
{
     obstack_init(&text_stk);
     token_stack = xmalloc(token_stack_length*sizeof(*token_stack));
     clearstack();
}

void
save_token(TOKSTK *tokptr)
{
     int len;
    
     switch (tokptr->type) {
     case IDENTIFIER:
     case TYPE:
     case STRUCT:
     case PARM_WRAPPER:
     case WORD:
     case QUALIFIER:
	  if (need_space) 
	       obstack_1grow(&text_stk, ' ');
	  len = strlen(tokptr->token);
	  obstack_grow(&text_stk, tokptr->token, len);
	  need_space = 1;
	  break;
     case MODIFIER:
	  if (need_space) 
	       obstack_1grow(&text_stk, ' ');
	  if (tokptr->token[0] == '*') 
	       need_space = 0;
	  else
	       need_space = 1;
	  len = strlen(tokptr->token);
	  obstack_grow(&text_stk, tokptr->token, len);
	  break;
     case EXTERN: /* storage class specifiers are already taken care of */
     case STATIC:
	  break;
     case ',':
	  obstack_1grow(&text_stk, ',');
	  need_space = 1;
	  break;
     case '(':
	  if (need_space) 
	       obstack_1grow(&text_stk, ' ');
	  obstack_1grow(&text_stk, tokptr->type);
	  need_space = 0;
	  break;
     case ')':
	  obstack_1grow(&text_stk, tokptr->type);
	  need_space = 1;
	  break;
     case '[':
     case ']':
	  obstack_1grow(&text_stk, tokptr->type);
	  need_space = 0;
	  break;
     case OP:
	  obstack_1grow(&text_stk, ' ');
	  obstack_grow(&text_stk, tokptr->token, strlen(tokptr->token));
	  need_space = 1;
	  break;
     default:
	  if (verbose)
	       file_error(_("unrecognized definition"), tokptr);
     }
}

static Stackpos start_pos; /* Start position in stack for saving tokens */
static int save_end;       /* Stack position up to which the tokens are saved */

void
save_stack()
{
     mark(start_pos);
     save_end = curs - 1;
}

void
undo_save_stack()
{
     save_end = -1;
}

char *
finish_save_stack(char *name)
{
     int i;
     int level = 0;
     int found_ident = !omit_symbol_names_option;

     need_space = 0;
     for (i = 0; i < save_end ; i++) {
	  switch (token_stack[i].type) {
	  case '(':
	       if (omit_arguments_option) {
		    if (level == 0) {
			 save_token(token_stack+i);
		    }
		    level++;
	       }
	       break;
	  case ')':
	       if (omit_arguments_option) 
		    level--;
	       break;
	  case IDENTIFIER:
	       if (!found_ident && strcmp (name, token_stack[i].token) == 0) {
		    need_space = 1;
		    found_ident = 1;
		    continue;
	       }
	  }
	  if (level == 0)
	       save_token(token_stack+i);
     }
     obstack_1grow(&text_stk, 0);
     return obstack_finish(&text_stk);
}

void
skip_to(int c)
{
     while (nexttoken()) {
	  if (tok.type == c)
	       break;
     }
}

int
skip_balanced(int open_tok, int close_tok, int level)
{
     if (level == 0) {
	  if (nexttoken() != open_tok) {
	       return 1;
	  }
	  level++;
     }
     while (nexttoken()) {
	  if (tok.type == LBRACE0 && open_tok == '{')
	       tok.type = '{';
	  else if (tok.type == RBRACE0 && close_tok == '}')
	       tok.type = '}';
	  
	  if (tok.type == open_tok) 
	       level++;
	  else if (tok.type == close_tok) {
	       if (--level == 0) {
		    nexttoken();
		    return 0;
	       }
	  } 
     }
     return -1;
}

int
yyparse()
{
     Ident identifier;

     level = 0;
     caller = NULL;
     clearstack();
     while (nexttoken()) {
	  identifier.storage = ExternStorage;
	  switch (tok.type) {
	  case 0:
	       return 0;
	  case QUALIFIER:
	       continue;
	  case TYPEDEF:
	       parse_typedef();
	       break;
	  case EXTERN:
	       identifier.storage = ExplicitExternStorage;
	       parse_declaration(&identifier, 0);
	       break;
	  case STATIC:
	       identifier.storage = StaticStorage;
	       nexttoken();
	       /* FALLTHRU */
	  default:
	       parse_declaration(&identifier, 0);
	       break;
	  }
	  cleanup_stack();
     }
     return 0;
}

static int
is_function()
{
     Stackpos sp;
     int res = 0;

     mark(sp);
     while (1) {
	  switch (tok.type) {
	  case QUALIFIER:
	  case TYPE:
	  case IDENTIFIER:
	  case MODIFIER:
	  case STATIC:
	  case EXTERN:
	  case STRUCT:
	  case UNION:
	  case ENUM:
	       nexttoken();
	       continue;
	  case PARM_WRAPPER:
	       if (skip_balanced('(', ')', 0) == -1)
		    file_error(_("unexpected end of file in declaration"),
			       NULL);
	       continue;
	  case '(':
	       res = nexttoken() != MODIFIER;
	       break;
	  default:
	       break;
	  }
	  break;
     }
     
     restore(sp);
     return res;
}

void
parse_declaration(Ident *ident, int parm)
{
     if (is_function()) 
	  parse_function_declaration(ident, parm);
     else
	  parse_variable_declaration(ident, parm);
     delete_parms(parm_level);
}


void
expression()
{
     char *name;
     int line;
     int parens_lev;

     parens_lev = 0;
     while (1) {
	  switch (tok.type) {
	  case ';':
	       return;
	  case LBRACE:
	  case LBRACE0:
	  case RBRACE:
	  case RBRACE0:
	       putback();
	       return;
	  case ',':
	       if (parens_lev == 0)
		    return;
	       break;
	  case 0:
	       if (verbose)
		    file_error(_("unexpected end of file in expression"),
			       NULL);
	       return;
	    
	  case IDENTIFIER:
	       name = tok.token;
	       line = tok.line;
	       nexttoken();
	       if (tok.type == '(') {
		    call(name, line);
		    parens_lev++;
	       } else {
		    reference(name, line);
		    if (tok.type == MEMBER_OF) {
			 while (tok.type == MEMBER_OF)
			      nexttoken();
		    } else {
			 putback();
		    }
	       }
	       break;
	  case '(':
	       /* maybe typecast */
	       if (nexttoken() == TYPE)
		    skip_to(')');
	       else {
		    putback();
		    parens_lev++;
	       }
	       break;
	  case ')':
	       parens_lev--;
	       break;
	  }
	  nexttoken();
     }
}

void
parse_function_declaration(Ident *ident, int parm)
{
     int error_recovery = 0;
     ident->type_end = -1;
     parse_knr_dcl(ident);

 restart:
     switch (tok.type) {
     case ')':
	  if (parm)
	       break;
	  /*FALLTHROUGH*/
     default:
	  if (error_recovery) 
	       nexttoken();
	  else {
	       if (verbose) 
		    file_error(_("expected `;'"), &tok);
	       error_recovery = 1;
	  }
	  goto restart;
	  
     case ';':
     case ',':
	  break;
     case LBRACE0:
     case LBRACE:
	  if (ident->name) {
	       caller = lookup(ident->name);
	       if (caller && caller->storage == AutoStorage)
		    caller = NULL;
	       func_body();
	  }
	  break;
     case 0:
	  if (verbose)
	       file_error(_("unexpected end of file in declaration"), NULL);
     }
}

int
fake_struct(Ident *ident)
{
     Stackpos sp;
     
     mark(sp);
     ident->type_end = -1;
     if (tok.type == STRUCT) {
	  if (nexttoken() == IDENTIFIER) {
	       ident->type_end = curs;
	  }
	  putback();
	  skip_struct();
	  if (tok.type == IDENTIFIER || tok.type == MODIFIER) {
	       int pos = curs-1;
	       restore(sp);
	       if (ident->type_end == -1) {
		    /* there was no tag. Insert { ... } */
		    tokdel(curs, pos - 1);
		    tokins(curs, IDENTIFIER, tok.line, "{ ... }");
		    debugtoken(&tok, "modified stack");
	       }
	  } else if (tok.type == '(')
	       return 0;
	  else if (tok.type != ';')
	       file_error(_("missing `;' after struct declaration"), &tok);
	  return 1;
     }
     return 0;
}

void
parse_variable_declaration(Ident *ident, int parm)
{
     Stackpos sp;
     
     mark(sp);
     ident->type_end = -1;
     if (tok.type == STRUCT || tok.type == UNION) {
	  if (nexttoken() == IDENTIFIER) {
	       ident->type_end = curs;
	  }
	  putback();
	  skip_struct();
	  while (tok.type == MODIFIER || tok.type == QUALIFIER)
	       nexttoken();
	  if (tok.type == IDENTIFIER) {
	       int pos = curs-1;
	       restore(sp);
	       if (ident->type_end == -1) {
		    /* there was no tag. Insert { ... } */
		    tokdel(curs, pos - 1);
		    tokins(curs, IDENTIFIER, tok.line, "{ ... }");
		    debugtoken(&tok, "modified stack");
	       }
	  } else {
	       if (tok.type == ';')
		    return;
	       restore(sp);
	  }
     }
 again:
     parse_dcl(ident, 0);
     
 select:    
     switch (tok.type) {
     case ')':
	  if (parm)
	       break;
	  /*FALLTHROUGH*/
     default:
	  if (verbose) 
	       file_error(_("expected `;'"), &tok);
	  /* FIXME: should putback() here */
	  /* FALLTHRU */
     case ';':
	  break;
     case ',':
	  if (parm)
	       break;
	  tos = ident->type_end;
	  restore(sp);
	  goto again;
     case '=':
	  nexttoken();
	  if (tok.type == LBRACE || tok.type == LBRACE0)
	       initializer_list();
	  else
	       expression();
	  goto select;
	  break;
     case LBRACE0:
     case LBRACE:
	  func_body();
	  break;
     case 0:
	  if (verbose)
	       file_error(_("unexpected end of file in declaration"), NULL);
     }
}

void
initializer_list()
{
     int lev = 0;
     while (1) {
	  switch (tok.type) {
	  case LBRACE:
	  case LBRACE0:
	       lev++;
	       break;
	  case RBRACE:
	  case RBRACE0:
	       if (--lev <= 0) {
		    nexttoken();
		    return;
	       }
	       break;
	  case 0:
	       file_error(_("unexpected end of file in initializer list"),
			  NULL);
	       return;
	  case ',':
	       break;
	  default:
	       expression();
	       break;
	  }
	  nexttoken();
     }
}

void
parse_knr_dcl(Ident *ident)
{
     ident->type_end = -1;
     parse_dcl(ident, !strict_ansi);     
}

void
skip_struct()
{
     if (nexttoken() == IDENTIFIER) {
	  nexttoken();
     } else if (tok.type == ';')
	  return;
     
     if (tok.type == LBRACE || tok.type == LBRACE0) {
	  if (skip_balanced('{', '}', 1) == -1) {
	       file_error(_("unexpected end of file in struct"), NULL);
	       return;
	  }
     }

     while (tok.type == PARM_WRAPPER) {
	  if (skip_balanced('(', ')', 0) == -1)
	      file_error(_("unexpected end of file in struct"), NULL);
     }
}

void
parse_typedef()
{
     Ident ident;
     
     ident.name = NULL;
     ident.type_end = -1;
     ident.parmcnt = -1;
     ident.line = -1;
     ident.storage = AnyStorage;
     
     nexttoken();
     if (!fake_struct(&ident))
	  putback();
     
     dcl(&ident);
     if (ident.name) 
	  declare_type(&ident);
}

void
parse_dcl(Ident *ident, int maybe_knr)
{
     ident->parmcnt = -1;
     ident->name = NULL;
     putback();
     dcl(ident);
     save_stack();
     if (ident->name)
	  declare(ident, maybe_knr);
     else 
	  undo_save_stack();
}

int
dcl(Ident *idptr)
{
     while (nexttoken() != 0 && tok.type != '(') {
	  if (tok.type == MODIFIER) {
	       if (idptr && idptr->type_end == -1)
		    idptr->type_end = curs-1;
	  } else if (tok.type == PARM_WRAPPER) {
	       if (skip_balanced('(', ')', 0) == -1) {
		    file_error(_("unexpected end of file in function declaration"),
			       NULL);
		    return 1;
	       }
	  } else if (tok.type == IDENTIFIER) {
	       int type;
	       
	       while (tok.type == IDENTIFIER)
		    nexttoken();
	       type = tok.type;
	       putback();
	       if (!(type == TYPE ||
		     type == MODIFIER || type == QUALIFIER))
		    break;
	  } else if (tok.type == ')' || tok.type == ';') {
	       return 1;
	  }
     }
     if (idptr && idptr->type_end == -1)
	  idptr->type_end = curs-1;
     return dirdcl(idptr);
}

int
dirdcl(Ident *idptr)
{
     int wrapper = 0;
     int *parm_ptr = NULL;
     
     if (tok.type == '(') {
	  dcl(idptr);
	  if (tok.type != ')' && verbose) {
	       file_error(_("expected `)'"), &tok);
	       return 1;
	  }
     } else if (tok.type == IDENTIFIER) {
	  if (idptr) {
	       idptr->name = tok.token;
	       idptr->line = tok.line;
	       parm_ptr = &idptr->parmcnt;
	  }
     }
     
     if (nexttoken() == PARM_WRAPPER) {
	  wrapper = 1;
	  nexttoken(); /* read '(' */
     } else
	  putback();

     while (nexttoken() == '[' || tok.type == '(') {
	  if (tok.type == '[') 
	       skip_to(']');
	  else {
	       maybe_parm_list(parm_ptr);
	       if (tok.type != ')' && verbose) {
		    file_error(_("expected `)'"), &tok);
		    return 1;
	       }
	  }
     }
     if (wrapper)
	  nexttoken(); /* read ')' */

     while (tok.type == PARM_WRAPPER) {
	  if (skip_balanced('(', ')', 0) == -1)
	       file_error(_("unexpected end of file in function declaration"),
			  NULL);
     }
     
     return 0;
}

int
parmdcl(Ident *idptr)
{
     int type;

     while (nexttoken() != 0 && tok.type != '(') {
	  if (tok.type == MODIFIER) {
	       if (idptr && idptr->type_end == -1)
		    idptr->type_end = curs-1;
	  } else if (tok.type == IDENTIFIER) {
	       while (tok.type == IDENTIFIER)
		    nexttoken();
	       type = tok.type;
	       putback();
	       if (type != MODIFIER) 
		    break;
	  } else if (tok.type == ')' || tok.type == ',') 
	       return 0;
     }
     if (idptr && idptr->type_end == -1)
	  idptr->type_end = curs-1;
     return dirdcl(idptr);
}


void
maybe_parm_list(int *parm_cnt_return)
{
     int parmcnt = 0;
     Ident ident;
     int level;

     parm_level++;
     while (nexttoken()) {
	  switch (tok.type) {
	  case ')':
	       if (parm_cnt_return)
		    *parm_cnt_return = parmcnt;
	       parm_level--;
	       return;
	  case ',':
	       break;
	  case QUALIFIER:
	  case IDENTIFIER:
	  case MODIFIER: /* unsigned * */
	  case STRUCT:
	  case UNION:
	  case TYPE:
	       parmcnt++;
	       ident.storage = AutoStorage;
	       parse_declaration(&ident, 1);
	       putback();
	       break;
	  default:
	       if (verbose)
		    file_error(_("unexpected token in parameter list"),
			       &tok);
	       level = 0;
	       do {
		    if (tok.type == '(') 
			 level++;
		    else if (tok.type == ')') {
			 if (level-- == 0)
			      break;
		    }
	       } while (nexttoken());
		    ;
	       putback();
	  }
     }
     if (verbose)
	  file_error(_("unexpected end of file in parameter list"), NULL);
}

void
func_body()
{
     Ident ident;
     
     level++;
     move_parms(level);
     while (level) {
	  cleanup_stack();
	  nexttoken();
	  switch (tok.type) {
	  default:
	       expression();
	       break;
	  case STATIC:
	       ident.storage = StaticStorage;
	       nexttoken();
	       parse_variable_declaration(&ident, 0);
	       break;
	  case TYPE:
	  case STRUCT:
	       ident.storage = AutoStorage;
	       parse_variable_declaration(&ident, 0);
	       break;
	  case EXTERN:
	       ident.storage = ExplicitExternStorage;
	       parse_declaration(&ident, 0);
	       break;
	  case LBRACE0:
	  case '{':
	       level++;
	       break;
	  case RBRACE0:
	       if (use_indentation) {
		    if (verbose && level != 1)
			 file_error(_("forced function body close"), NULL);
		    for ( ; level; level--) {
			 delete_autos(level);
		    }
		    break;
	       }
	       /* else: */
	       /* FALLTHRU */
	  case '}':
	       delete_autos(level);
	       level--;
	       break;
	  case 0:
	       if (verbose)
		    file_error(_("unexpected end of file in function body"),
			       NULL);
	       caller = NULL;
	       return;
	  }
     }
     caller = NULL;
}

int
get_knr_args(Ident *ident)
{
     int parmcnt, stop;
     Stackpos sp, new_sp;
     Ident id;

     switch (tok.type) {
     case IDENTIFIER:
     case TYPE:
     case STRUCT:
	  /* maybe K&R function definition */
	  
	  mark(sp);
	  parmcnt = 0;
	  
	  for (stop = 0; !stop && parmcnt < ident->parmcnt;
	       nexttoken()) {
	       id.type_end = -1;
	       switch (tok.type) {
	       case LBRACE:
	       case LBRACE0:
		    putback();
		    stop = 1;
		    break;
	       case TYPE:
	       case IDENTIFIER:
	       case STRUCT:
		    putback();
		    mark(new_sp);
		    if (dcl(&id) == 0) {
			 parmcnt++;
			 if (tok.type == ',') {
			      do {
				   tos = id.type_end; /* ouch! */
				   restore(new_sp);
				   dcl(&id);
			      } while (tok.type == ',');
			 } else if (tok.type != ';')
			      putback();
			 break;
		    }
		    /* else */
		    /* FALLTHRU */
	       default:
		    restore(sp);
		    return 1;
	       }
	  }
     }
     return 0;
}

void
declare(Ident *ident, int maybe_knr)
{
     Symbol *sp;
     
     if (ident->storage == AutoStorage) {
	  undo_save_stack();
	  sp = install_ident(ident->name, ident->storage);
	  if (parm_level) {
	       sp->level = parm_level;
	       sp->flag = symbol_parm;
	  } else
	       sp->level = level;
	  sp->arity = -1;
	  return;
     } 

     if ((ident->parmcnt >= 0
	  && (!maybe_knr || get_knr_args(ident) == 0)
	  && !(tok.type == LBRACE || tok.type == LBRACE0 || tok.type == TYPE
	       || tok.type == PARM_WRAPPER))
	 || (ident->parmcnt < 0 && ident->storage == ExplicitExternStorage)) {
	  undo_save_stack();
	  /* add_external()?? */
	  return;
     }
     
     sp = get_symbol(ident->name);
     if (sp->source) {
	  if (ident->storage == StaticStorage
	      && (sp->storage != StaticStorage || level > 0)) {
	       sp = install_ident(ident->name, ident->storage);
	  } else {
	       if (sp->arity >= 0)
		    error_at_line(0, 0, filename, ident->line, 
				  _("%s/%d redefined"),
				  ident->name, sp->arity);
	       else
		    error_at_line(0, 0, filename, ident->line, 
				  _("%s redefined"),
				  ident->name);
	       error_at_line(0, 0, sp->source, sp->def_line,
			     _("this is the place of previous definition"));
	  }
     }

     sp->type = SymIdentifier;
     sp->arity = ident->parmcnt;
     ident_change_storage(sp, 
			  (ident->storage == ExplicitExternStorage) ?
			  ExternStorage : ident->storage);
     sp->decl = finish_save_stack(ident->name);
     sp->source = filename;
     sp->def_line = ident->line;
     sp->level = level;
     if (debug)
	  fprintf(stderr, _("%s:%d: %s/%d defined to %s\n"),
		 filename,
		 line_num,
		 ident->name, ident->parmcnt,
		 sp->decl);
}

void
declare_type(Ident *ident)
{
     Symbol *sp;
     
     undo_save_stack();
     sp = lookup(ident->name);
     for ( ; sp; sp = sp->next)
	  if (sp->type == SymToken && sp->token_type == TYPE)
	       break;
     if (!sp)
	  sp = install(ident->name, INSTALL_UNIT_LOCAL);
     sp->type = SymToken;
     sp->token_type = TYPE;
     sp->source = filename;
     sp->def_line = ident->line;
     sp->ref_line = NULL;
     if (debug)
	  fprintf(stderr, _("%s:%d: type %s\n"), filename, line_num,
		  ident->name);
}

Symbol *
get_symbol(char *name)
{
     Symbol *sp = lookup(name) ;
     
     if (sp) {
	  for (; sp; sp = sp->next) {
	       if (sp->type == SymIdentifier && strcmp(sp->name, name) == 0)
		    break;
	  }
	  if (sp)
	       return sp;
     }
     return install_ident(name, ExternStorage);
}

Symbol *
add_reference(char *name, int line)
{
     Symbol *sp = get_symbol(name);
     Ref *refptr;

     if (sp->storage == AutoStorage
	 || (sp->storage == StaticStorage && globals_only()))
	  return NULL;
     refptr = xmalloc(sizeof(*refptr));
     refptr->source = filename;
     refptr->line = line;
     if (!sp->ref_line)
	  sp->ref_line = linked_list_create(free);
     linked_list_append(&sp->ref_line, refptr);
     return sp;
}


void
call(char *name, int line)
{
     Symbol *sp;

     sp = add_reference(name, line);
     if (!sp)
	  return;
     if (sp->arity < 0)
	  sp->arity = 0;
     if (caller) {
	  if (!data_in_list(caller, sp->caller))
	       linked_list_append(&sp->caller, caller);
	  if (!data_in_list(sp, caller->callee))
	       linked_list_append(&caller->callee, sp);
     }
}

void
reference(char *name, int line)
{
     Symbol *sp = add_reference(name, line);
     if (!sp)
	  return;
     if (caller) {
	  if (!data_in_list(caller, sp->caller))
	       linked_list_append(&sp->caller, caller);
	  if (!data_in_list(sp, caller->callee))
	       linked_list_append(&caller->callee, sp);
     }
}

Return to:

Send suggestions and report system problems to the System administrator.