ebrowse.c 93.1 KB
Newer Older
Gerd Moellmann's avatar
Gerd Moellmann committed
1 2
/* ebrowse.c --- parsing files for the ebrowse C++ browser

3
Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001,
Glenn Morris's avatar
Glenn Morris committed
4
              2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
              Free Software Foundation, Inc.

This file is part of GNU Emacs.

GNU Emacs is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

GNU Emacs is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.  */

Gerd Moellmann's avatar
Gerd Moellmann committed
22

23 24 25 26
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

Gerd Moellmann's avatar
Gerd Moellmann committed
27
#include <stdio.h>
28 29

#ifdef HAVE_STDLIB_H
Gerd Moellmann's avatar
Gerd Moellmann committed
30
#include <stdlib.h>
31 32 33
#endif

#ifdef HAVE_STRING_H
Gerd Moellmann's avatar
Gerd Moellmann committed
34
#include <string.h>
35 36
#endif

Gerd Moellmann's avatar
Gerd Moellmann committed
37 38 39 40
#include <ctype.h>
#include <assert.h>
#include "getopt.h"

41 42 43 44 45
/* The SunOS compiler doesn't have SEEK_END.  */
#ifndef SEEK_END
#define SEEK_END 2
#endif

Gerd Moellmann's avatar
Gerd Moellmann committed
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
/* Conditionalize function prototypes.  */

#ifdef PROTOTYPES		/* From config.h.  */
#define P_(x) x
#else
#define P_(x) ()
#endif

/* Value is non-zero if strings X and Y compare equal.  */

#define streq(X, Y) (*(X) == *(Y) && strcmp ((X) + 1, (Y) + 1) == 0)

/* The ubiquitous `max' and `min' macros.  */

#ifndef max
#define max(X, Y)	((X) > (Y) ? (X) : (Y))
#define min(X, Y)	((X) < (Y) ? (X) : (Y))
#endif

/* Files are read in chunks of this number of bytes.  */

#define READ_CHUNK_SIZE (100 * 1024)

/* The character used as a separator in path lists (like $PATH).  */

71
#if defined(__MSDOS__)
72 73 74
#define PATH_LIST_SEPARATOR ';'
#define FILENAME_EQ(X,Y)    (strcasecmp(X,Y) == 0)
#else
75 76 77 78
#if defined(WINDOWSNT)
#define PATH_LIST_SEPARATOR ';'
#define FILENAME_EQ(X,Y)    (stricmp(X,Y) == 0)
#else
Gerd Moellmann's avatar
Gerd Moellmann committed
79
#define PATH_LIST_SEPARATOR ':'
80 81
#define FILENAME_EQ(X,Y)    (streq(X,Y))
#endif
82
#endif
Gerd Moellmann's avatar
Gerd Moellmann committed
83 84
/* The default output file name.  */

85
#define DEFAULT_OUTFILE "BROWSE"
Gerd Moellmann's avatar
Gerd Moellmann committed
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285

/* A version string written to the output file.  Change this whenever
   the structure of the output file changes.  */

#define EBROWSE_FILE_VERSION "ebrowse 5.0"

/* The output file consists of a tree of Lisp objects, with major
   nodes built out of Lisp structures.  These are the heads of the
   Lisp structs with symbols identifying their type.  */

#define TREE_HEADER_STRUCT	"[ebrowse-hs "
#define TREE_STRUCT		"[ebrowse-ts "
#define MEMBER_STRUCT		"[ebrowse-ms "
#define BROWSE_STRUCT		"[ebrowse-bs "
#define CLASS_STRUCT		"[ebrowse-cs "

/* The name of the symbol table entry for global functions, variables,
   defines etc.  This name also appears in the browser display.  */

#define GLOBALS_NAME "*Globals*"

/* Token definitions.  */

enum token
{
  YYEOF = 0,			/* end of file */
  CSTRING = 256,		/* string constant */
  CCHAR,			/* character constant */
  CINT,				/* integral constant */
  CFLOAT,			/* real constant */

  ELLIPSIS,			/* ... */
  LSHIFTASGN,			/* <<= */
  RSHIFTASGN,			/* >>= */
  ARROWSTAR,			/* ->* */
  IDENT,			/* identifier */
  DIVASGN,			/* /= */
  INC,				/* ++ */
  ADDASGN,			/* += */
  DEC,				/* -- */
  ARROW,			/* -> */
  SUBASGN,			/* -= */
  MULASGN,			/* *= */
  MODASGN,			/* %= */
  LOR,				/* || */
  ORASGN,			/* |= */
  LAND,				/* && */
  ANDASGN,			/* &= */
  XORASGN,			/* ^= */
  POINTSTAR,			/* .* */
  DCOLON,			/* :: */
  EQ,				/* == */
  NE,				/* != */
  LE,				/* <= */
  LSHIFT,			/* << */
  GE,				/* >= */
  RSHIFT,			/* >> */

/* Keywords.  The undef's are there because these
   three symbols are very likely to be defined somewhere.  */
#undef BOOL
#undef TRUE
#undef FALSE

  ASM,				/* asm */
  AUTO,				/* auto */
  BREAK,			/* break */
  CASE,				/* case  */
  CATCH,			/* catch */
  CHAR,				/* char */
  CLASS,			/* class */
  CONST,			/* const */
  CONTINUE,			/* continue */
  DEFAULT,			/* default */
  DELETE,			/* delete */
  DO,				/* do */
  DOUBLE,			/* double */
  ELSE,				/* else */
  ENUM,				/* enum */
  EXTERN,			/* extern */
  FLOAT,			/* float */
  FOR,				/* for */
  FRIEND,			/* friend */
  GOTO,				/* goto */
  IF,				/* if */
  T_INLINE,			/* inline */
  INT,				/* int */
  LONG,				/* long */
  NEW,				/* new */
  OPERATOR,			/* operator */
  PRIVATE,			/* private */
  PROTECTED,			/* protected */
  PUBLIC,			/* public */
  REGISTER,			/* register */
  RETURN,			/* return */
  SHORT,			/* short */
  SIGNED,			/* signed */
  SIZEOF,			/* sizeof */
  STATIC,			/* static */
  STRUCT,			/* struct */
  SWITCH,			/* switch */
  TEMPLATE,			/* template */
  THIS,				/* this */
  THROW,			/* throw */
  TRY,				/* try */
  TYPEDEF,			/* typedef */
  UNION,			/* union */
  UNSIGNED,			/* unsigned */
  VIRTUAL,			/* virtual */
  VOID,				/* void */
  VOLATILE,			/* volatile */
  WHILE,			/* while */
  MUTABLE,			/* mutable */
  BOOL,				/* bool */
  TRUE,				/* true */
  FALSE,			/* false */
  SIGNATURE,			/* signature (GNU extension) */
  NAMESPACE,			/* namespace */
  EXPLICIT,			/* explicit */
  TYPENAME,			/* typename */
  CONST_CAST,			/* const_cast */
  DYNAMIC_CAST,			/* dynamic_cast */
  REINTERPRET_CAST,		/* reinterpret_cast */
  STATIC_CAST,			/* static_cast */
  TYPEID,			/* typeid */
  USING,			/* using */
  WCHAR				/* wchar_t */
};

/* Storage classes, in a wider sense.  */

enum sc
{
  SC_UNKNOWN,
  SC_MEMBER,			/* Is an instance member.  */
  SC_STATIC,			/* Is static member.  */
  SC_FRIEND,			/* Is friend function.  */
  SC_TYPE			/* Is a type definition.  */
};

/* Member visibility.  */

enum visibility
{
  V_PUBLIC,
  V_PROTECTED,
  V_PRIVATE
};

/* Member flags.  */

#define F_VIRTUAL	1	/* Is virtual function.  */
#define F_INLINE	2	/* Is inline function.  */
#define F_CONST		4	/* Is const.  */
#define F_PURE		8	/* Is pure virtual function.  */
#define F_MUTABLE	16	/* Is mutable.  */
#define F_TEMPLATE	32	/* Is a template.  */
#define F_EXPLICIT	64	/* Is explicit constructor.  */
#define F_THROW		128	/* Has a throw specification.  */
#define F_EXTERNC	256	/* Is declared extern "C".  */
#define F_DEFINE	512	/* Is a #define.  */

