Thanks for using Compiler Explorer
Sponsors
Jakt
C++
Ada
Analysis
Android Java
Android Kotlin
Assembly
C
C3
Carbon
C++ (Circle)
CIRCT
Clean
CMake
CMakeScript
COBOL
C++ for OpenCL
MLIR
Cppx
Cppx-Blue
Cppx-Gold
Cpp2-cppfront
Crystal
C#
CUDA C++
D
Dart
Elixir
Erlang
Fortran
F#
GLSL
Go
Haskell
HLSL
Hook
Hylo
IL
ispc
Java
Julia
Kotlin
LLVM IR
LLVM MIR
Modula-2
Nim
Objective-C
Objective-C++
OCaml
Odin
OpenCL C
Pascal
Pony
Python
Racket
Ruby
Rust
Snowball
Scala
Slang
Solidity
Spice
SPIR-V
Swift
LLVM TableGen
Toit
TypeScript Native
V
Vala
Visual Basic
Vyper
WASM
Zig
Javascript
GIMPLE
Ygen
d source #1
Output
Compile to binary object
Link to binary
Execute the code
Intel asm syntax
Demangle identifiers
Verbose demangling
Filters
Unused labels
Library functions
Directives
Comments
Horizontal whitespace
Debug intrinsics
Compiler
dmd 2.078.3
dmd 2.079.0
dmd 2.079.1
dmd 2.080.1
dmd 2.081.2
dmd 2.082.0
dmd 2.089.0
dmd 2.092.0
dmd 2.094.2
dmd nightly
gdc (trunk)
gdc 10.1
gdc 10.2
gdc 10.5
gdc 10.5 (assertions)
gdc 11.1
gdc 11.1 (assertions)
gdc 11.3
gdc 11.3 (assertions)
gdc 11.4
gdc 11.4 (assertions)
gdc 12.1
gdc 12.1 (assertions)
gdc 12.2
gdc 12.2 (assertions)
gdc 12.3
gdc 12.3 (assertions)
gdc 12.4
gdc 12.4 (assertions)
gdc 13.1
gdc 13.1 (assertions)
gdc 13.2
gdc 13.2 (assertions)
gdc 13.3
gdc 13.3 (assertions)
gdc 14.1
gdc 14.1 (assertions)
gdc 14.2
gdc 14.2 (assertions)
gdc 9.2
gdc 9.3
gdc 9.5
gdc arm 12.2.0
gdc arm 12.3.0
gdc arm 12.4.0
gdc arm 13.1.0
gdc arm 13.2.0
gdc arm 13.3.0
gdc arm 14.1.0
gdc arm 14.2.0
gdc arm64 12.2.0
gdc arm64 12.3.0
gdc arm64 12.4.0
gdc arm64 13.1.0
gdc arm64 13.2.0
gdc arm64 13.3.0
gdc arm64 14.1.0
gdc arm64 14.2.0
gdc hppa 14.2.0
gdc loongarch64 14.1.0
gdc loongarch64 14.2.0
gdc mips 12.1.0
gdc mips 12.2.0
gdc mips 12.3.0
gdc mips 12.4.0
gdc mips 13.1.0
gdc mips 13.2.0
gdc mips 13.3.0
gdc mips 14.1.0
gdc mips 14.2.0
gdc mips64 12.1.0
gdc mips64 12.2.0
gdc mips64 12.3.0
gdc mips64 12.4.0
gdc mips64 13.1.0
gdc mips64 13.2.0
gdc mips64 13.3.0
gdc mips64 14.1.0
gdc mips64 14.2.0
gdc mipsel 12.1.0
gdc mipsel 12.2.0
gdc mipsel 12.3.0
gdc mipsel 12.4.0
gdc mipsel 13.1.0
gdc mipsel 13.2.0
gdc mipsel 13.3.0
gdc mipsel 14.1.0
gdc mipsel 14.2.0
gdc powerpc 12.1.0
gdc powerpc 12.2.0
gdc powerpc 12.3.0
gdc powerpc 12.4.0
gdc powerpc 13.1.0
gdc powerpc 13.2.0
gdc powerpc 13.3.0
gdc powerpc 14.1.0
gdc powerpc 14.2.0
gdc powerpc64 12.1.0
gdc powerpc64 12.2.0
gdc powerpc64 12.3.0
gdc powerpc64 12.4.0
gdc powerpc64 13.1.0
gdc powerpc64 13.2.0
gdc powerpc64 13.3.0
gdc powerpc64 14.1.0
gdc powerpc64 14.2.0
gdc powerpc64 trunk
gdc powerpc64le 12.1.0
gdc powerpc64le 12.2.0
gdc powerpc64le 12.3.0
gdc powerpc64le 12.4.0
gdc powerpc64le 13.1.0
gdc powerpc64le 13.2.0
gdc powerpc64le 13.3.0
gdc powerpc64le 14.1.0
gdc powerpc64le 14.2.0
gdc powerpc64le trunk
gdc riscv32 12.2.0
gdc riscv32 12.3.0
gdc riscv32 12.4.0
gdc riscv32 13.1.0
gdc riscv32 13.2.0
gdc riscv32 13.3.0
gdc riscv32 14.1.0
gdc riscv32 14.2.0
gdc riscv32 trunk
gdc riscv64 12.2.0
gdc riscv64 12.3.0
gdc riscv64 12.4.0
gdc riscv64 13.1.0
gdc riscv64 13.2.0
gdc riscv64 13.3.0
gdc riscv64 14.1.0
gdc riscv64 14.2.0
gdc riscv64 trunk
gdc s390x 12.1.0
gdc s390x 12.2.0
gdc s390x 12.3.0
gdc s390x 12.4.0
gdc s390x 13.1.0
gdc s390x 13.2.0
gdc s390x 13.3.0
gdc s390x 14.1.0
gdc s390x 14.2.0
ldc 0.17.2
ldc 1.0.0
ldc 1.1.0
ldc 1.10.0
ldc 1.11.0
ldc 1.12.0
ldc 1.13.0
ldc 1.14.0
ldc 1.15.0
ldc 1.16.0
ldc 1.17.0
ldc 1.18.0
ldc 1.19.0
ldc 1.2.0
ldc 1.20.0
ldc 1.21.0
ldc 1.22.0
ldc 1.23.0
ldc 1.24.0
ldc 1.25.1
ldc 1.26.0
ldc 1.27.1
ldc 1.28.1
ldc 1.29.0
ldc 1.3.0
ldc 1.30.0
ldc 1.31.0
ldc 1.32.1
ldc 1.33.0
ldc 1.34.0
ldc 1.35.0
ldc 1.36.0
ldc 1.37.0
ldc 1.38.0
ldc 1.39.0
ldc 1.4.0
ldc 1.5.0
ldc 1.6.0
ldc 1.7.0
ldc 1.8.0
ldc 1.9.0
ldc beta
ldc latest CI
Options
Source code
mixin template EnumUnion(T) if (is(T == struct) && T.tupleof.length > 0) { static assert( is(typeof(this) == struct) || is(typeof(this) == class), "Only `struct` and `class` types can mixin `EnumUnion`" ); alias __Types = typeof(T.tupleof); enum string[] __names = { string[] result; static foreach (alias name; T.tupleof) result ~= name.stringof; return result; }(); static foreach (name; __names) { static assert(name.length > 0, "Every option must have a non-empty name"); static assert(name.length == 1 || name[0 .. 2] != "__", "Option names cannot start with double underscore"); } import std.conv : __text = text; static if (__Types.length <= ubyte .max) alias __IndexBase = ubyte; else static if (__Types.length <= ushort.max) alias __IndexBase = ushort; else static if (__Types.length <= uint .max) alias __IndexBase = uint; else alias __IndexBase = ulong; mixin({ string result = "enum __Index : __IndexBase {"; static foreach (name; __names) { result ~= name; result ~= ","; } return result ~ "}"; }()); static foreach (name; __names) { mixin(iq{enum __$(name)_t : __Index { index = __Index.$(name) }}.__text); } private __Index __index; invariant(__index < __Types.length); union { static foreach (i, name; __names) { mixin(iq{ private __Types[i] __$(name); }.__text); } } static foreach (i, name; __names) { mixin(iq{ @trusted pure @nogc nothrow ref /* inout(__Types[i]) */ $(name)() inout @property { static immutable string errorMessage = i"EnumUnion: member `$(name)` (index $(i)) not active".__text; if (__index != __Index.$(name)) assert(0, errorMessage); return __$(name); } @trusted pure @nogc nothrow auto /* inout(__Types[i])* */ __as_$(name)() inout @property => __index == __Index.$(name) ? &__$(name) : null; @safe pure @nogc nothrow bool __is_$(name)() const @property => __index == __Index.$(name); @system pure @nogc nothrow ref /* inout(__Types[i]) */ __unsafe_$(name)() inout @property in (__index == __Index.$(name)) => __$(name); $(is(typeof((__Types[i] x) @nogc { import core.lifetime : move; x = move(x); })) ? "@nogc" : "") $(is(typeof((__Types[i] x) nothrow { import core.lifetime : move; x = move(x); })) ? "nothrow" : "") $(is(typeof((__Types[i] x) pure { import core.lifetime : move; x = move(x); })) ? "pure" : "") void $(name)(__Types[i] value) @property @system { import core.lifetime : move; this.__index = __Index.$(name); auto self = (() @trusted => &this.__$(name))(); *self = move(value); } $(is(typeof((ref __Types[i] x) @nogc { x = x; })) ? "@nogc" : "") $(is(typeof((ref __Types[i] x) nothrow { x = x; })) ? "nothrow" : "") $(is(typeof((ref __Types[i] x) pure { x = x; })) ? "pure" : "") void $(name)(ref __Types[i] value) @property @system { this.__index = __Index.$(name); auto self = (() @trusted => &this.__$(name))(); *self = value; } }.__text); } static foreach (i, name; __names) { mixin(iq{ $(is(typeof((__Types[i] x) @safe { import core.lifetime : move; __Types[i] y = move(x); })) ? "@safe" : "") $(is(typeof((__Types[i] x) @nogc { import core.lifetime : move; __Types[i] y = move(x); })) ? "@nogc" : "") $(is(typeof((__Types[i] x) nothrow { import core.lifetime : move; __Types[i] y = move(x); })) ? "nothrow" : "") $(is(typeof((__Types[i] x) pure { import core.lifetime : move; __Types[i] y = move(x); })) ? "pure" : "") this(__Types[i] $(name), __$(name)_t = __$(name)_t.index) { import core.lifetime : move; this.__index = __Index.$(name); this.__$(name) = move($(name)); } $(is(typeof((__Types[i] x) @safe { __Types[i] y = x; })) ? "@safe" : "") $(is(typeof((__Types[i] x) @nogc { __Types[i] y = x; })) ? "@nogc" : "") $(is(typeof((__Types[i] x) nothrow { __Types[i] y = x; })) ? "nothrow" : "") $(is(typeof((__Types[i] x) pure { __Types[i] y = x; })) ? "pure" : "") this(ref __Types[i] $(name), __$(name)_t = __$(name)_t.index) { this.__index = __Index.$(name); this.__$(name) = $(name); } }.__text); } static if (!is(typeof(this) == class) && !is(typeof(this) == interface)) void opAssign(ref typeof(this) rhs) { final switch (rhs.__index) { static foreach (i, name; __names) { case i: mixin(iq{$(name) = rhs.$(name);}.__text); return; } } } static if (!is(typeof(this) == class) && !is(typeof(this) == interface)) void opAssign(typeof(this) rhs) { import core.lifetime : move; final switch (rhs.__index) { static foreach (i, name; __names) { case i: if (__ctfe) mixin(iq{$(name) = rhs.$(name);}.__text); else mixin(iq{$(name) = move(rhs.$(name));}.__text); return; } } } private enum bool __anyDtor = { static foreach (alias T; __Types) { static if (!is(T == class) && __traits(hasMember, T, "__dtor")) { return true; } } return false; }(); private enum string __dtorAttribute(string attr) = { string result = attr; static foreach (i, alias T; __Types) { static if (!is(T == class) && __traits(hasMember, T, "__dtor")) { static if (!is(typeof(mixin(iq{(T x) $(attr) { x.__dtor; }}.__text)))) result = ""; } } return result; }(); static if (__anyDtor) { mixin(iq{ $(__dtorAttribute!"@safe") $(__dtorAttribute!"pure") $(__dtorAttribute!"nothrow") $(__dtorAttribute!"@nogc") $(__dtorAttribute!"scope") ~this() { dtors: switch (__index) { default: break dtors; static foreach (name; __names) { static if (!is(T == class) && __traits(hasMember, T, "__dtor")) { case mixin("__Index.", name): mixin("__", name, ".__dtor()"); break dtors; } } } } }.__text); alias __dtor_ = __dtor; } static foreach (alias dataMember; typeof(this).tupleof) { static assert(dataMember.stringof.length > 2 && dataMember.stringof[0 .. 2] == "__" || { foreach (name; __names) if (dataMember.stringof == name) return true; return false; }(), "you cannot declare custom data members"); } static assert(__traits(getOverloads, typeof(this), "__ctor").length == __Types.length * 2, "you cannot declare custom constructors" ); static foreach (i, alias ctor; __traits(getOverloads, typeof(this), "__ctor")) { static if ({ mixin(iq{ alias Expected = $(is(typeof(this) == class) || is(typeof(this) == interface) ? "" : "ref") typeof(this) function( $(i % 2 != 0 ? "ref" : "") __Types[i/2] $(__names[i/2]), __$(__names[i/2])_t = __$(__names[i/2])_t.index, ); }.__text); return !is(typeof(&ctor) : Expected); }()) { static assert(0, "you cannot declare custom constructors"); } } static assert(__anyDtor || !__traits(hasMember, typeof(this), "__dtor"), "you cannot declare a custom destructor" ); static assert(!__anyDtor || __traits(isSame, typeof(this).__dtor, __dtor_), "you cannot declare a custom destructor" ); } @safe pure nothrow @nogc EU.__Index index(EU)(scope ref const EU eu) @property => eu.__index; EU.__Index index(EU)(scope const EU eu) @property => eu.__index; private enum bool anyOptionAssignableFrom(EU, T) = { import std.conv : text; static foreach (name; EU.__names) {{ static if (__traits(compiles, (ref EU eu, T value) { mixin(iq{*&(eu.$(name)()) = value;}.text); })) { return true; } }} return false; }(); /// Assigns `value` to the currently active option of an enum union. /// It is an error if the active option does not support assignment from void assignActiveOption(EU, T)(ref EU eu, auto ref T value) if (anyOptionAssignableFrom!(EU, T)) { import std.conv : text; final switch (eu.__index) { static foreach (i, name; eu.__names) { case i: static if (__traits(compiles, { mixin(iq{*&(eu.$(name)()) = value;}.text); })) { mixin(iq{*&(eu.$(name)()) = value;}.text); return; } else { static immutable errorMessage = "Active option `" ~ name ~ "` of type `" ~ EU.__Types[i].stringof ~ "` cannot be assigned a value of type `" ~ T.stringof ~ "`"; assert(0, errorMessage); } } } } template matchOrdered(fs...) { auto ref matchOrdered(EU)(auto ref EU eu) if (fs.length == EU.__Types.length) { import std.traits, std.meta, std.conv; static if (__traits(isRef, eu)) alias valueOf = lvalueOf; else alias valueOf = rvalueOf; alias R = mixin({ string result = "CommonType!("; static foreach (i, name; EU.__names) { result ~= iq{typeof(fs[$(i)](valueOf!(EU.__Types[$(i)]))),}.text; } return result ~ ")"; }()); final switch (eu.__index) { static foreach (i, name; EU.__names) {{ static if (isFunction!(fs[i]) || is(typeof(fs[i]) == struct) || is(typeof(fs[i]) == union) || is(typeof(fs[i]) == class) || is(typeof(fs[i]) == interface)) { static assert(0, "function pointer or delegate required: " ~ "use " ~ name ~ " => " ~ __traits(identifier, fs[i]) ~ "(" ~ name ~ ") " ~ "or " ~ name ~ " => " ~ name ~ "." ~ __traits(identifier, fs[i]) ); } else static if ( (isFunctionPointer!( fs[i] ) || isDelegate!( fs[i] )) && is(typeof( fs[i] ) ps == __parameters) || (isFunctionPointer!(Instantiate!(fs[i], EU.__Types[i])) || isDelegate!(Instantiate!(fs[i], EU.__Types[i]))) && is(typeof(Instantiate!(fs[i], EU.__Types[i])) ps == __parameters)) { static if (!__traits(compiles, mixin(iq{(ps[0 .. 1]){}($(name): valueOf!(EU.__Types[i]))}.text) )) { static void fail(ps[0 .. 1]) { static assert(0, "name mismatch: enum union member name is `" ~ name ~"`, " ~ "but parameter name is `" ~ __traits(parameters)[0].stringof ~ "`" ); } } else { case i: static if (__traits(isRef, eu) || is(EU == class) || is(EU == interface)) { return cast(R) fs[i]( (function ref (ref EU eu) @trusted scope => mixin("eu.__", name))(eu) ); } else { import core.lifetime : move; return cast(R) fs[i](move( (function ref (ref EU eu) @trusted scope => mixin("eu.__", name))(eu) )); } } } else { static assert(0, "function pointer or delegate syntax required"); } }} } } } template matchDefault(fs...) if (fs.length > 0) { auto ref matchDefault(EU)(auto ref EU eu) if (fs.length - 1 <= EU.__Types.length) { import std.traits, std.meta, std.conv; static foreach (i, name; eu.__names) { static foreach (j, f; fs[0 .. $-1]) { static if (isFunction!f || is(typeof(f) == struct) || is(typeof(f) == union) || is(typeof(f) == class) || is(typeof(f) == interface)) { static assert(0, "function pointer or delegate required: " ~ "use " ~ name ~ " => " ~ __traits(identifier, f) ~ "(" ~ name ~ ") " ~ "or " ~ name ~ " => " ~ name ~ "." ~ __traits(identifier, f) ); } else static if (mixin(iq{ (isFunctionPointer! f || isDelegate! f ) && is(typeof(f ) ps$(i)_$(j) == __parameters) || (isFunctionPointer!(f!(EU.__Types[i])) || isDelegate!(f!(EU.__Types[i]))) && is(typeof(f!(EU.__Types[i])) ps$(i)_$(j) == __parameters) }.text)) { static if (__traits(compiles, mixin(iq{(ps$(i)_$(j)[0 .. 1]){}($(name): lvalueOf!(EU.__Types[i]))}.text) )) { static if (is(typeof(mixin(iq{f$(i)}.text)))) { static assert(0, i"double handler for option $(name)".text); } else { mixin(iq{alias f$(i) = f;}.text); } } else { // pragma(msg, mixin(iq{(ps$(i)_$(j)[0 .. 1])}.text), i" is a handler, but not the handler for $(name)".text); } } } static if (!is(typeof(mixin(iq{f$(i)}.text)))) { mixin(iq{alias f$(i) = fs[$ - 1];}.text); } } enum string orderedFsStr = { string result = "AliasSeq!("; foreach (i; 0 .. EU.__Types.length) { result ~= i"f$(i), ".text; } return result ~ ")"; }(); //pragma(msg, orderedFsStr); alias orderedFs = mixin(orderedFsStr); import core.lifetime : forward; return matchOrdered!orderedFs(forward!eu); } } template match(fs...) { auto ref match(EU)(auto ref EU eu) if (fs.length == EU.__Types.length) { static struct __X {} static noreturn f(T)(__X) { static assert(0); assert(0); }; import core.lifetime : forward; return matchDefault!(fs, f)(forward!eu); } } template matchOrderedDefault(fs...) if (fs.length > 0) { import core.lifetime : forward; import std.conv : text; alias matchers = fs[0 .. $ - 1]; auto matchOrderedDefault(EU)(auto ref EU eu) if (fs.length <= EU.__Types.length + 1) { enum string defaultMatchers = () { string result; foreach (name; EU.__names[matchers.length .. EU.__Types.length]) { result ~= iq{$(name) => fs[$ - 1]($(name)),}.text; } return result; }(); mixin(iq{ alias mo = matchOrdered!(matchers, $(defaultMatchers)); }.text); return mo!EU(forward!eu); } } final class Expr { struct Binary { Expr lhs, rhs; } private static struct Impl { int constant; string variable; Expr minus; Binary plus, times; } mixin EnumUnion!Impl; int eval(scope const int[string] context) const scope @safe pure nothrow /*@nogc*/ => this.matchOrdered!( (constant) => constant, (variable) => context[variable], (minus) => -minus.eval(context), (plus) => plus.lhs.eval(context) + plus.rhs.eval(context), (times) => times.lhs.eval(context) * times.rhs.eval(context), ); int eval2(scope const int[string] context) const scope @safe @nogc pure nothrow { // Compiler isn’t yet smart enough to figure out that handlers don’t escape. scope c = delegate(const int constant) @safe @nogc pure nothrow scope => constant; scope v = delegate(const string variable) @safe @nogc pure nothrow scope => context[variable]; scope m = delegate(const Expr minus) @safe @nogc pure nothrow scope => -minus.eval2(context); scope p = delegate(const Binary plus) @safe @nogc pure nothrow scope => plus.lhs.eval2(context) + plus.rhs.eval2(context); scope t = delegate(const Binary times) @safe @nogc pure nothrow scope => times.lhs.eval2(context) * times.rhs.eval2(context); return this.matchOrdered!(c, v, m, p, t); } void toString(Sink)(scope Sink sink, immutable uint precedence = 0) const scope { //https://issues.dlang.org/show_bug.cgi?id=7205 final switch (__index) { import std.range.primitives : put; case __Index.constant: scope char[12] buf; int c = (() @trusted => __unsafe_constant())(); immutable bool negative = c < 0; if (negative) c = -c; size_t i = buf.length; foreach_reverse (ref ch; buf) { ch = '0' + c % 10; c /= 10; --i; if (c == 0) break; } if (negative && precedence >= 2) put(sink, "("); if (negative) put(sink, "\−"); put(sink, buf[i .. $]); if (negative && precedence >= 2) put(sink, ")"); return; case __Index.variable: sink.put((() @trusted => __unsafe_variable())()); return; case __Index.minus: if (precedence >= 1) put(sink, "("); put(sink, "\−"); const(Expr) minus = (() @trusted => __unsafe_minus())(); minus.toString(sink); if (precedence >= 1) put(sink, ")"); return; case __Index.plus: if (precedence >= 2) put(sink, "("); const(Binary) plus = (() @trusted => __unsafe_plus())(); plus.lhs.toString(sink, 0); if (plus.rhs.index == __Index.minus) { put(sink, " \− "); plus.rhs.minus().toString(sink, 1); } else { put(sink, " + "); plus.rhs.toString(sink, 1); } if (precedence >= 2) put(sink, ")"); return; case __Index.times: const(Binary) times = (() @trusted => __unsafe_times())(); times.lhs.toString(sink, 2); put(sink, " * "); times.rhs.toString(sink, 2); return; } } override string toString() const scope @safe pure nothrow { import std.array : appender; auto result = appender!string(); toString(result); return result[]; } } void main() @safe { Expr expr = new Expr(plus: Expr.Binary( new Expr(times: Expr.Binary( new Expr(-2), new Expr(1) )), new Expr(minus: new Expr("x")) )); import std.stdio; writeln(expr, " = ", expr.eval(["x": 1])); }
Become a Patron
Sponsor on GitHub
Donate via PayPal
Source on GitHub
Mailing list
Installed libraries
Wiki
Report an issue
How it works
Contact the author
CE on Mastodon
CE on Bluesky
About the author
Statistics
Changelog
Version tree