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

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

Issue 1707073002: Serialize and resynthesize variable initializers. (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: Created 4 years, 10 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 serialization.elements; 5 library serialization.elements;
6 6
7 import 'package:analyzer/dart/element/element.dart'; 7 import 'package:analyzer/dart/element/element.dart';
8 import 'package:analyzer/dart/element/type.dart'; 8 import 'package:analyzer/dart/element/type.dart';
9 import 'package:analyzer/src/dart/element/element.dart'; 9 import 'package:analyzer/src/dart/element/element.dart';
10 import 'package:analyzer/src/dart/element/member.dart'; 10 import 'package:analyzer/src/dart/element/member.dart';
(...skipping 20 matching lines...) Expand all
31 } 31 }
32 32
33 ReferenceKind _getReferenceKind(Element element) { 33 ReferenceKind _getReferenceKind(Element element) {
34 if (element == null || 34 if (element == null ||
35 element is ClassElement || 35 element is ClassElement ||
36 element is DynamicElementImpl) { 36 element is DynamicElementImpl) {
37 return ReferenceKind.classOrEnum; 37 return ReferenceKind.classOrEnum;
38 } else if (element is ConstructorElement) { 38 } else if (element is ConstructorElement) {
39 return ReferenceKind.constructor; 39 return ReferenceKind.constructor;
40 } else if (element is FunctionElement) { 40 } else if (element is FunctionElement) {
41 return ReferenceKind.topLevelFunction; 41 if (element.enclosingElement is CompilationUnitElement) {
42 return ReferenceKind.topLevelFunction;
43 }
44 return ReferenceKind.function;
42 } else if (element is FunctionTypeAliasElement) { 45 } else if (element is FunctionTypeAliasElement) {
43 return ReferenceKind.typedef; 46 return ReferenceKind.typedef;
44 } else if (element is PropertyAccessorElement) { 47 } else if (element is PropertyAccessorElement) {
45 if (element.enclosingElement is ClassElement) { 48 if (element.enclosingElement is ClassElement) {
46 return ReferenceKind.propertyAccessor; 49 return ReferenceKind.propertyAccessor;
47 } 50 }
48 return ReferenceKind.topLevelPropertyAccessor; 51 return ReferenceKind.topLevelPropertyAccessor;
49 } else if (element is MethodElement) { 52 } else if (element is MethodElement) {
50 return ReferenceKind.method; 53 return ReferenceKind.method;
51 } else { 54 } else {
(...skipping 460 matching lines...) Expand 10 before | Expand all | Expand 10 after
512 } 515 }
513 516
514 /** 517 /**
515 * Serialize the given [executableElement], creating an [UnlinkedExecutable]. 518 * Serialize the given [executableElement], creating an [UnlinkedExecutable].
516 */ 519 */
517 UnlinkedExecutableBuilder serializeExecutable( 520 UnlinkedExecutableBuilder serializeExecutable(
518 ExecutableElement executableElement) { 521 ExecutableElement executableElement) {
519 UnlinkedExecutableBuilder b = new UnlinkedExecutableBuilder(); 522 UnlinkedExecutableBuilder b = new UnlinkedExecutableBuilder();
520 b.name = executableElement.name; 523 b.name = executableElement.name;
521 b.nameOffset = executableElement.nameOffset; 524 b.nameOffset = executableElement.nameOffset;
522 if (executableElement is! ConstructorElement) { 525 if (executableElement.enclosingElement is VariableElement) {
526 // TODO(scheglov) remove this check and serialize initializer types
527 // Note that for code like `var v = null` w need to support Bottom.
528 } else if (executableElement is! ConstructorElement) {
523 if (!executableElement.hasImplicitReturnType) { 529 if (!executableElement.hasImplicitReturnType) {
524 b.returnType = serializeTypeRef( 530 b.returnType = serializeTypeRef(
525 executableElement.type.returnType, executableElement); 531 executableElement.type.returnType, executableElement);
526 } else if (!executableElement.isStatic) { 532 } else if (!executableElement.isStatic) {
527 b.inferredReturnTypeSlot = 533 b.inferredReturnTypeSlot =
528 storeInferredType(executableElement.returnType, executableElement); 534 storeInferredType(executableElement.returnType, executableElement);
529 } 535 }
530 } 536 }
531 b.typeParameters = 537 b.typeParameters =
532 executableElement.typeParameters.map(serializeTypeParam).toList(); 538 executableElement.typeParameters.map(serializeTypeParam).toList();
(...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after
693 b.type = serializeTypeRef(type, context); 699 b.type = serializeTypeRef(type, context);
694 } 700 }
695 } 701 }
696 if (parameter is ConstVariableElement) { 702 if (parameter is ConstVariableElement) {
697 ConstVariableElement constParameter = parameter as ConstVariableElement; 703 ConstVariableElement constParameter = parameter as ConstVariableElement;
698 Expression initializer = constParameter.constantInitializer; 704 Expression initializer = constParameter.constantInitializer;
699 if (initializer != null) { 705 if (initializer != null) {
700 b.defaultValue = serializeConstExpr(initializer); 706 b.defaultValue = serializeConstExpr(initializer);
701 } 707 }
702 } 708 }
709 if (parameter is! Member && parameter.initializer != null) {
Paul Berry 2016/02/17 21:15:30 Can you include a comment explaining why we want t
scheglov 2016/02/17 21:27:51 Done.
710 b.initializer = serializeExecutable(parameter.initializer);
711 }
703 { 712 {
704 SourceRange visibleRange = parameter.visibleRange; 713 SourceRange visibleRange = parameter.visibleRange;
705 if (visibleRange != null) { 714 if (visibleRange != null) {
706 b.visibleOffset = visibleRange.offset; 715 b.visibleOffset = visibleRange.offset;
707 b.visibleLength = visibleRange.length; 716 b.visibleLength = visibleRange.length;
708 } 717 }
709 } 718 }
710 return b; 719 return b;
711 } 720 }
712 721
(...skipping 193 matching lines...) Expand 10 before | Expand all | Expand 10 after
906 (variable.initializer != null || !variable.isStatic)) { 915 (variable.initializer != null || !variable.isStatic)) {
907 b.inferredTypeSlot = storeInferredType(variable.type, variable); 916 b.inferredTypeSlot = storeInferredType(variable.type, variable);
908 } 917 }
909 if (variable is LocalVariableElement) { 918 if (variable is LocalVariableElement) {
910 SourceRange visibleRange = variable.visibleRange; 919 SourceRange visibleRange = variable.visibleRange;
911 if (visibleRange != null) { 920 if (visibleRange != null) {
912 b.visibleOffset = visibleRange.offset; 921 b.visibleOffset = visibleRange.offset;
913 b.visibleLength = visibleRange.length; 922 b.visibleLength = visibleRange.length;
914 } 923 }
915 } 924 }
925 if (variable.initializer != null) {
926 // TODO(scheglov) local functions and variables
927 b.initializer = serializeExecutable(variable.initializer);
928 // b.initializer = new UnlinkedExecutableBuilder(
Paul Berry 2016/02/17 21:15:30 Was this commented out code left here unintentiona
scheglov 2016/02/17 21:27:51 This code should be removed. Thanks.
929 // nameOffset: variable.initializer.nameOffset);
930 }
916 return b; 931 return b;
917 } 932 }
918 933
919 /** 934 /**
920 * Create a slot id for the given [type] (which is an inferred type). If 935 * Create a slot id for the given [type] (which is an inferred type). If
921 * strong mode is enabled and [type] is not `dynamic`, it is stored in 936 * strong mode is enabled and [type] is not `dynamic`, it is stored in
922 * [linkedTypes] so that once the compilation unit has been fully visited, it 937 * [linkedTypes] so that once the compilation unit has been fully visited, it
923 * will be serialized into [LinkedUnit.types]. 938 * will be serialized into [LinkedUnit.types].
924 * 939 *
925 * [context] is the element within which the slot id will appear; this is 940 * [context] is the element within which the slot id will appear; this is
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
989 if (enclosing == null || enclosing is CompilationUnitElement) { 1004 if (enclosing == null || enclosing is CompilationUnitElement) {
990 // Figure out a prefix that may be used to refer to the given element. 1005 // Figure out a prefix that may be used to refer to the given element.
991 // TODO(paulberry): to avoid subtle relinking inconsistencies we 1006 // TODO(paulberry): to avoid subtle relinking inconsistencies we
992 // should use the actual prefix from the AST (a given type may be 1007 // should use the actual prefix from the AST (a given type may be
993 // reachable via multiple prefixes), but sadly, this information is 1008 // reachable via multiple prefixes), but sadly, this information is
994 // not recorded in the element model. 1009 // not recorded in the element model.
995 PrefixElement prefix = librarySerializer.prefixMap[element]; 1010 PrefixElement prefix = librarySerializer.prefixMap[element];
996 if (prefix != null) { 1011 if (prefix != null) {
997 prefixReference = serializePrefix(prefix); 1012 prefixReference = serializePrefix(prefix);
998 } 1013 }
1014 } else if (element.isSynthetic &&
Paul Berry 2016/02/17 21:15:30 It seems like this code shouldn't be reachable.
scheglov 2016/02/17 21:27:51 You're right. I don't remember why I added it, but
1015 element is FunctionElement &&
1016 element.enclosingElement is VariableElement) {
1017 prefixReference = 0;
999 } else { 1018 } else {
1000 prefixReference = _getElementReferenceId(enclosing, linked: linked); 1019 prefixReference = _getElementReferenceId(enclosing, linked: linked);
1001 } 1020 }
1002 index = serializeUnlinkedReference(name, kind, 1021 index = serializeUnlinkedReference(name, kind,
1003 prefixReference: prefixReference, unit: unit); 1022 prefixReference: prefixReference, unit: unit);
1004 linkedReference = linkedReferences[index]; 1023 linkedReference = linkedReferences[index];
1005 } 1024 }
1006 linkedReference.dependency = 1025 linkedReference.dependency =
1007 librarySerializer.serializeDependency(dependentLibrary); 1026 librarySerializer.serializeDependency(dependentLibrary);
1008 if (element is TypeParameterizedElement) { 1027 if (element is TypeParameterizedElement) {
(...skipping 340 matching lines...) Expand 10 before | Expand all | Expand 10 after
1349 exportNames.add(new LinkedExportNameBuilder( 1368 exportNames.add(new LinkedExportNameBuilder(
1350 name: name, 1369 name: name,
1351 dependency: serializeDependency(dependentLibrary), 1370 dependency: serializeDependency(dependentLibrary),
1352 unit: unit, 1371 unit: unit,
1353 kind: kind)); 1372 kind: kind));
1354 } 1373 }
1355 pb.exportNames = exportNames; 1374 pb.exportNames = exportNames;
1356 return pb; 1375 return pb;
1357 } 1376 }
1358 } 1377 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698