| | @@ -1,2762 +0,0 @@ |
| 1 | | -/*
|
| 2 | | -** This program is free software; you can redistribute it and/or
|
| 3 | | -** modify it under the terms of the Simplified BSD License (also
|
| 4 | | -** known as the "2-Clause License" or "FreeBSD License".)
|
| 5 | | -**
|
| 6 | | -** Copyright 1993 D. Richard Hipp. All rights reserved.
|
| 7 | | -**
|
| 8 | | -** Redistribution and use in source and binary forms, with or
|
| 9 | | -** without modification, are permitted provided that the following
|
| 10 | | -** conditions are met:
|
| 11 | | -**
|
| 12 | | -** 1. Redistributions of source code must retain the above copyright
|
| 13 | | -** notice, this list of conditions and the following disclaimer.
|
| 14 | | -**
|
| 15 | | -** 2. Redistributions in binary form must reproduce the above copyright
|
| 16 | | -** notice, this list of conditions and the following disclaimer in
|
| 17 | | -** the documentation and/or other materials provided with the
|
| 18 | | -** distribution.
|
| 19 | | -**
|
| 20 | | -** This software is provided "as is" and any express or implied warranties,
|
| 21 | | -** including, but not limited to, the implied warranties of merchantability
|
| 22 | | -** and fitness for a particular purpose are disclaimed. In no event shall
|
| 23 | | -** the author or contributors be liable for any direct, indirect, incidental,
|
| 24 | | -** special, exemplary, or consequential damages (including, but not limited
|
| 25 | | -** to, procurement of substitute goods or services; loss of use, data or
|
| 26 | | -** profits; or business interruption) however caused and on any theory of
|
| 27 | | -** liability, whether in contract, strict liability, or tort (including
|
| 28 | | -** negligence or otherwise) arising in any way out of the use of this
|
| 29 | | -** software, even if advised of the possibility of such damage.
|
| 30 | | -**
|
| 31 | | -** This program is distributed in the hope that it will be useful,
|
| 32 | | -** but without any warranty; without even the implied warranty of
|
| 33 | | -** merchantability or fitness for a particular purpose.
|
| 34 | | -*/
|
| 35 | | -#include <stdio.h>
|
| 36 | | -#include <stdlib.h>
|
| 37 | | -#include <ctype.h>
|
| 38 | | -#include <memory.h>
|
| 39 | | -#include <sys/stat.h>
|
| 40 | | -#include <assert.h>
|
| 41 | | -#include <string.h>
|
| 42 | | -
|
| 43 | | -#if defined( __MINGW32__) || defined(__DMC__) || defined(_MSC_VER) || defined(__POCC__)
|
| 44 | | -# ifndef WIN32
|
| 45 | | -# define WIN32
|
| 46 | | -# endif
|
| 47 | | -#else
|
| 48 | | -# include <unistd.h>
|
| 49 | | -#endif
|
| 50 | | -
|
| 51 | | -/*
|
| 52 | | -** Macros for debugging.
|
| 53 | | -*/
|
| 54 | | -#ifdef DEBUG
|
| 55 | | -static int debugMask = 0;
|
| 56 | | -# define debug0(F,M) if( (F)&debugMask ){ fprintf(stderr,M); }
|
| 57 | | -# define debug1(F,M,A) if( (F)&debugMask ){ fprintf(stderr,M,A); }
|
| 58 | | -# define debug2(F,M,A,B) if( (F)&debugMask ){ fprintf(stderr,M,A,B); }
|
| 59 | | -# define debug3(F,M,A,B,C) if( (F)&debugMask ){ fprintf(stderr,M,A,B,C); }
|
| 60 | | -# define PARSER 0x00000001
|
| 61 | | -# define DECL_DUMP 0x00000002
|
| 62 | | -# define TOKENIZER 0x00000004
|
| 63 | | -#else
|
| 64 | | -# define debug0(Flags, Format)
|
| 65 | | -# define debug1(Flags, Format, A)
|
| 66 | | -# define debug2(Flags, Format, A, B)
|
| 67 | | -# define debug3(Flags, Format, A, B, C)
|
| 68 | | -#endif
|
| 69 | | -
|
| 70 | | -/*
|
| 71 | | -** The following macros are purely for the purpose of testing this
|
| 72 | | -** program on itself. They don't really contribute to the code.
|
| 73 | | -*/
|
| 74 | | -#define INTERFACE 1
|
| 75 | | -#define EXPORT_INTERFACE 1
|
| 76 | | -#define EXPORT
|
| 77 | | -
|
| 78 | | -/*
|
| 79 | | -** Each token in a source file is represented by an instance of
|
| 80 | | -** the following structure. Tokens are collected onto a list.
|
| 81 | | -*/
|
| 82 | | -typedef struct Token Token;
|
| 83 | | -struct Token {
|
| 84 | | - const char *zText; /* The text of the token */
|
| 85 | | - int nText; /* Number of characters in the token's text */
|
| 86 | | - int eType; /* The type of this token */
|
| 87 | | - int nLine; /* The line number on which the token starts */
|
| 88 | | - Token *pComment; /* Most recent block comment before this token */
|
| 89 | | - Token *pNext; /* Next token on the list */
|
| 90 | | - Token *pPrev; /* Previous token on the list */
|
| 91 | | -};
|
| 92 | | -
|
| 93 | | -/*
|
| 94 | | -** During tokenization, information about the state of the input
|
| 95 | | -** stream is held in an instance of the following structure
|
| 96 | | -*/
|
| 97 | | -typedef struct InStream InStream;
|
| 98 | | -struct InStream {
|
| 99 | | - const char *z; /* Complete text of the input */
|
| 100 | | - int i; /* Next character to read from the input */
|
| 101 | | - int nLine; /* The line number for character z[i] */
|
| 102 | | -};
|
| 103 | | -
|
| 104 | | -/*
|
| 105 | | -** Each declaration in the C or C++ source files is parsed out and stored as
|
| 106 | | -** an instance of the following structure.
|
| 107 | | -**
|
| 108 | | -** A "forward declaration" is a declaration that an object exists that
|
| 109 | | -** doesn't tell about the objects structure. A typical forward declaration
|
| 110 | | -** is:
|
| 111 | | -**
|
| 112 | | -** struct Xyzzy;
|
| 113 | | -**
|
| 114 | | -** Not every object has a forward declaration. If it does, thought, the
|
| 115 | | -** forward declaration will be contained in the zFwd field for C and
|
| 116 | | -** the zFwdCpp for C++. The zDecl field contains the complete
|
| 117 | | -** declaration text.
|
| 118 | | -*/
|
| 119 | | -typedef struct Decl Decl;
|
| 120 | | -struct Decl {
|
| 121 | | - char *zName; /* Name of the object being declared. The appearance
|
| 122 | | - ** of this name is a source file triggers the declaration
|
| 123 | | - ** to be added to the header for that file. */
|
| 124 | | - const char *zFile; /* File from which extracted. */
|
| 125 | | - char *zIf; /* Surround the declaration with this #if */
|
| 126 | | - char *zFwd; /* A forward declaration. NULL if there is none. */
|
| 127 | | - char *zFwdCpp; /* Use this forward declaration for C++. */
|
| 128 | | - char *zDecl; /* A full declaration of this object */
|
| 129 | | - char *zExtra; /* Extra declaration text inserted into class objects */
|
| 130 | | - int extraType; /* Last public:, protected: or private: in zExtraDecl */
|
| 131 | | - struct Include *pInclude; /* #includes that come before this declaration */
|
| 132 | | - int flags; /* See the "Properties" below */
|
| 133 | | - Token *pComment; /* A block comment associated with this declaration */
|
| 134 | | - Token tokenCode; /* Implementation of functions and procedures */
|
| 135 | | - Decl *pSameName; /* Next declaration with the same "zName" */
|
| 136 | | - Decl *pSameHash; /* Next declaration with same hash but different zName */
|
| 137 | | - Decl *pNext; /* Next declaration with a different name */
|
| 138 | | -};
|
| 139 | | -
|
| 140 | | -/*
|
| 141 | | -** Properties associated with declarations.
|
| 142 | | -**
|
| 143 | | -** DP_Forward and DP_Declared are used during the generation of a single
|
| 144 | | -** header file in order to prevent duplicate declarations and definitions.
|
| 145 | | -** DP_Forward is set after the object has been given a forward declaration
|
| 146 | | -** and DP_Declared is set after the object gets a full declarations.
|
| 147 | | -** (Example: A forward declaration is "typedef struct Abc Abc;" and the
|
| 148 | | -** full declaration is "struct Abc { int a; float b; };".)
|
| 149 | | -**
|
| 150 | | -** The DP_Export and DP_Local flags are more permanent. They mark objects
|
| 151 | | -** that have EXPORT scope and LOCAL scope respectively. If both of these
|
| 152 | | -** marks are missing, then the object has library scope. The meanings of
|
| 153 | | -** the scopes are as follows:
|
| 154 | | -**
|
| 155 | | -** LOCAL scope The object is only usable within the file in
|
| 156 | | -** which it is declared.
|
| 157 | | -**
|
| 158 | | -** library scope The object is visible and usable within other
|
| 159 | | -** files in the same project. By if the project is
|
| 160 | | -** a library, then the object is not visible to users
|
| 161 | | -** of the library. (i.e. the object does not appear
|
| 162 | | -** in the output when using the -H option.)
|
| 163 | | -**
|
| 164 | | -** EXPORT scope The object is visible and usable everywhere.
|
| 165 | | -**
|
| 166 | | -** The DP_Flag is a temporary use flag that is used during processing to
|
| 167 | | -** prevent an infinite loop. It's use is localized.
|
| 168 | | -**
|
| 169 | | -** The DP_Cplusplus, DP_ExternCReqd and DP_ExternReqd flags are permanent
|
| 170 | | -** and are used to specify what type of declaration the object requires.
|
| 171 | | -*/
|
| 172 | | -#define DP_Forward 0x001 /* Has a forward declaration in this file */
|
| 173 | | -#define DP_Declared 0x002 /* Has a full declaration in this file */
|
| 174 | | -#define DP_Export 0x004 /* Export this declaration */
|
| 175 | | -#define DP_Local 0x008 /* Declare in its home file only */
|
| 176 | | -#define DP_Flag 0x010 /* Use to mark a subset of a Decl list
|
| 177 | | - ** for special processing */
|
| 178 | | -#define DP_Cplusplus 0x020 /* Has C++ linkage and cannot appear in a
|
| 179 | | - ** C header file */
|
| 180 | | -#define DP_ExternCReqd 0x040 /* Prepend 'extern "C"' in a C++ header.
|
| 181 | | - ** Prepend nothing in a C header */
|
| 182 | | -#define DP_ExternReqd 0x080 /* Prepend 'extern "C"' in a C++ header if
|
| 183 | | - ** DP_Cplusplus is not also set. If DP_Cplusplus
|
| 184 | | - ** is set or this is a C header then
|
| 185 | | - ** prepend 'extern' */
|
| 186 | | -
|
| 187 | | -/*
|
| 188 | | -** Convenience macros for dealing with declaration properties
|
| 189 | | -*/
|
| 190 | | -#define DeclHasProperty(D,P) (((D)->flags&(P))==(P))
|
| 191 | | -#define DeclHasAnyProperty(D,P) (((D)->flags&(P))!=0)
|
| 192 | | -#define DeclSetProperty(D,P) (D)->flags |= (P)
|
| 193 | | -#define DeclClearProperty(D,P) (D)->flags &= ~(P)
|
| 194 | | -
|
| 195 | | -/*
|
| 196 | | -** These are state properties of the parser. Each of the values is
|
| 197 | | -** distinct from the DP_ values above so that both can be used in
|
| 198 | | -** the same "flags" field.
|
| 199 | | -**
|
| 200 | | -** Be careful not to confuse PS_Export with DP_Export or
|
| 201 | | -** PS_Local with DP_Local. Their names are similar, but the meanings
|
| 202 | | -** of these flags are very different.
|
| 203 | | -*/
|
| 204 | | -#define PS_Extern 0x000800 /* "extern" has been seen */
|
| 205 | | -#define PS_Export 0x001000 /* If between "#if EXPORT_INTERFACE"
|
| 206 | | - ** and "#endif" */
|
| 207 | | -#define PS_Export2 0x002000 /* If "EXPORT" seen */
|
| 208 | | -#define PS_Typedef 0x004000 /* If "typedef" has been seen */
|
| 209 | | -#define PS_Static 0x008000 /* If "static" has been seen */
|
| 210 | | -#define PS_Interface 0x010000 /* If within #if INTERFACE..#endif */
|
| 211 | | -#define PS_Method 0x020000 /* If "::" token has been seen */
|
| 212 | | -#define PS_Local 0x040000 /* If within #if LOCAL_INTERFACE..#endif */
|
| 213 | | -#define PS_Local2 0x080000 /* If "LOCAL" seen. */
|
| 214 | | -#define PS_Public 0x100000 /* If "PUBLIC" seen. */
|
| 215 | | -#define PS_Protected 0x200000 /* If "PROTECTED" seen. */
|
| 216 | | -#define PS_Private 0x400000 /* If "PRIVATE" seen. */
|
| 217 | | -#define PS_PPP 0x700000 /* If any of PUBLIC, PRIVATE, PROTECTED */
|
| 218 | | -
|
| 219 | | -/*
|
| 220 | | -** The following set of flags are ORed into the "flags" field of
|
| 221 | | -** a Decl in order to identify what type of object is being
|
| 222 | | -** declared.
|
| 223 | | -*/
|
| 224 | | -#define TY_Class 0x00100000
|
| 225 | | -#define TY_Subroutine 0x00200000
|
| 226 | | -#define TY_Macro 0x00400000
|
| 227 | | -#define TY_Typedef 0x00800000
|
| 228 | | -#define TY_Variable 0x01000000
|
| 229 | | -#define TY_Structure 0x02000000
|
| 230 | | -#define TY_Union 0x04000000
|
| 231 | | -#define TY_Enumeration 0x08000000
|
| 232 | | -#define TY_Defunct 0x10000000 /* Used to erase a declaration */
|
| 233 | | -
|
| 234 | | -/*
|
| 235 | | -** Each nested #if (or #ifdef or #ifndef) is stored in a stack of
|
| 236 | | -** instances of the following structure.
|
| 237 | | -*/
|
| 238 | | -typedef struct Ifmacro Ifmacro;
|
| 239 | | -struct Ifmacro {
|
| 240 | | - int nLine; /* Line number where this macro occurs */
|
| 241 | | - char *zCondition; /* Text of the condition for this macro */
|
| 242 | | - Ifmacro *pNext; /* Next down in the stack */
|
| 243 | | - int flags; /* Can hold PS_Export, PS_Interface or PS_Local flags */
|
| 244 | | -};
|
| 245 | | -
|
| 246 | | -/*
|
| 247 | | -** When parsing a file, we need to keep track of what other files have
|
| 248 | | -** be #include-ed. For each #include found, we create an instance of
|
| 249 | | -** the following structure.
|
| 250 | | -*/
|
| 251 | | -typedef struct Include Include;
|
| 252 | | -struct Include {
|
| 253 | | - char *zFile; /* The name of file include. Includes "" or <> */
|
| 254 | | - char *zIf; /* If not NULL, #include should be enclosed in #if */
|
| 255 | | - char *zLabel; /* A unique label used to test if this #include has
|
| 256 | | - * appeared already in a file or not */
|
| 257 | | - Include *pNext; /* Previous include file, or NULL if this is the first */
|
| 258 | | -};
|
| 259 | | -
|
| 260 | | -/*
|
| 261 | | -** Identifiers found in a source file that might be used later to provoke
|
| 262 | | -** the copying of a declaration into the corresponding header file are
|
| 263 | | -** stored in a hash table as instances of the following structure.
|
| 264 | | -*/
|
| 265 | | -typedef struct Ident Ident;
|
| 266 | | -struct Ident {
|
| 267 | | - char *zName; /* The text of this identifier */
|
| 268 | | - Ident *pCollide; /* Next identifier with the same hash */
|
| 269 | | - Ident *pNext; /* Next identifier in a list of them all */
|
| 270 | | -};
|
| 271 | | -
|
| 272 | | -/*
|
| 273 | | -** A complete table of identifiers is stored in an instance of
|
| 274 | | -** the next structure.
|
| 275 | | -*/
|
| 276 | | -#define IDENT_HASH_SIZE 2237
|
| 277 | | -typedef struct IdentTable IdentTable;
|
| 278 | | -struct IdentTable {
|
| 279 | | - Ident *pList; /* List of all identifiers in this table */
|
| 280 | | - Ident *apTable[IDENT_HASH_SIZE]; /* The hash table */
|
| 281 | | -};
|
| 282 | | -
|
| 283 | | -/*
|
| 284 | | -** The following structure holds all information for a single
|
| 285 | | -** source file named on the command line of this program.
|
| 286 | | -*/
|
| 287 | | -typedef struct InFile InFile;
|
| 288 | | -struct InFile {
|
| 289 | | - char *zSrc; /* Name of input file */
|
| 290 | | - char *zHdr; /* Name of the generated .h file for this input.
|
| 291 | | - ** Will be NULL if input is to be scanned only */
|
| 292 | | - int flags; /* One or more DP_, PS_ and/or TY_ flags */
|
| 293 | | - InFile *pNext; /* Next input file in the list of them all */
|
| 294 | | - IdentTable idTable; /* All identifiers in this input file */
|
| 295 | | -};
|
| 296 | | -
|
| 297 | | -/*
|
| 298 | | -** An unbounded string is able to grow without limit. We use these
|
| 299 | | -** to construct large in-memory strings from lots of smaller components.
|
| 300 | | -*/
|
| 301 | | -typedef struct String String;
|
| 302 | | -struct String {
|
| 303 | | - int nAlloc; /* Number of bytes allocated */
|
| 304 | | - int nUsed; /* Number of bytes used (not counting nul terminator) */
|
| 305 | | - char *zText; /* Text of the string */
|
| 306 | | -};
|
| 307 | | -
|
| 308 | | -/*
|
| 309 | | -** The following structure contains a lot of state information used
|
| 310 | | -** while generating a .h file. We put the information in this structure
|
| 311 | | -** and pass around a pointer to this structure, rather than pass around
|
| 312 | | -** all of the information separately. This helps reduce the number of
|
| 313 | | -** arguments to generator functions.
|
| 314 | | -*/
|
| 315 | | -typedef struct GenState GenState;
|
| 316 | | -struct GenState {
|
| 317 | | - String *pStr; /* Write output to this string */
|
| 318 | | - IdentTable *pTable; /* A table holding the zLabel of every #include that
|
| 319 | | - * has already been generated. Used to avoid
|
| 320 | | - * generating duplicate #includes. */
|
| 321 | | - const char *zIf; /* If not NULL, then we are within a #if with
|
| 322 | | - * this argument. */
|
| 323 | | - int nErr; /* Number of errors */
|
| 324 | | - const char *zFilename; /* Name of the source file being scanned */
|
| 325 | | - int flags; /* Various flags (DP_ and PS_ flags above) */
|
| 326 | | -};
|
| 327 | | -
|
| 328 | | -/*
|
| 329 | | -** The following text line appears at the top of every file generated
|
| 330 | | -** by this program. By recognizing this line, the program can be sure
|
| 331 | | -** never to read a file that it generated itself.
|
| 332 | | -**
|
| 333 | | -** The "#undef INTERFACE" part is a hack to work around a name collision
|
| 334 | | -** in MSVC 2008.
|
| 335 | | -*/
|
| 336 | | -const char zTopLine[] =
|
| 337 | | - "/* \aThis file was automatically generated. Do not edit! */\n"
|
| 338 | | - "#undef INTERFACE\n";
|
| 339 | | -#define nTopLine (sizeof(zTopLine)-1)
|
| 340 | | -
|
| 341 | | -/*
|
| 342 | | -** The name of the file currently being parsed.
|
| 343 | | -*/
|
| 344 | | -static const char *zFilename;
|
| 345 | | -
|
| 346 | | -/*
|
| 347 | | -** The stack of #if macros for the file currently being parsed.
|
| 348 | | -*/
|
| 349 | | -static Ifmacro *ifStack = 0;
|
| 350 | | -
|
| 351 | | -/*
|
| 352 | | -** A list of all files that have been #included so far in a file being
|
| 353 | | -** parsed.
|
| 354 | | -*/
|
| 355 | | -static Include *includeList = 0;
|
| 356 | | -
|
| 357 | | -/*
|
| 358 | | -** The last block comment seen.
|
| 359 | | -*/
|
| 360 | | -static Token *blockComment = 0;
|
| 361 | | -
|
| 362 | | -/*
|
| 363 | | -** The following flag is set if the -doc flag appears on the
|
| 364 | | -** command line.
|
| 365 | | -*/
|
| 366 | | -static int doc_flag = 0;
|
| 367 | | -
|
| 368 | | -/*
|
| 369 | | -** If the following flag is set, then makeheaders will attempt to
|
| 370 | | -** generate prototypes for static functions and procedures.
|
| 371 | | -*/
|
| 372 | | -static int proto_static = 0;
|
| 373 | | -
|
| 374 | | -/*
|
| 375 | | -** A list of all declarations. The list is held together using the
|
| 376 | | -** pNext field of the Decl structure.
|
| 377 | | -*/
|
| 378 | | -static Decl *pDeclFirst; /* First on the list */
|
| 379 | | -static Decl *pDeclLast; /* Last on the list */
|
| 380 | | -
|
| 381 | | -/*
|
| 382 | | -** A hash table of all declarations
|
| 383 | | -*/
|
| 384 | | -#define DECL_HASH_SIZE 3371
|
| 385 | | -static Decl *apTable[DECL_HASH_SIZE];
|
| 386 | | -
|
| 387 | | -/*
|
| 388 | | -** The TEST macro must be defined to something. Make sure this is the
|
| 389 | | -** case.
|
| 390 | | -*/
|
| 391 | | -#ifndef TEST
|
| 392 | | -# define TEST 0
|
| 393 | | -#endif
|
| 394 | | -
|
| 395 | | -#ifdef NOT_USED
|
| 396 | | -/*
|
| 397 | | -** We do our own assertion macro so that we can have more control
|
| 398 | | -** over debugging.
|
| 399 | | -*/
|
| 400 | | -#define Assert(X) if(!(X)){ CantHappen(__LINE__); }
|
| 401 | | -#define CANT_HAPPEN CantHappen(__LINE__)
|
| 402 | | -static void CantHappen(int iLine){
|
| 403 | | - fprintf(stderr,"Assertion failed on line %d\n",iLine);
|
| 404 | | - *(char*)1 = 0; /* Force a core-dump */
|
| 405 | | -}
|
| 406 | | -#endif
|
| 407 | | -
|
| 408 | | -/*
|
| 409 | | -** Memory allocation functions that are guaranteed never to return NULL.
|
| 410 | | -*/
|
| 411 | | -static void *SafeMalloc(int nByte){
|
| 412 | | - void *p = malloc( nByte );
|
| 413 | | - if( p==0 ){
|
| 414 | | - fprintf(stderr,"Out of memory. Can't allocate %d bytes.\n",nByte);
|
| 415 | | - exit(1);
|
| 416 | | - }
|
| 417 | | - return p;
|
| 418 | | -}
|
| 419 | | -static void SafeFree(void *pOld){
|
| 420 | | - if( pOld ){
|
| 421 | | - free(pOld);
|
| 422 | | - }
|
| 423 | | -}
|
| 424 | | -static void *SafeRealloc(void *pOld, int nByte){
|
| 425 | | - void *p;
|
| 426 | | - if( pOld==0 ){
|
| 427 | | - p = SafeMalloc(nByte);
|
| 428 | | - }else{
|
| 429 | | - p = realloc(pOld, nByte);
|
| 430 | | - if( p==0 ){
|
| 431 | | - fprintf(stderr,
|
| 432 | | - "Out of memory. Can't enlarge an allocation to %d bytes\n",nByte);
|
| 433 | | - exit(1);
|
| 434 | | - }
|
| 435 | | - }
|
| 436 | | - return p;
|
| 437 | | -}
|
| 438 | | -static char *StrDup(const char *zSrc, int nByte){
|
| 439 | | - char *zDest;
|
| 440 | | - if( nByte<=0 ){
|
| 441 | | - nByte = strlen(zSrc);
|
| 442 | | - }
|
| 443 | | - zDest = SafeMalloc( nByte + 1 );
|
| 444 | | - strncpy(zDest,zSrc,nByte);
|
| 445 | | - zDest[nByte] = 0;
|
| 446 | | - return zDest;
|
| 447 | | -}
|
| 448 | | -
|
| 449 | | -/*
|
| 450 | | -** Return TRUE if the character X can be part of an identifier
|
| 451 | | -*/
|
| 452 | | -#define ISALNUM(X) ((X)=='_' || isalnum(X))
|
| 453 | | -
|
| 454 | | -/*
|
| 455 | | -** Routines for dealing with unbounded strings.
|
| 456 | | -*/
|
| 457 | | -static void StringInit(String *pStr){
|
| 458 | | - pStr->nAlloc = 0;
|
| 459 | | - pStr->nUsed = 0;
|
| 460 | | - pStr->zText = 0;
|
| 461 | | -}
|
| 462 | | -static void StringReset(String *pStr){
|
| 463 | | - SafeFree(pStr->zText);
|
| 464 | | - StringInit(pStr);
|
| 465 | | -}
|
| 466 | | -static void StringAppend(String *pStr, const char *zText, int nByte){
|
| 467 | | - if( nByte<=0 ){
|
| 468 | | - nByte = strlen(zText);
|
| 469 | | - }
|
| 470 | | - if( pStr->nUsed + nByte >= pStr->nAlloc ){
|
| 471 | | - if( pStr->nAlloc==0 ){
|
| 472 | | - pStr->nAlloc = nByte + 100;
|
| 473 | | - pStr->zText = SafeMalloc( pStr->nAlloc );
|
| 474 | | - }else{
|
| 475 | | - pStr->nAlloc = pStr->nAlloc*2 + nByte;
|
| 476 | | - pStr->zText = SafeRealloc(pStr->zText, pStr->nAlloc);
|
| 477 | | - }
|
| 478 | | - }
|
| 479 | | - strncpy(&pStr->zText[pStr->nUsed],zText,nByte);
|
| 480 | | - pStr->nUsed += nByte;
|
| 481 | | - pStr->zText[pStr->nUsed] = 0;
|
| 482 | | -}
|
| 483 | | -#define StringGet(S) ((S)->zText?(S)->zText:"")
|
| 484 | | -
|
| 485 | | -/*
|
| 486 | | -** Compute a hash on a string. The number returned is a non-negative
|
| 487 | | -** value between 0 and 2**31 - 1
|
| 488 | | -*/
|
| 489 | | -static int Hash(const char *z, int n){
|
| 490 | | - int h = 0;
|
| 491 | | - if( n<=0 ){
|
| 492 | | - n = strlen(z);
|
| 493 | | - }
|
| 494 | | - while( n-- ){
|
| 495 | | - h = h ^ (h & 0x7fffffff; free software; are; you can redistribute it and/or
|
| 496 | | -** modify it under the terms of the Simplified BSD License (also
|
| 497 | | -** known as the "2-Clause License" or "FreeBSD License".)
|
| 498 | | -**
|
| 499 | | -** Copyright 1993 D. Richard Hipp. All rights reserved.
|
| 500 | | -**
|
| 501 | | -** Redistribution and use in source and binary forms, with or
|
| 502 | | -** without modification, are permitted provided that the following
|
| 503 | | -** conditions are met:
|
| 504 | | -**
|
| 505 | | -** 1. Redistributions of source code must retain the above copyright
|
| 506 | | -** notice, this list of conditions and the following disclaimer.
|
| 507 | | -**
|
| 508 | | -** 2. Redistributions in binary form must reproduce the above copyright
|
| 509 | | -** notice, thisprintf(pDecl/*
|
| 510 | | -** This program is free software; you can redistribute it and/or
|
| 511 | | -** modify it under the terms of the Simplified BSD License (also
|
| 512 | | -** known as the "2-Clause License" or "FreeBSD License".)
|
| 513 | | -**
|
| 514 | | -** Copyright 1993 D. Richard Hipp. All rights reserved.
|
| 515 | | -**
|
| 516 | | -** Redistribution and use in source and binary forms, with or
|
| 517 | | -** without modification, are permitted provided that the following
|
| 518 | | -** conditions are met:
|
| 519 | | -**
|
| 520 | | -** 1. Redistributions of source code must retain the above copyright
|
| 521 | | -** notice, this list of conditions and the following disclaimer.
|
| 522 | | -**
|
| 523 | | -** 2. Redistributions in binary form must reproduce the above copyright
|
| 524 | | -** notice, this list of conditions and the following disclaimer in
|
| 525 | | -** the documentation and/or other materials provided with the
|
| 526 | | -** distribution.
|
| 527 | | -**
|
| 528 | | -** This software is provided "as is" and any express or implied warranties,
|
| 529 | | -** including, but not limited to, the implied warranties of merchantability
|
| 530 | | -** and fitness for a particular purpose are disclaimed. In no event shall
|
| 531 | | -** the author or contributors be liable for any direct, indirect, incidental,
|
| 532 | | -** special, exemplary, or consequential damages (including, but not limited
|
| 533 | | -** to, procurement of substitute goods or services; loss of use, data or
|
| 534 | | -** profits; or business interruption) however caused and on any theory of
|
| 535 | | -** liability, whether in contract, strict liability, or tort (including
|
| 536 | | -** negligence or otherwise) arising in any way out of the use of this
|
| 537 | | -** software, even if advised of the possibility of such damage.
|
| 538 | | -**
|
| 539 | | -** This program is distributed in the hope that it will be useful,
|
| 540 | | -** but without any warranty; without even the implied warranty of
|
| 541 | | -** merchantability or fitness for a particular purpose.
|
| 542 | | -*/
|
| 543 | | -#include <stdio.h>
|
| 544 | | -#include <stdlib.h>
|
| 545 | | -#include <ctype.h>
|
| 546 | | -#include <memory.h>
|
| 547 | | -#include <sys/stat.h>
|
| 548 | | -#include <assert.h>
|
| 549 | | -#include <string.h>
|
| 550 | | -
|
| 551 | | -#if defined( __MINGW32__) || defined(__DMC__) || defined(_MSC_VER) || defined(__POCC__)
|
| 552 | | -# ifndef WIN32
|
| 553 | | -# define WIN32
|
| 554 | | -# endif
|
| 555 | | -#else
|
| 556 | | -# include <unistd.h>
|
| 557 | | -#endif
|
| 558 | | -
|
| 559 | | -/*
|
| 560 | | -** Macros for debugging.
|
| 561 | | -*/
|
| 562 | | -#ifdef DEBUG
|
| 563 | | -static int debugMask = 0;
|
| 564 | | -# define debug0(F,M) if( (F)&debugMask ){ fprintf(stderr,M); }
|
| 565 | | -# define debug1(F,M,A) if( (F)&debugMask ){ fprintf(stderr,M,A); }
|
| 566 | | -# define debug2(F,M,A,B) if( (F)&debugMask ){ fprintf(stderr,M,A,B); }
|
| 567 | | -# define debug3(F,M,A,B,C) if( (F)&debugMask ){ fprintf(stderr,M,A,B,C); }
|
| 568 | | -# define PARSER 0x00000001
|
| 569 | | -# define DECL_DUMP 0x00000002
|
| 570 | | -# define TOKENIZER 0x00000004
|
| 571 | | -#else
|
| 572 | | -# define debug0(Flags, Format)
|
| 573 | | -# define debug1(Flags, Format, A)
|
| 574 | | -# define debug2(Flags, Format, A, B)
|
| 575 | | -# define debug3(Flags, Format, A, B, C)
|
| 576 | | -#endif
|
| 577 | | -
|
| 578 | | -/*
|
| 579 | | -** The following macros are purely for the purpose of testing this
|
| 580 | | -** program on itself. They don't really contribute to the code.
|
| 581 | | -*/
|
| 582 | | -#define INTERFACE 1
|
| 583 | | -#define EXPORT_INTERFACE 1
|
| 584 | | -#define EXPORT
|
| 585 | | -
|
| 586 | | -/*
|
| 587 | | -** Each token in a source file is represented by an instance of
|
| 588 | | -** the following structure. Tokens are collected onto a list.
|
| 589 | | -*/
|
| 590 | | -typedef struct Token Token;
|
| 591 | | -struct Token {
|
| 592 | | - const char *zText; /* The text of the token */
|
| 593 | | - int nText; /* Number of characters in the token's text */
|
| 594 | | - int eType; /* The type of this token */
|
| 595 | | - int nLine; /* The line number on which the token starts */
|
| 596 | | - Token *pComment; /* Most recent block comment before this token */
|
| 597 | | - Token *pNext; /* Next token on the list */
|
| 598 | | - Token *pPrev; /* Previous token on the list */
|
| 599 | | -};
|
| 600 | | -
|
| 601 | | -/*
|
| 602 | | -** During tokenization, information about the state of the input
|
| 603 | | -** stream is held in an instance of the following structure
|
| 604 | | -*/
|
| 605 | | -typedef struct InStream InStream;
|
| 606 | | -struct InStream {
|
| 607 | | - const char *z; /* Complete text of the input */
|
| 608 | | - int i; /* Next character to read from the input */
|
| 609 | | - int nLine; /* The line number for character z[i] */
|
| 610 | | -};
|
| 611 | | -
|
| 612 | | -/*
|
| 613 | | -** Each declaration in the C or C++ source files is parsed out and stored as
|
| 614 | | -** an instance of the following structure.
|
| 615 | | -**
|
| 616 | | -** A "forward declaration" is a declaration that an object exists that
|
| 617 | | -** doesn't tell about the objects structure. A typical forward declaration
|
| 618 | | -** is:
|
| 619 | | -**
|
| 620 | | -** struct Xyzzy;
|
| 621 | | -**
|
| 622 | | -** Not every object has a forward declaration. If it does, thought, the
|
| 623 | | -** forward declaration will be contained in the zFwd field for C and
|
| 624 | | -** the zFwdCpp for C++. The zDecl field contains the complete
|
| 625 | | -** declaration text.
|
| 626 | | -*/
|
| 627 | | -typedef struct Decl Decl;
|
| 628 | | -struct Decl {
|
| 629 | | - char *zName; /* Name of the object being declared. The appearance
|
| 630 | | - ** of this name is a source file triggers the declaration
|
| 631 | | - ** to be added to the header for that file. */
|
| 632 | | - const char *zFile; /* File from which extracted. */
|
| 633 | | - char *zIf; /* Surround the declaration with this #if */
|
| 634 | | - char *zFwd; /* A forward declaration. NULL if there is none. */
|
| 635 | | - char *zFwdCpp; /* Use this forward declaration for C++. */
|
| 636 | | - char *zDecl; /* A full declaration of this object */
|
| 637 | | - char *zExtra; /* Extra declaration text inserted into class objects */
|
| 638 | | - int extraType; /* Last public:, protected: or private: in zExtraDecl */
|
| 639 | | - struct Include *pInclude; /* #includes that come before this declaration */
|
| 640 | | - int flags; /* See the "Properties" below */
|
| 641 | | - Token *pComment; /* A block comment associated with this declaration */
|
| 642 | | - Token tokenCode; /* Implementation of functions and procedures */
|
| 643 | | - Decl *pSameName; /* Next declaration with the same "zName" */
|
| 644 | | - Decl *pSameHash; /* Next declaration with same hash but different zName */
|
| 645 | | - Decl *pNext; /* Next declaration with a different name */
|
| 646 | | -};
|
| 647 | | -
|
| 648 | | -/*
|
| 649 | | -** Properties associated with declarations.
|
| 650 | | -**
|
| 651 | | -** DP_Forward and DP_Declared are used during the generation of a single
|
| 652 | | -** header file in order to prevent duplicate declarations and definitions.
|
| 653 | | -** DP_Forward is set after the object has been given a forward declaration
|
| 654 | | -** and DP_Declared is set after the object gets a full declarations.
|
| 655 | | -** (Example: A forward declaration is "typedef struct Abc Abc;" and the
|
| 656 | | -** full declaration is "struct Abc { int a; float b; };".)
|
| 657 | | -**
|
| 658 | | -** The DP_Export and DP_Local flags are more permanent. They mark objects
|
| 659 | | -** that have EXPORT scope and LOCAL scope respectively. If both of these
|
| 660 | | -** marks are missing, then the object has library scope. The meanings of
|
| 661 | | -** the scopes are as follows:
|
| 662 | | -**
|
| 663 | | -** LOCAL scope The object is only usable within the file in
|
| 664 | | -** which it is declared.
|
| 665 | | -**
|
| 666 | | -** library scope The object is visible and usable within other
|
| 667 | | -** files in the same project. By if the project is
|
| 668 | | -** a library, then the object is not visible to users
|
| 669 | | -** of the library. (i.e. the object does not appear
|
| 670 | | -** in the output when using the -H option.)
|
| 671 | | -**
|
| 672 | | -** EXPORT scope The object is visible and usable everywhere.
|
| 673 | | -**
|
| 674 | | -** The DP_Flag is a temporary use flag that is used during processing to
|
| 675 | | -** prevent an infinite loop. It's use is localized.
|
| 676 | | -**
|
| 677 | | -** The DP_Cplusplus, DP_ExternCReqd and DP_ExternReqd flags are permanent
|
| 678 | | -** and are used to specify what type of declaration the object requires.
|
| 679 | | -*/
|
| 680 | | -#define DP_Forward 0x001 /* Has a forward declaration in this file */
|
| 681 | | -#define DP_Declared 0x002 /* Has a full declaration in this file */
|
| 682 | | -#define DP_Export 0x004 /* Export this declaration */
|
| 683 | | -#define DP_Local 0x008 /* Declare in its home file only */
|
| 684 | | -#define DP_Flag 0x010 /* Use to mark a subset of a Decl list
|
| 685 | | - ** for special processing */
|
| 686 | | -#define DP_Cplusplus 0x020 /* Has C++ linkage and cannot appear in a
|
| 687 | | - ** C header file */
|
| 688 | | -#define DP_ExternCReqd 0x040 /* Prepend 'extern "C"' in a C++ header.
|
| 689 | | - ** Prepend nothing in a C header */
|
| 690 | | -#define DP_ExternReqd 0x080 /* Prepend 'extern "C"' in a C++ header if
|
| 691 | | - ** DP_Cplusplus is not also set. If DP_Cplusplus
|
| 692 | | - ** is set or this is a C header then
|
| 693 | | - ** prepend 'extern' */
|
| 694 | | -
|
| 695 | | -/*
|
| 696 | | -** Convenience macros for dealing with declaration properties
|
| 697 | | -*/
|
| 698 | | -#define DeclHasProperty(D,P) (((D)->flags&(P))==(P))
|
| 699 | | -#define DeclHasAnyProperty(D,P) (((D)->flags&(P))!=0)
|
| 700 | | -#define DeclSetProperty(D,P) (D)->flags |= (P)
|
| 701 | | -#define DeclClearProperty(D,P) (D)->flags &= ~(P)
|
| 702 | | -
|
| 703 | | -/*
|
| 704 | | -** These are state properties of the parser. Each of the values is
|
| 705 | | -** distinct from the DP_ values above so that both can be used in
|
| 706 | | -** the same "flags" field.
|
| 707 | | -**
|
| 708 | | -** Be careful not to confuse PS_Export with DP_Export or
|
| 709 | | -** PS_Local with DP_Local. Their names are similar, but the meanings
|
| 710 | | -** of these flags are very different.
|
| 711 | | -*/
|
| 712 | | -#define PS_Extern 0x000800 /* "extern" has been seen */
|
| 713 | | -#define PS_Export 0x001000 /* If between "#if EXPORT_INTERFACE"
|
| 714 | | - ** and "#endif" */
|
| 715 | | -#define PS_Export2 0x002000 /* If "EXPORT" seen */
|
| 716 | | -#define PS_Typedef 0x004000 /* If "typedef" has been seen */
|
| 717 | | -#define PS_Static 0x008000 /* If "static" has been seen */
|
| 718 | | -#define PS_Interface 0x010000 /* If within #if INTERFACE..#endif */
|
| 719 | | -#define PS_Method 0x020000 /* If "::" token has been seen */
|
| 720 | | -#define PS_Local 0x040000 /* If within #if LOCAL_INTERFACE..#endif */
|
| 721 | | -#define PS_Local2 0x080000 /* If "LOCAL" seen. */
|
| 722 | | -#define PS_Public 0x100000 /* If "PUBLIC" seen. */
|
| 723 | | -#define PS_Protected 0x200000 /* If "PROTECTED" seen. */
|
| 724 | | -#define PS_Private 0x400000 /* If "PRIVATE" seen. */
|
| 725 | | -#define PS_PPP 0x700000 /* If any of PUBLIC, PRIVATE, PROTECTED */
|
| 726 | | -
|
| 727 | | -/*
|
| 728 | | -** The following set of flags are ORed into the "flags" field of
|
| 729 | | -** a Decl in order to identify what type of object is being
|
| 730 | | -** declared.
|
| 731 | | -*/
|
| 732 | | -#define TY_Class 0x00100000
|
| 733 | | -#define TY_Subroutine 0x00200000
|
| 734 | | -#define TY_Macro 0x00400000
|
| 735 | | -#define TY_Typedef 0x00800000
|
| 736 | | -#define TY_Variable 0x01000000
|
| 737 | | -#define TY_Structure 0x02000000
|
| 738 | | -#define TY_Union 0x04000000
|
| 739 | | -#define TY_Enumeration 0x08000000
|
| 740 | | -#define TY_Defunct 0x10000000 /* Used to erase a declaration */
|
| 741 | | -
|
| 742 | | -/*
|
| 743 | | -** Each nested #if (or #ifdef or #ifndef) is stored in a stack of
|
| 744 | | -** instances of the following structure.
|
| 745 | | -*/
|
| 746 | | -typedef struct Ifmacro Ifmacro;
|
| 747 | | -struct Ifmacro {
|
| 748 | | - int nLine; /* Line number where this macro occurs */
|
| 749 | | - char *zCondition; /* Text of the condition for this macro */
|
| 750 | | - Ifmacro *pNext; /* Next down in the stack */
|
| 751 | | - int flags; /* Can hold PS_Export, PS_Interface or PS_Local flags */
|
| 752 | | -};
|
| 753 | | -
|
| 754 | | -/*
|
| 755 | | -** When parsing a file, we need to keep track of what other files have
|
| 756 | | -** be #include-ed. For each #include found, we create an instance of
|
| 757 | | -** the following structure.
|
| 758 | | -*/
|
| 759 | | -typedef struct Include Include;
|
| 760 | | -struct Include {
|
| 761 | | - char *zFile; /* The name of file include. Includes "" or <> */
|
| 762 | | - char *zIf; /* If not NULL, #include should be enclosed in #if */
|
| 763 | | - char *zLabel; /* A unique label used to test if this #include has
|
| 764 | | - * appeared already in a file or not */
|
| 765 | | - Include *pNext; /* Previous include file, or NULL if this is the first */
|
| 766 | | -};
|
| 767 | | -
|
| 768 | | -/*
|
| 769 | | -** Identifiers found in a source file that might be used later to provoke
|
| 770 | | -** the copying of a declaration into the corresponding header file are
|
| 771 | | -** stored in a hash table as instances of the following structure.
|
| 772 | | -*/
|
| 773 | | -typedef struct Ident Ident;
|
| 774 | | -struct Ident {
|
| 775 | | - char *zName; /* The text of this identifier */
|
| 776 | | - Ident *pCollide; /* Next identifier with the same hash */
|
| 777 | | - Ident *pNext; /* Next identifier in a list of them all */
|
| 778 | | -};
|
| 779 | | -
|
| 780 | | -/*
|
| 781 | | -** A complete table of identifiers is stored in an instance of
|
| 782 | | -** the next structure.
|
| 783 | | -*/
|
| 784 | | -#define IDENT_HASH_SIZE 2237
|
| 785 | | -typedef struct IdentTable IdentTable;
|
| 786 | | -struct IdentTable {
|
| 787 | | - Ident *pList; /* List of all identifiers in this table */
|
| 788 | | - Ident *apTable[IDENT_HASH_SIZE]; /* The hash table */
|
| 789 | | -};
|
| 790 | | -
|
| 791 | | -/*
|
| 792 | | -** The following structure holds all information for a single
|
| 793 | | -** source file named on the command line of this program.
|
| 794 | | -*/
|
| 795 | | -typedef struct InFile InFile;
|
| 796 | | -struct InFile {
|
| 797 | | - char *zSrc; /* Name of input file */
|
| 798 | | - char *zHdr; /* Name of the generated .h file for this input.
|
| 799 | | - ** Will be NULL if input is to be scanned only */
|
| 800 | | - int flags; /* One or more DP_, PS_ and/or TY_ flags */
|
| 801 | | - InFile *pNext; /* Next input file in the list of them all */
|
| 802 | | - IdentTable idTable; /* All identifiers in this input file */
|
| 803 | | -};
|
| 804 | | -
|
| 805 | | -/*
|
| 806 | | -** An unbounded string is able to grow without limit. We use these
|
| 807 | | -** to construct large in-memory strings from lots of smaller components.
|
| 808 | | -*/
|
| 809 | | -typedef struct String String;
|
| 810 | | -struct String {
|
| 811 | | - int nAlloc; /* Number of bytes allocated */
|
| 812 | | - int nUsed; /* Number of bytes used (not counting nul terminator) */
|
| 813 | | - char *zText; /* Text of the string */
|
| 814 | | -};
|
| 815 | | -
|
| 816 | | -/*
|
| 817 | | -** The following structure contains a lot of state information used
|
| 818 | | -** while generating a .h file. We put the information in this structure
|
| 819 | | -** and pass around a pointer to this structure, rather than pass around
|
| 820 | | -** all of the information separately. This helps reduce the number of
|
| 821 | | -** arguments to generator functions.
|
| 822 | | -*/
|
| 823 | | -typedef struct GenState GenState;
|
| 824 | | -struct GenState {
|
| 825 | | - String *pStr; /* Write output to this string */
|
| 826 | | - IdentTable *pTable; /* A table holding the zLabel of every #include that
|
| 827 | | - * has already been generated. Used to avoid
|
| 828 | | - * generating duplicate #includes. */
|
| 829 | | - const char *zIf; /* If not NULL, then we are within a #if with
|
| 830 | | - * this argument. */
|
| 831 | | - int nErr; /* Number of errors */
|
| 832 | | - const char *zFilename; /* Name of the source file being scanned */
|
| 833 | | - int flags; /* Various flags (DP_ and PS_ flags above) */
|
| 834 | | -};
|
| 835 | | -
|
| 836 | | -/*
|
| 837 | | -** The following text line appears at the top of every file generated
|
| 838 | | -** by this program. By recognizing this line, the program can be sure
|
| 839 | | -** never to read a file that it generated itself.
|
| 840 | | -**
|
| 841 | | -** The "#undef INTERFACE" part is a hack to work around a name collision
|
| 842 | | -** in MSVC 2008.
|
| 843 | | -*/
|
| 844 | | -const char zTopLine[] =
|
| 845 | | - "/* \aThis file was automatically generated. Do not edit! */\n"
|
| 846 | | - "#undef INTERFACE\n";
|
| 847 | | -#define nTopLine (sizeof(zTopLine)-1)
|
| 848 | | -
|
| 849 | | -/*
|
| 850 | | -** The name of the file currently being parsed.
|
| 851 | | -*/
|
| 852 | | -static const char *zFilename;
|
| 853 | | -
|
| 854 | | -/*
|
| 855 | | -** The stack of #if macros for the file currently being parsed.
|
| 856 | | -*/
|
| 857 | | -static Ifmacro *ifStack = 0;
|
| 858 | | -
|
| 859 | | -/*
|
| 860 | | -** A list of all files that have been #included so far in a file being
|
| 861 | | -** parsed.
|
| 862 | | -*/
|
| 863 | | -static Include *includeList = 0;
|
| 864 | | -
|
| 865 | | -/*
|
| 866 | | -** The last block comment seen.
|
| 867 | | -*/
|
| 868 | | -static Token *blockComment = 0;
|
| 869 | | -
|
| 870 | | -/*
|
| 871 | | -** The following flag is set if the -doc flag appears on the
|
| 872 | | -** command line.
|
| 873 | | -*/
|
| 874 | | -static int doc_flag = 0;
|
| 875 | | -
|
| 876 | | -/*
|
| 877 | | -** If the following flag is set, then makeheaders will attempt to
|
| 878 | | -** generate prototypes for static functions and procedures.
|
| 879 | | -*/
|
| 880 | | -static int proto_static = 0;
|
| 881 | | -
|
| 882 | | -/*
|
| 883 | | -** A list of all declarations. The list is held together using the
|
| 884 | | -** pNext field of the Decl structure.
|
| 885 | | -*/
|
| 886 | | -static Decl *pDeclFirst; /* First on the list */
|
| 887 | | -static Decl *pDeclLast; /* Last on the list */
|
| 888 | | -
|
| 889 | | -/*
|
| 890 | | -** A hash table of all declarations
|
| 891 | | -*/
|
| 892 | | -#define DECL_HASH_SIZE 3371
|
| 893 | | -static Decl *apTable[DECL_HASH_SIZE];
|
| 894 | | -
|
| 895 | | -/*
|
| 896 | | -** The TEST macro must be defined to something. Make sure this is the
|
| 897 | | -** case.
|
| 898 | | -*/
|
| 899 | | -#ifndef TEST
|
| 900 | | -# define TEST 0
|
| 901 | | -#endif
|
| 902 | | -
|
| 903 | | -#ifdef NOT_USED
|
| 904 | | -/*
|
| 905 | | -** We do our own assertion macro so that we can have more control
|
| 906 | | -** over debugging.
|
| 907 | | -*/
|
| 908 | | -#define Assert(X) if(!(X)){ CantHappen(__LINE__); }
|
| 909 | | -#define CANT_HAPPEN CantHappen(__LINE__)
|
| 910 | | -static void CantHappen(int iLine){
|
| 911 | | - fprintf(stderr,"Assertion failed on line %d\n",iLine);
|
| 912 | | - *(char*)1 = 0; /* Force a core-dump */
|
| 913 | | -}
|
| 914 | | -#endif
|
| 915 | | -
|
| 916 | | -/*
|
| 917 | | -** Memory allocation functions that are guaranteed never to return NULL.
|
| 918 | | -*/
|
| 919 | | -static void *SafeMalloc(int nByte){
|
| 920 | | - void *p = malloc( nByte );
|
| 921 | | - if( p==0 ){
|
| 922 | | - fprintf(stderr,"Out of memory. Can't allocate %d bytes.\n",nByte);
|
| 923 | | - exit(1);
|
| 924 | | - }
|
| 925 | | - return p;
|
| 926 | | -}
|
| 927 | | -static void SafeFree(void *pOld){
|
| 928 | | - if( pOld ){
|
| 929 | | - free(pOld);
|
| 930 | | - }
|
| 931 | | -}
|
| 932 | | -static void *SafeRealloc(void *pOld, int nByte){
|
| 933 | | - void *p;
|
| 934 | | - if( pOld==0 ){
|
| 935 | | - p = SafeMalloc(nByte);
|
| 936 | | - }else{
|
| 937 | | - p = realloc(pOld, nByte);
|
| 938 | | - if( p==0 ){
|
| 939 | | - fprintf(stderr,
|
| 940 | | - "Out of memory. Can't enlarge an allocation to %d bytes\n",nByte);
|
| 941 | | - exit(1);
|
| 942 | | - }
|
| 943 | | - }
|
| 944 | | - return p;
|
| 945 | | -}
|
| 946 | | -static char *StrDup(const char *zSrc, int nByte){
|
| 947 | | - char *zDest;
|
| 948 | | - if( nByte<=0 ){
|
| 949 | | - nByte = strlen(zSrc);
|
| 950 | | - }
|
| 951 | | - zDest = SafeMalloc( nByte + 1 );
|
| 952 | | - strncpy(zDest,zSrc,nByte);
|
| 953 | | - zDest[nByte] = 0;
|
| 954 | | - return zDest;
|
| 955 | | -}
|
| 956 | | -
|
| 957 | | -/*
|
| 958 | | -** Return TRUE if the character X can be part of an identifier
|
| 959 | | -*/
|
| 960 | | -#define ISALNUM(X) ((X)=='_' || isalnum(X))
|
| 961 | | -
|
| 962 | | -/*
|
| 963 | | -** Routines for dealing with unbounded strings.
|
| 964 | | -*/
|
| 965 | | -static void StringInit(String *pStr){
|
| 966 | | - pStr->nAlloc = 0;
|
| 967 | | - pStr->nUsed = 0;
|
| 968 | | - pStr->zText = 0;
|
| 969 | | -}
|
| 970 | | -static void StringReset(String *pStr){
|
| 971 | | - SafeFree(pStr->zText);
|
| 972 | | - StringInit(pStr);
|
| 973 | | -}
|
| 974 | | -static void StringAppend(String *pStr, const char *zText, int nByte){
|
| 975 | | - if( nByte<=0 ){
|
| 976 | | - nByte = strlen(zText);
|
| 977 | | - }
|
| 978 | | - if( pStr->nUsed + nByte >= pStr->nAlloc ){
|
| 979 | | - if( pStr->nAlloc==0 ){
|
| 980 | | - pStr->nAlloc = nByte + 100;
|
| 981 | | - pStr->zText = SafeMalloc( pStr->nAlloc );
|
| 982 | | - }else{
|
| 983 | | - pStr->nAlloc = pStr->nAlloc*2 + nByte;
|
| 984 | | - pStr->zText = SafeRealloc(pStr->zText, pStr->nAlloc);
|
| 985 | | - }
|
| 986 | | - }
|
| 987 | | - strncpy(&pStr->zText[pStr->nUsed],zText,nByte);
|
| 988 | | - pStr->nUsed += nByte;
|
| 989 | | - pStr->zText[pStr->nUsed] = 0;
|
| 990 | | -}
|
| 991 | | -#define StringGet(S) ((S)->zText?(S)->zText:"")
|
| 992 | | -
|
| 993 | | -/*
|
| 994 | | -** Compute a hash on a string. The number returned is a non-negative
|
| 995 | | -** value between 0 and 2**31 - 1
|
| 996 | | -*/
|
| 997 | | -static int Hash(const char *z, int n){
|
| 998 | | - int h = 0;
|
| 999 | | - if( n<=0 ){
|
| 1000 | | - n = strlen(z);
|
| 1001 | | - }
|
| 1002 | | - while( n-- ){
|
| 1003 | | - h = h ^ (h & 0x7fffffff; free software; are; you can redistribute it and/or
|
| 1004 | | -** modify it under the terms of the Simplified BSD License (also
|
| 1005 | | -** known as the "2-Clause License" or "FreeBSD License".)
|
| 1006 | | -**
|
| 1007 | | -** Copyright 1993 D. Richard Hipp. All rights reserved.
|
| 1008 | | -**
|
| 1009 | | -** Redistribution and use in source and binary forms, with or
|
| 1010 | | -** without modification, are permitted provided that the following
|
| 1011 | | -** conditions are met:
|
| 1012 | | -**
|
| 1013 | | -** 1. Redistributions of source code must retain the above copyright
|
| 1014 | | -** notice, this list of conditions and the following disclaimer.
|
| 1015 | | -**
|
| 1016 | | -** 2. Redistributions in binary form must reproduce the above copyright
|
| 1017 | | -** notice, this list of conditions and the following disclaimer in
|
| 1018 | | -** the documentation and/or other materials provided with the
|
| 1019 | | -** distribution.
|
| 1020 | | -**
|
| 1021 | | -** This software is provided "as is" and any express or implied warranties,
|
| 1022 | | -** including, but not limited to, the implied warranties of merchantability
|
| 1023 | | -** and fitness for a particular purpose are disclaimed. In no event shall
|
| 1024 | | -** the author or contributors be liable for any direct, indirect, incidental,
|
| 1025 | | -** special, exemplary, or consequential damages (including, but not limited
|
| 1026 | | -** to, procurement of substitute goods or services; loss of use, data or
|
| 1027 | | -** profits; or business interruption) however caused and on any theory of
|
| 1028 | | -** liability, whether in contract, strict liability, or tort (including
|
| 1029 | | -** negligence or otherwise) arising in any way out of the use of this
|
| 1030 | | -** software, even if advised of the possibility of such damage.
|
| 1031 | | -**
|
| 1032 | | -** This program is distributed in the hope that it will be useful,
|
| 1033 | | -** but without any warranty; without even the implied warranty of
|
| 1034 | | -** merchantability or fitness for a particular purpose.
|
| 1035 | | -*/
|
| 1036 | | -#include <stdio.h>
|
| 1037 | | -#include <stdlib.h>
|
| 1038 | | -#include <ctype.h>
|
| 1039 | | -#include <memory.h>
|
| 1040 | | -#include <sys/stat.h>
|
| 1041 | | -#include <assert.h>
|
| 1042 | | -#include <string.h>
|
| 1043 | | -
|
| 1044 | | -#if defined( __MINGW32__) || defined(__DMC__) || \
|
| 1045 | | - defined(_MSC_VER) || defined(__POCC__)
|
| 1046 | | -# ifndef WIN32
|
| 1047 | | -# define WIN32
|
| 1048 | | -# endif
|
| 1049 | | -#else
|
| 1050 | | -# include <unistd.h>
|
| 1051 | | -#endif
|
| 1052 | | -
|
| 1053 | | -/*
|
| 1054 | | -** Macros for debugging.
|
| 1055 | | -*/
|
| 1056 | | -#ifdef DEBUG
|
| 1057 | | -static int debugMask = 0;
|
| 1058 | | -# define debug0(F,M) if( (F)&debugMask ){ fprintf(stderr,M); }
|
| 1059 | | -# define debug1(F,M,A) if( (F)&debugMask ){ fprintf(stderr,M,A); }
|
| 1060 | | -# define debug2(F,M,A,B) if( (F)&debugMask ){ fprintf(stderr,M,A,B); }
|
| 1061 | | -# define debug3(F,M,A,B,C) if( (F)&debugMask ){ fprintf(stderr,M,A,B,C); }
|
| 1062 | | -# define PARSER 0x00000001
|
| 1063 | | -# define DECL_DUMP 0x00000002
|
| 1064 | | -# define TOKENIZER 0x00000004
|
| 1065 | | -#else
|
| 1066 | | -# define debug0(Flags, Format)
|
| 1067 | | -# define debug1(Flags, Format, A)
|
| 1068 | | -# define debug2(Flags, Format, A, B)
|
| 1069 | | -# define debug3(Flags, Format, A, B, C)
|
| 1070 | | -#endif
|
| 1071 | | -
|
| 1072 | | -/*
|
| 1073 | | -** The following macros are purely for the purpose of testing this
|
| 1074 | | -** program on itself. They don't really contribute to the code.
|
| 1075 | | -*/
|
| 1076 | | -#define INTERFACE 1
|
| 1077 | | -#define EXPORT_INTERFACE 1
|
| 1078 | | -#define EXPORT
|
| 1079 | | -
|
| 1080 | | -/*
|
| 1081 | | -** Each token in a source file is represented by an instance of
|
| 1082 | | -** the following structure. Tokens are collected onto a list.
|
| 1083 | | -*/
|
| 1084 | | -typedef struct Token Token;
|
| 1085 | | -struct Token {
|
| 1086 | | - const char *zText; /* The text of the token */
|
| 1087 | | - int nText; /* Number of characters in the token's text */
|
| 1088 | | - int eType; /* The type of this token */
|
| 1089 | | - int nLine; /* The line number on which the token starts */
|
| 1090 | | - Token *pComment; /* Most recent block comment before this token */
|
| 1091 | | - Token *pNext; /* Next token on the list */
|
| 1092 | | - Token *pPrev; /* Previous token on the list */
|
| 1093 | | -};
|
| 1094 | | -
|
| 1095 | | -/*
|
| 1096 | | -** During tokenization, information about the state of the input
|
| 1097 | | -** stream is held in an instance of the following structure
|
| 1098 | | -*/
|
| 1099 | | -typedef struct InStream InStream;
|
| 1100 | | -struct InStream {
|
| 1101 | | - const char *z; /* Complete text of the input */
|
| 1102 | | - int i; /* Next character to read from the input */
|
| 1103 | | - int nLine; /* The line number for character z[i] */
|
| 1104 | | -};
|
| 1105 | | -
|
| 1106 | | -/*
|
| 1107 | | -** Each declaration in the C or C++ source files is parsed out and stored as
|
| 1108 | | -** an instance of the following structure.
|
| 1109 | | -**
|
| 1110 | | -** A "forward declaration" is a declaration that an object exists that
|
| 1111 | | -** doesn't tell about the objects structure. A typical forward declaration
|
| 1112 | | -** is:
|
| 1113 | | -**
|
| 1114 | | -** struct Xyzzy;
|
| 1115 | | -**
|
| 1116 | | -** Not every object has a forward declaration. If it does, thought, the
|
| 1117 | | -** forward declaration will be contained in the zFwd field for C and
|
| 1118 | | -** the zFwdCpp for C++. The zDecl field contains the complete
|
| 1119 | | -** declaration text.
|
| 1120 | | -*/
|
| 1121 | | -typedef struct Decl Decl;
|
| 1122 | | -struct Decl {
|
| 1123 | | - char *zName; /* Name of the object being declared. The appearance
|
| 1124 | | - ** of this name is a source file triggers the declaration
|
| 1125 | | - ** to be added to the header for that file. */
|
| 1126 | | - const char *zFile; /* File from which extracted. */
|
| 1127 | | - char *zIf; /* Surround the declaration with this #if */
|
| 1128 | | - char *zFwd; /* A forward declaration. NULL if there is none. */
|
| 1129 | | - char *zFwdCpp; /* Use this forward declaration for C++. */
|
| 1130 | | - char *zDecl; /* A full declaration of this object */
|
| 1131 | | - char *zExtra; /* Extra declaration text inserted into class objects */
|
| 1132 | | - int extraType; /* Last public:, protected: or private: in zExtraDecl */
|
| 1133 | | - struct Include *pInclude; /* #includes that come before this declaration */
|
| 1134 | | - int flags; /* See the "Properties" below */
|
| 1135 | | - Token *pComment; /* A block comment associated with this declaration */
|
| 1136 | | - Token tokenCode; /* Implementation of functions and procedures */
|
| 1137 | | - Decl *pSameName; /* Next declaration with the same "zName" */
|
| 1138 | | - Decl *pSameHash; /* Next declaration with same hash but different zName */
|
| 1139 | | - Decl *pNext; /* Next declaration with a different name */
|
| 1140 | | -};
|
| 1141 | | -
|
| 1142 | | -/*
|
| 1143 | | -** Properties associated with declarations.
|
| 1144 | | -**
|
| 1145 | | -** DP_Forward and DP_Declared are used during the generation of a single
|
| 1146 | | -** header file in order to prevent duplicate declarations and definitions.
|
| 1147 | | -** DP_Forward is set after the object has been given a forward declaration
|
| 1148 | | -** and DP_Declared is set after the object gets a full declarations.
|
| 1149 | | -** (Example: A forward declaration is "typedef struct Abc Abc;" and the
|
| 1150 | | -** full declaration is "struct Abc { int a; float b; };".)
|
| 1151 | | -**
|
| 1152 | | -** The DP_Export and DP_Local flags are more permanent. They mark objects
|
| 1153 | | -** that have EXPORT scope and LOCAL scope respectively. If both of these
|
| 1154 | | -** marks are missing, then the object has library scope. The meanings of
|
| 1155 | | -** the scopes are as follows:
|
| 1156 | | -**
|
| 1157 | | -** LOCAL scope The object is only usable within the file in
|
| 1158 | | -** which it is declared.
|
| 1159 | | -**
|
| 1160 | | -** library scope The object is visible and usable within other
|
| 1161 | | -** files in the same project. By if the project is
|
| 1162 | | -** a library, then the object is not visible to users
|
| 1163 | | -** of the library. (i.e. the object does not appear
|
| 1164 | | -** in the output when using the -H option.)
|
| 1165 | | -**
|
| 1166 | | -** EXPORT scope The object is visible and usable everywhere.
|
| 1167 | | -**
|
| 1168 | | -** The DP_Flag is a temporary use flag that is used during processing to
|
| 1169 | | -** prevent an infinite loop. It's use is localized.
|
| 1170 | | -**
|
| 1171 | | -** The DP_Cplusplus, DP_ExternCReqd and DP_ExternReqd flags are permanent
|
| 1172 | | -** and are used to specify what type of declaration the object requires.
|
| 1173 | | -*/
|
| 1174 | | -#define DP_Forward 0x001 /* Has a forward declaration in this file */
|
| 1175 | | -#define DP_Declared 0x002 /* Has a full declaration in this file */
|
| 1176 | | -#define DP_Export 0x004 /* Export this declaration */
|
| 1177 | | -#define DP_Local 0x008 /* Declare in its home file only */
|
| 1178 | | -#define DP_Flag 0x010 /* Use to mark a subset of a Decl list
|
| 1179 | | - ** for special processing */
|
| 1180 | | -#define DP_Cplusplus 0x020 /* Has C++ linkage and cannot appear in a
|
| 1181 | | - ** C header file */
|
| 1182 | | -#define DP_ExternCReqd 0x040 /* Prepend 'extern "C"' in a C++ header.
|
| 1183 | | - ** Prepend nothing in a C header */
|
| 1184 | | -#define DP_ExternReqd 0x080 /* Prepend 'extern "C"' in a C++ header if
|
| 1185 | | - ** DP_Cplusplus is not also set. If DP_Cplusplus
|
| 1186 | | - ** is set or this is a C header then
|
| 1187 | | - ** prepend 'extern' */
|
| 1188 | | -
|
| 1189 | | -/*
|
| 1190 | | -** Convenience macros for dealing with declaration properties
|
| 1191 | | -*/
|
| 1192 | | -#define DeclHasProperty(D,P) (((D)->flags&(P))==(P))
|
| 1193 | | -#define DeclHasAnyProperty(D,P) (((D)->flags&(P))!=0)
|
| 1194 | | -#define DeclSetProperty(D,P) (D)->flags |= (P)
|
| 1195 | | -#define DeclClearProperty(D,P) (D)->flags &= ~(P)
|
| 1196 | | -
|
| 1197 | | -/*
|
| 1198 | | -** These are state properties of the parser. Each of the values is
|
| 1199 | | -** distinct from the DP_ values above so that both can be used in
|
| 1200 | | -** the same "flags" field.
|
| 1201 | | -**
|
| 1202 | | -** Be careful not to confuse PS_Export with DP_Export or
|
| 1203 | | -** PS_Local with DP_Local. Their names are similar, but the meanings
|
| 1204 | | -** of these flags are very different.
|
| 1205 | | -*/
|
| 1206 | | -#define PS_Extern 0x000800 /* "extern" has been seen */
|
| 1207 | | -#define PS_Export 0x001000 /* If between "#if EXPORT_INTERFACE"
|
| 1208 | | - ** and "#endif" */
|
| 1209 | | -#define PS_Export2 0x002000 /* If "EXPORT" seen */
|
| 1210 | | -#define PS_Typedef 0x004000 /* If "typedef" has been seen */
|
| 1211 | | -#define PS_Static 0x008000 /* If "static" has been seen */
|
| 1212 | | -#define PS_Interface 0x010000 /* If within #if INTERFACE..#endif */
|
| 1213 | | -#define PS_Method 0x020000 /* If "::" token has been seen */
|
| 1214 | | -#define PS_Local 0x040000 /* If within #if LOCAL_INTERFACE..#endif */
|
| 1215 | | -#define PS_Local2 0x080000 /* If "LOCAL" seen. */
|
| 1216 | | -#define PS_Public 0x100000 /* If "PUBLIC" seen. */
|
| 1217 | | -#define PS_Protected 0x200000 /* If "PROTECTED" seen. */
|
| 1218 | | -#define PS_Private 0x400000 /* If "PRIVATE" seen. */
|
| 1219 | | -#define PS_PPP 0x700000 /* If any of PUBLIC, PRIVATE, PROTECTED */
|
| 1220 | | -
|
| 1221 | | -/*
|
| 1222 | | -** The following set of flags are ORed into the "flags" field of
|
| 1223 | | -** a Decl in order to identify what type of object is being
|
| 1224 | | -** declared.
|
| 1225 | | -*/
|
| 1226 | | -#define TY_Class 0x00100000
|
| 1227 | | -#define TY_Subroutine 0x00200000
|
| 1228 | | -#define TY_Macro 0x00400000
|
| 1229 | | -#define TY_Typedef 0x00800000
|
| 1230 | | -#define TY_Variable 0x01000000
|
| 1231 | | -#define TY_Structure 0x02000000
|
| 1232 | | -#define TY_Union 0x04000000
|
| 1233 | | -#define TY_Enumeration 0x08000000
|
| 1234 | | -#define TY_Defunct 0x10000000 /* Used to erase a declaration */
|
| 1235 | | -
|
| 1236 | | -/*
|
| 1237 | | -** Each nested #if (or #ifdef or #ifndef) is stored in a stack of
|
| 1238 | | -** instances of the following structure.
|
| 1239 | | -*/
|
| 1240 | | -typedef struct Ifmacro Ifmacro;
|
| 1241 | | -struct Ifmacro {
|
| 1242 | | - int nLine; /* Line number where this macro occurs */
|
| 1243 | | - char *zCondition; /* Text of the condition for this macro */
|
| 1244 | | - Ifmacro *pNext; /* Next down in the stack */
|
| 1245 | | - int flags; /* Can hold PS_Export, PS_Interface or PS_Local flags */
|
| 1246 | | -};
|
| 1247 | | -
|
| 1248 | | -/*
|
| 1249 | | -** When parsing a file, we need to keep track of what other files have
|
| 1250 | | -** be #include-ed. For each #include found, we create an instance of
|
| 1251 | | -** the following structure.
|
| 1252 | | -*/
|
| 1253 | | -typedef struct Include Include;
|
| 1254 | | -struct Include {
|
| 1255 | | - char *zFile; /* The name of file include. Includes "" or <> */
|
| 1256 | | - char *zIf; /* If not NULL, #include should be enclosed in #if */
|
| 1257 | | - char *zLabel; /* A unique label used to test if this #include has
|
| 1258 | | - * appeared already in a file or not */
|
| 1259 | | - Include *pNext; /* Previous include file, or NULL if this is the first */
|
| 1260 | | -};
|
| 1261 | | -
|
| 1262 | | -/*
|
| 1263 | | -** Identifiers found in a source file that might be used later to provoke
|
| 1264 | | -** the copying of a declaration into the corresponding header file are
|
| 1265 | | -** stored in a hash table as instances of the following structure.
|
| 1266 | | -*/
|
| 1267 | | -typedef struct Ident Ident;
|
| 1268 | | -struct Ident {
|
| 1269 | | - char *zName; /* The text of this identifier */
|
| 1270 | | - Ident *pCollide; /* Next identifier with the same hash */
|
| 1271 | | - Ident *pNext; /* Next identifier in a list of them all */
|
| 1272 | | -};
|
| 1273 | | -
|
| 1274 | | -/*
|
| 1275 | | -** A complete table of identifiers is stored in an instance of
|
| 1276 | | -** the next structure.
|
| 1277 | | -*/
|
| 1278 | | -#define IDENT_HASH_SIZE 2237
|
| 1279 | | -typedef struct IdentTable IdentTable;
|
| 1280 | | -struct IdentTable {
|
| 1281 | | - Ident *pList; /* List of all identifiers in this table */
|
| 1282 | | - Ident *apTable[IDENT_HASH_SIZE]; /* The hash table */
|
| 1283 | | -};
|
| 1284 | | -
|
| 1285 | | -/*
|
| 1286 | | -** The following structure holds all information for a single
|
| 1287 | | -** source file named on the command line of this program.
|
| 1288 | | -*/
|
| 1289 | | -typedef struct InFile InFile;
|
| 1290 | | -struct InFile {
|
| 1291 | | - char *zSrc; /* Name of input file */
|
| 1292 | | - char *zHdr; /* Name of the generated .h file for this input.
|
| 1293 | | - ** Will be NULL if input is to be scanned only */
|
| 1294 | | - int flags; /* One or more DP_, PS_ and/or TY_ flags */
|
| 1295 | | - InFile *pNext; /* Next input file in the list of them all */
|
| 1296 | | - IdentTable idTable; /* All identifiers in this input file */
|
| 1297 | | -};
|
| 1298 | | -
|
| 1299 | | -/*
|
| 1300 | | -** An unbounded string is able to grow without limit. We use these
|
| 1301 | | -** to construct large in-memory strings from lots of smaller components.
|
| 1302 | | -*/
|
| 1303 | | -typedef struct String String;
|
| 1304 | | -struct String {
|
| 1305 | | - int nAlloc; /* Number of bytes allocated */
|
| 1306 | | - int nUsed; /* Number of bytes used (not counting nul terminator) */
|
| 1307 | | - char *zText; /* Text of the string */
|
| 1308 | | -};
|
| 1309 | | -
|
| 1310 | | -/*
|
| 1311 | | -** The following structure contains a lot of state information used
|
| 1312 | | -** while generating a .h file. We put the information in this structure
|
| 1313 | | -** and pass around a pointer to this structure, rather than pass around
|
| 1314 | | -** all of the information separately. This helps reduce the number of
|
| 1315 | | -** arguments to generator functions.
|
| 1316 | | -*/
|
| 1317 | | -typedef struct GenState GenState;
|
| 1318 | | -struct GenState {
|
| 1319 | | - String *pStr; /* Write output to this string */
|
| 1320 | | - IdentTable *pTable; /* A table holding the zLabel of every #include that
|
| 1321 | | - * has already been generated. Used to avoid
|
| 1322 | | - * generating duplicate #includes. */
|
| 1323 | | - const char *zIf; /* If not NULL, then we are within a #if with
|
| 1324 | | - * this argument. */
|
| 1325 | | - int nErr; /* Number of errors */
|
| 1326 | | - const char *zFilename; /* Name of the source file being scanned */
|
| 1327 | | - int flags; /* Various flags (DP_ and PS_ flags above) */
|
| 1328 | | -};
|
| 1329 | | -
|
| 1330 | | -/*
|
| 1331 | | -** The following text line appears at the top of every file generated
|
| 1332 | | -** by this program. By recognizing this line, the program can be sure
|
| 1333 | | -** never to read a file that it generated itself.
|
| 1334 | | -**
|
| 1335 | | -** The "#undef INTERFACE" part is a hack to work around a name collision
|
| 1336 | | -** in MSVC 2008.
|
| 1337 | | -*/
|
| 1338 | | -const char zTopLine[] =
|
| 1339 | | - "/* \aThis file was automatically generated. Do not edit! */\n"
|
| 1340 | | - "#undef INTERFACE\n";
|
| 1341 | | -#define nTopLine (sizeof(zTopLine)-1)
|
| 1342 | | -
|
| 1343 | | -/*
|
| 1344 | | -** The name of the file currently being parsed.
|
| 1345 | | -*/
|
| 1346 | | -static const char *zFilename;
|
| 1347 | | -
|
| 1348 | | -/*
|
| 1349 | | -** The stack of #if macros for the file currently being parsed.
|
| 1350 | | -*/
|
| 1351 | | -static Ifmacro *ifStack = 0;
|
| 1352 | | -
|
| 1353 | | -/*
|
| 1354 | | -** A list of all files that have been #included so far in a file being
|
| 1355 | | -** parsed.
|
| 1356 | | -*/
|
| 1357 | | -static Include *includeList = 0;
|
| 1358 | | -
|
| 1359 | | -/*
|
| 1360 | | -** The last block comment seen.
|
| 1361 | | -*/
|
| 1362 | | -static Token *blockComment = 0;
|
| 1363 | | -
|
| 1364 | | -/*
|
| 1365 | | -** The following flag is set if the -doc flag appears on the
|
| 1366 | | -** command line.
|
| 1367 | | -*/
|
| 1368 | | -static int doc_flag = 0;
|
| 1369 | | -
|
| 1370 | | -/*
|
| 1371 | | -** If the following flag is set, then makeheaders will attempt to
|
| 1372 | | -** generate prototypes for static functions and procedures.
|
| 1373 | | -*/
|
| 1374 | | -static int proto_static = 0;
|
| 1375 | | -
|
| 1376 | | -/*
|
| 1377 | | -** A list of all declarations. The list is held together using the
|
| 1378 | | -** pNext field of the Decl structure.
|
| 1379 | | -*/
|
| 1380 | | -static Decl *pDeclFirst; /* First on the list */
|
| 1381 | | -static Decl *pDeclLast; /* Last on the list */
|
| 1382 | | -
|
| 1383 | | -/*
|
| 1384 | | -** A hash table of all declarations
|
| 1385 | | -*/
|
| 1386 | | -#define DECL_HASH_SIZE 3371
|
| 1387 | | -static Decl *apTable[DECL_HASH_SIZE];
|
| 1388 | | -
|
| 1389 | | -/*
|
| 1390 | | -** The TEST macro must be defined to something. Make sure this is the
|
| 1391 | | -** case.
|
| 1392 | | -*/
|
| 1393 | | -#ifndef TEST
|
| 1394 | | -# define TEST 0
|
| 1395 | | -#endif
|
| 1396 | | -
|
| 1397 | | -#ifdef NOT_USED
|
| 1398 | | -/*
|
| 1399 | | -** We do our own assertion macro so that we can have more control
|
| 1400 | | -** over debugging.
|
| 1401 | | -*/
|
| 1402 | | -#define Assert(X) if(!(X)){ CantHappen(__LINE__); }
|
| 1403 | | -#define CANT_HAPPEN CantHappen(__LINE__)
|
| 1404 | | -static void CantHappen(int iLine){
|
| 1405 | | - fprintf(stderr,"Assertion failed on line %d\n",iLine);
|
| 1406 | | - *(char*)1 = 0; /* Force a core-dump */
|
| 1407 | | -}
|
| 1408 | | -#endif
|
| 1409 | | -
|
| 1410 | | -/*
|
| 1411 | | -** Memory allocation functions that are guaranteed never to return NULL.
|
| 1412 | | -*/
|
| 1413 | | -static void *SafeMalloc(int nByte){
|
| 1414 | | - void *p = malloc( nByte );
|
| 1415 | | - if( p==0 ){
|
| 1416 | | - fprintf(stderr,"Out of memory. Can't allocate %d bytes.\n",nByte);
|
| 1417 | | - exit(1);
|
| 1418 | | - }
|
| 1419 | | - return p;
|
| 1420 | | -}
|
| 1421 | | -static void SafeFree(void *pOld){
|
| 1422 | | - if( pOld ){
|
| 1423 | | - free(pOld);
|
| 1424 | | - }
|
| 1425 | | -}
|
| 1426 | | -static void *SafeRealloc(void *pOld, int nByte){
|
| 1427 | | - void *p;
|
| 1428 | | - if( pOld==0 ){
|
| 1429 | | - p = SafeMalloc(nByte);
|
| 1430 | | - }else{
|
| 1431 | | - p = realloc(pOld, nByte);
|
| 1432 | | - if( p==0 ){
|
| 1433 | | - fprintf(stderr,
|
| 1434 | | - "Out of memory. Can't enlarge an allocation to %d bytes\n",nByte);
|
| 1435 | | - exit(1);
|
| 1436 | | - }
|
| 1437 | | - }
|
| 1438 | | - return p;
|
| 1439 | | -}
|
| 1440 | | -static char *StrDup(const char *zSrc, int nByte){
|
| 1441 | | - char *zDest;
|
| 1442 | | - if( nByte<=0 ){
|
| 1443 | | - nByte = strlen(zSrc);
|
| 1444 | | - }
|
| 1445 | | - zDest = SafeMalloc( nByte + 1 );
|
| 1446 | | - strncpy(zDest,zSrc,nByte);
|
| 1447 | | - zDest[nByte] = 0;
|
| 1448 | | - return zDest;
|
| 1449 | | -}
|
| 1450 | | -
|
| 1451 | | -/*
|
| 1452 | | -** Return TRUE if the character X can be part of an identifier
|
| 1453 | | -*/
|
| 1454 | | -#define ISALNUM(X) ((X)=='_' || isalnum(X))
|
| 1455 | | -
|
| 1456 | | -/*
|
| 1457 | | -** Routines for dealing with unbounded strings.
|
| 1458 | | -*/
|
| 1459 | | -static void StringInit(String *pStr){
|
| 1460 | | - pStr->nAlloc = 0;
|
| 1461 | | - pStr->nUsed = 0;
|
| 1462 | | - pStr->zText = 0;
|
| 1463 | | -}
|
| 1464 | | -static void StringReset(String *pStr){
|
| 1465 | | - SafeFree(pStr->zText);
|
| 1466 | | - StringInit(pStr);
|
| 1467 | | -}
|
| 1468 | | -static void StringAppend(String *pStr, const char *zText, int nByte){
|
| 1469 | | - if( nByte<=0 ){
|
| 1470 | | - nByte = strlen(zText);
|
| 1471 | | - }
|
| 1472 | | - if( pStr->nUsed + nByte >= pStr->nAlloc ){
|
| 1473 | | - if( pStr->nAlloc==0 ){
|
| 1474 | | - pStr->nAlloc = nByte + 100;
|
| 1475 | | - pStr->zText = SafeMalloc( pStr->nAlloc );
|
| 1476 | | - }else{
|
| 1477 | | - pStr->nAlloc = pStr->nAlloc*2 + nByte;
|
| 1478 | | - pStr->zText = SafeRealloc(pStr->zText, pStr->nAlloc);
|
| 1479 | | - }
|
| 1480 | | - }
|
| 1481 | | - strncpy(&pStr->zText[pStr->nUsed],zText,nByte);
|
| 1482 | | - pStr->nUsed += nByte;
|
| 1483 | | - pStr->zText[pStr->nUsed] = 0;
|
| 1484 | | -}
|
| 1485 | | -#define StringGet(S) ((S)->zText?(S)->zText:"")
|
| 1486 | | -
|
| 1487 | | -/*
|
| 1488 | | -** Compute a hash on a string. The number returned is a non-negative
|
| 1489 | | -** value between 0 and 2**31 - 1
|
| 1490 | | -*/
|
| 1491 | | -static int Hash(const char *z, int n){
|
| 1492 | | - unsigned int h = 0;
|
| 1493 | | - if( n<=0 ){
|
| 1494 | | - n = strlen(z);
|
| 1495 | | - }
|
| 1496 | | - while( n-- ){
|
| 1497 | | - h = h ^ (h<<5) ^ *z++;
|
| 1498 | | - }
|
| 1499 | | - return (int)(h & 0x7fffffff);
|
| 1500 | | -}
|
| 1501 | | -
|
| 1502 | | -/*
|
| 1503 | | -** Given an identifier name, try to find a declaration for that
|
| 1504 | | -** identifier in the hash table. If found, return a pointer to
|
| 1505 | | -** the Decl structure. If not found, return 0.
|
| 1506 | | -*/
|
| 1507 | | -static Decl *FindDecl(const char *zName, int len){
|
| 1508 | | - int h;
|
| 1509 | | - Decl *p;
|
| 1510 | | -
|
| 1511 | | - if( len<=0 ){
|
| 1512 | | - len = strlen(zName);
|
| 1513 | | - }
|
| 1514 | | - h = Hash(zName,len) % DECL_HASH_SIZE;
|
| 1515 | | - p = apTable[h];
|
| 1516 | | - while( p && (strncmp(p->zName,zName,len)!=0 || p->zName[len]!=0) ){
|
| 1517 | | - p = p->pSameHash;
|
| 1518 | | - }
|
| 1519 | | - return p;
|
| 1520 | | -}
|
| 1521 | | -
|
| 1522 | | -/*
|
| 1523 | | -** Install the given declaration both in the hash table and on
|
| 1524 | | -** the list of all declarations.
|
| 1525 | | -*/
|
| 1526 | | -static void InstallDecl(Decl *pDecl){
|
| 1527 | | - int h;
|
| 1528 | | - Decl *pOther;
|
| 1529 | | -
|
| 1530 | | - h = Hash(pDecl->zName,0) % DECL_HASH_SIZE;
|
| 1531 | | - pOther = apTable[h];
|
| 1532 | | - while( pOther && strcmp(pDecl->zName,pOther->zName)!=0 ){
|
| 1533 | | - pOther = pOther->pSameHash;
|
| 1534 | | - }
|
| 1535 | | - if( pOther ){
|
| 1536 | | - pDecl->pSameName = pOther->pSameName;
|
| 1537 | | - pOther->pSameName = pDecl;
|
| 1538 | | - }else{
|
| 1539 | | - pDecl->pSameName = 0;
|
| 1540 | | - pDecl->pSameHash = apTable[h];
|
| 1541 | | - apTable[h] = pDecl;
|
| 1542 | | - }
|
| 1543 | | - pDecl->pNext = 0;
|
| 1544 | | - if( pDeclFirst==0 ){
|
| 1545 | | - pDeclFirst = pDeclLast = pDecl;
|
| 1546 | | - }else{
|
| 1547 | | - pDeclLast->pNext = pDecl;
|
| 1548 | | - pDeclLast = pDecl;
|
| 1549 | | - }
|
| 1550 | | -}
|
| 1551 | | -
|
| 1552 | | -/*
|
| 1553 | | -** Look at the current ifStack. If anything declared at the current
|
| 1554 | | -** position must be surrounded with
|
| 1555 | | -**
|
| 1556 | | -** #if STUFF
|
| 1557 | | -** #endif
|
| 1558 | | -**
|
| 1559 | | -** Then this routine computes STUFF and returns a pointer to it. Memory
|
| 1560 | | -** to hold the value returned is obtained from malloc().
|
| 1561 | | -*/
|
| 1562 | | -static char *GetIfString(void){
|
| 1563 | | - Ifmacro *pIf;
|
| 1564 | | - char *zResult = 0;
|
| 1565 | | - int hasIf = 0;
|
| 1566 | | - String str;
|
| 1567 | | -
|
| 1568 | | - for(pIf = ifStack; pIf; pIf=pIf->pNext){
|
| 1569 | | - if( pIf->zCondition==0 || *pIf->zCondition==0 ) continue;
|
| 1570 | | - if( !hasIf ){
|
| 1571 | | - hasIf = 1;
|
| 1572 | | - StringInit(&str);
|
| 1573 | | - }else{
|
| 1574 | | - StringAppend(&str," && ",4);
|
| 1575 | | - }
|
| 1576 | | - StringAppend(&str,pIf->zCondition,0);
|
| 1577 | | - }
|
| 1578 | | - if( hasIf ){
|
| 1579 | | - zResult = StrDup(StringGet(&str),0);
|
| 1580 | | - StringReset(&str);
|
| 1581 | | - }else{
|
| 1582 | | - zResult = 0;
|
| 1583 | | - }
|
| 1584 | | - return zResult;
|
| 1585 | | -}
|
| 1586 | | -
|
| 1587 | | -/*
|
| 1588 | | -** Create a new declaration and put it in the hash table. Also
|
| 1589 | | -** return a pointer to it so that we can fill in the zFwd and zDecl
|
| 1590 | | -** fields, and so forth.
|
| 1591 | | -*/
|
| 1592 | | -static Decl *CreateDecl(
|
| 1593 | | - const char *zName, /* Name of the object being declared. */
|
| 1594 | | - int nName /* Length of the name */
|
| 1595 | | -){
|
| 1596 | | - Decl *pDecl;
|
| 1597 | | -
|
| 1598 | | - pDecl = SafeMalloc( sizeof(Decl) + nName + 1);
|
| 1599 | | - memset(pDecl,0,sizeof(Decl));
|
| 1600 | | - pDecl->zName = (char*)&pDecl[1];
|
| 1601 | | - memcpy(pDecl->zName, zName, nName);
|
| 1602 | | - pDecl->zName[nName] = 0;
|
| 1603 | | - pDecl->zFile = zFilename;
|
| 1604 | | - pDecl->pInclude = includeList;
|
| 1605 | | - pDecl->zIf = GetIfString();
|
| 1606 | | - InstallDecl(pDecl);
|
| 1607 | | - return pDecl;
|
| 1608 | | -}
|
| 1609 | | -
|
| 1610 | | -/*
|
| 1611 | | -** Insert a new identifier into an table of identifiers. Return TRUE if
|
| 1612 | | -** a new identifier was inserted and return FALSE if the identifier was
|
| 1613 | | -** already in the table.
|
| 1614 | | -*/
|
| 1615 | | -static int IdentTableInsert(
|
| 1616 | | - IdentTable *pTable, /* The table into which we will insert */
|
| 1617 | | - const char *zId, /* Name of the identifiers */
|
| 1618 | | - int nId /* Length of the identifier name */
|
| 1619 | | -){
|
| 1620 | | - int h;
|
| 1621 | | - Ident *pId;
|
| 1622 | | -
|
| 1623 | | - if( nId<=0 ){
|
| 1624 | | - nId = strlen(zId);
|
| 1625 | | - }
|
| 1626 | | - h = Hash(zId,nId) % IDENT_HASH_SIZE;
|
| 1627 | | - for(pId = pTable->apTable[h]; pId; pId=pId->pCollide){
|
| 1628 | | - if( strncmp(zId,pId->zName,nId)==0 && pId->zName[nId]==0 ){
|
| 1629 | | - /* printf("Already in table: %.*s\n",nId,zId); */
|
| 1630 | | - return 0;
|
| 1631 | | - }
|
| 1632 | | - }
|
| 1633 | | - pId = SafeMalloc( sizeof(Ident) + nId + 1 );
|
| 1634 | | - pId->zName = (char*)&pId[1];
|
| 1635 | | - memcpy(pId->zName, zId, nId);
|
| 1636 | | - pId->zName[nId] = 0;
|
| 1637 | | - pId->pNext = pTable->pList;
|
| 1638 | | - pTable->pList = pId;
|
| 1639 | | - pId->pCollide = pTable->apTable[h];
|
| 1640 | | - pTable->apTable[h] = pId;
|
| 1641 | | - /* printf("Add to table: %.*s\n",nId,zId); */
|
| 1642 | | - return 1;
|
| 1643 | | -}
|
| 1644 | | -
|
| 1645 | | -/*
|
| 1646 | | -** Check to see if the given value is in the given IdentTable. Return
|
| 1647 | | -** true if it is and false if it is not.
|
| 1648 | | -*/
|
| 1649 | | -static int IdentTableTest(
|
| 1650 | | - IdentTable *pTable, /* The table in which to search */
|
| 1651 | | - const char *zId, /* Name of the identifiers */
|
| 1652 | | - int nId /* Length of the identifier name */
|
| 1653 | | -){
|
| 1654 | | - int h;
|
| 1655 | | - Ident *pId;
|
| 1656 | | -
|
| 1657 | | - if( nId<=0 ){
|
| 1658 | | - nId = strlen(zId);
|
| 1659 | | - }
|
| 1660 | | - h = Hash(zId,nId) % IDENT_HASH_SIZE;
|
| 1661 | | - for(pId = pTable->apTable[h]; pId; pId=pId->pCollide){
|
| 1662 | | - if( strncmp(zId,pId->zName,nId)==0 && pId->zName[nId]==0 ){
|
| 1663 | | - return 1;
|
| 1664 | | - }
|
| 1665 | | - }
|
| 1666 | | - return 0;
|
| 1667 | | -}
|
| 1668 | | -
|
| 1669 | | -/*
|
| 1670 | | -** Remove every identifier from the given table. Reset the table to
|
| 1671 | | -** its initial state.
|
| 1672 | | -*/
|
| 1673 | | -static void IdentTableReset(IdentTable *pTable){
|
| 1674 | | - Ident *pId, *pNext;
|
| 1675 | | -
|
| 1676 | | - for(pId = pTable->pList; pId; pId = pNext){
|
| 1677 | | - pNext = pId->pNext;
|
| 1678 | | - SafeFree(pId);
|
| 1679 | | - }
|
| 1680 | | - memset(pTable,0,sizeof(IdentTable));
|
| 1681 | | -}
|
| 1682 | | -
|
| 1683 | | -#ifdef DEBUG
|
| 1684 | | -/*
|
| 1685 | | -** Print the name of every identifier in the given table, one per line
|
| 1686 | | -*/
|
| 1687 | | -static void IdentTablePrint(IdentTable *pTable, FILE *pOut){
|
| 1688 | | - Ident *pId;
|
| 1689 | | -
|
| 1690 | | - for(pId = pTable->pList; pId; pId = pId->pNext){
|
| 1691 | | - fprintf(pOut,"%s\n",pId->zName);
|
| 1692 | | - }
|
| 1693 | | -}
|
| 1694 | | -#endif
|
| 1695 | | -
|
| 1696 | | -/*
|
| 1697 | | -** Read an entire file into memory. Return a pointer to the memory.
|
| 1698 | | -**
|
| 1699 | | -** The memory is obtained from SafeMalloc and must be freed by the
|
| 1700 | | -** calling function.
|
| 1701 | | -**
|
| 1702 | | -** If the read fails for any reason, 0 is returned.
|
| 1703 | | -*/
|
| 1704 | | -static char *ReadFile(const char *zFilename){
|
| 1705 | | - struct stat sStat;
|
| 1706 | | - FILE *pIn;
|
| 1707 | | - char *zBuf;
|
| 1708 | | - int n;
|
| 1709 | | -
|
| 1710 | | - if( stat(zFilename,&sStat)!=0
|
| 1711 | | -#ifndef WIN32
|
| 1712 | | - || !S_ISREG(sStat.st_mode)
|
| 1713 | | -#endif
|
| 1714 | | - ){
|
| 1715 | | - return 0;
|
| 1716 | | - }
|
| 1717 | | - pIn = fopen(zFilename,"r");
|
| 1718 | | - if( pIn==0 ){
|
| 1719 | | - return 0;
|
| 1720 | | - }
|
| 1721 | | - zBuf = SafeMalloc( sStat.st_size + 1 );
|
| 1722 | | - n = fread(zBuf,1,sStat.st_size,pIn);
|
| 1723 | | - zBuf[n] = 0;
|
| 1724 | | - fclose(pIn);
|
| 1725 | | - return zBuf;
|
| 1726 | | -}
|
| 1727 | | -
|
| 1728 | | -/*
|
| 1729 | | -** Write the contents of a string into a file. Return the number of
|
| 1730 | | -** errors
|
| 1731 | | -*/
|
| 1732 | | -static int WriteFile(const char *zFilename, const char *zOutput){
|
| 1733 | | - FILE *pOut;
|
| 1734 | | - pOut = fopen(zFilename,"w");
|
| 1735 | | - if( pOut==0 ){
|
| 1736 | | - return 1;
|
| 1737 | | - }
|
| 1738 | | - fwrite(zOutput,1,strlen(zOutput),pOut);
|
| 1739 | | - fclose(pOut);
|
| 1740 | | - return 0;
|
| 1741 | | -}
|
| 1742 | | -
|
| 1743 | | -/*
|
| 1744 | | -** Major token types
|
| 1745 | | -*/
|
| 1746 | | -#define TT_Space 1 /* Contiguous white space */
|
| 1747 | | -#define TT_Id 2 /* An identifier */
|
| 1748 | | -#define TT_Preprocessor 3 /* Any C preprocessor directive */
|
| 1749 | | -#define TT_Comment 4 /* Either C or C++ style comment */
|
| 1750 | | -#define TT_Number 5 /* Any numeric constant */
|
| 1751 | | -#define TT_String 6 /* String or character constants. ".." or '.' */
|
| 1752 | | -#define TT_Braces 7 /* All text between { and a matching } */
|
| 1753 | | -#define TT_EOF 8 /* End of file */
|
| 1754 | | -#define TT_Error 9 /* An error condition */
|
| 1755 | | -#define TT_BlockComment 10 /* A C-Style comment at the left margin that
|
| 1756 | | - * spans multiple lines */
|
| 1757 | | -#define TT_Other 0 /* None of the above */
|
| 1758 | | -
|
| 1759 | | -/*
|
| 1760 | | -** Get a single low-level token from the input file. Update the
|
| 1761 | | -** file pointer so that it points to the first character beyond the
|
| 1762 | | -** token.
|
| 1763 | | -**
|
| 1764 | | -** A "low-level token" is any token except TT_Braces. A TT_Braces token
|
| 1765 | | -** consists of many smaller tokens and is assembled by a routine that
|
| 1766 | | -** calls this one.
|
| 1767 | | -**
|
| 1768 | | -** The function returns the number of errors. An error is an
|
| 1769 | | -** unterminated string or character literal or an unterminated
|
| 1770 | | -** comment.
|
| 1771 | | -**
|
| 1772 | | -** Profiling shows that this routine consumes about half the
|
| 1773 | | -** CPU time on a typical run of makeheaders.
|
| 1774 | | -*/
|
| 1775 | | -static int GetToken(InStream *pIn, Token *pToken){
|
| 1776 | | - int i;
|
| 1777 | | - const char *z;
|
| 1778 | | - int cStart;
|
| 1779 | | - int c;
|
| 1780 | | - int startLine; /* Line on which a structure begins */
|
| 1781 | | - int nlisc = 0; /* True if there is a new-line in a ".." or '..' */
|
| 1782 | | - int nErr = 0; /* Number of errors seen */
|
| 1783 | | -
|
| 1784 | | - z = pIn->z;
|
| 1785 | | - i = pIn->i;
|
| 1786 | | - pToken->nLine = pIn->nLine;
|
| 1787 | | - pToken->zText = &z[i];
|
| 1788 | | - switch( z[i] ){
|
| 1789 | | - case 0:
|
| 1790 | | - pToken->eType = TT_EOF;
|
| 1791 | | - pToken->nText = 0;
|
| 1792 | | - break;
|
| 1793 | | -
|
| 1794 | | - case '#':
|
| 1795 | | - if( i==0 || z[i-1]=='\n' || (i>1 && z[i-1]=='\r' && z[i-2]=='\n')){
|
| 1796 | | - /* We found a preprocessor statement */
|
| 1797 | | - pToken->eType = TT_Preprocessor;
|
| 1798 | | - i++;
|
| 1799 | | - while( z[i]!=0 && z[i]!='\n' ){
|
| 1800 | | - if( z[i]=='\\' ){
|
| 1801 | | - i++;
|
| 1802 | | - if( z[i]=='\n' ) pIn->nLine++;
|
| 1803 | | - }
|
| 1804 | | - i++;
|
| 1805 | | - }
|
| 1806 | | - pToken->nText = i - pIn->i;
|
| 1807 | | - }else{
|
| 1808 | | - /* Just an operator */
|
| 1809 | | - pToken->eType = TT_Other;
|
| 1810 | | - pToken->nText = 1;
|
| 1811 | | - }
|
| 1812 | | - break;
|
| 1813 | | -
|
| 1814 | | - case ' ':
|
| 1815 | | - case '\t':
|
| 1816 | | - case '\r':
|
| 1817 | | - case '\f':
|
| 1818 | | - case '\n':
|
| 1819 | | - while( isspace(z[i]) ){
|
| 1820 | | - if( z[i]=='\n' ) pIn->nLine++;
|
| 1821 | | - i++;
|
| 1822 | | - }
|
| 1823 | | - pToken->eType = TT_Space;
|
| 1824 | | - pToken->nText = i - pIn->i;
|
| 1825 | | - break;
|
| 1826 | | -
|
| 1827 | | - case '\\':
|
| 1828 | | - pToken->nText = 2;
|
| 1829 | | - pToken->eType = TT_Other;
|
| 1830 | | - if( z[i+1]=='\n' ){
|
| 1831 | | - pIn->nLine++;
|
| 1832 | | - pToken->eType = TT_Space;
|
| 1833 | | - }else if( z[i+1]==0 ){
|
| 1834 | | - pToken->nText = 1;
|
| 1835 | | - }
|
| 1836 | | - break;
|
| 1837 | | -
|
| 1838 | | - case '\'':
|
| 1839 | | - case '\"':
|
| 1840 | | - cStart = z[i];
|
| 1841 | | - startLine = pIn->nLine;
|
| 1842 | | - do{
|
| 1843 | | - i++;
|
| 1844 | | - c = z[i];
|
| 1845 | | - if( c=='\n' ){
|
| 1846 | | - if( !nlisc ){
|
| 1847 | | - fprintf(stderr,
|
| 1848 | | - "%s:%d: (warning) Newline in string or character literal.\n",
|
| 1849 | | - zFilename, pIn->nLine);
|
| 1850 | | - nlisc = 1;
|
| 1851 | | - }
|
| 1852 | | - pIn->nLine++;
|
| 1853 | | - }
|
| 1854 | | - if( c=='\\' ){
|
| 1855 | | - i++;
|
| 1856 | | - nLine++;
|
| 1857 | | - }
|
| 1858 | | - }else if( c==cStart ){
|
| 1859 | | - i++;
|
| 1860 | | - c = 0;
|
| 1861 | | - }else if( c==0 ){
|
| 1862 | | - fprintf(stderr, "%s:%d: Unterminated string or character literal.\n",
|
| 1863 | | - zFilename, startLine);
|
| 1864 | | - nErr++;
|
| 1865 | | - }
|
| 1866 | | - }while( c );
|
| 1867 | | - pToken->eType = TT_String;
|
| 1868 | | - pToken->nText = i - pIn->i;
|
| 1869 | | - break;
|
| 1870 | | -
|
| 1871 | | - case '/':
|
| 1872 | | - if( z[i+1]=='/' ){
|
| 1873 | | - /* C++ style comment */
|
| 1874 | | - while( z[i] && z[i]!='\n' ){ i++; }
|
| 1875 | | - pToken->eType = TT_Comment;
|
| 1876 | | - pToken->nText = i - pIn->i;
|
| 1877 | | - }else if( z[i+1]=='*' ){
|
| 1878 | | - /* C style comment */
|
| 1879 | | - int isBlockComment = i==0 || z[i-1]=='\n';
|
| 1880 | | - i += 2;
|
| 1881 | | - startLine = pIn->nLine;
|
| 1882 | | - while( z[i] && (z[i]!='*' || z[i+1]!='/') ){
|
| 1883 | | - if( z[i]=='\n' ){
|
| 1884 | | - pIn->nLine++;
|
| 1885 | | - if( isBlockComment ){
|
| 1886 | | - if( z[i+1]=='*' || z[i+2]=='*' ){
|
| 1887 | | - isBlockComment = 2;
|
| 1888 | | - }else{
|
| 1889 | | - isBlockComment = 0;
|
| 1890 | | - }
|
| 1891 | | - }
|
| 1892 | | - }
|
| 1893 | | - i++;
|
| 1894 | | - }
|
| 1895 | | - if( z[i] ){
|
| 1896 | | - i += 2;
|
| 1897 | | - }else{
|
| 1898 | | - isBlockComment = 0;
|
| 1899 | | - fprintf(stderr,"%s:%d: Unterminated comment\n",
|
| 1900 | | - zFilename, startLine);
|
| 1901 | | - nErr++;
|
| 1902 | | - }
|
| 1903 | | - pToken->eType = isBlockComment==2 ? TT_BlockComment : TT_Comment;
|
| 1904 | | - pToken->nText = i - pIn->i;
|
| 1905 | | - }else{
|
| 1906 | | - /* A divide operator */
|
| 1907 | | - pToken->eType = TT_Other;
|
| 1908 | | - pToken->nText = 1 + (z[i+1]=='+');
|
| 1909 | | - }
|
| 1910 | | - break;
|
| 1911 | | -
|
| 1912 | | - case '0':
|
| 1913 | | - if( z[i+1]=='x' || z[i+1]=='X' ){
|
| 1914 | | - /* A hex constant */
|
| 1915 | | - i += 2;
|
| 1916 | | - while( isxdigit(z[i]) ){ i++; }
|
| 1917 | | - }else{
|
| 1918 | | - /* An octal constant */
|
| 1919 | | - while( isdigit(z[i]) ){ i++; }
|
| 1920 | | - }
|
| 1921 | | - pToken->eType = TT_Number;
|
| 1922 | | - pToken->nText = i - pIn->i;
|
| 1923 | | - break;
|
| 1924 | | -
|
| 1925 | | - case '1': case '2': case '3': case '4':
|
| 1926 | | - case '5': case '6': case '7': case '8': case '9':
|
| 1927 | | - while( isdigit(z[i]) ){ i++; }
|
| 1928 | | - if( (c=z[i])=='.' ){
|
| 1929 | | - i++;
|
| 1930 | | - while( isdigit(z[i]) ){ i++; }
|
| 1931 | | - c = z[i];
|
| 1932 | | - if( c=='e' || c=='E' ){
|
| 1933 | | - i++;
|
| 1934 | | - if( ((c=z[i])=='+' || c=='-') && isdigit(z[i+1]) ){ i++; }
|
| 1935 | | - while( isdigit(z[i]) ){ i++; }
|
| 1936 | | - c = z[i];
|
| 1937 | | - }
|
| 1938 | | - if( c=='f' || c=='F' || c=='l' || c=='L' ){ i++; }
|
| 1939 | | - }else if( c=='e' || c=='E' ){
|
| 1940 | | - i++;
|
| 1941 | | - if( ((c=z[i])=='+' || c=='-') && isdigit(z[i+1]) ){ i++; }
|
| 1942 | | - while( isdigit(z[i]) ){ i++; }
|
| 1943 | | - }else if( c=='L' || c=='l' ){
|
| 1944 | | - i++;
|
| 1945 | | - c = z[i];
|
| 1946 | | - if( ){
|
| 1947 | | - i++;
|
| 1948 | | - c = z[i];
|
| 1949 | | - if( c=='l' || c=='L' ){ i++; }
|
| 1950 | | - }
|
| 1951 | | - pToken->eType = TT_Number;
|
| 1952 | | - pToken->nText = i - pIn->i;
|
| 1953 | | - break;
|
| 1954 | | -
|
| 1955 | | - case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g':
|
| 1956 | | - case 'h': case 'i': case 'j': case 'k': case 'l': case 'm': case 'n':
|
| 1957 | | - case 'o': case 'p': case 'r contributors be liable for any direct, indirect, incidental,
|
| 1958 | | -** special, exemplary, or consequential damages (including, but not limited
|
| 1959 | | -** to, procurement of substitute goods or services; loss of use, data or
|
| 1960 | | -** profits; or business interruption) however caused and on any theory of
|
| 1961 | | -** liability, whether in contract, strict liability, or tort (including
|
| 1962 | | -** negligence or otherwise) arising in any way out of the use of this
|
| 1963 | | -** software, even if advised of the possibility of such damage.
|
| 1964 | | -**
|
| 1965 | | -** This program is distributed in the hope that it will be useful,
|
| 1966 | | -** but without any warranty; without even the implied warranty of
|
| 1967 | | -** merchantability or fitness for a particular purpose.
|
| 1968 | | -*/
|
| 1969 | | -#include <stdio.h>
|
| 1970 | | -#include <stdlib.h>
|
| 1971 | | -#include <ctype.h>
|
| 1972 | | -#include <memory.h>
|
| 1973 | | -#include <sys/stat.h>
|
| 1974 | | -#include <assert.h>
|
| 1975 | | -#include <string.h>
|
| 1976 | | -
|
| 1977 | | -#if defined( __MINGW32__) || defined(__DMC__) || defined(_MSC_VER) || defined(__POCC__)
|
| 1978 | | -# ifndef WIN32
|
| 1979 | | -# define WIN32
|
| 1980 | | -# endif
|
| 1981 | | -#else
|
| 1982 | | -# include <unistd.h>
|
| 1983 | | -#endif
|
| 1984 | | -
|
| 1985 | | -/*
|
| 1986 | | -** Macros for debugging.
|
| 1987 | | -*/
|
| 1988 | | -#ifdef DEBUG
|
| 1989 | | -static int debugMask = 0;
|
| 1990 | | -# define debug0(F,M) if( (F)&debugMask ){ fprintf(stderr,M); }
|
| 1991 | | -# define debug1(F,M,A) if( (F)&debugMask ){ fprintf(stderr,M,A); }
|
| 1992 | | -# define debug2(F,M,A,B) if( (F)&debugMask ){ fprintf(stderr,M,A,B); }
|
| 1993 | | -# define debug3(F,M,A,B,C) if( (F)&debugMask ){ fprintf(stderr,M,A,B,C); }
|
| 1994 | | -# define PARSER 0x00000001
|
| 1995 | | -# define DECL_DUMP 0x00000002
|
| 1996 | | -# define TOKENIZER 0x00000004
|
| 1997 | | -#else
|
| 1998 | | -# define debug0(Flags, Format)
|
| 1999 | | -# define debug1(Flags, Format, A)
|
| 2000 | | -# define debug2(Flags, Format, A, B)
|
| 2001 | | -# define debug3(Flags, Format, A, B, C)
|
| 2002 | | -#endif
|
| 2003 | | -
|
| 2004 | | -/*
|
| 2005 | | -** The following macros are purely for the purpose of testing this
|
| 2006 | | -** program on itself. They don't really contribute to the code.
|
| 2007 | | -*/
|
| 2008 | | -#define INTERFACE 1
|
| 2009 | | -#define EXPORT_INTERFACE 1
|
| 2010 | | -#define EXPORT
|
| 2011 | | -
|
| 2012 | | -/*
|
| 2013 | | -** Each token in a source file is represented by an instance of
|
| 2014 | | -** the following structure. Tokens are collected onto a list.
|
| 2015 | | -*/
|
| 2016 | | -typedef struct Token Token;
|
| 2017 | | -struct Token {
|
| 2018 | | - const char *zText; /* The text of the token */
|
| 2019 | | - int nText; /* Number of characters in the token's text */
|
| 2020 | | - int eType; /* The type of this token */
|
| 2021 | | - int nLine; /* The line number on which the token starts */
|
| 2022 | | - Token *pComment; /* Most recent block comment before this token */
|
| 2023 | | - Token *pNext; /* Next token on the list */
|
| 2024 | | - Token *pPrev; /* Previous token on the list */
|
| 2025 | | -};
|
| 2026 | | -
|
| 2027 | | -/*
|
| 2028 | | -** During tokenization, information about the state of the input
|
| 2029 | | -** stream is held in an instance of the following structure
|
| 2030 | | -*/
|
| 2031 | | -typedef struct InStream InStream;
|
| 2032 | | -struct InStream {
|
| 2033 | | - const char *z; /* Complete text of the input */
|
| 2034 | | - int i; /* Next character to read from the input */
|
| 2035 | | - int nLine; /* The line number for character z[i] */
|
| 2036 | | -};
|
| 2037 | | -
|
| 2038 | | -/*
|
| 2039 | | -** Each declaration in the C or C++ source files is parsed out and stored as
|
| 2040 | | -** an instance of the following structure.
|
| 2041 | | -**
|
| 2042 | | -** A "forward declaration" is a declaration that an object exists that
|
| 2043 | | -** doesn't tell about the objects structure. A typical forward declaration
|
| 2044 | | -** is:
|
| 2045 | | -**
|
| 2046 | | -** struct Xyzzy;
|
| 2047 | | -**
|
| 2048 | | -** Not every object has a forward declaration. If it does, thought, the
|
| 2049 | | -** forward declaration will be contained in the zFwd field for C and
|
| 2050 | | -** the zFwdCpp for C++. The zDecl field contains the complete
|
| 2051 | | -** declaration text.
|
| 2052 | | -*/
|
| 2053 | | -typedef struct Decl Decl;
|
| 2054 | | -struct Decl {
|
| 2055 | | - char *zName; /* Name of the object being declared. The appearance
|
| 2056 | | - ** of this name is a source file triggers the declaration
|
| 2057 | | - ** to be added to the header for that file. */
|
| 2058 | | - const char *zFile; /* File from which extracted. */
|
| 2059 | | - char *zIf; /* Surround the declaration with this #if */
|
| 2060 | | - char *zFwd; /* A forward declaration. NULL if there is none. */
|
| 2061 | | - char *zFwdCpp; /* Use this forward declaration for C++. */
|
| 2062 | | - char *zDecl; /* A full declaration of this object */
|
| 2063 | | - char *zExtra; /* Extra declaration text inserted into class objects */
|
| 2064 | | - int extraType; /* Last public:, protected: or private: in zExtraDecl */
|
| 2065 | | - struct Include *pInclude; /* #includes that come before this declaration */
|
| 2066 | | - int flags; /* See the "Properties" below */
|
| 2067 | | - Token *pComment; /* A block comment associated with this declaration */
|
| 2068 | | - Token tokenCode; /* Implementation of functions and procedures */
|
| 2069 | | - Decl *pSameName; /* Next declaration with the same "zName" */
|
| 2070 | | - Decl *pSameHash; /* Next declaration with same hash but different zName */
|
| 2071 | | - Decl *pNext; /* Next declaration with a different name */
|
| 2072 | | -};
|
| 2073 | | -
|
| 2074 | | -/*
|
| 2075 | | -** Properties associated with declarations.
|
| 2076 | | -**
|
| 2077 | | -** DP_Forward and DP_Declared are used during the generation of a single
|
| 2078 | | -** header file in order to prevent duplicate declarations and definitions.
|
| 2079 | | -** DP_Forward is set after the object has been given a forward declaration
|
| 2080 | | -** and DP_Declared is set after the object gets a full declarations.
|
| 2081 | | -** (Example: A forward declaration is "typedef struct Abc Abc;" and the
|
| 2082 | | -** full declaration is "struct Abc { int a; float b; };".)
|
| 2083 | | -**
|
| 2084 | | -** The DP_Export and DP_Local flags are more permanent. They mark objects
|
| 2085 | | -** that have EXPORT scope and LOCAL scope respectively. If both of these
|
| 2086 | | -** marks are missing, then the object has library scope. The meanings of
|
| 2087 | | -** the scopes are as follows:
|
| 2088 | | -**
|
| 2089 | | -** LOCAL scope The object is only usable within the file in
|
| 2090 | | -** which it is declared.
|
| 2091 | | -**
|
| 2092 | | -** library scope The object is visible and usable within other
|
| 2093 | | -** files in the same project. By if the project is
|
| 2094 | | -** a library, then the object is not visible to users
|
| 2095 | | -** of the library. (i.e. the object does not appear
|
| 2096 | | -** in the output when using the -H option.)
|
| 2097 | | -**
|
| 2098 | | -** EXPORT scope The object is visible and usable everywhere.
|
| 2099 | | -**
|
| 2100 | | -** The DP_Flag is a temporary use flag that is used during processing to
|
| 2101 | | -** prevent an infinite loop. It's use is localized.
|
| 2102 | | -**
|
| 2103 | | -** The DP_Cplusplus, DP_ExternCReqd and DP_ExternReqd flags are permanent
|
| 2104 | | -** and are used to specify what type of declaration the object requires.
|
| 2105 | | -*/
|
| 2106 | | -#define DP_Forward 0x001 /* Has a forward declaration in this file */
|
| 2107 | | -#define DP_Declared 0x002 /* Has a full declaration in this file */
|
| 2108 | | -#define DP_Export 0x004 /* Export this declaration */
|
| 2109 | | -#define DP_Local 0x008 /* Declare in its home file only */
|
| 2110 | | -#define DP_Flag 0x010 /* Use to mark a subset of a Decl list
|
| 2111 | | - ** for special processing */
|
| 2112 | | -#define DP_Cplusplus 0x020 /* Has C++ linkage and cannot appear in a
|
| 2113 | | - ** C header file */
|
| 2114 | | -#define DP_ExternCReqd 0x040 /* Prepend 'extern "C"' in a C++ header.
|
| 2115 | | - ** Prepend nothing in a C header */
|
| 2116 | | -#define DP_ExternReqd 0x080 /* Prepend 'extern "C"' in a C++ header if
|
| 2117 | | - ** DP_Cplusplus is not also set. If DP_Cplusplus
|
| 2118 | | - ** is set or this is a C header then
|
| 2119 | | - ** prepend 'extern' */
|
| 2120 | | -
|
| 2121 | | -/*
|
| 2122 | | -** Convenience macros for dealing with declaration properties
|
| 2123 | | -*/
|
| 2124 | | -#define DeclHasProperty(D,P) (((D)->flags&(P))==(P))
|
| 2125 | | -#define DeclHasAnyProperty(D,P) (((D)->flags&(P))!=0)
|
| 2126 | | -#define DeclSetProperty(D,P) (D)->flags |= (P)
|
| 2127 | | -#define DeclClearProperty(D,P) (D)->flags &= ~(P)
|
| 2128 | | -
|
| 2129 | | -/*
|
| 2130 | | -** These are state properties of the parser. Each of the values is
|
| 2131 | | -** distinct from the DP_ values above so that both can be used in
|
| 2132 | | -** the same "flags" field.
|
| 2133 | | -**
|
| 2134 | | -** Be careful not to confuse PS_Export with DP_Export or
|
| 2135 | | -** PS_Local with DP_Local. Their names are similar, but the meanings
|
| 2136 | | -** of these flags are very different.
|
| 2137 | | -*/
|
| 2138 | | -#define PS_Extern 0x000800 /* "extern" has been seen */
|
| 2139 | | -#define PS_Export 0x001000 /* If between "#if EXPORT_INTERFACE"
|
| 2140 | | - ** and "#endif" */
|
| 2141 | | -#define PS_Export2 0x002000 /* If "EXPORT" seen */
|
| 2142 | | -#define PS_Typedef 0x004000 /* If "typedef" has been seen */
|
| 2143 | | -#define PS_Static 0x008000 /* If "static" has been seen */
|
| 2144 | | -#define PS_Interface 0x010000 /* If within #if INTERFACE..#endif */
|
| 2145 | | -#define PS_Method 0x020000 /* If "::" token has been seen */
|
| 2146 | | -#define PS_Local 0x040000 /* If within #if LOCAL_INTERFACE..#endif */
|
| 2147 | | -#define PS_Local2 0x080000 /* If "LOCAL" seen. */
|
| 2148 | | -#define PS_Public 0x100000 /* If "PUBLIC" seen. */
|
| 2149 | | -#define PS_Protected 0x200000 /* If "PROTECTED" seen. */
|
| 2150 | | -#define PS_Private 0x400000 /* If "PRIVATE" seen. */
|
| 2151 | | -#define PS_PPP 0x700000 /* If any of PUBLIC, PRIVATE, PROTECTED */
|
| 2152 | | -
|
| 2153 | | -/*
|
| 2154 | | -** The following set of flags are ORed into the "flags" field of
|
| 2155 | | -** a Decl in order to identify what type of object is being
|
| 2156 | | -** declared.
|
| 2157 | | -*/
|
| 2158 | | -#define TY_Class 0x00100000
|
| 2159 | | -#define TY_Subroutine 0x00200000
|
| 2160 | | -#define TY_Macro 0x00400000
|
| 2161 | | -#define TY_Typedef 0x00800000
|
| 2162 | | -#define TY_Variable 0x01000000
|
| 2163 | | -#define TY_Structure 0x02000000
|
| 2164 | | -#define TY_Union 0x04000000
|
| 2165 | | -#define TY_Enumeration 0x08000000
|
| 2166 | | -#define TY_Defunct 0x10000000 /* Used to erase a declaration */
|
| 2167 | | -
|
| 2168 | | -/*
|
| 2169 | | -** Each nested #if (or #ifdef or #ifndef) is stored in a stack of
|
| 2170 | | -** instances of the following structure.
|
| 2171 | | -*/
|
| 2172 | | -typedef struct Ifmacro Ifmacro;
|
| 2173 | | -struct Ifmacro {
|
| 2174 | | - int nLine; /* Line number where this macro occurs */
|
| 2175 | | - char *zCondition; /* Text of the condition for this macro */
|
| 2176 | | - Ifmacro *pNext; /* Next down in the stack */
|
| 2177 | | - int flags; /* Can hold PS_Export, PS_Interface or PS_Local flags */
|
| 2178 | | -};
|
| 2179 | | -
|
| 2180 | | -/*
|
| 2181 | | -** When parsing a file, we need to keep track of what other files have
|
| 2182 | | -** be #include-ed. For each #include found, we create an instance of
|
| 2183 | | -** the following structure.
|
| 2184 | | -*/
|
| 2185 | | -typedef struct Include Include;
|
| 2186 | | -struct Include {
|
| 2187 | | - char *zFile; /* The name of file include. Includes "" or <> */
|
| 2188 | | - char *zIf; /* If not NULL, #include should be enclosed in #if */
|
| 2189 | | - char *zLabel; /* A unique label used to test if this #include has
|
| 2190 | | - * appeared already in a file or not */
|
| 2191 | | - Include *pNext; /* Previous include file, or NULL if this is the first */
|
| 2192 | | -};
|
| 2193 | | -
|
| 2194 | | -/*
|
| 2195 | | -** Identifiers found in a source file that might be used later to provoke
|
| 2196 | | -** the copying of a declaration into the corresponding header file are
|
| 2197 | | -** stored in a hash table as instances of the following structure.
|
| 2198 | | -*/
|
| 2199 | | -typedef struct Ident Ident;
|
| 2200 | | -struct Ident {
|
| 2201 | | - char *zName; /* The text of this identifier */
|
| 2202 | | - Ident *pCollide; /* Next identifier with the same hash */
|
| 2203 | | - Ident *pNext; /* Next identifier in a list of them all */
|
| 2204 | | -};
|
| 2205 | | -
|
| 2206 | | -/*
|
| 2207 | | -** A complete table of identifiers is stored in an instance of
|
| 2208 | | -** the next structure.
|
| 2209 | | -*/
|
| 2210 | | -#define IDENT_HASH_SIZE 2237
|
| 2211 | | -typedef struct IdentTable IdentTable;
|
| 2212 | | -struct IdentTable {
|
| 2213 | | - Ident *pList; /* List of all identifiers in this table */
|
| 2214 | | - Ident *apTable[IDENT_HASH_SIZE]; /* The hash table */
|
| 2215 | | -};
|
| 2216 | | -
|
| 2217 | | -/*
|
| 2218 | | -** The following structure holds all information for a single
|
| 2219 | | -** source file named on the command line of this program.
|
| 2220 | | -*/
|
| 2221 | | -typedef struct InFile InFile;
|
| 2222 | | -struct InFile {
|
| 2223 | | - char *zSrc; /* Name of input file */
|
| 2224 | | - char *zHdr; /* Name of the generated .h file for this input.
|
| 2225 | | - ** Will be NULL if input is to be scanned only */
|
| 2226 | | - int flags; /* One or more DP_, PS_ and/or TY_ flags */
|
| 2227 | | - InFile *pNext; /* Next input file in the list of them all */
|
| 2228 | | - IdentTable idTable; /* All identifiers in this input file */
|
| 2229 | | -};
|
| 2230 | | -
|
| 2231 | | -/*
|
| 2232 | | -** An unbounded string is able to grow without limit. We use these
|
| 2233 | | -** to construct large in-memory strings from lots of smaller components.
|
| 2234 | | -*/
|
| 2235 | | -typedef struct String String;
|
| 2236 | | -struct String {
|
| 2237 | | - int nAlloc; /* Number of bytes allocated */
|
| 2238 | | - int nUsed; /* Number of bytes used (not counting nul terminator) */
|
| 2239 | | - char *zText; /* Text of the string */
|
| 2240 | | -};
|
| 2241 | | -
|
| 2242 | | -/*
|
| 2243 | | -** The following structure contains a lot of state information used
|
| 2244 | | -** while generating a .h file. We put the information in this structure
|
| 2245 | | -** and pass around a pointer to this structure, rather than pass around
|
| 2246 | | -** all of the information separately. This helps reduce the number of
|
| 2247 | | -** arguments to generator functions.
|
| 2248 | | -*/
|
| 2249 | | -typedef struct GenState GenState;
|
| 2250 | | -struct GenState {
|
| 2251 | | - String *pStr; /* Write output to this string */
|
| 2252 | | - IdentTable *pTable; /* A table holding the zLabel of every #include that
|
| 2253 | | - * has already been generated. Used to avoid
|
| 2254 | | - * generating duplicate #includes. */
|
| 2255 | | - const char *zIf; /* If not NULL, then we are within a #if with
|
| 2256 | | - * this argument. */
|
| 2257 | | - int nErr; /* Number of errors */
|
| 2258 | | - const char *zFilename; /* Name of the source file being scanned */
|
| 2259 | | - int flags; /* Various flags (DP_ and PS_ flags above) */
|
| 2260 | | -};
|
| 2261 | | -
|
| 2262 | | -/*
|
| 2263 | | -** The following text line appears at the top of every file generated
|
| 2264 | | -** by this program. By recognizing this line, the program can be sure
|
| 2265 | | -** never to read a file that it generated itself.
|
| 2266 | | -**
|
| 2267 | | -** The "#undef INTERFACE" part is a hack to work around a name collision
|
| 2268 | | -** in MSVC 2008.
|
| 2269 | | -*/
|
| 2270 | | -const char zTopLine[] =
|
| 2271 | | - "/* \aThis file was automatically generated. Do not edit! */\n"
|
| 2272 | | - "#undef INTERFACE\n";
|
| 2273 | | -#define nTopLine (sizeof(zTopLine)-1)
|
| 2274 | | -
|
| 2275 | | -/*
|
| 2276 | | -** The name of the file currently being parsed.
|
| 2277 | | -*/
|
| 2278 | | -static const char *zFilename;
|
| 2279 | | -
|
| 2280 | | -/*
|
| 2281 | | -** The stack of #if macros for the file currently being parsed.
|
| 2282 | | -*/
|
| 2283 | | -static Ifmacro *ifStack = 0;
|
| 2284 | | -
|
| 2285 | | -/*
|
| 2286 | | -** A list of all files that have been #included so far in a file being
|
| 2287 | | -** parsed.
|
| 2288 | | -*/
|
| 2289 | | -static Include *includeList = 0;
|
| 2290 | | -
|
| 2291 | | -/*
|
| 2292 | | -** The last block comment seen.
|
| 2293 | | -*/
|
| 2294 | | -static Token *blockComment = 0;
|
| 2295 | | -
|
| 2296 | | -/*
|
| 2297 | | -** The following flag is set if the -doc flag appears on the
|
| 2298 | | -** command line.
|
| 2299 | | -*/
|
| 2300 | | -static int doc_flag = 0;
|
| 2301 | | -
|
| 2302 | | -/*
|
| 2303 | | -** If the following flag is set, then makeheaders will attempt to
|
| 2304 | | -** generate prototypes for static functions and procedures.
|
| 2305 | | -*/
|
| 2306 | | -static int proto_static = 0;
|
| 2307 | | -
|
| 2308 | | -/*
|
| 2309 | | -** A list of all declarations. The list is held together using the
|
| 2310 | | -** pNext field of the Decl structure.
|
| 2311 | | -*/
|
| 2312 | | -static Decl *pDeclFirst; /* First on the list */
|
| 2313 | | -static Decl *pDeclLast; /* Last on the list */
|
| 2314 | | -
|
| 2315 | | -/*
|
| 2316 | | -** A hash table of all declarations
|
| 2317 | | -*/
|
| 2318 | | -#define DECL_HASH_SIZE 3371
|
| 2319 | | -static Decl *apTable[DECL_HASH_SIZE];
|
| 2320 | | -
|
| 2321 | | -/*
|
| 2322 | | -** The TEST macro must be defined to something. Make sure this is the
|
| 2323 | | -** case.
|
| 2324 | | -*/
|
| 2325 | | -#ifndef TEST
|
| 2326 | | -# define TEST 0
|
| 2327 | | -#endif
|
| 2328 | | -
|
| 2329 | | -#ifdef NOT_USED
|
| 2330 | | -/*
|
| 2331 | | -** We do our own assertion macro so that we can have more control
|
| 2332 | | -** over debugging.
|
| 2333 | | -*/
|
| 2334 | | -#define Assert(X) if(!(X)){ CantHappen(__LINE__); }
|
| 2335 | | -#define CANT_HAPPEN CantHappen(__LINE__)
|
| 2336 | | -static void CantHappen(int iLine){
|
| 2337 | | - fprintf(stderr,"Assertion failed on line %d\n",iLine);
|
| 2338 | | - *(char*)1 = 0; /* Force a core-dump */
|
| 2339 | | -}
|
| 2340 | | -#endif
|
| 2341 | | -
|
| 2342 | | -/*
|
| 2343 | | -** Memory allocation functions that are guaranteed never to return NULL.
|
| 2344 | | -*/
|
| 2345 | | -static void *SafeMalloc(int nByte){
|
| 2346 | | - void *p = malloc( nByte );
|
| 2347 | | - if( p==0 ){
|
| 2348 | | - fprintf(stderr,"Out of memory. Can't allocate %d bytes.\n",nByte);
|
| 2349 | | - exit(1);
|
| 2350 | | - }
|
| 2351 | | - return p;
|
| 2352 | | -}
|
| 2353 | | -static void SafeFree(void *pOld){
|
| 2354 | | - if( pOld ){
|
| 2355 | | - free(pOld);
|
| 2356 | | - }
|
| 2357 | | -}
|
| 2358 | | -static void *SafeRealloc(void *pOld, int nByte){
|
| 2359 | | - void *p;
|
| 2360 | | - if( pOld==0 ){
|
| 2361 | | - p = SafeMalloc(nByte);
|
| 2362 | | - }else{
|
| 2363 | | - p = realloc(pOld, nByte);
|
| 2364 | | - if( p==0 ){
|
| 2365 | | - fprintf(stderr,
|
| 2366 | | - "Out of memory. Can't enlarge an allocation to %d bytes\n",nByte);
|
| 2367 | | - exit(1);
|
| 2368 | | - }
|
| 2369 | | - }
|
| 2370 | | - return p;
|
| 2371 | | -}
|
| 2372 | | -static char *StrDup(const char *zSrc, int nByte){
|
| 2373 | | - char *zDest;
|
| 2374 | | - if( nByte<=0 ){
|
| 2375 | | - nByte = strlen(zSrc);
|
| 2376 | | - }
|
| 2377 | | - zDest = SafeMalloc( nByte + 1 );
|
| 2378 | | - strncpy(zDest,zSrc,nByte);
|
| 2379 | | - zDest[nByte] = 0;
|
| 2380 | | - return zDest;
|
| 2381 | | -}
|
| 2382 | | -
|
| 2383 | | -/*
|
| 2384 | | -** Return TRUE if the character X can be part of an identifier
|
| 2385 | | -*/
|
| 2386 | | -#define ISALNUM(X) ((X)=='_' || isalnum(X))
|
| 2387 | | -
|
| 2388 | | -/*
|
| 2389 | | -** Routines for dealing with unbounded strings.
|
| 2390 | | -*/
|
| 2391 | | -static void StringInit(String *pStr){
|
| 2392 | | - pStr->nAlloc = 0;
|
| 2393 | | - pStr->nUsed = 0;
|
| 2394 | | - pStr->zText = 0;
|
| 2395 | | -}
|
| 2396 | | -static void StringReset(String *pStr){
|
| 2397 | | - SafeFree(pStr->zText);
|
| 2398 | | - StringInit(pStr);
|
| 2399 | | -}
|
| 2400 | | -static void StringAppend(String *pStr, const char *zText, int nByte){
|
| 2401 | | - if( nByte<=0 ){
|
| 2402 | | - nByte = strlen(zText);
|
| 2403 | | - }
|
| 2404 | | - if( pStr->nUsed + nByte >= pStr->nAlloc ){
|
| 2405 | | - if( pStr->nAlloc==0 ){
|
| 2406 | | - pStr->nAlloc = nByte + 100;
|
| 2407 | | - pStr->zText = SafeMalloc( pStr->nAlloc );
|
| 2408 | | - }else{
|
| 2409 | | - pStr->nAlloc = pStr->nAlloc*2 + nByte;
|
| 2410 | | - pStr->zText = SafeRealloc(pStr->zText, pStr->nAlloc);
|
| 2411 | | - }
|
| 2412 | | - }
|
| 2413 | | - strncpy(&pStr->zText[pStr->nUsed],zText,nByte);
|
| 2414 | | - pStr->nUsed += nByte;
|
| 2415 | | - pStr->zText[pStr->nUsed] = 0;
|
| 2416 | | -}
|
| 2417 | | -#define StringGet(S) ((S)->zText?(S)->zText:"")
|
| 2418 | | -
|
| 2419 | | -/*
|
| 2420 | | -** Compute a hash on a string. The number returned is a non-negative
|
| 2421 | | -** value between 0 and 2**31 - 1
|
| 2422 | | -*/
|
| 2423 | | -static int Hash(const char *z, int n){
|
| 2424 | | - int h = 0;
|
| 2425 | | - if( n<=0 ){
|
| 2426 | | - n = strlen(z);
|
| 2427 | | - }
|
| 2428 | | - while( n-- ){
|
| 2429 | | - h = h ^ (h & 0x7fffffff; free software; are; you can redistribute it and/or
|
| 2430 | | -** modify it under the terms of the Simplified BSD License (also
|
| 2431 | | -** known as the "2-Clause License" or "FreeBSD License".)
|
| 2432 | | -**
|
| 2433 | | -** Copyright 1993 D. Richard Hipp. All rights reserved.
|
| 2434 | | -**
|
| 2435 | | -** Redistribution and use in source and binary forms, with or
|
| 2436 | | -** without modification, are permitted provided that the following
|
| 2437 | | -** conditions are met:
|
| 2438 | | -**
|
| 2439 | | -** 1. Redistributions of source code must retain the above copyright
|
| 2440 | | -** notice, this list of conditions and the following disclaimer.
|
| 2441 | | -**
|
| 2442 | | -** 2. Redistributions in binary form must reproduce the above copyright
|
| 2443 | | -** notice, thisprintf(pDecl/*
|
| 2444 | | -** This program is free software; you can redistribute it and/or
|
| 2445 | | -** modify it under the terms of the Simplified BSD License (also
|
| 2446 | | -** known as the "2-Clause License" or "FreeBSD License".)
|
| 2447 | | -**
|
| 2448 | | -** Copyright 1993 D. Richard Hipp. All rights reserved.
|
| 2449 | | -**
|
| 2450 | | -** Redistribution and use in source and binary forms, with or
|
| 2451 | | -** without modification, are permitted provided that the following
|
| 2452 | | -** conditions are met:
|
| 2453 | | -**
|
| 2454 | | -** 1. Redistributions of source code must retain the above copyright
|
| 2455 | | -** notice, this list of conditions and the following disclaimer.
|
| 2456 | | -**
|
| 2457 | | -** 2. Redistributions in binary form must reproduce the above copyright
|
| 2458 | | -** notice, this list of conditions and the following disclaimer in
|
| 2459 | | -** the documentation and/or other materials provided with the
|
| 2460 | | -** distribution.
|
| 2461 | | -**
|
| 2462 | | -** This software is provided "as is" and any express or implied warranties,
|
| 2463 | | -** including, but not limited to, the implied warranties of merchantability
|
| 2464 | | -** and fitness for a particular purpose are disclaimed. In no event shall
|
| 2465 | | -** the author or contributors be liable for any direct, indirect, incidental,
|
| 2466 | | -** special, exemplary, or consequential damages (including, but not limited
|
| 2467 | | -** to, procurement of substitute goods or services; loss of use, data or
|
| 2468 | | -** profits; or business interruption) however caused and on any theory of
|
| 2469 | | -** liability, whether in contract, strict liability, or tort (including
|
| 2470 | | -** negligence or otherwise) arising in any way out of the use of this
|
| 2471 | | -** software, even if advised of the possibility of such damage.
|
| 2472 | | -**
|
| 2473 | | -** This program is distributed in the hope that it will be useful,
|
| 2474 | | -** but without any warranty; without even the implied warranty of
|
| 2475 | | -** merchantability or fitness for a particular purpose.
|
| 2476 | | -*/
|
| 2477 | | -#include <stdio.h>
|
| 2478 | | -#include <stdlib.h>
|
| 2479 | | -#include <ctype.h>
|
| 2480 | | -#include <memory.h>
|
| 2481 | | -#include <sys/stat.h>
|
| 2482 | | -#include <assert.h>
|
| 2483 | | -#include <string.h>
|
| 2484 | | -
|
| 2485 | | -#if defined( __MINGW32__) || defined(__DMC__) || defined(_MSC_VER) || defined(__POCC__)
|
| 2486 | | -# ifndef WIN32
|
| 2487 | | -# define WIN32
|
| 2488 | | -# endif
|
| 2489 | | -#else
|
| 2490 | | -# include <unistd.h>
|
| 2491 | | -#endif
|
| 2492 | | -
|
| 2493 | | -/*
|
| 2494 | | -** Macros for debugging.
|
| 2495 | | -*/
|
| 2496 | | -#ifdef DEBUG
|
| 2497 | | -static int debugMask = 0;
|
| 2498 | | -# define debug0(F,M) if( (F)&debugMask ){ fprintf(stderr,M); }
|
| 2499 | | -# define debug1(F,M,A) if( (F)&debugMask ){ fprintf(stderr,M,A); }
|
| 2500 | | -# define debug2(F,M,A,B) if( (F)&debugMask ){ fprintf(stderr,M,A,B); }
|
| 2501 | | -# define debug3(F,M,A,B,C) if( (F)&debugMask ){ fprintf(stderr,M,A,B,C); }
|
| 2502 | | -# define PARSER 0x00000001
|
| 2503 | | -# define DECL_DUMP 0x00000002
|
| 2504 | | -# define TOKENIZER 0x00000004
|
| 2505 | | -#else
|
| 2506 | | -# define debug0(Flags, Format)
|
| 2507 | | -# define debug1(Flags, Format, A)
|
| 2508 | | -# define debug2(Flags, Format, A, B)
|
| 2509 | | -# define debug3(Flags, Format, A, B, C)
|
| 2510 | | -#endif
|
| 2511 | | -
|
| 2512 | | -/*
|
| 2513 | | -** The following macros are purely for the purpose of testing this
|
| 2514 | | -** program on itself. They don't really contribute to the code.
|
| 2515 | | -*/
|
| 2516 | | -#define INTERFACE 1
|
| 2517 | | -#define EXPORT_INTERFACE 1
|
| 2518 | | -#define EXPORT
|
| 2519 | | -
|
| 2520 | | -/*
|
| 2521 | | -** Each token in a source file is represented by an instance of
|
| 2522 | | -** the following structure. Tokens are collected onto a list.
|
| 2523 | | -*/
|
| 2524 | | -typedef struct Token Token;
|
| 2525 | | -struct Token {
|
| 2526 | | - const char *zText; /* The text of the token */
|
| 2527 | | - int nText; /* Number of characters in the token's text */
|
| 2528 | | - int eType; /* The type of this token */
|
| 2529 | | - int nLine; /* The line number on which the token starts */
|
| 2530 | | - Token *pComment; /* Most recent block comment before this token */
|
| 2531 | | - Token *pNext; /* Next token on the list */
|
| 2532 | | - Token *pPrev; /* Previous token on the list */
|
| 2533 | | -};
|
| 2534 | | -
|
| 2535 | | -/*
|
| 2536 | | -** During tokenization, information about the state of the input
|
| 2537 | | -** stream is held in an instance of the following structure
|
| 2538 | | -*/
|
| 2539 | | -typedef struct InStream InStream;
|
| 2540 | | -struct InStream {
|
| 2541 | | - const char *z; /* Complete text of the input */
|
| 2542 | | - int i; /* Next character to read from the input */
|
| 2543 | | - int nLine; /* The line number for character z[i] */
|
| 2544 | | -};
|
| 2545 | | -
|
| 2546 | | -/*
|
| 2547 | | -** Each declaration in the C or C++ source files is parsed out and stored as
|
| 2548 | | -** an instance of the following structure.
|
| 2549 | | -**
|
| 2550 | | -** A "forward declaration" is a declaration that an object exists that
|
| 2551 | | -** doesn't tell about the objects structure. A typical forward declaration
|
| 2552 | | -** is:
|
| 2553 | | -**
|
| 2554 | | -** struct Xyzzy;
|
| 2555 | | -**
|
| 2556 | | -** Not every object has a forward declaration. If it does, thought, the
|
| 2557 | | -** forward declaration will be contained in the zFwd field for C and
|
| 2558 | | -** the zFwdCpp for C++. The zDecl field contains the complete
|
| 2559 | | -** declaration text.
|
| 2560 | | -*/
|
| 2561 | | -typedef struct Decl Decl;
|
| 2562 | | -struct Decl {
|
| 2563 | | - char *zName; /* Name of the object being declared. The appearance
|
| 2564 | | - ** of this name is a source file triggers the declaration
|
| 2565 | | - ** to be added to the header for that file. */
|
| 2566 | | - const char *zFile; /* File from which extracted. */
|
| 2567 | | - char *zIf; /* Surround the declaration with this #if */
|
| 2568 | | - char *zFwd; /* A forward declaration. NULL if there is none. */
|
| 2569 | | - char *zFwdCpp; /* Use this forward declaration for C++. */
|
| 2570 | | - char *zDecl; /* A full declaration of this object */
|
| 2571 | | - char *zExtra; /* Extra declaration text inserted into class objects */
|
| 2572 | | - int extraType; /* Last public:, protected: or private: in zExtraDecl */
|
| 2573 | | - struct Include *pInclude; /* #includes that come before this declaration */
|
| 2574 | | - int flags; /* See the "Properties" below */
|
| 2575 | | - Token *pComment; /* A block comment associated with this declaration */
|
| 2576 | | - Token tokenCode; /* Implementation of functions and procedures */
|
| 2577 | | - Decl *pSameName; /* Next declaration with the same "zName" */
|
| 2578 | | - Decl *pSameHash; /* Next declaration with same hash but different zName */
|
| 2579 | | - Decl *pNext; /* Next declaration with a different name */
|
| 2580 | | -};
|
| 2581 | | -
|
| 2582 | | -/*
|
| 2583 | | -** Properties associated with declarations.
|
| 2584 | | -**
|
| 2585 | | -** DP_Forward and DP_Declared are used during the generation of a single
|
| 2586 | | -** header file in order to prevent duplicate declarations and definitions.
|
| 2587 | | -** DP_Forward is set after the object has been given a forward declaration
|
| 2588 | | -** and DP_Declared is set after the object gets a full declarations.
|
| 2589 | | -** (Example: A forward declaration is "typedef struct Abc Abc;" and the
|
| 2590 | | -** full declaration is "struct Abc { int a; float b; };".)
|
| 2591 | | -**
|
| 2592 | | -** The DP_Export and DP_Local flags are more permanent. They mark objects
|
| 2593 | | -** that have EXPORT scope and LOCAL scope respectively. If both of these
|
| 2594 | | -** marks are missing, then the object has library scope. The meanings of
|
| 2595 | | -** the scopes are as follows:
|
| 2596 | | -**
|
| 2597 | | -** LOCAL scope The object is only usable within the file in
|
| 2598 | | -** which it is declared.
|
| 2599 | | -**
|
| 2600 | | -** library scope The object is visible and usable within other
|
| 2601 | | -** files in the same project. By if the project is
|
| 2602 | | -** a library, then the object is not visible to users
|
| 2603 | | -** of the library. (i.e. the object does not appear
|
| 2604 | | -** in the output when using the -H option.)
|
| 2605 | | -**
|
| 2606 | | -** EXPORT scope The object is visible and usable everywhere.
|
| 2607 | | -**
|
| 2608 | | -** The DP_Flag is a temporary use flag that is used during processing to
|
| 2609 | | -** prevent an infinite loop. It's use is localized.
|
| 2610 | | -**
|
| 2611 | | -** The DP_Cplusplus, DP_ExternCReqd and DP_ExternReqd flags are permanent
|
| 2612 | | -** and are used to specify what type of declaration the object requires.
|
| 2613 | | -*/
|
| 2614 | | -#define DP_Forward 0x001 /* Has a forward declaration in this file */
|
| 2615 | | -#define DP_Declared 0x002 /* Has a full declaration in this file */
|
| 2616 | | -#define DP_Export 0x004 /* Export this declaration */
|
| 2617 | | -#define DP_Local 0x008 /* Declare in its home file only */
|
| 2618 | | -#define DP_Flag 0x010 /* Use to mark a subset of a Decl list
|
| 2619 | | - ** for special processing */
|
| 2620 | | -#define DP_Cplusplus 0x020 /* Has C++ linkage and cannot appear in a
|
| 2621 | | - ** C header file */
|
| 2622 | | -#define DP_ExternCReqd 0x040 /* Prepend 'extern "C"' in a C++ header.
|
| 2623 | | - ** Prepend nothing in a C header */
|
| 2624 | | -#define DP_ExternReqd 0x080 /* Prepend 'extern "C"' in a C++ header if
|
| 2625 | | - ** DP_Cplusplus is not also set. If DP_Cplusplus
|
| 2626 | | - ** is set or this is a C header then
|
| 2627 | | - ** prepend 'extern' */
|
| 2628 | | -
|
| 2629 | | -/*
|
| 2630 | | -** Convenience macros for dealing with declaration properties
|
| 2631 | | -*/
|
| 2632 | | -#define DeclHasProperty(D,P) (((D)->flags&(P))==(P))
|
| 2633 | | -#define DeclHasAnyProperty(D,P) (((D)->flags&(P))!=0)
|
| 2634 | | -#define DeclSetProperty(D,P) (D)->flags |= (P)
|
| 2635 | | -#define DeclClearProperty(D,P) (D)->flags &= ~(P)
|
| 2636 | | -
|
| 2637 | | -/*
|
| 2638 | | -** These are state properties of the parser. Each of the values is
|
| 2639 | | -** distinct from the DP_ values above so that both can be used in
|
| 2640 | | -** the same "flags" field.
|
| 2641 | | -**
|
| 2642 | | -** Be careful not to confuse PS_Export with DP_Export or
|
| 2643 | | -** PS_Local with DP_Local. Their names are similar, but the meanings
|
| 2644 | | -** of these flags are very different.
|
| 2645 | | -*/
|
| 2646 | | -#define PS_Extern 0x000800 /* "extern" has been seen */
|
| 2647 | | -#define PS_Export 0x001000 /* If between "#if EXPORT_INTERFACE"
|
| 2648 | | - ** and "#endif" */
|
| 2649 | | -#define PS_Export2 0x002000 /* If "EXPORT" seen */
|
| 2650 | | -#define PS_Typedef 0x004000 /* If "typedef" has been seen */
|
| 2651 | | -#define PS_Static 0x008000 /* If "static" has been seen */
|
| 2652 | | -#define PS_Interface 0x010000 /* If within #if INTERFACE..#endif */
|
| 2653 | | -#define PS_Method 0x020000 /* If "::" token has been seen */
|
| 2654 | | -#define PS_Local 0x040000 /* If within #if LOCAL_INTERFACE..#endif */
|
| 2655 | | -#define PS_Local2 0x080000 /* If "LOCAL" seen. */
|
| 2656 | | -#define PS_Public 0x100000 /* If "PUBLIC" seen. */
|
| 2657 | | -#define PS_Protected 0x200000 /* If "PROTECTED" seen. */
|
| 2658 | | -#define PS_Private 0x400000 /* If "PRIVATE" seen. */
|
| 2659 | | -#define PS_PPP 0x700000 /* If any of PUBLIC, PRIVATE, PROTECTED */
|
| 2660 | | -
|
| 2661 | | -/*
|
| 2662 | | -** The following set of flags are ORed into the "flags" field of
|
| 2663 | | -** a Decl in order to identify what type of object is being
|
| 2664 | | -** declared.
|
| 2665 | | -*/
|
| 2666 | | -#define TY_Class 0x00100000
|
| 2667 | | -#define TY_Subroutine 0x00200000
|
| 2668 | | -#define TY_Macro 0x00400000
|
| 2669 | | -#define TY_Typedef 0x00800000
|
| 2670 | | -#define TY_Variable 0x01000000
|
| 2671 | | -#define TY_Structure 0x02000000
|
| 2672 | | -#define TY_Union 0x04000000
|
| 2673 | | -#define TY_Enumeration 0x08000000
|
| 2674 | | -#define TY_Defunct 0x10000000 /* Used to erase a declaration */
|
| 2675 | | -
|
| 2676 | | -/*
|
| 2677 | | -** Each nested #if (or #ifdef or #ifndef) is stored in a stack of
|
| 2678 | | -** instances of the following structure.
|
| 2679 | | -*/
|
| 2680 | | -typedef struct Ifmacro Ifmacro;
|
| 2681 | | -struct Ifmacro {
|
| 2682 | | - int nLine; /* Line number where this macro occurs */
|
| 2683 | | - char *zCondition; /* Text of the condition for this macro */
|
| 2684 | | - Ifmacro *pNext; /* Next down in the stack */
|
| 2685 | | - int flags; /* Can hold PS_Export, PS_Interface or PS_Local flags */
|
| 2686 | | -};
|
| 2687 | | -
|
| 2688 | | -/*
|
| 2689 | | -** When parsing a file, we need to keep track of what other files have
|
| 2690 | | -** be #include-ed. For each #include found, we create an instance of
|
| 2691 | | -** the following structure.
|
| 2692 | | -*/
|
| 2693 | | -typedef struct Include Include;
|
| 2694 | | -struct Include {
|
| 2695 | | - char *zFile; /* The name of file include. Includes "" or <> */
|
| 2696 | | - char *zIf; /* If not NULL, #include should be enclosed in #if */
|
| 2697 | | - char *zLabel; /* A unique label used to test if this #include has
|
| 2698 | | - * appeared already in a file or not */
|
| 2699 | | - Include *pNext; /* Previous include file, or NULL if this is the first */
|
| 2700 | | -};
|
| 2701 | | -
|
| 2702 | | -/*
|
| 2703 | | -** Identifiers found in a source file that might be used later to provoke
|
| 2704 | | -** the copying of a declaration into the corresponding header file are
|
| 2705 | | -** stored in a hash table as instances of the following structure.
|
| 2706 | | -*/
|
| 2707 | | -typedef struct Ident Ident;
|
| 2708 | | -struct Ident {
|
| 2709 | | - char *zName; /* The text of this identifier */
|
| 2710 | | - Ident *pCollide; /* Next identifier with the same hash */
|
| 2711 | | - Ident *pNext; /* Next identifier in a list of them all */
|
| 2712 | | -};
|
| 2713 | | -
|
| 2714 | | -/*
|
| 2715 | | -** A complete table of identifiers is stored in an instance of
|
| 2716 | | -** the next structure.
|
| 2717 | | -*/
|
| 2718 | | -#define IDENT_HASH_SIZE 2237
|
| 2719 | | -typedef struct IdentTable IdentTable;
|
| 2720 | | -struct IdentTable {
|
| 2721 | | - Ident *pList; /* List of all identifiers in this table */
|
| 2722 | | - Ident *apTable[IDENT_HASH_SIZE]; /* The hash table */
|
| 2723 | | -};
|
| 2724 | | -
|
| 2725 | | -/*
|
| 2726 | | -** The following structure holds all information for a single
|
| 2727 | | -** source file named on the command line of this program.
|
| 2728 | | -*/
|
| 2729 | | -typedef struct InFile InFile;
|
| 2730 | | -struct InFile {
|
| 2731 | | - char *zSrc; /* Name of input file */
|
| 2732 | | - char *zHdr; /* Name of the generated .h file for this input.
|
| 2733 | | - ** Will be NULL if input is to be scanned only */
|
| 2734 | | - int flags; /* One or more DP_, PS_ and/or TY_ flags */
|
| 2735 | | - InFile *pNext; /* Next input file in the list of them all */
|
| 2736 | | - IdentTable idTable; /* All identifiers in this input file */
|
| 2737 | | -};
|
| 2738 | | -
|
| 2739 | | -/*
|
| 2740 | | -** An unbounded string is able to grow without limit. We use these
|
| 2741 | | -** to construct large in-memory strings from lots of smaller components.
|
| 2742 | | -*/
|
| 2743 | | -typedef struct String String;
|
| 2744 | | -struct String {
|
| 2745 | | - int nAlloc; /* Number of bytes allocated */
|
| 2746 | | - int nUsed; /* Number of bytes used (not counting nul terminator) */
|
| 2747 | | - char *zText; /* Text of the string */
|
| 2748 | | -};
|
| 2749 | | -
|
| 2750 | | -/*
|
| 2751 | | -** The following structure contains a lot of state information used
|
| 2752 | | -** while generating a .h file. We put the information in this structure
|
| 2753 | | -** and pass around a pointer to this structure, rather than pass around
|
| 2754 | | -** all of the information separately. This helps reduce the number of
|
| 2755 | | -** arguments to generator functions.
|
| 2756 | | -*/
|
| 2757 | | -typedef struct GenState GenState;
|
| 2758 | | -struct GenState {
|
| 2759 | | - String *pStr; /* Write output to this string */
|
| 2760 | | - IdentTable *pTable; /* A table holding the zLabel of every #include that
|
| 2761 | | - * has already been generated. Used to avoid
|
| 2762 | | - |