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

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

Issue 2734943002: More support for generic function types (Closed)
Patch Set: Created 3 years, 9 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 4857 matching lines...) Expand 10 before | Expand all | Expand 10 after
4868 4868
4869 @override 4869 @override
4870 void visitChildren(ElementVisitor visitor) { 4870 void visitChildren(ElementVisitor visitor) {
4871 super.visitChildren(visitor); 4871 super.visitChildren(visitor);
4872 safelyVisitChildren(parameters, visitor); 4872 safelyVisitChildren(parameters, visitor);
4873 safelyVisitChildren(typeParameters, visitor); 4873 safelyVisitChildren(typeParameters, visitor);
4874 } 4874 }
4875 } 4875 }
4876 4876
4877 /** 4877 /**
4878 * A function type alias of the form
4879 * `typedef` identifier typeParameters = genericFunctionType;
4880 *
4881 * Clients may not extend, implement or mix-in this class.
4882 */
4883 class GenericTypeAliasElementImpl extends ElementImpl
4884 with TypeParameterizedElementMixin
4885 implements FunctionTypeAliasElement {
4886 /**
4887 * The unlinked representation of the type in the summary.
4888 */
4889 final UnlinkedTypedef _unlinkedTypedef;
4890
4891 /**
4892 * The element representing the generic function type if this is a generic
4893 * function type alias, or `null` if it isn't.
4894 */
4895 FunctionElement _function;
4896
4897 /**
4898 * The type of function defined by this type alias.
4899 */
4900 FunctionType _type;
4901
4902 /**
4903 * A list containing all of the type parameters defined for this type.
4904 */
4905 List<TypeParameterElement> _typeParameters = TypeParameterElement.EMPTY_LIST;
4906
4907 /**
4908 * Initialize a newly created type alias element to have the given [name].
4909 */
4910 GenericTypeAliasElementImpl.forNode(Identifier name)
4911 : _unlinkedTypedef = null,
4912 super.forNode(name);
4913
4914 /**
4915 * Initialize using the given serialized information.
4916 */
4917 GenericTypeAliasElementImpl.forSerialized(
4918 this._unlinkedTypedef, CompilationUnitElementImpl enclosingUnit)
4919 : super.forSerialized(enclosingUnit);
4920
4921 @override
4922 int get codeLength {
4923 if (_unlinkedTypedef != null) {
4924 return _unlinkedTypedef.codeRange?.length;
4925 }
4926 return super.codeLength;
4927 }
4928
4929 @override
4930 int get codeOffset {
4931 if (_unlinkedTypedef != null) {
4932 return _unlinkedTypedef.codeRange?.offset;
4933 }
4934 return super.codeOffset;
4935 }
4936
4937 @override
4938 String get displayName => name;
4939
4940 @override
4941 String get documentationComment {
4942 if (_unlinkedTypedef != null) {
4943 return _unlinkedTypedef?.documentationComment?.text;
4944 }
4945 return super.documentationComment;
4946 }
4947
4948 @override
4949 CompilationUnitElement get enclosingElement =>
4950 super.enclosingElement as CompilationUnitElement;
4951
4952 @override
4953 TypeParameterizedElementMixin get enclosingTypeParameterContext => null;
4954
4955 @override
4956 CompilationUnitElementImpl get enclosingUnit =>
4957 _enclosingElement as CompilationUnitElementImpl;
4958
4959 /**
4960 * Return the function element representing the generic function type on the
4961 * right side of the equals.
4962 */
4963 FunctionElement get function {
4964 if (_function == null && _unlinkedTypedef != null) {
4965 DartType type = enclosingUnit.resynthesizerContext.resolveTypeRef(
4966 _unlinkedTypedef.returnType, this,
4967 declaredType: true);
4968 if (type is FunctionType) {
4969 _function = type.element;
Brian Wilkerson 2017/03/06 19:58:07 Will this work? If we synthesize a type will it ha
Paul Berry 2017/03/06 20:21:15 I confess that I don't know off the top of my head
scheglov 2017/03/06 20:22:22 I think so. If it does not have the corresponding
Brian Wilkerson 2017/03/06 22:27:02 Thanks! I'm going to go ahead and commit this. I'm
4970 }
4971 }
4972 return _function;
4973 }
4974
4975 /**
4976 * Set the function element representing the generic function type on the
4977 * right side of the equals to the given [function].
4978 */
4979 void set function(FunctionElement function) {
4980 _assertNotResynthesized(_unlinkedTypedef);
4981 if (function != null) {
4982 (function as FunctionElementImpl).enclosingElement = this;
4983 }
4984 _function = function;
4985 }
4986
4987 @override
4988 ElementKind get kind => ElementKind.FUNCTION_TYPE_ALIAS;
4989
4990 @override
4991 List<ElementAnnotation> get metadata {
4992 if (_unlinkedTypedef != null) {
4993 return _metadata ??=
4994 _buildAnnotations(enclosingUnit, _unlinkedTypedef.annotations);
4995 }
4996 return super.metadata;
4997 }
4998
4999 @override
5000 String get name {
5001 if (_unlinkedTypedef != null) {
5002 return _unlinkedTypedef.name;
5003 }
5004 return super.name;
5005 }
5006
5007 @override
5008 int get nameOffset {
5009 int offset = super.nameOffset;
5010 if (offset == 0 && _unlinkedTypedef != null) {
5011 return _unlinkedTypedef.nameOffset;
5012 }
5013 return offset;
5014 }
5015
5016 @override
5017 List<ParameterElement> get parameters => function.parameters;
5018
5019 @override
5020 DartType get returnType => function.returnType;
5021
5022 @override
5023 FunctionType get type {
5024 if (_unlinkedTypedef != null && _type == null) {
5025 _type = new FunctionTypeImpl.forTypedef(this);
5026 }
5027 return _type;
5028 }
5029
5030 void set type(FunctionType type) {
5031 _assertNotResynthesized(_unlinkedTypedef);
5032 _type = type;
5033 }
5034
5035 @override
5036 TypeParameterizedElementMixin get typeParameterContext => this;
5037
5038 @override
5039 List<TypeParameterElement> get typeParameters {
5040 if (_unlinkedTypedef != null) {
5041 return super.typeParameters;
5042 }
5043 return _typeParameters;
5044 }
5045
5046 /**
5047 * Set the type parameters defined for this type to the given
5048 * [typeParameters].
5049 */
5050 void set typeParameters(List<TypeParameterElement> typeParameters) {
5051 _assertNotResynthesized(_unlinkedTypedef);
5052 for (TypeParameterElement typeParameter in typeParameters) {
5053 (typeParameter as TypeParameterElementImpl).enclosingElement = this;
5054 }
5055 this._typeParameters = typeParameters;
5056 }
5057
5058 @override
5059 List<UnlinkedTypeParam> get unlinkedTypeParams =>
5060 _unlinkedTypedef.typeParameters;
5061
5062 @override
5063 /*=T*/ accept/*<T>*/(ElementVisitor<dynamic/*=T*/ > visitor) =>
5064 visitor.visitFunctionTypeAliasElement(this);
5065
5066 @override
5067 void appendTo(StringBuffer buffer) {
5068 buffer.write("typedef ");
5069 buffer.write(displayName);
5070 int typeParameterCount = _typeParameters.length;
5071 if (typeParameterCount > 0) {
5072 buffer.write("<");
5073 for (int i = 0; i < typeParameterCount; i++) {
5074 if (i > 0) {
5075 buffer.write(", ");
5076 }
5077 (_typeParameters[i] as TypeParameterElementImpl).appendTo(buffer);
5078 }
5079 buffer.write(">");
5080 }
5081 buffer.write(" = ");
5082 (function as FunctionElementImpl).appendTo(buffer);
5083 }
5084
5085 @override
5086 FunctionTypeAlias computeNode() =>
5087 getNodeMatching((node) => node is GenericTypeAlias);
5088
5089 @override
5090 ElementImpl getChild(String identifier) {
5091 for (TypeParameterElement typeParameter in _typeParameters) {
5092 TypeParameterElementImpl typeParameterImpl = typeParameter;
5093 if (typeParameterImpl.identifier == identifier) {
5094 return typeParameterImpl;
5095 }
5096 }
5097 return null;
5098 }
5099
5100 @override
5101 void visitChildren(ElementVisitor visitor) {
5102 super.visitChildren(visitor);
5103 safelyVisitChildren(typeParameters, visitor);
5104 function?.accept(visitor);
5105 }
5106 }
5107
5108 /**
4878 * A concrete implementation of a [HideElementCombinator]. 5109 * A concrete implementation of a [HideElementCombinator].
4879 */ 5110 */
4880 class HideElementCombinatorImpl implements HideElementCombinator { 5111 class HideElementCombinatorImpl implements HideElementCombinator {
4881 /** 5112 /**
4882 * The unlinked representation of the combinator in the summary. 5113 * The unlinked representation of the combinator in the summary.
4883 */ 5114 */
4884 final UnlinkedCombinator _unlinkedCombinator; 5115 final UnlinkedCombinator _unlinkedCombinator;
4885 5116
4886 /** 5117 /**
4887 * The names that are not to be made visible in the importing library even if 5118 * The names that are not to be made visible in the importing library even if
(...skipping 3761 matching lines...) Expand 10 before | Expand all | Expand 10 after
8649 8880
8650 @override 8881 @override
8651 void visitElement(Element element) { 8882 void visitElement(Element element) {
8652 int offset = element.nameOffset; 8883 int offset = element.nameOffset;
8653 if (offset != -1) { 8884 if (offset != -1) {
8654 map[offset] = element; 8885 map[offset] = element;
8655 } 8886 }
8656 super.visitElement(element); 8887 super.visitElement(element);
8657 } 8888 }
8658 } 8889 }
OLDNEW
« no previous file with comments | « pkg/analyzer/lib/src/dart/element/builder.dart ('k') | pkg/analyzer/lib/src/generated/error_verifier.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698