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

Side by Side Diff: pkg/analyzer2dart/lib/src/cps_generator.dart

Issue 762213002: Support top-level field declaration and assignment in analyzer2dart. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Updated cf. comments. Created 6 years 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 | Annotate | Revision Log
« no previous file with comments | « pkg/analyzer2dart/lib/src/converted_world.dart ('k') | pkg/analyzer2dart/lib/src/modely.dart » ('j') | 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) 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;
11 import 'package:analyzer/src/generated/source.dart'; 11 import 'package:analyzer/src/generated/source.dart';
12 import 'package:analyzer/src/generated/element.dart' as analyzer; 12 import 'package:analyzer/src/generated/element.dart' as analyzer;
13 13
14 import 'package:compiler/src/dart2jslib.dart' 14 import 'package:compiler/src/dart2jslib.dart'
15 show DART_CONSTANT_SYSTEM; 15 show DART_CONSTANT_SYSTEM;
16 import 'package:compiler/src/cps_ir/cps_ir_nodes.dart' as ir; 16 import 'package:compiler/src/cps_ir/cps_ir_nodes.dart' as ir;
17 import 'package:compiler/src/cps_ir/cps_ir_builder.dart'; 17 import 'package:compiler/src/cps_ir/cps_ir_builder.dart';
18 import 'package:compiler/src/universe/universe.dart'; 18 import 'package:compiler/src/universe/universe.dart';
19 19
20 import 'semantic_visitor.dart'; 20 import 'semantic_visitor.dart';
21 import 'element_converter.dart'; 21 import 'element_converter.dart';
22 import 'util.dart'; 22 import 'util.dart';
23 import 'identifier_semantics.dart'; 23 import 'identifier_semantics.dart';
24 24
25 /// Visitor that converts the AST node of an analyzer element into a CPS ir
26 /// node.
27 class CpsElementVisitor extends analyzer.SimpleElementVisitor<ir.Node> {
28 final ElementConverter converter;
29 final AstNode node;
30
31 CpsElementVisitor(this.converter, this.node);
32
33 @override
34 ir.FunctionDefinition visitFunctionElement(analyzer.FunctionElement element) {
35 CpsGeneratingVisitor visitor = new CpsGeneratingVisitor(converter, element);
36 FunctionDeclaration functionDeclaration = node;
37 return visitor.handleFunctionDeclaration(
38 element, functionDeclaration.functionExpression);
39 }
40
41 @override
42 ir.FieldDefinition visitTopLevelVariableElement(
43 analyzer.TopLevelVariableElement element) {
44 CpsGeneratingVisitor visitor = new CpsGeneratingVisitor(converter, element);
45 VariableDeclaration variableDeclaration = node;
46 return visitor.handleFieldDeclaration(element, variableDeclaration);
47 }
48 }
49
50 /// Visitor that converts analyzer AST nodes into CPS ir nodes.
25 class CpsGeneratingVisitor extends SemanticVisitor<ir.Node> 51 class CpsGeneratingVisitor extends SemanticVisitor<ir.Node>
26 with IrBuilderMixin<AstNode> { 52 with IrBuilderMixin<AstNode> {
27 final analyzer.Element element; 53 final analyzer.Element element;
28 final ElementConverter converter; 54 final ElementConverter converter;
29 55
30 CpsGeneratingVisitor(this.converter, this.element); 56 CpsGeneratingVisitor(this.converter, this.element);
31 57
32 Source get currentSource => element.source; 58 Source get currentSource => element.source;
33 59
34 analyzer.LibraryElement get currentLibrary => element.library; 60 analyzer.LibraryElement get currentLibrary => element.library;
35 61
36 ir.Node visit(AstNode node) => node.accept(this); 62 ir.Node visit(AstNode node) => node.accept(this);
37 63
38 @override 64 ir.FieldDefinition handleFieldDeclaration(
39 ir.Primitive visitFunctionExpression(FunctionExpression node) { 65 analyzer.PropertyInducingElement field, VariableDeclaration node) {
40 return irBuilder.buildFunctionExpression( 66 dart2js.FieldElement element = converter.convertElement(field);
41 handleFunctionDeclaration(node.element, node)); 67 return withBuilder(
68 new IrBuilder(DART_CONSTANT_SYSTEM,
69 element,
70 // TODO(johnniwinther): Supported closure variables.
71 const <dart2js.Local>[]),
72 () {
73 ir.Primitive initializer = build(node.initializer);
74 return irBuilder.makeFieldDefinition(initializer);
75 });
42 } 76 }
43 77
44 ir.FunctionDefinition handleFunctionDeclaration( 78 ir.FunctionDefinition handleFunctionDeclaration(
45 analyzer.FunctionElement function, FunctionExpression node) { 79 analyzer.FunctionElement function, FunctionExpression node) {
46 dart2js.FunctionElement element = converter.convertElement(function); 80 dart2js.FunctionElement element = converter.convertElement(function);
47 return withBuilder( 81 return withBuilder(
48 new IrBuilder(DART_CONSTANT_SYSTEM, 82 new IrBuilder(DART_CONSTANT_SYSTEM,
49 element, 83 element,
50 // TODO(johnniwinther): Supported closure variables. 84 // TODO(johnniwinther): Supported closure variables.
51 const <dart2js.Local>[]), 85 const <dart2js.Local>[]),
52 () { 86 () {
53 function.parameters.forEach((analyzer.ParameterElement parameter) { 87 function.parameters.forEach((analyzer.ParameterElement parameter) {
54 // TODO(johnniwinther): Support "closure variables", that is variables 88 // TODO(johnniwinther): Support "closure variables", that is variables
55 // accessed from an inner function. 89 // accessed from an inner function.
56 irBuilder.createParameter(converter.convertElement(parameter)); 90 irBuilder.createParameter(converter.convertElement(parameter));
57 }); 91 });
58 // Visit the body directly to avoid processing the signature as 92 // Visit the body directly to avoid processing the signature as
59 // expressions. 93 // expressions.
60 visit(node.body); 94 visit(node.body);
61 return irBuilder.buildFunctionDefinition(const []); 95 return irBuilder.makeFunctionDefinition(const []);
62 }); 96 });
63 } 97 }
64 98
65 @override 99 @override
100 ir.Primitive visitFunctionExpression(FunctionExpression node) {
101 return irBuilder.buildFunctionExpression(
102 handleFunctionDeclaration(node.element, node));
103 }
104
105 @override
66 ir.FunctionDefinition visitFunctionDeclaration(FunctionDeclaration node) { 106 ir.FunctionDefinition visitFunctionDeclaration(FunctionDeclaration node) {
67 return handleFunctionDeclaration(node.element, node.functionExpression); 107 return handleFunctionDeclaration(node.element, node.functionExpression);
68 } 108 }
69 109
70 @override 110 @override
71 visitFunctionDeclarationStatement(FunctionDeclarationStatement node) { 111 visitFunctionDeclarationStatement(FunctionDeclarationStatement node) {
72 FunctionDeclaration functionDeclaration = node.functionDeclaration; 112 FunctionDeclaration functionDeclaration = node.functionDeclaration;
73 analyzer.FunctionElement function = functionDeclaration.element; 113 analyzer.FunctionElement function = functionDeclaration.element;
74 dart2js.FunctionElement element = converter.convertElement(function); 114 dart2js.FunctionElement element = converter.convertElement(function);
75 ir.FunctionDefinition definition = handleFunctionDeclaration( 115 ir.FunctionDefinition definition = handleFunctionDeclaration(
(...skipping 209 matching lines...) Expand 10 before | Expand all | Expand 10 after
285 return handleLocalAssignment(node, semantics); 325 return handleLocalAssignment(node, semantics);
286 } 326 }
287 327
288 @override 328 @override
289 ir.Node visitParameterAssignment(AssignmentExpression node, 329 ir.Node visitParameterAssignment(AssignmentExpression node,
290 AccessSemantics semantics) { 330 AccessSemantics semantics) {
291 return handleLocalAssignment(node, semantics); 331 return handleLocalAssignment(node, semantics);
292 } 332 }
293 333
294 @override 334 @override
335 ir.Node visitStaticFieldAssignment(AssignmentExpression node,
336 AccessSemantics semantics) {
337 if (node.operator.lexeme != '=') {
338 return giveUp(node, 'Assignment operator: ${node.operator.lexeme}');
339 }
340 analyzer.Element element = semantics.element;
341 dart2js.Element target = converter.convertElement(element);
342 // TODO(johnniwinther): Selector information should be computed in the
343 // [TreeShaker] and shared with the [CpsGeneratingVisitor].
344 assert(invariant(node, target.isTopLevel || target.isStatic,
345 '$target expected to be top-level or static.'));
346 return irBuilder.buildStaticSet(
347 target,
348 new Selector.setter(target.name, target.library),
349 build(node.rightHandSide));
350 }
351
352 @override
295 ir.Node visitDynamicAccess(AstNode node, AccessSemantics semantics) { 353 ir.Node visitDynamicAccess(AstNode node, AccessSemantics semantics) {
296 // TODO(johnniwinther): Handle implicit `this`. 354 // TODO(johnniwinther): Handle implicit `this`.
297 ir.Primitive receiver = build(semantics.target); 355 ir.Primitive receiver = build(semantics.target);
298 return irBuilder.buildDynamicGet(receiver, 356 return irBuilder.buildDynamicGet(receiver,
299 new Selector.getter(semantics.identifier.name, 357 new Selector.getter(semantics.identifier.name,
300 converter.convertElement(element.library))); 358 converter.convertElement(element.library)));
301 } 359 }
302 360
303 @override 361 @override
304 ir.Node visitStaticFieldAccess(AstNode node, AccessSemantics semantics) { 362 ir.Node visitStaticFieldAccess(AstNode node, AccessSemantics semantics) {
(...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after
456 } 514 }
457 515
458 @override 516 @override
459 ir.Primitive visitAsExpression(AsExpression node) { 517 ir.Primitive visitAsExpression(AsExpression node) {
460 return irBuilder.buildTypeOperator( 518 return irBuilder.buildTypeOperator(
461 visit(node.expression), 519 visit(node.expression),
462 converter.convertType(node.type.type), 520 converter.convertType(node.type.type),
463 isTypeTest: false); 521 isTypeTest: false);
464 } 522 }
465 } 523 }
OLDNEW
« no previous file with comments | « pkg/analyzer2dart/lib/src/converted_world.dart ('k') | pkg/analyzer2dart/lib/src/modely.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698