native 0.0.1
Vectors, masks and wide register packs for C++26
Loading...
Searching...
No Matches
attributes.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
12
13#ifdef __cplusplus
14
22
30
31#ifdef __has_attribute
32 #define native_has_attribute(x) __has_attribute(x)
33#else
34 #define native_has_attribute(__x) 0
35#endif
36
43#if native_has_attribute(diagnose_if)
44 #define native_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 native_diagnose_if(condition,message)
51#endif
52
65
66#if defined(_MSC_VER)
67 #define native_has_declspec_attribute(__x) 1
68#elif defined(NATIVE_USE_DECLSPEC) && defined(__has_declspec_attribute)
69 #define native_has_declspec_attribute(x) __has_declspec_attribute(x)
70#else
71 #define native_has_declspec_attribute(__x) 0
72#endif
73
74// MS ABI otherwise allocates space for a second empty accessor base.
75#if native_has_declspec_attribute(empty_bases)
76 #define native_empty_bases __declspec(empty_bases)
77#else
78 #define native_empty_bases
79#endif
80
88
104
105#if native_has_attribute(const)
106 #define native_const __attribute__((const))
107#else
108 #define native_const
109#endif
110
122
123#if native_has_attribute(pure)
124 #define native_pure __attribute__((pure))
125#else
126 #define native_pure
127#endif
128
130
137
157
158#if native_has_attribute(lifetimebound)
159 #define native_lifetimebound [[clang::lifetimebound]]
160#else
161 #define native_lifetimebound
162#endif
163
171
172#if native_has_attribute(noescape)
173 #define native_noescape __attribute__((noescape))
174#else
175 #define native_noescape
176#endif
177
188
189#define native_nodiscard [[nodiscard]]
190
192
193
202
206
207#if native_has_attribute(always_inline)
208 #define native_inline inline __attribute__((always_inline))
209#elif defined _WIN32
210 #define native_inline __forceinline
211#else
212 #define native_inline inline
213#endif
214
215
224
225#if native_has_attribute(flatten)
226 #define native_flatten __attribute__((flatten))
227#else
228 #define native_flatten
229#endif
230
239
240#if native_has_attribute(artificial)
241 #define native_artificial __attribute__((artificial))
242#elif native_has_attribute(__artificial__)
243 #define native_artificial __attribute__((__artificial__))
244#else
245 #define native_artificial
246 #warning "[[artificial]] is not supported"
247#endif
248
256
257#if native_has_attribute(noinline)
258 #define native_noinline __attribute__((noinline))
259#else
260 #define native_noinline
261#endif
262
271
272#if native_has_attribute(optnone)
273 #define native_optnone __attribute__((optnone))
274#else
275 #define native_optnone
276#endif
277
287
295
296#if native_has_attribute(weak)
297 #define native_weak __attribute__((weak))
298#else
299 #define native_weak
300#endif
301
315
316#if native_has_attribute(internal_linkage)
317 #define native_internal_linkage __attribute__((internal_linkage))
318#else
319 #define native_internal_linkage
320#endif
321
330
331#if __has_cpp_attribute(clang::preferred_name)
332 #define native_preferred_name(x) [[clang::preferred_name(__x)]]
333#else
334 #define native_preferred_name(__x)
335#endif
336
346
354
355#if native_has_attribute(__visibility__)
356 #define native_visibility(...) __attribute__((__visibility__(__VA_ARGS__)))
357#else
358 #define native_visibility(...)
359 #warning "[[visibility]] is not supported"
360#endif
361
369
370#if native_has_attribute(exclude_from_explicit_instantiation)
371 #define native_exclude_from_explicit_instantiation __attribute__((exclude_from_explicit_instantiation))
372#else
373 #define native_exclude_from_explicit_instantiation
374 #warning "[[exclude_from_explicit_instantiation]] is not supported"
375#endif
376
389
390#define native_hidden native_visibility("hidden") native_exclude_from_explicit_instantiation
391
393
400
408
409#if __cpp_constinit
410 #define native_constinit constinit
411#elif native_has_attribute(require_constant_initialization)
412 #define native_constinit __attribute__((require_constant_initialization))
413#else
414 #define native_constinit
415#endif
416
424
425#if native_has_attribute(uninitialized)
426 #define native_uninitialized __attribute__((uninitialized))
427#else
428 #define native_uninitialized
429#endif
430
439
440#if __has_cpp_attribute(clang::reinitializes)
441 #define native_reinitializes [[clang::reinitializes]]
442#else
443 #define native_reinitializes
444#endif
445
447
466
475
476#define native_target(x) __attribute__((target(x)))
477
485
486#define native_target_clones(...) __attribute__((target_clones(_VA_ARGS_)))
487
489
498
506
507#if native_has_attribute(hot)
508 #define native_hot __attribute__((hot))
509#else
510 #define native_hot
511#endif
512
520
521#if native_has_attribute(cold)
522 #define native_cold __attribute__((cold))
523#else
524 #define native_cold
525#endif
526
528
538
548
549#if native_has_attribute(consumable)
550 #define native_consumable(__x) __attribute__((consumable(__x)))
551#else
552 #define native_consumable(__x)
553#endif
554
562
563#if native_has_attribute(callable_when)
564 #define native_callable_when(...) __attribute__((callable_when(__VA_ARGS__)))
565#else
566 #define native_callable_when(...)
567#endif
568
577
578#if native_has_attribute(param_typestate)
579 #define native_param_typestate(...) __attribute__((param_typestate(__VA_ARGS__)))
580#else
581 #define native_param_typestate(...)
582#endif
583
598
599#if native_has_attribute(return_typestate)
600 #define native_return_typestate(...) __attribute__((return_typestate(__VA_ARGS__)))
601#else
602 #define native_return_typestate(...)
603#endif
604
613
614#define native_moving native_return_typestate("consumed") native_param_typestate("unconsumed")
615
623
624#if native_has_attribute(set_typestate)
625 #define native_set_typestate(...) __attribute__((set_typestate(__VA_ARGS__)))
626#else
627 #define native_set_typestate(...)
628#endif
629
637
638#if native_has_attribute(test_typestate)
639 #define native_test_typestate(...) __attribute__((test_typestate(__VA_ARGS__)))
640#else
641 #define native_test_typestate
642 #define native_test_typestate(...)
643#endif // native_has_attribute(test_typestate)
644
646
655
663
664#if native_has_attribute(no_thread_safety_analysis)
665 #define native_no_thread_safety_analysis __attribute__((no_thread_safety_analysis))
666#else
667 #define native_no_thread_safety_analysis
668#endif
669
677
678#if native_has_attribute(lockable)
679 #define native_lockable __attribute__((lockable))
680#else
681 #define native_lockable
682#endif
683
692
693#if native_has_attribute(scoped_lockable)
694 #define native_scoped_lockable __attribute__((scoped_lockable))
695#else
696 #define native_scoped_lockable
697#endif
698
705
706#if native_has_attribute(guarded_var)
707 #define native_guarded_var __attribute__((guarded_var))
708#else
709 #define native_guarded_var
710#endif
711
718
719#if native_has_attribute(pt_guarded_var)
720 #define native_pt_guarded_var __attribute__((pt_guarded_var))
721#else
722 #define native_pt_guarded_var
723#endif
724
731
732#if native_has_attribute(guarded_by)
733 #define native_guarded_by(x) __attribute__((guarded_by(x)))
734#else
735 #define native_guarded_by(x)
736#endif
737
744
745#if native_has_attribute(pt_guarded_by)
746 #define native_pt_guarded_by(x) __attribute__((pt_guarded_by(x)))
747#else
748 #define native_pt_guarded_by(x)
749#endif
750
759
760#if native_has_attribute(acquired_before)
761 #define native_acquired_before(...) __attribute__((acquired_before(__VA_ARGS__)))
762#else
763 #define native_acquired_before(...)
764#endif
765
774
775#if native_has_attribute(acquired_after)
776 #define native_acquired_after(...) __attribute__((acquired_after(__VA_ARGS__)))
777#else
778 #define native_acquired_after(...)
779#endif
780
791
792#if native_has_attribute(exclusive_lock_function)
793 #define native_exclusive_lock_function(...) __attribute__((exclusive_lock_function(__VA_ARGS__)))
794#else
795 #define native_exclusive_lock_function(...)
796#endif
797
811
812#if native_has_attribute(shared_trylock_function)
813 #define native_shared_trylock_function(...) __attribute__((shared_trylock_function(__VA_ARGS__)))
814#else
815 #define native_shared_trylock_function(...)
816#endif
817
827
828#if native_has_attribute(unlock_function)
829 #define native_unlock_function(...) __attribute__((unlock_function(__VA_ARGS__)))
830#else
831 #define native_unlock_function(...)
832#endif
833
842
843#if native_has_attribute(lock_returned)
844 #define native_lock_returned(__x) __attribute__((lock_returned(__x)))
845#else
846 #define native_lock_returned(__x)
847#endif
848
856
857#if native_has_attribute(locks_excluded)
858 #define native_locks_excluded(...) __attribute__((locks_excluded(__VA_ARGS__)))
859#else
860 #define native_locks_excluded(...)
861#endif
862
870
871#if native_has_attribute(exclusive_locks_required)
872 #define native_exclusive_locks_required(...) __attribute__((exclusive_locks_required(__VA_ARGS__)))
873#else
874 #define native_exclusive_locks_required(...)
875#endif
876
884
885#if native_has_attribute(shared_locks_required)
886 #define native_shared_locks_required(...) __attribute__((shared_locks_required(__VA_ARGS__)))
887#else
888 #define native_shared_locks_required(...)
889#endif
890
892
910
911
922
923#if __has_cpp_attribute(acquire_handle)
924 #define native_acquire_handle(__x) [[clang::acquire_handle(__x)]]
925#else
926 #define native_acquire_handle(__x)
927#endif
928
932
933#if __has_cpp_attribute(release_handle)
934 #define native_release_handle(__x) [[clang::release_handle(__x)]]
935#else
936 #define native_release_handle(__x)
937#endif
938
942
943#if __has_cpp_attribute(use_handle)
944 #define native_use_handle(__x) [[clang::use_handle(__x)]]
945#else
946 #define native_use_handle(__x)
947#endif
948
950
959
962#if native_has_attribute(aligned)
963 #define native_align(x) __attribute__((aligned(x)))
964#else
965 #define native_align(x)
966#endif
967
971
972#if native_has_attribute(assume_aligned)
973 #define native_assume_aligned(x) __attribute__((assume_aligned(x)))
974#else
975 #define native_assume_aligned(x)
976#endif
977
985
986#if native_has_attribute(align_value)
987 #define native_align_value(x) __attribute__((align_value(x)))
988#else
989 #define native_align_value(x)
990#endif
991
993
1002
1010
1011#if native_has_attribute(malloc)
1012 #define native_malloc __attribute__((malloc))
1013#else
1014 #define native_malloc
1015#endif
1016
1028
1029#if native_has_attribute(alloc_align)
1030 #define native_alloc_align(N) __attribute__((alloc_align(N)))
1031#else
1032 #define native_alloc_align(__N)
1033#endif
1034
1042
1043#if native_has_attribute(alloc_size)
1044 #define native_alloc_size(N) __attribute__((alloc_size(N)))
1045#else
1046 #define native_alloc_size(__N)
1047#endif
1048
1050
1057
1069
1070#if native_has_attribute(null_terminated_string_arg)
1071 #define native_null_terminated_string_arg(x) __attribute__((null_terminated_string_arg(x)))
1072#else
1073 #define native_null_terminated_string_arg(x)
1074#endif
1075
1077
1088
1098
1099#if native_has_attribute(callback)
1100 #define native_callback(...) __attribute__((callback(__VA_ARGS__)))
1101#else
1102 #define native_callback(...)
1103#endif
1104
1114
1115#if native_has_attribute(noreturn)
1116#define native_noreturn __attribute__((noreturn))
1117#else
1118#define native_noreturn
1119#endif
1120
1123
1130
1138
1139#if native_has_attribute(returns_nonnull)
1140 #define native_returns_nonnull __attribute__((returns_nonnull))
1141#else
1142 #define native_returns_nonnull
1143#endif
1144
1154
1155#if native_has_attribute(nonnull)
1156 #define native_nonnull(...) __attribute__((nonnull(__VA_ARGS__)))
1157#else
1158 #define native_nonnull(...)
1159#endif
1160
1186
1187#ifdef __clang__
1188#define native_Nonnull _Nonnull
1189#define native_Nullable _Nullable
1190#define native_Null_unspecified _Null_unspecified
1191#else
1192#define native_Nonnull
1193#define native_Nullable
1194#define native_Null_unspecified
1195#endif
1196
1198
1205
1239
1240#ifdef __CUDACC__
1241#define native_host __host__
1242#define native_device __device__
1243#define native_global __global__
1244#else
1245#define native_host
1246#define native_device
1247#define native_global
1248#endif
1249
1250#define native_hd native_host native_device
1251
1253
1278
1315
1316#if native_has_attribute(nonblocking)
1317 #define native_nonblocking [[clang::nonblocking]]
1318 #define native_blocking [[clang::blocking]]
1319 #define native_nonallocating [[clang::nonallocating]]
1320 #define native_allocating [[clang::allocating]]
1321 #define native_noalloc(x) [[clang::nonallocating(x)]]
1322 #define native_noblock(x) [[clang::nonblocking(x)]]
1323#else
1324 #define native_nonblocking
1325 #define native_blocking
1326 #define native_nonallocating
1327 #define native_allocating
1328 #define native_noalloc(__x)
1329 #define native_noblock(__x)
1330#endif
1331
1333
1334#define native_constexpr constexpr
1335
1336#else
1337// The shared shader declarations carry no host compiler promises.
1338#define native_nodiscard
1339#define native_constexpr
1340#define native_inline inline
1341#define native_pure
1342#define native_const
1343#define native_lifetimebound
1344#define native_noescape
1345#define native_reinitializes
1346#define native_artificial
1347#define native_flatten
1348#endif