/* Two macros to set and test a bit in an int.  */

#define SET_FLAG(F, FLAG)	((F) |= (FLAG))
#define HAS_FLAG(F, FLAG)	(((F) & (FLAG)) != 0)

/* Structure describing a class member.  */

struct member
{
  struct member *next;		/* Next in list of members.  */
  struct member *anext;		/* Collision chain in member_table.  */
  struct member **list;		/* Pointer to list in class.  */
  unsigned param_hash;		/* Hash value for parameter types.  */
  int vis;			/* Visibility (public, ...).  */
  int flags;			/* See F_* above.  */
  char *regexp;			/* Matching regular expression.  */
  char *filename;		/* Don't free this shared string.  */
  int pos;			/* Buffer position of occurrence.  */
  char *def_regexp;		/* Regular expression matching definition.  */
  char *def_filename;		/* File name of definition.  */
  int def_pos;			/* Buffer position of definition.  */
  char name[1];			/* Member name.  */
};

/* Structures of this type are used to connect class structures with
   their super and subclasses.  */

struct link
{
  struct sym *sym;		/* The super or subclass.  */
  struct link *next;		/* Next in list or NULL.  */
};

/* Structure used to record namespace aliases.  */

struct alias
{
  struct alias *next;		/* Next in list.  */
286 287
  struct sym *namesp;		/* Namespace in which defined.  */
  struct link *aliasee;		/* List of aliased namespaces (A::B::C...).  */
Gerd Moellmann's avatar
Gerd Moellmann committed
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406
  char name[1];			/* Alias name.  */
};

/* The structure used to describe a class in the symbol table,
   or a namespace in all_namespaces.  */

struct sym
{
  int flags;			/* Is class a template class?.  */
  unsigned char visited;	/* Used to find circles.  */
  struct sym *next;             /* Hash collision list.  */
  struct link *subs;		/* List of subclasses.  */
  struct link *supers;		/* List of superclasses.  */
  struct member *vars;		/* List of instance variables.  */
  struct member *fns;		/* List of instance functions.  */
  struct member *static_vars;	/* List of static variables.  */
  struct member *static_fns;	/* List of static functions.  */
  struct member *friends;	/* List of friend functions.  */
  struct member *types;		/* List of local types.  */
  char *regexp;			/* Matching regular expression.  */
  int pos;			/* Buffer position.  */
  char *filename;		/* File in which it can be found.  */
  char *sfilename;		/* File in which members can be found.  */
  struct sym *namesp;		/* Namespace in which defined. .  */
  char name[1];                 /* Name of the class.  */
};

/* Experimental: Print info for `--position-info'.  We print
   '(CLASS-NAME SCOPE MEMBER-NAME).  */

#define P_DEFN	1
#define P_DECL  2

int info_where;
struct sym *info_cls = NULL;
struct member *info_member = NULL;

/* Experimental.  For option `--position-info', the buffer position we
   are interested in.  When this position is reached, print out
   information about what we know about that point.  */

int info_position = -1;

/* Command line options structure for getopt_long.  */

struct option options[] =
{
  {"append", 			no_argument, 	   NULL, 'a'},
  {"files", 			required_argument, NULL, 'f'},
  {"help", 			no_argument, 	   NULL, -2},
  {"min-regexp-length", 	required_argument, NULL, 'm'},
  {"max-regexp-length", 	required_argument, NULL, 'M'},
  {"no-nested-classes", 	no_argument, 	   NULL, 'n'},
  {"no-regexps", 		no_argument, 	   NULL, 'x'},
  {"no-structs-or-unions", 	no_argument, 	   NULL, 's'},
  {"output-file", 		required_argument, NULL, 'o'},
  {"position-info", 		required_argument, NULL, 'p'},
  {"search-path", 		required_argument, NULL, 'I'},
  {"verbose", 			no_argument, 	   NULL, 'v'},
  {"version", 			no_argument, 	   NULL, -3},
  {"very-verbose", 		no_argument, 	   NULL, 'V'},
  {NULL, 			0, 		   NULL, 0}
};

/* Semantic values of tokens.  Set by yylex..  */

unsigned yyival;		/* Set for token CINT.  */
char *yytext;			/* Set for token IDENT.  */
char *yytext_end;

/* Output file.  */

FILE *yyout;

/* Current line number.  */

int yyline;

/* The name of the current input file.  */

char *filename;

/* Three character class vectors, and macros to test membership
   of characters.  */

char is_ident[255];
char is_digit[255];
char is_white[255];

#define IDENTP(C)	is_ident[(unsigned char) (C)]
#define DIGITP(C)	is_digit[(unsigned char) (C)]
#define WHITEP(C)	is_white[(unsigned char) (C)]

/* Command line flags.  */

int f_append;
int f_verbose;
int f_very_verbose;
int f_structs = 1;
int f_regexps = 1;
int f_nested_classes = 1;

/* Maximum and minimum lengths of regular expressions matching a
   member, class etc., for writing them to the output file.  These are
   overridable from the command line.  */

int min_regexp = 5;
int max_regexp = 50;

/* Input buffer.  */

char *inbuffer;
char *in;
int inbuffer_size;

/* Return the current buffer position in the input file.  */

#define BUFFER_POS() (in - inbuffer)

407
/* If current lookahead is CSTRING, the following points to the
Gerd Moellmann's avatar
Gerd Moellmann committed
408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427
   first character in the string constant.  Used for recognizing
   extern "C".  */

char *string_start;

/* The size of the hash tables for classes.and members.  Should be
   prime.  */

#define TABLE_SIZE 1001

/* The hash table for class symbols.  */

struct sym *class_table[TABLE_SIZE];

/* Hash table containing all member structures.  This is generally
   faster for member lookup than traversing the member lists of a
   `struct sym'.  */

struct member *member_table[TABLE_SIZE];

428 429 430 431
/* Hash table for namespace aliases */

struct alias *namespace_alias_table[TABLE_SIZE];

Gerd Moellmann's avatar
Gerd Moellmann committed
432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481
/* The special class symbol used to hold global functions,
   variables etc.  */

struct sym *global_symbols;

/* The current namespace.  */

struct sym *current_namespace;

/* The list of all known namespaces.  */

struct sym *all_namespaces;

/* Stack of namespaces we're currently nested in, during the parse.  */

struct sym **namespace_stack;
int namespace_stack_size;
int namespace_sp;

/* The current lookahead token.  */

int tk = -1;

/* Structure describing a keyword.  */

struct kw
{
  char *name;			/* Spelling.  */
  int tk;			/* Token value.  */
  struct kw *next;		/* Next in collision chain.  */
};

/* Keywords are lookup up in a hash table of their own.  */

#define KEYWORD_TABLE_SIZE 1001
struct kw *keyword_table[KEYWORD_TABLE_SIZE];

/* Search path.  */

struct search_path
{
  char *path;
  struct search_path *next;
};

struct search_path *search_path;
struct search_path *search_path_tail;

/* Function prototypes.  */

482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536
int yylex (void);
void yyparse (void);
void re_init_parser (void);
char *token_string (int);
char *matching_regexp (void);
void init_sym (void);
struct sym *add_sym (char *, struct sym *);
void add_link (struct sym *, struct sym *);
void add_member_defn (struct sym *, char *, char *,
                      int, unsigned, int, int, int);
void add_member_decl (struct sym *, char *, char *, int,
                      unsigned, int, int, int, int);
