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

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

Issue 1574053002: Move NameFilter to its own file so it can be conveniently reused. (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: Created 4 years, 11 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 import 'package:analyzer/src/summary/format.dart'; 5 import 'package:analyzer/src/summary/format.dart';
6 import 'package:analyzer/src/summary/name_filter.dart';
6 7
7 /** 8 /**
8 * Create a [PrelinkedLibraryBuilder] corresponding to the given 9 * Create a [PrelinkedLibraryBuilder] corresponding to the given
9 * [definingUnit], which should be the defining compilation unit for a library. 10 * [definingUnit], which should be the defining compilation unit for a library.
10 * Compilation units referenced by the defining compilation unit via `part` 11 * Compilation units referenced by the defining compilation unit via `part`
11 * declarations will be retrieved using [getPart]. Public namespaces for 12 * declarations will be retrieved using [getPart]. Public namespaces for
12 * libraries referenced by the defining compilation unit via `import` 13 * libraries referenced by the defining compilation unit via `import`
13 * declarations (and files reachable from them via `part` and `export` 14 * declarations (and files reachable from them via `part` and `export`
14 * declarations) will be retrieved using [getImport]. 15 * declarations) will be retrieved using [getImport].
15 */ 16 */
(...skipping 17 matching lines...) Expand all
33 * Type of the callback used by the prelinker to obtain unlinked summaries of 34 * Type of the callback used by the prelinker to obtain unlinked summaries of
34 * part files of the library to be prelinked. [relaviteUri] should be 35 * part files of the library to be prelinked. [relaviteUri] should be
35 * interpreted relative to the defining compilation unit of the library being 36 * interpreted relative to the defining compilation unit of the library being
36 * prelinked. 37 * prelinked.
37 * 38 *
38 * If no file exists at the given uri, `null` should be returned. 39 * If no file exists at the given uri, `null` should be returned.
39 */ 40 */
40 typedef UnlinkedUnit GetPartCallback(String relativeUri); 41 typedef UnlinkedUnit GetPartCallback(String relativeUri);
41 42
42 /** 43 /**
43 * A [NameFilter] represents the set of filtering rules implied by zero or more
44 * combinators in an `export` or `import` statement.
45 */
46 class NameFilter {
47 /**
48 * A [NameFilter] representing no filtering at all (i.e. no combinators).
49 */
50 static final NameFilter identity =
51 new NameFilter._(hiddenNames: new Set<String>());
52
53 /**
54 * If this [NameFilter] accepts a finite number of names and hides all
55 * others, the (possibly empty) set of names it accepts. Otherwise `null`.
56 */
57 final Set<String> shownNames;
58
59 /**
60 * If [shownNames] is `null`, the (possibly empty) set of names not accepted
61 * by this filter (all other names are accepted). If [shownNames] is not
62 * `null`, then [hiddenNames] will be `null`.
63 */
64 final Set<String> hiddenNames;
65
66 /**
67 * Create a [NameFilter] based on the given [combinator].
68 */
69 factory NameFilter.forCombinator(UnlinkedCombinator combinator) {
70 if (combinator.shows.isNotEmpty) {
71 return new NameFilter._(shownNames: combinator.shows.toSet());
72 } else {
73 return new NameFilter._(hiddenNames: combinator.hides.toSet());
74 }
75 }
76
77 /**
78 * Create a [NameFilter] based on the given (possibly empty) sequence of
79 * [combinators].
80 */
81 factory NameFilter.forCombinators(List<UnlinkedCombinator> combinators) {
82 NameFilter result = identity;
83 for (UnlinkedCombinator combinator in combinators) {
84 result = result.merge(new NameFilter.forCombinator(combinator));
85 }
86 return result;
87 }
88
89 const NameFilter._({this.shownNames, this.hiddenNames});
90
91 /**
92 * Determine if the given [name] is accepted by this [NameFilter].
93 */
94 bool accepts(String name) {
95 if (name.endsWith('=')) {
96 name = name.substring(0, name.length - 1);
97 }
98 if (shownNames != null) {
99 return shownNames.contains(name);
100 } else {
101 return !hiddenNames.contains(name);
102 }
103 }
104
105 /**
106 * Produce a new [NameFilter] by combining this [NameFilter] with another
107 * one. The new [NameFilter] will only accept names that would be accepted
108 * by both input filters.
109 */
110 NameFilter merge(NameFilter other) {
111 if (shownNames != null) {
112 if (other.shownNames != null) {
113 return new NameFilter._(
114 shownNames: shownNames.intersection(other.shownNames));
115 } else {
116 return new NameFilter._(
117 shownNames: shownNames.difference(other.hiddenNames));
118 }
119 } else {
120 if (other.shownNames != null) {
121 return new NameFilter._(
122 shownNames: other.shownNames.difference(hiddenNames));
123 } else {
124 return new NameFilter._(
125 hiddenNames: hiddenNames.union(other.hiddenNames));
126 }
127 }
128 }
129 }
130
131 /**
132 * A [_Meaning] stores all the information necessary to find the declaration 44 * A [_Meaning] stores all the information necessary to find the declaration
133 * referred to by a name in a namespace. 45 * referred to by a name in a namespace.
134 */ 46 */
135 class _Meaning { 47 class _Meaning {
136 /** 48 /**
137 * Which unit in the dependent library contains the declared entity. 49 * Which unit in the dependent library contains the declared entity.
138 */ 50 */
139 final int unit; 51 final int unit;
140 52
141 /** 53 /**
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after
283 if (exportedNamespace != null) { 195 if (exportedNamespace != null) {
284 for (UnlinkedExportPublic export in exportedNamespace.exports) { 196 for (UnlinkedExportPublic export in exportedNamespace.exports) {
285 String exportUri = resolveUri(relativeUri, export.uri); 197 String exportUri = resolveUri(relativeUri, export.uri);
286 aggregatePublicNamespace(exportUri) 198 aggregatePublicNamespace(exportUri)
287 .forEach((String name, _Meaning meaning) { 199 .forEach((String name, _Meaning meaning) {
288 if (filter.accepts(name) && !exportNamespace.containsKey(name)) { 200 if (filter.accepts(name) && !exportNamespace.containsKey(name)) {
289 exportNamespace[name] = meaning; 201 exportNamespace[name] = meaning;
290 } 202 }
291 }); 203 });
292 chaseExports( 204 chaseExports(
293 filter.merge(new NameFilter.forCombinators(export.combinators)), 205 filter.merge(
206 new NameFilter.forUnlinkedCombinators(export.combinators)),
294 exportUri, 207 exportUri,
295 seenUris); 208 seenUris);
296 } 209 }
297 } 210 }
298 seenUris.remove(relativeUri); 211 seenUris.remove(relativeUri);
299 } 212 }
300 } 213 }
301 chaseExports(NameFilter.identity, relativeUri, new Set<String>()); 214 chaseExports(NameFilter.identity, relativeUri, new Set<String>());
302 return exportNamespace; 215 return exportNamespace;
303 } 216 }
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
340 253
341 /** 254 /**
342 * Filter the export namespace for the library whose URI is reachable from 255 * Filter the export namespace for the library whose URI is reachable from
343 * [definingUnit] via [relativeUri], retaining only those names accepted by 256 * [definingUnit] via [relativeUri], retaining only those names accepted by
344 * [combinators], and store the resulting names in [result]. Names that 257 * [combinators], and store the resulting names in [result]. Names that
345 * already exist in [result] are not overwritten. 258 * already exist in [result] are not overwritten.
346 */ 259 */
347 void filterExportNamespace(String relativeUri, 260 void filterExportNamespace(String relativeUri,
348 List<UnlinkedCombinator> combinators, Map<String, _Meaning> result) { 261 List<UnlinkedCombinator> combinators, Map<String, _Meaning> result) {
349 Map<String, _Meaning> exportNamespace = computeExportNamespace(relativeUri); 262 Map<String, _Meaning> exportNamespace = computeExportNamespace(relativeUri);
350 NameFilter filter = new NameFilter.forCombinators(combinators); 263 NameFilter filter = new NameFilter.forUnlinkedCombinators(combinators);
351 exportNamespace.forEach((String name, _Meaning meaning) { 264 exportNamespace.forEach((String name, _Meaning meaning) {
352 if (filter.accepts(name) && !result.containsKey(name)) { 265 if (filter.accepts(name) && !result.containsKey(name)) {
353 result[name] = meaning; 266 result[name] = meaning;
354 } 267 }
355 }); 268 });
356 } 269 }
357 270
358 /** 271 /**
359 * Wrapper around [getImport] that caches the return value in [importCache]. 272 * Wrapper around [getImport] that caches the return value in [importCache].
360 */ 273 */
(...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after
494 * [sourceUri] is also relative. 407 * [sourceUri] is also relative.
495 */ 408 */
496 String resolveUri(String sourceUri, String relativeUri) { 409 String resolveUri(String sourceUri, String relativeUri) {
497 if (sourceUri == null) { 410 if (sourceUri == null) {
498 return relativeUri; 411 return relativeUri;
499 } else { 412 } else {
500 return Uri.parse(sourceUri).resolve(relativeUri).toString(); 413 return Uri.parse(sourceUri).resolve(relativeUri).toString();
501 } 414 }
502 } 415 }
503 } 416 }
OLDNEW
« no previous file with comments | « pkg/analyzer/lib/src/summary/name_filter.dart ('k') | pkg/analyzer/lib/src/summary/summarize_elements.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698