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

Side by Side Diff: sdk/lib/_internal/compiler/implementation/cps_ir/cps_ir_builder.dart

Issue 666863002: Support conditional expressions in analyzer2dart. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Visit conditionally. Created 6 years, 2 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 | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2013, 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 dart2js.ir_builder; 5 library dart2js.ir_builder;
6 6
7 import '../constants/expressions.dart'; 7 import '../constants/expressions.dart';
8 import '../constants/values.dart' show PrimitiveConstantValue; 8 import '../constants/values.dart' show PrimitiveConstantValue;
9 import '../dart_backend/dart_backend.dart' show DartBackend; 9 import '../dart_backend/dart_backend.dart' show DartBackend;
10 import '../dart_types.dart'; 10 import '../dart_types.dart';
(...skipping 337 matching lines...) Expand 10 before | Expand all | Expand 10 after
348 ir.Constant buildNullLiteral() { 348 ir.Constant buildNullLiteral() {
349 return _buildPrimitiveConstant(state.constantSystem.createNull()); 349 return _buildPrimitiveConstant(state.constantSystem.createNull());
350 } 350 }
351 351
352 /// Create a string literal. 352 /// Create a string literal.
353 ir.Constant buildStringLiteral(String value) { 353 ir.Constant buildStringLiteral(String value) {
354 return _buildPrimitiveConstant( 354 return _buildPrimitiveConstant(
355 state.constantSystem.createString(new ast.DartString.literal(value))); 355 state.constantSystem.createString(new ast.DartString.literal(value)));
356 } 356 }
357 357
358 /// Creates a conditional expression with the provided [condition] where the
359 /// then and else expression are created through the [buildThenExpression] and
360 /// [buildElseExpression] functions, respectively.
361 ir.Primitive buildConditional(
362 ir.Primitive condition,
363 ir.Primitive buildThenExpression(IrBuilder builder),
364 ir.Primitive buildElseExpression(IrBuilder builder)) {
365
366 assert(isOpen);
367
368 // The then and else expressions are delimited.
369 IrBuilder thenBuilder = new IrBuilder.delimited(this);
370 IrBuilder elseBuilder = new IrBuilder.delimited(this);
371 ir.Primitive thenValue = buildThenExpression(thenBuilder);
372 ir.Primitive elseValue = buildElseExpression(elseBuilder);
373
374 // Treat the values of the subexpressions as named values in the
375 // environment, so they will be treated as arguments to the join-point
376 // continuation.
377 assert(environment.length == thenBuilder.environment.length);
378 assert(environment.length == elseBuilder.environment.length);
379 thenBuilder.environment.extend(null, thenValue);
380 elseBuilder.environment.extend(null, elseValue);
381 JumpCollector jumps = new JumpCollector(null);
382 jumps.addJump(thenBuilder);
383 jumps.addJump(elseBuilder);
384 ir.Continuation joinContinuation =
385 createJoin(environment.length + 1, jumps);
386
387 // Build the term
388 // let cont join(x, ..., result) = [] in
389 // let cont then() = [[thenPart]]; join(v, ...) in
390 // let cont else() = [[elsePart]]; join(v, ...) in
391 // if condition (then, else)
392 ir.Continuation thenContinuation = new ir.Continuation([]);
393 ir.Continuation elseContinuation = new ir.Continuation([]);
394 thenContinuation.body = thenBuilder._root;
395 elseContinuation.body = elseBuilder._root;
396 add(new ir.LetCont(joinContinuation,
397 new ir.LetCont(thenContinuation,
398 new ir.LetCont(elseContinuation,
399 new ir.Branch(new ir.IsTrue(condition),
400 thenContinuation,
401 elseContinuation)))));
402 return (thenValue == elseValue)
403 ? thenValue
404 : joinContinuation.parameters.last;
405
406 }
407
358 /// Create a get access of [local]. 408 /// Create a get access of [local].
359 ir.Primitive buildLocalGet(Element local) { 409 ir.Primitive buildLocalGet(Element local) {
360 assert(isOpen); 410 assert(isOpen);
361 return environment.lookup(local); 411 return environment.lookup(local);
362 } 412 }
363 413
364 /// Create a get access of the static [element]. 414 /// Create a get access of the static [element].
365 ir.Primitive buildStaticGet(Element element, Selector selector) { 415 ir.Primitive buildStaticGet(Element element, Selector selector) {
366 assert(isOpen); 416 assert(isOpen);
367 assert(selector.isGetter); 417 assert(selector.isGetter);
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
440 /// Create a static invocation of [element] with arguments structure defined 490 /// Create a static invocation of [element] with arguments structure defined
441 /// by [selector] and argument values defined by [arguments]. 491 /// by [selector] and argument values defined by [arguments].
442 ir.Primitive buildStaticInvocation(Element element, 492 ir.Primitive buildStaticInvocation(Element element,
443 Selector selector, 493 Selector selector,
444 List<ir.Definition> arguments) { 494 List<ir.Definition> arguments) {
445 return continueWithExpression( 495 return continueWithExpression(
446 (k) => new ir.InvokeStatic(element, selector, k, arguments)); 496 (k) => new ir.InvokeStatic(element, selector, k, arguments));
447 } 497 }
448 498
449 /// Creates an if-then-else statement with the provided [condition] where the 499 /// Creates an if-then-else statement with the provided [condition] where the
450 /// then and else branches are created throught the [buildThenPart] and 500 /// then and else branches are created through the [buildThenPart] and
451 /// [buildElsePart] functions, respectively. 501 /// [buildElsePart] functions, respectively.
452 /// 502 ///
453 /// An if-then statement is created if [buildElsePart] is a no-op. 503 /// An if-then statement is created if [buildElsePart] is a no-op.
454 void buildIf(ir.Primitive condition, 504 void buildIf(ir.Primitive condition,
455 void buildThenPart(IrBuilder builder), 505 void buildThenPart(IrBuilder builder),
456 void buildElsePart(IrBuilder builder)) { 506 void buildElsePart(IrBuilder builder)) {
457 assert(isOpen); 507 assert(isOpen);
458 508
459 // The then and else parts are delimited. 509 // The then and else parts are delimited.
460 IrBuilder thenBuilder = new IrBuilder.delimited(this); 510 IrBuilder thenBuilder = new IrBuilder.delimited(this);
(...skipping 313 matching lines...) Expand 10 before | Expand all | Expand 10 after
774 index = 0; 824 index = 0;
775 for (int i = 0; i < environment.length; ++i) { 825 for (int i = 0; i < environment.length; ++i) {
776 if (common[i] == null) { 826 if (common[i] == null) {
777 environment.index2value[i] = parameters[index++]; 827 environment.index2value[i] = parameters[index++];
778 } 828 }
779 } 829 }
780 830
781 return join; 831 return join;
782 } 832 }
783 } 833 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698