void dump_roots (FILE *);
void *xmalloc (int);
void xfree (void *);
void add_global_defn (char *, char *, int, unsigned, int, int, int);
void add_global_decl (char *, char *, int, unsigned, int, int, int);
void add_define (char *, char *, int);
void mark_inherited_virtual (void);
void leave_namespace (void);
void enter_namespace (char *);
void register_namespace_alias (char *, struct link *);
void insert_keyword (char *, int);
void re_init_scanner (void);
void init_scanner (void);
void usage (int);
void version (void);
void process_file (char *);
void add_search_path (char *);
FILE *open_file (char *);
int process_pp_line (void);
int dump_members (FILE *, struct member *);
void dump_sym (FILE *, struct sym *);
int dump_tree (FILE *, struct sym *);
struct member *find_member (struct sym *, char *, int, int, unsigned);
struct member *add_member (struct sym *, char *, int, int, unsigned);
void mark_virtual (struct sym *);
void mark_virtual (struct sym *);
struct sym *make_namespace (char *, struct sym *);
char *sym_scope (struct sym *);
char *sym_scope_1 (struct sym *);
int skip_to (int);
void skip_matching (void);
void member (struct sym *, int);
void class_body (struct sym *, int);
void class_definition (struct sym *, int, int, int);
void declaration (int);
unsigned parm_list (int *);
char *operator_name (int *);
struct sym *parse_classname (void);
struct sym *parse_qualified_ident_or_type (char **);
void parse_qualified_param_ident_or_type (char **);
int globals (int);
void yyerror (char *, char *);
void usage (int) NO_RETURN;
537
void version (void) NO_RETURN;
Gerd Moellmann's avatar
Gerd Moellmann committed
538 539 540 541 542 543 544 545 546 547 548



/***********************************************************************
			      Utilities
 ***********************************************************************/

/* Print an error in a printf-like style with the current input file
   name and line number.  */

void
549
yyerror (char *format, char *s)
Gerd Moellmann's avatar
Gerd Moellmann committed
550 551
{
  fprintf (stderr, "%s:%d: ", filename, yyline);
552
  fprintf (stderr, format, s);
Gerd Moellmann's avatar
Gerd Moellmann committed
553 554 555 556 557
  putc ('\n', stderr);
}


/* Like malloc but print an error and exit if not enough memory is
Dave Love's avatar
Dave Love committed
558
   available.  */
Gerd Moellmann's avatar
Gerd Moellmann committed
559 560

void *
561
xmalloc (int nbytes)
Gerd Moellmann's avatar
Gerd Moellmann committed
562 563
{
  void *p = malloc (nbytes);
564 565
  if (p == NULL)
    {
566
      yyerror ("out of memory", NULL);
567
      exit (EXIT_FAILURE);
568 569
    }
  return p;
Gerd Moellmann's avatar
Gerd Moellmann committed
570 571 572 573 574 575
}


/* Like realloc but print an error and exit if out of memory.  */

void *
576
xrealloc (void *p, int sz)
Gerd Moellmann's avatar
Gerd Moellmann committed
577 578
{
  p = realloc (p, sz);
579 580
  if (p == NULL)
    {
581
      yyerror ("out of memory", NULL);
582
      exit (EXIT_FAILURE);
583 584
    }
  return p;
Gerd Moellmann's avatar
Gerd Moellmann committed
585 586 587 588 589 590 591
}


/* Like strdup, but print an error and exit if not enough memory is
   available..  If S is null, return null.  */

char *
592
xstrdup (char *s)
Gerd Moellmann's avatar
Gerd Moellmann committed
593 594
{
  if (s)
595
    s = strcpy (xmalloc (strlen (s) + 1), s);
Gerd Moellmann's avatar
Gerd Moellmann committed
596 597 598 599 600 601 602 603 604 605 606 607 608
  return s;
}



/***********************************************************************
			       Symbols
 ***********************************************************************/

/* Initialize the symbol table.  This currently only sets up the
   special symbol for globals (`*Globals*').  */

void
609
init_sym (void)
Gerd Moellmann's avatar
Gerd Moellmann committed
610 611 612 613 614 615 616 617 618 619 620 621 622
{
  global_symbols = add_sym (GLOBALS_NAME, NULL);
}


/* Add a symbol for class NAME to the symbol table.  NESTED_IN_CLASS
   is the class in which class NAME was found.  If it is null,
   this means the scope of NAME is the current namespace.

   If a symbol for NAME already exists, return that.  Otherwise
   create a new symbol and set it to default values.  */

struct sym *
623
add_sym (char *name, struct sym *nested_in_class)
Gerd Moellmann's avatar
Gerd Moellmann committed
624 625 626 627 628 629 630 631 632 633 634
{
  struct sym *sym;
  unsigned h;
  char *s;
  struct sym *scope = nested_in_class ? nested_in_class : current_namespace;

  for (s = name, h = 0; *s; ++s)
    h = (h << 1) ^ *s;
  h %= TABLE_SIZE;

  for (sym = class_table[h]; sym; sym = sym->next)
635 636 637 638
    if (streq (name, sym->name)
	&& ((!sym->namesp && !scope)
	    || (sym->namesp && scope
		&& streq (sym->namesp->name, scope->name))))
Gerd Moellmann's avatar
Gerd Moellmann committed
639 640 641 642 643 644 645 646 647 648
      break;

  if (sym == NULL)
    {
      if (f_very_verbose)
	{
	  putchar ('\t');
	  puts (name);
	}

649
      sym = (struct sym *) xmalloc (sizeof *sym + strlen (name));
Gerd Moellmann's avatar
Gerd Moellmann committed
650 651 652 653 654 655 656 657 658 659 660 661 662 663
      bzero (sym, sizeof *sym);
      strcpy (sym->name, name);
      sym->namesp = scope;
      sym->next = class_table[h];
      class_table[h] = sym;
    }

  return sym;
}


/* Add links between superclass SUPER and subclass SUB.  */

void
664
add_link (struct sym *super, struct sym *sub)
Gerd Moellmann's avatar
Gerd Moellmann committed
665 666 667 668 669 670 671 672 673 674 675 676
{
  struct link *lnk, *lnk2, *p, *prev;

  /* See if a link already exists.  */
  for (p = super->subs, prev = NULL;
       p && strcmp (sub->name, p->sym->name) > 0;
       prev = p, p = p->next)
    ;

  /* Avoid duplicates.  */
  if (p == NULL || p->sym != sub)
    {
677 678
      lnk = (struct link *) xmalloc (sizeof *lnk);
      lnk2 = (struct link *) xmalloc (sizeof *lnk2);
679

Gerd Moellmann's avatar
Gerd Moellmann committed
680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703
      lnk->sym = sub;
      lnk->next = p;

      if (prev)
	prev->next = lnk;
      else
	super->subs = lnk;

      lnk2->sym = super;
      lnk2->next = sub->supers;
      sub->supers = lnk2;
    }
}


/* Find in class CLS member NAME.

   VAR non-zero means look for a member variable; otherwise a function
   is searched.  SC specifies what kind of member is searched---a
   static, or per-instance member etc.  HASH is a hash code for the
   parameter types of functions.  Value is a pointer to the member
   found or null if not found.  */

struct member *
704
find_member (struct sym *cls, char *name, int var, int sc, unsigned int hash)
Gerd Moellmann's avatar
Gerd Moellmann committed
705 706 707 708 709 710 711 712 713 714 715 716
{
  struct member **list;
  struct member *p;
  unsigned name_hash = 0;
  char *s;
  int i;

  switch (sc)
    {
    case SC_FRIEND:
      list = &cls->friends;
      break;
717

Gerd Moellmann's avatar
Gerd Moellmann committed
718 719 720
    case SC_TYPE:
      list = &cls->types;
      break;
721

Gerd Moellmann's avatar
Gerd Moellmann committed
722 723 724
    case SC_STATIC:
      list = var ? &cls->static_vars : &cls->static_fns;
      break;
725

Gerd Moellmann's avatar
Gerd Moellmann committed
726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753
    default:
      list = var ? &cls->vars : &cls->fns;
      break;
    }

  for (s = name; *s; ++s)
    name_hash = (name_hash << 1) ^ *s;
  i = name_hash % TABLE_SIZE;

  for (p = member_table[i]; p; p = p->anext)
    if (p->list == list && p->param_hash == hash && streq (name, p->name))
      break;

  return p;
}


/* Add to class CLS information for the declaration of member NAME.
   REGEXP is a regexp matching the declaration, if non-null.  POS is
   the position in the source where the declaration is found.  HASH is
   a hash code for the parameter list of the member, if it's a
   function.  VAR non-zero means member is a variable or type.  SC
   specifies the type of member (instance member, static, ...).  VIS
   is the member's visibility (public, protected, private).  FLAGS is
   a bit set giving additional information about the member (see the
   F_* defines).  */

