Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(36)

Side by Side Diff: pkg/analyzer/lib/src/summary/summary_file_builder.dart

Issue 2351863002: Generate SDK summaries using ASTs. (Closed)
Patch Set: Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 analyzer.src.summary.summary_file_builder; 5 library analyzer.src.summary.summary_file_builder;
6 6
7 import 'dart:collection'; 7 import 'dart:collection';
8 8
9 import 'package:analyzer/dart/element/element.dart'; 9 import 'package:analyzer/dart/ast/ast.dart';
10 import 'package:analyzer/dart/ast/token.dart';
11 import 'package:analyzer/error/listener.dart';
10 import 'package:analyzer/file_system/file_system.dart'; 12 import 'package:analyzer/file_system/file_system.dart';
11 import 'package:analyzer/file_system/physical_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';
12 import 'package:analyzer/src/dart/sdk/sdk.dart'; 16 import 'package:analyzer/src/dart/sdk/sdk.dart';
13 import 'package:analyzer/src/generated/engine.dart'; 17 import 'package:analyzer/src/generated/engine.dart';
18 import 'package:analyzer/src/generated/parser.dart';
14 import 'package:analyzer/src/generated/sdk.dart'; 19 import 'package:analyzer/src/generated/sdk.dart';
15 import 'package:analyzer/src/generated/source.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';
16 import 'package:analyzer/src/summary/summarize_elements.dart'; 25 import 'package:analyzer/src/summary/summarize_elements.dart';
17 26
18 class SummaryBuilder { 27 class SummaryBuilder {
19 final Iterable<Source> librarySources; 28 final Iterable<Source> librarySources;
20 final AnalysisContext context; 29 final AnalysisContext context;
21 final bool strong; 30 final bool strong;
22 31
23 /** 32 /**
24 * Create a summary builder for these [librarySources] and [context]. 33 * Create a summary builder for these [librarySources] and [context].
25 */ 34 */
(...skipping 26 matching lines...) Expand all
52 for (String uri in uriSet) { 61 for (String uri in uriSet) {
53 librarySources.add(sdk.mapDartUri(uri)); 62 librarySources.add(sdk.mapDartUri(uri));
54 } 63 }
55 64
56 return new SummaryBuilder(librarySources, sdk.context, strong); 65 return new SummaryBuilder(librarySources, sdk.context, strong);
57 } 66 }
58 67
59 /** 68 /**
60 * Build the linked bundle and return its bytes. 69 * Build the linked bundle and return its bytes.
61 */ 70 */
62 List<int> build() => new _Builder(context, librarySources).build(); 71 List<int> build() => new _Builder(context, librarySources, strong).build();
63 } 72 }
64 73
65 class _Builder { 74 class _Builder {
66 final AnalysisContext context; 75 final AnalysisContext context;
67 final Iterable<Source> librarySources; 76 final Iterable<Source> librarySources;
77 final bool strong;
68 78
69 final Set<Source> processedSources = new Set<Source>(); 79 final Set<String> libraryUris = new Set<String>();
80 final Map<String, UnlinkedUnit> unlinkedMap = <String, UnlinkedUnit>{};
81
70 final PackageBundleAssembler bundleAssembler = new PackageBundleAssembler(); 82 final PackageBundleAssembler bundleAssembler = new PackageBundleAssembler();
71 83
72 _Builder(this.context, this.librarySources); 84 _Builder(this.context, this.librarySources, this.strong);
73 85
74 /** 86 /**
75 * Build the linked bundle and return its bytes. 87 * Build the linked bundle and return its bytes.
76 */ 88 */
77 List<int> build() { 89 List<int> build() {
78 librarySources.forEach(_serializeLibrary); 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
79 return bundleAssembler.assemble().toBuffer(); 105 return bundleAssembler.assemble().toBuffer();
80 } 106 }
81 107
82 /** 108 void _addLibrary(Source source) {
83 * Serialize the library with the given [source] and all its direct or 109 String uriStr = source.uri.toString();
84 * indirect imports and exports. 110 if (!libraryUris.add(uriStr)) {
85 */
86 void _serializeLibrary(Source source) {
87 if (!processedSources.add(source)) {
88 return; 111 return;
89 } 112 }
90 LibraryElement element = context.computeLibraryElement(source); 113 CompilationUnit unit = _addUnlinked(source);
91 bundleAssembler.serializeLibraryElement(element); 114 for (Directive directive in unit.directives) {
92 element.importedLibraries.forEach((e) => _serializeLibrary(e.source)); 115 if (directive is NamespaceDirective) {
93 element.exportedLibraries.forEach((e) => _serializeLibrary(e.source)); 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;
94 } 149 }
95 } 150 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698