1 /* 2 Permission is hereby granted, free of charge, to any person obtaining a copy of 3 this software and associated documentation files (the "Software"), to deal in 4 the Software without restriction, including without limitation the rights to 5 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies 6 of the Software, and to permit persons to whom the Software is furnished to do 7 so, subject to the following conditions: 8 9 The above copyright notice and this permission notice shall be included in all 10 copies or substantial portions of the Software. 11 12 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 13 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 14 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 15 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 16 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 17 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 18 SOFTWARE. 19 */ 20 module util.typeinfo; 21 22 import std.traits; 23 24 enum Constness { 25 Mutable, 26 Const, 27 Immutable, 28 Wildcard 29 } 30 31 string constness_ToString(Constness c) { 32 switch(c){ 33 case Constness.Mutable: 34 return "mutable"; 35 case Constness.Const: 36 return "const"; 37 case Constness.Immutable: 38 return "immutable"; 39 case Constness.Wildcard: 40 return "inout"; 41 default: 42 assert(0); 43 } 44 } 45 46 template constness(T) { 47 static if(is(T == immutable)) { 48 enum constness = Constness.Immutable; 49 }else static if(is(T == const)) { 50 enum constness = Constness.Const; 51 }else static if(is(T == inout)) { 52 enum constness = Constness.Wildcard; 53 }else { 54 enum constness = Constness.Mutable; 55 } 56 } 57 58 bool constCompatible(Constness c1, Constness c2) { 59 return c1 == c2 || 60 c1 == Constness.Const && c2 != Constness.Wildcard || 61 c2 == Constness.Const && c1 != Constness.Wildcard; 62 } 63 64 template ApplyConstness(T, Constness constness) { 65 alias Unqual!T Tu; 66 static if(constness == Constness.Mutable) { 67 alias Tu ApplyConstness; 68 }else static if(constness == Constness.Const) { 69 alias const(Tu) ApplyConstness; 70 }else static if(constness == Constness.Wildcard) { 71 alias inout(Tu) ApplyConstness; 72 }else static if(constness == Constness.Immutable) { 73 alias immutable(Tu) ApplyConstness; 74 }else { 75 static assert(0); 76 } 77 } 78 79 template ApplyConstness2(T, Constness constness) { 80 alias Unqual!T Tu; 81 static if(constness == Constness.Mutable) { 82 alias Tu ApplyConstness2; 83 }else static if(constness == Constness.Const) { 84 alias const(Tu) ApplyConstness2; 85 }else static if(constness == Constness.Wildcard) { 86 alias Tu ApplyConstness2; 87 }else static if(constness == Constness.Immutable) { 88 alias immutable(Tu) ApplyConstness2; 89 }else { 90 static assert(0); 91 } 92 } 93 94 string attrs_to_string(uint attrs) { 95 string s = ""; 96 with(FunctionAttribute) { 97 if(attrs & pure_) s ~= " pure"; 98 if(attrs & nothrow_) s ~= " nothrow"; 99 if(attrs & ref_) s ~= " ref"; 100 if(attrs & property) s ~= " @property"; 101 if(attrs & trusted) s ~= " @trusted"; 102 if(attrs & safe) s ~= " @safe"; 103 } 104 return s; 105 } 106 107 // what U should be so 'new U' returns a T 108 template NewParamT(T) { 109 static if(isPointer!T && is(PointerTarget!T == struct)) 110 alias PointerTarget!T NewParamT; 111 else alias T NewParamT; 112 } 113 114 template StripSafeTrusted(F) { 115 enum attrs = functionAttributes!F ; 116 enum desired_attrs = attrs & ~FunctionAttribute.safe & ~FunctionAttribute.trusted; 117 enum linkage = functionLinkage!F; 118 alias SetFunctionAttributes!(F, linkage, desired_attrs) unqual_F; 119 static if(isFunctionPointer!F) { 120 enum constn = constness!(pointerTarget!F); 121 alias ApplyConstness!(pointerTarget!unqual_F, constn)* StripSafeTrusted; 122 }else static if(isDelegate!F) { 123 enum constn = constness!(F); 124 alias ApplyConstness!(unqual_F, constn) StripSafeTrusted; 125 }else{ 126 enum constn = constness!(F); 127 alias ApplyConstness!(unqual_F, constn) StripSafeTrusted; 128 } 129 130 131 } 132 133 class Z { 134 void a() immutable 135 { 136 } 137 } 138 //static assert(is(StripSafeTrusted!(typeof(&Z.a)) == typeof(&Z.a) )); 139 //static assert(is(StripSafeTrusted!(typeof(&Z.init.a)) == typeof(&Z.init.a) ));