| OLD | NEW |
| 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 'package:front_end/src/fasta/parser/parser.dart' show | 7 import 'package:front_end/src/fasta/parser/parser.dart' show |
| 8 FormalParameterType, | 8 FormalParameterType, |
| 9 optional; | 9 optional; |
| 10 | 10 |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 72 import '../modifier.dart' show | 72 import '../modifier.dart' show |
| 73 Modifier, | 73 Modifier, |
| 74 constMask, | 74 constMask, |
| 75 finalMask; | 75 finalMask; |
| 76 | 76 |
| 77 import 'redirecting_factory_body.dart' show | 77 import 'redirecting_factory_body.dart' show |
| 78 getRedirectionTarget; | 78 getRedirectionTarget; |
| 79 | 79 |
| 80 import 'kernel_builder.dart'; | 80 import 'kernel_builder.dart'; |
| 81 | 81 |
| 82 const bool showNits = false; | |
| 83 | |
| 84 final Name callName = new Name("call"); | 82 final Name callName = new Name("call"); |
| 85 | 83 |
| 86 final Name plusName = new Name("+"); | 84 final Name plusName = new Name("+"); |
| 87 | 85 |
| 88 final Name minusName = new Name("-"); | 86 final Name minusName = new Name("-"); |
| 89 | 87 |
| 90 final Name multiplyName = new Name("*"); | 88 final Name multiplyName = new Name("*"); |
| 91 | 89 |
| 92 final Name divisionName = new Name("/"); | 90 final Name divisionName = new Name("/"); |
| 93 | 91 |
| (...skipping 25 matching lines...) Expand all Loading... |
| 119 | 117 |
| 120 final bool isInstanceMember; | 118 final bool isInstanceMember; |
| 121 | 119 |
| 122 final Map<String, FieldInitializer> fieldInitializers = | 120 final Map<String, FieldInitializer> fieldInitializers = |
| 123 <String, FieldInitializer>{}; | 121 <String, FieldInitializer>{}; |
| 124 | 122 |
| 125 final Scope enclosingScope; | 123 final Scope enclosingScope; |
| 126 | 124 |
| 127 final bool isDartLibrary; | 125 final bool isDartLibrary; |
| 128 | 126 |
| 127 @override |
| 128 final Uri uri; |
| 129 |
| 129 Scope formalParameterScope; | 130 Scope formalParameterScope; |
| 130 | 131 |
| 131 bool isFirstIdentifier = false; | 132 bool isFirstIdentifier = false; |
| 132 | 133 |
| 133 bool hasParserError = false; | 134 bool hasParserError = false; |
| 134 | 135 |
| 135 bool inInitializer = false; | 136 bool inInitializer = false; |
| 136 | 137 |
| 137 bool inCatchClause = false; | 138 bool inCatchClause = false; |
| 138 | 139 |
| 139 int functionNestingLevel = 0; | 140 int functionNestingLevel = 0; |
| 140 | 141 |
| 141 Statement compileTimeErrorInTry; | 142 Statement compileTimeErrorInTry; |
| 142 | 143 |
| 143 Statement compileTimeErrorInLoopOrSwitch; | 144 Statement compileTimeErrorInLoopOrSwitch; |
| 144 | 145 |
| 145 Scope switchScope; | 146 Scope switchScope; |
| 146 | 147 |
| 147 CloneVisitor cloner; | 148 CloneVisitor cloner; |
| 148 | 149 |
| 149 BodyBuilder(KernelLibraryBuilder library, this.member, Scope scope, | 150 BodyBuilder(KernelLibraryBuilder library, this.member, Scope scope, |
| 150 this.formalParameterScope, this.hierarchy, this.coreTypes, | 151 this.formalParameterScope, this.hierarchy, this.coreTypes, |
| 151 this.classBuilder, this.isInstanceMember) | 152 this.classBuilder, this.isInstanceMember, this.uri) |
| 152 : enclosingScope = scope, | 153 : enclosingScope = scope, |
| 153 library = library, | 154 library = library, |
| 154 isDartLibrary = library.uri.scheme == "dart", | 155 isDartLibrary = library.uri.scheme == "dart", |
| 155 super(scope); | 156 super(scope); |
| 156 | 157 |
| 157 bool get inConstructor { | 158 bool get inConstructor { |
| 158 return functionNestingLevel == 0 && member is KernelConstructorBuilder; | 159 return functionNestingLevel == 0 && member is KernelConstructorBuilder; |
| 159 } | 160 } |
| 160 | 161 |
| 161 bool get isInstanceContext { | 162 bool get isInstanceContext { |
| (...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 266 void enterSwitchScope() { | 267 void enterSwitchScope() { |
| 267 push(switchScope ?? NullValue.SwitchScope); | 268 push(switchScope ?? NullValue.SwitchScope); |
| 268 switchScope = scope; | 269 switchScope = scope; |
| 269 } | 270 } |
| 270 | 271 |
| 271 void exitSwitchScope() { | 272 void exitSwitchScope() { |
| 272 switchScope = pop(); | 273 switchScope = pop(); |
| 273 } | 274 } |
| 274 | 275 |
| 275 @override | 276 @override |
| 276 Uri get uri => library.fileUri ?? library.uri; | |
| 277 | |
| 278 @override | |
| 279 JumpTarget createJumpTarget(JumpTargetKind kind, int charOffset) { | 277 JumpTarget createJumpTarget(JumpTargetKind kind, int charOffset) { |
| 280 return new JumpTarget(kind, member, charOffset); | 278 return new JumpTarget(kind, member, charOffset); |
| 281 } | 279 } |
| 282 | 280 |
| 283 @override | 281 @override |
| 284 void endMetadata(Token beginToken, Token periodBeforeName, Token endToken) { | 282 void endMetadata(Token beginToken, Token periodBeforeName, Token endToken) { |
| 285 debugEvent("Metadata"); | 283 debugEvent("Metadata"); |
| 286 pop(); // Arguments. | 284 pop(); // Arguments. |
| 287 popIfNotNull(periodBeforeName); // Postfix. | 285 popIfNotNull(periodBeforeName); // Postfix. |
| 288 pop(); // Type arguments. | 286 pop(); // Type arguments. |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 333 field.initializer = initializer; | 331 field.initializer = initializer; |
| 334 } | 332 } |
| 335 } | 333 } |
| 336 pop(); // Type. | 334 pop(); // Type. |
| 337 pop(); // Modifiers. | 335 pop(); // Modifiers. |
| 338 } | 336 } |
| 339 | 337 |
| 340 @override | 338 @override |
| 341 void endMember() { | 339 void endMember() { |
| 342 debugEvent("Member"); | 340 debugEvent("Member"); |
| 343 checkEmpty(); | 341 checkEmpty(-1); |
| 344 } | 342 } |
| 345 | 343 |
| 346 @override | 344 @override |
| 347 void endFunctionBody(int count, Token beginToken, Token endToken) { | 345 void endFunctionBody(int count, Token beginToken, Token endToken) { |
| 348 debugEvent("FunctionBody"); | 346 debugEvent("FunctionBody"); |
| 349 if (beginToken == null) { | 347 if (beginToken == null) { |
| 350 assert(count == 0); | 348 assert(count == 0); |
| 351 push(NullValue.Block); | 349 push(NullValue.Block); |
| 352 } else { | 350 } else { |
| 353 Block block = popBlock(count); | 351 Block block = popBlock(count); |
| (...skipping 792 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1146 } | 1144 } |
| 1147 | 1145 |
| 1148 DartType toKernelType(String name, List<DartType> arguments, int charOffset) { | 1146 DartType toKernelType(String name, List<DartType> arguments, int charOffset) { |
| 1149 if (identical(name, "void")) return const VoidType(); | 1147 if (identical(name, "void")) return const VoidType(); |
| 1150 if (identical(name, "dynamic")) return const DynamicType(); | 1148 if (identical(name, "dynamic")) return const DynamicType(); |
| 1151 Builder builder = scope.lookup(name, charOffset, uri); | 1149 Builder builder = scope.lookup(name, charOffset, uri); |
| 1152 if (builder is TypeDeclarationBuilder) { | 1150 if (builder is TypeDeclarationBuilder) { |
| 1153 return builder.buildTypesWithBuiltArguments(arguments); | 1151 return builder.buildTypesWithBuiltArguments(arguments); |
| 1154 } | 1152 } |
| 1155 if (builder == null) { | 1153 if (builder == null) { |
| 1156 print("$uri: Type not found: $name"); | 1154 warning("Type not found: '$name'.", charOffset); |
| 1157 } else { | 1155 } else { |
| 1158 print("$uri: Not a type: $name"); | 1156 warning("Not a type: '$name'.", charOffset); |
| 1159 } | 1157 } |
| 1160 // TODO(ahe): Create an error somehow. | 1158 // TODO(ahe): Create an error somehow. |
| 1161 return const DynamicType(); | 1159 return const DynamicType(); |
| 1162 } | 1160 } |
| 1163 | 1161 |
| 1164 @override | 1162 @override |
| 1165 void endType(Token beginToken, Token endToken) { | 1163 void endType(Token beginToken, Token endToken) { |
| 1166 // TODO(ahe): The scope is wrong for return types of generic functions. | 1164 // TODO(ahe): The scope is wrong for return types of generic functions. |
| 1167 debugEvent("Type"); | 1165 debugEvent("Type"); |
| 1168 List<DartType> arguments = pop(); | 1166 List<DartType> arguments = pop(); |
| (...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1293 "'${name.name}' isn't a field in this class."); | 1291 "'${name.name}' isn't a field in this class."); |
| 1294 thisKeyword = null; | 1292 thisKeyword = null; |
| 1295 } | 1293 } |
| 1296 } else if (thisKeyword == null) { | 1294 } else if (thisKeyword == null) { |
| 1297 variable = builder.build(); | 1295 variable = builder.build(); |
| 1298 variable.initializer = name.initializer; | 1296 variable.initializer = name.initializer; |
| 1299 } else if (builder.isField && builder.parent == classBuilder) { | 1297 } else if (builder.isField && builder.parent == classBuilder) { |
| 1300 FieldBuilder field = builder; | 1298 FieldBuilder field = builder; |
| 1301 if (type != null) { | 1299 if (type != null) { |
| 1302 nit("Ignoring type on 'this' parameter '${name.name}'.", | 1300 nit("Ignoring type on 'this' parameter '${name.name}'.", |
| 1303 name.fileOffset); | 1301 thisKeyword.charOffset); |
| 1304 } | 1302 } |
| 1305 type = field.target.type ?? const DynamicType(); | 1303 type = field.target.type ?? const DynamicType(); |
| 1306 variable = new VariableDeclaration(name.name, type: type, | 1304 variable = new VariableDeclaration(name.name, type: type, |
| 1307 initializer: name.initializer); | 1305 initializer: name.initializer); |
| 1308 } else { | 1306 } else { |
| 1309 addCompileTimeError(name.fileOffset, | 1307 addCompileTimeError(name.fileOffset, |
| 1310 "'${name.name}' isn't a field in this class."); | 1308 "'${name.name}' isn't a field in this class."); |
| 1311 } | 1309 } |
| 1312 } | 1310 } |
| 1313 variable ??= new VariableDeclaration(name.name, | 1311 variable ??= new VariableDeclaration(name.name, |
| (...skipping 846 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2160 void handleModifiers(int count) { | 2158 void handleModifiers(int count) { |
| 2161 debugEvent("Modifiers"); | 2159 debugEvent("Modifiers"); |
| 2162 // TODO(ahe): Copied from outline_builder.dart. | 2160 // TODO(ahe): Copied from outline_builder.dart. |
| 2163 push(popList(count) ?? NullValue.Modifiers); | 2161 push(popList(count) ?? NullValue.Modifiers); |
| 2164 } | 2162 } |
| 2165 | 2163 |
| 2166 @override | 2164 @override |
| 2167 void handleRecoverableError(Token token, ErrorKind kind, Map arguments) { | 2165 void handleRecoverableError(Token token, ErrorKind kind, Map arguments) { |
| 2168 super.handleRecoverableError(token, kind, arguments); | 2166 super.handleRecoverableError(token, kind, arguments); |
| 2169 if (!hasParserError) { | 2167 if (!hasParserError) { |
| 2170 print("$uri:${recoverableErrors.last}"); | 2168 print(new InputError(uri, recoverableErrors.last.beginOffset, |
| 2169 recoverableErrors.last.kind).format()); |
| 2171 } | 2170 } |
| 2172 hasParserError = true; | 2171 hasParserError = true; |
| 2173 } | 2172 } |
| 2174 | 2173 |
| 2175 @override | 2174 @override |
| 2176 Token handleUnrecoverableError(Token token, ErrorKind kind, Map arguments) { | 2175 Token handleUnrecoverableError(Token token, ErrorKind kind, Map arguments) { |
| 2177 if (isDartLibrary && kind == ErrorKind.ExpectedFunctionBody) { | 2176 if (isDartLibrary && kind == ErrorKind.ExpectedFunctionBody) { |
| 2178 Token recover = skipNativeClause(token); | 2177 Token recover = skipNativeClause(token); |
| 2179 if (recover != null) return recover; | 2178 if (recover != null) return recover; |
| 2180 } else if (kind == ErrorKind.UnexpectedToken) { | 2179 } else if (kind == ErrorKind.UnexpectedToken) { |
| 2181 String expected = arguments["expected"]; | 2180 String expected = arguments["expected"]; |
| 2182 const List<String> trailing = const <String>[")", "}", ";", ","]; | 2181 const List<String> trailing = const <String>[")", "}", ";", ","]; |
| 2183 if (trailing.contains(token.stringValue) && trailing.contains(expected)) { | 2182 if (trailing.contains(token.stringValue) && trailing.contains(expected)) { |
| 2184 arguments.putIfAbsent("actual", () => token.value); | 2183 arguments.putIfAbsent("actual", () => token.value); |
| 2185 handleRecoverableError(token, ErrorKind.ExpectedButGot, arguments); | 2184 handleRecoverableError(token, ErrorKind.ExpectedButGot, arguments); |
| 2186 } | 2185 } |
| 2187 return token; | 2186 return token; |
| 2188 } | 2187 } |
| 2189 return super.handleUnrecoverableError(token, kind, arguments); | 2188 return super.handleUnrecoverableError(token, kind, arguments); |
| 2190 } | 2189 } |
| 2191 | 2190 |
| 2192 void warning(error, [int charOffset = -1]) { | |
| 2193 String message = new InputError(uri, charOffset, error).format(); | |
| 2194 print(message); | |
| 2195 } | |
| 2196 | |
| 2197 void nit(error, [int charOffset = -1]) { | |
| 2198 if (!showNits) return; | |
| 2199 String message = new InputError(uri, charOffset, error).format(); | |
| 2200 print(message); | |
| 2201 } | |
| 2202 | |
| 2203 @override | 2191 @override |
| 2204 Expression buildCompileTimeError(error, [int charOffset = -1]) { | 2192 Expression buildCompileTimeError(error, [int charOffset = -1]) { |
| 2205 String message = new InputError(uri, charOffset, error).format(); | 2193 String message = new InputError(uri, charOffset, error).format(); |
| 2206 print(message); | 2194 print(message); |
| 2207 Builder constructor = library.loader.getCompileTimeError(); | 2195 Builder constructor = library.loader.getCompileTimeError(); |
| 2208 return new Throw( | 2196 return new Throw( |
| 2209 buildStaticInvocation(constructor.target, | 2197 buildStaticInvocation(constructor.target, |
| 2210 new Arguments(<Expression>[new StringLiteral(message)]), | 2198 new Arguments(<Expression>[new StringLiteral(message)]), |
| 2211 isConst: false)); // TODO(ahe): Make this const. | 2199 isConst: false)); // TODO(ahe): Make this const. |
| 2212 } | 2200 } |
| (...skipping 473 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2686 } else if (node is TypeDeclarationBuilder) { | 2674 } else if (node is TypeDeclarationBuilder) { |
| 2687 return node.name; | 2675 return node.name; |
| 2688 } else if (node is PrefixBuilder) { | 2676 } else if (node is PrefixBuilder) { |
| 2689 return node.name; | 2677 return node.name; |
| 2690 } else if (node is ThisPropertyAccessor) { | 2678 } else if (node is ThisPropertyAccessor) { |
| 2691 return node.name.name; | 2679 return node.name.name; |
| 2692 } else { | 2680 } else { |
| 2693 return internalError("Unhandled: ${node.runtimeType}"); | 2681 return internalError("Unhandled: ${node.runtimeType}"); |
| 2694 } | 2682 } |
| 2695 } | 2683 } |
| OLD | NEW |