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

Side by Side Diff: lib/src/mirror_loader.dart

Issue 1018643002: change to source-order crawling of directives instead of alphabetical (Closed) Base URL: git@github.com:dart-lang/static-init.git@master
Patch Set: switch to bug fix instead of breaking change, and use definingCompilationUnit Created 5 years, 9 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
« no previous file with comments | « CHANGELOG.md ('k') | lib/transformer.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2015, 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 library initialize.mirror_loader; 4 library initialize.mirror_loader;
5 5
6 import 'dart:collection' show Queue; 6 import 'dart:collection' show Queue;
7 import 'dart:mirrors'; 7 import 'dart:mirrors';
8 import 'package:path/path.dart' as path; 8 import 'package:path/path.dart' as path;
9 import 'package:initialize/initialize.dart'; 9 import 'package:initialize/initialize.dart';
10 10
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after
91 return Uri.parse('package:$packagePath'); 91 return Uri.parse('package:$packagePath');
92 } 92 }
93 93
94 // Reads Initializer annotations on this library and all its dependencies in 94 // Reads Initializer annotations on this library and all its dependencies in
95 // post-order. 95 // post-order.
96 Queue<Function> _readLibraryDeclarations(LibraryMirror lib, 96 Queue<Function> _readLibraryDeclarations(LibraryMirror lib,
97 Set<LibraryMirror> librariesSeen, Queue<Function> queue) { 97 Set<LibraryMirror> librariesSeen, Queue<Function> queue) {
98 librariesSeen.add(lib); 98 librariesSeen.add(lib);
99 99
100 // First visit all our dependencies. 100 // First visit all our dependencies.
101 for (var dependency in _sortedLibraryDependencies(lib)) { 101 for (var dependency in lib.libraryDependencies) {
102 // Skip dart: imports, they never use this package. 102 // Skip dart: imports, they never use this package.
103 if (dependency.targetLibrary.uri.toString().startsWith('dart:')) continue; 103 if (dependency.targetLibrary.uri.toString().startsWith('dart:')) continue;
104 if (librariesSeen.contains(dependency.targetLibrary)) continue; 104 if (librariesSeen.contains(dependency.targetLibrary)) continue;
105 105
106 _readLibraryDeclarations(dependency.targetLibrary, librariesSeen, queue); 106 _readLibraryDeclarations(dependency.targetLibrary, librariesSeen, queue);
107 } 107 }
108 108
109 // Second parse the library directive annotations. 109 // Second parse the library directive annotations.
110 _readAnnotations(lib, queue); 110 _readAnnotations(lib, queue);
111 111
112 // Last, parse all class and method annotations. 112 // Last, parse all class and method annotations.
113 for (var declaration in _sortedLibraryDeclarations(lib)) { 113 for (var declaration in _sortedDeclarationsWithMetadata(lib)) {
114 _readAnnotations(declaration, queue); 114 _readAnnotations(declaration, queue);
115 // Check classes for static annotations which are not supported 115 // Check classes for static annotations which are not supported
116 if (declaration is ClassMirror) { 116 if (declaration is ClassMirror) {
117 for (var classDeclaration in declaration.declarations.values) { 117 for (var classDeclaration in declaration.declarations.values) {
118 _readAnnotations(classDeclaration, queue); 118 _readAnnotations(classDeclaration, queue);
119 } 119 }
120 } 120 }
121 } 121 }
122 122
123 return queue; 123 return queue;
124 } 124 }
125 125
126 Iterable<LibraryDependencyMirror> _sortedLibraryDependencies( 126 Iterable<DeclarationMirror> _sortedDeclarationsWithMetadata(
127 LibraryMirror lib) => new List.from(lib.libraryDependencies) 127 LibraryMirror lib) {
128 ..sort((a, b) { 128 return new List()
129 var aScheme = a.targetLibrary.uri.scheme; 129 ..addAll(_sortDeclarations(
130 var bScheme = b.targetLibrary.uri.scheme; 130 lib, lib.declarations.values.where(
131 if (aScheme != 'file' && bScheme == 'file') return -1; 131 (d) => d is MethodMirror && d.metadata.isNotEmpty)))
132 if (bScheme != 'file' && aScheme == 'file') return 1; 132 ..addAll(_sortDeclarations(
133 return _relativeLibraryUri(a).compareTo(_relativeLibraryUri(b)); 133 lib, lib.declarations.values.where(
134 }); 134 (d) => d is ClassMirror && d.metadata.isNotEmpty)));
135
136 String _relativeLibraryUri(LibraryDependencyMirror lib) {
137 if (lib.targetLibrary.uri.scheme == 'file' &&
138 lib.sourceLibrary.uri.scheme == 'file') {
139 return path.relative(lib.targetLibrary.uri.path,
140 from: path.dirname(lib.sourceLibrary.uri.path));
141 }
142 return lib.targetLibrary.uri.toString();
143 } 135 }
144 136
145 Iterable<DeclarationMirror> _sortedLibraryDeclarations(LibraryMirror lib) => 137 List<DeclarationMirror> _sortDeclarations(
146 lib.declarations.values 138 LibraryMirror sourceLib, Iterable<DeclarationMirror> declarations) {
147 .where((d) => d is ClassMirror || d is MethodMirror) 139 var declarationList = declarations.toList();
148 .toList() 140 declarationList.sort((DeclarationMirror a, DeclarationMirror b) {
149 ..sort((a, b) { 141 // If in the same file, compare by line.
150 if (a is MethodMirror && b is ClassMirror) return -1; 142 var aSourceUri = a.location.sourceUri;
151 if (a is ClassMirror && b is MethodMirror) return 1; 143 var bSourceUri = b.location.sourceUri;
152 return _declarationName(a).compareTo(_declarationName(b)); 144 if (aSourceUri == bSourceUri) {
145 return a.location.line.compareTo(b.location.line);
146 }
147
148 // Run parts first if one is from the original library.
149 if (aSourceUri == sourceLib.uri) return 1;
150 if (bSourceUri == sourceLib.uri) return -1;
151
152 // Sort parts alphabetically.
153 return aSourceUri.path.compareTo(bSourceUri.path);
153 }); 154 });
155 return declarationList;
156 }
154 157
155 String _declarationName(DeclarationMirror declaration) => 158 String _declarationName(DeclarationMirror declaration) =>
156 MirrorSystem.getName(declaration.qualifiedName); 159 MirrorSystem.getName(declaration.qualifiedName);
157 160
158 // Reads annotations on declarations and adds them to `_initQueue` if they are 161 // Reads annotations on declarations and adds them to `_initQueue` if they are
159 // initializers. 162 // initializers.
160 void _readAnnotations(DeclarationMirror declaration, Queue<Function> queue) { 163 void _readAnnotations(DeclarationMirror declaration, Queue<Function> queue) {
161 var annotations = 164 var annotations =
162 declaration.metadata.where((m) => _filterMetadata(declaration, m)); 165 declaration.metadata.where((m) => _filterMetadata(declaration, m));
163 for (var meta in annotations) { 166 for (var meta in annotations) {
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
238 return true; 241 return true;
239 } 242 }
240 } 243 }
241 244
242 final _TOP_LEVEL_FUNCTIONS_ONLY = new UnsupportedError( 245 final _TOP_LEVEL_FUNCTIONS_ONLY = new UnsupportedError(
243 'Only top level methods are supported for initializers'); 246 'Only top level methods are supported for initializers');
244 247
245 final _UNSUPPORTED_DECLARATION = new UnsupportedError( 248 final _UNSUPPORTED_DECLARATION = new UnsupportedError(
246 'Initializers are only supported on libraries, classes, and top level ' 249 'Initializers are only supported on libraries, classes, and top level '
247 'methods'); 250 'methods');
OLDNEW
« no previous file with comments | « CHANGELOG.md ('k') | lib/transformer.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698