| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 library dart2js.mirrors; | 5 library dart2js.mirrors; |
| 6 | 6 |
| 7 import 'dart:collection' show UnmodifiableListView, UnmodifiableMapView; | 7 import 'dart:collection' show UnmodifiableListView, UnmodifiableMapView; |
| 8 | 8 |
| 9 import '../elements/elements.dart'; | 9 import '../elements/elements.dart'; |
| 10 import '../scanner/scannerlib.dart'; | 10 import '../scanner/scannerlib.dart'; |
| (...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 159 bool get isNameSynthetic => false; | 159 bool get isNameSynthetic => false; |
| 160 | 160 |
| 161 /** | 161 /** |
| 162 * Computes the first token for this declaration using the begin token of the | 162 * Computes the first token for this declaration using the begin token of the |
| 163 * element node or element position as indicator. | 163 * element node or element position as indicator. |
| 164 */ | 164 */ |
| 165 Token getBeginToken() { | 165 Token getBeginToken() { |
| 166 // TODO(johnniwinther): Avoid calling [parseNode]. | 166 // TODO(johnniwinther): Avoid calling [parseNode]. |
| 167 Node node = _element.parseNode(mirrorSystem.compiler); | 167 Node node = _element.parseNode(mirrorSystem.compiler); |
| 168 if (node == null) { | 168 if (node == null) { |
| 169 return _element.position(); | 169 return _element.position; |
| 170 } | 170 } |
| 171 return node.getBeginToken(); | 171 return node.getBeginToken(); |
| 172 } | 172 } |
| 173 | 173 |
| 174 /** | 174 /** |
| 175 * Computes the last token for this declaration using the end token of the | 175 * Computes the last token for this declaration using the end token of the |
| 176 * element node or element position as indicator. | 176 * element node or element position as indicator. |
| 177 */ | 177 */ |
| 178 Token getEndToken() { | 178 Token getEndToken() { |
| 179 // TODO(johnniwinther): Avoid calling [parseNode]. | 179 // TODO(johnniwinther): Avoid calling [parseNode]. |
| 180 Node node = _element.parseNode(mirrorSystem.compiler); | 180 Node node = _element.parseNode(mirrorSystem.compiler); |
| 181 if (node == null) { | 181 if (node == null) { |
| 182 return _element.position(); | 182 return _element.position; |
| 183 } | 183 } |
| 184 return node.getEndToken(); | 184 return node.getEndToken(); |
| 185 } | 185 } |
| 186 | 186 |
| 187 /** | 187 /** |
| 188 * Returns the first token for the source of this declaration, including | 188 * Returns the first token for the source of this declaration, including |
| 189 * metadata annotations. | 189 * metadata annotations. |
| 190 */ | 190 */ |
| 191 Token getFirstToken() { | 191 Token getFirstToken() { |
| 192 if (!_element.metadata.isEmpty) { | 192 if (!_element.metadata.isEmpty) { |
| 193 for (MetadataAnnotation metadata in _element.metadata) { | 193 for (MetadataAnnotation metadata in _element.metadata) { |
| 194 if (metadata.beginToken != null) { | 194 if (metadata.beginToken != null) { |
| 195 return metadata.beginToken; | 195 return metadata.beginToken; |
| 196 } | 196 } |
| 197 } | 197 } |
| 198 } | 198 } |
| 199 return getBeginToken(); | 199 return getBeginToken(); |
| 200 } | 200 } |
| 201 | 201 |
| 202 Script getScript() => _element.getCompilationUnit().script; | 202 Script getScript() => _element.compilationUnit.script; |
| 203 | 203 |
| 204 SourceLocation get location { | 204 SourceLocation get location { |
| 205 Token beginToken = getFirstToken(); | 205 Token beginToken = getFirstToken(); |
| 206 Script script = getScript(); | 206 Script script = getScript(); |
| 207 SourceSpan span; | 207 SourceSpan span; |
| 208 if (beginToken == null) { | 208 if (beginToken == null) { |
| 209 span = new SourceSpan(script.readableUri, 0, 0); | 209 span = new SourceSpan(script.readableUri, 0, 0); |
| 210 } else { | 210 } else { |
| 211 Token endToken = getEndToken(); | 211 Token endToken = getEndToken(); |
| 212 span = mirrorSystem.compiler.spanFromTokens( | 212 span = mirrorSystem.compiler.spanFromTokens( |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 244 DeclarationMirror lookupInScope(String name) { | 244 DeclarationMirror lookupInScope(String name) { |
| 245 // TODO(11653): Support lookup of constructors. | 245 // TODO(11653): Support lookup of constructors. |
| 246 Scope scope = _element.buildScope(); | 246 Scope scope = _element.buildScope(); |
| 247 Element result; | 247 Element result; |
| 248 int index = name.indexOf('.'); | 248 int index = name.indexOf('.'); |
| 249 if (index != -1) { | 249 if (index != -1) { |
| 250 // Lookup [: prefix.id :]. | 250 // Lookup [: prefix.id :]. |
| 251 String prefix = name.substring(0, index); | 251 String prefix = name.substring(0, index); |
| 252 String id = name.substring(index+1); | 252 String id = name.substring(index+1); |
| 253 result = scope.lookup(prefix); | 253 result = scope.lookup(prefix); |
| 254 if (result != null && result.isPrefix()) { | 254 if (result != null && result.isPrefix) { |
| 255 PrefixElement prefix = result; | 255 PrefixElement prefix = result; |
| 256 result = prefix.lookupLocalMember(id); | 256 result = prefix.lookupLocalMember(id); |
| 257 } else { | 257 } else { |
| 258 result = null; | 258 result = null; |
| 259 } | 259 } |
| 260 } else { | 260 } else { |
| 261 // Lookup [: id :]. | 261 // Lookup [: id :]. |
| 262 result = scope.lookup(name); | 262 result = scope.lookup(name); |
| 263 } | 263 } |
| 264 if (result == null || result.isPrefix()) return null; | 264 if (result == null || result.isPrefix) return null; |
| 265 return _convertElementToDeclarationMirror(mirrorSystem, result); | 265 return _convertElementToDeclarationMirror(mirrorSystem, result); |
| 266 } | 266 } |
| 267 | 267 |
| 268 bool operator ==(var other) { | 268 bool operator ==(var other) { |
| 269 if (identical(this, other)) return true; | 269 if (identical(this, other)) return true; |
| 270 if (other == null) return false; | 270 if (other == null) return false; |
| 271 if (other is! Dart2JsElementMirror) return false; | 271 if (other is! Dart2JsElementMirror) return false; |
| 272 return _element == other._element && | 272 return _element == other._element && |
| 273 owner == other.owner; | 273 owner == other.owner; |
| 274 } | 274 } |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 347 } else { | 347 } else { |
| 348 return new Dart2JsTypedefMirror(this, type); | 348 return new Dart2JsTypedefMirror(this, type); |
| 349 } | 349 } |
| 350 } | 350 } |
| 351 compiler.internalError(type.element, | 351 compiler.internalError(type.element, |
| 352 "Unexpected type $type of kind ${type.kind}."); | 352 "Unexpected type $type of kind ${type.kind}."); |
| 353 return null; | 353 return null; |
| 354 } | 354 } |
| 355 | 355 |
| 356 DeclarationMirror _getTypeDeclarationMirror(TypeDeclarationElement element) { | 356 DeclarationMirror _getTypeDeclarationMirror(TypeDeclarationElement element) { |
| 357 if (element.isClass()) { | 357 if (element.isClass) { |
| 358 return new Dart2JsClassDeclarationMirror(this, element.thisType); | 358 return new Dart2JsClassDeclarationMirror(this, element.thisType); |
| 359 } else if (element.isTypedef()) { | 359 } else if (element.isTypedef) { |
| 360 return new Dart2JsTypedefDeclarationMirror(this, element.thisType); | 360 return new Dart2JsTypedefDeclarationMirror(this, element.thisType); |
| 361 } | 361 } |
| 362 compiler.internalError(element, "Unexpected element $element."); | 362 compiler.internalError(element, "Unexpected element $element."); |
| 363 return null; | 363 return null; |
| 364 } | 364 } |
| 365 } | 365 } |
| 366 | 366 |
| 367 abstract class ContainerMixin { | 367 abstract class ContainerMixin { |
| 368 UnmodifiableMapView<Symbol, DeclarationMirror> _declarations; | 368 UnmodifiableMapView<Symbol, DeclarationMirror> _declarations; |
| 369 | 369 |
| (...skipping 27 matching lines...) Expand all Loading... |
| 397 } | 397 } |
| 398 | 398 |
| 399 /** | 399 /** |
| 400 * Converts [element] into its corresponding [DeclarationMirror], if any. | 400 * Converts [element] into its corresponding [DeclarationMirror], if any. |
| 401 * | 401 * |
| 402 * If [element] is an [AbstractFieldElement] the mirror for its getter is | 402 * If [element] is an [AbstractFieldElement] the mirror for its getter is |
| 403 * returned or, if not present, the mirror for its setter. | 403 * returned or, if not present, the mirror for its setter. |
| 404 */ | 404 */ |
| 405 DeclarationMirror _convertElementToDeclarationMirror(Dart2JsMirrorSystem system, | 405 DeclarationMirror _convertElementToDeclarationMirror(Dart2JsMirrorSystem system, |
| 406 Element element) { | 406 Element element) { |
| 407 if (element.isTypeVariable()) { | 407 if (element.isTypeVariable) { |
| 408 TypeVariableElement typeVariable = element; | 408 TypeVariableElement typeVariable = element; |
| 409 return new Dart2JsTypeVariableMirror(system, typeVariable.type); | 409 return new Dart2JsTypeVariableMirror(system, typeVariable.type); |
| 410 } | 410 } |
| 411 | 411 |
| 412 Dart2JsLibraryMirror library = system._libraryMap[element.getLibrary()]; | 412 Dart2JsLibraryMirror library = system._libraryMap[element.library]; |
| 413 if (element.isLibrary()) return library; | 413 if (element.isLibrary) return library; |
| 414 if (element.isTypedef()) { | 414 if (element.isTypedef) { |
| 415 TypedefElement typedefElement = element; | 415 TypedefElement typedefElement = element; |
| 416 return new Dart2JsTypedefMirror.fromLibrary( | 416 return new Dart2JsTypedefMirror.fromLibrary( |
| 417 library, typedefElement.thisType); | 417 library, typedefElement.thisType); |
| 418 } | 418 } |
| 419 | 419 |
| 420 Dart2JsDeclarationMirror container = library; | 420 Dart2JsDeclarationMirror container = library; |
| 421 if (element.getEnclosingClass() != null) { | 421 if (element.enclosingClass != null) { |
| 422 container = system._getTypeDeclarationMirror(element.getEnclosingClass()); | 422 container = system._getTypeDeclarationMirror(element.enclosingClass); |
| 423 } | 423 } |
| 424 if (element.isClass()) return container; | 424 if (element.isClass) return container; |
| 425 if (element.isParameter()) { | 425 if (element.isParameter) { |
| 426 Dart2JsMethodMirror method = _convertElementMethodToMethodMirror( | 426 Dart2JsMethodMirror method = _convertElementMethodToMethodMirror( |
| 427 container, element.getOutermostEnclosingMemberOrTopLevel()); | 427 container, element.outermostEnclosingMemberOrTopLevel); |
| 428 // TODO(johnniwinther): Find the right info for [isOptional] and [isNamed]. | 428 // TODO(johnniwinther): Find the right info for [isOptional] and [isNamed]. |
| 429 return new Dart2JsParameterMirror( | 429 return new Dart2JsParameterMirror( |
| 430 method, element, isOptional: false, isNamed: false); | 430 method, element, isOptional: false, isNamed: false); |
| 431 } | 431 } |
| 432 Iterable<DeclarationMirror> members = | 432 Iterable<DeclarationMirror> members = |
| 433 container._getDeclarationMirrors(element); | 433 container._getDeclarationMirrors(element); |
| 434 if (members.isEmpty) return null; | 434 if (members.isEmpty) return null; |
| 435 return members.first; | 435 return members.first; |
| 436 } | 436 } |
| 437 | 437 |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 506 } | 506 } |
| 507 | 507 |
| 508 static ResolvedNode defaultValueSyntaxOf(Dart2JsParameterMirror parameter) { | 508 static ResolvedNode defaultValueSyntaxOf(Dart2JsParameterMirror parameter) { |
| 509 if (!parameter.hasDefaultValue) return null; | 509 if (!parameter.hasDefaultValue) return null; |
| 510 var node = parameter._element.initializer; | 510 var node = parameter._element.initializer; |
| 511 return new ResolvedNode(node, | 511 return new ResolvedNode(node, |
| 512 (parameter.owner as Dart2JsElementMirror)._element.treeElements, | 512 (parameter.owner as Dart2JsElementMirror)._element.treeElements, |
| 513 parameter.mirrorSystem); | 513 parameter.mirrorSystem); |
| 514 } | 514 } |
| 515 } | 515 } |
| OLD | NEW |