void
754
add_member_decl (struct sym *cls, char *name, char *regexp, int pos, unsigned int hash, int var, int sc, int vis, int flags)
Gerd Moellmann's avatar
Gerd Moellmann committed
755 756 757 758 759 760 761 762
{
  struct member *m;

  m = find_member (cls, name, var, sc, hash);
  if (m == NULL)
    m = add_member (cls, name, var, sc, hash);

  /* Have we seen a new filename?  If so record that.  */
763
  if (!cls->filename || !FILENAME_EQ (cls->filename, filename))
Gerd Moellmann's avatar
Gerd Moellmann committed
764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801
    m->filename = filename;

  m->regexp = regexp;
  m->pos = pos;
  m->flags = flags;

  switch (vis)
    {
    case PRIVATE:
      m->vis = V_PRIVATE;
      break;

    case PROTECTED:
      m->vis = V_PROTECTED;
      break;

    case PUBLIC:
      m->vis = V_PUBLIC;
      break;
    }

  info_where = P_DECL;
  info_cls = cls;
  info_member = m;
}


/* Add to class CLS information for the definition of member NAME.
   REGEXP is a regexp matching the declaration, if non-null.  POS is
   the position in the source where the declaration is found.  HASH is
   a hash code for the parameter list of the member, if it's a
   function.  VAR non-zero means member is a variable or type.  SC
   specifies the type of member (instance member, static, ...).  VIS
   is the member's visibility (public, protected, private).  FLAGS is
   a bit set giving additional information about the member (see the
   F_* defines).  */

void
802
add_member_defn (struct sym *cls, char *name, char *regexp, int pos, unsigned int hash, int var, int sc, int flags)
Gerd Moellmann's avatar
Gerd Moellmann committed
803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825
{
  struct member *m;

  if (sc == SC_UNKNOWN)
    {
      m = find_member (cls, name, var, SC_MEMBER, hash);
      if (m == NULL)
	{
	  m = find_member (cls, name, var, SC_STATIC, hash);
	  if (m == NULL)
	    m = add_member (cls, name, var, sc, hash);
	}
    }
  else
    {
      m = find_member (cls, name, var, sc, hash);
      if (m == NULL)
	m = add_member (cls, name, var, sc, hash);
    }

  if (!cls->sfilename)
    cls->sfilename = filename;

826
  if (!FILENAME_EQ (cls->sfilename, filename))
Gerd Moellmann's avatar
Gerd Moellmann committed
827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843
    m->def_filename = filename;

  m->def_regexp = regexp;
  m->def_pos = pos;
  m->flags |= flags;

  info_where = P_DEFN;
  info_cls = cls;
  info_member = m;
}


/* Add a symbol for a define named NAME to the symbol table.
   REGEXP is a regular expression matching the define in the source,
   if it is non-null.  POS is the position in the file.  */

void
844
add_define (char *name, char *regexp, int pos)
Gerd Moellmann's avatar
Gerd Moellmann committed
845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861
{
  add_global_defn (name, regexp, pos, 0, 1, SC_FRIEND, F_DEFINE);
  add_global_decl (name, regexp, pos, 0, 1, SC_FRIEND, F_DEFINE);
}


/* Add information for the global definition of NAME.
   REGEXP is a regexp matching the declaration, if non-null.  POS is
   the position in the source where the declaration is found.  HASH is
   a hash code for the parameter list of the member, if it's a
   function.  VAR non-zero means member is a variable or type.  SC
   specifies the type of member (instance member, static, ...).  VIS
   is the member's visibility (public, protected, private).  FLAGS is
   a bit set giving additional information about the member (see the
   F_* defines).  */

void
862
add_global_defn (char *name, char *regexp, int pos, unsigned int hash, int var, int sc, int flags)
Gerd Moellmann's avatar
Gerd Moellmann committed
863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891
{
  int i;
  struct sym *sym;

  /* Try to find out for which classes a function is a friend, and add
     what we know about it to them.  */
  if (!var)
    for (i = 0; i < TABLE_SIZE; ++i)
      for (sym = class_table[i]; sym; sym = sym->next)
	if (sym != global_symbols && sym->friends)
	  if (find_member (sym, name, 0, SC_FRIEND, hash))
	    add_member_defn (sym, name, regexp, pos, hash, 0,
			     SC_FRIEND, flags);

  /* Add to global symbols.  */
  add_member_defn (global_symbols, name, regexp, pos, hash, var, sc, flags);
}


/* Add information for the global declaration of NAME.
   REGEXP is a regexp matching the declaration, if non-null.  POS is
   the position in the source where the declaration is found.  HASH is
   a hash code for the parameter list of the member, if it's a
   function.  VAR non-zero means member is a variable or type.  SC
   specifies the type of member (instance member, static, ...).  VIS
   is the member's visibility (public, protected, private).  FLAGS is
   a bit set giving additional information about the member (see the
   F_* defines).  */

892
void
893
add_global_decl (char *name, char *regexp, int pos, unsigned int hash, int var, int sc, int flags)
Gerd Moellmann's avatar
Gerd Moellmann committed
894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910
{
  /* Add declaration only if not already declared.  Header files must
     be processed before source files for this to have the right effect.
     I do not want to handle implicit declarations at the moment.  */
  struct member *m;
  struct member *found;

  m = found = find_member (global_symbols, name, var, sc, hash);
  if (m == NULL)
    m = add_member (global_symbols, name, var, sc, hash);

  /* Definition already seen => probably last declaration implicit.
     Override.  This means that declarations must always be added to
     the symbol table before definitions.  */
  if (!found)
    {
      if (!global_symbols->filename
911
	  || !FILENAME_EQ (global_symbols->filename, filename))
Gerd Moellmann's avatar
Gerd Moellmann committed
912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931
	m->filename = filename;

      m->regexp = regexp;
      m->pos = pos;
      m->vis = V_PUBLIC;
      m->flags = flags;

      info_where = P_DECL;
      info_cls = global_symbols;
      info_member = m;
    }
}


/* Add a symbol for member NAME to class CLS.
   VAR non-zero means it's a variable.  SC specifies the kind of
   member.  HASH is a hash code for the parameter types of a function.
   Value is a pointer to the member's structure.  */

struct member *
932
add_member (struct sym *cls, char *name, int var, int sc, unsigned int hash)
Gerd Moellmann's avatar
Gerd Moellmann committed
933
{
934
  struct member *m = (struct member *) xmalloc (sizeof *m + strlen (name));
Gerd Moellmann's avatar
Gerd Moellmann committed
935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960
  struct member **list;
  struct member *p;
  struct member *prev;
  unsigned name_hash = 0;
  int i;
  char *s;

  strcpy (m->name, name);
  m->param_hash = hash;

  m->vis = 0;
  m->flags = 0;
  m->regexp = NULL;
  m->filename = NULL;
  m->pos = 0;
  m->def_regexp = NULL;
  m->def_filename = NULL;
  m->def_pos = 0;

  assert (cls != NULL);

  switch (sc)
    {
    case SC_FRIEND:
      list = &cls->friends;
      break;
961

Gerd Moellmann's avatar
Gerd Moellmann committed
962 963 964
    case SC_TYPE:
      list = &cls->types;
      break;
965

Gerd Moellmann's avatar
Gerd Moellmann committed
966 967 968
    case SC_STATIC:
      list = var ? &cls->static_vars : &cls->static_fns;
      break;
969

Gerd Moellmann's avatar
Gerd Moellmann committed
970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002
    default:
      list = var ? &cls->vars : &cls->fns;
      break;
    }

  for (s = name; *s; ++s)
    name_hash = (name_hash << 1) ^ *s;
  i = name_hash % TABLE_SIZE;
  m->anext = member_table[i];
  member_table[i] = m;
  m->list = list;

  /* Keep the member list sorted.  It's cheaper to do it here than to
     sort them in Lisp.  */
  for (prev = NULL, p = *list;
       p && strcmp (name, p->name) > 0;
       prev = p, p = p->next)
    ;

  m->next = p;
  if (prev)
    prev->next = m;
  else
    *list = m;
  return m;
}


