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

Side by Side Diff: pkg/compiler/lib/src/mirrors/dart2js_library_mirror.dart

Issue 1805563002: Revert "Delete support for source mirrors" (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: Created 4 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
OLDNEW
(Empty)
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file
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.
4
5 part of dart2js.mirrors;
6
7 class Dart2JsLibraryMirror
8 extends Dart2JsElementMirror
9 with ObjectMirrorMixin, ContainerMixin
10 implements LibrarySourceMirror {
11 List<LibraryDependencySourceMirror> _libraryDependencies;
12
13 Dart2JsLibraryMirror(Dart2JsMirrorSystem system, LibraryElement library)
14 : super(system, library);
15
16 Function operator [](Symbol name) {
17 throw new UnsupportedError('LibraryMirror.operator [] unsupported.');
18 }
19
20 // TODO(johnniwinther): Avoid the need for [LibraryElementX].
21 LibraryElementX get _element => super._element;
22
23 Uri get uri => _element.canonicalUri;
24
25 DeclarationMirror get owner => null;
26
27 bool get isPrivate => false;
28
29 LibraryMirror library() => this;
30
31 /**
32 * Returns the library name (for libraries with a library tag) or the script
33 * file name (for scripts without a library tag). The latter case is used to
34 * provide a 'library name' for scripts, to use for instance in dartdoc.
35 */
36 String get _simpleNameString => _element.libraryOrScriptName;
37
38 Symbol get qualifiedName => simpleName;
39
40 void _forEachElement(f(Element element)) => _element.forEachLocalMember(f);
41
42 Iterable<Dart2JsDeclarationMirror> _getDeclarationMirrors(Element element) {
43 if (element.isClass || element.isTypedef) {
44 return [mirrorSystem._getTypeDeclarationMirror(element)];
45 } else {
46 return super._getDeclarationMirrors(element);
47 }
48 }
49
50 Map<Symbol, MethodMirror> get topLevelMembers => null;
51
52 /**
53 * Computes the first token of this library using the first library tag as
54 * indicator.
55 */
56 Token getBeginToken() {
57 if (_element.libraryTag != null) {
58 return _element.libraryTag.getBeginToken();
59 } else if (!_element.tags.isEmpty) {
60 return _element.tags.first.getBeginToken();
61 }
62 return null;
63 }
64
65 /**
66 * Computes the first token of this library using the last library tag as
67 * indicator.
68 */
69 Token getEndToken() {
70 if (!_element.tags.isEmpty) {
71 return _element.tags.last.getEndToken();
72 }
73 return null;
74 }
75
76 void _ensureLibraryDependenciesAnalyzed() {
77 if (_libraryDependencies == null) {
78 // TODO(johnniwinther): Support order of declarations on [LibraryElement].
79 Map<LibraryDependency, Dart2JsLibraryDependencyMirror> mirrorMap =
80 <LibraryDependency, Dart2JsLibraryDependencyMirror>{};
81
82 void addLibraryDependency(LibraryDependency libraryDependency,
83 LibraryElement targetLibraryElement) {
84 assert(targetLibraryElement != null);
85 LibraryMirror targetLibrary =
86 mirrorSystem._getLibrary(targetLibraryElement);
87 mirrorMap[libraryDependency] = new Dart2JsLibraryDependencyMirror(
88 libraryDependency, this, targetLibrary);
89 }
90 for (ImportElement import in _element.imports) {
91 addLibraryDependency(import.node, import.importedLibrary);
92 }
93 for (ExportElement export in _element.exports) {
94 addLibraryDependency(export.node, export.exportedLibrary);
95 }
96
97 _libraryDependencies = <LibraryDependencySourceMirror>[];
98
99 for (LibraryTag node in _element.tags) {
100 LibraryDependency libraryDependency = node.asLibraryDependency();
101 if (libraryDependency != null) {
102 Dart2JsLibraryDependencyMirror mirror = mirrorMap[libraryDependency];
103 assert(mirror != null);
104 _libraryDependencies.add(mirror);
105 }
106 }
107 }
108 }
109
110 List<LibraryDependencyMirror> get libraryDependencies {
111 _ensureLibraryDependenciesAnalyzed();
112 return _libraryDependencies;
113 }
114 }
115
116 class Dart2JsLibraryDependencyMirror implements LibraryDependencySourceMirror {
117 final LibraryDependency _node;
118 final Dart2JsLibraryMirror _sourceLibrary;
119 final Dart2JsLibraryMirror _targetLibrary;
120 List<CombinatorMirror> _combinators;
121
122 Dart2JsLibraryDependencyMirror(this._node,
123 this._sourceLibrary,
124 this._targetLibrary);
125
126 SourceLocation get location {
127 return new Dart2JsSourceLocation(
128 _sourceLibrary._element.entryCompilationUnit.script,
129 _sourceLibrary.mirrorSystem.compiler.reporter.spanFromSpannable(_node));
130 }
131
132 List<CombinatorMirror> get combinators {
133 if (_combinators == null) {
134 _combinators = <CombinatorMirror>[];
135 if (_node.combinators != null) {
136 for (Combinator combinator in _node.combinators.nodes) {
137 List<String> identifiers = <String>[];
138 for (Identifier identifier in combinator.identifiers.nodes) {
139 identifiers.add(identifier.source);
140 }
141 _combinators.add(new Dart2JsCombinatorMirror(
142 identifiers, isShow: combinator.isShow));
143 }
144 }
145 }
146 return _combinators;
147 }
148
149 LibraryMirror get sourceLibrary => _sourceLibrary;
150
151 LibraryMirror get targetLibrary => _targetLibrary;
152
153 /*String*/ get prefix {
154 Import import = _node.asImport();
155 if (import != null && import.prefix != null) {
156 return import.prefix.source;
157 }
158 return null;
159 }
160
161 bool get isImport => _node.asImport() != null;
162
163 bool get isExport => _node.asExport() != null;
164
165 bool get isDeferred {
166 if (_node is Import) {
167 Import import = _node;
168 return import.isDeferred;
169 }
170 return false;
171 }
172
173 List<InstanceMirror> get metadata => const <InstanceMirror>[];
174
175 /*Future<LibraryMirror>*/ loadLibrary() {
176 throw new UnsupportedError(
177 'LibraryDependencyMirror.loadLibrary unsupported.');
178 }
179 }
180
181 class Dart2JsCombinatorMirror implements CombinatorSourceMirror {
182 final List/*<String>*/ identifiers;
183 final bool isShow;
184
185 Dart2JsCombinatorMirror(this.identifiers, {bool isShow: true})
186 : this.isShow = isShow;
187
188 bool get isHide => !isShow;
189 }
190
191 class Dart2JsSourceLocation implements SourceLocation {
192 final Script _script;
193 final SourceSpan _span;
194 int _line;
195 int _column;
196
197 Dart2JsSourceLocation(this._script, this._span);
198
199 int _computeLine() {
200 var sourceFile = _script.file;
201 if (sourceFile != null) {
202 return sourceFile.getLine(offset) + 1;
203 }
204 var index = 0;
205 var lineNumber = 0;
206 while (index <= offset && index < sourceText.length) {
207 index = sourceText.indexOf('\n', index) + 1;
208 if (index <= 0) break;
209 lineNumber++;
210 }
211 return lineNumber;
212 }
213
214 int get line {
215 if (_line == null) {
216 _line = _computeLine();
217 }
218 return _line;
219 }
220
221 int _computeColumn() {
222 if (length == 0) return 0;
223
224 var sourceFile = _script.file;
225 if (sourceFile != null) {
226 return sourceFile.getColumn(sourceFile.getLine(offset), offset) + 1;
227 }
228 int index = offset - 1;
229 var columnNumber = 0;
230 while (0 <= index && index < sourceText.length) {
231 columnNumber++;
232 var codeUnit = sourceText.codeUnitAt(index);
233 if (codeUnit == $CR || codeUnit == $LF) {
234 break;
235 }
236 index--;
237 }
238 return columnNumber;
239 }
240
241 int get column {
242 if (_column == null) {
243 _column = _computeColumn();
244 }
245 return _column;
246 }
247
248 int get offset => _span.begin;
249
250 int get length => _span.end - _span.begin;
251
252 String get text => _script.text.substring(_span.begin, _span.end);
253
254 Uri get sourceUri => _script.resourceUri;
255
256 String get sourceText => _script.text;
257 }
OLDNEW
« no previous file with comments | « pkg/compiler/lib/src/mirrors/dart2js_instance_mirrors.dart ('k') | pkg/compiler/lib/src/mirrors/dart2js_member_mirrors.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698