P99
p99_type.h
Go to the documentation of this file.
1 /* This may look like nonsense, but it really is -*- mode: C; coding: utf-8 -*- */
2 /* */
3 /* Except for parts copied from previous work and as explicitly stated below, */
4 /* the authors and copyright holders for this work are as follows: */
5 /* (C) copyright 2010-2013, 2018 Jens Gustedt, INRIA, France */
6 /* (C) copyright 2012 William Morris */
7 /* */
8 /* This file is free software; it is part of the P99 project. */
9 /* */
10 /* Licensed under the Apache License, Version 2.0 (the "License"); */
11 /* you may not use this file except in compliance with the License. */
12 /* You may obtain a copy of the License at */
13 /* */
14 /* http://www.apache.org/licenses/LICENSE-2.0 */
15 /* */
16 /* Unless required by applicable law or agreed to in writing, software */
17 /* distributed under the License is distributed on an "AS IS" BASIS, */
18 /* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. */
19 /* See the License for the specific language governing permissions and */
20 /* limitations under the License. */
21 /* */
26 #ifndef P99_TYPE_H_
27 # define P99_TYPE_H_
28 
29 #include "p99_paste.h"
30 
31 
32 /* This is defined in some header file since C11. Since C11 allows to
33  repeat typedefs as long as they resolve to the same type, this can
34  be done at any place. */
38 typedef int errno_t;
39 
58 P00_DOCUMENT_IDENTIFIER_ARGUMENT(P99_DECLARE_STRUCT, 0)
59 #define P99_DECLARE_STRUCT(NAME) \
60 typedef struct NAME NAME
61 
62 
73 P00_DOCUMENT_IDENTIFIER_ARGUMENT(P99_DECLARE_UNION, 0)
74 #define P99_DECLARE_UNION(NAME) \
75 typedef union NAME NAME
76 
101 #if __STDC_VERSION__ < 201101L
102 P00_DOCUMENT_IDENTIFIER_ARGUMENT(P99_DEFINE_UNION, 0)
103 P00_DOCUMENT_DECLARATION_ARGUMENT(P99_DEFINE_UNION, 1)
104 P00_DOCUMENT_DECLARATION_ARGUMENT(P99_DEFINE_UNION, 2)
105 P00_DOCUMENT_DECLARATION_ARGUMENT(P99_DEFINE_UNION, 3)
106 #define P99_DEFINE_UNION(NAME, ...) \
107  \
108 union NAME { \
109  uint8_t p00_allbytes[sizeof(union { __VA_ARGS__ })]; \
110  __VA_ARGS__ \
111 }
112 #else
113 P00_DOCUMENT_IDENTIFIER_ARGUMENT(P99_DEFINE_UNION, 0)
114 P00_DOCUMENT_DECLARATION_ARGUMENT(P99_DEFINE_UNION, 1)
115 P00_DOCUMENT_DECLARATION_ARGUMENT(P99_DEFINE_UNION, 2)
116 P00_DOCUMENT_DECLARATION_ARGUMENT(P99_DEFINE_UNION, 3)
117 #define P99_DEFINE_UNION(NAME, ...) \
118  \
119 union NAME { __VA_ARGS__ }
120 #endif
121 
122 /* For each one word integer type have a signed and unsigned variant. */
123 #define P00_ONE_TOK(T, NAME) \
124  \
125  \
126 typedef T NAME
127 
131 P00_DOCUMENT_TYPE_IDENTIFIER_ARGUMENT(P99_POINTER_TYPE, 0)
132 #define P99_POINTER_TYPE(T) \
133  \
134 typedef T *P99_PASTE2(T, _ptr); \
135  \
136 typedef T const*P99_PASTE2(T, _cptr)
137 
141 P00_DOCUMENT_TYPE_IDENTIFIER_ARGUMENT(P99_PLAIN_TYPE, 0)
142 #define P99_PLAIN_TYPE(T) \
143  \
144 p99_inline T* P99_PASTE2(T, _init)(T *p00_id) { \
145  if (p00_id) *p00_id = 0; \
146  return p00_id; \
147 } \
148  \
149 p99_inline void P99_PASTE2(T, _destroy)(T* p00_id) { \
150  /* empty */ \
151  (void)p00_id; \
152 } \
153 P99_MACRO_END(P99_PLAIN_TYPE, T)
154 
159 P00_DOCUMENT_TYPE_IDENTIFIER_ARGUMENT(P99_DERIVED_TYPES, 0)
160 #define P99_DERIVED_TYPES(T) \
161 P99_PLAIN_TYPE(T); \
162 P99_PLAIN_TYPE(P99_PASTE2(T, _cptr)); \
163 P99_PLAIN_TYPE(P99_PASTE2(T, _ptr))
164 
165 
176 P00_DOCUMENT_TYPE_ARGUMENT(P99_ENC_DECLARE, 0)
177 P00_DOCUMENT_IDENTIFIER_ARGUMENT(P99_ENC_DECLARE, 1)
178 #define P99_ENC_DECLARE(T, NAME) \
179  \
180 struct NAME { \
181  \
182  T p00_val; \
183 }; \
184 P99_DECLARE_STRUCT(NAME)
185 
190 #define P99_ENC(OBJ) ((OBJ).p00_val)
191 
196 #define P99_ENCP(OBJP) ((OBJP)->p00_val)
197 
206 #define P99_ENC_INIT(V) { .p00_val = (V), }
207 
208 #if CHAR_MAX == UCHAR_MAX
209 # define P99_STD_UNSIGNED_TYPES \
210  _Bool, \
211  char, \
212  uchar, \
213  ushort, \
214  unsigned, \
215  ulong, \
216  ullong
217 # define P99_STD_UNSIGNED_EXTS b, c, uhh, uh, u, ul, ull
218 # define P99_STD_SIGNED_TYPES \
219  schar, \
220  short, \
221  int, \
222  long, \
223  llong
224 # define P99_STD_SIGNED_EXTS hh, h, i, l, ll
225 # define P99_STD_REAL_FLOATING_TYPES \
226  float, \
227  double, \
228  ldouble
229 #else
230 # define P99_STD_UNSIGNED_TYPES \
231  _Bool, \
232  uchar, \
233  ushort, \
234  unsigned, \
235  ulong, \
236  ullong
237 # define P99_STD_UNSIGNED_EXTS b, uhh, uh, u, ul, ull
238 # define P99_STD_SIGNED_TYPES \
239  char, \
240  schar, \
241  short, \
242  int, \
243  long, \
244  llong
245 # define P99_STD_SIGNED_EXTS c, hh, h, i, l, ll
246 # define P99_STD_REAL_FLOATING_TYPES \
247  float, \
248  double, \
249  ldouble
250 #endif
251 
252 #define P99_STD_REAL_FLOATING_EXTS f, d, ld
253 
254 #ifndef __STDC_NO_COMPLEX__
255 # define P99_STD_COMPLEX_TYPES \
256  cfloat, \
257  cdouble, \
258  cldouble
259 # define P99_STD_COMPLEX_EXTS fc, dc, ldc
260 # define P99_STD_FLOATING_TYPES P99_STD_REAL_FLOATING_TYPES, P99_STD_COMPLEX_TYPES
261 # define P99_STD_FLOATING_EXTS P99_STD_REAL_FLOATING_EXTS, P99_STD_COMPLEX_EXTS
262 #else
263 # define P99_STD_FLOATING_TYPES P99_STD_REAL_FLOATING_TYPES
264 # define P99_STD_FLOATING_EXTS P99_STD_REAL_FLOATING_EXTS
265 #endif
266 
267 #define P99_STD_BASIC_TYPES \
268  P99_STD_INTEGER_TYPES, \
269  P99_STD_FLOATING_TYPES
270 
271 #define P99_STD_BASIC_EXTS \
272  P99_STD_INTEGER_EXTS, \
273  P99_STD_FLOATING_EXTS
274 
275 #define P99_STD_CHARACTER_TYPES \
276  char, \
277  schar, \
278  uchar
279 
280 #define P99_STD_CHARACTER_EXTS c, hh, uhh
281 
282 #define P99_STD_INTEGER_TYPES \
283  P99_STD_SIGNED_TYPES, \
284  P99_STD_UNSIGNED_TYPES
285 
286 #define P99_STD_INTEGER_EXTS \
287  P99_STD_SIGNED_EXTS, \
288  P99_STD_UNSIGNED_EXTS
289 
290 #define P99_STD_REAL_TYPES \
291  P99_STD_INTEGER_TYPES, \
292  P99_STD_REAL_FLOATING_TYPES
293 
294 #define P99_STD_REAL_EXTS \
295  P99_STD_INTEGER_EXTS, \
296  P99_STD_REAL_FLOATING_EXTS
297 
298 #define P99_STD_ARITHMETIC_TYPES \
299  P99_STD_INTEGER_TYPES, \
300  P99_STD_FLOATING_TYPES
301 
302 #define P99_STD_ARITHMETIC_EXTS \
303  P99_STD_INTEGER_EXTS, \
304  P99_STD_FLOATING_EXTS
305 
306 #ifdef p99x_uint128
307 # define P99_EXT_INTEGER_TYPES p99x_uintmax, p99x_intmax
308 # define P99_EXT_ARITHMETIC_TYPES P99_STD_ARITHMETIC_TYPES, P99_EXT_INTEGER_TYPES
309 # define P99_EXT_REAL_TYPES P99_STD_REAL_TYPES, P99_EXT_INTEGER_TYPES
310 #else
311 # define P99_EXT_INTEGER_TYPES
312 # define P99_EXT_ARITHMETIC_TYPES P99_STD_ARITHMETIC_TYPES
313 # define P99_EXT_REAL_TYPES P99_STD_REAL_TYPES
314 #endif
315 
319 #endif /* !P99_TYPE_H_ */
P99_DEFINE_UNION
#define P99_DEFINE_UNION(NAME,...)
definition of a union NAME
Definition: p99_type.h:106
p99_paste.h
macros that paste tokens in a predictable way
P99_DECLARE_STRUCT
#define P99_DECLARE_STRUCT(NAME)
forward declaration of a struct NAME
Definition: p99_type.h:59
P99_POINTER_TYPE
#define P99_POINTER_TYPE(T)
Declare derived pointer types with endings "_ptr" and "_cptr".
Definition: p99_type.h:132
P99_ENC_DECLARE
#define P99_ENC_DECLARE(T, NAME)
Encapsulate an object of type T in a new type called NAME.
Definition: p99_type.h:178
P99_PLAIN_TYPE
#define P99_PLAIN_TYPE(T)
Declare trivial init and destroy functions for type T.
Definition: p99_type.h:142
P99_DERIVED_TYPES
#define P99_DERIVED_TYPES(T)
Declare trivial init and destroy functions for type T and its pointer derivatives.
Definition: p99_type.h:160
P99_DECLARE_UNION
#define P99_DECLARE_UNION(NAME)
forward declaration of a union NAME
Definition: p99_type.h:74
errno_t
int errno_t
Definition: p99_type.h:38