/* Given the root R of a class tree, step through all subclasses
   recursively, marking functions as virtual that are declared virtual
   in base classes.  */

void
1003
mark_virtual (struct sym *r)
Gerd Moellmann's avatar
Gerd Moellmann committed
1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026
{
  struct link *p;
  struct member *m, *m2;

  for (p = r->subs; p; p = p->next)
    {
      for (m = r->fns; m; m = m->next)
        if (HAS_FLAG (m->flags, F_VIRTUAL))
          {
            for (m2 = p->sym->fns; m2; m2 = m2->next)
              if (m->param_hash == m2->param_hash && streq (m->name, m2->name))
                SET_FLAG (m2->flags, F_VIRTUAL);
          }

      mark_virtual (p->sym);
    }
}


/* For all roots of the class tree, mark functions as virtual that
   are virtual because of a virtual declaration in a base class.  */

void
1027
mark_inherited_virtual (void)
Gerd Moellmann's avatar
Gerd Moellmann committed
1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041
{
  struct sym *r;
  int i;

  for (i = 0; i < TABLE_SIZE; ++i)
    for (r = class_table[i]; r; r = r->next)
      if (r->supers == NULL)
        mark_virtual (r);
}


/* Create and return a symbol for a namespace with name NAME.  */

struct sym *
1042
make_namespace (char *name, struct sym *context)
Gerd Moellmann's avatar
Gerd Moellmann committed
1043
{
1044
  struct sym *s = (struct sym *) xmalloc (sizeof *s + strlen (name));
Gerd Moellmann's avatar
Gerd Moellmann committed
1045 1046 1047
  bzero (s, sizeof *s);
  strcpy (s->name, name);
  s->next = all_namespaces;
1048
  s->namesp = context;
Gerd Moellmann's avatar
Gerd Moellmann committed
1049 1050 1051 1052 1053
  all_namespaces = s;
  return s;
}


1054
/* Find the symbol for namespace NAME.  If not found, retrun NULL */
Gerd Moellmann's avatar
Gerd Moellmann committed
1055 1056

struct sym *
1057
check_namespace (char *name, struct sym *context)
Gerd Moellmann's avatar
Gerd Moellmann committed
1058
{
1059
  struct sym *p = NULL;
1060

Gerd Moellmann's avatar
Gerd Moellmann committed
1061 1062
  for (p = all_namespaces; p; p = p->next)
    {
1063
      if (streq (p->name, name) && (p->namesp == context))
Gerd Moellmann's avatar
Gerd Moellmann committed
1064 1065
	    break;
	}
1066 1067

  return p;
Gerd Moellmann's avatar
Gerd Moellmann committed
1068 1069
    }

1070 1071 1072 1073
/* Find the symbol for namespace NAME.  If not found, add a new symbol
   for NAME to all_namespaces.  */

struct sym *
1074
find_namespace (char *name, struct sym *context)
1075 1076 1077
{
  struct sym *p = check_namespace (name, context);

Gerd Moellmann's avatar
Gerd Moellmann committed
1078
  if (p == NULL)
1079
    p = make_namespace (name, context);
Gerd Moellmann's avatar
Gerd Moellmann committed
1080 1081 1082

  return p;
}
1083

Gerd Moellmann's avatar
Gerd Moellmann committed
1084

1085 1086 1087
/* Find namespace alias with name NAME. If not found return NULL. */

struct link *
1088
check_namespace_alias (char *name)
1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109
{
  struct link *p = NULL;
  struct alias *al;
  unsigned h;
  char *s;

  for (s = name, h = 0; *s; ++s)
    h = (h << 1) ^ *s;
  h %= TABLE_SIZE;

  for (al = namespace_alias_table[h]; al; al = al->next)
    if (streq (name, al->name) && (al->namesp == current_namespace))
      {
        p = al->aliasee;
        break;
      }

  return p;
}

/* Register the name NEW_NAME as an alias for namespace list OLD_NAME.  */
Gerd Moellmann's avatar
Gerd Moellmann committed
1110 1111

void
1112
register_namespace_alias (char *new_name, struct link *old_name)
Gerd Moellmann's avatar
Gerd Moellmann committed
1113
{
1114 1115
  unsigned h;
  char *s;
Gerd Moellmann's avatar
Gerd Moellmann committed
1116 1117
  struct alias *al;

1118 1119 1120 1121 1122 1123 1124 1125
  for (s = new_name, h = 0; *s; ++s)
    h = (h << 1) ^ *s;
  h %= TABLE_SIZE;


  /* Is it already in the table of aliases?  */
  for (al = namespace_alias_table[h]; al; al = al->next)
    if (streq (new_name, al->name) && (al->namesp == current_namespace))
Gerd Moellmann's avatar
Gerd Moellmann committed
1126 1127
      return;

1128
  al = (struct alias *) xmalloc (sizeof *al + strlen (new_name));
Gerd Moellmann's avatar
Gerd Moellmann committed
1129
  strcpy (al->name, new_name);
1130 1131 1132 1133
  al->next = namespace_alias_table[h];
  al->namesp = current_namespace;
  al->aliasee = old_name;
  namespace_alias_table[h] = al;
Gerd Moellmann's avatar
Gerd Moellmann committed
1134 1135 1136 1137 1138 1139
}


/* Enter namespace with name NAME.  */

void
1140
enter_namespace (char *name)
Gerd Moellmann's avatar
Gerd Moellmann committed
1141
{
1142
  struct sym *p = find_namespace (name, current_namespace);
Gerd Moellmann's avatar
Gerd Moellmann committed
1143 1144 1145 1146

  if (namespace_sp == namespace_stack_size)
    {
      int size = max (10, 2 * namespace_stack_size);
1147 1148 1149
      namespace_stack
	= (struct sym **) xrealloc ((void *)namespace_stack,
				    size * sizeof *namespace_stack);
Gerd Moellmann's avatar
Gerd Moellmann committed
1150 1151
      namespace_stack_size = size;
    }
1152

Gerd Moellmann's avatar
Gerd Moellmann committed
1153 1154 1155 1156 1157 1158 1159 1160
  namespace_stack[namespace_sp++] = current_namespace;
  current_namespace = p;
}


/* Leave the current namespace.  */

void
1161
leave_namespace (void)
Gerd Moellmann's avatar
Gerd Moellmann committed
1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202
{
  assert (namespace_sp > 0);
  current_namespace = namespace_stack[--namespace_sp];
}



/***********************************************************************
		       Writing the Output File
 ***********************************************************************/

/* Write string S to the output file FP in a Lisp-readable form.
   If S is null, write out `()'.  */

#define PUTSTR(s, fp)				\
  do {						\
    if (!s)					\
      {						\
        putc ('(', fp);				\
        putc (')', fp);				\
        putc (' ', fp);				\
      }						\
    else					\
      {						\
        putc ('"', fp);				\
        fputs (s, fp);				\
        putc ('"', fp);				\
        putc (' ', fp);				\
      }						\
   } while (0)

/* A dynamically allocated buffer for constructing a scope name.  */

char *scope_buffer;
int scope_buffer_size;
int scope_buffer_len;


/* Make sure scope_buffer has enough room to add LEN chars to it.  */

void
1203
ensure_scope_buffer_room (int len)
Gerd Moellmann's avatar
Gerd Moellmann committed
1204 1205 1206 1207
{
  if (scope_buffer_len + len >= scope_buffer_size)
    {
      int new_size = max (2 * scope_buffer_size, scope_buffer_len + len);
1208
      scope_buffer = (char *) xrealloc (scope_buffer, new_size);
Gerd Moellmann's avatar
Gerd Moellmann committed
1209 1210 1211 1212 1213 1214 1215 1216 1217 1218
      scope_buffer_size = new_size;
    }
}


/* Recursively add the scope names of symbol P and the scopes of its
   namespaces to scope_buffer.  Value is a pointer to the complete
   scope name constructed.  */

