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 import std.compiler;
24 
25 enum Constness {
26     Mutable,
27     Const,
28     Immutable,
29     Wildcard
30 }
31 
32 string constness_ToString(Constness c) {
33     switch(c){
34         case Constness.Mutable:
35             return "mutable";
36         case Constness.Const:
37             return "const";
38         case Constness.Immutable:
39             return "immutable";
40         case Constness.Wildcard:
41             return "inout";
42         default:
43             assert(0);
44     }
45 }
46 
47 template constness(T) {
48     static if(is(T == immutable)) {
49         enum constness = Constness.Immutable;
50     }else static if(is(T == const)) {
51         enum constness = Constness.Const;
52     }else static if(is(T == inout)) {
53         enum constness = Constness.Wildcard;
54     }else {
55         enum constness = Constness.Mutable;
56     }
57 }
58 
59 bool constCompatible(Constness c1, Constness c2) {
60     return c1 == c2 ||
61         c1 == Constness.Const && c2 != Constness.Wildcard ||
62         c2 == Constness.Const && c1 != Constness.Wildcard;
63 }
64 
65 template ApplyConstness(T, Constness constness) {
66     alias Unqual!T Tu;
67     static if(constness == Constness.Mutable) {
68         alias Tu ApplyConstness;
69     }else static if(constness == Constness.Const) {
70         alias const(Tu) ApplyConstness;
71     }else static if(constness == Constness.Wildcard) {
72         alias inout(Tu) ApplyConstness;
73     }else static if(constness == Constness.Immutable) {
74         alias immutable(Tu) ApplyConstness;
75     }else {
76         static assert(0);
77     }
78 }
79 
80 template ApplyConstness2(T, Constness constness) {
81     alias Unqual!T Tu;
82     static if(constness == Constness.Mutable) {
83         alias Tu ApplyConstness2;
84     }else static if(constness == Constness.Const) {
85         alias const(Tu) ApplyConstness2;
86     }else static if(constness == Constness.Wildcard) {
87         alias Tu ApplyConstness2;
88     }else static if(constness == Constness.Immutable) {
89         alias immutable(Tu) ApplyConstness2;
90     }else {
91         static assert(0);
92     }
93 }
94 
95 string attrs_to_string(uint attrs) {
96     string s = "";
97     with(FunctionAttribute) {
98         if(attrs & pure_) s ~= " pure";
99         if(attrs & nothrow_) s ~= " nothrow";
100         if(attrs & ref_) s ~= " ref";
101         if(attrs & property) s ~= " @property";
102         if(attrs & trusted) s ~= " @trusted";
103         if(attrs & safe) s ~= " @safe";
104         if(attrs & nogc) s ~= " @nogc";
105         static if(version_major == 2 && version_minor >= 67) {
106             if(attrs & return_) s ~= " return";
107         }
108     }
109     return s;
110 }
111 
112 // what U should be so 'new U' returns a T
113 template NewParamT(T) {
114     static if(isPointer!T && is(PointerTarget!T == struct))
115         alias PointerTarget!T NewParamT;
116     else alias T NewParamT;
117 }
118 
119 template StripSafeTrusted(F) {
120     enum attrs = functionAttributes!F ;
121     enum desired_attrs = attrs & ~FunctionAttribute.safe & ~FunctionAttribute.trusted;
122     enum linkage = functionLinkage!F;
123     alias SetFunctionAttributes!(F, linkage, desired_attrs) unqual_F;
124     static if(isFunctionPointer!F) {
125         enum constn = constness!(PointerTarget!F);
126         alias ApplyConstness!(PointerTarget!unqual_F, constn)* StripSafeTrusted;
127     }else static if(isDelegate!F) {
128         enum constn = constness!(F);
129         alias ApplyConstness!(unqual_F, constn) StripSafeTrusted;
130     }else{
131         enum constn = constness!(F);
132         alias ApplyConstness!(unqual_F, constn) StripSafeTrusted;
133     }
134 
135 
136 }
137 
138 class Z {
139     void a() immutable
140     {
141     }
142 }
143 //static assert(is(StripSafeTrusted!(typeof(&Z.a)) == typeof(&Z.a) ));
144 //static assert(is(StripSafeTrusted!(typeof(&Z.init.a)) == typeof(&Z.init.a) ));
145 import std.traits : isCallable;
146 import std.typetuple : TypeTuple;
147 template WorkaroundParameterDefaults(func...)
148 	if (func.length == 1 && isCallable!func)
149 {
150     static if (is(FunctionTypeOf!(func[0]) PT == __parameters))
151     {
152         template Get(size_t i)
153         {
154             // workaround scope escape check, see
155             // https://issues.dlang.org/show_bug.cgi?id=16582
156             // should use return scope once available
157             enum get = (PT[i .. i + 1] __args) @trusted
158 			{
159                 // If __args[0] is lazy, we force it to be evaluated like this.
160                 auto __pd_value = __args[0];
161                 auto __pd_val = &__pd_value; // workaround Bugzilla 16582
162                 return *__pd_val;
163             };
164             static if (is(typeof(get())))
165                 enum Get = get();
166             else
167                 alias Get = void;
168                 // If default arg doesn't exist, returns void instead.
169         }
170     }
171     else
172     {
173         static assert(0, func[0].stringof ~ "is not a function");
174 
175         // Define dummy entities to avoid pointless errors
176         template Get(size_t i) { enum Get = ""; }
177         alias PT = TypeTuple!();
178     }
179 
180     template Impl(size_t i = 0)
181     {
182         static if (i == PT.length)
183             alias Impl = TypeTuple!();
184         else
185             alias Impl = TypeTuple!(Get!i, Impl!(i + 1));
186     }
187 
188     alias WorkaroundParameterDefaults = Impl!();
189 }
190 
191 @safe unittest
192 {
193     int foo(int num, string name = "hello", int[] = [1,2,3], lazy int x = 0);
194     static assert(is(WorkaroundParameterDefaults!foo[0] == void));
195     static assert(   WorkaroundParameterDefaults!foo[1] == "hello");
196     static assert(   WorkaroundParameterDefaults!foo[2] == [1,2,3]);
197     static assert(   WorkaroundParameterDefaults!foo[3] == 0);
198 }
199 
200 @safe unittest
201 {
202     alias PDVT = WorkaroundParameterDefaults;
203 
204     void bar(int n = 1, string s = "hello"){}
205     static assert(PDVT!bar.length == 2);
206     static assert(PDVT!bar[0] == 1);
207     static assert(PDVT!bar[1] == "hello");
208     static assert(is(typeof(PDVT!bar) == typeof(TypeTuple!(1, "hello"))));
209 
210     void baz(int x, int n = 1, string s = "hello"){}
211     static assert(PDVT!baz.length == 3);
212     static assert(is(PDVT!baz[0] == void));
213     static assert(   PDVT!baz[1] == 1);
214     static assert(   PDVT!baz[2] == "hello");
215     static assert(is(typeof(PDVT!baz) == typeof(TypeTuple!(void, 1, "hello"))));
216 
217     // bug 10800 - property functions return empty string
218     @property void foo(int x = 3) { }
219     static assert(PDVT!foo.length == 1);
220     static assert(PDVT!foo[0] == 3);
221     static assert(is(typeof(PDVT!foo) == typeof(TypeTuple!(3))));
222 
223     struct Colour
224     {
225         ubyte a,r,g,b;
226 
227         static immutable Colour white = Colour(255,255,255,255);
228     }
229     void bug8106(Colour c = Colour.white) {}
230     //pragma(msg, PDVT!bug8106);
231     static assert(PDVT!bug8106[0] == Colour.white);
232     void bug16582(scope int* val = null) {}
233     static assert(PDVT!bug16582[0] is null);
234 }