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

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

Issue 1691693002: Serialize local functions and variables. (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: Fixes for review comments. 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
« no previous file with comments | « pkg/analyzer/lib/src/summary/summarize_elements.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/generated/engine.dart'; 10 import 'package:analyzer/src/generated/engine.dart';
(...skipping 2731 matching lines...) Expand 10 before | Expand all | Expand 10 after
2742 String text = 'class C { C(); }'; 2742 String text = 'class C { C(); }';
2743 UnlinkedExecutable executable = 2743 UnlinkedExecutable executable =
2744 findExecutable('', executables: serializeClassText(text).executables); 2744 findExecutable('', executables: serializeClassText(text).executables);
2745 expect(executable.kind, UnlinkedExecutableKind.constructor); 2745 expect(executable.kind, UnlinkedExecutableKind.constructor);
2746 expect(executable.returnType, isNull); 2746 expect(executable.returnType, isNull);
2747 expect(executable.isExternal, isFalse); 2747 expect(executable.isExternal, isFalse);
2748 expect(executable.nameOffset, text.indexOf('C();')); 2748 expect(executable.nameOffset, text.indexOf('C();'));
2749 expect(executable.isRedirectedConstructor, isFalse); 2749 expect(executable.isRedirectedConstructor, isFalse);
2750 expect(executable.redirectedConstructor, isNull); 2750 expect(executable.redirectedConstructor, isNull);
2751 expect(executable.redirectedConstructorName, isEmpty); 2751 expect(executable.redirectedConstructorName, isEmpty);
2752 expect(executable.visibleOffset, 0);
2753 expect(executable.visibleLength, 0);
2752 } 2754 }
2753 2755
2754 test_constructor_anonymous() { 2756 test_constructor_anonymous() {
2755 UnlinkedExecutable executable = findExecutable('', 2757 UnlinkedExecutable executable = findExecutable('',
2756 executables: serializeClassText('class C { C(); }').executables); 2758 executables: serializeClassText('class C { C(); }').executables);
2757 expect(executable.name, isEmpty); 2759 expect(executable.name, isEmpty);
2758 } 2760 }
2759 2761
2760 test_constructor_const() { 2762 test_constructor_const() {
2761 UnlinkedExecutable executable = findExecutable('', 2763 UnlinkedExecutable executable = findExecutable('',
(...skipping 742 matching lines...) Expand 10 before | Expand all | Expand 10 after
3504 expect(executable.isAbstract, isFalse); 3506 expect(executable.isAbstract, isFalse);
3505 } 3507 }
3506 3508
3507 test_executable_function() { 3509 test_executable_function() {
3508 String text = ' f() {}'; 3510 String text = ' f() {}';
3509 UnlinkedExecutable executable = serializeExecutableText(text); 3511 UnlinkedExecutable executable = serializeExecutableText(text);
3510 expect(executable.kind, UnlinkedExecutableKind.functionOrMethod); 3512 expect(executable.kind, UnlinkedExecutableKind.functionOrMethod);
3511 expect(executable.returnType, isNull); 3513 expect(executable.returnType, isNull);
3512 expect(executable.isExternal, isFalse); 3514 expect(executable.isExternal, isFalse);
3513 expect(executable.nameOffset, text.indexOf('f')); 3515 expect(executable.nameOffset, text.indexOf('f'));
3516 expect(executable.visibleOffset, 0);
3517 expect(executable.visibleLength, 0);
3514 expect(unlinkedUnits[0].publicNamespace.names, hasLength(1)); 3518 expect(unlinkedUnits[0].publicNamespace.names, hasLength(1));
3515 expect(unlinkedUnits[0].publicNamespace.names[0].kind, 3519 expect(unlinkedUnits[0].publicNamespace.names[0].kind,
3516 ReferenceKind.topLevelFunction); 3520 ReferenceKind.topLevelFunction);
3517 expect(unlinkedUnits[0].publicNamespace.names[0].name, 'f'); 3521 expect(unlinkedUnits[0].publicNamespace.names[0].name, 'f');
3518 expect(unlinkedUnits[0].publicNamespace.names[0].numTypeParameters, 0); 3522 expect(unlinkedUnits[0].publicNamespace.names[0].numTypeParameters, 0);
3519 } 3523 }
3520 3524
3521 test_executable_function_explicit_return() { 3525 test_executable_function_explicit_return() {
3522 UnlinkedExecutable executable = 3526 UnlinkedExecutable executable =
3523 serializeExecutableText('dynamic f() => null;'); 3527 serializeExecutableText('dynamic f() => null;');
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
3565 checkTypeRef(executable.returnType, 'dart:core', 'dart:core', 'int'); 3569 checkTypeRef(executable.returnType, 'dart:core', 'dart:core', 'int');
3566 expect(executable.parameters, isEmpty); 3570 expect(executable.parameters, isEmpty);
3567 } 3571 }
3568 3572
3569 test_executable_getter_type_implicit() { 3573 test_executable_getter_type_implicit() {
3570 UnlinkedExecutable executable = serializeExecutableText('get f => 1;'); 3574 UnlinkedExecutable executable = serializeExecutableText('get f => 1;');
3571 expect(executable.returnType, isNull); 3575 expect(executable.returnType, isNull);
3572 expect(executable.parameters, isEmpty); 3576 expect(executable.parameters, isEmpty);
3573 } 3577 }
3574 3578
3579 test_executable_localFunctions() {
3580 String code = r'''
3581 f() { // 1
3582 f1() {}
3583 { // 2
3584 f2() {}
3585 } // 3
3586 } // 4
3587 ''';
3588 UnlinkedExecutable executable = serializeExecutableText(code);
3589 List<UnlinkedExecutable> functions = executable.localFunctions;
3590 expect(functions, hasLength(2));
3591 {
3592 UnlinkedExecutable f1 = functions.singleWhere((v) => v.name == 'f1');
3593 _assertExecutableVisible(code, f1, '{ // 1', '} // 4');
3594 }
3595 {
3596 UnlinkedExecutable f2 = functions.singleWhere((v) => v.name == 'f2');
3597 _assertExecutableVisible(code, f2, '{ // 2', '} // 3');
3598 }
3599 }
3600
3601 test_executable_localVariables_empty() {
3602 UnlinkedExecutable executable = serializeExecutableText(r'''
3603 f() {
3604 }
3605 ''');
3606 expect(executable.localVariables, isEmpty);
3607 }
3608
3609 test_executable_localVariables_inConstructor() {
3610 String code = r'''
3611 class C {
3612 C() { // 1
3613 int v;
3614 } // 2
3615 }
3616 ''';
3617 UnlinkedExecutable executable =
3618 findExecutable('', executables: serializeClassText(code).executables);
3619 List<UnlinkedVariable> variables = executable.localVariables;
3620 expect(variables, hasLength(1));
3621 {
3622 UnlinkedVariable v = variables.singleWhere((v) => v.name == 'v');
3623 _assertVariableVisible(code, v, '{ // 1', '} // 2');
3624 checkTypeRef(v.type, 'dart:core', 'dart:core', 'int');
3625 }
3626 }
3627
3628 test_executable_localVariables_inLocalFunctions() {
3629 String code = r'''
3630 f() {
3631 f1() { // 1
3632 int v1 = 1;
3633 } // 2
3634 f2() { // 3
3635 int v1 = 1;
3636 f3() { // 4
3637 int v2 = 1;
3638 } // 5
3639 } // 6
3640 } // 7
3641 ''';
3642 UnlinkedExecutable executable = serializeExecutableText(code);
3643 List<UnlinkedExecutable> functions = executable.localFunctions;
3644 expect(functions, hasLength(2));
3645 // f - f1
3646 {
3647 UnlinkedExecutable f1 = functions.singleWhere((v) => v.name == 'f1');
3648 List<UnlinkedVariable> variables = f1.localVariables;
3649 expect(variables, hasLength(1));
3650 // f1 - v1
3651 UnlinkedVariable v1 = variables.singleWhere((v) => v.name == 'v1');
3652 _assertVariableVisible(code, v1, '{ // 1', '} // 2');
3653 checkTypeRef(v1.type, 'dart:core', 'dart:core', 'int');
3654 }
3655 // f - f2
3656 {
3657 UnlinkedExecutable f2 = functions.singleWhere((v) => v.name == 'f2');
3658 List<UnlinkedVariable> variables2 = f2.localVariables;
3659 List<UnlinkedExecutable> functions2 = f2.localFunctions;
3660 expect(variables2, hasLength(1));
3661 expect(functions2, hasLength(1));
3662 // f - f2 - v1
3663 UnlinkedVariable v1 = variables2.singleWhere((v) => v.name == 'v1');
3664 _assertVariableVisible(code, v1, '{ // 3', '} // 6');
3665 checkTypeRef(v1.type, 'dart:core', 'dart:core', 'int');
3666 // f - f2 - f3
3667 UnlinkedExecutable f3 = functions2.singleWhere((v) => v.name == 'f3');
3668 _assertExecutableVisible(code, f3, '{ // 3', '} // 6');
3669 List<UnlinkedVariable> variables3 = f3.localVariables;
3670 List<UnlinkedExecutable> functions3 = f3.localFunctions;
3671 expect(variables3, hasLength(1));
3672 expect(functions3, hasLength(0));
3673 // f - f3 - v2
3674 UnlinkedVariable v2 = variables3.singleWhere((v) => v.name == 'v2');
3675 _assertVariableVisible(code, v2, '{ // 4', '} // 5');
3676 checkTypeRef(v2.type, 'dart:core', 'dart:core', 'int');
3677 }
3678 }
3679
3680 test_executable_localVariables_inMethod() {
3681 String code = r'''
3682 class C {
3683 m() { // 1
3684 int v;
3685 f() {}
3686 } // 2
3687 }
3688 ''';
3689 UnlinkedExecutable executable =
3690 findExecutable('m', executables: serializeClassText(code).executables);
3691 {
3692 List<UnlinkedExecutable> functions = executable.localFunctions;
3693 expect(functions, hasLength(1));
3694 UnlinkedExecutable f = functions.singleWhere((v) => v.name == 'f');
3695 _assertExecutableVisible(code, f, '{ // 1', '} // 2');
3696 }
3697 {
3698 List<UnlinkedVariable> variables = executable.localVariables;
3699 expect(variables, hasLength(1));
3700 UnlinkedVariable v = variables.singleWhere((v) => v.name == 'v');
3701 _assertVariableVisible(code, v, '{ // 1', '} // 2');
3702 checkTypeRef(v.type, 'dart:core', 'dart:core', 'int');
3703 }
3704 }
3705
3706 test_executable_localVariables_inTopLevelFunction() {
3707 String code = r'''
3708 f() { // 1
3709 int v1 = 1;
3710 { // 2
3711 int v2 = 2;
3712 } // 3
3713 var v3 = 3;
3714 } // 4
3715 ''';
3716 UnlinkedExecutable executable = serializeExecutableText(code);
3717 List<UnlinkedVariable> variables = executable.localVariables;
3718 expect(variables, hasLength(3));
3719 {
3720 UnlinkedVariable v1 = variables.singleWhere((v) => v.name == 'v1');
3721 _assertVariableVisible(code, v1, '{ // 1', '} // 4');
3722 checkTypeRef(v1.type, 'dart:core', 'dart:core', 'int');
3723 }
3724 {
3725 UnlinkedVariable v2 = variables.singleWhere((v) => v.name == 'v2');
3726 _assertVariableVisible(code, v2, '{ // 2', '} // 3');
3727 checkTypeRef(v2.type, 'dart:core', 'dart:core', 'int');
3728 }
3729 {
3730 UnlinkedVariable v3 = variables.singleWhere((v) => v.name == 'v3');
3731 _assertVariableVisible(code, v3, '{ // 1', '} // 4');
3732 expect(v3.type, isNull);
3733 }
3734 }
3735
3736 test_executable_localVariables_inTopLevelGetter() {
3737 String code = r'''
3738 get g { // 1
3739 int v;
3740 f() {}
3741 } // 2
3742 ''';
3743 UnlinkedExecutable executable = serializeExecutableText(code, 'g');
3744 {
3745 List<UnlinkedExecutable> functions = executable.localFunctions;
3746 expect(functions, hasLength(1));
3747 UnlinkedExecutable f = functions.singleWhere((v) => v.name == 'f');
3748 _assertExecutableVisible(code, f, '{ // 1', '} // 2');
3749 }
3750 {
3751 List<UnlinkedVariable> variables = executable.localVariables;
3752 expect(variables, hasLength(1));
3753 UnlinkedVariable v = variables.singleWhere((v) => v.name == 'v');
3754 _assertVariableVisible(code, v, '{ // 1', '} // 2');
3755 checkTypeRef(v.type, 'dart:core', 'dart:core', 'int');
3756 }
3757 }
3758
3575 test_executable_member_function() { 3759 test_executable_member_function() {
3576 UnlinkedExecutable executable = findExecutable('f', 3760 UnlinkedExecutable executable = findExecutable('f',
3577 executables: serializeClassText('class C { f() {} }').executables); 3761 executables: serializeClassText('class C { f() {} }').executables);
3578 expect(executable.kind, UnlinkedExecutableKind.functionOrMethod); 3762 expect(executable.kind, UnlinkedExecutableKind.functionOrMethod);
3579 expect(executable.returnType, isNull); 3763 expect(executable.returnType, isNull);
3580 expect(executable.isExternal, isFalse); 3764 expect(executable.isExternal, isFalse);
3765 expect(executable.visibleOffset, 0);
3766 expect(executable.visibleLength, 0);
3581 } 3767 }
3582 3768
3583 test_executable_member_function_explicit_return() { 3769 test_executable_member_function_explicit_return() {
3584 UnlinkedExecutable executable = findExecutable('f', 3770 UnlinkedExecutable executable = findExecutable('f',
3585 executables: 3771 executables:
3586 serializeClassText('class C { dynamic f() => null; }').executables); 3772 serializeClassText('class C { dynamic f() => null; }').executables);
3587 expect(executable.returnType, isNotNull); 3773 expect(executable.returnType, isNotNull);
3588 } 3774 }
3589 3775
3590 test_executable_member_function_external() { 3776 test_executable_member_function_external() {
(...skipping 2496 matching lines...) Expand 10 before | Expand all | Expand 10 after
6087 default: 6273 default:
6088 // This reference should have a dependency of 0, since it refers to 6274 // This reference should have a dependency of 0, since it refers to
6089 // an element that is contained within some other element. 6275 // an element that is contained within some other element.
6090 expect(reference.dependency, 0, 6276 expect(reference.dependency, 0,
6091 reason: 'Nonzero dependency for ${reference.kind}'); 6277 reason: 'Nonzero dependency for ${reference.kind}');
6092 } 6278 }
6093 } 6279 }
6094 } 6280 }
6095 } 6281 }
6096 6282
6283 void _assertExecutableVisible(String code, UnlinkedExecutable f,
6284 String visibleBegin, String visibleEnd) {
6285 int expectedVisibleOffset = code.indexOf(visibleBegin);
6286 int expectedVisibleLength =
6287 code.indexOf(visibleEnd) - expectedVisibleOffset + 1;
6288 expect(f.visibleOffset, expectedVisibleOffset);
6289 expect(f.visibleLength, expectedVisibleLength);
6290 }
6291
6097 void _assertUnlinkedConst(UnlinkedConst constExpr, 6292 void _assertUnlinkedConst(UnlinkedConst constExpr,
6098 {bool isInvalid: false, 6293 {bool isInvalid: false,
6099 List<UnlinkedConstOperation> operators: const <UnlinkedConstOperation>[], 6294 List<UnlinkedConstOperation> operators: const <UnlinkedConstOperation>[],
6100 List<int> ints: const <int>[], 6295 List<int> ints: const <int>[],
6101 List<double> doubles: const <double>[], 6296 List<double> doubles: const <double>[],
6102 List<String> strings: const <String>[], 6297 List<String> strings: const <String>[],
6103 List<_EntityRefValidator> referenceValidators: 6298 List<_EntityRefValidator> referenceValidators:
6104 const <_EntityRefValidator>[]}) { 6299 const <_EntityRefValidator>[]}) {
6105 expect(constExpr, isNotNull); 6300 expect(constExpr, isNotNull);
6106 expect(constExpr.isInvalid, isInvalid); 6301 expect(constExpr.isInvalid, isInvalid);
6107 expect(constExpr.operations, operators); 6302 expect(constExpr.operations, operators);
6108 expect(constExpr.ints, ints); 6303 expect(constExpr.ints, ints);
6109 expect(constExpr.doubles, doubles); 6304 expect(constExpr.doubles, doubles);
6110 expect(constExpr.strings, strings); 6305 expect(constExpr.strings, strings);
6111 expect(constExpr.references, hasLength(referenceValidators.length)); 6306 expect(constExpr.references, hasLength(referenceValidators.length));
6112 for (int i = 0; i < referenceValidators.length; i++) { 6307 for (int i = 0; i < referenceValidators.length; i++) {
6113 referenceValidators[i](constExpr.references[i]); 6308 referenceValidators[i](constExpr.references[i]);
6114 } 6309 }
6115 } 6310 }
6311
6312 void _assertVariableVisible(
6313 String code, UnlinkedVariable v, String visibleBegin, String visibleEnd) {
6314 int expectedVisibleOffset = code.indexOf(visibleBegin);
6315 int expectedVisibleLength =
6316 code.indexOf(visibleEnd) - expectedVisibleOffset + 1;
6317 expect(v.visibleOffset, expectedVisibleOffset);
6318 expect(v.visibleLength, expectedVisibleLength);
6319 }
6116 } 6320 }
6117 6321
6118 /** 6322 /**
6119 * Description of expectations for a prelinked prefix reference. 6323 * Description of expectations for a prelinked prefix reference.
6120 */ 6324 */
6121 class _PrefixExpectation { 6325 class _PrefixExpectation {
6122 final ReferenceKind kind; 6326 final ReferenceKind kind;
6123 final String name; 6327 final String name;
6124 final bool inLibraryDefiningUnit; 6328 final bool inLibraryDefiningUnit;
6125 final String absoluteUri; 6329 final String absoluteUri;
6126 final String relativeUri; 6330 final String relativeUri;
6127 final int numTypeParameters; 6331 final int numTypeParameters;
6128 6332
6129 _PrefixExpectation(this.kind, this.name, 6333 _PrefixExpectation(this.kind, this.name,
6130 {this.inLibraryDefiningUnit: false, 6334 {this.inLibraryDefiningUnit: false,
6131 this.absoluteUri, 6335 this.absoluteUri,
6132 this.relativeUri, 6336 this.relativeUri,
6133 this.numTypeParameters: 0}); 6337 this.numTypeParameters: 0});
6134 } 6338 }
OLDNEW
« no previous file with comments | « pkg/analyzer/lib/src/summary/summarize_elements.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698