(comment-indent): Paren typo.
[emacs.git] / lib-src / ebrowse.c
blob7bceae94c340548c1d2eb72874fd9c25de561e89
1 /* ebrowse.c --- parsing files for the ebrowse C++ browser
3 Copyright (C) 1992-1999, 2000 Free Software Foundation Inc.
5 Author: Gerd Moellmann <gerd@gnu.org>
6 Maintainer: FSF
8 This file is part of GNU Emacs.
10 GNU Emacs is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2, or (at your option)
13 any later version.
15 GNU Emacs is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with GNU Emacs; see the file COPYING. If not, write to
22 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <ctype.h>
32 #include <assert.h>
33 #include "getopt.h"
35 /* Conditionalize function prototypes. */
37 #ifdef PROTOTYPES /* From config.h. */
38 #define P_(x) x
39 #else
40 #define P_(x) ()
41 #endif
43 /* Value is non-zero if strings X and Y compare equal. */
45 #define streq(X, Y) (*(X) == *(Y) && strcmp ((X) + 1, (Y) + 1) == 0)
47 /* The ubiquitous `max' and `min' macros. */
49 #ifndef max
50 #define max(X, Y) ((X) > (Y) ? (X) : (Y))
51 #define min(X, Y) ((X) < (Y) ? (X) : (Y))
52 #endif
54 /* Files are read in chunks of this number of bytes. */
56 #define READ_CHUNK_SIZE (100 * 1024)
58 /* The character used as a separator in path lists (like $PATH). */
60 #if defined(__MSDOS__)
61 #define PATH_LIST_SEPARATOR ';'
62 #define FILENAME_EQ(X,Y) (strcasecmp(X,Y) == 0)
63 #else
64 #if defined(WINDOWSNT)
65 #define PATH_LIST_SEPARATOR ';'
66 #define FILENAME_EQ(X,Y) (stricmp(X,Y) == 0)
67 #else
68 #define PATH_LIST_SEPARATOR ':'
69 #define FILENAME_EQ(X,Y) (streq(X,Y))
70 #endif
71 #endif
72 /* The default output file name. */
74 #define DEFAULT_OUTFILE "BROWSE"
76 /* A version string written to the output file. Change this whenever
77 the structure of the output file changes. */
79 #define EBROWSE_FILE_VERSION "ebrowse 5.0"
81 /* The output file consists of a tree of Lisp objects, with major
82 nodes built out of Lisp structures. These are the heads of the
83 Lisp structs with symbols identifying their type. */
85 #define TREE_HEADER_STRUCT "[ebrowse-hs "
86 #define TREE_STRUCT "[ebrowse-ts "
87 #define MEMBER_STRUCT "[ebrowse-ms "
88 #define BROWSE_STRUCT "[ebrowse-bs "
89 #define CLASS_STRUCT "[ebrowse-cs "
91 /* The name of the symbol table entry for global functions, variables,
92 defines etc. This name also appears in the browser display. */
94 #define GLOBALS_NAME "*Globals*"
96 /* Token definitions. */
98 enum token
100 YYEOF = 0, /* end of file */
101 CSTRING = 256, /* string constant */
102 CCHAR, /* character constant */
103 CINT, /* integral constant */
104 CFLOAT, /* real constant */
106 ELLIPSIS, /* ... */
107 LSHIFTASGN, /* <<= */
108 RSHIFTASGN, /* >>= */
109 ARROWSTAR, /* ->* */
110 IDENT, /* identifier */
111 DIVASGN, /* /= */
112 INC, /* ++ */
113 ADDASGN, /* += */
114 DEC, /* -- */
115 ARROW, /* -> */
116 SUBASGN, /* -= */
117 MULASGN, /* *= */
118 MODASGN, /* %= */
119 LOR, /* || */
120 ORASGN, /* |= */
121 LAND, /* && */
122 ANDASGN, /* &= */
123 XORASGN, /* ^= */
124 POINTSTAR, /* .* */
125 DCOLON, /* :: */
126 EQ, /* == */
127 NE, /* != */
128 LE, /* <= */
129 LSHIFT, /* << */
130 GE, /* >= */
131 RSHIFT, /* >> */
133 /* Keywords. The undef's are there because these
134 three symbols are very likely to be defined somewhere. */
135 #undef BOOL
136 #undef TRUE
137 #undef FALSE
139 ASM, /* asm */
140 AUTO, /* auto */
141 BREAK, /* break */
142 CASE, /* case */
143 CATCH, /* catch */
144 CHAR, /* char */
145 CLASS, /* class */
146 CONST, /* const */
147 CONTINUE, /* continue */
148 DEFAULT, /* default */
149 DELETE, /* delete */
150 DO, /* do */
151 DOUBLE, /* double */
152 ELSE, /* else */
153 ENUM, /* enum */
154 EXTERN, /* extern */
155 FLOAT, /* float */
156 FOR, /* for */
157 FRIEND, /* friend */
158 GOTO, /* goto */
159 IF, /* if */
160 T_INLINE, /* inline */
161 INT, /* int */
162 LONG, /* long */
163 NEW, /* new */
164 OPERATOR, /* operator */
165 PRIVATE, /* private */
166 PROTECTED, /* protected */
167 PUBLIC, /* public */
168 REGISTER, /* register */
169 RETURN, /* return */
170 SHORT, /* short */
171 SIGNED, /* signed */
172 SIZEOF, /* sizeof */
173 STATIC, /* static */
174 STRUCT, /* struct */
175 SWITCH, /* switch */
176 TEMPLATE, /* template */
177 THIS, /* this */
178 THROW, /* throw */
179 TRY, /* try */
180 TYPEDEF, /* typedef */
181 UNION, /* union */
182 UNSIGNED, /* unsigned */
183 VIRTUAL, /* virtual */
184 VOID, /* void */
185 VOLATILE, /* volatile */
186 WHILE, /* while */
187 MUTABLE, /* mutable */
188 BOOL, /* bool */
189 TRUE, /* true */
190 FALSE, /* false */
191 SIGNATURE, /* signature (GNU extension) */
192 NAMESPACE, /* namespace */
193 EXPLICIT, /* explicit */
194 TYPENAME, /* typename */
195 CONST_CAST, /* const_cast */
196 DYNAMIC_CAST, /* dynamic_cast */
197 REINTERPRET_CAST, /* reinterpret_cast */
198 STATIC_CAST, /* static_cast */
199 TYPEID, /* typeid */
200 USING, /* using */
201 WCHAR /* wchar_t */
204 /* Storage classes, in a wider sense. */
206 enum sc
208 SC_UNKNOWN,
209 SC_MEMBER, /* Is an instance member. */
210 SC_STATIC, /* Is static member. */
211 SC_FRIEND, /* Is friend function. */
212 SC_TYPE /* Is a type definition. */
215 /* Member visibility. */
217 enum visibility
219 V_PUBLIC,
220 V_PROTECTED,
221 V_PRIVATE
224 /* Member flags. */
226 #define F_VIRTUAL 1 /* Is virtual function. */
227 #define F_INLINE 2 /* Is inline function. */
228 #define F_CONST 4 /* Is const. */
229 #define F_PURE 8 /* Is pure virtual function. */
230 #define F_MUTABLE 16 /* Is mutable. */
231 #define F_TEMPLATE 32 /* Is a template. */
232 #define F_EXPLICIT 64 /* Is explicit constructor. */
233 #define F_THROW 128 /* Has a throw specification. */
234 #define F_EXTERNC 256 /* Is declared extern "C". */
235 #define F_DEFINE 512 /* Is a #define. */
237 /* Two macros to set and test a bit in an int. */
239 #define SET_FLAG(F, FLAG) ((F) |= (FLAG))
240 #define HAS_FLAG(F, FLAG) (((F) & (FLAG)) != 0)
242 /* Structure describing a class member. */
244 struct member
246 struct member *next; /* Next in list of members. */
247 struct member *anext; /* Collision chain in member_table. */
248 struct member **list; /* Pointer to list in class. */
249 unsigned param_hash; /* Hash value for parameter types. */
250 int vis; /* Visibility (public, ...). */
251 int flags; /* See F_* above. */
252 char *regexp; /* Matching regular expression. */
253 char *filename; /* Don't free this shared string. */
254 int pos; /* Buffer position of occurrence. */
255 char *def_regexp; /* Regular expression matching definition. */
256 char *def_filename; /* File name of definition. */
257 int def_pos; /* Buffer position of definition. */
258 char name[1]; /* Member name. */
261 /* Structures of this type are used to connect class structures with
262 their super and subclasses. */
264 struct link
266 struct sym *sym; /* The super or subclass. */
267 struct link *next; /* Next in list or NULL. */
270 /* Structure used to record namespace aliases. */
272 struct alias
274 struct alias *next; /* Next in list. */
275 char name[1]; /* Alias name. */
278 /* The structure used to describe a class in the symbol table,
279 or a namespace in all_namespaces. */
281 struct sym
283 int flags; /* Is class a template class?. */
284 unsigned char visited; /* Used to find circles. */
285 struct sym *next; /* Hash collision list. */
286 struct link *subs; /* List of subclasses. */
287 struct link *supers; /* List of superclasses. */
288 struct member *vars; /* List of instance variables. */
289 struct member *fns; /* List of instance functions. */
290 struct member *static_vars; /* List of static variables. */
291 struct member *static_fns; /* List of static functions. */
292 struct member *friends; /* List of friend functions. */
293 struct member *types; /* List of local types. */
294 char *regexp; /* Matching regular expression. */
295 int pos; /* Buffer position. */
296 char *filename; /* File in which it can be found. */
297 char *sfilename; /* File in which members can be found. */
298 struct sym *namesp; /* Namespace in which defined. . */
299 struct alias *namesp_aliases; /* List of aliases for namespaces. */
300 char name[1]; /* Name of the class. */
303 /* Experimental: Print info for `--position-info'. We print
304 '(CLASS-NAME SCOPE MEMBER-NAME). */
306 #define P_DEFN 1
307 #define P_DECL 2
309 int info_where;
310 struct sym *info_cls = NULL;
311 struct member *info_member = NULL;
313 /* Experimental. For option `--position-info', the buffer position we
314 are interested in. When this position is reached, print out
315 information about what we know about that point. */
317 int info_position = -1;
319 /* Command line options structure for getopt_long. */
321 struct option options[] =
323 {"append", no_argument, NULL, 'a'},
324 {"files", required_argument, NULL, 'f'},
325 {"help", no_argument, NULL, -2},
326 {"min-regexp-length", required_argument, NULL, 'm'},
327 {"max-regexp-length", required_argument, NULL, 'M'},
328 {"no-nested-classes", no_argument, NULL, 'n'},
329 {"no-regexps", no_argument, NULL, 'x'},
330 {"no-structs-or-unions", no_argument, NULL, 's'},
331 {"output-file", required_argument, NULL, 'o'},
332 {"position-info", required_argument, NULL, 'p'},
333 {"search-path", required_argument, NULL, 'I'},
334 {"verbose", no_argument, NULL, 'v'},
335 {"version", no_argument, NULL, -3},
336 {"very-verbose", no_argument, NULL, 'V'},
337 {NULL, 0, NULL, 0}
340 /* Semantic values of tokens. Set by yylex.. */
342 unsigned yyival; /* Set for token CINT. */
343 char *yytext; /* Set for token IDENT. */
344 char *yytext_end;
346 /* Output file. */
348 FILE *yyout;
350 /* Current line number. */
352 int yyline;
354 /* The name of the current input file. */
356 char *filename;
358 /* Three character class vectors, and macros to test membership
359 of characters. */
361 char is_ident[255];
362 char is_digit[255];
363 char is_white[255];
365 #define IDENTP(C) is_ident[(unsigned char) (C)]
366 #define DIGITP(C) is_digit[(unsigned char) (C)]
367 #define WHITEP(C) is_white[(unsigned char) (C)]
369 /* Command line flags. */
371 int f_append;
372 int f_verbose;
373 int f_very_verbose;
374 int f_structs = 1;
375 int f_regexps = 1;
376 int f_nested_classes = 1;
378 /* Maximum and minimum lengths of regular expressions matching a
379 member, class etc., for writing them to the output file. These are
380 overridable from the command line. */
382 int min_regexp = 5;
383 int max_regexp = 50;
385 /* Input buffer. */
387 char *inbuffer;
388 char *in;
389 int inbuffer_size;
391 /* Return the current buffer position in the input file. */
393 #define BUFFER_POS() (in - inbuffer)
395 /* If current lookahead is CSTRING, the following points to the
396 first character in the string constant. Used for recognizing
397 extern "C". */
399 char *string_start;
401 /* The size of the hash tables for classes.and members. Should be
402 prime. */
404 #define TABLE_SIZE 1001
406 /* The hash table for class symbols. */
408 struct sym *class_table[TABLE_SIZE];
410 /* Hash table containing all member structures. This is generally
411 faster for member lookup than traversing the member lists of a
412 `struct sym'. */
414 struct member *member_table[TABLE_SIZE];
416 /* The special class symbol used to hold global functions,
417 variables etc. */
419 struct sym *global_symbols;
421 /* The current namespace. */
423 struct sym *current_namespace;
425 /* The list of all known namespaces. */
427 struct sym *all_namespaces;
429 /* Stack of namespaces we're currently nested in, during the parse. */
431 struct sym **namespace_stack;
432 int namespace_stack_size;
433 int namespace_sp;
435 /* The current lookahead token. */
437 int tk = -1;
439 /* Structure describing a keyword. */
441 struct kw
443 char *name; /* Spelling. */
444 int tk; /* Token value. */
445 struct kw *next; /* Next in collision chain. */
448 /* Keywords are lookup up in a hash table of their own. */
450 #define KEYWORD_TABLE_SIZE 1001
451 struct kw *keyword_table[KEYWORD_TABLE_SIZE];
453 /* Search path. */
455 struct search_path
457 char *path;
458 struct search_path *next;
461 struct search_path *search_path;
462 struct search_path *search_path_tail;
464 /* Function prototypes. */
466 int yylex P_ ((void));
467 void yyparse P_ ((void));
468 void re_init_parser P_ ((void));
469 char *token_string P_ ((int));
470 char *matching_regexp P_ ((void));
471 void init_sym P_ ((void));
472 struct sym *add_sym P_ ((char *, struct sym *));
473 void add_link P_ ((struct sym *, struct sym *));
474 void add_member_defn P_ ((struct sym *, char *, char *,
475 int, unsigned, int, int, int));
476 void add_member_decl P_ ((struct sym *, char *, char *, int,
477 unsigned, int, int, int, int));
478 void dump_roots P_ ((FILE *));
479 void *xmalloc P_ ((int));
480 void add_global_defn P_ ((char *, char *, int, unsigned, int, int, int));
481 void add_global_decl P_ ((char *, char *, int, unsigned, int, int, int));
482 void add_define P_ ((char *, char *, int));
483 void mark_inherited_virtual P_ ((void));
484 void leave_namespace P_ ((void));
485 void enter_namespace P_ ((char *));
486 void register_namespace_alias P_ ((char *, char *));
487 void insert_keyword P_ ((char *, int));
488 void re_init_scanner P_ ((void));
489 void init_scanner P_ ((void));
490 void usage P_ ((int));
491 void version P_ ((void));
492 void process_file P_ ((char *));
493 void add_search_path P_ ((char *));
494 FILE *open_file P_ ((char *));
495 int process_pp_line P_ ((void));
496 int dump_members P_ ((FILE *, struct member *));
497 void dump_sym P_ ((FILE *, struct sym *));
498 int dump_tree P_ ((FILE *, struct sym *));
499 struct member *find_member P_ ((struct sym *, char *, int, int, unsigned));
500 struct member *add_member P_ ((struct sym *, char *, int, int, unsigned));
501 void mark_virtual P_ ((struct sym *));
502 void mark_virtual P_ ((struct sym *));
503 struct sym *make_namespace P_ ((char *));
504 char *sym_scope P_ ((struct sym *));
505 char *sym_scope_1 P_ ((struct sym *));
506 int skip_to P_ ((int));
507 void skip_matching P_ ((void));
508 void member P_ ((struct sym *, int));
509 void class_body P_ ((struct sym *, int));
510 void class_definition P_ ((struct sym *, int, int, int));
511 void declaration P_ ((int));
512 unsigned parm_list P_ ((int *));
513 char *operator_name P_ ((int *));
514 struct sym *parse_classname P_ ((void));
515 struct sym *parse_qualified_ident_or_type P_ ((char **));
516 void parse_qualified_param_ident_or_type P_ ((char **));
517 int globals P_ ((int));
521 /***********************************************************************
522 Utilities
523 ***********************************************************************/
525 /* Print an error in a printf-like style with the current input file
526 name and line number. */
528 void
529 yyerror (format, a1, a2, a3, a4, a5)
530 char *format;
531 int a1, a2, a3, a4, a5;
533 fprintf (stderr, "%s:%d: ", filename, yyline);
534 fprintf (stderr, format, a1, a2, a3, a4, a5);
535 putc ('\n', stderr);
539 /* Like malloc but print an error and exit if not enough memory is
540 available. */
542 void *
543 xmalloc (nbytes)
544 int nbytes;
546 void *p = malloc (nbytes);
547 if (p == NULL)
549 yyerror ("out of memory");
550 exit (1);
552 return p;
556 /* Like realloc but print an error and exit if out of memory. */
558 void *
559 xrealloc (p, sz)
560 void *p;
561 int sz;
563 p = realloc (p, sz);
564 if (p == NULL)
566 yyerror ("out of memory");
567 exit (1);
569 return p;
573 /* Like strdup, but print an error and exit if not enough memory is
574 available.. If S is null, return null. */
576 char *
577 xstrdup (s)
578 char *s;
580 if (s)
581 s = strcpy (xmalloc (strlen (s) + 1), s);
582 return s;
587 /***********************************************************************
588 Symbols
589 ***********************************************************************/
591 /* Initialize the symbol table. This currently only sets up the
592 special symbol for globals (`*Globals*'). */
594 void
595 init_sym ()
597 global_symbols = add_sym (GLOBALS_NAME, NULL);
601 /* Add a symbol for class NAME to the symbol table. NESTED_IN_CLASS
602 is the class in which class NAME was found. If it is null,
603 this means the scope of NAME is the current namespace.
605 If a symbol for NAME already exists, return that. Otherwise
606 create a new symbol and set it to default values. */
608 struct sym *
609 add_sym (name, nested_in_class)
610 char *name;
611 struct sym *nested_in_class;
613 struct sym *sym;
614 unsigned h;
615 char *s;
616 struct sym *scope = nested_in_class ? nested_in_class : current_namespace;
618 for (s = name, h = 0; *s; ++s)
619 h = (h << 1) ^ *s;
620 h %= TABLE_SIZE;
622 for (sym = class_table[h]; sym; sym = sym->next)
623 if (streq (name, sym->name) && sym->namesp == scope)
624 break;
626 if (sym == NULL)
628 if (f_very_verbose)
630 putchar ('\t');
631 puts (name);
634 sym = (struct sym *) xmalloc (sizeof *sym + strlen (name));
635 bzero (sym, sizeof *sym);
636 strcpy (sym->name, name);
637 sym->namesp = scope;
638 sym->next = class_table[h];
639 class_table[h] = sym;
642 return sym;
646 /* Add links between superclass SUPER and subclass SUB. */
648 void
649 add_link (super, sub)
650 struct sym *super, *sub;
652 struct link *lnk, *lnk2, *p, *prev;
654 /* See if a link already exists. */
655 for (p = super->subs, prev = NULL;
656 p && strcmp (sub->name, p->sym->name) > 0;
657 prev = p, p = p->next)
660 /* Avoid duplicates. */
661 if (p == NULL || p->sym != sub)
663 lnk = (struct link *) xmalloc (sizeof *lnk);
664 lnk2 = (struct link *) xmalloc (sizeof *lnk2);
666 lnk->sym = sub;
667 lnk->next = p;
669 if (prev)
670 prev->next = lnk;
671 else
672 super->subs = lnk;
674 lnk2->sym = super;
675 lnk2->next = sub->supers;
676 sub->supers = lnk2;
681 /* Find in class CLS member NAME.
683 VAR non-zero means look for a member variable; otherwise a function
684 is searched. SC specifies what kind of member is searched---a
685 static, or per-instance member etc. HASH is a hash code for the
686 parameter types of functions. Value is a pointer to the member
687 found or null if not found. */
689 struct member *
690 find_member (cls, name, var, sc, hash)
691 struct sym *cls;
692 char *name;
693 int var, sc;
694 unsigned hash;
696 struct member **list;
697 struct member *p;
698 unsigned name_hash = 0;
699 char *s;
700 int i;
702 switch (sc)
704 case SC_FRIEND:
705 list = &cls->friends;
706 break;
708 case SC_TYPE:
709 list = &cls->types;
710 break;
712 case SC_STATIC:
713 list = var ? &cls->static_vars : &cls->static_fns;
714 break;
716 default:
717 list = var ? &cls->vars : &cls->fns;
718 break;
721 for (s = name; *s; ++s)
722 name_hash = (name_hash << 1) ^ *s;
723 i = name_hash % TABLE_SIZE;
725 for (p = member_table[i]; p; p = p->anext)
726 if (p->list == list && p->param_hash == hash && streq (name, p->name))
727 break;
729 return p;
733 /* Add to class CLS information for the declaration of member NAME.
734 REGEXP is a regexp matching the declaration, if non-null. POS is
735 the position in the source where the declaration is found. HASH is
736 a hash code for the parameter list of the member, if it's a
737 function. VAR non-zero means member is a variable or type. SC
738 specifies the type of member (instance member, static, ...). VIS
739 is the member's visibility (public, protected, private). FLAGS is
740 a bit set giving additional information about the member (see the
741 F_* defines). */
743 void
744 add_member_decl (cls, name, regexp, pos, hash, var, sc, vis, flags)
745 struct sym *cls;
746 char *name;
747 char *regexp;
748 int pos;
749 unsigned hash;
750 int var;
751 int sc;
752 int vis;
753 int flags;
755 struct member *m;
757 m = find_member (cls, name, var, sc, hash);
758 if (m == NULL)
759 m = add_member (cls, name, var, sc, hash);
761 /* Have we seen a new filename? If so record that. */
762 if (!cls->filename || !FILENAME_EQ (cls->filename, filename))
763 m->filename = filename;
765 m->regexp = regexp;
766 m->pos = pos;
767 m->flags = flags;
769 switch (vis)
771 case PRIVATE:
772 m->vis = V_PRIVATE;
773 break;
775 case PROTECTED:
776 m->vis = V_PROTECTED;
777 break;
779 case PUBLIC:
780 m->vis = V_PUBLIC;
781 break;
784 info_where = P_DECL;
785 info_cls = cls;
786 info_member = m;
790 /* Add to class CLS information for the definition of member NAME.
791 REGEXP is a regexp matching the declaration, if non-null. POS is
792 the position in the source where the declaration is found. HASH is
793 a hash code for the parameter list of the member, if it's a
794 function. VAR non-zero means member is a variable or type. SC
795 specifies the type of member (instance member, static, ...). VIS
796 is the member's visibility (public, protected, private). FLAGS is
797 a bit set giving additional information about the member (see the
798 F_* defines). */
800 void
801 add_member_defn (cls, name, regexp, pos, hash, var, sc, flags)
802 struct sym *cls;
803 char *name;
804 char *regexp;
805 int pos;
806 unsigned hash;
807 int var;
808 int sc;
809 int flags;
811 struct member *m;
813 if (sc == SC_UNKNOWN)
815 m = find_member (cls, name, var, SC_MEMBER, hash);
816 if (m == NULL)
818 m = find_member (cls, name, var, SC_STATIC, hash);
819 if (m == NULL)
820 m = add_member (cls, name, var, sc, hash);
823 else
825 m = find_member (cls, name, var, sc, hash);
826 if (m == NULL)
827 m = add_member (cls, name, var, sc, hash);
830 if (!cls->sfilename)
831 cls->sfilename = filename;
833 if (!FILENAME_EQ (cls->sfilename, filename))
834 m->def_filename = filename;
836 m->def_regexp = regexp;
837 m->def_pos = pos;
838 m->flags |= flags;
840 info_where = P_DEFN;
841 info_cls = cls;
842 info_member = m;
846 /* Add a symbol for a define named NAME to the symbol table.
847 REGEXP is a regular expression matching the define in the source,
848 if it is non-null. POS is the position in the file. */
850 void
851 add_define (name, regexp, pos)
852 char *name, *regexp;
853 int pos;
855 add_global_defn (name, regexp, pos, 0, 1, SC_FRIEND, F_DEFINE);
856 add_global_decl (name, regexp, pos, 0, 1, SC_FRIEND, F_DEFINE);
860 /* Add information for the global definition of NAME.
861 REGEXP is a regexp matching the declaration, if non-null. POS is
862 the position in the source where the declaration is found. HASH is
863 a hash code for the parameter list of the member, if it's a
864 function. VAR non-zero means member is a variable or type. SC
865 specifies the type of member (instance member, static, ...). VIS
866 is the member's visibility (public, protected, private). FLAGS is
867 a bit set giving additional information about the member (see the
868 F_* defines). */
870 void
871 add_global_defn (name, regexp, pos, hash, var, sc, flags)
872 char *name, *regexp;
873 int pos;
874 unsigned hash;
875 int var;
876 int sc;
877 int flags;
879 int i;
880 struct sym *sym;
882 /* Try to find out for which classes a function is a friend, and add
883 what we know about it to them. */
884 if (!var)
885 for (i = 0; i < TABLE_SIZE; ++i)
886 for (sym = class_table[i]; sym; sym = sym->next)
887 if (sym != global_symbols && sym->friends)
888 if (find_member (sym, name, 0, SC_FRIEND, hash))
889 add_member_defn (sym, name, regexp, pos, hash, 0,
890 SC_FRIEND, flags);
892 /* Add to global symbols. */
893 add_member_defn (global_symbols, name, regexp, pos, hash, var, sc, flags);
897 /* Add information for the global declaration of NAME.
898 REGEXP is a regexp matching the declaration, if non-null. POS is
899 the position in the source where the declaration is found. HASH is
900 a hash code for the parameter list of the member, if it's a
901 function. VAR non-zero means member is a variable or type. SC
902 specifies the type of member (instance member, static, ...). VIS
903 is the member's visibility (public, protected, private). FLAGS is
904 a bit set giving additional information about the member (see the
905 F_* defines). */
907 void
908 add_global_decl (name, regexp, pos, hash, var, sc, flags)
909 char *name, *regexp;
910 int pos;
911 unsigned hash;
912 int var;
913 int sc;
914 int flags;
916 /* Add declaration only if not already declared. Header files must
917 be processed before source files for this to have the right effect.
918 I do not want to handle implicit declarations at the moment. */
919 struct member *m;
920 struct member *found;
922 m = found = find_member (global_symbols, name, var, sc, hash);
923 if (m == NULL)
924 m = add_member (global_symbols, name, var, sc, hash);
926 /* Definition already seen => probably last declaration implicit.
927 Override. This means that declarations must always be added to
928 the symbol table before definitions. */
929 if (!found)
931 if (!global_symbols->filename
932 || !FILENAME_EQ (global_symbols->filename, filename))
933 m->filename = filename;
935 m->regexp = regexp;
936 m->pos = pos;
937 m->vis = V_PUBLIC;
938 m->flags = flags;
940 info_where = P_DECL;
941 info_cls = global_symbols;
942 info_member = m;
947 /* Add a symbol for member NAME to class CLS.
948 VAR non-zero means it's a variable. SC specifies the kind of
949 member. HASH is a hash code for the parameter types of a function.
950 Value is a pointer to the member's structure. */
952 struct member *
953 add_member (cls, name, var, sc, hash)
954 struct sym *cls;
955 char *name;
956 int var;
957 int sc;
958 unsigned hash;
960 struct member *m = (struct member *) xmalloc (sizeof *m + strlen (name));
961 struct member **list;
962 struct member *p;
963 struct member *prev;
964 unsigned name_hash = 0;
965 int i;
966 char *s;
968 strcpy (m->name, name);
969 m->param_hash = hash;
971 m->vis = 0;
972 m->flags = 0;
973 m->regexp = NULL;
974 m->filename = NULL;
975 m->pos = 0;
976 m->def_regexp = NULL;
977 m->def_filename = NULL;
978 m->def_pos = 0;
980 assert (cls != NULL);
982 switch (sc)
984 case SC_FRIEND:
985 list = &cls->friends;
986 break;
988 case SC_TYPE:
989 list = &cls->types;
990 break;
992 case SC_STATIC:
993 list = var ? &cls->static_vars : &cls->static_fns;
994 break;
996 default:
997 list = var ? &cls->vars : &cls->fns;
998 break;
1001 for (s = name; *s; ++s)
1002 name_hash = (name_hash << 1) ^ *s;
1003 i = name_hash % TABLE_SIZE;
1004 m->anext = member_table[i];
1005 member_table[i] = m;
1006 m->list = list;
1008 /* Keep the member list sorted. It's cheaper to do it here than to
1009 sort them in Lisp. */
1010 for (prev = NULL, p = *list;
1011 p && strcmp (name, p->name) > 0;
1012 prev = p, p = p->next)
1015 m->next = p;
1016 if (prev)
1017 prev->next = m;
1018 else
1019 *list = m;
1020 return m;
1024 /* Given the root R of a class tree, step through all subclasses
1025 recursively, marking functions as virtual that are declared virtual
1026 in base classes. */
1028 void
1029 mark_virtual (r)
1030 struct sym *r;
1032 struct link *p;
1033 struct member *m, *m2;
1035 for (p = r->subs; p; p = p->next)
1037 for (m = r->fns; m; m = m->next)
1038 if (HAS_FLAG (m->flags, F_VIRTUAL))
1040 for (m2 = p->sym->fns; m2; m2 = m2->next)
1041 if (m->param_hash == m2->param_hash && streq (m->name, m2->name))
1042 SET_FLAG (m2->flags, F_VIRTUAL);
1045 mark_virtual (p->sym);
1050 /* For all roots of the class tree, mark functions as virtual that
1051 are virtual because of a virtual declaration in a base class. */
1053 void
1054 mark_inherited_virtual ()
1056 struct sym *r;
1057 int i;
1059 for (i = 0; i < TABLE_SIZE; ++i)
1060 for (r = class_table[i]; r; r = r->next)
1061 if (r->supers == NULL)
1062 mark_virtual (r);
1066 /* Create and return a symbol for a namespace with name NAME. */
1068 struct sym *
1069 make_namespace (name)
1070 char *name;
1072 struct sym *s = (struct sym *) xmalloc (sizeof *s + strlen (name));
1073 bzero (s, sizeof *s);
1074 strcpy (s->name, name);
1075 s->next = all_namespaces;
1076 s->namesp = current_namespace;
1077 all_namespaces = s;
1078 return s;
1082 /* Find the symbol for namespace NAME. If not found, add a new symbol
1083 for NAME to all_namespaces. */
1085 struct sym *
1086 find_namespace (name)
1087 char *name;
1089 struct sym *p;
1091 for (p = all_namespaces; p; p = p->next)
1093 if (streq (p->name, name))
1094 break;
1095 else
1097 struct alias *p2;
1098 for (p2 = p->namesp_aliases; p2; p2 = p2->next)
1099 if (streq (p2->name, name))
1100 break;
1101 if (p2)
1102 break;
1106 if (p == NULL)
1107 p = make_namespace (name);
1109 return p;
1113 /* Register the name NEW_NAME as an alias for namespace OLD_NAME. */
1115 void
1116 register_namespace_alias (new_name, old_name)
1117 char *new_name, *old_name;
1119 struct sym *p = find_namespace (old_name);
1120 struct alias *al;
1122 /* Is it already in the list of aliases? */
1123 for (al = p->namesp_aliases; al; al = al->next)
1124 if (streq (new_name, p->name))
1125 return;
1127 al = (struct alias *) xmalloc (sizeof *al + strlen (new_name));
1128 strcpy (al->name, new_name);
1129 al->next = p->namesp_aliases;
1130 p->namesp_aliases = al;
1134 /* Enter namespace with name NAME. */
1136 void
1137 enter_namespace (name)
1138 char *name;
1140 struct sym *p = find_namespace (name);
1142 if (namespace_sp == namespace_stack_size)
1144 int size = max (10, 2 * namespace_stack_size);
1145 namespace_stack = (struct sym **) xrealloc (namespace_stack, size);
1146 namespace_stack_size = size;
1149 namespace_stack[namespace_sp++] = current_namespace;
1150 current_namespace = p;
1154 /* Leave the current namespace. */
1156 void
1157 leave_namespace ()
1159 assert (namespace_sp > 0);
1160 current_namespace = namespace_stack[--namespace_sp];
1165 /***********************************************************************
1166 Writing the Output File
1167 ***********************************************************************/
1169 /* Write string S to the output file FP in a Lisp-readable form.
1170 If S is null, write out `()'. */
1172 #define PUTSTR(s, fp) \
1173 do { \
1174 if (!s) \
1176 putc ('(', fp); \
1177 putc (')', fp); \
1178 putc (' ', fp); \
1180 else \
1182 putc ('"', fp); \
1183 fputs (s, fp); \
1184 putc ('"', fp); \
1185 putc (' ', fp); \
1187 } while (0)
1189 /* A dynamically allocated buffer for constructing a scope name. */
1191 char *scope_buffer;
1192 int scope_buffer_size;
1193 int scope_buffer_len;
1196 /* Make sure scope_buffer has enough room to add LEN chars to it. */
1198 void
1199 ensure_scope_buffer_room (len)
1200 int len;
1202 if (scope_buffer_len + len >= scope_buffer_size)
1204 int new_size = max (2 * scope_buffer_size, scope_buffer_len + len);
1205 scope_buffer = (char *) xrealloc (new_size);
1206 scope_buffer_size = new_size;
1211 /* Recursively add the scope names of symbol P and the scopes of its
1212 namespaces to scope_buffer. Value is a pointer to the complete
1213 scope name constructed. */
1215 char *
1216 sym_scope_1 (p)
1217 struct sym *p;
1219 int len;
1221 if (p->namesp)
1222 sym_scope_1 (p->namesp);
1224 if (*scope_buffer)
1226 ensure_scope_buffer_room (3);
1227 strcat (scope_buffer, "::");
1228 scope_buffer_len += 2;
1231 len = strlen (p->name);
1232 ensure_scope_buffer_room (len + 1);
1233 strcat (scope_buffer, p->name);
1234 scope_buffer_len += len;
1236 if (HAS_FLAG (p->flags, F_TEMPLATE))
1238 ensure_scope_buffer_room (3);
1239 strcat (scope_buffer, "<>");
1240 scope_buffer_len += 2;
1243 return scope_buffer;
1247 /* Return the scope of symbol P in printed representation, i.e.
1248 as it would appear in a C*+ source file. */
1250 char *
1251 sym_scope (p)
1252 struct sym *p;
1254 if (!scope_buffer)
1256 scope_buffer_size = 1024;
1257 scope_buffer = (char *) xmalloc (scope_buffer_size);
1260 *scope_buffer = '\0';
1261 scope_buffer_len = 0;
1263 if (p->namesp)
1264 sym_scope_1 (p->namesp);
1266 return scope_buffer;
1270 /* Dump the list of members M to file FP. Value is the length of the
1271 list. */
1274 dump_members (fp, m)
1275 FILE *fp;
1276 struct member *m;
1278 int n;
1280 putc ('(', fp);
1282 for (n = 0; m; m = m->next, ++n)
1284 fputs (MEMBER_STRUCT, fp);
1285 PUTSTR (m->name, fp);
1286 PUTSTR (NULL, fp); /* FIXME? scope for globals */
1287 fprintf (fp, "%u ", (unsigned) m->flags);
1288 PUTSTR (m->filename, fp);
1289 PUTSTR (m->regexp, fp);
1290 fprintf (fp, "%u ", (unsigned) m->pos);
1291 fprintf (fp, "%u ", (unsigned) m->vis);
1292 putc (' ', fp);
1293 PUTSTR (m->def_filename, fp);
1294 PUTSTR (m->def_regexp, fp);
1295 fprintf (fp, "%u", (unsigned) m->def_pos);
1296 putc (']', fp);
1297 putc ('\n', fp);
1300 putc (')', fp);
1301 putc ('\n', fp);
1302 return n;
1306 /* Dump class ROOT to stream FP. */
1308 void
1309 dump_sym (fp, root)
1310 FILE *fp;
1311 struct sym *root;
1313 fputs (CLASS_STRUCT, fp);
1314 PUTSTR (root->name, fp);
1316 /* Print scope, if any. */
1317 if (root->namesp)
1318 PUTSTR (sym_scope (root), fp);
1319 else
1320 PUTSTR (NULL, fp);
1322 /* Print flags. */
1323 fprintf (fp, "%u", root->flags);
1324 PUTSTR (root->filename, fp);
1325 PUTSTR (root->regexp, fp);
1326 fprintf (fp, "%u", (unsigned) root->pos);
1327 PUTSTR (root->sfilename, fp);
1328 putc (']', fp);
1329 putc ('\n', fp);
1333 /* Dump class ROOT and its subclasses to file FP. Value is the
1334 number of classes written. */
1337 dump_tree (fp, root)
1338 FILE *fp;
1339 struct sym *root;
1341 struct link *lk;
1342 unsigned n = 0;
1344 dump_sym (fp, root);
1346 if (f_verbose)
1348 putchar ('+');
1349 fflush (stdout);
1352 putc ('(', fp);
1354 for (lk = root->subs; lk; lk = lk->next)
1356 fputs (TREE_STRUCT, fp);
1357 n += dump_tree (fp, lk->sym);
1358 putc (']', fp);
1361 putc (')', fp);
1363 dump_members (fp, root->vars);
1364 n += dump_members (fp, root->fns);
1365 dump_members (fp, root->static_vars);
1366 n += dump_members (fp, root->static_fns);
1367 n += dump_members (fp, root->friends);
1368 dump_members (fp, root->types);
1370 /* Superclasses. */
1371 putc ('(', fp);
1372 putc (')', fp);
1374 /* Mark slot. */
1375 putc ('(', fp);
1376 putc (')', fp);
1378 putc ('\n', fp);
1379 return n;
1383 /* Dump the entire class tree to file FP. */
1385 void
1386 dump_roots (fp)
1387 FILE *fp;
1389 int i, n = 0;
1390 struct sym *r;
1392 /* Output file header containing version string, command line
1393 options etc. */
1394 if (!f_append)
1396 fputs (TREE_HEADER_STRUCT, fp);
1397 PUTSTR (EBROWSE_FILE_VERSION, fp);
1399 putc ('\"', fp);
1400 if (!f_structs)
1401 fputs (" -s", fp);
1402 if (f_regexps)
1403 fputs (" -x", fp);
1404 putc ('\"', fp);
1405 fputs (" ()", fp);
1406 fputs (" ()", fp);
1407 putc (']', fp);
1410 /* Mark functions as virtual that are so because of functions
1411 declared virtual in base classes. */
1412 mark_inherited_virtual ();
1414 /* Dump the roots of the graph. */
1415 for (i = 0; i < TABLE_SIZE; ++i)
1416 for (r = class_table[i]; r; r = r->next)
1417 if (!r->supers)
1419 fputs (TREE_STRUCT, fp);
1420 n += dump_tree (fp, r);
1421 putc (']', fp);
1424 if (f_verbose)
1425 putchar ('\n');
1430 /***********************************************************************
1431 Scanner
1432 ***********************************************************************/
1434 #ifdef DEBUG
1435 #define INCREMENT_LINENO \
1436 do { \
1437 if (f_very_verbose) \
1439 ++yyline; \
1440 printf ("%d:\n", yyline); \
1442 else \
1443 ++yyline; \
1444 } while (0)
1445 #else
1446 #define INCREMENT_LINENO ++yyline
1447 #endif
1449 /* Define two macros for accessing the input buffer (current input
1450 file). GET(C) sets C to the next input character and advances the
1451 input pointer. UNGET retracts the input pointer. */
1453 #define GET(C) ((C) = *in++)
1454 #define UNGET() (--in)
1457 /* Process a preprocessor line. Value is the next character from the
1458 input buffer not consumed. */
1461 process_pp_line ()
1463 int in_comment = 0, in_string = 0;
1464 int c;
1465 char *p = yytext;
1467 /* Skip over white space. The `#' has been consumed already. */
1468 while (WHITEP (GET (c)))
1471 /* Read the preprocessor command (if any). */
1472 while (IDENTP (c))
1474 *p++ = c;
1475 GET (c);
1478 /* Is it a `define'? */
1479 *p = '\0';
1481 if (*yytext && streq (yytext, "define"))
1483 p = yytext;
1484 while (WHITEP (c))
1485 GET (c);
1486 while (IDENTP (c))
1488 *p++ = c;
1489 GET (c);
1492 *p = '\0';
1494 if (*yytext)
1496 char *regexp = matching_regexp ();
1497 int pos = BUFFER_POS ();
1498 add_define (yytext, regexp, pos);
1502 while (c && (c != '\n' || in_comment || in_string))
1504 if (c == '\\')
1505 GET (c);
1506 else if (c == '/' && !in_comment)
1508 if (GET (c) == '*')
1509 in_comment = 1;
1511 else if (c == '*' && in_comment)
1513 if (GET (c) == '/')
1514 in_comment = 0;
1516 else if (c == '"')
1517 in_string = !in_string;
1519 if (c == '\n')
1520 INCREMENT_LINENO;
1522 GET (c);
1525 return c;
1529 /* Value is the next token from the input buffer. */
1532 yylex ()
1534 int c;
1535 char end_char;
1536 char *p;
1538 for (;;)
1540 while (WHITEP (GET (c)))
1543 switch (c)
1545 case '\n':
1546 INCREMENT_LINENO;
1547 break;
1549 case '\r':
1550 break;
1552 case 0:
1553 /* End of file. */
1554 return YYEOF;
1556 case '\\':
1557 GET (c);
1558 break;
1560 case '"':
1561 case '\'':
1562 /* String and character constants. */
1563 end_char = c;
1564 string_start = in;
1565 while (GET (c) && c != end_char)
1567 switch (c)
1569 case '\\':
1570 /* Escape sequences. */
1571 if (!GET (c))
1573 if (end_char == '\'')
1574 yyerror ("EOF in character constant");
1575 else
1576 yyerror ("EOF in string constant");
1577 goto end_string;
1579 else switch (c)
1581 case '\n':
1582 INCREMENT_LINENO;
1583 case 'a':
1584 case 'b':
1585 case 'f':
1586 case 'n':
1587 case 'r':
1588 case 't':
1589 case 'v':
1590 break;
1592 case 'x':
1594 /* Hexadecimal escape sequence. */
1595 int i;
1596 for (i = 0; i < 2; ++i)
1598 GET (c);
1600 if (c >= '0' && c <= '7')
1602 else if (c >= 'a' && c <= 'f')
1604 else if (c >= 'A' && c <= 'F')
1606 else
1608 UNGET ();
1609 break;
1613 break;
1615 case '0':
1617 /* Octal escape sequence. */
1618 int i;
1619 for (i = 0; i < 3; ++i)
1621 GET (c);
1623 if (c >= '0' && c <= '7')
1625 else
1627 UNGET ();
1628 break;
1632 break;
1634 default:
1635 break;
1637 break;
1639 case '\n':
1640 if (end_char == '\'')
1641 yyerror ("newline in character constant");
1642 else
1643 yyerror ("newline in string constant");
1644 INCREMENT_LINENO;
1645 break;
1647 default:
1648 break;
1652 end_string:
1653 return end_char == '\'' ? CCHAR : CSTRING;
1655 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g':
1656 case 'h': case 'i': case 'j': case 'k': case 'l': case 'm': case 'n':
1657 case 'o': case 'p': case 'q': case 'r': case 's': case 't': case 'u':
1658 case 'v': case 'w': case 'x': case 'y': case 'z':
1659 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G':
1660 case 'H': case 'I': case 'J': case 'K': case 'L': case 'M': case 'N':
1661 case 'O': case 'P': case 'Q': case 'R': case 'S': case 'T': case 'U':
1662 case 'V': case 'W': case 'X': case 'Y': case 'Z': case '_':
1664 /* Identifier and keywords. */
1665 unsigned hash;
1666 struct kw *k;
1668 p = yytext;
1669 *p++ = hash = c;
1671 while (IDENTP (GET (*p)))
1673 hash = (hash << 1) ^ *p++;
1674 if (p == yytext_end - 1)
1676 int size = yytext_end - yytext;
1677 yytext = (char *) xrealloc (yytext, 2 * size);
1678 yytext_end = yytext + 2 * size;
1679 p = yytext + size - 1;
1683 UNGET ();
1684 *p = 0;
1686 for (k = keyword_table[hash % KEYWORD_TABLE_SIZE]; k; k = k->next)
1687 if (streq (k->name, yytext))
1688 return k->tk;
1690 return IDENT;
1693 case '/':
1694 /* C and C++ comments, '/' and '/='. */
1695 switch (GET (c))
1697 case '*':
1698 while (GET (c))
1700 switch (c)
1702 case '*':
1703 if (GET (c) == '/')
1704 goto comment_end;
1705 UNGET ();
1706 break;
1707 case '\\':
1708 GET (c);
1709 break;
1710 case '\n':
1711 INCREMENT_LINENO;
1712 break;
1715 comment_end:;
1716 break;
1718 case '=':
1719 return DIVASGN;
1721 case '/':
1722 while (GET (c) && c != '\n')
1724 INCREMENT_LINENO;
1725 break;
1727 default:
1728 UNGET ();
1729 return '/';
1731 break;
1733 case '+':
1734 if (GET (c) == '+')
1735 return INC;
1736 else if (c == '=')
1737 return ADDASGN;
1738 UNGET ();
1739 return '+';
1741 case '-':
1742 switch (GET (c))
1744 case '-':
1745 return DEC;
1746 case '>':
1747 if (GET (c) == '*')
1748 return ARROWSTAR;
1749 UNGET ();
1750 return ARROW;
1751 case '=':
1752 return SUBASGN;
1754 UNGET ();
1755 return '-';
1757 case '*':
1758 if (GET (c) == '=')
1759 return MULASGN;
1760 UNGET ();
1761 return '*';
1763 case '%':
1764 if (GET (c) == '=')
1765 return MODASGN;
1766 UNGET ();
1767 return '%';
1769 case '|':
1770 if (GET (c) == '|')
1771 return LOR;
1772 else if (c == '=')
1773 return ORASGN;
1774 UNGET ();
1775 return '|';
1777 case '&':
1778 if (GET (c) == '&')
1779 return LAND;
1780 else if (c == '=')
1781 return ANDASGN;
1782 UNGET ();
1783 return '&';
1785 case '^':
1786 if (GET (c) == '=')
1787 return XORASGN;
1788 UNGET ();
1789 return '^';
1791 case '.':
1792 if (GET (c) == '*')
1793 return POINTSTAR;
1794 else if (c == '.')
1796 if (GET (c) != '.')
1797 yyerror ("invalid token '..' ('...' assumed)");
1798 UNGET ();
1799 return ELLIPSIS;
1801 else if (!DIGITP (c))
1803 UNGET ();
1804 return '.';
1806 goto mantissa;
1808 case ':':
1809 if (GET (c) == ':')
1810 return DCOLON;
1811 UNGET ();
1812 return ':';
1814 case '=':
1815 if (GET (c) == '=')
1816 return EQ;
1817 UNGET ();
1818 return '=';
1820 case '!':
1821 if (GET (c) == '=')
1822 return NE;
1823 UNGET ();
1824 return '!';
1826 case '<':
1827 switch (GET (c))
1829 case '=':
1830 return LE;
1831 case '<':
1832 if (GET (c) == '=')
1833 return LSHIFTASGN;
1834 UNGET ();
1835 return LSHIFT;
1837 UNGET ();
1838 return '<';
1840 case '>':
1841 switch (GET (c))
1843 case '=':
1844 return GE;
1845 case '>':
1846 if (GET (c) == '=')
1847 return RSHIFTASGN;
1848 UNGET ();
1849 return RSHIFT;
1851 UNGET ();
1852 return '>';
1854 case '#':
1855 c = process_pp_line ();
1856 if (c == 0)
1857 return YYEOF;
1858 break;
1860 case '(': case ')': case '[': case ']': case '{': case '}':
1861 case ';': case ',': case '?': case '~':
1862 return c;
1864 case '0':
1865 yyival = 0;
1867 if (GET (c) == 'x' || c == 'X')
1869 while (GET (c))
1871 if (DIGITP (c))
1872 yyival = yyival * 16 + c - '0';
1873 else if (c >= 'a' && c <= 'f')
1874 yyival = yyival * 16 + c - 'a' + 10;
1875 else if (c >= 'A' && c <= 'F')
1876 yyival = yyival * 16 + c - 'A' + 10;
1877 else
1878 break;
1881 goto int_suffixes;
1883 else if (c == '.')
1884 goto mantissa;
1886 while (c >= '0' && c <= '7')
1888 yyival = (yyival << 3) + c - '0';
1889 GET (c);
1892 int_suffixes:
1893 /* Integer suffixes. */
1894 while (isalpha (c))
1895 GET (c);
1896 UNGET ();
1897 return CINT;
1899 case '1': case '2': case '3': case '4': case '5': case '6':
1900 case '7': case '8': case '9':
1901 /* Integer or floating constant, part before '.'. */
1902 yyival = c - '0';
1904 while (GET (c) && DIGITP (c))
1905 yyival = 10 * yyival + c - '0';
1907 if (c != '.')
1908 goto int_suffixes;
1910 mantissa:
1911 /* Digits following '.'. */
1912 while (DIGITP (c))
1913 GET (c);
1915 /* Optional exponent. */
1916 if (c == 'E' || c == 'e')
1918 if (GET (c) == '-' || c == '+')
1919 GET (c);
1921 while (DIGITP (c))
1922 GET (c);
1925 /* Optional type suffixes. */
1926 while (isalpha (c))
1927 GET (c);
1928 UNGET ();
1929 return CFLOAT;
1931 default:
1932 break;
1938 /* Value is the string from the start of the line to the current
1939 position in the input buffer, or maybe a bit more if that string is
1940 shorter than min_regexp. */
1942 char *
1943 matching_regexp ()
1945 char *p;
1946 char *s;
1947 char *t;
1948 static char *buffer, *end_buf;
1950 if (!f_regexps)
1951 return NULL;
1953 if (buffer == NULL)
1955 buffer = (char *) xmalloc (max_regexp);
1956 end_buf = &buffer[max_regexp] - 1;
1959 /* Scan back to previous newline of buffer start. */
1960 for (p = in - 1; p > inbuffer && *p != '\n'; --p)
1963 if (*p == '\n')
1965 while (in - p < min_regexp && p > inbuffer)
1967 /* Line probably not significant enough */
1968 for (--p; p >= inbuffer && *p != '\n'; --p)
1971 if (*p == '\n')
1972 ++p;
1975 /* Copy from end to make sure significant portions are included.
1976 This implies that in the browser a regular expressing of the form
1977 `^.*{regexp}' has to be used. */
1978 for (s = end_buf - 1, t = in; s > buffer && t > p;)
1980 *--s = *--t;
1982 if (*s == '"')
1983 *--s = '\\';
1986 *(end_buf - 1) = '\0';
1987 return xstrdup (s);
1991 /* Return a printable representation of token T. */
1993 char *
1994 token_string (t)
1995 int t;
1997 static char b[3];
1999 switch (t)
2001 case CSTRING: return "string constant";
2002 case CCHAR: return "char constant";
2003 case CINT: return "int constant";
2004 case CFLOAT: return "floating constant";
2005 case ELLIPSIS: return "...";
2006 case LSHIFTASGN: return "<<=";
2007 case RSHIFTASGN: return ">>=";
2008 case ARROWSTAR: return "->*";
2009 case IDENT: return "identifier";
2010 case DIVASGN: return "/=";
2011 case INC: return "++";
2012 case ADDASGN: return "+=";
2013 case DEC: return "--";
2014 case ARROW: return "->";
2015 case SUBASGN: return "-=";
2016 case MULASGN: return "*=";
2017 case MODASGN: return "%=";
2018 case LOR: return "||";
2019 case ORASGN: return "|=";
2020 case LAND: return "&&";
2021 case ANDASGN: return "&=";
2022 case XORASGN: return "^=";
2023 case POINTSTAR: return ".*";
2024 case DCOLON: return "::";
2025 case EQ: return "==";
2026 case NE: return "!=";
2027 case LE: return "<=";
2028 case LSHIFT: return "<<";
2029 case GE: return ">=";
2030 case RSHIFT: return ">>";
2031 case ASM: return "asm";
2032 case AUTO: return "auto";
2033 case BREAK: return "break";
2034 case CASE: return "case";
2035 case CATCH: return "catch";
2036 case CHAR: return "char";
2037 case CLASS: return "class";
2038 case CONST: return "const";
2039 case CONTINUE: return "continue";
2040 case DEFAULT: return "default";
2041 case DELETE: return "delete";
2042 case DO: return "do";
2043 case DOUBLE: return "double";
2044 case ELSE: return "else";
2045 case ENUM: return "enum";
2046 case EXTERN: return "extern";
2047 case FLOAT: return "float";
2048 case FOR: return "for";
2049 case FRIEND: return "friend";
2050 case GOTO: return "goto";
2051 case IF: return "if";
2052 case T_INLINE: return "inline";
2053 case INT: return "int";
2054 case LONG: return "long";
2055 case NEW: return "new";
2056 case OPERATOR: return "operator";
2057 case PRIVATE: return "private";
2058 case PROTECTED: return "protected";
2059 case PUBLIC: return "public";
2060 case REGISTER: return "register";
2061 case RETURN: return "return";
2062 case SHORT: return "short";
2063 case SIGNED: return "signed";
2064 case SIZEOF: return "sizeof";
2065 case STATIC: return "static";
2066 case STRUCT: return "struct";
2067 case SWITCH: return "switch";
2068 case TEMPLATE: return "template";
2069 case THIS: return "this";
2070 case THROW: return "throw";
2071 case TRY: return "try";
2072 case TYPEDEF: return "typedef";
2073 case UNION: return "union";
2074 case UNSIGNED: return "unsigned";
2075 case VIRTUAL: return "virtual";
2076 case VOID: return "void";
2077 case VOLATILE: return "volatile";
2078 case WHILE: return "while";
2079 case MUTABLE: return "mutable";
2080 case BOOL: return "bool";
2081 case TRUE: return "true";
2082 case FALSE: return "false";
2083 case SIGNATURE: return "signature";
2084 case NAMESPACE: return "namespace";
2085 case EXPLICIT: return "explicit";
2086 case TYPENAME: return "typename";
2087 case CONST_CAST: return "const_cast";
2088 case DYNAMIC_CAST: return "dynamic_cast";
2089 case REINTERPRET_CAST: return "reinterpret_cast";
2090 case STATIC_CAST: return "static_cast";
2091 case TYPEID: return "typeid";
2092 case USING: return "using";
2093 case WCHAR: return "wchar_t";
2094 case YYEOF: return "EOF";
2096 default:
2097 if (t < 255)
2099 b[0] = t;
2100 b[1] = '\0';
2101 return b;
2103 else
2104 return "???";
2109 /* Reinitialize the scanner for a new input file. */
2111 void
2112 re_init_scanner ()
2114 in = inbuffer;
2115 yyline = 1;
2117 if (yytext == NULL)
2119 int size = 256;
2120 yytext = (char *) xmalloc (size * sizeof *yytext);
2121 yytext_end = yytext + size;
2126 /* Insert a keyword NAME with token value TK into the keyword hash
2127 table. */
2129 void
2130 insert_keyword (name, tk)
2131 char *name;
2132 int tk;
2134 char *s;
2135 unsigned h = 0;
2136 struct kw *k = (struct kw *) xmalloc (sizeof *k);
2138 for (s = name; *s; ++s)
2139 h = (h << 1) ^ *s;
2141 h %= KEYWORD_TABLE_SIZE;
2142 k->name = name;
2143 k->tk = tk;
2144 k->next = keyword_table[h];
2145 keyword_table[h] = k;
2149 /* Initialize the scanner for the first file. This sets up the
2150 character class vectors and fills the keyword hash table. */
2152 void
2153 init_scanner ()
2155 int i;
2157 /* Allocate the input buffer */
2158 inbuffer_size = READ_CHUNK_SIZE + 1;
2159 inbuffer = in = (char *) xmalloc (inbuffer_size);
2160 yyline = 1;
2162 /* Set up character class vectors. */
2163 for (i = 0; i < sizeof is_ident; ++i)
2165 if (i == '_' || isalnum (i))
2166 is_ident[i] = 1;
2168 if (i >= '0' && i <= '9')
2169 is_digit[i] = 1;
2171 if (i == ' ' || i == '\t' || i == '\f' || i == '\v')
2172 is_white[i] = 1;
2175 /* Fill keyword hash table. */
2176 insert_keyword ("and", LAND);
2177 insert_keyword ("and_eq", ANDASGN);
2178 insert_keyword ("asm", ASM);
2179 insert_keyword ("auto", AUTO);
2180 insert_keyword ("bitand", '&');
2181 insert_keyword ("bitor", '|');
2182 insert_keyword ("bool", BOOL);
2183 insert_keyword ("break", BREAK);
2184 insert_keyword ("case", CASE);
2185 insert_keyword ("catch", CATCH);
2186 insert_keyword ("char", CHAR);
2187 insert_keyword ("class", CLASS);
2188 insert_keyword ("compl", '~');
2189 insert_keyword ("const", CONST);
2190 insert_keyword ("const_cast", CONST_CAST);
2191 insert_keyword ("continue", CONTINUE);
2192 insert_keyword ("default", DEFAULT);
2193 insert_keyword ("delete", DELETE);
2194 insert_keyword ("do", DO);
2195 insert_keyword ("double", DOUBLE);
2196 insert_keyword ("dynamic_cast", DYNAMIC_CAST);
2197 insert_keyword ("else", ELSE);
2198 insert_keyword ("enum", ENUM);
2199 insert_keyword ("explicit", EXPLICIT);
2200 insert_keyword ("extern", EXTERN);
2201 insert_keyword ("false", FALSE);
2202 insert_keyword ("float", FLOAT);
2203 insert_keyword ("for", FOR);
2204 insert_keyword ("friend", FRIEND);
2205 insert_keyword ("goto", GOTO);
2206 insert_keyword ("if", IF);
2207 insert_keyword ("inline", T_INLINE);
2208 insert_keyword ("int", INT);
2209 insert_keyword ("long", LONG);
2210 insert_keyword ("mutable", MUTABLE);
2211 insert_keyword ("namespace", NAMESPACE);
2212 insert_keyword ("new", NEW);
2213 insert_keyword ("not", '!');
2214 insert_keyword ("not_eq", NE);
2215 insert_keyword ("operator", OPERATOR);
2216 insert_keyword ("or", LOR);
2217 insert_keyword ("or_eq", ORASGN);
2218 insert_keyword ("private", PRIVATE);
2219 insert_keyword ("protected", PROTECTED);
2220 insert_keyword ("public", PUBLIC);
2221 insert_keyword ("register", REGISTER);
2222 insert_keyword ("reinterpret_cast", REINTERPRET_CAST);
2223 insert_keyword ("return", RETURN);
2224 insert_keyword ("short", SHORT);
2225 insert_keyword ("signed", SIGNED);
2226 insert_keyword ("sizeof", SIZEOF);
2227 insert_keyword ("static", STATIC);
2228 insert_keyword ("static_cast", STATIC_CAST);
2229 insert_keyword ("struct", STRUCT);
2230 insert_keyword ("switch", SWITCH);
2231 insert_keyword ("template", TEMPLATE);
2232 insert_keyword ("this", THIS);
2233 insert_keyword ("throw", THROW);
2234 insert_keyword ("true", TRUE);
2235 insert_keyword ("try", TRY);
2236 insert_keyword ("typedef", TYPEDEF);
2237 insert_keyword ("typeid", TYPEID);
2238 insert_keyword ("typename", TYPENAME);
2239 insert_keyword ("union", UNION);
2240 insert_keyword ("unsigned", UNSIGNED);
2241 insert_keyword ("using", USING);
2242 insert_keyword ("virtual", VIRTUAL);
2243 insert_keyword ("void", VOID);
2244 insert_keyword ("volatile", VOLATILE);
2245 insert_keyword ("wchar_t", WCHAR);
2246 insert_keyword ("while", WHILE);
2247 insert_keyword ("xor", '^');
2248 insert_keyword ("xor_eq", XORASGN);
2253 /***********************************************************************
2254 Parser
2255 ***********************************************************************/
2257 /* Match the current lookahead token and set it to the next token. */
2259 #define MATCH() (tk = yylex ())
2261 /* Return the lookahead token. If current lookahead token is cleared,
2262 read a new token. */
2264 #define LA1 (tk == -1 ? (tk = yylex ()) : tk)
2266 /* Is the current lookahead equal to the token T? */
2268 #define LOOKING_AT(T) (tk == (T))
2270 /* Is the current lookahead one of T1 or T2? */
2272 #define LOOKING_AT2(T1, T2) (tk == (T1) || tk == (T2))
2274 /* Is the current lookahead one of T1, T2 or T3? */
2276 #define LOOKING_AT3(T1, T2, T3) (tk == (T1) || tk == (T2) || tk == (T3))
2278 /* Is the current lookahead one of T1...T4? */
2280 #define LOOKING_AT4(T1, T2, T3, T4) \
2281 (tk == (T1) || tk == (T2) || tk == (T3) || tk == (T4))
2283 /* Match token T if current lookahead is T. */
2285 #define MATCH_IF(T) if (LOOKING_AT (T)) MATCH (); else ((void) 0)
2287 /* Skip to matching token if current token is T. */
2289 #define SKIP_MATCHING_IF(T) \
2290 if (LOOKING_AT (T)) skip_matching (); else ((void) 0)
2293 /* Skip forward until a given token TOKEN or YYEOF is seen and return
2294 the current lookahead token after skipping. */
2297 skip_to (token)
2298 int token;
2300 while (!LOOKING_AT2 (YYEOF, token))
2301 MATCH ();
2302 return tk;
2306 /* Skip over pairs of tokens (parentheses, square brackets,
2307 angle brackets, curly brackets) matching the current lookahead. */
2309 void
2310 skip_matching ()
2312 int open, close, n;
2314 switch (open = LA1)
2316 case '{':
2317 close = '}';
2318 break;
2320 case '(':
2321 close = ')';
2322 break;
2324 case '<':
2325 close = '>';
2326 break;
2328 case '[':
2329 close = ']';
2330 break;
2332 default:
2333 abort ();
2336 for (n = 0;;)
2338 if (LOOKING_AT (open))
2339 ++n;
2340 else if (LOOKING_AT (close))
2341 --n;
2342 else if (LOOKING_AT (YYEOF))
2343 break;
2345 MATCH ();
2347 if (n == 0)
2348 break;
2353 /* Re-initialize the parser by resetting the lookahead token. */
2355 void
2356 re_init_parser ()
2358 tk = -1;
2362 /* Parse a parameter list, including the const-specifier,
2363 pure-specifier, and throw-list that may follow a parameter list.
2364 Return in FLAGS what was seen following the parameter list.
2365 Returns a hash code for the parameter types. This value is used to
2366 distinguish between overloaded functions. */
2368 unsigned
2369 parm_list (flags)
2370 int *flags;
2372 unsigned hash = 0;
2373 int type_seen = 0;
2375 while (!LOOKING_AT2 (YYEOF, ')'))
2377 switch (LA1)
2379 /* Skip over grouping parens or parameter lists in parameter
2380 declarations. */
2381 case '(':
2382 skip_matching ();
2383 break;
2385 /* Next parameter. */
2386 case ',':
2387 MATCH ();
2388 type_seen = 0;
2389 break;
2391 /* Ignore the scope part of types, if any. This is because
2392 some types need scopes when defined outside of a class body,
2393 and don't need them inside the class body. This means that
2394 we have to look for the last IDENT in a sequence of
2395 IDENT::IDENT::... */
2396 case IDENT:
2397 if (!type_seen)
2399 char *last_id;
2400 unsigned ident_type_hash = 0;
2402 parse_qualified_param_ident_or_type (&last_id);
2403 if (last_id)
2405 /* LAST_ID null means something like `X::*'. */
2406 for (; *last_id; ++last_id)
2407 ident_type_hash = (ident_type_hash << 1) ^ *last_id;
2408 hash = (hash << 1) ^ ident_type_hash;
2409 type_seen = 1;
2412 else
2413 MATCH ();
2414 break;
2416 case VOID:
2417 /* This distinction is made to make `func (void)' equivalent
2418 to `func ()'. */
2419 type_seen = 1;
2420 MATCH ();
2421 if (!LOOKING_AT (')'))
2422 hash = (hash << 1) ^ VOID;
2423 break;
2425 case BOOL: case CHAR: case CLASS: case CONST:
2426 case DOUBLE: case ENUM: case FLOAT: case INT:
2427 case LONG: case SHORT: case SIGNED: case STRUCT:
2428 case UNION: case UNSIGNED: case VOLATILE: case WCHAR:
2429 case ELLIPSIS:
2430 type_seen = 1;
2431 hash = (hash << 1) ^ LA1;
2432 MATCH ();
2433 break;
2435 case '*': case '&': case '[': case ']':
2436 hash = (hash << 1) ^ LA1;
2437 MATCH ();
2438 break;
2440 default:
2441 MATCH ();
2442 break;
2446 if (LOOKING_AT (')'))
2448 MATCH ();
2450 if (LOOKING_AT (CONST))
2452 /* We can overload the same function on `const' */
2453 hash = (hash << 1) ^ CONST;
2454 SET_FLAG (*flags, F_CONST);
2455 MATCH ();
2458 if (LOOKING_AT (THROW))
2460 MATCH ();
2461 SKIP_MATCHING_IF ('(');
2462 SET_FLAG (*flags, F_THROW);
2465 if (LOOKING_AT ('='))
2467 MATCH ();
2468 if (LOOKING_AT (CINT) && yyival == 0)
2470 MATCH ();
2471 SET_FLAG (*flags, F_PURE);
2476 return hash;
2480 /* Print position info to stdout. */
2482 void
2483 print_info ()
2485 if (info_position >= 0 && BUFFER_POS () <= info_position)
2486 if (info_cls)
2487 printf ("(\"%s\" \"%s\" \"%s\" %d)\n",
2488 info_cls->name, sym_scope (info_cls),
2489 info_member->name, info_where);
2493 /* Parse a member declaration within the class body of CLS. VIS is
2494 the access specifier for the member (private, protected,
2495 public). */
2497 void
2498 member (cls, vis)
2499 struct sym *cls;
2500 int vis;
2502 char *id = NULL;
2503 int sc = SC_MEMBER;
2504 char *regexp = NULL;
2505 int pos;
2506 int is_constructor;
2507 int anonymous = 0;
2508 int flags = 0;
2509 int class_tag;
2510 int type_seen = 0;
2511 int paren_seen = 0;
2512 unsigned hash = 0;
2513 int tilde = 0;
2515 while (!LOOKING_AT4 (';', '{', '}', YYEOF))
2517 switch (LA1)
2519 default:
2520 MATCH ();
2521 break;
2523 /* A function or class may follow. */
2524 case TEMPLATE:
2525 MATCH();
2526 SET_FLAG (flags, F_TEMPLATE);
2527 /* Skip over template argument list */
2528 SKIP_MATCHING_IF ('<');
2529 break;
2531 case EXPLICIT:
2532 SET_FLAG (flags, F_EXPLICIT);
2533 goto typeseen;
2535 case MUTABLE:
2536 SET_FLAG (flags, F_MUTABLE);
2537 goto typeseen;
2539 case T_INLINE:
2540 SET_FLAG (flags, F_INLINE);
2541 goto typeseen;
2543 case VIRTUAL:
2544 SET_FLAG (flags, F_VIRTUAL);
2545 goto typeseen;
2547 case '[':
2548 skip_matching ();
2549 break;
2551 case ENUM:
2552 sc = SC_TYPE;
2553 goto typeseen;
2555 case TYPEDEF:
2556 sc = SC_TYPE;
2557 goto typeseen;
2559 case FRIEND:
2560 sc = SC_FRIEND;
2561 goto typeseen;
2563 case STATIC:
2564 sc = SC_STATIC;
2565 goto typeseen;
2567 case '~':
2568 tilde = 1;
2569 MATCH ();
2570 break;
2572 case IDENT:
2573 /* Remember IDENTS seen so far. Among these will be the member
2574 name. */
2575 id = (char *) alloca (strlen (yytext) + 2);
2576 if (tilde)
2578 *id = '~';
2579 strcpy (id + 1, yytext);
2581 else
2582 strcpy (id, yytext);
2583 MATCH ();
2584 break;
2586 case OPERATOR:
2587 id = operator_name (&sc);
2588 break;
2590 case '(':
2591 /* Most probably the beginning of a parameter list. */
2592 MATCH ();
2593 paren_seen = 1;
2595 if (id && cls)
2597 if (!(is_constructor = streq (id, cls->name)))
2598 regexp = matching_regexp ();
2600 else
2601 is_constructor = 0;
2603 pos = BUFFER_POS ();
2604 hash = parm_list (&flags);
2606 if (is_constructor)
2607 regexp = matching_regexp ();
2609 if (id && cls != NULL)
2610 add_member_decl (cls, id, regexp, pos, hash, 0, sc, vis, flags);
2612 while (!LOOKING_AT3 (';', '{', YYEOF))
2613 MATCH ();
2615 if (LOOKING_AT ('{') && id && cls)
2616 add_member_defn (cls, id, regexp, pos, hash, 0, sc, flags);
2618 id = NULL;
2619 sc = SC_MEMBER;
2620 break;
2622 case STRUCT: case UNION: case CLASS:
2623 /* Nested class */
2624 class_tag = LA1;
2625 type_seen = 1;
2626 MATCH ();
2627 anonymous = 1;
2629 /* More than one ident here to allow for MS-DOS specialties
2630 like `_export class' etc. The last IDENT seen counts
2631 as the class name. */
2632 while (!LOOKING_AT4 (YYEOF, ';', ':', '{'))
2634 if (LOOKING_AT (IDENT))
2635 anonymous = 0;
2636 MATCH ();
2639 if (LOOKING_AT2 (':', '{'))
2640 class_definition (anonymous ? NULL : cls, class_tag, flags, 1);
2641 else
2642 skip_to (';');
2643 break;
2645 case INT: case CHAR: case LONG: case UNSIGNED:
2646 case SIGNED: case CONST: case DOUBLE: case VOID:
2647 case SHORT: case VOLATILE: case BOOL: case WCHAR:
2648 case TYPENAME:
2649 typeseen:
2650 type_seen = 1;
2651 MATCH ();
2652 break;
2656 if (LOOKING_AT (';'))
2658 /* The end of a member variable, a friend declaration or an access
2659 declaration. We don't want to add friend classes as members. */
2660 if (id && sc != SC_FRIEND && cls)
2662 regexp = matching_regexp ();
2663 pos = BUFFER_POS ();
2665 if (cls != NULL)
2667 if (type_seen || !paren_seen)
2668 add_member_decl (cls, id, regexp, pos, 0, 1, sc, vis, 0);
2669 else
2670 add_member_decl (cls, id, regexp, pos, hash, 0, sc, vis, 0);
2674 MATCH ();
2675 print_info ();
2677 else if (LOOKING_AT ('{'))
2679 /* A named enum. */
2680 if (sc == SC_TYPE && id && cls)
2682 regexp = matching_regexp ();
2683 pos = BUFFER_POS ();
2685 if (cls != NULL)
2687 add_member_decl (cls, id, regexp, pos, 0, 1, sc, vis, 0);
2688 add_member_defn (cls, id, regexp, pos, 0, 1, sc, 0);
2692 skip_matching ();
2693 print_info ();
2698 /* Parse the body of class CLS. TAG is the tag of the class (struct,
2699 union, class). */
2701 void
2702 class_body (cls, tag)
2703 struct sym *cls;
2704 int tag;
2706 int vis = tag == CLASS ? PRIVATE : PUBLIC;
2707 int temp;
2709 while (!LOOKING_AT2 (YYEOF, '}'))
2711 switch (LA1)
2713 case PRIVATE: case PROTECTED: case PUBLIC:
2714 temp = LA1;
2715 MATCH ();
2717 if (LOOKING_AT (':'))
2719 vis = temp;
2720 MATCH ();
2722 else
2724 /* Probably conditional compilation for inheritance list.
2725 We don't known whether there comes more of this.
2726 This is only a crude fix that works most of the time. */
2729 MATCH ();
2731 while (LOOKING_AT2 (IDENT, ',')
2732 || LOOKING_AT3 (PUBLIC, PROTECTED, PRIVATE));
2734 break;
2736 case TYPENAME:
2737 case USING:
2738 skip_to (';');
2739 break;
2741 /* Try to synchronize */
2742 case CHAR: case CLASS: case CONST:
2743 case DOUBLE: case ENUM: case FLOAT: case INT:
2744 case LONG: case SHORT: case SIGNED: case STRUCT:
2745 case UNION: case UNSIGNED: case VOID: case VOLATILE:
2746 case TYPEDEF: case STATIC: case T_INLINE: case FRIEND:
2747 case VIRTUAL: case TEMPLATE: case IDENT: case '~':
2748 case BOOL: case WCHAR: case EXPLICIT: case MUTABLE:
2749 member (cls, vis);
2750 break;
2752 default:
2753 MATCH ();
2754 break;
2760 /* Parse a qualified identifier. Current lookahead is IDENT. A
2761 qualified ident has the form `X<..>::Y<...>::T<...>. Returns a
2762 symbol for that class. */
2764 struct sym *
2765 parse_classname ()
2767 struct sym *last_class = NULL;
2769 while (LOOKING_AT (IDENT))
2771 last_class = add_sym (yytext, last_class);
2772 MATCH ();
2774 if (LOOKING_AT ('<'))
2776 skip_matching ();
2777 SET_FLAG (last_class->flags, F_TEMPLATE);
2780 if (!LOOKING_AT (DCOLON))
2781 break;
2783 MATCH ();
2786 return last_class;
2790 /* Parse an operator name. Add the `static' flag to *SC if an
2791 implicitly static operator has been parsed. Value is a pointer to
2792 a static buffer holding the constructed operator name string. */
2794 char *
2795 operator_name (sc)
2796 int *sc;
2798 static int id_size = 0;
2799 static char *id = NULL;
2800 char *s;
2801 int len;
2803 MATCH ();
2805 if (LOOKING_AT2 (NEW, DELETE))
2807 /* `new' and `delete' are implicitly static. */
2808 if (*sc != SC_FRIEND)
2809 *sc = SC_STATIC;
2811 s = token_string (LA1);
2812 MATCH ();
2814 len = strlen (s) + 10;
2815 if (len > id_size)
2817 int new_size = max (len, 2 * id_size);
2818 id = (char *) xrealloc (id, new_size);
2819 id_size = new_size;
2821 strcpy (id, s);
2823 /* Vector new or delete? */
2824 if (LOOKING_AT ('['))
2826 strcat (id, "[");
2827 MATCH ();
2829 if (LOOKING_AT (']'))
2831 strcat (id, "]");
2832 MATCH ();
2836 else
2838 int tokens_matched = 0;
2840 len = 20;
2841 if (len > id_size)
2843 int new_size = max (len, 2 * id_size);
2844 id = (char *) xrealloc (id, new_size);
2845 id_size = new_size;
2847 strcpy (id, "operator");
2849 /* Beware access declarations of the form "X::f;" Beware of
2850 `operator () ()'. Yet another difficulty is found in
2851 GCC 2.95's STL: `operator == __STL_NULL_TMPL_ARGS (...'. */
2852 while (!(LOOKING_AT ('(') && tokens_matched)
2853 && !LOOKING_AT2 (';', YYEOF))
2855 s = token_string (LA1);
2856 len += strlen (s) + 2;
2857 if (len > id_size)
2859 int new_size = max (len, 2 * id_size);
2860 id = (char *) xrealloc (id, new_size);
2861 id_size = new_size;
2864 if (*s != ')' && *s != ']')
2865 strcat (id, " ");
2866 strcat (id, s);
2867 MATCH ();
2869 /* If this is a simple operator like `+', stop now. */
2870 if (!isalpha (*s) && *s != '(' && *s != '[')
2871 break;
2873 ++tokens_matched;
2877 return id;
2881 /* This one consumes the last IDENT of a qualified member name like
2882 `X::Y::z'. This IDENT is returned in LAST_ID. Value if the
2883 symbol structure for the ident. */
2885 struct sym *
2886 parse_qualified_ident_or_type (last_id)
2887 char **last_id;
2889 struct sym *cls = NULL;
2890 static char *id = NULL;
2891 static int id_size = 0;
2893 while (LOOKING_AT (IDENT))
2895 int len = strlen (yytext) + 1;
2896 if (len > id_size)
2898 id = (char *) xrealloc (id, len);
2899 id_size = len;
2901 strcpy (id, yytext);
2902 *last_id = id;
2903 MATCH ();
2905 SKIP_MATCHING_IF ('<');
2907 if (LOOKING_AT (DCOLON))
2909 cls = add_sym (id, cls);
2910 *last_id = NULL;
2911 MATCH ();
2913 else
2914 break;
2917 return cls;
2921 /* This one consumes the last IDENT of a qualified member name like
2922 `X::Y::z'. This IDENT is returned in LAST_ID. Value if the
2923 symbol structure for the ident. */
2925 void
2926 parse_qualified_param_ident_or_type (last_id)
2927 char **last_id;
2929 struct sym *cls = NULL;
2930 static char *id = NULL;
2931 static int id_size = 0;
2933 while (LOOKING_AT (IDENT))
2935 int len = strlen (yytext) + 1;
2936 if (len > id_size)
2938 id = (char *) xrealloc (id, len);
2939 id_size = len;
2941 strcpy (id, yytext);
2942 *last_id = id;
2943 MATCH ();
2945 SKIP_MATCHING_IF ('<');
2947 if (LOOKING_AT (DCOLON))
2949 cls = add_sym (id, cls);
2950 *last_id = NULL;
2951 MATCH ();
2953 else
2954 break;
2959 /* Parse a class definition.
2961 CONTAINING is the class containing the class being parsed or null.
2962 This may also be null if NESTED != 0 if the containing class is
2963 anonymous. TAG is the tag of the class (struct, union, class).
2964 NESTED is non-zero if we are parsing a nested class.
2966 Current lookahead is the class name. */
2968 void
2969 class_definition (containing, tag, flags, nested)
2970 struct sym *containing;
2971 int tag;
2972 int flags;
2973 int nested;
2975 struct sym *current;
2976 struct sym *base_class;
2978 /* Set CURRENT to null if no entry has to be made for the class
2979 parsed. This is the case for certain command line flag
2980 settings. */
2981 if ((tag != CLASS && !f_structs) || (nested && !f_nested_classes))
2982 current = NULL;
2983 else
2985 current = add_sym (yytext, containing);
2986 current->pos = BUFFER_POS ();
2987 current->regexp = matching_regexp ();
2988 current->filename = filename;
2989 current->flags = flags;
2992 /* If at ':', base class list follows. */
2993 if (LOOKING_AT (':'))
2995 int done = 0;
2996 MATCH ();
2998 while (!done)
3000 switch (LA1)
3002 case VIRTUAL: case PUBLIC: case PROTECTED: case PRIVATE:
3003 MATCH ();
3004 break;
3006 case IDENT:
3007 base_class = parse_classname ();
3008 if (base_class && current && base_class != current)
3009 add_link (base_class, current);
3010 break;
3012 /* The `,' between base classes or the end of the base
3013 class list. Add the previously found base class.
3014 It's done this way to skip over sequences of
3015 `A::B::C' until we reach the end.
3017 FIXME: it is now possible to handle `class X : public B::X'
3018 because we have enough information. */
3019 case ',':
3020 MATCH ();
3021 break;
3023 default:
3024 /* A syntax error, possibly due to preprocessor constructs
3025 like
3027 #ifdef SOMETHING
3028 class A : public B
3029 #else
3030 class A : private B.
3032 MATCH until we see something like `;' or `{'. */
3033 while (!LOOKING_AT3 (';', YYEOF, '{'))
3034 MATCH ();
3035 done = 1;
3037 case '{':
3038 done = 1;
3039 break;
3044 /* Parse the class body if there is one. */
3045 if (LOOKING_AT ('{'))
3047 if (tag != CLASS && !f_structs)
3048 skip_matching ();
3049 else
3051 MATCH ();
3052 class_body (current, tag);
3054 if (LOOKING_AT ('}'))
3056 MATCH ();
3057 if (LOOKING_AT (';') && !nested)
3058 MATCH ();
3065 /* Parse a declaration. */
3067 void
3068 declaration (flags)
3069 int flags;
3071 char *id = NULL;
3072 struct sym *cls = NULL;
3073 char *regexp = NULL;
3074 int pos = 0;
3075 unsigned hash = 0;
3076 int is_constructor;
3077 int sc = 0;
3079 while (!LOOKING_AT3 (';', '{', YYEOF))
3081 switch (LA1)
3083 default:
3084 MATCH ();
3085 break;
3087 case '[':
3088 skip_matching ();
3089 break;
3091 case ENUM:
3092 case TYPEDEF:
3093 sc = SC_TYPE;
3094 MATCH ();
3095 break;
3097 case STATIC:
3098 sc = SC_STATIC;
3099 MATCH ();
3100 break;
3102 case INT: case CHAR: case LONG: case UNSIGNED:
3103 case SIGNED: case CONST: case DOUBLE: case VOID:
3104 case SHORT: case VOLATILE: case BOOL: case WCHAR:
3105 MATCH ();
3106 break;
3108 case CLASS: case STRUCT: case UNION:
3109 /* This is for the case `STARTWRAP class X : ...' or
3110 `declare (X, Y)\n class A : ...'. */
3111 if (id)
3112 return;
3114 case '=':
3115 /* Assumed to be the start of an initialization in this context.
3116 Skip over everything up to ';'. */
3117 skip_to (';');
3118 break;
3120 case OPERATOR:
3121 id = operator_name (&sc);
3122 break;
3124 case T_INLINE:
3125 SET_FLAG (flags, F_INLINE);
3126 MATCH ();
3127 break;
3129 case '~':
3130 MATCH ();
3131 if (LOOKING_AT (IDENT))
3133 id = (char *) alloca (strlen (yytext) + 2);
3134 *id = '~';
3135 strcpy (id + 1, yytext);
3136 MATCH ();
3138 break;
3140 case IDENT:
3141 cls = parse_qualified_ident_or_type (&id);
3142 break;
3144 case '(':
3145 /* Most probably the beginning of a parameter list. */
3146 if (cls)
3148 MATCH ();
3150 if (id && cls)
3152 if (!(is_constructor = streq (id, cls->name)))
3153 regexp = matching_regexp ();
3155 else
3156 is_constructor = 0;
3158 pos = BUFFER_POS ();
3159 hash = parm_list (&flags);
3161 if (is_constructor)
3162 regexp = matching_regexp ();
3164 if (id && cls)
3165 add_member_defn (cls, id, regexp, pos, hash, 0,
3166 SC_UNKNOWN, flags);
3168 else
3170 /* This may be a C functions, but also a macro
3171 call of the form `declare (A, B)' --- such macros
3172 can be found in some class libraries. */
3173 MATCH ();
3175 if (id)
3177 regexp = matching_regexp ();
3178 pos = BUFFER_POS ();
3179 hash = parm_list (&flags);
3180 add_global_decl (id, regexp, pos, hash, 0, sc, flags);
3183 /* This is for the case that the function really is
3184 a macro with no `;' following it. If a CLASS directly
3185 follows, we would miss it otherwise. */
3186 if (LOOKING_AT3 (CLASS, STRUCT, UNION))
3187 return;
3190 while (!LOOKING_AT3 (';', '{', YYEOF))
3191 MATCH ();
3193 if (!cls && id && LOOKING_AT ('{'))
3194 add_global_defn (id, regexp, pos, hash, 0, sc, flags);
3195 id = NULL;
3196 break;
3200 if (LOOKING_AT (';'))
3202 /* The end of a member variable or of an access declaration
3203 `X::f'. To distinguish between them we have to know whether
3204 type information has been seen. */
3205 if (id)
3207 char *regexp = matching_regexp ();
3208 int pos = BUFFER_POS ();
3210 if (cls)
3211 add_member_defn (cls, id, regexp, pos, 0, 1, SC_UNKNOWN, flags);
3212 else
3213 add_global_defn (id, regexp, pos, 0, 1, sc, flags);
3216 MATCH ();
3217 print_info ();
3219 else if (LOOKING_AT ('{'))
3221 if (sc == SC_TYPE && id)
3223 /* A named enumeration. */
3224 regexp = matching_regexp ();
3225 pos = BUFFER_POS ();
3226 add_global_defn (id, regexp, pos, 0, 1, sc, flags);
3229 skip_matching ();
3230 print_info ();
3235 /* Parse a list of top-level declarations/definitions. START_FLAGS
3236 says in which context we are parsing. If it is F_EXTERNC, we are
3237 parsing in an `extern "C"' block. Value is 1 if EOF is reached, 0
3238 otherwise. */
3241 globals (start_flags)
3242 int start_flags;
3244 int anonymous;
3245 int class_tk;
3246 int flags = start_flags;
3248 for (;;)
3250 char *prev_in = in;
3252 switch (LA1)
3254 case NAMESPACE:
3256 MATCH ();
3258 if (LOOKING_AT (IDENT))
3260 char *namespace_name
3261 = (char *) alloca (strlen (yytext) + 1);
3262 strcpy (namespace_name, yytext);
3263 MATCH ();
3265 if (LOOKING_AT ('='))
3267 if (skip_to (';') == ';')
3268 MATCH ();
3269 register_namespace_alias (namespace_name, yytext);
3271 else if (LOOKING_AT ('{'))
3273 MATCH ();
3274 enter_namespace (namespace_name);
3275 globals (0);
3276 leave_namespace ();
3277 MATCH_IF ('}');
3281 break;
3283 case EXTERN:
3284 MATCH ();
3285 if (LOOKING_AT (CSTRING) && *string_start == 'C'
3286 && *(string_start + 1) == '"')
3288 /* This is `extern "C"'. */
3289 MATCH ();
3291 if (LOOKING_AT ('{'))
3293 MATCH ();
3294 globals (F_EXTERNC);
3295 MATCH_IF ('}');
3297 else
3298 SET_FLAG (flags, F_EXTERNC);
3300 break;
3302 case TEMPLATE:
3303 MATCH ();
3304 SKIP_MATCHING_IF ('<');
3305 SET_FLAG (flags, F_TEMPLATE);
3306 break;
3308 case CLASS: case STRUCT: case UNION:
3309 class_tk = LA1;
3310 MATCH ();
3311 anonymous = 1;
3313 /* More than one ident here to allow for MS-DOS and OS/2
3314 specialties like `far', `_Export' etc. Some C++ libs
3315 have constructs like `_OS_DLLIMPORT(_OS_CLIENT)' in front
3316 of the class name. */
3317 while (!LOOKING_AT4 (YYEOF, ';', ':', '{'))
3319 if (LOOKING_AT (IDENT))
3320 anonymous = 0;
3321 MATCH ();
3324 /* Don't add anonymous unions. */
3325 if (LOOKING_AT2 (':', '{') && !anonymous)
3326 class_definition (NULL, class_tk, flags, 0);
3327 else
3329 if (skip_to (';') == ';')
3330 MATCH ();
3333 flags = start_flags;
3334 break;
3336 case YYEOF:
3337 return 1;
3339 case '}':
3340 return 0;
3342 default:
3343 declaration (flags);
3344 flags = start_flags;
3345 break;
3348 if (prev_in == in)
3349 yyerror ("parse error");
3354 /* Parse the current input file. */
3356 void
3357 yyparse ()
3359 while (globals (0) == 0)
3360 MATCH_IF ('}');
3365 /***********************************************************************
3366 Main Program
3367 ***********************************************************************/
3369 /* Add the list of paths PATH_LIST to the current search path for
3370 input files. */
3372 void
3373 add_search_path (path_list)
3374 char *path_list;
3376 while (*path_list)
3378 char *start = path_list;
3379 struct search_path *p;
3381 while (*path_list && *path_list != PATH_LIST_SEPARATOR)
3382 ++path_list;
3384 p = (struct search_path *) xmalloc (sizeof *p);
3385 p->path = (char *) xmalloc (path_list - start + 1);
3386 memcpy (p->path, start, path_list - start);
3387 p->path[path_list - start] = '\0';
3388 p->next = NULL;
3390 if (search_path_tail)
3392 search_path_tail->next = p;
3393 search_path_tail = p;
3395 else
3396 search_path = search_path_tail = p;
3398 while (*path_list == PATH_LIST_SEPARATOR)
3399 ++path_list;
3404 /* Open FILE and return a file handle for it, or -1 if FILE cannot be
3405 opened. Try to find FILE in search_path first, then try the
3406 unchanged file name. */
3408 FILE *
3409 open_file (file)
3410 char *file;
3412 FILE *fp = NULL;
3413 static char *buffer;
3414 static int buffer_size;
3415 struct search_path *path;
3416 int flen = strlen (file) + 1; /* +1 for the slash */
3418 filename = xstrdup (file);
3420 for (path = search_path; path && fp == NULL; path = path->next)
3422 int len = strlen (path->path) + flen;
3424 if (len + 1 >= buffer_size)
3426 buffer_size = max (len + 1, 2 * buffer_size);
3427 buffer = (char *) xrealloc (buffer, buffer_size);
3430 strcpy (buffer, path->path);
3431 strcat (buffer, "/");
3432 strcat (buffer, file);
3433 fp = fopen (buffer, "r");
3436 /* Try the original file name. */
3437 if (fp == NULL)
3438 fp = fopen (file, "r");
3440 if (fp == NULL)
3441 yyerror ("cannot open");
3443 return fp;
3447 /* Display usage information and exit program. */
3449 #define USAGE "\
3450 Usage: ebrowse [options] {files}\n\
3452 -a, --append append output\n\
3453 -f, --files=FILES read input file names from FILE\n\
3454 -I, --search-path=LIST set search path for input files\n\
3455 -m, --min-regexp-length=N set minimum regexp length to N\n\
3456 -M, --max-regexp-length=N set maximum regexp length to N\n\
3457 -n, --no-nested-classes exclude nested classes\n\
3458 -o, --output-file=FILE set output file name to FILE\n\
3459 -p, --position-info print info about position in file\n\
3460 -s, --no-structs-or-unions don't record structs or unions\n\
3461 -v, --verbose be verbose\n\
3462 -V, --very-verbose be very verbose\n\
3463 -x, --no-regexps don't record regular expressions\n\
3464 --help display this help\n\
3465 --version display version info\n\
3468 void
3469 usage (error)
3470 int error;
3472 puts (USAGE);
3473 exit (error ? 1 : 0);
3477 /* Display version and copyright info. The VERSION macro is set
3478 from the Makefile and contains the Emacs version. */
3480 void
3481 version ()
3483 printf ("ebrowse %s\n", VERSION);
3484 puts ("Copyright (C) 1992-1999, 2000 Free Software Foundation, Inc.");
3485 puts ("This program is distributed under the same terms as Emacs.");
3486 exit (0);
3490 /* Parse one input file FILE, adding classes and members to the symbol
3491 table. */
3493 void
3494 process_file (file)
3495 char *file;
3497 FILE *fp;
3499 fp = open_file (file);
3500 if (fp)
3502 int nread, nbytes;
3504 /* Give a progress indication if needed. */
3505 if (f_very_verbose)
3507 puts (filename);
3508 fflush (stdout);
3510 else if (f_verbose)
3512 putchar ('.');
3513 fflush (stdout);
3516 /* Read file to inbuffer. */
3517 for (nread = 0;;)
3519 if (nread + READ_CHUNK_SIZE >= inbuffer_size)
3521 inbuffer_size = nread + READ_CHUNK_SIZE + 1;
3522 inbuffer = (char *) xrealloc (inbuffer, inbuffer_size);
3525 nbytes = fread (inbuffer + nread, 1, READ_CHUNK_SIZE, fp);
3526 if (nbytes <= 0)
3527 break;
3528 nread += nbytes;
3530 if (nread < 0)
3531 nread = 0;
3532 inbuffer[nread] = '\0';
3534 /* Reinitialize scanner and parser for the new input file. */
3535 re_init_scanner ();
3536 re_init_parser ();
3538 /* Parse it and close the file. */
3539 yyparse ();
3540 fclose (fp);
3545 /* Read a line from stream FP and return a pointer to a static buffer
3546 containing its contents without the terminating newline. Value
3547 is null when EOF is reached. */
3549 char *
3550 read_line (fp)
3551 FILE *fp;
3553 static char *buffer;
3554 static int buffer_size;
3555 int i = 0, c;
3557 while ((c = getc (fp)) != EOF && c != '\n')
3559 if (i >= buffer_size)
3561 buffer_size = max (100, buffer_size * 2);
3562 buffer = (char *) xrealloc (buffer, buffer_size);
3565 buffer[i++] = c;
3568 if (c == EOF && i == 0)
3569 return NULL;
3571 if (i == buffer_size)
3573 buffer_size = max (100, buffer_size * 2);
3574 buffer = (char *) xrealloc (buffer, buffer_size);
3577 buffer[i] = '\0';
3578 return buffer;
3582 /* Main entry point. */
3585 main (argc, argv)
3586 int argc;
3587 char **argv;
3589 int i;
3590 int any_inputfiles = 0;
3591 static char *out_filename = DEFAULT_OUTFILE;
3592 static char **input_filenames = NULL;
3593 static int input_filenames_size = 0;
3594 static int n_input_files;
3596 filename = "command line";
3597 yyout = stdout;
3599 while ((i = getopt_long (argc, argv, "af:I:m:M:no:p:svVx",
3600 options, NULL)) != EOF)
3602 switch (i)
3604 /* Experimental. */
3605 case 'p':
3606 info_position = atoi (optarg);
3607 break;
3609 case 'n':
3610 f_nested_classes = 0;
3611 break;
3613 case 'x':
3614 f_regexps = 0;
3615 break;
3617 /* Add the name of a file containing more input files. */
3618 case 'f':
3619 if (n_input_files == input_filenames_size)
3621 input_filenames_size = max (10, 2 * input_filenames_size);
3622 input_filenames = (char **) xrealloc (input_filenames,
3623 input_filenames_size);
3625 input_filenames[n_input_files++] = xstrdup (optarg);
3626 break;
3628 /* Append new output to output file instead of truncating it. */
3629 case 'a':
3630 f_append = 1;
3631 break;
3633 /* Include structs in the output */
3634 case 's':
3635 f_structs = 0;
3636 break;
3638 /* Be verbose (give a progress indication). */
3639 case 'v':
3640 f_verbose = 1;
3641 break;
3643 /* Be very verbose (print file names as they are processed). */
3644 case 'V':
3645 f_verbose = 1;
3646 f_very_verbose = 1;
3647 break;
3649 /* Change the name of the output file. */
3650 case 'o':
3651 out_filename = optarg;
3652 break;
3654 /* Set minimum length for regular expression strings
3655 when recorded in the output file. */
3656 case 'm':
3657 min_regexp = atoi (optarg);
3658 break;
3660 /* Set maximum length for regular expression strings
3661 when recorded in the output file. */
3662 case 'M':
3663 max_regexp = atoi (optarg);
3664 break;
3666 /* Add to search path. */
3667 case 'I':
3668 add_search_path (optarg);
3669 break;
3671 /* Display help */
3672 case -2:
3673 usage (0);
3674 break;
3676 case -3:
3677 version ();
3678 break;
3682 /* Call init_scanner after command line flags have been processed to be
3683 able to add keywords depending on command line (not yet
3684 implemented). */
3685 init_scanner ();
3686 init_sym ();
3688 /* Open output file */
3689 if (*out_filename)
3691 yyout = fopen (out_filename, f_append ? "a" : "w");
3692 if (yyout == NULL)
3694 yyerror ("cannot open output file `%s'", out_filename);
3695 exit (1);
3699 /* Process input files specified on the command line. */
3700 while (optind < argc)
3702 process_file (argv[optind++]);
3703 any_inputfiles = 1;
3706 /* Process files given on stdin if no files specified. */
3707 if (!any_inputfiles && n_input_files == 0)
3709 char *file;
3710 while ((file = read_line (stdin)) != NULL)
3711 process_file (file);
3713 else
3715 /* Process files from `--files=FILE'. Every line in FILE names
3716 one input file to process. */
3717 for (i = 0; i < n_input_files; ++i)
3719 FILE *fp = fopen (input_filenames[i], "r");
3721 if (fp == NULL)
3722 yyerror ("cannot open input file `%s'", input_filenames[i]);
3723 else
3725 char *file;
3726 while ((file = read_line (fp)) != NULL)
3727 process_file (file);
3728 fclose (fp);
3733 /* Write output file. */
3734 dump_roots (yyout);
3736 /* Close output file. */
3737 if (yyout != stdout)
3738 fclose (yyout);
3740 return 0;
3744 /* ebrowse.c ends here. */