72
|
1 module dmd.EqualExp;
|
|
2
|
79
|
3 import dmd.ErrorExp;
|
72
|
4 import dmd.Expression;
|
0
|
5 import dmd.Id;
|
72
|
6 import dmd.Identifier;
|
|
7 import dmd.InterState;
|
|
8 import dmd.Loc;
|
|
9 import dmd.Scope;
|
|
10 import dmd.IRState;
|
|
11 import dmd.BinExp;
|
0
|
12 import dmd.TOK;
|
|
13 import dmd.Type;
|
|
14 import dmd.AddrExp;
|
|
15 import dmd.VarExp;
|
|
16 import dmd.IntegerExp;
|
|
17 import dmd.TY;
|
|
18 import dmd.Token;
|
|
19 import dmd.NotExp;
|
|
20 import dmd.WANT;
|
72
|
21 import dmd.GlobalExpressions;
|
|
22
|
0
|
23 import dmd.backend.elem;
|
|
24 import dmd.backend.OPER;
|
|
25 import dmd.backend.Util;
|
|
26 import dmd.backend.TYM;
|
|
27 import dmd.backend.RTLSYM;
|
|
28
|
|
29 import dmd.codegen.Util;
|
|
30
|
|
31 import dmd.expression.util.arrayTypeCompatible;
|
|
32 import dmd.expression.Util;
|
72
|
33 import dmd.expression.Equal;
|
|
34
|
0
|
35 class EqualExp : BinExp
|
|
36 {
|
|
37 this(TOK op, Loc loc, Expression e1, Expression e2)
|
|
38 {
|
|
39 super(loc, op, EqualExp.sizeof, e1, e2);
|
|
40 assert(op == TOK.TOKequal || op == TOK.TOKnotequal);
|
|
41 }
|
|
42
|
72
|
43 override Expression semantic(Scope sc)
|
0
|
44 {
|
|
45 Expression e;
|
|
46
|
|
47 //printf("EqualExp.semantic('%s')\n", toChars());
|
|
48 if (type)
|
|
49 return this;
|
|
50
|
|
51 BinExp.semanticp(sc);
|
|
52
|
|
53 /* Before checking for operator overloading, check to see if we're
|
|
54 * comparing the addresses of two statics. If so, we can just see
|
|
55 * if they are the same symbol.
|
|
56 */
|
|
57 if (e1.op == TOK.TOKaddress && e2.op == TOK.TOKaddress)
|
|
58 {
|
|
59 AddrExp ae1 = cast(AddrExp)e1;
|
|
60 AddrExp ae2 = cast(AddrExp)e2;
|
|
61
|
|
62 if (ae1.e1.op == TOK.TOKvar && ae2.e1.op == TOK.TOKvar)
|
|
63 {
|
|
64 VarExp ve1 = cast(VarExp)ae1.e1;
|
|
65 VarExp ve2 = cast(VarExp)ae2.e1;
|
|
66
|
|
67 if (ve1.var == ve2.var /*|| ve1.var.toSymbol() == ve2.var.toSymbol()*/)
|
|
68 {
|
|
69 // They are the same, result is 'true' for ==, 'false' for !=
|
|
70 e = new IntegerExp(loc, (op == TOK.TOKequal), Type.tboolean);
|
|
71 return e;
|
|
72 }
|
|
73 }
|
|
74 }
|
|
75
|
79
|
76 Type t1 = e1.type.toBasetype();
|
|
77 Type t2 = e2.type.toBasetype();
|
|
78 if (t1.ty == TY.Tclass && e2.op == TOK.TOKnull || t2.ty == TY.Tclass && e1.op == TOK.TOKnull)
|
0
|
79 {
|
|
80 error("use '%s' instead of '%s' when comparing with null",
|
|
81 Token.toChars(op == TOK.TOKequal ? TOK.TOKidentity : TOK.TOKnotidentity),
|
|
82 Token.toChars(op));
|
|
83 }
|
|
84
|
|
85 //if (e2.op != TOKnull)
|
|
86 {
|
|
87 e = op_overload(sc);
|
|
88 if (e)
|
|
89 {
|
|
90 if (op == TOK.TOKnotequal)
|
|
91 {
|
|
92 e = new NotExp(e.loc, e);
|
|
93 e = e.semantic(sc);
|
|
94 }
|
|
95
|
|
96 return e;
|
|
97 }
|
|
98 }
|
|
99
|
79
|
100 // Disallow comparing T[]==T and T==T[]
|
|
101 if (e1.op == TOKslice && t1.ty == Tarray && e2.implicitConvTo(t1.nextOf()) ||
|
|
102 e2.op == TOKslice && t2.ty == Tarray && e1.implicitConvTo(t2.nextOf()))
|
|
103 {
|
|
104 incompatibleTypes();
|
|
105 return new ErrorExp();
|
|
106 }
|
|
107
|
0
|
108 e = typeCombine(sc);
|
|
109 type = Type.tboolean;
|
|
110
|
|
111 // Special handling for array comparisons
|
|
112 if (!arrayTypeCompatible(loc, e1.type, e2.type))
|
|
113 {
|
|
114 if (e1.type != e2.type && e1.type.isfloating() && e2.type.isfloating())
|
|
115 {
|
|
116 // Cast both to complex
|
|
117 e1 = e1.castTo(sc, Type.tcomplex80);
|
|
118 e2 = e2.castTo(sc, Type.tcomplex80);
|
|
119 }
|
|
120 }
|
|
121
|
|
122 return e;
|
|
123 }
|
|
124
|
72
|
125 override Expression optimize(int result)
|
0
|
126 {
|
|
127 Expression e;
|
|
128
|
|
129 //printf("EqualExp::optimize(result = %x) %s\n", result, toChars());
|
|
130 e1 = e1.optimize(WANTvalue | (result & WANTinterpret));
|
|
131 e2 = e2.optimize(WANTvalue | (result & WANTinterpret));
|
|
132 e = this;
|
|
133
|
|
134 Expression e1 = fromConstInitializer(result, this.e1);
|
|
135 Expression e2 = fromConstInitializer(result, this.e2);
|
|
136
|
|
137 e = Equal(op, type, e1, e2);
|
|
138 if (e is EXP_CANT_INTERPRET)
|
|
139 e = this;
|
|
140 return e;
|
|
141 }
|
|
142
|
72
|
143 override Expression interpret(InterState istate)
|
0
|
144 {
|
|
145 assert(false);
|
|
146 }
|
|
147
|
72
|
148 override int isBit()
|
0
|
149 {
|
|
150 assert(false);
|
|
151 }
|
|
152
|
72
|
153 override bool isCommutative()
|
0
|
154 {
|
|
155 return true;
|
|
156 }
|
|
157
|
72
|
158 override Identifier opId()
|
0
|
159 {
|
|
160 return Id.eq;
|
|
161 }
|
|
162
|
72
|
163 override elem* toElem(IRState* irs)
|
0
|
164 {
|
|
165 //printf("EqualExp::toElem() %s\n", toChars());
|
|
166 elem* e;
|
|
167 OPER eop;
|
|
168 Type t1 = e1.type.toBasetype();
|
|
169 Type t2 = e2.type.toBasetype();
|
|
170
|
|
171 switch (op)
|
|
172 {
|
|
173 case TOKequal: eop = OPeqeq; break;
|
|
174 case TOKnotequal: eop = OPne; break;
|
|
175 default:
|
|
176 dump(0);
|
|
177 assert(0);
|
|
178 }
|
|
179
|
|
180 //printf("EqualExp::toElem()\n");
|
|
181 if (t1.ty == Tstruct)
|
|
182 { // Do bit compare of struct's
|
|
183 elem* es1;
|
|
184 elem* es2;
|
|
185 elem* ecount;
|
|
186
|
|
187 es1 = e1.toElem(irs);
|
|
188 es2 = e2.toElem(irs);
|
|
189 static if (true) {
|
|
190 es1 = addressElem(es1, t1);
|
|
191 es2 = addressElem(es2, t2);
|
|
192 } else {
|
|
193 es1 = el_una(OPaddr, TYnptr, es1);
|
|
194 es2 = el_una(OPaddr, TYnptr, es2);
|
|
195 }
|
|
196 e = el_param(es1, es2);
|
|
197 ecount = el_long(TYint, t1.size());
|
|
198 e = el_bin(OPmemcmp, TYint, e, ecount);
|
|
199 e = el_bin(eop, TYint, e, el_long(TYint, 0));
|
|
200 el_setLoc(e,loc);
|
|
201 }
|
|
202 /// static if (false) {
|
|
203 /// else if (t1.ty == Tclass && t2.ty == Tclass)
|
|
204 /// {
|
|
205 /// elem *ec1;
|
|
206 /// elem *ec2;
|
|
207 ///
|
|
208 /// ec1 = e1.toElem(irs);
|
|
209 /// ec2 = e2.toElem(irs);
|
|
210 /// e = el_bin(OPcall,TYint,el_var(rtlsym[RTLSYM_OBJ_EQ]),el_param(ec1, ec2));
|
|
211 /// }
|
|
212 /// }
|
|
213 else if ((t1.ty == Tarray || t1.ty == Tsarray) &&
|
|
214 (t2.ty == Tarray || t2.ty == Tsarray))
|
|
215 {
|
|
216 elem* ea1;
|
|
217 elem* ea2;
|
|
218 elem* ep;
|
|
219 Type telement = t1.nextOf().toBasetype();
|
|
220 int rtlfunc;
|
|
221
|
|
222 ea1 = e1.toElem(irs);
|
|
223 ea1 = array_toDarray(t1, ea1);
|
|
224 ea2 = e2.toElem(irs);
|
|
225 ea2 = array_toDarray(t2, ea2);
|
|
226
|
|
227 version (DMDV2) {
|
|
228 ep = el_params(telement.arrayOf().getInternalTypeInfo(null).toElem(irs),
|
|
229 ea2, ea1, null);
|
|
230 rtlfunc = RTLSYM_ARRAYEQ2;
|
|
231 } else {
|
|
232 ep = el_params(telement.getInternalTypeInfo(null).toElem(irs), ea2, ea1, null);
|
|
233 rtlfunc = RTLSYM_ARRAYEQ;
|
|
234 }
|
|
235 e = el_bin(OPcall, TYint, el_var(rtlsym[rtlfunc]), ep);
|
|
236 if (op == TOKnotequal)
|
|
237 e = el_bin(OPxor, TYint, e, el_long(TYint, 1));
|
|
238 el_setLoc(e,loc);
|
|
239 }
|
|
240 else
|
|
241 e = toElemBin(irs, eop);
|
|
242
|
|
243 return e;
|
|
244 }
|
|
245 }
|
|
246
|