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