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