Use Map(s) for top-level declarations.

This would help to implement 'get sources by name' that Paul pointed
out in a previous CL.

R=brianwilkerson@google.com, paulberry@google.com
BUG=

Review URL: https://codereview.chromium.org/2543263002 .
This commit is contained in:
Konstantin Shcheglov 2016-12-02 08:44:31 -08:00
parent 8e2011aa21
commit d6e89bf3a2
2 changed files with 35 additions and 38 deletions

View file

@ -101,8 +101,8 @@ class FileState {
Set<FileState> _transitiveFiles;
String _transitiveSignature;
List<TopLevelDeclaration> _topLevelDeclarations;
List<TopLevelDeclaration> _exportedTopLevelDeclarations;
Map<String, TopLevelDeclaration> _topLevelDeclarations;
Map<String, TopLevelDeclaration> _exportedTopLevelDeclarations;
FileState._(this._fsState, this.path, this.uri, this.source);
@ -133,47 +133,43 @@ class FileState {
List<FileState> get exportedFiles => _exportedFiles;
/**
* Return [TopLevelDeclaration]s exported from the this library file.
* Return [TopLevelDeclaration]s exported from the this library file. The
* keys to the map are names of declarations.
*/
List<TopLevelDeclaration> get exportedTopLevelDeclarations {
Map<String, TopLevelDeclaration> get exportedTopLevelDeclarations {
if (_exportedTopLevelDeclarations == null) {
_exportedTopLevelDeclarations = <TopLevelDeclaration>[];
_exportedTopLevelDeclarations = <String, TopLevelDeclaration>{};
Set<FileState> seenLibraries = new Set<FileState>();
/**
* Compute [TopLevelDeclaration]s exported from the [library].
*/
List<TopLevelDeclaration> computeExported(FileState library) {
Map<String, TopLevelDeclaration> computeExported(FileState library) {
var declarations = <String, TopLevelDeclaration>{};
if (seenLibraries.add(library)) {
// Append the library declarations.
for (TopLevelDeclaration t in library.topLevelDeclarations) {
declarations[t.name] = t;
}
for (FileState part in library.partedFiles) {
for (TopLevelDeclaration t in part.topLevelDeclarations) {
declarations[t.name] = t;
}
}
// Append the exported declarations.
for (int i = 0; i < library._exportedFiles.length; i++) {
List<TopLevelDeclaration> exported =
Map<String, TopLevelDeclaration> exported =
computeExported(library._exportedFiles[i]);
for (TopLevelDeclaration t in exported) {
if (!declarations.containsKey(t.name) &&
library._exportFilters[i].accepts(t.name)) {
for (TopLevelDeclaration t in exported.values) {
if (library._exportFilters[i].accepts(t.name)) {
declarations[t.name] = t;
}
}
}
// Append the library declarations.
declarations.addAll(library.topLevelDeclarations);
for (FileState part in library.partedFiles) {
declarations.addAll(part.topLevelDeclarations);
}
// We're done with this library.
seenLibraries.remove(library);
}
return declarations.values.toList();
return declarations;
}
_exportedTopLevelDeclarations = computeExported(this);
@ -224,15 +220,16 @@ class FileState {
Set<String> get referencedNames => _referencedNames;
/**
* Return public top-level declarations declared in the file.
* Return public top-level declarations declared in the file. The keys to the
* map are names of declarations.
*/
List<TopLevelDeclaration> get topLevelDeclarations {
Map<String, TopLevelDeclaration> get topLevelDeclarations {
if (_topLevelDeclarations == null) {
_topLevelDeclarations = <TopLevelDeclaration>[];
_topLevelDeclarations = <String, TopLevelDeclaration>{};
void addDeclaration(TopLevelDeclarationKind kind, String name) {
if (!name.startsWith('_')) {
_topLevelDeclarations.add(new TopLevelDeclaration(kind, name));
_topLevelDeclarations[name] = new TopLevelDeclaration(kind, name);
}
}

View file

@ -73,8 +73,9 @@ export 'a.dart';
class B {}
''');
FileState file = fileSystemState.getFileForPath(b);
List<TopLevelDeclaration> declarations = file.exportedTopLevelDeclarations;
expect(declarations.map((t) => t.name), unorderedEquals(['A', 'B']));
Map<String, TopLevelDeclaration> declarations =
file.exportedTopLevelDeclarations;
expect(declarations.keys, unorderedEquals(['A', 'B']));
}
test_exportedTopLevelDeclarations_export2_show() {
@ -162,9 +163,10 @@ export 'a.dart';
int V;
''');
FileState file = fileSystemState.getFileForPath(b);
List<TopLevelDeclaration> declarations = file.exportedTopLevelDeclarations;
expect(declarations.map((t) => t.name), unorderedEquals(['V']));
expect(declarations.single.kind, TopLevelDeclarationKind.variable);
Map<String, TopLevelDeclaration> declarations =
file.exportedTopLevelDeclarations;
expect(declarations.keys, unorderedEquals(['V']));
expect(declarations['V'].kind, TopLevelDeclarationKind.variable);
}
test_exportedTopLevelDeclarations_export_show() {
@ -499,16 +501,13 @@ set _V3(_) {}
''');
FileState file = fileSystemState.getFileForPath(path);
List<TopLevelDeclaration> declarations = file.topLevelDeclarations;
Map<String, TopLevelDeclaration> declarations = file.topLevelDeclarations;
void assertHas(String name, TopLevelDeclarationKind kind) {
expect(
declarations,
contains(predicate(
(TopLevelDeclaration t) => t.name == name && t.kind == kind)));
expect(declarations[name]?.kind, kind);
}
expect(declarations.map((t) => t.name),
expect(declarations.keys,
unorderedEquals(['C', 'F', 'E', 'f', 'V1', 'V2', 'V3', 'V4']));
assertHas('C', TopLevelDeclarationKind.type);
assertHas('F', TopLevelDeclarationKind.type);
@ -636,8 +635,9 @@ set _V3(_) {}
void _assertExportedTopLevelDeclarations(String path, List<String> expected) {
FileState file = fileSystemState.getFileForPath(path);
List<TopLevelDeclaration> declarations = file.exportedTopLevelDeclarations;
expect(declarations.map((t) => t.name), unorderedEquals(expected));
Map<String, TopLevelDeclaration> declarations =
file.exportedTopLevelDeclarations;
expect(declarations.keys, unorderedEquals(expected));
}
void _assertFilesWithoutTransitiveFiles(List<FileState> expected) {