001 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file
002 // for details. All rights reserved. Use of this source code is governed by a
003 // BSD-style license that can be found in the LICENSE file.
004
005 package com.google.dart.compiler.backend.js.ast;
006
007 /**
008 * A <code>for</code> statement. If specified at all, the initializer part is
009 * either a declaration of one or more variables, in which case
010 * {@link #getInitVars()} is used, or an expression, in which case
011 * {@link #getInitExpression()} is used. In the latter case, the comma operator is
012 * often used to create a compound expression.
013 * <p/>
014 * <p/>
015 * Note that any of the parts of the <code>for</code> loop header can be
016 * <code>null</code>, although the body will never be null.
017 */
018 public class JsFor extends SourceInfoAwareJsNode implements JsStatement {
019 private JsStatement body;
020 private JsExpression condition;
021 private JsExpression incrementExpression;
022 private JsExpression initExpression;
023 private JsVars initVars;
024
025 public JsFor(JsVars initVars, JsExpression condition, JsExpression incrementExpression) {
026 this(initVars, condition, incrementExpression, null);
027 }
028
029 public JsFor(JsVars initVars, JsExpression condition, JsExpression incrementExpression, JsStatement body) {
030 this.initVars = initVars;
031 this.incrementExpression = incrementExpression;
032 this.condition = condition;
033 this.body = body;
034 initExpression = null;
035 }
036
037 public JsFor(JsExpression initExpression, JsExpression condition, JsExpression incrementExpression) {
038 this(initExpression, condition, incrementExpression, null);
039 }
040
041 public JsFor(JsExpression initExpression, JsExpression condition, JsExpression incrementExpression, JsStatement body) {
042 this.initExpression = initExpression;
043 this.incrementExpression = incrementExpression;
044 this.condition = condition;
045 this.body = body;
046 initVars = null;
047 }
048
049 public JsStatement getBody() {
050 return body;
051 }
052
053 public JsExpression getCondition() {
054 return condition;
055 }
056
057 public JsExpression getIncrementExpression() {
058 return incrementExpression;
059 }
060
061 public JsExpression getInitExpression() {
062 return initExpression;
063 }
064
065 public JsVars getInitVars() {
066 return initVars;
067 }
068
069 public void setBody(JsStatement body) {
070 this.body = body;
071 }
072
073 @Override
074 public void accept(JsVisitor v) {
075 v.visitFor(this);
076 }
077
078 @Override
079 public void acceptChildren(JsVisitor visitor) {
080 assert (!(initExpression != null && initVars != null));
081
082 if (initExpression != null) {
083 visitor.accept(initExpression);
084 }
085 else if (initVars != null) {
086 visitor.accept(initVars);
087 }
088
089 if (condition != null) {
090 visitor.accept(condition);
091 }
092
093 if (incrementExpression != null) {
094 visitor.accept(incrementExpression);
095 }
096 visitor.accept(body);
097 }
098 }