-
Notifications
You must be signed in to change notification settings - Fork 1
/
nested.d
2584 lines (2090 loc) · 41.3 KB
/
nested.d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// REQUIRED_ARGS:
import core.stdc.stdio;
/*******************************************/
int bar(int a)
{
int foo(int b) { return b + 1; }
return foo(a);
}
void test1()
{
assert(bar(3) == 4);
}
/*******************************************/
int bar2(int a)
{
static int c = 4;
int foo(int b) { return b + c + 1; }
return foo(a);
}
void test2()
{
assert(bar2(3) == 8);
}
/*******************************************/
int bar3(int a)
{
static int foo(int b) { return b + 1; }
return foo(a);
}
void test3()
{
assert(bar3(3) == 4);
}
/*******************************************/
int bar4(int a)
{
static int c = 4;
static int foo(int b) { return b + c + 1; }
return foo(a);
}
void test4()
{
assert(bar4(3) == 8);
}
/*******************************************/
int bar5(int a)
{
int c = 4;
int foo(int b) { return b + c + 1; }
return c + foo(a);
}
void test5()
{
assert(bar5(3) == 12);
}
/*******************************************/
int bar6(int a)
{
int c = 4;
int foob(int b) { return b + c + 1; }
int fooa(int b) { return foob(c + b) * 7; }
return fooa(a);
}
void test6()
{
assert(bar6(3) == 84);
}
/*******************************************/
int bar7(int a)
{
static int c = 4;
static int foob(int b) { return b + c + 1; }
int function(int) fp = &foob;
return fp(a);
}
void test7()
{
assert(bar7(3) == 8);
}
/*******************************************/
int bar8(int a)
{
int c = 4;
int foob(int b) { return b + c + 1; }
int delegate(int) fp = &foob;
return fp(a);
}
void test8()
{
assert(bar8(3) == 8);
}
/*******************************************/
struct Abc9
{
int a;
int b;
int c = 7;
int bar(int x)
{
Abc9 *foo() { return &this; }
Abc9 *p = foo();
assert(p == &this);
return p.c + x;
}
}
void test9()
{
Abc9 x;
assert(x.bar(3) == 10);
}
/*******************************************/
class Abc10
{
int a;
int b;
int c = 7;
int bar(int x)
{
Abc10 foo() { return this; }
Abc10 p = foo();
assert(p == this);
return p.c + x;
}
}
void test10()
{
Abc10 x = new Abc10();
assert(x.bar(3) == 10);
}
/*******************************************/
class Collection
{
int[3] array;
void opApply(void delegate(int) fp)
{
for (int i = 0; i < array.length; i++)
fp(array[i]);
}
}
int func11(Collection c)
{
int max = int.min;
void comp_max(int i)
{
if (i > max)
max = i;
}
c.opApply(&comp_max);
return max;
}
void test11()
{
Collection c = new Collection();
c.array[0] = 7;
c.array[1] = 26;
c.array[2] = 25;
int m = func11(c);
assert(m == 26);
}
/*******************************************/
void SimpleNestedFunction ()
{
int nest () { return 432; }
assert (nest () == 432);
int delegate () func = &nest;
assert (func () == 432);
}
void AccessParentScope ()
{
int value = 9;
int nest () { assert (value == 9); return 9; }
assert (nest () == 9);
}
void CrossNestedScope ()
{
int x = 45;
void foo () { assert (x == 45); }
void bar () { int z = 16; foo (); }
bar ();
}
void BadMultipleNested ()
{
int x;
void foo ()
{
void bar ()
{
//erroneous x = 4; // Should fail.
}
}
}
/* This one kind of depends upon memory layout. GlobalScopeSpoof should
be called with no "this" pointer; this is trying to ensure that
everything is working properly. Of course, in the DMD calling
convention it'll fail if the caller passes too much/little data. */
void GlobalScopeSpoof (int x, int y)
{
assert (x == y && y == 487);
}
void GlobalScope ()
{
void bar () { GlobalScopeSpoof (487, 487); }
bar ();
}
class TestClass
{
int x = 6400;
void foo ()
{
void bar () { assert (x == 6400); }
bar ();
}
}
void test12()
{
SimpleNestedFunction ();
AccessParentScope ();
CrossNestedScope ();
GlobalScope ();
(new TestClass).foo ();
}
/*******************************************/
void test13()
{
struct Abc
{
int x = 3;
int y = 4;
}
Abc a;
assert(a.x == 3 && a.y == 4);
}
/*******************************************/
void test14()
{
struct Abc
{
int x = 3;
int y = 4;
int foo() { return y; }
}
Abc a;
assert(a.foo() == 4);
}
/*******************************************/
void test15()
{
static int z = 5;
struct Abc
{
int x = 3;
int y = 4;
int foo() { return y + z; }
}
Abc a;
assert(a.foo() == 9);
}
/*******************************************/
void test16()
{
static int z = 5;
static class Abc
{
int x = 3;
int y = 4;
int foo() { return y + z; }
}
Abc a = new Abc();
assert(a.foo() == 9);
}
/*******************************************/
void test17()
{
int function(int x) fp;
fp = function int(int y) { return y + 3; };
assert(fp(7) == 10);
}
/*******************************************/
void test18()
{
static int a = 3;
int function(int x) fp;
fp = function int(int y) { return y + a; };
assert(fp(7) == 10);
}
/*******************************************/
void test19()
{
int a = 3;
int delegate(int x) fp;
fp = delegate int(int y) { return y + a; };
assert(fp(7) == 10);
}
/*******************************************/
class Collection20
{
int[3] array;
void opApply(void delegate(int) fp)
{
for (int i = 0; i < array.length; i++)
fp(array[i]);
}
}
int func20(Collection20 c)
{
int max = int.min;
c.opApply(delegate(int i) { if (i > max) max = i; });
return max;
}
void test20()
{
Collection20 c = new Collection20();
c.array[0] = 7;
c.array[1] = 26;
c.array[2] = 25;
int m = func20(c);
assert(m == 26);
}
/*******************************************/
int bar21(int a)
{
int c = 3;
int foo(int b)
{
b += c; // 4 is added to b
c++; // bar.c is now 5
return b + c; // 12 is returned
}
c = 4;
int i = foo(a); // i is set to 12
return i + c; // returns 17
}
void test21()
{
int i = bar21(3); // i is assigned 17
assert(i == 17);
}
/*******************************************/
void foo22(void delegate() baz)
{
baz();
}
void bar22(int i)
{
int j = 14;
printf("%d,%d\n",i,j);
void fred()
{
printf("%d,%d\n",i,j);
assert(i == 12 && j == 14);
}
fred();
foo22(&fred);
}
void test22()
{
bar22(12);
}
/*******************************************/
void frelled(void delegate() baz)
{
baz();
}
class Foo23
{
void bar(int i)
{
int j = 14;
printf("%d,%d\n",i,j);
void fred()
{
printf("%d,%d\n",i,j);
assert(i == 12);
assert(j == 14);
}
frelled(&fred);
}
}
void test23()
{
Foo23 f = new Foo23();
f.bar(12);
}
/*******************************************/
void delegate () function (int x) store24;
void delegate () zoom24(int x)
{
return delegate void () { };
}
void test24()
{
store24 = &zoom24;
store24 (1) ();
}
/*******************************************/
void test25()
{
delegate() { printf("stop the insanity!\n"); }();
delegate() { printf("stop the insanity! 2\n"); }();
}
/*******************************************/
alias bool delegate(int) callback26;
bool foo26(callback26 a)
{
return a(12);
}
class Bar26
{
int func(int v)
{
printf("func(v=%d)\n", v);
foo26(delegate bool(int a)
{
printf("%d %d\n",a,v); return true;
assert(a == 12);
assert(v == 15);
assert(0);
} );
return v;
}
}
void test26()
{
Bar26 b = new Bar26();
b.func(15);
}
/*******************************************/
class A27
{
uint myFunc()
{
uint myInt = 13;
uint mySubFunc()
{
return myInt;
}
return mySubFunc();
}
}
void test27()
{
A27 myInstance = new A27;
int i = myInstance.myFunc();
printf("%d\n", i);
assert(i == 13);
}
/*******************************************/
void Foo28(void delegate() call)
{
call();
}
class Bar28
{
int func()
{
int count = 0;
Foo28(delegate void() { ++count; } );
return count;
}
}
void test28()
{
Bar28 b = new Bar28();
int i = b.func();
assert(i == 1);
}
/*******************************************/
class Foo29
{
void Func(void delegate() call)
{
for(int i = 0; i < 10; ++i)
call();
}
}
class Bar29
{
int Func()
{
int count = 0;
Foo29 ic = new Foo29();
ic.Func(delegate void() { ++count; } );
return count;
}
}
void test29()
{
Bar29 b = new Bar29();
int i = b.Func();
assert(i == 10);
}
/*******************************************/
struct Foo30
{
int[] arr;
}
void Func30(Foo30 bar)
{
void InnerFunc(int x, int y)
{
int a = bar.arr[y]; // Ok
if(bar.arr[y]) // Access violation
{
}
}
InnerFunc(5,5);
}
void test30()
{
Foo30 abc;
abc.arr.length = 10;
Func30(abc);
}
/*******************************************/
void call31(int d, void delegate(int d) f)
{
assert(d == 100 || d == 200);
printf("d = %d\n", d);
f(d);
}
void test31()
{
call31(100, delegate void(int d1)
{
printf("d1 = %d\n", d1);
assert(d1 == 100);
call31(200, delegate void(int d2)
{
printf("d1 = %d\n", d1);
printf("d2 = %d\n", d2);
assert(d1 == 100);
assert(d2 == 200);
});
});
}
/*******************************************/
void call32(int d, void delegate(int d) f)
{
assert(d == 100 || d == 200);
printf("d = %d\n", d);
f(d);
}
void test32()
{
call32(100, delegate void(int d1)
{
int a = 3;
int b = 4;
printf("d1 = %d, a = %d, b = %d\n", d1, a, b);
assert(a == 3);
assert(b == 4);
assert(d1 == 100);
call32(200, delegate void(int d2)
{
printf("d1 = %d, a = %d\n", d1, a);
printf("d2 = %d, b = %d\n", d2, b);
assert(a == 3);
assert(b == 4);
assert(d1 == 100);
assert(d2 == 200);
});
});
}
/*******************************************/
void test33()
{
extern (C) int Foo1(int a, int b, int c)
{
assert(a == 1);
assert(b == 2);
assert(c == 3);
return 1;
}
extern (D) int Foo2(int a, int b, int c)
{
assert(a == 1);
assert(b == 2);
assert(c == 3);
return 2;
}
extern (Windows) int Foo3(int a, int b, int c)
{
assert(a == 1);
assert(b == 2);
assert(c == 3);
return 3;
}
extern (Pascal) int Foo4(int a, int b, int c)
{
assert(a == 1);
assert(b == 2);
assert(c == 3);
return 4;
}
assert(Foo1(1, 2, 3) == 1);
assert(Foo2(1, 2, 3) == 2);
assert(Foo3(1, 2, 3) == 3);
assert(Foo4(1, 2, 3) == 4);
printf("test33 success\n");
}
/*******************************************/
class Foo34
{
int x;
class Bar
{
int y;
int delegate() getDelegate()
{
assert(y == 8);
auto i = sayHello();
assert(i == 23);
return &sayHello;
}
}
Bar bar;
int sayHello()
{
printf("Hello\n");
assert(x == 47);
return 23;
}
this()
{
x = 47;
bar = new Bar();
bar.y = 8;
}
}
void test34()
{
Foo34 foo = new Foo34();
int delegate() saydg = foo.bar.getDelegate();
printf("This should print Hello:\n");
auto i = saydg();
assert(i == 23);
}
/*******************************************/
class Foo35
{
int x = 42;
void bar()
{
int y = 43;
new class Object
{
this()
{
//writefln("x = %s", x);
//writefln("y = %s", y);
assert(x == 42);
assert(y == 43);
}
};
}
}
void test35()
{
Foo35 f = new Foo35();
f.bar();
}
/*******************************************/
class Foo36
{
int x = 42;
this()
{
int y = 43;
new class Object
{
this()
{
//writefln("x = %s", x);
//writefln("y = %s", y);
assert(x == 42);
assert(y == 43);
}
};
}
}
void test36()
{
Foo36 f = new Foo36();
}
/*******************************************/
class Foo37
{
int x = 42;
void bar()
{
int y = 43;
void abc()
{
new class Object
{
this()
{
//writefln("x = %s", x);
//writefln("y = %s", y);
assert(x == 42);
assert(y == 43);
}
};
}
abc();
}
}
void test37()
{
Foo37 f = new Foo37();
f.bar();
}
/*******************************************/
void test38()
{
int status = 3;
int delegate() foo()
{
class C
{
int dg()
{
return ++status;
}
}
C c = new C();
return &c.dg;
}
int delegate() bar = foo();
if(status != 3)
{
assert(0);
}
if(bar() != 4)
{
assert(0);
}
if(status != 4)
{
assert(0);
}
}
/*******************************************/
void test39()
{
int status;
int delegate() foo()
{
return &(new class
{
int dg()
{
return ++status;
}
}
).dg;
}
int delegate() bar = foo();
if(status != 0)
{
assert(0);
}