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

Side by Side Diff: pkg/analyzer/test/src/summary/summary_common.dart

Issue 1856883002: Serialize assignment expressions. (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: Created 4 years, 8 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
« no previous file with comments | « pkg/analyzer/lib/src/summary/summarize_const_expr.dart ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 analyzer.test.src.summary.summary_common; 5 library analyzer.test.src.summary.summary_common;
6 6
7 import 'package:analyzer/analyzer.dart'; 7 import 'package:analyzer/analyzer.dart';
8 import 'package:analyzer/dart/ast/ast.dart'; 8 import 'package:analyzer/dart/ast/ast.dart';
9 import 'package:analyzer/dart/element/element.dart'; 9 import 'package:analyzer/dart/element/element.dart';
10 import 'package:analyzer/src/dart/scanner/reader.dart'; 10 import 'package:analyzer/src/dart/scanner/reader.dart';
(...skipping 5844 matching lines...) Expand 10 before | Expand all | Expand 10 after
5855 expect(getter, isNotNull); 5855 expect(getter, isNotNull);
5856 checkExportName(getter, absUri('/a.dart'), 'a.dart', 'v', 5856 checkExportName(getter, absUri('/a.dart'), 'a.dart', 'v',
5857 ReferenceKind.topLevelPropertyAccessor); 5857 ReferenceKind.topLevelPropertyAccessor);
5858 LinkedExportName setter = 5858 LinkedExportName setter =
5859 linked.exportNames.firstWhere((e) => e.name == 'v='); 5859 linked.exportNames.firstWhere((e) => e.name == 'v=');
5860 expect(setter, isNotNull); 5860 expect(setter, isNotNull);
5861 checkExportName(setter, absUri('/a.dart'), 'a.dart', 'v=', 5861 checkExportName(setter, absUri('/a.dart'), 'a.dart', 'v=',
5862 ReferenceKind.topLevelPropertyAccessor); 5862 ReferenceKind.topLevelPropertyAccessor);
5863 } 5863 }
5864 5864
5865 test_expr_assignOperator_assign() {
5866 _assertAssignmentOperator(
5867 '(a = 1 + 2) + 3', UnlinkedExprAssignOperator.assign);
5868 }
5869
5870 test_expr_assignOperator_bitAnd() {
5871 _assertAssignmentOperator(
5872 '(a &= 1 + 2) + 3', UnlinkedExprAssignOperator.bitAnd);
5873 }
5874
5875 test_expr_assignOperator_bitOr() {
5876 _assertAssignmentOperator(
5877 '(a |= 1 + 2) + 3', UnlinkedExprAssignOperator.bitOr);
5878 }
5879
5880 test_expr_assignOperator_bitXor() {
5881 _assertAssignmentOperator(
5882 '(a ^= 1 + 2) + 3', UnlinkedExprAssignOperator.bitXor);
5883 }
5884
5885 test_expr_assignOperator_divide() {
5886 _assertAssignmentOperator(
5887 '(a /= 1 + 2) + 3', UnlinkedExprAssignOperator.divide);
5888 }
5889
5890 test_expr_assignOperator_floorDivide() {
5891 _assertAssignmentOperator(
5892 '(a ~/= 1 + 2) + 3', UnlinkedExprAssignOperator.floorDivide);
5893 }
5894
5895 test_expr_assignOperator_ifNull() {
5896 _assertAssignmentOperator(
5897 '(a ??= 1 + 2) + 3', UnlinkedExprAssignOperator.ifNull);
5898 }
5899
5900 test_expr_assignOperator_minus() {
5901 _assertAssignmentOperator(
5902 '(a -= 1 + 2) + 3', UnlinkedExprAssignOperator.minus);
5903 }
5904
5905 test_expr_assignOperator_modulo() {
5906 _assertAssignmentOperator(
5907 '(a %= 1 + 2) + 3', UnlinkedExprAssignOperator.modulo);
5908 }
5909
5910 test_expr_assignOperator_multiply() {
5911 _assertAssignmentOperator(
5912 '(a *= 1 + 2) + 3', UnlinkedExprAssignOperator.multiply);
5913 }
5914
5915 test_expr_assignOperator_plus() {
5916 _assertAssignmentOperator(
5917 '(a += 1 + 2) + 3', UnlinkedExprAssignOperator.plus);
5918 }
5919
5920 test_expr_assignOperator_shiftLeft() {
5921 _assertAssignmentOperator(
5922 '(a <<= 1 + 2) + 3', UnlinkedExprAssignOperator.shiftLeft);
5923 }
5924
5925 test_expr_assignOperator_shiftRight() {
5926 _assertAssignmentOperator(
5927 '(a >>= 1 + 2) + 3', UnlinkedExprAssignOperator.shiftRight);
5928 }
5929
5930 test_expr_assignToIndex_ofDoubleIndex() {
5931 // TODO(scheglov) add `[]` read serialization
5932 // if (skipNonConstInitializers) {
5933 // return;
5934 // }
5935 // UnlinkedVariable variable = serializeVariableText('''
5936 //class A {
5937 // List<B> b;
5938 //}
5939 //class B {
5940 // List<C> c;
5941 //}
5942 //class C {
5943 // List<int> f = <int>[0, 1, 2];
5944 //}
5945 //A a = new A();
5946 //final v = (a.b[1].c[2].f[3] = 5);
5947 //''');
5948 // _assertUnlinkedConst(variable.constExpr, operators: [
5949 // UnlinkedConstOperation.pushReference,
5950 // UnlinkedConstOperation.pushInt,
5951 // UnlinkedConstOperation.pushInt,
5952 // UnlinkedConstOperation.assignToIndex,
5953 // ], assignmentOperators: [
5954 // (UnlinkedExprAssignOperator.assign)
5955 // ], ints: [
5956 // 1, 2, 3,
5957 // 5
5958 // ], strings: [], referenceValidators: [
5959 // (EntityRef r) => checkTypeRef(r, null, null, 'f',
5960 // expectedKind: ReferenceKind.unresolved,
5961 // prefixExpectations: [
5962 // new _PrefixExpectation(ReferenceKind.unresolved, 'c'),
5963 // new _PrefixExpectation(ReferenceKind.unresolved, 'b'),
5964 // new _PrefixExpectation(
5965 // ReferenceKind.topLevelPropertyAccessor, 'a')
5966 // ])
5967 // ]);
5968 }
5969
5970 test_expr_assignToIndex_ofFieldSequence() {
5971 if (skipNonConstInitializers) {
5972 return;
5973 }
5974 UnlinkedVariable variable = serializeVariableText('''
5975 class A {
5976 B b;
5977 }
5978 class B {
5979 C c;
5980 }
5981 class C {
5982 List<int> f = <int>[0, 1, 2];
5983 }
5984 A a = new A();
5985 final v = (a.b.c.f[1] = 5);
5986 ''');
5987 _assertUnlinkedConst(variable.constExpr, operators: [
5988 UnlinkedConstOperation.pushReference,
5989 UnlinkedConstOperation.pushInt,
5990 UnlinkedConstOperation.pushInt,
5991 UnlinkedConstOperation.assignToIndex,
5992 ], assignmentOperators: [
5993 (UnlinkedExprAssignOperator.assign)
5994 ], ints: [
5995 1,
5996 5
5997 ], strings: [], referenceValidators: [
5998 (EntityRef r) => checkTypeRef(r, null, null, 'f',
5999 expectedKind: ReferenceKind.unresolved,
6000 prefixExpectations: [
6001 new _PrefixExpectation(ReferenceKind.unresolved, 'c'),
6002 new _PrefixExpectation(ReferenceKind.unresolved, 'b'),
6003 new _PrefixExpectation(
6004 ReferenceKind.topLevelPropertyAccessor, 'a')
6005 ])
6006 ]);
6007 }
6008
6009 test_expr_assignToIndex_ofTopLevelVariable() {
6010 if (skipNonConstInitializers) {
6011 return;
6012 }
6013 UnlinkedVariable variable = serializeVariableText('''
6014 List<int> a = <int>[0, 1, 2];
6015 final v = (a[1] = 5);
6016 ''');
6017 _assertUnlinkedConst(variable.constExpr, operators: [
6018 UnlinkedConstOperation.pushReference,
6019 UnlinkedConstOperation.pushInt,
6020 UnlinkedConstOperation.pushInt,
6021 UnlinkedConstOperation.assignToIndex,
6022 ], assignmentOperators: [
6023 (UnlinkedExprAssignOperator.assign)
6024 ], ints: [
6025 1,
6026 5
6027 ], strings: [], referenceValidators: [
6028 (EntityRef r) => checkTypeRef(r, null, null, 'a',
6029 expectedKind: ReferenceKind.topLevelPropertyAccessor)
6030 ]);
6031 }
6032
6033 test_expr_assignToProperty_ofInstanceCreation() {
6034 if (skipNonConstInitializers) {
6035 return;
6036 }
6037 UnlinkedVariable variable = serializeVariableText('''
6038 class C {
6039 int f;
6040 }
6041 final v = (new C().f = 5);
6042 ''');
6043 _assertUnlinkedConst(variable.constExpr, operators: [
6044 UnlinkedConstOperation.invokeConstructor,
6045 UnlinkedConstOperation.pushInt,
6046 UnlinkedConstOperation.assignToProperty,
6047 ], assignmentOperators: [
6048 (UnlinkedExprAssignOperator.assign)
6049 ], ints: [
6050 0,
6051 0,
6052 5
6053 ], strings: [
6054 'f'
6055 ], referenceValidators: [
6056 (EntityRef r) => checkTypeRef(r, null, null, 'C',
6057 expectedKind: ReferenceKind.classOrEnum)
6058 ]);
6059 }
6060
6061 test_expr_assignToRef_classStaticField() {
6062 if (skipNonConstInitializers) {
6063 return;
6064 }
6065 UnlinkedVariable variable = serializeVariableText('''
6066 class C {
6067 static int f;
6068 }
6069 final v = (C.f = 1);
6070 ''');
6071 _assertUnlinkedConst(variable.constExpr, operators: [
6072 UnlinkedConstOperation.pushInt,
6073 UnlinkedConstOperation.assignToRef,
6074 ], assignmentOperators: [
6075 (UnlinkedExprAssignOperator.assign)
6076 ], ints: [
6077 1,
6078 ], strings: [], referenceValidators: [
6079 (EntityRef r) => checkTypeRef(r, null, null, 'f',
6080 expectedKind: ReferenceKind.unresolved,
6081 prefixExpectations: [
6082 new _PrefixExpectation(ReferenceKind.classOrEnum, 'C')
6083 ])
6084 ]);
6085 }
6086
6087 test_expr_assignToRef_fieldSequence() {
6088 if (skipNonConstInitializers) {
6089 return;
6090 }
6091 UnlinkedVariable variable = serializeVariableText('''
6092 class A {
6093 B b;
6094 }
6095 class B {
6096 C c;
6097 }
6098 class C {
6099 int f;
6100 }
6101 A a = new A();
6102 final v = (a.b.c.f = 1);
6103 ''');
6104 _assertUnlinkedConst(variable.constExpr, operators: [
6105 UnlinkedConstOperation.pushInt,
6106 UnlinkedConstOperation.assignToRef,
6107 ], assignmentOperators: [
6108 (UnlinkedExprAssignOperator.assign)
6109 ], ints: [
6110 1,
6111 ], strings: [], referenceValidators: [
6112 (EntityRef r) => checkTypeRef(r, null, null, 'f',
6113 expectedKind: ReferenceKind.unresolved,
6114 prefixExpectations: [
6115 new _PrefixExpectation(ReferenceKind.unresolved, 'c'),
6116 new _PrefixExpectation(ReferenceKind.unresolved, 'b'),
6117 new _PrefixExpectation(
6118 ReferenceKind.topLevelPropertyAccessor, 'a')
6119 ])
6120 ]);
6121 }
6122
6123 test_expr_assignToRef_topLevelVariable() {
6124 if (skipNonConstInitializers) {
6125 return;
6126 }
6127 UnlinkedVariable variable = serializeVariableText('''
6128 int a = 0;
6129 final v = (a = 1);
6130 ''');
6131 _assertUnlinkedConst(variable.constExpr, operators: [
6132 UnlinkedConstOperation.pushInt,
6133 UnlinkedConstOperation.assignToRef,
6134 ], assignmentOperators: [
6135 (UnlinkedExprAssignOperator.assign)
6136 ], ints: [
6137 1,
6138 ], strings: [], referenceValidators: [
6139 (EntityRef r) => checkTypeRef(r, null, null, 'a',
6140 expectedKind: ReferenceKind.topLevelPropertyAccessor)
6141 ]);
6142 }
6143
6144 test_expr_assignToRef_topLevelVariable_imported() {
6145 if (skipNonConstInitializers) {
6146 return;
6147 }
6148 addNamedSource(
6149 '/a.dart',
6150 '''
6151 int a = 0;
6152 ''');
6153 UnlinkedVariable variable = serializeVariableText('''
6154 import 'a.dart';
6155 final v = (a = 1);
6156 ''');
6157 _assertUnlinkedConst(variable.constExpr, operators: [
6158 UnlinkedConstOperation.pushInt,
6159 UnlinkedConstOperation.assignToRef,
6160 ], assignmentOperators: [
6161 (UnlinkedExprAssignOperator.assign)
6162 ], ints: [
6163 1,
6164 ], strings: [], referenceValidators: [
6165 (EntityRef r) => checkTypeRef(r, absUri('/a.dart'), 'a.dart', 'a',
6166 expectedKind: ReferenceKind.topLevelPropertyAccessor)
6167 ]);
6168 }
6169
6170 test_expr_assignToRef_topLevelVariable_imported_withPrefix() {
6171 if (skipNonConstInitializers) {
6172 return;
6173 }
6174 addNamedSource(
6175 '/a.dart',
6176 '''
6177 int a = 0;
6178 ''');
6179 UnlinkedVariable variable = serializeVariableText('''
6180 import 'a.dart' as p;
6181 final v = (p.a = 1);
6182 ''');
6183 _assertUnlinkedConst(variable.constExpr, operators: [
6184 UnlinkedConstOperation.pushInt,
6185 UnlinkedConstOperation.assignToRef,
6186 ], assignmentOperators: [
6187 (UnlinkedExprAssignOperator.assign)
6188 ], ints: [
6189 1,
6190 ], strings: [], referenceValidators: [
6191 (EntityRef r) {
6192 return checkTypeRef(r, absUri('/a.dart'), 'a.dart', 'a',
6193 expectedKind: ReferenceKind.topLevelPropertyAccessor,
6194 expectedPrefix: 'p');
6195 }
6196 ]);
6197 }
6198
5865 test_expr_extractProperty_ofInvokeConstructor() { 6199 test_expr_extractProperty_ofInvokeConstructor() {
5866 if (skipNonConstInitializers) { 6200 if (skipNonConstInitializers) {
5867 return; 6201 return;
5868 } 6202 }
5869 UnlinkedVariable variable = serializeVariableText(''' 6203 UnlinkedVariable variable = serializeVariableText('''
5870 class C { 6204 class C {
5871 int f = 0; 6205 int f = 0;
5872 } 6206 }
5873 final v = new C().f; 6207 final v = new C().f;
5874 '''); 6208 ''');
(...skipping 2434 matching lines...) Expand 10 before | Expand all | Expand 10 after
8309 default: 8643 default:
8310 // This reference should have a dependency of 0, since it refers to 8644 // This reference should have a dependency of 0, since it refers to
8311 // an element that is contained within some other element. 8645 // an element that is contained within some other element.
8312 expect(reference.dependency, 0, 8646 expect(reference.dependency, 0,
8313 reason: 'Nonzero dependency for ${reference.kind}'); 8647 reason: 'Nonzero dependency for ${reference.kind}');
8314 } 8648 }
8315 } 8649 }
8316 } 8650 }
8317 } 8651 }
8318 8652
8653 /**
8654 * Assert that serializing the given [expr] of form `(a op= 1 + 2) + 3`
8655 * uses the given [expectedAssignOperator].
8656 */
8657 void _assertAssignmentOperator(
8658 String expr, UnlinkedExprAssignOperator expectedAssignOperator) {
8659 if (skipNonConstInitializers) {
8660 return;
8661 }
8662 UnlinkedVariable variable = serializeVariableText('''
8663 int a = 0;
8664 final v = $expr;
8665 ''');
8666 _assertUnlinkedConst(variable.constExpr, operators: [
8667 UnlinkedConstOperation.pushInt,
8668 UnlinkedConstOperation.pushInt,
8669 UnlinkedConstOperation.add,
8670 UnlinkedConstOperation.assignToRef,
8671 UnlinkedConstOperation.pushInt,
8672 UnlinkedConstOperation.add,
8673 ], assignmentOperators: [
8674 expectedAssignOperator
8675 ], ints: [
8676 1,
8677 2,
8678 3
8679 ], strings: [], referenceValidators: [
8680 (EntityRef r) => checkTypeRef(r, null, null, 'a',
8681 expectedKind: ReferenceKind.topLevelPropertyAccessor)
8682 ]);
8683 }
8684
8319 void _assertCodeRange(CodeRange codeRange, int offset, int length) { 8685 void _assertCodeRange(CodeRange codeRange, int offset, int length) {
8320 expect(codeRange, isNotNull); 8686 expect(codeRange, isNotNull);
8321 expect(codeRange.offset, offset); 8687 expect(codeRange.offset, offset);
8322 expect(codeRange.length, length); 8688 expect(codeRange.length, length);
8323 } 8689 }
8324 8690
8325 void _assertExecutableVisible(String code, UnlinkedExecutable f, 8691 void _assertExecutableVisible(String code, UnlinkedExecutable f,
8326 String visibleBegin, String visibleEnd) { 8692 String visibleBegin, String visibleEnd) {
8327 int expectedVisibleOffset = code.indexOf(visibleBegin); 8693 int expectedVisibleOffset = code.indexOf(visibleBegin);
8328 int expectedVisibleLength = 8694 int expectedVisibleLength =
(...skipping 15 matching lines...) Expand all
8344 expect(p.visibleOffset, isZero); 8710 expect(p.visibleOffset, isZero);
8345 expect(p.visibleLength, isZero); 8711 expect(p.visibleLength, isZero);
8346 } 8712 }
8347 8713
8348 /** 8714 /**
8349 * TODO(scheglov) rename "Const" to "Expr" everywhere 8715 * TODO(scheglov) rename "Const" to "Expr" everywhere
8350 */ 8716 */
8351 void _assertUnlinkedConst(UnlinkedConst constExpr, 8717 void _assertUnlinkedConst(UnlinkedConst constExpr,
8352 {bool isInvalid: false, 8718 {bool isInvalid: false,
8353 List<UnlinkedConstOperation> operators: const <UnlinkedConstOperation>[], 8719 List<UnlinkedConstOperation> operators: const <UnlinkedConstOperation>[],
8720 List<UnlinkedExprAssignOperator> assignmentOperators:
8721 const <UnlinkedExprAssignOperator>[],
8354 List<int> ints: const <int>[], 8722 List<int> ints: const <int>[],
8355 List<double> doubles: const <double>[], 8723 List<double> doubles: const <double>[],
8356 List<String> strings: const <String>[], 8724 List<String> strings: const <String>[],
8357 List<_EntityRefValidator> referenceValidators: 8725 List<_EntityRefValidator> referenceValidators:
8358 const <_EntityRefValidator>[]}) { 8726 const <_EntityRefValidator>[]}) {
8359 expect(constExpr, isNotNull); 8727 expect(constExpr, isNotNull);
8360 expect(constExpr.isInvalid, isInvalid); 8728 expect(constExpr.isInvalid, isInvalid);
8361 expect(constExpr.operations, operators); 8729 expect(constExpr.operations, operators);
8362 expect(constExpr.ints, ints); 8730 expect(constExpr.ints, ints);
8363 expect(constExpr.doubles, doubles); 8731 expect(constExpr.doubles, doubles);
8364 expect(constExpr.strings, strings); 8732 expect(constExpr.strings, strings);
8733 expect(constExpr.assignmentOperators, assignmentOperators);
8365 expect(constExpr.references, hasLength(referenceValidators.length)); 8734 expect(constExpr.references, hasLength(referenceValidators.length));
8366 for (int i = 0; i < referenceValidators.length; i++) { 8735 for (int i = 0; i < referenceValidators.length; i++) {
8367 referenceValidators[i](constExpr.references[i]); 8736 referenceValidators[i](constExpr.references[i]);
8368 } 8737 }
8369 } 8738 }
8370 8739
8371 void _assertVariableVisible( 8740 void _assertVariableVisible(
8372 String code, UnlinkedVariable v, String visibleBegin, String visibleEnd) { 8741 String code, UnlinkedVariable v, String visibleBegin, String visibleEnd) {
8373 int expectedVisibleOffset = code.indexOf(visibleBegin); 8742 int expectedVisibleOffset = code.indexOf(visibleBegin);
8374 int expectedVisibleLength = 8743 int expectedVisibleLength =
8375 code.indexOf(visibleEnd) - expectedVisibleOffset + 1; 8744 code.indexOf(visibleEnd) - expectedVisibleOffset + 1;
8376 expect(v.visibleOffset, expectedVisibleOffset); 8745 expect(v.visibleOffset, expectedVisibleOffset);
8377 expect(v.visibleLength, expectedVisibleLength); 8746 expect(v.visibleLength, expectedVisibleLength);
8378 } 8747 }
8379 } 8748 }
8380 8749
8381 /** 8750 /**
8382 * Description of expectations for a prelinked prefix reference. 8751 * Description of expectations for a prelinked prefix reference.
8383 */ 8752 */
8384 class _PrefixExpectation { 8753 class _PrefixExpectation {
8385 final ReferenceKind kind; 8754 final ReferenceKind kind;
8386 final String name; 8755 final String name;
8387 final String absoluteUri; 8756 final String absoluteUri;
8388 final String relativeUri; 8757 final String relativeUri;
8389 final int numTypeParameters; 8758 final int numTypeParameters;
8390 8759
8391 _PrefixExpectation(this.kind, this.name, 8760 _PrefixExpectation(this.kind, this.name,
8392 {this.absoluteUri, this.relativeUri, this.numTypeParameters: 0}); 8761 {this.absoluteUri, this.relativeUri, this.numTypeParameters: 0});
8393 } 8762 }
OLDNEW
« no previous file with comments | « pkg/analyzer/lib/src/summary/summarize_const_expr.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698