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

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

Issue 1700203002: Serialize and resynthesize unresolved identifiers. (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
« no previous file with comments | « pkg/analyzer/test/src/summary/resynthesize_test.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 450 matching lines...) Expand 10 before | Expand all | Expand 10 after
461 expect(definingUnit.references[prefixReference].kind, ReferenceKind.prefix); 461 expect(definingUnit.references[prefixReference].kind, ReferenceKind.prefix);
462 expect(definingUnit.references[prefixReference].unit, 0); 462 expect(definingUnit.references[prefixReference].unit, 0);
463 } 463 }
464 464
465 /** 465 /**
466 * Check the data structures that are reachable from an index in the 466 * Check the data structures that are reachable from an index in the
467 * references table.. If the reference in question is an explicit 467 * references table.. If the reference in question is an explicit
468 * reference, return the [UnlinkedReference] that is used to make the 468 * reference, return the [UnlinkedReference] that is used to make the
469 * explicit reference. If the type reference in question is an implicit 469 * explicit reference. If the type reference in question is an implicit
470 * reference, return `null`. 470 * reference, return `null`.
471 *
472 * TODO(scheglov) remove [checkAstDerivedDataOverride] once elements-based
473 * serializer can record unresolved information.
471 */ 474 */
472 UnlinkedReference checkReferenceIndex(int referenceIndex, String absoluteUri, 475 UnlinkedReference checkReferenceIndex(int referenceIndex, String absoluteUri,
473 String relativeUri, String expectedName, 476 String relativeUri, String expectedName,
474 {ReferenceKind expectedKind: ReferenceKind.classOrEnum, 477 {ReferenceKind expectedKind: ReferenceKind.classOrEnum,
475 int expectedTargetUnit: 0, 478 int expectedTargetUnit: 0,
476 LinkedUnit linkedSourceUnit, 479 LinkedUnit linkedSourceUnit,
477 UnlinkedUnit unlinkedSourceUnit, 480 UnlinkedUnit unlinkedSourceUnit,
478 int numTypeParameters: 0}) { 481 int numTypeParameters: 0,
482 bool checkAstDerivedDataOverride: false}) {
479 linkedSourceUnit ??= definingUnit; 483 linkedSourceUnit ??= definingUnit;
480 unlinkedSourceUnit ??= unlinkedUnits[0]; 484 unlinkedSourceUnit ??= unlinkedUnits[0];
481 LinkedReference referenceResolution = 485 LinkedReference referenceResolution =
482 linkedSourceUnit.references[referenceIndex]; 486 linkedSourceUnit.references[referenceIndex];
483 String name; 487 String name;
484 UnlinkedReference reference; 488 UnlinkedReference reference;
485 if (referenceIndex < unlinkedSourceUnit.references.length) { 489 if (referenceIndex < unlinkedSourceUnit.references.length) {
486 // This is an explicit reference, so its name and prefix should be in 490 // This is an explicit reference, so its name and prefix should be in
487 // [UnlinkedUnit.references]. 491 // [UnlinkedUnit.references].
488 expect(referenceResolution.name, isEmpty); 492 expect(referenceResolution.name, isEmpty);
489 reference = unlinkedSourceUnit.references[referenceIndex]; 493 reference = unlinkedSourceUnit.references[referenceIndex];
490 name = reference.name; 494 name = reference.name;
491 if (reference.prefixReference != 0) { 495 if (reference.prefixReference != 0) {
492 // Prefixes should appear in the references table before any reference 496 // Prefixes should appear in the references table before any reference
493 // that uses them. 497 // that uses them.
494 expect(reference.prefixReference, lessThan(referenceIndex)); 498 expect(reference.prefixReference, lessThan(referenceIndex));
495 } 499 }
496 } else { 500 } else {
497 // This is an implicit reference, so its name should be in 501 // This is an implicit reference, so its name should be in
498 // [LinkedUnit.references]. 502 // [LinkedUnit.references].
499 name = referenceResolution.name; 503 name = referenceResolution.name;
500 } 504 }
501 // Index 0 is reserved. 505 // Index 0 is reserved.
502 expect(referenceIndex, isNot(0)); 506 expect(referenceIndex, isNot(0));
503 if (absoluteUri == null) { 507 if (absoluteUri == null) {
504 expect(referenceResolution.dependency, 0); 508 expect(referenceResolution.dependency, 0);
505 } else { 509 } else {
506 checkDependency(referenceResolution.dependency, absoluteUri, relativeUri); 510 checkDependency(referenceResolution.dependency, absoluteUri, relativeUri);
507 } 511 }
508 if (expectedKind == ReferenceKind.unresolved && !checkAstDerivedData) { 512 if (expectedKind == ReferenceKind.unresolved &&
513 !checkAstDerivedData &&
514 !checkAstDerivedDataOverride) {
509 // summarize_elements.dart isn't yet able to record the name of 515 // summarize_elements.dart isn't yet able to record the name of
510 // unresolved references. TODO(paulberry): fix this. 516 // unresolved references. TODO(paulberry): fix this.
511 expect(name, '*unresolved*'); 517 expect(name, '*unresolved*');
512 } else { 518 } else {
513 if (expectedName == null) { 519 if (expectedName == null) {
514 expect(name, isEmpty); 520 expect(name, isEmpty);
515 } else { 521 } else {
516 expect(name, expectedName); 522 expect(name, expectedName);
517 } 523 }
518 } 524 }
519 expect(referenceResolution.kind, expectedKind); 525 expect(referenceResolution.kind, expectedKind);
520 expect(referenceResolution.unit, expectedTargetUnit); 526 expect(referenceResolution.unit, expectedTargetUnit);
521 expect(referenceResolution.numTypeParameters, numTypeParameters); 527 expect(referenceResolution.numTypeParameters, numTypeParameters);
522 return reference; 528 return reference;
523 } 529 }
524 530
525 /** 531 /**
526 * Verify that the given [typeRef] represents a reference to a type declared 532 * Verify that the given [typeRef] represents a reference to a type declared
527 * in a file reachable via [absoluteUri] and [relativeUri], having name 533 * in a file reachable via [absoluteUri] and [relativeUri], having name
528 * [expectedName]. If [expectedPrefix] is supplied, verify that the type is 534 * [expectedName]. If [expectedPrefix] is supplied, verify that the type is
529 * reached via the given prefix. If [allowTypeParameters] is true, allow the 535 * reached via the given prefix. If [allowTypeParameters] is true, allow the
530 * type reference to supply type parameters. [expectedKind] is the kind of 536 * type reference to supply type parameters. [expectedKind] is the kind of
531 * object referenced. [linkedSourceUnit] and [unlinkedSourceUnit] refer 537 * object referenced. [linkedSourceUnit] and [unlinkedSourceUnit] refer
532 * to the compilation unit within which the [typeRef] appears; if not 538 * to the compilation unit within which the [typeRef] appears; if not
533 * specified they are assumed to refer to the defining compilation unit. 539 * specified they are assumed to refer to the defining compilation unit.
534 * [expectedTargetUnit] is the index of the compilation unit in which the 540 * [expectedTargetUnit] is the index of the compilation unit in which the
535 * target of the [typeRef] is expected to appear; if not specified it is 541 * target of the [typeRef] is expected to appear; if not specified it is
536 * assumed to be the defining compilation unit. [numTypeParameters] is the 542 * assumed to be the defining compilation unit. [numTypeParameters] is the
537 * number of type parameters of the thing being referred to. 543 * number of type parameters of the thing being referred to.
544 *
545 * TODO(scheglov) remove [checkAstDerivedDataOverride] once elements-based
546 * serializer can record unresolved information.
538 */ 547 */
539 void checkTypeRef(EntityRef typeRef, String absoluteUri, String relativeUri, 548 void checkTypeRef(EntityRef typeRef, String absoluteUri, String relativeUri,
540 String expectedName, 549 String expectedName,
541 {String expectedPrefix, 550 {String expectedPrefix,
542 List<_PrefixExpectation> prefixExpectations, 551 List<_PrefixExpectation> prefixExpectations,
543 bool allowTypeParameters: false, 552 bool allowTypeParameters: false,
544 ReferenceKind expectedKind: ReferenceKind.classOrEnum, 553 ReferenceKind expectedKind: ReferenceKind.classOrEnum,
545 int expectedTargetUnit: 0, 554 int expectedTargetUnit: 0,
546 LinkedUnit linkedSourceUnit, 555 LinkedUnit linkedSourceUnit,
547 UnlinkedUnit unlinkedSourceUnit, 556 UnlinkedUnit unlinkedSourceUnit,
548 int numTypeParameters: 0}) { 557 int numTypeParameters: 0,
558 bool checkAstDerivedDataOverride: false}) {
549 linkedSourceUnit ??= definingUnit; 559 linkedSourceUnit ??= definingUnit;
550 expect(typeRef, new isInstanceOf<EntityRef>()); 560 expect(typeRef, new isInstanceOf<EntityRef>());
551 expect(typeRef.paramReference, 0); 561 expect(typeRef.paramReference, 0);
552 int index = typeRef.reference; 562 int index = typeRef.reference;
553 if (!allowTypeParameters) { 563 if (!allowTypeParameters) {
554 expect(typeRef.typeArguments, isEmpty); 564 expect(typeRef.typeArguments, isEmpty);
555 } 565 }
556 UnlinkedReference reference = checkReferenceIndex( 566 UnlinkedReference reference = checkReferenceIndex(
557 index, absoluteUri, relativeUri, expectedName, 567 index, absoluteUri, relativeUri, expectedName,
558 expectedKind: expectedKind, 568 expectedKind: expectedKind,
559 expectedTargetUnit: expectedTargetUnit, 569 expectedTargetUnit: expectedTargetUnit,
560 linkedSourceUnit: linkedSourceUnit, 570 linkedSourceUnit: linkedSourceUnit,
561 unlinkedSourceUnit: unlinkedSourceUnit, 571 unlinkedSourceUnit: unlinkedSourceUnit,
562 numTypeParameters: numTypeParameters); 572 numTypeParameters: numTypeParameters,
573 checkAstDerivedDataOverride: checkAstDerivedDataOverride);
563 expect(reference, isNotNull, 574 expect(reference, isNotNull,
564 reason: 'Unlinked type refs must refer to an explicit reference'); 575 reason: 'Unlinked type refs must refer to an explicit reference');
565 if (expectedKind == ReferenceKind.unresolved && !checkAstDerivedData) { 576 if (expectedKind == ReferenceKind.unresolved &&
577 !checkAstDerivedData &&
578 !checkAstDerivedDataOverride) {
566 // summarize_elements.dart isn't yet able to record the prefix of 579 // summarize_elements.dart isn't yet able to record the prefix of
567 // unresolved references. TODO(paulberry): fix this. 580 // unresolved references. TODO(paulberry): fix this.
568 expect(reference.prefixReference, 0); 581 expect(reference.prefixReference, 0);
569 } else if (expectedPrefix != null) { 582 } else if (expectedPrefix != null) {
570 checkPrefix(reference.prefixReference, expectedPrefix); 583 checkPrefix(reference.prefixReference, expectedPrefix);
571 } else if (prefixExpectations != null) { 584 } else if (prefixExpectations != null) {
572 for (_PrefixExpectation expectation in prefixExpectations) { 585 for (_PrefixExpectation expectation in prefixExpectations) {
573 expect(reference.prefixReference, isNot(0)); 586 expect(reference.prefixReference, isNot(0));
574 reference = checkReferenceIndex( 587 reference = checkReferenceIndex(
575 reference.prefixReference, 588 reference.prefixReference,
(...skipping 2083 matching lines...) Expand 10 before | Expand all | Expand 10 after
2659 String text = ''' 2672 String text = '''
2660 class C<T> { 2673 class C<T> {
2661 static const a = T; 2674 static const a = T;
2662 } 2675 }
2663 '''; 2676 ''';
2664 UnlinkedVariable variable = 2677 UnlinkedVariable variable =
2665 serializeClassText(text, allowErrors: true).fields[0]; 2678 serializeClassText(text, allowErrors: true).fields[0];
2666 _assertUnlinkedConst(variable.constExpr, isInvalid: true); 2679 _assertUnlinkedConst(variable.constExpr, isInvalid: true);
2667 } 2680 }
2668 2681
2682 test_constExpr_pushReference_unresolved_prefix0() {
2683 UnlinkedVariable variable = serializeVariableText(
2684 '''
2685 const v = foo;
2686 ''',
2687 allowErrors: true);
2688 _assertUnlinkedConst(variable.constExpr, operators: [
2689 UnlinkedConstOperation.pushReference
2690 ], referenceValidators: [
2691 (EntityRef r) => checkTypeRef(r, null, null, 'foo',
2692 expectedKind: ReferenceKind.unresolved,
2693 checkAstDerivedDataOverride: true)
2694 ]);
2695 }
2696
2697 test_constExpr_pushReference_unresolved_prefix1() {
2698 UnlinkedVariable variable = serializeVariableText(
2699 '''
2700 class C {}
2701 const v = C.foo;
2702 ''',
2703 allowErrors: true);
2704 _assertUnlinkedConst(variable.constExpr, operators: [
2705 UnlinkedConstOperation.pushReference
2706 ], referenceValidators: [
2707 (EntityRef r) => checkTypeRef(r, null, null, 'foo',
2708 expectedKind: ReferenceKind.unresolved,
2709 checkAstDerivedDataOverride: true,
2710 prefixExpectations: [
2711 new _PrefixExpectation(ReferenceKind.classOrEnum, 'C')
2712 ])
2713 ]);
2714 }
2715
2716 test_constExpr_pushReference_unresolved_prefix2() {
2717 addNamedSource(
2718 '/a.dart',
2719 '''
2720 class C {}
2721 ''');
2722 UnlinkedVariable variable = serializeVariableText(
2723 '''
2724 import 'a.dart' as p;
2725 const v = p.C.foo;
2726 ''',
2727 allowErrors: true);
2728 _assertUnlinkedConst(variable.constExpr, operators: [
2729 UnlinkedConstOperation.pushReference
2730 ], referenceValidators: [
2731 (EntityRef r) => checkTypeRef(r, null, null, 'foo',
2732 expectedKind: ReferenceKind.unresolved,
2733 checkAstDerivedDataOverride: true,
2734 prefixExpectations: [
2735 new _PrefixExpectation(ReferenceKind.classOrEnum, 'C',
2736 absoluteUri: absUri('/a.dart'), relativeUri: 'a.dart'),
2737 new _PrefixExpectation(ReferenceKind.prefix, 'p',
2738 inLibraryDefiningUnit: true),
2739 ])
2740 ]);
2741 }
2742
2669 test_constExpr_pushString_adjacent() { 2743 test_constExpr_pushString_adjacent() {
2670 UnlinkedVariable variable = 2744 UnlinkedVariable variable =
2671 serializeVariableText('const v = "aaa" "b" "ccc";'); 2745 serializeVariableText('const v = "aaa" "b" "ccc";');
2672 _assertUnlinkedConst(variable.constExpr, 2746 _assertUnlinkedConst(variable.constExpr,
2673 operators: [UnlinkedConstOperation.pushString], strings: ['aaabccc']); 2747 operators: [UnlinkedConstOperation.pushString], strings: ['aaabccc']);
2674 } 2748 }
2675 2749
2676 test_constExpr_pushString_adjacent_interpolation() { 2750 test_constExpr_pushString_adjacent_interpolation() {
2677 UnlinkedVariable variable = 2751 UnlinkedVariable variable =
2678 serializeVariableText(r'const v = "aaa" "bb ${42} bbb" "cccc";'); 2752 serializeVariableText(r'const v = "aaa" "bb ${42} bbb" "cccc";');
(...skipping 4027 matching lines...) Expand 10 before | Expand all | Expand 10 after
6706 final String absoluteUri; 6780 final String absoluteUri;
6707 final String relativeUri; 6781 final String relativeUri;
6708 final int numTypeParameters; 6782 final int numTypeParameters;
6709 6783
6710 _PrefixExpectation(this.kind, this.name, 6784 _PrefixExpectation(this.kind, this.name,
6711 {this.inLibraryDefiningUnit: false, 6785 {this.inLibraryDefiningUnit: false,
6712 this.absoluteUri, 6786 this.absoluteUri,
6713 this.relativeUri, 6787 this.relativeUri,
6714 this.numTypeParameters: 0}); 6788 this.numTypeParameters: 0});
6715 } 6789 }
OLDNEW
« no previous file with comments | « pkg/analyzer/test/src/summary/resynthesize_test.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698