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