Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(158)

Side by Side Diff: pkg/compiler/lib/src/constant_system_dart.dart

Issue 1559233002: WIP: Compute constant expressions in resolution. (Closed) Base URL: https://github.com/dart-lang/sdk.git@master
Patch Set: Created 4 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2012, 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.constant_system.dart; 5 library dart2js.constant_system.dart;
6 6
7 import 'compiler.dart' show 7 import 'compiler.dart' show
8 Compiler; 8 Compiler;
9 import 'constants/constant_system.dart'; 9 import 'constants/constant_system.dart';
10 import 'constants/values.dart'; 10 import 'constants/values.dart';
11 import 'dart_types.dart'; 11 import 'dart_types.dart';
12 import 'elements/elements.dart' show
13 ClassElement,
14 FieldElement;
12 import 'tree/tree.dart' show 15 import 'tree/tree.dart' show
13 DartString; 16 DartString;
14 17
15 const DART_CONSTANT_SYSTEM = const DartConstantSystem(); 18 const DART_CONSTANT_SYSTEM = const DartConstantSystem();
16 19
17 class BitNotOperation implements UnaryOperation { 20 class BitNotOperation implements UnaryOperation {
18 final String name = '~'; 21 final String name = '~';
19 const BitNotOperation(); 22 const BitNotOperation();
20 ConstantValue fold(ConstantValue constant) { 23 ConstantValue fold(ConstantValue constant) {
21 if (constant.isInt) { 24 if (constant.isInt) {
(...skipping 290 matching lines...) Expand 10 before | Expand all | Expand 10 after
312 if (left.isConstructedObject) { 315 if (left.isConstructedObject) {
313 // Unless we know that the user-defined object does not implement the 316 // Unless we know that the user-defined object does not implement the
314 // equality operator we cannot fold here. 317 // equality operator we cannot fold here.
315 return null; 318 return null;
316 } 319 }
317 return DART_CONSTANT_SYSTEM.createBool(left == right); 320 return DART_CONSTANT_SYSTEM.createBool(left == right);
318 } 321 }
319 apply(left, right) => left == right; 322 apply(left, right) => left == right;
320 } 323 }
321 324
325 class NotEqualsOperation implements BinaryOperation {
326 final String name = '!=';
327 const NotEqualsOperation();
328
329 @override
330 ConstantValue fold(ConstantValue left, ConstantValue right) {
331 BoolConstantValue result = const EqualsOperation().fold(left, right);
332 if (result != null) {
333 result = result.negate();
334 }
335 return result;
336 }
337
338 apply(left, right) => left != right;
339 }
340
322 class IdentityOperation implements BinaryOperation { 341 class IdentityOperation implements BinaryOperation {
323 final String name = '==='; 342 final String name = '===';
324 const IdentityOperation(); 343 const IdentityOperation();
325 BoolConstantValue fold(ConstantValue left, ConstantValue right) { 344 BoolConstantValue fold(ConstantValue left, ConstantValue right) {
326 // In order to preserve runtime semantics which says that NaN !== NaN don't 345 // In order to preserve runtime semantics which says that NaN !== NaN don't
327 // constant fold NaN === NaN. Otherwise the output depends on inlined 346 // constant fold NaN === NaN. Otherwise the output depends on inlined
328 // variables and other optimizations. 347 // variables and other optimizations.
329 if (left.isNaN && right.isNaN) return null; 348 if (left.isNaN && right.isNaN) return null;
330 return DART_CONSTANT_SYSTEM.createBool(left == right); 349 return DART_CONSTANT_SYSTEM.createBool(left == right);
331 } 350 }
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
381 final equal = const EqualsOperation(); 400 final equal = const EqualsOperation();
382 final greaterEqual = const GreaterEqualOperation(); 401 final greaterEqual = const GreaterEqualOperation();
383 final greater = const GreaterOperation(); 402 final greater = const GreaterOperation();
384 final identity = const IdentityOperation(); 403 final identity = const IdentityOperation();
385 final lessEqual = const LessEqualOperation(); 404 final lessEqual = const LessEqualOperation();
386 final less = const LessOperation(); 405 final less = const LessOperation();
387 final modulo = const ModuloOperation(); 406 final modulo = const ModuloOperation();
388 final multiply = const MultiplyOperation(); 407 final multiply = const MultiplyOperation();
389 final negate = const NegateOperation(); 408 final negate = const NegateOperation();
390 final not = const NotOperation(); 409 final not = const NotOperation();
410 final notEqual = const NotEqualsOperation();
391 final shiftLeft = const ShiftLeftOperation(); 411 final shiftLeft = const ShiftLeftOperation();
392 final shiftRight = const ShiftRightOperation(); 412 final shiftRight = const ShiftRightOperation();
393 final subtract = const SubtractOperation(); 413 final subtract = const SubtractOperation();
394 final truncatingDivide = const TruncatingDivideOperation(); 414 final truncatingDivide = const TruncatingDivideOperation();
395 final codeUnitAt = const CodeUnitAtConstantOperation(); 415 final codeUnitAt = const CodeUnitAtConstantOperation();
396 416
397 const DartConstantSystem(); 417 const DartConstantSystem();
398 418
399 419
400 @override 420 @override
(...skipping 29 matching lines...) Expand all
430 450
431 @override 451 @override
432 ConstantValue createType(Compiler compiler, DartType type) { 452 ConstantValue createType(Compiler compiler, DartType type) {
433 // TODO(johnniwinther): Change the `Type` type to 453 // TODO(johnniwinther): Change the `Type` type to
434 // `compiler.coreTypes.typeType` and check the backend specific value in 454 // `compiler.coreTypes.typeType` and check the backend specific value in
435 // [checkConstMapKeysDontOverrideEquals] in 'members.dart'. 455 // [checkConstMapKeysDontOverrideEquals] in 'members.dart'.
436 return new TypeConstantValue(type, 456 return new TypeConstantValue(type,
437 compiler.backend.typeImplementation.computeType(compiler.resolution)); 457 compiler.backend.typeImplementation.computeType(compiler.resolution));
438 } 458 }
439 459
460 @override
461 ConstantValue createSymbol(Compiler compiler, String text) {
462 // TODO(johnniwinther): Create a backend agnostic value.
463 InterfaceType type = compiler.symbolClass.rawType;
464 ConstantValue argument = createString(new DartString.literal(text));
465 Map<FieldElement, ConstantValue> fields = <FieldElement, ConstantValue>{};
466 compiler.symbolImplementationClass.forEachInstanceField(
467 (ClassElement enclosingClass, FieldElement field) {
468 fields[field] = argument;
469 });
470 assert(fields.length == 1);
471 return new ConstructedConstantValue(type, fields);
472 }
473
440 bool isInt(ConstantValue constant) => constant.isInt; 474 bool isInt(ConstantValue constant) => constant.isInt;
441 bool isDouble(ConstantValue constant) => constant.isDouble; 475 bool isDouble(ConstantValue constant) => constant.isDouble;
442 bool isString(ConstantValue constant) => constant.isString; 476 bool isString(ConstantValue constant) => constant.isString;
443 bool isBool(ConstantValue constant) => constant.isBool; 477 bool isBool(ConstantValue constant) => constant.isBool;
444 bool isNull(ConstantValue constant) => constant.isNull; 478 bool isNull(ConstantValue constant) => constant.isNull;
445 479
446 bool isSubtype(DartTypes types, DartType s, DartType t) { 480 bool isSubtype(DartTypes types, DartType s, DartType t) {
447 return types.isSubtype(s, t); 481 return types.isSubtype(s, t);
448 } 482 }
449 } 483 }
OLDNEW
« no previous file with comments | « pkg/compiler/lib/src/compile_time_constants.dart ('k') | pkg/compiler/lib/src/constants/constant_system.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698