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