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