char *
1219
sym_scope_1 (struct sym *p)
Gerd Moellmann's avatar
Gerd Moellmann committed
1220 1221
{
  int len;
1222

Gerd Moellmann's avatar
Gerd Moellmann committed
1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236
  if (p->namesp)
    sym_scope_1 (p->namesp);

  if (*scope_buffer)
    {
      ensure_scope_buffer_room (3);
      strcat (scope_buffer, "::");
      scope_buffer_len += 2;
    }

  len = strlen (p->name);
  ensure_scope_buffer_room (len + 1);
  strcat (scope_buffer, p->name);
  scope_buffer_len += len;
1237

Gerd Moellmann's avatar
Gerd Moellmann committed
1238 1239 1240 1241 1242 1243
  if (HAS_FLAG (p->flags, F_TEMPLATE))
    {
      ensure_scope_buffer_room (3);
      strcat (scope_buffer, "<>");
      scope_buffer_len += 2;
    }
1244

Gerd Moellmann's avatar
Gerd Moellmann committed
1245 1246 1247 1248 1249 1250 1251 1252
  return scope_buffer;
}


/* Return the scope of symbol P in printed representation, i.e.
   as it would appear in a C*+ source file.  */

char *
1253
sym_scope (struct sym *p)
Gerd Moellmann's avatar
Gerd Moellmann committed
1254 1255 1256 1257
{
  if (!scope_buffer)
    {
      scope_buffer_size = 1024;
1258
      scope_buffer = (char *) xmalloc (scope_buffer_size);
Gerd Moellmann's avatar
Gerd Moellmann committed
1259
    }
1260

Gerd Moellmann's avatar
Gerd Moellmann committed
1261 1262
  *scope_buffer = '\0';
  scope_buffer_len = 0;
1263

Gerd Moellmann's avatar
Gerd Moellmann committed
1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274
  if (p->namesp)
    sym_scope_1 (p->namesp);

  return scope_buffer;
}


/* Dump the list of members M to file FP.  Value is the length of the
   list.  */

int
1275
dump_members (FILE *fp, struct member *m)
Gerd Moellmann's avatar
Gerd Moellmann committed
1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307
{
  int n;

  putc ('(', fp);

  for (n = 0; m; m = m->next, ++n)
    {
      fputs (MEMBER_STRUCT, fp);
      PUTSTR (m->name, fp);
      PUTSTR (NULL, fp);		/* FIXME? scope for globals */
      fprintf (fp, "%u ", (unsigned) m->flags);
      PUTSTR (m->filename, fp);
      PUTSTR (m->regexp, fp);
      fprintf (fp, "%u ", (unsigned) m->pos);
      fprintf (fp, "%u ", (unsigned) m->vis);
      putc (' ', fp);
      PUTSTR (m->def_filename, fp);
      PUTSTR (m->def_regexp, fp);
      fprintf (fp, "%u", (unsigned) m->def_pos);
      putc (']', fp);
      putc ('\n', fp);
    }

  putc (')', fp);
  putc ('\n', fp);
  return n;
}


/* Dump class ROOT to stream FP.  */

void
1308
dump_sym (FILE *fp, struct sym *root)
Gerd Moellmann's avatar
Gerd Moellmann committed
1309 1310 1311
{
  fputs (CLASS_STRUCT, fp);
  PUTSTR (root->name, fp);
1312

Gerd Moellmann's avatar
Gerd Moellmann committed
1313 1314 1315 1316 1317
  /* Print scope, if any.  */
  if (root->namesp)
    PUTSTR (sym_scope (root), fp);
  else
    PUTSTR (NULL, fp);
1318

Gerd Moellmann's avatar
Gerd Moellmann committed
1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333
  /* Print flags.  */
  fprintf (fp, "%u", root->flags);
  PUTSTR (root->filename, fp);
  PUTSTR (root->regexp, fp);
  fprintf (fp, "%u", (unsigned) root->pos);
  PUTSTR (root->sfilename, fp);
  putc (']', fp);
  putc ('\n', fp);
}


/* Dump class ROOT and its subclasses to file FP.  Value is the
   number of classes written.  */

int
1334
dump_tree (FILE *fp, struct sym *root)
Gerd Moellmann's avatar
Gerd Moellmann committed
1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380
{
  struct link *lk;
  unsigned n = 0;

  dump_sym (fp, root);

  if (f_verbose)
    {
      putchar ('+');
      fflush (stdout);
    }

  putc ('(', fp);

  for (lk = root->subs; lk; lk = lk->next)
    {
      fputs (TREE_STRUCT, fp);
      n += dump_tree (fp, lk->sym);
      putc (']', fp);
    }

  putc (')', fp);

  dump_members (fp, root->vars);
  n += dump_members (fp, root->fns);
  dump_members (fp, root->static_vars);
  n += dump_members (fp, root->static_fns);
  n += dump_members (fp, root->friends);
  dump_members (fp, root->types);

  /* Superclasses.  */
  putc ('(', fp);
  putc (')', fp);

  /* Mark slot.  */
  putc ('(', fp);
  putc (')', fp);

  putc ('\n', fp);
  return n;
}


/* Dump the entire class tree to file FP.  */

void
1381
dump_roots (FILE *fp)
Gerd Moellmann's avatar
Gerd Moellmann committed
1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454
{
  int i, n = 0;
  struct sym *r;

  /* Output file header containing version string, command line
     options etc.  */
  if (!f_append)
    {
      fputs (TREE_HEADER_STRUCT, fp);
      PUTSTR (EBROWSE_FILE_VERSION, fp);

      putc ('\"', fp);
      if (!f_structs)
	fputs (" -s", fp);
      if (f_regexps)
	fputs (" -x", fp);
      putc ('\"', fp);
      fputs (" ()", fp);
      fputs (" ()", fp);
      putc (']', fp);
    }

  /* Mark functions as virtual that are so because of functions
     declared virtual in base classes.  */
  mark_inherited_virtual ();

  /* Dump the roots of the graph.  */
  for (i = 0; i < TABLE_SIZE; ++i)
    for (r = class_table[i]; r; r = r->next)
      if (!r->supers)
        {
	  fputs (TREE_STRUCT, fp);
          n += dump_tree (fp, r);
	  putc (']', fp);
        }

  if (f_verbose)
    putchar ('\n');
}



/***********************************************************************
				Scanner
 ***********************************************************************/

#ifdef DEBUG
#define INCREMENT_LINENO 			\
do {						\
  if (f_very_verbose)				\
    {						\
      ++yyline;					\
      printf ("%d:\n", yyline);			\
    }						\
  else						\
    ++yyline;					\
} while (0)
#else
#define INCREMENT_LINENO	++yyline
#endif

/* Define two macros for accessing the input buffer (current input
   file).  GET(C) sets C to the next input character and advances the
   input pointer.  UNGET retracts the input pointer.  */

#define GET(C)	((C) = *in++)
#define UNGET() (--in)


/* Process a preprocessor line.  Value is the next character from the
   input buffer not consumed.  */

int
1455
process_pp_line (void)
Gerd Moellmann's avatar
Gerd Moellmann committed
1456
{
1457
  int in_comment = 0, in_string = 0;
Gerd Moellmann's avatar
Gerd Moellmann committed
1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494
  int c;
  char *p = yytext;

  /* Skip over white space.  The `#' has been consumed already.  */
  while (WHITEP (GET (c)))
    ;

  /* Read the preprocessor command (if any).  */
  while (IDENTP (c))
    {
      *p++ = c;
      GET (c);
    }

  /* Is it a `define'?  */
  *p = '\0';

  if (*yytext && streq (yytext, "define"))
    {
      p = yytext;
      while (WHITEP (c))
	GET (c);
      while (IDENTP (c))
	{
	  *p++ = c;
	  GET (c);
	}

      *p = '\0';

      if (*yytext)
	{
	  char *regexp = matching_regexp ();
	  int pos = BUFFER_POS ();
	  add_define (yytext, regexp, pos);
	}
    }
1495

1496
  while (c && (c != '\n' || in_comment || in_string))
Gerd Moellmann's avatar
Gerd Moellmann committed
1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509
    {
      if (c == '\\')
	GET (c);
      else if (c == '/' && !in_comment)
	{
	  if (GET (c) == '*')
	    in_comment = 1;
	}
      else if (c == '*' && in_comment)
	{
	  if (GET (c) == '/')
	    in_comment = 0;
	}
1510 1511
      else if (c == '"')
	in_string = !in_string;
1512

Gerd Moellmann's avatar
Gerd Moellmann committed
1513 1514 1515 1516 1517
      if (c == '\n')
	INCREMENT_LINENO;

      GET (c);
    }
1518

Gerd Moellmann's avatar
Gerd Moellmann committed
1519 1520 1521 1522 1523 1524 1525
  return c;
}


