-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathvariant.d
843 lines (780 loc) · 26.5 KB
/
variant.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
// Written in the D programming language
// Author: Timon Gehr
// License: http://www.boost.org/LICENSE_1_0.txt, Boost License 1.0
import lexer, operators, expression, declaration, type, semantic, util;
import std.algorithm: map;
import std.traits: isIntegral, isFloatingPoint, Unqual;
import std.range: ElementType;
import std.conv: to, text;
//import std.string : text;
import std.stdio;
// TODO: refactor the whole code base into smaller modules
struct BCSlice{
void[] container;
void[] slice;
this(void[] va){
container = slice = va;
}
this(void[] cnt, void[] slc){
container = cnt;
slice = slc;
}
BCPointer getPtr(){
return BCPointer(container, slice.ptr);
}
size_t getLength(){
return slice.length;
}
void setLength(size_t len){
slice.length = len;
if(slice.ptr<container.ptr||slice.ptr>=container.ptr+container.length)
container = slice;
}
}
struct BCPointer{
void[] container;
void* ptr;
}
enum Occupies{
none, str, wstr, dstr, int64, cent_, ucent_, flt80, fli80, cmp80, arr, ptr_, vars, fptr, dg, tpl, void_, err
}
import cent_;
template getOccupied(T){
static if(is(T==string))
enum getOccupied = Occupies.str;
else static if(is(T==wstring))
enum getOccupied = Occupies.wstr;
else static if(is(T==dstring)){
enum getOccupied = Occupies.dstr;
}else static if(is(T:long)&&!is(T==struct)&&!is(T==class))
enum getOccupied = Occupies.int64;
else static if(is(T==Cent))
enum getOccupied = Occupies.cent_;
else static if(is(T==UCent))
enum getOccupied = Occupies.ucent_;
else static if(isFloatingPoint!T){
static assert(T.sizeof<=typeof(Variant.flt80).sizeof);
enum getOccupied = Occupies.flt80;
}else static if(is(T==ifloat)||is(T==idouble)||is(T==ireal))
enum getOccupied = Occupies.fli80;
else static if(is(T==cfloat)||is(T==cdouble)||is(T==creal))
enum getOccupied = Occupies.cmp80;
else static if(is(T==Variant[]))
enum getOccupied = Occupies.arr;
else static if(is(T==Variant*))
enum getOccupied = Occupies.ptr_;
else static if(is(T==typeof(null)))
enum getOccupied = Occupies.none;
else static if(is(T==Variant[VarDecl]))
enum getOccupied = Occupies.vars;
else static if(is(T==void))
enum getOccupied = Occupies.void_;
else static assert(0);
}
private bool occString(Occupies occ){
static assert(Occupies.str+1 == Occupies.wstr && Occupies.wstr+1 == Occupies.dstr);
return occ >= Occupies.str && occ <= Occupies.dstr;
}
/+
template FullyUnqual(T){
static if(is(T _:U[], U)) alias FullyUnqual!U[] FullyUnqual;
else static if(is(T _:U*, U)) alias FullyUnqual!U* FullyUnqual;
else alias Unqual!T FullyUnqual;
}+/
/+
private struct WithLoc(T){
T payload;
// TODO: this can be represented in a smarter way
Location[] locs;
this(T pl, Location[] lcs){payload = pl; locs=lcs;}
this(T pl, Location loc){payload = pl; locs=[loc];}
WithLoc opBinary(op: "~")(WithLoc rhs){
return WithLoc(payload~rhs.payload, locs~rhs.locs);
}
WithLoc opIndex(
}
+/
struct FieldAccess{
@property isIndex(){ return _isindex; }
@property index()in{assert(isIndex);}body{ return _index; }
@property field()in{assert(!isIndex);}body{ return _field; }
this(size_t index){
this._index = index;
_isindex = true;
}
this(VarDecl field)in{assert(field.isField);}body{
this._field = field;
_isindex = false;
}
string toString(){
if(isIndex) return "["~index.to!string~"]";
return "."~field.name.to!string;
}
private:
union{
size_t _index;
VarDecl _field;
}
bool _isindex;
}
struct Variant{
private Type type;
private @property Occupies occupies(){
// TODO: this is probably too slow
// TODO: cache inside type?
Occupies occupies(Type type){
if(!type) return Occupies.vars;
if(type.isTypeTuple()) return Occupies.tpl;
auto tu=type.getHeadUnqual();
if(tu.isAggregateTy()) return Occupies.vars;
if(tu is Type.get!string()) return Occupies.str;
if(tu is Type.get!wstring()) return Occupies.wstr;
if(tu is Type.get!dstring()) return Occupies.dstr;
if(tu.getElementType()) return Occupies.arr;
if(auto ptr=tu.isPointerTy())
return ptr.ty.isFunctionTy()?Occupies.fptr:Occupies.ptr_;
if(tu.isDelegateTy()) return Occupies.dg;
if(auto bt=tu.isBasicType()){
switch(bt.op){
foreach(x;ToTuple!basicTypes){
case Tok!x:
mixin("alias BasicTypeRep!`"~x~"` T;"); // workaround DMD bug
return getOccupied!T;
}
default: assert(0,text(bt.op));
}
}else if(auto et=tu.isEnumTy()){
assert(et.decl.base);
return occupies(et.decl.base);
}
assert(tu is Type.get!(typeof(null)), tu.text);
return Occupies.none;
}
return occupies(type);
}
this(T)(T value, Type type)if(!is(T==Variant[])&&!is(T==Variant[VarDecl])&&!is(T==Symbol))in{
assert(type.sstate==SemState.completed);
// TODO: suitable in contract for type based on T
}body{
//type = Type.get!T();
this.type = type;
mixin(to!string(getOccupied!T)~` = value;`);
static if(is(T==typeof(null))) assert(occupies == Occupies.void_ || occupies == Occupies.none);
else assert(occupies == getOccupied!T);
}
this()(Variant[] tpl, TypeTuple type){
this.type=type;
this.tpl=tpl;
assert(occupies == Occupies.tpl);
}
this()(Variant[] arr, Variant[] cnt, Type type)in{ // TODO: extend FieldAccess and merge with ptr constructor
assert(cnt.ptr<=arr.ptr && arr.ptr+arr.length<=cnt.ptr+cnt.length); // TODO: relax?
assert(type.getElementType(),text(type));
auto tt=type.getElementType().getUnqual(); // TODO: more restrictive assertion desirable
if(tt !is Type.get!(void*)()&&tt !is Type.get!(void[])()&&tt!is Type.get!void())
foreach(x;cnt) assert(tt.equals(x.type.getUnqual()),text(cnt," ",tt," ",x.type," ",x));
}body{
this.type=type;
switch(occupies){
case Occupies.arr:
this.arr=arr;
this.cnt=cnt;
break;
// TODO: this is a temporary hack (the special-casing of strings should be transparent)
case Occupies.str: string r; foreach(v;arr) r~=v.get!char(); str=r; break;
case Occupies.wstr: wstring r; foreach(v;arr) r~=v.get!wchar(); wstr=r; break;
case Occupies.dstr: dstring r; foreach(v;arr) r~=v.get!dchar(); dstr=r; break;
default: assert(0);
}
}
this()(FieldAccess[] ptr, Variant[] cnt, Type type)in{
auto pt=type.getHeadUnqual().isPointerTy();
assert(!!pt);
auto tt=pt.ty.getUnqual();
if(tt !is Type.get!(void*)()&&tt!is Type.get!(void[])())
foreach(x;cnt) assert(tt is x.type.getUnqual());
}body{
this.type=type;
this.ptr_=ptr;
this.cnt=cnt;
assert(occupies==Occupies.ptr_);
}
this()(Variant[VarDecl] vars, Type type = null){ // templated because of DMD bug
this.type=type;
this.vars=vars;
assert(occupies == Occupies.vars);
}
this()(typeof(null)){ // templated because of DMD bug
this.type=Type.get!(typeof(null))();
assert(occupies == Occupies.none);
}
this()(typeof(null), Type type = null){ // templated because of DMD bug
this.type=type;
assert(occupies == Occupies.none || occupies == Occupies.void_);
}
this()(Symbol fptr, Type type)in{
auto tu=type.getHeadUnqual();
assert(tu.isPointerTy()&&tu.getFunctionTy());
if(fptr&&fptr.type){
auto ft=fptr.type.isFunctionTy();
assert(!!ft);
assert(tu.getFunctionTy().equals(ft));
}
}out{
assert(occupies == Occupies.fptr);
}body{
this.type=type;
this.fptr=fptr;
assert(occupies == Occupies.fptr);
}
this()(Symbol dgfptr, Variant[VarDecl] dgctx, Type type)in{
if(dgfptr&&dgfptr.type){
auto ft=dgfptr.type.isFunctionTy();
assert(!!ft);
}
auto tu=type.getHeadUnqual();
assert(tu.isDelegateTy());
}out{
assert(occupies == Occupies.dg);
}body{
this.type=type;
this.dgfptr=dgfptr;
this.dgctx=dgctx;
assert(occupies == Occupies.dg);
}
private union{
typeof(null) none;
string str; wstring wstr; dstring dstr;
ulong int64; Cent cent_; UCent ucent_;
real flt80; ireal fli80; creal cmp80;
struct{
union{
Variant[] arr;
FieldAccess[] ptr_;
}
Variant[] cnt;
Variant[VarDecl] ptrvars;
VarDecl ptrvd;
}
Variant[VarDecl] vars; // structs, classes, closures
Symbol fptr;
struct{
Symbol dgfptr;
Variant[VarDecl] dgctx;
}
Variant[] tpl;
string err;
}
T get(T)(){
static if(is(T==string)){
if(occupies == Occupies.str) return str;
else if(occupies == Occupies.wstr) return to!string(wstr);
else if(occupies == Occupies.dstr) return to!string(dstr);
else return toString();
}
else static if(is(T==wstring)){assert(occupies == Occupies.wstr); return wstr;}
else static if(is(T==dstring)){assert(occupies == Occupies.dstr); return dstr;}
else static if(getOccupied!T==Occupies.int64){assert(occupies == Occupies.int64,"occupies was "~to!string(occupies)~" instead of int64"); return cast(T)int64;}
else static if(is(T==Cent)){assert(occupies == Occupies.cent_); return cent_; }
else static if(is(T==UCent)){assert(occupies == Occupies.ucent_); return ucent_; }
else static if(is(T==float)||is(T==double)||is(T==real)){
assert(occupies == Occupies.flt80||occupies == Occupies.fli80);
return flt80;
}else static if(is(T==ifloat) || is(T==idouble)||is(T==ireal)){
assert(occupies == Occupies.fli80);
return fli80;
}else static if(is(T==cfloat) || is(T==cdouble)||is(T==creal)){
assert(occupies == Occupies.cmp80);
return cmp80;
}else static if(is(T==Variant[VarDecl])){
if(occupies == Occupies.none) return null;
assert(occupies == Occupies.vars,text(occupies));
return vars;
}else static if(is(T==Variant[])){
assert(occupies == Occupies.arr,text(occupies));
return arr;
}else static assert(0, "cannot get this field (yet?)");
}
Variant[] getContainer()in{
assert(occupies == Occupies.arr||occupies == Occupies.ptr_);
}body{
return cnt;
}
Symbol getFunctionPointer()in{
assert(occupies == Occupies.fptr);
}body{
return fptr;
}
Symbol getDelegateFunctionPointer()in{
assert(occupies == Occupies.dg);
}body{
return dgfptr;
}
Variant[VarDecl] getDelegateContext()in{
assert(occupies == Occupies.dg);
}body{
return dgctx;
}
Variant[] getTuple()in{
assert(occupies == Occupies.tpl);
}body{
return tpl;
}
/* returns a type that fully specifies the memory layout
for strings, the relevant type qualifiers are preserved
otherwise, gives no guarantees for type qualifier preservation
*/
Type getType()out{assert(!type||type.sstate==SemState.completed);}body{
return type;
}
Expression toExpr(){
if(type){
auto r = LiteralExp.factory(this);
r.semantic(null);
return r;
}else return New!ErrorExp();
}
string toString(){
if(!type){
assert(occupies==Occupies.vars);
return to!string(vars);
}
auto type=type.getHeadUnqual();
if(type is Type.get!(typeof(null))()) return "null";
if(type.isSomeString()){
foreach(T;Seq!(string,wstring,dstring)){
if(type !is Type.get!T()) continue;
enum occ = getOccupied!T;
enum sfx = is(T==string) ? "c" :
is(T==wstring) ? "w" :
is(T==dstring) ? "d" : "";
return to!string('"'~escape(mixin(`this.`~to!string(occ)))~'"'~sfx);
}
}
if(auto bt=type.isBasicType()){
switch(bt.op){
foreach(x;ToTuple!basicTypes){
static if(x=="void"){case Tok!"void": return "<void>";} // (this is what DMD does)
else case Tok!x:
{
mixin(`alias typeof(BasicTypeRep!"`~x~`".init) T;`); // dmd parser workaround
enum sfx = is(T==uint) ? "U" :
is(T==long)||is(T==real) ? "L" :
is(T==ulong) ? "LU" :
is(T==float) ? "f" :
is(T==ifloat) ? "fi" :
is(T==idouble) ? "i" :
is(T==ireal) ? "Li":"";
enum left = is(T==char)||is(T==wchar)||is(T==dchar) ? "'" : "";
enum right = left;
// remove redundant "i" from imaginary literals
enum oc1 = getOccupied!T;
enum occ = oc1==Occupies.fli80?Occupies.flt80:oc1;
string rlsfx = ""; // TODO: extract into its own function?
string res = to!string(mixin(`cast(T)this.`~to!string(occ)));
static if(occ==Occupies.flt80)
if(this.flt80%1==0&&!res.canFind("e")) rlsfx=".0";
return left~res~right~rlsfx~sfx;
}
}
default: assert(0);
}
}
if(type.getElementType()){
import std.algorithm, std.array;
return '['~join(map!(to!string)(this.arr),",")~']';
}
if(type.isPointerTy()){
if(type.getFunctionTy()){
assert(occupies==Occupies.fptr);
return fptr.loc.line?fptr.loc.rep:fptr.toString();
}
assert(occupies==Occupies.ptr_);
return "&("~cnt.to!string~ptr_.map!(to!string).join~")"; // TODO: fix
}
if(type.isDelegateTy()){
assert(occupies==Occupies.dg);
return dgfptr.loc.line?dgfptr.loc.rep:dgfptr.toString();
}
if(type.isAggregateTy()){
if(this.vars is null) return "null";
return this.type.toString(); // TODO: pick up toString?
}
if(auto et=type.getHeadUnqual().isEnumTy())
return et.valueToString(this);
assert(0,"cannot get string");
}
Variant convertTo(Type to)in{assert(!!type);}body{
if(to is type) return this;
auto type = getType().getHeadUnqual();
auto tou = to.getHeadUnqual();
if(type is Type.get!(typeof(null))()){
if(tou is Type.get!(typeof(null))()) return this;
if(tou.getElementType()) return Variant((Variant[]).init,(Variant[]).init,to);
if(tou.isAggregateTy()) return Variant((Variant[VarDecl]).init, to);
// TODO: null pointers and delegates
assert(0,"cannot convert");
}else if(type.isSomeString()){
foreach(T;Seq!(string,wstring,dstring)){
enum occ=getOccupied!T;
if(type !is Type.get!T()) continue;
if(tou is Type.get!string())
return Variant(.to!string(mixin(`this.`~.to!string(occ))),to);
else if(tou is Type.get!wstring())
return Variant(.to!wstring(mixin(`this.`~.to!string(occ))),to);
else if(tou is Type.get!dstring())
return Variant(.to!dstring(mixin(`this.`~.to!string(occ))),to);
else if(auto ety2=tou.getElementType()){
// TODO: revise allocation
auto r = new Variant[this.length];
foreach(i,x;mixin(`this.`~.to!string(occ))) r[i]=Variant(x,ety2);
return Variant(r,r,to);//TODO: aliasing?
}
return this; // TODO: this is a hack and might break stuff (?)
}
}else if(auto tbt=type.isBasicType()){
if(auto bt = tou.isBasicType()){
switch(tbt.op){
foreach(tx;ToTuple!basicTypes){
static if(tx!="void"){
case Tok!tx:// TODO: code generated for integral types is more or less identical
mixin(`alias typeof(BasicTypeRep!"`~tx~`".init) T;`); // dmd parser workaround
enum occ=getOccupied!T;
switch(bt.op){
foreach(x;ToTuple!basicTypes){
static if(x!="void")
case Tok!x:
{
static convert(To,From)(From from){
static if(is(To==Cent)||is(To==UCent)) return To(from);
else return cast(To)from; // TODO: case distinction shouldn't be necessary
}
return Variant(mixin(`convert!(BasicTypeRep!"`~x~`")(cast(T)this.`~.to!string(occ)~`)`),to);
}
}
case Tok!"void": return this;
default: assert(0);
}
}
}
default: assert(0);
}
}
}else if(type.isDynArrTy()){
// assert(to.getHeadUnqual().getElementType()!is null);
if(tou is Type.get!string()){
string s;
foreach(x; this.arr) s~=cast(char)x.int64;
return Variant(s,to);
}else if(tou is Type.get!wstring()){
wstring s;
foreach(x; this.arr) s~=cast(wchar)x.int64;
return Variant(s,to);
}else if(tou is Type.get!dstring()){
dstring s;
foreach(x; this.arr) s~=cast(wchar)x.int64;
return Variant(s,to);
}
// TODO: Sanity check?
if(tou.getUnqual() is Type.get!(void[])()) return this;
return Variant(arr,cnt,to);
}else if(type is Type.get!EmptyArray()){
assert(tou.isDynArrTy()||tou.isArrayTy()&&tou.isArrayTy().length==0);
if(tou.isSomeString()){
foreach(T;Seq!(string,wstring,dstring))
if(tou is Type.get!T()) return Variant(T.init/+,T.init+/,to);
}
return Variant((Variant[]).init,(Variant[]).init,to);
}else if(type.isPointerTy()){
if(tou is Type.get!bool()) return Variant(cnt !is null,to); // TODO: fix?
}
if(auto et=tou.isEnumTy()){
assert(!!et.decl.base);
auto rv=convertTo(et.decl.base);
rv.type=to;
return rv;
}
return this;
}
bool opCast(T)()if(is(T==bool)){
assert(type == Type.get!bool(), to!string(type)~" "~toString());
return cast(bool)int64;
}
private Variant strToArr()in{
assert(occString(occupies));
}body{ // TODO: get rid of this
Variant[] r = new Variant[length];
auto elt=type.getElementType().getUnqual(); // TODO: should this be const sometimes?
theswitch:switch(occupies){
foreach(occ;ToTuple!([Occupies.str, Occupies.wstr, Occupies.dstr])){
case occ:
foreach(i,x; mixin(to!string(occ))) r[i] = Variant(x,elt);
break theswitch;
}
default: assert(0);
}
return Variant(r,r,elt.getDynArr());
}
bool opEquals(Variant rhs){ return cast(bool)opBinary!"=="(rhs); }
size_t toHash_() @trusted{
// TODO: differing containers should result in the same template instantiations!
// -> strip containers?
final switch(occupies){
case Occupies.none: return 0;
foreach(x; EnumMembers!Occupies[1..$]){
static if(x!=Occupies.dg && x!=Occupies.void_)
case x: return typeid(mixin(to!string(x))).getHash(&mixin(to!string(x)));
}
case Occupies.dg: // TODO!
return 0;
case Occupies.void_:// better value?
return 0;
}
assert(0); // TODO: file bug
}
size_t toHash()const @trusted nothrow{
try return (cast(Variant*)&this).toHash_();
catch(Exception) assert(0);
}
// TODO: BUG: shift ops not entirely correct
Variant opBinary(string op)(Variant rhs)in{
static if(isShiftOp(Tok!op)){
assert(occupies == Occupies.int64 && rhs.occupies == Occupies.int64);
}else{
//assert(occupies == Occupies.arr || id.whichBasicType!=Tok!"" && id.whichBasicType == rhs.id.whichBasicType,
// to!string(id.whichBasicType)~"!="~to!string(rhs.id.whichBasicType));
assert(occupies == rhs.occupies
|| occupies == Occupies.arr && occString(rhs.occupies)
|| rhs.occupies == Occupies.arr && occString(occupies)
|| op == "%" && occupies == Occupies.cmp80 &&
(rhs.occupies == Occupies.flt80 || rhs.occupies == Occupies.fli80),
to!string(this)~" is incompatible with "~
to!string(rhs)~" in binary '"~op~"' expression");
}
}body{
static Variant compareMultiple(bool areArrays)(Variant[] al, Variant[] ar){
// TODO: create these as templates instead
auto l1 = al.length, l2=ar.length;
static if(op=="=="){if(l1!=l2) return Variant(false,Type.get!bool());}
else static if(op=="!=") if(l1!=l2) return Variant(true,Type.get!bool());
if(l1&&l2){
auto tyd = al[0].type.combine(ar[0].type);
assert(!tyd.dependee);// should still be ok though.
Type ty = tyd.value;
foreach(i,v; al[0..l1<l2?l1:l2]){
static if(areArrays) auto l = v.convertTo(ty), r = ar[i].convertTo(ty);
else{ alias v l; auto r=ar[i]; }
if(l.opBinary!"=="(r)) continue;
else{
static if(op=="==") return Variant(false,Type.get!bool());
else static if(op=="!=") return Variant(true,Type.get!bool());
else return l.opBinary!op(r);
}
}
}
enum op2 = TokChars!(toIntegerRelationalOp(Tok!op));
// for ==, != we know that the lengths must be equal
static if(op2=="=="||op2=="<>=") return Variant(true,Type.get!bool());
else static if(op2=="!="||op2=="!<>=") return Variant(false,Type.get!bool());
else return Variant(mixin(`l1 `~op2~` l2`),Type.get!bool());
}
if(occupies == Occupies.arr){
if(rhs.occupies != Occupies.arr) rhs=rhs.strToArr();
static if(op=="~"){
auto r=arr~rhs.arr;
return Variant(r,r,type);
}static if(op=="is"||op=="!is"){
return Variant(mixin(`arr `~op~` rhs.arr`),Type.get!bool());
}static if(op=="in"||op=="!in"){
// TODO: implement this
assert(0,"TODO");
}else static if(isRelationalOp(Tok!op)){
return compareMultiple!true(arr,rhs.arr);
}
}else if(occupies == Occupies.ptr_){
assert(rhs.occupies==Occupies.ptr_);
// TODO: other relational operators
static if(op=="is"||op=="=="||op=="!is"||op=="!="){
return Variant((op=="!is"||op=="!=")^(cnt is rhs.cnt && ptr_ == rhs.ptr_),Type.get!bool());
}else assert(0);
}else if(occupies == Occupies.vars){
assert(rhs.occupies==Occupies.vars);
assert(!type || type.getHeadUnqual().isAggregateTy());
// (if no type, we are looking at a delegate context)
auto decl=type?type.getHeadUnqual().isAggregateTy().decl:null;
static if(op=="is"||op=="!is"||op=="=="||op=="!="){
enum neq=op=="!is"||op=="!=";
if(!decl || decl.isReferenceAggregateDecl())
return Variant(neq^(vars is rhs.vars),Type.get!bool());
else{
foreach(k,v;vars)
if(k !in rhs.vars||!v.opBinary!"=="(rhs.vars[k]))
return Variant(neq, Type.get!bool());
foreach(k,v;rhs.vars)
if(k !in vars)
return Variant(neq, Type.get!bool());
return Variant(!neq, Type.get!bool());
}
}else assert(0);
}else if(occupies == Occupies.none){
static if(is(typeof(mixin(`null `~op~` null`))))
return Variant(mixin(`null `~op~` null`),
Type.get!(typeof(mixin(`null `~op~` null`)))());
else assert(0);
}
static if(isRelationalOp(Tok!op)&&op!="in"&&op!="!in"&&op!="is"&&op!="!is"){
if(occupies==Occupies.tpl){
assert(rhs.occupies==occupies.tpl);
return compareMultiple!false(tpl,rhs.tpl);
}
static if(op=="!>=") return (this.opBinary!">="(rhs)).opUnary!"!";
static if(op=="!<=") return (this.opBinary!"<="(rhs)).opUnary!"!";
static if(op=="<>") return this.opBinary!"<"(rhs)|this.opBinary!">"(rhs);
static if(op=="!<>") return (this.opBinary!"<"(rhs)|this.opBinary!">"(rhs)).opUnary!"!";
static if(op=="<>=") return this.opBinary!"<="(rhs)|this.opBinary!">="(rhs);
static if(op=="!<>=") return (this.opBinary!"<="(rhs)|this.opBinary!">="(rhs)).opUnary!"!";
}
if(type.getHeadUnqual().isSomeString()){
foreach(x; ToTuple!(["``c","``w","``d"])){
alias typeof(mixin(x)) T;
alias getOccupied!T occ;
if(occupies != occ) continue;
enum code = to!string(occ)~` `~op~` rhs.`~to!string(occ);
static if(op!="-" && op!="+" && op!="<>=" && op!="!<>=") // DMD bug
static if(is(typeof(mixin(code)))){
if(type.getHeadUnqual() is Type.get!(typeof(mixin(x)))()){
if(rhs.occupies == occupies)
return Variant(mixin(code),Type.get!(typeof(mixin(code)))());
else return strToArr().opBinary!op(rhs);
}
}
}
}
static if(op=="=="){
if(occupies == Occupies.fptr){
assert(rhs.occupies == Occupies.fptr);
return Variant(fptr is rhs.fptr, Type.get!bool);
}else if(occupies == Occupies.dg){
assert(rhs.occupies == Occupies.dg);
return Variant(dgfptr is rhs.dgfptr &&
Variant(dgctx).opBinary!"=="(Variant(rhs.dgctx)),
Type.get!bool());
}
}
auto type=type;
if(auto et=type.getHeadUnqual().isEnumTy()){
assert(!!et.decl.base);
type=et.decl.base;
}
assert(cast(BasicType)type.getHeadUnqual(),text(type));
switch((cast(BasicType)cast(void*)type.getHeadUnqual()).op){
foreach(x; ToTuple!basicTypes){
static if(x!="void"){
alias typeof(mixin(`BasicTypeRep!"`~x~`".init`)) T;
alias getOccupied!T occ;
static if(occ == Occupies.cmp80 && op == "%")
// can do complex modulo real
// relies on same representation for flt and fli
enum occ2 = Occupies.flt80;
else enum occ2 = occ;
assert(occupies == occ);
//assert(rhs.occupies == occ2);
static if(isShiftOp(Tok!op)|| occ2 != occ) enum cst = ``;
else enum cst = q{ cast(T) };
enum code = q{
mixin(`cast(T)` ~ to!string(occ) ~` `~ op ~ cst~`rhs.` ~ to!string(occ2))
};
static if(is(typeof(mixin(code))))
case Tok!x: return Variant(mixin(code),Type.get!(typeof(mixin(code)))());
}
}
default: break;
}
assert(0, "no binary '"~op~"' support for "~type.toString());
}
Variant opUnary(string op)(){
auto type=type;
if(auto et=type.getHeadUnqual().isEnumTy()){
assert(!!et.decl.base);
type=et.decl.base;
}
assert(cast(BasicType)type.getHeadUnqual());
switch((cast(BasicType)cast(void*)type.getHeadUnqual()).op){
foreach(x; ToTuple!basicTypes){
static if(x!="void"){
alias typeof(mixin(`BasicTypeRep!"`~x~`".init`)) T;
alias getOccupied!T occ;
enum code = q{ mixin(op~`cast(T)`~to!string(occ)) };
static if(is(typeof(mixin(code))==T)){
case Tok!x: return Variant(mixin(code), Type.get!T());
}else static if(is(typeof(mixin(code))==bool)){
case Tok!x: return Variant(mixin(code), Type.get!bool());
}
}
}
default: assert(0, "no unary '"~op~"' support for "~type.toString());
}
}
@property Variant ptr()in{
assert(occupies==Occupies.arr); // TODO: pointers to string
}body{
return Variant([FieldAccess(arr.ptr-cnt.ptr)], cnt, type.getElementType().getPointer());
}
@property size_t length()in{
assert(occupies==Occupies.arr||occupies == Occupies.str
|| occupies == Occupies.wstr || occupies == Occupies.dstr);
}body{
if(occupies == Occupies.arr) return arr.length;
else switch(occupies){
foreach(x; ToTuple!(["str","wstr","dstr"])){
case mixin(`Occupies.`~x): return mixin(x).length;
}
default: assert(0);
}
}
FieldAccess[] getFieldAccess()in{
assert(occupies==Occupies.ptr_);
}body{
return ptr_;
}
Variant opIndex(Variant index)in{
assert(index.occupies==Occupies.int64);
assert(occupies == Occupies.arr||occupies == Occupies.str
|| occupies == Occupies.wstr || occupies == Occupies.dstr);
}body{
if(occupies == Occupies.arr){
assert(index.int64<arr.length, to!string(index.int64)~">="~to!string(arr.length));
return arr[cast(size_t)index.int64];
}else switch(occupies){
foreach(x; ToTuple!(["str","wstr","dstr"])){
case mixin(`Occupies.`~x):
assert(index.int64<mixin(x).length);
return Variant(mixin(x)[cast(size_t)index.int64], Type.get!(ElementType!(typeof(mixin(x)))));
}
default: assert(0);
}
}
Variant opIndex(size_t index){
return this[Variant(index,Type.get!Size_t())];
}
Variant opSlice(Variant l, Variant r)in{
assert(l.occupies==Occupies.int64&&r.occupies==Occupies.int64);
assert(occupies == Occupies.arr||occupies == Occupies.str
|| occupies == Occupies.wstr || occupies == Occupies.dstr);
}body{
if(occupies == Occupies.arr){
assert(l.int64<=r.int64 && r.int64<=arr.length);
return Variant(arr[cast(size_t)l.int64..cast(size_t)r.int64],cnt,type.getElementType().getDynArr()); // aliasing ok?
}else switch(occupies){
foreach(x; ToTuple!(["str","wstr","dstr"])){
case mixin(`Occupies.`~x):
assert(l.int64<=r.int64 && r.int64<=mixin(x).length);
return Variant(mixin(x)[cast(size_t)l.int64..cast(size_t)r.int64],Type.get!(typeof(mixin(x))));
}
default: assert(0);
}
}
Variant opSlice(size_t l, size_t r){
return this[Variant(l,Type.get!Size_t())..Variant(r,Type.get!Size_t())];
}
}