Mercurial > projects > ddmd
annotate dmd/CatAssignExp.d @ 178:e3afd1303184
Many small bugs fixed
Made all classes derive from TObject to detect memory leaks (functionality is disabled for now)
Began work on overriding backend memory allocations (to avoid memory leaks)
author | korDen |
---|---|
date | Sun, 17 Oct 2010 07:42:00 +0400 |
parents | 60bb0fe4563e |
children | b0d41ff5e0df |
rev | line source |
---|---|
0 | 1 module dmd.CatAssignExp; |
2 | |
114 | 3 import dmd.common; |
0 | 4 import dmd.BinExp; |
5 import dmd.Loc; | |
6 import dmd.Expression; | |
7 import dmd.Scope; | |
8 import dmd.InterState; | |
9 import dmd.SliceExp; | |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
117
diff
changeset
|
10 import dmd.ErrorExp; |
0 | 11 import dmd.Identifier; |
12 import dmd.IRState; | |
13 import dmd.TOK; | |
14 import dmd.TY; | |
15 import dmd.Id; | |
16 import dmd.Type; | |
17 import dmd.backend.elem; | |
18 import dmd.backend.RTLSYM; | |
19 import dmd.backend.Util; | |
20 import dmd.backend.OPER; | |
21 import dmd.backend.TYM; | |
22 import dmd.backend.mTY; | |
23 | |
117 | 24 import dmd.expression.Cat; |
0 | 25 import dmd.expression.Util; |
26 | |
27 class CatAssignExp : BinExp | |
28 { | |
29 this(Loc loc, Expression e1, Expression e2) | |
30 { | |
178 | 31 register(); |
32 | |
0 | 33 super(loc, TOK.TOKcatass, CatAssignExp.sizeof, e1, e2); |
34 } | |
35 | |
72 | 36 override Expression semantic(Scope sc) |
0 | 37 { |
38 Expression e; | |
39 | |
40 BinExp.semantic(sc); | |
41 e2 = resolveProperties(sc, e2); | |
42 | |
43 e = op_overload(sc); | |
44 if (e) | |
45 return e; | |
46 | |
47 if (e1.op == TOKslice) | |
48 { | |
49 SliceExp se = cast(SliceExp)e1; | |
50 | |
51 if (se.e1.type.toBasetype().ty == Tsarray) | |
52 error("cannot append to static array %s", se.e1.type.toChars()); | |
53 } | |
54 | |
55 e1 = e1.modifiableLvalue(sc, e1); | |
56 | |
57 Type tb1 = e1.type.toBasetype(); | |
58 Type tb2 = e2.type.toBasetype(); | |
59 | |
60 e2.rvalue(); | |
61 | |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
117
diff
changeset
|
62 Type tb1next = tb1.nextOf(); |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
117
diff
changeset
|
63 |
0 | 64 if ((tb1.ty == Tarray) && |
65 (tb2.ty == Tarray || tb2.ty == Tsarray) && | |
66 (e2.implicitConvTo(e1.type) || | |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
117
diff
changeset
|
67 //version(DMDV2) { |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
117
diff
changeset
|
68 tb2.nextOf().implicitConvTo(tb1next) |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
117
diff
changeset
|
69 //} |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
117
diff
changeset
|
70 ) |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
117
diff
changeset
|
71 |
0 | 72 ) |
73 { // Append array | |
74 e2 = e2.castTo(sc, e1.type); | |
75 type = e1.type; | |
76 e = this; | |
77 } | |
78 else if ((tb1.ty == Tarray) && | |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
117
diff
changeset
|
79 e2.implicitConvTo(tb1next) |
0 | 80 ) |
81 { // Append element | |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
117
diff
changeset
|
82 e2 = e2.castTo(sc, tb1next); |
0 | 83 type = e1.type; |
84 e = this; | |
85 } | |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
117
diff
changeset
|
86 else if (tb1.ty == Tarray && |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
117
diff
changeset
|
87 (tb1next.ty == Tchar || tb1next.ty == Twchar) && |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
117
diff
changeset
|
88 e2.implicitConvTo(Type.tdchar) |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
117
diff
changeset
|
89 ) |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
117
diff
changeset
|
90 { // Append dchar to char[] or wchar[] |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
117
diff
changeset
|
91 e2 = e2.castTo(sc, Type.tdchar); |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
117
diff
changeset
|
92 type = e1.type; |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
117
diff
changeset
|
93 e = this; |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
117
diff
changeset
|
94 |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
117
diff
changeset
|
95 /* Do not allow appending wchar to char[] because if wchar happens |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
117
diff
changeset
|
96 * to be a surrogate pair, nothing good can result. |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
117
diff
changeset
|
97 */ |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
117
diff
changeset
|
98 } |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
117
diff
changeset
|
99 else |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
117
diff
changeset
|
100 { |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
117
diff
changeset
|
101 error("cannot append type %s to type %s", tb2.toChars(), tb1.toChars()); |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
117
diff
changeset
|
102 e = new ErrorExp(); |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
117
diff
changeset
|
103 } |
0 | 104 return e; |
105 } | |
106 | |
72 | 107 override Expression interpret(InterState istate) |
0 | 108 { |
117 | 109 return interpretAssignCommon(istate, &Cat); |
0 | 110 } |
111 | |
72 | 112 override Identifier opId() /* For operator overloading */ |
0 | 113 { |
114 return Id.catass; | |
115 } | |
116 | |
72 | 117 override elem* toElem(IRState* irs) |
0 | 118 { |
119 //printf("CatAssignExp.toElem('%s')\n", toChars()); | |
120 elem* e; | |
121 Type tb1 = e1.type.toBasetype(); | |
122 Type tb2 = e2.type.toBasetype(); | |
123 | |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
117
diff
changeset
|
124 if (tb1.ty == Tarray && tb2.ty == Tdchar && |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
117
diff
changeset
|
125 (tb1.nextOf().ty == Tchar || tb1.nextOf().ty == Twchar)) |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
117
diff
changeset
|
126 { // Append dchar to char[] or wchar[] |
0 | 127 |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
117
diff
changeset
|
128 auto e1 = this.e1.toElem(irs); |
0 | 129 e1 = el_una(OPaddr, TYnptr, e1); |
130 | |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
117
diff
changeset
|
131 auto e2 = this.e2.toElem(irs); |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
117
diff
changeset
|
132 |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
117
diff
changeset
|
133 auto ep = el_params(e2, e1, null); |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
117
diff
changeset
|
134 int rtl = (tb1.nextOf().ty == Tchar) |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
117
diff
changeset
|
135 ? RTLSYM_ARRAYAPPENDCD |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
117
diff
changeset
|
136 : RTLSYM_ARRAYAPPENDWD; |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
117
diff
changeset
|
137 e = el_bin(OPcall, TYdarray, el_var(rtlsym[rtl]), ep); |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
117
diff
changeset
|
138 el_setLoc(e,loc); |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
117
diff
changeset
|
139 } |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
117
diff
changeset
|
140 else if (tb1.ty == Tarray || tb2.ty == Tsarray) |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
117
diff
changeset
|
141 { |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
117
diff
changeset
|
142 auto e1 = this.e1.toElem(irs); |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
117
diff
changeset
|
143 e1 = el_una(OPaddr, TYnptr, e1); |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
117
diff
changeset
|
144 |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
117
diff
changeset
|
145 auto e2 = this.e2.toElem(irs); |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
117
diff
changeset
|
146 |
108
6da99741178e
e2ir.c changes, mainly accounts for static arrays being value types now
Trass3r
parents:
72
diff
changeset
|
147 if (tybasic(e2.Ety) == TYstruct || tybasic(e2.Ety) == TYarray) |
0 | 148 { |
149 e2 = el_una(OPstrpar, TYstruct, e2); | |
150 e2.Enumbytes = e2.E1.Enumbytes; | |
151 assert(e2.Enumbytes); | |
152 } | |
153 | |
154 Type tb1n = tb1.nextOf().toBasetype(); | |
155 if ((tb2.ty == Tarray || tb2.ty == Tsarray) && | |
156 tb1n.equals(tb2.nextOf().toBasetype())) | |
157 { // Append array | |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
117
diff
changeset
|
158 auto ep = el_params(e2, e1, this.e1.type.getTypeInfo(null).toElem(irs), null); |
0 | 159 e = el_bin(OPcall, TYdarray, el_var(rtlsym[RTLSYM_ARRAYAPPENDT]), ep); |
160 } | |
161 else | |
162 { // Append element | |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
117
diff
changeset
|
163 auto ep = el_params(e2, e1, this.e1.type.getTypeInfo(null).toElem(irs), null); |
0 | 164 e = el_bin(OPcall, TYdarray, el_var(rtlsym[RTLSYM_ARRAYAPPENDCT]), ep); |
165 } | |
166 el_setLoc(e,loc); | |
167 } | |
168 else | |
169 assert(0); | |
170 | |
171 return e; | |
172 } | |
72 | 173 } |