| OLD | NEW |
| 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/element/element.dart'; | 9 import 'package:analyzer/dart/element/element.dart'; |
| 10 import 'package:analyzer/dart/element/type.dart'; | 10 import 'package:analyzer/dart/element/type.dart'; |
| (...skipping 264 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 275 * are nested (e.g. a generic executable inside a generic class), this is the | 275 * are nested (e.g. a generic executable inside a generic class), this is the |
| 276 * concatenation of all type parameters from all declarations currently in | 276 * concatenation of all type parameters from all declarations currently in |
| 277 * force, with the outermost declaration appearing first. If there are no | 277 * force, with the outermost declaration appearing first. If there are no |
| 278 * type parameters, or we are not currently resynthesizing a class, typedef, | 278 * type parameters, or we are not currently resynthesizing a class, typedef, |
| 279 * or executable, then this is an empty list. | 279 * or executable, then this is an empty list. |
| 280 */ | 280 */ |
| 281 List<TypeParameterElement> currentTypeParameters = <TypeParameterElement>[]; | 281 List<TypeParameterElement> currentTypeParameters = <TypeParameterElement>[]; |
| 282 | 282 |
| 283 /** | 283 /** |
| 284 * If a class is currently being resynthesized, map from field name to the | 284 * If a class is currently being resynthesized, map from field name to the |
| 285 * type of the corresponding field. This is used to populate the types of | 285 * corresponding field element. This is used when resynthesizing |
| 286 * initializing formal parameters whose type is implicit. | 286 * initializing formal parameters. |
| 287 */ | 287 */ |
| 288 Map<String, DartType> fieldTypes; | 288 Map<String, FieldElementImpl> fields; |
| 289 | 289 |
| 290 _LibraryResynthesizer(this.summaryResynthesizer, this.linkedLibrary, | 290 _LibraryResynthesizer(this.summaryResynthesizer, this.linkedLibrary, |
| 291 this.unlinkedUnits, this.librarySource) { | 291 this.unlinkedUnits, this.librarySource) { |
| 292 isCoreLibrary = librarySource.uri.toString() == 'dart:core'; | 292 isCoreLibrary = librarySource.uri.toString() == 'dart:core'; |
| 293 } | 293 } |
| 294 | 294 |
| 295 /** | 295 /** |
| 296 * Return a list of type arguments corresponding to [currentTypeParameters]. | 296 * Return a list of type arguments corresponding to [currentTypeParameters]. |
| 297 */ | 297 */ |
| 298 List<TypeParameterType> get currentTypeArguments => currentTypeParameters | 298 List<TypeParameterType> get currentTypeArguments => currentTypeParameters |
| (...skipping 22 matching lines...) Expand all Loading... |
| 321 delayedObjectSubclasses.add(classElement); | 321 delayedObjectSubclasses.add(classElement); |
| 322 } else { | 322 } else { |
| 323 classElement.supertype = summaryResynthesizer.typeProvider.objectType; | 323 classElement.supertype = summaryResynthesizer.typeProvider.objectType; |
| 324 } | 324 } |
| 325 } | 325 } |
| 326 classElement.interfaces = | 326 classElement.interfaces = |
| 327 serializedClass.interfaces.map(buildType).toList(); | 327 serializedClass.interfaces.map(buildType).toList(); |
| 328 classElement.mixins = serializedClass.mixins.map(buildType).toList(); | 328 classElement.mixins = serializedClass.mixins.map(buildType).toList(); |
| 329 classElement.typeParameters = currentTypeParameters; | 329 classElement.typeParameters = currentTypeParameters; |
| 330 ElementHolder memberHolder = new ElementHolder(); | 330 ElementHolder memberHolder = new ElementHolder(); |
| 331 fieldTypes = <String, DartType>{}; | 331 fields = <String, FieldElementImpl>{}; |
| 332 for (UnlinkedVariable serializedVariable in serializedClass.fields) { | 332 for (UnlinkedVariable serializedVariable in serializedClass.fields) { |
| 333 buildVariable(serializedVariable, memberHolder); | 333 buildVariable(serializedVariable, memberHolder); |
| 334 } | 334 } |
| 335 bool constructorFound = false; | 335 bool constructorFound = false; |
| 336 for (UnlinkedExecutable serializedExecutable | 336 for (UnlinkedExecutable serializedExecutable |
| 337 in serializedClass.executables) { | 337 in serializedClass.executables) { |
| 338 switch (serializedExecutable.kind) { | 338 switch (serializedExecutable.kind) { |
| 339 case UnlinkedExecutableKind.constructor: | 339 case UnlinkedExecutableKind.constructor: |
| 340 constructorFound = true; | 340 constructorFound = true; |
| 341 buildConstructor( | 341 buildConstructor( |
| (...skipping 21 matching lines...) Expand all Loading... |
| 363 } | 363 } |
| 364 classElement.accessors = memberHolder.accessors; | 364 classElement.accessors = memberHolder.accessors; |
| 365 classElement.fields = memberHolder.fields; | 365 classElement.fields = memberHolder.fields; |
| 366 classElement.methods = memberHolder.methods; | 366 classElement.methods = memberHolder.methods; |
| 367 correspondingType.typeArguments = currentTypeArguments; | 367 correspondingType.typeArguments = currentTypeArguments; |
| 368 classElement.type = correspondingType; | 368 classElement.type = correspondingType; |
| 369 buildDocumentation(classElement, serializedClass.documentationComment); | 369 buildDocumentation(classElement, serializedClass.documentationComment); |
| 370 unitHolder.addType(classElement); | 370 unitHolder.addType(classElement); |
| 371 } finally { | 371 } finally { |
| 372 currentTypeParameters = <TypeParameterElement>[]; | 372 currentTypeParameters = <TypeParameterElement>[]; |
| 373 fieldTypes = null; | 373 fields = null; |
| 374 } | 374 } |
| 375 } | 375 } |
| 376 | 376 |
| 377 /** | 377 /** |
| 378 * Resynthesize a [NamespaceCombinator]. | 378 * Resynthesize a [NamespaceCombinator]. |
| 379 */ | 379 */ |
| 380 NamespaceCombinator buildCombinator(UnlinkedCombinator serializedCombinator) { | 380 NamespaceCombinator buildCombinator(UnlinkedCombinator serializedCombinator) { |
| 381 if (serializedCombinator.shows.isNotEmpty) { | 381 if (serializedCombinator.shows.isNotEmpty) { |
| 382 ShowElementCombinatorImpl combinator = new ShowElementCombinatorImpl(); | 382 ShowElementCombinatorImpl combinator = new ShowElementCombinatorImpl(); |
| 383 // Note: we call toList() so that we don't retain a reference to the | 383 // Note: we call toList() so that we don't retain a reference to the |
| (...skipping 472 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 856 library.createLoadLibraryFunction(summaryResynthesizer.typeProvider); | 856 library.createLoadLibraryFunction(summaryResynthesizer.typeProvider); |
| 857 } | 857 } |
| 858 // Done. | 858 // Done. |
| 859 return library; | 859 return library; |
| 860 } | 860 } |
| 861 | 861 |
| 862 /** | 862 /** |
| 863 * Resynthesize a [ParameterElement]. | 863 * Resynthesize a [ParameterElement]. |
| 864 */ | 864 */ |
| 865 ParameterElement buildParameter(UnlinkedParam serializedParameter) { | 865 ParameterElement buildParameter(UnlinkedParam serializedParameter) { |
| 866 ParameterElementImpl parameterElement = new ParameterElementImpl( | 866 ParameterElementImpl parameterElement; |
| 867 serializedParameter.name, serializedParameter.nameOffset); | 867 if (serializedParameter.isInitializingFormal) { |
| 868 parameterElement = new FieldFormalParameterElementImpl.forNameAndOffset( |
| 869 serializedParameter.name, serializedParameter.nameOffset) |
| 870 ..field = fields[serializedParameter.name]; |
| 871 } else { |
| 872 parameterElement = new ParameterElementImpl( |
| 873 serializedParameter.name, serializedParameter.nameOffset); |
| 874 } |
| 868 if (serializedParameter.isFunctionTyped) { | 875 if (serializedParameter.isFunctionTyped) { |
| 869 FunctionElementImpl parameterTypeElement = | 876 FunctionElementImpl parameterTypeElement = |
| 870 new FunctionElementImpl('', -1); | 877 new FunctionElementImpl('', -1); |
| 871 parameterTypeElement.synthetic = true; | 878 parameterTypeElement.synthetic = true; |
| 872 parameterElement.parameters = | 879 parameterElement.parameters = |
| 873 serializedParameter.parameters.map(buildParameter).toList(); | 880 serializedParameter.parameters.map(buildParameter).toList(); |
| 874 parameterTypeElement.enclosingElement = parameterElement; | 881 parameterTypeElement.enclosingElement = parameterElement; |
| 875 parameterTypeElement.shareParameters(parameterElement.parameters); | 882 parameterTypeElement.shareParameters(parameterElement.parameters); |
| 876 if (serializedParameter.type != null) { | 883 if (serializedParameter.type != null) { |
| 877 parameterTypeElement.returnType = buildType(serializedParameter.type); | 884 parameterTypeElement.returnType = buildType(serializedParameter.type); |
| 878 } else { | 885 } else { |
| 879 parameterTypeElement.returnType = VoidTypeImpl.instance; | 886 parameterTypeElement.returnType = VoidTypeImpl.instance; |
| 880 } | 887 } |
| 881 parameterElement.type = new FunctionTypeImpl.elementWithNameAndArgs( | 888 parameterElement.type = new FunctionTypeImpl.elementWithNameAndArgs( |
| 882 parameterTypeElement, null, currentTypeArguments, false); | 889 parameterTypeElement, null, currentTypeArguments, false); |
| 883 } else { | 890 } else { |
| 884 if (serializedParameter.isInitializingFormal && | 891 if (serializedParameter.isInitializingFormal && |
| 885 serializedParameter.hasImplicitType) { | 892 serializedParameter.hasImplicitType) { |
| 886 // The type is inherited from the matching field. | 893 // The type is inherited from the matching field. |
| 887 parameterElement.type = fieldTypes[serializedParameter.name] ?? | 894 parameterElement.type = fields[serializedParameter.name]?.type ?? |
| 888 summaryResynthesizer.typeProvider.dynamicType; | 895 summaryResynthesizer.typeProvider.dynamicType; |
| 889 } else { | 896 } else { |
| 890 parameterElement.type = buildType(serializedParameter.type); | 897 parameterElement.type = buildType(serializedParameter.type); |
| 891 } | 898 } |
| 892 parameterElement.hasImplicitType = serializedParameter.hasImplicitType; | 899 parameterElement.hasImplicitType = serializedParameter.hasImplicitType; |
| 893 } | 900 } |
| 894 switch (serializedParameter.kind) { | 901 switch (serializedParameter.kind) { |
| 895 case UnlinkedParamKind.named: | 902 case UnlinkedParamKind.named: |
| 896 parameterElement.parameterKind = ParameterKind.NAMED; | 903 parameterElement.parameterKind = ParameterKind.NAMED; |
| 897 break; | 904 break; |
| (...skipping 165 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1063 buildVariableCommonParts(element, serializedVariable); | 1070 buildVariableCommonParts(element, serializedVariable); |
| 1064 unitHolder.addTopLevelVariable(element); | 1071 unitHolder.addTopLevelVariable(element); |
| 1065 buildImplicitAccessors(element, unitHolder); | 1072 buildImplicitAccessors(element, unitHolder); |
| 1066 } else { | 1073 } else { |
| 1067 FieldElementImpl element = new FieldElementImpl( | 1074 FieldElementImpl element = new FieldElementImpl( |
| 1068 serializedVariable.name, serializedVariable.nameOffset); | 1075 serializedVariable.name, serializedVariable.nameOffset); |
| 1069 buildVariableCommonParts(element, serializedVariable); | 1076 buildVariableCommonParts(element, serializedVariable); |
| 1070 element.static = serializedVariable.isStatic; | 1077 element.static = serializedVariable.isStatic; |
| 1071 holder.addField(element); | 1078 holder.addField(element); |
| 1072 buildImplicitAccessors(element, holder); | 1079 buildImplicitAccessors(element, holder); |
| 1073 fieldTypes[element.name] = element.type; | 1080 fields[element.name] = element; |
| 1074 } | 1081 } |
| 1075 } | 1082 } |
| 1076 | 1083 |
| 1077 /** | 1084 /** |
| 1078 * Handle the parts that are common to top level variables and fields. | 1085 * Handle the parts that are common to top level variables and fields. |
| 1079 */ | 1086 */ |
| 1080 void buildVariableCommonParts(PropertyInducingElementImpl element, | 1087 void buildVariableCommonParts(PropertyInducingElementImpl element, |
| 1081 UnlinkedVariable serializedVariable) { | 1088 UnlinkedVariable serializedVariable) { |
| 1082 element.type = buildType(serializedVariable.type); | 1089 element.type = buildType(serializedVariable.type); |
| 1083 element.const3 = serializedVariable.isConst; | 1090 element.const3 = serializedVariable.isConst; |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1165 } | 1172 } |
| 1166 for (PropertyAccessorElementImpl accessor in unit.accessors) { | 1173 for (PropertyAccessorElementImpl accessor in unit.accessors) { |
| 1167 elementMap[accessor.identifier] = accessor; | 1174 elementMap[accessor.identifier] = accessor; |
| 1168 } | 1175 } |
| 1169 resummarizedElements[absoluteUri] = elementMap; | 1176 resummarizedElements[absoluteUri] = elementMap; |
| 1170 unitHolder = null; | 1177 unitHolder = null; |
| 1171 linkedUnit = null; | 1178 linkedUnit = null; |
| 1172 unlinkedUnit = null; | 1179 unlinkedUnit = null; |
| 1173 } | 1180 } |
| 1174 } | 1181 } |
| OLD | NEW |