| 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 |
| 11 import 'package:front_end/src/fasta/parser/error_kind.dart' show | 11 import 'package:front_end/src/fasta/parser/error_kind.dart' show |
| 12 ErrorKind; | 12 ErrorKind; |
| 13 | 13 |
| 14 import 'package:kernel/ast.dart'; | 14 import 'package:kernel/ast.dart'; |
| 15 | 15 |
| 16 import 'package:kernel/clone.dart' show | 16 import 'package:kernel/clone.dart' show |
| 17 CloneVisitor; | 17 CloneVisitor; |
| 18 | 18 |
| 19 import 'package:kernel/transformations/flags.dart' show | 19 import 'package:kernel/transformations/flags.dart' show |
| 20 TransformerFlag; | 20 TransformerFlag; |
| 21 | 21 |
| 22 import 'package:kernel/class_hierarchy.dart' show | 22 import 'package:kernel/class_hierarchy.dart' show |
| 23 ClassHierarchy; | 23 ClassHierarchy; |
| 24 | 24 |
| 25 import 'package:kernel/core_types.dart' show | 25 import 'package:kernel/core_types.dart' show |
| 26 CoreTypes; | 26 CoreTypes; |
| 27 | 27 |
| 28 import '../parser/dart_vm_native.dart' show |
| 29 skipNativeClause; |
| 30 |
| 28 import 'package:front_end/src/fasta/scanner/token.dart' show | 31 import 'package:front_end/src/fasta/scanner/token.dart' show |
| 29 BeginGroupToken, | 32 BeginGroupToken, |
| 30 Token, | 33 Token, |
| 31 isBinaryOperator, | 34 isBinaryOperator, |
| 32 isMinusOperator; | 35 isMinusOperator; |
| 33 | 36 |
| 34 import '../errors.dart' show | 37 import '../errors.dart' show |
| 35 InputError, | 38 InputError, |
| 36 internalError; | 39 internalError; |
| 37 | 40 |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 117 @override | 120 @override |
| 118 final CoreTypes coreTypes; | 121 final CoreTypes coreTypes; |
| 119 | 122 |
| 120 final bool isInstanceMember; | 123 final bool isInstanceMember; |
| 121 | 124 |
| 122 final Map<String, FieldInitializer> fieldInitializers = | 125 final Map<String, FieldInitializer> fieldInitializers = |
| 123 <String, FieldInitializer>{}; | 126 <String, FieldInitializer>{}; |
| 124 | 127 |
| 125 final Scope enclosingScope; | 128 final Scope enclosingScope; |
| 126 | 129 |
| 130 final bool isDartLibrary; |
| 131 |
| 127 Scope formalParameterScope; | 132 Scope formalParameterScope; |
| 128 | 133 |
| 129 bool isFirstIdentifier = false; | 134 bool isFirstIdentifier = false; |
| 130 | 135 |
| 131 bool hasParserError = false; | 136 bool hasParserError = false; |
| 132 | 137 |
| 133 bool inInitializer = false; | 138 bool inInitializer = false; |
| 134 | 139 |
| 135 bool inCatchClause = false; | 140 bool inCatchClause = false; |
| 136 | 141 |
| 137 int functionNestingLevel = 0; | 142 int functionNestingLevel = 0; |
| 138 | 143 |
| 139 Statement compileTimeErrorInTry; | 144 Statement compileTimeErrorInTry; |
| 140 | 145 |
| 141 Statement compileTimeErrorInLoopOrSwitch; | 146 Statement compileTimeErrorInLoopOrSwitch; |
| 142 | 147 |
| 143 Scope switchScope; | 148 Scope switchScope; |
| 144 | 149 |
| 145 CloneVisitor cloner; | 150 CloneVisitor cloner; |
| 146 | 151 |
| 147 BodyBuilder(this.library, this.member, Scope scope, this.formalParameterScope, | 152 /// Set to true each time we parse a native function body. It is reset in |
| 148 this.hierarchy, this.coreTypes, this.classBuilder, this.isInstanceMember) | 153 /// [handleInvalidFunctionBody] which is called immediately after. |
| 154 bool lastErrorWasNativeFunctionBody = false; |
| 155 |
| 156 BodyBuilder(KernelLibraryBuilder library, this.member, Scope scope, |
| 157 this.formalParameterScope, this.hierarchy, this.coreTypes, |
| 158 this.classBuilder, this.isInstanceMember) |
| 149 : enclosingScope = scope, | 159 : enclosingScope = scope, |
| 160 library = library, |
| 161 isDartLibrary = library.uri.scheme == "dart", |
| 150 super(scope); | 162 super(scope); |
| 151 | 163 |
| 152 bool get inConstructor { | 164 bool get inConstructor { |
| 153 return functionNestingLevel == 0 && member is KernelConstructorBuilder; | 165 return functionNestingLevel == 0 && member is KernelConstructorBuilder; |
| 154 } | 166 } |
| 155 | 167 |
| 156 bool get isInstanceContext { | 168 bool get isInstanceContext { |
| 157 return isInstanceMember || member is KernelConstructorBuilder; | 169 return isInstanceMember || member is KernelConstructorBuilder; |
| 158 } | 170 } |
| 159 | 171 |
| (...skipping 1974 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2134 void handleRecoverableError(Token token, ErrorKind kind, Map arguments) { | 2146 void handleRecoverableError(Token token, ErrorKind kind, Map arguments) { |
| 2135 super.handleRecoverableError(token, kind, arguments); | 2147 super.handleRecoverableError(token, kind, arguments); |
| 2136 if (!hasParserError) { | 2148 if (!hasParserError) { |
| 2137 print("$uri:${recoverableErrors.last}"); | 2149 print("$uri:${recoverableErrors.last}"); |
| 2138 } | 2150 } |
| 2139 hasParserError = true; | 2151 hasParserError = true; |
| 2140 } | 2152 } |
| 2141 | 2153 |
| 2142 @override | 2154 @override |
| 2143 Token handleUnrecoverableError(Token token, ErrorKind kind, Map arguments) { | 2155 Token handleUnrecoverableError(Token token, ErrorKind kind, Map arguments) { |
| 2144 if (kind == ErrorKind.UnexpectedToken) { | 2156 if (isDartLibrary && kind == ErrorKind.ExpectedFunctionBody) { |
| 2157 Token recover = skipNativeClause(token); |
| 2158 if (recover != null) { |
| 2159 buildNative(unescapeString(token.next.value)); |
| 2160 return recover; |
| 2161 } |
| 2162 } else if (kind == ErrorKind.UnexpectedToken) { |
| 2145 String expected = arguments["expected"]; | 2163 String expected = arguments["expected"]; |
| 2146 const List<String> trailing = const <String>[")", "}", ";", ","]; | 2164 const List<String> trailing = const <String>[")", "}", ";", ","]; |
| 2147 if (trailing.contains(token.stringValue) && trailing.contains(expected)) { | 2165 if (trailing.contains(token.stringValue) && trailing.contains(expected)) { |
| 2148 arguments.putIfAbsent("actual", () => token.value); | 2166 arguments.putIfAbsent("actual", () => token.value); |
| 2149 handleRecoverableError(token, ErrorKind.ExpectedButGot, arguments); | 2167 handleRecoverableError(token, ErrorKind.ExpectedButGot, arguments); |
| 2150 } | 2168 } |
| 2151 return token; | 2169 return token; |
| 2152 } | 2170 } |
| 2153 return super.handleUnrecoverableError(token, kind, arguments); | 2171 return super.handleUnrecoverableError(token, kind, arguments); |
| 2154 } | 2172 } |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2198 void handleOperator(Token token) { | 2216 void handleOperator(Token token) { |
| 2199 debugEvent("Operator"); | 2217 debugEvent("Operator"); |
| 2200 push(new Operator(token.stringValue)..fileOffset = token.charOffset); | 2218 push(new Operator(token.stringValue)..fileOffset = token.charOffset); |
| 2201 } | 2219 } |
| 2202 | 2220 |
| 2203 dynamic inputError(String message, [int charOffset = -1]) { | 2221 dynamic inputError(String message, [int charOffset = -1]) { |
| 2204 return errors.inputError(uri, charOffset, message); | 2222 return errors.inputError(uri, charOffset, message); |
| 2205 } | 2223 } |
| 2206 | 2224 |
| 2207 @override | 2225 @override |
| 2226 void handleInvalidFunctionBody(Token token) { |
| 2227 if (!lastErrorWasNativeFunctionBody) { |
| 2228 push(new Block(<Statement>[new InvalidStatement()])); |
| 2229 } |
| 2230 lastErrorWasNativeFunctionBody = false; |
| 2231 } |
| 2232 |
| 2233 void buildNative(String native) { |
| 2234 lastErrorWasNativeFunctionBody = true; |
| 2235 |
| 2236 // From dartk: |
| 2237 // |
| 2238 // currentMember.isExternal = true; |
| 2239 // currentMember.addAnnotation(new ast.ConstructorInvocation( |
| 2240 // scope.loader.getCoreClassConstructorReference('ExternalName', |
| 2241 // library: 'dart:_internal'), |
| 2242 // new ast.Arguments(<ast.Expression>[ |
| 2243 // new ast.StringLiteral(body.stringLiteral.stringValue) |
| 2244 // ]), |
| 2245 // isConst: true)); |
| 2246 push(new Block(<Statement>[new InvalidStatement()])); |
| 2247 } |
| 2248 |
| 2249 @override |
| 2208 void debugEvent(String name) { | 2250 void debugEvent(String name) { |
| 2209 // printEvent(name); | 2251 // printEvent(name); |
| 2210 } | 2252 } |
| 2211 } | 2253 } |
| 2212 | 2254 |
| 2213 // TODO(ahe): Shouldn't need to be an expression. | 2255 // TODO(ahe): Shouldn't need to be an expression. |
| 2214 class UnresolvedIdentifier extends InvalidExpression { | 2256 class UnresolvedIdentifier extends InvalidExpression { |
| 2215 final Name name; | 2257 final Name name; |
| 2216 | 2258 |
| 2217 UnresolvedIdentifier(this.name); | 2259 UnresolvedIdentifier(this.name); |
| (...skipping 414 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2632 } else if (node is TypeDeclarationBuilder) { | 2674 } else if (node is TypeDeclarationBuilder) { |
| 2633 return node.name; | 2675 return node.name; |
| 2634 } else if (node is PrefixBuilder) { | 2676 } else if (node is PrefixBuilder) { |
| 2635 return node.name; | 2677 return node.name; |
| 2636 } else if (node is ThisPropertyAccessor) { | 2678 } else if (node is ThisPropertyAccessor) { |
| 2637 return node.name.name; | 2679 return node.name.name; |
| 2638 } else { | 2680 } else { |
| 2639 return internalError("Unhandled: ${node.runtimeType}"); | 2681 return internalError("Unhandled: ${node.runtimeType}"); |
| 2640 } | 2682 } |
| 2641 } | 2683 } |
| OLD | NEW |