[ChangeLog]
[bpt/emacs.git] / lib-src / ebrowse.c
1 /* ebrowse.c --- parsing files for the ebrowse C++ browser
2
3 Copyright (C) 1992-2011 Free Software Foundation, Inc.
4
5 This file is part of GNU Emacs.
6
7 GNU Emacs is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
19
20
21 #include <config.h>
22 #include <stdio.h>
23
24 #ifdef HAVE_STDLIB_H
25 #include <stdlib.h>
26 #endif
27
28 #include <string.h>
29 #include <ctype.h>
30 #include <assert.h>
31 #include <getopt.h>
32
33 /* The SunOS compiler doesn't have SEEK_END. */
34 #ifndef SEEK_END
35 #define SEEK_END 2
36 #endif
37
38 /* Conditionalize function prototypes. */
39
40 /* Value is non-zero if strings X and Y compare equal. */
41
42 #define streq(X, Y) (*(X) == *(Y) && strcmp ((X) + 1, (Y) + 1) == 0)
43
44 #include <min-max.h>
45
46 /* Files are read in chunks of this number of bytes. */
47
48 #define READ_CHUNK_SIZE (100 * 1024)
49
50 /* The character used as a separator in path lists (like $PATH). */
51
52 #if defined(__MSDOS__)
53 #define PATH_LIST_SEPARATOR ';'
54 #define FILENAME_EQ(X,Y) (strcasecmp(X,Y) == 0)
55 #else
56 #if defined(WINDOWSNT)
57 #define PATH_LIST_SEPARATOR ';'
58 #define FILENAME_EQ(X,Y) (stricmp(X,Y) == 0)
59 #else
60 #define PATH_LIST_SEPARATOR ':'
61 #define FILENAME_EQ(X,Y) (streq(X,Y))
62 #endif
63 #endif
64 /* The default output file name. */
65
66 #define DEFAULT_OUTFILE "BROWSE"
67
68 /* A version string written to the output file. Change this whenever
69 the structure of the output file changes. */
70
71 #define EBROWSE_FILE_VERSION "ebrowse 5.0"
72
73 /* The output file consists of a tree of Lisp objects, with major
74 nodes built out of Lisp structures. These are the heads of the
75 Lisp structs with symbols identifying their type. */
76
77 #define TREE_HEADER_STRUCT "[ebrowse-hs "
78 #define TREE_STRUCT "[ebrowse-ts "
79 #define MEMBER_STRUCT "[ebrowse-ms "
80 #define BROWSE_STRUCT "[ebrowse-bs "
81 #define CLASS_STRUCT "[ebrowse-cs "
82
83 /* The name of the symbol table entry for global functions, variables,
84 defines etc. This name also appears in the browser display. */
85
86 #define GLOBALS_NAME "*Globals*"
87
88 /* Token definitions. */
89
90 enum token
91 {
92 YYEOF = 0, /* end of file */
93 CSTRING = 256, /* string constant */
94 CCHAR, /* character constant */
95 CINT, /* integral constant */
96 CFLOAT, /* real constant */
97
98 ELLIPSIS, /* ... */
99 LSHIFTASGN, /* <<= */
100 RSHIFTASGN, /* >>= */
101 ARROWSTAR, /* ->* */
102 IDENT, /* identifier */
103 DIVASGN, /* /= */
104 INC, /* ++ */
105 ADDASGN, /* += */
106 DEC, /* -- */
107 ARROW, /* -> */
108 SUBASGN, /* -= */
109 MULASGN, /* *= */
110 MODASGN, /* %= */
111 LOR, /* || */
112 ORASGN, /* |= */
113 LAND, /* && */
114 ANDASGN, /* &= */
115 XORASGN, /* ^= */
116 POINTSTAR, /* .* */
117 DCOLON, /* :: */
118 EQ, /* == */
119 NE, /* != */
120 LE, /* <= */
121 LSHIFT, /* << */
122 GE, /* >= */
123 RSHIFT, /* >> */
124
125 /* Keywords. The undef's are there because these
126 three symbols are very likely to be defined somewhere. */
127 #undef BOOL
128 #undef TRUE
129 #undef FALSE
130
131 ASM, /* asm */
132 AUTO, /* auto */
133 BREAK, /* break */
134 CASE, /* case */
135 CATCH, /* catch */
136 CHAR, /* char */
137 CLASS, /* class */
138 CONST, /* const */
139 CONTINUE, /* continue */
140 DEFAULT, /* default */
141 DELETE, /* delete */
142 DO, /* do */
143 DOUBLE, /* double */
144 ELSE, /* else */
145 ENUM, /* enum */
146 EXTERN, /* extern */
147 FLOAT, /* float */
148 FOR, /* for */
149 FRIEND, /* friend */
150 GOTO, /* goto */
151 IF, /* if */
152 T_INLINE, /* inline */
153 INT, /* int */
154 LONG, /* long */
155 NEW, /* new */
156 OPERATOR, /* operator */
157 PRIVATE, /* private */
158 PROTECTED, /* protected */
159 PUBLIC, /* public */
160 REGISTER, /* register */
161 RETURN, /* return */
162 SHORT, /* short */
163 SIGNED, /* signed */
164 SIZEOF, /* sizeof */
165 STATIC, /* static */
166 STRUCT, /* struct */
167 SWITCH, /* switch */
168 TEMPLATE, /* template */
169 THIS, /* this */
170 THROW, /* throw */
171 TRY, /* try */
172 TYPEDEF, /* typedef */
173 UNION, /* union */
174 UNSIGNED, /* unsigned */
175 VIRTUAL, /* virtual */
176 VOID, /* void */
177 VOLATILE, /* volatile */
178 WHILE, /* while */
179 MUTABLE, /* mutable */
180 BOOL, /* bool */
181 TRUE, /* true */
182 FALSE, /* false */
183 SIGNATURE, /* signature (GNU extension) */
184 NAMESPACE, /* namespace */
185 EXPLICIT, /* explicit */
186 TYPENAME, /* typename */
187 CONST_CAST, /* const_cast */
188 DYNAMIC_CAST, /* dynamic_cast */
189 REINTERPRET_CAST, /* reinterpret_cast */
190 STATIC_CAST, /* static_cast */
191 TYPEID, /* typeid */
192 USING, /* using */
193 WCHAR /* wchar_t */
194 };
195
196 /* Storage classes, in a wider sense. */
197
198 enum sc
199 {
200 SC_UNKNOWN,
201 SC_MEMBER, /* Is an instance member. */
202 SC_STATIC, /* Is static member. */
203 SC_FRIEND, /* Is friend function. */
204 SC_TYPE /* Is a type definition. */
205 };
206
207 /* Member visibility. */
208
209 enum visibility
210 {
211 V_PUBLIC,
212 V_PROTECTED,
213 V_PRIVATE
214 };
215
216 /* Member flags. */
217
218 #define F_VIRTUAL 1 /* Is virtual function. */
219 #define F_INLINE 2 /* Is inline function. */
220 #define F_CONST 4 /* Is const. */
221 #define F_PURE 8 /* Is pure virtual function. */
222 #define F_MUTABLE 16 /* Is mutable. */
223 #define F_TEMPLATE 32 /* Is a template. */
224 #define F_EXPLICIT 64 /* Is explicit constructor. */
225 #define F_THROW 128 /* Has a throw specification. */
226 #define F_EXTERNC 256 /* Is declared extern "C". */
227 #define F_DEFINE 512 /* Is a #define. */
228
229 /* Two macros to set and test a bit in an int. */
230
231 #define SET_FLAG(F, FLAG) ((F) |= (FLAG))
232 #define HAS_FLAG(F, FLAG) (((F) & (FLAG)) != 0)
233
234 /* Structure describing a class member. */
235
236 struct member
237 {
238 struct member *next; /* Next in list of members. */
239 struct member *anext; /* Collision chain in member_table. */
240 struct member **list; /* Pointer to list in class. */
241 unsigned param_hash; /* Hash value for parameter types. */
242 int vis; /* Visibility (public, ...). */
243 int flags; /* See F_* above. */
244 char *regexp; /* Matching regular expression. */
245 const char *filename; /* Don't free this shared string. */
246 int pos; /* Buffer position of occurrence. */
247 char *def_regexp; /* Regular expression matching definition. */
248 const char *def_filename; /* File name of definition. */
249 int def_pos; /* Buffer position of definition. */
250 char name[1]; /* Member name. */
251 };
252
253 /* Structures of this type are used to connect class structures with
254 their super and subclasses. */
255
256 struct link
257 {
258 struct sym *sym; /* The super or subclass. */
259 struct link *next; /* Next in list or NULL. */
260 };
261
262 /* Structure used to record namespace aliases. */
263
264 struct alias
265 {
266 struct alias *next; /* Next in list. */
267 struct sym *namesp; /* Namespace in which defined. */
268 struct link *aliasee; /* List of aliased namespaces (A::B::C...). */
269 char name[1]; /* Alias name. */
270 };
271
272 /* The structure used to describe a class in the symbol table,
273 or a namespace in all_namespaces. */
274
275 struct sym
276 {
277 int flags; /* Is class a template class?. */
278 unsigned char visited; /* Used to find circles. */
279 struct sym *next; /* Hash collision list. */
280 struct link *subs; /* List of subclasses. */
281 struct link *supers; /* List of superclasses. */
282 struct member *vars; /* List of instance variables. */
283 struct member *fns; /* List of instance functions. */
284 struct member *static_vars; /* List of static variables. */
285 struct member *static_fns; /* List of static functions. */
286 struct member *friends; /* List of friend functions. */
287 struct member *types; /* List of local types. */
288 char *regexp; /* Matching regular expression. */
289 int pos; /* Buffer position. */
290 const char *filename; /* File in which it can be found. */
291 const char *sfilename; /* File in which members can be found. */
292 struct sym *namesp; /* Namespace in which defined. . */
293 char name[1]; /* Name of the class. */
294 };
295
296 /* Experimental: Print info for `--position-info'. We print
297 '(CLASS-NAME SCOPE MEMBER-NAME). */
298
299 #define P_DEFN 1
300 #define P_DECL 2
301
302 int info_where;
303 struct sym *info_cls = NULL;
304 struct member *info_member = NULL;
305
306 /* Experimental. For option `--position-info', the buffer position we
307 are interested in. When this position is reached, print out
308 information about what we know about that point. */
309
310 int info_position = -1;
311
312 /* Command line options structure for getopt_long. */
313
314 struct option options[] =
315 {
316 {"append", no_argument, NULL, 'a'},
317 {"files", required_argument, NULL, 'f'},
318 {"help", no_argument, NULL, -2},
319 {"min-regexp-length", required_argument, NULL, 'm'},
320 {"max-regexp-length", required_argument, NULL, 'M'},
321 {"no-nested-classes", no_argument, NULL, 'n'},
322 {"no-regexps", no_argument, NULL, 'x'},
323 {"no-structs-or-unions", no_argument, NULL, 's'},
324 {"output-file", required_argument, NULL, 'o'},
325 {"position-info", required_argument, NULL, 'p'},
326 {"search-path", required_argument, NULL, 'I'},
327 {"verbose", no_argument, NULL, 'v'},
328 {"version", no_argument, NULL, -3},
329 {"very-verbose", no_argument, NULL, 'V'},
330 {NULL, 0, NULL, 0}
331 };
332
333 /* Semantic values of tokens. Set by yylex.. */
334
335 unsigned yyival; /* Set for token CINT. */
336 char *yytext; /* Set for token IDENT. */
337 char *yytext_end;
338
339 /* Output file. */
340
341 FILE *yyout;
342
343 /* Current line number. */
344
345 int yyline;
346
347 /* The name of the current input file. */
348
349 const char *filename;
350
351 /* Three character class vectors, and macros to test membership
352 of characters. */
353
354 char is_ident[255];
355 char is_digit[255];
356 char is_white[255];
357
358 #define IDENTP(C) is_ident[(unsigned char) (C)]
359 #define DIGITP(C) is_digit[(unsigned char) (C)]
360 #define WHITEP(C) is_white[(unsigned char) (C)]
361
362 /* Command line flags. */
363
364 int f_append;
365 int f_verbose;
366 int f_very_verbose;
367 int f_structs = 1;
368 int f_regexps = 1;
369 int f_nested_classes = 1;
370
371 /* Maximum and minimum lengths of regular expressions matching a
372 member, class etc., for writing them to the output file. These are
373 overridable from the command line. */
374
375 int min_regexp = 5;
376 int max_regexp = 50;
377
378 /* Input buffer. */
379
380 char *inbuffer;
381 char *in;
382 int inbuffer_size;
383
384 /* Return the current buffer position in the input file. */
385
386 #define BUFFER_POS() (in - inbuffer)
387
388 /* If current lookahead is CSTRING, the following points to the
389 first character in the string constant. Used for recognizing
390 extern "C". */
391
392 char *string_start;
393
394 /* The size of the hash tables for classes.and members. Should be
395 prime. */
396
397 #define TABLE_SIZE 1001
398
399 /* The hash table for class symbols. */
400
401 struct sym *class_table[TABLE_SIZE];
402
403 /* Hash table containing all member structures. This is generally
404 faster for member lookup than traversing the member lists of a
405 `struct sym'. */
406
407 struct member *member_table[TABLE_SIZE];
408
409 /* Hash table for namespace aliases */
410
411 struct alias *namespace_alias_table[TABLE_SIZE];
412
413 /* The special class symbol used to hold global functions,
414 variables etc. */
415
416 struct sym *global_symbols;
417
418 /* The current namespace. */
419
420 struct sym *current_namespace;
421
422 /* The list of all known namespaces. */
423
424 struct sym *all_namespaces;
425
426 /* Stack of namespaces we're currently nested in, during the parse. */
427
428 struct sym **namespace_stack;
429 int namespace_stack_size;
430 int namespace_sp;
431
432 /* The current lookahead token. */
433
434 int tk = -1;
435
436 /* Structure describing a keyword. */
437
438 struct kw
439 {
440 const char *name; /* Spelling. */
441 int tk; /* Token value. */
442 struct kw *next; /* Next in collision chain. */
443 };
444
445 /* Keywords are lookup up in a hash table of their own. */
446
447 #define KEYWORD_TABLE_SIZE 1001
448 struct kw *keyword_table[KEYWORD_TABLE_SIZE];
449
450 /* Search path. */
451
452 struct search_path
453 {
454 char *path;
455 struct search_path *next;
456 };
457
458 struct search_path *search_path;
459 struct search_path *search_path_tail;
460
461 /* Function prototypes. */
462
463 static char *matching_regexp (void);
464 static struct sym *add_sym (const char *, struct sym *);
465 static void add_global_defn (char *, char *, int, unsigned, int, int, int);
466 static void add_global_decl (char *, char *, int, unsigned, int, int, int);
467 static struct member *add_member (struct sym *, char *, int, int, unsigned);
468 static void class_definition (struct sym *, int, int, int);
469 static char *operator_name (int *);
470 static void parse_qualified_param_ident_or_type (char **);
471 static void usage (int) NO_RETURN;
472 static void version (void) NO_RETURN;
473
474
475 \f
476 /***********************************************************************
477 Utilities
478 ***********************************************************************/
479
480 /* Print an error in a printf-like style with the current input file
481 name and line number. */
482
483 static void
484 yyerror (const char *format, const char *s)
485 {
486 fprintf (stderr, "%s:%d: ", filename, yyline);
487 fprintf (stderr, format, s);
488 putc ('\n', stderr);
489 }
490
491
492 /* Like malloc but print an error and exit if not enough memory is
493 available. */
494
495 static void *
496 xmalloc (int nbytes)
497 {
498 void *p = malloc (nbytes);
499 if (p == NULL)
500 {
501 yyerror ("out of memory", NULL);
502 exit (EXIT_FAILURE);
503 }
504 return p;
505 }
506
507
508 /* Like realloc but print an error and exit if out of memory. */
509
510 static void *
511 xrealloc (void *p, int sz)
512 {
513 p = realloc (p, sz);
514 if (p == NULL)
515 {
516 yyerror ("out of memory", NULL);
517 exit (EXIT_FAILURE);
518 }
519 return p;
520 }
521
522
523 /* Like strdup, but print an error and exit if not enough memory is
524 available.. If S is null, return null. */
525
526 static char *
527 xstrdup (char *s)
528 {
529 if (s)
530 s = strcpy (xmalloc (strlen (s) + 1), s);
531 return s;
532 }
533
534
535 \f
536 /***********************************************************************
537 Symbols
538 ***********************************************************************/
539
540 /* Initialize the symbol table. This currently only sets up the
541 special symbol for globals (`*Globals*'). */
542
543 static void
544 init_sym (void)
545 {
546 global_symbols = add_sym (GLOBALS_NAME, NULL);
547 }
548
549
550 /* Add a symbol for class NAME to the symbol table. NESTED_IN_CLASS
551 is the class in which class NAME was found. If it is null,
552 this means the scope of NAME is the current namespace.
553
554 If a symbol for NAME already exists, return that. Otherwise
555 create a new symbol and set it to default values. */
556
557 static struct sym *
558 add_sym (const char *name, struct sym *nested_in_class)
559 {
560 struct sym *sym;
561 unsigned h;
562 const char *s;
563 struct sym *scope = nested_in_class ? nested_in_class : current_namespace;
564
565 for (s = name, h = 0; *s; ++s)
566 h = (h << 1) ^ *s;
567 h %= TABLE_SIZE;
568
569 for (sym = class_table[h]; sym; sym = sym->next)
570 if (streq (name, sym->name)
571 && ((!sym->namesp && !scope)
572 || (sym->namesp && scope
573 && streq (sym->namesp->name, scope->name))))
574 break;
575
576 if (sym == NULL)
577 {
578 if (f_very_verbose)
579 {
580 putchar ('\t');
581 puts (name);
582 }
583
584 sym = (struct sym *) xmalloc (sizeof *sym + strlen (name));
585 memset (sym, 0, sizeof *sym);
586 strcpy (sym->name, name);
587 sym->namesp = scope;
588 sym->next = class_table[h];
589 class_table[h] = sym;
590 }
591
592 return sym;
593 }
594
595
596 /* Add links between superclass SUPER and subclass SUB. */
597
598 static void
599 add_link (struct sym *super, struct sym *sub)
600 {
601 struct link *lnk, *lnk2, *p, *prev;
602
603 /* See if a link already exists. */
604 for (p = super->subs, prev = NULL;
605 p && strcmp (sub->name, p->sym->name) > 0;
606 prev = p, p = p->next)
607 ;
608
609 /* Avoid duplicates. */
610 if (p == NULL || p->sym != sub)
611 {
612 lnk = (struct link *) xmalloc (sizeof *lnk);
613 lnk2 = (struct link *) xmalloc (sizeof *lnk2);
614
615 lnk->sym = sub;
616 lnk->next = p;
617
618 if (prev)
619 prev->next = lnk;
620 else
621 super->subs = lnk;
622
623 lnk2->sym = super;
624 lnk2->next = sub->supers;
625 sub->supers = lnk2;
626 }
627 }
628
629
630 /* Find in class CLS member NAME.
631
632 VAR non-zero means look for a member variable; otherwise a function
633 is searched. SC specifies what kind of member is searched---a
634 static, or per-instance member etc. HASH is a hash code for the
635 parameter types of functions. Value is a pointer to the member
636 found or null if not found. */
637
638 static struct member *
639 find_member (struct sym *cls, char *name, int var, int sc, unsigned int hash)
640 {
641 struct member **list;
642 struct member *p;
643 unsigned name_hash = 0;
644 char *s;
645 int i;
646
647 switch (sc)
648 {
649 case SC_FRIEND:
650 list = &cls->friends;
651 break;
652
653 case SC_TYPE:
654 list = &cls->types;
655 break;
656
657 case SC_STATIC:
658 list = var ? &cls->static_vars : &cls->static_fns;
659 break;
660
661 default:
662 list = var ? &cls->vars : &cls->fns;
663 break;
664 }
665
666 for (s = name; *s; ++s)
667 name_hash = (name_hash << 1) ^ *s;
668 i = name_hash % TABLE_SIZE;
669
670 for (p = member_table[i]; p; p = p->anext)
671 if (p->list == list && p->param_hash == hash && streq (name, p->name))
672 break;
673
674 return p;
675 }
676
677
678 /* Add to class CLS information for the declaration of member NAME.
679 REGEXP is a regexp matching the declaration, if non-null. POS is
680 the position in the source where the declaration is found. HASH is
681 a hash code for the parameter list of the member, if it's a
682 function. VAR non-zero means member is a variable or type. SC
683 specifies the type of member (instance member, static, ...). VIS
684 is the member's visibility (public, protected, private). FLAGS is
685 a bit set giving additional information about the member (see the
686 F_* defines). */
687
688 static void
689 add_member_decl (struct sym *cls, char *name, char *regexp, int pos, unsigned int hash, int var, int sc, int vis, int flags)
690 {
691 struct member *m;
692
693 m = find_member (cls, name, var, sc, hash);
694 if (m == NULL)
695 m = add_member (cls, name, var, sc, hash);
696
697 /* Have we seen a new filename? If so record that. */
698 if (!cls->filename || !FILENAME_EQ (cls->filename, filename))
699 m->filename = filename;
700
701 m->regexp = regexp;
702 m->pos = pos;
703 m->flags = flags;
704
705 switch (vis)
706 {
707 case PRIVATE:
708 m->vis = V_PRIVATE;
709 break;
710
711 case PROTECTED:
712 m->vis = V_PROTECTED;
713 break;
714
715 case PUBLIC:
716 m->vis = V_PUBLIC;
717 break;
718 }
719
720 info_where = P_DECL;
721 info_cls = cls;
722 info_member = m;
723 }
724
725
726 /* Add to class CLS information for the definition of member NAME.
727 REGEXP is a regexp matching the declaration, if non-null. POS is
728 the position in the source where the declaration is found. HASH is
729 a hash code for the parameter list of the member, if it's a
730 function. VAR non-zero means member is a variable or type. SC
731 specifies the type of member (instance member, static, ...). VIS
732 is the member's visibility (public, protected, private). FLAGS is
733 a bit set giving additional information about the member (see the
734 F_* defines). */
735
736 static void
737 add_member_defn (struct sym *cls, char *name, char *regexp, int pos, unsigned int hash, int var, int sc, int flags)
738 {
739 struct member *m;
740
741 if (sc == SC_UNKNOWN)
742 {
743 m = find_member (cls, name, var, SC_MEMBER, hash);
744 if (m == NULL)
745 {
746 m = find_member (cls, name, var, SC_STATIC, hash);
747 if (m == NULL)
748 m = add_member (cls, name, var, sc, hash);
749 }
750 }
751 else
752 {
753 m = find_member (cls, name, var, sc, hash);
754 if (m == NULL)
755 m = add_member (cls, name, var, sc, hash);
756 }
757
758 if (!cls->sfilename)
759 cls->sfilename = filename;
760
761 if (!FILENAME_EQ (cls->sfilename, filename))
762 m->def_filename = filename;
763
764 m->def_regexp = regexp;
765 m->def_pos = pos;
766 m->flags |= flags;
767
768 info_where = P_DEFN;
769 info_cls = cls;
770 info_member = m;
771 }
772
773
774 /* Add a symbol for a define named NAME to the symbol table.
775 REGEXP is a regular expression matching the define in the source,
776 if it is non-null. POS is the position in the file. */
777
778 static void
779 add_define (char *name, char *regexp, int pos)
780 {
781 add_global_defn (name, regexp, pos, 0, 1, SC_FRIEND, F_DEFINE);
782 add_global_decl (name, regexp, pos, 0, 1, SC_FRIEND, F_DEFINE);
783 }
784
785
786 /* Add information for the global definition of NAME.
787 REGEXP is a regexp matching the declaration, if non-null. POS is
788 the position in the source where the declaration is found. HASH is
789 a hash code for the parameter list of the member, if it's a
790 function. VAR non-zero means member is a variable or type. SC
791 specifies the type of member (instance member, static, ...). VIS
792 is the member's visibility (public, protected, private). FLAGS is
793 a bit set giving additional information about the member (see the
794 F_* defines). */
795
796 static void
797 add_global_defn (char *name, char *regexp, int pos, unsigned int hash, int var, int sc, int flags)
798 {
799 int i;
800 struct sym *sym;
801
802 /* Try to find out for which classes a function is a friend, and add
803 what we know about it to them. */
804 if (!var)
805 for (i = 0; i < TABLE_SIZE; ++i)
806 for (sym = class_table[i]; sym; sym = sym->next)
807 if (sym != global_symbols && sym->friends)
808 if (find_member (sym, name, 0, SC_FRIEND, hash))
809 add_member_defn (sym, name, regexp, pos, hash, 0,
810 SC_FRIEND, flags);
811
812 /* Add to global symbols. */
813 add_member_defn (global_symbols, name, regexp, pos, hash, var, sc, flags);
814 }
815
816
817 /* Add information for the global declaration of NAME.
818 REGEXP is a regexp matching the declaration, if non-null. POS is
819 the position in the source where the declaration is found. HASH is
820 a hash code for the parameter list of the member, if it's a
821 function. VAR non-zero means member is a variable or type. SC
822 specifies the type of member (instance member, static, ...). VIS
823 is the member's visibility (public, protected, private). FLAGS is
824 a bit set giving additional information about the member (see the
825 F_* defines). */
826
827 static void
828 add_global_decl (char *name, char *regexp, int pos, unsigned int hash, int var, int sc, int flags)
829 {
830 /* Add declaration only if not already declared. Header files must
831 be processed before source files for this to have the right effect.
832 I do not want to handle implicit declarations at the moment. */
833 struct member *m;
834 struct member *found;
835
836 m = found = find_member (global_symbols, name, var, sc, hash);
837 if (m == NULL)
838 m = add_member (global_symbols, name, var, sc, hash);
839
840 /* Definition already seen => probably last declaration implicit.
841 Override. This means that declarations must always be added to
842 the symbol table before definitions. */
843 if (!found)
844 {
845 if (!global_symbols->filename
846 || !FILENAME_EQ (global_symbols->filename, filename))
847 m->filename = filename;
848
849 m->regexp = regexp;
850 m->pos = pos;
851 m->vis = V_PUBLIC;
852 m->flags = flags;
853
854 info_where = P_DECL;
855 info_cls = global_symbols;
856 info_member = m;
857 }
858 }
859
860
861 /* Add a symbol for member NAME to class CLS.
862 VAR non-zero means it's a variable. SC specifies the kind of
863 member. HASH is a hash code for the parameter types of a function.
864 Value is a pointer to the member's structure. */
865
866 static struct member *
867 add_member (struct sym *cls, char *name, int var, int sc, unsigned int hash)
868 {
869 struct member *m = (struct member *) xmalloc (sizeof *m + strlen (name));
870 struct member **list;
871 struct member *p;
872 struct member *prev;
873 unsigned name_hash = 0;
874 int i;
875 char *s;
876
877 strcpy (m->name, name);
878 m->param_hash = hash;
879
880 m->vis = 0;
881 m->flags = 0;
882 m->regexp = NULL;
883 m->filename = NULL;
884 m->pos = 0;
885 m->def_regexp = NULL;
886 m->def_filename = NULL;
887 m->def_pos = 0;
888
889 assert (cls != NULL);
890
891 switch (sc)
892 {
893 case SC_FRIEND:
894 list = &cls->friends;
895 break;
896
897 case SC_TYPE:
898 list = &cls->types;
899 break;
900
901 case SC_STATIC:
902 list = var ? &cls->static_vars : &cls->static_fns;
903 break;
904
905 default:
906 list = var ? &cls->vars : &cls->fns;
907 break;
908 }
909
910 for (s = name; *s; ++s)
911 name_hash = (name_hash << 1) ^ *s;
912 i = name_hash % TABLE_SIZE;
913 m->anext = member_table[i];
914 member_table[i] = m;
915 m->list = list;
916
917 /* Keep the member list sorted. It's cheaper to do it here than to
918 sort them in Lisp. */
919 for (prev = NULL, p = *list;
920 p && strcmp (name, p->name) > 0;
921 prev = p, p = p->next)
922 ;
923
924 m->next = p;
925 if (prev)
926 prev->next = m;
927 else
928 *list = m;
929 return m;
930 }
931
932
933 /* Given the root R of a class tree, step through all subclasses
934 recursively, marking functions as virtual that are declared virtual
935 in base classes. */
936
937 static void
938 mark_virtual (struct sym *r)
939 {
940 struct link *p;
941 struct member *m, *m2;
942
943 for (p = r->subs; p; p = p->next)
944 {
945 for (m = r->fns; m; m = m->next)
946 if (HAS_FLAG (m->flags, F_VIRTUAL))
947 {
948 for (m2 = p->sym->fns; m2; m2 = m2->next)
949 if (m->param_hash == m2->param_hash && streq (m->name, m2->name))
950 SET_FLAG (m2->flags, F_VIRTUAL);
951 }
952
953 mark_virtual (p->sym);
954 }
955 }
956
957
958 /* For all roots of the class tree, mark functions as virtual that
959 are virtual because of a virtual declaration in a base class. */
960
961 static void
962 mark_inherited_virtual (void)
963 {
964 struct sym *r;
965 int i;
966
967 for (i = 0; i < TABLE_SIZE; ++i)
968 for (r = class_table[i]; r; r = r->next)
969 if (r->supers == NULL)
970 mark_virtual (r);
971 }
972
973
974 /* Create and return a symbol for a namespace with name NAME. */
975
976 static struct sym *
977 make_namespace (char *name, struct sym *context)
978 {
979 struct sym *s = (struct sym *) xmalloc (sizeof *s + strlen (name));
980 memset (s, 0, sizeof *s);
981 strcpy (s->name, name);
982 s->next = all_namespaces;
983 s->namesp = context;
984 all_namespaces = s;
985 return s;
986 }
987
988
989 /* Find the symbol for namespace NAME. If not found, retrun NULL */
990
991 static struct sym *
992 check_namespace (char *name, struct sym *context)
993 {
994 struct sym *p = NULL;
995
996 for (p = all_namespaces; p; p = p->next)
997 {
998 if (streq (p->name, name) && (p->namesp == context))
999 break;
1000 }
1001
1002 return p;
1003 }
1004
1005 /* Find the symbol for namespace NAME. If not found, add a new symbol
1006 for NAME to all_namespaces. */
1007
1008 static struct sym *
1009 find_namespace (char *name, struct sym *context)
1010 {
1011 struct sym *p = check_namespace (name, context);
1012
1013 if (p == NULL)
1014 p = make_namespace (name, context);
1015
1016 return p;
1017 }
1018
1019
1020 /* Find namespace alias with name NAME. If not found return NULL. */
1021
1022 static struct link *
1023 check_namespace_alias (char *name)
1024 {
1025 struct link *p = NULL;
1026 struct alias *al;
1027 unsigned h;
1028 char *s;
1029
1030 for (s = name, h = 0; *s; ++s)
1031 h = (h << 1) ^ *s;
1032 h %= TABLE_SIZE;
1033
1034 for (al = namespace_alias_table[h]; al; al = al->next)
1035 if (streq (name, al->name) && (al->namesp == current_namespace))
1036 {
1037 p = al->aliasee;
1038 break;
1039 }
1040
1041 return p;
1042 }
1043
1044 /* Register the name NEW_NAME as an alias for namespace list OLD_NAME. */
1045
1046 static void
1047 register_namespace_alias (char *new_name, struct link *old_name)
1048 {
1049 unsigned h;
1050 char *s;
1051 struct alias *al;
1052
1053 for (s = new_name, h = 0; *s; ++s)
1054 h = (h << 1) ^ *s;
1055 h %= TABLE_SIZE;
1056
1057
1058 /* Is it already in the table of aliases? */
1059 for (al = namespace_alias_table[h]; al; al = al->next)
1060 if (streq (new_name, al->name) && (al->namesp == current_namespace))
1061 return;
1062
1063 al = (struct alias *) xmalloc (sizeof *al + strlen (new_name));
1064 strcpy (al->name, new_name);
1065 al->next = namespace_alias_table[h];
1066 al->namesp = current_namespace;
1067 al->aliasee = old_name;
1068 namespace_alias_table[h] = al;
1069 }
1070
1071
1072 /* Enter namespace with name NAME. */
1073
1074 static void
1075 enter_namespace (char *name)
1076 {
1077 struct sym *p = find_namespace (name, current_namespace);
1078
1079 if (namespace_sp == namespace_stack_size)
1080 {
1081 int size = max (10, 2 * namespace_stack_size);
1082 namespace_stack
1083 = (struct sym **) xrealloc ((void *)namespace_stack,
1084 size * sizeof *namespace_stack);
1085 namespace_stack_size = size;
1086 }
1087
1088 namespace_stack[namespace_sp++] = current_namespace;
1089 current_namespace = p;
1090 }
1091
1092
1093 /* Leave the current namespace. */
1094
1095 static void
1096 leave_namespace (void)
1097 {
1098 assert (namespace_sp > 0);
1099 current_namespace = namespace_stack[--namespace_sp];
1100 }
1101
1102
1103 \f
1104 /***********************************************************************
1105 Writing the Output File
1106 ***********************************************************************/
1107
1108 /* Write string S to the output file FP in a Lisp-readable form.
1109 If S is null, write out `()'. */
1110
1111 #define PUTSTR(s, fp) \
1112 do { \
1113 if (!s) \
1114 { \
1115 putc ('(', fp); \
1116 putc (')', fp); \
1117 putc (' ', fp); \
1118 } \
1119 else \
1120 { \
1121 putc ('"', fp); \
1122 fputs (s, fp); \
1123 putc ('"', fp); \
1124 putc (' ', fp); \
1125 } \
1126 } while (0)
1127
1128 /* A dynamically allocated buffer for constructing a scope name. */
1129
1130 char *scope_buffer;
1131 int scope_buffer_size;
1132 int scope_buffer_len;
1133
1134
1135 /* Make sure scope_buffer has enough room to add LEN chars to it. */
1136
1137 static void
1138 ensure_scope_buffer_room (int len)
1139 {
1140 if (scope_buffer_len + len >= scope_buffer_size)
1141 {
1142 int new_size = max (2 * scope_buffer_size, scope_buffer_len + len);
1143 scope_buffer = (char *) xrealloc (scope_buffer, new_size);
1144 scope_buffer_size = new_size;
1145 }
1146 }
1147
1148
1149 /* Recursively add the scope names of symbol P and the scopes of its
1150 namespaces to scope_buffer. Value is a pointer to the complete
1151 scope name constructed. */
1152
1153 static char *
1154 sym_scope_1 (struct sym *p)
1155 {
1156 int len;
1157
1158 if (p->namesp)
1159 sym_scope_1 (p->namesp);
1160
1161 if (*scope_buffer)
1162 {
1163 ensure_scope_buffer_room (3);
1164 strcat (scope_buffer, "::");
1165 scope_buffer_len += 2;
1166 }
1167
1168 len = strlen (p->name);
1169 ensure_scope_buffer_room (len + 1);
1170 strcat (scope_buffer, p->name);
1171 scope_buffer_len += len;
1172
1173 if (HAS_FLAG (p->flags, F_TEMPLATE))
1174 {
1175 ensure_scope_buffer_room (3);
1176 strcat (scope_buffer, "<>");
1177 scope_buffer_len += 2;
1178 }
1179
1180 return scope_buffer;
1181 }
1182
1183
1184 /* Return the scope of symbol P in printed representation, i.e.
1185 as it would appear in a C*+ source file. */
1186
1187 static char *
1188 sym_scope (struct sym *p)
1189 {
1190 if (!scope_buffer)
1191 {
1192 scope_buffer_size = 1024;
1193 scope_buffer = (char *) xmalloc (scope_buffer_size);
1194 }
1195
1196 *scope_buffer = '\0';
1197 scope_buffer_len = 0;
1198
1199 if (p->namesp)
1200 sym_scope_1 (p->namesp);
1201
1202 return scope_buffer;
1203 }
1204
1205
1206 /* Dump the list of members M to file FP. Value is the length of the
1207 list. */
1208
1209 static int
1210 dump_members (FILE *fp, struct member *m)
1211 {
1212 int n;
1213
1214 putc ('(', fp);
1215
1216 for (n = 0; m; m = m->next, ++n)
1217 {
1218 fputs (MEMBER_STRUCT, fp);
1219 PUTSTR (m->name, fp);
1220 PUTSTR (NULL, fp); /* FIXME? scope for globals */
1221 fprintf (fp, "%u ", (unsigned) m->flags);
1222 PUTSTR (m->filename, fp);
1223 PUTSTR (m->regexp, fp);
1224 fprintf (fp, "%u ", (unsigned) m->pos);
1225 fprintf (fp, "%u ", (unsigned) m->vis);
1226 putc (' ', fp);
1227 PUTSTR (m->def_filename, fp);
1228 PUTSTR (m->def_regexp, fp);
1229 fprintf (fp, "%u", (unsigned) m->def_pos);
1230 putc (']', fp);
1231 putc ('\n', fp);
1232 }
1233
1234 putc (')', fp);
1235 putc ('\n', fp);
1236 return n;
1237 }
1238
1239
1240 /* Dump class ROOT to stream FP. */
1241
1242 static void
1243 dump_sym (FILE *fp, struct sym *root)
1244 {
1245 fputs (CLASS_STRUCT, fp);
1246 PUTSTR (root->name, fp);
1247
1248 /* Print scope, if any. */
1249 if (root->namesp)
1250 PUTSTR (sym_scope (root), fp);
1251 else
1252 PUTSTR (NULL, fp);
1253
1254 /* Print flags. */
1255 fprintf (fp, "%u", root->flags);
1256 PUTSTR (root->filename, fp);
1257 PUTSTR (root->regexp, fp);
1258 fprintf (fp, "%u", (unsigned) root->pos);
1259 PUTSTR (root->sfilename, fp);
1260 putc (']', fp);
1261 putc ('\n', fp);
1262 }
1263
1264
1265 /* Dump class ROOT and its subclasses to file FP. Value is the
1266 number of classes written. */
1267
1268 static int
1269 dump_tree (FILE *fp, struct sym *root)
1270 {
1271 struct link *lk;
1272 unsigned n = 0;
1273
1274 dump_sym (fp, root);
1275
1276 if (f_verbose)
1277 {
1278 putchar ('+');
1279 fflush (stdout);
1280 }
1281
1282 putc ('(', fp);
1283
1284 for (lk = root->subs; lk; lk = lk->next)
1285 {
1286 fputs (TREE_STRUCT, fp);
1287 n += dump_tree (fp, lk->sym);
1288 putc (']', fp);
1289 }
1290
1291 putc (')', fp);
1292
1293 dump_members (fp, root->vars);
1294 n += dump_members (fp, root->fns);
1295 dump_members (fp, root->static_vars);
1296 n += dump_members (fp, root->static_fns);
1297 n += dump_members (fp, root->friends);
1298 dump_members (fp, root->types);
1299
1300 /* Superclasses. */
1301 putc ('(', fp);
1302 putc (')', fp);
1303
1304 /* Mark slot. */
1305 putc ('(', fp);
1306 putc (')', fp);
1307
1308 putc ('\n', fp);
1309 return n;
1310 }
1311
1312
1313 /* Dump the entire class tree to file FP. */
1314
1315 static void
1316 dump_roots (FILE *fp)
1317 {
1318 int i, n = 0;
1319 struct sym *r;
1320
1321 /* Output file header containing version string, command line
1322 options etc. */
1323 if (!f_append)
1324 {
1325 fputs (TREE_HEADER_STRUCT, fp);
1326 PUTSTR (EBROWSE_FILE_VERSION, fp);
1327
1328 putc ('\"', fp);
1329 if (!f_structs)
1330 fputs (" -s", fp);
1331 if (f_regexps)
1332 fputs (" -x", fp);
1333 putc ('\"', fp);
1334 fputs (" ()", fp);
1335 fputs (" ()", fp);
1336 putc (']', fp);
1337 }
1338
1339 /* Mark functions as virtual that are so because of functions
1340 declared virtual in base classes. */
1341 mark_inherited_virtual ();
1342
1343 /* Dump the roots of the graph. */
1344 for (i = 0; i < TABLE_SIZE; ++i)
1345 for (r = class_table[i]; r; r = r->next)
1346 if (!r->supers)
1347 {
1348 fputs (TREE_STRUCT, fp);
1349 n += dump_tree (fp, r);
1350 putc (']', fp);
1351 }
1352
1353 if (f_verbose)
1354 putchar ('\n');
1355 }
1356
1357
1358 \f
1359 /***********************************************************************
1360 Scanner
1361 ***********************************************************************/
1362
1363 #ifdef DEBUG
1364 #define INCREMENT_LINENO \
1365 do { \
1366 if (f_very_verbose) \
1367 { \
1368 ++yyline; \
1369 printf ("%d:\n", yyline); \
1370 } \
1371 else \
1372 ++yyline; \
1373 } while (0)
1374 #else
1375 #define INCREMENT_LINENO ++yyline
1376 #endif
1377
1378 /* Define two macros for accessing the input buffer (current input
1379 file). GET(C) sets C to the next input character and advances the
1380 input pointer. UNGET retracts the input pointer. */
1381
1382 #define GET(C) ((C) = *in++)
1383 #define UNGET() (--in)
1384
1385
1386 /* Process a preprocessor line. Value is the next character from the
1387 input buffer not consumed. */
1388
1389 static int
1390 process_pp_line (void)
1391 {
1392 int in_comment = 0, in_string = 0;
1393 int c;
1394 char *p = yytext;
1395
1396 /* Skip over white space. The `#' has been consumed already. */
1397 while (WHITEP (GET (c)))
1398 ;
1399
1400 /* Read the preprocessor command (if any). */
1401 while (IDENTP (c))
1402 {
1403 *p++ = c;
1404 GET (c);
1405 }
1406
1407 /* Is it a `define'? */
1408 *p = '\0';
1409
1410 if (*yytext && streq (yytext, "define"))
1411 {
1412 p = yytext;
1413 while (WHITEP (c))
1414 GET (c);
1415 while (IDENTP (c))
1416 {
1417 *p++ = c;
1418 GET (c);
1419 }
1420
1421 *p = '\0';
1422
1423 if (*yytext)
1424 {
1425 char *regexp = matching_regexp ();
1426 int pos = BUFFER_POS ();
1427 add_define (yytext, regexp, pos);
1428 }
1429 }
1430
1431 while (c && (c != '\n' || in_comment || in_string))
1432 {
1433 if (c == '\\')
1434 GET (c);
1435 else if (c == '/' && !in_comment)
1436 {
1437 if (GET (c) == '*')
1438 in_comment = 1;
1439 }
1440 else if (c == '*' && in_comment)
1441 {
1442 if (GET (c) == '/')
1443 in_comment = 0;
1444 }
1445 else if (c == '"')
1446 in_string = !in_string;
1447
1448 if (c == '\n')
1449 INCREMENT_LINENO;
1450
1451 GET (c);
1452 }
1453
1454 return c;
1455 }
1456
1457
1458 /* Value is the next token from the input buffer. */
1459
1460 static int
1461 yylex (void)
1462 {
1463 int c;
1464 char end_char;
1465 char *p;
1466
1467 for (;;)
1468 {
1469 while (WHITEP (GET (c)))
1470 ;
1471
1472 switch (c)
1473 {
1474 case '\n':
1475 INCREMENT_LINENO;
1476 break;
1477
1478 case '\r':
1479 break;
1480
1481 case 0:
1482 /* End of file. */
1483 return YYEOF;
1484
1485 case '\\':
1486 GET (c);
1487 break;
1488
1489 case '"':
1490 case '\'':
1491 /* String and character constants. */
1492 end_char = c;
1493 string_start = in;
1494 while (GET (c) && c != end_char)
1495 {
1496 switch (c)
1497 {
1498 case '\\':
1499 /* Escape sequences. */
1500 if (!GET (c))
1501 {
1502 if (end_char == '\'')
1503 yyerror ("EOF in character constant", NULL);
1504 else
1505 yyerror ("EOF in string constant", NULL);
1506 goto end_string;
1507 }
1508 else switch (c)
1509 {
1510 case '\n':
1511 INCREMENT_LINENO;
1512 case 'a':
1513 case 'b':
1514 case 'f':
1515 case 'n':
1516 case 'r':
1517 case 't':
1518 case 'v':
1519 break;
1520
1521 case 'x':
1522 {
1523 /* Hexadecimal escape sequence. */
1524 int i;
1525 for (i = 0; i < 2; ++i)
1526 {
1527 GET (c);
1528
1529 if (c >= '0' && c <= '7')
1530 ;
1531 else if (c >= 'a' && c <= 'f')
1532 ;
1533 else if (c >= 'A' && c <= 'F')
1534 ;
1535 else
1536 {
1537 UNGET ();
1538 break;
1539 }
1540 }
1541 }
1542 break;
1543
1544 case '0':
1545 {
1546 /* Octal escape sequence. */
1547 int i;
1548 for (i = 0; i < 3; ++i)
1549 {
1550 GET (c);
1551
1552 if (c >= '0' && c <= '7')
1553 ;
1554 else
1555 {
1556 UNGET ();
1557 break;
1558 }
1559 }
1560 }
1561 break;
1562
1563 default:
1564 break;
1565 }
1566 break;
1567
1568 case '\n':
1569 if (end_char == '\'')
1570 yyerror ("newline in character constant", NULL);
1571 else
1572 yyerror ("newline in string constant", NULL);
1573 INCREMENT_LINENO;
1574 break;
1575
1576 default:
1577 break;
1578 }
1579 }
1580
1581 end_string:
1582 return end_char == '\'' ? CCHAR : CSTRING;
1583
1584 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g':
1585 case 'h': case 'i': case 'j': case 'k': case 'l': case 'm': case 'n':
1586 case 'o': case 'p': case 'q': case 'r': case 's': case 't': case 'u':
1587 case 'v': case 'w': case 'x': case 'y': case 'z':
1588 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G':
1589 case 'H': case 'I': case 'J': case 'K': case 'L': case 'M': case 'N':
1590 case 'O': case 'P': case 'Q': case 'R': case 'S': case 'T': case 'U':
1591 case 'V': case 'W': case 'X': case 'Y': case 'Z': case '_':
1592 {
1593 /* Identifier and keywords. */
1594 unsigned hash;
1595 struct kw *k;
1596
1597 p = yytext;
1598 *p++ = hash = c;
1599
1600 while (IDENTP (GET (*p)))
1601 {
1602 hash = (hash << 1) ^ *p++;
1603 if (p == yytext_end - 1)
1604 {
1605 int size = yytext_end - yytext;
1606 yytext = (char *) xrealloc (yytext, 2 * size);
1607 yytext_end = yytext + 2 * size;
1608 p = yytext + size - 1;
1609 }
1610 }
1611
1612 UNGET ();
1613 *p = 0;
1614
1615 for (k = keyword_table[hash % KEYWORD_TABLE_SIZE]; k; k = k->next)
1616 if (streq (k->name, yytext))
1617 return k->tk;
1618
1619 return IDENT;
1620 }
1621
1622 case '/':
1623 /* C and C++ comments, '/' and '/='. */
1624 switch (GET (c))
1625 {
1626 case '*':
1627 while (GET (c))
1628 {
1629 switch (c)
1630 {
1631 case '*':
1632 if (GET (c) == '/')
1633 goto comment_end;
1634 UNGET ();
1635 break;
1636 case '\\':
1637 GET (c);
1638 break;
1639 case '\n':
1640 INCREMENT_LINENO;
1641 break;
1642 }
1643 }
1644 comment_end:;
1645 break;
1646
1647 case '=':
1648 return DIVASGN;
1649
1650 case '/':
1651 while (GET (c) && c != '\n')
1652 ;
1653 /* Don't try to read past the end of the input buffer if
1654 the file ends in a C++ comment without a newline. */
1655 if (c == 0)
1656 return YYEOF;
1657
1658 INCREMENT_LINENO;
1659 break;
1660
1661 default:
1662 UNGET ();
1663 return '/';
1664 }
1665 break;
1666
1667 case '+':
1668 if (GET (c) == '+')
1669 return INC;
1670 else if (c == '=')
1671 return ADDASGN;
1672 UNGET ();
1673 return '+';
1674
1675 case '-':
1676 switch (GET (c))
1677 {
1678 case '-':
1679 return DEC;
1680 case '>':
1681 if (GET (c) == '*')
1682 return ARROWSTAR;
1683 UNGET ();
1684 return ARROW;
1685 case '=':
1686 return SUBASGN;
1687 }
1688 UNGET ();
1689 return '-';
1690
1691 case '*':
1692 if (GET (c) == '=')
1693 return MULASGN;
1694 UNGET ();
1695 return '*';
1696
1697 case '%':
1698 if (GET (c) == '=')
1699 return MODASGN;
1700 UNGET ();
1701 return '%';
1702
1703 case '|':
1704 if (GET (c) == '|')
1705 return LOR;
1706 else if (c == '=')
1707 return ORASGN;
1708 UNGET ();
1709 return '|';
1710
1711 case '&':
1712 if (GET (c) == '&')
1713 return LAND;
1714 else if (c == '=')
1715 return ANDASGN;
1716 UNGET ();
1717 return '&';
1718
1719 case '^':
1720 if (GET (c) == '=')
1721 return XORASGN;
1722 UNGET ();
1723 return '^';
1724
1725 case '.':
1726 if (GET (c) == '*')
1727 return POINTSTAR;
1728 else if (c == '.')
1729 {
1730 if (GET (c) != '.')
1731 yyerror ("invalid token '..' ('...' assumed)", NULL);
1732 UNGET ();
1733 return ELLIPSIS;
1734 }
1735 else if (!DIGITP (c))
1736 {
1737 UNGET ();
1738 return '.';
1739 }
1740 goto mantissa;
1741
1742 case ':':
1743 if (GET (c) == ':')
1744 return DCOLON;
1745 UNGET ();
1746 return ':';
1747
1748 case '=':
1749 if (GET (c) == '=')
1750 return EQ;
1751 UNGET ();
1752 return '=';
1753
1754 case '!':
1755 if (GET (c) == '=')
1756 return NE;
1757 UNGET ();
1758 return '!';
1759
1760 case '<':
1761 switch (GET (c))
1762 {
1763 case '=':
1764 return LE;
1765 case '<':
1766 if (GET (c) == '=')
1767 return LSHIFTASGN;
1768 UNGET ();
1769 return LSHIFT;
1770 }
1771 UNGET ();
1772 return '<';
1773
1774 case '>':
1775 switch (GET (c))
1776 {
1777 case '=':
1778 return GE;
1779 case '>':
1780 if (GET (c) == '=')
1781 return RSHIFTASGN;
1782 UNGET ();
1783 return RSHIFT;
1784 }
1785 UNGET ();
1786 return '>';
1787
1788 case '#':
1789 c = process_pp_line ();
1790 if (c == 0)
1791 return YYEOF;
1792 break;
1793
1794 case '(': case ')': case '[': case ']': case '{': case '}':
1795 case ';': case ',': case '?': case '~':
1796 return c;
1797
1798 case '0':
1799 yyival = 0;
1800
1801 if (GET (c) == 'x' || c == 'X')
1802 {
1803 while (GET (c))
1804 {
1805 if (DIGITP (c))
1806 yyival = yyival * 16 + c - '0';
1807 else if (c >= 'a' && c <= 'f')
1808 yyival = yyival * 16 + c - 'a' + 10;
1809 else if (c >= 'A' && c <= 'F')
1810 yyival = yyival * 16 + c - 'A' + 10;
1811 else
1812 break;
1813 }
1814
1815 goto int_suffixes;
1816 }
1817 else if (c == '.')
1818 goto mantissa;
1819
1820 while (c >= '0' && c <= '7')
1821 {
1822 yyival = (yyival << 3) + c - '0';
1823 GET (c);
1824 }
1825
1826 int_suffixes:
1827 /* Integer suffixes. */
1828 while (isalpha (c))
1829 GET (c);
1830 UNGET ();
1831 return CINT;
1832
1833 case '1': case '2': case '3': case '4': case '5': case '6':
1834 case '7': case '8': case '9':
1835 /* Integer or floating constant, part before '.'. */
1836 yyival = c - '0';
1837
1838 while (GET (c) && DIGITP (c))
1839 yyival = 10 * yyival + c - '0';
1840
1841 if (c != '.')
1842 goto int_suffixes;
1843
1844 mantissa:
1845 /* Digits following '.'. */
1846 while (DIGITP (c))
1847 GET (c);
1848
1849 /* Optional exponent. */
1850 if (c == 'E' || c == 'e')
1851 {
1852 if (GET (c) == '-' || c == '+')
1853 GET (c);
1854
1855 while (DIGITP (c))
1856 GET (c);
1857 }
1858
1859 /* Optional type suffixes. */
1860 while (isalpha (c))
1861 GET (c);
1862 UNGET ();
1863 return CFLOAT;
1864
1865 default:
1866 break;
1867 }
1868 }
1869 }
1870
1871
1872 /* Actually local to matching_regexp. These variables must be in
1873 global scope for the case that `static' get's defined away. */
1874
1875 static char *matching_regexp_buffer, *matching_regexp_end_buf;
1876
1877
1878 /* Value is the string from the start of the line to the current
1879 position in the input buffer, or maybe a bit more if that string is
1880 shorter than min_regexp. */
1881
1882 static char *
1883 matching_regexp (void)
1884 {
1885 char *p;
1886 char *s;
1887 char *t;
1888
1889 if (!f_regexps)
1890 return NULL;
1891
1892 if (matching_regexp_buffer == NULL)
1893 {
1894 matching_regexp_buffer = (char *) xmalloc (max_regexp);
1895 matching_regexp_end_buf = &matching_regexp_buffer[max_regexp] - 1;
1896 }
1897
1898 /* Scan back to previous newline of buffer start. */
1899 for (p = in - 1; p > inbuffer && *p != '\n'; --p)
1900 ;
1901
1902 if (*p == '\n')
1903 {
1904 while (in - p < min_regexp && p > inbuffer)
1905 {
1906 /* Line probably not significant enough */
1907 for (--p; p > inbuffer && *p != '\n'; --p)
1908 ;
1909 }
1910 if (*p == '\n')
1911 ++p;
1912 }
1913
1914 /* Copy from end to make sure significant portions are included.
1915 This implies that in the browser a regular expressing of the form
1916 `^.*{regexp}' has to be used. */
1917 for (s = matching_regexp_end_buf - 1, t = in;
1918 s > matching_regexp_buffer && t > p;)
1919 {
1920 *--s = *--t;
1921
1922 if (*s == '"' || *s == '\\')
1923 *--s = '\\';
1924 }
1925
1926 *(matching_regexp_end_buf - 1) = '\0';
1927 return xstrdup (s);
1928 }
1929
1930
1931 /* Return a printable representation of token T. */
1932
1933 static const char *
1934 token_string (int t)
1935 {
1936 static char b[3];
1937
1938 switch (t)
1939 {
1940 case CSTRING: return "string constant";
1941 case CCHAR: return "char constant";
1942 case CINT: return "int constant";
1943 case CFLOAT: return "floating constant";
1944 case ELLIPSIS: return "...";
1945 case LSHIFTASGN: return "<<=";
1946 case RSHIFTASGN: return ">>=";
1947 case ARROWSTAR: return "->*";
1948 case IDENT: return "identifier";
1949 case DIVASGN: return "/=";
1950 case INC: return "++";
1951 case ADDASGN: return "+=";
1952 case DEC: return "--";
1953 case ARROW: return "->";
1954 case SUBASGN: return "-=";
1955 case MULASGN: return "*=";
1956 case MODASGN: return "%=";
1957 case LOR: return "||";
1958 case ORASGN: return "|=";
1959 case LAND: return "&&";
1960 case ANDASGN: return "&=";
1961 case XORASGN: return "^=";
1962 case POINTSTAR: return ".*";
1963 case DCOLON: return "::";
1964 case EQ: return "==";
1965 case NE: return "!=";
1966 case LE: return "<=";
1967 case LSHIFT: return "<<";
1968 case GE: return ">=";
1969 case RSHIFT: return ">>";
1970 case ASM: return "asm";
1971 case AUTO: return "auto";
1972 case BREAK: return "break";
1973 case CASE: return "case";
1974 case CATCH: return "catch";
1975 case CHAR: return "char";
1976 case CLASS: return "class";
1977 case CONST: return "const";
1978 case CONTINUE: return "continue";
1979 case DEFAULT: return "default";
1980 case DELETE: return "delete";
1981 case DO: return "do";
1982 case DOUBLE: return "double";
1983 case ELSE: return "else";
1984 case ENUM: return "enum";
1985 case EXTERN: return "extern";
1986 case FLOAT: return "float";
1987 case FOR: return "for";
1988 case FRIEND: return "friend";
1989 case GOTO: return "goto";
1990 case IF: return "if";
1991 case T_INLINE: return "inline";
1992 case INT: return "int";
1993 case LONG: return "long";
1994 case NEW: return "new";
1995 case OPERATOR: return "operator";
1996 case PRIVATE: return "private";
1997 case PROTECTED: return "protected";
1998 case PUBLIC: return "public";
1999 case REGISTER: return "register";
2000 case RETURN: return "return";
2001 case SHORT: return "short";
2002 case SIGNED: return "signed";
2003 case SIZEOF: return "sizeof";
2004 case STATIC: return "static";
2005 case STRUCT: return "struct";
2006 case SWITCH: return "switch";
2007 case TEMPLATE: return "template";
2008 case THIS: return "this";
2009 case THROW: return "throw";
2010 case TRY: return "try";
2011 case TYPEDEF: return "typedef";
2012 case UNION: return "union";
2013 case UNSIGNED: return "unsigned";
2014 case VIRTUAL: return "virtual";
2015 case VOID: return "void";
2016 case VOLATILE: return "volatile";
2017 case WHILE: return "while";
2018 case MUTABLE: return "mutable";
2019 case BOOL: return "bool";
2020 case TRUE: return "true";
2021 case FALSE: return "false";
2022 case SIGNATURE: return "signature";
2023 case NAMESPACE: return "namespace";
2024 case EXPLICIT: return "explicit";
2025 case TYPENAME: return "typename";
2026 case CONST_CAST: return "const_cast";
2027 case DYNAMIC_CAST: return "dynamic_cast";
2028 case REINTERPRET_CAST: return "reinterpret_cast";
2029 case STATIC_CAST: return "static_cast";
2030 case TYPEID: return "typeid";
2031 case USING: return "using";
2032 case WCHAR: return "wchar_t";
2033 case YYEOF: return "EOF";
2034
2035 default:
2036 if (t < 255)
2037 {
2038 b[0] = t;
2039 b[1] = '\0';
2040 return b;
2041 }
2042 else
2043 return "???";
2044 }
2045 }
2046
2047
2048 /* Reinitialize the scanner for a new input file. */
2049
2050 static void
2051 re_init_scanner (void)
2052 {
2053 in = inbuffer;
2054 yyline = 1;
2055
2056 if (yytext == NULL)
2057 {
2058 int size = 256;
2059 yytext = (char *) xmalloc (size * sizeof *yytext);
2060 yytext_end = yytext + size;
2061 }
2062 }
2063
2064
2065 /* Insert a keyword NAME with token value TK into the keyword hash
2066 table. */
2067
2068 static void
2069 insert_keyword (const char *name, int tk)
2070 {
2071 const char *s;
2072 unsigned h = 0;
2073 struct kw *k = (struct kw *) xmalloc (sizeof *k);
2074
2075 for (s = name; *s; ++s)
2076 h = (h << 1) ^ *s;
2077
2078 h %= KEYWORD_TABLE_SIZE;
2079 k->name = name;
2080 k->tk = tk;
2081 k->next = keyword_table[h];
2082 keyword_table[h] = k;
2083 }
2084
2085
2086 /* Initialize the scanner for the first file. This sets up the
2087 character class vectors and fills the keyword hash table. */
2088
2089 static void
2090 init_scanner (void)
2091 {
2092 int i;
2093
2094 /* Allocate the input buffer */
2095 inbuffer_size = READ_CHUNK_SIZE + 1;
2096 inbuffer = in = (char *) xmalloc (inbuffer_size);
2097 yyline = 1;
2098
2099 /* Set up character class vectors. */
2100 for (i = 0; i < sizeof is_ident; ++i)
2101 {
2102 if (i == '_' || isalnum (i))
2103 is_ident[i] = 1;
2104
2105 if (i >= '0' && i <= '9')
2106 is_digit[i] = 1;
2107
2108 if (i == ' ' || i == '\t' || i == '\f' || i == '\v')
2109 is_white[i] = 1;
2110 }
2111
2112 /* Fill keyword hash table. */
2113 insert_keyword ("and", LAND);
2114 insert_keyword ("and_eq", ANDASGN);
2115 insert_keyword ("asm", ASM);
2116 insert_keyword ("auto", AUTO);
2117 insert_keyword ("bitand", '&');
2118 insert_keyword ("bitor", '|');
2119 insert_keyword ("bool", BOOL);
2120 insert_keyword ("break", BREAK);
2121 insert_keyword ("case", CASE);
2122 insert_keyword ("catch", CATCH);
2123 insert_keyword ("char", CHAR);
2124 insert_keyword ("class", CLASS);
2125 insert_keyword ("compl", '~');
2126 insert_keyword ("const", CONST);
2127 insert_keyword ("const_cast", CONST_CAST);
2128 insert_keyword ("continue", CONTINUE);
2129 insert_keyword ("default", DEFAULT);
2130 insert_keyword ("delete", DELETE);
2131 insert_keyword ("do", DO);
2132 insert_keyword ("double", DOUBLE);
2133 insert_keyword ("dynamic_cast", DYNAMIC_CAST);
2134 insert_keyword ("else", ELSE);
2135 insert_keyword ("enum", ENUM);
2136 insert_keyword ("explicit", EXPLICIT);
2137 insert_keyword ("extern", EXTERN);
2138 insert_keyword ("false", FALSE);
2139 insert_keyword ("float", FLOAT);
2140 insert_keyword ("for", FOR);
2141 insert_keyword ("friend", FRIEND);
2142 insert_keyword ("goto", GOTO);
2143 insert_keyword ("if", IF);
2144 insert_keyword ("inline", T_INLINE);
2145 insert_keyword ("int", INT);
2146 insert_keyword ("long", LONG);
2147 insert_keyword ("mutable", MUTABLE);
2148 insert_keyword ("namespace", NAMESPACE);
2149 insert_keyword ("new", NEW);
2150 insert_keyword ("not", '!');
2151 insert_keyword ("not_eq", NE);
2152 insert_keyword ("operator", OPERATOR);
2153 insert_keyword ("or", LOR);
2154 insert_keyword ("or_eq", ORASGN);
2155 insert_keyword ("private", PRIVATE);
2156 insert_keyword ("protected", PROTECTED);
2157 insert_keyword ("public", PUBLIC);
2158 insert_keyword ("register", REGISTER);
2159 insert_keyword ("reinterpret_cast", REINTERPRET_CAST);
2160 insert_keyword ("return", RETURN);
2161 insert_keyword ("short", SHORT);
2162 insert_keyword ("signed", SIGNED);
2163 insert_keyword ("sizeof", SIZEOF);
2164 insert_keyword ("static", STATIC);
2165 insert_keyword ("static_cast", STATIC_CAST);
2166 insert_keyword ("struct", STRUCT);
2167 insert_keyword ("switch", SWITCH);
2168 insert_keyword ("template", TEMPLATE);
2169 insert_keyword ("this", THIS);
2170 insert_keyword ("throw", THROW);
2171 insert_keyword ("true", TRUE);
2172 insert_keyword ("try", TRY);
2173 insert_keyword ("typedef", TYPEDEF);
2174 insert_keyword ("typeid", TYPEID);
2175 insert_keyword ("typename", TYPENAME);
2176 insert_keyword ("union", UNION);
2177 insert_keyword ("unsigned", UNSIGNED);
2178 insert_keyword ("using", USING);
2179 insert_keyword ("virtual", VIRTUAL);
2180 insert_keyword ("void", VOID);
2181 insert_keyword ("volatile", VOLATILE);
2182 insert_keyword ("wchar_t", WCHAR);
2183 insert_keyword ("while", WHILE);
2184 insert_keyword ("xor", '^');
2185 insert_keyword ("xor_eq", XORASGN);
2186 }
2187
2188
2189 \f
2190 /***********************************************************************
2191 Parser
2192 ***********************************************************************/
2193
2194 /* Match the current lookahead token and set it to the next token. */
2195
2196 #define MATCH() (tk = yylex ())
2197
2198 /* Return the lookahead token. If current lookahead token is cleared,
2199 read a new token. */
2200
2201 #define LA1 (tk == -1 ? (tk = yylex ()) : tk)
2202
2203 /* Is the current lookahead equal to the token T? */
2204
2205 #define LOOKING_AT(T) (tk == (T))
2206
2207 /* Is the current lookahead one of T1 or T2? */
2208
2209 #define LOOKING_AT2(T1, T2) (tk == (T1) || tk == (T2))
2210
2211 /* Is the current lookahead one of T1, T2 or T3? */
2212
2213 #define LOOKING_AT3(T1, T2, T3) (tk == (T1) || tk == (T2) || tk == (T3))
2214
2215 /* Is the current lookahead one of T1...T4? */
2216
2217 #define LOOKING_AT4(T1, T2, T3, T4) \
2218 (tk == (T1) || tk == (T2) || tk == (T3) || tk == (T4))
2219
2220 /* Match token T if current lookahead is T. */
2221
2222 #define MATCH_IF(T) if (LOOKING_AT (T)) MATCH (); else ((void) 0)
2223
2224 /* Skip to matching token if current token is T. */
2225
2226 #define SKIP_MATCHING_IF(T) \
2227 if (LOOKING_AT (T)) skip_matching (); else ((void) 0)
2228
2229
2230 /* Skip forward until a given token TOKEN or YYEOF is seen and return
2231 the current lookahead token after skipping. */
2232
2233 static int
2234 skip_to (int token)
2235 {
2236 while (!LOOKING_AT2 (YYEOF, token))
2237 MATCH ();
2238 return tk;
2239 }
2240
2241 /* Skip over pairs of tokens (parentheses, square brackets,
2242 angle brackets, curly brackets) matching the current lookahead. */
2243
2244 static void
2245 skip_matching (void)
2246 {
2247 int open, close, n;
2248
2249 switch (open = LA1)
2250 {
2251 case '{':
2252 close = '}';
2253 break;
2254
2255 case '(':
2256 close = ')';
2257 break;
2258
2259 case '<':
2260 close = '>';
2261 break;
2262
2263 case '[':
2264 close = ']';
2265 break;
2266
2267 default:
2268 abort ();
2269 }
2270
2271 for (n = 0;;)
2272 {
2273 if (LOOKING_AT (open))
2274 ++n;
2275 else if (LOOKING_AT (close))
2276 --n;
2277 else if (LOOKING_AT (YYEOF))
2278 break;
2279
2280 MATCH ();
2281
2282 if (n == 0)
2283 break;
2284 }
2285 }
2286
2287 static void
2288 skip_initializer (void)
2289 {
2290 for (;;)
2291 {
2292 switch (LA1)
2293 {
2294 case ';':
2295 case ',':
2296 case YYEOF:
2297 return;
2298
2299 case '{':
2300 case '[':
2301 case '(':
2302 skip_matching ();
2303 break;
2304
2305 default:
2306 MATCH ();
2307 break;
2308 }
2309 }
2310 }
2311
2312 /* Build qualified namespace alias (A::B::c) and return it. */
2313
2314 static struct link *
2315 match_qualified_namespace_alias (void)
2316 {
2317 struct link *head = NULL;
2318 struct link *cur = NULL;
2319 struct link *tmp = NULL;
2320
2321 for (;;)
2322 {
2323 MATCH ();
2324 switch (LA1)
2325 {
2326 case IDENT:
2327 tmp = (struct link *) xmalloc (sizeof *cur);
2328 tmp->sym = find_namespace (yytext, cur ? cur->sym : NULL);
2329 tmp->next = NULL;
2330 if (head)
2331 {
2332 cur = cur->next = tmp;
2333 }
2334 else
2335 {
2336 head = cur = tmp;
2337 }
2338 break;
2339 case DCOLON:
2340 /* Just skip */
2341 break;
2342 default:
2343 return head;
2344 break;
2345 }
2346 }
2347 }
2348
2349 /* Re-initialize the parser by resetting the lookahead token. */
2350
2351 static void
2352 re_init_parser (void)
2353 {
2354 tk = -1;
2355 }
2356
2357
2358 /* Parse a parameter list, including the const-specifier,
2359 pure-specifier, and throw-list that may follow a parameter list.
2360 Return in FLAGS what was seen following the parameter list.
2361 Returns a hash code for the parameter types. This value is used to
2362 distinguish between overloaded functions. */
2363
2364 static unsigned
2365 parm_list (int *flags)
2366 {
2367 unsigned hash = 0;
2368 int type_seen = 0;
2369
2370 while (!LOOKING_AT2 (YYEOF, ')'))
2371 {
2372 switch (LA1)
2373 {
2374 /* Skip over grouping parens or parameter lists in parameter
2375 declarations. */
2376 case '(':
2377 skip_matching ();
2378 break;
2379
2380 /* Next parameter. */
2381 case ',':
2382 MATCH ();
2383 type_seen = 0;
2384 break;
2385
2386 /* Ignore the scope part of types, if any. This is because
2387 some types need scopes when defined outside of a class body,
2388 and don't need them inside the class body. This means that
2389 we have to look for the last IDENT in a sequence of
2390 IDENT::IDENT::... */
2391 case IDENT:
2392 if (!type_seen)
2393 {
2394 char *last_id;
2395 unsigned ident_type_hash = 0;
2396
2397 parse_qualified_param_ident_or_type (&last_id);
2398 if (last_id)
2399 {
2400 /* LAST_ID null means something like `X::*'. */
2401 for (; *last_id; ++last_id)
2402 ident_type_hash = (ident_type_hash << 1) ^ *last_id;
2403 hash = (hash << 1) ^ ident_type_hash;
2404 type_seen = 1;
2405 }
2406 }
2407 else
2408 MATCH ();
2409 break;
2410
2411 case VOID:
2412 /* This distinction is made to make `func (void)' equivalent
2413 to `func ()'. */
2414 type_seen = 1;
2415 MATCH ();
2416 if (!LOOKING_AT (')'))
2417 hash = (hash << 1) ^ VOID;
2418 break;
2419
2420 case BOOL: case CHAR: case CLASS: case CONST:
2421 case DOUBLE: case ENUM: case FLOAT: case INT:
2422 case LONG: case SHORT: case SIGNED: case STRUCT:
2423 case UNION: case UNSIGNED: case VOLATILE: case WCHAR:
2424 case ELLIPSIS:
2425 type_seen = 1;
2426 hash = (hash << 1) ^ LA1;
2427 MATCH ();
2428 break;
2429
2430 case '*': case '&': case '[': case ']':
2431 hash = (hash << 1) ^ LA1;
2432 MATCH ();
2433 break;
2434
2435 default:
2436 MATCH ();
2437 break;
2438 }
2439 }
2440
2441 if (LOOKING_AT (')'))
2442 {
2443 MATCH ();
2444
2445 if (LOOKING_AT (CONST))
2446 {
2447 /* We can overload the same function on `const' */
2448 hash = (hash << 1) ^ CONST;
2449 SET_FLAG (*flags, F_CONST);
2450 MATCH ();
2451 }
2452
2453 if (LOOKING_AT (THROW))
2454 {
2455 MATCH ();
2456 SKIP_MATCHING_IF ('(');
2457 SET_FLAG (*flags, F_THROW);
2458 }
2459
2460 if (LOOKING_AT ('='))
2461 {
2462 MATCH ();
2463 if (LOOKING_AT (CINT) && yyival == 0)
2464 {
2465 MATCH ();
2466 SET_FLAG (*flags, F_PURE);
2467 }
2468 }
2469 }
2470
2471 return hash;
2472 }
2473
2474
2475 /* Print position info to stdout. */
2476
2477 static void
2478 print_info (void)
2479 {
2480 if (info_position >= 0 && BUFFER_POS () <= info_position)
2481 if (info_cls)
2482 printf ("(\"%s\" \"%s\" \"%s\" %d)\n",
2483 info_cls->name, sym_scope (info_cls),
2484 info_member->name, info_where);
2485 }
2486
2487
2488 /* Parse a member declaration within the class body of CLS. VIS is
2489 the access specifier for the member (private, protected,
2490 public). */
2491
2492 static void
2493 member (struct sym *cls, int vis)
2494 {
2495 char *id = NULL;
2496 int sc = SC_MEMBER;
2497 char *regexp = NULL;
2498 int pos;
2499 int is_constructor;
2500 int anonymous = 0;
2501 int flags = 0;
2502 int class_tag;
2503 int type_seen = 0;
2504 int paren_seen = 0;
2505 unsigned hash = 0;
2506 int tilde = 0;
2507
2508 while (!LOOKING_AT4 (';', '{', '}', YYEOF))
2509 {
2510 switch (LA1)
2511 {
2512 default:
2513 MATCH ();
2514 break;
2515
2516 /* A function or class may follow. */
2517 case TEMPLATE:
2518 MATCH();
2519 SET_FLAG (flags, F_TEMPLATE);
2520 /* Skip over template argument list */
2521 SKIP_MATCHING_IF ('<');
2522 break;
2523
2524 case EXPLICIT:
2525 SET_FLAG (flags, F_EXPLICIT);
2526 goto typeseen;
2527
2528 case MUTABLE:
2529 SET_FLAG (flags, F_MUTABLE);
2530 goto typeseen;
2531
2532 case T_INLINE:
2533 SET_FLAG (flags, F_INLINE);
2534 goto typeseen;
2535
2536 case VIRTUAL:
2537 SET_FLAG (flags, F_VIRTUAL);
2538 goto typeseen;
2539
2540 case '[':
2541 skip_matching ();
2542 break;
2543
2544 case ENUM:
2545 sc = SC_TYPE;
2546 goto typeseen;
2547
2548 case TYPEDEF:
2549 sc = SC_TYPE;
2550 goto typeseen;
2551
2552 case FRIEND:
2553 sc = SC_FRIEND;
2554 goto typeseen;
2555
2556 case STATIC:
2557 sc = SC_STATIC;
2558 goto typeseen;
2559
2560 case '~':
2561 tilde = 1;
2562 MATCH ();
2563 break;
2564
2565 case IDENT:
2566 /* Remember IDENTS seen so far. Among these will be the member
2567 name. */
2568 id = (char *) xrealloc (id, strlen (yytext) + 2);
2569 if (tilde)
2570 {
2571 *id = '~';
2572 strcpy (id + 1, yytext);
2573 }
2574 else
2575 strcpy (id, yytext);
2576 MATCH ();
2577 break;
2578
2579 case OPERATOR:
2580 {
2581 char *s = operator_name (&sc);
2582 id = (char *) xrealloc (id, strlen (s) + 1);
2583 strcpy (id, s);
2584 }
2585 break;
2586
2587 case '(':
2588 /* Most probably the beginning of a parameter list. */
2589 MATCH ();
2590 paren_seen = 1;
2591
2592 if (id && cls)
2593 {
2594 if (!(is_constructor = streq (id, cls->name)))
2595 regexp = matching_regexp ();
2596 }
2597 else
2598 is_constructor = 0;
2599
2600 pos = BUFFER_POS ();
2601 hash = parm_list (&flags);
2602
2603 if (is_constructor)
2604 regexp = matching_regexp ();
2605
2606 if (id && cls != NULL)
2607 add_member_decl (cls, id, regexp, pos, hash, 0, sc, vis, flags);
2608
2609 while (!LOOKING_AT3 (';', '{', YYEOF))
2610 MATCH ();
2611
2612 if (LOOKING_AT ('{') && id && cls)
2613 add_member_defn (cls, id, regexp, pos, hash, 0, sc, flags);
2614
2615 free (id);
2616 id = NULL;
2617 sc = SC_MEMBER;
2618 break;
2619
2620 case STRUCT: case UNION: case CLASS:
2621 /* Nested class */
2622 class_tag = LA1;
2623 type_seen = 1;
2624 MATCH ();
2625 anonymous = 1;
2626
2627 /* More than one ident here to allow for MS-DOS specialties
2628 like `_export class' etc. The last IDENT seen counts
2629 as the class name. */
2630 while (!LOOKING_AT4 (YYEOF, ';', ':', '{'))
2631 {
2632 if (LOOKING_AT (IDENT))
2633 anonymous = 0;
2634 MATCH ();
2635 }
2636
2637 if (LOOKING_AT2 (':', '{'))
2638 class_definition (anonymous ? NULL : cls, class_tag, flags, 1);
2639 else
2640 skip_to (';');
2641 break;
2642
2643 case INT: case CHAR: case LONG: case UNSIGNED:
2644 case SIGNED: case CONST: case DOUBLE: case VOID:
2645 case SHORT: case VOLATILE: case BOOL: case WCHAR:
2646 case TYPENAME:
2647 typeseen:
2648 type_seen = 1;
2649 MATCH ();
2650 break;
2651 }
2652 }
2653
2654 if (LOOKING_AT (';'))
2655 {
2656 /* The end of a member variable, a friend declaration or an access
2657 declaration. We don't want to add friend classes as members. */
2658 if (id && sc != SC_FRIEND && cls)
2659 {
2660 regexp = matching_regexp ();
2661 pos = BUFFER_POS ();
2662
2663 if (cls != NULL)
2664 {
2665 if (type_seen || !paren_seen)
2666 add_member_decl (cls, id, regexp, pos, 0, 1, sc, vis, 0);
2667 else
2668 add_member_decl (cls, id, regexp, pos, hash, 0, sc, vis, 0);
2669 }
2670 }
2671
2672 MATCH ();
2673 print_info ();
2674 }
2675 else if (LOOKING_AT ('{'))
2676 {
2677 /* A named enum. */
2678 if (sc == SC_TYPE && id && cls)
2679 {
2680 regexp = matching_regexp ();
2681 pos = BUFFER_POS ();
2682
2683 if (cls != NULL)
2684 {
2685 add_member_decl (cls, id, regexp, pos, 0, 1, sc, vis, 0);
2686 add_member_defn (cls, id, regexp, pos, 0, 1, sc, 0);
2687 }
2688 }
2689
2690 skip_matching ();
2691 print_info ();
2692 }
2693
2694 free (id);
2695 }
2696
2697
2698 /* Parse the body of class CLS. TAG is the tag of the class (struct,
2699 union, class). */
2700
2701 static void
2702 class_body (struct sym *cls, int tag)
2703 {
2704 int vis = tag == CLASS ? PRIVATE : PUBLIC;
2705 int temp;
2706
2707 while (!LOOKING_AT2 (YYEOF, '}'))
2708 {
2709 switch (LA1)
2710 {
2711 case PRIVATE: case PROTECTED: case PUBLIC:
2712 temp = LA1;
2713 MATCH ();
2714
2715 if (LOOKING_AT (':'))
2716 {
2717 vis = temp;
2718 MATCH ();
2719 }
2720 else
2721 {
2722 /* Probably conditional compilation for inheritance list.
2723 We don't known whether there comes more of this.
2724 This is only a crude fix that works most of the time. */
2725 do
2726 {
2727 MATCH ();
2728 }
2729 while (LOOKING_AT2 (IDENT, ',')
2730 || LOOKING_AT3 (PUBLIC, PROTECTED, PRIVATE));
2731 }
2732 break;
2733
2734 case TYPENAME:
2735 case USING:
2736 skip_to (';');
2737 break;
2738
2739 /* Try to synchronize */
2740 case CHAR: case CLASS: case CONST:
2741 case DOUBLE: case ENUM: case FLOAT: case INT:
2742 case LONG: case SHORT: case SIGNED: case STRUCT:
2743 case UNION: case UNSIGNED: case VOID: case VOLATILE:
2744 case TYPEDEF: case STATIC: case T_INLINE: case FRIEND:
2745 case VIRTUAL: case TEMPLATE: case IDENT: case '~':
2746 case BOOL: case WCHAR: case EXPLICIT: case MUTABLE:
2747 member (cls, vis);
2748 break;
2749
2750 default:
2751 MATCH ();
2752 break;
2753 }
2754 }
2755 }
2756
2757
2758 /* Parse a qualified identifier. Current lookahead is IDENT. A
2759 qualified ident has the form `X<..>::Y<...>::T<...>. Returns a
2760 symbol for that class. */
2761
2762 static struct sym *
2763 parse_classname (void)
2764 {
2765 struct sym *last_class = NULL;
2766
2767 while (LOOKING_AT (IDENT))
2768 {
2769 last_class = add_sym (yytext, last_class);
2770 MATCH ();
2771
2772 if (LOOKING_AT ('<'))
2773 {
2774 skip_matching ();
2775 SET_FLAG (last_class->flags, F_TEMPLATE);
2776 }
2777
2778 if (!LOOKING_AT (DCOLON))
2779 break;
2780
2781 MATCH ();
2782 }
2783
2784 return last_class;
2785 }
2786
2787
2788 /* Parse an operator name. Add the `static' flag to *SC if an
2789 implicitly static operator has been parsed. Value is a pointer to
2790 a static buffer holding the constructed operator name string. */
2791
2792 static char *
2793 operator_name (int *sc)
2794 {
2795 static int id_size = 0;
2796 static char *id = NULL;
2797 const char *s;
2798 int len;
2799
2800 MATCH ();
2801
2802 if (LOOKING_AT2 (NEW, DELETE))
2803 {
2804 /* `new' and `delete' are implicitly static. */
2805 if (*sc != SC_FRIEND)
2806 *sc = SC_STATIC;
2807
2808 s = token_string (LA1);
2809 MATCH ();
2810
2811 len = strlen (s) + 10;
2812 if (len > id_size)
2813 {
2814 int new_size = max (len, 2 * id_size);
2815 id = (char *) xrealloc (id, new_size);
2816 id_size = new_size;
2817 }
2818 strcpy (id, s);
2819
2820 /* Vector new or delete? */
2821 if (LOOKING_AT ('['))
2822 {
2823 strcat (id, "[");
2824 MATCH ();
2825
2826 if (LOOKING_AT (']'))
2827 {
2828 strcat (id, "]");
2829 MATCH ();
2830 }
2831 }
2832 }
2833 else
2834 {
2835 int tokens_matched = 0;
2836
2837 len = 20;
2838 if (len > id_size)
2839 {
2840 int new_size = max (len, 2 * id_size);
2841 id = (char *) xrealloc (id, new_size);
2842 id_size = new_size;
2843 }
2844 strcpy (id, "operator");
2845
2846 /* Beware access declarations of the form "X::f;" Beware of
2847 `operator () ()'. Yet another difficulty is found in
2848 GCC 2.95's STL: `operator == __STL_NULL_TMPL_ARGS (...'. */
2849 while (!(LOOKING_AT ('(') && tokens_matched)
2850 && !LOOKING_AT2 (';', YYEOF))
2851 {
2852 s = token_string (LA1);
2853 len += strlen (s) + 2;
2854 if (len > id_size)
2855 {
2856 int new_size = max (len, 2 * id_size);
2857 id = (char *) xrealloc (id, new_size);
2858 id_size = new_size;
2859 }
2860
2861 if (*s != ')' && *s != ']')
2862 strcat (id, " ");
2863 strcat (id, s);
2864 MATCH ();
2865
2866 /* If this is a simple operator like `+', stop now. */
2867 if (!isalpha ((unsigned char) *s) && *s != '(' && *s != '[')
2868 break;
2869
2870 ++tokens_matched;
2871 }
2872 }
2873
2874 return id;
2875 }
2876
2877
2878 /* This one consumes the last IDENT of a qualified member name like
2879 `X::Y::z'. This IDENT is returned in LAST_ID. Value is the
2880 symbol structure for the ident. */
2881
2882 static struct sym *
2883 parse_qualified_ident_or_type (char **last_id)
2884 {
2885 struct sym *cls = NULL;
2886 char *id = NULL;
2887 size_t id_size = 0;
2888 int enter = 0;
2889
2890 while (LOOKING_AT (IDENT))
2891 {
2892 int len = strlen (yytext) + 1;
2893 if (len > id_size)
2894 {
2895 id = (char *) xrealloc (id, len);
2896 id_size = len;
2897 }
2898 strcpy (id, yytext);
2899 *last_id = id;
2900 MATCH ();
2901
2902 SKIP_MATCHING_IF ('<');
2903
2904 if (LOOKING_AT (DCOLON))
2905 {
2906 struct sym *pcn = NULL;
2907 struct link *pna = check_namespace_alias (id);
2908 if (pna)
2909 {
2910 do
2911 {
2912 enter_namespace (pna->sym->name);
2913 enter++;
2914 pna = pna->next;
2915 }
2916 while (pna);
2917 }
2918 else if ((pcn = check_namespace (id, current_namespace)))
2919 {
2920 enter_namespace (pcn->name);
2921 enter++;
2922 }
2923 else
2924 cls = add_sym (id, cls);
2925
2926 *last_id = NULL;
2927 free (id);
2928 id = NULL;
2929 id_size = 0;
2930 MATCH ();
2931 }
2932 else
2933 break;
2934 }
2935
2936 while (enter--)
2937 leave_namespace();
2938
2939 return cls;
2940 }
2941
2942
2943 /* This one consumes the last IDENT of a qualified member name like
2944 `X::Y::z'. This IDENT is returned in LAST_ID. Value is the
2945 symbol structure for the ident. */
2946
2947 static void
2948 parse_qualified_param_ident_or_type (char **last_id)
2949 {
2950 struct sym *cls = NULL;
2951 static char *id = NULL;
2952 static int id_size = 0;
2953
2954 while (LOOKING_AT (IDENT))
2955 {
2956 int len = strlen (yytext) + 1;
2957 if (len > id_size)
2958 {
2959 id = (char *) xrealloc (id, len);
2960 id_size = len;
2961 }
2962 strcpy (id, yytext);
2963 *last_id = id;
2964 MATCH ();
2965
2966 SKIP_MATCHING_IF ('<');
2967
2968 if (LOOKING_AT (DCOLON))
2969 {
2970 cls = add_sym (id, cls);
2971 *last_id = NULL;
2972 MATCH ();
2973 }
2974 else
2975 break;
2976 }
2977 }
2978
2979
2980 /* Parse a class definition.
2981
2982 CONTAINING is the class containing the class being parsed or null.
2983 This may also be null if NESTED != 0 if the containing class is
2984 anonymous. TAG is the tag of the class (struct, union, class).
2985 NESTED is non-zero if we are parsing a nested class.
2986
2987 Current lookahead is the class name. */
2988
2989 static void
2990 class_definition (struct sym *containing, int tag, int flags, int nested)
2991 {
2992 struct sym *current;
2993 struct sym *base_class;
2994
2995 /* Set CURRENT to null if no entry has to be made for the class
2996 parsed. This is the case for certain command line flag
2997 settings. */
2998 if ((tag != CLASS && !f_structs) || (nested && !f_nested_classes))
2999 current = NULL;
3000 else
3001 {
3002 current = add_sym (yytext, containing);
3003 current->pos = BUFFER_POS ();
3004 current->regexp = matching_regexp ();
3005 current->filename = filename;
3006 current->flags = flags;
3007 }
3008
3009 /* If at ':', base class list follows. */
3010 if (LOOKING_AT (':'))
3011 {
3012 int done = 0;
3013 MATCH ();
3014
3015 while (!done)
3016 {
3017 switch (LA1)
3018 {
3019 case VIRTUAL: case PUBLIC: case PROTECTED: case PRIVATE:
3020 MATCH ();
3021 break;
3022
3023 case IDENT:
3024 base_class = parse_classname ();
3025 if (base_class && current && base_class != current)
3026 add_link (base_class, current);
3027 break;
3028
3029 /* The `,' between base classes or the end of the base
3030 class list. Add the previously found base class.
3031 It's done this way to skip over sequences of
3032 `A::B::C' until we reach the end.
3033
3034 FIXME: it is now possible to handle `class X : public B::X'
3035 because we have enough information. */
3036 case ',':
3037 MATCH ();
3038 break;
3039
3040 default:
3041 /* A syntax error, possibly due to preprocessor constructs
3042 like
3043
3044 #ifdef SOMETHING
3045 class A : public B
3046 #else
3047 class A : private B.
3048
3049 MATCH until we see something like `;' or `{'. */
3050 while (!LOOKING_AT3 (';', YYEOF, '{'))
3051 MATCH ();
3052 done = 1;
3053
3054 case '{':
3055 done = 1;
3056 break;
3057 }
3058 }
3059 }
3060
3061 /* Parse the class body if there is one. */
3062 if (LOOKING_AT ('{'))
3063 {
3064 if (tag != CLASS && !f_structs)
3065 skip_matching ();
3066 else
3067 {
3068 MATCH ();
3069 class_body (current, tag);
3070
3071 if (LOOKING_AT ('}'))
3072 {
3073 MATCH ();
3074 if (LOOKING_AT (';') && !nested)
3075 MATCH ();
3076 }
3077 }
3078 }
3079 }
3080
3081 /* Add to class *CLS information for the declaration of variable or
3082 type *ID. If *CLS is null, this means a global declaration. SC is
3083 the storage class of *ID. FLAGS is a bit set giving additional
3084 information about the member (see the F_* defines). */
3085
3086 static void
3087 add_declarator (struct sym **cls, char **id, int flags, int sc)
3088 {
3089 if (LOOKING_AT2 (';', ','))
3090 {
3091 /* The end of a member variable or of an access declaration
3092 `X::f'. To distinguish between them we have to know whether
3093 type information has been seen. */
3094 if (*id)
3095 {
3096 char *regexp = matching_regexp ();
3097 int pos = BUFFER_POS ();
3098
3099 if (*cls)
3100 add_member_defn (*cls, *id, regexp, pos, 0, 1, SC_UNKNOWN, flags);
3101 else
3102 add_global_defn (*id, regexp, pos, 0, 1, sc, flags);
3103 }
3104
3105 MATCH ();
3106 print_info ();
3107 }
3108 else if (LOOKING_AT ('{'))
3109 {
3110 if (sc == SC_TYPE && *id)
3111 {
3112 /* A named enumeration. */
3113 char *regexp = matching_regexp ();
3114 int pos = BUFFER_POS ();
3115 add_global_defn (*id, regexp, pos, 0, 1, sc, flags);
3116 }
3117
3118 skip_matching ();
3119 print_info ();
3120 }
3121
3122 free (*id);
3123 *id = NULL;
3124 *cls = NULL;
3125 }
3126
3127 /* Parse a declaration. */
3128
3129 static void
3130 declaration (int flags)
3131 {
3132 char *id = NULL;
3133 struct sym *cls = NULL;
3134 char *regexp = NULL;
3135 int pos = 0;
3136 unsigned hash = 0;
3137 int is_constructor;
3138 int sc = 0;
3139
3140 while (!LOOKING_AT3 (';', '{', YYEOF))
3141 {
3142 switch (LA1)
3143 {
3144 default:
3145 MATCH ();
3146 break;
3147
3148 case '[':
3149 skip_matching ();
3150 break;
3151
3152 case ENUM:
3153 case TYPEDEF:
3154 sc = SC_TYPE;
3155 MATCH ();
3156 break;
3157
3158 case STATIC:
3159 sc = SC_STATIC;
3160 MATCH ();
3161 break;
3162
3163 case INT: case CHAR: case LONG: case UNSIGNED:
3164 case SIGNED: case CONST: case DOUBLE: case VOID:
3165 case SHORT: case VOLATILE: case BOOL: case WCHAR:
3166 MATCH ();
3167 break;
3168
3169 case CLASS: case STRUCT: case UNION:
3170 /* This is for the case `STARTWRAP class X : ...' or
3171 `declare (X, Y)\n class A : ...'. */
3172 if (id)
3173 {
3174 free (id);
3175 return;
3176 }
3177
3178 case '=':
3179 /* Assumed to be the start of an initialization in this
3180 context. */
3181 skip_initializer ();
3182 break;
3183
3184 case ',':
3185 add_declarator (&cls, &id, flags, sc);
3186 break;
3187
3188 case OPERATOR:
3189 {
3190 char *s = operator_name (&sc);
3191 id = (char *) xrealloc (id, strlen (s) + 1);
3192 strcpy (id, s);
3193 }
3194 break;
3195
3196 case T_INLINE:
3197 SET_FLAG (flags, F_INLINE);
3198 MATCH ();
3199 break;
3200
3201 case '~':
3202 MATCH ();
3203 if (LOOKING_AT (IDENT))
3204 {
3205 id = (char *) xrealloc (id, strlen (yytext) + 2);
3206 *id = '~';
3207 strcpy (id + 1, yytext);
3208 MATCH ();
3209 }
3210 break;
3211
3212 case IDENT:
3213 cls = parse_qualified_ident_or_type (&id);
3214 break;
3215
3216 case '(':
3217 /* Most probably the beginning of a parameter list. */
3218 if (cls)
3219 {
3220 MATCH ();
3221
3222 if (id && cls)
3223 {
3224 if (!(is_constructor = streq (id, cls->name)))
3225 regexp = matching_regexp ();
3226 }
3227 else
3228 is_constructor = 0;
3229
3230 pos = BUFFER_POS ();
3231 hash = parm_list (&flags);
3232
3233 if (is_constructor)
3234 regexp = matching_regexp ();
3235
3236 if (id && cls)
3237 add_member_defn (cls, id, regexp, pos, hash, 0,
3238 SC_UNKNOWN, flags);
3239 }
3240 else
3241 {
3242 /* This may be a C functions, but also a macro
3243 call of the form `declare (A, B)' --- such macros
3244 can be found in some class libraries. */
3245 MATCH ();
3246
3247 if (id)
3248 {
3249 regexp = matching_regexp ();
3250 pos = BUFFER_POS ();
3251 hash = parm_list (&flags);
3252 add_global_decl (id, regexp, pos, hash, 0, sc, flags);
3253 }
3254
3255 /* This is for the case that the function really is
3256 a macro with no `;' following it. If a CLASS directly
3257 follows, we would miss it otherwise. */
3258 if (LOOKING_AT3 (CLASS, STRUCT, UNION))
3259 return;
3260 }
3261
3262 while (!LOOKING_AT3 (';', '{', YYEOF))
3263 MATCH ();
3264
3265 if (!cls && id && LOOKING_AT ('{'))
3266 add_global_defn (id, regexp, pos, hash, 0, sc, flags);
3267
3268 free (id);
3269 id = NULL;
3270 break;
3271 }
3272 }
3273
3274 add_declarator (&cls, &id, flags, sc);
3275 }
3276
3277
3278 /* Parse a list of top-level declarations/definitions. START_FLAGS
3279 says in which context we are parsing. If it is F_EXTERNC, we are
3280 parsing in an `extern "C"' block. Value is 1 if EOF is reached, 0
3281 otherwise. */
3282
3283 static int
3284 globals (int start_flags)
3285 {
3286 int anonymous;
3287 int class_tk;
3288 int flags = start_flags;
3289
3290 for (;;)
3291 {
3292 char *prev_in = in;
3293
3294 switch (LA1)
3295 {
3296 case NAMESPACE:
3297 {
3298 MATCH ();
3299
3300 if (LOOKING_AT (IDENT))
3301 {
3302 char *namespace_name = xstrdup (yytext);
3303 MATCH ();
3304
3305 if (LOOKING_AT ('='))
3306 {
3307 struct link *qna = match_qualified_namespace_alias ();
3308 if (qna)
3309 register_namespace_alias (namespace_name, qna);
3310
3311 if (skip_to (';') == ';')
3312 MATCH ();
3313 }
3314 else if (LOOKING_AT ('{'))
3315 {
3316 MATCH ();
3317 enter_namespace (namespace_name);
3318 globals (0);
3319 leave_namespace ();
3320 MATCH_IF ('}');
3321 }
3322
3323 free (namespace_name);
3324 }
3325 }
3326 break;
3327
3328 case EXTERN:
3329 MATCH ();
3330 if (LOOKING_AT (CSTRING) && *string_start == 'C'
3331 && *(string_start + 1) == '"')
3332 {
3333 /* This is `extern "C"'. */
3334 MATCH ();
3335
3336 if (LOOKING_AT ('{'))
3337 {
3338 MATCH ();
3339 globals (F_EXTERNC);
3340 MATCH_IF ('}');
3341 }
3342 else
3343 SET_FLAG (flags, F_EXTERNC);
3344 }
3345 break;
3346
3347 case TEMPLATE:
3348 MATCH ();
3349 SKIP_MATCHING_IF ('<');
3350 SET_FLAG (flags, F_TEMPLATE);
3351 break;
3352
3353 case CLASS: case STRUCT: case UNION:
3354 class_tk = LA1;
3355 MATCH ();
3356 anonymous = 1;
3357
3358 /* More than one ident here to allow for MS-DOS and OS/2
3359 specialties like `far', `_Export' etc. Some C++ libs
3360 have constructs like `_OS_DLLIMPORT(_OS_CLIENT)' in front
3361 of the class name. */
3362 while (!LOOKING_AT4 (YYEOF, ';', ':', '{'))
3363 {
3364 if (LOOKING_AT (IDENT))
3365 anonymous = 0;
3366 MATCH ();
3367 }
3368
3369 /* Don't add anonymous unions. */
3370 if (LOOKING_AT2 (':', '{') && !anonymous)
3371 class_definition (NULL, class_tk, flags, 0);
3372 else
3373 {
3374 if (skip_to (';') == ';')
3375 MATCH ();
3376 }
3377
3378 flags = start_flags;
3379 break;
3380
3381 case YYEOF:
3382 return 1;
3383
3384 case '}':
3385 return 0;
3386
3387 default:
3388 declaration (flags);
3389 flags = start_flags;
3390 break;
3391 }
3392
3393 if (prev_in == in)
3394 yyerror ("parse error", NULL);
3395 }
3396 }
3397
3398
3399 /* Parse the current input file. */
3400
3401 static void
3402 yyparse (void)
3403 {
3404 while (globals (0) == 0)
3405 MATCH_IF ('}');
3406 }
3407
3408
3409 \f
3410 /***********************************************************************
3411 Main Program
3412 ***********************************************************************/
3413
3414 /* Add the list of paths PATH_LIST to the current search path for
3415 input files. */
3416
3417 static void
3418 add_search_path (char *path_list)
3419 {
3420 while (*path_list)
3421 {
3422 char *start = path_list;
3423 struct search_path *p;
3424
3425 while (*path_list && *path_list != PATH_LIST_SEPARATOR)
3426 ++path_list;
3427
3428 p = (struct search_path *) xmalloc (sizeof *p);
3429 p->path = (char *) xmalloc (path_list - start + 1);
3430 memcpy (p->path, start, path_list - start);
3431 p->path[path_list - start] = '\0';
3432 p->next = NULL;
3433
3434 if (search_path_tail)
3435 {
3436 search_path_tail->next = p;
3437 search_path_tail = p;
3438 }
3439 else
3440 search_path = search_path_tail = p;
3441
3442 while (*path_list == PATH_LIST_SEPARATOR)
3443 ++path_list;
3444 }
3445 }
3446
3447
3448 /* Open FILE and return a file handle for it, or -1 if FILE cannot be
3449 opened. Try to find FILE in search_path first, then try the
3450 unchanged file name. */
3451
3452 static FILE *
3453 open_file (char *file)
3454 {
3455 FILE *fp = NULL;
3456 static char *buffer;
3457 static int buffer_size;
3458 struct search_path *path;
3459 int flen = strlen (file) + 1; /* +1 for the slash */
3460
3461 filename = xstrdup (file);
3462
3463 for (path = search_path; path && fp == NULL; path = path->next)
3464 {
3465 int len = strlen (path->path) + flen;
3466
3467 if (len + 1 >= buffer_size)
3468 {
3469 buffer_size = max (len + 1, 2 * buffer_size);
3470 buffer = (char *) xrealloc (buffer, buffer_size);
3471 }
3472
3473 strcpy (buffer, path->path);
3474 strcat (buffer, "/");
3475 strcat (buffer, file);
3476 fp = fopen (buffer, "r");
3477 }
3478
3479 /* Try the original file name. */
3480 if (fp == NULL)
3481 fp = fopen (file, "r");
3482
3483 if (fp == NULL)
3484 yyerror ("cannot open", NULL);
3485
3486 return fp;
3487 }
3488
3489
3490 /* Display usage information and exit program. */
3491
3492 #define USAGE "\
3493 Usage: ebrowse [options] {files}\n\
3494 \n\
3495 -a, --append append output to existing file\n\
3496 -f, --files=FILES read input file names from FILE\n\
3497 -I, --search-path=LIST set search path for input files\n\
3498 -m, --min-regexp-length=N set minimum regexp length to N\n\
3499 -M, --max-regexp-length=N set maximum regexp length to N\n\
3500 -n, --no-nested-classes exclude nested classes\n\
3501 -o, --output-file=FILE set output file name to FILE\n\
3502 -p, --position-info print info about position in file\n\
3503 -s, --no-structs-or-unions don't record structs or unions\n\
3504 -v, --verbose be verbose\n\
3505 -V, --very-verbose be very verbose\n\
3506 -x, --no-regexps don't record regular expressions\n\
3507 --help display this help\n\
3508 --version display version info\n\
3509 "
3510
3511 static void
3512 usage (int error)
3513 {
3514 puts (USAGE);
3515 exit (error ? EXIT_FAILURE : EXIT_SUCCESS);
3516 }
3517
3518
3519 /* Display version and copyright info. The VERSION macro is set
3520 from config.h and contains the Emacs version. */
3521
3522 #ifndef VERSION
3523 # define VERSION "21"
3524 #endif
3525
3526 static void
3527 version (void)
3528 {
3529 /* Makes it easier to update automatically. */
3530 char emacs_copyright[] = "Copyright (C) 2011 Free Software Foundation, Inc.";
3531
3532 printf ("ebrowse %s\n", VERSION);
3533 puts (emacs_copyright);
3534 puts ("This program is distributed under the same terms as Emacs.");
3535 exit (EXIT_SUCCESS);
3536 }
3537
3538
3539 /* Parse one input file FILE, adding classes and members to the symbol
3540 table. */
3541
3542 static void
3543 process_file (char *file)
3544 {
3545 FILE *fp;
3546
3547 fp = open_file (file);
3548 if (fp)
3549 {
3550 int nread, nbytes;
3551
3552 /* Give a progress indication if needed. */
3553 if (f_very_verbose)
3554 {
3555 puts (filename);
3556 fflush (stdout);
3557 }
3558 else if (f_verbose)
3559 {
3560 putchar ('.');
3561 fflush (stdout);
3562 }
3563
3564 /* Read file to inbuffer. */
3565 for (nread = 0;;)
3566 {
3567 if (nread + READ_CHUNK_SIZE >= inbuffer_size)
3568 {
3569 inbuffer_size = nread + READ_CHUNK_SIZE + 1;
3570 inbuffer = (char *) xrealloc (inbuffer, inbuffer_size);
3571 }
3572
3573 nbytes = fread (inbuffer + nread, 1, READ_CHUNK_SIZE, fp);
3574 if (nbytes <= 0)
3575 break;
3576 nread += nbytes;
3577 }
3578 if (nread < 0)
3579 nread = 0;
3580 inbuffer[nread] = '\0';
3581
3582 /* Reinitialize scanner and parser for the new input file. */
3583 re_init_scanner ();
3584 re_init_parser ();
3585
3586 /* Parse it and close the file. */
3587 yyparse ();
3588 fclose (fp);
3589 }
3590 }
3591
3592
3593 /* Read a line from stream FP and return a pointer to a static buffer
3594 containing its contents without the terminating newline. Value
3595 is null when EOF is reached. */
3596
3597 static char *
3598 read_line (FILE *fp)
3599 {
3600 static char *buffer;
3601 static int buffer_size;
3602 int i = 0, c;
3603
3604 while ((c = getc (fp)) != EOF && c != '\n')
3605 {
3606 if (i >= buffer_size)
3607 {
3608 buffer_size = max (100, buffer_size * 2);
3609 buffer = (char *) xrealloc (buffer, buffer_size);
3610 }
3611
3612 buffer[i++] = c;
3613 }
3614
3615 if (c == EOF && i == 0)
3616 return NULL;
3617
3618 if (i == buffer_size)
3619 {
3620 buffer_size = max (100, buffer_size * 2);
3621 buffer = (char *) xrealloc (buffer, buffer_size);
3622 }
3623
3624 buffer[i] = '\0';
3625 if (i > 0 && buffer[i - 1] == '\r')
3626 buffer[i - 1] = '\0';
3627 return buffer;
3628 }
3629
3630
3631 /* Main entry point. */
3632
3633 int
3634 main (int argc, char **argv)
3635 {
3636 int i;
3637 int any_inputfiles = 0;
3638 static const char *out_filename = DEFAULT_OUTFILE;
3639 static char **input_filenames = NULL;
3640 static int input_filenames_size = 0;
3641 static int n_input_files;
3642
3643 filename = "command line";
3644 yyout = stdout;
3645
3646 while ((i = getopt_long (argc, argv, "af:I:m:M:no:p:svVx",
3647 options, NULL)) != EOF)
3648 {
3649 switch (i)
3650 {
3651 /* Experimental. */
3652 case 'p':
3653 info_position = atoi (optarg);
3654 break;
3655
3656 case 'n':
3657 f_nested_classes = 0;
3658 break;
3659
3660 case 'x':
3661 f_regexps = 0;
3662 break;
3663
3664 /* Add the name of a file containing more input files. */
3665 case 'f':
3666 if (n_input_files == input_filenames_size)
3667 {
3668 input_filenames_size = max (10, 2 * input_filenames_size);
3669 input_filenames = (char **) xrealloc ((void *)input_filenames,
3670 input_filenames_size);
3671 }
3672 input_filenames[n_input_files++] = xstrdup (optarg);
3673 break;
3674
3675 /* Append new output to output file instead of truncating it. */
3676 case 'a':
3677 f_append = 1;
3678 break;
3679
3680 /* Include structs in the output */
3681 case 's':
3682 f_structs = 0;
3683 break;
3684
3685 /* Be verbose (give a progress indication). */
3686 case 'v':
3687 f_verbose = 1;
3688 break;
3689
3690 /* Be very verbose (print file names as they are processed). */
3691 case 'V':
3692 f_verbose = 1;
3693 f_very_verbose = 1;
3694 break;
3695
3696 /* Change the name of the output file. */
3697 case 'o':
3698 out_filename = optarg;
3699 break;
3700
3701 /* Set minimum length for regular expression strings
3702 when recorded in the output file. */
3703 case 'm':
3704 min_regexp = atoi (optarg);
3705 break;
3706
3707 /* Set maximum length for regular expression strings
3708 when recorded in the output file. */
3709 case 'M':
3710 max_regexp = atoi (optarg);
3711 break;
3712
3713 /* Add to search path. */
3714 case 'I':
3715 add_search_path (optarg);
3716 break;
3717
3718 /* Display help */
3719 case -2:
3720 usage (0);
3721 break;
3722
3723 case -3:
3724 version ();
3725 break;
3726 }
3727 }
3728
3729 /* Call init_scanner after command line flags have been processed to be
3730 able to add keywords depending on command line (not yet
3731 implemented). */
3732 init_scanner ();
3733 init_sym ();
3734
3735 /* Open output file */
3736 if (*out_filename)
3737 {
3738 if (f_append)
3739 {
3740 /* Check that the file to append to exists, and is not
3741 empty. More specifically, it should be a valid file
3742 produced by a previous run of ebrowse, but that's too
3743 difficult to check. */
3744 FILE *fp;
3745 int rc;
3746
3747 fp = fopen (out_filename, "r");
3748 if (fp == NULL)
3749 {
3750 yyerror ("file `%s' must exist for --append", out_filename);
3751 exit (EXIT_FAILURE);
3752 }
3753
3754 rc = fseek (fp, 0, SEEK_END);
3755 if (rc == -1)
3756 {
3757 yyerror ("error seeking in file `%s'", out_filename);
3758 exit (EXIT_FAILURE);
3759 }
3760
3761 rc = ftell (fp);
3762 if (rc == -1)
3763 {
3764 yyerror ("error getting size of file `%s'", out_filename);
3765 exit (EXIT_FAILURE);
3766 }
3767
3768 else if (rc == 0)
3769 {
3770 yyerror ("file `%s' is empty", out_filename);
3771 /* It may be ok to use an empty file for appending.
3772 exit (EXIT_FAILURE); */
3773 }
3774
3775 fclose (fp);
3776 }
3777
3778 yyout = fopen (out_filename, f_append ? "a" : "w");
3779 if (yyout == NULL)
3780 {
3781 yyerror ("cannot open output file `%s'", out_filename);
3782 exit (EXIT_FAILURE);
3783 }
3784 }
3785
3786 /* Process input files specified on the command line. */
3787 while (optind < argc)
3788 {
3789 process_file (argv[optind++]);
3790 any_inputfiles = 1;
3791 }
3792
3793 /* Process files given on stdin if no files specified. */
3794 if (!any_inputfiles && n_input_files == 0)
3795 {
3796 char *file;
3797 while ((file = read_line (stdin)) != NULL)
3798 process_file (file);
3799 }
3800 else
3801 {
3802 /* Process files from `--files=FILE'. Every line in FILE names
3803 one input file to process. */
3804 for (i = 0; i < n_input_files; ++i)
3805 {
3806 FILE *fp = fopen (input_filenames[i], "r");
3807
3808 if (fp == NULL)
3809 yyerror ("cannot open input file `%s'", input_filenames[i]);
3810 else
3811 {
3812 char *file;
3813 while ((file = read_line (fp)) != NULL)
3814 process_file (file);
3815 fclose (fp);
3816 }
3817 }
3818 }
3819
3820 /* Write output file. */
3821 dump_roots (yyout);
3822
3823 /* Close output file. */
3824 if (yyout != stdout)
3825 fclose (yyout);
3826
3827 return EXIT_SUCCESS;
3828 }
3829
3830 /* ebrowse.c ends here */