| OLD | NEW |
| 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file |
| 2 | 2 |
| 3 // for details. All rights reserved. Use of this source code is governed by a | 3 // for details. All rights reserved. Use of this source code is governed by a |
| 4 // BSD-style license that can be found in the LICENSE file. | 4 // BSD-style license that can be found in the LICENSE file. |
| 5 | 5 |
| 6 import 'dart:collection' show HashMap, HashSet; | 6 import 'dart:collection' show HashMap, HashSet; |
| 7 import 'dart:math' show min, max; | 7 import 'dart:math' show min, max; |
| 8 | 8 |
| 9 import 'package:analyzer/analyzer.dart' hide ConstantEvaluator; | 9 import 'package:analyzer/analyzer.dart' hide ConstantEvaluator; |
| 10 import 'package:analyzer/dart/ast/ast.dart'; | 10 import 'package:analyzer/dart/ast/ast.dart'; |
| (...skipping 565 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 576 } | 576 } |
| 577 } | 577 } |
| 578 | 578 |
| 579 @override | 579 @override |
| 580 visitCompilationUnit(CompilationUnit unit) { | 580 visitCompilationUnit(CompilationUnit unit) { |
| 581 // NOTE: this method isn't the right place to initialize | 581 // NOTE: this method isn't the right place to initialize |
| 582 // per-compilation-unit state. Declarations can be visited out of order, | 582 // per-compilation-unit state. Declarations can be visited out of order, |
| 583 // this is only to catch things that haven't been emitted yet. | 583 // this is only to catch things that haven't been emitted yet. |
| 584 // | 584 // |
| 585 // See _emitTypeDeclaration. | 585 // See _emitTypeDeclaration. |
| 586 _currentElements.add(unit.element); | 586 var library = unit.element.library; |
| 587 var isInternalSdk = isSdkInternalRuntime(currentLibrary); | 587 bool internalSdk = isSdkInternalRuntime(library); |
| 588 _currentElements.add(library); |
| 588 List<VariableDeclaration> fields; | 589 List<VariableDeclaration> fields; |
| 589 for (var declaration in unit.declarations) { | 590 for (var declaration in unit.declarations) { |
| 590 if (declaration is TopLevelVariableDeclaration) { | 591 if (declaration is TopLevelVariableDeclaration) { |
| 591 inferNullableTypes(declaration); | 592 inferNullableTypes(declaration); |
| 592 if (isInternalSdk && declaration.variables.isFinal) { | 593 if (internalSdk && declaration.variables.isFinal) { |
| 593 _emitInternalSdkFields(declaration.variables.variables); | 594 _emitInternalSdkFields(declaration.variables.variables); |
| 594 } else { | 595 } else { |
| 595 (fields ??= []).addAll(declaration.variables.variables); | 596 (fields ??= []).addAll(declaration.variables.variables); |
| 596 } | 597 } |
| 597 continue; | 598 continue; |
| 598 } | 599 } |
| 599 | 600 |
| 600 if (fields != null) { | 601 if (fields != null) { |
| 601 _emitTopLevelFields(fields); | 602 _emitTopLevelFields(fields); |
| 602 fields = null; | 603 fields = null; |
| 603 } | 604 } |
| 604 | 605 |
| 605 var element = declaration.element; | 606 var element = declaration.element; |
| 606 if (element is TypeDefiningElement) { | 607 if (element is TypeDefiningElement) { |
| 607 _emitTypeDeclaration(element); | 608 _emitTypeDeclaration(element); |
| 608 continue; | 609 continue; |
| 609 } | 610 } |
| 610 | 611 |
| 611 inferNullableTypes(declaration); | 612 inferNullableTypes(declaration); |
| 612 var item = _visit(declaration); | 613 var item = _visit(declaration); |
| 613 if (isInternalSdk && element is FunctionElement) { | 614 if (internalSdk && element is FunctionElement) { |
| 614 _internalSdkFunctions.add(item); | 615 _internalSdkFunctions.add(item); |
| 615 } else { | 616 } else { |
| 616 _moduleItems.add(item); | 617 _moduleItems.add(item); |
| 617 } | 618 } |
| 618 } | 619 } |
| 619 | 620 |
| 620 if (fields != null) _emitTopLevelFields(fields); | 621 if (fields != null) _emitTopLevelFields(fields); |
| 621 | 622 |
| 622 _currentElements.removeLast(); | 623 _currentElements.removeLast(); |
| 623 } | 624 } |
| (...skipping 1733 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2357 } else { | 2358 } else { |
| 2358 value = new JS.LiteralNull(); | 2359 value = new JS.LiteralNull(); |
| 2359 } | 2360 } |
| 2360 fields[element] = value; | 2361 fields[element] = value; |
| 2361 }); | 2362 }); |
| 2362 | 2363 |
| 2363 var body = <JS.Statement>[]; | 2364 var body = <JS.Statement>[]; |
| 2364 fields.forEach((FieldElement e, JS.Expression initialValue) { | 2365 fields.forEach((FieldElement e, JS.Expression initialValue) { |
| 2365 JS.Expression access = | 2366 JS.Expression access = |
| 2366 _classProperties.virtualFields[e] ?? _declareMemberName(e.getter); | 2367 _classProperties.virtualFields[e] ?? _declareMemberName(e.getter); |
| 2367 body.add(initialValue | 2368 body.add(js.statement('this.# = #;', [access, initialValue])); |
| 2368 .toAssignExpression(js.call('this.#', [access])) | |
| 2369 .toStatement()); | |
| 2370 }); | 2369 }); |
| 2371 | 2370 |
| 2372 return _statement(body); | 2371 return _statement(body); |
| 2373 } | 2372 } |
| 2374 | 2373 |
| 2375 FormalParameterList _parametersOf(node) { | 2374 FormalParameterList _parametersOf(node) { |
| 2376 // TODO(jmesserly): clean this up. If we can model ES6 spread/rest args, we | 2375 // TODO(jmesserly): clean this up. If we can model ES6 spread/rest args, we |
| 2377 // could handle argument initializers more consistently in a separate | 2376 // could handle argument initializers more consistently in a separate |
| 2378 // lowering pass. | 2377 // lowering pass. |
| 2379 if (node is ConstructorDeclaration) return node.parameters; | 2378 if (node is ConstructorDeclaration) return node.parameters; |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2468 fn = _emitNativeFunctionBody(node); | 2467 fn = _emitNativeFunctionBody(node); |
| 2469 } else { | 2468 } else { |
| 2470 fn = _emitFunctionBody(node.element, node.parameters, node.body); | 2469 fn = _emitFunctionBody(node.element, node.parameters, node.body); |
| 2471 } | 2470 } |
| 2472 | 2471 |
| 2473 return annotate( | 2472 return annotate( |
| 2474 new JS.Method(_declareMemberName(node.element), fn, | 2473 new JS.Method(_declareMemberName(node.element), fn, |
| 2475 isGetter: node.isGetter, | 2474 isGetter: node.isGetter, |
| 2476 isSetter: node.isSetter, | 2475 isSetter: node.isSetter, |
| 2477 isStatic: node.isStatic), | 2476 isStatic: node.isStatic), |
| 2478 null, // don't annotate as this breaks stepping for one-line functions. | 2477 node, |
| 2479 node.element); | 2478 node.element); |
| 2480 } | 2479 } |
| 2481 | 2480 |
| 2482 /// Transform the function so the last parameter is always returned. | 2481 /// Transform the function so the last parameter is always returned. |
| 2483 /// | 2482 /// |
| 2484 /// This is useful for indexed set methods, which otherwise would not have | 2483 /// This is useful for indexed set methods, which otherwise would not have |
| 2485 /// the right return value in JS. | 2484 /// the right return value in JS. |
| 2486 JS.Block _alwaysReturnLastParameter(JS.Block body, JS.Parameter lastParam) { | 2485 JS.Block _alwaysReturnLastParameter(JS.Block body, JS.Parameter lastParam) { |
| 2487 JS.Statement blockBody = body; | 2486 JS.Statement blockBody = body; |
| 2488 if (JS.Return.foundIn(body)) { | 2487 if (JS.Return.foundIn(body)) { |
| (...skipping 3491 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 5980 if (targetIdentifier.staticElement is! PrefixElement) return false; | 5979 if (targetIdentifier.staticElement is! PrefixElement) return false; |
| 5981 var prefix = targetIdentifier.staticElement as PrefixElement; | 5980 var prefix = targetIdentifier.staticElement as PrefixElement; |
| 5982 | 5981 |
| 5983 // The library the prefix is referring to must come from a deferred import. | 5982 // The library the prefix is referring to must come from a deferred import. |
| 5984 var containingLibrary = resolutionMap | 5983 var containingLibrary = resolutionMap |
| 5985 .elementDeclaredByCompilationUnit(target.root as CompilationUnit) | 5984 .elementDeclaredByCompilationUnit(target.root as CompilationUnit) |
| 5986 .library; | 5985 .library; |
| 5987 var imports = containingLibrary.getImportsWithPrefix(prefix); | 5986 var imports = containingLibrary.getImportsWithPrefix(prefix); |
| 5988 return imports.length == 1 && imports[0].isDeferred; | 5987 return imports.length == 1 && imports[0].isDeferred; |
| 5989 } | 5988 } |
| OLD | NEW |