]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/testsuite/gdc.test/runnable/mixin2.d
d: Import dmd b8384668f, druntime e6caaab9, phobos 5ab9ad256 (v2.098.0-beta.1)
[thirdparty/gcc.git] / gcc / testsuite / gdc.test / runnable / mixin2.d
CommitLineData
5fee5ec3 1/*
7da827c9
IB
2TEST_OUTPUT:
3---
4hello
5hello
6
7
8
9hello
10hello
11---
12
13RUN_OUTPUT:
14---
1531
1642
1753
1864
1975
2086
2197
22108
23119
241210
255
26test4
27Constructor
28 Inside Scope
29Destructor
30hey
31Success
32---
33*/
5fee5ec3 34import core.stdc.stdio;
b4c522fa
IB
35
36/*********************************************/
37
38int foo(int x)
39{
40 return mixin("x + 1"w);
41}
42
43void test1()
44{
45 assert(foo(3) == 4);
46}
47
48/*********************************************/
49
50void test2()
51{
52 int j;
53 mixin("
54 int x = 3;
55 for (int i = 0; i < 10; i++)
5fee5ec3 56 printf(\"%d%d\n\", x + i, ++j);
b4c522fa
IB
57 ");
58 assert(j == 10);
59}
60
61/*********************************************/
62
63mixin("int abc3 = 5;");
64
65void test3()
66{
5fee5ec3 67 printf("%d\n", abc3);
b4c522fa
IB
68 assert(abc3 == 5);
69}
70
71/*********************************************/
72
73mixin("
74void test4()
75{
5fee5ec3 76 printf(\"test4\n\");
b4c522fa
IB
77" ~ "}");
78
79/*********************************************/
80
81int x5;
82
5fee5ec3 83class Foo5
b4c522fa
IB
84{
85 this ()
86 {
5fee5ec3 87 printf ("Constructor\n");
b4c522fa
IB
88 assert(x5 == 0);
89 x5++;
90 }
91 ~this ()
92 {
5fee5ec3 93 printf ("Destructor\n");
b4c522fa
IB
94 assert(x5 == 2);
95 x5++;
96 }
97}
98
99void test5()
100{
101 {
102 mixin ("scope Foo5 f = new Foo5;\n");
5fee5ec3 103 printf (" Inside Scope\n");
b4c522fa
IB
104 assert(x5 == 1);
105 x5++;
106 }
107 assert(x5 == 3);
108}
109
110/*********************************************/
111
112void test6()
113{
114 static const b = "printf(`hey\n`);";
115
116 if (true)
117 mixin(b);
118}
119
120/*********************************************/
121
122template Foo7(alias f)
123{
124}
125
126class Bar7
127{
128 mixin Foo7!( function {} );
129}
130
131void test7()
132{
133}
134
135/*********************************************/
136
137template TupleDecls(T, R ...) {
138 T value;
139 static if (R.length)
140 mixin TupleDecls!(R) Inner;
141}
142
143struct TupleStruct(T ...) { mixin TupleDecls!(T); }
144
145void test8() {
146 alias TupleStruct!(char[], char[]) twoStrings;
147}
148
149/*********************************************/
150
151template Magic()
152{
153 void* magic = null;
154}
155
156struct Item
157{
158 mixin Magic A;
159}
160
161struct Foo9(alias S)
162{
163}
164
165void test9()
166{
167 Foo9!(Item.A) bar;
168}
169
170/*********************************************/
171
172pragma(msg, "hello");
173pragma(msg, ['h', 'e', 'l', 'l', 'o']);
174pragma(msg, "");
175pragma(msg, []);
176pragma(msg, null);
177mixin("string hello;");
178mixin(['i', 'n', 't', ' ', 't', 'e', 's', 't', '1', '0', 'x', ';']);
179mixin("");
180mixin([]);
181mixin(null);
182void test10()
183{
184 pragma(msg, "hello");
185 pragma(msg, ['h', 'e', 'l', 'l', 'o']);
186 pragma(msg, "");
187 pragma(msg, []);
188 pragma(msg, null);
189 mixin("string hello;");
190 mixin(['i', 'n', 't', ' ', 'a', ';']);
191 mixin("");
192 mixin([]);
193 mixin(null);
194}
195
196/*********************************************/
5fee5ec3 197// https://issues.dlang.org/show_bug.cgi?id=7560
b4c522fa
IB
198
199class Base7560
200{
201 template getter(T)
202 {
203 void get(ref T[] i, uint n) {}
204 }
205 mixin getter!uint;
206 mixin getter!char;
207}
208
209class Derived7560 : Base7560
210{
211 alias Base7560.get get;
212 void get(ref char[] x) {}
213}
214
215/*********************************************/
5fee5ec3 216// https://issues.dlang.org/show_bug.cgi?id=10577
b4c522fa
IB
217
218enum sync10577;
219
220public template get_sync10577(size_t I, A...)
221{
222 static if (I == A.length) enum bool get_sync10577 = false;
223 else static if (is(A[I] == sync10577)) enum bool get_sync10577 = true;
224 else enum bool get_sync10577 = get_sync!10577(I+1, A);
225}
226
227template add_sync10577(T, size_t ITER=0)
228{
229 static if (ITER == (__traits(derivedMembers, T).length))
230 {
231 enum string add_sync10577 = "";
232 }
233 else
234 {
235 enum string mem = __traits(derivedMembers, T)[ITER];
236 enum string add_sync10577 =
237 "static if (! __traits(isVirtualMethod, " ~ mem ~ ")) {" ~
238 "mixin(add_sync10577!(get_sync10577!(0, __traits(getAttributes, "
239 ~ mem ~ ")), \"" ~ mem ~ "\"));} " ~ add_sync10577!(T, ITER+1);
240 }
241}
242
243template add_sync10577(bool A, string M)
244{
245 static if (A)
246 {
247 enum string add_sync10577 = " auto " ~ M[1..$] ~
248 "() { synchronized(this) return " ~ M ~ "; }";
249 }
250 else
251 {
252 enum string add_sync10577 = "";
253 }
254}
255
256class base10577
257{
258 public void foo() {}
259}
260
261class derived10577 : base10577
262{
263 mixin(add_sync10577!(derived10577));
264 @sync10577 private bool _bar;
265
266 public override void foo() {}
267}
268
269/*********************************************/
5fee5ec3 270// https://issues.dlang.org/show_bug.cgi?id=10583
b4c522fa
IB
271
272enum sync10583;
273
274public template get_sync10583(size_t I, A...)
275{
276 static if (I == A.length)
277 enum bool get_sync10583 = false;
278 else static if (is(A[I] == sync10583))
279 enum bool get_sync10583 = true;
280 else
281 enum bool get_sync10583 = get_sync10583!(I+1, A);
282}
283
284template add_sync10583(T, size_t ITER = 0)
285{
286 static if (ITER == (__traits(derivedMembers, T).length))
287 {
288 enum string add_sync10583 = "";
289 }
290 else
291 {
292 enum string mem = __traits(derivedMembers, T)[ITER];
293 enum string add_sync10583 =
294 "mixin(add_sync10583!(get_sync10583!(0, __traits(getAttributes, "
295 ~ mem ~ ")), __traits(getProtection, "
296 ~ mem ~ "), \"" ~ mem ~ "\"));\n" ~ add_sync10583!(T, ITER+1);
297 }
298}
299
300template add_sync10583(bool A, string P, string M)
301{
302 static if (A)
303 {
304 enum string add_sync10583 = " auto " ~ M[1..$] ~
305 "() { synchronized(this) return " ~ M ~ "; }";
306 }
307 else
308 enum string add_sync10583 = "";
309}
310
311class derived10583
312{
313 mixin(add_sync10583!(derived10583));
314 @sync10583 private bool _bar;
315 void frop() {}
316}
317
318/*********************************************/
319
320string rep(string s, int n)
321{
322 return n > 1 ? s ~ rep(s, n-1) : s;
323}
324
325void test7156()
326{
327 int i;
328 mixin(rep("++i;", 200));
329}
330
331/*********************************************/
5fee5ec3 332// https://issues.dlang.org/show_bug.cgi?id=7553
b4c522fa
IB
333
334template Foo7553()
335{
336 struct Range7553 {}
337}
338template Biz7553()
339{
340 struct Range7553 {}
341}
342
343class Bar7553
344{
345 mixin Foo7553!() index0;
346 mixin Biz7553!() index1;
347
348 auto to_range(Range)(Range r)
349 {
350 return r;
351 }
352
353}
354
355void test7553()
356{
357 auto r2 = new Bar7553().to_range(1);
358}
359
360/*********************************************/
5fee5ec3 361// https://issues.dlang.org/show_bug.cgi?id=13479
b4c522fa
IB
362
363mixin template F13479()
364{
365 void t()()
366 {
367 }
368
369 alias t!() a;
370}
371
372void test13479()
373{
374 mixin F13479!();
375 a();
376}
377
378/*********************************************/
379
380void main()
381{
382 test1();
383 test2();
384 test3();
385 test4();
386 test5();
387 test6();
388 test7();
389 test8();
390 test9();
391 test10();
392 test7156();
393 test13479();
394
5fee5ec3 395 printf("Success\n");
b4c522fa 396}