/* Value is the next token from the input buffer.  */

int
1526
yylex (void)
Gerd Moellmann's avatar
Gerd Moellmann committed
1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567
{
  int c;
  char end_char;
  char *p;

  for (;;)
    {
      while (WHITEP (GET (c)))
        ;

      switch (c)
        {
        case '\n':
          INCREMENT_LINENO;
          break;

        case '\r':
          break;

        case 0:
          /* End of file.  */
          return YYEOF;

        case '\\':
          GET (c);
          break;

        case '"':
        case '\'':
          /* String and character constants.  */
          end_char = c;
          string_start = in;
          while (GET (c) && c != end_char)
            {
              switch (c)
                {
                case '\\':
                  /* Escape sequences.  */
                  if (!GET (c))
                    {
                      if (end_char == '\'')
1568
                        yyerror ("EOF in character constant", NULL);
Gerd Moellmann's avatar
Gerd Moellmann committed
1569
                      else
1570
                        yyerror ("EOF in string constant", NULL);
Gerd Moellmann's avatar
Gerd Moellmann committed
1571 1572 1573 1574 1575
                      goto end_string;
                    }
                  else switch (c)
                    {
                    case '\n':
1576
		      INCREMENT_LINENO;
Gerd Moellmann's avatar
Gerd Moellmann committed
1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634
                    case 'a':
                    case 'b':
                    case 'f':
                    case 'n':
                    case 'r':
                    case 't':
                    case 'v':
                      break;

                    case 'x':
                      {
                        /* Hexadecimal escape sequence.  */
                        int i;
                        for (i = 0; i < 2; ++i)
                          {
                            GET (c);

                            if (c >= '0' && c <= '7')
                              ;
                            else if (c >= 'a' && c <= 'f')
                              ;
                            else if (c >= 'A' && c <= 'F')
                              ;
                            else
                              {
                                UNGET ();
                                break;
                              }
                          }
                      }
                      break;

                    case '0':
                      {
                        /* Octal escape sequence.  */
                        int i;
                        for (i = 0; i < 3; ++i)
                          {
                            GET (c);

                            if (c >= '0' && c <= '7')
                              ;
                            else
                              {
                                UNGET ();
                                break;
                              }
                          }
                      }
                      break;

                    default:
                      break;
                    }
                  break;

                case '\n':
                  if (end_char == '\'')
1635
                    yyerror ("newline in character constant", NULL);
Gerd Moellmann's avatar
Gerd Moellmann committed
1636
                  else
1637
                    yyerror ("newline in string constant", NULL);
Gerd Moellmann's avatar
Gerd Moellmann committed
1638
                  INCREMENT_LINENO;
1639
                  break;
Gerd Moellmann's avatar
Gerd Moellmann committed
1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670

                default:
                  break;
                }
            }

        end_string:
          return end_char == '\'' ? CCHAR : CSTRING;

        case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g':
        case 'h': case 'i': case 'j': case 'k': case 'l': case 'm': case 'n':
        case 'o': case 'p': case 'q': case 'r': case 's': case 't': case 'u':
        case 'v': case 'w': case 'x': case 'y': case 'z':
        case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G':
        case 'H': case 'I': case 'J': case 'K': case 'L': case 'M': case 'N':
        case 'O': case 'P': case 'Q': case 'R': case 'S': case 'T': case 'U':
        case 'V': case 'W': case 'X': case 'Y': case 'Z': case '_':
          {
            /* Identifier and keywords.  */
            unsigned hash;
            struct kw *k;

            p = yytext;
            *p++ = hash = c;

            while (IDENTP (GET (*p)))
	      {
		hash = (hash << 1) ^ *p++;
		if (p == yytext_end - 1)
		  {
		    int size = yytext_end - yytext;
1671
		    yytext = (char *) xrealloc (yytext, 2 * size);
Gerd Moellmann's avatar
Gerd Moellmann committed
1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790
		    yytext_end = yytext + 2 * size;
		    p = yytext + size - 1;
		  }
	      }

            UNGET ();
            *p = 0;

            for (k = keyword_table[hash % KEYWORD_TABLE_SIZE]; k; k = k->next)
              if (streq (k->name, yytext))
                return k->tk;

            return IDENT;
          }

        case '/':
          /* C and C++ comments, '/' and '/='.  */
          switch (GET (c))
            {
            case '*':
              while (GET (c))
                {
                  switch (c)
                    {
                    case '*':
                      if (GET (c) == '/')
                        goto comment_end;
                      UNGET ();
                      break;
                    case '\\':
                      GET (c);
                      break;
                    case '\n':
                      INCREMENT_LINENO;
                      break;
                    }
                }
            comment_end:;
              break;

            case '=':
              return DIVASGN;

            case '/':
	      while (GET (c) && c != '\n')
		;
	      INCREMENT_LINENO;
	      break;

            default:
              UNGET ();
              return '/';
            }
          break;

        case '+':
          if (GET (c) == '+')
            return INC;
          else if (c == '=')
            return ADDASGN;
          UNGET ();
          return '+';

        case '-':
          switch (GET (c))
            {
            case '-':
              return DEC;
            case '>':
              if (GET (c) == '*')
                return ARROWSTAR;
              UNGET ();
              return ARROW;
            case '=':
              return SUBASGN;
            }
          UNGET ();
          return '-';

        case '*':
          if (GET (c) == '=')
            return MULASGN;
          UNGET ();
          return '*';

        case '%':
          if (GET (c) == '=')
            return MODASGN;
          UNGET ();
          return '%';

        case '|':
          if (GET (c) == '|')
            return LOR;
          else if (c == '=')
            return ORASGN;
          UNGET ();
          return '|';

        case '&':
          if (GET (c) == '&')
            return LAND;
          else if (c == '=')
            return ANDASGN;
          UNGET ();
          return '&';

        case '^':
          if (GET (c) == '=')
            return XORASGN;
          UNGET ();
          return '^';

        case '.':
          if (GET (c) == '*')
            return POINTSTAR;
          else if (c == '.')
            {
              if (GET (c) != '.')
1791
                yyerror ("invalid token '..' ('...' assumed)", NULL);
Gerd Moellmann's avatar
Gerd Moellmann committed
1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931
              UNGET ();
              return ELLIPSIS;
            }
          else if (!DIGITP (c))
            {
              UNGET ();
              return '.';
            }
          goto mantissa;

        case ':':
          if (GET (c) == ':')
            return DCOLON;
          UNGET ();
          return ':';

        case '=':
          if (GET (c) == '=')
            return EQ;
          UNGET ();
          return '=';

        case '!':
          if (GET (c) == '=')
            return NE;
          UNGET ();
          return '!';

        case '<':
          switch (GET (c))
            {
            case '=':
              return LE;
            case '<':
              if (GET (c) == '=')
                return LSHIFTASGN;
              UNGET ();
              return LSHIFT;
            }
          UNGET ();
          return '<';

        case '>':
          switch (GET (c))
            {
            case '=':
              return GE;
            case '>':
              if (GET (c) == '=')
                return RSHIFTASGN;
              UNGET ();
              return RSHIFT;
            }
          UNGET ();
          return '>';

        case '#':
          c = process_pp_line ();
          if (c == 0)
            return YYEOF;
          break;

        case '(': case ')': case '[': case ']': case '{': case '}':
        case ';': case ',': case '?': case '~':
          return c;

        case '0':
          yyival = 0;

          if (GET (c) == 'x' || c == 'X')
            {
              while (GET (c))
                {
                  if (DIGITP (c))
                    yyival = yyival * 16 + c - '0';
                  else if (c >= 'a' && c <= 'f')
                    yyival = yyival * 16 + c - 'a' + 10;
                  else if (c >= 'A' && c <= 'F')
                    yyival = yyival * 16 + c - 'A' + 10;
                  else
                    break;
                }

              goto int_suffixes;
            }
          else if (c == '.')
            goto mantissa;

          while (c >= '0' && c <= '7')
            {
              yyival = (yyival << 3) + c - '0';
              GET (c);
            }

        int_suffixes:
          /* Integer suffixes.  */
          while (isalpha (c))
            GET (c);
          UNGET ();
          return CINT;

        case '1': case '2': case '3': case '4': case '5': case '6':
        case '7': case '8': case '9':
          /* Integer or floating constant, part before '.'.  */
          yyival = c - '0';

          while (GET (c) && DIGITP (c))
            yyival = 10 * yyival + c - '0';

          if (c != '.')
            goto int_suffixes;

        mantissa:
          /* Digits following '.'.  */
          while (DIGITP (c))
            GET (c);

          /* Optional exponent.  */
          if (c == 'E' || c == 'e')
            {
              if (GET (c) == '-' || c == '+')
                GET (c);

              while (DIGITP (c))
                GET (c);
            }

          /* Optional type suffixes.  */
          while (isalpha (c))
            GET (c);
	  UNGET ();
          return CFLOAT;

        default:
          break;
        }
    }
}


