| 1 | /* @title: Driver Verifier */ |
| 2 | #pragma once |
| 3 | #include <compiler/core.h> |
| 4 | #include <compiler/intrinsic.h> |
| 5 | /* Comptime verification that structures matches spec |
| 6 | * |
| 7 | * Each macro here is a big wrapper around a `static_assert`, |
| 8 | * so nothing here emits any code. TODO: introduce eventual |
| 9 | * spec name and section definition support. |
| 10 | * |
| 11 | * These are used in layout.c of various subsystems, so as to |
| 12 | * not pollute a header and avoid emitting one warning many times. |
| 13 | * |
| 14 | * NOTE: these macros CAN'T do a few things: |
| 15 | * - bitfields |
| 16 | * - reference spec |
| 17 | * - give full names to members */ |
| 18 | #define dv_size_full(type, bytes, ...) \ |
| 19 | static_assert(sizeof(type) == (bytes), \ |
| 20 | PP_STRINGIZE(type) " is not " #bytes " bytes") |
| 21 | |
| 22 | #define dv_align_full(type, bytes, ...) \ |
| 23 | static_assert(cc_alignof(type) == (bytes), \ |
| 24 | PP_STRINGIZE(type) " is not " #bytes "-byte aligned") |
| 25 | |
| 26 | #define dv_field_full(type, member_type, member, offset, ...) \ |
| 27 | static_assert( \ |
| 28 | ci_offsetof(type, member) == (offset) && \ |
| 29 | ci_types_compatible_p(typeof(((type *) 0)->member), member_type), \ |
| 30 | PP_STRINGIZE(type) "." #member " is not " #member_type " at " #offset) |
| 31 | |
| 32 | #define dv_field_at_full(type, member, offset, ...) \ |
| 33 | static_assert(ci_offsetof(type, member) == (offset), \ |
| 34 | PP_STRINGIZE(type) "." #member " is not at " #offset) |
| 35 | |
| 36 | #define dv_size(...) dv_size_full(struct DV_STRUCT, __VA_ARGS__) |
| 37 | #define dv_align(...) dv_align_full(struct DV_STRUCT, __VA_ARGS__) |
| 38 | #define dv_field(...) dv_field_full(struct DV_STRUCT, __VA_ARGS__) |
| 39 | #define dv_field_at(...) dv_field_at_full(struct DV_STRUCT, __VA_ARGS__) |
| 40 | |