aboutsummaryrefslogtreecommitdiff
path: root/src/parser.c
blob: 30bc9c972ab4c9c2d3862f837676abba8006dbf8 (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
/* This file is part of GNU cflow
   Copyright (C) 1997, 2005, 2006 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 2 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 GNU cflow; if not, write to the Free Software
   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
   MA 02110-1301 USA */

#include <cflow.h>
#include <parser.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);
Symbol *install_ident(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:
	  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 OP:
	  fprintf(stderr, "OP"); /* ouch!!! */
	  break;
     default:
	  fprintf(stderr, "`%c'", tokptr->type);
     }
}

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

void
mark(Stackpos pos)
{
     pos[0] = curs;
}

void
restore(Stackpos pos)
{
     curs = pos[0];
     if (curs)
	  tok = token_stack[curs-1];
}

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) 
	  memmove(token_stack, token_stack+curs, delta*sizeof(token_stack[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);
     }
     tok = token_stack[curs];
     curs++;
     return tok.type;
}

int
putback()
{
     if (curs == 0)
	  error(10, 0, _("INTERNAL ERROR: cannot return token to stream"));
     curs--;
     if (curs > 0) {
	  tok.type = token_stack[curs-1].type;
	  tok.token = token_stack[curs-1].token;
     } else
	  tok.type = 0;
     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:
	  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 '(':
	  if (need_space) 
	       obstack_1grow(&text_stk, ' ');
	  /*FALLTHRU*/
     default:
	  obstack_1grow(&text_stk, tokptr->type);
	  need_space = 0;
     }
}

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
yyparse()
{
     Ident identifier;

     level = 0;
     caller = NULL;
     clearstack();
     while (nexttoken()) {
	  identifier.storage = ExternStorage;
	  switch (tok.type) {
	  case 0:
	       return 0;
	  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();
     }
     /*NOTREACHED*/
}

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

     mark(sp);
/*    if (tok.type == STRUCT)
	nexttoken();*/
     while (tok.type == TYPE ||
	    tok.type == IDENTIFIER ||
	    tok.type == MODIFIER ||
	    tok.type == STATIC ||
	    tok.type == EXTERN)
	  nexttoken();
     
     if (tok.type == '(') 
	  res = nexttoken() != MODIFIER;
     
     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"), 0);
	       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 `;'"), 1);
	       error_recovery = 1;
	  }
	  goto restart;
	  
     case ';':
     case ',':
	  break;
     case LBRACE0:
     case LBRACE:
	  if (ident->name) {
	       caller = lookup(ident->name);
	       func_body();
	  }
	  break;
     case 0:
	  if (verbose)
	       file_error(_("unexpected end of file in declaration"), 0);
     }
}

int
fake_struct(Ident *ident)
{
     Stackpos sp;
     
     mark(sp);
     ident->type_end = -1;
     if (tok.type == STRUCT) {
	  if (nexttoken() == IDENTIFIER) {
	       ident->type_end = tos;
	  }
	  putback();
	  skip_struct();
	  if (tok.type == IDENTIFIER || tok.type == MODIFIER) {
	       TOKSTK hold = tok;
	       restore(sp);
	       if (ident->type_end == -1) {
		    /* there was no tag. Insert { ... } */
		    tos = curs;
		    token_stack[curs].type = IDENTIFIER;
		    token_stack[curs].token = "{ ... }";
		    tos++;
	       } else {
		    tos = curs + 1;
	       }
	       tokpush(hold.type, hold.line, hold.token);
	  } else {
	       if (tok.type != ';')
		    file_error(_("missing `;' after struct declaration"), 0);
	  }
	  return 1;
     }
     return 0;
}

