| OLD | NEW |
| 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, 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 analyzer.src.dart.element.element; | 5 library analyzer.src.dart.element.element; |
| 6 | 6 |
| 7 import 'dart:collection'; | 7 import 'dart:collection'; |
| 8 import 'dart:math' show min; | 8 import 'dart:math' show min; |
| 9 | 9 |
| 10 import 'package:analyzer/dart/ast/ast.dart'; | 10 import 'package:analyzer/dart/ast/ast.dart'; |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 64 */ | 64 */ |
| 65 class ClassElementImpl extends ElementImpl | 65 class ClassElementImpl extends ElementImpl |
| 66 with TypeParameterizedElementMixin | 66 with TypeParameterizedElementMixin |
| 67 implements ClassElement { | 67 implements ClassElement { |
| 68 /** | 68 /** |
| 69 * The unlinked representation of the class in the summary. | 69 * The unlinked representation of the class in the summary. |
| 70 */ | 70 */ |
| 71 final UnlinkedClass _unlinkedClass; | 71 final UnlinkedClass _unlinkedClass; |
| 72 | 72 |
| 73 /** | 73 /** |
| 74 * The unlinked representation of the enumeration in the summary. |
| 75 */ |
| 76 final UnlinkedEnum _unlinkedEnum; |
| 77 |
| 78 /** |
| 74 * A list containing all of the accessors (getters and setters) contained in | 79 * A list containing all of the accessors (getters and setters) contained in |
| 75 * this class. | 80 * this class. |
| 76 */ | 81 */ |
| 77 List<PropertyAccessorElement> _accessors = PropertyAccessorElement.EMPTY_LIST; | 82 List<PropertyAccessorElement> _accessors = PropertyAccessorElement.EMPTY_LIST; |
| 78 | 83 |
| 79 /** | 84 /** |
| 80 * For classes which are not mixin applications, a list containing all of the | 85 * For classes which are not mixin applications, a list containing all of the |
| 81 * constructors contained in this class, or `null` if the list of | 86 * constructors contained in this class, or `null` if the list of |
| 82 * constructors has not yet been built. | 87 * constructors has not yet been built. |
| 83 * | 88 * |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 131 * this class have been inferred. | 136 * this class have been inferred. |
| 132 */ | 137 */ |
| 133 bool hasBeenInferred = false; | 138 bool hasBeenInferred = false; |
| 134 | 139 |
| 135 /** | 140 /** |
| 136 * Initialize a newly created class element to have the given [name] at the | 141 * Initialize a newly created class element to have the given [name] at the |
| 137 * given [offset] in the file that contains the declaration of this element. | 142 * given [offset] in the file that contains the declaration of this element. |
| 138 */ | 143 */ |
| 139 ClassElementImpl(String name, int offset) | 144 ClassElementImpl(String name, int offset) |
| 140 : _unlinkedClass = null, | 145 : _unlinkedClass = null, |
| 146 _unlinkedEnum = null, |
| 141 super(name, offset); | 147 super(name, offset); |
| 142 | 148 |
| 143 /** | 149 /** |
| 144 * Initialize a newly created class element to have the given [name]. | 150 * Initialize a newly created class element to have the given [name]. |
| 145 */ | 151 */ |
| 146 ClassElementImpl.forNode(Identifier name) | 152 ClassElementImpl.forNode(Identifier name) |
| 147 : _unlinkedClass = null, | 153 : _unlinkedClass = null, |
| 154 _unlinkedEnum = null, |
| 148 super.forNode(name); | 155 super.forNode(name); |
| 149 | 156 |
| 150 /** | 157 /** |
| 151 * Initialize using the given serialized information. | 158 * Initialize using the given serialized information. |
| 152 */ | 159 */ |
| 153 ClassElementImpl.forSerialized( | 160 ClassElementImpl.forSerialized(this._unlinkedClass, this._unlinkedEnum, |
| 154 this._unlinkedClass, CompilationUnitElementImpl enclosingUnit) | 161 CompilationUnitElementImpl enclosingUnit) |
| 155 : super.forSerialized(enclosingUnit); | 162 : super.forSerialized(enclosingUnit); |
| 156 | 163 |
| 157 /** | 164 /** |
| 158 * Set whether this class is abstract. | 165 * Set whether this class is abstract. |
| 159 */ | 166 */ |
| 160 void set abstract(bool isAbstract) { | 167 void set abstract(bool isAbstract) { |
| 161 assert(_unlinkedClass == null); | 168 assert(_unlinkedClass == null && _unlinkedEnum == null); |
| 162 setModifier(Modifier.ABSTRACT, isAbstract); | 169 setModifier(Modifier.ABSTRACT, isAbstract); |
| 163 } | 170 } |
| 164 | 171 |
| 165 @override | 172 @override |
| 166 List<PropertyAccessorElement> get accessors => _accessors; | 173 List<PropertyAccessorElement> get accessors => _accessors; |
| 167 | 174 |
| 168 /** | 175 /** |
| 169 * Set the accessors contained in this class to the given [accessors]. | 176 * Set the accessors contained in this class to the given [accessors]. |
| 170 */ | 177 */ |
| 171 void set accessors(List<PropertyAccessorElement> accessors) { | 178 void set accessors(List<PropertyAccessorElement> accessors) { |
| 172 for (PropertyAccessorElement accessor in accessors) { | 179 for (PropertyAccessorElement accessor in accessors) { |
| 173 (accessor as PropertyAccessorElementImpl).enclosingElement = this; | 180 (accessor as PropertyAccessorElementImpl).enclosingElement = this; |
| 174 } | 181 } |
| 175 this._accessors = accessors; | 182 this._accessors = accessors; |
| 176 } | 183 } |
| 177 | 184 |
| 178 @override | 185 @override |
| 179 List<InterfaceType> get allSupertypes { | 186 List<InterfaceType> get allSupertypes { |
| 180 List<InterfaceType> list = new List<InterfaceType>(); | 187 List<InterfaceType> list = new List<InterfaceType>(); |
| 181 _collectAllSupertypes(list); | 188 _collectAllSupertypes(list); |
| 182 return list; | 189 return list; |
| 183 } | 190 } |
| 184 | 191 |
| 185 @override | 192 @override |
| 186 int get codeLength { | 193 int get codeLength { |
| 187 if (_unlinkedClass != null) { | 194 if (_unlinkedClass != null) { |
| 188 return _unlinkedClass.codeRange?.length; | 195 return _unlinkedClass.codeRange?.length; |
| 189 } | 196 } |
| 197 if (_unlinkedEnum != null) { |
| 198 return _unlinkedEnum.codeRange?.length; |
| 199 } |
| 190 return super.codeLength; | 200 return super.codeLength; |
| 191 } | 201 } |
| 192 | 202 |
| 193 @override | 203 @override |
| 194 int get codeOffset { | 204 int get codeOffset { |
| 195 if (_unlinkedClass != null) { | 205 if (_unlinkedClass != null) { |
| 196 return _unlinkedClass.codeRange?.offset; | 206 return _unlinkedClass.codeRange?.offset; |
| 197 } | 207 } |
| 208 if (_unlinkedEnum != null) { |
| 209 return _unlinkedEnum.codeRange?.offset; |
| 210 } |
| 198 return super.codeOffset; | 211 return super.codeOffset; |
| 199 } | 212 } |
| 200 | 213 |
| 201 @override | 214 @override |
| 202 List<ConstructorElement> get constructors { | 215 List<ConstructorElement> get constructors { |
| 203 if (!isMixinApplication) { | 216 if (!isMixinApplication) { |
| 204 assert(_constructors != null); | 217 assert(_constructors != null); |
| 205 return _constructors ?? ConstructorElement.EMPTY_LIST; | 218 return _constructors ?? ConstructorElement.EMPTY_LIST; |
| 206 } | 219 } |
| 207 return _computeMixinAppConstructors(); | 220 return _computeMixinAppConstructors(); |
| (...skipping 17 matching lines...) Expand all Loading... |
| 225 | 238 |
| 226 @override | 239 @override |
| 227 SourceRange get docRange { | 240 SourceRange get docRange { |
| 228 if (_unlinkedClass != null) { | 241 if (_unlinkedClass != null) { |
| 229 UnlinkedDocumentationComment comment = | 242 UnlinkedDocumentationComment comment = |
| 230 _unlinkedClass.documentationComment; | 243 _unlinkedClass.documentationComment; |
| 231 return comment != null | 244 return comment != null |
| 232 ? new SourceRange(comment.offset, comment.length) | 245 ? new SourceRange(comment.offset, comment.length) |
| 233 : null; | 246 : null; |
| 234 } | 247 } |
| 248 if (_unlinkedEnum != null) { |
| 249 UnlinkedDocumentationComment comment = _unlinkedEnum.documentationComment; |
| 250 return comment != null |
| 251 ? new SourceRange(comment.offset, comment.length) |
| 252 : null; |
| 253 } |
| 235 return super.docRange; | 254 return super.docRange; |
| 236 } | 255 } |
| 237 | 256 |
| 238 @override | 257 @override |
| 239 String get documentationComment { | 258 String get documentationComment { |
| 240 if (_unlinkedClass != null) { | 259 if (_unlinkedClass != null) { |
| 241 return _unlinkedClass?.documentationComment?.text; | 260 return _unlinkedClass?.documentationComment?.text; |
| 242 } | 261 } |
| 262 if (_unlinkedEnum != null) { |
| 263 return _unlinkedEnum?.documentationComment?.text; |
| 264 } |
| 243 return super.documentationComment; | 265 return super.documentationComment; |
| 244 } | 266 } |
| 245 | 267 |
| 246 /** | 268 /** |
| 247 * Return `true` if [CompileTimeErrorCode.MIXIN_HAS_NO_CONSTRUCTORS] should | 269 * Return `true` if [CompileTimeErrorCode.MIXIN_HAS_NO_CONSTRUCTORS] should |
| 248 * be reported for this class. | 270 * be reported for this class. |
| 249 */ | 271 */ |
| 250 bool get doesMixinLackConstructors { | 272 bool get doesMixinLackConstructors { |
| 251 if (!isMixinApplication && mixins.isEmpty) { | 273 if (!isMixinApplication && mixins.isEmpty) { |
| 252 // This class is not a mixin application and it doesn't have a "with" | 274 // This class is not a mixin application and it doesn't have a "with" |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 287 return !nearestNonMixinClass.constructors.any(isSuperConstructorAccessible); | 309 return !nearestNonMixinClass.constructors.any(isSuperConstructorAccessible); |
| 288 } | 310 } |
| 289 | 311 |
| 290 @override | 312 @override |
| 291 TypeParameterizedElementMixin get enclosingTypeParameterContext => null; | 313 TypeParameterizedElementMixin get enclosingTypeParameterContext => null; |
| 292 | 314 |
| 293 /** | 315 /** |
| 294 * Set whether this class is defined by an enum declaration. | 316 * Set whether this class is defined by an enum declaration. |
| 295 */ | 317 */ |
| 296 void set enum2(bool isEnum) { | 318 void set enum2(bool isEnum) { |
| 319 assert(_unlinkedClass == null && _unlinkedEnum == null); |
| 297 setModifier(Modifier.ENUM, isEnum); | 320 setModifier(Modifier.ENUM, isEnum); |
| 298 } | 321 } |
| 299 | 322 |
| 300 @override | 323 @override |
| 301 List<FieldElement> get fields => _fields; | 324 List<FieldElement> get fields => _fields; |
| 302 | 325 |
| 303 /** | 326 /** |
| 304 * Set the fields contained in this class to the given [fields]. | 327 * Set the fields contained in this class to the given [fields]. |
| 305 */ | 328 */ |
| 306 void set fields(List<FieldElement> fields) { | 329 void set fields(List<FieldElement> fields) { |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 376 if (_unlinkedClass != null && _interfaces == null) { | 399 if (_unlinkedClass != null && _interfaces == null) { |
| 377 ResynthesizerContext context = enclosingUnit.resynthesizerContext; | 400 ResynthesizerContext context = enclosingUnit.resynthesizerContext; |
| 378 _interfaces = _unlinkedClass.interfaces | 401 _interfaces = _unlinkedClass.interfaces |
| 379 .map((EntityRef t) => context.resolveTypeRef(t, this)) | 402 .map((EntityRef t) => context.resolveTypeRef(t, this)) |
| 380 .toList(growable: false); | 403 .toList(growable: false); |
| 381 } | 404 } |
| 382 return _interfaces ?? const <InterfaceType>[]; | 405 return _interfaces ?? const <InterfaceType>[]; |
| 383 } | 406 } |
| 384 | 407 |
| 385 void set interfaces(List<InterfaceType> interfaces) { | 408 void set interfaces(List<InterfaceType> interfaces) { |
| 386 assert(_unlinkedClass == null); | 409 assert(_unlinkedClass == null && _unlinkedEnum == null); |
| 387 _interfaces = interfaces; | 410 _interfaces = interfaces; |
| 388 } | 411 } |
| 389 | 412 |
| 390 @override | 413 @override |
| 391 bool get isAbstract { | 414 bool get isAbstract { |
| 392 if (_unlinkedClass != null) { | 415 if (_unlinkedClass != null) { |
| 393 return _unlinkedClass.isAbstract; | 416 return _unlinkedClass.isAbstract; |
| 394 } | 417 } |
| 418 if (_unlinkedEnum != null) { |
| 419 return false; |
| 420 } |
| 395 return hasModifier(Modifier.ABSTRACT); | 421 return hasModifier(Modifier.ABSTRACT); |
| 396 } | 422 } |
| 397 | 423 |
| 398 @override | 424 @override |
| 399 bool get isEnum => hasModifier(Modifier.ENUM); | 425 bool get isEnum { |
| 426 if (_unlinkedClass != null) { |
| 427 return false; |
| 428 } |
| 429 if (_unlinkedEnum != null) { |
| 430 return true; |
| 431 } |
| 432 return hasModifier(Modifier.ENUM); |
| 433 } |
| 400 | 434 |
| 401 @override | 435 @override |
| 402 bool get isMixinApplication { | 436 bool get isMixinApplication { |
| 403 if (_unlinkedClass != null) { | 437 if (_unlinkedClass != null) { |
| 404 return _unlinkedClass.isMixinApplication; | 438 return _unlinkedClass.isMixinApplication; |
| 405 } | 439 } |
| 440 if (_unlinkedEnum != null) { |
| 441 return false; |
| 442 } |
| 406 return hasModifier(Modifier.MIXIN_APPLICATION); | 443 return hasModifier(Modifier.MIXIN_APPLICATION); |
| 407 } | 444 } |
| 408 | 445 |
| 409 @override | 446 @override |
| 410 bool get isOrInheritsProxy => | 447 bool get isOrInheritsProxy => |
| 411 _safeIsOrInheritsProxy(this, new HashSet<ClassElement>()); | 448 _safeIsOrInheritsProxy(this, new HashSet<ClassElement>()); |
| 412 | 449 |
| 413 @override | 450 @override |
| 414 bool get isProxy { | 451 bool get isProxy { |
| 415 for (ElementAnnotation annotation in metadata) { | 452 for (ElementAnnotation annotation in metadata) { |
| (...skipping 24 matching lines...) Expand all Loading... |
| 440 | 477 |
| 441 @override | 478 @override |
| 442 ElementKind get kind => ElementKind.CLASS; | 479 ElementKind get kind => ElementKind.CLASS; |
| 443 | 480 |
| 444 @override | 481 @override |
| 445 List<ElementAnnotation> get metadata { | 482 List<ElementAnnotation> get metadata { |
| 446 if (_unlinkedClass != null) { | 483 if (_unlinkedClass != null) { |
| 447 return _metadata ??= | 484 return _metadata ??= |
| 448 _buildAnnotations(enclosingUnit, _unlinkedClass.annotations); | 485 _buildAnnotations(enclosingUnit, _unlinkedClass.annotations); |
| 449 } | 486 } |
| 487 if (_unlinkedEnum != null) { |
| 488 return _metadata ??= |
| 489 _buildAnnotations(enclosingUnit, _unlinkedEnum.annotations); |
| 490 } |
| 450 return super.metadata; | 491 return super.metadata; |
| 451 } | 492 } |
| 452 | 493 |
| 453 @override | 494 @override |
| 454 List<MethodElement> get methods => _methods; | 495 List<MethodElement> get methods => _methods; |
| 455 | 496 |
| 456 /** | 497 /** |
| 457 * Set the methods contained in this class to the given [methods]. | 498 * Set the methods contained in this class to the given [methods]. |
| 458 */ | 499 */ |
| 459 void set methods(List<MethodElement> methods) { | 500 void set methods(List<MethodElement> methods) { |
| 501 assert(_unlinkedEnum == null); |
| 460 for (MethodElement method in methods) { | 502 for (MethodElement method in methods) { |
| 461 (method as MethodElementImpl).enclosingElement = this; | 503 (method as MethodElementImpl).enclosingElement = this; |
| 462 } | 504 } |
| 463 this._methods = methods; | 505 this._methods = methods; |
| 464 } | 506 } |
| 465 | 507 |
| 466 /** | 508 /** |
| 467 * Set whether this class is a mixin application. | 509 * Set whether this class is a mixin application. |
| 468 */ | 510 */ |
| 469 void set mixinApplication(bool isMixinApplication) { | 511 void set mixinApplication(bool isMixinApplication) { |
| 470 assert(_unlinkedClass == null); | 512 assert(_unlinkedClass == null); |
| 471 setModifier(Modifier.MIXIN_APPLICATION, isMixinApplication); | 513 setModifier(Modifier.MIXIN_APPLICATION, isMixinApplication); |
| 472 } | 514 } |
| 473 | 515 |
| 474 @override | 516 @override |
| 475 List<InterfaceType> get mixins { | 517 List<InterfaceType> get mixins { |
| 476 if (_unlinkedClass != null && _mixins == null) { | 518 if (_unlinkedClass != null && _mixins == null) { |
| 477 ResynthesizerContext context = enclosingUnit.resynthesizerContext; | 519 ResynthesizerContext context = enclosingUnit.resynthesizerContext; |
| 478 _mixins = _unlinkedClass.mixins | 520 _mixins = _unlinkedClass.mixins |
| 479 .map((EntityRef t) => context.resolveTypeRef(t, this)) | 521 .map((EntityRef t) => context.resolveTypeRef(t, this)) |
| 480 .toList(growable: false); | 522 .toList(growable: false); |
| 481 } | 523 } |
| 482 return _mixins ?? const <InterfaceType>[]; | 524 return _mixins ?? const <InterfaceType>[]; |
| 483 } | 525 } |
| 484 | 526 |
| 485 void set mixins(List<InterfaceType> mixins) { | 527 void set mixins(List<InterfaceType> mixins) { |
| 486 assert(_unlinkedClass == null); | 528 assert(_unlinkedClass == null && _unlinkedEnum == null); |
| 487 _mixins = mixins; | 529 _mixins = mixins; |
| 488 } | 530 } |
| 489 | 531 |
| 490 @override | 532 @override |
| 491 String get name { | 533 String get name { |
| 492 if (_unlinkedClass != null) { | 534 if (_unlinkedClass != null) { |
| 493 return _unlinkedClass.name; | 535 return _unlinkedClass.name; |
| 494 } | 536 } |
| 537 if (_unlinkedEnum != null) { |
| 538 return _unlinkedEnum.name; |
| 539 } |
| 495 return super.name; | 540 return super.name; |
| 496 } | 541 } |
| 497 | 542 |
| 498 @override | 543 @override |
| 499 int get nameOffset { | 544 int get nameOffset { |
| 500 if (_unlinkedClass != null) { | 545 if (_unlinkedClass != null) { |
| 501 return _unlinkedClass.nameOffset; | 546 return _unlinkedClass.nameOffset; |
| 502 } | 547 } |
| 548 if (_unlinkedEnum != null) { |
| 549 return _unlinkedEnum.nameOffset; |
| 550 } |
| 503 return super.nameOffset; | 551 return super.nameOffset; |
| 504 } | 552 } |
| 505 | 553 |
| 506 @override | 554 @override |
| 507 TypeParameterizedElementMixin get typeParameterContext => this; | 555 TypeParameterizedElementMixin get typeParameterContext => this; |
| 508 | 556 |
| 509 @override | 557 @override |
| 510 List<TypeParameterElement> get typeParameters { | 558 List<TypeParameterElement> get typeParameters { |
| 511 if (_unlinkedClass != null) { | 559 if (_unlinkedClass != null) { |
| 512 return super.typeParameters; | 560 return super.typeParameters; |
| 513 } | 561 } |
| 514 return _typeParameters; | 562 return _typeParameters; |
| 515 } | 563 } |
| 516 | 564 |
| 517 /** | 565 /** |
| 518 * Set the type parameters defined for this class to the given | 566 * Set the type parameters defined for this class to the given |
| 519 * [typeParameters]. | 567 * [typeParameters]. |
| 520 */ | 568 */ |
| 521 void set typeParameters(List<TypeParameterElement> typeParameters) { | 569 void set typeParameters(List<TypeParameterElement> typeParameters) { |
| 522 assert(_unlinkedClass == null); | 570 assert(_unlinkedClass == null && _unlinkedEnum == null); |
| 523 for (TypeParameterElement typeParameter in typeParameters) { | 571 for (TypeParameterElement typeParameter in typeParameters) { |
| 524 (typeParameter as TypeParameterElementImpl).enclosingElement = this; | 572 (typeParameter as TypeParameterElementImpl).enclosingElement = this; |
| 525 } | 573 } |
| 526 this._typeParameters = typeParameters; | 574 this._typeParameters = typeParameters; |
| 527 } | 575 } |
| 528 | 576 |
| 529 @override | 577 @override |
| 530 List<UnlinkedTypeParam> get unlinkedTypeParams => | 578 List<UnlinkedTypeParam> get unlinkedTypeParams { |
| 531 _unlinkedClass.typeParameters; | 579 return _unlinkedClass?.typeParameters ?? const <UnlinkedTypeParam>[]; |
| 580 } |
| 532 | 581 |
| 533 @override | 582 @override |
| 534 ConstructorElement get unnamedConstructor { | 583 ConstructorElement get unnamedConstructor { |
| 535 for (ConstructorElement element in constructors) { | 584 for (ConstructorElement element in constructors) { |
| 536 String name = element.displayName; | 585 String name = element.displayName; |
| 537 if (name == null || name.isEmpty) { | 586 if (name == null || name.isEmpty) { |
| 538 return element; | 587 return element; |
| 539 } | 588 } |
| 540 } | 589 } |
| 541 return null; | 590 return null; |
| (...skipping 7153 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 7695 | 7744 |
| 7696 @override | 7745 @override |
| 7697 void visitElement(Element element) { | 7746 void visitElement(Element element) { |
| 7698 int offset = element.nameOffset; | 7747 int offset = element.nameOffset; |
| 7699 if (offset != -1) { | 7748 if (offset != -1) { |
| 7700 map[offset] = element; | 7749 map[offset] = element; |
| 7701 } | 7750 } |
| 7702 super.visitElement(element); | 7751 super.visitElement(element); |
| 7703 } | 7752 } |
| 7704 } | 7753 } |
| OLD | NEW |