]> git.sur5r.net Git - cc65/blob - src/cc65/symentry.h
Renamed attribute handling functions. Added SymHasAttr().
[cc65] / src / cc65 / symentry.h
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                symentry.h                                 */
4 /*                                                                           */
5 /*               Symbol table entries for the cc65 C compiler                */
6 /*                                                                           */
7 /*                                                                           */
8 /*                                                                           */
9 /* (C) 2000-2009, Ullrich von Bassewitz                                      */
10 /*                Roemerstrasse 52                                           */
11 /*                D-70794 Filderstadt                                        */
12 /* EMail:         uz@cc65.org                                                */
13 /*                                                                           */
14 /*                                                                           */
15 /* This software is provided 'as-is', without any expressed or implied       */
16 /* warranty.  In no event will the authors be held liable for any damages    */
17 /* arising from the use of this software.                                    */
18 /*                                                                           */
19 /* Permission is granted to anyone to use this software for any purpose,     */
20 /* including commercial applications, and to alter it and redistribute it    */
21 /* freely, subject to the following restrictions:                            */
22 /*                                                                           */
23 /* 1. The origin of this software must not be misrepresented; you must not   */
24 /*    claim that you wrote the original software. If you use this software   */
25 /*    in a product, an acknowledgment in the product documentation would be  */
26 /*    appreciated but is not required.                                       */
27 /* 2. Altered source versions must be plainly marked as such, and must not   */
28 /*    be misrepresented as being the original software.                      */
29 /* 3. This notice may not be removed or altered from any source              */
30 /*    distribution.                                                          */
31 /*                                                                           */
32 /*****************************************************************************/
33
34
35
36 #ifndef SYMENTRY_H
37 #define SYMENTRY_H
38
39
40
41 #include <stdio.h>
42
43 /* common */
44 #include "coll.h"
45 #include "inline.h"
46
47 /* cc65 */
48 #include "datatype.h"
49 #include "declattr.h"
50
51
52
53 /*****************************************************************************/
54 /*                                  Forwards                                 */
55 /*****************************************************************************/
56
57
58
59 struct Segments;
60
61
62
63 /*****************************************************************************/
64 /*                              struct SymEntry                              */
65 /*****************************************************************************/
66
67
68
69 /* Storage classes and flags */
70 #define SC_AUTO         0x0001U         /* Auto variable */
71 #define SC_REGISTER     0x0002U         /* Register variable */
72 #define SC_STATIC       0x0004U         /* Static */
73 #define SC_EXTERN       0x0008U         /* Extern linkage */
74
75 #define SC_ENUM         0x0030U         /* An enum */
76 #define SC_CONST        0x0020U         /* A numeric constant with a type */
77 #define SC_LABEL        0x0040U         /* A goto label */
78 #define SC_PARAM        0x0080U         /* A function parameter */
79 #define SC_FUNC         0x0100U         /* A function */
80
81 #define SC_DEFTYPE      0x0200U         /* Parameter has default type (=int, old style) */
82 #define SC_STORAGE      0x0400U         /* Symbol with associated storage */
83 #define SC_DEFAULT      0x0800U         /* Flag: default storage class was used */
84
85 #define SC_DEF          0x1000U         /* Symbol is defined */
86 #define SC_REF          0x2000U         /* Symbol is referenced */
87
88 #define SC_TYPE         0x4000U         /* This is a type, struct, typedef, etc. */
89 #define SC_STRUCT       0x4001U         /* Struct or union */
90 #define SC_STRUCTFIELD  0x4002U         /* Struct or union field */
91 #define SC_BITFIELD     0x4004U         /* A bit-field inside a struct or union */
92 #define SC_TYPEDEF      0x4008U         /* A typedef */
93
94 #define SC_ZEROPAGE     0x8000U         /* Symbol marked as zeropage */
95
96 #define SC_HAVEATTR     0x10000U        /* Symbol has attributes */
97
98
99
100 /* Symbol table entry */
101 typedef struct SymEntry SymEntry;
102 struct SymEntry {
103     SymEntry*                   NextHash; /* Next entry in hash list */
104     SymEntry*                   PrevSym;  /* Previous symbol in dl list */
105     SymEntry*                   NextSym;  /* Next symbol double linked list */
106     SymEntry*                   Link;     /* General purpose single linked list */
107     struct SymTable*            Owner;    /* Symbol table the symbol is in */
108     unsigned                    Flags;    /* Symbol flags */
109     Type*                       Type;     /* Symbol type */
110     Collection*                 Attr;     /* Attribute list if any */
111     char*                       AsmName;  /* Assembler name if any */
112
113     /* Data that differs for the different symbol types */
114     union {
115
116         /* Offset for locals or struct members */
117         int                     Offs;
118
119         /* Label name for static symbols */
120         unsigned                Label;
121
122         /* Register bank offset and offset of the saved copy on stack for
123          * register variables.
124          */
125         struct {
126             int                 RegOffs;
127             int                 SaveOffs;
128         } R;
129
130         /* Value for constants (including enums) */
131         long                    ConstVal;
132
133         /* Data for structs/unions */
134         struct {
135             struct SymTable*    SymTab;   /* Member symbol table */
136             unsigned            Size;     /* Size of the union/struct */
137         } S;
138
139         /* Data for bit fields */
140         struct {
141             unsigned            Offs;     /* Byte offset into struct */
142             unsigned            BitOffs;  /* Bit offset into storage unit */
143             unsigned            BitWidth; /* Width in bits */
144         } B;
145
146         /* Data for functions */
147         struct {
148             struct FuncDesc*    Func;     /* Function descriptor */
149             struct Segments*    Seg;      /* Segments for this function */
150         } F;
151
152     } V;
153     char                       Name[1]; /* Name, dynamically allocated */
154 };
155
156
157
158 /*****************************************************************************/
159 /*                                   Code                                    */
160 /*****************************************************************************/
161
162
163
164 SymEntry* NewSymEntry (const char* Name, unsigned Flags);
165 /* Create a new symbol table with the given name */
166
167 void FreeSymEntry (SymEntry* E);
168 /* Free a symbol entry */
169
170 void DumpSymEntry (FILE* F, const SymEntry* E);
171 /* Dump the given symbol table entry to the file in readable form */
172
173 #if defined(HAVE_INLINE)
174 INLINE int SymIsBitField (const SymEntry* Sym)
175 /* Return true if the given entry is a bit-field entry */
176 {
177     return ((Sym->Flags & SC_BITFIELD) == SC_BITFIELD);
178 }
179 #else
180 #  define SymIsBitField(Sym)    (((Sym)->Flags & SC_BITFIELD) == SC_BITFIELD)
181 #endif
182
183 #if defined(HAVE_INLINE)
184 INLINE int SymIsTypeDef (const SymEntry* Sym)
185 /* Return true if the given entry is a typedef entry */
186 {
187     return ((Sym->Flags & SC_TYPEDEF) == SC_TYPEDEF);
188 }
189 #else
190 #  define SymIsTypeDef(Sym)     (((Sym)->Flags & SC_TYPEDEF) == SC_TYPEDEF)
191 #endif
192
193 #if defined(HAVE_INLINE)
194 INLINE int SymIsDef (const SymEntry* Sym)
195 /* Return true if the given entry is defined */
196 {
197     return ((Sym->Flags & SC_DEF) == SC_DEF);
198 }
199 #else
200 #  define SymIsDef(Sym)     (((Sym)->Flags & SC_DEF) == SC_DEF)
201 #endif
202
203 #if defined(HAVE_INLINE)
204 INLINE int SymIsRef (const SymEntry* Sym)
205 /* Return true if the given entry is referenced */
206 {
207     return ((Sym->Flags & SC_REF) == SC_REF);
208 }
209 #else
210 #  define SymIsRef(Sym)     (((Sym)->Flags & SC_REF) == SC_REF)
211 #endif
212
213 #if defined(HAVE_INLINE)
214 INLINE int SymIsRegVar (const SymEntry* Sym)
215 /* Return true if the given entry is a register variable */
216 /* ### HACK! Fix the ugly type flags! */
217 {
218     return ((Sym->Flags & (SC_REGISTER|SC_TYPE)) == SC_REGISTER);
219 }
220 #else
221 #  define SymIsRegVar(Sym)      (((Sym)->Flags & (SC_REGISTER|SC_TYPE)) == SC_REGISTER)
222 #endif
223
224 #if defined(HAVE_INLINE)
225 INLINE const char* SymGetAsmName (const SymEntry* Sym)
226 /* Return the assembler label name for the symbol (beware: may be NULL!) */
227 {
228     return Sym->AsmName;
229 }
230 #else
231 #  define SymGetAsmName(Sym)      ((Sym)->AsmName)
232 #endif
233
234 const DeclAttr* SymGetAttr (const SymEntry* Sym, DeclAttrType AttrType);
235 /* Return an attribute for this symbol or NULL if the attribute does not exist */
236
237 #if defined(HAVE_INLINE)
238 INLINE int SymHasAttr (const SymEntry* Sym, DeclAttrType A)
239 /* Return true if the symbol has the given attribute */
240 {
241     return (SymGetAttr (Sym, A) != 0);    
242 }
243 #else
244 #  define SymHasAttr(Sym, A)       (SymGetAttr (Sym, A) != 0)
245 #endif
246
247 void SymUseAttr (SymEntry* Sym, struct Declaration* D);
248 /* Use the attributes from the declaration for this symbol */
249
250 void CvtRegVarToAuto (SymEntry* Sym);
251 /* Convert a register variable to an auto variable */
252
253 void ChangeSymType (SymEntry* Entry, Type* T);
254 /* Change the type of the given symbol */
255
256 void ChangeAsmName (SymEntry* Entry, const char* NewAsmName);
257 /* Change the assembler name of the symbol */
258
259 int HasAnonName (const SymEntry* Entry);
260 /* Return true if the symbol entry has an anonymous name */
261
262
263
264 /* End of symentry.h */
265 #endif
266
267
268