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

Side by Side Diff: pkg/analyzer/lib/src/generated/resolver.dart

Issue 2622303006: Reapply "Add support for generic function type syntax, part 1" (Closed)
Patch Set: Created 3 years, 11 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.generated.resolver; 5 library analyzer.src.generated.resolver;
6 6
7 import 'dart:collection'; 7 import 'dart:collection';
8 8
9 import 'package:analyzer/dart/ast/ast.dart'; 9 import 'package:analyzer/dart/ast/ast.dart';
10 import 'package:analyzer/dart/ast/standard_resolution_map.dart'; 10 import 'package:analyzer/dart/ast/standard_resolution_map.dart';
(...skipping 354 matching lines...) Expand 10 before | Expand all | Expand 10 after
365 * checks expressed using an is expression. 365 * checks expressed using an is expression.
366 * 366 *
367 * @param node the is expression to check 367 * @param node the is expression to check
368 * @return `true` if and only if a hint code is generated on the passed node 368 * @return `true` if and only if a hint code is generated on the passed node
369 * See [HintCode.TYPE_CHECK_IS_NOT_NULL], [HintCode.TYPE_CHECK_IS_NULL], 369 * See [HintCode.TYPE_CHECK_IS_NOT_NULL], [HintCode.TYPE_CHECK_IS_NULL],
370 * [HintCode.UNNECESSARY_TYPE_CHECK_TRUE], and 370 * [HintCode.UNNECESSARY_TYPE_CHECK_TRUE], and
371 * [HintCode.UNNECESSARY_TYPE_CHECK_FALSE]. 371 * [HintCode.UNNECESSARY_TYPE_CHECK_FALSE].
372 */ 372 */
373 bool _checkAllTypeChecks(IsExpression node) { 373 bool _checkAllTypeChecks(IsExpression node) {
374 Expression expression = node.expression; 374 Expression expression = node.expression;
375 TypeName typeName = node.type; 375 TypeAnnotation typeName = node.type;
376 DartType lhsType = expression.staticType; 376 DartType lhsType = expression.staticType;
377 DartType rhsType = typeName.type; 377 DartType rhsType = typeName.type;
378 if (lhsType == null || rhsType == null) { 378 if (lhsType == null || rhsType == null) {
379 return false; 379 return false;
380 } 380 }
381 String rhsNameStr = typeName.name.name; 381 String rhsNameStr = typeName is TypeName ? typeName.name.name : null;
382 // if x is dynamic 382 // if x is dynamic
383 if (rhsType.isDynamic && rhsNameStr == Keyword.DYNAMIC.syntax) { 383 if (rhsType.isDynamic && rhsNameStr == Keyword.DYNAMIC.syntax) {
384 if (node.notOperator == null) { 384 if (node.notOperator == null) {
385 // the is case 385 // the is case
386 _errorReporter.reportErrorForNode( 386 _errorReporter.reportErrorForNode(
387 HintCode.UNNECESSARY_TYPE_CHECK_TRUE, node); 387 HintCode.UNNECESSARY_TYPE_CHECK_TRUE, node);
388 } else { 388 } else {
389 // the is not case 389 // the is not case
390 _errorReporter.reportErrorForNode( 390 _errorReporter.reportErrorForNode(
391 HintCode.UNNECESSARY_TYPE_CHECK_FALSE, node); 391 HintCode.UNNECESSARY_TYPE_CHECK_FALSE, node);
(...skipping 485 matching lines...) Expand 10 before | Expand all | Expand 10 after
877 * `null`, avoiding these implicit returns is considered a best practice. 877 * `null`, avoiding these implicit returns is considered a best practice.
878 * 878 *
879 * Note: for async functions/methods, this hint only applies when the 879 * Note: for async functions/methods, this hint only applies when the
880 * function has a return type that Future<Null> is not assignable to. 880 * function has a return type that Future<Null> is not assignable to.
881 * 881 *
882 * @param node the binary expression to check 882 * @param node the binary expression to check
883 * @param body the function body 883 * @param body the function body
884 * @return `true` if and only if a hint code is generated on the passed node 884 * @return `true` if and only if a hint code is generated on the passed node
885 * See [HintCode.MISSING_RETURN]. 885 * See [HintCode.MISSING_RETURN].
886 */ 886 */
887 void _checkForMissingReturn(TypeName returnType, FunctionBody body) { 887 void _checkForMissingReturn(TypeAnnotation returnType, FunctionBody body) {
888 // Check that the method or function has a return type, and a function body 888 // Check that the method or function has a return type, and a function body
889 if (returnType == null || body == null) { 889 if (returnType == null || body == null) {
890 return; 890 return;
891 } 891 }
892 // Check that the body is a BlockFunctionBody 892 // Check that the body is a BlockFunctionBody
893 if (body is BlockFunctionBody) { 893 if (body is BlockFunctionBody) {
894 // Generators are never required to have a return statement. 894 // Generators are never required to have a return statement.
895 if (body.isGenerator) { 895 if (body.isGenerator) {
896 return; 896 return;
897 } 897 }
(...skipping 942 matching lines...) Expand 10 before | Expand all | Expand 10 after
1840 * `x is! int`. 1840 * `x is! int`.
1841 * 1841 *
1842 * @param node the is expression to check 1842 * @param node the is expression to check
1843 * @return `true` if and only if a hint code is generated on the passed node 1843 * @return `true` if and only if a hint code is generated on the passed node
1844 * See [HintCode.IS_DOUBLE], 1844 * See [HintCode.IS_DOUBLE],
1845 * [HintCode.IS_INT], 1845 * [HintCode.IS_INT],
1846 * [HintCode.IS_NOT_DOUBLE], and 1846 * [HintCode.IS_NOT_DOUBLE], and
1847 * [HintCode.IS_NOT_INT]. 1847 * [HintCode.IS_NOT_INT].
1848 */ 1848 */
1849 bool _checkForIsDoubleHints(IsExpression node) { 1849 bool _checkForIsDoubleHints(IsExpression node) {
1850 TypeName typeName = node.type; 1850 DartType type = node.type.type;
1851 DartType type = typeName.type;
1852 Element element = type?.element; 1851 Element element = type?.element;
1853 if (element != null) { 1852 if (element != null) {
1854 String typeNameStr = element.name; 1853 String typeNameStr = element.name;
1855 LibraryElement libraryElement = element.library; 1854 LibraryElement libraryElement = element.library;
1856 // if (typeNameStr.equals(INT_TYPE_NAME) && libraryElement != null 1855 // if (typeNameStr.equals(INT_TYPE_NAME) && libraryElement != null
1857 // && libraryElement.isDartCore()) { 1856 // && libraryElement.isDartCore()) {
1858 // if (node.getNotOperator() == null) { 1857 // if (node.getNotOperator() == null) {
1859 // errorReporter.reportError(HintCode.IS_INT, node); 1858 // errorReporter.reportError(HintCode.IS_INT, node);
1860 // } else { 1859 // } else {
1861 // errorReporter.reportError(HintCode.IS_NOT_INT, node); 1860 // errorReporter.reportError(HintCode.IS_NOT_INT, node);
(...skipping 1151 matching lines...) Expand 10 before | Expand all | Expand 10 after
3013 3012
3014 @override 3013 @override
3015 bool visitFunctionExpressionInvocation(FunctionExpressionInvocation node) { 3014 bool visitFunctionExpressionInvocation(FunctionExpressionInvocation node) {
3016 if (_nodeExits(node.function)) { 3015 if (_nodeExits(node.function)) {
3017 return true; 3016 return true;
3018 } 3017 }
3019 return node.argumentList.accept(this); 3018 return node.argumentList.accept(this);
3020 } 3019 }
3021 3020
3022 @override 3021 @override
3022 bool visitGenericFunctionType(GenericFunctionType node) => false;
3023
3024 @override
3023 bool visitIdentifier(Identifier node) => false; 3025 bool visitIdentifier(Identifier node) => false;
3024 3026
3025 @override 3027 @override
3026 bool visitIfStatement(IfStatement node) { 3028 bool visitIfStatement(IfStatement node) {
3027 Expression conditionExpression = node.condition; 3029 Expression conditionExpression = node.condition;
3028 Statement thenStatement = node.thenStatement; 3030 Statement thenStatement = node.thenStatement;
3029 Statement elseStatement = node.elseStatement; 3031 Statement elseStatement = node.elseStatement;
3030 if (_nodeExits(conditionExpression)) { 3032 if (_nodeExits(conditionExpression)) {
3031 return true; 3033 return true;
3032 } 3034 }
(...skipping 3052 matching lines...) Expand 10 before | Expand all | Expand 10 after
6085 return null; 6087 return null;
6086 } 6088 }
6087 6089
6088 @override 6090 @override
6089 void visitFunctionTypeAliasInScope(FunctionTypeAlias node) { 6091 void visitFunctionTypeAliasInScope(FunctionTypeAlias node) {
6090 super.visitFunctionTypeAliasInScope(node); 6092 super.visitFunctionTypeAliasInScope(node);
6091 safelyVisitComment(node.documentationComment); 6093 safelyVisitComment(node.documentationComment);
6092 } 6094 }
6093 6095
6094 @override 6096 @override
6097 Object visitGenericFunctionType(GenericFunctionType node) => null;
6098
6099 @override
6095 Object visitHideCombinator(HideCombinator node) => null; 6100 Object visitHideCombinator(HideCombinator node) => null;
6096 6101
6097 @override 6102 @override
6098 Object visitIfStatement(IfStatement node) { 6103 Object visitIfStatement(IfStatement node) {
6099 Expression condition = node.condition; 6104 Expression condition = node.condition;
6100 condition?.accept(this); 6105 condition?.accept(this);
6101 Map<VariableElement, DartType> thenOverrides = 6106 Map<VariableElement, DartType> thenOverrides =
6102 const <VariableElement, DartType>{}; 6107 const <VariableElement, DartType>{};
6103 Statement thenStatement = node.thenStatement; 6108 Statement thenStatement = node.thenStatement;
6104 if (thenStatement != null) { 6109 if (thenStatement != null) {
(...skipping 1913 matching lines...) Expand 10 before | Expand all | Expand 10 after
8018 for (Match match in matches) { 8023 for (Match match in matches) {
8019 int offset = commentToken.offset + match.start + match.group(1).length; 8024 int offset = commentToken.offset + match.start + match.group(1).length;
8020 int length = match.group(2).length; 8025 int length = match.group(2).length;
8021 _errorReporter.reportErrorForOffset( 8026 _errorReporter.reportErrorForOffset(
8022 TodoCode.TODO, offset, length, [match.group(2)]); 8027 TodoCode.TODO, offset, length, [match.group(2)]);
8023 } 8028 }
8024 } 8029 }
8025 } 8030 }
8026 8031
8027 /** 8032 /**
8028 * Helper for resolving [TypeName]s. 8033 * Helper for resolving types.
8029 * 8034 *
8030 * The client must set [nameScope] before calling [resolveTypeName]. 8035 * The client must set [nameScope] before calling [resolveTypeName].
8031 */ 8036 */
8032 class TypeNameResolver { 8037 class TypeNameResolver {
8033 final TypeSystem typeSystem; 8038 final TypeSystem typeSystem;
8034 final DartType dynamicType; 8039 final DartType dynamicType;
8035 final DartType undefinedType; 8040 final DartType undefinedType;
8036 final LibraryElement definingLibrary; 8041 final LibraryElement definingLibrary;
8037 final Source source; 8042 final Source source;
8038 final AnalysisErrorListener errorListener; 8043 final AnalysisErrorListener errorListener;
(...skipping 258 matching lines...) Expand 10 before | Expand all | Expand 10 after
8297 } else { 8302 } else {
8298 reportErrorForNode( 8303 reportErrorForNode(
8299 StaticWarningCode.NOT_A_TYPE, typeName, [typeName.name]); 8304 StaticWarningCode.NOT_A_TYPE, typeName, [typeName.name]);
8300 } 8305 }
8301 } 8306 }
8302 typeName.staticType = dynamicType; 8307 typeName.staticType = dynamicType;
8303 node.type = dynamicType; 8308 node.type = dynamicType;
8304 return; 8309 return;
8305 } 8310 }
8306 if (argumentList != null) { 8311 if (argumentList != null) {
8307 NodeList<TypeName> arguments = argumentList.arguments; 8312 NodeList<TypeAnnotation> arguments = argumentList.arguments;
8308 int argumentCount = arguments.length; 8313 int argumentCount = arguments.length;
8309 List<DartType> parameters = typeSystem.typeFormalsAsTypes(type); 8314 List<DartType> parameters = typeSystem.typeFormalsAsTypes(type);
8310 int parameterCount = parameters.length; 8315 int parameterCount = parameters.length;
8311 List<DartType> typeArguments = new List<DartType>(parameterCount); 8316 List<DartType> typeArguments = new List<DartType>(parameterCount);
8312 if (argumentCount == parameterCount) { 8317 if (argumentCount == parameterCount) {
8313 for (int i = 0; i < parameterCount; i++) { 8318 for (int i = 0; i < parameterCount; i++) {
8314 TypeName argumentTypeName = arguments[i]; 8319 DartType argumentType = _getType(arguments[i]);
8315 DartType argumentType = _getType(argumentTypeName);
8316 if (argumentType == null) { 8320 if (argumentType == null) {
8317 argumentType = dynamicType; 8321 argumentType = dynamicType;
8318 } 8322 }
8319 typeArguments[i] = argumentType; 8323 typeArguments[i] = argumentType;
8320 } 8324 }
8321 } else { 8325 } else {
8322 reportErrorForNode(_getInvalidTypeParametersErrorCode(node), node, 8326 reportErrorForNode(_getInvalidTypeParametersErrorCode(node), node,
8323 [typeName.name, parameterCount, argumentCount]); 8327 [typeName.name, parameterCount, argumentCount]);
8324 for (int i = 0; i < parameterCount; i++) { 8328 for (int i = 0; i < parameterCount; i++) {
8325 typeArguments[i] = dynamicType; 8329 typeArguments[i] = dynamicType;
8326 } 8330 }
8327 } 8331 }
8328 type = typeSystem.instantiateType(type, typeArguments); 8332 type = typeSystem.instantiateType(type, typeArguments);
8329 } else { 8333 } else {
8330 type = typeSystem.instantiateToBounds(type); 8334 type = typeSystem.instantiateToBounds(type);
8331 } 8335 }
8332 typeName.staticType = type; 8336 typeName.staticType = type;
8333 node.type = type; 8337 node.type = type;
8334 } 8338 }
8335 8339
8336 /** 8340 /**
8337 * The number of type arguments in the given type name does not match the numb er of parameters in 8341 * The number of type arguments in the given [typeName] does not match the
8338 * the corresponding class element. Return the error code that should be used to report this 8342 * number of parameters in the corresponding class element. Return the error
8339 * error. 8343 * code that should be used to report this error.
8340 *
8341 * @param node the type name with the wrong number of type arguments
8342 * @return the error code that should be used to report that the wrong number of type arguments
8343 * were provided
8344 */ 8344 */
8345 ErrorCode _getInvalidTypeParametersErrorCode(TypeName node) { 8345 ErrorCode _getInvalidTypeParametersErrorCode(TypeName typeName) {
8346 AstNode parent = node.parent; 8346 AstNode parent = typeName.parent;
8347 if (parent is ConstructorName) { 8347 if (parent is ConstructorName) {
8348 parent = parent.parent; 8348 parent = parent.parent;
8349 if (parent is InstanceCreationExpression) { 8349 if (parent is InstanceCreationExpression) {
8350 if (parent.isConst) { 8350 if (parent.isConst) {
8351 return CompileTimeErrorCode.CONST_WITH_INVALID_TYPE_PARAMETERS; 8351 return CompileTimeErrorCode.CONST_WITH_INVALID_TYPE_PARAMETERS;
8352 } else { 8352 } else {
8353 return StaticWarningCode.NEW_WITH_INVALID_TYPE_PARAMETERS; 8353 return StaticWarningCode.NEW_WITH_INVALID_TYPE_PARAMETERS;
8354 } 8354 }
8355 } 8355 }
8356 } 8356 }
8357 return StaticTypeWarningCode.WRONG_NUMBER_OF_TYPE_ARGUMENTS; 8357 return StaticTypeWarningCode.WRONG_NUMBER_OF_TYPE_ARGUMENTS;
8358 } 8358 }
8359 8359
8360 /** 8360 /**
8361 * Checks if the given type name is the target in a redirected constructor. 8361 * Checks if the given [typeName] is the target in a redirected constructor.
8362 *
8363 * @param typeName the type name to analyze
8364 * @return some [RedirectingConstructorKind] if the given type name is used as the type in a
8365 * redirected constructor, or `null` otherwise
8366 */ 8362 */
8367 RedirectingConstructorKind _getRedirectingConstructorKind(TypeName typeName) { 8363 RedirectingConstructorKind _getRedirectingConstructorKind(TypeName typeName) {
8368 AstNode parent = typeName.parent; 8364 AstNode parent = typeName.parent;
8369 if (parent is ConstructorName) { 8365 if (parent is ConstructorName) {
8370 AstNode grandParent = parent.parent; 8366 AstNode grandParent = parent.parent;
8371 if (grandParent is ConstructorDeclaration) { 8367 if (grandParent is ConstructorDeclaration) {
8372 if (identical(grandParent.redirectedConstructor, parent)) { 8368 if (identical(grandParent.redirectedConstructor, parent)) {
8373 if (grandParent.constKeyword != null) { 8369 if (grandParent.constKeyword != null) {
8374 return RedirectingConstructorKind.CONST; 8370 return RedirectingConstructorKind.CONST;
8375 } 8371 }
8376 return RedirectingConstructorKind.NORMAL; 8372 return RedirectingConstructorKind.NORMAL;
8377 } 8373 }
8378 } 8374 }
8379 } 8375 }
8380 return null; 8376 return null;
8381 } 8377 }
8382 8378
8383 /** 8379 /**
8384 * Return the type represented by the given type name. 8380 * Return the type represented by the given type [annotation].
8385 *
8386 * @param typeName the type name representing the type to be returned
8387 * @return the type represented by the type name
8388 */ 8381 */
8389 DartType _getType(TypeName typeName) { 8382 DartType _getType(TypeAnnotation annotation) {
8390 DartType type = typeName.type; 8383 DartType type = annotation.type;
8391 if (type == null) { 8384 if (type == null) {
8392 return undefinedType; 8385 return undefinedType;
8393 } 8386 }
8394 return type; 8387 return type;
8395 } 8388 }
8396 8389
8397 /** 8390 /**
8398 * Returns the simple identifier of the given (may be qualified) type name. 8391 * Returns the simple identifier of the given (may be qualified) type name.
8399 * 8392 *
8400 * @param typeName the (may be qualified) qualified type name 8393 * @param typeName the (may be qualified) qualified type name
(...skipping 23 matching lines...) Expand all
8424 if (type != null) { 8417 if (type != null) {
8425 return null; 8418 return null;
8426 } 8419 }
8427 type = element.type; 8420 type = element.type;
8428 } 8421 }
8429 } 8422 }
8430 return type; 8423 return type;
8431 } 8424 }
8432 8425
8433 /** 8426 /**
8434 * Checks if the given type name is used as the type in an as expression. 8427 * Checks if the given [typeName] is used as the type in an as expression.
8435 *
8436 * @param typeName the type name to analyzer
8437 * @return `true` if the given type name is used as the type in an as expressi on
8438 */ 8428 */
8439 bool _isTypeNameInAsExpression(TypeName typeName) { 8429 bool _isTypeNameInAsExpression(TypeName typeName) {
8440 AstNode parent = typeName.parent; 8430 AstNode parent = typeName.parent;
8441 if (parent is AsExpression) { 8431 if (parent is AsExpression) {
8442 return identical(parent.type, typeName); 8432 return identical(parent.type, typeName);
8443 } 8433 }
8444 return false; 8434 return false;
8445 } 8435 }
8446 8436
8447 /** 8437 /**
8448 * Checks if the given type name is used as the exception type in a catch clau se. 8438 * Checks if the given [typeName] is used as the exception type in a catch
8449 * 8439 * clause.
8450 * @param typeName the type name to analyzer
8451 * @return `true` if the given type name is used as the exception type in a ca tch clause
8452 */ 8440 */
8453 bool _isTypeNameInCatchClause(TypeName typeName) { 8441 bool _isTypeNameInCatchClause(TypeName typeName) {
8454 AstNode parent = typeName.parent; 8442 AstNode parent = typeName.parent;
8455 if (parent is CatchClause) { 8443 if (parent is CatchClause) {
8456 return identical(parent.exceptionType, typeName); 8444 return identical(parent.exceptionType, typeName);
8457 } 8445 }
8458 return false; 8446 return false;
8459 } 8447 }
8460 8448
8461 /** 8449 /**
8462 * Checks if the given type name is used as the type in an instance creation e xpression. 8450 * Checks if the given [typeName] is used as the type in an instance creation
8463 * 8451 * expression.
8464 * @param typeName the type name to analyzer
8465 * @return `true` if the given type name is used as the type in an instance cr eation
8466 * expression
8467 */ 8452 */
8468 bool _isTypeNameInInstanceCreationExpression(TypeName typeName) { 8453 bool _isTypeNameInInstanceCreationExpression(TypeName typeName) {
8469 AstNode parent = typeName.parent; 8454 AstNode parent = typeName.parent;
8470 if (parent is ConstructorName && 8455 if (parent is ConstructorName &&
8471 parent.parent is InstanceCreationExpression) { 8456 parent.parent is InstanceCreationExpression) {
8472 return parent != null && identical(parent.type, typeName); 8457 return parent != null && identical(parent.type, typeName);
8473 } 8458 }
8474 return false; 8459 return false;
8475 } 8460 }
8476 8461
8477 /** 8462 /**
8478 * Checks if the given type name is used as the type in an is expression. 8463 * Checks if the given [typeName] is used as the type in an is expression.
8479 *
8480 * @param typeName the type name to analyzer
8481 * @return `true` if the given type name is used as the type in an is expressi on
8482 */ 8464 */
8483 bool _isTypeNameInIsExpression(TypeName typeName) { 8465 bool _isTypeNameInIsExpression(TypeName typeName) {
8484 AstNode parent = typeName.parent; 8466 AstNode parent = typeName.parent;
8485 if (parent is IsExpression) { 8467 if (parent is IsExpression) {
8486 return identical(parent.type, typeName); 8468 return identical(parent.type, typeName);
8487 } 8469 }
8488 return false; 8470 return false;
8489 } 8471 }
8490 8472
8491 /** 8473 /**
8492 * Checks if the given type name used in a type argument list. 8474 * Checks if the given [typeName] used in a type argument list.
8493 *
8494 * @param typeName the type name to analyzer
8495 * @return `true` if the given type name is in a type argument list
8496 */ 8475 */
8497 bool _isTypeNameInTypeArgumentList(TypeName typeName) => 8476 bool _isTypeNameInTypeArgumentList(TypeName typeName) =>
8498 typeName.parent is TypeArgumentList; 8477 typeName.parent is TypeArgumentList;
8499 8478
8500 /** 8479 /**
8501 * Records the new Element for a TypeName's Identifier. 8480 * Records the new Element for a TypeName's Identifier.
8502 * 8481 *
8503 * A null may be passed in to indicate that the element can't be resolved. 8482 * A null may be passed in to indicate that the element can't be resolved.
8504 * (During a re-run of a task, it's important to clear any previous value 8483 * (During a re-run of a task, it's important to clear any previous value
8505 * of the element.) 8484 * of the element.)
8506 */ 8485 */
8507 void _setElement(Identifier typeName, Element element) { 8486 void _setElement(Identifier typeName, Element element) {
8508 if (typeName is SimpleIdentifier) { 8487 if (typeName is SimpleIdentifier) {
8509 typeName.staticElement = element; 8488 typeName.staticElement = element;
8510 } else if (typeName is PrefixedIdentifier) { 8489 } else if (typeName is PrefixedIdentifier) {
8511 typeName.identifier.staticElement = element; 8490 typeName.identifier.staticElement = element;
8512 SimpleIdentifier prefix = typeName.prefix; 8491 SimpleIdentifier prefix = typeName.prefix;
8513 prefix.staticElement = nameScope.lookup(prefix, definingLibrary); 8492 prefix.staticElement = nameScope.lookup(prefix, definingLibrary);
8514 } 8493 }
8515 } 8494 }
8516 8495
8517 /** 8496 /**
8518 * @return `true` if the name of the given [TypeName] is an built-in identifie r. 8497 * Return `true` if the name of the given [typeName] is an built-in identifier .
8519 */ 8498 */
8520 static bool _isBuiltInIdentifier(TypeName node) { 8499 static bool _isBuiltInIdentifier(TypeName typeName) {
8521 Token token = node.name.beginToken; 8500 Token token = typeName.name.beginToken;
8522 return token.type == TokenType.KEYWORD; 8501 return token.type == TokenType.KEYWORD;
8523 } 8502 }
8524 8503
8525 /** 8504 /**
8526 * @return `true` if given [TypeName] is used as a type annotation. 8505 * @return `true` if given [typeName] is used as a type annotation.
8527 */ 8506 */
8528 static bool _isTypeAnnotation(TypeName node) { 8507 static bool _isTypeAnnotation(TypeName typeName) {
8529 AstNode parent = node.parent; 8508 AstNode parent = typeName.parent;
8530 if (parent is VariableDeclarationList) { 8509 if (parent is VariableDeclarationList) {
8531 return identical(parent.type, node); 8510 return identical(parent.type, typeName);
8532 } else if (parent is FieldFormalParameter) { 8511 } else if (parent is FieldFormalParameter) {
8533 return identical(parent.type, node); 8512 return identical(parent.type, typeName);
8534 } else if (parent is SimpleFormalParameter) { 8513 } else if (parent is SimpleFormalParameter) {
8535 return identical(parent.type, node); 8514 return identical(parent.type, typeName);
8536 } 8515 }
8537 return false; 8516 return false;
8538 } 8517 }
8539 } 8518 }
8540 8519
8541 /** 8520 /**
8542 * Instances of the class `TypeOverrideManager` manage the ability to override t he type of an 8521 * Instances of the class `TypeOverrideManager` manage the ability to override t he type of an
8543 * element within a given context. 8522 * element within a given context.
8544 */ 8523 */
8545 class TypeOverrideManager { 8524 class TypeOverrideManager {
(...skipping 247 matching lines...) Expand 10 before | Expand all | Expand 10 after
8793 } else if (unitMember is ClassTypeAlias) { 8772 } else if (unitMember is ClassTypeAlias) {
8794 _resolveTypeParameters(unitMember.typeParameters, 8773 _resolveTypeParameters(unitMember.typeParameters,
8795 () => new TypeParameterScope(libraryScope, unitMember.element)); 8774 () => new TypeParameterScope(libraryScope, unitMember.element));
8796 } else if (unitMember is FunctionTypeAlias) { 8775 } else if (unitMember is FunctionTypeAlias) {
8797 _resolveTypeParameters(unitMember.typeParameters, 8776 _resolveTypeParameters(unitMember.typeParameters,
8798 () => new FunctionTypeScope(libraryScope, unitMember.element)); 8777 () => new FunctionTypeScope(libraryScope, unitMember.element));
8799 } 8778 }
8800 } 8779 }
8801 } 8780 }
8802 8781
8803 void _resolveTypeName(TypeName typeName) { 8782 void _resolveTypeName(TypeAnnotation type) {
8804 typeName.typeArguments?.arguments?.forEach(_resolveTypeName); 8783 if (type is TypeName) {
8805 typeNameResolver.resolveTypeName(typeName); 8784 type.typeArguments?.arguments?.forEach(_resolveTypeName);
8806 // TODO(scheglov) report error when don't apply type bounds for type bounds 8785 typeNameResolver.resolveTypeName(type);
8786 // TODO(scheglov) report error when don't apply type bounds for type bound s
8787 } else {
8788 // TODO(brianwilkerson) Add resolution of GenericFunctionType
8789 throw new ArgumentError('Cannot resolve a ${type.runtimeType}');
8790 }
8807 } 8791 }
8808 8792
8809 void _resolveTypeParameters( 8793 void _resolveTypeParameters(
8810 TypeParameterList typeParameters, Scope createTypeParametersScope()) { 8794 TypeParameterList typeParameters, Scope createTypeParametersScope()) {
8811 if (typeParameters != null) { 8795 if (typeParameters != null) {
8812 Scope typeParametersScope = null; 8796 Scope typeParametersScope = null;
8813 for (TypeParameter typeParameter in typeParameters.typeParameters) { 8797 for (TypeParameter typeParameter in typeParameters.typeParameters) {
8814 TypeName bound = typeParameter.bound; 8798 TypeAnnotation bound = typeParameter.bound;
8815 if (bound != null) { 8799 if (bound != null) {
8816 Element typeParameterElement = typeParameter.name.staticElement; 8800 Element typeParameterElement = typeParameter.name.staticElement;
8817 if (typeParameterElement is TypeParameterElementImpl) { 8801 if (typeParameterElement is TypeParameterElementImpl) {
8818 if (LibraryElementImpl.hasResolutionCapability( 8802 if (LibraryElementImpl.hasResolutionCapability(
8819 library, LibraryResolutionCapability.resolvedTypeNames)) { 8803 library, LibraryResolutionCapability.resolvedTypeNames)) {
8820 bound.type = typeParameterElement.bound; 8804 if (bound is TypeName) {
8805 bound.type = typeParameterElement.bound;
8806 } else {
8807 // TODO(brianwilkerson) Add resolution of GenericFunctionType
8808 throw new ArgumentError(
8809 'Cannot resolve a ${bound.runtimeType}');
8810 }
8821 } else { 8811 } else {
8822 libraryScope ??= new LibraryScope(library); 8812 libraryScope ??= new LibraryScope(library);
8823 typeParametersScope ??= createTypeParametersScope(); 8813 typeParametersScope ??= createTypeParametersScope();
8824 typeNameResolver ??= new TypeNameResolver( 8814 typeNameResolver ??= new TypeNameResolver(
8825 new TypeSystemImpl(typeProvider), 8815 new TypeSystemImpl(typeProvider),
8826 typeProvider, 8816 typeProvider,
8827 library, 8817 library,
8828 source, 8818 source,
8829 errorListener); 8819 errorListener);
8830 typeNameResolver.nameScope = typeParametersScope; 8820 typeNameResolver.nameScope = typeParametersScope;
(...skipping 657 matching lines...) Expand 10 before | Expand all | Expand 10 after
9488 * True if we're analyzing in strong mode. 9478 * True if we're analyzing in strong mode.
9489 */ 9479 */
9490 bool _strongMode; 9480 bool _strongMode;
9491 9481
9492 /** 9482 /**
9493 * Type type system in use for this resolver pass. 9483 * Type type system in use for this resolver pass.
9494 */ 9484 */
9495 TypeSystem _typeSystem; 9485 TypeSystem _typeSystem;
9496 9486
9497 /** 9487 /**
9498 * The helper to resolve [TypeName]s. 9488 * The helper to resolve types.
9499 */ 9489 */
9500 TypeNameResolver _typeNameResolver; 9490 TypeNameResolver _typeNameResolver;
9501 9491
9502 final TypeResolverMode mode; 9492 final TypeResolverMode mode;
9503 9493
9504 /** 9494 /**
9505 * Is `true` when we are visiting all nodes in [TypeResolverMode.local] mode. 9495 * Is `true` when we are visiting all nodes in [TypeResolverMode.local] mode.
9506 */ 9496 */
9507 bool _localModeVisitAll = false; 9497 bool _localModeVisitAll = false;
9508 9498
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
9570 } 9560 }
9571 9561
9572 @override 9562 @override
9573 Object visitCatchClause(CatchClause node) { 9563 Object visitCatchClause(CatchClause node) {
9574 super.visitCatchClause(node); 9564 super.visitCatchClause(node);
9575 SimpleIdentifier exception = node.exceptionParameter; 9565 SimpleIdentifier exception = node.exceptionParameter;
9576 if (exception != null) { 9566 if (exception != null) {
9577 // If an 'on' clause is provided the type of the exception parameter is 9567 // If an 'on' clause is provided the type of the exception parameter is
9578 // the type in the 'on' clause. Otherwise, the type of the exception 9568 // the type in the 'on' clause. Otherwise, the type of the exception
9579 // parameter is 'Object'. 9569 // parameter is 'Object'.
9580 TypeName exceptionTypeName = node.exceptionType; 9570 TypeAnnotation exceptionTypeName = node.exceptionType;
9581 DartType exceptionType; 9571 DartType exceptionType;
9582 if (exceptionTypeName == null) { 9572 if (exceptionTypeName == null) {
9583 exceptionType = typeProvider.dynamicType; 9573 exceptionType = typeProvider.dynamicType;
9584 } else { 9574 } else {
9585 exceptionType = _typeNameResolver._getType(exceptionTypeName); 9575 exceptionType = _typeNameResolver._getType(exceptionTypeName);
9586 } 9576 }
9587 _recordType(exception, exceptionType); 9577 _recordType(exception, exceptionType);
9588 Element element = exception.staticElement; 9578 Element element = exception.staticElement;
9589 if (element is VariableElementImpl) { 9579 if (element is VariableElementImpl) {
9590 element.declaredType = exceptionType; 9580 element.declaredType = exceptionType;
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after
9709 AnalysisEngine.instance.logger.logError(buffer.toString(), 9699 AnalysisEngine.instance.logger.logError(buffer.toString(),
9710 new CaughtException(new AnalysisException(), null)); 9700 new CaughtException(new AnalysisException(), null));
9711 } 9701 }
9712 return null; 9702 return null;
9713 } 9703 }
9714 9704
9715 @override 9705 @override
9716 Object visitDeclaredIdentifier(DeclaredIdentifier node) { 9706 Object visitDeclaredIdentifier(DeclaredIdentifier node) {
9717 super.visitDeclaredIdentifier(node); 9707 super.visitDeclaredIdentifier(node);
9718 DartType declaredType; 9708 DartType declaredType;
9719 TypeName typeName = node.type; 9709 TypeAnnotation typeName = node.type;
9720 if (typeName == null) { 9710 if (typeName == null) {
9721 declaredType = _dynamicType; 9711 declaredType = _dynamicType;
9722 } else { 9712 } else {
9723 declaredType = _typeNameResolver._getType(typeName); 9713 declaredType = _typeNameResolver._getType(typeName);
9724 } 9714 }
9725 LocalVariableElementImpl element = node.element as LocalVariableElementImpl; 9715 LocalVariableElementImpl element = node.element as LocalVariableElementImpl;
9726 element.declaredType = declaredType; 9716 element.declaredType = declaredType;
9727 return null; 9717 return null;
9728 } 9718 }
9729 9719
9730 @override 9720 @override
9731 Object visitFieldFormalParameter(FieldFormalParameter node) { 9721 Object visitFieldFormalParameter(FieldFormalParameter node) {
9732 super.visitFieldFormalParameter(node); 9722 super.visitFieldFormalParameter(node);
9733 Element element = node.identifier.staticElement; 9723 Element element = node.identifier.staticElement;
9734 if (element is ParameterElementImpl) { 9724 if (element is ParameterElementImpl) {
9735 FormalParameterList parameterList = node.parameters; 9725 FormalParameterList parameterList = node.parameters;
9736 if (parameterList == null) { 9726 if (parameterList == null) {
9737 DartType type; 9727 DartType type;
9738 TypeName typeName = node.type; 9728 TypeAnnotation typeName = node.type;
9739 if (typeName == null) { 9729 if (typeName == null) {
9740 element.hasImplicitType = true; 9730 element.hasImplicitType = true;
9741 if (element is FieldFormalParameterElement) { 9731 if (element is FieldFormalParameterElement) {
9742 FieldElement fieldElement = 9732 FieldElement fieldElement =
9743 (element as FieldFormalParameterElement).field; 9733 (element as FieldFormalParameterElement).field;
9744 type = fieldElement?.type; 9734 type = fieldElement?.type;
9745 } 9735 }
9746 } else { 9736 } else {
9747 type = _typeNameResolver._getType(typeName); 9737 type = _typeNameResolver._getType(typeName);
9748 } 9738 }
(...skipping 202 matching lines...) Expand 10 before | Expand all | Expand 10 after
9951 } 9941 }
9952 9942
9953 // The mode in which we visit all nodes. 9943 // The mode in which we visit all nodes.
9954 return super.visitNode(node); 9944 return super.visitNode(node);
9955 } 9945 }
9956 9946
9957 @override 9947 @override
9958 Object visitSimpleFormalParameter(SimpleFormalParameter node) { 9948 Object visitSimpleFormalParameter(SimpleFormalParameter node) {
9959 super.visitSimpleFormalParameter(node); 9949 super.visitSimpleFormalParameter(node);
9960 DartType declaredType; 9950 DartType declaredType;
9961 TypeName typeName = node.type; 9951 TypeAnnotation typeName = node.type;
9962 if (typeName == null) { 9952 if (typeName == null) {
9963 declaredType = _dynamicType; 9953 declaredType = _dynamicType;
9964 } else { 9954 } else {
9965 declaredType = _typeNameResolver._getType(typeName); 9955 declaredType = _typeNameResolver._getType(typeName);
9966 } 9956 }
9967 Element element = node.identifier.staticElement; 9957 Element element = node.identifier.staticElement;
9968 if (element is ParameterElementImpl) { 9958 if (element is ParameterElementImpl) {
9969 element.declaredType = declaredType; 9959 element.declaredType = declaredType;
9970 } else { 9960 } else {
9971 // TODO(brianwilkerson) Report the internal error. 9961 // TODO(brianwilkerson) Report the internal error.
(...skipping 18 matching lines...) Expand all
9990 @override 9980 @override
9991 Object visitTypeParameter(TypeParameter node) { 9981 Object visitTypeParameter(TypeParameter node) {
9992 super.visitTypeParameter(node); 9982 super.visitTypeParameter(node);
9993 AstNode parent2 = node.parent?.parent; 9983 AstNode parent2 = node.parent?.parent;
9994 if (parent2 is ClassDeclaration || 9984 if (parent2 is ClassDeclaration ||
9995 parent2 is ClassTypeAlias || 9985 parent2 is ClassTypeAlias ||
9996 parent2 is FunctionTypeAlias) { 9986 parent2 is FunctionTypeAlias) {
9997 // Bounds of parameters of classes and function type aliases are 9987 // Bounds of parameters of classes and function type aliases are
9998 // already resolved. 9988 // already resolved.
9999 } else { 9989 } else {
10000 TypeName bound = node.bound; 9990 TypeAnnotation bound = node.bound;
10001 if (bound != null) { 9991 if (bound != null) {
10002 TypeParameterElementImpl typeParameter = 9992 TypeParameterElementImpl typeParameter =
10003 node.name.staticElement as TypeParameterElementImpl; 9993 node.name.staticElement as TypeParameterElementImpl;
10004 if (typeParameter != null) { 9994 if (typeParameter != null) {
10005 typeParameter.bound = bound.type; 9995 typeParameter.bound = bound.type;
10006 } 9996 }
10007 } 9997 }
10008 } 9998 }
10009 return null; 9999 return null;
10010 } 10000 }
10011 10001
10012 @override 10002 @override
10013 Object visitVariableDeclaration(VariableDeclaration node) { 10003 Object visitVariableDeclaration(VariableDeclaration node) {
10014 super.visitVariableDeclaration(node); 10004 super.visitVariableDeclaration(node);
10015 var variableList = node.parent as VariableDeclarationList; 10005 var variableList = node.parent as VariableDeclarationList;
10016 // When the library is resynthesized, the types of field elements are 10006 // When the library is resynthesized, the types of field elements are
10017 // already set - statically or inferred. We don't want to overwrite them. 10007 // already set - statically or inferred. We don't want to overwrite them.
10018 if (variableList.parent is FieldDeclaration && 10008 if (variableList.parent is FieldDeclaration &&
10019 LibraryElementImpl.hasResolutionCapability( 10009 LibraryElementImpl.hasResolutionCapability(
10020 definingLibrary, LibraryResolutionCapability.resolvedTypeNames)) { 10010 definingLibrary, LibraryResolutionCapability.resolvedTypeNames)) {
10021 return null; 10011 return null;
10022 } 10012 }
10023 // Resolve the type. 10013 // Resolve the type.
10024 DartType declaredType; 10014 DartType declaredType;
10025 TypeName typeName = variableList.type; 10015 TypeAnnotation typeName = variableList.type;
10026 if (typeName == null) { 10016 if (typeName == null) {
10027 declaredType = _dynamicType; 10017 declaredType = _dynamicType;
10028 } else { 10018 } else {
10029 declaredType = _typeNameResolver._getType(typeName); 10019 declaredType = _typeNameResolver._getType(typeName);
10030 } 10020 }
10031 Element element = node.name.staticElement; 10021 Element element = node.name.staticElement;
10032 if (element is VariableElementImpl) { 10022 if (element is VariableElementImpl) {
10033 element.declaredType = declaredType; 10023 element.declaredType = declaredType;
10034 } 10024 }
10035 return null; 10025 return null;
10036 } 10026 }
10037 10027
10038 /** 10028 /**
10039 * Given a type name representing the return type of a function, compute the r eturn type of the 10029 * Given the [returnType] of a function, compute the return type of the
10040 * function. 10030 * function.
10041 *
10042 * @param returnType the type name representing the return type of the functio n
10043 * @return the return type that was computed
10044 */ 10031 */
10045 DartType _computeReturnType(TypeName returnType) { 10032 DartType _computeReturnType(TypeAnnotation returnType) {
10046 if (returnType == null) { 10033 if (returnType == null) {
10047 return _dynamicType; 10034 return _dynamicType;
10048 } else { 10035 } else {
10049 return returnType.type; 10036 return returnType.type;
10050 } 10037 }
10051 } 10038 }
10052 10039
10053 /** 10040 /**
10054 * Return the class element that represents the class whose name was provided. 10041 * Return the class element that represents the class whose name was provided.
10055 * 10042 *
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
10117 Object _recordType(Expression expression, DartType type) { 10104 Object _recordType(Expression expression, DartType type) {
10118 if (type == null) { 10105 if (type == null) {
10119 expression.staticType = _dynamicType; 10106 expression.staticType = _dynamicType;
10120 } else { 10107 } else {
10121 expression.staticType = type; 10108 expression.staticType = type;
10122 } 10109 }
10123 return null; 10110 return null;
10124 } 10111 }
10125 10112
10126 /** 10113 /**
10127 * Resolve the types in the given with and implements clauses and associate th ose types with the 10114 * Resolve the types in the given [withClause] and [implementsClause] and
10128 * given class element. 10115 * associate those types with the given [classElement].
10129 *
10130 * @param classElement the class element with which the mixin and interface ty pes are to be
10131 * associated
10132 * @param withClause the with clause to be resolved
10133 * @param implementsClause the implements clause to be resolved
10134 */ 10116 */
10135 void _resolve(ClassElementImpl classElement, WithClause withClause, 10117 void _resolve(ClassElementImpl classElement, WithClause withClause,
10136 ImplementsClause implementsClause) { 10118 ImplementsClause implementsClause) {
10137 if (withClause != null) { 10119 if (withClause != null) {
10138 List<InterfaceType> mixinTypes = _resolveTypes( 10120 List<InterfaceType> mixinTypes = _resolveTypes(
10139 withClause.mixinTypes, 10121 withClause.mixinTypes,
10140 CompileTimeErrorCode.MIXIN_OF_NON_CLASS, 10122 CompileTimeErrorCode.MIXIN_OF_NON_CLASS,
10141 CompileTimeErrorCode.MIXIN_OF_ENUM, 10123 CompileTimeErrorCode.MIXIN_OF_ENUM,
10142 CompileTimeErrorCode.MIXIN_OF_NON_CLASS); 10124 CompileTimeErrorCode.MIXIN_OF_NON_CLASS);
10143 if (classElement != null) { 10125 if (classElement != null) {
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after
10232 InterfaceType type = 10214 InterfaceType type =
10233 _resolveType(typeName, nonTypeError, enumTypeError, dynamicTypeError); 10215 _resolveType(typeName, nonTypeError, enumTypeError, dynamicTypeError);
10234 if (type != null) { 10216 if (type != null) {
10235 types.add(type); 10217 types.add(type);
10236 } 10218 }
10237 } 10219 }
10238 return types; 10220 return types;
10239 } 10221 }
10240 10222
10241 /** 10223 /**
10242 * Given a parameter element, create a function type based on the given return type and parameter 10224 * Given a parameter [element], create a function type based on the given
10243 * list and associate the created type with the element. 10225 * [returnType] and [parameterList] and associate the created type with the
10244 * 10226 * element.
10245 * @param element the parameter element whose type is to be set
10246 * @param returnType the (possibly `null`) return type of the function
10247 * @param parameterList the list of parameters to the function
10248 */ 10227 */
10249 void _setFunctionTypedParameterType(ParameterElementImpl element, 10228 void _setFunctionTypedParameterType(ParameterElementImpl element,
10250 TypeName returnType, FormalParameterList parameterList) { 10229 TypeAnnotation returnType, FormalParameterList parameterList) {
10251 List<ParameterElement> parameters = _getElements(parameterList); 10230 List<ParameterElement> parameters = _getElements(parameterList);
10252 FunctionElementImpl functionElement = new FunctionElementImpl.forNode(null); 10231 FunctionElementImpl functionElement = new FunctionElementImpl.forNode(null);
10253 functionElement.isSynthetic = true; 10232 functionElement.isSynthetic = true;
10254 functionElement.shareParameters(parameters); 10233 functionElement.shareParameters(parameters);
10255 functionElement.declaredReturnType = _computeReturnType(returnType); 10234 functionElement.declaredReturnType = _computeReturnType(returnType);
10256 functionElement.enclosingElement = element; 10235 functionElement.enclosingElement = element;
10257 functionElement.shareTypeParameters(element.typeParameters); 10236 functionElement.shareTypeParameters(element.typeParameters);
10258 element.type = new FunctionTypeImpl(functionElement); 10237 element.type = new FunctionTypeImpl(functionElement);
10259 functionElement.type = element.type; 10238 functionElement.type = element.type;
10260 } 10239 }
(...skipping 537 matching lines...) Expand 10 before | Expand all | Expand 10 after
10798 return null; 10777 return null;
10799 } 10778 }
10800 if (identical(node.staticElement, variable)) { 10779 if (identical(node.staticElement, variable)) {
10801 if (node.inSetterContext()) { 10780 if (node.inSetterContext()) {
10802 result = true; 10781 result = true;
10803 } 10782 }
10804 } 10783 }
10805 return null; 10784 return null;
10806 } 10785 }
10807 } 10786 }
OLDNEW
« no previous file with comments | « pkg/analyzer/lib/src/generated/error_verifier.dart ('k') | pkg/analyzer/lib/src/generated/static_type_analyzer.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698