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

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

Issue 2017173002: Resynthesize local variables lazily. (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: Created 4 years, 6 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 1527 matching lines...) Expand 10 before | Expand all | Expand 10 after
1538 List<FunctionElementImpl> buildTopLevelFunctions() { 1538 List<FunctionElementImpl> buildTopLevelFunctions() {
1539 return _unitResynthesizer.buildTopLevelFunctions(); 1539 return _unitResynthesizer.buildTopLevelFunctions();
1540 } 1540 }
1541 1541
1542 @override 1542 @override
1543 UnitExplicitTopLevelVariables buildTopLevelVariables() { 1543 UnitExplicitTopLevelVariables buildTopLevelVariables() {
1544 return _unitResynthesizer.buildUnitExplicitTopLevelVariables(); 1544 return _unitResynthesizer.buildUnitExplicitTopLevelVariables();
1545 } 1545 }
1546 1546
1547 @override 1547 @override
1548 FunctionElementImpl buildVariableInitializer(
1549 VariableElementImpl variable, UnlinkedExecutable serializedInitializer) {
1550 return _unitResynthesizer.buildVariableInitializer(
1551 variable, serializedInitializer);
1552 }
1553
1554 @override
1548 DartType resolveLinkedType( 1555 DartType resolveLinkedType(
1549 int slot, TypeParameterizedElementMixin typeParameterContext) { 1556 int slot, TypeParameterizedElementMixin typeParameterContext) {
1550 return _unitResynthesizer.buildLinkedType(slot, typeParameterContext); 1557 return _unitResynthesizer.buildLinkedType(slot, typeParameterContext);
1551 } 1558 }
1552 1559
1553 @override 1560 @override
1554 DartType resolveTypeRef( 1561 DartType resolveTypeRef(
1555 EntityRef type, TypeParameterizedElementMixin typeParameterContext, 1562 EntityRef type, TypeParameterizedElementMixin typeParameterContext,
1556 {bool defaultVoid: false, bool instantiateToBoundsAllowed: true}) { 1563 {bool defaultVoid: false, bool instantiateToBoundsAllowed: true}) {
1557 return _unitResynthesizer.buildType(type, typeParameterContext, 1564 return _unitResynthesizer.buildType(type, typeParameterContext,
(...skipping 559 matching lines...) Expand 10 before | Expand all | Expand 10 after
2117 List<UnlinkedLabel> unlinkedLabels = serializedExecutable.localLabels; 2124 List<UnlinkedLabel> unlinkedLabels = serializedExecutable.localLabels;
2118 int length = unlinkedLabels.length; 2125 int length = unlinkedLabels.length;
2119 if (length != 0) { 2126 if (length != 0) {
2120 List<LabelElementImpl> localLabels = new List<LabelElementImpl>(length); 2127 List<LabelElementImpl> localLabels = new List<LabelElementImpl>(length);
2121 for (int i = 0; i < length; i++) { 2128 for (int i = 0; i < length; i++) {
2122 localLabels[i] = buildLocalLabel(unlinkedLabels[i]); 2129 localLabels[i] = buildLocalLabel(unlinkedLabels[i]);
2123 } 2130 }
2124 executableElement.labels = localLabels; 2131 executableElement.labels = localLabels;
2125 } 2132 }
2126 } 2133 }
2127 {
2128 List<UnlinkedVariable> unlinkedVariables =
2129 serializedExecutable.localVariables;
2130 int length = unlinkedVariables.length;
2131 if (length != 0) {
2132 List<LocalVariableElementImpl> localVariables =
2133 new List<LocalVariableElementImpl>(length);
2134 for (int i = 0; i < length; i++) {
2135 localVariables[i] =
2136 buildLocalVariable(unlinkedVariables[i], executableElement);
2137 }
2138 executableElement.localVariables = localVariables;
2139 }
2140 }
2141 currentTypeParameters.removeLast(); 2134 currentTypeParameters.removeLast();
2142 } 2135 }
2143 2136
2144 /** 2137 /**
2145 * Build the implicit getter and setter associated with [element], and place 2138 * Build the implicit getter and setter associated with [element], and place
2146 * them in [holder]. 2139 * them in [holder].
2147 */ 2140 */
2148 void buildImplicitAccessors( 2141 void buildImplicitAccessors(
2149 PropertyInducingElementImpl element, ElementHolder holder) { 2142 PropertyInducingElementImpl element, ElementHolder holder) {
2150 PropertyAccessorElementImpl getter = buildImplicitGetter(element); 2143 PropertyAccessorElementImpl getter = buildImplicitGetter(element);
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after
2239 */ 2232 */
2240 LabelElement buildLocalLabel(UnlinkedLabel serializedLabel) { 2233 LabelElement buildLocalLabel(UnlinkedLabel serializedLabel) {
2241 return new LabelElementImpl( 2234 return new LabelElementImpl(
2242 serializedLabel.name, 2235 serializedLabel.name,
2243 serializedLabel.nameOffset, 2236 serializedLabel.nameOffset,
2244 serializedLabel.isOnSwitchStatement, 2237 serializedLabel.isOnSwitchStatement,
2245 serializedLabel.isOnSwitchMember); 2238 serializedLabel.isOnSwitchMember);
2246 } 2239 }
2247 2240
2248 /** 2241 /**
2249 * Resynthesize a [LocalVariableElement].
2250 */
2251 LocalVariableElement buildLocalVariable(UnlinkedVariable serializedVariable,
2252 ExecutableElementImpl enclosingExecutable) {
2253 LocalVariableElementImpl element;
2254 if (serializedVariable.initializer?.bodyExpr != null &&
2255 serializedVariable.isConst) {
2256 ConstLocalVariableElementImpl constElement =
2257 new ConstLocalVariableElementImpl.forSerialized(
2258 serializedVariable, enclosingExecutable);
2259 element = constElement;
2260 constElement.constantInitializer = _buildConstExpression(
2261 enclosingExecutable, serializedVariable.initializer.bodyExpr);
2262 } else {
2263 element = new LocalVariableElementImpl.forSerialized(
2264 serializedVariable, enclosingExecutable);
2265 }
2266 if (serializedVariable.visibleOffset != 0) {
2267 element.setVisibleRange(
2268 serializedVariable.visibleOffset, serializedVariable.visibleLength);
2269 }
2270 buildVariableCommonParts(element, serializedVariable);
2271 return element;
2272 }
2273
2274 /**
2275 * Resynthesize a [ParameterElement]. 2242 * Resynthesize a [ParameterElement].
2276 */ 2243 */
2277 ParameterElement buildParameter( 2244 ParameterElement buildParameter(
2278 UnlinkedParam serializedParameter, ElementImpl enclosingElement, 2245 UnlinkedParam serializedParameter, ElementImpl enclosingElement,
2279 {bool synthetic: false}) { 2246 {bool synthetic: false}) {
2280 ParameterElementImpl parameterElement; 2247 ParameterElementImpl parameterElement;
2281 if (serializedParameter.isInitializingFormal) { 2248 if (serializedParameter.isInitializingFormal) {
2282 if (serializedParameter.kind == UnlinkedParamKind.required) { 2249 if (serializedParameter.kind == UnlinkedParamKind.required) {
2283 parameterElement = new FieldFormalParameterElementImpl.forSerialized( 2250 parameterElement = new FieldFormalParameterElementImpl.forSerialized(
2284 serializedParameter, enclosingElement); 2251 serializedParameter, enclosingElement);
(...skipping 28 matching lines...) Expand all
2313 } else { 2280 } else {
2314 parameterElement.parameters = subParameters; 2281 parameterElement.parameters = subParameters;
2315 parameterTypeElement.shareParameters(subParameters); 2282 parameterTypeElement.shareParameters(subParameters);
2316 } 2283 }
2317 parameterTypeElement.returnType = 2284 parameterTypeElement.returnType =
2318 buildType(serializedParameter.type, _currentTypeParameterizedElement); 2285 buildType(serializedParameter.type, _currentTypeParameterizedElement);
2319 parameterElement.type = new FunctionTypeImpl.elementWithNameAndArgs( 2286 parameterElement.type = new FunctionTypeImpl.elementWithNameAndArgs(
2320 parameterTypeElement, null, getCurrentTypeArguments(), false); 2287 parameterTypeElement, null, getCurrentTypeArguments(), false);
2321 parameterTypeElement.type = parameterElement.type; 2288 parameterTypeElement.type = parameterElement.type;
2322 } 2289 }
2323 buildVariableInitializer(parameterElement, serializedParameter.initializer);
2324 return parameterElement; 2290 return parameterElement;
2325 } 2291 }
2326 2292
2327 List<FunctionElementImpl> buildTopLevelFunctions() { 2293 List<FunctionElementImpl> buildTopLevelFunctions() {
2328 List<FunctionElementImpl> functions = <FunctionElementImpl>[]; 2294 List<FunctionElementImpl> functions = <FunctionElementImpl>[];
2329 List<UnlinkedExecutable> executables = unlinkedUnit.executables; 2295 List<UnlinkedExecutable> executables = unlinkedUnit.executables;
2330 for (UnlinkedExecutable unlinkedExecutable in executables) { 2296 for (UnlinkedExecutable unlinkedExecutable in executables) {
2331 if (unlinkedExecutable.kind == UnlinkedExecutableKind.functionOrMethod) { 2297 if (unlinkedExecutable.kind == UnlinkedExecutableKind.functionOrMethod) {
2332 FunctionElementImpl function = 2298 FunctionElementImpl function =
2333 new FunctionElementImpl.forSerialized(unlinkedExecutable, unit); 2299 new FunctionElementImpl.forSerialized(unlinkedExecutable, unit);
(...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after
2508 ConstTopLevelVariableElementImpl constElement = 2474 ConstTopLevelVariableElementImpl constElement =
2509 new ConstTopLevelVariableElementImpl.forSerialized( 2475 new ConstTopLevelVariableElementImpl.forSerialized(
2510 unlinkedVariable, unit); 2476 unlinkedVariable, unit);
2511 element = constElement; 2477 element = constElement;
2512 constElement.constantInitializer = 2478 constElement.constantInitializer =
2513 _buildConstExpression(null, unlinkedVariable.initializer.bodyExpr); 2479 _buildConstExpression(null, unlinkedVariable.initializer.bodyExpr);
2514 } else { 2480 } else {
2515 element = new TopLevelVariableElementImpl.forSerialized( 2481 element = new TopLevelVariableElementImpl.forSerialized(
2516 unlinkedVariable, unit); 2482 unlinkedVariable, unit);
2517 } 2483 }
2518 buildVariableCommonParts(element, unlinkedVariable);
2519 variablesData.variables[i] = element; 2484 variablesData.variables[i] = element;
2520 // implicit accessors 2485 // implicit accessors
2521 variablesData.implicitAccessors.add(buildImplicitGetter(element)); 2486 variablesData.implicitAccessors.add(buildImplicitGetter(element));
2522 if (!(element.isConst || element.isFinal)) { 2487 if (!(element.isConst || element.isFinal)) {
2523 variablesData.implicitAccessors.add(buildImplicitSetter(element)); 2488 variablesData.implicitAccessors.add(buildImplicitSetter(element));
2524 } 2489 }
2525 } 2490 }
2526 return variablesData; 2491 return variablesData;
2527 } 2492 }
2528 2493
2529 /** 2494 /**
2530 * Resynthesize a [TopLevelVariableElement] or [FieldElement]. 2495 * Resynthesize a [TopLevelVariableElement] or [FieldElement].
2531 */ 2496 */
2532 void buildVariable( 2497 void buildVariable(
2533 ClassElementImpl enclosingClass, UnlinkedVariable serializedVariable, 2498 ClassElementImpl enclosingClass, UnlinkedVariable serializedVariable,
2534 [ElementHolder holder]) { 2499 [ElementHolder holder]) {
2535 if (holder == null) { 2500 if (holder == null) {
2536 throw new UnimplementedError('Must be lazy'); 2501 throw new UnimplementedError('Must be lazy');
2537 } else { 2502 } else {
2538 FieldElementImpl element; 2503 FieldElementImpl element;
2539 if (serializedVariable.initializer?.bodyExpr != null && 2504 if (serializedVariable.initializer?.bodyExpr != null &&
2540 (serializedVariable.isConst || 2505 (serializedVariable.isConst ||
2541 serializedVariable.isFinal && !serializedVariable.isStatic)) { 2506 serializedVariable.isFinal && !serializedVariable.isStatic)) {
2542 element = new ConstFieldElementImpl.forSerialized( 2507 element = new ConstFieldElementImpl.forSerialized(
2543 serializedVariable, enclosingClass); 2508 serializedVariable, enclosingClass);
2544 } else { 2509 } else {
2545 element = new FieldElementImpl.forSerialized( 2510 element = new FieldElementImpl.forSerialized(
2546 serializedVariable, enclosingClass); 2511 serializedVariable, enclosingClass);
2547 } 2512 }
2548 buildVariableCommonParts(element, serializedVariable);
2549 element.static = serializedVariable.isStatic; 2513 element.static = serializedVariable.isStatic;
2550 holder.addField(element); 2514 holder.addField(element);
2551 buildImplicitAccessors(element, holder); 2515 buildImplicitAccessors(element, holder);
2552 fields[element.name] = element; 2516 fields[element.name] = element;
2553 } 2517 }
2554 } 2518 }
2555 2519
2556 /** 2520 /**
2557 * Handle the parts that are common to variables. 2521 * If the given [serializedInitializer] is not `null`, return the
2522 * corresponding [FunctionElementImpl], otherwise return `null`.
2558 */ 2523 */
2559 void buildVariableCommonParts( 2524 FunctionElementImpl buildVariableInitializer(
2560 VariableElementImpl element, UnlinkedVariable serializedVariable) {
2561 buildVariableInitializer(element, serializedVariable.initializer);
2562 }
2563
2564 /**
2565 * If the given [serializedInitializer] is not `null`, create the
2566 * corresponding [FunctionElementImpl] and set it for the [variable].
2567 */
2568 void buildVariableInitializer(
2569 VariableElementImpl variable, UnlinkedExecutable serializedInitializer) { 2525 VariableElementImpl variable, UnlinkedExecutable serializedInitializer) {
2570 if (serializedInitializer == null) { 2526 if (serializedInitializer == null) {
2571 return null; 2527 return null;
2572 } 2528 }
2573 FunctionElementImpl initializerElement = 2529 FunctionElementImpl initializerElement =
2574 buildLocalFunction(serializedInitializer, variable); 2530 buildLocalFunction(serializedInitializer, variable);
2575 initializerElement.synthetic = true; 2531 initializerElement.synthetic = true;
2576 variable.initializer = initializerElement; 2532 return initializerElement;
2577 } 2533 }
2578 2534
2579 /** 2535 /**
2580 * Finish creating a [TypeParameterElement] by deserializing its bound. 2536 * Finish creating a [TypeParameterElement] by deserializing its bound.
2581 */ 2537 */
2582 void finishTypeParameter(UnlinkedTypeParam serializedTypeParameter, 2538 void finishTypeParameter(UnlinkedTypeParam serializedTypeParameter,
2583 TypeParameterElementImpl typeParameterElement) { 2539 TypeParameterElementImpl typeParameterElement) {
2584 if (serializedTypeParameter.bound != null) { 2540 if (serializedTypeParameter.bound != null) {
2585 typeParameterElement.bound = buildType( 2541 typeParameterElement.bound = buildType(
2586 serializedTypeParameter.bound, _currentTypeParameterizedElement, 2542 serializedTypeParameter.bound, _currentTypeParameterizedElement,
(...skipping 255 matching lines...) Expand 10 before | Expand all | Expand 10 after
2842 static String _getElementIdentifier(String name, ReferenceKind kind) { 2798 static String _getElementIdentifier(String name, ReferenceKind kind) {
2843 if (kind == ReferenceKind.topLevelPropertyAccessor || 2799 if (kind == ReferenceKind.topLevelPropertyAccessor ||
2844 kind == ReferenceKind.propertyAccessor) { 2800 kind == ReferenceKind.propertyAccessor) {
2845 if (!name.endsWith('=')) { 2801 if (!name.endsWith('=')) {
2846 return name + '?'; 2802 return name + '?';
2847 } 2803 }
2848 } 2804 }
2849 return name; 2805 return name;
2850 } 2806 }
2851 } 2807 }
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