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