Hint 0.1.0
Loading...
Searching...
No Matches
hint.h
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2024, 2026 Edward Kmett <ekmett@gmail.com>
2// SPDX-License-Identifier: BSD-2-Clause OR Apache-2.0
3#pragma once
4
13#ifdef __cplusplus
14
22
31#ifdef __has_attribute
32 #define hint_has_attribute(x) __has_attribute(x)
33#else
34 #define hint_has_attribute(__x) 0
35#endif
36
43#if hint_has_attribute(diagnose_if)
44 #define hint_diagnose_if(condition,message) \
45 _Pragma("clang diagnostic push") \
46 _Pragma("clang diagnostic ignored \"-Wgcc-compat\"") \
47 __attribute__((diagnose_if(condition,message,"error"))) \
48 _Pragma("clang diagnostic pop")
49#else
50 #define hint_diagnose_if(condition,message)
51#endif
52
66#if defined(_MSC_VER)
67 #define hint_has_declspec_attribute(__x) 1
68#elif defined(HINT_USE_DECLSPEC) && defined(__has_declspec_attribute)
69 #define hint_has_declspec_attribute(x) __has_declspec_attribute(x)
70#else
71 #define hint_has_declspec_attribute(__x) 0
72#endif
73
79#if hint_has_declspec_attribute(empty_bases)
80 #define hint_empty_bases __declspec(empty_bases)
81#else
82 #define hint_empty_bases
83#endif
84
92
109#if hint_has_attribute(const)
110 #define hint_const __attribute__((const))
111#else
112 #define hint_const
113#endif
114
127#if hint_has_attribute(pure)
128 #define hint_pure __attribute__((pure))
129#else
130 #define hint_pure
131#endif
132
134
141
162#if hint_has_attribute(lifetimebound)
163 #define hint_lifetimebound [[clang::lifetimebound]]
164#else
165 #define hint_lifetimebound
166#endif
167
176#if hint_has_attribute(noescape)
177 #define hint_noescape __attribute__((noescape))
178#else
179 #define hint_noescape
180#endif
181
183
184
193
198#if hint_has_attribute(always_inline)
199 #define hint_inline inline __attribute__((always_inline))
200#elif defined _WIN32
201 #define hint_inline __forceinline
202#else
203 #define hint_inline inline
204#endif
205
206
216#if hint_has_attribute(flatten)
217 #define hint_flatten __attribute__((flatten))
218#else
219 #define hint_flatten
220#endif
221
231#if hint_has_attribute(artificial)
232 #define hint_artificial __attribute__((artificial))
233#elif hint_has_attribute(__artificial__)
234 #define hint_artificial __attribute__((__artificial__))
235#else
236 #define hint_artificial
237 #warning "[[artificial]] is not supported"
238#endif
239
248#if hint_has_attribute(noinline)
249 #define hint_noinline __attribute__((noinline))
250#else
251 #define hint_noinline
252#endif
253
263#if hint_has_attribute(optnone)
264 #define hint_optnone __attribute__((optnone))
265#else
266 #define hint_optnone
267#endif
268
278
287#if hint_has_attribute(weak)
288 #define hint_weak __attribute__((weak))
289#else
290 #define hint_weak
291#endif
292
307#if hint_has_attribute(internal_linkage)
308 #define hint_internal_linkage __attribute__((internal_linkage))
309#else
310 #define hint_internal_linkage
311#endif
312
322#if __has_cpp_attribute(clang::preferred_name)
323 #define hint_preferred_name(x) [[clang::preferred_name(x)]]
324#else
325 #define hint_preferred_name(x)
326#endif
327
337
346#if hint_has_attribute(__visibility__)
347 #define hint_visibility(...) __attribute__((__visibility__(__VA_ARGS__)))
348#else
349 #define hint_visibility(...)
350 #warning "[[visibility]] is not supported"
351#endif
352
361#if hint_has_attribute(exclude_from_explicit_instantiation)
362 #define hint_exclude_from_explicit_instantiation __attribute__((exclude_from_explicit_instantiation))
363#else
364 #define hint_exclude_from_explicit_instantiation
365 #warning "[[exclude_from_explicit_instantiation]] is not supported"
366#endif
367
381#define hint_hidden hint_visibility("hidden") hint_exclude_from_explicit_instantiation
382
384
391
400#if __cpp_constinit
401 #define hint_constinit constinit
402#elif hint_has_attribute(require_constant_initialization)
403 #define hint_constinit __attribute__((require_constant_initialization))
404#else
405 #define hint_constinit
406#endif
407
416#if hint_has_attribute(uninitialized)
417 #define hint_uninitialized __attribute__((uninitialized))
418#else
419 #define hint_uninitialized
420#endif
421
431#if __has_cpp_attribute(clang::reinitializes)
432 #define hint_reinitializes [[clang::reinitializes]]
433#else
434 #define hint_reinitializes
435#endif
436
438
444
452#define hint_target(x) __attribute__((target(x)))
453
459#define hint_target_clones(...) __attribute__((target_clones(__VA_ARGS__)))
460
462
471
480#if hint_has_attribute(hot)
481 #define hint_hot __attribute__((hot))
482#else
483 #define hint_hot
484#endif
485
494#if hint_has_attribute(cold)
495 #define hint_cold __attribute__((cold))
496#else
497 #define hint_cold
498#endif
499
501
511
522#if hint_has_attribute(consumable)
523 #define hint_consumable(__x) __attribute__((consumable(__x)))
524#else
525 #define hint_consumable(__x)
526#endif
527
536#if hint_has_attribute(callable_when)
537 #define hint_callable_when(...) __attribute__((callable_when(__VA_ARGS__)))
538#else
539 #define hint_callable_when(...)
540#endif
541
551#if hint_has_attribute(param_typestate)
552 #define hint_param_typestate(...) __attribute__((param_typestate(__VA_ARGS__)))
553#else
554 #define hint_param_typestate(...)
555#endif
556
572#if hint_has_attribute(return_typestate)
573 #define hint_return_typestate(...) __attribute__((return_typestate(__VA_ARGS__)))
574#else
575 #define hint_return_typestate(...)
576#endif
577
587#define hint_moving hint_return_typestate("consumed") hint_param_typestate("unconsumed")
588
597#if hint_has_attribute(set_typestate)
598 #define hint_set_typestate(...) __attribute__((set_typestate(__VA_ARGS__)))
599#else
600 #define hint_set_typestate(...)
601#endif
602
611#if hint_has_attribute(test_typestate)
612 #define hint_test_typestate(...) __attribute__((test_typestate(__VA_ARGS__)))
613#else
614 #define hint_test_typestate(...)
615#endif // hint_has_attribute(test_typestate)
616
618
627
636#if hint_has_attribute(no_thread_safety_analysis)
637 #define hint_no_thread_safety_analysis __attribute__((no_thread_safety_analysis))
638#else
639 #define hint_no_thread_safety_analysis
640#endif
641
650#if hint_has_attribute(lockable)
651 #define hint_lockable __attribute__((lockable))
652#else
653 #define hint_lockable
654#endif
655
665#if hint_has_attribute(scoped_lockable)
666 #define hint_scoped_lockable __attribute__((scoped_lockable))
667#else
668 #define hint_scoped_lockable
669#endif
670
678#if hint_has_attribute(guarded_var)
679 #define hint_guarded_var __attribute__((guarded_var))
680#else
681 #define hint_guarded_var
682#endif
683
691#if hint_has_attribute(pt_guarded_var)
692 #define hint_pt_guarded_var __attribute__((pt_guarded_var))
693#else
694 #define hint_pt_guarded_var
695#endif
696
704#if hint_has_attribute(guarded_by)
705 #define hint_guarded_by(x) __attribute__((guarded_by(x)))
706#else
707 #define hint_guarded_by(x)
708#endif
709
717#if hint_has_attribute(pt_guarded_by)
718 #define hint_pt_guarded_by(x) __attribute__((pt_guarded_by(x)))
719#else
720 #define hint_pt_guarded_by(x)
721#endif
722
732#if hint_has_attribute(acquired_before)
733 #define hint_acquired_before(...) __attribute__((acquired_before(__VA_ARGS__)))
734#else
735 #define hint_acquired_before(...)
736#endif
737
747#if hint_has_attribute(acquired_after)
748 #define hint_acquired_after(...) __attribute__((acquired_after(__VA_ARGS__)))
749#else
750 #define hint_acquired_after(...)
751#endif
752
764#if hint_has_attribute(exclusive_lock_function)
765 #define hint_exclusive_lock_function(...) __attribute__((exclusive_lock_function(__VA_ARGS__)))
766#else
767 #define hint_exclusive_lock_function(...)
768#endif
769
784#if hint_has_attribute(shared_trylock_function)
785 #define hint_shared_trylock_function(...) __attribute__((shared_trylock_function(__VA_ARGS__)))
786#else
787 #define hint_shared_trylock_function(...)
788#endif
789
800#if hint_has_attribute(unlock_function)
801 #define hint_unlock_function(...) __attribute__((unlock_function(__VA_ARGS__)))
802#else
803 #define hint_unlock_function(...)
804#endif
805
815#if hint_has_attribute(lock_returned)
816 #define hint_lock_returned(__x) __attribute__((lock_returned(__x)))
817#else
818 #define hint_lock_returned(__x)
819#endif
820
829#if hint_has_attribute(locks_excluded)
830 #define hint_locks_excluded(...) __attribute__((locks_excluded(__VA_ARGS__)))
831#else
832 #define hint_locks_excluded(...)
833#endif
834
843#if hint_has_attribute(exclusive_locks_required)
844 #define hint_exclusive_locks_required(...) __attribute__((exclusive_locks_required(__VA_ARGS__)))
845#else
846 #define hint_exclusive_locks_required(...)
847#endif
848
857#if hint_has_attribute(shared_locks_required)
858 #define hint_shared_locks_required(...) __attribute__((shared_locks_required(__VA_ARGS__)))
859#else
860 #define hint_shared_locks_required(...)
861#endif
862
864
882
883
895#if __has_cpp_attribute(clang::acquire_handle)
896 #define hint_acquire_handle(__x) [[clang::acquire_handle(__x)]]
897#else
898 #define hint_acquire_handle(__x)
899#endif
900
905#if __has_cpp_attribute(clang::release_handle)
906 #define hint_release_handle(__x) [[clang::release_handle(__x)]]
907#else
908 #define hint_release_handle(__x)
909#endif
910
915#if __has_cpp_attribute(clang::use_handle)
916 #define hint_use_handle(__x) [[clang::use_handle(__x)]]
917#else
918 #define hint_use_handle(__x)
919#endif
920
922
931
934#if hint_has_attribute(aligned)
935 #define hint_align(x) __attribute__((aligned(x)))
936#else
937 #define hint_align(x)
938#endif
939
944#if hint_has_attribute(assume_aligned)
945 #define hint_assume_aligned(x) __attribute__((assume_aligned(x)))
946#else
947 #define hint_assume_aligned(x)
948#endif
949
958#if hint_has_attribute(align_value)
959 #define hint_align_value(x) __attribute__((align_value(x)))
960#else
961 #define hint_align_value(x)
962#endif
963
965
974
983#if hint_has_attribute(malloc)
984 #define hint_malloc __attribute__((malloc))
985#else
986 #define hint_malloc
987#endif
988
1001#if hint_has_attribute(alloc_align)
1002 #define hint_alloc_align(N) __attribute__((alloc_align(N)))
1003#else
1004 #define hint_alloc_align(__N)
1005#endif
1006
1015#if hint_has_attribute(alloc_size)
1016 #define hint_alloc_size(N) __attribute__((alloc_size(N)))
1017#else
1018 #define hint_alloc_size(__N)
1019#endif
1020
1022
1029
1042#if hint_has_attribute(null_terminated_string_arg)
1043 #define hint_null_terminated_string_arg(x) __attribute__((null_terminated_string_arg(x)))
1044#else
1045 #define hint_null_terminated_string_arg(x)
1046#endif
1047
1049
1060
1071#if hint_has_attribute(callback)
1072 #define hint_callback(...) __attribute__((callback(__VA_ARGS__)))
1073#else
1074 #define hint_callback(...)
1075#endif
1076
1087#if hint_has_attribute(noreturn)
1088#define hint_noreturn __attribute__((noreturn))
1089#else
1090#define hint_noreturn
1091#endif
1092
1095
1102
1111#if hint_has_attribute(returns_nonnull)
1112 #define hint_returns_nonnull __attribute__((returns_nonnull))
1113#else
1114 #define hint_returns_nonnull
1115#endif
1116
1127#if hint_has_attribute(nonnull)
1128 #define hint_nonnull(...) __attribute__((nonnull(__VA_ARGS__)))
1129#else
1130 #define hint_nonnull(...)
1131#endif
1132
1159#ifdef __clang__
1160#define hint_Nonnull _Nonnull
1161#define hint_Nullable _Nullable
1162#define hint_Null_unspecified _Null_unspecified
1163#else
1164#define hint_Nonnull
1165#define hint_Nullable
1166#define hint_Null_unspecified
1167#endif
1168
1170
1177
1212#ifdef __CUDACC__
1213#define hint_host __host__
1214#define hint_device __device__
1215#define hint_global __global__
1216#else
1217#define hint_host
1218#define hint_device
1219#define hint_global
1220#endif
1221
1222#define hint_hd hint_host hint_device
1223
1225
1250
1288#if hint_has_attribute(nonblocking)
1289 #define hint_nonblocking [[clang::nonblocking]]
1290 #define hint_blocking [[clang::blocking]]
1291 #define hint_nonallocating [[clang::nonallocating]]
1292 #define hint_allocating [[clang::allocating]]
1293 #define hint_noalloc(x) [[clang::nonallocating(x)]]
1294 #define hint_noblock(x) [[clang::nonblocking(x)]]
1295#else
1296 #define hint_nonblocking
1297 #define hint_blocking
1298 #define hint_nonallocating
1299 #define hint_allocating
1300 #define hint_noalloc(__x)
1301 #define hint_noblock(__x)
1302#endif
1303
1305
1308#define hint_constexpr constexpr
1309
1310#else
1311// The shared shader declarations carry no host compiler promises.
1312#define hint_constexpr
1313#define hint_inline inline
1314#define hint_pure
1315#define hint_const
1316#define hint_lifetimebound
1317#define hint_noescape
1318#define hint_reinitializes
1319#define hint_artificial
1320#define hint_flatten
1321#endif