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