void
parse_variable_declaration(Ident *ident, int parm)
{
     Stackpos sp;
     
     mark(sp);
     ident->type_end = -1;
     if (tok.type == STRUCT) {
	  if (nexttoken() == IDENTIFIER) {
	       ident->type_end = tos;
	  }
	  putback();
	  skip_struct();
	  if (tok.type == IDENTIFIER) {
	       TOKSTK hold = tok;
	       restore(sp);
	       if (ident->type_end == -1) {
		    /* there was no tag. Insert { ... } */
		    tos = curs;
		    token_stack[curs].type = IDENTIFIER;
		    token_stack[curs].token = "{ ... }";
		    tos++;
	       } else {
		    tos = curs + 1;
	       }
	       tokpush(hold.type, hold.line, hold.token);
	  } 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 `;'"), 1);
	  /* 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"), 0);
     }
}

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"), 0);
	       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()
{
     int lev = 0;
     
     if (nexttoken() == IDENTIFIER) {
	  nexttoken();
     } else if (tok.type == ';')
	  return;
     
     if (tok.type == LBRACE || tok.type == LBRACE0) {
	  do {
	       switch (tok.type) {
	       case 0:
		    file_error(_("unexpected end of file in struct"), 0);
		    return;
	       case LBRACE:
	       case LBRACE0:
		    lev++;
		    break;
	       case RBRACE:
	       case RBRACE0:
		    lev--;
	       }
	       nexttoken();
	  } while (lev);
     }
}

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)
{
     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 == TYPE)
		    continue;
	       else if (type != MODIFIER) 
		    break;
	  } else if (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 `)'"), 1);
	       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 `)'"), 1);
		    return 1;
	       }
	  }
     }
     if (wrapper)
	  nexttoken(); /* read ')' */

     if (tok.type == PARM_WRAPPER) {
	  if (nexttoken() == '(') {
	       int level = 0;
	       while (nexttoken()) {
		    if (tok.type == 0) {
			 file_error(_("unexpected end of file in function declaration"),
				    0);
			 return 1;
		    } else if (tok.type == '(') 
			 level++;
		    else if (tok.type == ')') {
			 if (level-- == 0) {
			      nexttoken();
			      break;
			 }
		    } 
	       }
	  } else
	       putback();
     }
     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 IDENTIFIER:
	  case STRUCT:
	  case UNION:
	  case ENUM:
	  case TYPE:
	       parmcnt++;
	       ident.storage = AutoStorage;
	       parse_declaration(&ident, 1);
	       putback();
	       break;
	  default:
	       if (verbose)
		    file_error(_("unexpected token in parameter list"), 1);
	       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"), 0);
}

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"), 0);
		    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"), 0);
	       return;
	  }
     }
}

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->name);
	  sp->type = SymIdentifier;
	  sp->storage = 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))
	 || (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);
	  } else {
	       error_at_line(0, 0, filename, ident->line, 
			     _("%s/%d redefined"),
			     ident->name, sp->arity);
	       error_at_line(0, 0, sp->source, sp->def_line,
			     _("this is the place of previous definition"));
	  }
     }

     sp->type = SymIdentifier;
     sp->arity = ident->parmcnt;
     sp->storage = (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)
	  printf(_("%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);
     sp->type = SymToken;
     sp->token_type = TYPE;
     sp->source = filename;
     sp->def_line = ident->line;
     sp->ref_line = NULL;
     if (debug)
	  printf(_("%s:%d: type %s\n"),
		 filename,
		 line_num,
		 ident->name);
}

Symbol *
install_ident(char *name)
{
     Symbol *sp;

     sp = install(name);
     sp->type = SymIdentifier;
     sp->arity = -1;
     sp->storage = ExternStorage;
     sp->decl = NULL;
     sp->source = NULL;
     sp->def_line = -1;
     sp->ref_line = NULL;
     sp->caller = sp->callee = NULL;
     sp->level = -1;
     return sp;
}

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

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;
     append_to_list(&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 (!symbol_in_list(caller, sp->caller))
	       append_to_list(&sp->caller, caller);
	  if (!symbol_in_list(sp, caller->callee))
	       append_to_list(&caller->callee, sp);
     }
}

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

Return to:

Send suggestions and report system problems to the System administrator.