]> git.sur5r.net Git - cc65/blob - src/cc65/datatype.h
Use gcc function attributes, fix several format related problems
[cc65] / src / cc65 / datatype.h
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                datatype.h                                 */
4 /*                                                                           */
5 /*               Type string handling for the cc65 C compiler                */
6 /*                                                                           */
7 /*                                                                           */
8 /*                                                                           */
9 /* (C) 1998     Ullrich von Bassewitz                                        */
10 /*              Wacholderweg 14                                              */
11 /*              D-70597 Stuttgart                                            */
12 /* EMail:       uz@musoftware.de                                             */
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 DATATYPE_H
37 #define DATATYPE_H
38
39
40
41 #include <stdio.h>
42
43
44
45 /*****************************************************************************/
46 /*                                   Data                                    */
47 /*****************************************************************************/
48
49
50
51
52 // Basic data types
53 enum type_t {
54     T_NONE          = 0x0000,
55
56     // Basic types
57     T_TYPE_NONE     = 0x0000,
58     T_TYPE_CHAR     = 0x0001,
59     T_TYPE_INT      = 0x0002,
60     T_TYPE_ENUM     = 0x0003,
61     T_TYPE_FLOAT    = 0x0004,
62     T_TYPE_DOUBLE   = 0x0005,
63     T_TYPE_VOID     = 0x0006,
64     T_TYPE_STRUCT   = 0x0007,
65     T_TYPE_UNION    = 0x0008,
66     T_TYPE_ARRAY    = 0x0009,
67     T_TYPE_PTR      = 0x000A,
68     T_TYPE_FUNC     = 0x000B,
69     T_MASK_TYPE     = 0x001F,
70
71     // Type classes
72     T_CLASS_NONE    = 0x0000,
73     T_CLASS_INT     = 0x0020,
74     T_CLASS_FLOAT   = 0x0040,
75     T_CLASS_PTR     = 0x0060,
76     T_CLASS_STRUCT  = 0x0080,
77     T_CLASS_FUNC    = 0x00A0,
78     T_MASK_CLASS    = 0x00E0,
79
80     // Type signedness
81     T_SIGN_NONE     = 0x0000,
82     T_SIGN_UNSIGNED = 0x0100,
83     T_SIGN_SIGNED   = 0x0200,
84     T_MASK_SIGN     = 0x0300,
85
86     // Type size modifiers
87     T_SIZE_NONE     = 0x0000,
88     T_SIZE_SHORT    = 0x0400,
89     T_SIZE_LONG     = 0x0800,
90     T_SIZE_LONGLONG = 0x0C00,
91     T_MASK_SIZE     = 0x0C00,
92
93     // Type qualifiers
94     T_QUAL_NONE     = 0x0000,
95     T_QUAL_CONST    = 0x1000,
96     T_QUAL_VOLATILE = 0x2000,
97     T_MASK_QUAL     = 0x3000,
98
99     // Types
100     T_CHAR          = T_TYPE_CHAR     | T_CLASS_INT    | T_SIGN_UNSIGNED | T_SIZE_NONE,
101     T_SCHAR         = T_TYPE_CHAR     | T_CLASS_INT    | T_SIGN_SIGNED   | T_SIZE_NONE,
102     T_UCHAR         = T_TYPE_CHAR     | T_CLASS_INT    | T_SIGN_UNSIGNED | T_SIZE_NONE,
103     T_SHORT         = T_TYPE_INT      | T_CLASS_INT    | T_SIGN_SIGNED   | T_SIZE_SHORT,
104     T_USHORT        = T_TYPE_INT      | T_CLASS_INT    | T_SIGN_UNSIGNED | T_SIZE_SHORT,
105     T_INT           = T_TYPE_INT      | T_CLASS_INT    | T_SIGN_SIGNED   | T_SIZE_NONE,
106     T_UINT          = T_TYPE_INT      | T_CLASS_INT    | T_SIGN_UNSIGNED | T_SIZE_NONE,
107     T_LONG          = T_TYPE_INT      | T_CLASS_INT    | T_SIGN_SIGNED   | T_SIZE_LONG,
108     T_ULONG         = T_TYPE_INT      | T_CLASS_INT    | T_SIGN_UNSIGNED | T_SIZE_LONG,
109     T_LONGLONG      = T_TYPE_INT      | T_CLASS_INT    | T_SIGN_SIGNED   | T_SIZE_LONGLONG,
110     T_ULONGLONG     = T_TYPE_INT      | T_CLASS_INT    | T_SIGN_UNSIGNED | T_SIZE_LONGLONG,
111     T_ENUM          = T_TYPE_ENUM     | T_CLASS_INT    | T_SIGN_SIGNED   | T_SIZE_NONE,
112     T_FLOAT         = T_TYPE_FLOAT    | T_CLASS_FLOAT  | T_SIGN_NONE     | T_SIZE_NONE,
113     T_DOUBLE        = T_TYPE_DOUBLE   | T_CLASS_FLOAT  | T_SIGN_NONE     | T_SIZE_NONE,
114     T_VOID          = T_TYPE_VOID     | T_CLASS_NONE   | T_SIGN_NONE     | T_SIZE_NONE,
115     T_STRUCT        = T_TYPE_STRUCT   | T_CLASS_STRUCT | T_SIGN_NONE     | T_SIZE_NONE,
116     T_UNION         = T_TYPE_UNION    | T_CLASS_STRUCT | T_SIGN_NONE     | T_SIZE_NONE,
117     T_ARRAY         = T_TYPE_ARRAY    | T_CLASS_PTR    | T_SIGN_NONE     | T_SIZE_NONE,
118     T_PTR           = T_TYPE_PTR      | T_CLASS_PTR    | T_SIGN_NONE     | T_SIZE_NONE,
119     T_FUNC          = T_TYPE_FUNC     | T_CLASS_FUNC   | T_SIGN_NONE     | T_SIZE_NONE,
120
121 };
122
123
124
125 /* Data types */
126 #define T_END           0x0000
127 #define T_CHAR          0x0011
128 #define T_INT           0x0012
129 #define T_SHORT         0x0013
130 #define T_LONG          0x0014
131 #define T_ENUM          0x0015
132 #define T_UCHAR         0x0019
133 #define T_UINT          0x001A
134 #define T_USHORT        0x001B
135 #define T_ULONG         0x001C
136
137 #define T_FLOAT         0x0025
138 #define T_DOUBLE        0x0026
139
140 #define T_VOID          0x0001          /* void parameter list */
141 #define T_FUNC          0x0002          /* Function */
142
143 #define T_UNSIGNED      0x0008          /* Class */
144 #define T_INTEGER       0x0010          /* Class */
145 #define T_REAL          0x0020          /* Class */
146 #define T_POINTER       0x0040          /* Class */
147 #define T_PTR           0x0049
148 #define T_ARRAY         0x004A
149 #define T_STRUCT        0x0080
150 #define T_UNION         0x0081
151 #define T_SMASK         0x003F
152
153
154
155 /* Forward for a symbol entry */
156 struct SymEntry;
157
158 /* Type entry */
159 typedef unsigned short type;
160
161 /* Maximum length of a type string */
162 #define MAXTYPELEN      30
163
164 /* type elements needed for Encode/Decode */
165 #define DECODE_SIZE     5
166
167 /* Predefined type strings */
168 extern type type_int [];
169 extern type type_uint [];
170 extern type type_long [];
171 extern type type_ulong [];
172 extern type type_void [];
173 extern type type_pschar [];
174 extern type type_puchar [];
175
176
177
178 /*****************************************************************************/
179 /*                                   Code                                    */
180 /*****************************************************************************/
181
182
183
184 unsigned TypeLen (const type* Type);
185 /* Return the length of the type string */
186
187 int TypeCmp (const type* T1, const type* T2);
188 /* Compare two type strings */
189
190 type* TypeCpy (type* Dest, const type* Src);
191 /* Copy a type string */
192
193 type* TypeCat (type* Dest, const type* Src);
194 /* Append Src */
195
196 type* TypeDup (const type* Type);
197 /* Create a copy of the given type on the heap */
198
199 type* TypeAlloc (unsigned Len);
200 /* Allocate memory for a type string of length Len. Len *must* include the
201  * trailing T_END.
202  */
203
204 void TypeFree (type* Type);
205 /* Free a type string */
206
207 type GetDefaultChar (void);
208 /* Return the default char type (signed/unsigned) depending on the settings */
209
210 type* GetCharArrayType (unsigned Len);
211 /* Return the type for a char array of the given length */
212
213 type* GetImplicitFuncType (void);
214 /* Return a type string for an inplicitly declared function */
215
216 void PrintType (FILE* F, const type* Type);
217 /* Output translation of type array. */
218
219 void PrintRawType (FILE* F, const type* Type);
220 /* Print a type string in raw format (for debugging) */
221
222 void Encode (type* Type, unsigned long Val);
223 /* Encode an unsigned long into a type array */
224
225 void EncodePtr (type* Type, void* P);
226 /* Encode a pointer into a type array */
227
228 unsigned long Decode (const type* Type);
229 /* Decode an unsigned long from a type array */
230
231 void* DecodePtr (const type* Type);
232 /* Decode a pointer from a type array */
233
234 int HasEncode (const type* Type);
235 /* Return true if the given type has encoded data */
236
237 void CopyEncode (const type* Source, type* Target);
238 /* Copy encoded data from Source to Target */
239
240 unsigned SizeOf (const type* Type);
241 /* Compute size of object represented by type array. */
242
243 unsigned PSizeOf (const type* Type);
244 /* Compute size of pointer object. */
245
246 unsigned TypeOf (const type* Type);
247 /* Get the code generator base type of the object */
248
249 type* Indirect (type* Type);
250 /* Do one indirection for the given type, that is, return the type where the
251  * given type points to.
252  */
253
254 int IsVoid (const type* Type);
255 /* Return true if this is a void type */
256
257 int IsPtr (const type* Type);
258 /* Return true if this is a pointer type */
259
260 int IsChar (const type* Type);
261 /* Return true if this is a character type */
262
263 int IsInt (const type* Type);
264 /* Return true if this is an integer type */
265
266 int IsLong (const type* Type);
267 /* Return true if this is a long type (signed or unsigned) */
268
269 int IsUnsigned (const type* Type);
270 /* Return true if this is an unsigned type */
271
272 int IsStruct (const type* Type);
273 /* Return true if this is a struct type */
274
275 int IsFunc (const type* Type);
276 /* Return true if this is a function type */
277
278 int IsFastCallFunc (const type* Type);
279 /* Return true if this is a function type with __fastcall__ calling conventions */
280
281 int IsFuncPtr (const type* Type);
282 /* Return true if this is a function pointer */
283
284 int IsArray (const type* Type);
285 /* Return true if this is an array type */
286
287 struct FuncDesc* GetFuncDesc (const type* Type);
288 /* Get the FuncDesc pointer from a function or pointer-to-function type */
289
290
291
292 /* End of datatype.h */
293
294 #endif
295
296
297