OLD | NEW |
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 library dart2js.operators; | 5 library dart2js.operators; |
6 | 6 |
7 import '../elements/elements.dart'; | 7 import '../elements/elements.dart'; |
8 import '../universe/call_structure.dart' show | 8 import '../universe/call_structure.dart' show CallStructure; |
9 CallStructure; | 9 import '../universe/selector.dart' show Selector, SelectorKind; |
10 import '../universe/selector.dart' show | |
11 Selector, | |
12 SelectorKind; | |
13 | 10 |
14 enum UnaryOperatorKind { | 11 enum UnaryOperatorKind { NOT, NEGATE, COMPLEMENT, } |
15 NOT, | |
16 NEGATE, | |
17 COMPLEMENT, | |
18 } | |
19 | 12 |
20 class UnaryOperator { | 13 class UnaryOperator { |
21 final UnaryOperatorKind kind; | 14 final UnaryOperatorKind kind; |
22 final String name; | 15 final String name; |
23 final String selectorName; | 16 final String selectorName; |
24 | 17 |
25 const UnaryOperator(this.kind, this.name, this.selectorName); | 18 const UnaryOperator(this.kind, this.name, this.selectorName); |
26 | 19 |
27 bool get isUserDefinable => selectorName != null; | 20 bool get isUserDefinable => selectorName != null; |
28 | 21 |
29 Selector get selector => new Selector( | 22 Selector get selector => new Selector(SelectorKind.OPERATOR, |
30 SelectorKind.OPERATOR, | 23 new PublicName(selectorName), CallStructure.NO_ARGS); |
31 new PublicName(selectorName), | |
32 CallStructure.NO_ARGS); | |
33 | 24 |
34 String toString() => name; | 25 String toString() => name; |
35 | 26 |
36 /// The unary ! operator. | 27 /// The unary ! operator. |
37 static const UnaryOperator NOT = | 28 static const UnaryOperator NOT = |
38 const UnaryOperator(UnaryOperatorKind.NOT, '!', null); | 29 const UnaryOperator(UnaryOperatorKind.NOT, '!', null); |
39 | 30 |
40 /// The unary - operator. | 31 /// The unary - operator. |
41 static const UnaryOperator NEGATE = | 32 static const UnaryOperator NEGATE = |
42 const UnaryOperator(UnaryOperatorKind.NEGATE, '-', 'unary-'); | 33 const UnaryOperator(UnaryOperatorKind.NEGATE, '-', 'unary-'); |
43 | 34 |
44 /// The unary ~ operator. | 35 /// The unary ~ operator. |
45 static const UnaryOperator COMPLEMENT = | 36 static const UnaryOperator COMPLEMENT = |
46 const UnaryOperator(UnaryOperatorKind.COMPLEMENT, '~', '~'); | 37 const UnaryOperator(UnaryOperatorKind.COMPLEMENT, '~', '~'); |
47 | 38 |
48 static UnaryOperator parse(String value) { | 39 static UnaryOperator parse(String value) { |
49 switch (value) { | 40 switch (value) { |
50 case '!': return NOT; | 41 case '!': |
51 case '-': return NEGATE; | 42 return NOT; |
52 case '~': return COMPLEMENT; | 43 case '-': |
53 default: return null; | 44 return NEGATE; |
| 45 case '~': |
| 46 return COMPLEMENT; |
| 47 default: |
| 48 return null; |
54 } | 49 } |
55 } | 50 } |
56 | 51 |
57 static UnaryOperator fromKind(UnaryOperatorKind kind) { | 52 static UnaryOperator fromKind(UnaryOperatorKind kind) { |
58 switch (kind) { | 53 switch (kind) { |
59 case UnaryOperatorKind.NOT: return NOT; | 54 case UnaryOperatorKind.NOT: |
60 case UnaryOperatorKind.NEGATE: return NEGATE; | 55 return NOT; |
61 case UnaryOperatorKind.COMPLEMENT: return COMPLEMENT; | 56 case UnaryOperatorKind.NEGATE: |
| 57 return NEGATE; |
| 58 case UnaryOperatorKind.COMPLEMENT: |
| 59 return COMPLEMENT; |
62 } | 60 } |
63 } | 61 } |
64 } | 62 } |
65 | 63 |
66 enum BinaryOperatorKind { | 64 enum BinaryOperatorKind { |
67 EQ, | 65 EQ, |
68 NOT_EQ, | 66 NOT_EQ, |
69 INDEX, | 67 INDEX, |
70 ADD, | 68 ADD, |
71 SUB, | 69 SUB, |
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
179 /// The binary | operator. | 177 /// The binary | operator. |
180 static const BinaryOperator LOGICAL_OR = | 178 static const BinaryOperator LOGICAL_OR = |
181 const _LogicalOperator(BinaryOperatorKind.LOGICAL_OR, '||'); | 179 const _LogicalOperator(BinaryOperatorKind.LOGICAL_OR, '||'); |
182 | 180 |
183 /// The if-null ?? operator. | 181 /// The if-null ?? operator. |
184 static const BinaryOperator IF_NULL = | 182 static const BinaryOperator IF_NULL = |
185 const _IfNullOperator(BinaryOperatorKind.IF_NULL, '??'); | 183 const _IfNullOperator(BinaryOperatorKind.IF_NULL, '??'); |
186 | 184 |
187 static BinaryOperator parse(String value) { | 185 static BinaryOperator parse(String value) { |
188 switch (value) { | 186 switch (value) { |
189 case '==': return EQ; | 187 case '==': |
190 case '!=': return NOT_EQ; | 188 return EQ; |
191 case '[]': return INDEX; | 189 case '!=': |
192 case '*': return MUL; | 190 return NOT_EQ; |
193 case '/': return DIV; | 191 case '[]': |
194 case '%': return MOD; | 192 return INDEX; |
195 case '~/': return IDIV; | 193 case '*': |
196 case '+': return ADD; | 194 return MUL; |
197 case '-': return SUB; | 195 case '/': |
198 case '<<': return SHL; | 196 return DIV; |
199 case '>>': return SHR; | 197 case '%': |
200 case '>=': return GTEQ; | 198 return MOD; |
201 case '>': return GT; | 199 case '~/': |
202 case '<=': return LTEQ; | 200 return IDIV; |
203 case '<': return LT; | 201 case '+': |
204 case '&': return AND; | 202 return ADD; |
205 case '^': return XOR; | 203 case '-': |
206 case '|': return OR; | 204 return SUB; |
207 case '&&': return LOGICAL_AND; | 205 case '<<': |
208 case '||': return LOGICAL_OR; | 206 return SHL; |
209 case '??': return IF_NULL; | 207 case '>>': |
210 default: return null; | 208 return SHR; |
| 209 case '>=': |
| 210 return GTEQ; |
| 211 case '>': |
| 212 return GT; |
| 213 case '<=': |
| 214 return LTEQ; |
| 215 case '<': |
| 216 return LT; |
| 217 case '&': |
| 218 return AND; |
| 219 case '^': |
| 220 return XOR; |
| 221 case '|': |
| 222 return OR; |
| 223 case '&&': |
| 224 return LOGICAL_AND; |
| 225 case '||': |
| 226 return LOGICAL_OR; |
| 227 case '??': |
| 228 return IF_NULL; |
| 229 default: |
| 230 return null; |
211 } | 231 } |
212 } | 232 } |
213 | 233 |
214 static BinaryOperator fromKind(BinaryOperatorKind kind) { | 234 static BinaryOperator fromKind(BinaryOperatorKind kind) { |
215 switch (kind) { | 235 switch (kind) { |
216 case BinaryOperatorKind.EQ: return EQ; | 236 case BinaryOperatorKind.EQ: |
217 case BinaryOperatorKind.NOT_EQ: return NOT_EQ; | 237 return EQ; |
218 case BinaryOperatorKind.INDEX: return INDEX; | 238 case BinaryOperatorKind.NOT_EQ: |
219 case BinaryOperatorKind.MUL: return MUL; | 239 return NOT_EQ; |
220 case BinaryOperatorKind.DIV: return DIV; | 240 case BinaryOperatorKind.INDEX: |
221 case BinaryOperatorKind.MOD: return MOD; | 241 return INDEX; |
222 case BinaryOperatorKind.IDIV: return IDIV; | 242 case BinaryOperatorKind.MUL: |
223 case BinaryOperatorKind.ADD: return ADD; | 243 return MUL; |
224 case BinaryOperatorKind.SUB: return SUB; | 244 case BinaryOperatorKind.DIV: |
225 case BinaryOperatorKind.SHL: return SHL; | 245 return DIV; |
226 case BinaryOperatorKind.SHR: return SHR; | 246 case BinaryOperatorKind.MOD: |
227 case BinaryOperatorKind.GTEQ: return GTEQ; | 247 return MOD; |
228 case BinaryOperatorKind.GT: return GT; | 248 case BinaryOperatorKind.IDIV: |
229 case BinaryOperatorKind.LTEQ: return LTEQ; | 249 return IDIV; |
230 case BinaryOperatorKind.LT: return LT; | 250 case BinaryOperatorKind.ADD: |
231 case BinaryOperatorKind.AND: return AND; | 251 return ADD; |
232 case BinaryOperatorKind.XOR: return XOR; | 252 case BinaryOperatorKind.SUB: |
233 case BinaryOperatorKind.OR: return OR; | 253 return SUB; |
234 case BinaryOperatorKind.LOGICAL_AND: return LOGICAL_AND; | 254 case BinaryOperatorKind.SHL: |
235 case BinaryOperatorKind.LOGICAL_OR: return LOGICAL_OR; | 255 return SHL; |
236 case BinaryOperatorKind.IF_NULL: return IF_NULL; | 256 case BinaryOperatorKind.SHR: |
| 257 return SHR; |
| 258 case BinaryOperatorKind.GTEQ: |
| 259 return GTEQ; |
| 260 case BinaryOperatorKind.GT: |
| 261 return GT; |
| 262 case BinaryOperatorKind.LTEQ: |
| 263 return LTEQ; |
| 264 case BinaryOperatorKind.LT: |
| 265 return LT; |
| 266 case BinaryOperatorKind.AND: |
| 267 return AND; |
| 268 case BinaryOperatorKind.XOR: |
| 269 return XOR; |
| 270 case BinaryOperatorKind.OR: |
| 271 return OR; |
| 272 case BinaryOperatorKind.LOGICAL_AND: |
| 273 return LOGICAL_AND; |
| 274 case BinaryOperatorKind.LOGICAL_OR: |
| 275 return LOGICAL_OR; |
| 276 case BinaryOperatorKind.IF_NULL: |
| 277 return IF_NULL; |
237 } | 278 } |
238 } | 279 } |
239 } | 280 } |
240 | 281 |
241 /// The operator !=, which is not user definable operator but instead is a | 282 /// The operator !=, which is not user definable operator but instead is a |
242 /// negation of a call to user definable operator, namely ==. | 283 /// negation of a call to user definable operator, namely ==. |
243 class _NotEqualsOperator extends BinaryOperator { | 284 class _NotEqualsOperator extends BinaryOperator { |
244 const _NotEqualsOperator() : super._(BinaryOperatorKind.NOT_EQ, '!='); | 285 const _NotEqualsOperator() : super._(BinaryOperatorKind.NOT_EQ, '!='); |
245 | 286 |
246 bool get isUserDefinable => false; | 287 bool get isUserDefinable => false; |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
285 XOR, | 326 XOR, |
286 } | 327 } |
287 | 328 |
288 class AssignmentOperator { | 329 class AssignmentOperator { |
289 final AssignmentOperatorKind kind; | 330 final AssignmentOperatorKind kind; |
290 final BinaryOperator binaryOperator; | 331 final BinaryOperator binaryOperator; |
291 final String name; | 332 final String name; |
292 final bool isUserDefinable; | 333 final bool isUserDefinable; |
293 | 334 |
294 const AssignmentOperator._(this.kind, this.name, this.binaryOperator, | 335 const AssignmentOperator._(this.kind, this.name, this.binaryOperator, |
295 {this.isUserDefinable: true}); | 336 {this.isUserDefinable: true}); |
296 | 337 |
297 String get selectorName { | 338 String get selectorName { |
298 return binaryOperator != null ? binaryOperator.selectorName: null; | 339 return binaryOperator != null ? binaryOperator.selectorName : null; |
299 } | 340 } |
300 | 341 |
301 String toString() => name; | 342 String toString() => name; |
302 | 343 |
303 /// The = operator. | 344 /// The = operator. |
304 static const AssignmentOperator ASSIGN = | 345 static const AssignmentOperator ASSIGN = const AssignmentOperator._( |
305 const AssignmentOperator._(AssignmentOperatorKind.ASSIGN, '=', | 346 AssignmentOperatorKind.ASSIGN, '=', null, |
306 null, isUserDefinable: false); | 347 isUserDefinable: false); |
307 | 348 |
308 /// The ??= operator. | 349 /// The ??= operator. |
309 static const AssignmentOperator IF_NULL = | 350 static const AssignmentOperator IF_NULL = const AssignmentOperator._( |
310 const AssignmentOperator._(AssignmentOperatorKind.IF_NULL, '??=', | 351 AssignmentOperatorKind.IF_NULL, '??=', BinaryOperator.IF_NULL, |
311 BinaryOperator.IF_NULL, | 352 isUserDefinable: false); |
312 isUserDefinable: false); | |
313 | 353 |
314 /// The += assignment operator. | 354 /// The += assignment operator. |
315 static const AssignmentOperator ADD = | 355 static const AssignmentOperator ADD = const AssignmentOperator._( |
316 const AssignmentOperator._(AssignmentOperatorKind.ADD, '+=', | 356 AssignmentOperatorKind.ADD, '+=', BinaryOperator.ADD); |
317 BinaryOperator.ADD); | |
318 | 357 |
319 /// The -= assignment operator. | 358 /// The -= assignment operator. |
320 static const AssignmentOperator SUB = | 359 static const AssignmentOperator SUB = const AssignmentOperator._( |
321 const AssignmentOperator._(AssignmentOperatorKind.SUB, '-=', | 360 AssignmentOperatorKind.SUB, '-=', BinaryOperator.SUB); |
322 BinaryOperator.SUB); | |
323 | 361 |
324 /// The *= assignment operator. | 362 /// The *= assignment operator. |
325 static const AssignmentOperator MUL = | 363 static const AssignmentOperator MUL = const AssignmentOperator._( |
326 const AssignmentOperator._(AssignmentOperatorKind.MUL, '*=', | 364 AssignmentOperatorKind.MUL, '*=', BinaryOperator.MUL); |
327 BinaryOperator.MUL); | |
328 | 365 |
329 /// The /= assignment operator. | 366 /// The /= assignment operator. |
330 static const AssignmentOperator DIV = | 367 static const AssignmentOperator DIV = const AssignmentOperator._( |
331 const AssignmentOperator._(AssignmentOperatorKind.DIV, '/=', | 368 AssignmentOperatorKind.DIV, '/=', BinaryOperator.DIV); |
332 BinaryOperator.DIV); | |
333 | 369 |
334 /// The ~/= assignment operator. | 370 /// The ~/= assignment operator. |
335 static const AssignmentOperator IDIV = | 371 static const AssignmentOperator IDIV = const AssignmentOperator._( |
336 const AssignmentOperator._(AssignmentOperatorKind.IDIV, '~/=', | 372 AssignmentOperatorKind.IDIV, '~/=', BinaryOperator.IDIV); |
337 BinaryOperator.IDIV); | |
338 | 373 |
339 /// The %= assignment operator. | 374 /// The %= assignment operator. |
340 static const AssignmentOperator MOD = | 375 static const AssignmentOperator MOD = const AssignmentOperator._( |
341 const AssignmentOperator._(AssignmentOperatorKind.MOD, '%=', | 376 AssignmentOperatorKind.MOD, '%=', BinaryOperator.MOD); |
342 BinaryOperator.MOD); | |
343 | 377 |
344 /// The <<= assignment operator. | 378 /// The <<= assignment operator. |
345 static const AssignmentOperator SHL = | 379 static const AssignmentOperator SHL = const AssignmentOperator._( |
346 const AssignmentOperator._(AssignmentOperatorKind.SHL, '<<=', | 380 AssignmentOperatorKind.SHL, '<<=', BinaryOperator.SHL); |
347 BinaryOperator.SHL); | |
348 | 381 |
349 /// The >>= assignment operator. | 382 /// The >>= assignment operator. |
350 static const AssignmentOperator SHR = | 383 static const AssignmentOperator SHR = const AssignmentOperator._( |
351 const AssignmentOperator._(AssignmentOperatorKind.SHR, '>>=', | 384 AssignmentOperatorKind.SHR, '>>=', BinaryOperator.SHR); |
352 BinaryOperator.SHR); | |
353 | 385 |
354 /// The &= assignment operator. | 386 /// The &= assignment operator. |
355 static const AssignmentOperator AND = | 387 static const AssignmentOperator AND = const AssignmentOperator._( |
356 const AssignmentOperator._(AssignmentOperatorKind.AND, '&=', | 388 AssignmentOperatorKind.AND, '&=', BinaryOperator.AND); |
357 BinaryOperator.AND); | |
358 | 389 |
359 /// The |= assignment operator. | 390 /// The |= assignment operator. |
360 static const AssignmentOperator OR = | 391 static const AssignmentOperator OR = const AssignmentOperator._( |
361 const AssignmentOperator._(AssignmentOperatorKind.OR, '|=', | 392 AssignmentOperatorKind.OR, '|=', BinaryOperator.OR); |
362 BinaryOperator.OR); | |
363 | 393 |
364 /// The ^= assignment operator. | 394 /// The ^= assignment operator. |
365 static const AssignmentOperator XOR = | 395 static const AssignmentOperator XOR = const AssignmentOperator._( |
366 const AssignmentOperator._(AssignmentOperatorKind.XOR, '^=', | 396 AssignmentOperatorKind.XOR, '^=', BinaryOperator.XOR); |
367 BinaryOperator.XOR); | |
368 | 397 |
369 static AssignmentOperator parse(String value) { | 398 static AssignmentOperator parse(String value) { |
370 switch (value) { | 399 switch (value) { |
371 case '=': return ASSIGN; | 400 case '=': |
372 case '??=': return IF_NULL; | 401 return ASSIGN; |
373 case '*=': return MUL; | 402 case '??=': |
374 case '/=': return DIV; | 403 return IF_NULL; |
375 case '%=': return MOD; | 404 case '*=': |
376 case '~/=': return IDIV; | 405 return MUL; |
377 case '+=': return ADD; | 406 case '/=': |
378 case '-=': return SUB; | 407 return DIV; |
379 case '<<=': return SHL; | 408 case '%=': |
380 case '>>=': return SHR; | 409 return MOD; |
381 case '&=': return AND; | 410 case '~/=': |
382 case '^=': return XOR; | 411 return IDIV; |
383 case '|=': return OR; | 412 case '+=': |
384 default: return null; | 413 return ADD; |
| 414 case '-=': |
| 415 return SUB; |
| 416 case '<<=': |
| 417 return SHL; |
| 418 case '>>=': |
| 419 return SHR; |
| 420 case '&=': |
| 421 return AND; |
| 422 case '^=': |
| 423 return XOR; |
| 424 case '|=': |
| 425 return OR; |
| 426 default: |
| 427 return null; |
385 } | 428 } |
386 } | 429 } |
387 } | 430 } |
388 | 431 |
389 | 432 enum IncDecOperatorKind { INC, DEC } |
390 enum IncDecOperatorKind { | |
391 INC, DEC | |
392 } | |
393 | 433 |
394 class IncDecOperator { | 434 class IncDecOperator { |
395 final IncDecOperatorKind kind; | 435 final IncDecOperatorKind kind; |
396 final String name; | 436 final String name; |
397 final BinaryOperator binaryOperator; | 437 final BinaryOperator binaryOperator; |
398 | 438 |
399 const IncDecOperator._(this.kind, this.name, this.binaryOperator); | 439 const IncDecOperator._(this.kind, this.name, this.binaryOperator); |
400 | 440 |
401 String get selectorName => binaryOperator.selectorName; | 441 String get selectorName => binaryOperator.selectorName; |
402 | 442 |
403 String toString() => name; | 443 String toString() => name; |
404 | 444 |
405 /// The prefix/postfix ++ operator. | 445 /// The prefix/postfix ++ operator. |
406 static const IncDecOperator INC = | 446 static const IncDecOperator INC = |
407 const IncDecOperator._(IncDecOperatorKind.INC, '++', BinaryOperator.ADD); | 447 const IncDecOperator._(IncDecOperatorKind.INC, '++', BinaryOperator.ADD); |
408 | 448 |
409 /// The prefix/postfix -- operator. | 449 /// The prefix/postfix -- operator. |
410 static const IncDecOperator DEC = | 450 static const IncDecOperator DEC = |
411 const IncDecOperator._(IncDecOperatorKind.DEC, '--', BinaryOperator.SUB); | 451 const IncDecOperator._(IncDecOperatorKind.DEC, '--', BinaryOperator.SUB); |
412 | 452 |
413 static IncDecOperator parse(String value) { | 453 static IncDecOperator parse(String value) { |
414 switch (value) { | 454 switch (value) { |
415 case '++': return INC; | 455 case '++': |
416 case '--': return DEC; | 456 return INC; |
417 default: return null; | 457 case '--': |
| 458 return DEC; |
| 459 default: |
| 460 return null; |
418 } | 461 } |
419 } | 462 } |
420 } | 463 } |
OLD | NEW |