OLD | NEW |
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, 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.generated.resolver_test; | 5 library analyzer.test.generated.resolver_test; |
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 2677 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2688 // The type is resolved. | 2688 // The type is resolved. |
2689 expect(vd.variables.type.toString(), 'A'); | 2689 expect(vd.variables.type.toString(), 'A'); |
2690 // The initializer is not resolved. | 2690 // The initializer is not resolved. |
2691 VariableDeclaration v = vd.variables.variables[0]; | 2691 VariableDeclaration v = vd.variables.variables[0]; |
2692 var vi = v.initializer as InstanceCreationExpression; | 2692 var vi = v.initializer as InstanceCreationExpression; |
2693 expect(vi.constructorName.type.type, isNull); | 2693 expect(vi.constructorName.type.type, isNull); |
2694 } | 2694 } |
2695 } | 2695 } |
2696 | 2696 |
2697 void test_modeLocal_noContext() { | 2697 void test_modeLocal_noContext() { |
2698 CompilationUnit unit = ParserTestCase.parseCompilationUnit(r''' | 2698 CompilationUnit unit; |
| 2699 _resolveTypeModeLocal( |
| 2700 r''' |
2699 class C { | 2701 class C { |
2700 A f = new A(); | 2702 A f = new A(); |
2701 A m([A p = const A()]) { | 2703 A m([A p = const A()]) { |
2702 A v; | 2704 A v; |
2703 } | 2705 } |
2704 } | 2706 } |
2705 A f([A p = const A()]) { | 2707 A f([A p = const A()]) { |
2706 A v1 = new A(); | 2708 A v1 = new A(); |
2707 A f2(A p2) { | 2709 A f2(A p2) { |
2708 A v2; | 2710 A v2; |
2709 } | 2711 } |
2710 } | 2712 } |
2711 A V = new A(); | 2713 A V = new A(); |
2712 A get G => new A(); | 2714 A get G => new A(); |
2713 '''); | 2715 ''', (CompilationUnit u) { |
2714 var unitElement = new CompilationUnitElementImpl('/test.dart'); | 2716 unit = u; |
2715 ClassElementImpl A = ElementFactory.classElement2('A'); | 2717 return u; |
2716 | 2718 }); |
2717 // Build API elements. | |
2718 { | |
2719 var holder = new ElementHolder(); | |
2720 unit.accept(new ElementBuilder(holder, unitElement)); | |
2721 } | |
2722 | |
2723 // Resolve API types. | |
2724 { | |
2725 MemoryResourceProvider resourceProvider = new MemoryResourceProvider(); | |
2726 InternalAnalysisContext context = AnalysisContextFactory.contextWithCore( | |
2727 resourceProvider: resourceProvider); | |
2728 var source = resourceProvider.getFile('/test.dart').createSource(); | |
2729 var libraryElement = new LibraryElementImpl.forNode(context, null) | |
2730 ..definingCompilationUnit = unitElement; | |
2731 var libraryScope = new LibraryScope(libraryElement); | |
2732 var visitor = new TypeResolverVisitor( | |
2733 libraryElement, source, _typeProvider, _listener, | |
2734 nameScope: libraryScope, mode: TypeResolverMode.local); | |
2735 libraryScope.define(A); | |
2736 unit.accept(visitor); | |
2737 } | |
2738 | 2719 |
2739 // Top-level: C | 2720 // Top-level: C |
2740 { | 2721 { |
2741 var c = unit.declarations[0] as ClassDeclaration; | 2722 var c = unit.declarations[0] as ClassDeclaration; |
2742 { | 2723 { |
2743 var fd = c.members[0] as FieldDeclaration; | 2724 var fd = c.members[0] as FieldDeclaration; |
2744 // The type of "f" is not resolved. | 2725 // The type of "f" is not resolved. |
2745 expect(fd.fields.type.type, isNull); | 2726 expect(fd.fields.type.type, isNull); |
2746 // The initializer of "f" is resolved. | 2727 // The initializer of "f" is resolved. |
2747 var f = fd.fields.variables[0]; | 2728 var f = fd.fields.variables[0]; |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2820 var g = unit.declarations[3] as FunctionDeclaration; | 2801 var g = unit.declarations[3] as FunctionDeclaration; |
2821 // The return type is not resolved. | 2802 // The return type is not resolved. |
2822 expect(g.returnType.type, isNull); | 2803 expect(g.returnType.type, isNull); |
2823 // The body is resolved. | 2804 // The body is resolved. |
2824 var gb = g.functionExpression.body as ExpressionFunctionBody; | 2805 var gb = g.functionExpression.body as ExpressionFunctionBody; |
2825 var ge = gb.expression as InstanceCreationExpression; | 2806 var ge = gb.expression as InstanceCreationExpression; |
2826 expect(ge.constructorName.type.type.toString(), 'A'); | 2807 expect(ge.constructorName.type.type.toString(), 'A'); |
2827 } | 2808 } |
2828 } | 2809 } |
2829 | 2810 |
| 2811 void test_modeLocal_withContext_class() { |
| 2812 ClassDeclaration c; |
| 2813 _resolveTypeModeLocal( |
| 2814 r''' |
| 2815 class C<T1> { |
| 2816 A m<T2>() { |
| 2817 T1 v1; |
| 2818 T2 v2; |
| 2819 } |
| 2820 } |
| 2821 ''', (CompilationUnit u) { |
| 2822 c = u.declarations[0] as ClassDeclaration; |
| 2823 return c; |
| 2824 }); |
| 2825 var m = c.members[0] as MethodDeclaration; |
| 2826 |
| 2827 // The return type of "m" is not resolved. |
| 2828 expect(m.returnType.type, isNull); |
| 2829 |
| 2830 var mb = m.body as BlockFunctionBody; |
| 2831 var ms = mb.block.statements; |
| 2832 |
| 2833 // The type of "v1" is resolved. |
| 2834 { |
| 2835 var vd = ms[0] as VariableDeclarationStatement; |
| 2836 expect(vd.variables.type.type.toString(), 'T1'); |
| 2837 } |
| 2838 |
| 2839 // The type of "v2" is resolved. |
| 2840 { |
| 2841 var vd = ms[1] as VariableDeclarationStatement; |
| 2842 expect(vd.variables.type.type.toString(), 'T2'); |
| 2843 } |
| 2844 } |
| 2845 |
| 2846 void test_modeLocal_withContext_inClass_constructor() { |
| 2847 ConstructorDeclaration cc; |
| 2848 _resolveTypeModeLocal( |
| 2849 r''' |
| 2850 class C<T> { |
| 2851 C() { |
| 2852 T v1; |
| 2853 } |
| 2854 } |
| 2855 ''', (CompilationUnit u) { |
| 2856 var c = u.declarations[0] as ClassDeclaration; |
| 2857 cc = c.members[0] as ConstructorDeclaration; |
| 2858 return cc; |
| 2859 }); |
| 2860 |
| 2861 var ccb = cc.body as BlockFunctionBody; |
| 2862 var ccs = ccb.block.statements; |
| 2863 |
| 2864 // The type of "v" is resolved. |
| 2865 { |
| 2866 var vd = ccs[0] as VariableDeclarationStatement; |
| 2867 expect(vd.variables.type.type.toString(), 'T'); |
| 2868 } |
| 2869 } |
| 2870 |
| 2871 void test_modeLocal_withContext_inClass_method() { |
| 2872 MethodDeclaration m; |
| 2873 _resolveTypeModeLocal( |
| 2874 r''' |
| 2875 class C<T1> { |
| 2876 A m<T2>() { |
| 2877 T1 v1; |
| 2878 T2 v2; |
| 2879 } |
| 2880 } |
| 2881 ''', (CompilationUnit u) { |
| 2882 var c = u.declarations[0] as ClassDeclaration; |
| 2883 m = c.members[0] as MethodDeclaration; |
| 2884 return m; |
| 2885 }); |
| 2886 |
| 2887 // The return type of "m" is not resolved. |
| 2888 expect(m.returnType.type, isNull); |
| 2889 |
| 2890 var mb = m.body as BlockFunctionBody; |
| 2891 var ms = mb.block.statements; |
| 2892 |
| 2893 // The type of "v1" is resolved. |
| 2894 { |
| 2895 var vd = ms[0] as VariableDeclarationStatement; |
| 2896 expect(vd.variables.type.type.toString(), 'T1'); |
| 2897 } |
| 2898 |
| 2899 // The type of "v2" is resolved. |
| 2900 { |
| 2901 var vd = ms[1] as VariableDeclarationStatement; |
| 2902 expect(vd.variables.type.type.toString(), 'T2'); |
| 2903 } |
| 2904 } |
| 2905 |
| 2906 void test_modeLocal_withContext_methodBody() { |
| 2907 expect(() { |
| 2908 _resolveTypeModeLocal( |
| 2909 r''' |
| 2910 class C<T1> { |
| 2911 A m<T2>() { |
| 2912 T1 v1; |
| 2913 T2 v2; |
| 2914 } |
| 2915 } |
| 2916 ''', (CompilationUnit u) { |
| 2917 var c = u.declarations[0] as ClassDeclaration; |
| 2918 var m = c.members[0] as MethodDeclaration; |
| 2919 var mb = m.body as BlockFunctionBody; |
| 2920 return mb; |
| 2921 }); |
| 2922 }, throwsStateError); |
| 2923 } |
| 2924 |
| 2925 void test_modeLocal_withContext_topLevelFunction() { |
| 2926 FunctionDeclaration f; |
| 2927 _resolveTypeModeLocal( |
| 2928 r''' |
| 2929 A m<T>() { |
| 2930 T v; |
| 2931 } |
| 2932 ''', (CompilationUnit u) { |
| 2933 f = u.declarations[0] as FunctionDeclaration; |
| 2934 return f; |
| 2935 }); |
| 2936 |
| 2937 // The return type of "f" is not resolved. |
| 2938 expect(f.returnType.type, isNull); |
| 2939 |
| 2940 var fb = f.functionExpression.body as BlockFunctionBody; |
| 2941 var fs = fb.block.statements; |
| 2942 |
| 2943 // The type of "v" is resolved. |
| 2944 var vd = fs[0] as VariableDeclarationStatement; |
| 2945 expect(vd.variables.type.type.toString(), 'T'); |
| 2946 } |
| 2947 |
| 2948 void test_modeLocal_withContext_topLevelVariable() { |
| 2949 TopLevelVariableDeclaration v; |
| 2950 _resolveTypeModeLocal( |
| 2951 r''' |
| 2952 A v = new A(); |
| 2953 ''', (CompilationUnit u) { |
| 2954 v = u.declarations[0] as TopLevelVariableDeclaration; |
| 2955 return v; |
| 2956 }); |
| 2957 |
| 2958 // The type of "v" is not resolved. |
| 2959 expect(v.variables.type.type, isNull); |
| 2960 |
| 2961 // The type of "v" initializer is resolved. |
| 2962 var vi = v.variables.variables[0].initializer as InstanceCreationExpression; |
| 2963 expect(vi.constructorName.type.type.toString(), 'A'); |
| 2964 } |
| 2965 |
2830 void test_visitCatchClause_exception() { | 2966 void test_visitCatchClause_exception() { |
2831 // catch (e) | 2967 // catch (e) |
2832 CatchClause clause = AstFactory.catchClause("e"); | 2968 CatchClause clause = AstFactory.catchClause("e"); |
2833 SimpleIdentifier exceptionParameter = clause.exceptionParameter; | 2969 SimpleIdentifier exceptionParameter = clause.exceptionParameter; |
2834 exceptionParameter.staticElement = | 2970 exceptionParameter.staticElement = |
2835 new LocalVariableElementImpl.forNode(exceptionParameter); | 2971 new LocalVariableElementImpl.forNode(exceptionParameter); |
2836 _resolveCatchClause(clause, _typeProvider.dynamicType, null); | 2972 _resolveCatchClause(clause, _typeProvider.dynamicType, null); |
2837 _listener.assertNoErrors(); | 2973 _listener.assertNoErrors(); |
2838 } | 2974 } |
2839 | 2975 |
(...skipping 566 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3406 * @return the element to which the expression was resolved | 3542 * @return the element to which the expression was resolved |
3407 */ | 3543 */ |
3408 void _resolveNode(AstNode node, [List<Element> definedElements]) { | 3544 void _resolveNode(AstNode node, [List<Element> definedElements]) { |
3409 if (definedElements != null) { | 3545 if (definedElements != null) { |
3410 for (Element element in definedElements) { | 3546 for (Element element in definedElements) { |
3411 libraryScope.define(element); | 3547 libraryScope.define(element); |
3412 } | 3548 } |
3413 } | 3549 } |
3414 node.accept(_visitor); | 3550 node.accept(_visitor); |
3415 } | 3551 } |
| 3552 |
| 3553 /** |
| 3554 * Parse the given [code], build elements and resolve in the |
| 3555 * [TypeResolverMode.local] mode. The [code] is allowed to use only the type |
| 3556 * named `A`. |
| 3557 */ |
| 3558 void _resolveTypeModeLocal( |
| 3559 String code, AstNode getNodeToResolve(CompilationUnit unit)) { |
| 3560 CompilationUnit unit = |
| 3561 ParserTestCase.parseCompilationUnit2(code, parseGenericMethods: true); |
| 3562 var unitElement = new CompilationUnitElementImpl('/test.dart'); |
| 3563 |
| 3564 // Build API elements. |
| 3565 { |
| 3566 var holder = new ElementHolder(); |
| 3567 unit.accept(new ElementBuilder(holder, unitElement)); |
| 3568 } |
| 3569 |
| 3570 // Prepare for resolution. |
| 3571 LibraryScope libraryScope; |
| 3572 TypeResolverVisitor visitor; |
| 3573 { |
| 3574 MemoryResourceProvider resourceProvider = new MemoryResourceProvider(); |
| 3575 InternalAnalysisContext context = AnalysisContextFactory.contextWithCore( |
| 3576 resourceProvider: resourceProvider); |
| 3577 var source = resourceProvider.getFile('/test.dart').createSource(); |
| 3578 var libraryElement = new LibraryElementImpl.forNode(context, null) |
| 3579 ..definingCompilationUnit = unitElement; |
| 3580 libraryScope = new LibraryScope(libraryElement); |
| 3581 visitor = new TypeResolverVisitor( |
| 3582 libraryElement, source, _typeProvider, _listener, |
| 3583 nameScope: libraryScope, mode: TypeResolverMode.local); |
| 3584 } |
| 3585 |
| 3586 // Define top-level types. |
| 3587 ClassElementImpl A = ElementFactory.classElement2('A'); |
| 3588 libraryScope.define(A); |
| 3589 |
| 3590 // Perform resolution. |
| 3591 AstNode nodeToResolve = getNodeToResolve(unit); |
| 3592 nodeToResolve.accept(visitor); |
| 3593 } |
3416 } | 3594 } |
3417 | 3595 |
3418 class _RootScope extends Scope { | 3596 class _RootScope extends Scope { |
3419 @override | 3597 @override |
3420 Element internalLookup(Identifier identifier, String name, | 3598 Element internalLookup(Identifier identifier, String name, |
3421 LibraryElement referencingLibrary) => | 3599 LibraryElement referencingLibrary) => |
3422 null; | 3600 null; |
3423 } | 3601 } |
3424 | 3602 |
3425 /** | 3603 /** |
3426 * Represents an element left over from a previous resolver run. | 3604 * Represents an element left over from a previous resolver run. |
3427 * | 3605 * |
3428 * A _StaleElement should always be replaced with either null or a new Element. | 3606 * A _StaleElement should always be replaced with either null or a new Element. |
3429 */ | 3607 */ |
3430 class _StaleElement extends ElementImpl { | 3608 class _StaleElement extends ElementImpl { |
3431 _StaleElement() : super("_StaleElement", -1); | 3609 _StaleElement() : super("_StaleElement", -1); |
3432 | 3610 |
3433 @override | 3611 @override |
3434 get kind => throw "_StaleElement's kind shouldn't be accessed"; | 3612 get kind => throw "_StaleElement's kind shouldn't be accessed"; |
3435 | 3613 |
3436 @override | 3614 @override |
3437 accept(_) => throw "_StaleElement shouldn't be visited"; | 3615 accept(_) => throw "_StaleElement shouldn't be visited"; |
3438 } | 3616 } |
OLD | NEW |