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