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

Side by Side Diff: pkg/compiler/lib/src/universe/codegen_world_builder.dart

Issue 2916893002: Handle int constant (Closed)
Patch Set: Updated cf. comments Created 3 years, 6 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) 2017, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2017, 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 part of world_builder; 5 part of world_builder;
6 6
7 /// World builder specific to codegen. 7 /// World builder specific to codegen.
8 /// 8 ///
9 /// This adds additional access to liveness of selectors and elements. 9 /// This adds additional access to liveness of selectors and elements.
10 abstract class CodegenWorldBuilder implements WorldBuilder { 10 abstract class CodegenWorldBuilder implements WorldBuilder {
(...skipping 30 matching lines...) Expand all
41 Iterable<FunctionEntity> get staticFunctionsNeedingGetter; 41 Iterable<FunctionEntity> get staticFunctionsNeedingGetter;
42 Iterable<FunctionEntity> get methodsNeedingSuperGetter; 42 Iterable<FunctionEntity> get methodsNeedingSuperGetter;
43 43
44 /// The set of all referenced static fields. 44 /// The set of all referenced static fields.
45 /// 45 ///
46 /// Invariant: Elements are declaration elements. 46 /// Invariant: Elements are declaration elements.
47 Iterable<FieldEntity> get allReferencedStaticFields; 47 Iterable<FieldEntity> get allReferencedStaticFields;
48 48
49 /// Set of methods in instantiated classes that are potentially closurized. 49 /// Set of methods in instantiated classes that are potentially closurized.
50 Iterable<FunctionEntity> get closurizedMembers; 50 Iterable<FunctionEntity> get closurizedMembers;
51
52 /// Register [constant] as needed for emission.
53 void addCompileTimeConstantForEmission(ConstantValue constant);
54
55 /// Returns a list of constants topologically sorted so that dependencies
56 /// appear before the dependent constant.
57 ///
58 /// [preSortCompare] is a comparator function that gives the constants a
59 /// consistent order prior to the topological sort which gives the constants
60 /// an ordering that is less sensitive to perturbations in the source code.
61 List<ConstantValue> getConstantsForEmission(
62 [Comparator<ConstantValue> preSortCompare]);
51 } 63 }
52 64
53 abstract class CodegenWorldBuilderImpl implements CodegenWorldBuilder { 65 abstract class CodegenWorldBuilderImpl implements CodegenWorldBuilder {
54 final ElementEnvironment _elementEnvironment; 66 final ElementEnvironment _elementEnvironment;
55 final NativeBasicData _nativeBasicData; 67 final NativeBasicData _nativeBasicData;
56 final ClosedWorld _world; 68 final ClosedWorld _world;
57 69
58 /// The set of all directly instantiated classes, that is, classes with a 70 /// The set of all directly instantiated classes, that is, classes with a
59 /// generative constructor that has been called directly and not only through 71 /// generative constructor that has been called directly and not only through
60 /// a super-call. 72 /// a super-call.
(...skipping 419 matching lines...) Expand 10 before | Expand all | Expand 10 after
480 return true; 492 return true;
481 } 493 }
482 return false; 494 return false;
483 } 495 }
484 496
485 while (cls != null && processClass(cls)) { 497 while (cls != null && processClass(cls)) {
486 cls = _elementEnvironment.getSuperClass(cls); 498 cls = _elementEnvironment.getSuperClass(cls);
487 } 499 }
488 } 500 }
489 501
490 bool registerConstantUse(ConstantUse use); 502 /// Set of all registered compiled constants.
503 final Set<ConstantValue> compiledConstants = new Set<ConstantValue>();
504
505 @override
506 void addCompileTimeConstantForEmission(ConstantValue constant) {
507 compiledConstants.add(constant);
508 }
509
510 @override
511 List<ConstantValue> getConstantsForEmission(
512 [Comparator<ConstantValue> preSortCompare]) {
513 // We must emit dependencies before their uses.
514 Set<ConstantValue> seenConstants = new Set<ConstantValue>();
515 List<ConstantValue> result = new List<ConstantValue>();
516
517 void addConstant(ConstantValue constant) {
518 if (!seenConstants.contains(constant)) {
519 constant.getDependencies().forEach(addConstant);
520 assert(!seenConstants.contains(constant));
521 result.add(constant);
522 seenConstants.add(constant);
523 }
524 }
525
526 List<ConstantValue> sorted = compiledConstants.toList();
527 if (preSortCompare != null) {
528 sorted.sort(preSortCompare);
529 }
530 sorted.forEach(addConstant);
531 return result;
532 }
533
534 /// Register the constant [use] with this world builder. Returns `true` if
535 /// the constant use was new to the world.
536 bool registerConstantUse(ConstantUse use) {
537 if (use.kind == ConstantUseKind.DIRECT) {
538 addCompileTimeConstantForEmission(use.value);
539 }
540 return _constantValues.add(use.value);
541 }
491 } 542 }
492 543
493 class ElementCodegenWorldBuilderImpl extends CodegenWorldBuilderImpl { 544 class ElementCodegenWorldBuilderImpl extends CodegenWorldBuilderImpl {
494 final JavaScriptConstantCompiler _constants;
495
496 ElementCodegenWorldBuilderImpl( 545 ElementCodegenWorldBuilderImpl(
497 ElementEnvironment elementEnvironment, 546 ElementEnvironment elementEnvironment,
498 NativeBasicData nativeBasicData, 547 NativeBasicData nativeBasicData,
499 ClosedWorld world, 548 ClosedWorld world,
500 this._constants,
501 SelectorConstraintsStrategy selectorConstraintsStrategy) 549 SelectorConstraintsStrategy selectorConstraintsStrategy)
502 : super(elementEnvironment, nativeBasicData, world, 550 : super(elementEnvironment, nativeBasicData, world,
503 selectorConstraintsStrategy); 551 selectorConstraintsStrategy);
504 552
505 /// Calls [f] with every instance field, together with its declarer, in an 553 /// Calls [f] with every instance field, together with its declarer, in an
506 /// instance of [cls]. 554 /// instance of [cls].
507 void forEachInstanceField( 555 void forEachInstanceField(
508 ClassElement cls, void f(ClassEntity declarer, FieldEntity field)) { 556 ClassElement cls, void f(ClassEntity declarer, FieldEntity field)) {
509 cls.implementation 557 cls.implementation
510 .forEachInstanceField(f, includeSuperAndInjectedMembers: true); 558 .forEachInstanceField(f, includeSuperAndInjectedMembers: true);
(...skipping 23 matching lines...) Expand all
534 return super._getMemberUsage(member, memberUsed); 582 return super._getMemberUsage(member, memberUsed);
535 } 583 }
536 584
537 void registerStaticUse(StaticUse staticUse, MemberUsedCallback memberUsed) { 585 void registerStaticUse(StaticUse staticUse, MemberUsedCallback memberUsed) {
538 Element element = staticUse.element; 586 Element element = staticUse.element;
539 assert(element.isDeclaration, 587 assert(element.isDeclaration,
540 failedAt(element, "Element ${element} is not the declaration.")); 588 failedAt(element, "Element ${element} is not the declaration."));
541 super.registerStaticUse(staticUse, memberUsed); 589 super.registerStaticUse(staticUse, memberUsed);
542 } 590 }
543 591
544 /// Register the constant [use] with this world builder. Returns `true` if
545 /// the constant use was new to the world.
546 @override
547 bool registerConstantUse(ConstantUse use) {
548 if (use.kind == ConstantUseKind.DIRECT) {
549 _constants.addCompileTimeConstantForEmission(use.value);
550 }
551 return _constantValues.add(use.value);
552 }
553
554 void registerIsCheck(ResolutionDartType type) { 592 void registerIsCheck(ResolutionDartType type) {
555 // Even in checked mode, type annotations for return type and argument 593 // Even in checked mode, type annotations for return type and argument
556 // types do not imply type checks, so there should never be a check 594 // types do not imply type checks, so there should never be a check
557 // against the type variable of a typedef. 595 // against the type variable of a typedef.
558 assert(!type.isTypeVariable || !type.element.enclosingElement.isTypedef); 596 assert(!type.isTypeVariable || !type.element.enclosingElement.isTypedef);
559 super.registerIsCheck(type); 597 super.registerIsCheck(type);
560 } 598 }
561 } 599 }
562 600
563 class KernelCodegenWorldBuilder extends CodegenWorldBuilderImpl { 601 class KernelCodegenWorldBuilder extends CodegenWorldBuilderImpl {
564 KernelCodegenWorldBuilder( 602 KernelCodegenWorldBuilder(
565 ElementEnvironment elementEnvironment, 603 ElementEnvironment elementEnvironment,
566 NativeBasicData nativeBasicData, 604 NativeBasicData nativeBasicData,
567 ClosedWorld world, 605 ClosedWorld world,
568 SelectorConstraintsStrategy selectorConstraintsStrategy) 606 SelectorConstraintsStrategy selectorConstraintsStrategy)
569 : super(elementEnvironment, nativeBasicData, world, 607 : super(elementEnvironment, nativeBasicData, world,
570 selectorConstraintsStrategy); 608 selectorConstraintsStrategy);
571 609
572 @override 610 @override
573 bool registerConstantUse(ConstantUse use) {
574 throw new UnimplementedError(
575 'KernelCodegenWorldBuilder.registerConstantUse');
576 }
577
578 @override
579 void forEachParameter( 611 void forEachParameter(
580 FunctionEntity function, void f(DartType type, String name)) { 612 FunctionEntity function, void f(DartType type, String name)) {
581 throw new UnimplementedError('KernelCodegenWorldBuilder.forEachParameter'); 613 throw new UnimplementedError('KernelCodegenWorldBuilder.forEachParameter');
582 } 614 }
583 615
584 @override 616 @override
585 void forEachInstanceField( 617 void forEachInstanceField(
586 ClassEntity cls, void f(ClassEntity declarer, FieldEntity field)) { 618 ClassEntity cls, void f(ClassEntity declarer, FieldEntity field)) {
587 throw new UnimplementedError( 619 throw new UnimplementedError(
588 'KernelCodegenWorldBuilder.forEachInstanceField'); 620 'KernelCodegenWorldBuilder.forEachInstanceField');
589 } 621 }
590 } 622 }
OLDNEW
« no previous file with comments | « pkg/compiler/lib/src/resolution/resolution_strategy.dart ('k') | pkg/compiler/lib/src/universe/world_builder.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698