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