1932 1933 1934 1935 1936 1937
/* Actually local to matching_regexp.  These variables must be in
   global scope for the case that `static' get's defined away.  */

static char *matching_regexp_buffer, *matching_regexp_end_buf;


Gerd Moellmann's avatar
Gerd Moellmann committed
1938 1939 1940 1941 1942
/* Value is the string from the start of the line to the current
   position in the input buffer, or maybe a bit more if that string is
   shorter than min_regexp.  */

char *
1943
matching_regexp (void)
Gerd Moellmann's avatar
Gerd Moellmann committed
1944 1945 1946 1947 1948 1949 1950 1951
{
  char *p;
  char *s;
  char *t;

  if (!f_regexps)
    return NULL;

1952
  if (matching_regexp_buffer == NULL)
Gerd Moellmann's avatar
Gerd Moellmann committed
1953
    {
1954 1955
      matching_regexp_buffer = (char *) xmalloc (max_regexp);
      matching_regexp_end_buf = &matching_regexp_buffer[max_regexp] - 1;
Gerd Moellmann's avatar
Gerd Moellmann committed
1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966
    }

  /* Scan back to previous newline of buffer start.  */
  for (p = in - 1; p > inbuffer && *p != '\n'; --p)
    ;

  if (*p == '\n')
    {
      while (in - p < min_regexp && p > inbuffer)
        {
          /* Line probably not significant enough */
1967
          for (--p; p > inbuffer && *p != '\n'; --p)
Gerd Moellmann's avatar
Gerd Moellmann committed
1968 1969 1970 1971 1972 1973 1974 1975 1976
            ;
        }
      if (*p == '\n')
        ++p;
    }

  /* Copy from end to make sure significant portions are included.
     This implies that in the browser a regular expressing of the form
     `^.*{regexp}' has to be used.  */
1977 1978
  for (s = matching_regexp_end_buf - 1, t = in;
       s > matching_regexp_buffer && t > p;)
Gerd Moellmann's avatar
Gerd Moellmann committed
1979 1980 1981
    {
      *--s = *--t;

1982
      if (*s == '"' || *s == '\\')
Gerd Moellmann's avatar
Gerd Moellmann committed
1983 1984 1985
        *--s = '\\';
    }

1986
  *(matching_regexp_end_buf - 1) = '\0';
Gerd Moellmann's avatar
Gerd Moellmann committed
1987 1988 1989 1990 1991 1992 1993
  return xstrdup (s);
}


/* Return a printable representation of token T.  */

char *
1994
token_string (int t)
Gerd Moellmann's avatar
Gerd Moellmann committed
1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077
{
  static char b[3];

  switch (t)
    {
    case CSTRING:               return "string constant";
    case CCHAR:                 return "char constant";
    case CINT:                  return "int constant";
    case CFLOAT:                return "floating constant";
    case ELLIPSIS:              return "...";
    case LSHIFTASGN:            return "<<=";
    case RSHIFTASGN:            return ">>=";
    case ARROWSTAR:             return "->*";
    case IDENT:                 return "identifier";
    case DIVASGN:               return "/=";
    case INC:                   return "++";
    case ADDASGN:               return "+=";
    case DEC:                   return "--";
    case ARROW:                 return "->";
    case SUBASGN:               return "-=";
    case MULASGN:               return "*=";
    case MODASGN:               return "%=";
    case LOR:                   return "||";
    case ORASGN:                return "|=";
    case LAND:                  return "&&";
    case ANDASGN:               return "&=";
    case XORASGN:               return "^=";
    case POINTSTAR:             return ".*";
    case DCOLON:                return "::";
    case EQ:                    return "==";
    case NE:                    return "!=";
    case LE:                    return "<=";
    case LSHIFT:                return "<<";
    case GE:                    return ">=";
    case RSHIFT:                return ">>";
    case ASM:                   return "asm";
    case AUTO:                  return "auto";
    case BREAK:                 return "break";
    case CASE:                  return "case";
    case CATCH:                 return "catch";
    case CHAR:                  return "char";
    case CLASS:                 return "class";
    case CONST:                 return "const";
    case CONTINUE:              return "continue";
    case DEFAULT:               return "default";
    case DELETE:                return "delete";
    case DO:                    return "do";
    case DOUBLE:                return "double";
    case ELSE:                  return "else";
    case ENUM:                  return "enum";
    case EXTERN:                return "extern";
    case FLOAT:                 return "float";
    case FOR:                   return "for";
    case FRIEND:                return "friend";
    case GOTO:                  return "goto";
    case IF:                    return "if";
    case T_INLINE:              return "inline";
    case INT:                   return "int";
    case LONG:                  return "long";
    case NEW:                   return "new";
    case OPERATOR:              return "operator";
    case PRIVATE:               return "private";
    case PROTECTED:             return "protected";
    case PUBLIC:                return "public";
    case REGISTER:              return "register";
    case RETURN:                return "return";
    case SHORT:                 return "short";
    case SIGNED:                return "signed";
    case SIZEOF:                return "sizeof";
    case STATIC:                return "static";
    case STRUCT:                return "struct";
    case SWITCH:                return "switch";
    case TEMPLATE:              return "template";
    case THIS:                  return "this";
    case THROW:                 return "throw";
    case TRY:                   return "try";
    case TYPEDEF:               return "typedef";
    case UNION:                 return "union";
    case UNSIGNED:              return "unsigned";
    case VIRTUAL:               return "virtual";
    case VOID:                  return "void";
    case VOLATILE:              return "volatile";
    case WHILE:                 return "while";
2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092
    case MUTABLE:		return "mutable";
    case BOOL:			return "bool";
    case TRUE:			return "true";
    case FALSE:			return "false";
    case SIGNATURE:		return "signature";
    case NAMESPACE:		return "namespace";
    case EXPLICIT:		return "explicit";
    case TYPENAME:		return "typename";
    case CONST_CAST:		return "const_cast";
    case DYNAMIC_CAST:		return "dynamic_cast";
    case REINTERPRET_CAST:	return "reinterpret_cast";
    case STATIC_CAST:		return "static_cast";
    case TYPEID:		return "typeid";
    case USING:			return "using";
    case WCHAR:			return "wchar_t";
Gerd Moellmann's avatar
Gerd Moellmann committed
2093 2094
    case YYEOF:                 return "EOF";

2095 2096 2097 2098 2099 2100 2101 2102 2103 2104
    default:
      if (t < 255)
	{
	  b[0] = t;
	  b[1] = '\0';
	  return b;
	}
      else
	return "???";
    }
Gerd Moellmann's avatar
Gerd Moellmann committed
2105 2106 2107