convert line ends
[canaan.git] / prj / tech / libsrc / dstruct / symtab.h
blob4f5197465ec005ec783bc8fc1c4b208498251f54
1 // SYM.H Symbol table header file
2 // Rex E. Bradford (REX)
3 /*
4 * $Header: x:/prj/tech/libsrc/dstruct/RCS/symtab.h 1.4 1996/10/21 09:35:06 TOML Exp $
5 * $Log: symtab.h $
6 * Revision 1.4 1996/10/21 09:35:06 TOML
7 * msvc
9 * Revision 1.3 1996/01/22 15:39:14 DAVET
11 * Added cplusplus stuff.
13 * Revision 1.2 1994/02/10 15:58:56 eric
14 * Fixed unbound parenthesis in SymSetUserSymDel.
16 * Revision 1.1 1993/05/03 10:53:49 rex
17 * Initial revision
21 #ifndef SYM_H
22 #define SYM_H
24 #include <types.h>
25 #include <fix.h>
26 #include <slist.h>
27 #include <llist.h>
28 #include <hheap.h>
30 // Symbol types, extensible by user
32 #ifdef __cplusplus
33 extern "C" {
34 #endif
36 #define TSYM_NULL 0 // invalid symbol type
37 #define TSYM_INTEGER 1 // 32-bit integer
38 #define TSYM_FIX 2 // 32-bit fixed-point number
39 #define TSYM_STRING 3 // 32-bit string
40 #define TSYM_USER 4 // user-defined types start here
42 typedef ushort SymType;
44 // Symbol layout
46 typedef struct {
47 char *name; // symbol name (allocated with StringAlloc)
48 SymType type; // symbol type (TSYM_XXX)
49 union {
50 long integer; // integer value
51 fix fixval; // fixed-point value
52 char *string; // string value
53 void *data; // generic data ptr (for user types convenience)
55 } Symbol;
57 // Symbol table format. A symbol table allocates its big data structures
58 // on the heap, so the symbol table is small enough to put in static
59 // or stack storage, or embedded in some other structure (82 bytes last
60 // time I counted).
62 typedef struct {
63 slist_head *hashTable; // hash table of symbol slist headers
64 short numSymHashEntries; // size of hash table
65 HheapHead symHeap; // symbol heap header
66 llist_head sortedList; // sorted list header
67 ushort flags; // SYMTABF_XXX
68 int (*f_cmp)(char *s1, char *s2); // compare func
69 ushort (*f_hash)(char *s, short hashSize); // hash func
70 void (*f_userSymDel)(Symbol *psym); // user symbol delete func
71 } Symtab;
73 // Symbol table flags, set at SymInitTable() time
75 #define SYMTABF_CASESENSE 0x01 // use case-sensitive comparison
76 #define SYMTABF_ALPHABETIC 0x02 // sort table alphabetically
78 // Handy constants for numHashEntries argument to SymInitTable().
79 // If some other constant is used, it should be prime.
81 #define SYMTAB_HASHSIZE_SMALL 101 // these are all prime
82 #define SYMTAB_HASHSIZE_STD 401
83 #define SYMTAB_HASHSIZE_LARGE 2039
85 // Initialize a symbol table. Set flags from SYMTABF_XXX, and set
86 // numHashEntries to SYMTAB_HASHSIZE_XXX or your own prime number size
88 void SymInitTable(Symtab *psymtab, ushort flags, short numHashEntries);
90 // Free all memory allocated by a symbol table. It may now be
91 // discarded or re-initialized.
93 void SymFreeTable(Symtab *psymtab);
95 // Find a name in the table, return symbol ptr or NULL if not found
97 Symbol *SymFind(Symtab *pymstab, char *nm);
99 // Add to symbol table. If matching name is found and replace argument
100 // is true, current entry will be replaced, irrespective of symbol types.
101 // If matching name is found and replace argument is false, will warn and
102 // return NULL. After SymAdd(), you must fill in type and data field.
104 Symbol *SymAdd(Symtab *psymtab, char *nm, bool replace);
106 // The next 4 are convenience functions which use SymAdd() and then
107 // fill in type and data fields. The returned symbol is fully formed.
109 Symbol *SymAddInt(Symtab *psymtab, char *nm, bool replace, long value);
110 Symbol *SymAddFix(Symtab *psymtab, char *nm, bool replace, fix value);
111 Symbol *SymAddString(Symtab *psymtab, char *nm, bool replace, char *str);
112 Symbol *SymAddUser(Symtab *psymtab, char *nm, bool replace, SymType type,
113 void *data);
115 // Remove symbol from table, freeing all its allocated memory. This
116 // includes calling "user symbol delete" function for user data types.
118 bool SymDel(Symtab *psymtab, char *nm);
120 // Get pointer to first symbol in table in sorted order, or NULL
121 // if table is empty.
123 Symbol *SymFirst(Symtab *psymtab);
125 // Get ptr to next symbol after supplied one. SymFirst() and SymNext()
126 // can be used to traverse the symbol table in sorted order. If you
127 // plan to delete the current symbol from the table while traversing, get
128 // the SymNext() of the current symbol before calling SymDel().
130 Symbol *SymNext(Symtab *psymtab, Symbol *psym);
132 // Dump the symbol table to standard-out. If sortOrder is true, dumps
133 // in sorted order. If it is false, dumps the linked list of each
134 // hash entry.
136 void SymDumpTable(Symtab *psymtab, bool sortOrder);
138 // Prints a symbol to a buffer
140 void SymPrint(char *buff, Symbol *psym);
142 // Set user symbol delete function. When SymDel() is called for
143 // symbol types TSYM_USER and above, this function will be called.
144 // It should delete data pointed to by the 'data' field in the symbol
145 // appropriately.
147 #define SymSetUserSymDel(psymtab,f) ( (psymtab)->f_userSymDel = (f))
149 #ifdef __cplusplus
151 #endif
153 #endif