OLD | NEW |
1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2016, 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 library kernel.analyzer.ast_from_analyzer; | 4 library kernel.analyzer.ast_from_analyzer; |
5 | 5 |
6 import '../ast.dart' as ast; | 6 import '../ast.dart' as ast; |
7 import '../frontend/accessors.dart'; | 7 import '../frontend/accessors.dart'; |
8 import '../frontend/super_initializers.dart'; | 8 import '../frontend/super_initializers.dart'; |
9 import '../log.dart'; | 9 import '../log.dart'; |
10 import '../type_algebra.dart'; | 10 import '../type_algebra.dart'; |
11 import '../transformations/flags.dart'; | 11 import '../transformations/flags.dart'; |
12 import 'analyzer.dart'; | 12 import 'analyzer.dart'; |
13 import 'loader.dart'; | 13 import 'loader.dart'; |
14 import 'package:analyzer/analyzer.dart'; | 14 import 'package:analyzer/analyzer.dart'; |
| 15 import 'package:analyzer/dart/ast/resolution_accessors.dart'; |
15 import 'package:analyzer/src/generated/parser.dart'; | 16 import 'package:analyzer/src/generated/parser.dart'; |
16 import 'package:analyzer/src/dart/element/member.dart'; | 17 import 'package:analyzer/src/dart/element/member.dart'; |
17 import 'package:analyzer/src/error/codes.dart'; | 18 import 'package:analyzer/src/error/codes.dart'; |
18 | 19 |
19 /// Provides reference-level access to libraries, classes, and members. | 20 /// Provides reference-level access to libraries, classes, and members. |
20 /// | 21 /// |
21 /// "Reference level" objects are incomplete nodes that have no children but | 22 /// "Reference level" objects are incomplete nodes that have no children but |
22 /// can be used for linking until the loader promotes the node to a higher | 23 /// can be used for linking until the loader promotes the node to a higher |
23 /// loading level. | 24 /// loading level. |
24 /// | 25 /// |
(...skipping 509 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
534 // TypeScope.buildFunctionInterface. | 535 // TypeScope.buildFunctionInterface. |
535 var positional = <ast.VariableDeclaration>[]; | 536 var positional = <ast.VariableDeclaration>[]; |
536 var named = <ast.VariableDeclaration>[]; | 537 var named = <ast.VariableDeclaration>[]; |
537 int requiredParameterCount = 0; | 538 int requiredParameterCount = 0; |
538 var formals = formalParameters?.parameters ?? const <FormalParameter>[]; | 539 var formals = formalParameters?.parameters ?? const <FormalParameter>[]; |
539 for (var parameter in formals) { | 540 for (var parameter in formals) { |
540 var declaration = makeVariableDeclaration(parameter.element, | 541 var declaration = makeVariableDeclaration(parameter.element, |
541 initializer: parameter is DefaultFormalParameter | 542 initializer: parameter is DefaultFormalParameter |
542 ? buildOptionalTopLevelExpression(parameter.defaultValue) | 543 ? buildOptionalTopLevelExpression(parameter.defaultValue) |
543 : null, | 544 : null, |
544 type: buildType(parameter.element.type)); | 545 type: buildType(elementForFormalParameter(parameter).type)); |
545 switch (parameter.kind) { | 546 switch (parameter.kind) { |
546 case ParameterKind.REQUIRED: | 547 case ParameterKind.REQUIRED: |
547 positional.add(declaration); | 548 positional.add(declaration); |
548 ++requiredParameterCount; | 549 ++requiredParameterCount; |
549 declaration.initializer = null; | 550 declaration.initializer = null; |
550 break; | 551 break; |
551 | 552 |
552 case ParameterKind.POSITIONAL: | 553 case ParameterKind.POSITIONAL: |
553 positional.add(declaration); | 554 positional.add(declaration); |
554 break; | 555 break; |
(...skipping 1773 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2328 } | 2329 } |
2329 | 2330 |
2330 /// True if the given class member should not be emitted, and does not | 2331 /// True if the given class member should not be emitted, and does not |
2331 /// correspond to any Kernel member. | 2332 /// correspond to any Kernel member. |
2332 /// | 2333 /// |
2333 /// This is true for redirecting factories with a resolved target. These are | 2334 /// This is true for redirecting factories with a resolved target. These are |
2334 /// always bypassed at the call site. | 2335 /// always bypassed at the call site. |
2335 bool _isIgnoredMember(ClassMember node) { | 2336 bool _isIgnoredMember(ClassMember node) { |
2336 return node is ConstructorDeclaration && | 2337 return node is ConstructorDeclaration && |
2337 node.factoryKeyword != null && | 2338 node.factoryKeyword != null && |
2338 node.element.redirectedConstructor != null; | 2339 elementForConstructorDeclaration(node).redirectedConstructor != null; |
2339 } | 2340 } |
2340 | 2341 |
2341 visitClassDeclaration(ClassDeclaration node) { | 2342 visitClassDeclaration(ClassDeclaration node) { |
2342 addAnnotations(node.metadata); | 2343 addAnnotations(node.metadata); |
2343 ast.Class classNode = currentClass; | 2344 ast.Class classNode = currentClass; |
2344 assert(classNode.members.isEmpty); // All members will be added here. | 2345 assert(classNode.members.isEmpty); // All members will be added here. |
2345 | 2346 |
2346 bool foundConstructor = false; | 2347 bool foundConstructor = false; |
2347 for (var member in node.members) { | 2348 for (var member in node.members) { |
2348 if (_isIgnoredMember(member)) continue; | 2349 if (_isIgnoredMember(member)) continue; |
(...skipping 254 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2603 } | 2604 } |
2604 } | 2605 } |
2605 bool hasExplicitConstructorCall = false; | 2606 bool hasExplicitConstructorCall = false; |
2606 for (var initializer in node.initializers) { | 2607 for (var initializer in node.initializers) { |
2607 var node = scope.buildInitializer(initializer); | 2608 var node = scope.buildInitializer(initializer); |
2608 constructor.initializers.add(node..parent = constructor); | 2609 constructor.initializers.add(node..parent = constructor); |
2609 if (node is ast.SuperInitializer || node is ast.RedirectingInitializer) { | 2610 if (node is ast.SuperInitializer || node is ast.RedirectingInitializer) { |
2610 hasExplicitConstructorCall = true; | 2611 hasExplicitConstructorCall = true; |
2611 } | 2612 } |
2612 } | 2613 } |
2613 ClassElement classElement = node.element.enclosingElement; | 2614 ClassElement classElement = |
| 2615 elementForConstructorDeclaration(node).enclosingElement; |
2614 if (classElement.supertype != null && !hasExplicitConstructorCall) { | 2616 if (classElement.supertype != null && !hasExplicitConstructorCall) { |
2615 ConstructorElement targetElement = | 2617 ConstructorElement targetElement = |
2616 scope.findDefaultConstructor(classElement.supertype.element); | 2618 scope.findDefaultConstructor(classElement.supertype.element); |
2617 ast.Constructor target = scope.resolveConstructor(targetElement); | 2619 ast.Constructor target = scope.resolveConstructor(targetElement); |
2618 ast.Initializer initializer = target == null | 2620 ast.Initializer initializer = target == null |
2619 ? new ast.InvalidInitializer() | 2621 ? new ast.InvalidInitializer() |
2620 : new ast.SuperInitializer( | 2622 : new ast.SuperInitializer( |
2621 target, new ast.Arguments(<ast.Expression>[])); | 2623 target, new ast.Arguments(<ast.Expression>[])); |
2622 constructor.initializers.add(initializer..parent = constructor); | 2624 constructor.initializers.add(initializer..parent = constructor); |
2623 } else { | 2625 } else { |
2624 moveSuperInitializerLast(constructor); | 2626 moveSuperInitializerLast(constructor); |
2625 } | 2627 } |
2626 } | 2628 } |
2627 | 2629 |
2628 void buildFactoryConstructor(ConstructorDeclaration node) { | 2630 void buildFactoryConstructor(ConstructorDeclaration node) { |
2629 addAnnotations(node.metadata); | 2631 addAnnotations(node.metadata); |
2630 ast.Procedure procedure = currentMember; | 2632 ast.Procedure procedure = currentMember; |
2631 ClassElement classElement = node.element.enclosingElement; | 2633 ClassElement classElement = |
| 2634 elementForConstructorDeclaration(node).enclosingElement; |
2632 ast.Class classNode = procedure.enclosingClass; | 2635 ast.Class classNode = procedure.enclosingClass; |
2633 var types = getFreshTypeParameters(classNode.typeParameters); | 2636 var types = getFreshTypeParameters(classNode.typeParameters); |
2634 for (int i = 0; i < classElement.typeParameters.length; ++i) { | 2637 for (int i = 0; i < classElement.typeParameters.length; ++i) { |
2635 scope.localTypeParameters[classElement.typeParameters[i]] = | 2638 scope.localTypeParameters[classElement.typeParameters[i]] = |
2636 types.freshTypeParameters[i]; | 2639 types.freshTypeParameters[i]; |
2637 } | 2640 } |
2638 var function = scope.buildFunctionNode(node.parameters, node.body, | 2641 var function = scope.buildFunctionNode(node.parameters, node.body, |
2639 typeParameters: types.freshTypeParameters, | 2642 typeParameters: types.freshTypeParameters, |
2640 inferredReturnType: new ast.InterfaceType(classNode, | 2643 inferredReturnType: new ast.InterfaceType(classNode, |
2641 types.freshTypeParameters.map(makeTypeParameterType).toList())); | 2644 types.freshTypeParameters.map(makeTypeParameterType).toList())); |
2642 procedure.function = function..parent = procedure; | 2645 procedure.function = function..parent = procedure; |
2643 handleNativeBody(node.body); | 2646 handleNativeBody(node.body); |
2644 if (node.redirectedConstructor != null) { | 2647 if (node.redirectedConstructor != null) { |
2645 // Redirecting factories with resolved targets don't show up here. | 2648 // Redirecting factories with resolved targets don't show up here. |
2646 assert(node.element.redirectedConstructor == null); | 2649 assert( |
| 2650 elementForConstructorDeclaration(node).redirectedConstructor == null); |
2647 var function = procedure.function; | 2651 var function = procedure.function; |
2648 var name = node.redirectedConstructor.type.name.name; | 2652 var name = node.redirectedConstructor.type.name.name; |
2649 if (node.redirectedConstructor.name != null) { | 2653 if (node.redirectedConstructor.name != null) { |
2650 name += '.' + node.redirectedConstructor.name.name; | 2654 name += '.' + node.redirectedConstructor.name.name; |
2651 } | 2655 } |
2652 // TODO(asgerf): Sometimes a TypeError should be thrown. | 2656 // TODO(asgerf): Sometimes a TypeError should be thrown. |
2653 function.body = new ast.ExpressionStatement( | 2657 function.body = new ast.ExpressionStatement( |
2654 scope.buildThrowNoSuchMethodError( | 2658 scope.buildThrowNoSuchMethodError( |
2655 new ast.NullLiteral(), name, new ast.Arguments.empty())) | 2659 new ast.NullLiteral(), name, new ast.Arguments.empty())) |
2656 ..parent = function; | 2660 ..parent = function; |
2657 } | 2661 } |
2658 } | 2662 } |
2659 | 2663 |
2660 visitMethodDeclaration(MethodDeclaration node) { | 2664 visitMethodDeclaration(MethodDeclaration node) { |
2661 addAnnotations(node.metadata); | 2665 addAnnotations(node.metadata); |
2662 ast.Procedure procedure = currentMember; | 2666 ast.Procedure procedure = currentMember; |
2663 procedure.function = scope.buildFunctionNode(node.parameters, node.body, | 2667 procedure.function = scope.buildFunctionNode(node.parameters, node.body, |
2664 returnType: node.returnType, | 2668 returnType: node.returnType, |
2665 inferredReturnType: scope.buildType(node.element.returnType), | 2669 inferredReturnType: |
| 2670 scope.buildType(elementForMethodDeclaration(node).returnType), |
2666 typeParameters: | 2671 typeParameters: |
2667 scope.buildOptionalTypeParameterList(node.typeParameters)) | 2672 scope.buildOptionalTypeParameterList(node.typeParameters)) |
2668 ..parent = procedure; | 2673 ..parent = procedure; |
2669 handleNativeBody(node.body); | 2674 handleNativeBody(node.body); |
2670 } | 2675 } |
2671 | 2676 |
2672 visitVariableDeclaration(VariableDeclaration node) { | 2677 visitVariableDeclaration(VariableDeclaration node) { |
2673 addAnnotations(node.metadata); | 2678 addAnnotations(node.metadata); |
2674 ast.Field field = currentMember; | 2679 ast.Field field = currentMember; |
2675 field.type = scope.buildType(node.element.type); | 2680 field.type = scope.buildType(elementForVariableDeclaration(node).type); |
2676 if (node.initializer != null) { | 2681 if (node.initializer != null) { |
2677 field.initializer = scope.buildTopLevelExpression(node.initializer) | 2682 field.initializer = scope.buildTopLevelExpression(node.initializer) |
2678 ..parent = field; | 2683 ..parent = field; |
2679 } else if (field.isStatic) { | 2684 } else if (field.isStatic) { |
2680 // Add null initializer to static fields without an initializer. | 2685 // Add null initializer to static fields without an initializer. |
2681 // For instance fields, this is handled when building the class. | 2686 // For instance fields, this is handled when building the class. |
2682 field.initializer = new ast.NullLiteral()..parent = field; | 2687 field.initializer = new ast.NullLiteral()..parent = field; |
2683 } | 2688 } |
2684 } | 2689 } |
2685 | 2690 |
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2808 if (list[i - 1].compareTo(item) == 0) { | 2813 if (list[i - 1].compareTo(item) == 0) { |
2809 ++deleted; | 2814 ++deleted; |
2810 } else if (deleted > 0) { | 2815 } else if (deleted > 0) { |
2811 list[i - deleted] = item; | 2816 list[i - deleted] = item; |
2812 } | 2817 } |
2813 } | 2818 } |
2814 if (deleted > 0) { | 2819 if (deleted > 0) { |
2815 list.length -= deleted; | 2820 list.length -= deleted; |
2816 } | 2821 } |
2817 } | 2822 } |
OLD | NEW |