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

Unified 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 side-by-side diff with in-line comments
Download patch
Index: pkg/analyzer/lib/src/summary/summary_file_builder.dart
diff --git a/pkg/analyzer/lib/src/summary/summary_file_builder.dart b/pkg/analyzer/lib/src/summary/summary_file_builder.dart
index ad03a10960e611d9364583823a45e5fe73859b9c..d27133f55ef2a4d5d37af5a2763fe8dc7a8c245c 100644
--- a/pkg/analyzer/lib/src/summary/summary_file_builder.dart
+++ b/pkg/analyzer/lib/src/summary/summary_file_builder.dart
@@ -6,13 +6,22 @@ library analyzer.src.summary.summary_file_builder;
import 'dart:collection';
-import 'package:analyzer/dart/element/element.dart';
+import 'package:analyzer/dart/ast/ast.dart';
+import 'package:analyzer/dart/ast/token.dart';
+import 'package:analyzer/error/listener.dart';
import 'package:analyzer/file_system/file_system.dart';
import 'package:analyzer/file_system/physical_file_system.dart';
+import 'package:analyzer/src/dart/scanner/reader.dart';
+import 'package:analyzer/src/dart/scanner/scanner.dart';
import 'package:analyzer/src/dart/sdk/sdk.dart';
import 'package:analyzer/src/generated/engine.dart';
+import 'package:analyzer/src/generated/parser.dart';
import 'package:analyzer/src/generated/sdk.dart';
import 'package:analyzer/src/generated/source.dart';
+import 'package:analyzer/src/summary/format.dart';
+import 'package:analyzer/src/summary/idl.dart';
+import 'package:analyzer/src/summary/link.dart';
+import 'package:analyzer/src/summary/summarize_ast.dart';
import 'package:analyzer/src/summary/summarize_elements.dart';
class SummaryBuilder {
@@ -59,37 +68,83 @@ class SummaryBuilder {
/**
* Build the linked bundle and return its bytes.
*/
- List<int> build() => new _Builder(context, librarySources).build();
+ List<int> build() => new _Builder(context, librarySources, strong).build();
}
class _Builder {
final AnalysisContext context;
final Iterable<Source> librarySources;
+ final bool strong;
+
+ final Set<String> libraryUris = new Set<String>();
+ final Map<String, UnlinkedUnit> unlinkedMap = <String, UnlinkedUnit>{};
- final Set<Source> processedSources = new Set<Source>();
final PackageBundleAssembler bundleAssembler = new PackageBundleAssembler();
- _Builder(this.context, this.librarySources);
+ _Builder(this.context, this.librarySources, this.strong);
/**
* Build the linked bundle and return its bytes.
*/
List<int> build() {
- librarySources.forEach(_serializeLibrary);
+ librarySources.forEach(_addLibrary);
+
+ Map<String, LinkedLibraryBuilder> map = link(libraryUris, (uri) {
+ throw new StateError('Unexpected call to GetDependencyCallback($uri).');
+ }, (uri) {
+ UnlinkedUnit unlinked = unlinkedMap[uri];
+ if (unlinked == null) {
+ throw new StateError('Unable to find unresolved unit $uri.');
+ }
+ return unlinked;
+ }, (String name) {
+ throw new StateError('Unexpected call to GetDeclaredVariable($name).');
+ }, strong);
+ map.forEach(bundleAssembler.addLinkedLibrary);
+
return bundleAssembler.assemble().toBuffer();
}
- /**
- * Serialize the library with the given [source] and all its direct or
- * indirect imports and exports.
- */
- void _serializeLibrary(Source source) {
- if (!processedSources.add(source)) {
+ void _addLibrary(Source source) {
+ String uriStr = source.uri.toString();
+ if (!libraryUris.add(uriStr)) {
return;
}
- LibraryElement element = context.computeLibraryElement(source);
- bundleAssembler.serializeLibraryElement(element);
- element.importedLibraries.forEach((e) => _serializeLibrary(e.source));
- element.exportedLibraries.forEach((e) => _serializeLibrary(e.source));
+ CompilationUnit unit = _addUnlinked(source);
+ for (Directive directive in unit.directives) {
+ if (directive is NamespaceDirective) {
+ String libUri = directive.uri.stringValue;
+ Source libSource = context.sourceFactory.resolveUri(source, libUri);
+ _addLibrary(libSource);
+ } else if (directive is PartDirective) {
+ String partUri = directive.uri.stringValue;
+ Source partSource = context.sourceFactory.resolveUri(source, partUri);
+ _addUnlinked(partSource);
+ }
+ }
+ }
+
+ CompilationUnit _addUnlinked(Source source) {
+ String uriStr = source.uri.toString();
+ CompilationUnit unit = _parse(source);
+ UnlinkedUnitBuilder unlinked = serializeAstUnlinked(unit);
+ unlinkedMap[uriStr] = unlinked;
+ bundleAssembler.addUnlinkedUnit(source, unlinked);
+ return unit;
+ }
+
+ CompilationUnit _parse(Source source) {
+ AnalysisErrorListener errorListener = AnalysisErrorListener.NULL_LISTENER;
+ String code = source.contents.data;
+ CharSequenceReader reader = new CharSequenceReader(code);
+ Scanner scanner = new Scanner(source, reader, errorListener);
+ scanner.scanGenericMethodComments = strong;
+ Token token = scanner.tokenize();
+ LineInfo lineInfo = new LineInfo(scanner.lineStarts);
+ Parser parser = new Parser(source, errorListener);
+ parser.parseGenericMethodComments = strong;
+ CompilationUnit unit = parser.parseCompilationUnit(token);
+ unit.lineInfo = lineInfo;
+ return unit;
}
}

Powered by Google App Engine
This is Rietveld 408576698