Commit | Line | Data |
---|---|---|
be0dbdab GM |
1 | /* ebrowse.c --- parsing files for the ebrowse C++ browser |
2 | ||
e7cff550 | 3 | Copyright (C) 1992,92,94,95,96,97,98,99,2000 Free Software Foundation Inc. |
be0dbdab GM |
4 | |
5 | Author: Gerd Moellmann <gerd@gnu.org> | |
6 | Maintainer: FSF | |
7 | ||
8 | This file is part of GNU Emacs. | |
9 | ||
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. | |
14 | ||
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. | |
19 | ||
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. */ | |
23 | ||
53245ee2 DL |
24 | #ifdef HAVE_CONFIG_H |
25 | #include <config.h> | |
26 | #endif | |
27 | ||
be0dbdab GM |
28 | #include <stdio.h> |
29 | #include <stdlib.h> | |
30 | #include <string.h> | |
31 | #include <ctype.h> | |
32 | #include <assert.h> | |
33 | #include "getopt.h" | |
34 | ||
be0dbdab GM |
35 | /* Conditionalize function prototypes. */ |
36 | ||
37 | #ifdef PROTOTYPES /* From config.h. */ | |
38 | #define P_(x) x | |
39 | #else | |
40 | #define P_(x) () | |
41 | #endif | |
42 | ||
43 | /* Value is non-zero if strings X and Y compare equal. */ | |
44 | ||
45 | #define streq(X, Y) (*(X) == *(Y) && strcmp ((X) + 1, (Y) + 1) == 0) | |
46 | ||
47 | /* The ubiquitous `max' and `min' macros. */ | |
48 | ||
49 | #ifndef max | |
50 | #define max(X, Y) ((X) > (Y) ? (X) : (Y)) | |
51 | #define min(X, Y) ((X) < (Y) ? (X) : (Y)) | |
52 | #endif | |
53 | ||
54 | /* Files are read in chunks of this number of bytes. */ | |
55 | ||
56 | #define READ_CHUNK_SIZE (100 * 1024) | |
57 | ||
58 | /* The character used as a separator in path lists (like $PATH). */ | |
59 | ||
94549912 | 60 | #if defined(__MSDOS__) |
fd72561d EZ |
61 | #define PATH_LIST_SEPARATOR ';' |
62 | #define FILENAME_EQ(X,Y) (strcasecmp(X,Y) == 0) | |
63 | #else | |
94549912 JR |
64 | #if defined(WINDOWSNT) |
65 | #define PATH_LIST_SEPARATOR ';' | |
66 | #define FILENAME_EQ(X,Y) (stricmp(X,Y) == 0) | |
67 | #else | |
be0dbdab | 68 | #define PATH_LIST_SEPARATOR ':' |
fd72561d EZ |
69 | #define FILENAME_EQ(X,Y) (streq(X,Y)) |
70 | #endif | |
94549912 | 71 | #endif |
be0dbdab GM |
72 | /* The default output file name. */ |
73 | ||
42eaac21 | 74 | #define DEFAULT_OUTFILE "BROWSE" |
be0dbdab GM |
75 | |
76 | /* A version string written to the output file. Change this whenever | |
77 | the structure of the output file changes. */ | |
78 | ||
79 | #define EBROWSE_FILE_VERSION "ebrowse 5.0" | |
80 | ||
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. */ | |
84 | ||
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 " | |
90 | ||
91 | /* The name of the symbol table entry for global functions, variables, | |
92 | defines etc. This name also appears in the browser display. */ | |
93 | ||
94 | #define GLOBALS_NAME "*Globals*" | |
95 | ||
96 | /* Token definitions. */ | |
97 | ||
98 | enum token | |
99 | { | |
100 | YYEOF = 0, /* end of file */ | |
101 | CSTRING = 256, /* string constant */ | |
102 | CCHAR, /* character constant */ | |
103 | CINT, /* integral constant */ | |
104 | CFLOAT, /* real constant */ | |
105 | ||
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, /* >> */ | |
132 | ||
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 | |
138 | ||
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 */ | |
202 | }; | |
203 | ||
204 | /* Storage classes, in a wider sense. */ | |
205 | ||
206 | enum sc | |
207 | { | |
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. */ | |
213 | }; | |
214 | ||
215 | /* Member visibility. */ | |
216 | ||
217 | enum visibility | |
218 | { | |
219 | V_PUBLIC, | |
220 | V_PROTECTED, | |
221 | V_PRIVATE | |
222 | }; | |
223 | ||
224 | /* Member flags. */ | |
225 | ||
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. */ | |
236 | ||
237 | /* Two macros to set and test a bit in an int. */ | |
238 | ||
239 | #define SET_FLAG(F, FLAG) ((F) |= (FLAG)) | |
240 | #define HAS_FLAG(F, FLAG) (((F) & (FLAG)) != 0) | |
241 | ||
242 | /* Structure describing a class member. */ | |
243 | ||
244 | struct member | |
245 | { | |
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. */ | |
259 | }; | |
260 | ||
261 | /* Structures of this type are used to connect class structures with | |
262 | their super and subclasses. */ | |
263 | ||
264 | struct link | |
265 | { | |
266 | struct sym *sym; /* The super or subclass. */ | |
267 | struct link *next; /* Next in list or NULL. */ | |
268 | }; | |
269 | ||
270 | /* Structure used to record namespace aliases. */ | |
271 | ||
272 | struct alias | |
273 | { | |
274 | struct alias *next; /* Next in list. */ | |
275 | char name[1]; /* Alias name. */ | |
276 | }; | |
277 | ||
278 | /* The structure used to describe a class in the symbol table, | |
279 | or a namespace in all_namespaces. */ | |
280 | ||
281 | struct sym | |
282 | { | |
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. */ | |
301 | }; | |
302 | ||
303 | /* Experimental: Print info for `--position-info'. We print | |
304 | '(CLASS-NAME SCOPE MEMBER-NAME). */ | |
305 | ||
306 | #define P_DEFN 1 | |
307 | #define P_DECL 2 | |
308 | ||
309 | int info_where; | |
310 | struct sym *info_cls = NULL; | |
311 | struct member *info_member = NULL; | |
312 | ||
313 | /* Experimental. For option `--position-info', the buffer position we | |
314 | are interested in. When this position is reached, print out | |
315 | information about what we know about that point. */ | |
316 | ||
317 | int info_position = -1; | |
318 | ||
319 | /* Command line options structure for getopt_long. */ | |
320 | ||
321 | struct option options[] = | |
322 | { | |
323 | {"append", no_argument, NULL, 'a'}, | |
324 | {"files", required_argument, NULL, 'f'}, | |
325 | {"help", no_argument, NULL, -2}, | |
326 | {"min-regexp-length", required_argument, NULL, 'm'}, | |
327 | {"max-regexp-length", required_argument, NULL, 'M'}, | |
328 | {"no-nested-classes", no_argument, NULL, 'n'}, | |
329 | {"no-regexps", no_argument, NULL, 'x'}, | |
330 | {"no-structs-or-unions", no_argument, NULL, 's'}, | |
331 | {"output-file", required_argument, NULL, 'o'}, | |
332 | {"position-info", required_argument, NULL, 'p'}, | |
333 | {"search-path", required_argument, NULL, 'I'}, | |
334 | {"verbose", no_argument, NULL, 'v'}, | |
335 | {"version", no_argument, NULL, -3}, | |
336 | {"very-verbose", no_argument, NULL, 'V'}, | |
337 | {NULL, 0, NULL, 0} | |
338 | }; | |
339 | ||
340 | /* Semantic values of tokens. Set by yylex.. */ | |
341 | ||
342 | unsigned yyival; /* Set for token CINT. */ | |
343 | char *yytext; /* Set for token IDENT. */ | |
344 | char *yytext_end; | |
345 | ||
346 | /* Output file. */ | |
347 | ||
348 | FILE *yyout; | |
349 | ||
350 | /* Current line number. */ | |
351 | ||
352 | int yyline; | |
353 | ||
354 | /* The name of the current input file. */ | |
355 | ||
356 | char *filename; | |
357 | ||
358 | /* Three character class vectors, and macros to test membership | |
359 | of characters. */ | |
360 | ||
361 | char is_ident[255]; | |
362 | char is_digit[255]; | |
363 | char is_white[255]; | |
364 | ||
365 | #define IDENTP(C) is_ident[(unsigned char) (C)] | |
366 | #define DIGITP(C) is_digit[(unsigned char) (C)] | |
367 | #define WHITEP(C) is_white[(unsigned char) (C)] | |
368 | ||
369 | /* Command line flags. */ | |
370 | ||
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; | |
377 | ||
378 | /* Maximum and minimum lengths of regular expressions matching a | |
379 | member, class etc., for writing them to the output file. These are | |
380 | overridable from the command line. */ | |
381 | ||
382 | int min_regexp = 5; | |
383 | int max_regexp = 50; | |
384 | ||
385 | /* Input buffer. */ | |
386 | ||
387 | char *inbuffer; | |
388 | char *in; | |
389 | int inbuffer_size; | |
390 | ||
391 | /* Return the current buffer position in the input file. */ | |
392 | ||
393 | #define BUFFER_POS() (in - inbuffer) | |
394 | ||
395 | /* If current lookahead is CSTRING, the following points to the | |
396 | first character in the string constant. Used for recognizing | |
397 | extern "C". */ | |
398 | ||
399 | char *string_start; | |
400 | ||
401 | /* The size of the hash tables for classes.and members. Should be | |
402 | prime. */ | |
403 | ||
404 | #define TABLE_SIZE 1001 | |
405 | ||
406 | /* The hash table for class symbols. */ | |
407 | ||
408 | struct sym *class_table[TABLE_SIZE]; | |
409 | ||
410 | /* Hash table containing all member structures. This is generally | |
411 | faster for member lookup than traversing the member lists of a | |
412 | `struct sym'. */ | |
413 | ||
414 | struct member *member_table[TABLE_SIZE]; | |
415 | ||
416 | /* The special class symbol used to hold global functions, | |
417 | variables etc. */ | |
418 | ||
419 | struct sym *global_symbols; | |
420 | ||
421 | /* The current namespace. */ | |
422 | ||
423 | struct sym *current_namespace; | |
424 | ||
425 | /* The list of all known namespaces. */ | |
426 | ||
427 | struct sym *all_namespaces; | |
428 | ||
429 | /* Stack of namespaces we're currently nested in, during the parse. */ | |
430 | ||
431 | struct sym **namespace_stack; | |
432 | int namespace_stack_size; | |
433 | int namespace_sp; | |
434 | ||
435 | /* The current lookahead token. */ | |
436 | ||
437 | int tk = -1; | |
438 | ||
439 | /* Structure describing a keyword. */ | |
440 | ||
441 | struct kw | |
442 | { | |
443 | char *name; /* Spelling. */ | |
444 | int tk; /* Token value. */ | |
445 | struct kw *next; /* Next in collision chain. */ | |
446 | }; | |
447 | ||
448 | /* Keywords are lookup up in a hash table of their own. */ | |
449 | ||
450 | #define KEYWORD_TABLE_SIZE 1001 | |
451 | struct kw *keyword_table[KEYWORD_TABLE_SIZE]; | |
452 | ||
453 | /* Search path. */ | |
454 | ||
455 | struct search_path | |
456 | { | |
457 | char *path; | |
458 | struct search_path *next; | |
459 | }; | |
460 | ||
461 | struct search_path *search_path; | |
462 | struct search_path *search_path_tail; | |
463 | ||
464 | /* Function prototypes. */ | |
465 | ||
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 *)); | |
c43a1ff6 | 479 | void *xmalloc P_ ((int)); |
be0dbdab GM |
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)); | |
8bef35f2 | 511 | void declaration P_ ((int)); |
be0dbdab GM |
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)); | |
518 | ||
519 | ||
520 | \f | |
521 | /*********************************************************************** | |
522 | Utilities | |
523 | ***********************************************************************/ | |
524 | ||
525 | /* Print an error in a printf-like style with the current input file | |
526 | name and line number. */ | |
527 | ||
528 | void | |
529 | yyerror (format, a1, a2, a3, a4, a5) | |
530 | char *format; | |
531 | int a1, a2, a3, a4, a5; | |
532 | { | |
533 | fprintf (stderr, "%s:%d: ", filename, yyline); | |
534 | fprintf (stderr, format, a1, a2, a3, a4, a5); | |
535 | putc ('\n', stderr); | |
536 | } | |
537 | ||
538 | ||
539 | /* Like malloc but print an error and exit if not enough memory is | |
38748aca | 540 | available. */ |
be0dbdab GM |
541 | |
542 | void * | |
c43a1ff6 | 543 | xmalloc (nbytes) |
be0dbdab GM |
544 | int nbytes; |
545 | { | |
546 | void *p = malloc (nbytes); | |
8bef35f2 GM |
547 | if (p == NULL) |
548 | { | |
549 | yyerror ("out of memory"); | |
550 | exit (1); | |
551 | } | |
552 | return p; | |
be0dbdab GM |
553 | } |
554 | ||
555 | ||
556 | /* Like realloc but print an error and exit if out of memory. */ | |
557 | ||
558 | void * | |
c43a1ff6 | 559 | xrealloc (p, sz) |
be0dbdab GM |
560 | void *p; |
561 | int sz; | |
562 | { | |
563 | p = realloc (p, sz); | |
8bef35f2 GM |
564 | if (p == NULL) |
565 | { | |
566 | yyerror ("out of memory"); | |
567 | exit (1); | |
568 | } | |
569 | return p; | |
be0dbdab GM |
570 | } |
571 | ||
572 | ||
573 | /* Like strdup, but print an error and exit if not enough memory is | |
574 | available.. If S is null, return null. */ | |
575 | ||
576 | char * | |
577 | xstrdup (s) | |
578 | char *s; | |
579 | { | |
580 | if (s) | |
c43a1ff6 | 581 | s = strcpy (xmalloc (strlen (s) + 1), s); |
be0dbdab GM |
582 | return s; |
583 | } | |
584 | ||
585 | ||
586 | \f | |
587 | /*********************************************************************** | |
588 | Symbols | |
589 | ***********************************************************************/ | |
590 | ||
591 | /* Initialize the symbol table. This currently only sets up the | |
592 | special symbol for globals (`*Globals*'). */ | |
593 | ||
594 | void | |
595 | init_sym () | |
596 | { | |
597 | global_symbols = add_sym (GLOBALS_NAME, NULL); | |
598 | } | |
599 | ||
600 | ||
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. | |
604 | ||
605 | If a symbol for NAME already exists, return that. Otherwise | |
606 | create a new symbol and set it to default values. */ | |
607 | ||
608 | struct sym * | |
609 | add_sym (name, nested_in_class) | |
610 | char *name; | |
611 | struct sym *nested_in_class; | |
612 | { | |
613 | struct sym *sym; | |
614 | unsigned h; | |
615 | char *s; | |
616 | struct sym *scope = nested_in_class ? nested_in_class : current_namespace; | |
617 | ||
618 | for (s = name, h = 0; *s; ++s) | |
619 | h = (h << 1) ^ *s; | |
620 | h %= TABLE_SIZE; | |
621 | ||
622 | for (sym = class_table[h]; sym; sym = sym->next) | |
623 | if (streq (name, sym->name) && sym->namesp == scope) | |
624 | break; | |
625 | ||
626 | if (sym == NULL) | |
627 | { | |
628 | if (f_very_verbose) | |
629 | { | |
630 | putchar ('\t'); | |
631 | puts (name); | |
632 | } | |
633 | ||
c43a1ff6 | 634 | sym = (struct sym *) xmalloc (sizeof *sym + strlen (name)); |
be0dbdab GM |
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; | |
640 | } | |
641 | ||
642 | return sym; | |
643 | } | |
644 | ||
645 | ||
646 | /* Add links between superclass SUPER and subclass SUB. */ | |
647 | ||
648 | void | |
649 | add_link (super, sub) | |
650 | struct sym *super, *sub; | |
651 | { | |
652 | struct link *lnk, *lnk2, *p, *prev; | |
653 | ||
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) | |
658 | ; | |
659 | ||
660 | /* Avoid duplicates. */ | |
661 | if (p == NULL || p->sym != sub) | |
662 | { | |
c43a1ff6 GM |
663 | lnk = (struct link *) xmalloc (sizeof *lnk); |
664 | lnk2 = (struct link *) xmalloc (sizeof *lnk2); | |
be0dbdab GM |
665 | |
666 | lnk->sym = sub; | |
667 | lnk->next = p; | |
668 | ||
669 | if (prev) | |
670 | prev->next = lnk; | |
671 | else | |
672 | super->subs = lnk; | |
673 | ||
674 | lnk2->sym = super; | |
675 | lnk2->next = sub->supers; | |
676 | sub->supers = lnk2; | |
677 | } | |
678 | } | |
679 | ||
680 | ||
681 | /* Find in class CLS member NAME. | |
682 | ||
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. */ | |
688 | ||
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; | |
695 | { | |
696 | struct member **list; | |
697 | struct member *p; | |
698 | unsigned name_hash = 0; | |
699 | char *s; | |
700 | int i; | |
701 | ||
702 | switch (sc) | |
703 | { | |
704 | case SC_FRIEND: | |
705 | list = &cls->friends; | |
706 | break; | |
707 | ||
708 | case SC_TYPE: | |
709 | list = &cls->types; | |
710 | break; | |
711 | ||
712 | case SC_STATIC: | |
713 | list = var ? &cls->static_vars : &cls->static_fns; | |
714 | break; | |
715 | ||
716 | default: | |
717 | list = var ? &cls->vars : &cls->fns; | |
718 | break; | |
719 | } | |
720 | ||
721 | for (s = name; *s; ++s) | |
722 | name_hash = (name_hash << 1) ^ *s; | |
723 | i = name_hash % TABLE_SIZE; | |
724 | ||
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; | |
728 | ||
729 | return p; | |
730 | } | |
731 | ||
732 | ||
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). */ | |
742 | ||
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; | |
754 | { | |
755 | struct member *m; | |
756 | ||
757 | m = find_member (cls, name, var, sc, hash); | |
758 | if (m == NULL) | |
759 | m = add_member (cls, name, var, sc, hash); | |
760 | ||
761 | /* Have we seen a new filename? If so record that. */ | |
fd72561d | 762 | if (!cls->filename || !FILENAME_EQ (cls->filename, filename)) |
be0dbdab GM |
763 | m->filename = filename; |
764 | ||
765 | m->regexp = regexp; | |
766 | m->pos = pos; | |
767 | m->flags = flags; | |
768 | ||
769 | switch (vis) | |
770 | { | |
771 | case PRIVATE: | |
772 | m->vis = V_PRIVATE; | |
773 | break; | |
774 | ||
775 | case PROTECTED: | |
776 | m->vis = V_PROTECTED; | |
777 | break; | |
778 | ||
779 | case PUBLIC: | |
780 | m->vis = V_PUBLIC; | |
781 | break; | |
782 | } | |
783 | ||
784 | info_where = P_DECL; | |
785 | info_cls = cls; | |
786 | info_member = m; | |
787 | } | |
788 | ||
789 | ||
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). */ | |
799 | ||
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; | |
810 | { | |
811 | struct member *m; | |
812 | ||
813 | if (sc == SC_UNKNOWN) | |
814 | { | |
815 | m = find_member (cls, name, var, SC_MEMBER, hash); | |
816 | if (m == NULL) | |
817 | { | |
818 | m = find_member (cls, name, var, SC_STATIC, hash); | |
819 | if (m == NULL) | |
820 | m = add_member (cls, name, var, sc, hash); | |
821 | } | |
822 | } | |
823 | else | |
824 | { | |
825 | m = find_member (cls, name, var, sc, hash); | |
826 | if (m == NULL) | |
827 | m = add_member (cls, name, var, sc, hash); | |
828 | } | |
829 | ||
830 | if (!cls->sfilename) | |
831 | cls->sfilename = filename; | |
832 | ||
fd72561d | 833 | if (!FILENAME_EQ (cls->sfilename, filename)) |
be0dbdab GM |
834 | m->def_filename = filename; |
835 | ||
836 | m->def_regexp = regexp; | |
837 | m->def_pos = pos; | |
838 | m->flags |= flags; | |
839 | ||
840 | info_where = P_DEFN; | |
841 | info_cls = cls; | |
842 | info_member = m; | |
843 | } | |
844 | ||
845 | ||
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. */ | |
849 | ||
850 | void | |
851 | add_define (name, regexp, pos) | |
852 | char *name, *regexp; | |
853 | int pos; | |
854 | { | |
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); | |
857 | } | |
858 | ||
859 | ||
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). */ | |
869 | ||
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; | |
878 | { | |
879 | int i; | |
880 | struct sym *sym; | |
881 | ||
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); | |
891 | ||
892 | /* Add to global symbols. */ | |
893 | add_member_defn (global_symbols, name, regexp, pos, hash, var, sc, flags); | |
894 | } | |
895 | ||
896 | ||
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). */ | |
906 | ||
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; | |
915 | { | |
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; | |
921 | ||
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); | |
925 | ||
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) | |
930 | { | |
931 | if (!global_symbols->filename | |
fd72561d | 932 | || !FILENAME_EQ (global_symbols->filename, filename)) |
be0dbdab GM |
933 | m->filename = filename; |
934 | ||
935 | m->regexp = regexp; | |
936 | m->pos = pos; | |
937 | m->vis = V_PUBLIC; | |
938 | m->flags = flags; | |
939 | ||
940 | info_where = P_DECL; | |
941 | info_cls = global_symbols; | |
942 | info_member = m; | |
943 | } | |
944 | } | |
945 | ||
946 | ||
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. */ | |
951 | ||
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; | |
959 | { | |
c43a1ff6 | 960 | struct member *m = (struct member *) xmalloc (sizeof *m + strlen (name)); |
be0dbdab GM |
961 | struct member **list; |
962 | struct member *p; | |
963 | struct member *prev; | |
964 | unsigned name_hash = 0; | |
965 | int i; | |
966 | char *s; | |
967 | ||
968 | strcpy (m->name, name); | |
969 | m->param_hash = hash; | |
970 | ||
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; | |
979 | ||
980 | assert (cls != NULL); | |
981 | ||
982 | switch (sc) | |
983 | { | |
984 | case SC_FRIEND: | |
985 | list = &cls->friends; | |
986 | break; | |
987 | ||
988 | case SC_TYPE: | |
989 | list = &cls->types; | |
990 | break; | |
991 | ||
992 | case SC_STATIC: | |
993 | list = var ? &cls->static_vars : &cls->static_fns; | |
994 | break; | |
995 | ||
996 | default: | |
997 | list = var ? &cls->vars : &cls->fns; | |
998 | break; | |
999 | } | |
1000 | ||
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; | |
1007 | ||
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) | |
1013 | ; | |
1014 | ||
1015 | m->next = p; | |
1016 | if (prev) | |
1017 | prev->next = m; | |
1018 | else | |
1019 | *list = m; | |
1020 | return m; | |
1021 | } | |
1022 | ||
1023 | ||
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. */ | |
1027 | ||
1028 | void | |
1029 | mark_virtual (r) | |
1030 | struct sym *r; | |
1031 | { | |
1032 | struct link *p; | |
1033 | struct member *m, *m2; | |
1034 | ||
1035 | for (p = r->subs; p; p = p->next) | |
1036 | { | |
1037 | for (m = r->fns; m; m = m->next) | |
1038 | if (HAS_FLAG (m->flags, F_VIRTUAL)) | |
1039 | { | |
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); | |
1043 | } | |
1044 | ||
1045 | mark_virtual (p->sym); | |
1046 | } | |
1047 | } | |
1048 | ||
1049 | ||
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. */ | |
1052 | ||
1053 | void | |
1054 | mark_inherited_virtual () | |
1055 | { | |
1056 | struct sym *r; | |
1057 | int i; | |
1058 | ||
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); | |
1063 | } | |
1064 | ||
1065 | ||
1066 | /* Create and return a symbol for a namespace with name NAME. */ | |
1067 | ||
1068 | struct sym * | |
1069 | make_namespace (name) | |
1070 | char *name; | |
1071 | { | |
c43a1ff6 | 1072 | struct sym *s = (struct sym *) xmalloc (sizeof *s + strlen (name)); |
be0dbdab GM |
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; | |
1079 | } | |
1080 | ||
1081 | ||
1082 | /* Find the symbol for namespace NAME. If not found, add a new symbol | |
1083 | for NAME to all_namespaces. */ | |
1084 | ||
1085 | struct sym * | |
1086 | find_namespace (name) | |
1087 | char *name; | |
1088 | { | |
1089 | struct sym *p; | |
1090 | ||
1091 | for (p = all_namespaces; p; p = p->next) | |
1092 | { | |
1093 | if (streq (p->name, name)) | |
1094 | break; | |
1095 | else | |
1096 | { | |
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; | |
1103 | } | |
1104 | } | |
1105 | ||
1106 | if (p == NULL) | |
1107 | p = make_namespace (name); | |
1108 | ||
1109 | return p; | |
1110 | } | |
1111 | ||
1112 | ||
1113 | /* Register the name NEW_NAME as an alias for namespace OLD_NAME. */ | |
1114 | ||
1115 | void | |
1116 | register_namespace_alias (new_name, old_name) | |
1117 | char *new_name, *old_name; | |
1118 | { | |
1119 | struct sym *p = find_namespace (old_name); | |
1120 | struct alias *al; | |
1121 | ||
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; | |
1126 | ||
c43a1ff6 | 1127 | al = (struct alias *) xmalloc (sizeof *al + strlen (new_name)); |
be0dbdab GM |
1128 | strcpy (al->name, new_name); |
1129 | al->next = p->namesp_aliases; | |
1130 | p->namesp_aliases = al; | |
1131 | } | |
1132 | ||
1133 | ||
1134 | /* Enter namespace with name NAME. */ | |
1135 | ||
1136 | void | |
1137 | enter_namespace (name) | |
1138 | char *name; | |
1139 | { | |
1140 | struct sym *p = find_namespace (name); | |
1141 | ||
1142 | if (namespace_sp == namespace_stack_size) | |
1143 | { | |
1144 | int size = max (10, 2 * namespace_stack_size); | |
c43a1ff6 | 1145 | namespace_stack = (struct sym **) xrealloc (namespace_stack, size); |
be0dbdab GM |
1146 | namespace_stack_size = size; |
1147 | } | |
1148 | ||
1149 | namespace_stack[namespace_sp++] = current_namespace; | |
1150 | current_namespace = p; | |
1151 | } | |
1152 | ||
1153 | ||
1154 | /* Leave the current namespace. */ | |
1155 | ||
1156 | void | |
1157 | leave_namespace () | |
1158 | { | |
1159 | assert (namespace_sp > 0); | |
1160 | current_namespace = namespace_stack[--namespace_sp]; | |
1161 | } | |
1162 | ||
1163 | ||
1164 | \f | |
1165 | /*********************************************************************** | |
1166 | Writing the Output File | |
1167 | ***********************************************************************/ | |
1168 | ||
1169 | /* Write string S to the output file FP in a Lisp-readable form. | |
1170 | If S is null, write out `()'. */ | |
1171 | ||
1172 | #define PUTSTR(s, fp) \ | |
1173 | do { \ | |
1174 | if (!s) \ | |
1175 | { \ | |
1176 | putc ('(', fp); \ | |
1177 | putc (')', fp); \ | |
1178 | putc (' ', fp); \ | |
1179 | } \ | |
1180 | else \ | |
1181 | { \ | |
1182 | putc ('"', fp); \ | |
1183 | fputs (s, fp); \ | |
1184 | putc ('"', fp); \ | |
1185 | putc (' ', fp); \ | |
1186 | } \ | |
1187 | } while (0) | |
1188 | ||
1189 | /* A dynamically allocated buffer for constructing a scope name. */ | |
1190 | ||
1191 | char *scope_buffer; | |
1192 | int scope_buffer_size; | |
1193 | int scope_buffer_len; | |
1194 | ||
1195 | ||
1196 | /* Make sure scope_buffer has enough room to add LEN chars to it. */ | |
1197 | ||
1198 | void | |
1199 | ensure_scope_buffer_room (len) | |
1200 | int len; | |
1201 | { | |
1202 | if (scope_buffer_len + len >= scope_buffer_size) | |
1203 | { | |
1204 | int new_size = max (2 * scope_buffer_size, scope_buffer_len + len); | |
f94b82d9 | 1205 | scope_buffer = (char *) xrealloc (scope_buffer, new_size); |
be0dbdab GM |
1206 | scope_buffer_size = new_size; |
1207 | } | |
1208 | } | |
1209 | ||
1210 | ||
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. */ | |
1214 | ||
1215 | char * | |
1216 | sym_scope_1 (p) | |
1217 | struct sym *p; | |
1218 | { | |
1219 | int len; | |
1220 | ||
1221 | if (p->namesp) | |
1222 | sym_scope_1 (p->namesp); | |
1223 | ||
1224 | if (*scope_buffer) | |
1225 | { | |
1226 | ensure_scope_buffer_room (3); | |
1227 | strcat (scope_buffer, "::"); | |
1228 | scope_buffer_len += 2; | |
1229 | } | |
1230 | ||
1231 | len = strlen (p->name); | |
1232 | ensure_scope_buffer_room (len + 1); | |
1233 | strcat (scope_buffer, p->name); | |
1234 | scope_buffer_len += len; | |
1235 | ||
1236 | if (HAS_FLAG (p->flags, F_TEMPLATE)) | |
1237 | { | |
1238 | ensure_scope_buffer_room (3); | |
1239 | strcat (scope_buffer, "<>"); | |
1240 | scope_buffer_len += 2; | |
1241 | } | |
1242 | ||
1243 | return scope_buffer; | |
1244 | } | |
1245 | ||
1246 | ||
1247 | /* Return the scope of symbol P in printed representation, i.e. | |
1248 | as it would appear in a C*+ source file. */ | |
1249 | ||
1250 | char * | |
1251 | sym_scope (p) | |
1252 | struct sym *p; | |
1253 | { | |
1254 | if (!scope_buffer) | |
1255 | { | |
1256 | scope_buffer_size = 1024; | |
c43a1ff6 | 1257 | scope_buffer = (char *) xmalloc (scope_buffer_size); |
be0dbdab GM |
1258 | } |
1259 | ||
1260 | *scope_buffer = '\0'; | |
1261 | scope_buffer_len = 0; | |
1262 | ||
1263 | if (p->namesp) | |
1264 | sym_scope_1 (p->namesp); | |
1265 | ||
1266 | return scope_buffer; | |
1267 | } | |
1268 | ||
1269 | ||
1270 | /* Dump the list of members M to file FP. Value is the length of the | |
1271 | list. */ | |
1272 | ||
1273 | int | |
1274 | dump_members (fp, m) | |
1275 | FILE *fp; | |
1276 | struct member *m; | |
1277 | { | |
1278 | int n; | |
1279 | ||
1280 | putc ('(', fp); | |
1281 | ||
1282 | for (n = 0; m; m = m->next, ++n) | |
1283 | { | |
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); | |
1298 | } | |
1299 | ||
1300 | putc (')', fp); | |
1301 | putc ('\n', fp); | |
1302 | return n; | |
1303 | } | |
1304 | ||
1305 | ||
1306 | /* Dump class ROOT to stream FP. */ | |
1307 | ||
1308 | void | |
1309 | dump_sym (fp, root) | |
1310 | FILE *fp; | |
1311 | struct sym *root; | |
1312 | { | |
1313 | fputs (CLASS_STRUCT, fp); | |
1314 | PUTSTR (root->name, fp); | |
1315 | ||
1316 | /* Print scope, if any. */ | |
1317 | if (root->namesp) | |
1318 | PUTSTR (sym_scope (root), fp); | |
1319 | else | |
1320 | PUTSTR (NULL, fp); | |
1321 | ||
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); | |
1330 | } | |
1331 | ||
1332 | ||
1333 | /* Dump class ROOT and its subclasses to file FP. Value is the | |
1334 | number of classes written. */ | |
1335 | ||
1336 | int | |
1337 | dump_tree (fp, root) | |
1338 | FILE *fp; | |
1339 | struct sym *root; | |
1340 | { | |
1341 | struct link *lk; | |
1342 | unsigned n = 0; | |
1343 | ||
1344 | dump_sym (fp, root); | |
1345 | ||
1346 | if (f_verbose) | |
1347 | { | |
1348 | putchar ('+'); | |
1349 | fflush (stdout); | |
1350 | } | |
1351 | ||
1352 | putc ('(', fp); | |
1353 | ||
1354 | for (lk = root->subs; lk; lk = lk->next) | |
1355 | { | |
1356 | fputs (TREE_STRUCT, fp); | |
1357 | n += dump_tree (fp, lk->sym); | |
1358 | putc (']', fp); | |
1359 | } | |
1360 | ||
1361 | putc (')', fp); | |
1362 | ||
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); | |
1369 | ||
1370 | /* Superclasses. */ | |
1371 | putc ('(', fp); | |
1372 | putc (')', fp); | |
1373 | ||
1374 | /* Mark slot. */ | |
1375 | putc ('(', fp); | |
1376 | putc (')', fp); | |
1377 | ||
1378 | putc ('\n', fp); | |
1379 | return n; | |
1380 | } | |
1381 | ||
1382 | ||
1383 | /* Dump the entire class tree to file FP. */ | |
1384 | ||
1385 | void | |
1386 | dump_roots (fp) | |
1387 | FILE *fp; | |
1388 | { | |
1389 | int i, n = 0; | |
1390 | struct sym *r; | |
1391 | ||
1392 | /* Output file header containing version string, command line | |
1393 | options etc. */ | |
1394 | if (!f_append) | |
1395 | { | |
1396 | fputs (TREE_HEADER_STRUCT, fp); | |
1397 | PUTSTR (EBROWSE_FILE_VERSION, fp); | |
1398 | ||
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); | |
1408 | } | |
1409 | ||
1410 | /* Mark functions as virtual that are so because of functions | |
1411 | declared virtual in base classes. */ | |
1412 | mark_inherited_virtual (); | |
1413 | ||
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) | |
1418 | { | |
1419 | fputs (TREE_STRUCT, fp); | |
1420 | n += dump_tree (fp, r); | |
1421 | putc (']', fp); | |
1422 | } | |
1423 | ||
1424 | if (f_verbose) | |
1425 | putchar ('\n'); | |
1426 | } | |
1427 | ||
1428 | ||
1429 | \f | |
1430 | /*********************************************************************** | |
1431 | Scanner | |
1432 | ***********************************************************************/ | |
1433 | ||
1434 | #ifdef DEBUG | |
1435 | #define INCREMENT_LINENO \ | |
1436 | do { \ | |
1437 | if (f_very_verbose) \ | |
1438 | { \ | |
1439 | ++yyline; \ | |
1440 | printf ("%d:\n", yyline); \ | |
1441 | } \ | |
1442 | else \ | |
1443 | ++yyline; \ | |
1444 | } while (0) | |
1445 | #else | |
1446 | #define INCREMENT_LINENO ++yyline | |
1447 | #endif | |
1448 | ||
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. */ | |
1452 | ||
1453 | #define GET(C) ((C) = *in++) | |
1454 | #define UNGET() (--in) | |
1455 | ||
1456 | ||
1457 | /* Process a preprocessor line. Value is the next character from the | |
1458 | input buffer not consumed. */ | |
1459 | ||
1460 | int | |
1461 | process_pp_line () | |
1462 | { | |
d65b0571 | 1463 | int in_comment = 0, in_string = 0; |
be0dbdab GM |
1464 | int c; |
1465 | char *p = yytext; | |
1466 | ||
1467 | /* Skip over white space. The `#' has been consumed already. */ | |
1468 | while (WHITEP (GET (c))) | |
1469 | ; | |
1470 | ||
1471 | /* Read the preprocessor command (if any). */ | |
1472 | while (IDENTP (c)) | |
1473 | { | |
1474 | *p++ = c; | |
1475 | GET (c); | |
1476 | } | |
1477 | ||
1478 | /* Is it a `define'? */ | |
1479 | *p = '\0'; | |
1480 | ||
1481 | if (*yytext && streq (yytext, "define")) | |
1482 | { | |
1483 | p = yytext; | |
1484 | while (WHITEP (c)) | |
1485 | GET (c); | |
1486 | while (IDENTP (c)) | |
1487 | { | |
1488 | *p++ = c; | |
1489 | GET (c); | |
1490 | } | |
1491 | ||
1492 | *p = '\0'; | |
1493 | ||
1494 | if (*yytext) | |
1495 | { | |
1496 | char *regexp = matching_regexp (); | |
1497 | int pos = BUFFER_POS (); | |
1498 | add_define (yytext, regexp, pos); | |
1499 | } | |
1500 | } | |
1501 | ||
d65b0571 | 1502 | while (c && (c != '\n' || in_comment || in_string)) |
be0dbdab GM |
1503 | { |
1504 | if (c == '\\') | |
1505 | GET (c); | |
1506 | else if (c == '/' && !in_comment) | |
1507 | { | |
1508 | if (GET (c) == '*') | |
1509 | in_comment = 1; | |
1510 | } | |
1511 | else if (c == '*' && in_comment) | |
1512 | { | |
1513 | if (GET (c) == '/') | |
1514 | in_comment = 0; | |
1515 | } | |
d65b0571 GM |
1516 | else if (c == '"') |
1517 | in_string = !in_string; | |
be0dbdab GM |
1518 | |
1519 | if (c == '\n') | |
1520 | INCREMENT_LINENO; | |
1521 | ||
1522 | GET (c); | |
1523 | } | |
d65b0571 | 1524 | |
be0dbdab GM |
1525 | return c; |
1526 | } | |
1527 | ||
1528 | ||
1529 | /* Value is the next token from the input buffer. */ | |
1530 | ||
1531 | int | |
1532 | yylex () | |
1533 | { | |
1534 | int c; | |
1535 | char end_char; | |
1536 | char *p; | |
1537 | ||
1538 | for (;;) | |
1539 | { | |
1540 | while (WHITEP (GET (c))) | |
1541 | ; | |
1542 | ||
1543 | switch (c) | |
1544 | { | |
1545 | case '\n': | |
1546 | INCREMENT_LINENO; | |
1547 | break; | |
1548 | ||
1549 | case '\r': | |
1550 | break; | |
1551 | ||
1552 | case 0: | |
1553 | /* End of file. */ | |
1554 | return YYEOF; | |
1555 | ||
1556 | case '\\': | |
1557 | GET (c); | |
1558 | break; | |
1559 | ||
1560 | case '"': | |
1561 | case '\'': | |
1562 | /* String and character constants. */ | |
1563 | end_char = c; | |
1564 | string_start = in; | |
1565 | while (GET (c) && c != end_char) | |
1566 | { | |
1567 | switch (c) | |
1568 | { | |
1569 | case '\\': | |
1570 | /* Escape sequences. */ | |
1571 | if (!GET (c)) | |
1572 | { | |
1573 | if (end_char == '\'') | |
1574 | yyerror ("EOF in character constant"); | |
1575 | else | |
1576 | yyerror ("EOF in string constant"); | |
1577 | goto end_string; | |
1578 | } | |
1579 | else switch (c) | |
1580 | { | |
1581 | case '\n': | |
d65b0571 | 1582 | INCREMENT_LINENO; |
be0dbdab GM |
1583 | case 'a': |
1584 | case 'b': | |
1585 | case 'f': | |
1586 | case 'n': | |
1587 | case 'r': | |
1588 | case 't': | |
1589 | case 'v': | |
1590 | break; | |
1591 | ||
1592 | case 'x': | |
1593 | { | |
1594 | /* Hexadecimal escape sequence. */ | |
1595 | int i; | |
1596 | for (i = 0; i < 2; ++i) | |
1597 | { | |
1598 | GET (c); | |
1599 | ||
1600 | if (c >= '0' && c <= '7') | |
1601 | ; | |
1602 | else if (c >= 'a' && c <= 'f') | |
1603 | ; | |
1604 | else if (c >= 'A' && c <= 'F') | |
1605 | ; | |
1606 | else | |
1607 | { | |
1608 | UNGET (); | |
1609 | break; | |
1610 | } | |
1611 | } | |
1612 | } | |
1613 | break; | |
1614 | ||
1615 | case '0': | |
1616 | { | |
1617 | /* Octal escape sequence. */ | |
1618 | int i; | |
1619 | for (i = 0; i < 3; ++i) | |
1620 | { | |
1621 | GET (c); | |
1622 | ||
1623 | if (c >= '0' && c <= '7') | |
1624 | ; | |
1625 | else | |
1626 | { | |
1627 | UNGET (); | |
1628 | break; | |
1629 | } | |
1630 | } | |
1631 | } | |
1632 | break; | |
1633 | ||
1634 | default: | |
1635 | break; | |
1636 | } | |
1637 | break; | |
1638 | ||
1639 | case '\n': | |
1640 | if (end_char == '\'') | |
1641 | yyerror ("newline in character constant"); | |
1642 | else | |
1643 | yyerror ("newline in string constant"); | |
1644 | INCREMENT_LINENO; | |
d65b0571 | 1645 | break; |
be0dbdab GM |
1646 | |
1647 | default: | |
1648 | break; | |
1649 | } | |
1650 | } | |
1651 | ||
1652 | end_string: | |
1653 | return end_char == '\'' ? CCHAR : CSTRING; | |
1654 | ||
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 '_': | |
1663 | { | |
1664 | /* Identifier and keywords. */ | |
1665 | unsigned hash; | |
1666 | struct kw *k; | |
1667 | ||
1668 | p = yytext; | |
1669 | *p++ = hash = c; | |
1670 | ||
1671 | while (IDENTP (GET (*p))) | |
1672 | { | |
1673 | hash = (hash << 1) ^ *p++; | |
1674 | if (p == yytext_end - 1) | |
1675 | { | |
1676 | int size = yytext_end - yytext; | |
c43a1ff6 | 1677 | yytext = (char *) xrealloc (yytext, 2 * size); |
be0dbdab GM |
1678 | yytext_end = yytext + 2 * size; |
1679 | p = yytext + size - 1; | |
1680 | } | |
1681 | } | |
1682 | ||
1683 | UNGET (); | |
1684 | *p = 0; | |
1685 | ||
1686 | for (k = keyword_table[hash % KEYWORD_TABLE_SIZE]; k; k = k->next) | |
1687 | if (streq (k->name, yytext)) | |
1688 | return k->tk; | |
1689 | ||
1690 | return IDENT; | |
1691 | } | |
1692 | ||
1693 | case '/': | |
1694 | /* C and C++ comments, '/' and '/='. */ | |
1695 | switch (GET (c)) | |
1696 | { | |
1697 | case '*': | |
1698 | while (GET (c)) | |
1699 | { | |
1700 | switch (c) | |
1701 | { | |
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; | |
1713 | } | |
1714 | } | |
1715 | comment_end:; | |
1716 | break; | |
1717 | ||
1718 | case '=': | |
1719 | return DIVASGN; | |
1720 | ||
1721 | case '/': | |
1722 | while (GET (c) && c != '\n') | |
1723 | ; | |
1724 | INCREMENT_LINENO; | |
1725 | break; | |
1726 | ||
1727 | default: | |
1728 | UNGET (); | |
1729 | return '/'; | |
1730 | } | |
1731 | break; | |
1732 | ||
1733 | case '+': | |
1734 | if (GET (c) == '+') | |
1735 | return INC; | |
1736 | else if (c == '=') | |
1737 | return ADDASGN; | |
1738 | UNGET (); | |
1739 | return '+'; | |
1740 | ||
1741 | case '-': | |
1742 | switch (GET (c)) | |
1743 | { | |
1744 | case '-': | |
1745 | return DEC; | |
1746 | case '>': | |
1747 | if (GET (c) == '*') | |
1748 | return ARROWSTAR; | |
1749 | UNGET (); | |
1750 | return ARROW; | |
1751 | case '=': | |
1752 | return SUBASGN; | |
1753 | } | |
1754 | UNGET (); | |
1755 | return '-'; | |
1756 | ||
1757 | case '*': | |
1758 | if (GET (c) == '=') | |
1759 | return MULASGN; | |
1760 | UNGET (); | |
1761 | return '*'; | |
1762 | ||
1763 | case '%': | |
1764 | if (GET (c) == '=') | |
1765 | return MODASGN; | |
1766 | UNGET (); | |
1767 | return '%'; | |
1768 | ||
1769 | case '|': | |
1770 | if (GET (c) == '|') | |
1771 | return LOR; | |
1772 | else if (c == '=') | |
1773 | return ORASGN; | |
1774 | UNGET (); | |
1775 | return '|'; | |
1776 | ||
1777 | case '&': | |
1778 | if (GET (c) == '&') | |
1779 | return LAND; | |
1780 | else if (c == '=') | |
1781 | return ANDASGN; | |
1782 | UNGET (); | |
1783 | return '&'; | |
1784 | ||
1785 | case '^': | |
1786 | if (GET (c) == '=') | |
1787 | return XORASGN; | |
1788 | UNGET (); | |
1789 | return '^'; | |
1790 | ||
1791 | case '.': | |
1792 | if (GET (c) == '*') | |
1793 | return POINTSTAR; | |
1794 | else if (c == '.') | |
1795 | { | |
1796 | if (GET (c) != '.') | |
1797 | yyerror ("invalid token '..' ('...' assumed)"); | |
1798 | UNGET (); | |
1799 | return ELLIPSIS; | |
1800 | } | |
1801 | else if (!DIGITP (c)) | |
1802 | { | |
1803 | UNGET (); | |
1804 | return '.'; | |
1805 | } | |
1806 | goto mantissa; | |
1807 | ||
1808 | case ':': | |
1809 | if (GET (c) == ':') | |
1810 | return DCOLON; | |
1811 | UNGET (); | |
1812 | return ':'; | |
1813 | ||
1814 | case '=': | |
1815 | if (GET (c) == '=') | |
1816 | return EQ; | |
1817 | UNGET (); | |
1818 | return '='; | |
1819 | ||
1820 | case '!': | |
1821 | if (GET (c) == '=') | |
1822 | return NE; | |
1823 | UNGET (); | |
1824 | return '!'; | |
1825 | ||
1826 | case '<': | |
1827 | switch (GET (c)) | |
1828 | { | |
1829 | case '=': | |
1830 | return LE; | |
1831 | case '<': | |
1832 | if (GET (c) == '=') | |
1833 | return LSHIFTASGN; | |
1834 | UNGET (); | |
1835 | return LSHIFT; | |
1836 | } | |
1837 | UNGET (); | |
1838 | return '<'; | |
1839 | ||
1840 | case '>': | |
1841 | switch (GET (c)) | |
1842 | { | |
1843 | case '=': | |
1844 | return GE; | |
1845 | case '>': | |
1846 | if (GET (c) == '=') | |
1847 | return RSHIFTASGN; | |
1848 | UNGET (); | |
1849 | return RSHIFT; | |
1850 | } | |
1851 | UNGET (); | |
1852 | return '>'; | |
1853 | ||
1854 | case '#': | |
1855 | c = process_pp_line (); | |
1856 | if (c == 0) | |
1857 | return YYEOF; | |
1858 | break; | |
1859 | ||
1860 | case '(': case ')': case '[': case ']': case '{': case '}': | |
1861 | case ';': case ',': case '?': case '~': | |
1862 | return c; | |
1863 | ||
1864 | case '0': | |
1865 | yyival = 0; | |
1866 | ||
1867 | if (GET (c) == 'x' || c == 'X') | |
1868 | { | |
1869 | while (GET (c)) | |
1870 | { | |
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; | |
1879 | } | |
1880 | ||
1881 | goto int_suffixes; | |
1882 | } | |
1883 | else if (c == '.') | |
1884 | goto mantissa; | |
1885 | ||
1886 | while (c >= '0' && c <= '7') | |
1887 | { | |
1888 | yyival = (yyival << 3) + c - '0'; | |
1889 | GET (c); | |
1890 | } | |
1891 | ||
1892 | int_suffixes: | |
1893 | /* Integer suffixes. */ | |
1894 | while (isalpha (c)) | |
1895 | GET (c); | |
1896 | UNGET (); | |
1897 | return CINT; | |
1898 | ||
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'; | |
1903 | ||
1904 | while (GET (c) && DIGITP (c)) | |
1905 | yyival = 10 * yyival + c - '0'; | |
1906 | ||
1907 | if (c != '.') | |
1908 | goto int_suffixes; | |
1909 | ||
1910 | mantissa: | |
1911 | /* Digits following '.'. */ | |
1912 | while (DIGITP (c)) | |
1913 | GET (c); | |
1914 | ||
1915 | /* Optional exponent. */ | |
1916 | if (c == 'E' || c == 'e') | |
1917 | { | |
1918 | if (GET (c) == '-' || c == '+') | |
1919 | GET (c); | |
1920 | ||
1921 | while (DIGITP (c)) | |
1922 | GET (c); | |
1923 | } | |
1924 | ||
1925 | /* Optional type suffixes. */ | |
1926 | while (isalpha (c)) | |
1927 | GET (c); | |
1928 | UNGET (); | |
1929 | return CFLOAT; | |
1930 | ||
1931 | default: | |
1932 | break; | |
1933 | } | |
1934 | } | |
1935 | } | |
1936 | ||
1937 | ||
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. */ | |
1941 | ||
1942 | char * | |
1943 | matching_regexp () | |
1944 | { | |
1945 | char *p; | |
1946 | char *s; | |
1947 | char *t; | |
1948 | static char *buffer, *end_buf; | |
1949 | ||
1950 | if (!f_regexps) | |
1951 | return NULL; | |
1952 | ||
1953 | if (buffer == NULL) | |
1954 | { | |
c43a1ff6 | 1955 | buffer = (char *) xmalloc (max_regexp); |
be0dbdab GM |
1956 | end_buf = &buffer[max_regexp] - 1; |
1957 | } | |
1958 | ||
1959 | /* Scan back to previous newline of buffer start. */ | |
1960 | for (p = in - 1; p > inbuffer && *p != '\n'; --p) | |
1961 | ; | |
1962 | ||
1963 | if (*p == '\n') | |
1964 | { | |
1965 | while (in - p < min_regexp && p > inbuffer) | |
1966 | { | |
1967 | /* Line probably not significant enough */ | |
1968 | for (--p; p >= inbuffer && *p != '\n'; --p) | |
1969 | ; | |
1970 | } | |
1971 | if (*p == '\n') | |
1972 | ++p; | |
1973 | } | |
1974 | ||
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;) | |
1979 | { | |
1980 | *--s = *--t; | |
1981 | ||
1982 | if (*s == '"') | |
1983 | *--s = '\\'; | |
1984 | } | |
1985 | ||
1986 | *(end_buf - 1) = '\0'; | |
1987 | return xstrdup (s); | |
1988 | } | |
1989 | ||
1990 | ||
1991 | /* Return a printable representation of token T. */ | |
1992 | ||
1993 | char * | |
1994 | token_string (t) | |
1995 | int t; | |
1996 | { | |
1997 | static char b[3]; | |
1998 | ||
1999 | switch (t) | |
2000 | { | |
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"; | |
2faf048a GM |
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"; | |
be0dbdab | 2094 | case YYEOF: return "EOF"; |
be0dbdab | 2095 | |
2faf048a GM |
2096 | default: |
2097 | if (t < 255) | |
2098 | { | |
2099 | b[0] = t; | |
2100 | b[1] = '\0'; | |
2101 | return b; | |
2102 | } | |
2103 | else | |
2104 | return "???"; | |
2105 | } | |
be0dbdab GM |
2106 | } |
2107 | ||
2108 | ||
2109 | /* Reinitialize the scanner for a new input file. */ | |
2110 | ||
2111 | void | |
2112 | re_init_scanner () | |
2113 | { | |
2114 | in = inbuffer; | |
2115 | yyline = 1; | |
2116 | ||
2117 | if (yytext == NULL) | |
2118 | { | |
2119 | int size = 256; | |
c43a1ff6 | 2120 | yytext = (char *) xmalloc (size * sizeof *yytext); |
be0dbdab GM |
2121 | yytext_end = yytext + size; |
2122 | } | |
2123 | } | |
2124 | ||
2125 | ||
2126 | /* Insert a keyword NAME with token value TK into the keyword hash | |
2127 | table. */ | |
2128 | ||
2129 | void | |
2130 | insert_keyword (name, tk) | |
2131 | char *name; | |
2132 | int tk; | |
2133 | { | |
2134 | char *s; | |
2135 | unsigned h = 0; | |
c43a1ff6 | 2136 | struct kw *k = (struct kw *) xmalloc (sizeof *k); |
be0dbdab GM |
2137 | |
2138 | for (s = name; *s; ++s) | |
2139 | h = (h << 1) ^ *s; | |
2140 | ||
2141 | h %= KEYWORD_TABLE_SIZE; | |
2142 | k->name = name; | |
2143 | k->tk = tk; | |
2144 | k->next = keyword_table[h]; | |
2145 | keyword_table[h] = k; | |
2146 | } | |
2147 | ||
2148 | ||
2149 | /* Initialize the scanner for the first file. This sets up the | |
2150 | character class vectors and fills the keyword hash table. */ | |
2151 | ||
2152 | void | |
2153 | init_scanner () | |
2154 | { | |
2155 | int i; | |
2156 | ||
2157 | /* Allocate the input buffer */ | |
2158 | inbuffer_size = READ_CHUNK_SIZE + 1; | |
c43a1ff6 | 2159 | inbuffer = in = (char *) xmalloc (inbuffer_size); |
be0dbdab GM |
2160 | yyline = 1; |
2161 | ||
2162 | /* Set up character class vectors. */ | |
2163 | for (i = 0; i < sizeof is_ident; ++i) | |
2164 | { | |
2165 | if (i == '_' || isalnum (i)) | |
2166 | is_ident[i] = 1; | |
2167 | ||
2168 | if (i >= '0' && i <= '9') | |
2169 | is_digit[i] = 1; | |
2170 | ||
2171 | if (i == ' ' || i == '\t' || i == '\f' || i == '\v') | |
2172 | is_white[i] = 1; | |
2173 | } | |
2174 | ||
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); | |
2249 | } | |
2250 | ||
2251 | ||
2252 | \f | |
2253 | /*********************************************************************** | |
2254 | Parser | |
2255 | ***********************************************************************/ | |
2256 | ||
2257 | /* Match the current lookahead token and set it to the next token. */ | |
2258 | ||
2259 | #define MATCH() (tk = yylex ()) | |
2260 | ||
2261 | /* Return the lookahead token. If current lookahead token is cleared, | |
2262 | read a new token. */ | |
2263 | ||
2264 | #define LA1 (tk == -1 ? (tk = yylex ()) : tk) | |
2265 | ||
2266 | /* Is the current lookahead equal to the token T? */ | |
2267 | ||
2268 | #define LOOKING_AT(T) (tk == (T)) | |
2269 | ||
2270 | /* Is the current lookahead one of T1 or T2? */ | |
2271 | ||
2272 | #define LOOKING_AT2(T1, T2) (tk == (T1) || tk == (T2)) | |
2273 | ||
2274 | /* Is the current lookahead one of T1, T2 or T3? */ | |
2275 | ||
2276 | #define LOOKING_AT3(T1, T2, T3) (tk == (T1) || tk == (T2) || tk == (T3)) | |
2277 | ||
2278 | /* Is the current lookahead one of T1...T4? */ | |
2279 | ||
2280 | #define LOOKING_AT4(T1, T2, T3, T4) \ | |
2281 | (tk == (T1) || tk == (T2) || tk == (T3) || tk == (T4)) | |
2282 | ||
2283 | /* Match token T if current lookahead is T. */ | |
2284 | ||
2285 | #define MATCH_IF(T) if (LOOKING_AT (T)) MATCH (); else ((void) 0) | |
2286 | ||
2287 | /* Skip to matching token if current token is T. */ | |
2288 | ||
2289 | #define SKIP_MATCHING_IF(T) \ | |
2290 | if (LOOKING_AT (T)) skip_matching (); else ((void) 0) | |
2291 | ||
2292 | ||
2293 | /* Skip forward until a given token TOKEN or YYEOF is seen and return | |
2294 | the current lookahead token after skipping. */ | |
2295 | ||
2296 | int | |
2297 | skip_to (token) | |
2298 | int token; | |
2299 | { | |
2300 | while (!LOOKING_AT2 (YYEOF, token)) | |
2301 | MATCH (); | |
2302 | return tk; | |
2303 | } | |
2304 | ||
2305 | ||
2306 | /* Skip over pairs of tokens (parentheses, square brackets, | |
2307 | angle brackets, curly brackets) matching the current lookahead. */ | |
2308 | ||
2309 | void | |
2310 | skip_matching () | |
2311 | { | |
2312 | int open, close, n; | |
2313 | ||
2314 | switch (open = LA1) | |
2315 | { | |
2316 | case '{': | |
2317 | close = '}'; | |
2318 | break; | |
2319 | ||
2320 | case '(': | |
2321 | close = ')'; | |
2322 | break; | |
2323 | ||
2324 | case '<': | |
2325 | close = '>'; | |
2326 | break; | |
2327 | ||
2328 | case '[': | |
2329 | close = ']'; | |
2330 | break; | |
2331 | ||
2332 | default: | |
2333 | abort (); | |
2334 | } | |
2335 | ||
2336 | for (n = 0;;) | |
2337 | { | |
2338 | if (LOOKING_AT (open)) | |
2339 | ++n; | |
2340 | else if (LOOKING_AT (close)) | |
2341 | --n; | |
2342 | else if (LOOKING_AT (YYEOF)) | |
2343 | break; | |
2344 | ||
2345 | MATCH (); | |
2346 | ||
2347 | if (n == 0) | |
2348 | break; | |
2349 | } | |
2350 | } | |
2351 | ||
2352 | ||
2353 | /* Re-initialize the parser by resetting the lookahead token. */ | |
2354 | ||
2355 | void | |
2356 | re_init_parser () | |
2357 | { | |
2358 | tk = -1; | |
2359 | } | |
2360 | ||
2361 | ||
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. */ | |
2367 | ||
2368 | unsigned | |
2369 | parm_list (flags) | |
2370 | int *flags; | |
2371 | { | |
2372 | unsigned hash = 0; | |
2373 | int type_seen = 0; | |
2374 | ||
2375 | while (!LOOKING_AT2 (YYEOF, ')')) | |
2376 | { | |
2377 | switch (LA1) | |
2378 | { | |
2379 | /* Skip over grouping parens or parameter lists in parameter | |
2380 | declarations. */ | |
2381 | case '(': | |
2382 | skip_matching (); | |
2383 | break; | |
2384 | ||
2385 | /* Next parameter. */ | |
2386 | case ',': | |
2387 | MATCH (); | |
2388 | type_seen = 0; | |
2389 | break; | |
2390 | ||
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) | |
2398 | { | |
2faf048a | 2399 | char *last_id; |
be0dbdab GM |
2400 | unsigned ident_type_hash = 0; |
2401 | ||
2faf048a GM |
2402 | parse_qualified_param_ident_or_type (&last_id); |
2403 | if (last_id) | |
2404 | { | |
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; | |
2410 | } | |
be0dbdab GM |
2411 | } |
2412 | else | |
2413 | MATCH (); | |
2414 | break; | |
2415 | ||
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; | |
2424 | ||
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; | |
2434 | ||
2435 | case '*': case '&': case '[': case ']': | |
2436 | hash = (hash << 1) ^ LA1; | |
2437 | MATCH (); | |
2438 | break; | |
2439 | ||
2440 | default: | |
2441 | MATCH (); | |
2442 | break; | |
2443 | } | |
2444 | } | |
2445 | ||
2446 | if (LOOKING_AT (')')) | |
2447 | { | |
2448 | MATCH (); | |
2449 | ||
2450 | if (LOOKING_AT (CONST)) | |
2451 | { | |
2452 | /* We can overload the same function on `const' */ | |
2453 | hash = (hash << 1) ^ CONST; | |
2454 | SET_FLAG (*flags, F_CONST); | |
2455 | MATCH (); | |
2456 | } | |
2457 | ||
2458 | if (LOOKING_AT (THROW)) | |
2459 | { | |
2460 | MATCH (); | |
2461 | SKIP_MATCHING_IF ('('); | |
2462 | SET_FLAG (*flags, F_THROW); | |
2463 | } | |
2464 | ||
2465 | if (LOOKING_AT ('=')) | |
2466 | { | |
2467 | MATCH (); | |
2468 | if (LOOKING_AT (CINT) && yyival == 0) | |
2469 | { | |
2470 | MATCH (); | |
2471 | SET_FLAG (*flags, F_PURE); | |
2472 | } | |
2473 | } | |
2474 | } | |
2475 | ||
2476 | return hash; | |
2477 | } | |
2478 | ||
2479 | ||
2480 | /* Print position info to stdout. */ | |
2481 | ||
2482 | void | |
2483 | print_info () | |
2484 | { | |
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); | |
2490 | } | |
2491 | ||
2492 | ||
2493 | /* Parse a member declaration within the class body of CLS. VIS is | |
2494 | the access specifier for the member (private, protected, | |
2495 | public). */ | |
2496 | ||
2497 | void | |
2498 | member (cls, vis) | |
2499 | struct sym *cls; | |
2500 | int vis; | |
2501 | { | |
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; | |
2514 | ||
2515 | while (!LOOKING_AT4 (';', '{', '}', YYEOF)) | |
2516 | { | |
2517 | switch (LA1) | |
2518 | { | |
2519 | default: | |
2520 | MATCH (); | |
2521 | break; | |
2522 | ||
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; | |
2530 | ||
2531 | case EXPLICIT: | |
2532 | SET_FLAG (flags, F_EXPLICIT); | |
2533 | goto typeseen; | |
2534 | ||
2535 | case MUTABLE: | |
2536 | SET_FLAG (flags, F_MUTABLE); | |
2537 | goto typeseen; | |
2538 | ||
2539 | case T_INLINE: | |
2540 | SET_FLAG (flags, F_INLINE); | |
2541 | goto typeseen; | |
2542 | ||
2543 | case VIRTUAL: | |
2544 | SET_FLAG (flags, F_VIRTUAL); | |
2545 | goto typeseen; | |
2546 | ||
2547 | case '[': | |
2548 | skip_matching (); | |
2549 | break; | |
2550 | ||
2551 | case ENUM: | |
2552 | sc = SC_TYPE; | |
2553 | goto typeseen; | |
2554 | ||
2555 | case TYPEDEF: | |
2556 | sc = SC_TYPE; | |
2557 | goto typeseen; | |
2558 | ||
2559 | case FRIEND: | |
2560 | sc = SC_FRIEND; | |
2561 | goto typeseen; | |
2562 | ||
2563 | case STATIC: | |
2564 | sc = SC_STATIC; | |
2565 | goto typeseen; | |
2566 | ||
2567 | case '~': | |
2568 | tilde = 1; | |
2569 | MATCH (); | |
2570 | break; | |
2571 | ||
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) | |
2577 | { | |
2578 | *id = '~'; | |
2579 | strcpy (id + 1, yytext); | |
2580 | } | |
2581 | else | |
2582 | strcpy (id, yytext); | |
2583 | MATCH (); | |
2584 | break; | |
2585 | ||
2586 | case OPERATOR: | |
2587 | id = operator_name (&sc); | |
2588 | break; | |
2589 | ||
2590 | case '(': | |
2591 | /* Most probably the beginning of a parameter list. */ | |
2592 | MATCH (); | |
2593 | paren_seen = 1; | |
2594 | ||
2595 | if (id && cls) | |
2596 | { | |
2597 | if (!(is_constructor = streq (id, cls->name))) | |
2598 | regexp = matching_regexp (); | |
2599 | } | |
2600 | else | |
2601 | is_constructor = 0; | |
2602 | ||
2603 | pos = BUFFER_POS (); | |
2604 | hash = parm_list (&flags); | |
2605 | ||
2606 | if (is_constructor) | |
2607 | regexp = matching_regexp (); | |
2608 | ||
2609 | if (id && cls != NULL) | |
2610 | add_member_decl (cls, id, regexp, pos, hash, 0, sc, vis, flags); | |
2611 | ||
2612 | while (!LOOKING_AT3 (';', '{', YYEOF)) | |
2613 | MATCH (); | |
2614 | ||
2615 | if (LOOKING_AT ('{') && id && cls) | |
2616 | add_member_defn (cls, id, regexp, pos, hash, 0, sc, flags); | |
2617 | ||
2618 | id = NULL; | |
2619 | sc = SC_MEMBER; | |
2620 | break; | |
2621 | ||
2622 | case STRUCT: case UNION: case CLASS: | |
2623 | /* Nested class */ | |
2624 | class_tag = LA1; | |
2625 | type_seen = 1; | |
2626 | MATCH (); | |
2627 | anonymous = 1; | |
2628 | ||
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, ';', ':', '{')) | |
2633 | { | |
2634 | if (LOOKING_AT (IDENT)) | |
2635 | anonymous = 0; | |
2636 | MATCH (); | |
2637 | } | |
2638 | ||
2639 | if (LOOKING_AT2 (':', '{')) | |
2640 | class_definition (anonymous ? NULL : cls, class_tag, flags, 1); | |
2641 | else | |
2642 | skip_to (';'); | |
2643 | break; | |
2644 | ||
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; | |
2653 | } | |
2654 | } | |
2655 | ||
2656 | if (LOOKING_AT (';')) | |
2657 | { | |
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) | |
2661 | { | |
2662 | regexp = matching_regexp (); | |
2663 | pos = BUFFER_POS (); | |
2664 | ||
2665 | if (cls != NULL) | |
2666 | { | |
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); | |
2671 | } | |
2672 | } | |
2673 | ||
2674 | MATCH (); | |
2675 | print_info (); | |
2676 | } | |
2677 | else if (LOOKING_AT ('{')) | |
2678 | { | |
2679 | /* A named enum. */ | |
2680 | if (sc == SC_TYPE && id && cls) | |
2681 | { | |
2682 | regexp = matching_regexp (); | |
2683 | pos = BUFFER_POS (); | |
2684 | ||
2685 | if (cls != NULL) | |
2686 | { | |
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); | |
2689 | } | |
2690 | } | |
2691 | ||
2692 | skip_matching (); | |
2693 | print_info (); | |
2694 | } | |
2695 | } | |
2696 | ||
2697 | ||
2698 | /* Parse the body of class CLS. TAG is the tag of the class (struct, | |
2699 | union, class). */ | |
2700 | ||
2701 | void | |
2702 | class_body (cls, tag) | |
2703 | struct sym *cls; | |
2704 | int tag; | |
2705 | { | |
2706 | int vis = tag == CLASS ? PRIVATE : PUBLIC; | |
2707 | int temp; | |
2708 | ||
2709 | while (!LOOKING_AT2 (YYEOF, '}')) | |
2710 | { | |
2711 | switch (LA1) | |
2712 | { | |
2713 | case PRIVATE: case PROTECTED: case PUBLIC: | |
2714 | temp = LA1; | |
2715 | MATCH (); | |
2716 | ||
2717 | if (LOOKING_AT (':')) | |
2718 | { | |
2719 | vis = temp; | |
2720 | MATCH (); | |
2721 | } | |
2722 | else | |
2723 | { | |
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. */ | |
2727 | do | |
2728 | { | |
2729 | MATCH (); | |
2730 | } | |
2731 | while (LOOKING_AT2 (IDENT, ',') | |
2732 | || LOOKING_AT3 (PUBLIC, PROTECTED, PRIVATE)); | |
2733 | } | |
2734 | break; | |
2735 | ||
2736 | case TYPENAME: | |
2737 | case USING: | |
2738 | skip_to (';'); | |
2739 | break; | |
2740 | ||
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; | |
2751 | ||
2752 | default: | |
2753 | MATCH (); | |
2754 | break; | |
2755 | } | |
2756 | } | |
2757 | } | |
2758 | ||
2759 | ||
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. */ | |
2763 | ||
2764 | struct sym * | |
2765 | parse_classname () | |
2766 | { | |
2767 | struct sym *last_class = NULL; | |
2768 | ||
2769 | while (LOOKING_AT (IDENT)) | |
2770 | { | |
2771 | last_class = add_sym (yytext, last_class); | |
2772 | MATCH (); | |
2773 | ||
2774 | if (LOOKING_AT ('<')) | |
2775 | { | |
2776 | skip_matching (); | |
2777 | SET_FLAG (last_class->flags, F_TEMPLATE); | |
2778 | } | |
2779 | ||
2780 | if (!LOOKING_AT (DCOLON)) | |
2781 | break; | |
2782 | ||
2783 | MATCH (); | |
2784 | } | |
2785 | ||
2786 | return last_class; | |
2787 | } | |
2788 | ||
2789 | ||
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. */ | |
2793 | ||
2794 | char * | |
2795 | operator_name (sc) | |
2796 | int *sc; | |
2797 | { | |
2798 | static int id_size = 0; | |
2799 | static char *id = NULL; | |
2800 | char *s; | |
2801 | int len; | |
2802 | ||
2803 | MATCH (); | |
2804 | ||
2805 | if (LOOKING_AT2 (NEW, DELETE)) | |
2806 | { | |
2807 | /* `new' and `delete' are implicitly static. */ | |
2808 | if (*sc != SC_FRIEND) | |
2809 | *sc = SC_STATIC; | |
2810 | ||
2811 | s = token_string (LA1); | |
2812 | MATCH (); | |
2813 | ||
2814 | len = strlen (s) + 10; | |
2815 | if (len > id_size) | |
2816 | { | |
2817 | int new_size = max (len, 2 * id_size); | |
c43a1ff6 | 2818 | id = (char *) xrealloc (id, new_size); |
be0dbdab GM |
2819 | id_size = new_size; |
2820 | } | |
2821 | strcpy (id, s); | |
2822 | ||
2823 | /* Vector new or delete? */ | |
2824 | if (LOOKING_AT ('[')) | |
2825 | { | |
2826 | strcat (id, "["); | |
2827 | MATCH (); | |
2828 | ||
2829 | if (LOOKING_AT (']')) | |
2830 | { | |
2831 | strcat (id, "]"); | |
2832 | MATCH (); | |
2833 | } | |
2834 | } | |
2835 | } | |
2836 | else | |
2837 | { | |
2838 | int tokens_matched = 0; | |
2839 | ||
2840 | len = 20; | |
2841 | if (len > id_size) | |
2842 | { | |
2843 | int new_size = max (len, 2 * id_size); | |
c43a1ff6 | 2844 | id = (char *) xrealloc (id, new_size); |
be0dbdab GM |
2845 | id_size = new_size; |
2846 | } | |
2847 | strcpy (id, "operator"); | |
2848 | ||
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)) | |
2854 | { | |
2855 | s = token_string (LA1); | |
2856 | len += strlen (s) + 2; | |
2857 | if (len > id_size) | |
2858 | { | |
2859 | int new_size = max (len, 2 * id_size); | |
c43a1ff6 | 2860 | id = (char *) xrealloc (id, new_size); |
be0dbdab GM |
2861 | id_size = new_size; |
2862 | } | |
2863 | ||
2864 | if (*s != ')' && *s != ']') | |
2865 | strcat (id, " "); | |
2866 | strcat (id, s); | |
2867 | MATCH (); | |
2868 | ||
2869 | /* If this is a simple operator like `+', stop now. */ | |
2870 | if (!isalpha (*s) && *s != '(' && *s != '[') | |
2871 | break; | |
2872 | ||
2873 | ++tokens_matched; | |
2874 | } | |
2875 | } | |
2876 | ||
2877 | return id; | |
2878 | } | |
2879 | ||
2880 | ||
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. */ | |
2884 | ||
2885 | struct sym * | |
2886 | parse_qualified_ident_or_type (last_id) | |
2887 | char **last_id; | |
2888 | { | |
2889 | struct sym *cls = NULL; | |
2890 | static char *id = NULL; | |
2891 | static int id_size = 0; | |
2892 | ||
2893 | while (LOOKING_AT (IDENT)) | |
2894 | { | |
2895 | int len = strlen (yytext) + 1; | |
2896 | if (len > id_size) | |
2897 | { | |
c43a1ff6 | 2898 | id = (char *) xrealloc (id, len); |
be0dbdab GM |
2899 | id_size = len; |
2900 | } | |
2901 | strcpy (id, yytext); | |
2902 | *last_id = id; | |
2903 | MATCH (); | |
2904 | ||
2905 | SKIP_MATCHING_IF ('<'); | |
2906 | ||
2907 | if (LOOKING_AT (DCOLON)) | |
2908 | { | |
2909 | cls = add_sym (id, cls); | |
2910 | *last_id = NULL; | |
2911 | MATCH (); | |
2912 | } | |
2913 | else | |
2914 | break; | |
2915 | } | |
2916 | ||
2917 | return cls; | |
2918 | } | |
2919 | ||
2920 | ||
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. */ | |
2924 | ||
2925 | void | |
2926 | parse_qualified_param_ident_or_type (last_id) | |
2927 | char **last_id; | |
2928 | { | |
2929 | struct sym *cls = NULL; | |
2930 | static char *id = NULL; | |
2931 | static int id_size = 0; | |
2faf048a | 2932 | |
be0dbdab GM |
2933 | while (LOOKING_AT (IDENT)) |
2934 | { | |
2935 | int len = strlen (yytext) + 1; | |
2936 | if (len > id_size) | |
2937 | { | |
c43a1ff6 | 2938 | id = (char *) xrealloc (id, len); |
be0dbdab GM |
2939 | id_size = len; |
2940 | } | |
2941 | strcpy (id, yytext); | |
2942 | *last_id = id; | |
2943 | MATCH (); | |
2944 | ||
2945 | SKIP_MATCHING_IF ('<'); | |
2946 | ||
2947 | if (LOOKING_AT (DCOLON)) | |
2948 | { | |
2949 | cls = add_sym (id, cls); | |
2950 | *last_id = NULL; | |
2951 | MATCH (); | |
2952 | } | |
2953 | else | |
2954 | break; | |
2955 | } | |
2956 | } | |
2957 | ||
2958 | ||
2959 | /* Parse a class definition. | |
2960 | ||
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. | |
2965 | ||
2966 | Current lookahead is the class name. */ | |
2967 | ||
2968 | void | |
2969 | class_definition (containing, tag, flags, nested) | |
2970 | struct sym *containing; | |
2971 | int tag; | |
2972 | int flags; | |
2973 | int nested; | |
2974 | { | |
be0dbdab GM |
2975 | struct sym *current; |
2976 | struct sym *base_class; | |
2977 | ||
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 | |
2984 | { | |
2985 | current = add_sym (yytext, containing); | |
2986 | current->pos = BUFFER_POS (); | |
2987 | current->regexp = matching_regexp (); | |
2988 | current->filename = filename; | |
2989 | current->flags = flags; | |
2990 | } | |
2991 | ||
2992 | /* If at ':', base class list follows. */ | |
2993 | if (LOOKING_AT (':')) | |
2994 | { | |
2995 | int done = 0; | |
2996 | MATCH (); | |
2997 | ||
2998 | while (!done) | |
2999 | { | |
8bef35f2 | 3000 | switch (LA1) |
be0dbdab GM |
3001 | { |
3002 | case VIRTUAL: case PUBLIC: case PROTECTED: case PRIVATE: | |
3003 | MATCH (); | |
3004 | break; | |
3005 | ||
3006 | case IDENT: | |
3007 | base_class = parse_classname (); | |
3008 | if (base_class && current && base_class != current) | |
3009 | add_link (base_class, current); | |
3010 | break; | |
3011 | ||
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. | |
3016 | ||
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; | |
3022 | ||
3023 | default: | |
3024 | /* A syntax error, possibly due to preprocessor constructs | |
3025 | like | |
3026 | ||
3027 | #ifdef SOMETHING | |
3028 | class A : public B | |
3029 | #else | |
3030 | class A : private B. | |
3031 | ||
3032 | MATCH until we see something like `;' or `{'. */ | |
3033 | while (!LOOKING_AT3 (';', YYEOF, '{')) | |
3034 | MATCH (); | |
3035 | done = 1; | |
3036 | ||
3037 | case '{': | |
3038 | done = 1; | |
3039 | break; | |
3040 | } | |
3041 | } | |
3042 | } | |
3043 | ||
3044 | /* Parse the class body if there is one. */ | |
3045 | if (LOOKING_AT ('{')) | |
3046 | { | |
3047 | if (tag != CLASS && !f_structs) | |
3048 | skip_matching (); | |
3049 | else | |
3050 | { | |
3051 | MATCH (); | |
3052 | class_body (current, tag); | |
3053 | ||
3054 | if (LOOKING_AT ('}')) | |
3055 | { | |
3056 | MATCH (); | |
3057 | if (LOOKING_AT (';') && !nested) | |
3058 | MATCH (); | |
3059 | } | |
3060 | } | |
3061 | } | |
3062 | } | |
3063 | ||
3064 | ||
3065 | /* Parse a declaration. */ | |
3066 | ||
3067 | void | |
8bef35f2 | 3068 | declaration (flags) |
be0dbdab GM |
3069 | int flags; |
3070 | { | |
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; | |
3078 | ||
3079 | while (!LOOKING_AT3 (';', '{', YYEOF)) | |
3080 | { | |
3081 | switch (LA1) | |
3082 | { | |
3083 | default: | |
3084 | MATCH (); | |
3085 | break; | |
3086 | ||
3087 | case '[': | |
3088 | skip_matching (); | |
3089 | break; | |
3090 | ||
3091 | case ENUM: | |
3092 | case TYPEDEF: | |
3093 | sc = SC_TYPE; | |
3094 | MATCH (); | |
3095 | break; | |
3096 | ||
3097 | case STATIC: | |
3098 | sc = SC_STATIC; | |
3099 | MATCH (); | |
3100 | break; | |
3101 | ||
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; | |
3107 | ||
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; | |
3113 | ||
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; | |
3119 | ||
3120 | case OPERATOR: | |
3121 | id = operator_name (&sc); | |
3122 | break; | |
3123 | ||
3124 | case T_INLINE: | |
3125 | SET_FLAG (flags, F_INLINE); | |
3126 | MATCH (); | |
3127 | break; | |
3128 | ||
3129 | case '~': | |
3130 | MATCH (); | |
3131 | if (LOOKING_AT (IDENT)) | |
3132 | { | |
3133 | id = (char *) alloca (strlen (yytext) + 2); | |
3134 | *id = '~'; | |
3135 | strcpy (id + 1, yytext); | |
3136 | MATCH (); | |
3137 | } | |
3138 | break; | |
3139 | ||
3140 | case IDENT: | |
3141 | cls = parse_qualified_ident_or_type (&id); | |
3142 | break; | |
3143 | ||
3144 | case '(': | |
3145 | /* Most probably the beginning of a parameter list. */ | |
3146 | if (cls) | |
3147 | { | |
3148 | MATCH (); | |
3149 | ||
3150 | if (id && cls) | |
3151 | { | |
3152 | if (!(is_constructor = streq (id, cls->name))) | |
3153 | regexp = matching_regexp (); | |
3154 | } | |
3155 | else | |
3156 | is_constructor = 0; | |
3157 | ||
3158 | pos = BUFFER_POS (); | |
3159 | hash = parm_list (&flags); | |
3160 | ||
3161 | if (is_constructor) | |
3162 | regexp = matching_regexp (); | |
3163 | ||
3164 | if (id && cls) | |
3165 | add_member_defn (cls, id, regexp, pos, hash, 0, | |
3166 | SC_UNKNOWN, flags); | |
3167 | } | |
3168 | else | |
3169 | { | |
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 (); | |
3174 | ||
3175 | if (id) | |
3176 | { | |
3177 | regexp = matching_regexp (); | |
3178 | pos = BUFFER_POS (); | |
3179 | hash = parm_list (&flags); | |
3180 | add_global_decl (id, regexp, pos, hash, 0, sc, flags); | |
3181 | } | |
3182 | ||
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; | |
3188 | } | |
3189 | ||
3190 | while (!LOOKING_AT3 (';', '{', YYEOF)) | |
3191 | MATCH (); | |
3192 | ||
3193 | if (!cls && id && LOOKING_AT ('{')) | |
3194 | add_global_defn (id, regexp, pos, hash, 0, sc, flags); | |
3195 | id = NULL; | |
3196 | break; | |
3197 | } | |
3198 | } | |
3199 | ||
3200 | if (LOOKING_AT (';')) | |
3201 | { | |
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) | |
3206 | { | |
3207 | char *regexp = matching_regexp (); | |
3208 | int pos = BUFFER_POS (); | |
3209 | ||
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); | |
3214 | } | |
3215 | ||
3216 | MATCH (); | |
3217 | print_info (); | |
3218 | } | |
3219 | else if (LOOKING_AT ('{')) | |
3220 | { | |
3221 | if (sc == SC_TYPE && id) | |
3222 | { | |
3223 | /* A named enumeration. */ | |
3224 | regexp = matching_regexp (); | |
3225 | pos = BUFFER_POS (); | |
3226 | add_global_defn (id, regexp, pos, 0, 1, sc, flags); | |
3227 | } | |
3228 | ||
3229 | skip_matching (); | |
3230 | print_info (); | |
3231 | } | |
3232 | } | |
3233 | ||
3234 | ||
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. */ | |
3239 | ||
3240 | int | |
3241 | globals (start_flags) | |
3242 | int start_flags; | |
3243 | { | |
3244 | int anonymous; | |
3245 | int class_tk; | |
3246 | int flags = start_flags; | |
3247 | ||
3248 | for (;;) | |
3249 | { | |
3250 | char *prev_in = in; | |
3251 | ||
3252 | switch (LA1) | |
3253 | { | |
3254 | case NAMESPACE: | |
3255 | { | |
3256 | MATCH (); | |
3257 | ||
3258 | if (LOOKING_AT (IDENT)) | |
3259 | { | |
3260 | char *namespace_name | |
3261 | = (char *) alloca (strlen (yytext) + 1); | |
3262 | strcpy (namespace_name, yytext); | |
3263 | MATCH (); | |
3264 | ||
3265 | if (LOOKING_AT ('=')) | |
3266 | { | |
3267 | if (skip_to (';') == ';') | |
3268 | MATCH (); | |
3269 | register_namespace_alias (namespace_name, yytext); | |
3270 | } | |
3271 | else if (LOOKING_AT ('{')) | |
3272 | { | |
3273 | MATCH (); | |
3274 | enter_namespace (namespace_name); | |
3275 | globals (0); | |
3276 | leave_namespace (); | |
3277 | MATCH_IF ('}'); | |
3278 | } | |
3279 | } | |
3280 | } | |
3281 | break; | |
3282 | ||
3283 | case EXTERN: | |
3284 | MATCH (); | |
3285 | if (LOOKING_AT (CSTRING) && *string_start == 'C' | |
3286 | && *(string_start + 1) == '"') | |
3287 | { | |
3288 | /* This is `extern "C"'. */ | |
3289 | MATCH (); | |
3290 | ||
3291 | if (LOOKING_AT ('{')) | |
3292 | { | |
3293 | MATCH (); | |
3294 | globals (F_EXTERNC); | |
3295 | MATCH_IF ('}'); | |
3296 | } | |
3297 | else | |
3298 | SET_FLAG (flags, F_EXTERNC); | |
3299 | } | |
3300 | break; | |
3301 | ||
3302 | case TEMPLATE: | |
3303 | MATCH (); | |
3304 | SKIP_MATCHING_IF ('<'); | |
3305 | SET_FLAG (flags, F_TEMPLATE); | |
3306 | break; | |
3307 | ||
3308 | case CLASS: case STRUCT: case UNION: | |
3309 | class_tk = LA1; | |
3310 | MATCH (); | |
3311 | anonymous = 1; | |
3312 | ||
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, ';', ':', '{')) | |
3318 | { | |
3319 | if (LOOKING_AT (IDENT)) | |
3320 | anonymous = 0; | |
3321 | MATCH (); | |
3322 | } | |
3323 | ||
3324 | /* Don't add anonymous unions. */ | |
3325 | if (LOOKING_AT2 (':', '{') && !anonymous) | |
3326 | class_definition (NULL, class_tk, flags, 0); | |
3327 | else | |
3328 | { | |
3329 | if (skip_to (';') == ';') | |
3330 | MATCH (); | |
3331 | } | |
3332 | ||
3333 | flags = start_flags; | |
3334 | break; | |
3335 | ||
3336 | case YYEOF: | |
3337 | return 1; | |
3338 | ||
3339 | case '}': | |
3340 | return 0; | |
3341 | ||
3342 | default: | |
8bef35f2 | 3343 | declaration (flags); |
be0dbdab GM |
3344 | flags = start_flags; |
3345 | break; | |
3346 | } | |
3347 | ||
3348 | if (prev_in == in) | |
3349 | yyerror ("parse error"); | |
3350 | } | |
3351 | } | |
3352 | ||
3353 | ||
3354 | /* Parse the current input file. */ | |
3355 | ||
3356 | void | |
3357 | yyparse () | |
3358 | { | |
3359 | while (globals (0) == 0) | |
3360 | MATCH_IF ('}'); | |
3361 | } | |
3362 | ||
3363 | ||
3364 | \f | |
3365 | /*********************************************************************** | |
3366 | Main Program | |
3367 | ***********************************************************************/ | |
3368 | ||
3369 | /* Add the list of paths PATH_LIST to the current search path for | |
3370 | input files. */ | |
3371 | ||
3372 | void | |
3373 | add_search_path (path_list) | |
3374 | char *path_list; | |
3375 | { | |
3376 | while (*path_list) | |
3377 | { | |
3378 | char *start = path_list; | |
3379 | struct search_path *p; | |
3380 | ||
3381 | while (*path_list && *path_list != PATH_LIST_SEPARATOR) | |
3382 | ++path_list; | |
3383 | ||
c43a1ff6 GM |
3384 | p = (struct search_path *) xmalloc (sizeof *p); |
3385 | p->path = (char *) xmalloc (path_list - start + 1); | |
be0dbdab GM |
3386 | memcpy (p->path, start, path_list - start); |
3387 | p->path[path_list - start] = '\0'; | |
3388 | p->next = NULL; | |
3389 | ||
3390 | if (search_path_tail) | |
3391 | { | |
3392 | search_path_tail->next = p; | |
3393 | search_path_tail = p; | |
3394 | } | |
3395 | else | |
3396 | search_path = search_path_tail = p; | |
3397 | ||
3398 | while (*path_list == PATH_LIST_SEPARATOR) | |
3399 | ++path_list; | |
3400 | } | |
3401 | } | |
3402 | ||
3403 | ||
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. */ | |
3407 | ||
3408 | FILE * | |
3409 | open_file (file) | |
3410 | char *file; | |
3411 | { | |
3412 | FILE *fp = NULL; | |
3413 | static char *buffer; | |
3414 | static int buffer_size; | |
3415 | struct search_path *path; | |
fd72561d | 3416 | int flen = strlen (file) + 1; /* +1 for the slash */ |
be0dbdab GM |
3417 | |
3418 | filename = xstrdup (file); | |
3419 | ||
3420 | for (path = search_path; path && fp == NULL; path = path->next) | |
3421 | { | |
fd72561d | 3422 | int len = strlen (path->path) + flen; |
be0dbdab GM |
3423 | |
3424 | if (len + 1 >= buffer_size) | |
3425 | { | |
3426 | buffer_size = max (len + 1, 2 * buffer_size); | |
c43a1ff6 | 3427 | buffer = (char *) xrealloc (buffer, buffer_size); |
be0dbdab GM |
3428 | } |
3429 | ||
3430 | strcpy (buffer, path->path); | |
3431 | strcat (buffer, "/"); | |
3432 | strcat (buffer, file); | |
3433 | fp = fopen (buffer, "r"); | |
3434 | } | |
3435 | ||
3436 | /* Try the original file name. */ | |
3437 | if (fp == NULL) | |
3438 | fp = fopen (file, "r"); | |
3439 | ||
3440 | if (fp == NULL) | |
3441 | yyerror ("cannot open"); | |
3442 | ||
3443 | return fp; | |
3444 | } | |
3445 | ||
3446 | ||
3447 | /* Display usage information and exit program. */ | |
3448 | ||
3449 | #define USAGE "\ | |
3450 | Usage: ebrowse [options] {files}\n\ | |
3451 | \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\ | |
3466 | " | |
3467 | ||
3468 | void | |
3469 | usage (error) | |
3470 | int error; | |
3471 | { | |
3472 | puts (USAGE); | |
3473 | exit (error ? 1 : 0); | |
3474 | } | |
3475 | ||
3476 | ||
3477 | /* Display version and copyright info. The VERSION macro is set | |
3478 | from the Makefile and contains the Emacs version. */ | |
3479 | ||
2fe9a71c AI |
3480 | #ifndef VERSION |
3481 | # define VERSION "21" | |
3482 | #endif | |
3483 | ||
be0dbdab GM |
3484 | void |
3485 | version () | |
3486 | { | |
3487 | printf ("ebrowse %s\n", VERSION); | |
3488 | puts ("Copyright (C) 1992-1999, 2000 Free Software Foundation, Inc."); | |
3489 | puts ("This program is distributed under the same terms as Emacs."); | |
3490 | exit (0); | |
3491 | } | |
3492 | ||
3493 | ||
3494 | /* Parse one input file FILE, adding classes and members to the symbol | |
3495 | table. */ | |
3496 | ||
3497 | void | |
3498 | process_file (file) | |
3499 | char *file; | |
3500 | { | |
3501 | FILE *fp; | |
3502 | ||
3503 | fp = open_file (file); | |
3504 | if (fp) | |
3505 | { | |
3506 | int nread, nbytes; | |
3507 | ||
3508 | /* Give a progress indication if needed. */ | |
3509 | if (f_very_verbose) | |
3510 | { | |
3511 | puts (filename); | |
3512 | fflush (stdout); | |
3513 | } | |
3514 | else if (f_verbose) | |
3515 | { | |
3516 | putchar ('.'); | |
3517 | fflush (stdout); | |
3518 | } | |
3519 | ||
3520 | /* Read file to inbuffer. */ | |
3521 | for (nread = 0;;) | |
3522 | { | |
3523 | if (nread + READ_CHUNK_SIZE >= inbuffer_size) | |
3524 | { | |
3525 | inbuffer_size = nread + READ_CHUNK_SIZE + 1; | |
c43a1ff6 | 3526 | inbuffer = (char *) xrealloc (inbuffer, inbuffer_size); |
be0dbdab GM |
3527 | } |
3528 | ||
3529 | nbytes = fread (inbuffer + nread, 1, READ_CHUNK_SIZE, fp); | |
fd72561d | 3530 | if (nbytes <= 0) |
be0dbdab | 3531 | break; |
fd72561d | 3532 | nread += nbytes; |
be0dbdab | 3533 | } |
fd72561d EZ |
3534 | if (nread < 0) |
3535 | nread = 0; | |
be0dbdab GM |
3536 | inbuffer[nread] = '\0'; |
3537 | ||
3538 | /* Reinitialize scanner and parser for the new input file. */ | |
3539 | re_init_scanner (); | |
3540 | re_init_parser (); | |
3541 | ||
3542 | /* Parse it and close the file. */ | |
3543 | yyparse (); | |
3544 | fclose (fp); | |
3545 | } | |
3546 | } | |
3547 | ||
3548 | ||
3549 | /* Read a line from stream FP and return a pointer to a static buffer | |
3550 | containing its contents without the terminating newline. Value | |
3551 | is null when EOF is reached. */ | |
3552 | ||
3553 | char * | |
3554 | read_line (fp) | |
3555 | FILE *fp; | |
3556 | { | |
3557 | static char *buffer; | |
3558 | static int buffer_size; | |
3559 | int i = 0, c; | |
3560 | ||
3561 | while ((c = getc (fp)) != EOF && c != '\n') | |
3562 | { | |
3563 | if (i >= buffer_size) | |
3564 | { | |
3565 | buffer_size = max (100, buffer_size * 2); | |
c43a1ff6 | 3566 | buffer = (char *) xrealloc (buffer, buffer_size); |
be0dbdab GM |
3567 | } |
3568 | ||
3569 | buffer[i++] = c; | |
3570 | } | |
3571 | ||
3572 | if (c == EOF && i == 0) | |
3573 | return NULL; | |
3574 | ||
3575 | if (i == buffer_size) | |
3576 | { | |
3577 | buffer_size = max (100, buffer_size * 2); | |
c43a1ff6 | 3578 | buffer = (char *) xrealloc (buffer, buffer_size); |
be0dbdab GM |
3579 | } |
3580 | ||
3581 | buffer[i] = '\0'; | |
3582 | return buffer; | |
3583 | } | |
3584 | ||
3585 | ||
3586 | /* Main entry point. */ | |
3587 | ||
3588 | int | |
3589 | main (argc, argv) | |
3590 | int argc; | |
3591 | char **argv; | |
3592 | { | |
3593 | int i; | |
3594 | int any_inputfiles = 0; | |
3595 | static char *out_filename = DEFAULT_OUTFILE; | |
3596 | static char **input_filenames = NULL; | |
3597 | static int input_filenames_size = 0; | |
3598 | static int n_input_files; | |
3599 | ||
3600 | filename = "command line"; | |
3601 | yyout = stdout; | |
3602 | ||
3603 | while ((i = getopt_long (argc, argv, "af:I:m:M:no:p:svVx", | |
3604 | options, NULL)) != EOF) | |
3605 | { | |
3606 | switch (i) | |
3607 | { | |
3608 | /* Experimental. */ | |
3609 | case 'p': | |
3610 | info_position = atoi (optarg); | |
3611 | break; | |
3612 | ||
3613 | case 'n': | |
3614 | f_nested_classes = 0; | |
3615 | break; | |
3616 | ||
3617 | case 'x': | |
3618 | f_regexps = 0; | |
3619 | break; | |
3620 | ||
3621 | /* Add the name of a file containing more input files. */ | |
3622 | case 'f': | |
3623 | if (n_input_files == input_filenames_size) | |
3624 | { | |
3625 | input_filenames_size = max (10, 2 * input_filenames_size); | |
c43a1ff6 | 3626 | input_filenames = (char **) xrealloc (input_filenames, |
be0dbdab GM |
3627 | input_filenames_size); |
3628 | } | |
3629 | input_filenames[n_input_files++] = xstrdup (optarg); | |
3630 | break; | |
3631 | ||
3632 | /* Append new output to output file instead of truncating it. */ | |
3633 | case 'a': | |
3634 | f_append = 1; | |
3635 | break; | |
3636 | ||
3637 | /* Include structs in the output */ | |
3638 | case 's': | |
3639 | f_structs = 0; | |
3640 | break; | |
3641 | ||
3642 | /* Be verbose (give a progress indication). */ | |
3643 | case 'v': | |
3644 | f_verbose = 1; | |
3645 | break; | |
3646 | ||
3647 | /* Be very verbose (print file names as they are processed). */ | |
3648 | case 'V': | |
3649 | f_verbose = 1; | |
3650 | f_very_verbose = 1; | |
3651 | break; | |
3652 | ||
3653 | /* Change the name of the output file. */ | |
3654 | case 'o': | |
3655 | out_filename = optarg; | |
3656 | break; | |
3657 | ||
3658 | /* Set minimum length for regular expression strings | |
3659 | when recorded in the output file. */ | |
3660 | case 'm': | |
3661 | min_regexp = atoi (optarg); | |
3662 | break; | |
3663 | ||
3664 | /* Set maximum length for regular expression strings | |
3665 | when recorded in the output file. */ | |
3666 | case 'M': | |
3667 | max_regexp = atoi (optarg); | |
3668 | break; | |
3669 | ||
3670 | /* Add to search path. */ | |
3671 | case 'I': | |
3672 | add_search_path (optarg); | |
3673 | break; | |
3674 | ||
3675 | /* Display help */ | |
3676 | case -2: | |
3677 | usage (0); | |
3678 | break; | |
3679 | ||
3680 | case -3: | |
3681 | version (); | |
3682 | break; | |
3683 | } | |
3684 | } | |
3685 | ||
3686 | /* Call init_scanner after command line flags have been processed to be | |
3687 | able to add keywords depending on command line (not yet | |
3688 | implemented). */ | |
3689 | init_scanner (); | |
3690 | init_sym (); | |
3691 | ||
3692 | /* Open output file */ | |
3693 | if (*out_filename) | |
3694 | { | |
3695 | yyout = fopen (out_filename, f_append ? "a" : "w"); | |
3696 | if (yyout == NULL) | |
3697 | { | |
3698 | yyerror ("cannot open output file `%s'", out_filename); | |
3699 | exit (1); | |
3700 | } | |
3701 | } | |
3702 | ||
3703 | /* Process input files specified on the command line. */ | |
3704 | while (optind < argc) | |
3705 | { | |
3706 | process_file (argv[optind++]); | |
3707 | any_inputfiles = 1; | |
3708 | } | |
3709 | ||
3710 | /* Process files given on stdin if no files specified. */ | |
3711 | if (!any_inputfiles && n_input_files == 0) | |
3712 | { | |
3713 | char *file; | |
3714 | while ((file = read_line (stdin)) != NULL) | |
3715 | process_file (file); | |
3716 | } | |
3717 | else | |
3718 | { | |
3719 | /* Process files from `--files=FILE'. Every line in FILE names | |
3720 | one input file to process. */ | |
3721 | for (i = 0; i < n_input_files; ++i) | |
3722 | { | |
3723 | FILE *fp = fopen (input_filenames[i], "r"); | |
3724 | ||
3725 | if (fp == NULL) | |
3726 | yyerror ("cannot open input file `%s'", input_filenames[i]); | |
3727 | else | |
3728 | { | |
3729 | char *file; | |
3730 | while ((file = read_line (fp)) != NULL) | |
3731 | process_file (file); | |
3732 | fclose (fp); | |
3733 | } | |
3734 | } | |
3735 | } | |
3736 | ||
3737 | /* Write output file. */ | |
3738 | dump_roots (yyout); | |
3739 | ||
3740 | /* Close output file. */ | |
3741 | if (yyout != stdout) | |
3742 | fclose (yyout); | |
3743 | ||
3744 | return 0; | |
3745 | } | |
3746 | ||
3747 | ||
3748 | /* ebrowse.c ends here. */ |