| OLD | NEW |
| (Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 library analyzer.src.summary.summary_file_builder; |
| 6 |
| 7 import 'dart:collection'; |
| 8 |
| 9 import 'package:analyzer/dart/ast/ast.dart'; |
| 10 import 'package:analyzer/dart/ast/token.dart'; |
| 11 import 'package:analyzer/error/listener.dart'; |
| 12 import 'package:analyzer/file_system/file_system.dart'; |
| 13 import 'package:analyzer/file_system/physical_file_system.dart'; |
| 14 import 'package:analyzer/src/dart/scanner/reader.dart'; |
| 15 import 'package:analyzer/src/dart/scanner/scanner.dart'; |
| 16 import 'package:analyzer/src/dart/sdk/sdk.dart'; |
| 17 import 'package:analyzer/src/generated/engine.dart'; |
| 18 import 'package:analyzer/src/generated/parser.dart'; |
| 19 import 'package:analyzer/src/generated/sdk.dart'; |
| 20 import 'package:analyzer/src/generated/source.dart'; |
| 21 import 'package:analyzer/src/summary/format.dart'; |
| 22 import 'package:analyzer/src/summary/idl.dart'; |
| 23 import 'package:analyzer/src/summary/link.dart'; |
| 24 import 'package:analyzer/src/summary/summarize_ast.dart'; |
| 25 import 'package:analyzer/src/summary/summarize_elements.dart'; |
| 26 |
| 27 class SummaryBuilder { |
| 28 final Iterable<Source> librarySources; |
| 29 final AnalysisContext context; |
| 30 final bool strong; |
| 31 |
| 32 /** |
| 33 * Create a summary builder for these [librarySources] and [context]. |
| 34 */ |
| 35 SummaryBuilder(this.librarySources, this.context, this.strong); |
| 36 |
| 37 /** |
| 38 * Create an SDK summary builder for the dart SDK at the given [sdkPath]. |
| 39 */ |
| 40 factory SummaryBuilder.forSdk(String sdkPath, bool strong) { |
| 41 // |
| 42 // Prepare SDK. |
| 43 // |
| 44 ResourceProvider resourceProvider = PhysicalResourceProvider.INSTANCE; |
| 45 FolderBasedDartSdk sdk = new FolderBasedDartSdk( |
| 46 resourceProvider, resourceProvider.getFolder(sdkPath), strong); |
| 47 sdk.useSummary = false; |
| 48 sdk.analysisOptions = new AnalysisOptionsImpl()..strongMode = strong; |
| 49 |
| 50 // |
| 51 // Prepare 'dart:' URIs to serialize. |
| 52 // |
| 53 Set<String> uriSet = |
| 54 sdk.sdkLibraries.map((SdkLibrary library) => library.shortName).toSet(); |
| 55 if (!strong) { |
| 56 uriSet.add('dart:html/nativewrappers.dart'); |
| 57 } |
| 58 uriSet.add('dart:html_common/html_common_dart2js.dart'); |
| 59 |
| 60 Set<Source> librarySources = new HashSet<Source>(); |
| 61 for (String uri in uriSet) { |
| 62 librarySources.add(sdk.mapDartUri(uri)); |
| 63 } |
| 64 |
| 65 return new SummaryBuilder(librarySources, sdk.context, strong); |
| 66 } |
| 67 |
| 68 /** |
| 69 * Build the linked bundle and return its bytes. |
| 70 */ |
| 71 List<int> build() => new _Builder(context, librarySources, strong).build(); |
| 72 } |
| 73 |
| 74 class _Builder { |
| 75 final AnalysisContext context; |
| 76 final Iterable<Source> librarySources; |
| 77 final bool strong; |
| 78 |
| 79 final Set<String> libraryUris = new Set<String>(); |
| 80 final Map<String, UnlinkedUnit> unlinkedMap = <String, UnlinkedUnit>{}; |
| 81 |
| 82 final PackageBundleAssembler bundleAssembler = new PackageBundleAssembler(); |
| 83 |
| 84 _Builder(this.context, this.librarySources, this.strong); |
| 85 |
| 86 /** |
| 87 * Build the linked bundle and return its bytes. |
| 88 */ |
| 89 List<int> build() { |
| 90 librarySources.forEach(_addLibrary); |
| 91 |
| 92 Map<String, LinkedLibraryBuilder> map = link(libraryUris, (uri) { |
| 93 throw new StateError('Unexpected call to GetDependencyCallback($uri).'); |
| 94 }, (uri) { |
| 95 UnlinkedUnit unlinked = unlinkedMap[uri]; |
| 96 if (unlinked == null) { |
| 97 throw new StateError('Unable to find unresolved unit $uri.'); |
| 98 } |
| 99 return unlinked; |
| 100 }, (String name) { |
| 101 throw new StateError('Unexpected call to GetDeclaredVariable($name).'); |
| 102 }, strong); |
| 103 map.forEach(bundleAssembler.addLinkedLibrary); |
| 104 |
| 105 return bundleAssembler.assemble().toBuffer(); |
| 106 } |
| 107 |
| 108 void _addLibrary(Source source) { |
| 109 String uriStr = source.uri.toString(); |
| 110 if (!libraryUris.add(uriStr)) { |
| 111 return; |
| 112 } |
| 113 CompilationUnit unit = _addUnlinked(source); |
| 114 for (Directive directive in unit.directives) { |
| 115 if (directive is NamespaceDirective) { |
| 116 String libUri = directive.uri.stringValue; |
| 117 Source libSource = context.sourceFactory.resolveUri(source, libUri); |
| 118 _addLibrary(libSource); |
| 119 } else if (directive is PartDirective) { |
| 120 String partUri = directive.uri.stringValue; |
| 121 Source partSource = context.sourceFactory.resolveUri(source, partUri); |
| 122 _addUnlinked(partSource); |
| 123 } |
| 124 } |
| 125 } |
| 126 |
| 127 CompilationUnit _addUnlinked(Source source) { |
| 128 String uriStr = source.uri.toString(); |
| 129 CompilationUnit unit = _parse(source); |
| 130 UnlinkedUnitBuilder unlinked = serializeAstUnlinked(unit); |
| 131 unlinkedMap[uriStr] = unlinked; |
| 132 bundleAssembler.addUnlinkedUnit(source, unlinked); |
| 133 return unit; |
| 134 } |
| 135 |
| 136 CompilationUnit _parse(Source source) { |
| 137 AnalysisErrorListener errorListener = AnalysisErrorListener.NULL_LISTENER; |
| 138 String code = source.contents.data; |
| 139 CharSequenceReader reader = new CharSequenceReader(code); |
| 140 Scanner scanner = new Scanner(source, reader, errorListener); |
| 141 scanner.scanGenericMethodComments = strong; |
| 142 Token token = scanner.tokenize(); |
| 143 LineInfo lineInfo = new LineInfo(scanner.lineStarts); |
| 144 Parser parser = new Parser(source, errorListener); |
| 145 parser.parseGenericMethodComments = strong; |
| 146 CompilationUnit unit = parser.parseCompilationUnit(token); |
| 147 unit.lineInfo = lineInfo; |
| 148 return unit; |
| 149 } |
| 150 } |
| OLD | NEW |