| 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 // 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 import 'dart:collection' show HashMap; | 5 import 'dart:collection' show HashMap; |
| 6 | 6 |
| 7 import 'package:analyzer/dart/ast/ast.dart'; | 7 import 'package:analyzer/dart/ast/ast.dart'; |
| 8 import 'package:analyzer/dart/element/element.dart'; | 8 import 'package:analyzer/dart/element/element.dart'; |
| 9 import 'package:func/func.dart'; | 9 import 'package:func/func.dart'; |
| 10 import '../js_ast/js_ast.dart' as JS; | 10 import '../js_ast/js_ast.dart' as JS; |
| (...skipping 25 matching lines...) Expand all Loading... |
| 36 bool isLoaded(Element e) => !_declarationNodes.containsKey(e); | 36 bool isLoaded(Element e) => !_declarationNodes.containsKey(e); |
| 37 | 37 |
| 38 /// True if the element is currently being loaded. | 38 /// True if the element is currently being loaded. |
| 39 bool _isLoading(Element e) => _currentElements.contains(e); | 39 bool _isLoading(Element e) => _currentElements.contains(e); |
| 40 | 40 |
| 41 /// Start generating top-level code for the element [e]. | 41 /// Start generating top-level code for the element [e]. |
| 42 /// | 42 /// |
| 43 /// Subsequent [emitDeclaration] calls will cause those elements to be | 43 /// Subsequent [emitDeclaration] calls will cause those elements to be |
| 44 /// generated before this one, until [finishTopLevel] is called. | 44 /// generated before this one, until [finishTopLevel] is called. |
| 45 void startTopLevel(Element e) { | 45 void startTopLevel(Element e) { |
| 46 assert(identical(e, currentElement)); | 46 assert(e == currentElement); |
| 47 _topLevelElements.add(e); | 47 _topLevelElements.add(e); |
| 48 } | 48 } |
| 49 | 49 |
| 50 /// Finishes the top-level code for the element [e]. | 50 /// Finishes the top-level code for the element [e]. |
| 51 void finishTopLevel(Element e) { | 51 void finishTopLevel(Element e) { |
| 52 var last = _topLevelElements.removeLast(); | 52 var last = _topLevelElements.removeLast(); |
| 53 assert(identical(e, last)); | 53 assert(e == last); |
| 54 } | 54 } |
| 55 | 55 |
| 56 /// Starts recording calls to [declareBeforeUse], until | 56 /// Starts recording calls to [declareBeforeUse], until |
| 57 /// [finishCheckingReferences] is called. | 57 /// [finishCheckingReferences] is called. |
| 58 void startCheckingReferences() { | 58 void startCheckingReferences() { |
| 59 // This function should not be reentrant, and we should not current be | 59 // This function should not be reentrant, and we should not current be |
| 60 // emitting top-level code. | 60 // emitting top-level code. |
| 61 assert(_checkReferences == null); | 61 assert(_checkReferences == null); |
| 62 assert(_topLevelElements.isEmpty || | 62 assert(_topLevelElements.isEmpty || |
| 63 !identical(currentElement, _topLevelElements.last)); | 63 currentElement != _topLevelElements.last); |
| 64 // Assume true until proven otherwise | 64 // Assume true until proven otherwise |
| 65 _checkReferences = true; | 65 _checkReferences = true; |
| 66 } | 66 } |
| 67 | 67 |
| 68 /// Finishes recording references, and returns `true` if all referenced | 68 /// Finishes recording references, and returns `true` if all referenced |
| 69 /// items were loaded (or if no items were referenced). | 69 /// items were loaded (or if no items were referenced). |
| 70 bool finishCheckingReferences() { | 70 bool finishCheckingReferences() { |
| 71 var result = _checkReferences; | 71 var result = _checkReferences; |
| 72 _checkReferences = null; | 72 _checkReferences = null; |
| 73 return result; | 73 return result; |
| 74 } | 74 } |
| 75 | 75 |
| 76 /// Ensures a top-level declaration is generated, and returns `true` if it | 76 /// Ensures a top-level declaration is generated, and returns `true` if it |
| 77 /// is part of the current module. | 77 /// is part of the current module. |
| 78 /*=T*/ emitDeclaration/*<T extends JS.Node>*/( | 78 /*=T*/ emitDeclaration/*<T extends JS.Node>*/( |
| 79 Element e, Func1<AstNode, JS.Node/*=T*/ > visit) { | 79 Element e, Func1<AstNode, JS.Node/*=T*/ > visit) { |
| 80 var node = _declarationNodes.remove(e); | 80 var node = _declarationNodes.remove(e); |
| 81 if (node == null) return null; // not from this module or already loaded. | 81 if (node == null) return null; // not from this module or already loaded. |
| 82 | 82 |
| 83 _currentElements.add(e); | 83 _currentElements.add(e); |
| 84 | 84 |
| 85 var result = visit(node); | 85 var result = visit(node); |
| 86 | 86 |
| 87 var last = _currentElements.removeLast(); | 87 var last = _currentElements.removeLast(); |
| 88 assert(identical(e, last)); | 88 assert(e == last); |
| 89 | 89 |
| 90 return result; | 90 return result; |
| 91 } | 91 } |
| 92 | 92 |
| 93 /// To emit top-level module items, we sometimes need to reorder them. | 93 /// To emit top-level module items, we sometimes need to reorder them. |
| 94 /// | 94 /// |
| 95 /// This function takes care of that, and also detects cases where reordering | 95 /// This function takes care of that, and also detects cases where reordering |
| 96 /// failed, and we need to resort to lazy loading, by marking the element as | 96 /// failed, and we need to resort to lazy loading, by marking the element as |
| 97 /// lazy. All elements need to be aware of this possibility and generate code | 97 /// lazy. All elements need to be aware of this possibility and generate code |
| 98 /// accordingly. | 98 /// accordingly. |
| 99 /// | 99 /// |
| 100 /// If we are not emitting top-level code, this does nothing, because all | 100 /// If we are not emitting top-level code, this does nothing, because all |
| 101 /// declarations are assumed to be available before we start execution. | 101 /// declarations are assumed to be available before we start execution. |
| 102 /// See [startTopLevel]. | 102 /// See [startTopLevel]. |
| 103 void declareBeforeUse(Element e, VoidFunc1<Element> emit) { | 103 void declareBeforeUse(Element e, VoidFunc1<Element> emit) { |
| 104 if (e == null) return; | 104 if (e == null) return; |
| 105 | 105 |
| 106 if (_checkReferences != null) { | 106 if (_checkReferences != null) { |
| 107 _checkReferences = _checkReferences && isLoaded(e) && !_isLoading(e); | 107 _checkReferences = _checkReferences && isLoaded(e) && !_isLoading(e); |
| 108 return; | 108 return; |
| 109 } | 109 } |
| 110 | 110 |
| 111 var topLevel = _topLevelElements; | 111 var topLevel = _topLevelElements; |
| 112 if (topLevel.isNotEmpty && identical(currentElement, topLevel.last)) { | 112 if (topLevel.isNotEmpty && currentElement == topLevel.last) { |
| 113 // If the item is from our library, try to emit it now. | 113 // If the item is from our library, try to emit it now. |
| 114 emit(e); | 114 emit(e); |
| 115 } | 115 } |
| 116 } | 116 } |
| 117 } | 117 } |
| OLD | NEW |