Chromium Code Reviews| 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 analyzer2dart.cps_generator; | 5 library analyzer2dart.cps_generator; |
| 6 | 6 |
| 7 import 'package:analyzer/analyzer.dart'; | 7 import 'package:analyzer/analyzer.dart'; |
| 8 | 8 |
| 9 import 'package:compiler/src/dart_types.dart' as dart2js; | 9 import 'package:compiler/src/dart_types.dart' as dart2js; |
| 10 import 'package:compiler/src/elements/elements.dart' as dart2js; | 10 import 'package:compiler/src/elements/elements.dart' as dart2js; |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 28 final ElementConverter converter; | 28 final ElementConverter converter; |
| 29 final AstNode node; | 29 final AstNode node; |
| 30 | 30 |
| 31 CpsElementVisitor(this.converter, this.node); | 31 CpsElementVisitor(this.converter, this.node); |
| 32 | 32 |
| 33 @override | 33 @override |
| 34 ir.FunctionDefinition visitFunctionElement(analyzer.FunctionElement element) { | 34 ir.FunctionDefinition visitFunctionElement(analyzer.FunctionElement element) { |
| 35 CpsGeneratingVisitor visitor = new CpsGeneratingVisitor(converter, element); | 35 CpsGeneratingVisitor visitor = new CpsGeneratingVisitor(converter, element); |
| 36 FunctionDeclaration functionDeclaration = node; | 36 FunctionDeclaration functionDeclaration = node; |
| 37 return visitor.handleFunctionDeclaration( | 37 return visitor.handleFunctionDeclaration( |
| 38 element, functionDeclaration.functionExpression); | 38 element, functionDeclaration.functionExpression.body); |
| 39 } | 39 } |
| 40 | 40 |
| 41 @override | 41 @override |
| 42 ir.FunctionDefinition visitMethodElement(analyzer.MethodElement element) { | |
| 43 CpsGeneratingVisitor visitor = new CpsGeneratingVisitor(converter, element); | |
| 44 MethodDeclaration methodDeclaration = node; | |
| 45 return visitor.handleFunctionDeclaration(element, methodDeclaration.body); | |
| 46 } | |
| 47 | |
| 48 @override | |
| 42 ir.FieldDefinition visitTopLevelVariableElement( | 49 ir.FieldDefinition visitTopLevelVariableElement( |
| 43 analyzer.TopLevelVariableElement element) { | 50 analyzer.TopLevelVariableElement element) { |
| 44 CpsGeneratingVisitor visitor = new CpsGeneratingVisitor(converter, element); | 51 CpsGeneratingVisitor visitor = new CpsGeneratingVisitor(converter, element); |
| 45 VariableDeclaration variableDeclaration = node; | 52 VariableDeclaration variableDeclaration = node; |
| 46 return visitor.handleFieldDeclaration(element, variableDeclaration); | 53 return visitor.handleFieldDeclaration(element, variableDeclaration); |
| 47 } | 54 } |
| 48 | 55 |
| 49 @override | 56 @override |
| 50 ir.ExecutableDefinition visitConstructorElement( | 57 ir.ExecutableDefinition visitConstructorElement( |
| 51 analyzer.ConstructorElement element) { | 58 analyzer.ConstructorElement element) { |
| 52 CpsGeneratingVisitor visitor = new CpsGeneratingVisitor(converter, element); | 59 CpsGeneratingVisitor visitor = new CpsGeneratingVisitor(converter, element); |
| 53 if (!element.isFactory) { | 60 if (!element.isFactory) { |
| 54 ConstructorDeclaration constructorDeclaration = node; | 61 ConstructorDeclaration constructorDeclaration = node; |
| 55 return visitor.handleConstructorDeclaration( | 62 FunctionBody body; |
| 56 element, constructorDeclaration); | 63 if (constructorDeclaration != null) { |
| 64 body = constructorDeclaration.body; | |
| 65 } else { | |
| 66 assert(element.isSynthetic); | |
| 67 } | |
| 68 return visitor.handleConstructorDeclaration(element, body); | |
| 57 } | 69 } |
| 58 // TODO(johnniwinther): Support factory constructors. | 70 // TODO(johnniwinther): Support factory constructors. |
| 59 return null; | 71 return null; |
| 60 } | 72 } |
| 61 } | 73 } |
| 62 | 74 |
| 63 /// Visitor that converts analyzer AST nodes into CPS ir nodes. | 75 /// Visitor that converts analyzer AST nodes into CPS ir nodes. |
| 64 class CpsGeneratingVisitor extends SemanticVisitor<ir.Node> | 76 class CpsGeneratingVisitor extends SemanticVisitor<ir.Node> |
| 65 with IrBuilderMixin<AstNode> { | 77 with IrBuilderMixin<AstNode> { |
| 66 /// Promote the type of [irBuilder] to [DartIrBuilder]. | 78 /// Promote the type of [irBuilder] to [DartIrBuilder]. |
| 67 /// The JS backend requires closure conversion which we do not support yet. | 79 /// The JS backend requires closure conversion which we do not support yet. |
| 68 DartIrBuilder get irBuilder => super.irBuilder; | 80 DartIrBuilder get irBuilder => super.irBuilder; |
| 69 final analyzer.Element element; | 81 final analyzer.Element element; |
| 70 final ElementConverter converter; | 82 final ElementConverter converter; |
| 71 | 83 |
| 72 CpsGeneratingVisitor(this.converter, this.element); | 84 CpsGeneratingVisitor(this.converter, this.element); |
| 73 | 85 |
| 74 Source get currentSource => element.source; | 86 Source get currentSource => element.source; |
| 75 | 87 |
| 76 analyzer.LibraryElement get currentLibrary => element.library; | 88 analyzer.LibraryElement get currentLibrary => element.library; |
| 77 | 89 |
| 78 ir.Node visit(AstNode node) => node.accept(this); | 90 ir.Node visit(AstNode node) => node.accept(this); |
| 79 | 91 |
| 80 ir.ConstructorDefinition handleConstructorDeclaration( | 92 ir.ConstructorDefinition handleConstructorDeclaration( |
| 81 analyzer.ConstructorElement constructor, ConstructorDeclaration node) { | 93 analyzer.ConstructorElement constructor, FunctionBody body) { |
| 82 FunctionBody body = node.body; | |
| 83 dart2js.ConstructorElement element = converter.convertElement(constructor); | 94 dart2js.ConstructorElement element = converter.convertElement(constructor); |
| 84 return withBuilder( | 95 return withBuilder( |
| 85 new DartIrBuilder(DART_CONSTANT_SYSTEM, | 96 new DartIrBuilder(DART_CONSTANT_SYSTEM, |
| 86 element, | 97 element, |
| 87 // TODO(johnniwinther): Supported closure variables. | 98 // TODO(johnniwinther): Supported closure variables. |
| 88 new NullCapturedVariableInfo()), | 99 new NullCapturedVariableInfo()), |
| 89 () { | 100 () { |
| 90 irBuilder.buildFunctionHeader( | 101 irBuilder.buildFunctionHeader( |
| 91 constructor.parameters.map(converter.convertElement)); | 102 constructor.parameters.map(converter.convertElement)); |
| 92 // Visit the body directly to avoid processing the signature as | 103 // Visit the body directly to avoid processing the signature as |
| 93 // expressions. | 104 // expressions. |
| 94 visit(node.body); | 105 // Call to allow for `body == null` in case of synthesized constructors. |
|
sigurdm
2015/02/06 15:04:46
Call `build` ...
| |
| 106 build(body); | |
| 95 return irBuilder.makeConstructorDefinition(const [], const []); | 107 return irBuilder.makeConstructorDefinition(const [], const []); |
| 96 }); | 108 }); |
| 97 } | 109 } |
| 98 | 110 |
| 99 ir.FieldDefinition handleFieldDeclaration( | 111 ir.FieldDefinition handleFieldDeclaration( |
| 100 analyzer.PropertyInducingElement field, VariableDeclaration node) { | 112 analyzer.PropertyInducingElement field, VariableDeclaration node) { |
| 101 dart2js.FieldElement element = converter.convertElement(field); | 113 dart2js.FieldElement element = converter.convertElement(field); |
| 102 return withBuilder( | 114 return withBuilder( |
| 103 new DartIrBuilder(DART_CONSTANT_SYSTEM, | 115 new DartIrBuilder(DART_CONSTANT_SYSTEM, |
| 104 element, | 116 element, |
| 105 // TODO(johnniwinther): Supported closure variables. | 117 // TODO(johnniwinther): Supported closure variables. |
| 106 new NullCapturedVariableInfo()), | 118 new NullCapturedVariableInfo()), |
| 107 () { | 119 () { |
| 108 irBuilder.buildFieldInitializerHeader(); | 120 irBuilder.buildFieldInitializerHeader(); |
| 109 ir.Primitive initializer = build(node.initializer); | 121 ir.Primitive initializer = build(node.initializer); |
| 110 return irBuilder.makeFieldDefinition(initializer); | 122 return irBuilder.makeFieldDefinition(initializer); |
| 111 }); | 123 }); |
| 112 } | 124 } |
| 113 | 125 |
| 114 ir.FunctionDefinition handleFunctionDeclaration( | 126 ir.FunctionDefinition handleFunctionDeclaration( |
| 115 analyzer.FunctionElement function, FunctionExpression node) { | 127 analyzer.ExecutableElement function, FunctionBody body) { |
| 116 dart2js.FunctionElement element = converter.convertElement(function); | 128 dart2js.FunctionElement element = converter.convertElement(function); |
| 117 return withBuilder( | 129 return withBuilder( |
| 118 new DartIrBuilder(DART_CONSTANT_SYSTEM, | 130 new DartIrBuilder(DART_CONSTANT_SYSTEM, |
| 119 element, | 131 element, |
| 120 // TODO(johnniwinther): Supported closure variables. | 132 // TODO(johnniwinther): Supported closure variables. |
| 121 new NullCapturedVariableInfo()), | 133 new NullCapturedVariableInfo()), |
| 122 () { | 134 () { |
| 123 irBuilder.buildFunctionHeader( | 135 irBuilder.buildFunctionHeader( |
| 124 function.parameters.map(converter.convertElement)); | 136 function.parameters.map(converter.convertElement)); |
| 125 // Visit the body directly to avoid processing the signature as | 137 // Visit the body directly to avoid processing the signature as |
| 126 // expressions. | 138 // expressions. |
| 127 visit(node.body); | 139 visit(body); |
| 128 return irBuilder.makeFunctionDefinition(const []); | 140 return irBuilder.makeFunctionDefinition(const []); |
| 129 }); | 141 }); |
| 130 } | 142 } |
| 131 | 143 |
| 132 @override | 144 @override |
| 133 ir.Primitive visitFunctionExpression(FunctionExpression node) { | 145 ir.Primitive visitFunctionExpression(FunctionExpression node) { |
| 134 return irBuilder.buildFunctionExpression( | 146 return irBuilder.buildFunctionExpression( |
| 135 handleFunctionDeclaration(node.element, node)); | 147 handleFunctionDeclaration(node.element, node.body)); |
| 136 } | 148 } |
| 137 | 149 |
| 138 @override | 150 @override |
| 139 ir.FunctionDefinition visitFunctionDeclaration(FunctionDeclaration node) { | 151 ir.FunctionDefinition visitFunctionDeclaration(FunctionDeclaration node) { |
| 140 return handleFunctionDeclaration(node.element, node.functionExpression); | 152 return handleFunctionDeclaration( |
| 153 node.element, node.functionExpression.body); | |
| 141 } | 154 } |
| 142 | 155 |
| 143 @override | 156 @override |
| 144 visitFunctionDeclarationStatement(FunctionDeclarationStatement node) { | 157 visitFunctionDeclarationStatement(FunctionDeclarationStatement node) { |
| 145 FunctionDeclaration functionDeclaration = node.functionDeclaration; | 158 FunctionDeclaration functionDeclaration = node.functionDeclaration; |
| 146 analyzer.FunctionElement function = functionDeclaration.element; | 159 analyzer.FunctionElement function = functionDeclaration.element; |
| 147 dart2js.FunctionElement element = converter.convertElement(function); | 160 dart2js.FunctionElement element = converter.convertElement(function); |
| 148 ir.FunctionDefinition definition = handleFunctionDeclaration( | 161 ir.FunctionDefinition definition = handleFunctionDeclaration( |
| 149 function, functionDeclaration.functionExpression); | 162 function, functionDeclaration.functionExpression.body); |
| 150 irBuilder.declareLocalFunction(element, definition); | 163 irBuilder.declareLocalFunction(element, definition); |
| 151 } | 164 } |
| 152 | 165 |
| 153 List<ir.Primitive> visitArguments(ArgumentList argumentList) { | 166 List<ir.Primitive> visitArguments(ArgumentList argumentList) { |
| 154 List<ir.Primitive> arguments = <ir.Primitive>[]; | 167 List<ir.Primitive> arguments = <ir.Primitive>[]; |
| 155 for (Expression argument in argumentList.arguments) { | 168 for (Expression argument in argumentList.arguments) { |
| 156 ir.Primitive value = build(argument); | 169 ir.Primitive value = build(argument); |
| 157 if (value == null) { | 170 if (value == null) { |
| 158 giveUp(argument, | 171 giveUp(argument, |
| 159 'Unsupported argument: $argument (${argument.runtimeType}).'); | 172 'Unsupported argument: $argument (${argument.runtimeType}).'); |
| (...skipping 396 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 556 return irBuilder.buildTypeOperator( | 569 return irBuilder.buildTypeOperator( |
| 557 visit(node.expression), | 570 visit(node.expression), |
| 558 converter.convertType(node.type.type), | 571 converter.convertType(node.type.type), |
| 559 isTypeTest: false); | 572 isTypeTest: false); |
| 560 } | 573 } |
| 561 } | 574 } |
| 562 | 575 |
| 563 class NullCapturedVariableInfo extends DartCapturedVariableInfo { | 576 class NullCapturedVariableInfo extends DartCapturedVariableInfo { |
| 564 Iterable get capturedVariables => const []; | 577 Iterable get capturedVariables => const []; |
| 565 } | 578 } |
| OLD | NEW |