annotate dmd/expression/Equal.d @ 0:10317f0c89a5

Initial commit
author korDen
date Sat, 24 Oct 2009 08:42:06 +0400
parents
children cab4c37afb89
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1 module dmd.expression.Equal;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
2
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
3 import dmd.Expression;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
4 import dmd.Type;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
5 import dmd.TOK;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
6 import dmd.Loc;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
7 import dmd.StringExp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
8 import dmd.GlobalExpressions;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
9 import dmd.ArrayLiteralExp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
10 import dmd.StructLiteralExp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
11 import dmd.Global;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
12 import dmd.IntegerExp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
13
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
14 import core.stdc.string;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
15
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
16 /* Also returns EXP_CANT_INTERPRET if cannot be computed.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
17 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
18 Expression Equal(TOK op, Type type, Expression e1, Expression e2)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
19 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
20 Expression e;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
21 Loc loc = e1.loc;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
22 bool cmp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
23 real r1;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
24 real r2;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
25
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
26 //printf("Equal(e1 = %s, e2 = %s)\n", e1.toChars(), e2.toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
27
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
28 assert(op == TOK.TOKequal || op == TOK.TOKnotequal);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
29
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
30 if (e1.op == TOK.TOKnull)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
31 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
32 if (e2.op == TOK.TOKnull)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
33 cmp = true;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
34 else if (e2.op == TOK.TOKstring)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
35 { StringExp es2 = cast(StringExp)e2;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
36 cmp = (0 == es2.len);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
37 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
38 else if (e2.op == TOK.TOKarrayliteral)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
39 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
40 ArrayLiteralExp es2 = cast(ArrayLiteralExp)e2;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
41 cmp = !es2.elements || (0 == es2.elements.dim);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
42 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
43 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
44 return EXP_CANT_INTERPRET;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
45 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
46 else if (e2.op == TOK.TOKnull)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
47 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
48 if (e1.op == TOK.TOKstring)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
49 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
50 StringExp es1 = cast(StringExp)e1;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
51 cmp = (0 == es1.len);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
52 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
53 else if (e1.op == TOK.TOKarrayliteral)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
54 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
55 ArrayLiteralExp es1 = cast(ArrayLiteralExp)e1;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
56 cmp = !es1.elements || (0 == es1.elements.dim);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
57 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
58 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
59 return EXP_CANT_INTERPRET;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
60 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
61 else if (e1.op == TOK.TOKstring && e2.op == TOK.TOKstring)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
62 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
63 StringExp es1 = cast(StringExp)e1;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
64 StringExp es2 = cast(StringExp)e2;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
65
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
66 if (es1.sz != es2.sz)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
67 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
68 assert(global.errors);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
69 return EXP_CANT_INTERPRET;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
70 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
71 if (es1.len == es2.len && memcmp(es1.string_, es2.string_, es1.sz * es1.len) == 0)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
72 cmp = true;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
73 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
74 cmp = false;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
75 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
76 else if (e1.op == TOK.TOKarrayliteral && e2.op == TOK.TOKarrayliteral)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
77 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
78 ArrayLiteralExp es1 = cast(ArrayLiteralExp)e1;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
79 ArrayLiteralExp es2 = cast(ArrayLiteralExp)e2;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
80
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
81 if ((!es1.elements || !es1.elements.dim) && (!es2.elements || !es2.elements.dim))
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
82 cmp = true; // both arrays are empty
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
83 else if (!es1.elements || !es2.elements)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
84 cmp = false;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
85 else if (es1.elements.dim != es2.elements.dim)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
86 cmp = false;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
87 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
88 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
89 for (size_t i = 0; i < es1.elements.dim; i++)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
90 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
91 Expression ee1 = cast(Expression)es1.elements.data[i];
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
92 Expression ee2 = cast(Expression)es2.elements.data[i];
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
93
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
94 Expression v = Equal(TOK.TOKequal, Type.tint32, ee1, ee2);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
95 if (v == EXP_CANT_INTERPRET)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
96 return EXP_CANT_INTERPRET;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
97 long tmp = v.toInteger();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
98 cmp = (tmp != 0);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
99 if (!cmp)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
100 break;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
101 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
102 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
103 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
104 else if (e1.op == TOK.TOKarrayliteral && e2.op == TOK.TOKstring)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
105 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
106 // Swap operands and use common code
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
107 Expression ee = e1;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
108 e1 = e2;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
109 e2 = ee;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
110 goto Lsa;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
111 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
112 else if (e1.op == TOK.TOKstring && e2.op == TOK.TOKarrayliteral)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
113 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
114 Lsa:
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
115 StringExp es1 = cast(StringExp)e1;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
116 ArrayLiteralExp es2 = cast(ArrayLiteralExp)e2;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
117 size_t dim1 = es1.len;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
118 size_t dim2 = es2.elements ? es2.elements.dim : 0;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
119 if (dim1 != dim2)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
120 cmp = false;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
121 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
122 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
123 for (size_t i = 0; i < dim1; i++)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
124 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
125 ulong c = es1.charAt(i);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
126 Expression ee2 = cast(Expression)es2.elements.data[i];
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
127 if (ee2.isConst() != 1)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
128 return EXP_CANT_INTERPRET;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
129 cmp = (c == ee2.toInteger());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
130 if (!cmp)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
131 break;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
132 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
133 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
134 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
135 else if (e1.op == TOK.TOKstructliteral && e2.op == TOK.TOKstructliteral)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
136 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
137 StructLiteralExp es1 = cast(StructLiteralExp)e1;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
138 StructLiteralExp es2 = cast(StructLiteralExp)e2;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
139
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
140 if (es1.sd != es2.sd)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
141 cmp = false;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
142 else if ((!es1.elements || !es1.elements.dim) && (!es2.elements || !es2.elements.dim))
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
143 cmp = true; // both arrays are empty
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
144 else if (!es1.elements || !es2.elements)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
145 cmp = false;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
146 else if (es1.elements.dim != es2.elements.dim)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
147 cmp = false;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
148 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
149 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
150 cmp = true;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
151 for (size_t i = 0; i < es1.elements.dim; i++)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
152 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
153 Expression ee1 = cast(Expression)es1.elements.data[i];
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
154 Expression ee2 = cast(Expression)es2.elements.data[i];
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
155
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
156 if (ee1 == ee2)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
157 continue;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
158 if (!ee1 || !ee2)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
159 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
160 cmp = false;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
161 break;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
162 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
163 Expression v = Equal(TOK.TOKequal, Type.tint32, ee1, ee2);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
164 if (v == EXP_CANT_INTERPRET)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
165 return EXP_CANT_INTERPRET;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
166 long tmp = v.toInteger();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
167 cmp = (tmp != 0);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
168 if (!cmp)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
169 break;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
170 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
171 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
172 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
173 ///static if (false) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
174 /// else if (e1.op == TOKarrayliteral && e2.op == TOKstring)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
175 /// {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
176 /// }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
177 ///}
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
178 else if (e1.isConst() != 1 || e2.isConst() != 1)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
179 return EXP_CANT_INTERPRET;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
180 else if (e1.type.isreal())
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
181 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
182 r1 = e1.toReal();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
183 r2 = e2.toReal();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
184 goto L1;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
185 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
186 else if (e1.type.isimaginary())
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
187 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
188 r1 = e1.toImaginary();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
189 r2 = e2.toImaginary();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
190 L1:
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
191 cmp = (r1 == r2);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
192 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
193 else if (e1.type.iscomplex())
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
194 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
195 cmp = (e1.toComplex() == e2.toComplex());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
196 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
197 else if (e1.type.isintegral())
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
198 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
199 cmp = (e1.toInteger() == e2.toInteger());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
200 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
201 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
202 return EXP_CANT_INTERPRET;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
203 if (op == TOK.TOKnotequal)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
204 cmp = !cmp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
205
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
206 e = new IntegerExp(loc, cmp, type);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
207
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
208 return e;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
209 }