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

Side by Side Diff: pkg/analyzer/lib/src/dart/element/element.dart

Issue 1917203002: Remove more type casts (Closed) Base URL: https://github.com/dart-lang/sdk.git@master
Patch Set: Created 4 years, 8 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
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 445 matching lines...) Expand 10 before | Expand all | Expand 10 after
456 } 456 }
457 457
458 @override 458 @override
459 ElementImpl getChild(String identifier) { 459 ElementImpl getChild(String identifier) {
460 // 460 //
461 // The casts in this method are safe because the set methods would have 461 // The casts in this method are safe because the set methods would have
462 // thrown a CCE if any of the elements in the arrays were not of the 462 // thrown a CCE if any of the elements in the arrays were not of the
463 // expected types. 463 // expected types.
464 // 464 //
465 for (PropertyAccessorElement accessor in _accessors) { 465 for (PropertyAccessorElement accessor in _accessors) {
466 if ((accessor as PropertyAccessorElementImpl).identifier == identifier) { 466 PropertyAccessorElementImpl accessorImpl = accessor;
scheglov 2016/04/26 16:05:29 Is this implicit cast OK for strong mode?
Brian Wilkerson 2016/04/26 17:09:59 I think so; I had strong mode enabled and there wa
467 return accessor as PropertyAccessorElementImpl; 467 if (accessorImpl.identifier == identifier) {
468 return accessorImpl;
468 } 469 }
469 } 470 }
470 for (ConstructorElement constructor in _constructors) { 471 for (ConstructorElement constructor in _constructors) {
471 if ((constructor as ConstructorElementImpl).identifier == identifier) { 472 ConstructorElementImpl constructorImpl = constructor;
472 return constructor as ConstructorElementImpl; 473 if (constructorImpl.identifier == identifier) {
474 return constructorImpl;
473 } 475 }
474 } 476 }
475 for (FieldElement field in _fields) { 477 for (FieldElement field in _fields) {
476 if ((field as FieldElementImpl).identifier == identifier) { 478 FieldElementImpl fieldImpl = field;
477 return field as FieldElementImpl; 479 if (fieldImpl.identifier == identifier) {
480 return fieldImpl;
478 } 481 }
479 } 482 }
480 for (MethodElement method in _methods) { 483 for (MethodElement method in _methods) {
481 if ((method as MethodElementImpl).identifier == identifier) { 484 MethodElementImpl methodImpl = method;
482 return method as MethodElementImpl; 485 if (methodImpl.identifier == identifier) {
486 return methodImpl;
483 } 487 }
484 } 488 }
485 for (TypeParameterElement typeParameter in _typeParameters) { 489 for (TypeParameterElement typeParameter in _typeParameters) {
486 if ((typeParameter as TypeParameterElementImpl).identifier == 490 TypeParameterElementImpl typeParameterImpl = typeParameter;
487 identifier) { 491 if (typeParameterImpl.identifier == identifier) {
488 return typeParameter as TypeParameterElementImpl; 492 return typeParameterImpl;
489 } 493 }
490 } 494 }
491 return null; 495 return null;
492 } 496 }
493 497
494 @override 498 @override
495 FieldElement getField(String name) { 499 FieldElement getField(String name) {
496 for (FieldElement fieldElement in _fields) { 500 for (FieldElement fieldElement in _fields) {
497 if (name == fieldElement.name) { 501 if (name == fieldElement.name) {
498 return fieldElement; 502 return fieldElement;
(...skipping 272 matching lines...) Expand 10 before | Expand all | Expand 10 after
771 } 775 }
772 return method; 776 return method;
773 } 777 }
774 778
775 PropertyAccessorElement _internalLookUpConcreteSetter( 779 PropertyAccessorElement _internalLookUpConcreteSetter(
776 String setterName, LibraryElement library, bool includeThisClass) { 780 String setterName, LibraryElement library, bool includeThisClass) {
777 PropertyAccessorElement setter = 781 PropertyAccessorElement setter =
778 _internalLookUpSetter(setterName, library, includeThisClass); 782 _internalLookUpSetter(setterName, library, includeThisClass);
779 while (setter != null && setter.isAbstract) { 783 while (setter != null && setter.isAbstract) {
780 Element definingClass = setter.enclosingElement; 784 Element definingClass = setter.enclosingElement;
781 if (definingClass is! ClassElementImpl) { 785 if (definingClass is ClassElementImpl) {
786 setter =
787 definingClass._internalLookUpSetter(setterName, library, false);
788 } else {
782 return null; 789 return null;
783 } 790 }
784 setter = (definingClass as ClassElementImpl)
785 ._internalLookUpSetter(setterName, library, false);
786 } 791 }
787 return setter; 792 return setter;
788 } 793 }
789 794
790 PropertyAccessorElement _internalLookUpGetter( 795 PropertyAccessorElement _internalLookUpGetter(
791 String getterName, LibraryElement library, bool includeThisClass) { 796 String getterName, LibraryElement library, bool includeThisClass) {
792 HashSet<ClassElement> visitedClasses = new HashSet<ClassElement>(); 797 HashSet<ClassElement> visitedClasses = new HashSet<ClassElement>();
793 ClassElement currentElement = this; 798 ClassElement currentElement = this;
794 if (includeThisClass) { 799 if (includeThisClass) {
795 PropertyAccessorElement element = currentElement.getGetter(getterName); 800 PropertyAccessorElement element = currentElement.getGetter(getterName);
(...skipping 351 matching lines...) Expand 10 before | Expand all | Expand 10 after
1147 } 1152 }
1148 1153
1149 @override 1154 @override
1150 ElementImpl getChild(String identifier) { 1155 ElementImpl getChild(String identifier) {
1151 // 1156 //
1152 // The casts in this method are safe because the set methods would have 1157 // The casts in this method are safe because the set methods would have
1153 // thrown a CCE if any of the elements in the arrays were not of the 1158 // thrown a CCE if any of the elements in the arrays were not of the
1154 // expected types. 1159 // expected types.
1155 // 1160 //
1156 for (PropertyAccessorElement accessor in _accessors) { 1161 for (PropertyAccessorElement accessor in _accessors) {
1157 if ((accessor as PropertyAccessorElementImpl).identifier == identifier) { 1162 PropertyAccessorElementImpl accessorImpl = accessor;
1158 return accessor as PropertyAccessorElementImpl; 1163 if (accessorImpl.identifier == identifier) {
1164 return accessorImpl;
1159 } 1165 }
1160 } 1166 }
1161 for (VariableElement variable in _variables) { 1167 for (TopLevelVariableElement variable in _variables) {
1162 if ((variable as VariableElementImpl).identifier == identifier) { 1168 TopLevelVariableElementImpl variableImpl = variable;
1163 return variable as VariableElementImpl; 1169 if (variableImpl.identifier == identifier) {
1170 return variableImpl;
1164 } 1171 }
1165 } 1172 }
1166 for (ExecutableElement function in _functions) { 1173 for (FunctionElement function in _functions) {
1167 if ((function as ExecutableElementImpl).identifier == identifier) { 1174 FunctionElementImpl functionImpl = function;
1168 return function as ExecutableElementImpl; 1175 if (functionImpl.identifier == identifier) {
1176 return functionImpl;
1169 } 1177 }
1170 } 1178 }
1171 for (FunctionTypeAliasElement typeAlias in _typeAliases) { 1179 for (FunctionTypeAliasElement typeAlias in _typeAliases) {
1172 if ((typeAlias as FunctionTypeAliasElementImpl).identifier == 1180 FunctionTypeAliasElementImpl typeAliasImpl = typeAlias;
1173 identifier) { 1181 if (typeAliasImpl.identifier == identifier) {
1174 return typeAlias as FunctionTypeAliasElementImpl; 1182 return typeAliasImpl;
1175 } 1183 }
1176 } 1184 }
1177 for (ClassElement type in _types) { 1185 for (ClassElement type in _types) {
1178 if ((type as ClassElementImpl).identifier == identifier) { 1186 ClassElementImpl typeImpl = type;
1179 return type as ClassElementImpl; 1187 if (typeImpl.identifier == identifier) {
1188 return typeImpl;
1180 } 1189 }
1181 } 1190 }
1182 for (ClassElement type in _enums) { 1191 for (ClassElement type in _enums) {
1183 if ((type as ClassElementImpl).identifier == identifier) { 1192 ClassElementImpl typeImpl = type;
1184 return type as ClassElementImpl; 1193 if (typeImpl.identifier == identifier) {
1194 return typeImpl;
1185 } 1195 }
1186 } 1196 }
1187 return null; 1197 return null;
1188 } 1198 }
1189 1199
1190 @override 1200 @override
1191 Element getElementAt(int offset) { 1201 Element getElementAt(int offset) {
1192 if (_offsetToElementMap.isEmpty) { 1202 if (_offsetToElementMap.isEmpty) {
1193 accept(new _BuildOffsetToElementMap(_offsetToElementMap)); 1203 accept(new _BuildOffsetToElementMap(_offsetToElementMap));
1194 } 1204 }
(...skipping 1132 matching lines...) Expand 10 before | Expand all | Expand 10 after
2327 result = JenkinsSmiHash.combine(result, component.hashCode); 2337 result = JenkinsSmiHash.combine(result, component.hashCode);
2328 } 2338 }
2329 return result; 2339 return result;
2330 } 2340 }
2331 2341
2332 @override 2342 @override
2333 bool operator ==(Object object) { 2343 bool operator ==(Object object) {
2334 if (identical(this, object)) { 2344 if (identical(this, object)) {
2335 return true; 2345 return true;
2336 } 2346 }
2337 if (object is! ElementLocationImpl) { 2347 if (object is ElementLocationImpl) {
2338 return false; 2348 List<String> otherComponents = object._components;
2339 } 2349 int length = _components.length;
2340 ElementLocationImpl location = object as ElementLocationImpl; 2350 if (otherComponents.length != length) {
2341 List<String> otherComponents = location._components;
2342 int length = _components.length;
2343 if (otherComponents.length != length) {
2344 return false;
2345 }
2346 for (int i = 0; i < length; i++) {
2347 if (_components[i] != otherComponents[i]) {
2348 return false; 2351 return false;
2349 } 2352 }
2353 for (int i = 0; i < length; i++) {
2354 if (_components[i] != otherComponents[i]) {
2355 return false;
2356 }
2357 }
2358 return true;
2350 } 2359 }
2351 return true; 2360 return false;
2352 } 2361 }
2353 2362
2354 @override 2363 @override
2355 String toString() => encoding; 2364 String toString() => encoding;
2356 2365
2357 /** 2366 /**
2358 * Decode the [encoding] of a location into a list of components and return 2367 * Decode the [encoding] of a location into a list of components and return
2359 * the components. 2368 * the components.
2360 */ 2369 */
2361 List<String> _decode(String encoding) { 2370 List<String> _decode(String encoding) {
(...skipping 225 matching lines...) Expand 10 before | Expand all | Expand 10 after
2587 buffer.write('>'); 2596 buffer.write('>');
2588 } 2597 }
2589 buffer.write("("); 2598 buffer.write("(");
2590 String closing = null; 2599 String closing = null;
2591 ParameterKind kind = ParameterKind.REQUIRED; 2600 ParameterKind kind = ParameterKind.REQUIRED;
2592 int parameterCount = _parameters.length; 2601 int parameterCount = _parameters.length;
2593 for (int i = 0; i < parameterCount; i++) { 2602 for (int i = 0; i < parameterCount; i++) {
2594 if (i > 0) { 2603 if (i > 0) {
2595 buffer.write(", "); 2604 buffer.write(", ");
2596 } 2605 }
2597 ParameterElementImpl parameter = _parameters[i] as ParameterElementImpl; 2606 ParameterElement parameter = _parameters[i];
2598 ParameterKind parameterKind = parameter.parameterKind; 2607 ParameterKind parameterKind = parameter.parameterKind;
2599 if (parameterKind != kind) { 2608 if (parameterKind != kind) {
2600 if (closing != null) { 2609 if (closing != null) {
2601 buffer.write(closing); 2610 buffer.write(closing);
2602 } 2611 }
2603 if (parameterKind == ParameterKind.POSITIONAL) { 2612 if (parameterKind == ParameterKind.POSITIONAL) {
2604 buffer.write("["); 2613 buffer.write("[");
2605 closing = "]"; 2614 closing = "]";
2606 } else if (parameterKind == ParameterKind.NAMED) { 2615 } else if (parameterKind == ParameterKind.NAMED) {
2607 buffer.write("{"); 2616 buffer.write("{");
(...skipping 11 matching lines...) Expand all
2619 buffer.write(")"); 2628 buffer.write(")");
2620 } 2629 }
2621 if (type != null) { 2630 if (type != null) {
2622 buffer.write(ElementImpl.RIGHT_ARROW); 2631 buffer.write(ElementImpl.RIGHT_ARROW);
2623 buffer.write(type.returnType); 2632 buffer.write(type.returnType);
2624 } 2633 }
2625 } 2634 }
2626 2635
2627 @override 2636 @override
2628 ElementImpl getChild(String identifier) { 2637 ElementImpl getChild(String identifier) {
2629 for (ExecutableElement function in _functions) { 2638 for (FunctionElement function in _functions) {
2630 if ((function as ExecutableElementImpl).identifier == identifier) { 2639 FunctionElementImpl functionImpl = function;
2631 return function as ExecutableElementImpl; 2640 if (functionImpl.identifier == identifier) {
2641 return functionImpl;
2632 } 2642 }
2633 } 2643 }
2634 for (LabelElement label in _labels) { 2644 for (LabelElement label in _labels) {
2635 if ((label as LabelElementImpl).identifier == identifier) { 2645 LabelElementImpl labelImpl = label;
2636 return label as LabelElementImpl; 2646 if (labelImpl.identifier == identifier) {
2647 return labelImpl;
2637 } 2648 }
2638 } 2649 }
2639 for (VariableElement variable in _localVariables) { 2650 for (LocalVariableElement variable in _localVariables) {
2640 if ((variable as VariableElementImpl).identifier == identifier) { 2651 LocalVariableElementImpl variableImpl = variable;
2641 return variable as VariableElementImpl; 2652 if (variableImpl.identifier == identifier) {
2653 return variableImpl;
2642 } 2654 }
2643 } 2655 }
2644 for (ParameterElement parameter in _parameters) { 2656 for (ParameterElement parameter in _parameters) {
2645 if ((parameter as ParameterElementImpl).identifier == identifier) { 2657 ParameterElementImpl parameterImpl = parameter;
2646 return parameter as ParameterElementImpl; 2658 if (parameterImpl.identifier == identifier) {
2659 return parameterImpl;
2647 } 2660 }
2648 } 2661 }
2649 return null; 2662 return null;
2650 } 2663 }
2651 2664
2652 @override 2665 @override
2653 void visitChildren(ElementVisitor visitor) { 2666 void visitChildren(ElementVisitor visitor) {
2654 super.visitChildren(visitor); 2667 super.visitChildren(visitor);
2655 safelyVisitChildren(_functions, visitor); 2668 safelyVisitChildren(_functions, visitor);
2656 safelyVisitChildren(_labels, visitor); 2669 safelyVisitChildren(_labels, visitor);
(...skipping 360 matching lines...) Expand 10 before | Expand all | Expand 10 after
3017 buffer.write(returnType); 3030 buffer.write(returnType);
3018 } 3031 }
3019 } 3032 }
3020 3033
3021 @override 3034 @override
3022 FunctionTypeAlias computeNode() => 3035 FunctionTypeAlias computeNode() =>
3023 getNodeMatching((node) => node is FunctionTypeAlias); 3036 getNodeMatching((node) => node is FunctionTypeAlias);
3024 3037
3025 @override 3038 @override
3026 ElementImpl getChild(String identifier) { 3039 ElementImpl getChild(String identifier) {
3027 for (VariableElement parameter in _parameters) { 3040 for (ParameterElement parameter in _parameters) {
3028 if ((parameter as VariableElementImpl).identifier == identifier) { 3041 ParameterElementImpl parameterImpl = parameter;
3029 return parameter as VariableElementImpl; 3042 if (parameterImpl.identifier == identifier) {
3043 return parameterImpl;
3030 } 3044 }
3031 } 3045 }
3032 for (TypeParameterElement typeParameter in _typeParameters) { 3046 for (TypeParameterElement typeParameter in _typeParameters) {
3033 if ((typeParameter as TypeParameterElementImpl).identifier == 3047 TypeParameterElementImpl typeParameterImpl = typeParameter;
3034 identifier) { 3048 if (typeParameterImpl.identifier == identifier) {
3035 return typeParameter as TypeParameterElementImpl; 3049 return typeParameterImpl;
3036 } 3050 }
3037 } 3051 }
3038 return null; 3052 return null;
3039 } 3053 }
3040 3054
3041 @override 3055 @override
3042 void visitChildren(ElementVisitor visitor) { 3056 void visitChildren(ElementVisitor visitor) {
3043 super.visitChildren(visitor); 3057 super.visitChildren(visitor);
3044 safelyVisitChildren(_parameters, visitor); 3058 safelyVisitChildren(_parameters, visitor);
3045 safelyVisitChildren(_typeParameters, visitor); 3059 safelyVisitChildren(_typeParameters, visitor);
(...skipping 544 matching lines...) Expand 10 before | Expand all | Expand 10 after
3590 new FunctionElementImpl(FunctionElement.LOAD_LIBRARY_NAME, -1); 3604 new FunctionElementImpl(FunctionElement.LOAD_LIBRARY_NAME, -1);
3591 function.synthetic = true; 3605 function.synthetic = true;
3592 function.enclosingElement = this; 3606 function.enclosingElement = this;
3593 function.returnType = typeProvider.futureDynamicType; 3607 function.returnType = typeProvider.futureDynamicType;
3594 function.type = new FunctionTypeImpl(function); 3608 function.type = new FunctionTypeImpl(function);
3595 _loadLibraryFunction = function; 3609 _loadLibraryFunction = function;
3596 } 3610 }
3597 3611
3598 @override 3612 @override
3599 ElementImpl getChild(String identifier) { 3613 ElementImpl getChild(String identifier) {
3600 if ((_definingCompilationUnit as CompilationUnitElementImpl).identifier == 3614 CompilationUnitElementImpl unitImpl = _definingCompilationUnit;
3601 identifier) { 3615 if (unitImpl.identifier == identifier) {
3602 return _definingCompilationUnit as CompilationUnitElementImpl; 3616 return unitImpl;
3603 } 3617 }
3604 for (CompilationUnitElement part in _parts) { 3618 for (CompilationUnitElement part in _parts) {
3605 if ((part as CompilationUnitElementImpl).identifier == identifier) { 3619 CompilationUnitElementImpl partImpl = part;
3606 return part as CompilationUnitElementImpl; 3620 if (partImpl.identifier == identifier) {
3621 return partImpl;
3607 } 3622 }
3608 } 3623 }
3609 for (ImportElement importElement in _imports) { 3624 for (ImportElement importElement in _imports) {
3610 if ((importElement as ImportElementImpl).identifier == identifier) { 3625 ImportElementImpl importElementImpl = importElement;
3611 return importElement as ImportElementImpl; 3626 if (importElementImpl.identifier == identifier) {
3627 return importElementImpl;
3612 } 3628 }
3613 } 3629 }
3614 for (ExportElement exportElement in _exports) { 3630 for (ExportElement exportElement in _exports) {
3615 if ((exportElement as ExportElementImpl).identifier == identifier) { 3631 ExportElementImpl exportElementImpl = exportElement;
3616 return exportElement as ExportElementImpl; 3632 if (exportElementImpl.identifier == identifier) {
3633 return exportElementImpl;
3617 } 3634 }
3618 } 3635 }
3619 return null; 3636 return null;
3620 } 3637 }
3621 3638
3622 @override 3639 @override
3623 List<ImportElement> getImportsWithPrefix(PrefixElement prefixElement) { 3640 List<ImportElement> getImportsWithPrefix(PrefixElement prefixElement) {
3624 int count = _imports.length; 3641 int count = _imports.length;
3625 List<ImportElement> importList = new List<ImportElement>(); 3642 List<ImportElement> importList = new List<ImportElement>();
3626 for (int i = 0; i < count; i++) { 3643 for (int i = 0; i < count; i++) {
(...skipping 848 matching lines...) Expand 10 before | Expand all | Expand 10 after
4475 buffer.write(right); 4492 buffer.write(right);
4476 } 4493 }
4477 4494
4478 @override 4495 @override
4479 FormalParameter computeNode() => 4496 FormalParameter computeNode() =>
4480 getNodeMatching((node) => node is FormalParameter); 4497 getNodeMatching((node) => node is FormalParameter);
4481 4498
4482 @override 4499 @override
4483 ElementImpl getChild(String identifier) { 4500 ElementImpl getChild(String identifier) {
4484 for (ParameterElement parameter in _parameters) { 4501 for (ParameterElement parameter in _parameters) {
4485 if ((parameter as ParameterElementImpl).identifier == identifier) { 4502 ParameterElementImpl parameterImpl = parameter;
4486 return parameter as ParameterElementImpl; 4503 if (parameterImpl.identifier == identifier) {
4504 return parameterImpl;
4487 } 4505 }
4488 } 4506 }
4489 return null; 4507 return null;
4490 } 4508 }
4491 4509
4492 /** 4510 /**
4493 * Set the visible range for this element to the range starting at the given 4511 * Set the visible range for this element to the range starting at the given
4494 * [offset] with the given [length]. 4512 * [offset] with the given [length].
4495 */ 4513 */
4496 void setVisibleRange(int offset, int length) { 4514 void setVisibleRange(int offset, int length) {
(...skipping 525 matching lines...) Expand 10 before | Expand all | Expand 10 after
5022 5040
5023 @override 5041 @override
5024 void visitElement(Element element) { 5042 void visitElement(Element element) {
5025 int offset = element.nameOffset; 5043 int offset = element.nameOffset;
5026 if (offset != -1) { 5044 if (offset != -1) {
5027 map[offset] = element; 5045 map[offset] = element;
5028 } 5046 }
5029 super.visitElement(element); 5047 super.visitElement(element);
5030 } 5048 }
5031 } 5049 }
OLDNEW
« no previous file with comments | « pkg/analyzer/lib/src/dart/constant/value.dart ('k') | pkg/analyzer/lib/src/dart/element/type.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698