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

Side by Side Diff: pkg/front_end/lib/src/fasta/kernel/body_builder.dart

Issue 2769113004: Setup type and modifiers of local variables on creation. (Closed)
Patch Set: Address review comments. Created 3 years, 9 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
« no previous file with comments | « no previous file | pkg/front_end/lib/src/fasta/kernel/kernel_variable_builder.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2016, 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 fasta.body_builder; 5 library fasta.body_builder;
6 6
7 import '../parser/parser.dart' show FormalParameterType, optional; 7 import '../parser/parser.dart' show FormalParameterType, optional;
8 8
9 import '../parser/error_kind.dart' show ErrorKind; 9 import '../parser/error_kind.dart' show ErrorKind;
10 10
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after
119 Statement compileTimeErrorInTry; 119 Statement compileTimeErrorInTry;
120 120
121 Statement compileTimeErrorInLoopOrSwitch; 121 Statement compileTimeErrorInLoopOrSwitch;
122 122
123 Scope switchScope; 123 Scope switchScope;
124 124
125 CloneVisitor cloner; 125 CloneVisitor cloner;
126 126
127 bool constantExpressionRequired = false; 127 bool constantExpressionRequired = false;
128 128
129 DartType currentLocalVariableType;
130
131 // Using non-null value to initialize this field based on performance advice
132 // from VM engineers. TODO(ahe): Does this still apply?
133 int currentLocalVariableModifiers = -1;
134
129 BodyBuilder( 135 BodyBuilder(
130 KernelLibraryBuilder library, 136 KernelLibraryBuilder library,
131 this.member, 137 this.member,
132 Scope scope, 138 Scope scope,
133 this.formalParameterScope, 139 this.formalParameterScope,
134 this.hierarchy, 140 this.hierarchy,
135 this.coreTypes, 141 this.coreTypes,
136 this.classBuilder, 142 this.classBuilder,
137 this.isInstanceMember, 143 this.isInstanceMember,
138 this.uri) 144 this.uri)
(...skipping 753 matching lines...) Expand 10 before | Expand all | Expand 10 after
892 Statement elsePart = popStatementIfNotNull(elseToken); 898 Statement elsePart = popStatementIfNotNull(elseToken);
893 Statement thenPart = popStatement(); 899 Statement thenPart = popStatement();
894 Expression condition = popForValue(); 900 Expression condition = popForValue();
895 push(new IfStatement(condition, thenPart, elsePart)); 901 push(new IfStatement(condition, thenPart, elsePart));
896 } 902 }
897 903
898 @override 904 @override
899 void endVariableInitializer(Token assignmentOperator) { 905 void endVariableInitializer(Token assignmentOperator) {
900 debugEvent("VariableInitializer"); 906 debugEvent("VariableInitializer");
901 assert(assignmentOperator.stringValue == "="); 907 assert(assignmentOperator.stringValue == "=");
902 Expression initializer = popForValue(); 908 pushNewLocalVariable(popForValue(),
903 Identifier identifier = pop(); 909 equalsCharOffset: assignmentOperator.charOffset);
904 push(new VariableDeclaration(identifier.name, initializer: initializer)
905 ..fileEqualsOffset = assignmentOperator.charOffset);
906 } 910 }
907 911
908 @override 912 @override
909 void handleNoVariableInitializer(Token token) { 913 void handleNoVariableInitializer(Token token) {
910 debugEvent("NoVariableInitializer"); 914 debugEvent("NoVariableInitializer");
915 pushNewLocalVariable(null);
916 }
917
918 void pushNewLocalVariable(Expression initializer,
919 {int equalsCharOffset: TreeNode.noOffset}) {
920 Identifier identifier = pop();
921 assert(currentLocalVariableModifiers != -1);
922 bool isConst = (currentLocalVariableModifiers & constMask) != 0;
923 bool isFinal = (currentLocalVariableModifiers & finalMask) != 0;
924 assert(isConst == constantExpressionRequired);
925 push(new VariableDeclaration(identifier.name,
926 initializer: initializer,
927 type: currentLocalVariableType ?? const DynamicType(),
928 isFinal: isFinal,
929 isConst: isConst)..fileEqualsOffset = equalsCharOffset);
911 } 930 }
912 931
913 @override 932 @override
914 void endFieldInitializer(Token assignmentOperator) { 933 void endFieldInitializer(Token assignmentOperator) {
915 debugEvent("FieldInitializer"); 934 debugEvent("FieldInitializer");
916 assert(assignmentOperator.stringValue == "="); 935 assert(assignmentOperator.stringValue == "=");
917 push(popForValue()); 936 push(popForValue());
918 } 937 }
919 938
920 @override 939 @override
921 void handleNoFieldInitializer(Token token) { 940 void handleNoFieldInitializer(Token token) {
922 debugEvent("NoFieldInitializer"); 941 debugEvent("NoFieldInitializer");
923 push(NullValue.FieldInitializer); 942 push(NullValue.FieldInitializer);
924 } 943 }
925 944
926 @override 945 @override
927 void endInitializedIdentifier(Token nameToken) { 946 void endInitializedIdentifier(Token nameToken) {
928 // TODO(ahe): Use [InitializedIdentifier] here? 947 // TODO(ahe): Use [InitializedIdentifier] here?
929 debugEvent("InitializedIdentifier"); 948 debugEvent("InitializedIdentifier");
930 TreeNode node = pop(); 949 VariableDeclaration variable = pop();
931 VariableDeclaration variable;
932 if (node is VariableDeclaration) {
933 variable = node;
934 } else if (node is Identifier) {
935 variable = new VariableDeclaration(node.name);
936 } else {
937 internalError("unhandled identifier: ${node.runtimeType}");
938 }
939 variable.fileOffset = nameToken.charOffset; 950 variable.fileOffset = nameToken.charOffset;
940 push(variable); 951 push(variable);
941 scope[variable.name] = new KernelVariableBuilder( 952 scope[variable.name] = new KernelVariableBuilder(
942 variable, member ?? classBuilder ?? library, uri); 953 variable, member ?? classBuilder ?? library, uri);
943 } 954 }
944 955
945 @override 956 @override
957 void beginVariablesDeclaration(Token token) {
958 debugEvent("beginVariablesDeclaration");
959 DartType type = pop();
960 int modifiers = Modifier.validate(pop());
961 super.push(currentLocalVariableModifiers);
962 super.push(currentLocalVariableType ?? NullValue.Type);
963 currentLocalVariableType = type;
964 currentLocalVariableModifiers = modifiers;
965 super.push(constantExpressionRequired);
966 constantExpressionRequired = (modifiers & constMask) != 0;
967 }
968
969 @override
946 void endVariablesDeclaration(int count, Token endToken) { 970 void endVariablesDeclaration(int count, Token endToken) {
947 debugEvent("VariablesDeclaration"); 971 debugEvent("VariablesDeclaration");
948 List<VariableDeclaration> variables = popList(count); 972 List<VariableDeclaration> variables = popList(count);
949 DartType type = pop(); 973 constantExpressionRequired = pop();
950 int modifiers = Modifier.validate(pop()); 974 currentLocalVariableType = pop();
951 bool isConst = (modifiers & constMask) != 0; 975 currentLocalVariableModifiers = pop();
952 bool isFinal = (modifiers & finalMask) != 0;
953 if (type != null || isConst || isFinal) {
954 type ??= const DynamicType();
955 for (VariableDeclaration variable in variables) {
956 variable
957 ..type = type
958 ..isConst = isConst
959 ..isFinal = isFinal;
960 }
961 }
962 if (variables.length != 1) { 976 if (variables.length != 1) {
963 push(variables); 977 push(variables);
964 } else { 978 } else {
965 push(variables.single); 979 push(variables.single);
966 } 980 }
967 } 981 }
968 982
969 @override 983 @override
970 void endBlock(int count, Token beginToken, Token endToken) { 984 void endBlock(int count, Token beginToken, Token endToken) {
971 debugEvent("Block"); 985 debugEvent("Block");
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after
1098 void handleLiteralBool(Token token) { 1112 void handleLiteralBool(Token token) {
1099 debugEvent("LiteralBool"); 1113 debugEvent("LiteralBool");
1100 bool value = optional("true", token); 1114 bool value = optional("true", token);
1101 assert(value || optional("false", token)); 1115 assert(value || optional("false", token));
1102 push(new BoolLiteral(value)..fileOffset = token.charOffset); 1116 push(new BoolLiteral(value)..fileOffset = token.charOffset);
1103 } 1117 }
1104 1118
1105 @override 1119 @override
1106 void handleLiteralDouble(Token token) { 1120 void handleLiteralDouble(Token token) {
1107 debugEvent("LiteralDouble"); 1121 debugEvent("LiteralDouble");
1108 push(new DoubleLiteral(double.parse(token.lexeme))..fileOffset = token.charO ffset); 1122 push(new DoubleLiteral(double.parse(token.lexeme))
1123 ..fileOffset = token.charOffset);
1109 } 1124 }
1110 1125
1111 @override 1126 @override
1112 void handleLiteralNull(Token token) { 1127 void handleLiteralNull(Token token) {
1113 debugEvent("LiteralNull"); 1128 debugEvent("LiteralNull");
1114 push(new NullLiteral()..fileOffset = token.charOffset); 1129 push(new NullLiteral()..fileOffset = token.charOffset);
1115 } 1130 }
1116 1131
1117 @override 1132 @override
1118 void handleLiteralMap( 1133 void handleLiteralMap(
(...skipping 1663 matching lines...) Expand 10 before | Expand all | Expand 10 after
2782 } else if (node is PrefixBuilder) { 2797 } else if (node is PrefixBuilder) {
2783 return node.name; 2798 return node.name;
2784 } else if (node is ThisAccessor) { 2799 } else if (node is ThisAccessor) {
2785 return node.isSuper ? "super" : "this"; 2800 return node.isSuper ? "super" : "this";
2786 } else if (node is BuilderAccessor) { 2801 } else if (node is BuilderAccessor) {
2787 return node.plainNameForRead; 2802 return node.plainNameForRead;
2788 } else { 2803 } else {
2789 return internalError("Unhandled: ${node.runtimeType}"); 2804 return internalError("Unhandled: ${node.runtimeType}");
2790 } 2805 }
2791 } 2806 }
OLDNEW
« no previous file with comments | « no previous file | pkg/front_end/lib/src/fasta/kernel/kernel_variable_builder.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698