summaryrefslogtreecommitdiff
path: root/source/slang/core.meta.slang
diff options
context:
space:
mode:
authorYong He <yonghe@outlook.com>2025-06-04 13:07:11 -0700
committerGitHub <noreply@github.com>2025-06-04 13:07:11 -0700
commit2d7106640addf0ac88e0a5462117cd90b13a5e73 (patch)
tree6ce27db4d6c5bf06db84dee755a82e09b1b3a2ca /source/slang/core.meta.slang
parent812e478989e27983b8dea7ab11964de751654ba2 (diff)
Add legalization for 0-sized arrays. (#7327)
* Add legalization for 0-sized arrays. * Allow 0-sized arrays in the front-end. * More tests. * Add `Conditional<T, hasValue>` type to core module. * Update toc. * Fix wording. * Update test.
Diffstat (limited to 'source/slang/core.meta.slang')
-rw-r--r--source/slang/core.meta.slang44
1 files changed, 44 insertions, 0 deletions
diff --git a/source/slang/core.meta.slang b/source/slang/core.meta.slang
index 484f51bfc..51e2f326e 100644
--- a/source/slang/core.meta.slang
+++ b/source/slang/core.meta.slang
@@ -1513,6 +1513,50 @@ bool operator!=(__none_t noneVal, Optional<T> val)
return val.hasValue;
}
+struct Conditional<T, bool hasValue>
+{
+ internal T storage[hasValue];
+
+ __implicit_conversion($(kConversionCost_ValToOptional))
+ [__unsafeForceInlineEarly]
+ __init(T val) { if (hasValue) storage[0] = val;}
+
+ [__unsafeForceInlineEarly]
+ public Optional<T> get()
+ {
+ if (hasValue)
+ {
+ return Optional<T>(storage[0]);
+ }
+ else
+ {
+ return none;
+ }
+ }
+
+ [__unsafeForceInlineEarly]
+ [mutating]
+ public void set(T value)
+ {
+ if (hasValue)
+ storage[0] = value;
+ }
+}
+
+extension<T> Optional<T>
+{
+ __implicit_conversion($(kConversionCost_ImplicitDereference))
+ __generic<bool condHasValue>
+ [__unsafeForceInlineEarly]
+ __init(Conditional<T, condHasValue> condVal)
+ {
+ if (condHasValue)
+ this = Optional<T>(condVal.storage[0]);
+ else
+ this = none;
+ }
+}
+
//@public:
/// A variadic generic storing the product of several types.