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