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

Side by Side Diff: pkg/compiler/lib/src/cps_ir/cps_ir_nodes.dart

Issue 787603003: Generative constructors in the new dart backend. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Rebased Created 6 years 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 | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2013, 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 // IrNodes are kept in a separate library to have precise control over their 5 // IrNodes are kept in a separate library to have precise control over their
6 // dependencies on other parts of the system. 6 // dependencies on other parts of the system.
7 library dart2js.ir_nodes; 7 library dart2js.ir_nodes;
8 8
9 import '../constants/expressions.dart'; 9 import '../constants/expressions.dart';
10 import '../constants/values.dart' as values show ConstantValue; 10 import '../constants/values.dart' as values show ConstantValue;
(...skipping 531 matching lines...) Expand 10 before | Expand all | Expand 10 after
542 bool get isReturnContinuation => body == null; 542 bool get isReturnContinuation => body == null;
543 543
544 Continuation(this.parameters); 544 Continuation(this.parameters);
545 545
546 Continuation.retrn() : parameters = <Parameter>[new Parameter(null)]; 546 Continuation.retrn() : parameters = <Parameter>[new Parameter(null)];
547 547
548 accept(Visitor visitor) => visitor.visitContinuation(this); 548 accept(Visitor visitor) => visitor.visitContinuation(this);
549 } 549 }
550 550
551 abstract class ExecutableDefinition implements Node { 551 abstract class ExecutableDefinition implements Node {
552 Expression get body; 552 RunnableBody get body;
553 553
554 applyPass(Pass pass); 554 applyPass(Pass pass);
555 } 555 }
556 556
557 // This is basically a function definition with an empty parameter list and a 557 // This is basically a function definition with an empty parameter list and a
558 // field element instead of a function element and no const declarations, and 558 // field element instead of a function element and no const declarations, and
559 // never a getter or setter, though that's less important. 559 // never a getter or setter, though that's less important.
560 class FieldDefinition extends Node 560 class FieldDefinition extends Node implements ExecutableDefinition {
561 implements InteriorNode, ExecutableDefinition {
562 final FieldElement element; 561 final FieldElement element;
563 final Continuation returnContinuation; 562 RunnableBody body;
564 Expression body;
565 563
566 FieldDefinition(this.element, this.returnContinuation, this.body); 564 FieldDefinition(this.element, this.body);
567 565
568 FieldDefinition.withoutInitializer(this.element) 566 FieldDefinition.withoutInitializer(this.element)
569 : this.returnContinuation = null; 567 : this.body = null;
570 568
571 accept(Visitor visitor) => visitor.visitFieldDefinition(this); 569 accept(Visitor visitor) => visitor.visitFieldDefinition(this);
572 applyPass(Pass pass) => pass.rewriteFieldDefinition(this); 570 applyPass(Pass pass) => pass.rewriteFieldDefinition(this);
573 571
574 /// `true` if this field has no initializer. 572 /// `true` if this field has no initializer.
575 /// 573 ///
576 /// If `true` [body] and [returnContinuation] are `null`. 574 /// If `true` [body] and [returnContinuation] are `null`.
577 /// 575 ///
578 /// This is different from a initializer that is `null`. Consider this class: 576 /// This is different from a initializer that is `null`. Consider this class:
579 /// 577 ///
(...skipping 15 matching lines...) Expand all
595 class ClosureVariable extends Definition { 593 class ClosureVariable extends Definition {
596 /// Body of code that declares this closure variable. 594 /// Body of code that declares this closure variable.
597 ExecutableElement host; 595 ExecutableElement host;
598 Entity hint; 596 Entity hint;
599 597
600 ClosureVariable(this.host, this.hint); 598 ClosureVariable(this.host, this.hint);
601 599
602 accept(Visitor v) => v.visitClosureVariable(this); 600 accept(Visitor v) => v.visitClosureVariable(this);
603 } 601 }
604 602
603 class RunnableBody implements InteriorNode {
604 Expression body;
605 final Continuation returnContinuation;
606 Node parent;
607 RunnableBody(this.body, this.returnContinuation);
608 accept(Visitor visitor) => visitor.visitRunnableBody(this);
609 }
610
605 /// A function definition, consisting of parameters and a body. The parameters 611 /// A function definition, consisting of parameters and a body. The parameters
606 /// include a distinguished continuation parameter. 612 /// include a distinguished continuation parameter.
607 class FunctionDefinition extends Node 613 class FunctionDefinition extends Node
608 implements InteriorNode, ExecutableDefinition { 614 implements ExecutableDefinition {
609 final FunctionElement element; 615 final FunctionElement element;
610 final Continuation returnContinuation;
611 /// Mixed list of [Parameter]s and [ClosureVariable]s. 616 /// Mixed list of [Parameter]s and [ClosureVariable]s.
612 final List<Definition> parameters; 617 final List<Definition> parameters;
613 Expression body; 618 final RunnableBody body;
614 final List<ConstDeclaration> localConstants; 619 final List<ConstDeclaration> localConstants;
615 620
616 /// Values for optional parameters. 621 /// Values for optional parameters.
617 final List<ConstantExpression> defaultParameterValues; 622 final List<ConstantExpression> defaultParameterValues;
618 623
619 /// Closure variables declared by this function. 624 /// Closure variables declared by this function.
620 final List<ClosureVariable> closureVariables; 625 final List<ClosureVariable> closureVariables;
621 626
622 FunctionDefinition(this.element, this.returnContinuation, 627 FunctionDefinition(this.element,
623 this.parameters, this.body, this.localConstants, 628 this.parameters,
624 this.defaultParameterValues, this.closureVariables); 629 this.body,
630 this.localConstants,
631 this.defaultParameterValues,
632 this.closureVariables);
625 633
626 FunctionDefinition.abstract(this.element, 634 FunctionDefinition.abstract(this.element,
627 this.parameters, 635 this.parameters,
628 this.defaultParameterValues) 636 this.defaultParameterValues)
629 : this.returnContinuation = null, 637 : body = null,
630 this.localConstants = const <ConstDeclaration>[], 638 localConstants = const <ConstDeclaration>[],
631 this.closureVariables = const <ClosureVariable>[]; 639 closureVariables = const <ClosureVariable>[];
632 640
633 accept(Visitor visitor) => visitor.visitFunctionDefinition(this); 641 accept(Visitor visitor) => visitor.visitFunctionDefinition(this);
634 applyPass(Pass pass) => pass.rewriteFunctionDefinition(this); 642 applyPass(Pass pass) => pass.rewriteFunctionDefinition(this);
635 643
636 /// Returns `true` if this function is abstract or external. 644 /// Returns `true` if this function is abstract or external.
637 /// 645 ///
638 /// If `true`, [body] and [returnContinuation] are `null` and [localConstants] 646 /// If `true`, [body] and [returnContinuation] are `null` and [localConstants]
639 /// is empty. 647 /// is empty.
640 bool get isAbstract => body == null; 648 bool get isAbstract => body == null;
641 } 649 }
642 650
651 abstract class Initializer extends Node {}
652
653 class FieldInitializer implements Initializer {
654 final FieldElement element;
655 final RunnableBody body;
656 Node parent;
657
658 FieldInitializer(this.element, this.body);
659 accept(Visitor visitor) => visitor.visitFieldInitializer(this);
660 }
661
662 class SuperInitializer implements Initializer {
663 final ConstructorElement target;
664 final List<RunnableBody> arguments;
665 final Selector selector;
666 Node parent;
667 SuperInitializer(this.target, this.arguments, this.selector);
668 accept(Visitor visitor) => visitor.visitSuperInitializer(this);
669 }
670
671 class ConstructorDefinition extends FunctionDefinition {
672 final List<Initializer> initializers;
673 final bool isConst;
674
675 ConstructorDefinition(ConstructorElement element,
676 List<Definition> parameters,
677 RunnableBody body,
678 this.initializers,
679 List<ConstDeclaration> localConstants,
680 List<ConstantExpression> defaultParameterValues,
681 this.isConst,
682 List<ClosureVariable> closureVariables)
683 : super(element, parameters, body, localConstants,
684 defaultParameterValues, closureVariables);
685
686 // 'Abstract' here means "has no body" and is used to represent external
687 // constructors.
688 ConstructorDefinition.abstract(
689 ConstructorElement element,
690 List<Parameter> parameters,
691 List<ConstantExpression> defaultParameterValues,
692 this.isConst)
693 : initializers = null,
694 super.abstract(element, parameters, defaultParameterValues);
695
696 accept(Visitor visitor) => visitor.visitConstructorDefinition(this);
697 applyPass(Pass pass) => pass.rewriteConstructorDefinition(this);
698 }
699
643 List<Reference<Primitive>> _referenceList(Iterable<Primitive> definitions) { 700 List<Reference<Primitive>> _referenceList(Iterable<Primitive> definitions) {
644 return definitions.map((e) => new Reference<Primitive>(e)).toList(); 701 return definitions.map((e) => new Reference<Primitive>(e)).toList();
645 } 702 }
646 703
647 abstract class Visitor<T> { 704 abstract class Visitor<T> {
648 const Visitor(); 705 const Visitor();
649 706
650 T visit(Node node) => node.accept(this); 707 T visit(Node node) => node.accept(this);
651 // Abstract classes. 708 // Abstract classes.
652 T visitNode(Node node) => null; 709 T visitNode(Node node) => null;
653 T visitExpression(Expression node) => visitNode(node); 710 T visitExpression(Expression node) => visitNode(node);
654 T visitDefinition(Definition node) => visitNode(node); 711 T visitDefinition(Definition node) => visitNode(node);
655 T visitPrimitive(Primitive node) => visitDefinition(node); 712 T visitPrimitive(Primitive node) => visitDefinition(node);
656 T visitCondition(Condition node) => visitNode(node); 713 T visitCondition(Condition node) => visitNode(node);
714 T visitRunnableBody(RunnableBody node) => visitNode(node);
657 715
658 // Concrete classes. 716 // Concrete classes.
659 T visitFieldDefinition(FieldDefinition node) => visitNode(node); 717 T visitFieldDefinition(FieldDefinition node) => visitNode(node);
660 T visitFunctionDefinition(FunctionDefinition node) => visitNode(node); 718 T visitFunctionDefinition(FunctionDefinition node) => visitNode(node);
719 T visitConstructorDefinition(ConstructorDefinition node) {
720 return visitFunctionDefinition(node);
721 }
722
723 // Initializers
724 T visitInitializer(Initializer node) => visitNode(node);
725 T visitFieldInitializer(FieldInitializer node) => visitInitializer(node);
726 T visitSuperInitializer(SuperInitializer node) => visitInitializer(node);
661 727
662 // Expressions. 728 // Expressions.
663 T visitLetPrim(LetPrim node) => visitExpression(node); 729 T visitLetPrim(LetPrim node) => visitExpression(node);
664 T visitLetCont(LetCont node) => visitExpression(node); 730 T visitLetCont(LetCont node) => visitExpression(node);
665 T visitInvokeStatic(InvokeStatic node) => visitExpression(node); 731 T visitInvokeStatic(InvokeStatic node) => visitExpression(node);
666 T visitInvokeContinuation(InvokeContinuation node) => visitExpression(node); 732 T visitInvokeContinuation(InvokeContinuation node) => visitExpression(node);
667 T visitInvokeMethod(InvokeMethod node) => visitExpression(node); 733 T visitInvokeMethod(InvokeMethod node) => visitExpression(node);
668 T visitInvokeSuperMethod(InvokeSuperMethod node) => visitExpression(node); 734 T visitInvokeSuperMethod(InvokeSuperMethod node) => visitExpression(node);
669 T visitInvokeConstructor(InvokeConstructor node) => visitExpression(node); 735 T visitInvokeConstructor(InvokeConstructor node) => visitExpression(node);
670 T visitConcatenateStrings(ConcatenateStrings node) => visitExpression(node); 736 T visitConcatenateStrings(ConcatenateStrings node) => visitExpression(node);
(...skipping 24 matching lines...) Expand all
695 /// Recursively visits the entire CPS term, and calls abstract `process*` 761 /// Recursively visits the entire CPS term, and calls abstract `process*`
696 /// (i.e. `processLetPrim`) functions in pre-order. 762 /// (i.e. `processLetPrim`) functions in pre-order.
697 abstract class RecursiveVisitor extends Visitor { 763 abstract class RecursiveVisitor extends Visitor {
698 const RecursiveVisitor(); 764 const RecursiveVisitor();
699 765
700 // Ensures that RecursiveVisitor contains overrides for all relevant nodes. 766 // Ensures that RecursiveVisitor contains overrides for all relevant nodes.
701 // As a rule of thumb, nodes with structure to traverse should be overridden 767 // As a rule of thumb, nodes with structure to traverse should be overridden
702 // with the appropriate visits in this class (for example, visitLetCont), 768 // with the appropriate visits in this class (for example, visitLetCont),
703 // while leaving other nodes for subclasses (i.e., visitLiteralList). 769 // while leaving other nodes for subclasses (i.e., visitLiteralList).
704 visitNode(Node node) { 770 visitNode(Node node) {
705 throw "RecursiveVisitor is stale, add missing visit overrides"; 771 throw "$this is stale, add missing visit override for $node";
706 } 772 }
707 773
708 processReference(Reference ref) {} 774 processReference(Reference ref) {}
709 775
776 processRunnableBody(RunnableBody node) {}
777 visitRunnableBody(RunnableBody node) {
778 processRunnableBody(node);
779 visit(node.body);
780 }
781
710 processFieldDefinition(FieldDefinition node) {} 782 processFieldDefinition(FieldDefinition node) {}
711 visitFieldDefinition(FieldDefinition node) { 783 visitFieldDefinition(FieldDefinition node) {
712 processFieldDefinition(node); 784 processFieldDefinition(node);
713 if (node.hasInitializer) { 785 if (node.hasInitializer) {
714 visit(node.body); 786 visit(node.body);
715 } 787 }
716 } 788 }
717 789
718 processFunctionDefinition(FunctionDefinition node) {} 790 processFunctionDefinition(FunctionDefinition node) {}
719 visitFunctionDefinition(FunctionDefinition node) { 791 visitFunctionDefinition(FunctionDefinition node) {
720 processFunctionDefinition(node); 792 processFunctionDefinition(node);
721 node.parameters.forEach(visit); 793 node.parameters.forEach(visit);
722 if (!node.isAbstract) { 794 if (!node.isAbstract) {
723 visit(node.body); 795 visit(node.body);
724 } 796 }
725 } 797 }
726 798
799 processConstructorDefinition(ConstructorDefinition node) {}
800 visitConstructorDefinition(ConstructorDefinition node) {
801 processConstructorDefinition(node);
802 node.parameters.forEach(visit);
803 node.initializers.forEach(visit);
804 visit(node.body);
805 }
806
807 processFieldInitializer(FieldInitializer node) {}
808 visitFieldInitializer(FieldInitializer node) {
809 processFieldInitializer(node);
810 visit(node.body.body);
811 }
812
813 processSuperInitializer(SuperInitializer node) {}
814 visitSuperInitializer(SuperInitializer node) {
815 processSuperInitializer(node);
816 node.arguments.forEach(
817 (RunnableBody argument) => visit(argument.body));
818 }
819
727 // Expressions. 820 // Expressions.
728 821
729 processLetPrim(LetPrim node) {} 822 processLetPrim(LetPrim node) {}
730 visitLetPrim(LetPrim node) { 823 visitLetPrim(LetPrim node) {
731 processLetPrim(node); 824 processLetPrim(node);
732 visit(node.primitive); 825 visit(node.primitive);
733 visit(node.body); 826 visit(node.body);
734 } 827 }
735 828
736 processLetCont(LetCont node) {} 829 processLetCont(LetCont node) {}
(...skipping 201 matching lines...) Expand 10 before | Expand all | Expand 10 after
938 void visitReference(Reference reference) { 1031 void visitReference(Reference reference) {
939 allocate(reference.definition); 1032 allocate(reference.definition);
940 } 1033 }
941 1034
942 void visitFieldDefinition(FieldDefinition node) { 1035 void visitFieldDefinition(FieldDefinition node) {
943 if (node.hasInitializer) { 1036 if (node.hasInitializer) {
944 visit(node.body); 1037 visit(node.body);
945 } 1038 }
946 } 1039 }
947 1040
1041 void visitRunnableBody(RunnableBody node) {
1042 visit(node.body);
1043 }
1044
948 void visitFunctionDefinition(FunctionDefinition node) { 1045 void visitFunctionDefinition(FunctionDefinition node) {
949 if (!node.isAbstract) { 1046 if (!node.isAbstract) {
950 visit(node.body); 1047 visit(node.body);
951 } 1048 }
952 // Assign indices to unused parameters. 1049 // Assign indices to unused parameters.
953 for (Definition param in node.parameters) { 1050 for (Definition param in node.parameters) {
954 if (param is Primitive) { 1051 if (param is Primitive) {
955 allocate(param); 1052 allocate(param);
956 } 1053 }
957 } 1054 }
958 } 1055 }
959 1056
1057 void visitConstructorDefinition(ConstructorDefinition node) {
1058 if (!node.isAbstract) {
1059 node.initializers.forEach(visit);
1060 visit(node.body);
1061 }
1062 // Assign indices to unused parameters.
1063 for (Definition param in node.parameters) {
1064 if (param is Primitive) {
1065 allocate(param);
1066 }
1067 }
1068 }
1069
1070 void visitFieldInitializer(FieldInitializer node) {
1071 visit(node.body.body);
1072 }
1073
1074 void visitSuperInitializer(SuperInitializer node) {
1075 node.arguments.forEach((RunnableBody argument) => visit(argument.body));
1076 }
1077
960 void visitLetPrim(LetPrim node) { 1078 void visitLetPrim(LetPrim node) {
961 visit(node.body); 1079 visit(node.body);
962 release(node.primitive); 1080 release(node.primitive);
963 visit(node.primitive); 1081 visit(node.primitive);
964 } 1082 }
965 1083
966 void visitLetCont(LetCont node) { 1084 void visitLetCont(LetCont node) {
967 visit(node.continuation); 1085 visit(node.continuation);
968 visit(node.body); 1086 visit(node.body);
969 } 1087 }
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after
1058 } 1176 }
1059 1177
1060 // JavaScript specific nodes. 1178 // JavaScript specific nodes.
1061 1179
1062 void visitIdentical(Identical node) { 1180 void visitIdentical(Identical node) {
1063 visitReference(node.left); 1181 visitReference(node.left);
1064 visitReference(node.right); 1182 visitReference(node.right);
1065 } 1183 }
1066 } 1184 }
1067 1185
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698