solidc
Robust collection of general-purpose cross-platform C libraries and data structures designed for rapid and safe development in C
Loading...
Searching...
No Matches
types.h
1#ifndef SOLIDC_TYPES_H
2#define SOLIDC_TYPES_H
3
4#include <stdbool.h> // bool (C99+)
5#include <stddef.h> // size_t, ptrdiff_t
6#include <stdint.h> // intN_t, uintN_t
7
8// Types for C STDC_VERSION from C89 to C23.
9#define C_VERSION_89 (198900L) // C89/C90 (same value for both)
10#define C_VERSION_99 (199901L) // C99
11#define C_VERSION_11 (201112L) // C11
12#define C_VERSION_17 (201710L) // C17 (technically a bugfix release, but often treated as a separate version)
13#define C_VERSION_23 (202311L) // C23
14
15// Check for C11 compiler support.
16#if defined(__STDC_VERSION__) && __STDC_VERSION__ < 201112L
17#error "solidc requires a C11-compliant compiler"
18#endif
19
20// ── Detect compiler / platform ───────────────────────────────────────────────
21// Used to gate features that are not available on all compilers (e.g. atomics on MSVC).
22#if defined(_MSC_VER)
23#define SOLIDC_MSVC
24#elif defined(__GNUC__) || defined(__clang__)
25#define SOLIDC_GCC_LIKE
26#endif
27
28// ── Signed integers ──────────────────────────────────────────────────────────
29#ifndef i8
31typedef int8_t i8;
32#endif
33#ifndef i16
35typedef int16_t i16;
36#endif
37#ifndef i32
39typedef int32_t i32;
40#endif
41#ifndef i64
43typedef int64_t i64;
44#endif
45
46// ── Unsigned integers ────────────────────────────────────────────────────────
47#ifndef u8
49typedef uint8_t u8;
50#endif
51#ifndef u16
53typedef uint16_t u16;
54#endif
55#ifndef u32
57typedef uint32_t u32;
58#endif
59#ifndef u64
61typedef uint64_t u64;
62#endif
63
64// ── Floating point ───────────────────────────────────────────────────────────
65// Mirrors the integer naming convention; makes numeric code more consistent.
66#ifndef f32
68typedef float f32;
69#endif
70#ifndef f64
72typedef double f64;
73#endif
74
75// ── Platform-sized types ─────────────────────────────────────────────────────
76// These match the natural word size of the target — use them for array lengths,
77// memory sizes, and pointer arithmetic instead of assuming int/long widths.
78#ifndef usize
80typedef size_t usize;
81#endif
82#ifndef isize
84typedef ptrdiff_t isize;
85#endif
86#ifndef iptr
88typedef intptr_t iptr;
89#endif
90#ifndef uptr
92typedef uintptr_t uptr;
93#endif
94
95// ── Byte alias ───────────────────────────────────────────────────────────────
96#ifndef byte
98typedef uint8_t byte;
99#endif
100
101// ── Atomic variants ──────────────────────────────────────────────────────────
102// Gated behind C11 and a non-MSVC check:
103// out entirely (useful for embedded or C99 targets).
104// Check SOLIDC_HAS_ATOMICS in downstream code instead of repeating this guard.
105#if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L && !defined(SOLIDC_NO_ATOMICS)
106#include <stdatomic.h>
107#ifndef atomic_i32
109typedef _Atomic(i32) atomic_i32;
110#endif
111#ifndef atomic_u32
113typedef _Atomic(u32) atomic_u32;
114#endif
115#ifndef atomic_i64
117typedef _Atomic(i64) atomic_i64;
118#endif
119#ifndef atomic_u64
121typedef _Atomic(u64) atomic_u64;
122#endif
123
124#define SOLIDC_HAS_ATOMICS 1
125#else
126#define SOLIDC_HAS_ATOMICS 0
127#endif
128
129// ── Compile-time width checks ────────────────────────────────────────────────
130// Zero runtime cost. Catches platforms where float/double/pointer sizes deviate
131// from the expected layout (e.g. some DSPs or exotic embedded targets).
132// Guarded so the file stays valid on C99, where _Static_assert does not exist.
133#if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L
134_Static_assert(sizeof(i8) == 1, "i8 must be 1 byte");
135_Static_assert(sizeof(i16) == 2, "i16 must be 2 bytes");
136_Static_assert(sizeof(i32) == 4, "i32 must be 4 bytes");
137_Static_assert(sizeof(i64) == 8, "i64 must be 8 bytes");
138_Static_assert(sizeof(f32) == 4, "f32 must be 4 bytes");
139_Static_assert(sizeof(f64) == 8, "f64 must be 8 bytes");
140_Static_assert(sizeof(usize) == sizeof(void*), "usize must match pointer width");
141_Static_assert(sizeof(isize) == sizeof(void*), "isize must match pointer width");
142#endif
143
144#endif // SOLIDC_TYPES_H