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

Side by Side Diff: pkg/analyzer/lib/src/summary/resynthesize.dart

Issue 1993333003: Fix for resynthesizing default values referencing type parameters. (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: Created 4 years, 7 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) 2015, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2015, 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 summary_resynthesizer; 5 library summary_resynthesizer;
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/token.dart'; 10 import 'package:analyzer/dart/ast/token.dart';
(...skipping 281 matching lines...) Expand 10 before | Expand all | Expand 10 after
292 } 292 }
293 return hasLibrarySummary(uri); 293 return hasLibrarySummary(uri);
294 } 294 }
295 } 295 }
296 296
297 /** 297 /**
298 * Builder of [Expression]s from [UnlinkedConst]s. 298 * Builder of [Expression]s from [UnlinkedConst]s.
299 */ 299 */
300 class _ConstExprBuilder { 300 class _ConstExprBuilder {
301 final _UnitResynthesizer resynthesizer; 301 final _UnitResynthesizer resynthesizer;
302 final Element context;
302 final UnlinkedConst uc; 303 final UnlinkedConst uc;
303 304
305 bool _typeParameterContextReady = false;
306 TypeParameterizedElementMixin _typeParameterContext;
307
304 int intPtr = 0; 308 int intPtr = 0;
305 int doublePtr = 0; 309 int doublePtr = 0;
306 int stringPtr = 0; 310 int stringPtr = 0;
307 int refPtr = 0; 311 int refPtr = 0;
308 final List<Expression> stack = <Expression>[]; 312 final List<Expression> stack = <Expression>[];
309 313
310 _ConstExprBuilder(this.resynthesizer, this.uc); 314 _ConstExprBuilder(this.resynthesizer, this.context, this.uc);
315
316 TypeParameterizedElementMixin get typeParameterContext {
317 if (!_typeParameterContextReady) {
318 for (Element e = context; e != null; e = e.enclosingElement) {
319 if (e is TypeParameterizedElementMixin) {
320 _typeParameterContext = e;
321 break;
322 }
323 }
324 _typeParameterContextReady = true;
325 }
326 return _typeParameterContext;
327 }
311 328
312 Expression build() { 329 Expression build() {
313 if (!uc.isValidConst) { 330 if (!uc.isValidConst) {
314 return AstFactory.identifier3(r'$$invalidConstExpr$$'); 331 return AstFactory.identifier3(r'$$invalidConstExpr$$');
315 } 332 }
316 for (UnlinkedConstOperation operation in uc.operations) { 333 for (UnlinkedConstOperation operation in uc.operations) {
317 switch (operation) { 334 switch (operation) {
318 case UnlinkedConstOperation.pushNull: 335 case UnlinkedConstOperation.pushNull:
319 _push(AstFactory.nullLiteral()); 336 _push(AstFactory.nullLiteral());
320 break; 337 break;
(...skipping 290 matching lines...) Expand 10 before | Expand all | Expand 10 after
611 ConstructorElement constructorElement; 628 ConstructorElement constructorElement;
612 if (info.element != null) { 629 if (info.element != null) {
613 if (info.element is ConstructorElement) { 630 if (info.element is ConstructorElement) {
614 constructorName = info.name; 631 constructorName = info.name;
615 } else if (info.element is ClassElement) { 632 } else if (info.element is ClassElement) {
616 constructorName = null; 633 constructorName = null;
617 } else { 634 } else {
618 throw new StateError('Unsupported element for invokeConstructor ' 635 throw new StateError('Unsupported element for invokeConstructor '
619 '${info.element?.runtimeType}'); 636 '${info.element?.runtimeType}');
620 } 637 }
621 InterfaceType definingType = 638 InterfaceType definingType = resynthesizer._createConstructorDefiningType(
622 resynthesizer._createConstructorDefiningType(info, ref.typeArguments); 639 typeParameterContext, info, ref.typeArguments);
623 constructorElement = 640 constructorElement =
624 resynthesizer._createConstructorElement(definingType, info); 641 resynthesizer._createConstructorElement(definingType, info);
625 typeNode = _buildTypeAst(definingType); 642 typeNode = _buildTypeAst(definingType);
626 } else { 643 } else {
627 if (info.enclosing != null) { 644 if (info.enclosing != null) {
628 if (info.enclosing.enclosing != null) { 645 if (info.enclosing.enclosing != null) {
629 PrefixedIdentifier typeName = AstFactory.identifier5( 646 PrefixedIdentifier typeName = AstFactory.identifier5(
630 info.enclosing.enclosing.name, info.enclosing.name); 647 info.enclosing.enclosing.name, info.enclosing.name);
631 typeName.prefix.staticElement = info.enclosing.enclosing.element; 648 typeName.prefix.staticElement = info.enclosing.enclosing.element;
632 typeName.identifier.staticElement = info.enclosing.element; 649 typeName.identifier.staticElement = info.enclosing.element;
(...skipping 959 matching lines...) Expand 10 before | Expand all | Expand 10 after
1592 return typeArguments; 1609 return typeArguments;
1593 } 1610 }
1594 } 1611 }
1595 1612
1596 class _ResynthesizerContext implements ResynthesizerContext { 1613 class _ResynthesizerContext implements ResynthesizerContext {
1597 final _UnitResynthesizer _unitResynthesizer; 1614 final _UnitResynthesizer _unitResynthesizer;
1598 1615
1599 _ResynthesizerContext(this._unitResynthesizer); 1616 _ResynthesizerContext(this._unitResynthesizer);
1600 1617
1601 @override 1618 @override
1602 ElementAnnotationImpl buildAnnotation(UnlinkedConst uc) { 1619 ElementAnnotationImpl buildAnnotation(Element context, UnlinkedConst uc) {
1603 return _unitResynthesizer.buildAnnotation(uc); 1620 return _unitResynthesizer.buildAnnotation(context, uc);
1604 } 1621 }
1605 1622
1606 @override 1623 @override
1607 Expression buildExpression(UnlinkedConst uc) { 1624 Expression buildExpression(Element context, UnlinkedConst uc) {
1608 return _unitResynthesizer._buildConstExpression(uc); 1625 return _unitResynthesizer._buildConstExpression(context, uc);
1609 } 1626 }
1610 1627
1611 @override 1628 @override
1612 UnitExplicitTopLevelAccessors buildTopLevelAccessors() { 1629 UnitExplicitTopLevelAccessors buildTopLevelAccessors() {
1613 return _unitResynthesizer.buildUnitExplicitTopLevelAccessors(); 1630 return _unitResynthesizer.buildUnitExplicitTopLevelAccessors();
1614 } 1631 }
1615 1632
1616 @override 1633 @override
1617 UnitExplicitTopLevelVariables buildTopLevelVariables() { 1634 UnitExplicitTopLevelVariables buildTopLevelVariables() {
1618 return _unitResynthesizer.buildUnitExplicitTopLevelVariables(); 1635 return _unitResynthesizer.buildUnitExplicitTopLevelVariables();
(...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after
1747 } 1764 }
1748 1765
1749 SummaryResynthesizer get summaryResynthesizer => 1766 SummaryResynthesizer get summaryResynthesizer =>
1750 libraryResynthesizer.summaryResynthesizer; 1767 libraryResynthesizer.summaryResynthesizer;
1751 1768
1752 TypeProvider get typeProvider => summaryResynthesizer.typeProvider; 1769 TypeProvider get typeProvider => summaryResynthesizer.typeProvider;
1753 1770
1754 /** 1771 /**
1755 * Build [ElementAnnotationImpl] for the given [UnlinkedConst]. 1772 * Build [ElementAnnotationImpl] for the given [UnlinkedConst].
1756 */ 1773 */
1757 ElementAnnotationImpl buildAnnotation(UnlinkedConst uc) { 1774 ElementAnnotationImpl buildAnnotation(Element context, UnlinkedConst uc) {
1758 ElementAnnotationImpl elementAnnotation = new ElementAnnotationImpl(unit); 1775 ElementAnnotationImpl elementAnnotation = new ElementAnnotationImpl(unit);
1759 Expression constExpr = _buildConstExpression(uc); 1776 Expression constExpr = _buildConstExpression(context, uc);
1760 if (constExpr is Identifier) { 1777 if (constExpr is Identifier) {
1761 elementAnnotation.element = constExpr.staticElement; 1778 elementAnnotation.element = constExpr.staticElement;
1762 elementAnnotation.annotationAst = AstFactory.annotation(constExpr); 1779 elementAnnotation.annotationAst = AstFactory.annotation(constExpr);
1763 } else if (constExpr is InstanceCreationExpression) { 1780 } else if (constExpr is InstanceCreationExpression) {
1764 elementAnnotation.element = constExpr.staticElement; 1781 elementAnnotation.element = constExpr.staticElement;
1765 Identifier typeName = constExpr.constructorName.type.name; 1782 Identifier typeName = constExpr.constructorName.type.name;
1766 SimpleIdentifier constructorName = constExpr.constructorName.name; 1783 SimpleIdentifier constructorName = constExpr.constructorName.name;
1767 if (typeName is SimpleIdentifier && constructorName != null) { 1784 if (typeName is SimpleIdentifier && constructorName != null) {
1768 // E.g. `@cls.ctor()`. Since `cls.ctor` would have been parsed as 1785 // E.g. `@cls.ctor()`. Since `cls.ctor` would have been parsed as
1769 // a PrefixedIdentifier, we need to resynthesize it as one. 1786 // a PrefixedIdentifier, we need to resynthesize it as one.
1770 typeName = AstFactory.identifier(typeName, constructorName); 1787 typeName = AstFactory.identifier(typeName, constructorName);
1771 constructorName = null; 1788 constructorName = null;
1772 } 1789 }
1773 elementAnnotation.annotationAst = AstFactory.annotation2( 1790 elementAnnotation.annotationAst = AstFactory.annotation2(
1774 typeName, constructorName, constExpr.argumentList); 1791 typeName, constructorName, constExpr.argumentList);
1775 } else { 1792 } else {
1776 throw new StateError( 1793 throw new StateError(
1777 'Unexpected annotation type: ${constExpr.runtimeType}'); 1794 'Unexpected annotation type: ${constExpr.runtimeType}');
1778 } 1795 }
1779 return elementAnnotation; 1796 return elementAnnotation;
1780 } 1797 }
1781 1798
1782 /** 1799 /**
1783 * Build the annotations for the given [element]. 1800 * Build the annotations for the given [element].
1784 */ 1801 */
1785 void buildAnnotations( 1802 void buildAnnotations(
1786 ElementImpl element, List<UnlinkedConst> serializedAnnotations) { 1803 ElementImpl element, List<UnlinkedConst> serializedAnnotations) {
1787 if (serializedAnnotations.isNotEmpty) { 1804 if (serializedAnnotations.isNotEmpty) {
1788 element.metadata = serializedAnnotations.map(buildAnnotation).toList(); 1805 element.metadata = serializedAnnotations
1806 .map((a) => buildAnnotation(element, a))
1807 .toList();
1789 } 1808 }
1790 } 1809 }
1791 1810
1792 /** 1811 /**
1793 * Resynthesize a [ClassElement] and place it in [unitHolder]. 1812 * Resynthesize a [ClassElement] and place it in [unitHolder].
1794 */ 1813 */
1795 void buildClass(UnlinkedClass serializedClass) { 1814 void buildClass(UnlinkedClass serializedClass) {
1796 ClassElement classElement; 1815 ClassElement classElement;
1797 if (libraryResynthesizer.isCoreLibrary && 1816 if (libraryResynthesizer.isCoreLibrary &&
1798 serializedClass.supertype == null) { 1817 serializedClass.supertype == null) {
1799 classElement = buildClassImpl(serializedClass, null); 1818 classElement = buildClassImpl(serializedClass, null);
1800 if (!serializedClass.hasNoSupertype) { 1819 if (!serializedClass.hasNoSupertype) {
1801 libraryResynthesizer.delayedObjectSubclasses.add(classElement); 1820 libraryResynthesizer.delayedObjectSubclasses.add(classElement);
1802 } 1821 }
1803 } else { 1822 } else {
1804 classElement = new _DeferredClassElement(this, unit, serializedClass); 1823 classElement = new _DeferredClassElement(this, unit, serializedClass);
1805 } 1824 }
1806 unitHolder.addType(classElement); 1825 unitHolder.addType(classElement);
1807 } 1826 }
1808 1827
1809 /** 1828 /**
1810 * Fill the given [ClassElementImpl] with executable elements and fields. 1829 * Fill the given [ClassElementImpl] with executable elements and fields.
1811 */ 1830 */
1812 void buildClassExecutables( 1831 void buildClassExecutables(
1813 ClassElementImpl classElement, UnlinkedClass serializedClass) { 1832 ClassElementImpl classElement, UnlinkedClass serializedClass) {
1814 currentTypeParameters.add(classElement.typeParameters); 1833 currentTypeParameters.add(classElement.typeParameters);
1815 ElementHolder memberHolder = new ElementHolder(); 1834 ElementHolder memberHolder = new ElementHolder();
1816 fields = <String, FieldElementImpl>{}; 1835 fields = <String, FieldElementImpl>{};
1817 for (UnlinkedVariable serializedVariable in serializedClass.fields) { 1836 for (UnlinkedVariable serializedVariable in serializedClass.fields) {
1818 buildVariable(serializedVariable, memberHolder); 1837 buildVariable(classElement, serializedVariable, memberHolder);
1819 } 1838 }
1820 bool constructorFound = false; 1839 bool constructorFound = false;
1821 constructors = <String, ConstructorElementImpl>{}; 1840 constructors = <String, ConstructorElementImpl>{};
1822 for (UnlinkedExecutable serializedExecutable 1841 for (UnlinkedExecutable serializedExecutable
1823 in serializedClass.executables) { 1842 in serializedClass.executables) {
1824 switch (serializedExecutable.kind) { 1843 switch (serializedExecutable.kind) {
1825 case UnlinkedExecutableKind.constructor: 1844 case UnlinkedExecutableKind.constructor:
1826 constructorFound = true; 1845 constructorFound = true;
1827 buildConstructor(serializedExecutable, classElement, memberHolder); 1846 buildConstructor(serializedExecutable, classElement, memberHolder);
1828 break; 1847 break;
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after
1919 HideElementCombinatorImpl combinator = new HideElementCombinatorImpl(); 1938 HideElementCombinatorImpl combinator = new HideElementCombinatorImpl();
1920 // Note: we call toList() so that we don't retain a reference to the 1939 // Note: we call toList() so that we don't retain a reference to the
1921 // deserialized data structure. 1940 // deserialized data structure.
1922 combinator.hiddenNames = serializedCombinator.hides.toList(); 1941 combinator.hiddenNames = serializedCombinator.hides.toList();
1923 return combinator; 1942 return combinator;
1924 } 1943 }
1925 } 1944 }
1926 1945
1927 /** 1946 /**
1928 * Resynthesize a [ConstructorElement] and place it in the given [holder]. 1947 * Resynthesize a [ConstructorElement] and place it in the given [holder].
1929 * [classType] is the type of the class for which this element is a 1948 * [classElement] is the element of the class for which this element is a
1930 * constructor. 1949 * constructor.
1931 */ 1950 */
1932 void buildConstructor(UnlinkedExecutable serializedExecutable, 1951 void buildConstructor(UnlinkedExecutable serializedExecutable,
1933 ClassElementImpl classElement, ElementHolder holder) { 1952 ClassElementImpl classElement, ElementHolder holder) {
1934 assert(serializedExecutable.kind == UnlinkedExecutableKind.constructor); 1953 assert(serializedExecutable.kind == UnlinkedExecutableKind.constructor);
1935 currentConstructor = new ConstructorElementImpl.forSerialized( 1954 currentConstructor = new ConstructorElementImpl.forSerialized(
1936 serializedExecutable, classElement); 1955 serializedExecutable, classElement);
1937 currentConstructor.isCycleFree = serializedExecutable.isConst && 1956 currentConstructor.isCycleFree = serializedExecutable.isConst &&
1938 !constCycles.contains(serializedExecutable.constCycleSlot); 1957 !constCycles.contains(serializedExecutable.constCycleSlot);
1939 if (serializedExecutable.name.isEmpty) { 1958 if (serializedExecutable.name.isEmpty) {
1940 currentConstructor.nameEnd = 1959 currentConstructor.nameEnd =
1941 serializedExecutable.nameOffset + classElement.name.length; 1960 serializedExecutable.nameOffset + classElement.name.length;
1942 } else { 1961 } else {
1943 currentConstructor.nameEnd = serializedExecutable.nameEnd; 1962 currentConstructor.nameEnd = serializedExecutable.nameEnd;
1944 currentConstructor.periodOffset = serializedExecutable.periodOffset; 1963 currentConstructor.periodOffset = serializedExecutable.periodOffset;
1945 } 1964 }
1946 constructors[serializedExecutable.name] = currentConstructor; 1965 constructors[serializedExecutable.name] = currentConstructor;
1947 currentConstructor.returnType = classElement.type; 1966 currentConstructor.returnType = classElement.type;
1948 buildExecutableCommonParts(currentConstructor, serializedExecutable); 1967 buildExecutableCommonParts(currentConstructor, serializedExecutable);
1949 currentConstructor.constantInitializers = serializedExecutable 1968 currentConstructor.constantInitializers = serializedExecutable
1950 .constantInitializers 1969 .constantInitializers
1951 .map(buildConstructorInitializer) 1970 .map((i) => buildConstructorInitializer(currentConstructor, i))
1952 .toList(); 1971 .toList();
1953 if (serializedExecutable.isRedirectedConstructor) { 1972 if (serializedExecutable.isRedirectedConstructor) {
1954 if (serializedExecutable.isFactory) { 1973 if (serializedExecutable.isFactory) {
1955 EntityRef redirectedConstructor = 1974 EntityRef redirectedConstructor =
1956 serializedExecutable.redirectedConstructor; 1975 serializedExecutable.redirectedConstructor;
1957 _ReferenceInfo info = getReferenceInfo(redirectedConstructor.reference); 1976 _ReferenceInfo info = getReferenceInfo(redirectedConstructor.reference);
1958 List<EntityRef> typeArguments = redirectedConstructor.typeArguments; 1977 List<EntityRef> typeArguments = redirectedConstructor.typeArguments;
1959 currentConstructor.redirectedConstructor = _createConstructorElement( 1978 currentConstructor.redirectedConstructor = _createConstructorElement(
1960 _createConstructorDefiningType(info, typeArguments), info); 1979 _createConstructorDefiningType(classElement, info, typeArguments),
1980 info);
1961 } else { 1981 } else {
1962 List<String> locationComponents = unit.location.components.toList(); 1982 List<String> locationComponents = unit.location.components.toList();
1963 locationComponents.add(classElement.name); 1983 locationComponents.add(classElement.name);
1964 locationComponents.add(serializedExecutable.redirectedConstructorName); 1984 locationComponents.add(serializedExecutable.redirectedConstructorName);
1965 currentConstructor.redirectedConstructor = 1985 currentConstructor.redirectedConstructor =
1966 new _DeferredConstructorElement._( 1986 new _DeferredConstructorElement._(
1967 classElement.type, 1987 classElement.type,
1968 serializedExecutable.redirectedConstructorName, 1988 serializedExecutable.redirectedConstructorName,
1969 new ElementLocationImpl.con3(locationComponents)); 1989 new ElementLocationImpl.con3(locationComponents));
1970 } 1990 }
1971 } 1991 }
1972 holder.addConstructor(currentConstructor); 1992 holder.addConstructor(currentConstructor);
1973 currentConstructor = null; 1993 currentConstructor = null;
1974 } 1994 }
1975 1995
1976 /** 1996 /**
1977 * Resynthesize the [ConstructorInitializer] in context of 1997 * Resynthesize the [ConstructorInitializer] in context of
1978 * [currentConstructor], which is used to resolve constructor parameter names. 1998 * [currentConstructor], which is used to resolve constructor parameter names.
1979 */ 1999 */
1980 ConstructorInitializer buildConstructorInitializer( 2000 ConstructorInitializer buildConstructorInitializer(
2001 ConstructorElementImpl enclosingConstructor,
1981 UnlinkedConstructorInitializer serialized) { 2002 UnlinkedConstructorInitializer serialized) {
1982 UnlinkedConstructorInitializerKind kind = serialized.kind; 2003 UnlinkedConstructorInitializerKind kind = serialized.kind;
1983 String name = serialized.name; 2004 String name = serialized.name;
1984 List<Expression> arguments = <Expression>[]; 2005 List<Expression> arguments = <Expression>[];
1985 { 2006 {
1986 int numArguments = serialized.arguments.length; 2007 int numArguments = serialized.arguments.length;
1987 int numNames = serialized.argumentNames.length; 2008 int numNames = serialized.argumentNames.length;
1988 for (int i = 0; i < numArguments; i++) { 2009 for (int i = 0; i < numArguments; i++) {
1989 Expression expression = _buildConstExpression(serialized.arguments[i]); 2010 Expression expression = _buildConstExpression(
2011 enclosingConstructor, serialized.arguments[i]);
1990 int nameIndex = numNames + i - numArguments; 2012 int nameIndex = numNames + i - numArguments;
1991 if (nameIndex >= 0) { 2013 if (nameIndex >= 0) {
1992 expression = AstFactory.namedExpression2( 2014 expression = AstFactory.namedExpression2(
1993 serialized.argumentNames[nameIndex], expression); 2015 serialized.argumentNames[nameIndex], expression);
1994 } 2016 }
1995 arguments.add(expression); 2017 arguments.add(expression);
1996 } 2018 }
1997 } 2019 }
1998 switch (kind) { 2020 switch (kind) {
1999 case UnlinkedConstructorInitializerKind.field: 2021 case UnlinkedConstructorInitializerKind.field:
2000 return AstFactory.constructorFieldInitializer( 2022 return AstFactory.constructorFieldInitializer(false, name,
2001 false, name, _buildConstExpression(serialized.expression)); 2023 _buildConstExpression(enclosingConstructor, serialized.expression));
2002 case UnlinkedConstructorInitializerKind.superInvocation: 2024 case UnlinkedConstructorInitializerKind.superInvocation:
2003 return AstFactory.superConstructorInvocation2( 2025 return AstFactory.superConstructorInvocation2(
2004 name.isNotEmpty ? name : null, arguments); 2026 name.isNotEmpty ? name : null, arguments);
2005 case UnlinkedConstructorInitializerKind.thisInvocation: 2027 case UnlinkedConstructorInitializerKind.thisInvocation:
2006 return AstFactory.redirectingConstructorInvocation2( 2028 return AstFactory.redirectingConstructorInvocation2(
2007 name.isNotEmpty ? name : null, arguments); 2029 name.isNotEmpty ? name : null, arguments);
2008 } 2030 }
2009 } 2031 }
2010 2032
2011 /** 2033 /**
(...skipping 351 matching lines...) Expand 10 before | Expand all | Expand 10 after
2363 * Resynthesize a [LocalVariableElement]. 2385 * Resynthesize a [LocalVariableElement].
2364 */ 2386 */
2365 LocalVariableElement buildLocalVariable(UnlinkedVariable serializedVariable, 2387 LocalVariableElement buildLocalVariable(UnlinkedVariable serializedVariable,
2366 ExecutableElementImpl enclosingExecutable) { 2388 ExecutableElementImpl enclosingExecutable) {
2367 LocalVariableElementImpl element; 2389 LocalVariableElementImpl element;
2368 if (serializedVariable.constExpr != null && serializedVariable.isConst) { 2390 if (serializedVariable.constExpr != null && serializedVariable.isConst) {
2369 ConstLocalVariableElementImpl constElement = 2391 ConstLocalVariableElementImpl constElement =
2370 new ConstLocalVariableElementImpl.forSerialized( 2392 new ConstLocalVariableElementImpl.forSerialized(
2371 serializedVariable, enclosingExecutable); 2393 serializedVariable, enclosingExecutable);
2372 element = constElement; 2394 element = constElement;
2373 constElement.constantInitializer = 2395 constElement.constantInitializer = _buildConstExpression(
2374 _buildConstExpression(serializedVariable.constExpr); 2396 enclosingExecutable, serializedVariable.constExpr);
2375 } else { 2397 } else {
2376 element = new LocalVariableElementImpl.forSerialized( 2398 element = new LocalVariableElementImpl.forSerialized(
2377 serializedVariable, enclosingExecutable); 2399 serializedVariable, enclosingExecutable);
2378 } 2400 }
2379 if (serializedVariable.visibleOffset != 0) { 2401 if (serializedVariable.visibleOffset != 0) {
2380 element.setVisibleRange( 2402 element.setVisibleRange(
2381 serializedVariable.visibleOffset, serializedVariable.visibleLength); 2403 serializedVariable.visibleOffset, serializedVariable.visibleLength);
2382 } 2404 }
2383 buildVariableCommonParts(element, serializedVariable, 2405 buildVariableCommonParts(element, serializedVariable,
2384 isLazilyResynthesized: true); 2406 isLazilyResynthesized: true);
(...skipping 13 matching lines...) Expand all
2398 FieldFormalParameterElementImpl initializingParameter; 2420 FieldFormalParameterElementImpl initializingParameter;
2399 if (serializedParameter.kind == UnlinkedParamKind.required) { 2421 if (serializedParameter.kind == UnlinkedParamKind.required) {
2400 initializingParameter = new FieldFormalParameterElementImpl( 2422 initializingParameter = new FieldFormalParameterElementImpl(
2401 serializedParameter.name, nameOffset); 2423 serializedParameter.name, nameOffset);
2402 } else { 2424 } else {
2403 DefaultFieldFormalParameterElementImpl defaultParameter = 2425 DefaultFieldFormalParameterElementImpl defaultParameter =
2404 new DefaultFieldFormalParameterElementImpl( 2426 new DefaultFieldFormalParameterElementImpl(
2405 serializedParameter.name, nameOffset); 2427 serializedParameter.name, nameOffset);
2406 initializingParameter = defaultParameter; 2428 initializingParameter = defaultParameter;
2407 if (serializedParameter.defaultValue != null) { 2429 if (serializedParameter.defaultValue != null) {
2408 defaultParameter.constantInitializer = 2430 defaultParameter.constantInitializer = _buildConstExpression(
2409 _buildConstExpression(serializedParameter.defaultValue); 2431 enclosingElement, serializedParameter.defaultValue);
2410 defaultParameter.defaultValueCode = 2432 defaultParameter.defaultValueCode =
2411 serializedParameter.defaultValueCode; 2433 serializedParameter.defaultValueCode;
2412 } 2434 }
2413 } 2435 }
2414 parameterElement = initializingParameter; 2436 parameterElement = initializingParameter;
2415 initializingParameter.field = fields[serializedParameter.name]; 2437 initializingParameter.field = fields[serializedParameter.name];
2416 } else { 2438 } else {
2417 if (serializedParameter.kind == UnlinkedParamKind.required) { 2439 if (serializedParameter.kind == UnlinkedParamKind.required) {
2418 parameterElement = new ParameterElementImpl.forSerialized( 2440 parameterElement = new ParameterElementImpl.forSerialized(
2419 serializedParameter, enclosingElement); 2441 serializedParameter, enclosingElement);
(...skipping 247 matching lines...) Expand 10 before | Expand all | Expand 10 after
2667 UnitExplicitTopLevelVariables variablesData = 2689 UnitExplicitTopLevelVariables variablesData =
2668 new UnitExplicitTopLevelVariables(); 2690 new UnitExplicitTopLevelVariables();
2669 for (UnlinkedVariable unlinkedVariable in unlinkedUnit.variables) { 2691 for (UnlinkedVariable unlinkedVariable in unlinkedUnit.variables) {
2670 TopLevelVariableElementImpl element; 2692 TopLevelVariableElementImpl element;
2671 if (unlinkedVariable.constExpr != null && unlinkedVariable.isConst) { 2693 if (unlinkedVariable.constExpr != null && unlinkedVariable.isConst) {
2672 ConstTopLevelVariableElementImpl constElement = 2694 ConstTopLevelVariableElementImpl constElement =
2673 new ConstTopLevelVariableElementImpl.forSerialized( 2695 new ConstTopLevelVariableElementImpl.forSerialized(
2674 unlinkedVariable, unit); 2696 unlinkedVariable, unit);
2675 element = constElement; 2697 element = constElement;
2676 constElement.constantInitializer = 2698 constElement.constantInitializer =
2677 _buildConstExpression(unlinkedVariable.constExpr); 2699 _buildConstExpression(null, unlinkedVariable.constExpr);
2678 } else { 2700 } else {
2679 element = new TopLevelVariableElementImpl.forSerialized( 2701 element = new TopLevelVariableElementImpl.forSerialized(
2680 unlinkedVariable, unit); 2702 unlinkedVariable, unit);
2681 } 2703 }
2682 buildPropertyIntroducingElementCommonParts(element, unlinkedVariable, 2704 buildPropertyIntroducingElementCommonParts(element, unlinkedVariable,
2683 isLazilyResynthesized: true); 2705 isLazilyResynthesized: true);
2684 variablesData.variables.add(element); 2706 variablesData.variables.add(element);
2685 // implicit accessors 2707 // implicit accessors
2686 String name = element.name; 2708 String name = element.name;
2687 DartType type = element.type; 2709 DartType type = element.type;
2688 variablesData.implicitAccessors 2710 variablesData.implicitAccessors
2689 .add(buildImplicitGetter(element, name, type)); 2711 .add(buildImplicitGetter(element, name, type));
2690 if (!(element.isConst || element.isFinal)) { 2712 if (!(element.isConst || element.isFinal)) {
2691 variablesData.implicitAccessors 2713 variablesData.implicitAccessors
2692 .add(buildImplicitSetter(element, name, type)); 2714 .add(buildImplicitSetter(element, name, type));
2693 } 2715 }
2694 } 2716 }
2695 return variablesData; 2717 return variablesData;
2696 } 2718 }
2697 2719
2698 /** 2720 /**
2699 * Resynthesize a [TopLevelVariableElement] or [FieldElement]. 2721 * Resynthesize a [TopLevelVariableElement] or [FieldElement].
2700 */ 2722 */
2701 void buildVariable(UnlinkedVariable serializedVariable, 2723 void buildVariable(
2724 ClassElementImpl enclosingClass, UnlinkedVariable serializedVariable,
2702 [ElementHolder holder]) { 2725 [ElementHolder holder]) {
2703 if (holder == null) { 2726 if (holder == null) {
2704 throw new UnimplementedError('Must be lazy'); 2727 throw new UnimplementedError('Must be lazy');
2705 } else { 2728 } else {
2706 FieldElementImpl element; 2729 FieldElementImpl element;
2707 if (serializedVariable.constExpr != null && 2730 if (serializedVariable.constExpr != null &&
2708 (serializedVariable.isConst || 2731 (serializedVariable.isConst ||
2709 serializedVariable.isFinal && !serializedVariable.isStatic)) { 2732 serializedVariable.isFinal && !serializedVariable.isStatic)) {
2710 ConstFieldElementImpl constElement = new ConstFieldElementImpl( 2733 ConstFieldElementImpl constElement = new ConstFieldElementImpl(
2711 serializedVariable.name, serializedVariable.nameOffset); 2734 serializedVariable.name, serializedVariable.nameOffset);
2712 element = constElement; 2735 element = constElement;
2713 constElement.constantInitializer = 2736 constElement.constantInitializer =
2714 _buildConstExpression(serializedVariable.constExpr); 2737 _buildConstExpression(enclosingClass, serializedVariable.constExpr);
2715 } else { 2738 } else {
2716 element = new FieldElementImpl( 2739 element = new FieldElementImpl(
2717 serializedVariable.name, serializedVariable.nameOffset); 2740 serializedVariable.name, serializedVariable.nameOffset);
2718 } 2741 }
2719 buildPropertyIntroducingElementCommonParts(element, serializedVariable); 2742 buildPropertyIntroducingElementCommonParts(element, serializedVariable);
2720 element.static = serializedVariable.isStatic; 2743 element.static = serializedVariable.isStatic;
2721 holder.addField(element); 2744 holder.addField(element);
2722 buildImplicitAccessors(element, holder); 2745 buildImplicitAccessors(element, holder);
2723 fields[element.name] = element; 2746 fields[element.name] = element;
2724 } 2747 }
(...skipping 242 matching lines...) Expand 10 before | Expand all | Expand 10 after
2967 } else if (initializer is RedirectingConstructorInvocation) { 2990 } else if (initializer is RedirectingConstructorInvocation) {
2968 SimpleIdentifier nameNode = initializer.constructorName; 2991 SimpleIdentifier nameNode = initializer.constructorName;
2969 ConstructorElement element = constructors[nameNode?.name ?? '']; 2992 ConstructorElement element = constructors[nameNode?.name ?? ''];
2970 initializer.staticElement = element; 2993 initializer.staticElement = element;
2971 nameNode?.staticElement = element; 2994 nameNode?.staticElement = element;
2972 } 2995 }
2973 } 2996 }
2974 } 2997 }
2975 } 2998 }
2976 2999
2977 Expression _buildConstExpression(UnlinkedConst uc) { 3000 Expression _buildConstExpression(Element context, UnlinkedConst uc) {
2978 return new _ConstExprBuilder(this, uc).build(); 3001 return new _ConstExprBuilder(this, context, uc).build();
2979 } 3002 }
2980 3003
2981 /** 3004 /**
2982 * Return the defining type for a [ConstructorElement] by applying 3005 * Return the defining type for a [ConstructorElement] by applying
2983 * [typeArgumentRefs] to the given linked [info]. 3006 * [typeArgumentRefs] to the given linked [info].
2984 */ 3007 */
2985 InterfaceType _createConstructorDefiningType( 3008 InterfaceType _createConstructorDefiningType(
2986 _ReferenceInfo info, List<EntityRef> typeArgumentRefs) { 3009 TypeParameterizedElementMixin typeParameterContext,
3010 _ReferenceInfo info,
3011 List<EntityRef> typeArgumentRefs) {
2987 bool isClass = info.element is ClassElement; 3012 bool isClass = info.element is ClassElement;
2988 _ReferenceInfo classInfo = isClass ? info : info.enclosing; 3013 _ReferenceInfo classInfo = isClass ? info : info.enclosing;
2989 List<DartType> typeArguments = typeArgumentRefs 3014 List<DartType> typeArguments = typeArgumentRefs
2990 .map((t) => buildType(t, _currentTypeParameterizedElement)) 3015 .map((t) => buildType(t, typeParameterContext))
2991 .toList(); 3016 .toList();
2992 return classInfo.buildType(true, typeArguments.length, (i) { 3017 return classInfo.buildType(true, typeArguments.length, (i) {
2993 if (i < typeArguments.length) { 3018 if (i < typeArguments.length) {
2994 return typeArguments[i]; 3019 return typeArguments[i];
2995 } else { 3020 } else {
2996 return DynamicTypeImpl.instance; 3021 return DynamicTypeImpl.instance;
2997 } 3022 }
2998 }, const <int>[]); 3023 }, const <int>[]);
2999 } 3024 }
3000 3025
(...skipping 24 matching lines...) Expand all
3025 static String _getElementIdentifier(String name, ReferenceKind kind) { 3050 static String _getElementIdentifier(String name, ReferenceKind kind) {
3026 if (kind == ReferenceKind.topLevelPropertyAccessor || 3051 if (kind == ReferenceKind.topLevelPropertyAccessor ||
3027 kind == ReferenceKind.propertyAccessor) { 3052 kind == ReferenceKind.propertyAccessor) {
3028 if (!name.endsWith('=')) { 3053 if (!name.endsWith('=')) {
3029 return name + '?'; 3054 return name + '?';
3030 } 3055 }
3031 } 3056 }
3032 return name; 3057 return name;
3033 } 3058 }
3034 } 3059 }
OLDNEW
« no previous file with comments | « pkg/analyzer/lib/src/dart/element/element.dart ('k') | pkg/analyzer/test/src/summary/resynthesize_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698