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